1 /* Copyright (C) 1996-1999,2001,2002,2003 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <bits/libc-lock.h>
27 #include <stdio_ext.h>
33 #include <netinet/ether.h>
37 #if !defined DO_STATIC_NSS || defined SHARED
38 # include <gnu/lib-names.h>
42 #include "../nscd/nscd_proto.h"
44 /* Prototypes for the local functions. */
45 static name_database
*nss_parse_file (const char *fname
) internal_function
;
46 static name_database_entry
*nss_getline (char *line
) internal_function
;
47 static service_user
*nss_parse_service_list (const char *line
)
49 static service_library
*nss_new_service (name_database
*database
,
50 const char *name
) internal_function
;
53 /* Declare external database variables. */
54 #define DEFINE_DATABASE(name) \
55 extern service_user *__nss_##name##_database attribute_hidden; \
56 weak_extern (__nss_##name##_database)
57 #include "databases.def"
58 #undef DEFINE_DATABASE
60 /* Structure to map database name to variable. */
67 #define DEFINE_DATABASE(name) \
68 { #name, &__nss_##name##_database },
69 #include "databases.def"
70 #undef DEFINE_DATABASE
74 __libc_lock_define_initialized (static, lock
)
76 #if !defined DO_STATIC_NSS || defined SHARED
77 /* String with revision number of the shared object files. */
78 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
81 /* The root of the whole data base. */
82 static name_database
*service_table
;
85 /* -1 == database not found
86 0 == database entry pointer stored */
88 __nss_database_lookup (const char *database
, const char *alternate_name
,
89 const char *defconfig
, service_user
**ni
)
91 /* Prevent multiple threads to change the service table. */
92 __libc_lock_lock (lock
);
94 /* Reconsider database variable in case some other thread called
95 `__nss_configure_lookup' while we waited for the lock. */
98 __libc_lock_unlock (lock
);
102 /* Are we initialized yet? */
103 if (service_table
== NULL
)
104 /* Read config file. */
105 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
107 /* Test whether configuration data is available. */
108 if (service_table
!= NULL
)
110 /* Return first `service_user' entry for DATABASE. */
111 name_database_entry
*entry
;
113 /* XXX Could use some faster mechanism here. But each database is
114 only requested once and so this might not be critical. */
115 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
116 if (strcmp (database
, entry
->name
) == 0)
117 *ni
= entry
->service
;
119 if (*ni
== NULL
&& alternate_name
!= NULL
)
120 /* We haven't found an entry so far. Try to find it with the
122 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
123 if (strcmp (alternate_name
, entry
->name
) == 0)
124 *ni
= entry
->service
;
127 /* No configuration data is available, either because nsswitch.conf
128 doesn't exist or because it doesn't has a line for this database.
130 DEFCONFIG specifies the default service list for this database,
131 or null to use the most common default. */
133 *ni
= nss_parse_service_list (defconfig
134 ?: "nis [NOTFOUND=return] files");
136 __libc_lock_unlock (lock
);
140 libc_hidden_def (__nss_database_lookup
)
147 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
149 *fctp
= __nss_lookup_function (*ni
, fct_name
);
152 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
153 && (*ni
)->next
!= NULL
)
157 *fctp
= __nss_lookup_function (*ni
, fct_name
);
160 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
165 0 == adjusted for next function
168 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
173 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
174 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
175 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
176 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
181 /* This is really only for debugging. */
182 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
183 __libc_fatal ("illegal status in __nss_next");
185 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
189 if ((*ni
)->next
== NULL
)
196 *fctp
= __nss_lookup_function (*ni
, fct_name
);
199 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
200 && (*ni
)->next
!= NULL
);
202 return *fctp
!= NULL
? 0 : -1;
204 libc_hidden_def (__nss_next
)
208 __nss_configure_lookup (const char *dbname
, const char *service_line
)
210 service_user
*new_db
;
213 for (cnt
= 0; cnt
< sizeof databases
; ++cnt
)
215 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
220 __set_errno (EINVAL
);
225 if (cnt
== sizeof databases
)
227 __set_errno (EINVAL
);
231 /* Test whether it is really used. */
232 if (databases
[cnt
].dbp
== NULL
)
233 /* Nothing to do, but we could do. */
236 /* Try to generate new data. */
237 new_db
= nss_parse_service_list (service_line
);
240 /* Illegal service specification. */
241 __set_errno (EINVAL
);
245 /* Prevent multiple threads to change the service table. */
246 __libc_lock_lock (lock
);
248 /* Install new rules. */
249 *databases
[cnt
].dbp
= new_db
;
251 __libc_lock_unlock (lock
);
257 /* Comparison function for searching NI->known tree. */
259 known_compare (const void *p1
, const void *p2
)
261 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
262 *(const char *const *) p2
);
267 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
269 void **found
, *result
;
271 /* We now modify global data. Protect it. */
272 __libc_lock_lock (lock
);
274 /* Search the tree of functions previously requested. Data in the
275 tree are `known_function' structures, whose first member is a
276 `const char *', the lookup key. The search returns a pointer to
277 the tree node structure; the first member of the is a pointer to
278 our structure (i.e. what will be a `known_function'); since the
279 first member of that is the lookup key string, &FCT_NAME is close
280 enough to a pointer to our structure to use as a lookup key that
281 will be passed to `known_compare' (above). */
283 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
284 if (*found
!= &fct_name
)
285 /* The search found an existing structure in the tree. */
286 result
= ((known_function
*) *found
)->fct_ptr
;
289 /* This name was not known before. Now we have a node in the tree
290 (in the proper sorted position for FCT_NAME) that points to
291 &FCT_NAME instead of any real `known_function' structure.
292 Allocate a new structure and fill it in. */
294 known_function
*known
= malloc (sizeof *known
);
298 /* Oops. We can't instantiate this node properly.
299 Remove it from the tree. */
300 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
305 /* Point the tree node at this new structure. */
307 known
->fct_name
= fct_name
;
309 if (ni
->library
== NULL
)
311 /* This service has not yet been used. Fetch the service
312 library for it, creating a new one if need be. If there
313 is no service table from the file, this static variable
314 holds the head of the service_library list made from the
315 default configuration. */
316 static name_database default_table
;
317 ni
->library
= nss_new_service (service_table
?: &default_table
,
319 if (ni
->library
== NULL
)
321 /* This only happens when out of memory. */
323 goto remove_from_tree
;
327 #if !defined DO_STATIC_NSS || defined SHARED
328 if (ni
->library
->lib_handle
== NULL
)
330 /* Load the shared library. */
331 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
332 + strlen (__nss_shlib_revision
) + 1);
333 int saved_errno
= errno
;
334 char shlib_name
[shlen
];
336 /* Construct shared object name. */
337 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
341 __nss_shlib_revision
);
343 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
344 if (ni
->library
->lib_handle
== NULL
)
346 /* Failed to load the library. */
347 ni
->library
->lib_handle
= (void *) -1l;
348 __set_errno (saved_errno
);
352 if (ni
->library
->lib_handle
== (void *) -1l)
353 /* Library not found => function not found. */
357 /* Get the desired function. */
358 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
359 + strlen (fct_name
) + 1);
362 /* Construct the function name. */
363 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
368 /* Look up the symbol. */
369 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
372 /* We can't get function address dynamically in static linking. */
374 # define DEFINE_ENT(h,nm) \
375 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
376 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
377 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
378 # define DEFINE_GET(h,nm) \
379 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
380 # define DEFINE_GETBY(h,nm,ky) \
381 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
382 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
384 # include "function.def"
387 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
388 + strlen (fct_name
) + 1);
391 /* Construct the function name. */
392 __stpcpy (__stpcpy (__stpcpy (name
, ni
->library
->name
),
397 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
398 if (strcmp (tp
->fname
, name
) == 0)
406 /* Remember function pointer for later calls. Even if null, we
407 record it so a second try needn't search the library again. */
408 known
->fct_ptr
= result
;
412 /* Remove the lock. */
413 __libc_lock_unlock (lock
);
419 static name_database
*
421 nss_parse_file (const char *fname
)
424 name_database
*result
;
425 name_database_entry
*last
;
429 /* Open the configuration file. */
430 fp
= fopen (fname
, "r");
434 /* No threads use this stream. */
435 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
437 result
= (name_database
*) malloc (sizeof (name_database
));
441 result
->entry
= NULL
;
442 result
->library
= NULL
;
448 name_database_entry
*this;
451 n
= __getline (&line
, &len
, fp
);
454 if (line
[n
- 1] == '\n')
457 /* Because the file format does not know any form of quoting we
458 can search forward for the next '#' character and if found
459 make it terminating the line. */
460 *__strchrnul (line
, '#') = '\0';
462 /* If the line is blank it is ignored. */
466 /* Each line completely specifies the actions for a database. */
467 this = nss_getline (line
);
473 result
->entry
= this;
478 while (!feof_unlocked (fp
));
480 /* Free the buffer. */
482 /* Close configuration file. */
489 /* Read the source names:
490 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
492 static service_user
*
494 nss_parse_service_list (const char *line
)
496 service_user
*result
= NULL
, **nextp
= &result
;
500 service_user
*new_service
;
503 while (isspace (line
[0]))
506 /* No source specified. */
509 /* Read <source> identifier. */
511 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
517 new_service
= (service_user
*) malloc (sizeof (service_user
)
518 + (line
- name
+ 1));
519 if (new_service
== NULL
)
522 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
524 /* Set default actions. */
525 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
526 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
527 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
528 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
529 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
530 new_service
->library
= NULL
;
531 new_service
->known
= NULL
;
532 new_service
->next
= NULL
;
534 while (isspace (line
[0]))
539 /* Read criterions. */
542 while (line
[0] != '\0' && isspace (line
[0]));
547 enum nss_status status
;
548 lookup_actions action
;
550 /* Grok ! before name to mean all statii but that one. */
551 not = line
[0] == '!';
555 /* Read status name. */
557 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
561 /* Compare with known statii. */
562 if (line
- name
== 7)
564 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
565 status
= NSS_STATUS_SUCCESS
;
566 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
567 status
= NSS_STATUS_UNAVAIL
;
571 else if (line
- name
== 8)
573 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
574 status
= NSS_STATUS_NOTFOUND
;
575 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
576 status
= NSS_STATUS_TRYAGAIN
;
583 while (isspace (line
[0]))
589 while (isspace (line
[0]));
592 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
596 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
597 action
= NSS_ACTION_RETURN
;
598 else if (line
- name
== 8
599 && __strncasecmp (name
, "CONTINUE", 8) == 0)
600 action
= NSS_ACTION_CONTINUE
;
606 /* Save the current action setting for this status,
607 set them all to the given action, and reset this one. */
608 const lookup_actions save
= new_service
->actions
[2 + status
];
609 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
610 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
611 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
612 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
613 new_service
->actions
[2 + status
] = save
;
616 new_service
->actions
[2 + status
] = action
;
618 /* Skip white spaces. */
619 while (isspace (line
[0]))
622 while (line
[0] != ']');
628 *nextp
= new_service
;
629 nextp
= &new_service
->next
;
633 static name_database_entry
*
635 nss_getline (char *line
)
638 name_database_entry
*result
;
641 /* Ignore leading white spaces. ATTENTION: this is different from
642 what is implemented in Solaris. The Solaris man page says a line
643 beginning with a white space character is ignored. We regard
644 this as just another misfeature in Solaris. */
645 while (isspace (line
[0]))
648 /* Recognize `<database> ":"'. */
650 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
652 if (line
[0] == '\0' || name
== line
)
657 len
= strlen (name
) + 1;
659 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
663 /* Save the database name. */
664 memcpy (result
->name
, name
, len
);
666 /* Parse the list of services. */
667 result
->service
= nss_parse_service_list (line
);
674 static service_library
*
676 nss_new_service (name_database
*database
, const char *name
)
678 service_library
**currentp
= &database
->library
;
680 while (*currentp
!= NULL
)
682 if (strcmp ((*currentp
)->name
, name
) == 0)
684 currentp
= &(*currentp
)->next
;
687 /* We have to add the new service. */
688 *currentp
= (service_library
*) malloc (sizeof (service_library
));
689 if (*currentp
== NULL
)
692 (*currentp
)->name
= name
;
693 (*currentp
)->lib_handle
= NULL
;
694 (*currentp
)->next
= NULL
;
700 /* Called by nscd and nscd alone. */
702 __nss_disable_nscd (void)
704 /* Disable all uses of NSCD. */
705 __nss_not_use_nscd_passwd
= -1;
706 __nss_not_use_nscd_group
= -1;
707 __nss_not_use_nscd_hosts
= -1;
711 /* Free all resources if necessary. */
712 libc_freeres_fn (free_mem
)
714 name_database
*top
= service_table
;
715 name_database_entry
*entry
;
716 service_library
*library
;
719 /* Maybe we have not read the nsswitch.conf file. */
722 /* Don't disturb ongoing other threads (if there are any). */
723 service_table
= NULL
;
726 while (entry
!= NULL
)
728 name_database_entry
*olde
= entry
;
729 service_user
*service
= entry
->service
;
731 while (service
!= NULL
)
733 service_user
*olds
= service
;
735 if (service
->known
!= NULL
)
736 __tdestroy (service
->known
, free
);
738 service
= service
->next
;
746 library
= top
->library
;
747 while (library
!= NULL
)
749 service_library
*oldl
= library
;
751 __libc_dlclose (library
->lib_handle
);
753 library
= library
->next
;