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. */
23 #include <libc-lock.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 int nss_find_entry (struct entry **knownp, const char *key,
37 static void nss_insert_entry (struct entry **knownp, const char *key,
39 static name_database *nss_parse_file (const char *fname);
40 static name_database_entry *nss_getline (char *line);
41 static service_library *nss_new_service (name_database *database,
45 __libc_lock_define_initialized (static, lock);
48 /* Global variable. */
49 struct __res_state _res;
52 /* Known aliases for service names. */
58 { "nis+", "nisplus" },
63 /* Nonzero if the sevices are already initialized. */
64 static int nss_initialized;
67 /* The root of the whole data base. */
68 static name_database *service_table;
74 /* Prevent multiple threads to change the service table. */
75 __libc_lock_lock (lock);
77 if (service_table == NULL)
78 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
80 __libc_lock_unlock (lock);
84 /* -1 == database not found
85 0 == database entry pointer stored */
87 __nss_database_lookup (const char *database, service_user **ni)
89 /* Return first `service_user' entry for DATABASE.
90 XXX Will use perfect hashing function for known databases. */
91 name_database_entry *entry;
93 /* Test whether configuration data is available. */
94 if (service_table == NULL)
96 if (nss_initialized == 0)
99 if (service_table == NULL)
103 /* XXX Could use some faster mechanism here. But each database is
104 only requested once and so this might not be critical. */
105 for (entry = service_table->entry; entry != NULL; entry = entry->next)
106 if (strcmp (database, entry->name) == 0)
109 if (entry == NULL || (*ni = entry->service) == NULL)
117 0 == adjusted for next function */
119 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
121 *fctp = nss_lookup_function (*ni, fct_name);
124 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
125 && (*ni)->next != NULL)
129 *fctp = nss_lookup_function (*ni, fct_name);
132 return *fctp != NULL ? 0 : -1;
137 0 == adjusted for next function
140 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
145 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
146 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
147 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
148 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
153 /* This is really only for debugging. */
154 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
155 __libc_fatal ("illegal status in " __FUNCTION__);
157 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
161 if ((*ni)->next == NULL)
168 *fctp = nss_lookup_function (*ni, fct_name);
171 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
172 && (*ni)->next != NULL);
174 return *fctp != NULL ? 0 : -1;
179 nss_dlerror_run (void (*operate) (void))
181 const char *last_errstring = NULL;
182 const char *last_object_name = NULL;
184 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
186 return last_errstring != NULL;
191 nss_lookup_function (service_user *ni, const char *fct_name)
195 /* Determine whether current function is loaded. */
196 if (nss_find_entry (&ni->known, fct_name, &result) >= 0)
199 /* If we failed to allocate the needed data structures for the
200 service return an error. This should only happen when we are out
202 if (ni->library == NULL)
205 /* We now modify global data. Protect it. */
206 __libc_lock_lock (lock);
208 if (ni->library->lib_handle == NULL)
210 /* Load the shared library. */
211 size_t shlen = (7 + strlen (ni->library->name) + 3
212 + sizeof (NSS_SHLIB_REVISION));
213 char shlib_name[shlen];
217 /* The used function is found in GNU ld.so. XXX The first
218 argument to _dl_open used to be `_dl_loaded'. But this
219 does (currently) not work. */
220 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
223 /* Construct name. */
224 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"), ni->library->name),
225 ".so" NSS_SHLIB_REVISION);
227 if (nss_dlerror_run (do_open) != 0)
228 /* Failed to load the library. */
229 ni->library->lib_handle = (void *) -1;
232 if (ni->library->lib_handle == (void *) -1)
233 /* Library not found => function not found. */
237 /* Get the desired function. Again, GNU ld.so magic ahead. */
238 size_t namlen = (5 + strlen (ni->library->name) + 1
239 + strlen (fct_name) + 1);
241 struct link_map *map = ni->library->lib_handle;
243 const Elf32_Sym *ref = NULL;
246 struct link_map *scope[2] = { map, NULL };
247 loadbase = _dl_lookup_symbol (name, &ref, scope, map->l_name, 0, 0);
250 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
255 result = (nss_dlerror_run (get_sym)
256 ? NULL : (void *) (loadbase + ref->st_value));
259 /* Remember function pointer for the usage. */
260 nss_insert_entry (&ni->known, fct_name, result);
262 /* Remove the lock. */
263 __libc_lock_unlock (lock);
270 known_compare (const void *p1, const void *p2)
272 known_function *v1 = (known_function *) p1;
273 known_function *v2 = (known_function *) p2;
275 return strcmp (v1->fct_name, v2->fct_name);
280 nss_find_entry (struct entry **knownp, const char *key, void **valp)
282 known_function looking_for = { fct_name: key };
283 struct entry **found;
285 found = __tfind (&looking_for, (const void **) knownp, known_compare);
290 *valp = ((known_function *) (*found)->key)->fct_ptr;
297 nss_insert_entry (struct entry **knownp, const char *key, void *val)
299 known_function *to_insert;
301 to_insert = (known_function *) malloc (sizeof (known_function));
302 if (to_insert == NULL)
305 to_insert->fct_name = key;
306 to_insert->fct_ptr = val;
308 __tsearch (to_insert, (void **) knownp, known_compare);
312 static name_database *
313 nss_parse_file (const char *fname)
316 name_database *result;
317 name_database_entry *last;
321 /* Open the configuration file. */
322 fp = fopen (fname, "r");
326 result = (name_database *) malloc (sizeof (name_database));
330 result->entry = NULL;
331 result->library = NULL;
337 name_database_entry *this;
341 n = getline (&line, &len, fp);
344 if (line[n - 1] == '\n')
347 /* Because the file format does not know any form of quoting we
348 can search forward for the next '#' character and if found
349 make it terminating the line. */
350 cp = strchr (line, '#');
354 /* If the line is blank it is ignored. */
358 /* Each line completely specifies the actions for a database. */
359 this = nss_getline (line);
365 result->entry = this;
372 /* Free the buffer. */
374 /* Close configuration file. */
377 /* Now create for each service we could use an entry in LIBRARY list. */
378 for (last = result->entry; last != NULL; last = last->next)
382 for (runp = last->service; runp != NULL; runp = runp->next)
383 runp->library = nss_new_service (result, runp->name);
390 static name_database_entry *
391 nss_getline (char *line)
394 name_database_entry *result;
397 /* Ignore leading white spaces. ATTENTION: this is different from
398 what is implemented in Solaris. The Solaris man page says a line
399 beginning with a white space character is ignored. We regard
400 this as just another misfeature in Solaris. */
401 while (isspace (line[0]))
404 /* Recognize `<database> ":"'. */
406 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
408 if (line[0] == '\0' || name == line)
413 result = (name_database_entry *) malloc (sizeof (name_database_entry));
417 result->name = strdup (name);
418 if (result->name == NULL)
423 result->service = NULL;
427 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
430 service_user *new_service;
433 while (isspace (line[0]))
436 /* No source specified. */
439 /* Read <source> identifier. */
441 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
447 new_service = (service_user *) malloc (sizeof (service_user));
448 if (new_service == NULL)
451 /* Test whether the source name is one of the aliases. */
452 for (n = 0; n < sizeof (service_alias) / sizeof (service_alias[0]); ++n)
453 if (strncmp (service_alias[n].alias, name, line - name) == 0
454 && service_alias[n].alias[line - name] == '\0')
457 if (n < sizeof (service_alias) / sizeof (service_alias[0]))
458 new_service->name = service_alias[n].value;
461 char *source = (char *) malloc (line - name + 1);
467 memcpy (source, name, line - name);
468 source[line - name] = '\0';
470 new_service->name = source;
473 /* Set default actions. */
474 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
475 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
476 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
477 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
478 new_service->library = NULL;
479 new_service->known = NULL;
480 new_service->next = NULL;
482 while (isspace (line[0]))
489 /* Read criterions. */
492 while (line[0] != '\0' && isspace (line[0]));
496 /* Read status name. */
498 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
502 /* Compare with known statii. */
503 if (line - name == 7)
505 if (strncasecmp (name, "SUCCESS", 7) == 0)
506 status = NSS_STATUS_SUCCESS;
507 else if (strncasecmp (name, "UNAVAIL", 7) == 0)
508 status = NSS_STATUS_UNAVAIL;
512 else if (line - name == 8)
514 if (strncasecmp (name, "NOTFOUND", 8) == 0)
515 status = NSS_STATUS_NOTFOUND;
516 else if (strncasecmp (name, "TRYAGAIN", 8) == 0)
517 status = NSS_STATUS_TRYAGAIN;
524 while (isspace (line[0]))
530 while (isspace (line[0]));
533 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
537 if (line - name == 6 && strncasecmp (name, "RETURN", 6) == 0)
538 new_service->actions[2 + status] = NSS_ACTION_RETURN;
539 else if (line - name == 8
540 && strncasecmp (name, "CONTINUE", 8) == 0)
541 new_service->actions[2 + status] = NSS_ACTION_CONTINUE;
545 /* Match white spaces. */
546 while (isspace (line[0]))
549 while (line[0] != ']');
556 result->service = new_service;
558 last->next = new_service;
566 static service_library *
567 nss_new_service (name_database *database, const char *name)
569 service_library **currentp = &database->library;
571 while (*currentp != NULL)
573 if (strcmp ((*currentp)->name, name) == 0)
575 currentp = &(*currentp)->next;
578 /* We have to add the new service. */
579 *currentp = (service_library *) malloc (sizeof (service_library));
580 if (*currentp == NULL)
583 (*currentp)->name = name;
584 (*currentp)->lib_handle = NULL;
585 (*currentp)->next = NULL;