1 /* Copyright (C) 1996-1999, 2001-2006, 2007 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
72 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
75 __libc_lock_define_initialized (static, lock
)
77 #if !defined DO_STATIC_NSS || defined SHARED
78 /* String with revision number of the shared object files. */
79 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
82 /* The root of the whole data base. */
83 static name_database
*service_table
;
86 /* -1 == database not found
87 0 == database entry pointer stored */
89 __nss_database_lookup (const char *database
, const char *alternate_name
,
90 const char *defconfig
, service_user
**ni
)
92 /* Prevent multiple threads to change the service table. */
93 __libc_lock_lock (lock
);
95 /* Reconsider database variable in case some other thread called
96 `__nss_configure_lookup' while we waited for the lock. */
99 __libc_lock_unlock (lock
);
103 /* Are we initialized yet? */
104 if (service_table
== NULL
)
105 /* Read config file. */
106 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
108 /* Test whether configuration data is available. */
109 if (service_table
!= NULL
)
111 /* Return first `service_user' entry for DATABASE. */
112 name_database_entry
*entry
;
114 /* XXX Could use some faster mechanism here. But each database is
115 only requested once and so this might not be critical. */
116 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
117 if (strcmp (database
, entry
->name
) == 0)
118 *ni
= entry
->service
;
120 if (*ni
== NULL
&& alternate_name
!= NULL
)
121 /* We haven't found an entry so far. Try to find it with the
123 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
124 if (strcmp (alternate_name
, entry
->name
) == 0)
125 *ni
= entry
->service
;
128 /* No configuration data is available, either because nsswitch.conf
129 doesn't exist or because it doesn't has a line for this database.
131 DEFCONFIG specifies the default service list for this database,
132 or null to use the most common default. */
134 *ni
= nss_parse_service_list (defconfig
135 ?: "nis [NOTFOUND=return] files");
137 __libc_lock_unlock (lock
);
141 libc_hidden_def (__nss_database_lookup
)
148 __nss_lookup (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
151 *fctp
= __nss_lookup_function (*ni
, fct_name
);
152 if (*fctp
== NULL
&& fct2_name
!= NULL
)
153 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
156 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
157 && (*ni
)->next
!= NULL
)
161 *fctp
= __nss_lookup_function (*ni
, fct_name
);
162 if (*fctp
== NULL
&& fct2_name
!= NULL
)
163 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
166 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
171 0 == adjusted for next function
174 __nss_next2 (service_user
**ni
, const char *fct_name
, const char *fct2_name
,
175 void **fctp
, int status
, int all_values
)
179 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
180 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
181 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
182 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
187 /* This is really only for debugging. */
188 if (__builtin_expect (NSS_STATUS_TRYAGAIN
> status
189 || status
> NSS_STATUS_RETURN
, 0))
190 __libc_fatal ("illegal status in __nss_next");
192 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
196 if ((*ni
)->next
== NULL
)
203 *fctp
= __nss_lookup_function (*ni
, fct_name
);
204 if (*fctp
== NULL
&& fct2_name
!= NULL
)
205 *fctp
= __nss_lookup_function (*ni
, fct2_name
);
208 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
209 && (*ni
)->next
!= NULL
);
211 return *fctp
!= NULL
? 0 : -1;
213 libc_hidden_def (__nss_next2
)
217 attribute_compat_text_section
218 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
221 return __nss_next2 (ni
, fct_name
, NULL
, fctp
, status
, all_values
);
226 __nss_configure_lookup (const char *dbname
, const char *service_line
)
228 service_user
*new_db
;
231 for (cnt
= 0; cnt
< ndatabases
; ++cnt
)
233 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
238 __set_errno (EINVAL
);
243 if (cnt
== ndatabases
)
245 __set_errno (EINVAL
);
249 /* Test whether it is really used. */
250 if (databases
[cnt
].dbp
== NULL
)
251 /* Nothing to do, but we could do. */
254 /* Try to generate new data. */
255 new_db
= nss_parse_service_list (service_line
);
258 /* Illegal service specification. */
259 __set_errno (EINVAL
);
263 /* Prevent multiple threads to change the service table. */
264 __libc_lock_lock (lock
);
266 /* Install new rules. */
267 *databases
[cnt
].dbp
= new_db
;
269 __libc_lock_unlock (lock
);
275 /* Comparison function for searching NI->known tree. */
277 known_compare (const void *p1
, const void *p2
)
279 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
280 *(const char *const *) p2
);
285 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
287 void **found
, *result
;
289 /* We now modify global data. Protect it. */
290 __libc_lock_lock (lock
);
292 /* Search the tree of functions previously requested. Data in the
293 tree are `known_function' structures, whose first member is a
294 `const char *', the lookup key. The search returns a pointer to
295 the tree node structure; the first member of the is a pointer to
296 our structure (i.e. what will be a `known_function'); since the
297 first member of that is the lookup key string, &FCT_NAME is close
298 enough to a pointer to our structure to use as a lookup key that
299 will be passed to `known_compare' (above). */
301 found
= __tsearch (&fct_name
, &ni
->known
, &known_compare
);
302 if (*found
!= &fct_name
)
303 /* The search found an existing structure in the tree. */
304 result
= ((known_function
*) *found
)->fct_ptr
;
307 /* This name was not known before. Now we have a node in the tree
308 (in the proper sorted position for FCT_NAME) that points to
309 &FCT_NAME instead of any real `known_function' structure.
310 Allocate a new structure and fill it in. */
312 known_function
*known
= malloc (sizeof *known
);
316 /* Oops. We can't instantiate this node properly.
317 Remove it from the tree. */
318 __tdelete (&fct_name
, &ni
->known
, &known_compare
);
323 /* Point the tree node at this new structure. */
325 known
->fct_name
= fct_name
;
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
)
339 /* This only happens when out of memory. */
341 goto remove_from_tree
;
345 #if !defined DO_STATIC_NSS || defined SHARED
346 if (ni
->library
->lib_handle
== NULL
)
348 /* Load the shared library. */
349 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
350 + strlen (__nss_shlib_revision
) + 1);
351 int saved_errno
= errno
;
352 char shlib_name
[shlen
];
354 /* Construct shared object name. */
355 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
359 __nss_shlib_revision
);
361 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
362 if (ni
->library
->lib_handle
== NULL
)
364 /* Failed to load the library. */
365 ni
->library
->lib_handle
= (void *) -1l;
366 __set_errno (saved_errno
);
370 if (ni
->library
->lib_handle
== (void *) -1l)
371 /* Library not found => function not found. */
375 /* Get the desired function. */
376 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
377 + strlen (fct_name
) + 1);
380 /* Construct the function name. */
381 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
386 /* Look up the symbol. */
387 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
390 /* We can't get function address dynamically in static linking. */
392 # define DEFINE_ENT(h,nm) \
393 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
394 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
395 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
396 # define DEFINE_GET(h,nm) \
397 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
398 # define DEFINE_GETBY(h,nm,ky) \
399 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
400 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
402 # include "function.def"
405 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
406 + strlen (fct_name
) + 1);
409 /* Construct the function name. */
410 __stpcpy (__stpcpy (__stpcpy (name
, ni
->library
->name
),
415 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
416 if (strcmp (tp
->fname
, name
) == 0)
424 /* Remember function pointer for later calls. Even if null, we
425 record it so a second try needn't search the library again. */
426 known
->fct_ptr
= result
;
430 /* Remove the lock. */
431 __libc_lock_unlock (lock
);
435 libc_hidden_def (__nss_lookup_function
)
438 static name_database
*
440 nss_parse_file (const char *fname
)
443 name_database
*result
;
444 name_database_entry
*last
;
448 /* Open the configuration file. */
449 fp
= fopen (fname
, "rc");
453 /* No threads use this stream. */
454 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
456 result
= (name_database
*) malloc (sizeof (name_database
));
460 result
->entry
= NULL
;
461 result
->library
= NULL
;
467 name_database_entry
*this;
470 n
= __getline (&line
, &len
, fp
);
473 if (line
[n
- 1] == '\n')
476 /* Because the file format does not know any form of quoting we
477 can search forward for the next '#' character and if found
478 make it terminating the line. */
479 *__strchrnul (line
, '#') = '\0';
481 /* If the line is blank it is ignored. */
485 /* Each line completely specifies the actions for a database. */
486 this = nss_getline (line
);
492 result
->entry
= this;
497 while (!feof_unlocked (fp
));
499 /* Free the buffer. */
501 /* Close configuration file. */
508 /* Read the source names:
509 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
511 static service_user
*
513 nss_parse_service_list (const char *line
)
515 service_user
*result
= NULL
, **nextp
= &result
;
519 service_user
*new_service
;
522 while (isspace (line
[0]))
525 /* No source specified. */
528 /* Read <source> identifier. */
530 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
536 new_service
= (service_user
*) malloc (sizeof (service_user
)
537 + (line
- name
+ 1));
538 if (new_service
== NULL
)
541 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
543 /* Set default actions. */
544 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
545 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
546 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
547 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
548 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
549 new_service
->library
= NULL
;
550 new_service
->known
= NULL
;
551 new_service
->next
= NULL
;
553 while (isspace (line
[0]))
558 /* Read criterions. */
561 while (line
[0] != '\0' && isspace (line
[0]));
566 enum nss_status status
;
567 lookup_actions action
;
569 /* Grok ! before name to mean all statii but that one. */
570 not = line
[0] == '!';
574 /* Read status name. */
576 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
580 /* Compare with known statii. */
581 if (line
- name
== 7)
583 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
584 status
= NSS_STATUS_SUCCESS
;
585 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
586 status
= NSS_STATUS_UNAVAIL
;
590 else if (line
- name
== 8)
592 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
593 status
= NSS_STATUS_NOTFOUND
;
594 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
595 status
= NSS_STATUS_TRYAGAIN
;
602 while (isspace (line
[0]))
608 while (isspace (line
[0]));
611 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
615 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
616 action
= NSS_ACTION_RETURN
;
617 else if (line
- name
== 8
618 && __strncasecmp (name
, "CONTINUE", 8) == 0)
619 action
= NSS_ACTION_CONTINUE
;
625 /* Save the current action setting for this status,
626 set them all to the given action, and reset this one. */
627 const lookup_actions save
= new_service
->actions
[2 + status
];
628 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
629 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
630 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
631 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
632 new_service
->actions
[2 + status
] = save
;
635 new_service
->actions
[2 + status
] = action
;
637 /* Skip white spaces. */
638 while (isspace (line
[0]))
641 while (line
[0] != ']');
647 *nextp
= new_service
;
648 nextp
= &new_service
->next
;
652 static name_database_entry
*
654 nss_getline (char *line
)
657 name_database_entry
*result
;
660 /* Ignore leading white spaces. ATTENTION: this is different from
661 what is implemented in Solaris. The Solaris man page says a line
662 beginning with a white space character is ignored. We regard
663 this as just another misfeature in Solaris. */
664 while (isspace (line
[0]))
667 /* Recognize `<database> ":"'. */
669 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
671 if (line
[0] == '\0' || name
== line
)
676 len
= strlen (name
) + 1;
678 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
682 /* Save the database name. */
683 memcpy (result
->name
, name
, len
);
685 /* Parse the list of services. */
686 result
->service
= nss_parse_service_list (line
);
693 static service_library
*
695 nss_new_service (name_database
*database
, const char *name
)
697 service_library
**currentp
= &database
->library
;
699 while (*currentp
!= NULL
)
701 if (strcmp ((*currentp
)->name
, name
) == 0)
703 currentp
= &(*currentp
)->next
;
706 /* We have to add the new service. */
707 *currentp
= (service_library
*) malloc (sizeof (service_library
));
708 if (*currentp
== NULL
)
711 (*currentp
)->name
= name
;
712 (*currentp
)->lib_handle
= NULL
;
713 (*currentp
)->next
= NULL
;
719 /* Called by nscd and nscd alone. */
721 __nss_disable_nscd (void)
723 /* Disable all uses of NSCD. */
724 __nss_not_use_nscd_passwd
= -1;
725 __nss_not_use_nscd_group
= -1;
726 __nss_not_use_nscd_hosts
= -1;
727 __nss_not_use_nscd_services
= -1;
731 /* Free all resources if necessary. */
732 libc_freeres_fn (free_mem
)
734 name_database
*top
= service_table
;
735 name_database_entry
*entry
;
736 service_library
*library
;
739 /* Maybe we have not read the nsswitch.conf file. */
742 /* Don't disturb ongoing other threads (if there are any). */
743 service_table
= NULL
;
746 while (entry
!= NULL
)
748 name_database_entry
*olde
= entry
;
749 service_user
*service
= entry
->service
;
751 while (service
!= NULL
)
753 service_user
*olds
= service
;
755 if (service
->known
!= NULL
)
756 __tdestroy (service
->known
, free
);
758 service
= service
->next
;
766 library
= top
->library
;
767 while (library
!= NULL
)
769 service_library
*oldl
= library
;
771 if (library
->lib_handle
&& library
->lib_handle
!= (void *) -1l)
772 __libc_dlclose (library
->lib_handle
);
774 library
= library
->next
;