]> git.ipfire.org Git - thirdparty/glibc.git/blob - nss/nsswitch.c
update from main archive
[thirdparty/glibc.git] / nss / nsswitch.c
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.
4
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.
9
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.
14
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. */
19
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <libc-lock.h>
25 #include <search.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "nsswitch.h"
31 #include "../elf/link.h" /* We need some help from ld.so. */
32
33 /* Prototypes for the local functions. */
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);
40
41
42 /* Declare external database variables. */
43 #define DEFINE_DATABASE(name) \
44 extern service_user *__nss_##name##_database; \
45 weak_extern (__nss_##name##_database)
46 #include "databases.def"
47 #undef DEFINE_DATABASE
48
49 /* Structure to map database name to variable. */
50 static struct
51 {
52 const char *name;
53 service_user **dbp;
54 } databases[] =
55 {
56 #define DEFINE_DATABASE(name) \
57 { #name, &__nss_##name##_database },
58 #include "databases.def"
59 #undef DEFINE_DATABASE
60 };
61
62
63 __libc_lock_define_initialized (static, lock)
64
65
66 /* Nonzero if the sevices are already initialized. */
67 static int nss_initialized;
68
69
70 /* The root of the whole data base. */
71 static name_database *service_table;
72
73
74 /* -1 == database not found
75 0 == database entry pointer stored */
76 int
77 __nss_database_lookup (const char *database, const char *defconfig,
78 service_user **ni)
79 {
80 /* Prevent multiple threads to change the service table. */
81 __libc_lock_lock (lock);
82
83 /* Reconsider database variable in case some other thread called
84 `__nss_configure_lookup' while we waited for the lock. */
85 if (*ni != NULL)
86 {
87 __libc_lock_unlock (lock);
88 return 0;
89 }
90
91 if (nss_initialized == 0 && service_table == NULL)
92 /* Read config file. */
93 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
94
95 /* Test whether configuration data is available. */
96 if (service_table != NULL)
97 {
98 /* Return first `service_user' entry for DATABASE. */
99 name_database_entry *entry;
100
101 /* XXX Could use some faster mechanism here. But each database is
102 only requested once and so this might not be critical. */
103 for (entry = service_table->entry; entry != NULL; entry = entry->next)
104 if (strcmp (database, entry->name) == 0)
105 *ni = entry->service;
106 }
107
108 /* No configuration data is available, either because nsswitch.conf
109 doesn't exist or because it doesn't has a line for this database.
110
111 DEFCONFIG specifies the default service list for this database,
112 or null to use the most common default. */
113 if (*ni == NULL)
114 *ni = nss_parse_service_list (defconfig
115 ?: "compat [NOTFOUND=return] files");
116
117 __libc_lock_unlock (lock);
118
119 return 0;
120 }
121
122
123 /* -1 == not found
124 0 == adjusted for next function */
125 int
126 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
127 {
128 *fctp = nss_lookup_function (*ni, fct_name);
129
130 while (*fctp == NULL
131 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
132 && (*ni)->next != NULL)
133 {
134 *ni = (*ni)->next;
135
136 *fctp = nss_lookup_function (*ni, fct_name);
137 }
138
139 return *fctp != NULL ? 0 : -1;
140 }
141
142
143 /* -1 == not found
144 0 == adjusted for next function
145 1 == finished */
146 int
147 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
148 int all_values)
149 {
150 if (all_values)
151 {
152 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
153 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
154 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
155 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
156 return 1;
157 }
158 else
159 {
160 /* This is really only for debugging. */
161 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
162 __libc_fatal ("illegal status in " __FUNCTION__);
163
164 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
165 return 1;
166 }
167
168 if ((*ni)->next == NULL)
169 return -1;
170
171 do
172 {
173 *ni = (*ni)->next;
174
175 *fctp = nss_lookup_function (*ni, fct_name);
176 }
177 while (*fctp == NULL
178 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
179 && (*ni)->next != NULL);
180
181 return *fctp != NULL ? 0 : -1;
182 }
183
184
185 int
186 __nss_configure_lookup (const char *dbname, const char *service_line)
187 {
188 service_user *new_db;
189 size_t cnt;
190
191 for (cnt = 0; cnt < sizeof databases; ++cnt)
192 {
193 int cmp = strcmp (dbname, databases[cnt].name);
194 if (cmp == 0)
195 break;
196 if (cmp > 0)
197 {
198 __set_errno (EINVAL);
199 return -1;
200 }
201 }
202
203 if (cnt == sizeof databases)
204 {
205 __set_errno (EINVAL);
206 return -1;
207 }
208
209 /* Test whether it is really used. */
210 if (databases[cnt].dbp == NULL)
211 /* Nothing to do, but we could do. */
212 return 0;
213
214 /* Try to generate new data. */
215 new_db = nss_parse_service_list (service_line);
216 if (new_db == NULL)
217 {
218 /* Illegal service specification. */
219 __set_errno (EINVAL);
220 return -1;
221 }
222
223 /* Prevent multiple threads to change the service table. */
224 __libc_lock_lock (lock);
225
226 /* Install new rules. */
227 *databases[cnt].dbp = new_db;
228
229 __libc_lock_unlock (lock);
230
231 return 0;
232 }
233
234
235 static int
236 nss_dlerror_run (void (*operate) (void))
237 {
238 char *last_errstring = NULL;
239 const char *last_object_name = NULL;
240 int result;
241
242 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
243
244 result = last_errstring != NULL;
245 if (result)
246 free (last_errstring);
247
248 return result;
249 }
250
251
252 /* Comparison function for searching NI->known tree. */
253 static int
254 known_compare (const void *p1, const void *p2)
255 {
256 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
257 *(const char *const *) p2);
258 }
259
260
261 static void *
262 nss_lookup_function (service_user *ni, const char *fct_name)
263 {
264 void **found, *result;
265
266 /* We now modify global data. Protect it. */
267 __libc_lock_lock (lock);
268
269 /* Search the tree of functions previously requested. Data in the
270 tree are `known_function' structures, whose first member is a
271 `const char *', the lookup key. The search returns a pointer to
272 the tree node structure; the first member of the is a pointer to
273 our structure (i.e. what will be a `known_function'); since the
274 first member of that is the lookup key string, &FCT_NAME is close
275 enough to a pointer to our structure to use as a lookup key that
276 will be passed to `known_compare' (above). */
277
278 found = __tsearch (&fct_name, (void **) &ni->known, &known_compare);
279 if (*found != &fct_name)
280 /* The search found an existing structure in the tree. */
281 result = ((known_function *) *found)->fct_ptr;
282 else
283 {
284 /* This name was not known before. Now we have a node in the tree
285 (in the proper sorted position for FCT_NAME) that points to
286 &FCT_NAME instead of any real `known_function' structure.
287 Allocate a new structure and fill it in. */
288
289 known_function *known = malloc (sizeof *known);
290 if (! known)
291 {
292 remove_from_tree:
293 /* Oops. We can't instantiate this node properly.
294 Remove it from the tree. */
295 __tdelete (&fct_name, (void **) &ni->known, &known_compare);
296 result = NULL;
297 }
298 else
299 {
300 /* Point the tree node at this new structure. */
301 *found = known;
302 known->fct_name = fct_name;
303
304 if (ni->library == NULL)
305 {
306 /* This service has not yet been used. Fetch the service
307 library for it, creating a new one if need be. If there
308 is no service table from the file, this static variable
309 holds the head of the service_library list made from the
310 default configuration. */
311 static name_database default_table;
312 ni->library = nss_new_service (service_table ?: &default_table,
313 ni->name);
314 if (ni->library == NULL)
315 {
316 /* This only happens when out of memory. */
317 free (known);
318 goto remove_from_tree;
319 }
320 }
321
322 if (ni->library->lib_handle == NULL)
323 {
324 /* Load the shared library. */
325 size_t shlen = (7 + strlen (ni->library->name) + 3
326 + sizeof (NSS_SHLIB_REVISION));
327 char shlib_name[shlen];
328
329 void do_open (void)
330 {
331 /* Open and relocate the shared object. */
332 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
333 }
334
335 /* Construct shared object name. */
336 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"),
337 ni->library->name),
338 ".so" NSS_SHLIB_REVISION);
339
340 if (nss_dlerror_run (do_open) != 0)
341 /* Failed to load the library. */
342 ni->library->lib_handle = (void *) -1;
343 }
344
345 if (ni->library->lib_handle == (void *) -1)
346 /* Library not found => function not found. */
347 result = NULL;
348 else
349 {
350 /* Get the desired function. Again, GNU ld.so magic ahead. */
351 size_t namlen = (5 + strlen (ni->library->name) + 1
352 + strlen (fct_name) + 1);
353 char name[namlen];
354 struct link_map *map = ni->library->lib_handle;
355 ElfW(Addr) loadbase;
356 const ElfW(Sym) *ref = NULL;
357 void get_sym (void)
358 {
359 struct link_map *scope[2] = { map, NULL };
360 loadbase = _dl_lookup_symbol (name, &ref,
361 scope, map->l_name, 0);
362 }
363
364 /* Construct the function name. */
365 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
366 ni->library->name),
367 "_"),
368 fct_name);
369
370 /* Look up the symbol. */
371 result = (nss_dlerror_run (get_sym)
372 ? NULL : (void *) (loadbase + ref->st_value));
373 }
374
375 /* Remember function pointer for later calls. Even if null, we
376 record it so a second try needn't search the library again. */
377 known->fct_ptr = result;
378 }
379 }
380
381 /* Remove the lock. */
382 __libc_lock_unlock (lock);
383
384 return result;
385 }
386
387
388 static name_database *
389 nss_parse_file (const char *fname)
390 {
391 FILE *fp;
392 name_database *result;
393 name_database_entry *last;
394 char *line;
395 size_t len;
396
397 /* Open the configuration file. */
398 fp = fopen (fname, "r");
399 if (fp == NULL)
400 return NULL;
401
402 result = (name_database *) malloc (sizeof (name_database));
403 if (result == NULL)
404 return NULL;
405
406 result->entry = NULL;
407 result->library = NULL;
408 last = NULL;
409 line = NULL;
410 len = 0;
411 do
412 {
413 name_database_entry *this;
414 ssize_t n;
415 char *cp;
416
417 n = __getline (&line, &len, fp);
418 if (n < 0)
419 break;
420 if (line[n - 1] == '\n')
421 line[n - 1] = '\0';
422
423 /* Because the file format does not know any form of quoting we
424 can search forward for the next '#' character and if found
425 make it terminating the line. */
426 cp = strchr (line, '#');
427 if (cp != NULL)
428 *cp = '\0';
429
430 /* If the line is blank it is ignored. */
431 if (line[0] == '\0')
432 continue;
433
434 /* Each line completely specifies the actions for a database. */
435 this = nss_getline (line);
436 if (this != NULL)
437 {
438 if (last != NULL)
439 last->next = this;
440 else
441 result->entry = this;
442
443 last = this;
444 }
445 }
446 while (!feof (fp));
447
448 /* Free the buffer. */
449 free (line);
450 /* Close configuration file. */
451 fclose (fp);
452
453 return result;
454 }
455
456
457 /* Read the source names:
458 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
459 */
460 static service_user *
461 nss_parse_service_list (const char *line)
462 {
463 service_user *result = NULL, **nextp = &result;
464
465 while (1)
466 {
467 service_user *new_service;
468 const char *name;
469
470 while (isspace (line[0]))
471 ++line;
472 if (line[0] == '\0')
473 /* No source specified. */
474 return result;
475
476 /* Read <source> identifier. */
477 name = line;
478 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
479 ++line;
480 if (name == line)
481 return result;
482
483
484 new_service = (service_user *) malloc (sizeof (service_user));
485 if (new_service == NULL)
486 return result;
487 else
488 {
489 char *source = (char *) malloc (line - name + 1);
490 if (source == NULL)
491 {
492 free (new_service);
493 return result;
494 }
495 memcpy (source, name, line - name);
496 source[line - name] = '\0';
497
498 new_service->name = source;
499 }
500
501 /* Set default actions. */
502 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
503 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
504 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
505 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
506 new_service->library = NULL;
507 new_service->known = NULL;
508 new_service->next = NULL;
509
510 while (isspace (line[0]))
511 ++line;
512
513 if (line[0] == '[')
514 {
515 /* Read criterions. */
516 do
517 ++line;
518 while (line[0] != '\0' && isspace (line[0]));
519
520 do
521 {
522 int not;
523 enum nss_status status;
524 lookup_actions action;
525
526 /* Grok ! before name to mean all statii but that one. */
527 if (not = line[0] == '!')
528 ++line;
529
530 /* Read status name. */
531 name = line;
532 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
533 && line[0] != ']')
534 ++line;
535
536 /* Compare with known statii. */
537 if (line - name == 7)
538 {
539 if (__strncasecmp (name, "SUCCESS", 7) == 0)
540 status = NSS_STATUS_SUCCESS;
541 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
542 status = NSS_STATUS_UNAVAIL;
543 else
544 return result;
545 }
546 else if (line - name == 8)
547 {
548 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
549 status = NSS_STATUS_NOTFOUND;
550 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
551 status = NSS_STATUS_TRYAGAIN;
552 else
553 return result;
554 }
555 else
556 return result;
557
558 while (isspace (line[0]))
559 ++line;
560 if (line[0] != '=')
561 return result;
562 do
563 ++line;
564 while (isspace (line[0]));
565
566 name = line;
567 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
568 && line[0] != ']')
569 ++line;
570
571 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
572 action = NSS_ACTION_RETURN;
573 else if (line - name == 8
574 && __strncasecmp (name, "CONTINUE", 8) == 0)
575 action = NSS_ACTION_CONTINUE;
576 else
577 return result;
578
579 if (not)
580 {
581 /* Save the current action setting for this status,
582 set them all to the given action, and reset this one. */
583 const lookup_actions save = new_service->actions[2 + status];
584 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
585 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
586 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
587 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
588 new_service->actions[2 + status] = save;
589 }
590 else
591 new_service->actions[2 + status] = action;
592
593 /* Skip white spaces. */
594 while (isspace (line[0]))
595 ++line;
596 }
597 while (line[0] != ']');
598
599 /* Skip the ']'. */
600 ++line;
601 }
602
603 *nextp = new_service;
604 nextp = &new_service->next;
605 }
606 }
607
608 static name_database_entry *
609 nss_getline (char *line)
610 {
611 const char *name;
612 name_database_entry *result;
613
614 /* Ignore leading white spaces. ATTENTION: this is different from
615 what is implemented in Solaris. The Solaris man page says a line
616 beginning with a white space character is ignored. We regard
617 this as just another misfeature in Solaris. */
618 while (isspace (line[0]))
619 ++line;
620
621 /* Recognize `<database> ":"'. */
622 name = line;
623 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
624 ++line;
625 if (line[0] == '\0' || name == line)
626 /* Syntax error. */
627 return NULL;
628 *line++ = '\0';
629
630 result = (name_database_entry *) malloc (sizeof (name_database_entry));
631 if (result == NULL)
632 return NULL;
633
634 /* Save the database name. */
635 {
636 const size_t len = strlen (name) + 1;
637 char *new = malloc (len);
638 if (new == NULL)
639 {
640 free (result);
641 return NULL;
642 }
643 result->name = memcpy (new, name, len);
644 }
645
646 /* Parse the list of services. */
647 result->service = nss_parse_service_list (line);
648
649 result->next = NULL;
650 return result;
651 }
652
653
654 static service_library *
655 nss_new_service (name_database *database, const char *name)
656 {
657 service_library **currentp = &database->library;
658
659 while (*currentp != NULL)
660 {
661 if (strcmp ((*currentp)->name, name) == 0)
662 return *currentp;
663 currentp = &(*currentp)->next;
664 }
665
666 /* We have to add the new service. */
667 *currentp = (service_library *) malloc (sizeof (service_library));
668 if (*currentp == NULL)
669 return NULL;
670
671 (*currentp)->name = name;
672 (*currentp)->lib_handle = NULL;
673 (*currentp)->next = NULL;
674
675 return *currentp;
676 }