]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nsswitch.c
nss: Turn __nss_database_lookup into a compatibility symbol
[thirdparty/glibc.git] / nss / nsswitch.c
CommitLineData
04277e02 1/* Copyright (C) 1996-2019 Free Software Foundation, Inc.
2303f5fd
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5f0e6fc7 4
2303f5fd 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
5f0e6fc7 9
2303f5fd
UD
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
41bdb6e2 13 Lesser General Public License for more details.
5f0e6fc7 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
5f0e6fc7
RM
18
19#include <ctype.h>
20#include <dlfcn.h>
a8874ead 21#include <errno.h>
5f0e6fc7 22#include <netdb.h>
ec999b8e 23#include <libc-lock.h>
5f0e6fc7
RM
24#include <search.h>
25#include <stdio.h>
2706ee38 26#include <stdio_ext.h>
5f0e6fc7
RM
27#include <stdlib.h>
28#include <string.h>
5107cf1d 29
7f28638c
AJ
30#include <aliases.h>
31#include <grp.h>
32#include <netinet/ether.h>
33#include <pwd.h>
34#include <shadow.h>
35
b5567b2a 36#if !defined DO_STATIC_NSS || defined SHARED
5107cf1d
UD
37# include <gnu/lib-names.h>
38#endif
5f0e6fc7
RM
39
40#include "nsswitch.h"
ef4d5b32 41#include "../nscd/nscd_proto.h"
319b9ad4 42#include <sysdep.h>
a1a78204
SE
43#include <config.h>
44
45#ifdef LINK_OBSOLETE_NSL
46# define DEFAULT_CONFIG "compat [NOTFOUND=return] files"
47# define DEFAULT_DEFCONFIG "nis [NOTFOUND=return] files"
48#else
49# define DEFAULT_CONFIG "files"
50# define DEFAULT_DEFCONFIG "files"
51#endif
5f0e6fc7
RM
52
53/* Prototypes for the local functions. */
75b3047e
FW
54static name_database *nss_parse_file (const char *fname);
55static name_database_entry *nss_getline (char *line);
56static service_user *nss_parse_service_list (const char *line);
b1c608fe 57#if !defined DO_STATIC_NSS || defined SHARED
5f0e6fc7 58static service_library *nss_new_service (name_database *database,
75b3047e 59 const char *name);
b1c608fe 60#endif
5f0e6fc7
RM
61
62
a8874ead
UD
63/* Declare external database variables. */
64#define DEFINE_DATABASE(name) \
eaf6753f 65 service_user *__nss_##name##_database attribute_hidden; \
a8874ead
UD
66 weak_extern (__nss_##name##_database)
67#include "databases.def"
68#undef DEFINE_DATABASE
69
70/* Structure to map database name to variable. */
3d50529d 71static const struct
a8874ead 72{
3d50529d 73 const char name[10];
a8874ead
UD
74 service_user **dbp;
75} databases[] =
76{
77#define DEFINE_DATABASE(name) \
78 { #name, &__nss_##name##_database },
79#include "databases.def"
80#undef DEFINE_DATABASE
81};
e7c8359e 82#define ndatabases (sizeof (databases) / sizeof (databases[0]))
a8874ead 83
1dbbb1ec 84#ifdef USE_NSCD
c3e2f19b
UD
85/* Flags whether custom rules for database is set. */
86bool __nss_database_custom[NSS_DBSIDX_max];
1dbbb1ec 87#endif
c3e2f19b 88
a8874ead 89
1e16111c 90__libc_lock_define_initialized (static, lock)
5f0e6fc7 91
b5567b2a 92#if !defined DO_STATIC_NSS || defined SHARED
56552e42 93/* String with revision number of the shared object files. */
1fb05e3d 94static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15;
5107cf1d 95#endif
5f0e6fc7
RM
96
97/* The root of the whole data base. */
98static name_database *service_table;
99
d44638b0
PP
100/* List of default service lists that were generated by glibc because
101 /etc/nsswitch.conf did not provide a value.
102 The list is only maintained so we can free such service lists in
103 __libc_freeres. */
104static name_database_entry *defconfig_entries;
105
5f0e6fc7 106
3ab2021f 107#if defined USE_NSCD && (!defined DO_STATIC_NSS || defined SHARED)
319b9ad4
UD
108/* Nonzero if this is the nscd process. */
109static bool is_nscd;
110/* The callback passed to the init functions when nscd is used. */
111static void (*nscd_init_cb) (size_t, struct traced_file *);
3cc3ef96 112#endif
319b9ad4
UD
113
114
5f0e6fc7
RM
115/* -1 == database not found
116 0 == database entry pointer stored */
117int
a9368c34
FW
118__nss_database_lookup2 (const char *database, const char *alternate_name,
119 const char *defconfig, service_user **ni)
5f0e6fc7 120{
a8874ead
UD
121 /* Prevent multiple threads to change the service table. */
122 __libc_lock_lock (lock);
bba7bb78 123
a8874ead
UD
124 /* Reconsider database variable in case some other thread called
125 `__nss_configure_lookup' while we waited for the lock. */
126 if (*ni != NULL)
15a83d04
UD
127 {
128 __libc_lock_unlock (lock);
129 return 0;
130 }
a8874ead 131
56552e42
UD
132 /* Are we initialized yet? */
133 if (service_table == NULL)
a8874ead
UD
134 /* Read config file. */
135 service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
5f0e6fc7
RM
136
137 /* Test whether configuration data is available. */
a8874ead 138 if (service_table != NULL)
5f0e6fc7 139 {
a8874ead
UD
140 /* Return first `service_user' entry for DATABASE. */
141 name_database_entry *entry;
c66273aa
RM
142
143 /* XXX Could use some faster mechanism here. But each database is
144 only requested once and so this might not be critical. */
145 for (entry = service_table->entry; entry != NULL; entry = entry->next)
146 if (strcmp (database, entry->name) == 0)
15a83d04 147 *ni = entry->service;
68dbb3a6
UD
148
149 if (*ni == NULL && alternate_name != NULL)
e7fd8a39
UD
150 /* We haven't found an entry so far. Try to find it with the
151 alternative name. */
68dbb3a6
UD
152 for (entry = service_table->entry; entry != NULL; entry = entry->next)
153 if (strcmp (alternate_name, entry->name) == 0)
154 *ni = entry->service;
5f0e6fc7
RM
155 }
156
c66273aa 157 /* No configuration data is available, either because nsswitch.conf
319b9ad4 158 doesn't exist or because it doesn't have a line for this database.
a8874ead
UD
159
160 DEFCONFIG specifies the default service list for this database,
bba7bb78 161 or null to use the most common default. */
15a83d04 162 if (*ni == NULL)
d44638b0 163 {
a1a78204 164 *ni = nss_parse_service_list (defconfig ?: DEFAULT_DEFCONFIG);
d44638b0
PP
165 if (*ni != NULL)
166 {
167 /* Record the memory we've just allocated in defconfig_entries list,
168 so we can free it later. */
169 name_database_entry *entry;
170
171 /* Allocate ENTRY plus size of name (1 here). */
172 entry = (name_database_entry *) malloc (sizeof (*entry) + 1);
173
174 if (entry != NULL)
175 {
176 entry->next = defconfig_entries;
177 entry->service = *ni;
178 entry->name[0] = '\0';
179 defconfig_entries = entry;
180 }
181 }
182 }
a8874ead
UD
183
184 __libc_lock_unlock (lock);
bba7bb78 185
64031225 186 return *ni != NULL ? 0 : -1;
5f0e6fc7 187}
a9368c34 188libc_hidden_def (__nss_database_lookup2)
5f0e6fc7
RM
189
190
191/* -1 == not found
4317f9e1 192 0 == function found
14ea22e9 193 1 == finished */
5f0e6fc7 194int
384ca551
UD
195__nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name,
196 void **fctp)
5f0e6fc7 197{
899d423e 198 *fctp = __nss_lookup_function (*ni, fct_name);
384ca551 199 if (*fctp == NULL && fct2_name != NULL)
0dc6c5e4 200 *fctp = __nss_lookup_function (*ni, fct2_name);
5f0e6fc7
RM
201
202 while (*fctp == NULL
203 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
204 && (*ni)->next != NULL)
205 {
206 *ni = (*ni)->next;
207
899d423e 208 *fctp = __nss_lookup_function (*ni, fct_name);
384ca551 209 if (*fctp == NULL && fct2_name != NULL)
0dc6c5e4 210 *fctp = __nss_lookup_function (*ni, fct2_name);
5f0e6fc7
RM
211 }
212
14ea22e9 213 return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1;
5f0e6fc7 214}
684ae515 215libc_hidden_def (__nss_lookup)
5f0e6fc7
RM
216
217
218/* -1 == not found
219 0 == adjusted for next function
220 1 == finished */
221int
384ca551
UD
222__nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name,
223 void **fctp, int status, int all_values)
5f0e6fc7
RM
224{
225 if (all_values)
226 {
227 if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
228 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
229 && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
230 && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
231 return 1;
232 }
233 else
234 {
235 /* This is really only for debugging. */
384ca551
UD
236 if (__builtin_expect (NSS_STATUS_TRYAGAIN > status
237 || status > NSS_STATUS_RETURN, 0))
a6e8926f 238 __libc_fatal ("Illegal status in __nss_next.\n");
5f0e6fc7
RM
239
240 if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
241 return 1;
242 }
243
244 if ((*ni)->next == NULL)
245 return -1;
246
247 do
248 {
249 *ni = (*ni)->next;
250
899d423e 251 *fctp = __nss_lookup_function (*ni, fct_name);
384ca551
UD
252 if (*fctp == NULL && fct2_name != NULL)
253 *fctp = __nss_lookup_function (*ni, fct2_name);
5f0e6fc7
RM
254 }
255 while (*fctp == NULL
256 && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
257 && (*ni)->next != NULL);
258
259 return *fctp != NULL ? 0 : -1;
260}
384ca551
UD
261libc_hidden_def (__nss_next2)
262
a8874ead
UD
263int
264__nss_configure_lookup (const char *dbname, const char *service_line)
265{
266 service_user *new_db;
267 size_t cnt;
268
e7c8359e 269 for (cnt = 0; cnt < ndatabases; ++cnt)
df21c858
UD
270 {
271 int cmp = strcmp (dbname, databases[cnt].name);
272 if (cmp == 0)
273 break;
5a97622d 274 if (cmp < 0)
df21c858 275 {
c4029823 276 __set_errno (EINVAL);
df21c858
UD
277 return -1;
278 }
279 }
a8874ead 280
e7c8359e 281 if (cnt == ndatabases)
a8874ead 282 {
c4029823 283 __set_errno (EINVAL);
a8874ead
UD
284 return -1;
285 }
286
287 /* Test whether it is really used. */
288 if (databases[cnt].dbp == NULL)
289 /* Nothing to do, but we could do. */
290 return 0;
291
292 /* Try to generate new data. */
293 new_db = nss_parse_service_list (service_line);
294 if (new_db == NULL)
295 {
296 /* Illegal service specification. */
c4029823 297 __set_errno (EINVAL);
a8874ead
UD
298 return -1;
299 }
300
301 /* Prevent multiple threads to change the service table. */
302 __libc_lock_lock (lock);
303
304 /* Install new rules. */
305 *databases[cnt].dbp = new_db;
1dbbb1ec 306#ifdef USE_NSCD
c3e2f19b 307 __nss_database_custom[cnt] = true;
1dbbb1ec 308#endif
a8874ead
UD
309
310 __libc_lock_unlock (lock);
311
312 return 0;
313}
314
315
2064087b
RM
316/* Comparison function for searching NI->known tree. */
317static int
318known_compare (const void *p1, const void *p2)
319{
320 return p1 == p2 ? 0 : strcmp (*(const char *const *) p1,
321 *(const char *const *) p2);
322}
323
324
319b9ad4
UD
325#if !defined DO_STATIC_NSS || defined SHARED
326/* Load library. */
327static int
328nss_load_library (service_user *ni)
329{
330 if (ni->library == NULL)
331 {
332 /* This service has not yet been used. Fetch the service
333 library for it, creating a new one if need be. If there
334 is no service table from the file, this static variable
335 holds the head of the service_library list made from the
336 default configuration. */
337 static name_database default_table;
338 ni->library = nss_new_service (service_table ?: &default_table,
339 ni->name);
340 if (ni->library == NULL)
341 return -1;
342 }
343
344 if (ni->library->lib_handle == NULL)
345 {
346 /* Load the shared library. */
62470f60 347 size_t shlen = (7 + strlen (ni->name) + 3
319b9ad4
UD
348 + strlen (__nss_shlib_revision) + 1);
349 int saved_errno = errno;
350 char shlib_name[shlen];
351
352 /* Construct shared object name. */
353 __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name,
354 "libnss_"),
62470f60 355 ni->name),
319b9ad4
UD
356 ".so"),
357 __nss_shlib_revision);
358
359 ni->library->lib_handle = __libc_dlopen (shlib_name);
360 if (ni->library->lib_handle == NULL)
361 {
362 /* Failed to load the library. */
363 ni->library->lib_handle = (void *) -1l;
364 __set_errno (saved_errno);
365 }
3cc3ef96 366# ifdef USE_NSCD
319b9ad4
UD
367 else if (is_nscd)
368 {
369 /* Call the init function when nscd is used. */
62470f60 370 size_t initlen = (5 + strlen (ni->name)
319b9ad4
UD
371 + strlen ("_init") + 1);
372 char init_name[initlen];
373
374 /* Construct the init function name. */
375 __stpcpy (__stpcpy (__stpcpy (init_name,
376 "_nss_"),
62470f60 377 ni->name),
319b9ad4
UD
378 "_init");
379
380 /* Find the optional init function. */
381 void (*ifct) (void (*) (size_t, struct traced_file *))
382 = __libc_dlsym (ni->library->lib_handle, init_name);
383 if (ifct != NULL)
384 {
385 void (*cb) (size_t, struct traced_file *) = nscd_init_cb;
3cc3ef96 386# ifdef PTR_DEMANGLE
319b9ad4 387 PTR_DEMANGLE (cb);
3cc3ef96 388# endif
319b9ad4
UD
389 ifct (cb);
390 }
391 }
3cc3ef96 392# endif
319b9ad4
UD
393 }
394
395 return 0;
396}
397#endif
398
399
899d423e
UD
400void *
401__nss_lookup_function (service_user *ni, const char *fct_name)
5f0e6fc7 402{
dbe31b9a 403 void **found, *result;
5f0e6fc7 404
5f0e6fc7
RM
405 /* We now modify global data. Protect it. */
406 __libc_lock_lock (lock);
407
dbe31b9a
RM
408 /* Search the tree of functions previously requested. Data in the
409 tree are `known_function' structures, whose first member is a
410 `const char *', the lookup key. The search returns a pointer to
411 the tree node structure; the first member of the is a pointer to
412 our structure (i.e. what will be a `known_function'); since the
413 first member of that is the lookup key string, &FCT_NAME is close
414 enough to a pointer to our structure to use as a lookup key that
415 will be passed to `known_compare' (above). */
416
701666b7 417 found = __tsearch (&fct_name, &ni->known, &known_compare);
0490345c
JO
418 if (found == NULL)
419 /* This means out-of-memory. */
420 result = NULL;
421 else if (*found != &fct_name)
4ec77f72
UD
422 {
423 /* The search found an existing structure in the tree. */
424 result = ((known_function *) *found)->fct_ptr;
bea9b193 425#ifdef PTR_DEMANGLE
4ec77f72 426 PTR_DEMANGLE (result);
bea9b193 427#endif
4ec77f72 428 }
dbe31b9a 429 else
5f0e6fc7 430 {
dbe31b9a
RM
431 /* This name was not known before. Now we have a node in the tree
432 (in the proper sorted position for FCT_NAME) that points to
433 &FCT_NAME instead of any real `known_function' structure.
434 Allocate a new structure and fill it in. */
5f0e6fc7 435
dbe31b9a
RM
436 known_function *known = malloc (sizeof *known);
437 if (! known)
5f0e6fc7 438 {
b1c608fe 439#if !defined DO_STATIC_NSS || defined SHARED
dbe31b9a 440 remove_from_tree:
b1c608fe 441#endif
dbe31b9a
RM
442 /* Oops. We can't instantiate this node properly.
443 Remove it from the tree. */
701666b7 444 __tdelete (&fct_name, &ni->known, &known_compare);
054c0457 445 free (known);
dbe31b9a 446 result = NULL;
5f0e6fc7 447 }
dbe31b9a 448 else
5f0e6fc7 449 {
dbe31b9a
RM
450 /* Point the tree node at this new structure. */
451 *found = known;
452 known->fct_name = fct_name;
453
b5567b2a 454#if !defined DO_STATIC_NSS || defined SHARED
319b9ad4
UD
455 /* Load the appropriate library. */
456 if (nss_load_library (ni) != 0)
054c0457
UD
457 /* This only happens when out of memory. */
458 goto remove_from_tree;
dbe31b9a 459
8a523922 460 if (ni->library->lib_handle == (void *) -1l)
dbe31b9a
RM
461 /* Library not found => function not found. */
462 result = NULL;
463 else
464 {
b3fc5f84 465 /* Get the desired function. */
62470f60 466 size_t namlen = (5 + strlen (ni->name) + 1
dbe31b9a 467 + strlen (fct_name) + 1);
b3fc5f84 468 char name[namlen];
dbe31b9a
RM
469
470 /* Construct the function name. */
b3fc5f84 471 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
62470f60 472 ni->name),
dbe31b9a
RM
473 "_"),
474 fct_name);
475
476 /* Look up the symbol. */
b3fc5f84 477 result = __libc_dlsym (ni->library->lib_handle, name);
dbe31b9a 478 }
5107cf1d
UD
479#else
480 /* We can't get function address dynamically in static linking. */
481 {
5107cf1d 482# define DEFINE_ENT(h,nm) \
1522c368
UD
483 { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \
484 { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \
5107cf1d
UD
485 { #h"_set"#nm"ent", _nss_##h##_set##nm##ent },
486# define DEFINE_GET(h,nm) \
487 { #h"_get"#nm"_r", _nss_##h##_get##nm##_r },
488# define DEFINE_GETBY(h,nm,ky) \
489 { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r },
490 static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] =
491 {
1522c368 492# include "function.def"
5107cf1d
UD
493 { NULL, NULL }
494 };
62470f60 495 size_t namlen = (5 + strlen (ni->name) + 1
5107cf1d
UD
496 + strlen (fct_name) + 1);
497 char name[namlen];
498
499 /* Construct the function name. */
62470f60 500 __stpcpy (__stpcpy (__stpcpy (name, ni->name),
5107cf1d
UD
501 "_"),
502 fct_name);
503
504 result = NULL;
505 for (tp = &tbl[0]; tp->fname; tp++)
506 if (strcmp (tp->fname, name) == 0)
507 {
508 result = tp->fp;
509 break;
510 }
511 }
512#endif
dbe31b9a
RM
513
514 /* Remember function pointer for later calls. Even if null, we
515 record it so a second try needn't search the library again. */
516 known->fct_ptr = result;
bea9b193 517#ifdef PTR_MANGLE
4ec77f72 518 PTR_MANGLE (known->fct_ptr);
bea9b193 519#endif
5f0e6fc7 520 }
5f0e6fc7
RM
521 }
522
5f0e6fc7
RM
523 /* Remove the lock. */
524 __libc_lock_unlock (lock);
525
526 return result;
527}
5556fc6a 528libc_hidden_def (__nss_lookup_function)
5f0e6fc7
RM
529
530
5f0e6fc7
RM
531static name_database *
532nss_parse_file (const char *fname)
533{
534 FILE *fp;
535 name_database *result;
536 name_database_entry *last;
537 char *line;
538 size_t len;
539
540 /* Open the configuration file. */
312be3f9 541 fp = fopen (fname, "rce");
5f0e6fc7
RM
542 if (fp == NULL)
543 return NULL;
544
2706ee38
UD
545 /* No threads use this stream. */
546 __fsetlocking (fp, FSETLOCKING_BYCALLER);
547
5f0e6fc7
RM
548 result = (name_database *) malloc (sizeof (name_database));
549 if (result == NULL)
319b9ad4
UD
550 {
551 fclose (fp);
552 return NULL;
553 }
5f0e6fc7
RM
554
555 result->entry = NULL;
556 result->library = NULL;
557 last = NULL;
558 line = NULL;
559 len = 0;
560 do
561 {
562 name_database_entry *this;
563 ssize_t n;
5f0e6fc7 564
bba7bb78 565 n = __getline (&line, &len, fp);
5f0e6fc7
RM
566 if (n < 0)
567 break;
568 if (line[n - 1] == '\n')
569 line[n - 1] = '\0';
570
571 /* Because the file format does not know any form of quoting we
572 can search forward for the next '#' character and if found
573 make it terminating the line. */
c4563d2d 574 *__strchrnul (line, '#') = '\0';
5f0e6fc7
RM
575
576 /* If the line is blank it is ignored. */
577 if (line[0] == '\0')
578 continue;
579
580 /* Each line completely specifies the actions for a database. */
581 this = nss_getline (line);
582 if (this != NULL)
583 {
584 if (last != NULL)
585 last->next = this;
586 else
587 result->entry = this;
588
589 last = this;
590 }
591 }
7fc03cf3 592 while (!__feof_unlocked (fp));
5f0e6fc7
RM
593
594 /* Free the buffer. */
595 free (line);
596 /* Close configuration file. */
597 fclose (fp);
598
5f0e6fc7
RM
599 return result;
600}
601
602
a8874ead
UD
603/* Read the source names:
604 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
605 */
bba7bb78
RM
606static service_user *
607nss_parse_service_list (const char *line)
5f0e6fc7 608{
bba7bb78 609 service_user *result = NULL, **nextp = &result;
5f0e6fc7 610
5f0e6fc7
RM
611 while (1)
612 {
613 service_user *new_service;
3776d592 614 const char *name;
5f0e6fc7
RM
615
616 while (isspace (line[0]))
617 ++line;
618 if (line[0] == '\0')
619 /* No source specified. */
620 return result;
621
622 /* Read <source> identifier. */
623 name = line;
624 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
625 ++line;
626 if (name == line)
627 return result;
628
629
1a989e00
UD
630 new_service = (service_user *) malloc (sizeof (service_user)
631 + (line - name + 1));
5f0e6fc7
RM
632 if (new_service == NULL)
633 return result;
5f0e6fc7 634
1a989e00 635 *((char *) __mempcpy (new_service->name, name, line - name)) = '\0';
5f0e6fc7
RM
636
637 /* Set default actions. */
638 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
639 new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
640 new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
641 new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
a68b0d31 642 new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN;
5f0e6fc7
RM
643 new_service->library = NULL;
644 new_service->known = NULL;
645 new_service->next = NULL;
646
647 while (isspace (line[0]))
648 ++line;
649
650 if (line[0] == '[')
651 {
5f0e6fc7
RM
652 /* Read criterions. */
653 do
654 ++line;
655 while (line[0] != '\0' && isspace (line[0]));
656
657 do
658 {
bba7bb78
RM
659 int not;
660 enum nss_status status;
661 lookup_actions action;
662
663 /* Grok ! before name to mean all statii but that one. */
6796bc80
UD
664 not = line[0] == '!';
665 if (not)
bba7bb78
RM
666 ++line;
667
5f0e6fc7
RM
668 /* Read status name. */
669 name = line;
670 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
671 && line[0] != ']')
672 ++line;
673
674 /* Compare with known statii. */
675 if (line - name == 7)
676 {
bba7bb78 677 if (__strncasecmp (name, "SUCCESS", 7) == 0)
5f0e6fc7 678 status = NSS_STATUS_SUCCESS;
bba7bb78 679 else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
5f0e6fc7
RM
680 status = NSS_STATUS_UNAVAIL;
681 else
d44638b0 682 goto finish;
5f0e6fc7
RM
683 }
684 else if (line - name == 8)
685 {
bba7bb78 686 if (__strncasecmp (name, "NOTFOUND", 8) == 0)
5f0e6fc7 687 status = NSS_STATUS_NOTFOUND;
bba7bb78 688 else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
5f0e6fc7
RM
689 status = NSS_STATUS_TRYAGAIN;
690 else
d44638b0 691 goto finish;
5f0e6fc7
RM
692 }
693 else
d44638b0 694 goto finish;
5f0e6fc7
RM
695
696 while (isspace (line[0]))
697 ++line;
698 if (line[0] != '=')
d44638b0 699 goto finish;
5f0e6fc7
RM
700 do
701 ++line;
702 while (isspace (line[0]));
703
704 name = line;
705 while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
706 && line[0] != ']')
707 ++line;
708
bba7bb78
RM
709 if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
710 action = NSS_ACTION_RETURN;
5f0e6fc7 711 else if (line - name == 8
bba7bb78
RM
712 && __strncasecmp (name, "CONTINUE", 8) == 0)
713 action = NSS_ACTION_CONTINUE;
ced8f893
SG
714 else if (line - name == 5
715 && __strncasecmp (name, "MERGE", 5) == 0)
716 action = NSS_ACTION_MERGE;
5f0e6fc7 717 else
d44638b0 718 goto finish;
5f0e6fc7 719
bba7bb78
RM
720 if (not)
721 {
722 /* Save the current action setting for this status,
723 set them all to the given action, and reset this one. */
724 const lookup_actions save = new_service->actions[2 + status];
725 new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
726 new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
727 new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
728 new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
729 new_service->actions[2 + status] = save;
730 }
731 else
732 new_service->actions[2 + status] = action;
733
734 /* Skip white spaces. */
5f0e6fc7
RM
735 while (isspace (line[0]))
736 ++line;
737 }
738 while (line[0] != ']');
739
740 /* Skip the ']'. */
741 ++line;
742 }
743
bba7bb78
RM
744 *nextp = new_service;
745 nextp = &new_service->next;
d44638b0
PP
746 continue;
747
748 finish:
749 free (new_service);
750 return result;
5f0e6fc7 751 }
bba7bb78
RM
752}
753
754static name_database_entry *
755nss_getline (char *line)
756{
757 const char *name;
758 name_database_entry *result;
1a989e00 759 size_t len;
bba7bb78
RM
760
761 /* Ignore leading white spaces. ATTENTION: this is different from
762 what is implemented in Solaris. The Solaris man page says a line
763 beginning with a white space character is ignored. We regard
764 this as just another misfeature in Solaris. */
765 while (isspace (line[0]))
766 ++line;
767
768 /* Recognize `<database> ":"'. */
769 name = line;
770 while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
771 ++line;
772 if (line[0] == '\0' || name == line)
773 /* Syntax error. */
774 return NULL;
775 *line++ = '\0';
776
1a989e00
UD
777 len = strlen (name) + 1;
778
779 result = (name_database_entry *) malloc (sizeof (name_database_entry) + len);
bba7bb78
RM
780 if (result == NULL)
781 return NULL;
782
783 /* Save the database name. */
1a989e00 784 memcpy (result->name, name, len);
bba7bb78
RM
785
786 /* Parse the list of services. */
787 result->service = nss_parse_service_list (line);
788
789 result->next = NULL;
790 return result;
5f0e6fc7
RM
791}
792
793
d3b9fd9e 794#if !defined DO_STATIC_NSS || defined SHARED
5f0e6fc7
RM
795static service_library *
796nss_new_service (name_database *database, const char *name)
797{
798 service_library **currentp = &database->library;
799
800 while (*currentp != NULL)
801 {
802 if (strcmp ((*currentp)->name, name) == 0)
803 return *currentp;
804 currentp = &(*currentp)->next;
805 }
806
807 /* We have to add the new service. */
808 *currentp = (service_library *) malloc (sizeof (service_library));
809 if (*currentp == NULL)
810 return NULL;
811
812 (*currentp)->name = name;
813 (*currentp)->lib_handle = NULL;
814 (*currentp)->next = NULL;
815
816 return *currentp;
817}
d3b9fd9e 818#endif
d60d215c
UD
819
820
3cc3ef96 821#if defined SHARED && defined USE_NSCD
319b9ad4
UD
822/* Load all libraries for the service. */
823static void
824nss_load_all_libraries (const char *service, const char *def)
825{
826 service_user *ni = NULL;
827
a9368c34 828 if (__nss_database_lookup2 (service, NULL, def, &ni) == 0)
319b9ad4
UD
829 while (ni != NULL)
830 {
831 nss_load_library (ni);
832 ni = ni->next;
833 }
834}
835
836
ef4d5b32
UD
837/* Called by nscd and nscd alone. */
838void
319b9ad4 839__nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
ef4d5b32 840{
319b9ad4
UD
841# ifdef PTR_MANGLE
842 PTR_MANGLE (cb);
843# endif
844 nscd_init_cb = cb;
845 is_nscd = true;
846
847 /* Find all the relevant modules so that the init functions are called. */
a1a78204
SE
848 nss_load_all_libraries ("passwd", DEFAULT_CONFIG);
849 nss_load_all_libraries ("group", DEFAULT_CONFIG);
319b9ad4
UD
850 nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
851 nss_load_all_libraries ("services", NULL);
852
ef4d5b32
UD
853 /* Disable all uses of NSCD. */
854 __nss_not_use_nscd_passwd = -1;
855 __nss_not_use_nscd_group = -1;
856 __nss_not_use_nscd_hosts = -1;
b21fa963 857 __nss_not_use_nscd_services = -1;
684ae515 858 __nss_not_use_nscd_netgroup = -1;
ef4d5b32 859}
319b9ad4 860#endif
ef4d5b32 861
d44638b0
PP
862static void
863free_database_entries (name_database_entry *entry)
d60d215c 864{
d60d215c
UD
865 while (entry != NULL)
866 {
867 name_database_entry *olde = entry;
868 service_user *service = entry->service;
869
870 while (service != NULL)
871 {
872 service_user *olds = service;
873
874 if (service->known != NULL)
1483b753 875 __tdestroy (service->known, free);
d60d215c
UD
876
877 service = service->next;
878 free (olds);
879 }
880
881 entry = entry->next;
882 free (olde);
883 }
d44638b0
PP
884}
885
886/* Free all resources if necessary. */
887libc_freeres_fn (free_defconfig)
888{
889 name_database_entry *entry = defconfig_entries;
890
891 if (entry == NULL)
892 /* defconfig was not used. */
893 return;
894
895 /* Don't disturb ongoing other threads (if there are any). */
896 defconfig_entries = NULL;
897
898 free_database_entries (entry);
899}
900
901libc_freeres_fn (free_mem)
902{
903 name_database *top = service_table;
904 service_library *library;
905
906 if (top == NULL)
907 /* Maybe we have not read the nsswitch.conf file. */
908 return;
909
910 /* Don't disturb ongoing other threads (if there are any). */
911 service_table = NULL;
912
913 free_database_entries (top->entry);
d60d215c
UD
914
915 library = top->library;
916 while (library != NULL)
917 {
918 service_library *oldl = library;
919
0ab7f77e
RM
920 if (library->lib_handle && library->lib_handle != (void *) -1l)
921 __libc_dlclose (library->lib_handle);
d60d215c
UD
922
923 library = library->next;
924 free (oldl);
925 }
926
927 free (top);
928}