Sun Jul 14 01:51:39 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc/history.git] / nss / nsswitch.c
blobd259165269a1cf3e0f44bc1b1ecd53c0405d3a5d
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 /* Comparison function for searching NI->known tree. */
188 static int
189 known_compare (const void *p1, const void *p2)
191 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
192 *(const char *const *) p2);
196 static void *
197 nss_lookup_function (service_user *ni, const char *fct_name)
199 void **found, *result;
201 /* We now modify global data. Protect it. */
202 __libc_lock_lock (lock);
204 /* Search the tree of functions previously requested. Data in the
205 tree are `known_function' structures, whose first member is a
206 `const char *', the lookup key. The search returns a pointer to
207 the tree node structure; the first member of the is a pointer to
208 our structure (i.e. what will be a `known_function'); since the
209 first member of that is the lookup key string, &FCT_NAME is close
210 enough to a pointer to our structure to use as a lookup key that
211 will be passed to `known_compare' (above). */
213 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
214 if (*found != &fct_name)
215 /* The search found an existing structure in the tree. */
216 result = ((known_function *) *found)->fct_ptr;
217 else
219 /* This name was not known before. Now we have a node in the tree
220 (in the proper sorted position for FCT_NAME) that points to
221 &FCT_NAME instead of any real `known_function' structure.
222 Allocate a new structure and fill it in. */
224 known_function *known = malloc (sizeof *known);
225 if (! known)
227 remove_from_tree:
228 /* Oops. We can't instantiate this node properly.
229 Remove it from the tree. */
230 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
231 result = NULL;
233 else
235 /* Point the tree node at this new structure. */
236 *found = known;
237 known->fct_name = fct_name;
239 if (ni->library == NULL)
241 /* This service has not yet been used. Fetch the service
242 library for it, creating a new one if need be. If there
243 is no service table from the file, this static variable
244 holds the head of the service_library list made from the
245 default configuration. */
246 static name_database default_table;
247 ni->library = nss_new_service (service_table ?: &default_table,
248 ni->name);
249 if (ni->library == NULL)
251 /* This only happens when out of memory. */
252 free (known);
253 goto remove_from_tree;
257 if (ni->library->lib_handle == NULL)
259 /* Load the shared library. */
260 size_t shlen = (7 + strlen (ni->library->name) + 3
261 + sizeof (NSS_SHLIB_REVISION));
262 char shlib_name[shlen];
264 void do_open (void)
266 /* Open and relocate the shared object. */
267 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
270 /* Construct shared object name. */
271 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
272 ni->library->name),
273 ".so" NSS_SHLIB_REVISION);
275 if (nss_dlerror_run (do_open) != 0)
276 /* Failed to load the library. */
277 ni->library->lib_handle = (void *) -1;
280 if (ni->library->lib_handle == (void *) -1)
281 /* Library not found => function not found. */
282 result = NULL;
283 else
285 /* Get the desired function. Again, GNU ld.so magic ahead. */
286 size_t namlen = (5 + strlen (ni->library->name) + 1
287 + strlen (fct_name) + 1);
288 char name[namlen];
289 struct link_map *map = ni->library->lib_handle;
290 ElfW(Addr) loadbase;
291 const ElfW(Sym) *ref = NULL;
292 void get_sym (void)
294 struct link_map *scope[2] = { map, NULL };
295 loadbase = _dl_lookup_symbol (name, &ref,
296 scope, map->l_name, 0, 0);
299 /* Construct the function name. */
300 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
301 ni->library->name),
302 "_"),
303 fct_name);
305 /* Look up the symbol. */
306 result = (nss_dlerror_run (get_sym)
307 ? NULL : (void *) (loadbase + ref->st_value));
310 /* Remember function pointer for later calls. Even if null, we
311 record it so a second try needn't search the library again. */
312 known->fct_ptr = result;
316 /* Remove the lock. */
317 __libc_lock_unlock (lock);
319 return result;
323 static name_database *
324 nss_parse_file (const char *fname)
326 FILE *fp;
327 name_database *result;
328 name_database_entry *last;
329 char *line;
330 size_t len;
332 /* Open the configuration file. */
333 fp = fopen (fname, "r");
334 if (fp == NULL)
335 return NULL;
337 result = (name_database *) malloc (sizeof (name_database));
338 if (result == NULL)
339 return NULL;
341 result->entry = NULL;
342 result->library = NULL;
343 last = NULL;
344 line = NULL;
345 len = 0;
348 name_database_entry *this;
349 ssize_t n;
350 char *cp;
352 n = __getline (&line, &len, fp);
353 if (n < 0)
354 break;
355 if (line[n - 1] == '\n')
356 line[n - 1] = '\0';
358 /* Because the file format does not know any form of quoting we
359 can search forward for the next '#' character and if found
360 make it terminating the line. */
361 cp = strchr (line, '#');
362 if (cp != NULL)
363 *cp = '\0';
365 /* If the line is blank it is ignored. */
366 if (line[0] == '\0')
367 continue;
369 /* Each line completely specifies the actions for a database. */
370 this = nss_getline (line);
371 if (this != NULL)
373 if (last != NULL)
374 last->next = this;
375 else
376 result->entry = this;
378 last = this;
381 while (!feof (fp));
383 /* Free the buffer. */
384 free (line);
385 /* Close configuration file. */
386 fclose (fp);
388 return result;
392 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
393 static service_user *
394 nss_parse_service_list (const char *line)
396 service_user *result = NULL, **nextp = &result;
398 while (1)
400 service_user *new_service;
401 const char *name;
403 while (isspace (line[0]))
404 ++line;
405 if (line[0] == '\0')
406 /* No source specified. */
407 return result;
409 /* Read <source> identifier. */
410 name = line;
411 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
412 ++line;
413 if (name == line)
414 return result;
417 new_service = (service_user *) malloc (sizeof (service_user));
418 if (new_service == NULL)
419 return result;
420 else
422 char *source = (char *) malloc (line - name + 1);
423 if (source == NULL)
425 free (new_service);
426 return result;
428 memcpy (source, name, line - name);
429 source[line - name] = '\0';
431 new_service->name = source;
434 /* Set default actions. */
435 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
436 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
437 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
438 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
439 new_service->library = NULL;
440 new_service->known = NULL;
441 new_service->next = NULL;
443 while (isspace (line[0]))
444 ++line;
446 if (line[0] == '[')
448 /* Read criterions. */
450 ++line;
451 while (line[0] != '\0' && isspace (line[0]));
455 int not;
456 enum nss_status status;
457 lookup_actions action;
459 /* Grok ! before name to mean all statii but that one. */
460 if (not = line[0] == '!')
461 ++line;
463 /* Read status name. */
464 name = line;
465 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
466 && line[0] != ']')
467 ++line;
469 /* Compare with known statii. */
470 if (line - name == 7)
472 if (__strncasecmp (name, "SUCCESS", 7) == 0)
473 status = NSS_STATUS_SUCCESS;
474 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
475 status = NSS_STATUS_UNAVAIL;
476 else
477 return result;
479 else if (line - name == 8)
481 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
482 status = NSS_STATUS_NOTFOUND;
483 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
484 status = NSS_STATUS_TRYAGAIN;
485 else
486 return result;
488 else
489 return result;
491 while (isspace (line[0]))
492 ++line;
493 if (line[0] != '=')
494 return result;
496 ++line;
497 while (isspace (line[0]));
499 name = line;
500 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
501 && line[0] != ']')
502 ++line;
504 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
505 action = NSS_ACTION_RETURN;
506 else if (line - name == 8
507 && __strncasecmp (name, "CONTINUE", 8) == 0)
508 action = NSS_ACTION_CONTINUE;
509 else
510 return result;
512 if (not)
514 /* Save the current action setting for this status,
515 set them all to the given action, and reset this one. */
516 const lookup_actions save = new_service->actions[2 + status];
517 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
518 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
519 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
520 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
521 new_service->actions[2 + status] = save;
523 else
524 new_service->actions[2 + status] = action;
526 /* Skip white spaces. */
527 while (isspace (line[0]))
528 ++line;
530 while (line[0] != ']');
532 /* Skip the ']'. */
533 ++line;
536 *nextp = new_service;
537 nextp = &new_service->next;
541 static name_database_entry *
542 nss_getline (char *line)
544 const char *name;
545 name_database_entry *result;
547 /* Ignore leading white spaces. ATTENTION: this is different from
548 what is implemented in Solaris. The Solaris man page says a line
549 beginning with a white space character is ignored. We regard
550 this as just another misfeature in Solaris. */
551 while (isspace (line[0]))
552 ++line;
554 /* Recognize `<database> ":"'. */
555 name = line;
556 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
557 ++line;
558 if (line[0] == '\0' || name == line)
559 /* Syntax error. */
560 return NULL;
561 *line++ = '\0';
563 result = (name_database_entry *) malloc (sizeof (name_database_entry));
564 if (result == NULL)
565 return NULL;
567 /* Save the database name. */
569 const size_t len = strlen (name) + 1;
570 char *new = malloc (len);
571 if (new == NULL)
573 free (result);
574 return NULL;
576 result->name = memcpy (new, name, len);
579 /* Parse the list of services. */
580 result->service = nss_parse_service_list (line);
582 result->next = NULL;
583 return result;
587 static service_library *
588 nss_new_service (name_database *database, const char *name)
590 service_library **currentp = &database->library;
592 while (*currentp != NULL)
594 if (strcmp ((*currentp)->name, name) == 0)
595 return *currentp;
596 currentp = &(*currentp)->next;
599 /* We have to add the new service. */
600 *currentp = (service_library *) malloc (sizeof (service_library));
601 if (*currentp == NULL)
602 return NULL;
604 (*currentp)->name = name;
605 (*currentp)->lib_handle = NULL;
606 (*currentp)->next = NULL;
608 return *currentp;