]> sourceware.org Git - glibc.git/blob - nss/nsswitch.c
Mon Jul 1 12:29:50 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / nss / nsswitch.c
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.
4
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.
9
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.
14
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. */
19
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <netdb.h>
23 #include <libc-lock.h>
24 #include <search.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "nsswitch.h"
30 #include "../elf/link.h" /* We need some help from ld.so. */
31
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,
36 void **valp);
37 static void nss_insert_entry (struct entry **knownp, const char *key,
38 void *val);
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,
43 const char *name);
44
45
46 __libc_lock_define_initialized (static, lock);
47
48
49 /* Global variable. */
50 struct __res_state _res;
51
52
53 /* Nonzero if the sevices are already initialized. */
54 static int nss_initialized;
55
56
57 /* The root of the whole data base. */
58 static name_database *service_table;
59
60
61 static void
62 nss_init (void)
63 {
64 /* Prevent multiple threads to change the service table. */
65 __libc_lock_lock (lock);
66
67 if (service_table == NULL)
68 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
69
70 __libc_lock_unlock (lock);
71 }
72
73
74 /* -1 == database not found
75 0 == database entry pointer stored */
76 int
77 __nss_database_lookup (const char *database, const char *defconfig,
78 service_user **ni)
79 {
80 name_database_entry *entry;
81
82 if (nss_initialized == 0)
83 nss_init ();
84
85 /* Test whether configuration data is available. */
86 if (service_table)
87 {
88 /* Return first `service_user' entry for DATABASE.
89 XXX Will use perfect hashing function for known databases. */
90
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)
95 {
96 *ni = entry->service;
97 return 0;
98 }
99 }
100
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);
104 if (entry == NULL)
105 return -1;
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");
111
112 *ni = entry->service;
113 return 0;
114 }
115
116
117 /* -1 == not found
118 0 == adjusted for next function */
119 int
120 __nss_lookup (service_user **ni, const char *fct_name, void **fctp)
121 {
122 *fctp = nss_lookup_function (*ni, fct_name);
123
124 while (*fctp == NULL
125 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
126 && (*ni)->next != NULL)
127 {
128 *ni = (*ni)->next;
129
130 *fctp = nss_lookup_function (*ni, fct_name);
131 }
132
133 return *fctp != NULL ? 0 : -1;
134 }
135
136
137 /* -1 == not found
138 0 == adjusted for next function
139 1 == finished */
140 int
141 __nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
142 int all_values)
143 {
144 if (all_values)
145 {
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)
150 return 1;
151 }
152 else
153 {
154 /* This is really only for debugging. */
155 if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
156 __libc_fatal ("illegal status in " __FUNCTION__);
157
158 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
159 return 1;
160 }
161
162 if ((*ni)->next == NULL)
163 return -1;
164
165 do
166 {
167 *ni = (*ni)->next;
168
169 *fctp = nss_lookup_function (*ni, fct_name);
170 }
171 while (*fctp == NULL
172 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
173 && (*ni)->next != NULL);
174
175 return *fctp != NULL ? 0 : -1;
176 }
177
178
179 static int
180 nss_dlerror_run (void (*operate) (void))
181 {
182 const char *last_errstring = NULL;
183 const char *last_object_name = NULL;
184
185 (void) _dl_catch_error (&last_errstring, &last_object_name, operate);
186
187 return last_errstring != NULL;
188 }
189
190
191 static void *
192 nss_lookup_function (service_user *ni, const char *fct_name)
193 {
194 void *result;
195
196 /* Determine whether current function is loaded. */
197 if (nss_find_entry (&ni->known, fct_name, &result) >= 0)
198 return result;
199
200 /* We now modify global data. Protect it. */
201 __libc_lock_lock (lock);
202
203 if (ni->library == NULL)
204 {
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,
211 ni->name);
212 if (ni->library == NULL)
213 {
214 /* This only happens when out of memory. */
215 __libc_lock_unlock (lock);
216 return NULL;
217 }
218 }
219
220 if (ni->library->lib_handle == NULL)
221 {
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];
226
227 void do_open (void)
228 {
229 /* Open and relocate the shared object. */
230 ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
231 }
232
233 /* Construct name. */
234 __stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"), ni->library->name),
235 ".so" NSS_SHLIB_REVISION);
236
237 if (nss_dlerror_run (do_open) != 0)
238 /* Failed to load the library. */
239 ni->library->lib_handle = (void *) -1;
240 }
241
242 if (ni->library->lib_handle == (void *) -1)
243 /* Library not found => function not found. */
244 result = NULL;
245 else
246 {
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);
250 char name[namlen];
251 struct link_map *map = ni->library->lib_handle;
252 ElfW(Addr) loadbase;
253 const ElfW(Sym) *ref = NULL;
254 void get_sym (void)
255 {
256 struct link_map *scope[2] = { map, NULL };
257 loadbase = _dl_lookup_symbol (name, &ref, scope, map->l_name, 0, 0);
258 }
259
260 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
261 ni->library->name),
262 "_"),
263 fct_name);
264
265 result = (nss_dlerror_run (get_sym)
266 ? NULL : (void *) (loadbase + ref->st_value));
267 }
268
269 /* Remember function pointer for the usage. */
270 nss_insert_entry (&ni->known, fct_name, result);
271
272 /* Remove the lock. */
273 __libc_lock_unlock (lock);
274
275 return result;
276 }
277
278
279 static int
280 known_compare (const void *p1, const void *p2)
281 {
282 known_function *v1 = (known_function *) p1;
283 known_function *v2 = (known_function *) p2;
284
285 return strcmp (v1->fct_name, v2->fct_name);
286 }
287
288
289 static int
290 nss_find_entry (struct entry **knownp, const char *key, void **valp)
291 {
292 known_function looking_for = { fct_name: key };
293 struct entry **found;
294
295 found = __tfind (&looking_for, (const void **) knownp, known_compare);
296
297 if (found == NULL)
298 return -1;
299
300 *valp = ((known_function *) (*found)->key)->fct_ptr;
301
302 return 0;
303 }
304
305
306 static void
307 nss_insert_entry (struct entry **knownp, const char *key, void *val)
308 {
309 known_function *to_insert;
310
311 to_insert = (known_function *) malloc (sizeof (known_function));
312 if (to_insert == NULL)
313 return;
314
315 to_insert->fct_name = key;
316 to_insert->fct_ptr = val;
317
318 __tsearch (to_insert, (void **) knownp, known_compare);
319 }
320
321
322 static name_database *
323 nss_parse_file (const char *fname)
324 {
325 FILE *fp;
326 name_database *result;
327 name_database_entry *last;
328 char *line;
329 size_t len;
330
331 /* Open the configuration file. */
332 fp = fopen (fname, "r");
333 if (fp == NULL)
334 return NULL;
335
336 result = (name_database *) malloc (sizeof (name_database));
337 if (result == NULL)
338 return NULL;
339
340 result->entry = NULL;
341 result->library = NULL;
342 last = NULL;
343 line = NULL;
344 len = 0;
345 do
346 {
347 name_database_entry *this;
348 ssize_t n;
349 char *cp;
350
351 n = __getline (&line, &len, fp);
352 if (n < 0)
353 break;
354 if (line[n - 1] == '\n')
355 line[n - 1] = '\0';
356
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, '#');
361 if (cp != NULL)
362 *cp = '\0';
363
364 /* If the line is blank it is ignored. */
365 if (line[0] == '\0')
366 continue;
367
368 /* Each line completely specifies the actions for a database. */
369 this = nss_getline (line);
370 if (this != NULL)
371 {
372 if (last != NULL)
373 last->next = this;
374 else
375 result->entry = this;
376
377 last = this;
378 }
379 }
380 while (!feof (fp));
381
382 /* Free the buffer. */
383 free (line);
384 /* Close configuration file. */
385 fclose (fp);
386
387 return result;
388 }
389
390
391 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
392 static service_user *
393 nss_parse_service_list (const char *line)
394 {
395 service_user *result = NULL, **nextp = &result;
396
397 while (1)
398 {
399 service_user *new_service;
400 const char *name;
401
402 while (isspace (line[0]))
403 ++line;
404 if (line[0] == '\0')
405 /* No source specified. */
406 return result;
407
408 /* Read <source> identifier. */
409 name = line;
410 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
411 ++line;
412 if (name == line)
413 return result;
414
415
416 new_service = (service_user *) malloc (sizeof (service_user));
417 if (new_service == NULL)
418 return result;
419 else
420 {
421 char *source = (char *) malloc (line - name + 1);
422 if (source == NULL)
423 {
424 free (new_service);
425 return result;
426 }
427 memcpy (source, name, line - name);
428 source[line - name] = '\0';
429
430 new_service->name = source;
431 }
432
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;
441
442 while (isspace (line[0]))
443 ++line;
444
445 if (line[0] == '[')
446 {
447 /* Read criterions. */
448 do
449 ++line;
450 while (line[0] != '\0' && isspace (line[0]));
451
452 do
453 {
454 int not;
455 enum nss_status status;
456 lookup_actions action;
457
458 /* Grok ! before name to mean all statii but that one. */
459 if (not = line[0] == '!')
460 ++line;
461
462 /* Read status name. */
463 name = line;
464 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
465 && line[0] != ']')
466 ++line;
467
468 /* Compare with known statii. */
469 if (line - name == 7)
470 {
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;
475 else
476 return result;
477 }
478 else if (line - name == 8)
479 {
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;
484 else
485 return result;
486 }
487 else
488 return result;
489
490 while (isspace (line[0]))
491 ++line;
492 if (line[0] != '=')
493 return result;
494 do
495 ++line;
496 while (isspace (line[0]));
497
498 name = line;
499 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
500 && line[0] != ']')
501 ++line;
502
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;
508 else
509 return result;
510
511 if (not)
512 {
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;
521 }
522 else
523 new_service->actions[2 + status] = action;
524
525 /* Skip white spaces. */
526 while (isspace (line[0]))
527 ++line;
528 }
529 while (line[0] != ']');
530
531 /* Skip the ']'. */
532 ++line;
533 }
534
535 *nextp = new_service;
536 nextp = &new_service->next;
537 }
538 }
539
540 static name_database_entry *
541 nss_getline (char *line)
542 {
543 const char *name;
544 name_database_entry *result;
545
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]))
551 ++line;
552
553 /* Recognize `<database> ":"'. */
554 name = line;
555 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
556 ++line;
557 if (line[0] == '\0' || name == line)
558 /* Syntax error. */
559 return NULL;
560 *line++ = '\0';
561
562 result = (name_database_entry *) malloc (sizeof (name_database_entry));
563 if (result == NULL)
564 return NULL;
565
566 /* Save the database name. */
567 {
568 const size_t len = strlen (name) + 1;
569 char *new = malloc (len);
570 if (new == NULL)
571 {
572 free (result);
573 return NULL;
574 }
575 result->name = memcpy (new, name, len);
576 }
577
578 /* Parse the list of services. */
579 result->service = nss_parse_service_list (line);
580
581 result->next = NULL;
582 return result;
583 }
584
585
586 static service_library *
587 nss_new_service (name_database *database, const char *name)
588 {
589 service_library **currentp = &database->library;
590
591 while (*currentp != NULL)
592 {
593 if (strcmp ((*currentp)->name, name) == 0)
594 return *currentp;
595 currentp = &(*currentp)->next;
596 }
597
598 /* We have to add the new service. */
599 *currentp = (service_library *) malloc (sizeof (service_library));
600 if (*currentp == NULL)
601 return NULL;
602
603 (*currentp)->name = name;
604 (*currentp)->lib_handle = NULL;
605 (*currentp)->next = NULL;
606
607 return *currentp;
608 }
This page took 0.09255 seconds and 5 git commands to generate.