1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
/* Copyright (C) 1996 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <ctype.h>
#include <dlfcn.h>
#include <netdb.h>
#include <libc-lock.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nsswitch.h"
#include "../elf/link.h" /* We need some help from ld.so. */
/* Prototypes for the local functions. */
static void nss_init (void);
static void *nss_lookup_function (service_user *ni, const char *fct_name);
static int nss_find_entry (struct entry **knownp, const char *key,
void **valp);
static void nss_insert_entry (struct entry **knownp, const char *key,
void *val);
static name_database *nss_parse_file (const char *fname);
static name_database_entry *nss_getline (char *line);
static service_user *nss_parse_service_list (const char *line);
static service_library *nss_new_service (name_database *database,
const char *name);
__libc_lock_define_initialized (static, lock);
/* Global variable. */
struct __res_state _res;
/* Nonzero if the sevices are already initialized. */
static int nss_initialized;
/* The root of the whole data base. */
static name_database *service_table;
static void
nss_init (void)
{
/* Prevent multiple threads to change the service table. */
__libc_lock_lock (lock);
if (service_table == NULL)
service_table = nss_parse_file (_PATH_NSSWITCH_CONF);
__libc_lock_unlock (lock);
}
/* -1 == database not found
0 == database entry pointer stored */
int
__nss_database_lookup (const char *database, const char *defconfig,
service_user **ni)
{
name_database_entry *entry;
if (nss_initialized == 0)
nss_init ();
/* Test whether configuration data is available. */
if (service_table)
{
/* Return first `service_user' entry for DATABASE.
XXX Will use perfect hashing function for known databases. */
/* XXX Could use some faster mechanism here. But each database is
only requested once and so this might not be critical. */
for (entry = service_table->entry; entry != NULL; entry = entry->next)
if (strcmp (database, entry->name) == 0)
{
*ni = entry->service;
return 0;
}
}
/* No configuration data is available, either because nsswitch.conf
doesn't exist or because it doesn't have a line for this database. */
entry = malloc (sizeof *entry);
if (entry == NULL)
return -1;
entry->name = database;
/* DEFCONFIG specifies the default service list for this database,
or null to use the most common default. */
entry->service = nss_parse_service_list (defconfig ?:
"compat [NOTFOUND=return] files");
*ni = entry->service;
return 0;
}
/* -1 == not found
0 == adjusted for next function */
int
__nss_lookup (service_user **ni, const char *fct_name, void **fctp)
{
*fctp = nss_lookup_function (*ni, fct_name);
while (*fctp == NULL
&& nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
&& (*ni)->next != NULL)
{
*ni = (*ni)->next;
*fctp = nss_lookup_function (*ni, fct_name);
}
return *fctp != NULL ? 0 : -1;
}
/* -1 == not found
0 == adjusted for next function
1 == finished */
int
__nss_next (service_user **ni, const char *fct_name, void **fctp, int status,
int all_values)
{
if (all_values)
{
if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN
&& nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN
&& nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN
&& nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN)
return 1;
}
else
{
/* This is really only for debugging. */
if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_SUCCESS)
__libc_fatal ("illegal status in " __FUNCTION__);
if (nss_next_action (*ni, status) == NSS_ACTION_RETURN)
return 1;
}
if ((*ni)->next == NULL)
return -1;
do
{
*ni = (*ni)->next;
*fctp = nss_lookup_function (*ni, fct_name);
}
while (*fctp == NULL
&& nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE
&& (*ni)->next != NULL);
return *fctp != NULL ? 0 : -1;
}
static int
nss_dlerror_run (void (*operate) (void))
{
const char *last_errstring = NULL;
const char *last_object_name = NULL;
(void) _dl_catch_error (&last_errstring, &last_object_name, operate);
return last_errstring != NULL;
}
static void *
nss_lookup_function (service_user *ni, const char *fct_name)
{
void *result;
/* Determine whether current function is loaded. */
if (nss_find_entry (&ni->known, fct_name, &result) >= 0)
return result;
/* We now modify global data. Protect it. */
__libc_lock_lock (lock);
if (ni->library == NULL)
{
/* This service has not yet been used. Fetch the service library
for it, creating a new one if need be. If there is no service
table from the file, this static variable holds the head of the
service_library list made from the default configuration. */
static name_database default_table;
ni->library = nss_new_service (service_table ?: &default_table,
ni->name);
if (ni->library == NULL)
{
/* This only happens when out of memory. */
__libc_lock_unlock (lock);
return NULL;
}
}
if (ni->library->lib_handle == NULL)
{
/* Load the shared library. */
size_t shlen = (7 + strlen (ni->library->name) + 3
+ sizeof (NSS_SHLIB_REVISION));
char shlib_name[shlen];
void do_open (void)
{
/* Open and relocate the shared object. */
ni->library->lib_handle = _dl_open (shlib_name, RTLD_LAZY);
}
/* Construct name. */
__stpcpy (__stpcpy (__stpcpy (shlib_name, "libnss_"), ni->library->name),
".so" NSS_SHLIB_REVISION);
if (nss_dlerror_run (do_open) != 0)
/* Failed to load the library. */
ni->library->lib_handle = (void *) -1;
}
if (ni->library->lib_handle == (void *) -1)
/* Library not found => function not found. */
result = NULL;
else
{
/* Get the desired function. Again, GNU ld.so magic ahead. */
size_t namlen = (5 + strlen (ni->library->name) + 1
+ strlen (fct_name) + 1);
char name[namlen];
struct link_map *map = ni->library->lib_handle;
ElfW(Addr) loadbase;
const ElfW(Sym) *ref = NULL;
void get_sym (void)
{
struct link_map *scope[2] = { map, NULL };
loadbase = _dl_lookup_symbol (name, &ref, scope, map->l_name, 0, 0);
}
__stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"),
ni->library->name),
"_"),
fct_name);
result = (nss_dlerror_run (get_sym)
? NULL : (void *) (loadbase + ref->st_value));
}
/* Remember function pointer for the usage. */
nss_insert_entry (&ni->known, fct_name, result);
/* Remove the lock. */
__libc_lock_unlock (lock);
return result;
}
static int
known_compare (const void *p1, const void *p2)
{
known_function *v1 = (known_function *) p1;
known_function *v2 = (known_function *) p2;
return strcmp (v1->fct_name, v2->fct_name);
}
static int
nss_find_entry (struct entry **knownp, const char *key, void **valp)
{
known_function looking_for = { fct_name: key };
struct entry **found;
found = __tfind (&looking_for, (const void **) knownp, known_compare);
if (found == NULL)
return -1;
*valp = ((known_function *) (*found)->key)->fct_ptr;
return 0;
}
static void
nss_insert_entry (struct entry **knownp, const char *key, void *val)
{
known_function *to_insert;
to_insert = (known_function *) malloc (sizeof (known_function));
if (to_insert == NULL)
return;
to_insert->fct_name = key;
to_insert->fct_ptr = val;
__tsearch (to_insert, (void **) knownp, known_compare);
}
static name_database *
nss_parse_file (const char *fname)
{
FILE *fp;
name_database *result;
name_database_entry *last;
char *line;
size_t len;
/* Open the configuration file. */
fp = fopen (fname, "r");
if (fp == NULL)
return NULL;
result = (name_database *) malloc (sizeof (name_database));
if (result == NULL)
return NULL;
result->entry = NULL;
result->library = NULL;
last = NULL;
line = NULL;
len = 0;
do
{
name_database_entry *this;
ssize_t n;
char *cp;
n = __getline (&line, &len, fp);
if (n < 0)
break;
if (line[n - 1] == '\n')
line[n - 1] = '\0';
/* Because the file format does not know any form of quoting we
can search forward for the next '#' character and if found
make it terminating the line. */
cp = strchr (line, '#');
if (cp != NULL)
*cp = '\0';
/* If the line is blank it is ignored. */
if (line[0] == '\0')
continue;
/* Each line completely specifies the actions for a database. */
this = nss_getline (line);
if (this != NULL)
{
if (last != NULL)
last->next = this;
else
result->entry = this;
last = this;
}
}
while (!feof (fp));
/* Free the buffer. */
free (line);
/* Close configuration file. */
fclose (fp);
return result;
}
/* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
static service_user *
nss_parse_service_list (const char *line)
{
service_user *result = NULL, **nextp = &result;
while (1)
{
service_user *new_service;
const char *name;
while (isspace (line[0]))
++line;
if (line[0] == '\0')
/* No source specified. */
return result;
/* Read <source> identifier. */
name = line;
while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[')
++line;
if (name == line)
return result;
new_service = (service_user *) malloc (sizeof (service_user));
if (new_service == NULL)
return result;
else
{
char *source = (char *) malloc (line - name + 1);
if (source == NULL)
{
free (new_service);
return result;
}
memcpy (source, name, line - name);
source[line - name] = '\0';
new_service->name = source;
}
/* Set default actions. */
new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE;
new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE;
new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE;
new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN;
new_service->library = NULL;
new_service->known = NULL;
new_service->next = NULL;
while (isspace (line[0]))
++line;
if (line[0] == '[')
{
/* Read criterions. */
do
++line;
while (line[0] != '\0' && isspace (line[0]));
do
{
int not;
enum nss_status status;
lookup_actions action;
/* Grok ! before name to mean all statii but that one. */
if (not = line[0] == '!')
++line;
/* Read status name. */
name = line;
while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
&& line[0] != ']')
++line;
/* Compare with known statii. */
if (line - name == 7)
{
if (__strncasecmp (name, "SUCCESS", 7) == 0)
status = NSS_STATUS_SUCCESS;
else if (__strncasecmp (name, "UNAVAIL", 7) == 0)
status = NSS_STATUS_UNAVAIL;
else
return result;
}
else if (line - name == 8)
{
if (__strncasecmp (name, "NOTFOUND", 8) == 0)
status = NSS_STATUS_NOTFOUND;
else if (__strncasecmp (name, "TRYAGAIN", 8) == 0)
status = NSS_STATUS_TRYAGAIN;
else
return result;
}
else
return result;
while (isspace (line[0]))
++line;
if (line[0] != '=')
return result;
do
++line;
while (isspace (line[0]));
name = line;
while (line[0] != '\0' && !isspace (line[0]) && line[0] != '='
&& line[0] != ']')
++line;
if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0)
action = NSS_ACTION_RETURN;
else if (line - name == 8
&& __strncasecmp (name, "CONTINUE", 8) == 0)
action = NSS_ACTION_CONTINUE;
else
return result;
if (not)
{
/* Save the current action setting for this status,
set them all to the given action, and reset this one. */
const lookup_actions save = new_service->actions[2 + status];
new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action;
new_service->actions[2 + NSS_STATUS_UNAVAIL] = action;
new_service->actions[2 + NSS_STATUS_NOTFOUND] = action;
new_service->actions[2 + NSS_STATUS_SUCCESS] = action;
new_service->actions[2 + status] = save;
}
else
new_service->actions[2 + status] = action;
/* Skip white spaces. */
while (isspace (line[0]))
++line;
}
while (line[0] != ']');
/* Skip the ']'. */
++line;
}
*nextp = new_service;
nextp = &new_service->next;
}
}
static name_database_entry *
nss_getline (char *line)
{
const char *name;
name_database_entry *result;
/* Ignore leading white spaces. ATTENTION: this is different from
what is implemented in Solaris. The Solaris man page says a line
beginning with a white space character is ignored. We regard
this as just another misfeature in Solaris. */
while (isspace (line[0]))
++line;
/* Recognize `<database> ":"'. */
name = line;
while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':')
++line;
if (line[0] == '\0' || name == line)
/* Syntax error. */
return NULL;
*line++ = '\0';
result = (name_database_entry *) malloc (sizeof (name_database_entry));
if (result == NULL)
return NULL;
/* Save the database name. */
{
const size_t len = strlen (name) + 1;
char *new = malloc (len);
if (new == NULL)
{
free (result);
return NULL;
}
result->name = memcpy (new, name, len);
}
/* Parse the list of services. */
result->service = nss_parse_service_list (line);
result->next = NULL;
return result;
}
static service_library *
nss_new_service (name_database *database, const char *name)
{
service_library **currentp = &database->library;
while (*currentp != NULL)
{
if (strcmp ((*currentp)->name, name) == 0)
return *currentp;
currentp = &(*currentp)->next;
}
/* We have to add the new service. */
*currentp = (service_library *) malloc (sizeof (service_library));
if (*currentp == NULL)
return NULL;
(*currentp)->name = name;
(*currentp)->lib_handle = NULL;
(*currentp)->next = NULL;
return *currentp;
}
|