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