1 /* Copyright (C) 1996-2012 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, see
17 <http://www.gnu.org/licenses/>. */
23 #include <bits/libc-lock.h>
26 #include <stdio_ext.h>
32 #include <netinet/ether.h>
36 #if !defined DO_STATIC_NSS || defined SHARED
37 # include <gnu/lib-names.h>
41 #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
72 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
74 /* Flags whether custom rules for database is set. */
75 bool __nss_database_custom
[NSS_DBSIDX_max
];
78 __libc_lock_define_initialized (static, lock
)
80 #if !defined DO_STATIC_NSS || defined SHARED
81 /* String with revision number of the shared object files. */
82 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
85 /* The root of the whole data base. */
86 static name_database
*service_table
;
88 /* List of default service lists that were generated by glibc because
89 /etc/nsswitch.conf did not provide a value.
90 The list is only maintained so we can free such service lists in
92 static name_database_entry
*defconfig_entries
;
96 /* Nonzero if this is the nscd process. */
98 /* The callback passed to the init functions when nscd is used. */
99 static void (*nscd_init_cb
) (size_t, struct traced_file
*);
103 /* -1 == database not found
104 0 == database entry pointer stored */
106 __nss_database_lookup (const char *database
, const char *alternate_name
,
107 const char *defconfig
, service_user
**ni
)
109 /* Prevent multiple threads to change the service table. */
110 __libc_lock_lock (lock
);
112 /* Reconsider database variable in case some other thread called
113 `__nss_configure_lookup' while we waited for the lock. */
116 __libc_lock_unlock (lock
);
120 /* Are we initialized yet? */
121 if (service_table
== NULL
)
122 /* Read config file. */
123 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
125 /* Test whether configuration data is available. */
126 if (service_table
!= NULL
)
128 /* Return first `service_user' entry for DATABASE. */
129 name_database_entry
*entry
;
131 /* XXX Could use some faster mechanism here. But each database is
132 only requested once and so this might not be critical. */
133 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
134 if (strcmp (database
, entry
->name
) == 0)
135 *ni
= entry
->service
;
137 if (*ni
== NULL
&& alternate_name
!= NULL
)
138 /* We haven't found an entry so far. Try to find it with the
140 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
141 if (strcmp (alternate_name
, entry
->name
) == 0)
142 *ni
= entry
->service
;
145 /* No configuration data is available, either because nsswitch.conf
146 doesn't exist or because it doesn't have a line for this database.
148 DEFCONFIG specifies the default service list for this database,
149 or null to use the most common default. */
152 *ni
= nss_parse_service_list (defconfig
153 ?: "nis [NOTFOUND=return] files");
156 /* Record the memory we've just allocated in defconfig_entries list,
157 so we can free it later. */
158 name_database_entry
*entry
;
160 /* Allocate ENTRY plus size of name (1 here). */
161 entry
= (name_database_entry
*) malloc (sizeof (*entry
) + 1);
165 entry
->next
= defconfig_entries
;
166 entry
->service
= *ni
;
167 entry
->name
[0] = '\0';
168 defconfig_entries
= entry
;
173 __libc_lock_unlock (lock
);
175 return *ni
!= NULL
? 0 : -1;
177 libc_hidden_def (__nss_database_lookup
)
184 __nss_lookup (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
187 *fctp
= __nss_lookup_function (*ni
, fct_name
);
188 if (*fctp
== NULL
&& fct2_name
!= NULL
)
189 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
192 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
193 && (*ni
)->next
!= NULL
)
197 *fctp
= __nss_lookup_function (*ni
, fct_name
);
198 if (*fctp
== NULL
&& fct2_name
!= NULL
)
199 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
202 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
204 libc_hidden_def (__nss_lookup
)
208 0 == adjusted for next function
211 __nss_next2 (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
212 void **fctp
, int status
, int all_values
)
216 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
217 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
218 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
219 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
224 /* This is really only for debugging. */
225 if (__builtin_expect (NSS_STATUS_TRYAGAIN
> status
226 || status
> NSS_STATUS_RETURN
, 0))
227 __libc_fatal ("illegal status in __nss_next");
229 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
233 if ((*ni
)->next
== NULL
)
240 *fctp
= __nss_lookup_function (*ni
, fct_name
);
241 if (*fctp
== NULL
&& fct2_name
!= NULL
)
242 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
245 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
246 && (*ni
)->next
!= NULL
);
248 return *fctp
!= NULL
? 0 : -1;
250 libc_hidden_def (__nss_next2
)
254 attribute_compat_text_section
255 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
258 return __nss_next2 (ni
, fct_name
, NULL
, fctp
, status
, all_values
);
263 __nss_configure_lookup (const char *dbname
, const char *service_line
)
265 service_user
*new_db
;
268 for (cnt
= 0; cnt
< ndatabases
; ++cnt
)
270 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
275 __set_errno (EINVAL
);
280 if (cnt
== ndatabases
)
282 __set_errno (EINVAL
);
286 /* Test whether it is really used. */
287 if (databases
[cnt
].dbp
== NULL
)
288 /* Nothing to do, but we could do. */
291 /* Try to generate new data. */
292 new_db
= nss_parse_service_list (service_line
);
295 /* Illegal service specification. */
296 __set_errno (EINVAL
);
300 /* Prevent multiple threads to change the service table. */
301 __libc_lock_lock (lock
);
303 /* Install new rules. */
304 *databases
[cnt
].dbp
= new_db
;
305 __nss_database_custom
[cnt
] = true;
307 __libc_lock_unlock (lock
);
313 /* Comparison function for searching NI->known tree. */
315 known_compare (const void *p1
, const void *p2
)
317 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
318 *(const char *const *) p2
);
322 #if !defined DO_STATIC_NSS || defined SHARED
325 nss_load_library (service_user
*ni
)
327 if (ni
->library
== NULL
)
329 /* This service has not yet been used. Fetch the service
330 library for it, creating a new one if need be. If there
331 is no service table from the file, this static variable
332 holds the head of the service_library list made from the
333 default configuration. */
334 static name_database default_table
;
335 ni
->library
= nss_new_service (service_table
?: &default_table
,
337 if (ni
->library
== NULL
)
341 if (ni
->library
->lib_handle
== NULL
)
343 /* Load the shared library. */
344 size_t shlen
= (7 + strlen (ni
->name
) + 3
345 + strlen (__nss_shlib_revision
) + 1);
346 int saved_errno
= errno
;
347 char shlib_name
[shlen
];
349 /* Construct shared object name. */
350 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
354 __nss_shlib_revision
);
356 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
357 if (ni
->library
->lib_handle
== NULL
)
359 /* Failed to load the library. */
360 ni
->library
->lib_handle
= (void *) -1l;
361 __set_errno (saved_errno
);
366 /* Call the init function when nscd is used. */
367 size_t initlen
= (5 + strlen (ni
->name
)
368 + strlen ("_init") + 1);
369 char init_name
[initlen
];
371 /* Construct the init function name. */
372 __stpcpy (__stpcpy (__stpcpy (init_name
,
377 /* Find the optional init function. */
378 void (*ifct
) (void (*) (size_t, struct traced_file
*))
379 = __libc_dlsym (ni
->library
->lib_handle
, init_name
);
382 void (*cb
) (size_t, struct traced_file
*) = nscd_init_cb
;
398 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
400 void **found
, *result
;
402 /* We now modify global data. Protect it. */
403 __libc_lock_lock (lock
);
405 /* Search the tree of functions previously requested. Data in the
406 tree are `known_function' structures, whose first member is a
407 `const char *', the lookup key. The search returns a pointer to
408 the tree node structure; the first member of the is a pointer to
409 our structure (i.e. what will be a `known_function'); since the
410 first member of that is the lookup key string, &FCT_NAME is close
411 enough to a pointer to our structure to use as a lookup key that
412 will be passed to `known_compare' (above). */
414 found
= __tsearch (&fct_name
, &ni
->known
, &known_compare
);
416 /* This means out-of-memory. */
418 else if (*found
!= &fct_name
)
420 /* The search found an existing structure in the tree. */
421 result
= ((known_function
*) *found
)->fct_ptr
;
423 PTR_DEMANGLE (result
);
428 /* This name was not known before. Now we have a node in the tree
429 (in the proper sorted position for FCT_NAME) that points to
430 &FCT_NAME instead of any real `known_function' structure.
431 Allocate a new structure and fill it in. */
433 known_function
*known
= malloc (sizeof *known
);
437 /* Oops. We can't instantiate this node properly.
438 Remove it from the tree. */
439 __tdelete (&fct_name
, &ni
->known
, &known_compare
);
445 /* Point the tree node at this new structure. */
447 known
->fct_name
= fct_name
;
449 #if !defined DO_STATIC_NSS || defined SHARED
450 /* Load the appropriate library. */
451 if (nss_load_library (ni
) != 0)
452 /* This only happens when out of memory. */
453 goto remove_from_tree
;
455 if (ni
->library
->lib_handle
== (void *) -1l)
456 /* Library not found => function not found. */
460 /* Get the desired function. */
461 size_t namlen
= (5 + strlen (ni
->name
) + 1
462 + strlen (fct_name
) + 1);
465 /* Construct the function name. */
466 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
471 /* Look up the symbol. */
472 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
475 /* We can't get function address dynamically in static linking. */
477 # define DEFINE_ENT(h,nm) \
478 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
479 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
480 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
481 # define DEFINE_GET(h,nm) \
482 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
483 # define DEFINE_GETBY(h,nm,ky) \
484 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
485 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
487 # include "function.def"
490 size_t namlen
= (5 + strlen (ni
->name
) + 1
491 + strlen (fct_name
) + 1);
494 /* Construct the function name. */
495 __stpcpy (__stpcpy (__stpcpy (name
, ni
->name
),
500 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
501 if (strcmp (tp
->fname
, name
) == 0)
509 /* Remember function pointer for later calls. Even if null, we
510 record it so a second try needn't search the library again. */
511 known
->fct_ptr
= result
;
513 PTR_MANGLE (known
->fct_ptr
);
518 /* Remove the lock. */
519 __libc_lock_unlock (lock
);
523 libc_hidden_def (__nss_lookup_function
)
526 static name_database
*
528 nss_parse_file (const char *fname
)
531 name_database
*result
;
532 name_database_entry
*last
;
536 /* Open the configuration file. */
537 fp
= fopen (fname
, "rce");
541 /* No threads use this stream. */
542 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
544 result
= (name_database
*) malloc (sizeof (name_database
));
551 result
->entry
= NULL
;
552 result
->library
= NULL
;
558 name_database_entry
*this;
561 n
= __getline (&line
, &len
, fp
);
564 if (line
[n
- 1] == '\n')
567 /* Because the file format does not know any form of quoting we
568 can search forward for the next '#' character and if found
569 make it terminating the line. */
570 *__strchrnul (line
, '#') = '\0';
572 /* If the line is blank it is ignored. */
576 /* Each line completely specifies the actions for a database. */
577 this = nss_getline (line
);
583 result
->entry
= this;
588 while (!feof_unlocked (fp
));
590 /* Free the buffer. */
592 /* Close configuration file. */
599 /* Read the source names:
600 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
602 static service_user
*
604 nss_parse_service_list (const char *line
)
606 service_user
*result
= NULL
, **nextp
= &result
;
610 service_user
*new_service
;
613 while (isspace (line
[0]))
616 /* No source specified. */
619 /* Read <source> identifier. */
621 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
627 new_service
= (service_user
*) malloc (sizeof (service_user
)
628 + (line
- name
+ 1));
629 if (new_service
== NULL
)
632 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
634 /* Set default actions. */
635 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
636 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
637 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
638 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
639 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
640 new_service
->library
= NULL
;
641 new_service
->known
= NULL
;
642 new_service
->next
= NULL
;
644 while (isspace (line
[0]))
649 /* Read criterions. */
652 while (line
[0] != '\0' && isspace (line
[0]));
657 enum nss_status status
;
658 lookup_actions action
;
660 /* Grok ! before name to mean all statii but that one. */
661 not = line
[0] == '!';
665 /* Read status name. */
667 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
671 /* Compare with known statii. */
672 if (line
- name
== 7)
674 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
675 status
= NSS_STATUS_SUCCESS
;
676 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
677 status
= NSS_STATUS_UNAVAIL
;
681 else if (line
- name
== 8)
683 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
684 status
= NSS_STATUS_NOTFOUND
;
685 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
686 status
= NSS_STATUS_TRYAGAIN
;
693 while (isspace (line
[0]))
699 while (isspace (line
[0]));
702 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
706 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
707 action
= NSS_ACTION_RETURN
;
708 else if (line
- name
== 8
709 && __strncasecmp (name
, "CONTINUE", 8) == 0)
710 action
= NSS_ACTION_CONTINUE
;
716 /* Save the current action setting for this status,
717 set them all to the given action, and reset this one. */
718 const lookup_actions save
= new_service
->actions
[2 + status
];
719 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
720 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
721 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
722 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
723 new_service
->actions
[2 + status
] = save
;
726 new_service
->actions
[2 + status
] = action
;
728 /* Skip white spaces. */
729 while (isspace (line
[0]))
732 while (line
[0] != ']');
738 *nextp
= new_service
;
739 nextp
= &new_service
->next
;
748 static name_database_entry
*
750 nss_getline (char *line
)
753 name_database_entry
*result
;
756 /* Ignore leading white spaces. ATTENTION: this is different from
757 what is implemented in Solaris. The Solaris man page says a line
758 beginning with a white space character is ignored. We regard
759 this as just another misfeature in Solaris. */
760 while (isspace (line
[0]))
763 /* Recognize `<database> ":"'. */
765 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
767 if (line
[0] == '\0' || name
== line
)
772 len
= strlen (name
) + 1;
774 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
778 /* Save the database name. */
779 memcpy (result
->name
, name
, len
);
781 /* Parse the list of services. */
782 result
->service
= nss_parse_service_list (line
);
789 static service_library
*
791 nss_new_service (name_database
*database
, const char *name
)
793 service_library
**currentp
= &database
->library
;
795 while (*currentp
!= NULL
)
797 if (strcmp ((*currentp
)->name
, name
) == 0)
799 currentp
= &(*currentp
)->next
;
802 /* We have to add the new service. */
803 *currentp
= (service_library
*) malloc (sizeof (service_library
));
804 if (*currentp
== NULL
)
807 (*currentp
)->name
= name
;
808 (*currentp
)->lib_handle
= NULL
;
809 (*currentp
)->next
= NULL
;
815 #if defined SHARED && defined USE_NSCD
816 /* Load all libraries for the service. */
818 nss_load_all_libraries (const char *service
, const char *def
)
820 service_user
*ni
= NULL
;
822 if (__nss_database_lookup (service
, NULL
, def
, &ni
) == 0)
825 nss_load_library (ni
);
831 /* Called by nscd and nscd alone. */
833 __nss_disable_nscd (void (*cb
) (size_t, struct traced_file
*))
841 /* Find all the relevant modules so that the init functions are called. */
842 nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
843 nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
844 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
845 nss_load_all_libraries ("services", NULL
);
847 /* Disable all uses of NSCD. */
848 __nss_not_use_nscd_passwd
= -1;
849 __nss_not_use_nscd_group
= -1;
850 __nss_not_use_nscd_hosts
= -1;
851 __nss_not_use_nscd_services
= -1;
852 __nss_not_use_nscd_netgroup
= -1;
857 free_database_entries (name_database_entry
*entry
)
859 while (entry
!= NULL
)
861 name_database_entry
*olde
= entry
;
862 service_user
*service
= entry
->service
;
864 while (service
!= NULL
)
866 service_user
*olds
= service
;
868 if (service
->known
!= NULL
)
869 __tdestroy (service
->known
, free
);
871 service
= service
->next
;
880 /* Free all resources if necessary. */
881 libc_freeres_fn (free_defconfig
)
883 name_database_entry
*entry
= defconfig_entries
;
886 /* defconfig was not used. */
889 /* Don't disturb ongoing other threads (if there are any). */
890 defconfig_entries
= NULL
;
892 free_database_entries (entry
);
895 libc_freeres_fn (free_mem
)
897 name_database
*top
= service_table
;
898 service_library
*library
;
901 /* Maybe we have not read the nsswitch.conf file. */
904 /* Don't disturb ongoing other threads (if there are any). */
905 service_table
= NULL
;
907 free_database_entries (top
->entry
);
909 library
= top
->library
;
910 while (library
!= NULL
)
912 service_library
*oldl
= library
;
914 if (library
->lib_handle
&& library
->lib_handle
!= (void *) -1l)
915 __libc_dlclose (library
->lib_handle
);
917 library
= library
->next
;