1 /* Copyright (C) 1996, 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <bits/libc-lock.h>
30 #if !defined DO_STATIC_NSS || defined SHARED
31 # include <gnu/lib-names.h>
36 /* Prototypes for the local functions. */
37 static name_database
*nss_parse_file (const char *fname
) internal_function
;
38 static name_database_entry
*nss_getline (char *line
) internal_function
;
39 static service_user
*nss_parse_service_list (const char *line
)
41 static service_library
*nss_new_service (name_database
*database
,
42 const char *name
) internal_function
;
45 /* Declare external database variables. */
46 #define DEFINE_DATABASE(name) \
47 extern service_user *__nss_##name##_database; \
48 weak_extern (__nss_##name##_database)
49 #include "databases.def"
50 #undef DEFINE_DATABASE
52 /* Structure to map database name to variable. */
59 #define DEFINE_DATABASE(name) \
60 { #name, &__nss_##name##_database },
61 #include "databases.def"
62 #undef DEFINE_DATABASE
66 __libc_lock_define_initialized (static, lock
)
68 #if !defined DO_STATIC_NSS || defined SHARED
69 /* String with revision number of the shared object files. */
70 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
73 /* The root of the whole data base. */
74 static name_database
*service_table
;
77 /* -1 == database not found
78 0 == database entry pointer stored */
80 __nss_database_lookup (const char *database
, const char *alternate_name
,
81 const char *defconfig
, service_user
**ni
)
83 /* Prevent multiple threads to change the service table. */
84 __libc_lock_lock (lock
);
86 /* Reconsider database variable in case some other thread called
87 `__nss_configure_lookup' while we waited for the lock. */
90 __libc_lock_unlock (lock
);
94 /* Are we initialized yet? */
95 if (service_table
== NULL
)
96 /* Read config file. */
97 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
99 /* Test whether configuration data is available. */
100 if (service_table
!= NULL
)
102 /* Return first `service_user' entry for DATABASE. */
103 name_database_entry
*entry
;
105 /* XXX Could use some faster mechanism here. But each database is
106 only requested once and so this might not be critical. */
107 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
108 if (strcmp (database
, entry
->name
) == 0)
109 *ni
= entry
->service
;
111 if (*ni
== NULL
&& alternate_name
!= NULL
)
112 /* We haven't found an entry so far. Try to find it with the
114 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
115 if (strcmp (alternate_name
, entry
->name
) == 0)
116 *ni
= entry
->service
;
119 /* No configuration data is available, either because nsswitch.conf
120 doesn't exist or because it doesn't has a line for this database.
122 DEFCONFIG specifies the default service list for this database,
123 or null to use the most common default. */
125 *ni
= nss_parse_service_list (defconfig
126 ?: "nis [NOTFOUND=return] files");
128 __libc_lock_unlock (lock
);
138 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
140 *fctp
= __nss_lookup_function (*ni
, fct_name
);
143 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
144 && (*ni
)->next
!= NULL
)
148 *fctp
= __nss_lookup_function (*ni
, fct_name
);
151 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
156 0 == adjusted for next function
159 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
164 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
165 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
166 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
167 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
172 /* This is really only for debugging. */
173 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
174 __libc_fatal ("illegal status in " __FUNCTION__
);
176 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
180 if ((*ni
)->next
== NULL
)
187 *fctp
= __nss_lookup_function (*ni
, fct_name
);
190 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
191 && (*ni
)->next
!= NULL
);
193 return *fctp
!= NULL
? 0 : -1;
198 __nss_configure_lookup (const char *dbname
, const char *service_line
)
200 service_user
*new_db
;
203 for (cnt
= 0; cnt
< sizeof databases
; ++cnt
)
205 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
210 __set_errno (EINVAL
);
215 if (cnt
== sizeof databases
)
217 __set_errno (EINVAL
);
221 /* Test whether it is really used. */
222 if (databases
[cnt
].dbp
== NULL
)
223 /* Nothing to do, but we could do. */
226 /* Try to generate new data. */
227 new_db
= nss_parse_service_list (service_line
);
230 /* Illegal service specification. */
231 __set_errno (EINVAL
);
235 /* Prevent multiple threads to change the service table. */
236 __libc_lock_lock (lock
);
238 /* Install new rules. */
239 *databases
[cnt
].dbp
= new_db
;
241 __libc_lock_unlock (lock
);
247 /* Comparison function for searching NI->known tree. */
249 known_compare (const void *p1
, const void *p2
)
251 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
252 *(const char *const *) p2
);
257 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
259 void **found
, *result
;
261 /* We now modify global data. Protect it. */
262 __libc_lock_lock (lock
);
264 /* Search the tree of functions previously requested. Data in the
265 tree are `known_function' structures, whose first member is a
266 `const char *', the lookup key. The search returns a pointer to
267 the tree node structure; the first member of the is a pointer to
268 our structure (i.e. what will be a `known_function'); since the
269 first member of that is the lookup key string, &FCT_NAME is close
270 enough to a pointer to our structure to use as a lookup key that
271 will be passed to `known_compare' (above). */
273 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
274 if (*found
!= &fct_name
)
275 /* The search found an existing structure in the tree. */
276 result
= ((known_function
*) *found
)->fct_ptr
;
279 /* This name was not known before. Now we have a node in the tree
280 (in the proper sorted position for FCT_NAME) that points to
281 &FCT_NAME instead of any real `known_function' structure.
282 Allocate a new structure and fill it in. */
284 known_function
*known
= malloc (sizeof *known
);
288 /* Oops. We can't instantiate this node properly.
289 Remove it from the tree. */
290 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
295 /* Point the tree node at this new structure. */
297 known
->fct_name
= fct_name
;
299 if (ni
->library
== NULL
)
301 /* This service has not yet been used. Fetch the service
302 library for it, creating a new one if need be. If there
303 is no service table from the file, this static variable
304 holds the head of the service_library list made from the
305 default configuration. */
306 static name_database default_table
;
307 ni
->library
= nss_new_service (service_table
?: &default_table
,
309 if (ni
->library
== NULL
)
311 /* This only happens when out of memory. */
313 goto remove_from_tree
;
317 #if !defined DO_STATIC_NSS || defined SHARED
318 if (ni
->library
->lib_handle
== NULL
)
320 /* Load the shared library. */
321 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
322 + strlen (__nss_shlib_revision
) + 1);
323 int saved_errno
= errno
;
324 char shlib_name
[shlen
];
326 /* Construct shared object name. */
327 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name
,
331 __nss_shlib_revision
);
333 ni
->library
->lib_handle
= __libc_dlopen (shlib_name
);
334 if (ni
->library
->lib_handle
== NULL
)
336 /* Failed to load the library. */
337 ni
->library
->lib_handle
= (void *) -1l;
338 __set_errno (saved_errno
);
342 if (ni
->library
->lib_handle
== (void *) -1l)
343 /* Library not found => function not found. */
347 /* Get the desired function. */
348 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
349 + strlen (fct_name
) + 1);
352 /* Construct the function name. */
353 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
358 /* Look up the symbol. */
359 result
= __libc_dlsym (ni
->library
->lib_handle
, name
);
362 /* We can't get function address dynamically in static linking. */
364 # define DEFINE_ENT(h,nm) \
365 extern void _nss_##h##_get##nm##ent_r (void); \
366 extern void _nss_##h##_end##nm##ent (void); \
367 extern void _nss_##h##_set##nm##ent (void);
368 # define DEFINE_GET(h,nm) \
369 extern void _nss_##h##_get##nm##_r (void);
370 # define DEFINE_GETBY(h,nm,ky) \
371 extern void _nss_##h##_get##nm##by##ky##_r (void);
372 # include "function.def"
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
);
421 static name_database
*
423 nss_parse_file (const char *fname
)
426 name_database
*result
;
427 name_database_entry
*last
;
431 /* Open the configuration file. */
432 fp
= fopen (fname
, "r");
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
);