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 name_database
*nss_parse_file (const char *fname
);
36 static name_database_entry
*nss_getline (char *line
);
37 static service_user
*nss_parse_service_list (const char *line
);
38 static service_library
*nss_new_service (name_database
*database
,
42 __libc_lock_define_initialized (static, lock
);
45 /* Global variable. */
46 struct __res_state _res
;
49 /* Nonzero if the sevices are already initialized. */
50 static int nss_initialized
;
53 /* The root of the whole data base. */
54 static name_database
*service_table
;
60 /* Prevent multiple threads to change the service table. */
61 __libc_lock_lock (lock
);
63 if (service_table
== NULL
)
64 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
66 __libc_lock_unlock (lock
);
70 /* -1 == database not found
71 0 == database entry pointer stored */
73 __nss_database_lookup (const char *database
, const char *defconfig
,
76 name_database_entry
*entry
;
78 if (nss_initialized
== 0)
81 /* Test whether configuration data is available. */
84 /* Return first `service_user' entry for DATABASE.
85 XXX Will use perfect hashing function for known databases. */
87 /* XXX Could use some faster mechanism here. But each database is
88 only requested once and so this might not be critical. */
89 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
90 if (strcmp (database
, entry
->name
) == 0)
97 /* No configuration data is available, either because nsswitch.conf
98 doesn't exist or because it doesn't have a line for this database. */
99 entry
= malloc (sizeof *entry
);
102 entry
->name
= database
;
103 /* DEFCONFIG specifies the default service list for this database,
104 or null to use the most common default. */
105 entry
->service
= nss_parse_service_list (defconfig
?:
106 "compat [NOTFOUND=return] files");
108 *ni
= entry
->service
;
114 0 == adjusted for next function */
116 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
118 *fctp
= nss_lookup_function (*ni
, fct_name
);
121 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
122 && (*ni
)->next
!= NULL
)
126 *fctp
= nss_lookup_function (*ni
, fct_name
);
129 return *fctp
!= NULL
? 0 : -1;
134 0 == adjusted for next function
137 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
142 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
143 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
144 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
145 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
150 /* This is really only for debugging. */
151 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_SUCCESS
)
152 __libc_fatal ("illegal status in " __FUNCTION__
);
154 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
158 if ((*ni
)->next
== NULL
)
165 *fctp
= nss_lookup_function (*ni
, fct_name
);
168 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
169 && (*ni
)->next
!= NULL
);
171 return *fctp
!= NULL
? 0 : -1;
176 nss_dlerror_run (void (*operate
) (void))
178 const char *last_errstring
= NULL
;
179 const char *last_object_name
= NULL
;
181 (void) _dl_catch_error (&last_errstring
, &last_object_name
, operate
);
183 return last_errstring
!= NULL
;
188 nss_lookup_function (service_user
*ni
, const char *fct_name
)
190 /* Comparison function for searching NI->known tree. */
191 int known_compare (const void *p1
, const void *p2
)
193 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
194 *(const char *const *) p2
);
196 void **found
, *result
;
198 /* We now modify global data. Protect it. */
199 __libc_lock_lock (lock
);
201 /* Search the tree of functions previously requested. Data in the
202 tree are `known_function' structures, whose first member is a
203 `const char *', the lookup key. The search returns a pointer to
204 the tree node structure; the first member of the is a pointer to
205 our structure (i.e. what will be a `known_function'); since the
206 first member of that is the lookup key string, &FCT_NAME is close
207 enough to a pointer to our structure to use as a lookup key that
208 will be passed to `known_compare' (above). */
210 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
211 if (*found
!= &fct_name
)
212 /* The search found an existing structure in the tree. */
213 result
= ((known_function
*) *found
)->fct_ptr
;
216 /* This name was not known before. Now we have a node in the tree
217 (in the proper sorted position for FCT_NAME) that points to
218 &FCT_NAME instead of any real `known_function' structure.
219 Allocate a new structure and fill it in. */
221 known_function
*known
= malloc (sizeof *known
);
225 /* Oops. We can't instantiate this node properly.
226 Remove it from the tree. */
227 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
232 /* Point the tree node at this new structure. */
234 known
->fct_name
= fct_name
;
236 if (ni
->library
== NULL
)
238 /* This service has not yet been used. Fetch the service
239 library for it, creating a new one if need be. If there
240 is no service table from the file, this static variable
241 holds the head of the service_library list made from the
242 default configuration. */
243 static name_database default_table
;
244 ni
->library
= nss_new_service (service_table
?: &default_table
,
246 if (ni
->library
== NULL
)
248 /* This only happens when out of memory. */
250 goto remove_from_tree
;
254 if (ni
->library
->lib_handle
== NULL
)
256 /* Load the shared library. */
257 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
258 + sizeof (NSS_SHLIB_REVISION
));
259 char shlib_name
[shlen
];
263 /* Open and relocate the shared object. */
264 ni
->library
->lib_handle
= _dl_open (shlib_name
, RTLD_LAZY
);
267 /* Construct shared object name. */
268 __stpcpy (__stpcpy (__stpcpy (shlib_name
, "libnss_"),
270 ".so" NSS_SHLIB_REVISION
);
272 if (nss_dlerror_run (do_open
) != 0)
273 /* Failed to load the library. */
274 ni
->library
->lib_handle
= (void *) -1;
277 if (ni
->library
->lib_handle
== (void *) -1)
278 /* Library not found => function not found. */
282 /* Get the desired function. Again, GNU ld.so magic ahead. */
283 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
284 + strlen (fct_name
) + 1);
286 struct link_map
*map
= ni
->library
->lib_handle
;
288 const ElfW(Sym
) *ref
= NULL
;
291 struct link_map
*scope
[2] = { map
, NULL
};
292 loadbase
= _dl_lookup_symbol (name
, &ref
,
293 scope
, map
->l_name
, 0, 0);
296 /* Construct the function name. */
297 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
302 /* Look up the symbol. */
303 result
= (nss_dlerror_run (get_sym
)
304 ? NULL
: (void *) (loadbase
+ ref
->st_value
));
307 /* Remember function pointer for later calls. Even if null, we
308 record it so a second try needn't search the library again. */
309 known
->fct_ptr
= result
;
313 /* Remove the lock. */
314 __libc_lock_unlock (lock
);
320 static name_database
*
321 nss_parse_file (const char *fname
)
324 name_database
*result
;
325 name_database_entry
*last
;
329 /* Open the configuration file. */
330 fp
= fopen (fname
, "r");
334 result
= (name_database
*) malloc (sizeof (name_database
));
338 result
->entry
= NULL
;
339 result
->library
= NULL
;
345 name_database_entry
*this;
349 n
= __getline (&line
, &len
, fp
);
352 if (line
[n
- 1] == '\n')
355 /* Because the file format does not know any form of quoting we
356 can search forward for the next '#' character and if found
357 make it terminating the line. */
358 cp
= strchr (line
, '#');
362 /* If the line is blank it is ignored. */
366 /* Each line completely specifies the actions for a database. */
367 this = nss_getline (line
);
373 result
->entry
= this;
380 /* Free the buffer. */
382 /* Close configuration file. */
389 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
390 static service_user
*
391 nss_parse_service_list (const char *line
)
393 service_user
*result
= NULL
, **nextp
= &result
;
397 service_user
*new_service
;
400 while (isspace (line
[0]))
403 /* No source specified. */
406 /* Read <source> identifier. */
408 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
414 new_service
= (service_user
*) malloc (sizeof (service_user
));
415 if (new_service
== NULL
)
419 char *source
= (char *) malloc (line
- name
+ 1);
425 memcpy (source
, name
, line
- name
);
426 source
[line
- name
] = '\0';
428 new_service
->name
= source
;
431 /* Set default actions. */
432 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
433 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
434 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
435 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
436 new_service
->library
= NULL
;
437 new_service
->known
= NULL
;
438 new_service
->next
= NULL
;
440 while (isspace (line
[0]))
445 /* Read criterions. */
448 while (line
[0] != '\0' && isspace (line
[0]));
453 enum nss_status status
;
454 lookup_actions action
;
456 /* Grok ! before name to mean all statii but that one. */
457 if (not = line
[0] == '!')
460 /* Read status name. */
462 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
466 /* Compare with known statii. */
467 if (line
- name
== 7)
469 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
470 status
= NSS_STATUS_SUCCESS
;
471 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
472 status
= NSS_STATUS_UNAVAIL
;
476 else if (line
- name
== 8)
478 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
479 status
= NSS_STATUS_NOTFOUND
;
480 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
481 status
= NSS_STATUS_TRYAGAIN
;
488 while (isspace (line
[0]))
494 while (isspace (line
[0]));
497 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
501 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
502 action
= NSS_ACTION_RETURN
;
503 else if (line
- name
== 8
504 && __strncasecmp (name
, "CONTINUE", 8) == 0)
505 action
= NSS_ACTION_CONTINUE
;
511 /* Save the current action setting for this status,
512 set them all to the given action, and reset this one. */
513 const lookup_actions save
= new_service
->actions
[2 + status
];
514 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
515 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
516 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
517 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
518 new_service
->actions
[2 + status
] = save
;
521 new_service
->actions
[2 + status
] = action
;
523 /* Skip white spaces. */
524 while (isspace (line
[0]))
527 while (line
[0] != ']');
533 *nextp
= new_service
;
534 nextp
= &new_service
->next
;
538 static name_database_entry
*
539 nss_getline (char *line
)
542 name_database_entry
*result
;
544 /* Ignore leading white spaces. ATTENTION: this is different from
545 what is implemented in Solaris. The Solaris man page says a line
546 beginning with a white space character is ignored. We regard
547 this as just another misfeature in Solaris. */
548 while (isspace (line
[0]))
551 /* Recognize `<database> ":"'. */
553 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
555 if (line
[0] == '\0' || name
== line
)
560 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
));
564 /* Save the database name. */
566 const size_t len
= strlen (name
) + 1;
567 char *new = malloc (len
);
573 result
->name
= memcpy (new, name
, len
);
576 /* Parse the list of services. */
577 result
->service
= nss_parse_service_list (line
);
584 static service_library
*
585 nss_new_service (name_database
*database
, const char *name
)
587 service_library
**currentp
= &database
->library
;
589 while (*currentp
!= NULL
)
591 if (strcmp ((*currentp
)->name
, name
) == 0)
593 currentp
= &(*currentp
)->next
;
596 /* We have to add the new service. */
597 *currentp
= (service_library
*) malloc (sizeof (service_library
));
598 if (*currentp
== NULL
)
601 (*currentp
)->name
= name
;
602 (*currentp
)->lib_handle
= NULL
;
603 (*currentp
)->next
= NULL
;