1 /* Copyright (C) 1996, 1997, 1998 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>
29 #include <elf/ldsodefs.h> /* We need some help from ld.so. */
31 #if !defined DO_STATIC_NSS || defined PIC
32 # include <gnu/lib-names.h>
37 /* Prototypes for the local functions. */
38 static name_database
*nss_parse_file (const char *fname
) internal_function
;
39 static name_database_entry
*nss_getline (char *line
) internal_function
;
40 static service_user
*nss_parse_service_list (const char *line
)
42 static service_library
*nss_new_service (name_database
*database
,
43 const char *name
) internal_function
;
46 /* Declare external database variables. */
47 #define DEFINE_DATABASE(name) \
48 extern service_user *__nss_##name##_database; \
49 weak_extern (__nss_##name##_database)
50 #include "databases.def"
51 #undef DEFINE_DATABASE
53 /* Structure to map database name to variable. */
60 #define DEFINE_DATABASE(name) \
61 { #name, &__nss_##name##_database },
62 #include "databases.def"
63 #undef DEFINE_DATABASE
67 __libc_lock_define_initialized (static, lock
)
69 #if !defined DO_STATIC_NSS || defined PIC
70 /* String with revision number of the shared object files. */
71 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
74 /* The root of the whole data base. */
75 static name_database
*service_table
;
78 /* -1 == database not found
79 0 == database entry pointer stored */
81 __nss_database_lookup (const char *database
, const char *alternate_name
,
82 const char *defconfig
, service_user
**ni
)
84 /* Prevent multiple threads to change the service table. */
85 __libc_lock_lock (lock
);
87 /* Reconsider database variable in case some other thread called
88 `__nss_configure_lookup' while we waited for the lock. */
91 __libc_lock_unlock (lock
);
95 /* Are we initialized yet? */
96 if (service_table
== NULL
)
97 /* Read config file. */
98 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
100 /* Test whether configuration data is available. */
101 if (service_table
!= NULL
)
103 /* Return first `service_user' entry for DATABASE. */
104 name_database_entry
*entry
;
106 /* XXX Could use some faster mechanism here. But each database is
107 only requested once and so this might not be critical. */
108 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
109 if (strcmp (database
, entry
->name
) == 0)
110 *ni
= entry
->service
;
112 if (*ni
== NULL
&& alternate_name
!= NULL
)
113 /* We haven't found an entry so far. Try to find it with the
115 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
116 if (strcmp (alternate_name
, entry
->name
) == 0)
117 *ni
= entry
->service
;
120 /* No configuration data is available, either because nsswitch.conf
121 doesn't exist or because it doesn't has a line for this database.
123 DEFCONFIG specifies the default service list for this database,
124 or null to use the most common default. */
126 *ni
= nss_parse_service_list (defconfig
127 ?: "nis [NOTFOUND=return] files");
129 __libc_lock_unlock (lock
);
139 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
141 *fctp
= __nss_lookup_function (*ni
, fct_name
);
144 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
145 && (*ni
)->next
!= NULL
)
149 *fctp
= __nss_lookup_function (*ni
, fct_name
);
152 return *fctp
!= NULL
? 0 : (*ni
)->next
== NULL
? 1 : -1;
157 0 == adjusted for next function
160 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
165 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
166 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
167 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
168 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
173 /* This is really only for debugging. */
174 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
175 __libc_fatal ("illegal status in " __FUNCTION__
);
177 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
181 if ((*ni
)->next
== NULL
)
188 *fctp
= __nss_lookup_function (*ni
, fct_name
);
191 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
192 && (*ni
)->next
!= NULL
);
194 return *fctp
!= NULL
? 0 : -1;
199 __nss_configure_lookup (const char *dbname
, const char *service_line
)
201 service_user
*new_db
;
204 for (cnt
= 0; cnt
< sizeof databases
; ++cnt
)
206 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
211 __set_errno (EINVAL
);
216 if (cnt
== sizeof databases
)
218 __set_errno (EINVAL
);
222 /* Test whether it is really used. */
223 if (databases
[cnt
].dbp
== NULL
)
224 /* Nothing to do, but we could do. */
227 /* Try to generate new data. */
228 new_db
= nss_parse_service_list (service_line
);
231 /* Illegal service specification. */
232 __set_errno (EINVAL
);
236 /* Prevent multiple threads to change the service table. */
237 __libc_lock_lock (lock
);
239 /* Install new rules. */
240 *databases
[cnt
].dbp
= new_db
;
242 __libc_lock_unlock (lock
);
248 #if !defined DO_STATIC_NSS || defined PIC
250 nss_dlerror_run (void (*operate
) (void *), void *args
)
252 char *last_errstring
= NULL
;
255 (void) _dl_catch_error (&last_errstring
, operate
, args
);
257 result
= last_errstring
!= NULL
;
259 free (last_errstring
);
267 /* Argument to do_open. */
274 /* Arguments to get_sym. */
275 struct link_map
*map
;
278 /* Return values of get_sym. */
280 const ElfW(Sym
) *ref
;
286 struct do_open_args
*args
= (struct do_open_args
*) a
;
287 /* Open and relocate the shared object. */
288 args
->ni
->library
->lib_handle
= _dl_open (args
->shlib_name
, RTLD_LAZY
);
294 struct get_sym_args
*args
= (struct get_sym_args
*) a
;
296 args
->loadbase
= _dl_lookup_symbol (args
->name
, &args
->ref
,
297 args
->map
->l_local_scope
,
298 args
->map
->l_name
, 0);
302 /* Comparison function for searching NI->known tree. */
304 known_compare (const void *p1
, const void *p2
)
306 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
307 *(const char *const *) p2
);
312 __nss_lookup_function (service_user
*ni
, const char *fct_name
)
314 void **found
, *result
;
316 /* We now modify global data. Protect it. */
317 __libc_lock_lock (lock
);
319 /* Search the tree of functions previously requested. Data in the
320 tree are `known_function' structures, whose first member is a
321 `const char *', the lookup key. The search returns a pointer to
322 the tree node structure; the first member of the is a pointer to
323 our structure (i.e. what will be a `known_function'); since the
324 first member of that is the lookup key string, &FCT_NAME is close
325 enough to a pointer to our structure to use as a lookup key that
326 will be passed to `known_compare' (above). */
328 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
329 if (*found
!= &fct_name
)
330 /* The search found an existing structure in the tree. */
331 result
= ((known_function
*) *found
)->fct_ptr
;
334 /* This name was not known before. Now we have a node in the tree
335 (in the proper sorted position for FCT_NAME) that points to
336 &FCT_NAME instead of any real `known_function' structure.
337 Allocate a new structure and fill it in. */
339 known_function
*known
= malloc (sizeof *known
);
343 /* Oops. We can't instantiate this node properly.
344 Remove it from the tree. */
345 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
350 /* Point the tree node at this new structure. */
352 known
->fct_name
= fct_name
;
354 if (ni
->library
== NULL
)
356 /* This service has not yet been used. Fetch the service
357 library for it, creating a new one if need be. If there
358 is no service table from the file, this static variable
359 holds the head of the service_library list made from the
360 default configuration. */
361 static name_database default_table
;
362 ni
->library
= nss_new_service (service_table
?: &default_table
,
364 if (ni
->library
== NULL
)
366 /* This only happens when out of memory. */
368 goto remove_from_tree
;
372 #if !defined DO_STATIC_NSS || defined PIC
373 if (ni
->library
->lib_handle
== NULL
)
375 /* Load the shared library. */
376 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
377 + strlen (__nss_shlib_revision
) + 1);
378 int saved_errno
= errno
;
379 struct do_open_args args
;
380 args
.shlib_name
= __alloca (shlen
);
383 /* Construct shared object name. */
384 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args
.shlib_name
,
388 __nss_shlib_revision
);
390 if (nss_dlerror_run (do_open
, &args
) != 0)
392 /* Failed to load the library. */
393 ni
->library
->lib_handle
= (void *) -1l;
394 __set_errno (saved_errno
);
398 if (ni
->library
->lib_handle
== (void *) -1l)
399 /* Library not found => function not found. */
403 /* Get the desired function. Again, GNU ld.so magic ahead. */
404 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
405 + strlen (fct_name
) + 1);
406 struct get_sym_args args
;
407 args
.name
= __alloca (namlen
);
408 args
.map
= ni
->library
->lib_handle
;
410 /* Construct the function name. */
411 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args
.name
, "_nss_"),
416 /* Look up the symbol. */
417 result
= (nss_dlerror_run (get_sym
, &args
) ? NULL
418 : (void *) (args
.loadbase
+ args
.ref
->st_value
));
421 /* We can't get function address dynamically in static linking. */
423 # define DEFINE_ENT(h,nm) \
424 extern void _nss_##h##_get##nm##ent_r (void); \
425 extern void _nss_##h##_end##nm##ent (void); \
426 extern void _nss_##h##_set##nm##ent (void);
427 # define DEFINE_GET(h,nm) \
428 extern void _nss_##h##_get##nm##_r (void);
429 # define DEFINE_GETBY(h,nm,ky) \
430 extern void _nss_##h##_get##nm##by##ky##_r (void);
431 # include "function.def"
435 # define DEFINE_ENT(h,nm) \
436 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
437 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
438 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
439 # define DEFINE_GET(h,nm) \
440 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
441 # define DEFINE_GETBY(h,nm,ky) \
442 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
443 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
445 # include "function.def"
448 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
449 + strlen (fct_name
) + 1);
452 /* Construct the function name. */
453 __stpcpy (__stpcpy (__stpcpy (name
, ni
->library
->name
),
458 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
459 if (strcmp (tp
->fname
, name
) == 0)
467 /* Remember function pointer for later calls. Even if null, we
468 record it so a second try needn't search the library again. */
469 known
->fct_ptr
= result
;
473 /* Remove the lock. */
474 __libc_lock_unlock (lock
);
480 static name_database
*
482 nss_parse_file (const char *fname
)
485 name_database
*result
;
486 name_database_entry
*last
;
490 /* Open the configuration file. */
491 fp
= fopen (fname
, "r");
495 result
= (name_database
*) malloc (sizeof (name_database
));
499 result
->entry
= NULL
;
500 result
->library
= NULL
;
506 name_database_entry
*this;
510 n
= __getline (&line
, &len
, fp
);
513 if (line
[n
- 1] == '\n')
516 /* Because the file format does not know any form of quoting we
517 can search forward for the next '#' character and if found
518 make it terminating the line. */
519 cp
= strchr (line
, '#');
523 /* If the line is blank it is ignored. */
527 /* Each line completely specifies the actions for a database. */
528 this = nss_getline (line
);
534 result
->entry
= this;
539 while (!feof_unlocked (fp
));
541 /* Free the buffer. */
543 /* Close configuration file. */
550 /* Read the source names:
551 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
553 static service_user
*
555 nss_parse_service_list (const char *line
)
557 service_user
*result
= NULL
, **nextp
= &result
;
561 service_user
*new_service
;
564 while (isspace (line
[0]))
567 /* No source specified. */
570 /* Read <source> identifier. */
572 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
578 new_service
= (service_user
*) malloc (sizeof (service_user
));
579 if (new_service
== NULL
)
583 char *source
= (char *) malloc (line
- name
+ 1);
589 *((char *) __mempcpy (source
, name
, line
- name
)) = '\0';
591 new_service
->name
= source
;
594 /* Set default actions. */
595 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
596 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
597 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
598 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
599 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
600 new_service
->library
= NULL
;
601 new_service
->known
= NULL
;
602 new_service
->next
= NULL
;
604 while (isspace (line
[0]))
609 /* Read criterions. */
612 while (line
[0] != '\0' && isspace (line
[0]));
617 enum nss_status status
;
618 lookup_actions action
;
620 /* Grok ! before name to mean all statii but that one. */
621 not = line
[0] == '!';
625 /* Read status name. */
627 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
631 /* Compare with known statii. */
632 if (line
- name
== 7)
634 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
635 status
= NSS_STATUS_SUCCESS
;
636 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
637 status
= NSS_STATUS_UNAVAIL
;
641 else if (line
- name
== 8)
643 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
644 status
= NSS_STATUS_NOTFOUND
;
645 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
646 status
= NSS_STATUS_TRYAGAIN
;
653 while (isspace (line
[0]))
659 while (isspace (line
[0]));
662 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
666 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
667 action
= NSS_ACTION_RETURN
;
668 else if (line
- name
== 8
669 && __strncasecmp (name
, "CONTINUE", 8) == 0)
670 action
= NSS_ACTION_CONTINUE
;
676 /* Save the current action setting for this status,
677 set them all to the given action, and reset this one. */
678 const lookup_actions save
= new_service
->actions
[2 + status
];
679 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
680 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
681 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
682 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
683 new_service
->actions
[2 + status
] = save
;
686 new_service
->actions
[2 + status
] = action
;
688 /* Skip white spaces. */
689 while (isspace (line
[0]))
692 while (line
[0] != ']');
698 *nextp
= new_service
;
699 nextp
= &new_service
->next
;
703 static name_database_entry
*
705 nss_getline (char *line
)
708 name_database_entry
*result
;
710 /* Ignore leading white spaces. ATTENTION: this is different from
711 what is implemented in Solaris. The Solaris man page says a line
712 beginning with a white space character is ignored. We regard
713 this as just another misfeature in Solaris. */
714 while (isspace (line
[0]))
717 /* Recognize `<database> ":"'. */
719 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
721 if (line
[0] == '\0' || name
== line
)
726 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
));
730 /* Save the database name. */
732 const size_t len
= strlen (name
) + 1;
733 char *new = malloc (len
);
739 result
->name
= memcpy (new, name
, len
);
742 /* Parse the list of services. */
743 result
->service
= nss_parse_service_list (line
);
750 static service_library
*
752 nss_new_service (name_database
*database
, const char *name
)
754 service_library
**currentp
= &database
->library
;
756 while (*currentp
!= NULL
)
758 if (strcmp ((*currentp
)->name
, name
) == 0)
760 currentp
= &(*currentp
)->next
;
763 /* We have to add the new service. */
764 *currentp
= (service_library
*) malloc (sizeof (service_library
));
765 if (*currentp
== NULL
)
768 (*currentp
)->name
= name
;
769 (*currentp
)->lib_handle
= NULL
;
770 (*currentp
)->next
= NULL
;