1 /* Copyright (C) 1996,1997,1998,1999,2001,2002 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>
43 /* Prototypes for the local functions. */
44 static name_database
*nss_parse_file (const char *fname
) internal_function
;
45 static name_database_entry
*nss_getline (char *line
) internal_function
;
46 static service_user
*nss_parse_service_list (const char *line
)
48 static service_library
*nss_new_service (name_database
*database
,
49 const char *name
) internal_function
;
52 /* Declare external database variables. */
53 #define DEFINE_DATABASE(name) \
54 extern service_user *__nss_##name##_database attribute_hidden; \
55 weak_extern (__nss_##name##_database)
56 #include "databases.def"
57 #undef DEFINE_DATABASE
59 /* Structure to map database name to variable. */
66 #define DEFINE_DATABASE(name) \
67 { #name, &__nss_##name##_database },
68 #include "databases.def"
69 #undef DEFINE_DATABASE
73 __libc_lock_define_initialized (static, lock
)
75 #if !defined DO_STATIC_NSS || defined SHARED
76 /* String with revision number of the shared object files. */
77 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
80 /* The root of the whole data base. */
81 static name_database
*service_table
;
84 /* -1 == database not found
85 0 == database entry pointer stored */
87 __nss_database_lookup (const char *database
, const char *alternate_name
,
88 const char *defconfig
, service_user
**ni
)
90 /* Prevent multiple threads to change the service table. */
91 __libc_lock_lock (lock
);
93 /* Reconsider database variable in case some other thread called
94 `__nss_configure_lookup' while we waited for the lock. */
97 __libc_lock_unlock (lock
);
101 /* Are we initialized yet? */
102 if (service_table
== NULL
)
103 /* Read config file. */
104 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
106 /* Test whether configuration data is available. */
107 if (service_table
!= NULL
)
109 /* Return first `service_user' entry for DATABASE. */
110 name_database_entry
*entry
;
112 /* XXX Could use some faster mechanism here. But each database is
113 only requested once and so this might not be critical. */
114 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
115 if (strcmp (database
, entry
->name
) == 0)
116 *ni
= entry
->service
;
118 if (*ni
== NULL
&& alternate_name
!= NULL
)
119 /* We haven't found an entry so far. Try to find it with the
121 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
122 if (strcmp (alternate_name
, entry
->name
) == 0)
123 *ni
= entry
->service
;
126 /* No configuration data is available, either because nsswitch.conf
127 doesn't exist or because it doesn't has a line for this database.
129 DEFCONFIG specifies the default service list for this database,
130 or null to use the most common default. */
132 *ni
= nss_parse_service_list (defconfig
133 ?: "nis [NOTFOUND=return] files");
135 __libc_lock_unlock (lock
);
139 libc_hidden_def (__nss_database_lookup
)
146 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
148 *fctp
= __nss_lookup_function (*ni
, fct_name
);
151 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
152 && (*ni
)->next
!= NULL
)
156 *fctp
= __nss_lookup_function (*ni
, fct_name
);
159 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
164 0 == adjusted for next function
167 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
172 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
173 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
174 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
175 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
180 /* This is really only for debugging. */
181 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
182 __libc_fatal ("illegal status in __nss_next");
184 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
188 if ((*ni
)->next
== NULL
)
195 *fctp
= __nss_lookup_function (*ni
, fct_name
);
198 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
199 && (*ni
)->next
!= NULL
);
201 return *fctp
!= NULL
? 0 : -1;
203 libc_hidden_def (__nss_next
)
207 __nss_configure_lookup (const char *dbname
, const char *service_line
)
209 service_user
*new_db
;
212 for (cnt
= 0; cnt
< sizeof databases
; ++cnt
)
214 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
219 __set_errno (EINVAL
);
224 if (cnt
== sizeof databases
)
226 __set_errno (EINVAL
);
230 /* Test whether it is really used. */
231 if (databases
[cnt
].dbp
== NULL
)
232 /* Nothing to do, but we could do. */
235 /* Try to generate new data. */
236 new_db
= nss_parse_service_list (service_line
);
239 /* Illegal service specification. */
240 __set_errno (EINVAL
);
244 /* Prevent multiple threads to change the service table. */
245 __libc_lock_lock (lock
);
247 /* Install new rules. */
248 *databases
[cnt
].dbp
= new_db
;
250 __libc_lock_unlock (lock
);
256 /* Comparison function for searching NI->known tree. */
258 known_compare (const void *p1
, const void *p2
)
260 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
261 *(const char *const *) p2
);
266 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
268 void **found
, *result
;
270 /* We now modify global data. Protect it. */
271 __libc_lock_lock (lock
);
273 /* Search the tree of functions previously requested. Data in the
274 tree are `known_function' structures, whose first member is a
275 `const char *', the lookup key. The search returns a pointer to
276 the tree node structure; the first member of the is a pointer to
277 our structure (i.e. what will be a `known_function'); since the
278 first member of that is the lookup key string, &FCT_NAME is close
279 enough to a pointer to our structure to use as a lookup key that
280 will be passed to `known_compare' (above). */
282 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
283 if (*found
!= &fct_name
)
284 /* The search found an existing structure in the tree. */
285 result
= ((known_function
*) *found
)->fct_ptr
;
288 /* This name was not known before. Now we have a node in the tree
289 (in the proper sorted position for FCT_NAME) that points to
290 &FCT_NAME instead of any real `known_function' structure.
291 Allocate a new structure and fill it in. */
293 known_function
*known
= malloc (sizeof *known
);
297 /* Oops. We can't instantiate this node properly.
298 Remove it from the tree. */
299 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
304 /* Point the tree node at this new structure. */
306 known
->fct_name
= fct_name
;
308 if (ni
->library
== NULL
)
310 /* This service has not yet been used. Fetch the service
311 library for it, creating a new one if need be. If there
312 is no service table from the file, this static variable
313 holds the head of the service_library list made from the
314 default configuration. */
315 static name_database default_table
;
316 ni
->library
= nss_new_service (service_table
?: &default_table
,
318 if (ni
->library
== NULL
)
320 /* This only happens when out of memory. */
322 goto remove_from_tree
;
326 #if !defined DO_STATIC_NSS || defined SHARED
327 if (ni
->library
->lib_handle
== NULL
)
329 /* Load the shared library. */
330 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
331 + strlen (__nss_shlib_revision
) + 1);
332 int saved_errno
= errno
;
333 char shlib_name
[shlen
];
335 /* Construct shared object name. */
336 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
340 __nss_shlib_revision
);
342 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
343 if (ni
->library
->lib_handle
== NULL
)
345 /* Failed to load the library. */
346 ni
->library
->lib_handle
= (void *) -1l;
347 __set_errno (saved_errno
);
351 if (ni
->library
->lib_handle
== (void *) -1l)
352 /* Library not found => function not found. */
356 /* Get the desired function. */
357 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
358 + strlen (fct_name
) + 1);
361 /* Construct the function name. */
362 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
367 /* Look up the symbol. */
368 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
371 /* We can't get function address dynamically in static linking. */
373 # define DEFINE_ENT(h,nm) \
374 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
375 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
376 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
377 # define DEFINE_GET(h,nm) \
378 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
379 # define DEFINE_GETBY(h,nm,ky) \
380 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
381 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
383 # include "function.def"
386 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
387 + strlen (fct_name
) + 1);
390 /* Construct the function name. */
391 __stpcpy (__stpcpy (__stpcpy (name
, ni
->library
->name
),
396 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
397 if (strcmp (tp
->fname
, name
) == 0)
405 /* Remember function pointer for later calls. Even if null, we
406 record it so a second try needn't search the library again. */
407 known
->fct_ptr
= result
;
411 /* Remove the lock. */
412 __libc_lock_unlock (lock
);
418 static name_database
*
420 nss_parse_file (const char *fname
)
423 name_database
*result
;
424 name_database_entry
*last
;
428 /* Open the configuration file. */
429 fp
= fopen (fname
, "r");
433 /* No threads use this stream. */
434 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
436 result
= (name_database
*) malloc (sizeof (name_database
));
440 result
->entry
= NULL
;
441 result
->library
= NULL
;
447 name_database_entry
*this;
450 n
= __getline (&line
, &len
, fp
);
453 if (line
[n
- 1] == '\n')
456 /* Because the file format does not know any form of quoting we
457 can search forward for the next '#' character and if found
458 make it terminating the line. */
459 *__strchrnul (line
, '#') = '\0';
461 /* If the line is blank it is ignored. */
465 /* Each line completely specifies the actions for a database. */
466 this = nss_getline (line
);
472 result
->entry
= this;
477 while (!feof_unlocked (fp
));
479 /* Free the buffer. */
481 /* Close configuration file. */
488 /* Read the source names:
489 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
491 static service_user
*
493 nss_parse_service_list (const char *line
)
495 service_user
*result
= NULL
, **nextp
= &result
;
499 service_user
*new_service
;
502 while (isspace (line
[0]))
505 /* No source specified. */
508 /* Read <source> identifier. */
510 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
516 new_service
= (service_user
*) malloc (sizeof (service_user
)
517 + (line
- name
+ 1));
518 if (new_service
== NULL
)
521 *((char *) __mempcpy (new_service
->name
, name
, line
- name
)) = '\0';
523 /* Set default actions. */
524 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
525 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
526 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
527 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
528 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
529 new_service
->library
= NULL
;
530 new_service
->known
= NULL
;
531 new_service
->next
= NULL
;
533 while (isspace (line
[0]))
538 /* Read criterions. */
541 while (line
[0] != '\0' && isspace (line
[0]));
546 enum nss_status status
;
547 lookup_actions action
;
549 /* Grok ! before name to mean all statii but that one. */
550 not = line
[0] == '!';
554 /* Read status name. */
556 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
560 /* Compare with known statii. */
561 if (line
- name
== 7)
563 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
564 status
= NSS_STATUS_SUCCESS
;
565 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
566 status
= NSS_STATUS_UNAVAIL
;
570 else if (line
- name
== 8)
572 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
573 status
= NSS_STATUS_NOTFOUND
;
574 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
575 status
= NSS_STATUS_TRYAGAIN
;
582 while (isspace (line
[0]))
588 while (isspace (line
[0]));
591 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
595 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
596 action
= NSS_ACTION_RETURN
;
597 else if (line
- name
== 8
598 && __strncasecmp (name
, "CONTINUE", 8) == 0)
599 action
= NSS_ACTION_CONTINUE
;
605 /* Save the current action setting for this status,
606 set them all to the given action, and reset this one. */
607 const lookup_actions save
= new_service
->actions
[2 + status
];
608 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
609 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
610 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
611 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
612 new_service
->actions
[2 + status
] = save
;
615 new_service
->actions
[2 + status
] = action
;
617 /* Skip white spaces. */
618 while (isspace (line
[0]))
621 while (line
[0] != ']');
627 *nextp
= new_service
;
628 nextp
= &new_service
->next
;
632 static name_database_entry
*
634 nss_getline (char *line
)
637 name_database_entry
*result
;
640 /* Ignore leading white spaces. ATTENTION: this is different from
641 what is implemented in Solaris. The Solaris man page says a line
642 beginning with a white space character is ignored. We regard
643 this as just another misfeature in Solaris. */
644 while (isspace (line
[0]))
647 /* Recognize `<database> ":"'. */
649 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
651 if (line
[0] == '\0' || name
== line
)
656 len
= strlen (name
) + 1;
658 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
) + len
);
662 /* Save the database name. */
663 memcpy (result
->name
, name
, len
);
665 /* Parse the list of services. */
666 result
->service
= nss_parse_service_list (line
);
673 static service_library
*
675 nss_new_service (name_database
*database
, const char *name
)
677 service_library
**currentp
= &database
->library
;
679 while (*currentp
!= NULL
)
681 if (strcmp ((*currentp
)->name
, name
) == 0)
683 currentp
= &(*currentp
)->next
;
686 /* We have to add the new service. */
687 *currentp
= (service_library
*) malloc (sizeof (service_library
));
688 if (*currentp
== NULL
)
691 (*currentp
)->name
= name
;
692 (*currentp
)->lib_handle
= NULL
;
693 (*currentp
)->next
= NULL
;
699 /* Free all resources if necessary. */
700 static void __attribute__ ((unused
))
703 name_database
*top
= service_table
;
704 name_database_entry
*entry
;
705 service_library
*library
;
708 /* Maybe we have not read the nsswitch.conf file. */
711 /* Don't disturb ongoing other threads (if there are any). */
712 service_table
= NULL
;
715 while (entry
!= NULL
)
717 name_database_entry
*olde
= entry
;
718 service_user
*service
= entry
->service
;
720 while (service
!= NULL
)
722 service_user
*olds
= service
;
724 if (service
->known
!= NULL
)
725 __tdestroy (service
->known
, free
);
727 service
= service
->next
;
735 library
= top
->library
;
736 while (library
!= NULL
)
738 service_library
*oldl
= library
;
740 __libc_dlclose (library
->lib_handle
);
742 library
= library
->next
;
749 text_set_element (__libc_subfreeres
, free_mem
);