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
;
418 PTR_DEMANGLE (result
);
422 /* This name was not known before. Now we have a node in the tree
423 (in the proper sorted position for FCT_NAME) that points to
424 &FCT_NAME instead of any real `known_function' structure.
425 Allocate a new structure and fill it in. */
427 known_function
*known
= malloc (sizeof *known
);
431 /* Oops. We can't instantiate this node properly.
432 Remove it from the tree. */
433 __tdelete (&fct_name
, &ni
->known
, &known_compare
);
439 /* Point the tree node at this new structure. */
441 known
->fct_name
= fct_name
;
443 #if !defined DO_STATIC_NSS || defined SHARED
444 /* Load the appropriate library. */
445 if (nss_load_library (ni
) != 0)
446 /* This only happens when out of memory. */
447 goto remove_from_tree
;
449 if (ni
->library
->lib_handle
== (void *) -1l)
450 /* Library not found => function not found. */
454 /* Get the desired function. */
455 size_t namlen
= (5 + strlen (ni
->name
) + 1
456 + strlen (fct_name
) + 1);
459 /* Construct the function name. */
460 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
465 /* Look up the symbol. */
466 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
469 /* We can't get function address dynamically in static linking. */
471 # define DEFINE_ENT(h,nm) \
472 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
473 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
474 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
475 # define DEFINE_GET(h,nm) \
476 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
477 # define DEFINE_GETBY(h,nm,ky) \
478 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
479 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
481 # include "function.def"
484 size_t namlen
= (5 + strlen (ni
->name
) + 1
485 + strlen (fct_name
) + 1);
488 /* Construct the function name. */
489 __stpcpy (__stpcpy (__stpcpy (name
, ni
->name
),
494 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
495 if (strcmp (tp
->fname
, name
) == 0)
503 /* Remember function pointer for later calls. Even if null, we
504 record it so a second try needn't search the library again. */
505 known
->fct_ptr
= result
;
506 PTR_MANGLE (known
->fct_ptr
);
510 /* Remove the lock. */
511 __libc_lock_unlock (lock
);
515 libc_hidden_def (__nss_lookup_function
)
518 static name_database
*
520 nss_parse_file (const char *fname
)
523 name_database
*result
;
524 name_database_entry
*last
;
528 /* Open the configuration file. */
529 fp
= fopen (fname
, "rce");
533 /* No threads use this stream. */
534 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
536 result
= (name_database
*) malloc (sizeof (name_database
));
543 result
->entry
= NULL
;
544 result
->library
= NULL
;
550 name_database_entry
*this;
553 n
= __getline (&line
, &len
, fp
);
556 if (line
[n
- 1] == '\n')
559 /* Because the file format does not know any form of quoting we
560 can search forward for the next '#' character and if found
561 make it terminating the line. */
562 *__strchrnul (line
, '#') = '\0';
564 /* If the line is blank it is ignored. */
568 /* Each line completely specifies the actions for a database. */
569 this = nss_getline (line
);
575 result
->entry
= this;
580 while (!feof_unlocked (fp
));
582 /* Free the buffer. */
584 /* Close configuration file. */
591 /* Read the source names:
592 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
594 static service_user
*
596 nss_parse_service_list (const char *line
)
598 service_user
*result
= NULL
, **nextp
= &result
;
602 service_user
*new_service
;
605 while (isspace (line
[0]))
608 /* No source specified. */
611 /* Read <source> identifier. */
613 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
619 new_service
= (service_user
*) malloc (sizeof (service_user
)
620 + (line
- name
+ 1));
621 if (new_service
== NULL
)
624 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
626 /* Set default actions. */
627 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
628 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
629 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
630 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
631 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
632 new_service
->library
= NULL
;
633 new_service
->known
= NULL
;
634 new_service
->next
= NULL
;
636 while (isspace (line
[0]))
641 /* Read criterions. */
644 while (line
[0] != '\0' && isspace (line
[0]));
649 enum nss_status status
;
650 lookup_actions action
;
652 /* Grok ! before name to mean all statii but that one. */
653 not = line
[0] == '!';
657 /* Read status name. */
659 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
663 /* Compare with known statii. */
664 if (line
- name
== 7)
666 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
667 status
= NSS_STATUS_SUCCESS
;
668 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
669 status
= NSS_STATUS_UNAVAIL
;
673 else if (line
- name
== 8)
675 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
676 status
= NSS_STATUS_NOTFOUND
;
677 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
678 status
= NSS_STATUS_TRYAGAIN
;
685 while (isspace (line
[0]))
691 while (isspace (line
[0]));
694 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
698 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
699 action
= NSS_ACTION_RETURN
;
700 else if (line
- name
== 8
701 && __strncasecmp (name
, "CONTINUE", 8) == 0)
702 action
= NSS_ACTION_CONTINUE
;
708 /* Save the current action setting for this status,
709 set them all to the given action, and reset this one. */
710 const lookup_actions save
= new_service
->actions
[2 + status
];
711 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
712 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
713 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
714 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
715 new_service
->actions
[2 + status
] = save
;
718 new_service
->actions
[2 + status
] = action
;
720 /* Skip white spaces. */
721 while (isspace (line
[0]))
724 while (line
[0] != ']');
730 *nextp
= new_service
;
731 nextp
= &new_service
->next
;
740 static name_database_entry
*
742 nss_getline (char *line
)
745 name_database_entry
*result
;
748 /* Ignore leading white spaces. ATTENTION: this is different from
749 what is implemented in Solaris. The Solaris man page says a line
750 beginning with a white space character is ignored. We regard
751 this as just another misfeature in Solaris. */
752 while (isspace (line
[0]))
755 /* Recognize `<database> ":"'. */
757 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
759 if (line
[0] == '\0' || name
== line
)
764 len
= strlen (name
) + 1;
766 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
770 /* Save the database name. */
771 memcpy (result
->name
, name
, len
);
773 /* Parse the list of services. */
774 result
->service
= nss_parse_service_list (line
);
781 static service_library
*
783 nss_new_service (name_database
*database
, const char *name
)
785 service_library
**currentp
= &database
->library
;
787 while (*currentp
!= NULL
)
789 if (strcmp ((*currentp
)->name
, name
) == 0)
791 currentp
= &(*currentp
)->next
;
794 /* We have to add the new service. */
795 *currentp
= (service_library
*) malloc (sizeof (service_library
));
796 if (*currentp
== NULL
)
799 (*currentp
)->name
= name
;
800 (*currentp
)->lib_handle
= NULL
;
801 (*currentp
)->next
= NULL
;
808 /* Load all libraries for the service. */
810 nss_load_all_libraries (const char *service
, const char *def
)
812 service_user
*ni
= NULL
;
814 if (__nss_database_lookup (service
, NULL
, def
, &ni
) == 0)
817 nss_load_library (ni
);
823 /* Called by nscd and nscd alone. */
825 __nss_disable_nscd (void (*cb
) (size_t, struct traced_file
*))
833 /* Find all the relevant modules so that the init functions are called. */
834 nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
835 nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
836 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
837 nss_load_all_libraries ("services", NULL
);
839 /* Disable all uses of NSCD. */
840 __nss_not_use_nscd_passwd
= -1;
841 __nss_not_use_nscd_group
= -1;
842 __nss_not_use_nscd_hosts
= -1;
843 __nss_not_use_nscd_services
= -1;
844 __nss_not_use_nscd_netgroup
= -1;
849 free_database_entries (name_database_entry
*entry
)
851 while (entry
!= NULL
)
853 name_database_entry
*olde
= entry
;
854 service_user
*service
= entry
->service
;
856 while (service
!= NULL
)
858 service_user
*olds
= service
;
860 if (service
->known
!= NULL
)
861 __tdestroy (service
->known
, free
);
863 service
= service
->next
;
872 /* Free all resources if necessary. */
873 libc_freeres_fn (free_defconfig
)
875 name_database_entry
*entry
= defconfig_entries
;
878 /* defconfig was not used. */
881 /* Don't disturb ongoing other threads (if there are any). */
882 defconfig_entries
= NULL
;
884 free_database_entries (entry
);
887 libc_freeres_fn (free_mem
)
889 name_database
*top
= service_table
;
890 service_library
*library
;
893 /* Maybe we have not read the nsswitch.conf file. */
896 /* Don't disturb ongoing other threads (if there are any). */
897 service_table
= NULL
;
899 free_database_entries (top
->entry
);
901 library
= top
->library
;
902 while (library
!= NULL
)
904 service_library
*oldl
= library
;
906 if (library
->lib_handle
&& library
->lib_handle
!= (void *) -1l)
907 __libc_dlclose (library
->lib_handle
);
909 library
= library
->next
;