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