1 /* Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <bits/libc-lock.h>
28 #include <stdio_ext.h>
34 #include <netinet/ether.h>
38 #if !defined DO_STATIC_NSS || defined SHARED
39 # include <gnu/lib-names.h>
43 #include "../nscd/nscd_proto.h"
45 /* Prototypes for the local functions. */
46 static name_database
*nss_parse_file (const char *fname
) internal_function
;
47 static name_database_entry
*nss_getline (char *line
) internal_function
;
48 static service_user
*nss_parse_service_list (const char *line
)
50 static service_library
*nss_new_service (name_database
*database
,
51 const char *name
) internal_function
;
54 /* Declare external database variables. */
55 #define DEFINE_DATABASE(name) \
56 extern service_user *__nss_##name##_database attribute_hidden; \
57 weak_extern (__nss_##name##_database)
58 #include "databases.def"
59 #undef DEFINE_DATABASE
61 /* Structure to map database name to variable. */
68 #define DEFINE_DATABASE(name) \
69 { #name, &__nss_##name##_database },
70 #include "databases.def"
71 #undef DEFINE_DATABASE
73 #define ndatabases (sizeof (databases) / sizeof (databases[0]))
76 __libc_lock_define_initialized (static, lock
)
78 #if !defined DO_STATIC_NSS || defined SHARED
79 /* String with revision number of the shared object files. */
80 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
83 /* The root of the whole data base. */
84 static name_database
*service_table
;
87 /* -1 == database not found
88 0 == database entry pointer stored */
90 __nss_database_lookup (const char *database
, const char *alternate_name
,
91 const char *defconfig
, service_user
**ni
)
93 /* Prevent multiple threads to change the service table. */
94 __libc_lock_lock (lock
);
96 /* Reconsider database variable in case some other thread called
97 `__nss_configure_lookup' while we waited for the lock. */
100 __libc_lock_unlock (lock
);
104 /* Are we initialized yet? */
105 if (service_table
== NULL
)
106 /* Read config file. */
107 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
109 /* Test whether configuration data is available. */
110 if (service_table
!= NULL
)
112 /* Return first `service_user' entry for DATABASE. */
113 name_database_entry
*entry
;
115 /* XXX Could use some faster mechanism here. But each database is
116 only requested once and so this might not be critical. */
117 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
118 if (strcmp (database
, entry
->name
) == 0)
119 *ni
= entry
->service
;
121 if (*ni
== NULL
&& alternate_name
!= NULL
)
122 /* We haven't found an entry so far. Try to find it with the
124 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
125 if (strcmp (alternate_name
, entry
->name
) == 0)
126 *ni
= entry
->service
;
129 /* No configuration data is available, either because nsswitch.conf
130 doesn't exist or because it doesn't has a line for this database.
132 DEFCONFIG specifies the default service list for this database,
133 or null to use the most common default. */
135 *ni
= nss_parse_service_list (defconfig
136 ?: "nis [NOTFOUND=return] files");
138 __libc_lock_unlock (lock
);
142 libc_hidden_def (__nss_database_lookup
)
149 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
151 *fctp
= __nss_lookup_function (*ni
, fct_name
);
154 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
155 && (*ni
)->next
!= NULL
)
159 *fctp
= __nss_lookup_function (*ni
, fct_name
);
162 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
167 0 == adjusted for next function
170 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
175 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
176 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
177 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
178 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
183 /* This is really only for debugging. */
184 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
185 __libc_fatal ("illegal status in __nss_next");
187 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
191 if ((*ni
)->next
== NULL
)
198 *fctp
= __nss_lookup_function (*ni
, fct_name
);
201 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
202 && (*ni
)->next
!= NULL
);
204 return *fctp
!= NULL
? 0 : -1;
206 libc_hidden_def (__nss_next
)
210 __nss_configure_lookup (const char *dbname
, const char *service_line
)
212 service_user
*new_db
;
215 for (cnt
= 0; cnt
< ndatabases
; ++cnt
)
217 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
222 __set_errno (EINVAL
);
227 if (cnt
== ndatabases
)
229 __set_errno (EINVAL
);
233 /* Test whether it is really used. */
234 if (databases
[cnt
].dbp
== NULL
)
235 /* Nothing to do, but we could do. */
238 /* Try to generate new data. */
239 new_db
= nss_parse_service_list (service_line
);
242 /* Illegal service specification. */
243 __set_errno (EINVAL
);
247 /* Prevent multiple threads to change the service table. */
248 __libc_lock_lock (lock
);
250 /* Install new rules. */
251 *databases
[cnt
].dbp
= new_db
;
253 __libc_lock_unlock (lock
);
259 /* Comparison function for searching NI->known tree. */
261 known_compare (const void *p1
, const void *p2
)
263 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
264 *(const char *const *) p2
);
269 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
271 void **found
, *result
;
273 /* We now modify global data. Protect it. */
274 __libc_lock_lock (lock
);
276 /* Search the tree of functions previously requested. Data in the
277 tree are `known_function' structures, whose first member is a
278 `const char *', the lookup key. The search returns a pointer to
279 the tree node structure; the first member of the is a pointer to
280 our structure (i.e. what will be a `known_function'); since the
281 first member of that is the lookup key string, &FCT_NAME is close
282 enough to a pointer to our structure to use as a lookup key that
283 will be passed to `known_compare' (above). */
285 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
286 if (*found
!= &fct_name
)
287 /* The search found an existing structure in the tree. */
288 result
= ((known_function
*) *found
)->fct_ptr
;
291 /* This name was not known before. Now we have a node in the tree
292 (in the proper sorted position for FCT_NAME) that points to
293 &FCT_NAME instead of any real `known_function' structure.
294 Allocate a new structure and fill it in. */
296 known_function
*known
= malloc (sizeof *known
);
300 /* Oops. We can't instantiate this node properly.
301 Remove it from the tree. */
302 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
307 /* Point the tree node at this new structure. */
309 known
->fct_name
= fct_name
;
311 if (ni
->library
== NULL
)
313 /* This service has not yet been used. Fetch the service
314 library for it, creating a new one if need be. If there
315 is no service table from the file, this static variable
316 holds the head of the service_library list made from the
317 default configuration. */
318 static name_database default_table
;
319 ni
->library
= nss_new_service (service_table
?: &default_table
,
321 if (ni
->library
== NULL
)
323 /* This only happens when out of memory. */
325 goto remove_from_tree
;
329 #if !defined DO_STATIC_NSS || defined SHARED
330 if (ni
->library
->lib_handle
== NULL
)
332 /* Load the shared library. */
333 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
334 + strlen (__nss_shlib_revision
) + 1);
335 int saved_errno
= errno
;
336 char shlib_name
[shlen
];
338 /* Construct shared object name. */
339 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
343 __nss_shlib_revision
);
345 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
346 if (ni
->library
->lib_handle
== NULL
)
348 /* Failed to load the library. */
349 ni
->library
->lib_handle
= (void *) -1l;
350 __set_errno (saved_errno
);
354 if (ni
->library
->lib_handle
== (void *) -1l)
355 /* Library not found => function not found. */
359 /* Get the desired function. */
360 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
361 + strlen (fct_name
) + 1);
364 /* Construct the function name. */
365 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
370 /* Look up the symbol. */
371 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
374 /* We can't get function address dynamically in static linking. */
376 # define DEFINE_ENT(h,nm) \
377 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
378 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
379 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
380 # define DEFINE_GET(h,nm) \
381 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
382 # define DEFINE_GETBY(h,nm,ky) \
383 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
384 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
386 # include "function.def"
389 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
390 + strlen (fct_name
) + 1);
393 /* Construct the function name. */
394 __stpcpy (__stpcpy (__stpcpy (name
, ni
->library
->name
),
399 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
400 if (strcmp (tp
->fname
, name
) == 0)
408 /* Remember function pointer for later calls. Even if null, we
409 record it so a second try needn't search the library again. */
410 known
->fct_ptr
= result
;
414 /* Remove the lock. */
415 __libc_lock_unlock (lock
);
419 libc_hidden_def (__nss_lookup_function
)
422 static name_database
*
424 nss_parse_file (const char *fname
)
427 name_database
*result
;
428 name_database_entry
*last
;
432 /* Open the configuration file. */
433 fp
= fopen (fname
, "rc");
437 /* No threads use this stream. */
438 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
440 result
= (name_database
*) malloc (sizeof (name_database
));
444 result
->entry
= NULL
;
445 result
->library
= NULL
;
451 name_database_entry
*this;
454 n
= __getline (&line
, &len
, fp
);
457 if (line
[n
- 1] == '\n')
460 /* Because the file format does not know any form of quoting we
461 can search forward for the next '#' character and if found
462 make it terminating the line. */
463 *__strchrnul (line
, '#') = '\0';
465 /* If the line is blank it is ignored. */
469 /* Each line completely specifies the actions for a database. */
470 this = nss_getline (line
);
476 result
->entry
= this;
481 while (!feof_unlocked (fp
));
483 /* Free the buffer. */
485 /* Close configuration file. */
492 /* Read the source names:
493 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
495 static service_user
*
497 nss_parse_service_list (const char *line
)
499 service_user
*result
= NULL
, **nextp
= &result
;
503 service_user
*new_service
;
506 while (isspace (line
[0]))
509 /* No source specified. */
512 /* Read <source> identifier. */
514 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
520 new_service
= (service_user
*) malloc (sizeof (service_user
)
521 + (line
- name
+ 1));
522 if (new_service
== NULL
)
525 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
527 /* Set default actions. */
528 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
529 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
530 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
531 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
532 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
533 new_service
->library
= NULL
;
534 new_service
->known
= NULL
;
535 new_service
->next
= NULL
;
537 while (isspace (line
[0]))
542 /* Read criterions. */
545 while (line
[0] != '\0' && isspace (line
[0]));
550 enum nss_status status
;
551 lookup_actions action
;
553 /* Grok ! before name to mean all statii but that one. */
554 not = line
[0] == '!';
558 /* Read status name. */
560 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
564 /* Compare with known statii. */
565 if (line
- name
== 7)
567 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
568 status
= NSS_STATUS_SUCCESS
;
569 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
570 status
= NSS_STATUS_UNAVAIL
;
574 else if (line
- name
== 8)
576 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
577 status
= NSS_STATUS_NOTFOUND
;
578 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
579 status
= NSS_STATUS_TRYAGAIN
;
586 while (isspace (line
[0]))
592 while (isspace (line
[0]));
595 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
599 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
600 action
= NSS_ACTION_RETURN
;
601 else if (line
- name
== 8
602 && __strncasecmp (name
, "CONTINUE", 8) == 0)
603 action
= NSS_ACTION_CONTINUE
;
609 /* Save the current action setting for this status,
610 set them all to the given action, and reset this one. */
611 const lookup_actions save
= new_service
->actions
[2 + status
];
612 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
613 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
614 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
615 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
616 new_service
->actions
[2 + status
] = save
;
619 new_service
->actions
[2 + status
] = action
;
621 /* Skip white spaces. */
622 while (isspace (line
[0]))
625 while (line
[0] != ']');
631 *nextp
= new_service
;
632 nextp
= &new_service
->next
;
636 static name_database_entry
*
638 nss_getline (char *line
)
641 name_database_entry
*result
;
644 /* Ignore leading white spaces. ATTENTION: this is different from
645 what is implemented in Solaris. The Solaris man page says a line
646 beginning with a white space character is ignored. We regard
647 this as just another misfeature in Solaris. */
648 while (isspace (line
[0]))
651 /* Recognize `<database> ":"'. */
653 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
655 if (line
[0] == '\0' || name
== line
)
660 len
= strlen (name
) + 1;
662 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
666 /* Save the database name. */
667 memcpy (result
->name
, name
, len
);
669 /* Parse the list of services. */
670 result
->service
= nss_parse_service_list (line
);
677 static service_library
*
679 nss_new_service (name_database
*database
, const char *name
)
681 service_library
**currentp
= &database
->library
;
683 while (*currentp
!= NULL
)
685 if (strcmp ((*currentp
)->name
, name
) == 0)
687 currentp
= &(*currentp
)->next
;
690 /* We have to add the new service. */
691 *currentp
= (service_library
*) malloc (sizeof (service_library
));
692 if (*currentp
== NULL
)
695 (*currentp
)->name
= name
;
696 (*currentp
)->lib_handle
= NULL
;
697 (*currentp
)->next
= NULL
;
703 /* Called by nscd and nscd alone. */
705 __nss_disable_nscd (void)
707 /* Disable all uses of NSCD. */
708 __nss_not_use_nscd_passwd
= -1;
709 __nss_not_use_nscd_group
= -1;
710 __nss_not_use_nscd_hosts
= -1;
714 /* Free all resources if necessary. */
715 libc_freeres_fn (free_mem
)
717 name_database
*top
= service_table
;
718 name_database_entry
*entry
;
719 service_library
*library
;
722 /* Maybe we have not read the nsswitch.conf file. */
725 /* Don't disturb ongoing other threads (if there are any). */
726 service_table
= NULL
;
729 while (entry
!= NULL
)
731 name_database_entry
*olde
= entry
;
732 service_user
*service
= entry
->service
;
734 while (service
!= NULL
)
736 service_user
*olds
= service
;
738 if (service
->known
!= NULL
)
739 __tdestroy (service
->known
, free
);
741 service
= service
->next
;
749 library
= top
->library
;
750 while (library
!= NULL
)
752 service_library
*oldl
= library
;
754 if (library
->lib_handle
&& library
->lib_handle
!= (void *) -1l)
755 __libc_dlclose (library
->lib_handle
);
757 library
= library
->next
;