(pututline_r): Since we assign RESULT from lseek now, check that it's >= 0, not...
[glibc.git] / nss / nsswitch.c
blobc92f33b45fd0dc1ed35d10da4015c2301f86d239
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <netdb.h>
23 #include <libc-lock.h>
24 #include <search.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "nsswitch.h"
30 #include "../elf/link.h" /* We need some help from ld.so. */
32 /* Prototypes for the local functions. */
33 static void nss_init (void);
34 static void *nss_lookup_function (service_user *ni, const char *fct_name);
35 static name_database *nss_parse_file (const char *fname);
36 static name_database_entry *nss_getline (char *line);
37 static service_user *nss_parse_service_list (const char *line);
38 static service_library *nss_new_service (name_database *database,
39 const char *name);
42 __libc_lock_define_initialized (static, lock);
45 /* Global variable. */
46 struct __res_state _res;
49 /* Nonzero if the sevices are already initialized. */
50 static int nss_initialized;
53 /* The root of the whole data base. */
54 static name_database *service_table;
57 static void
58 nss_init (void)
60 /* Prevent multiple threads to change the service table. */
61 __libc_lock_lock (lock);
63 if (service_table == NULL)
64 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
66 __libc_lock_unlock (lock);
70 /* -1 == database not found
71 0 == database entry pointer stored */
72 int
73 __nss_database_lookup (const char *database, const char *defconfig,
74 service_user **ni)
76 name_database_entry *entry;
78 if (nss_initialized == 0)
79 nss_init ();
81 /* Test whether configuration data is available. */
82 if (service_table)
84 /* Return first `service_user' entry for DATABASE.
85 XXX Will use perfect hashing function for known databases. */
87 /* XXX Could use some faster mechanism here. But each database is
88 only requested once and so this might not be critical. */
89 for (entry = service_table->entry; entry != NULL; entry = entry->next)
90 if (strcmp (database, entry->name) == 0)
92 *ni = entry->service;
93 return 0;
97 /* No configuration data is available, either because nsswitch.conf
98 doesn't exist or because it doesn't have a line for this database. */
99 entry = malloc (sizeof *entry);
100 if (entry == NULL)
101 return -1;
102 entry->name = database;
103 /* DEFCONFIG specifies the default service list for this database,
104 or null to use the most common default. */
105 entry->service = nss_parse_service_list (defconfig ?:
106 "compat [NOTFOUND=return] files");
108 *ni = entry->service;
109 return 0;
113 /* -1 == not found
114 0 == adjusted for next function */
116 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
118 *fctp = nss_lookup_function (*ni, fct_name);
120 while (*fctp == NULL
121 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
122 && (*ni)->next != NULL)
124 *ni = (*ni)->next;
126 *fctp = nss_lookup_function (*ni, fct_name);
129 return *fctp != NULL ? 0 : -1;
133 /* -1 == not found
134 0 == adjusted for next function
135 1 == finished */
137 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
138 int all_values)
140 if (all_values)
142 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
143 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
144 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
145 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
146 return 1;
148 else
150 /* This is really only for debugging. */
151 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
152 __libc_fatal ("illegal status in " __FUNCTION__);
154 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
155 return 1;
158 if ((*ni)->next == NULL)
159 return -1;
163 *ni = (*ni)->next;
165 *fctp = nss_lookup_function (*ni, fct_name);
167 while (*fctp == NULL
168 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
169 && (*ni)->next != NULL);
171 return *fctp != NULL ? 0 : -1;
175 static int
176 nss_dlerror_run (void (*operate) (void))
178 const char *last_errstring = NULL;
179 const char *last_object_name = NULL;
181 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
183 return last_errstring != NULL;
187 static void *
188 nss_lookup_function (service_user *ni, const char *fct_name)
190 /* Comparison function for searching NI->known tree. */
191 int known_compare (const void *p1, const void *p2)
193 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
194 *(const char *const *) p2);
196 void **found, *result;
198 /* We now modify global data. Protect it. */
199 __libc_lock_lock (lock);
201 /* Search the tree of functions previously requested. Data in the
202 tree are `known_function' structures, whose first member is a
203 `const char *', the lookup key. The search returns a pointer to
204 the tree node structure; the first member of the is a pointer to
205 our structure (i.e. what will be a `known_function'); since the
206 first member of that is the lookup key string, &FCT_NAME is close
207 enough to a pointer to our structure to use as a lookup key that
208 will be passed to `known_compare' (above). */
210 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
211 if (*found != &fct_name)
212 /* The search found an existing structure in the tree. */
213 result = ((known_function *) *found)->fct_ptr;
214 else
216 /* This name was not known before. Now we have a node in the tree
217 (in the proper sorted position for FCT_NAME) that points to
218 &FCT_NAME instead of any real `known_function' structure.
219 Allocate a new structure and fill it in. */
221 known_function *known = malloc (sizeof *known);
222 if (! known)
224 remove_from_tree:
225 /* Oops. We can't instantiate this node properly.
226 Remove it from the tree. */
227 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
228 result = NULL;
230 else
232 /* Point the tree node at this new structure. */
233 *found = known;
234 known->fct_name = fct_name;
236 if (ni->library == NULL)
238 /* This service has not yet been used. Fetch the service
239 library for it, creating a new one if need be. If there
240 is no service table from the file, this static variable
241 holds the head of the service_library list made from the
242 default configuration. */
243 static name_database default_table;
244 ni->library = nss_new_service (service_table ?: &default_table,
245 ni->name);
246 if (ni->library == NULL)
248 /* This only happens when out of memory. */
249 free (known);
250 goto remove_from_tree;
254 if (ni->library->lib_handle == NULL)
256 /* Load the shared library. */
257 size_t shlen = (7 + strlen (ni->library->name) + 3
258 + sizeof (NSS_SHLIB_REVISION));
259 char shlib_name[shlen];
261 void do_open (void)
263 /* Open and relocate the shared object. */
264 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
267 /* Construct shared object name. */
268 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
269 ni->library->name),
270 ".so" NSS_SHLIB_REVISION);
272 if (nss_dlerror_run (do_open) != 0)
273 /* Failed to load the library. */
274 ni->library->lib_handle = (void *) -1;
277 if (ni->library->lib_handle == (void *) -1)
278 /* Library not found => function not found. */
279 result = NULL;
280 else
282 /* Get the desired function. Again, GNU ld.so magic ahead. */
283 size_t namlen = (5 + strlen (ni->library->name) + 1
284 + strlen (fct_name) + 1);
285 char name[namlen];
286 struct link_map *map = ni->library->lib_handle;
287 ElfW(Addr) loadbase;
288 const ElfW(Sym) *ref = NULL;
289 void get_sym (void)
291 struct link_map *scope[2] = { map, NULL };
292 loadbase = _dl_lookup_symbol (name, &ref,
293 scope, map->l_name, 0, 0);
296 /* Construct the function name. */
297 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
298 ni->library->name),
299 "_"),
300 fct_name);
302 /* Look up the symbol. */
303 result = (nss_dlerror_run (get_sym)
304 ? NULL : (void *) (loadbase + ref->st_value));
307 /* Remember function pointer for later calls. Even if null, we
308 record it so a second try needn't search the library again. */
309 known->fct_ptr = result;
313 /* Remove the lock. */
314 __libc_lock_unlock (lock);
316 return result;
320 static name_database *
321 nss_parse_file (const char *fname)
323 FILE *fp;
324 name_database *result;
325 name_database_entry *last;
326 char *line;
327 size_t len;
329 /* Open the configuration file. */
330 fp = fopen (fname, "r");
331 if (fp == NULL)
332 return NULL;
334 result = (name_database *) malloc (sizeof (name_database));
335 if (result == NULL)
336 return NULL;
338 result->entry = NULL;
339 result->library = NULL;
340 last = NULL;
341 line = NULL;
342 len = 0;
345 name_database_entry *this;
346 ssize_t n;
347 char *cp;
349 n = __getline (&line, &len, fp);
350 if (n < 0)
351 break;
352 if (line[n - 1] == '\n')
353 line[n - 1] = '\0';
355 /* Because the file format does not know any form of quoting we
356 can search forward for the next '#' character and if found
357 make it terminating the line. */
358 cp = strchr (line, '#');
359 if (cp != NULL)
360 *cp = '\0';
362 /* If the line is blank it is ignored. */
363 if (line[0] == '\0')
364 continue;
366 /* Each line completely specifies the actions for a database. */
367 this = nss_getline (line);
368 if (this != NULL)
370 if (last != NULL)
371 last->next = this;
372 else
373 result->entry = this;
375 last = this;
378 while (!feof (fp));
380 /* Free the buffer. */
381 free (line);
382 /* Close configuration file. */
383 fclose (fp);
385 return result;
389 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
390 static service_user *
391 nss_parse_service_list (const char *line)
393 service_user *result = NULL, **nextp = &result;
395 while (1)
397 service_user *new_service;
398 const char *name;
400 while (isspace (line[0]))
401 ++line;
402 if (line[0] == '\0')
403 /* No source specified. */
404 return result;
406 /* Read <source> identifier. */
407 name = line;
408 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
409 ++line;
410 if (name == line)
411 return result;
414 new_service = (service_user *) malloc (sizeof (service_user));
415 if (new_service == NULL)
416 return result;
417 else
419 char *source = (char *) malloc (line - name + 1);
420 if (source == NULL)
422 free (new_service);
423 return result;
425 memcpy (source, name, line - name);
426 source[line - name] = '\0';
428 new_service->name = source;
431 /* Set default actions. */
432 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
433 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
434 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
435 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
436 new_service->library = NULL;
437 new_service->known = NULL;
438 new_service->next = NULL;
440 while (isspace (line[0]))
441 ++line;
443 if (line[0] == '[')
445 /* Read criterions. */
447 ++line;
448 while (line[0] != '\0' && isspace (line[0]));
452 int not;
453 enum nss_status status;
454 lookup_actions action;
456 /* Grok ! before name to mean all statii but that one. */
457 if (not = line[0] == '!')
458 ++line;
460 /* Read status name. */
461 name = line;
462 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
463 && line[0] != ']')
464 ++line;
466 /* Compare with known statii. */
467 if (line - name == 7)
469 if (__strncasecmp (name, "SUCCESS", 7) == 0)
470 status = NSS_STATUS_SUCCESS;
471 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
472 status = NSS_STATUS_UNAVAIL;
473 else
474 return result;
476 else if (line - name == 8)
478 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
479 status = NSS_STATUS_NOTFOUND;
480 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
481 status = NSS_STATUS_TRYAGAIN;
482 else
483 return result;
485 else
486 return result;
488 while (isspace (line[0]))
489 ++line;
490 if (line[0] != '=')
491 return result;
493 ++line;
494 while (isspace (line[0]));
496 name = line;
497 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
498 && line[0] != ']')
499 ++line;
501 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
502 action = NSS_ACTION_RETURN;
503 else if (line - name == 8
504 && __strncasecmp (name, "CONTINUE", 8) == 0)
505 action = NSS_ACTION_CONTINUE;
506 else
507 return result;
509 if (not)
511 /* Save the current action setting for this status,
512 set them all to the given action, and reset this one. */
513 const lookup_actions save = new_service->actions[2 + status];
514 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
515 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
516 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
517 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
518 new_service->actions[2 + status] = save;
520 else
521 new_service->actions[2 + status] = action;
523 /* Skip white spaces. */
524 while (isspace (line[0]))
525 ++line;
527 while (line[0] != ']');
529 /* Skip the ']'. */
530 ++line;
533 *nextp = new_service;
534 nextp = &new_service->next;
538 static name_database_entry *
539 nss_getline (char *line)
541 const char *name;
542 name_database_entry *result;
544 /* Ignore leading white spaces. ATTENTION: this is different from
545 what is implemented in Solaris. The Solaris man page says a line
546 beginning with a white space character is ignored. We regard
547 this as just another misfeature in Solaris. */
548 while (isspace (line[0]))
549 ++line;
551 /* Recognize `<database> ":"'. */
552 name = line;
553 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
554 ++line;
555 if (line[0] == '\0' || name == line)
556 /* Syntax error. */
557 return NULL;
558 *line++ = '\0';
560 result = (name_database_entry *) malloc (sizeof (name_database_entry));
561 if (result == NULL)
562 return NULL;
564 /* Save the database name. */
566 const size_t len = strlen (name) + 1;
567 char *new = malloc (len);
568 if (new == NULL)
570 free (result);
571 return NULL;
573 result->name = memcpy (new, name, len);
576 /* Parse the list of services. */
577 result->service = nss_parse_service_list (line);
579 result->next = NULL;
580 return result;
584 static service_library *
585 nss_new_service (name_database *database, const char *name)
587 service_library **currentp = &database->library;
589 while (*currentp != NULL)
591 if (strcmp ((*currentp)->name, name) == 0)
592 return *currentp;
593 currentp = &(*currentp)->next;
596 /* We have to add the new service. */
597 *currentp = (service_library *) malloc (sizeof (service_library));
598 if (*currentp == NULL)
599 return NULL;
601 (*currentp)->name = name;
602 (*currentp)->lib_handle = NULL;
603 (*currentp)->next = NULL;
605 return *currentp;