]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1996-2015 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> |
5f0e6fc7 RM |
43 | |
44 | /* Prototypes for the local functions. */ | |
dfd2257a UD |
45 | static name_database *nss_parse_file (const char *fname) internal_function; |
46 | static name_database_entry *nss_getline (char *line) internal_function; | |
47 | static service_user *nss_parse_service_list (const char *line) | |
48 | internal_function; | |
b1c608fe | 49 | #if !defined DO_STATIC_NSS || defined SHARED |
5f0e6fc7 | 50 | static service_library *nss_new_service (name_database *database, |
dfd2257a | 51 | const char *name) internal_function; |
b1c608fe | 52 | #endif |
5f0e6fc7 RM |
53 | |
54 | ||
a8874ead UD |
55 | /* Declare external database variables. */ |
56 | #define DEFINE_DATABASE(name) \ | |
6455d255 | 57 | extern service_user *__nss_##name##_database attribute_hidden; \ |
a8874ead UD |
58 | weak_extern (__nss_##name##_database) |
59 | #include "databases.def" | |
60 | #undef DEFINE_DATABASE | |
61 | ||
62 | /* Structure to map database name to variable. */ | |
3d50529d | 63 | static const struct |
a8874ead | 64 | { |
3d50529d | 65 | const char name[10]; |
a8874ead UD |
66 | service_user **dbp; |
67 | } databases[] = | |
68 | { | |
69 | #define DEFINE_DATABASE(name) \ | |
70 | { #name, &__nss_##name##_database }, | |
71 | #include "databases.def" | |
72 | #undef DEFINE_DATABASE | |
73 | }; | |
e7c8359e | 74 | #define ndatabases (sizeof (databases) / sizeof (databases[0])) |
a8874ead | 75 | |
c3e2f19b UD |
76 | /* Flags whether custom rules for database is set. */ |
77 | bool __nss_database_custom[NSS_DBSIDX_max]; | |
78 | ||
a8874ead | 79 | |
1e16111c | 80 | __libc_lock_define_initialized (static, lock) |
5f0e6fc7 | 81 | |
b5567b2a | 82 | #if !defined DO_STATIC_NSS || defined SHARED |
56552e42 | 83 | /* String with revision number of the shared object files. */ |
1fb05e3d | 84 | static const char *const __nss_shlib_revision = LIBNSS_FILES_SO + 15; |
5107cf1d | 85 | #endif |
5f0e6fc7 RM |
86 | |
87 | /* The root of the whole data base. */ | |
88 | static name_database *service_table; | |
89 | ||
d44638b0 PP |
90 | /* List of default service lists that were generated by glibc because |
91 | /etc/nsswitch.conf did not provide a value. | |
92 | The list is only maintained so we can free such service lists in | |
93 | __libc_freeres. */ | |
94 | static name_database_entry *defconfig_entries; | |
95 | ||
5f0e6fc7 | 96 | |
3cc3ef96 | 97 | #ifdef USE_NSCD |
319b9ad4 UD |
98 | /* Nonzero if this is the nscd process. */ |
99 | static bool is_nscd; | |
100 | /* The callback passed to the init functions when nscd is used. */ | |
101 | static void (*nscd_init_cb) (size_t, struct traced_file *); | |
3cc3ef96 | 102 | #endif |
319b9ad4 UD |
103 | |
104 | ||
5f0e6fc7 RM |
105 | /* -1 == database not found |
106 | 0 == database entry pointer stored */ | |
107 | int | |
68dbb3a6 UD |
108 | __nss_database_lookup (const char *database, const char *alternate_name, |
109 | const char *defconfig, service_user **ni) | |
5f0e6fc7 | 110 | { |
a8874ead UD |
111 | /* Prevent multiple threads to change the service table. */ |
112 | __libc_lock_lock (lock); | |
bba7bb78 | 113 | |
a8874ead UD |
114 | /* Reconsider database variable in case some other thread called |
115 | `__nss_configure_lookup' while we waited for the lock. */ | |
116 | if (*ni != NULL) | |
15a83d04 UD |
117 | { |
118 | __libc_lock_unlock (lock); | |
119 | return 0; | |
120 | } | |
a8874ead | 121 | |
56552e42 UD |
122 | /* Are we initialized yet? */ |
123 | if (service_table == NULL) | |
a8874ead UD |
124 | /* Read config file. */ |
125 | service_table = nss_parse_file (_PATH_NSSWITCH_CONF); | |
5f0e6fc7 RM |
126 | |
127 | /* Test whether configuration data is available. */ | |
a8874ead | 128 | if (service_table != NULL) |
5f0e6fc7 | 129 | { |
a8874ead UD |
130 | /* Return first `service_user' entry for DATABASE. */ |
131 | name_database_entry *entry; | |
c66273aa RM |
132 | |
133 | /* XXX Could use some faster mechanism here. But each database is | |
134 | only requested once and so this might not be critical. */ | |
135 | for (entry = service_table->entry; entry != NULL; entry = entry->next) | |
136 | if (strcmp (database, entry->name) == 0) | |
15a83d04 | 137 | *ni = entry->service; |
68dbb3a6 UD |
138 | |
139 | if (*ni == NULL && alternate_name != NULL) | |
e7fd8a39 UD |
140 | /* We haven't found an entry so far. Try to find it with the |
141 | alternative name. */ | |
68dbb3a6 UD |
142 | for (entry = service_table->entry; entry != NULL; entry = entry->next) |
143 | if (strcmp (alternate_name, entry->name) == 0) | |
144 | *ni = entry->service; | |
5f0e6fc7 RM |
145 | } |
146 | ||
c66273aa | 147 | /* No configuration data is available, either because nsswitch.conf |
319b9ad4 | 148 | doesn't exist or because it doesn't have a line for this database. |
a8874ead UD |
149 | |
150 | DEFCONFIG specifies the default service list for this database, | |
bba7bb78 | 151 | or null to use the most common default. */ |
15a83d04 | 152 | if (*ni == NULL) |
d44638b0 PP |
153 | { |
154 | *ni = nss_parse_service_list (defconfig | |
155 | ?: "nis [NOTFOUND=return] files"); | |
156 | if (*ni != NULL) | |
157 | { | |
158 | /* Record the memory we've just allocated in defconfig_entries list, | |
159 | so we can free it later. */ | |
160 | name_database_entry *entry; | |
161 | ||
162 | /* Allocate ENTRY plus size of name (1 here). */ | |
163 | entry = (name_database_entry *) malloc (sizeof (*entry) + 1); | |
164 | ||
165 | if (entry != NULL) | |
166 | { | |
167 | entry->next = defconfig_entries; | |
168 | entry->service = *ni; | |
169 | entry->name[0] = '\0'; | |
170 | defconfig_entries = entry; | |
171 | } | |
172 | } | |
173 | } | |
a8874ead UD |
174 | |
175 | __libc_lock_unlock (lock); | |
bba7bb78 | 176 | |
64031225 | 177 | return *ni != NULL ? 0 : -1; |
5f0e6fc7 | 178 | } |
37ba7d66 | 179 | libc_hidden_def (__nss_database_lookup) |
5f0e6fc7 RM |
180 | |
181 | ||
182 | /* -1 == not found | |
4317f9e1 | 183 | 0 == function found |
14ea22e9 | 184 | 1 == finished */ |
5f0e6fc7 | 185 | int |
384ca551 UD |
186 | __nss_lookup (service_user **ni, const char *fct_name, const char *fct2_name, |
187 | void **fctp) | |
5f0e6fc7 | 188 | { |
899d423e | 189 | *fctp = __nss_lookup_function (*ni, fct_name); |
384ca551 | 190 | if (*fctp == NULL && fct2_name != NULL) |
0dc6c5e4 | 191 | *fctp = __nss_lookup_function (*ni, fct2_name); |
5f0e6fc7 RM |
192 | |
193 | while (*fctp == NULL | |
194 | && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE | |
195 | && (*ni)->next != NULL) | |
196 | { | |
197 | *ni = (*ni)->next; | |
198 | ||
899d423e | 199 | *fctp = __nss_lookup_function (*ni, fct_name); |
384ca551 | 200 | if (*fctp == NULL && fct2_name != NULL) |
0dc6c5e4 | 201 | *fctp = __nss_lookup_function (*ni, fct2_name); |
5f0e6fc7 RM |
202 | } |
203 | ||
14ea22e9 | 204 | return *fctp != NULL ? 0 : (*ni)->next == NULL ? 1 : -1; |
5f0e6fc7 | 205 | } |
684ae515 | 206 | libc_hidden_def (__nss_lookup) |
5f0e6fc7 RM |
207 | |
208 | ||
209 | /* -1 == not found | |
210 | 0 == adjusted for next function | |
211 | 1 == finished */ | |
212 | int | |
384ca551 UD |
213 | __nss_next2 (service_user **ni, const char *fct_name, const char *fct2_name, |
214 | void **fctp, int status, int all_values) | |
5f0e6fc7 RM |
215 | { |
216 | if (all_values) | |
217 | { | |
218 | if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN | |
219 | && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN | |
220 | && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN | |
221 | && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN) | |
222 | return 1; | |
223 | } | |
224 | else | |
225 | { | |
226 | /* This is really only for debugging. */ | |
384ca551 UD |
227 | if (__builtin_expect (NSS_STATUS_TRYAGAIN > status |
228 | || status > NSS_STATUS_RETURN, 0)) | |
79dbd981 | 229 | __libc_fatal ("illegal status in __nss_next"); |
5f0e6fc7 RM |
230 | |
231 | if (nss_next_action (*ni, status) == NSS_ACTION_RETURN) | |
232 | return 1; | |
233 | } | |
234 | ||
235 | if ((*ni)->next == NULL) | |
236 | return -1; | |
237 | ||
238 | do | |
239 | { | |
240 | *ni = (*ni)->next; | |
241 | ||
899d423e | 242 | *fctp = __nss_lookup_function (*ni, fct_name); |
384ca551 UD |
243 | if (*fctp == NULL && fct2_name != NULL) |
244 | *fctp = __nss_lookup_function (*ni, fct2_name); | |
5f0e6fc7 RM |
245 | } |
246 | while (*fctp == NULL | |
247 | && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE | |
248 | && (*ni)->next != NULL); | |
249 | ||
250 | return *fctp != NULL ? 0 : -1; | |
251 | } | |
384ca551 UD |
252 | libc_hidden_def (__nss_next2) |
253 | ||
254 | ||
255 | int | |
256 | attribute_compat_text_section | |
257 | __nss_next (service_user **ni, const char *fct_name, void **fctp, int status, | |
258 | int all_values) | |
259 | { | |
260 | return __nss_next2 (ni, fct_name, NULL, fctp, status, all_values); | |
261 | } | |
5f0e6fc7 RM |
262 | |
263 | ||
a8874ead UD |
264 | int |
265 | __nss_configure_lookup (const char *dbname, const char *service_line) | |
266 | { | |
267 | service_user *new_db; | |
268 | size_t cnt; | |
269 | ||
e7c8359e | 270 | for (cnt = 0; cnt < ndatabases; ++cnt) |
df21c858 UD |
271 | { |
272 | int cmp = strcmp (dbname, databases[cnt].name); | |
273 | if (cmp == 0) | |
274 | break; | |
5a97622d | 275 | if (cmp < 0) |
df21c858 | 276 | { |
c4029823 | 277 | __set_errno (EINVAL); |
df21c858 UD |
278 | return -1; |
279 | } | |
280 | } | |
a8874ead | 281 | |
e7c8359e | 282 | if (cnt == ndatabases) |
a8874ead | 283 | { |
c4029823 | 284 | __set_errno (EINVAL); |
a8874ead UD |
285 | return -1; |
286 | } | |
287 | ||
288 | /* Test whether it is really used. */ | |
289 | if (databases[cnt].dbp == NULL) | |
290 | /* Nothing to do, but we could do. */ | |
291 | return 0; | |
292 | ||
293 | /* Try to generate new data. */ | |
294 | new_db = nss_parse_service_list (service_line); | |
295 | if (new_db == NULL) | |
296 | { | |
297 | /* Illegal service specification. */ | |
c4029823 | 298 | __set_errno (EINVAL); |
a8874ead UD |
299 | return -1; |
300 | } | |
301 | ||
302 | /* Prevent multiple threads to change the service table. */ | |
303 | __libc_lock_lock (lock); | |
304 | ||
305 | /* Install new rules. */ | |
306 | *databases[cnt].dbp = new_db; | |
c3e2f19b | 307 | __nss_database_custom[cnt] = true; |
a8874ead UD |
308 | |
309 | __libc_lock_unlock (lock); | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
314 | ||
2064087b RM |
315 | /* Comparison function for searching NI->known tree. */ |
316 | static int | |
317 | known_compare (const void *p1, const void *p2) | |
318 | { | |
319 | return p1 == p2 ? 0 : strcmp (*(const char *const *) p1, | |
320 | *(const char *const *) p2); | |
321 | } | |
322 | ||
323 | ||
319b9ad4 UD |
324 | #if !defined DO_STATIC_NSS || defined SHARED |
325 | /* Load library. */ | |
326 | static int | |
327 | nss_load_library (service_user *ni) | |
328 | { | |
329 | if (ni->library == NULL) | |
330 | { | |
331 | /* This service has not yet been used. Fetch the service | |
332 | library for it, creating a new one if need be. If there | |
333 | is no service table from the file, this static variable | |
334 | holds the head of the service_library list made from the | |
335 | default configuration. */ | |
336 | static name_database default_table; | |
337 | ni->library = nss_new_service (service_table ?: &default_table, | |
338 | ni->name); | |
339 | if (ni->library == NULL) | |
340 | return -1; | |
341 | } | |
342 | ||
343 | if (ni->library->lib_handle == NULL) | |
344 | { | |
345 | /* Load the shared library. */ | |
62470f60 | 346 | size_t shlen = (7 + strlen (ni->name) + 3 |
319b9ad4 UD |
347 | + strlen (__nss_shlib_revision) + 1); |
348 | int saved_errno = errno; | |
349 | char shlib_name[shlen]; | |
350 | ||
351 | /* Construct shared object name. */ | |
352 | __stpcpy (__stpcpy (__stpcpy (__stpcpy (shlib_name, | |
353 | "libnss_"), | |
62470f60 | 354 | ni->name), |
319b9ad4 UD |
355 | ".so"), |
356 | __nss_shlib_revision); | |
357 | ||
358 | ni->library->lib_handle = __libc_dlopen (shlib_name); | |
359 | if (ni->library->lib_handle == NULL) | |
360 | { | |
361 | /* Failed to load the library. */ | |
362 | ni->library->lib_handle = (void *) -1l; | |
363 | __set_errno (saved_errno); | |
364 | } | |
3cc3ef96 | 365 | # ifdef USE_NSCD |
319b9ad4 UD |
366 | else if (is_nscd) |
367 | { | |
368 | /* Call the init function when nscd is used. */ | |
62470f60 | 369 | size_t initlen = (5 + strlen (ni->name) |
319b9ad4 UD |
370 | + strlen ("_init") + 1); |
371 | char init_name[initlen]; | |
372 | ||
373 | /* Construct the init function name. */ | |
374 | __stpcpy (__stpcpy (__stpcpy (init_name, | |
375 | "_nss_"), | |
62470f60 | 376 | ni->name), |
319b9ad4 UD |
377 | "_init"); |
378 | ||
379 | /* Find the optional init function. */ | |
380 | void (*ifct) (void (*) (size_t, struct traced_file *)) | |
381 | = __libc_dlsym (ni->library->lib_handle, init_name); | |
382 | if (ifct != NULL) | |
383 | { | |
384 | void (*cb) (size_t, struct traced_file *) = nscd_init_cb; | |
3cc3ef96 | 385 | # ifdef PTR_DEMANGLE |
319b9ad4 | 386 | PTR_DEMANGLE (cb); |
3cc3ef96 | 387 | # endif |
319b9ad4 UD |
388 | ifct (cb); |
389 | } | |
390 | } | |
3cc3ef96 | 391 | # endif |
319b9ad4 UD |
392 | } |
393 | ||
394 | return 0; | |
395 | } | |
396 | #endif | |
397 | ||
398 | ||
899d423e UD |
399 | void * |
400 | __nss_lookup_function (service_user *ni, const char *fct_name) | |
5f0e6fc7 | 401 | { |
dbe31b9a | 402 | void **found, *result; |
5f0e6fc7 | 403 | |
5f0e6fc7 RM |
404 | /* We now modify global data. Protect it. */ |
405 | __libc_lock_lock (lock); | |
406 | ||
dbe31b9a RM |
407 | /* Search the tree of functions previously requested. Data in the |
408 | tree are `known_function' structures, whose first member is a | |
409 | `const char *', the lookup key. The search returns a pointer to | |
410 | the tree node structure; the first member of the is a pointer to | |
411 | our structure (i.e. what will be a `known_function'); since the | |
412 | first member of that is the lookup key string, &FCT_NAME is close | |
413 | enough to a pointer to our structure to use as a lookup key that | |
414 | will be passed to `known_compare' (above). */ | |
415 | ||
701666b7 | 416 | found = __tsearch (&fct_name, &ni->known, &known_compare); |
0490345c JO |
417 | if (found == NULL) |
418 | /* This means out-of-memory. */ | |
419 | result = NULL; | |
420 | else if (*found != &fct_name) | |
4ec77f72 UD |
421 | { |
422 | /* The search found an existing structure in the tree. */ | |
423 | result = ((known_function *) *found)->fct_ptr; | |
bea9b193 | 424 | #ifdef PTR_DEMANGLE |
4ec77f72 | 425 | PTR_DEMANGLE (result); |
bea9b193 | 426 | #endif |
4ec77f72 | 427 | } |
dbe31b9a | 428 | else |
5f0e6fc7 | 429 | { |
dbe31b9a RM |
430 | /* This name was not known before. Now we have a node in the tree |
431 | (in the proper sorted position for FCT_NAME) that points to | |
432 | &FCT_NAME instead of any real `known_function' structure. | |
433 | Allocate a new structure and fill it in. */ | |
5f0e6fc7 | 434 | |
dbe31b9a RM |
435 | known_function *known = malloc (sizeof *known); |
436 | if (! known) | |
5f0e6fc7 | 437 | { |
b1c608fe | 438 | #if !defined DO_STATIC_NSS || defined SHARED |
dbe31b9a | 439 | remove_from_tree: |
b1c608fe | 440 | #endif |
dbe31b9a RM |
441 | /* Oops. We can't instantiate this node properly. |
442 | Remove it from the tree. */ | |
701666b7 | 443 | __tdelete (&fct_name, &ni->known, &known_compare); |
054c0457 | 444 | free (known); |
dbe31b9a | 445 | result = NULL; |
5f0e6fc7 | 446 | } |
dbe31b9a | 447 | else |
5f0e6fc7 | 448 | { |
dbe31b9a RM |
449 | /* Point the tree node at this new structure. */ |
450 | *found = known; | |
451 | known->fct_name = fct_name; | |
452 | ||
b5567b2a | 453 | #if !defined DO_STATIC_NSS || defined SHARED |
319b9ad4 UD |
454 | /* Load the appropriate library. */ |
455 | if (nss_load_library (ni) != 0) | |
054c0457 UD |
456 | /* This only happens when out of memory. */ |
457 | goto remove_from_tree; | |
dbe31b9a | 458 | |
8a523922 | 459 | if (ni->library->lib_handle == (void *) -1l) |
dbe31b9a RM |
460 | /* Library not found => function not found. */ |
461 | result = NULL; | |
462 | else | |
463 | { | |
b3fc5f84 | 464 | /* Get the desired function. */ |
62470f60 | 465 | size_t namlen = (5 + strlen (ni->name) + 1 |
dbe31b9a | 466 | + strlen (fct_name) + 1); |
b3fc5f84 | 467 | char name[namlen]; |
dbe31b9a RM |
468 | |
469 | /* Construct the function name. */ | |
b3fc5f84 | 470 | __stpcpy (__stpcpy (__stpcpy (__stpcpy (name, "_nss_"), |
62470f60 | 471 | ni->name), |
dbe31b9a RM |
472 | "_"), |
473 | fct_name); | |
474 | ||
475 | /* Look up the symbol. */ | |
b3fc5f84 | 476 | result = __libc_dlsym (ni->library->lib_handle, name); |
dbe31b9a | 477 | } |
5107cf1d UD |
478 | #else |
479 | /* We can't get function address dynamically in static linking. */ | |
480 | { | |
5107cf1d | 481 | # define DEFINE_ENT(h,nm) \ |
1522c368 UD |
482 | { #h"_get"#nm"ent_r", _nss_##h##_get##nm##ent_r }, \ |
483 | { #h"_end"#nm"ent", _nss_##h##_end##nm##ent }, \ | |
5107cf1d UD |
484 | { #h"_set"#nm"ent", _nss_##h##_set##nm##ent }, |
485 | # define DEFINE_GET(h,nm) \ | |
486 | { #h"_get"#nm"_r", _nss_##h##_get##nm##_r }, | |
487 | # define DEFINE_GETBY(h,nm,ky) \ | |
488 | { #h"_get"#nm"by"#ky"_r", _nss_##h##_get##nm##by##ky##_r }, | |
489 | static struct fct_tbl { const char *fname; void *fp; } *tp, tbl[] = | |
490 | { | |
1522c368 | 491 | # include "function.def" |
5107cf1d UD |
492 | { NULL, NULL } |
493 | }; | |
62470f60 | 494 | size_t namlen = (5 + strlen (ni->name) + 1 |
5107cf1d UD |
495 | + strlen (fct_name) + 1); |
496 | char name[namlen]; | |
497 | ||
498 | /* Construct the function name. */ | |
62470f60 | 499 | __stpcpy (__stpcpy (__stpcpy (name, ni->name), |
5107cf1d UD |
500 | "_"), |
501 | fct_name); | |
502 | ||
503 | result = NULL; | |
504 | for (tp = &tbl[0]; tp->fname; tp++) | |
505 | if (strcmp (tp->fname, name) == 0) | |
506 | { | |
507 | result = tp->fp; | |
508 | break; | |
509 | } | |
510 | } | |
511 | #endif | |
dbe31b9a RM |
512 | |
513 | /* Remember function pointer for later calls. Even if null, we | |
514 | record it so a second try needn't search the library again. */ | |
515 | known->fct_ptr = result; | |
bea9b193 | 516 | #ifdef PTR_MANGLE |
4ec77f72 | 517 | PTR_MANGLE (known->fct_ptr); |
bea9b193 | 518 | #endif |
5f0e6fc7 | 519 | } |
5f0e6fc7 RM |
520 | } |
521 | ||
5f0e6fc7 RM |
522 | /* Remove the lock. */ |
523 | __libc_lock_unlock (lock); | |
524 | ||
525 | return result; | |
526 | } | |
5556fc6a | 527 | libc_hidden_def (__nss_lookup_function) |
5f0e6fc7 RM |
528 | |
529 | ||
5f0e6fc7 | 530 | static name_database * |
dfd2257a | 531 | internal_function |
5f0e6fc7 RM |
532 | nss_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 | } | |
71412a8c | 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 | 606 | static service_user * |
dfd2257a | 607 | internal_function |
bba7bb78 | 608 | nss_parse_service_list (const char *line) |
5f0e6fc7 | 609 | { |
bba7bb78 | 610 | service_user *result = NULL, **nextp = &result; |
5f0e6fc7 | 611 | |
5f0e6fc7 RM |
612 | while (1) |
613 | { | |
614 | service_user *new_service; | |
3776d592 | 615 | const char *name; |
5f0e6fc7 RM |
616 | |
617 | while (isspace (line[0])) | |
618 | ++line; | |
619 | if (line[0] == '\0') | |
620 | /* No source specified. */ | |
621 | return result; | |
622 | ||
623 | /* Read <source> identifier. */ | |
624 | name = line; | |
625 | while (line[0] != '\0' && !isspace (line[0]) && line[0] != '[') | |
626 | ++line; | |
627 | if (name == line) | |
628 | return result; | |
629 | ||
630 | ||
1a989e00 UD |
631 | new_service = (service_user *) malloc (sizeof (service_user) |
632 | + (line - name + 1)); | |
5f0e6fc7 RM |
633 | if (new_service == NULL) |
634 | return result; | |
5f0e6fc7 | 635 | |
1a989e00 | 636 | *((char *) __mempcpy (new_service->name, name, line - name)) = '\0'; |
5f0e6fc7 RM |
637 | |
638 | /* Set default actions. */ | |
639 | new_service->actions[2 + NSS_STATUS_TRYAGAIN] = NSS_ACTION_CONTINUE; | |
640 | new_service->actions[2 + NSS_STATUS_UNAVAIL] = NSS_ACTION_CONTINUE; | |
641 | new_service->actions[2 + NSS_STATUS_NOTFOUND] = NSS_ACTION_CONTINUE; | |
642 | new_service->actions[2 + NSS_STATUS_SUCCESS] = NSS_ACTION_RETURN; | |
a68b0d31 | 643 | new_service->actions[2 + NSS_STATUS_RETURN] = NSS_ACTION_RETURN; |
5f0e6fc7 RM |
644 | new_service->library = NULL; |
645 | new_service->known = NULL; | |
646 | new_service->next = NULL; | |
647 | ||
648 | while (isspace (line[0])) | |
649 | ++line; | |
650 | ||
651 | if (line[0] == '[') | |
652 | { | |
5f0e6fc7 RM |
653 | /* Read criterions. */ |
654 | do | |
655 | ++line; | |
656 | while (line[0] != '\0' && isspace (line[0])); | |
657 | ||
658 | do | |
659 | { | |
bba7bb78 RM |
660 | int not; |
661 | enum nss_status status; | |
662 | lookup_actions action; | |
663 | ||
664 | /* Grok ! before name to mean all statii but that one. */ | |
6796bc80 UD |
665 | not = line[0] == '!'; |
666 | if (not) | |
bba7bb78 RM |
667 | ++line; |
668 | ||
5f0e6fc7 RM |
669 | /* Read status name. */ |
670 | name = line; | |
671 | while (line[0] != '\0' && !isspace (line[0]) && line[0] != '=' | |
672 | && line[0] != ']') | |
673 | ++line; | |
674 | ||
675 | /* Compare with known statii. */ | |
676 | if (line - name == 7) | |
677 | { | |
bba7bb78 | 678 | if (__strncasecmp (name, "SUCCESS", 7) == 0) |
5f0e6fc7 | 679 | status = NSS_STATUS_SUCCESS; |
bba7bb78 | 680 | else if (__strncasecmp (name, "UNAVAIL", 7) == 0) |
5f0e6fc7 RM |
681 | status = NSS_STATUS_UNAVAIL; |
682 | else | |
d44638b0 | 683 | goto finish; |
5f0e6fc7 RM |
684 | } |
685 | else if (line - name == 8) | |
686 | { | |
bba7bb78 | 687 | if (__strncasecmp (name, "NOTFOUND", 8) == 0) |
5f0e6fc7 | 688 | status = NSS_STATUS_NOTFOUND; |
bba7bb78 | 689 | else if (__strncasecmp (name, "TRYAGAIN", 8) == 0) |
5f0e6fc7 RM |
690 | status = NSS_STATUS_TRYAGAIN; |
691 | else | |
d44638b0 | 692 | goto finish; |
5f0e6fc7 RM |
693 | } |
694 | else | |
d44638b0 | 695 | goto finish; |
5f0e6fc7 RM |
696 | |
697 | while (isspace (line[0])) | |
698 | ++line; | |
699 | if (line[0] != '=') | |
d44638b0 | 700 | goto finish; |
5f0e6fc7 RM |
701 | do |
702 | ++line; | |
703 | while (isspace (line[0])); | |
704 | ||
705 | name = line; | |
706 | while (line[0] != '\0' && !isspace (line[0]) && line[0] != '=' | |
707 | && line[0] != ']') | |
708 | ++line; | |
709 | ||
bba7bb78 RM |
710 | if (line - name == 6 && __strncasecmp (name, "RETURN", 6) == 0) |
711 | action = NSS_ACTION_RETURN; | |
5f0e6fc7 | 712 | else if (line - name == 8 |
bba7bb78 RM |
713 | && __strncasecmp (name, "CONTINUE", 8) == 0) |
714 | action = NSS_ACTION_CONTINUE; | |
5f0e6fc7 | 715 | else |
d44638b0 | 716 | goto finish; |
5f0e6fc7 | 717 | |
bba7bb78 RM |
718 | if (not) |
719 | { | |
720 | /* Save the current action setting for this status, | |
721 | set them all to the given action, and reset this one. */ | |
722 | const lookup_actions save = new_service->actions[2 + status]; | |
723 | new_service->actions[2 + NSS_STATUS_TRYAGAIN] = action; | |
724 | new_service->actions[2 + NSS_STATUS_UNAVAIL] = action; | |
725 | new_service->actions[2 + NSS_STATUS_NOTFOUND] = action; | |
726 | new_service->actions[2 + NSS_STATUS_SUCCESS] = action; | |
727 | new_service->actions[2 + status] = save; | |
728 | } | |
729 | else | |
730 | new_service->actions[2 + status] = action; | |
731 | ||
732 | /* Skip white spaces. */ | |
5f0e6fc7 RM |
733 | while (isspace (line[0])) |
734 | ++line; | |
735 | } | |
736 | while (line[0] != ']'); | |
737 | ||
738 | /* Skip the ']'. */ | |
739 | ++line; | |
740 | } | |
741 | ||
bba7bb78 RM |
742 | *nextp = new_service; |
743 | nextp = &new_service->next; | |
d44638b0 PP |
744 | continue; |
745 | ||
746 | finish: | |
747 | free (new_service); | |
748 | return result; | |
5f0e6fc7 | 749 | } |
bba7bb78 RM |
750 | } |
751 | ||
752 | static name_database_entry * | |
dfd2257a | 753 | internal_function |
bba7bb78 RM |
754 | nss_getline (char *line) |
755 | { | |
756 | const char *name; | |
757 | name_database_entry *result; | |
1a989e00 | 758 | size_t len; |
bba7bb78 RM |
759 | |
760 | /* Ignore leading white spaces. ATTENTION: this is different from | |
761 | what is implemented in Solaris. The Solaris man page says a line | |
762 | beginning with a white space character is ignored. We regard | |
763 | this as just another misfeature in Solaris. */ | |
764 | while (isspace (line[0])) | |
765 | ++line; | |
766 | ||
767 | /* Recognize `<database> ":"'. */ | |
768 | name = line; | |
769 | while (line[0] != '\0' && !isspace (line[0]) && line[0] != ':') | |
770 | ++line; | |
771 | if (line[0] == '\0' || name == line) | |
772 | /* Syntax error. */ | |
773 | return NULL; | |
774 | *line++ = '\0'; | |
775 | ||
1a989e00 UD |
776 | len = strlen (name) + 1; |
777 | ||
778 | result = (name_database_entry *) malloc (sizeof (name_database_entry) + len); | |
bba7bb78 RM |
779 | if (result == NULL) |
780 | return NULL; | |
781 | ||
782 | /* Save the database name. */ | |
1a989e00 | 783 | memcpy (result->name, name, len); |
bba7bb78 RM |
784 | |
785 | /* Parse the list of services. */ | |
786 | result->service = nss_parse_service_list (line); | |
787 | ||
788 | result->next = NULL; | |
789 | return result; | |
5f0e6fc7 RM |
790 | } |
791 | ||
792 | ||
d3b9fd9e | 793 | #if !defined DO_STATIC_NSS || defined SHARED |
5f0e6fc7 | 794 | static service_library * |
dfd2257a | 795 | internal_function |
5f0e6fc7 RM |
796 | nss_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. */ |
823 | static void | |
824 | nss_load_all_libraries (const char *service, const char *def) | |
825 | { | |
826 | service_user *ni = NULL; | |
827 | ||
828 | if (__nss_database_lookup (service, NULL, def, &ni) == 0) | |
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. */ |
838 | void | |
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. */ | |
848 | nss_load_all_libraries ("passwd", "compat [NOTFOUND=return] files"); | |
849 | nss_load_all_libraries ("group", "compat [NOTFOUND=return] files"); | |
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 |
862 | static void |
863 | free_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. */ | |
887 | libc_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 | ||
901 | libc_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 | } |