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
;
95 /* Nonzero if this is the nscd process. */
97 /* The callback passed to the init functions when nscd is used. */
98 static void (*nscd_init_cb
) (size_t, struct traced_file
*);
101 /* -1 == database not found
102 0 == database entry pointer stored */
104 __nss_database_lookup (const char *database
, const char *alternate_name
,
105 const char *defconfig
, service_user
**ni
)
107 /* Prevent multiple threads to change the service table. */
108 __libc_lock_lock (lock
);
110 /* Reconsider database variable in case some other thread called
111 `__nss_configure_lookup' while we waited for the lock. */
114 __libc_lock_unlock (lock
);
118 /* Are we initialized yet? */
119 if (service_table
== NULL
)
120 /* Read config file. */
121 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
123 /* Test whether configuration data is available. */
124 if (service_table
!= NULL
)
126 /* Return first `service_user' entry for DATABASE. */
127 name_database_entry
*entry
;
129 /* XXX Could use some faster mechanism here. But each database is
130 only requested once and so this might not be critical. */
131 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
132 if (strcmp (database
, entry
->name
) == 0)
133 *ni
= entry
->service
;
135 if (*ni
== NULL
&& alternate_name
!= NULL
)
136 /* We haven't found an entry so far. Try to find it with the
138 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
139 if (strcmp (alternate_name
, entry
->name
) == 0)
140 *ni
= entry
->service
;
143 /* No configuration data is available, either because nsswitch.conf
144 doesn't exist or because it doesn't have a line for this database.
146 DEFCONFIG specifies the default service list for this database,
147 or null to use the most common default. */
150 *ni
= nss_parse_service_list (defconfig
151 ?: "nis [NOTFOUND=return] files");
154 /* Record the memory we've just allocated in defconfig_entries list,
155 so we can free it later. */
156 name_database_entry
*entry
;
158 /* Allocate ENTRY plus size of name (1 here). */
159 entry
= (name_database_entry
*) malloc (sizeof (*entry
) + 1);
163 entry
->next
= defconfig_entries
;
164 entry
->service
= *ni
;
165 entry
->name
[0] = '\0';
166 defconfig_entries
= entry
;
171 __libc_lock_unlock (lock
);
173 return *ni
!= NULL
? 0 : -1;
175 libc_hidden_def (__nss_database_lookup
)
182 __nss_lookup (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
185 *fctp
= __nss_lookup_function (*ni
, fct_name
);
186 if (*fctp
== NULL
&& fct2_name
!= NULL
)
187 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
190 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
191 && (*ni
)->next
!= NULL
)
195 *fctp
= __nss_lookup_function (*ni
, fct_name
);
196 if (*fctp
== NULL
&& fct2_name
!= NULL
)
197 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
200 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
202 libc_hidden_def (__nss_lookup
)
206 0 == adjusted for next function
209 __nss_next2 (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
210 void **fctp
, int status
, int all_values
)
214 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
215 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
216 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
217 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
222 /* This is really only for debugging. */
223 if (__builtin_expect (NSS_STATUS_TRYAGAIN
> status
224 || status
> NSS_STATUS_RETURN
, 0))
225 __libc_fatal ("illegal status in __nss_next");
227 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
231 if ((*ni
)->next
== NULL
)
238 *fctp
= __nss_lookup_function (*ni
, fct_name
);
239 if (*fctp
== NULL
&& fct2_name
!= NULL
)
240 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
243 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
244 && (*ni
)->next
!= NULL
);
246 return *fctp
!= NULL
? 0 : -1;
248 libc_hidden_def (__nss_next2
)
252 attribute_compat_text_section
253 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
256 return __nss_next2 (ni
, fct_name
, NULL
, fctp
, status
, all_values
);
261 __nss_configure_lookup (const char *dbname
, const char *service_line
)
263 service_user
*new_db
;
266 for (cnt
= 0; cnt
< ndatabases
; ++cnt
)
268 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
273 __set_errno (EINVAL
);
278 if (cnt
== ndatabases
)
280 __set_errno (EINVAL
);
284 /* Test whether it is really used. */
285 if (databases
[cnt
].dbp
== NULL
)
286 /* Nothing to do, but we could do. */
289 /* Try to generate new data. */
290 new_db
= nss_parse_service_list (service_line
);
293 /* Illegal service specification. */
294 __set_errno (EINVAL
);
298 /* Prevent multiple threads to change the service table. */
299 __libc_lock_lock (lock
);
301 /* Install new rules. */
302 *databases
[cnt
].dbp
= new_db
;
303 __nss_database_custom
[cnt
] = true;
305 __libc_lock_unlock (lock
);
311 /* Comparison function for searching NI->known tree. */
313 known_compare (const void *p1
, const void *p2
)
315 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
316 *(const char *const *) p2
);
320 #if !defined DO_STATIC_NSS || defined SHARED
323 nss_load_library (service_user
*ni
)
325 if (ni
->library
== NULL
)
327 /* This service has not yet been used. Fetch the service
328 library for it, creating a new one if need be. If there
329 is no service table from the file, this static variable
330 holds the head of the service_library list made from the
331 default configuration. */
332 static name_database default_table
;
333 ni
->library
= nss_new_service (service_table
?: &default_table
,
335 if (ni
->library
== NULL
)
339 if (ni
->library
->lib_handle
== NULL
)
341 /* Load the shared library. */
342 size_t shlen
= (7 + strlen (ni
->name
) + 3
343 + strlen (__nss_shlib_revision
) + 1);
344 int saved_errno
= errno
;
345 char shlib_name
[shlen
];
347 /* Construct shared object name. */
348 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
352 __nss_shlib_revision
);
354 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
355 if (ni
->library
->lib_handle
== NULL
)
357 /* Failed to load the library. */
358 ni
->library
->lib_handle
= (void *) -1l;
359 __set_errno (saved_errno
);
363 /* Call the init function when nscd is used. */
364 size_t initlen
= (5 + strlen (ni
->name
)
365 + strlen ("_init") + 1);
366 char init_name
[initlen
];
368 /* Construct the init function name. */
369 __stpcpy (__stpcpy (__stpcpy (init_name
,
374 /* Find the optional init function. */
375 void (*ifct
) (void (*) (size_t, struct traced_file
*))
376 = __libc_dlsym (ni
->library
->lib_handle
, init_name
);
379 void (*cb
) (size_t, struct traced_file
*) = nscd_init_cb
;
394 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
396 void **found
, *result
;
398 /* We now modify global data. Protect it. */
399 __libc_lock_lock (lock
);
401 /* Search the tree of functions previously requested. Data in the
402 tree are `known_function' structures, whose first member is a
403 `const char *', the lookup key. The search returns a pointer to
404 the tree node structure; the first member of the is a pointer to
405 our structure (i.e. what will be a `known_function'); since the
406 first member of that is the lookup key string, &FCT_NAME is close
407 enough to a pointer to our structure to use as a lookup key that
408 will be passed to `known_compare' (above). */
410 found
= __tsearch (&fct_name
, &ni
->known
, &known_compare
);
412 /* This means out-of-memory. */
414 else if (*found
!= &fct_name
)
416 /* The search found an existing structure in the tree. */
417 result
= ((known_function
*) *found
)->fct_ptr
;
419 PTR_DEMANGLE (result
);
424 /* This name was not known before. Now we have a node in the tree
425 (in the proper sorted position for FCT_NAME) that points to
426 &FCT_NAME instead of any real `known_function' structure.
427 Allocate a new structure and fill it in. */
429 known_function
*known
= malloc (sizeof *known
);
433 /* Oops. We can't instantiate this node properly.
434 Remove it from the tree. */
435 __tdelete (&fct_name
, &ni
->known
, &known_compare
);
441 /* Point the tree node at this new structure. */
443 known
->fct_name
= fct_name
;
445 #if !defined DO_STATIC_NSS || defined SHARED
446 /* Load the appropriate library. */
447 if (nss_load_library (ni
) != 0)
448 /* This only happens when out of memory. */
449 goto remove_from_tree
;
451 if (ni
->library
->lib_handle
== (void *) -1l)
452 /* Library not found => function not found. */
456 /* Get the desired function. */
457 size_t namlen
= (5 + strlen (ni
->name
) + 1
458 + strlen (fct_name
) + 1);
461 /* Construct the function name. */
462 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
467 /* Look up the symbol. */
468 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
471 /* We can't get function address dynamically in static linking. */
473 # define DEFINE_ENT(h,nm) \
474 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
475 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
476 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
477 # define DEFINE_GET(h,nm) \
478 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
479 # define DEFINE_GETBY(h,nm,ky) \
480 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
481 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
483 # include "function.def"
486 size_t namlen
= (5 + strlen (ni
->name
) + 1
487 + strlen (fct_name
) + 1);
490 /* Construct the function name. */
491 __stpcpy (__stpcpy (__stpcpy (name
, ni
->name
),
496 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
497 if (strcmp (tp
->fname
, name
) == 0)
505 /* Remember function pointer for later calls. Even if null, we
506 record it so a second try needn't search the library again. */
507 known
->fct_ptr
= result
;
509 PTR_MANGLE (known
->fct_ptr
);
514 /* Remove the lock. */
515 __libc_lock_unlock (lock
);
519 libc_hidden_def (__nss_lookup_function
)
522 static name_database
*
524 nss_parse_file (const char *fname
)
527 name_database
*result
;
528 name_database_entry
*last
;
532 /* Open the configuration file. */
533 fp
= fopen (fname
, "rce");
537 /* No threads use this stream. */
538 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
540 result
= (name_database
*) malloc (sizeof (name_database
));
547 result
->entry
= NULL
;
548 result
->library
= NULL
;
554 name_database_entry
*this;
557 n
= __getline (&line
, &len
, fp
);
560 if (line
[n
- 1] == '\n')
563 /* Because the file format does not know any form of quoting we
564 can search forward for the next '#' character and if found
565 make it terminating the line. */
566 *__strchrnul (line
, '#') = '\0';
568 /* If the line is blank it is ignored. */
572 /* Each line completely specifies the actions for a database. */
573 this = nss_getline (line
);
579 result
->entry
= this;
584 while (!feof_unlocked (fp
));
586 /* Free the buffer. */
588 /* Close configuration file. */
595 /* Read the source names:
596 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
598 static service_user
*
600 nss_parse_service_list (const char *line
)
602 service_user
*result
= NULL
, **nextp
= &result
;
606 service_user
*new_service
;
609 while (isspace (line
[0]))
612 /* No source specified. */
615 /* Read <source> identifier. */
617 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
623 new_service
= (service_user
*) malloc (sizeof (service_user
)
624 + (line
- name
+ 1));
625 if (new_service
== NULL
)
628 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
630 /* Set default actions. */
631 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
632 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
633 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
634 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
635 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
636 new_service
->library
= NULL
;
637 new_service
->known
= NULL
;
638 new_service
->next
= NULL
;
640 while (isspace (line
[0]))
645 /* Read criterions. */
648 while (line
[0] != '\0' && isspace (line
[0]));
653 enum nss_status status
;
654 lookup_actions action
;
656 /* Grok ! before name to mean all statii but that one. */
657 not = line
[0] == '!';
661 /* Read status name. */
663 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
667 /* Compare with known statii. */
668 if (line
- name
== 7)
670 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
671 status
= NSS_STATUS_SUCCESS
;
672 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
673 status
= NSS_STATUS_UNAVAIL
;
677 else if (line
- name
== 8)
679 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
680 status
= NSS_STATUS_NOTFOUND
;
681 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
682 status
= NSS_STATUS_TRYAGAIN
;
689 while (isspace (line
[0]))
695 while (isspace (line
[0]));
698 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
702 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
703 action
= NSS_ACTION_RETURN
;
704 else if (line
- name
== 8
705 && __strncasecmp (name
, "CONTINUE", 8) == 0)
706 action
= NSS_ACTION_CONTINUE
;
712 /* Save the current action setting for this status,
713 set them all to the given action, and reset this one. */
714 const lookup_actions save
= new_service
->actions
[2 + status
];
715 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
716 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
717 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
718 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
719 new_service
->actions
[2 + status
] = save
;
722 new_service
->actions
[2 + status
] = action
;
724 /* Skip white spaces. */
725 while (isspace (line
[0]))
728 while (line
[0] != ']');
734 *nextp
= new_service
;
735 nextp
= &new_service
->next
;
744 static name_database_entry
*
746 nss_getline (char *line
)
749 name_database_entry
*result
;
752 /* Ignore leading white spaces. ATTENTION: this is different from
753 what is implemented in Solaris. The Solaris man page says a line
754 beginning with a white space character is ignored. We regard
755 this as just another misfeature in Solaris. */
756 while (isspace (line
[0]))
759 /* Recognize `<database> ":"'. */
761 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
763 if (line
[0] == '\0' || name
== line
)
768 len
= strlen (name
) + 1;
770 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
774 /* Save the database name. */
775 memcpy (result
->name
, name
, len
);
777 /* Parse the list of services. */
778 result
->service
= nss_parse_service_list (line
);
785 static service_library
*
787 nss_new_service (name_database
*database
, const char *name
)
789 service_library
**currentp
= &database
->library
;
791 while (*currentp
!= NULL
)
793 if (strcmp ((*currentp
)->name
, name
) == 0)
795 currentp
= &(*currentp
)->next
;
798 /* We have to add the new service. */
799 *currentp
= (service_library
*) malloc (sizeof (service_library
));
800 if (*currentp
== NULL
)
803 (*currentp
)->name
= name
;
804 (*currentp
)->lib_handle
= NULL
;
805 (*currentp
)->next
= NULL
;
812 /* Load all libraries for the service. */
814 nss_load_all_libraries (const char *service
, const char *def
)
816 service_user
*ni
= NULL
;
818 if (__nss_database_lookup (service
, NULL
, def
, &ni
) == 0)
821 nss_load_library (ni
);
827 /* Called by nscd and nscd alone. */
829 __nss_disable_nscd (void (*cb
) (size_t, struct traced_file
*))
837 /* Find all the relevant modules so that the init functions are called. */
838 nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
839 nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
840 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
841 nss_load_all_libraries ("services", NULL
);
843 /* Disable all uses of NSCD. */
844 __nss_not_use_nscd_passwd
= -1;
845 __nss_not_use_nscd_group
= -1;
846 __nss_not_use_nscd_hosts
= -1;
847 __nss_not_use_nscd_services
= -1;
848 __nss_not_use_nscd_netgroup
= -1;
853 free_database_entries (name_database_entry
*entry
)
855 while (entry
!= NULL
)
857 name_database_entry
*olde
= entry
;
858 service_user
*service
= entry
->service
;
860 while (service
!= NULL
)
862 service_user
*olds
= service
;
864 if (service
->known
!= NULL
)
865 __tdestroy (service
->known
, free
);
867 service
= service
->next
;
876 /* Free all resources if necessary. */
877 libc_freeres_fn (free_defconfig
)
879 name_database_entry
*entry
= defconfig_entries
;
882 /* defconfig was not used. */
885 /* Don't disturb ongoing other threads (if there are any). */
886 defconfig_entries
= NULL
;
888 free_database_entries (entry
);
891 libc_freeres_fn (free_mem
)
893 name_database
*top
= service_table
;
894 service_library
*library
;
897 /* Maybe we have not read the nsswitch.conf file. */
900 /* Don't disturb ongoing other threads (if there are any). */
901 service_table
= NULL
;
903 free_database_entries (top
->entry
);
905 library
= top
->library
;
906 while (library
!= NULL
)
908 service_library
*oldl
= library
;
910 if (library
->lib_handle
&& library
->lib_handle
!= (void *) -1l)
911 __libc_dlclose (library
->lib_handle
);
913 library
= library
->next
;