1 /* Copyright (C) 1996-2016 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 <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 #if !defined DO_STATIC_NSS || defined SHARED
50 static service_library
*nss_new_service (name_database
*database
,
51 const char *name
) internal_function
;
55 /* Declare external database variables. */
56 #define DEFINE_DATABASE(name) \
57 extern service_user *__nss_##name##_database attribute_hidden; \
58 weak_extern (__nss_##name##_database)
59 #include "databases.def"
60 #undef DEFINE_DATABASE
62 /* Structure to map database name to variable. */
69 #define DEFINE_DATABASE(name) \
70 { #name, &__nss_##name##_database },
71 #include "databases.def"
72 #undef DEFINE_DATABASE
74 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
76 /* Flags whether custom rules for database is set. */
77 bool __nss_database_custom
[NSS_DBSIDX_max
];
80 __libc_lock_define_initialized (static, lock
)
82 #if !defined DO_STATIC_NSS || defined SHARED
83 /* String with revision number of the shared object files. */
84 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
87 /* The root of the whole data base. */
88 static name_database
*service_table
;
90 /* List of default service lists that were generated by glibc because
91 /etc/nsswitch.conf did not provide a value.
92 The list is only maintained so we can free such service lists in
94 static name_database_entry
*defconfig_entries
;
98 /* Nonzero if this is the nscd process. */
100 /* The callback passed to the init functions when nscd is used. */
101 static void (*nscd_init_cb
) (size_t, struct traced_file
*);
105 /* -1 == database not found
106 0 == database entry pointer stored */
108 __nss_database_lookup (const char *database
, const char *alternate_name
,
109 const char *defconfig
, service_user
**ni
)
111 /* Prevent multiple threads to change the service table. */
112 __libc_lock_lock (lock
);
114 /* Reconsider database variable in case some other thread called
115 `__nss_configure_lookup' while we waited for the lock. */
118 __libc_lock_unlock (lock
);
122 /* Are we initialized yet? */
123 if (service_table
== NULL
)
124 /* Read config file. */
125 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
127 /* Test whether configuration data is available. */
128 if (service_table
!= NULL
)
130 /* Return first `service_user' entry for DATABASE. */
131 name_database_entry
*entry
;
133 /* XXX Could use some faster mechanism here. But each database is
134 only requested once and so this might not be critical. */
135 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
136 if (strcmp (database
, entry
->name
) == 0)
137 *ni
= entry
->service
;
139 if (*ni
== NULL
&& alternate_name
!= NULL
)
140 /* We haven't found an entry so far. Try to find it with the
142 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
143 if (strcmp (alternate_name
, entry
->name
) == 0)
144 *ni
= entry
->service
;
147 /* No configuration data is available, either because nsswitch.conf
148 doesn't exist or because it doesn't have a line for this database.
150 DEFCONFIG specifies the default service list for this database,
151 or null to use the most common default. */
154 *ni
= nss_parse_service_list (defconfig
155 ?: "nis [NOTFOUND=return] files");
158 /* Record the memory we've just allocated in defconfig_entries list,
159 so we can free it later. */
160 name_database_entry
*entry
;
162 /* Allocate ENTRY plus size of name (1 here). */
163 entry
= (name_database_entry
*) malloc (sizeof (*entry
) + 1);
167 entry
->next
= defconfig_entries
;
168 entry
->service
= *ni
;
169 entry
->name
[0] = '\0';
170 defconfig_entries
= entry
;
175 __libc_lock_unlock (lock
);
177 return *ni
!= NULL
? 0 : -1;
179 libc_hidden_def (__nss_database_lookup
)
186 __nss_lookup (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
189 *fctp
= __nss_lookup_function (*ni
, fct_name
);
190 if (*fctp
== NULL
&& fct2_name
!= NULL
)
191 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
194 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
195 && (*ni
)->next
!= NULL
)
199 *fctp
= __nss_lookup_function (*ni
, fct_name
);
200 if (*fctp
== NULL
&& fct2_name
!= NULL
)
201 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
204 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
206 libc_hidden_def (__nss_lookup
)
210 0 == adjusted for next function
213 __nss_next2 (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
214 void **fctp
, int status
, int all_values
)
218 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
219 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
220 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
221 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
226 /* This is really only for debugging. */
227 if (__builtin_expect (NSS_STATUS_TRYAGAIN
> status
228 || status
> NSS_STATUS_RETURN
, 0))
229 __libc_fatal ("illegal status in __nss_next");
231 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
235 if ((*ni
)->next
== NULL
)
242 *fctp
= __nss_lookup_function (*ni
, fct_name
);
243 if (*fctp
== NULL
&& fct2_name
!= NULL
)
244 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
247 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
248 && (*ni
)->next
!= NULL
);
250 return *fctp
!= NULL
? 0 : -1;
252 libc_hidden_def (__nss_next2
)
256 attribute_compat_text_section
257 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
260 return __nss_next2 (ni
, fct_name
, NULL
, fctp
, status
, all_values
);
265 __nss_configure_lookup (const char *dbname
, const char *service_line
)
267 service_user
*new_db
;
270 for (cnt
= 0; cnt
< ndatabases
; ++cnt
)
272 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
277 __set_errno (EINVAL
);
282 if (cnt
== ndatabases
)
284 __set_errno (EINVAL
);
288 /* Test whether it is really used. */
289 if (databases
[cnt
].dbp
== NULL
)
290 /* Nothing to do, but we could do. */
293 /* Try to generate new data. */
294 new_db
= nss_parse_service_list (service_line
);
297 /* Illegal service specification. */
298 __set_errno (EINVAL
);
302 /* Prevent multiple threads to change the service table. */
303 __libc_lock_lock (lock
);
305 /* Install new rules. */
306 *databases
[cnt
].dbp
= new_db
;
307 __nss_database_custom
[cnt
] = true;
309 __libc_lock_unlock (lock
);
315 /* Comparison function for searching NI->known tree. */
317 known_compare (const void *p1
, const void *p2
)
319 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
320 *(const char *const *) p2
);
324 #if !defined DO_STATIC_NSS || defined SHARED
327 nss_load_library (service_user
*ni
)
329 if (ni
->library
== NULL
)
331 /* This service has not yet been used. Fetch the service
332 library for it, creating a new one if need be. If there
333 is no service table from the file, this static variable
334 holds the head of the service_library list made from the
335 default configuration. */
336 static name_database default_table
;
337 ni
->library
= nss_new_service (service_table
?: &default_table
,
339 if (ni
->library
== NULL
)
343 if (ni
->library
->lib_handle
== NULL
)
345 /* Load the shared library. */
346 size_t shlen
= (7 + strlen (ni
->name
) + 3
347 + strlen (__nss_shlib_revision
) + 1);
348 int saved_errno
= errno
;
349 char shlib_name
[shlen
];
351 /* Construct shared object name. */
352 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
356 __nss_shlib_revision
);
358 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
359 if (ni
->library
->lib_handle
== NULL
)
361 /* Failed to load the library. */
362 ni
->library
->lib_handle
= (void *) -1l;
363 __set_errno (saved_errno
);
368 /* Call the init function when nscd is used. */
369 size_t initlen
= (5 + strlen (ni
->name
)
370 + strlen ("_init") + 1);
371 char init_name
[initlen
];
373 /* Construct the init function name. */
374 __stpcpy (__stpcpy (__stpcpy (init_name
,
379 /* Find the optional init function. */
380 void (*ifct
) (void (*) (size_t, struct traced_file
*))
381 = __libc_dlsym (ni
->library
->lib_handle
, init_name
);
384 void (*cb
) (size_t, struct traced_file
*) = nscd_init_cb
;
400 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
402 void **found
, *result
;
404 /* We now modify global data. Protect it. */
405 __libc_lock_lock (lock
);
407 /* Search the tree of functions previously requested. Data in the
408 tree are `known_function' structures, whose first member is a
409 `const char *', the lookup key. The search returns a pointer to
410 the tree node structure; the first member of the is a pointer to
411 our structure (i.e. what will be a `known_function'); since the
412 first member of that is the lookup key string, &FCT_NAME is close
413 enough to a pointer to our structure to use as a lookup key that
414 will be passed to `known_compare' (above). */
416 found
= __tsearch (&fct_name
, &ni
->known
, &known_compare
);
418 /* This means out-of-memory. */
420 else if (*found
!= &fct_name
)
422 /* The search found an existing structure in the tree. */
423 result
= ((known_function
*) *found
)->fct_ptr
;
425 PTR_DEMANGLE (result
);
430 /* This name was not known before. Now we have a node in the tree
431 (in the proper sorted position for FCT_NAME) that points to
432 &FCT_NAME instead of any real `known_function' structure.
433 Allocate a new structure and fill it in. */
435 known_function
*known
= malloc (sizeof *known
);
438 #if !defined DO_STATIC_NSS || defined SHARED
441 /* Oops. We can't instantiate this node properly.
442 Remove it from the tree. */
443 __tdelete (&fct_name
, &ni
->known
, &known_compare
);
449 /* Point the tree node at this new structure. */
451 known
->fct_name
= fct_name
;
453 #if !defined DO_STATIC_NSS || defined SHARED
454 /* Load the appropriate library. */
455 if (nss_load_library (ni
) != 0)
456 /* This only happens when out of memory. */
457 goto remove_from_tree
;
459 if (ni
->library
->lib_handle
== (void *) -1l)
460 /* Library not found => function not found. */
464 /* Get the desired function. */
465 size_t namlen
= (5 + strlen (ni
->name
) + 1
466 + strlen (fct_name
) + 1);
469 /* Construct the function name. */
470 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
475 /* Look up the symbol. */
476 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
479 /* We can't get function address dynamically in static linking. */
481 # define DEFINE_ENT(h,nm) \
482 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
483 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
484 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
485 # define DEFINE_GET(h,nm) \
486 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
487 # define DEFINE_GETBY(h,nm,ky) \
488 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
489 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
491 # include "function.def"
494 size_t namlen
= (5 + strlen (ni
->name
) + 1
495 + strlen (fct_name
) + 1);
498 /* Construct the function name. */
499 __stpcpy (__stpcpy (__stpcpy (name
, ni
->name
),
504 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
505 if (strcmp (tp
->fname
, name
) == 0)
513 /* Remember function pointer for later calls. Even if null, we
514 record it so a second try needn't search the library again. */
515 known
->fct_ptr
= result
;
517 PTR_MANGLE (known
->fct_ptr
);
522 /* Remove the lock. */
523 __libc_lock_unlock (lock
);
527 libc_hidden_def (__nss_lookup_function
)
530 static name_database
*
532 nss_parse_file (const char *fname
)
535 name_database
*result
;
536 name_database_entry
*last
;
540 /* Open the configuration file. */
541 fp
= fopen (fname
, "rce");
545 /* No threads use this stream. */
546 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
548 result
= (name_database
*) malloc (sizeof (name_database
));
555 result
->entry
= NULL
;
556 result
->library
= NULL
;
562 name_database_entry
*this;
565 n
= __getline (&line
, &len
, fp
);
568 if (line
[n
- 1] == '\n')
571 /* Because the file format does not know any form of quoting we
572 can search forward for the next '#' character and if found
573 make it terminating the line. */
574 *__strchrnul (line
, '#') = '\0';
576 /* If the line is blank it is ignored. */
580 /* Each line completely specifies the actions for a database. */
581 this = nss_getline (line
);
587 result
->entry
= this;
592 while (!feof_unlocked (fp
));
594 /* Free the buffer. */
596 /* Close configuration file. */
603 /* Read the source names:
604 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
606 static service_user
*
608 nss_parse_service_list (const char *line
)
610 service_user
*result
= NULL
, **nextp
= &result
;
614 service_user
*new_service
;
617 while (isspace (line
[0]))
620 /* No source specified. */
623 /* Read <source> identifier. */
625 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
631 new_service
= (service_user
*) malloc (sizeof (service_user
)
632 + (line
- name
+ 1));
633 if (new_service
== NULL
)
636 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
638 /* Set default actions. */
639 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
640 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
641 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
642 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
643 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
644 new_service
->library
= NULL
;
645 new_service
->known
= NULL
;
646 new_service
->next
= NULL
;
648 while (isspace (line
[0]))
653 /* Read criterions. */
656 while (line
[0] != '\0' && isspace (line
[0]));
661 enum nss_status status
;
662 lookup_actions action
;
664 /* Grok ! before name to mean all statii but that one. */
665 not = line
[0] == '!';
669 /* Read status name. */
671 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
675 /* Compare with known statii. */
676 if (line
- name
== 7)
678 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
679 status
= NSS_STATUS_SUCCESS
;
680 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
681 status
= NSS_STATUS_UNAVAIL
;
685 else if (line
- name
== 8)
687 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
688 status
= NSS_STATUS_NOTFOUND
;
689 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
690 status
= NSS_STATUS_TRYAGAIN
;
697 while (isspace (line
[0]))
703 while (isspace (line
[0]));
706 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
710 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
711 action
= NSS_ACTION_RETURN
;
712 else if (line
- name
== 8
713 && __strncasecmp (name
, "CONTINUE", 8) == 0)
714 action
= NSS_ACTION_CONTINUE
;
715 else if (line
- name
== 5
716 && __strncasecmp (name
, "MERGE", 5) == 0)
717 action
= NSS_ACTION_MERGE
;
723 /* Save the current action setting for this status,
724 set them all to the given action, and reset this one. */
725 const lookup_actions save
= new_service
->actions
[2 + status
];
726 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
727 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
728 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
729 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
730 new_service
->actions
[2 + status
] = save
;
733 new_service
->actions
[2 + status
] = action
;
735 /* Skip white spaces. */
736 while (isspace (line
[0]))
739 while (line
[0] != ']');
745 *nextp
= new_service
;
746 nextp
= &new_service
->next
;
755 static name_database_entry
*
757 nss_getline (char *line
)
760 name_database_entry
*result
;
763 /* Ignore leading white spaces. ATTENTION: this is different from
764 what is implemented in Solaris. The Solaris man page says a line
765 beginning with a white space character is ignored. We regard
766 this as just another misfeature in Solaris. */
767 while (isspace (line
[0]))
770 /* Recognize `<database> ":"'. */
772 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
774 if (line
[0] == '\0' || name
== line
)
779 len
= strlen (name
) + 1;
781 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
785 /* Save the database name. */
786 memcpy (result
->name
, name
, len
);
788 /* Parse the list of services. */
789 result
->service
= nss_parse_service_list (line
);
796 #if !defined DO_STATIC_NSS || defined SHARED
797 static service_library
*
799 nss_new_service (name_database
*database
, const char *name
)
801 service_library
**currentp
= &database
->library
;
803 while (*currentp
!= NULL
)
805 if (strcmp ((*currentp
)->name
, name
) == 0)
807 currentp
= &(*currentp
)->next
;
810 /* We have to add the new service. */
811 *currentp
= (service_library
*) malloc (sizeof (service_library
));
812 if (*currentp
== NULL
)
815 (*currentp
)->name
= name
;
816 (*currentp
)->lib_handle
= NULL
;
817 (*currentp
)->next
= NULL
;
824 #if defined SHARED && defined USE_NSCD
825 /* Load all libraries for the service. */
827 nss_load_all_libraries (const char *service
, const char *def
)
829 service_user
*ni
= NULL
;
831 if (__nss_database_lookup (service
, NULL
, def
, &ni
) == 0)
834 nss_load_library (ni
);
840 /* Called by nscd and nscd alone. */
842 __nss_disable_nscd (void (*cb
) (size_t, struct traced_file
*))
850 /* Find all the relevant modules so that the init functions are called. */
851 nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files");
852 nss_load_all_libraries ("group", "compat [NOTFOUND=return] files");
853 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
854 nss_load_all_libraries ("services", NULL
);
856 /* Disable all uses of NSCD. */
857 __nss_not_use_nscd_passwd
= -1;
858 __nss_not_use_nscd_group
= -1;
859 __nss_not_use_nscd_hosts
= -1;
860 __nss_not_use_nscd_services
= -1;
861 __nss_not_use_nscd_netgroup
= -1;
866 free_database_entries (name_database_entry
*entry
)
868 while (entry
!= NULL
)
870 name_database_entry
*olde
= entry
;
871 service_user
*service
= entry
->service
;
873 while (service
!= NULL
)
875 service_user
*olds
= service
;
877 if (service
->known
!= NULL
)
878 __tdestroy (service
->known
, free
);
880 service
= service
->next
;
889 /* Free all resources if necessary. */
890 libc_freeres_fn (free_defconfig
)
892 name_database_entry
*entry
= defconfig_entries
;
895 /* defconfig was not used. */
898 /* Don't disturb ongoing other threads (if there are any). */
899 defconfig_entries
= NULL
;
901 free_database_entries (entry
);
904 libc_freeres_fn (free_mem
)
906 name_database
*top
= service_table
;
907 service_library
*library
;
910 /* Maybe we have not read the nsswitch.conf file. */
913 /* Don't disturb ongoing other threads (if there are any). */
914 service_table
= NULL
;
916 free_database_entries (top
->entry
);
918 library
= top
->library
;
919 while (library
!= NULL
)
921 service_library
*oldl
= library
;
923 if (library
->lib_handle
&& library
->lib_handle
!= (void *) -1l)
924 __libc_dlclose (library
->lib_handle
);
926 library
= library
->next
;