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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
23 #include <libc-lock.h>
30 #include "../elf/link.h" /* We need some help from ld.so. */
32 /* Prototypes for the local functions. */
33 static void nss_init (void);
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 __libc_lock_define_initialized (static, lock
);
45 /* Global variable. */
46 struct __res_state _res
;
49 /* Nonzero if the sevices are already initialized. */
50 static int nss_initialized
;
53 /* The root of the whole data base. */
54 static name_database
*service_table
;
60 /* Prevent multiple threads to change the service table. */
61 __libc_lock_lock (lock
);
63 if (service_table
== NULL
)
64 service_table
= nss_parse_file (_PATH_NSSWITCH_CONF
);
66 __libc_lock_unlock (lock
);
70 /* -1 == database not found
71 0 == database entry pointer stored */
73 __nss_database_lookup (const char *database
, const char *defconfig
,
76 name_database_entry
*entry
;
78 if (nss_initialized
== 0)
81 /* Test whether configuration data is available. */
84 /* Return first `service_user' entry for DATABASE.
85 XXX Will use perfect hashing function for known databases. */
87 /* XXX Could use some faster mechanism here. But each database is
88 only requested once and so this might not be critical. */
89 for (entry
= service_table
->entry
; entry
!= NULL
; entry
= entry
->next
)
90 if (strcmp (database
, entry
->name
) == 0)
97 /* No configuration data is available, either because nsswitch.conf
98 doesn't exist or because it doesn't have a line for this database. */
99 entry
= malloc (sizeof *entry
);
102 entry
->name
= database
;
103 /* DEFCONFIG specifies the default service list for this database,
104 or null to use the most common default. */
105 entry
->service
= nss_parse_service_list (defconfig
?:
106 "compat [NOTFOUND=return] files");
108 *ni
= entry
->service
;
114 0 == adjusted for next function */
116 __nss_lookup (service_user
**ni
, const char *fct_name
, void **fctp
)
118 *fctp
= nss_lookup_function (*ni
, fct_name
);
121 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
122 && (*ni
)->next
!= NULL
)
126 *fctp
= nss_lookup_function (*ni
, fct_name
);
129 return *fctp
!= NULL
? 0 : -1;
134 0 == adjusted for next function
137 __nss_next (service_user
**ni
, const char *fct_name
, void **fctp
, int status
,
142 if (nss_next_action (*ni
, NSS_STATUS_TRYAGAIN
) == NSS_ACTION_RETURN
143 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_RETURN
144 && nss_next_action (*ni
, NSS_STATUS_NOTFOUND
) == NSS_ACTION_RETURN
145 && nss_next_action (*ni
, NSS_STATUS_SUCCESS
) == NSS_ACTION_RETURN
)
150 /* This is really only for debugging. */
151 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_SUCCESS
)
152 __libc_fatal ("illegal status in " __FUNCTION__
);
154 if (nss_next_action (*ni
, status
) == NSS_ACTION_RETURN
)
158 if ((*ni
)->next
== NULL
)
165 *fctp
= nss_lookup_function (*ni
, fct_name
);
168 && nss_next_action (*ni
, NSS_STATUS_UNAVAIL
) == NSS_ACTION_CONTINUE
169 && (*ni
)->next
!= NULL
);
171 return *fctp
!= NULL
? 0 : -1;
176 nss_dlerror_run (void (*operate
) (void))
178 const char *last_errstring
= NULL
;
179 const char *last_object_name
= NULL
;
181 (void) _dl_catch_error (&last_errstring
, &last_object_name
, operate
);
183 return last_errstring
!= NULL
;
187 /* Comparison function for searching NI->known tree. */
189 known_compare (const void *p1
, const void *p2
)
191 return p1
== p2
? 0 : strcmp (*(const char *const *) p1
,
192 *(const char *const *) p2
);
197 nss_lookup_function (service_user
*ni
, const char *fct_name
)
199 void **found
, *result
;
201 /* We now modify global data. Protect it. */
202 __libc_lock_lock (lock
);
204 /* Search the tree of functions previously requested. Data in the
205 tree are `known_function' structures, whose first member is a
206 `const char *', the lookup key. The search returns a pointer to
207 the tree node structure; the first member of the is a pointer to
208 our structure (i.e. what will be a `known_function'); since the
209 first member of that is the lookup key string, &FCT_NAME is close
210 enough to a pointer to our structure to use as a lookup key that
211 will be passed to `known_compare' (above). */
213 found
= __tsearch (&fct_name
, (void **) &ni
->known
, &known_compare
);
214 if (*found
!= &fct_name
)
215 /* The search found an existing structure in the tree. */
216 result
= ((known_function
*) *found
)->fct_ptr
;
219 /* This name was not known before. Now we have a node in the tree
220 (in the proper sorted position for FCT_NAME) that points to
221 &FCT_NAME instead of any real `known_function' structure.
222 Allocate a new structure and fill it in. */
224 known_function
*known
= malloc (sizeof *known
);
228 /* Oops. We can't instantiate this node properly.
229 Remove it from the tree. */
230 __tdelete (&fct_name
, (void **) &ni
->known
, &known_compare
);
235 /* Point the tree node at this new structure. */
237 known
->fct_name
= fct_name
;
239 if (ni
->library
== NULL
)
241 /* This service has not yet been used. Fetch the service
242 library for it, creating a new one if need be. If there
243 is no service table from the file, this static variable
244 holds the head of the service_library list made from the
245 default configuration. */
246 static name_database default_table
;
247 ni
->library
= nss_new_service (service_table
?: &default_table
,
249 if (ni
->library
== NULL
)
251 /* This only happens when out of memory. */
253 goto remove_from_tree
;
257 if (ni
->library
->lib_handle
== NULL
)
259 /* Load the shared library. */
260 size_t shlen
= (7 + strlen (ni
->library
->name
) + 3
261 + sizeof (NSS_SHLIB_REVISION
));
262 char shlib_name
[shlen
];
266 /* Open and relocate the shared object. */
267 ni
->library
->lib_handle
= _dl_open (shlib_name
, RTLD_LAZY
);
270 /* Construct shared object name. */
271 __stpcpy (__stpcpy (__stpcpy (shlib_name
, "libnss_"),
273 ".so" NSS_SHLIB_REVISION
);
275 if (nss_dlerror_run (do_open
) != 0)
276 /* Failed to load the library. */
277 ni
->library
->lib_handle
= (void *) -1;
280 if (ni
->library
->lib_handle
== (void *) -1)
281 /* Library not found => function not found. */
285 /* Get the desired function. Again, GNU ld.so magic ahead. */
286 size_t namlen
= (5 + strlen (ni
->library
->name
) + 1
287 + strlen (fct_name
) + 1);
289 struct link_map
*map
= ni
->library
->lib_handle
;
291 const ElfW(Sym
) *ref
= NULL
;
294 struct link_map
*scope
[2] = { map
, NULL
};
295 loadbase
= _dl_lookup_symbol (name
, &ref
,
296 scope
, map
->l_name
, 0, 0);
299 /* Construct the function name. */
300 __stpcpy (__stpcpy (__stpcpy (__stpcpy (name
, "_nss_"),
305 /* Look up the symbol. */
306 result
= (nss_dlerror_run (get_sym
)
307 ? NULL
: (void *) (loadbase
+ ref
->st_value
));
310 /* Remember function pointer for later calls. Even if null, we
311 record it so a second try needn't search the library again. */
312 known
->fct_ptr
= result
;
316 /* Remove the lock. */
317 __libc_lock_unlock (lock
);
323 static name_database
*
324 nss_parse_file (const char *fname
)
327 name_database
*result
;
328 name_database_entry
*last
;
332 /* Open the configuration file. */
333 fp
= fopen (fname
, "r");
337 result
= (name_database
*) malloc (sizeof (name_database
));
341 result
->entry
= NULL
;
342 result
->library
= NULL
;
348 name_database_entry
*this;
352 n
= __getline (&line
, &len
, fp
);
355 if (line
[n
- 1] == '\n')
358 /* Because the file format does not know any form of quoting we
359 can search forward for the next '#' character and if found
360 make it terminating the line. */
361 cp
= strchr (line
, '#');
365 /* If the line is blank it is ignored. */
369 /* Each line completely specifies the actions for a database. */
370 this = nss_getline (line
);
376 result
->entry
= this;
383 /* Free the buffer. */
385 /* Close configuration file. */
392 /* Read the source names: `<source> ( "[" <status> "=" <action> "]" )*'. */
393 static service_user
*
394 nss_parse_service_list (const char *line
)
396 service_user
*result
= NULL
, **nextp
= &result
;
400 service_user
*new_service
;
403 while (isspace (line
[0]))
406 /* No source specified. */
409 /* Read <source> identifier. */
411 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
417 new_service
= (service_user
*) malloc (sizeof (service_user
));
418 if (new_service
== NULL
)
422 char *source
= (char *) malloc (line
- name
+ 1);
428 memcpy (source
, name
, line
- name
);
429 source
[line
- name
] = '\0';
431 new_service
->name
= source
;
434 /* Set default actions. */
435 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = NSS_ACTION_CONTINUE
;
436 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = NSS_ACTION_CONTINUE
;
437 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = NSS_ACTION_CONTINUE
;
438 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = NSS_ACTION_RETURN
;
439 new_service
->library
= NULL
;
440 new_service
->known
= NULL
;
441 new_service
->next
= NULL
;
443 while (isspace (line
[0]))
448 /* Read criterions. */
451 while (line
[0] != '\0' && isspace (line
[0]));
456 enum nss_status status
;
457 lookup_actions action
;
459 /* Grok ! before name to mean all statii but that one. */
460 if (not = line
[0] == '!')
463 /* Read status name. */
465 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
469 /* Compare with known statii. */
470 if (line
- name
== 7)
472 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
473 status
= NSS_STATUS_SUCCESS
;
474 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
475 status
= NSS_STATUS_UNAVAIL
;
479 else if (line
- name
== 8)
481 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
482 status
= NSS_STATUS_NOTFOUND
;
483 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
484 status
= NSS_STATUS_TRYAGAIN
;
491 while (isspace (line
[0]))
497 while (isspace (line
[0]));
500 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
504 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
505 action
= NSS_ACTION_RETURN
;
506 else if (line
- name
== 8
507 && __strncasecmp (name
, "CONTINUE", 8) == 0)
508 action
= NSS_ACTION_CONTINUE
;
514 /* Save the current action setting for this status,
515 set them all to the given action, and reset this one. */
516 const lookup_actions save
= new_service
->actions
[2 + status
];
517 new_service
->actions
[2 + NSS_STATUS_TRYAGAIN
] = action
;
518 new_service
->actions
[2 + NSS_STATUS_UNAVAIL
] = action
;
519 new_service
->actions
[2 + NSS_STATUS_NOTFOUND
] = action
;
520 new_service
->actions
[2 + NSS_STATUS_SUCCESS
] = action
;
521 new_service
->actions
[2 + status
] = save
;
524 new_service
->actions
[2 + status
] = action
;
526 /* Skip white spaces. */
527 while (isspace (line
[0]))
530 while (line
[0] != ']');
536 *nextp
= new_service
;
537 nextp
= &new_service
->next
;
541 static name_database_entry
*
542 nss_getline (char *line
)
545 name_database_entry
*result
;
547 /* Ignore leading white spaces. ATTENTION: this is different from
548 what is implemented in Solaris. The Solaris man page says a line
549 beginning with a white space character is ignored. We regard
550 this as just another misfeature in Solaris. */
551 while (isspace (line
[0]))
554 /* Recognize `<database> ":"'. */
556 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != ':')
558 if (line
[0] == '\0' || name
== line
)
563 result
= (name_database_entry
*) malloc (sizeof (name_database_entry
));
567 /* Save the database name. */
569 const size_t len
= strlen (name
) + 1;
570 char *new = malloc (len
);
576 result
->name
= memcpy (new, name
, len
);
579 /* Parse the list of services. */
580 result
->service
= nss_parse_service_list (line
);
587 static service_library
*
588 nss_new_service (name_database
*database
, const char *name
)
590 service_library
**currentp
= &database
->library
;
592 while (*currentp
!= NULL
)
594 if (strcmp ((*currentp
)->name
, name
) == 0)
596 currentp
= &(*currentp
)->next
;
599 /* We have to add the new service. */
600 *currentp
= (service_library
*) malloc (sizeof (service_library
));
601 if (*currentp
== NULL
)
604 (*currentp
)->name
= name
;
605 (*currentp
)->lib_handle
= NULL
;
606 (*currentp
)->next
= NULL
;