1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <libc-lock.h>
25 #include <link.h> /* We need some help from ld.so. */
33 /* Prototypes for the local functions. */
34 static void *nss_lookup_function (service_user
*ni
, const char *fct_name
);
35 static name_database
*nss_parse_file (const char *fname
);
36 static name_database_entry
*nss_getline (char *line
);
37 static service_user
*nss_parse_service_list (const char *line
);
38 static service_library
*nss_new_service (name_database
*database
,
42 /* Declare external database variables. */
43 #define DEFINE_DATABASE(name) \
44 extern service_user *__nss_##name##_database; \
45 weak_extern (__nss_##name##_database)
46 #include "databases.def"
47 #undef DEFINE_DATABASE
49 /* Structure to map database name to variable. */
56 #define DEFINE_DATABASE(name) \
57 { #name, &__nss_##name##_database },
58 #include "databases.def"
59 #undef DEFINE_DATABASE
63 __libc_lock_define_initialized (static, lock
)
66 /* Nonzero if the sevices are already initialized. */
67 static int nss_initialized
;
70 /* The root of the whole data base. */
71 static name_database
*service_table
;
74 /* -1 == database not found
75 0 == database entry pointer stored */
77 __nss_database_lookup (const char *database
, const char *alternate_name
,
78 const char *defconfig
, service_user
**ni
)
80 /* Prevent multiple threads to change the service table. */
81 __libc_lock_lock (lock
);
83 /* Reconsider database variable in case some other thread called
84 `__nss_configure_lookup' while we waited for the lock. */
87 __libc_lock_unlock (lock
);
91 if (nss_initialized
== 0 && service_table
== NULL
)
92 /* Read config file. */
93 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
95 /* Test whether configuration data is available. */
96 if (service_table
!= NULL
)
98 /* Return first `service_user' entry for DATABASE. */
99 name_database_entry
*entry
;
101 /* XXX Could use some faster mechanism here. But each database is
102 only requested once and so this might not be critical. */
103 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
104 if (strcmp (database
, entry
->name
) == 0)
105 *ni
= entry
->service
;
107 if (*ni
== NULL
&& alternate_name
!= NULL
)
108 /* We haven't found a an entry so far. Try to find it with
109 the alternative name. */
110 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
111 if (strcmp (alternate_name
, entry
->name
) == 0)
112 *ni
= entry
->service
;
115 /* No configuration data is available, either because nsswitch.conf
116 doesn't exist or because it doesn't has a line for this database.
118 DEFCONFIG specifies the default service list for this database,
119 or null to use the most common default. */
121 *ni
= nss_parse_service_list (defconfig
122 ?: "nis [NOTFOUND=return] files");
124 __libc_lock_unlock (lock
);
131 0 == adjusted for next function */
133 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
135 *fctp
= nss_lookup_function (*ni
, fct_name
);
138 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
139 && (*ni
)->next
!= NULL
)
143 *fctp
= nss_lookup_function (*ni
, fct_name
);
146 return *fctp
!= NULL
? 0 : -1;
151 0 == adjusted for next function
154 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
159 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
160 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
161 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
162 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
167 /* This is really only for debugging. */
168 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
169 __libc_fatal ("illegal status in " __FUNCTION__
);
171 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
175 if ((*ni
)->next
== NULL
)
182 *fctp
= nss_lookup_function (*ni
, fct_name
);
185 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
186 && (*ni
)->next
!= NULL
);
188 return *fctp
!= NULL
? 0 : -1;
193 __nss_configure_lookup (const char *dbname
, const char *service_line
)
195 service_user
*new_db
;
198 for (cnt
= 0; cnt
< sizeof databases
; ++cnt
)
200 int cmp
= strcmp (dbname
, databases
[cnt
].name
);
205 __set_errno (EINVAL
);
210 if (cnt
== sizeof databases
)
212 __set_errno (EINVAL
);
216 /* Test whether it is really used. */
217 if (databases
[cnt
].dbp
== NULL
)
218 /* Nothing to do, but we could do. */
221 /* Try to generate new data. */
222 new_db
= nss_parse_service_list (service_line
);
225 /* Illegal service specification. */
226 __set_errno (EINVAL
);
230 /* Prevent multiple threads to change the service table. */
231 __libc_lock_lock (lock
);
233 /* Install new rules. */
234 *databases
[cnt
].dbp
= new_db
;
236 __libc_lock_unlock (lock
);
243 nss_dlerror_run (void (*operate
) (void))
245 char *last_errstring
= NULL
;
246 const char *last_object_name
= NULL
;
249 (void) _dl_catch_error (&last_errstring
, &last_object_name
, operate
);
251 result
= last_errstring
!= NULL
;
253 free (last_errstring
);
259 /* Comparison function for searching NI->known tree. */
261 known_compare (const void *p1
, const void *p2
)
263 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
264 *(const char *const *) p2
);
269 nss_lookup_function (service_user
*ni
, const char *fct_name
)
271 void **found
, *result
;
273 /* We now modify global data. Protect it. */
274 __libc_lock_lock (lock
);
276 /* Search the tree of functions previously requested. Data in the
277 tree are `known_function' structures, whose first member is a
278 `const char *', the lookup key. The search returns a pointer to
279 the tree node structure; the first member of the is a pointer to
280 our structure (i.e. what will be a `known_function'); since the
281 first member of that is the lookup key string, &FCT_NAME is close
282 enough to a pointer to our structure to use as a lookup key that
283 will be passed to `known_compare' (above). */
285 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
286 if (*found
!= &fct_name
)
287 /* The search found an existing structure in the tree. */
288 result
= ((known_function
*) *found
)->fct_ptr
;
291 /* This name was not known before. Now we have a node in the tree
292 (in the proper sorted position for FCT_NAME) that points to
293 &FCT_NAME instead of any real `known_function' structure.
294 Allocate a new structure and fill it in. */
296 known_function
*known
= malloc (sizeof *known
);
300 /* Oops. We can't instantiate this node properly.
301 Remove it from the tree. */
302 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
307 /* Point the tree node at this new structure. */
309 known
->fct_name
= fct_name
;
311 if (ni
->library
== NULL
)
313 /* This service has not yet been used. Fetch the service
314 library for it, creating a new one if need be. If there
315 is no service table from the file, this static variable
316 holds the head of the service_library list made from the
317 default configuration. */
318 static name_database default_table
;
319 ni
->library
= nss_new_service (service_table
?: &default_table
,
321 if (ni
->library
== NULL
)
323 /* This only happens when out of memory. */
325 goto remove_from_tree
;
329 if (ni
->library
->lib_handle
== NULL
)
331 /* Load the shared library. */
332 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
333 + sizeof (NSS_SHLIB_REVISION
));
334 char shlib_name
[shlen
];
338 /* Open and relocate the shared object. */
339 ni
->library
->lib_handle
= _dl_open (shlib_name
, RTLD_LAZY
);
342 /* Construct shared object name. */
343 __stpcpy (__stpcpy (__stpcpy (shlib_name
, "libnss_"),
345 ".so" NSS_SHLIB_REVISION
);
347 if (nss_dlerror_run (do_open
) != 0)
348 /* Failed to load the library. */
349 ni
->library
->lib_handle
= (void *) -1l;
352 if (ni
->library
->lib_handle
== (void *) -1l)
353 /* Library not found => function not found. */
357 /* Get the desired function. Again, GNU ld.so magic ahead. */
358 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
359 + strlen (fct_name
) + 1);
361 struct link_map
*map
= ni
->library
->lib_handle
;
363 const ElfW(Sym
) *ref
= NULL
;
366 struct link_map
*scope
[2] = { map
, NULL
};
367 loadbase
= _dl_lookup_symbol (name
, &ref
,
368 scope
, map
->l_name
, 0);
371 /* Construct the function name. */
372 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
377 /* Look up the symbol. */
378 result
= (nss_dlerror_run (get_sym
)
379 ? NULL
: (void *) (loadbase
+ ref
->st_value
));
382 /* Remember function pointer for later calls. Even if null, we
383 record it so a second try needn't search the library again. */
384 known
->fct_ptr
= result
;
388 /* Remove the lock. */
389 __libc_lock_unlock (lock
);
395 static name_database
*
396 nss_parse_file (const char *fname
)
399 name_database
*result
;
400 name_database_entry
*last
;
404 /* Open the configuration file. */
405 fp
= fopen (fname
, "r");
409 result
= (name_database
*) malloc (sizeof (name_database
));
413 result
->entry
= NULL
;
414 result
->library
= NULL
;
420 name_database_entry
*this;
424 n
= __getline (&line
, &len
, fp
);
427 if (line
[n
- 1] == '\n')
430 /* Because the file format does not know any form of quoting we
431 can search forward for the next '#' character and if found
432 make it terminating the line. */
433 cp
= strchr (line
, '#');
437 /* If the line is blank it is ignored. */
441 /* Each line completely specifies the actions for a database. */
442 this = nss_getline (line
);
448 result
->entry
= this;
455 /* Free the buffer. */
457 /* Close configuration file. */
464 /* Read the source names:
465 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
467 static service_user
*
468 nss_parse_service_list (const char *line
)
470 service_user
*result
= NULL
, **nextp
= &result
;
474 service_user
*new_service
;
477 while (isspace (line
[0]))
480 /* No source specified. */
483 /* Read <source> identifier. */
485 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
491 new_service
= (service_user
*) malloc (sizeof (service_user
));
492 if (new_service
== NULL
)
496 char *source
= (char *) malloc (line
- name
+ 1);
502 memcpy (source
, name
, line
- name
);
503 source
[line
- name
] = '\0';
505 new_service
->name
= source
;
508 /* Set default actions. */
509 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
510 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
511 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
512 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
513 new_service
->actions
[2 + NSS_STATUS_RETURN
] = NSS_ACTION_RETURN
;
514 new_service
->library
= NULL
;
515 new_service
->known
= NULL
;
516 new_service
->next
= NULL
;
518 while (isspace (line
[0]))
523 /* Read criterions. */
526 while (line
[0] != '\0' && isspace (line
[0]));
531 enum nss_status status
;
532 lookup_actions action
;
534 /* Grok ! before name to mean all statii but that one. */
535 if (not = line
[0] == '!')
538 /* Read status name. */
540 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
544 /* Compare with known statii. */
545 if (line
- name
== 7)
547 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
548 status
= NSS_STATUS_SUCCESS
;
549 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
550 status
= NSS_STATUS_UNAVAIL
;
554 else if (line
- name
== 8)
556 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
557 status
= NSS_STATUS_NOTFOUND
;
558 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
559 status
= NSS_STATUS_TRYAGAIN
;
566 while (isspace (line
[0]))
572 while (isspace (line
[0]));
575 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
579 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
580 action
= NSS_ACTION_RETURN
;
581 else if (line
- name
== 8
582 && __strncasecmp (name
, "CONTINUE", 8) == 0)
583 action
= NSS_ACTION_CONTINUE
;
589 /* Save the current action setting for this status,
590 set them all to the given action, and reset this one. */
591 const lookup_actions save
= new_service
->actions
[2 + status
];
592 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
593 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
594 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
595 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
596 new_service
->actions
[2 + status
] = save
;
599 new_service
->actions
[2 + status
] = action
;
601 /* Skip white spaces. */
602 while (isspace (line
[0]))
605 while (line
[0] != ']');
611 *nextp
= new_service
;
612 nextp
= &new_service
->next
;
616 static name_database_entry
*
617 nss_getline (char *line
)
620 name_database_entry
*result
;
622 /* Ignore leading white spaces. ATTENTION: this is different from
623 what is implemented in Solaris. The Solaris man page says a line
624 beginning with a white space character is ignored. We regard
625 this as just another misfeature in Solaris. */
626 while (isspace (line
[0]))
629 /* Recognize `<database> ":"'. */
631 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
633 if (line
[0] == '\0' || name
== line
)
638 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
));
642 /* Save the database name. */
644 const size_t len
= strlen (name
) + 1;
645 char *new = malloc (len
);
651 result
->name
= memcpy (new, name
, len
);
654 /* Parse the list of services. */
655 result
->service
= nss_parse_service_list (line
);
662 static service_library
*
663 nss_new_service (name_database
*database
, const char *name
)
665 service_library
**currentp
= &database
->library
;
667 while (*currentp
!= NULL
)
669 if (strcmp ((*currentp
)->name
, name
) == 0)
671 currentp
= &(*currentp
)->next
;
674 /* We have to add the new service. */
675 *currentp
= (service_library
*) malloc (sizeof (service_library
));
676 if (*currentp
== NULL
)
679 (*currentp
)->name
= name
;
680 (*currentp
)->lib_handle
= NULL
;
681 (*currentp
)->next
= NULL
;