1 /* Copyright (C) 1996, 1997 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>
25 #include <link.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 void *nss_lookup_function (service_user
*ni
, const char *fct_name
);
39 static name_database
*nss_parse_file (const char *fname
);
40 static name_database_entry
*nss_getline (char *line
);
41 static service_user
*nss_parse_service_list (const char *line
);
42 static service_library
*nss_new_service (name_database
*database
,
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
)
70 #if !defined DO_STATIC_NSS || defined PIC
71 /* String with revision number of the shared object files. */
72 static const char *const __nss_shlib_revision
= LIBNSS_FILES_SO
+ 15;
75 /* The root of the whole data base. */
76 static name_database
*service_table
;
79 /* -1 == database not found
80 0 == database entry pointer stored */
82 __nss_database_lookup (const char *database
, const char *alternate_name
,
83 const char *defconfig
, service_user
**ni
)
85 /* Prevent multiple threads to change the service table. */
86 __libc_lock_lock (lock
);
88 /* Reconsider database variable in case some other thread called
89 `__nss_configure_lookup' while we waited for the lock. */
92 __libc_lock_unlock (lock
);
96 /* Are we initialized yet? */
97 if (service_table
== NULL
)
98 /* Read config file. */
99 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
101 /* Test whether configuration data is available. */
102 if (service_table
!= NULL
)
104 /* Return first `service_user' entry for DATABASE. */
105 name_database_entry
*entry
;
107 /* XXX Could use some faster mechanism here. But each database is
108 only requested once and so this might not be critical. */
109 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
110 if (strcmp (database
, entry
->name
) == 0)
111 *ni
= entry
->service
;
113 if (*ni
== NULL
&& alternate_name
!= NULL
)
114 /* We haven't found an entry so far. Try to find it with the
116 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
117 if (strcmp (alternate_name
, entry
->name
) == 0)
118 *ni
= entry
->service
;
121 /* No configuration data is available, either because nsswitch.conf
122 doesn't exist or because it doesn't has a line for this database.
124 DEFCONFIG specifies the default service list for this database,
125 or null to use the most common default. */
127 *ni
= nss_parse_service_list (defconfig
128 ?: "nis [NOTFOUND=return] files");
130 __libc_lock_unlock (lock
);
137 0 == adjusted for next function */
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 : -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
;
253 const char *last_object_name
= NULL
;
256 (void) _dl_catch_error (&last_errstring
, &last_object_name
, operate
, args
);
258 result
= last_errstring
!= NULL
;
260 free (last_errstring
);
268 /* Argument to do_open. */
275 /* Arguments to get_sym. */
276 struct link_map
*map
;
279 /* Return values of get_sym. */
281 const ElfW(Sym
) *ref
;
287 struct do_open_args
*args
= (struct do_open_args
*) a
;
288 /* Open and relocate the shared object. */
289 args
->ni
->library
->lib_handle
= _dl_open (args
->shlib_name
, RTLD_LAZY
);
295 struct get_sym_args
*args
= (struct get_sym_args
*) a
;
296 struct link_map
*scope
[2] = { args
->map
, NULL
};
298 args
->loadbase
= _dl_lookup_symbol (args
->name
, &args
->ref
,
299 scope
, args
->map
->l_name
, 0);
303 /* Comparison function for searching NI->known tree. */
305 known_compare (const void *p1
, const void *p2
)
307 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
308 *(const char *const *) p2
);
313 nss_lookup_function (service_user
*ni
, const char *fct_name
)
315 void **found
, *result
;
317 /* We now modify global data. Protect it. */
318 __libc_lock_lock (lock
);
320 /* Search the tree of functions previously requested. Data in the
321 tree are `known_function' structures, whose first member is a
322 `const char *', the lookup key. The search returns a pointer to
323 the tree node structure; the first member of the is a pointer to
324 our structure (i.e. what will be a `known_function'); since the
325 first member of that is the lookup key string, &FCT_NAME is close
326 enough to a pointer to our structure to use as a lookup key that
327 will be passed to `known_compare' (above). */
329 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
330 if (*found
!= &fct_name
)
331 /* The search found an existing structure in the tree. */
332 result
= ((known_function
*) *found
)->fct_ptr
;
335 /* This name was not known before. Now we have a node in the tree
336 (in the proper sorted position for FCT_NAME) that points to
337 &FCT_NAME instead of any real `known_function' structure.
338 Allocate a new structure and fill it in. */
340 known_function
*known
= malloc (sizeof *known
);
344 /* Oops. We can't instantiate this node properly.
345 Remove it from the tree. */
346 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
351 /* Point the tree node at this new structure. */
353 known
->fct_name
= fct_name
;
355 if (ni
->library
== NULL
)
357 /* This service has not yet been used. Fetch the service
358 library for it, creating a new one if need be. If there
359 is no service table from the file, this static variable
360 holds the head of the service_library list made from the
361 default configuration. */
362 static name_database default_table
;
363 ni
->library
= nss_new_service (service_table
?: &default_table
,
365 if (ni
->library
== NULL
)
367 /* This only happens when out of memory. */
369 goto remove_from_tree
;
373 #if !defined DO_STATIC_NSS || defined PIC
374 if (ni
->library
->lib_handle
== NULL
)
376 /* Load the shared library. */
377 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
378 + strlen (__nss_shlib_revision
) + 1);
380 struct do_open_args args
;
381 args
.shlib_name
= __alloca (shlen
);
384 /* Construct shared object name. */
385 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args
.shlib_name
,
389 __nss_shlib_revision
);
391 if (nss_dlerror_run (do_open
, &args
) != 0)
392 /* Failed to load the library. */
393 ni
->library
->lib_handle
= (void *) -1l;
396 if (ni
->library
->lib_handle
== (void *) -1l)
397 /* Library not found => function not found. */
401 /* Get the desired function. Again, GNU ld.so magic ahead. */
402 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
403 + strlen (fct_name
) + 1);
404 struct get_sym_args args
;
405 args
.name
= __alloca (namlen
);
406 args
.map
= ni
->library
->lib_handle
;
408 /* Construct the function name. */
409 __stpcpy (__stpcpy (__stpcpy (__stpcpy (args
.name
, "_nss_"),
414 /* Look up the symbol. */
415 result
= (nss_dlerror_run (get_sym
, &args
) ? NULL
416 : (void *) (args
.loadbase
+ args
.ref
->st_value
));
419 /* We can't get function address dynamically in static linking. */
421 # define DEFINE_ENT(h,nm) \
422 extern void _nss_##h##_get##nm##ent_r (void); \
423 extern void _nss_##h##_end##nm##ent (void); \
424 extern void _nss_##h##_set##nm##ent (void);
425 # define DEFINE_GET(h,nm) \
426 extern void _nss_##h##_get##nm##_r (void);
427 # define DEFINE_GETBY(h,nm,ky) \
428 extern void _nss_##h##_get##nm##by##ky##_r (void);
429 # include "function.def"
433 # define DEFINE_ENT(h,nm) \
434 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
435 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
436 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
437 # define DEFINE_GET(h,nm) \
438 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
439 # define DEFINE_GETBY(h,nm,ky) \
440 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
441 static struct fct_tbl
{ const char *fname
; void *fp
; } *tp
, tbl
[] =
443 # include "function.def"
446 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
447 + strlen (fct_name
) + 1);
450 /* Construct the function name. */
451 __stpcpy (__stpcpy (__stpcpy (name
, ni
->library
->name
),
456 for (tp
= &tbl
[0]; tp
->fname
; tp
++)
457 if (strcmp (tp
->fname
, name
) == 0)
465 /* Remember function pointer for later calls. Even if null, we
466 record it so a second try needn't search the library again. */
467 known
->fct_ptr
= result
;
471 /* Remove the lock. */
472 __libc_lock_unlock (lock
);
478 static name_database
*
479 nss_parse_file (const char *fname
)
482 name_database
*result
;
483 name_database_entry
*last
;
487 /* Open the configuration file. */
488 fp
= fopen (fname
, "r");
492 result
= (name_database
*) malloc (sizeof (name_database
));
496 result
->entry
= NULL
;
497 result
->library
= NULL
;
503 name_database_entry
*this;
507 n
= __getline (&line
, &len
, fp
);
510 if (line
[n
- 1] == '\n')
513 /* Because the file format does not know any form of quoting we
514 can search forward for the next '#' character and if found
515 make it terminating the line. */
516 cp
= strchr (line
, '#');
520 /* If the line is blank it is ignored. */
524 /* Each line completely specifies the actions for a database. */
525 this = nss_getline (line
);
531 result
->entry
= this;
538 /* Free the buffer. */
540 /* Close configuration file. */
547 /* Read the source names:
548 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
550 static service_user
*
551 nss_parse_service_list (const char *line
)
553 service_user
*result
= NULL
, **nextp
= &result
;
557 service_user
*new_service
;
560 while (isspace (line
[0]))
563 /* No source specified. */
566 /* Read <source> identifier. */
568 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
574 new_service
= (service_user
*) malloc (sizeof (service_user
));
575 if (new_service
== NULL
)
579 char *source
= (char *) malloc (line
- name
+ 1);
585 memcpy (source
, name
, line
- name
);
586 source
[line
- name
] = '\0';
588 new_service
->name
= source
;
591 /* Set default actions. */
592 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
593 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
594 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
595 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
596 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
597 new_service
->library
= NULL
;
598 new_service
->known
= NULL
;
599 new_service
->next
= NULL
;
601 while (isspace (line
[0]))
606 /* Read criterions. */
609 while (line
[0] != '\0' && isspace (line
[0]));
614 enum nss_status status
;
615 lookup_actions action
;
617 /* Grok ! before name to mean all statii but that one. */
618 if (not = line
[0] == '!')
621 /* Read status name. */
623 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
627 /* Compare with known statii. */
628 if (line
- name
== 7)
630 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
631 status
= NSS_STATUS_SUCCESS
;
632 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
633 status
= NSS_STATUS_UNAVAIL
;
637 else if (line
- name
== 8)
639 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
640 status
= NSS_STATUS_NOTFOUND
;
641 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
642 status
= NSS_STATUS_TRYAGAIN
;
649 while (isspace (line
[0]))
655 while (isspace (line
[0]));
658 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
662 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
663 action
= NSS_ACTION_RETURN
;
664 else if (line
- name
== 8
665 && __strncasecmp (name
, "CONTINUE", 8) == 0)
666 action
= NSS_ACTION_CONTINUE
;
672 /* Save the current action setting for this status,
673 set them all to the given action, and reset this one. */
674 const lookup_actions save
= new_service
->actions
[2 + status
];
675 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
676 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
677 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
678 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
679 new_service
->actions
[2 + status
] = save
;
682 new_service
->actions
[2 + status
] = action
;
684 /* Skip white spaces. */
685 while (isspace (line
[0]))
688 while (line
[0] != ']');
694 *nextp
= new_service
;
695 nextp
= &new_service
->next
;
699 static name_database_entry
*
700 nss_getline (char *line
)
703 name_database_entry
*result
;
705 /* Ignore leading white spaces. ATTENTION: this is different from
706 what is implemented in Solaris. The Solaris man page says a line
707 beginning with a white space character is ignored. We regard
708 this as just another misfeature in Solaris. */
709 while (isspace (line
[0]))
712 /* Recognize `<database> ":"'. */
714 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
716 if (line
[0] == '\0' || name
== line
)
721 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
));
725 /* Save the database name. */
727 const size_t len
= strlen (name
) + 1;
728 char *new = malloc (len
);
734 result
->name
= memcpy (new, name
, len
);
737 /* Parse the list of services. */
738 result
->service
= nss_parse_service_list (line
);
745 static service_library
*
746 nss_new_service (name_database
*database
, const char *name
)
748 service_library
**currentp
= &database
->library
;
750 while (*currentp
!= NULL
)
752 if (strcmp ((*currentp
)->name
, name
) == 0)
754 currentp
= &(*currentp
)->next
;
757 /* We have to add the new service. */
758 *currentp
= (service_library
*) malloc (sizeof (service_library
));
759 if (*currentp
== NULL
)
762 (*currentp
)->name
= name
;
763 (*currentp
)->lib_handle
= NULL
;
764 (*currentp
)->next
= NULL
;