1 /* Copyright (C) 1996 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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 #include <libc-lock.h>
30 #include "../elf/link.h" /* We need some help from ld.so. */
32 /* Prototypes for the local functions. */
33 static void nss_init (void);
34 static void *nss_lookup_function (service_user
*ni
, const char *fct_name
);
35 static int nss_find_entry (struct entry
**knownp
, const char *key
,
37 static void nss_insert_entry (struct entry
**knownp
, const char *key
,
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 __libc_lock_define_initialized (static, lock
);
49 /* Global variable. */
50 struct __res_state _res
;
53 /* Nonzero if the sevices are already initialized. */
54 static int nss_initialized
;
57 /* The root of the whole data base. */
58 static name_database
*service_table
;
64 /* Prevent multiple threads to change the service table. */
65 __libc_lock_lock (lock
);
67 if (service_table
== NULL
)
68 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
70 __libc_lock_unlock (lock
);
74 /* -1 == database not found
75 0 == database entry pointer stored */
77 __nss_database_lookup (const char *database
, const char *defconfig
,
80 name_database_entry
*entry
;
82 if (nss_initialized
== 0)
85 /* Test whether configuration data is available. */
88 /* Return first `service_user' entry for DATABASE.
89 XXX Will use perfect hashing function for known databases. */
91 /* XXX Could use some faster mechanism here. But each database is
92 only requested once and so this might not be critical. */
93 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
94 if (strcmp (database
, entry
->name
) == 0)
101 /* No configuration data is available, either because nsswitch.conf
102 doesn't exist or because it doesn't have a line for this database. */
103 entry
= malloc (sizeof *entry
);
106 entry
->name
= database
;
107 /* DEFCONFIG specifies the default service list for this database,
108 or null to use the most common default. */
109 entry
->service
= nss_parse_service_list (defconfig
?:
110 "compat [NOTFOUND=return] files");
112 *ni
= entry
->service
;
118 0 == adjusted for next function */
120 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
122 *fctp
= nss_lookup_function (*ni
, fct_name
);
125 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
126 && (*ni
)->next
!= NULL
)
130 *fctp
= nss_lookup_function (*ni
, fct_name
);
133 return *fctp
!= NULL
? 0 : -1;
138 0 == adjusted for next function
141 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
146 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
147 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
148 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
149 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
154 /* This is really only for debugging. */
155 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_SUCCESS
)
156 __libc_fatal ("illegal status in " __FUNCTION__
);
158 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
162 if ((*ni
)->next
== NULL
)
169 *fctp
= nss_lookup_function (*ni
, fct_name
);
172 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
173 && (*ni
)->next
!= NULL
);
175 return *fctp
!= NULL
? 0 : -1;
180 nss_dlerror_run (void (*operate
) (void))
182 const char *last_errstring
= NULL
;
183 const char *last_object_name
= NULL
;
185 (void) _dl_catch_error (&last_errstring
, &last_object_name
, operate
);
187 return last_errstring
!= NULL
;
192 nss_lookup_function (service_user
*ni
, const char *fct_name
)
196 /* Determine whether current function is loaded. */
197 if (nss_find_entry (&ni
->known
, fct_name
, &result
) >= 0)
200 /* We now modify global data. Protect it. */
201 __libc_lock_lock (lock
);
203 if (ni
->library
== NULL
)
205 /* This service has not yet been used. Fetch the service library
206 for it, creating a new one if need be. If there is no service
207 table from the file, this static variable holds the head of the
208 service_library list made from the default configuration. */
209 static name_database default_table
;
210 ni
->library
= nss_new_service (service_table
?: &default_table
,
212 if (ni
->library
== NULL
)
214 /* This only happens when out of memory. */
215 __libc_lock_unlock (lock
);
220 if (ni
->library
->lib_handle
== NULL
)
222 /* Load the shared library. */
223 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
224 + sizeof (NSS_SHLIB_REVISION
));
225 char shlib_name
[shlen
];
229 /* Open and relocate the shared object. */
230 ni
->library
->lib_handle
= _dl_open (shlib_name
, RTLD_LAZY
);
233 /* Construct name. */
234 __stpcpy (__stpcpy (__stpcpy (shlib_name
, "libnss_"), ni
->library
->name
),
235 ".so" NSS_SHLIB_REVISION
);
237 if (nss_dlerror_run (do_open
) != 0)
238 /* Failed to load the library. */
239 ni
->library
->lib_handle
= (void *) -1;
242 if (ni
->library
->lib_handle
== (void *) -1)
243 /* Library not found => function not found. */
247 /* Get the desired function. Again, GNU ld.so magic ahead. */
248 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
249 + strlen (fct_name
) + 1);
251 struct link_map
*map
= ni
->library
->lib_handle
;
253 const ElfW(Sym
) *ref
= NULL
;
256 struct link_map
*scope
[2] = { map
, NULL
};
257 loadbase
= _dl_lookup_symbol (name
, &ref
, scope
, map
->l_name
, 0, 0);
260 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
265 result
= (nss_dlerror_run (get_sym
)
266 ? NULL
: (void *) (loadbase
+ ref
->st_value
));
269 /* Remember function pointer for the usage. */
270 nss_insert_entry (&ni
->known
, fct_name
, result
);
272 /* Remove the lock. */
273 __libc_lock_unlock (lock
);
280 known_compare (const void *p1
, const void *p2
)
282 known_function
*v1
= (known_function
*) p1
;
283 known_function
*v2
= (known_function
*) p2
;
285 return strcmp (v1
->fct_name
, v2
->fct_name
);
290 nss_find_entry (struct entry
**knownp
, const char *key
, void **valp
)
292 known_function looking_for
= { fct_name
: key
};
293 struct entry
**found
;
295 found
= __tfind (&looking_for
, (const void **) knownp
, known_compare
);
300 *valp
= ((known_function
*) (*found
)->key
)->fct_ptr
;
307 nss_insert_entry (struct entry
**knownp
, const char *key
, void *val
)
309 known_function
*to_insert
;
311 to_insert
= (known_function
*) malloc (sizeof (known_function
));
312 if (to_insert
== NULL
)
315 to_insert
->fct_name
= key
;
316 to_insert
->fct_ptr
= val
;
318 __tsearch (to_insert
, (void **) knownp
, known_compare
);
322 static name_database
*
323 nss_parse_file (const char *fname
)
326 name_database
*result
;
327 name_database_entry
*last
;
331 /* Open the configuration file. */
332 fp
= fopen (fname
, "r");
336 result
= (name_database
*) malloc (sizeof (name_database
));
340 result
->entry
= NULL
;
341 result
->library
= NULL
;
347 name_database_entry
*this;
351 n
= __getline (&line
, &len
, fp
);
354 if (line
[n
- 1] == '\n')
357 /* Because the file format does not know any form of quoting we
358 can search forward for the next '#' character and if found
359 make it terminating the line. */
360 cp
= strchr (line
, '#');
364 /* If the line is blank it is ignored. */
368 /* Each line completely specifies the actions for a database. */
369 this = nss_getline (line
);
375 result
->entry
= this;
382 /* Free the buffer. */
384 /* Close configuration file. */
391 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
392 static service_user
*
393 nss_parse_service_list (const char *line
)
395 service_user
*result
= NULL
, **nextp
= &result
;
399 service_user
*new_service
;
402 while (isspace (line
[0]))
405 /* No source specified. */
408 /* Read <source> identifier. */
410 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
416 new_service
= (service_user
*) malloc (sizeof (service_user
));
417 if (new_service
== NULL
)
421 char *source
= (char *) malloc (line
- name
+ 1);
427 memcpy (source
, name
, line
- name
);
428 source
[line
- name
] = '\0';
430 new_service
->name
= source
;
433 /* Set default actions. */
434 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
435 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
436 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
437 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
438 new_service
->library
= NULL
;
439 new_service
->known
= NULL
;
440 new_service
->next
= NULL
;
442 while (isspace (line
[0]))
447 /* Read criterions. */
450 while (line
[0] != '\0' && isspace (line
[0]));
455 enum nss_status status
;
456 lookup_actions action
;
458 /* Grok ! before name to mean all statii but that one. */
459 if (not = line
[0] == '!')
462 /* Read status name. */
464 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
468 /* Compare with known statii. */
469 if (line
- name
== 7)
471 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
472 status
= NSS_STATUS_SUCCESS
;
473 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
474 status
= NSS_STATUS_UNAVAIL
;
478 else if (line
- name
== 8)
480 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
481 status
= NSS_STATUS_NOTFOUND
;
482 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
483 status
= NSS_STATUS_TRYAGAIN
;
490 while (isspace (line
[0]))
496 while (isspace (line
[0]));
499 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
503 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
504 action
= NSS_ACTION_RETURN
;
505 else if (line
- name
== 8
506 && __strncasecmp (name
, "CONTINUE", 8) == 0)
507 action
= NSS_ACTION_CONTINUE
;
513 /* Save the current action setting for this status,
514 set them all to the given action, and reset this one. */
515 const lookup_actions save
= new_service
->actions
[2 + status
];
516 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
517 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
518 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
519 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
520 new_service
->actions
[2 + status
] = save
;
523 new_service
->actions
[2 + status
] = action
;
525 /* Skip white spaces. */
526 while (isspace (line
[0]))
529 while (line
[0] != ']');
535 *nextp
= new_service
;
536 nextp
= &new_service
->next
;
540 static name_database_entry
*
541 nss_getline (char *line
)
544 name_database_entry
*result
;
546 /* Ignore leading white spaces. ATTENTION: this is different from
547 what is implemented in Solaris. The Solaris man page says a line
548 beginning with a white space character is ignored. We regard
549 this as just another misfeature in Solaris. */
550 while (isspace (line
[0]))
553 /* Recognize `<database> ":"'. */
555 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
557 if (line
[0] == '\0' || name
== line
)
562 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
));
566 /* Save the database name. */
568 const size_t len
= strlen (name
) + 1;
569 char *new = malloc (len
);
575 result
->name
= memcpy (new, name
, len
);
578 /* Parse the list of services. */
579 result
->service
= nss_parse_service_list (line
);
586 static service_library
*
587 nss_new_service (name_database
*database
, const char *name
)
589 service_library
**currentp
= &database
->library
;
591 while (*currentp
!= NULL
)
593 if (strcmp ((*currentp
)->name
, name
) == 0)
595 currentp
= &(*currentp
)->next
;
598 /* We have to add the new service. */
599 *currentp
= (service_library
*) malloc (sizeof (service_library
));
600 if (*currentp
== NULL
)
603 (*currentp
)->name
= name
;
604 (*currentp
)->lib_handle
= NULL
;
605 (*currentp
)->next
= NULL
;