getusershell.c revision 1.28 1 1.28 christos /* $NetBSD: getusershell.c,v 1.28 2011/10/15 23:00:01 christos Exp $ */
2 1.24 lukem
3 1.24 lukem /*-
4 1.24 lukem * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
5 1.24 lukem * All rights reserved.
6 1.24 lukem *
7 1.24 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.24 lukem * by Luke Mewburn.
9 1.24 lukem *
10 1.24 lukem * Redistribution and use in source and binary forms, with or without
11 1.24 lukem * modification, are permitted provided that the following conditions
12 1.24 lukem * are met:
13 1.24 lukem * 1. Redistributions of source code must retain the above copyright
14 1.24 lukem * notice, this list of conditions and the following disclaimer.
15 1.24 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.24 lukem * notice, this list of conditions and the following disclaimer in the
17 1.24 lukem * documentation and/or other materials provided with the distribution.
18 1.24 lukem *
19 1.24 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.24 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.24 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.24 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.24 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.24 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.24 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.24 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.24 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.24 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.24 lukem * POSSIBILITY OF SUCH DAMAGE.
30 1.24 lukem */
31 1.5 cgd
32 1.1 cgd /*
33 1.5 cgd * Copyright (c) 1985, 1993
34 1.5 cgd * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.23 agc * 3. Neither the name of the University nor the names of its contributors
45 1.1 cgd * may be used to endorse or promote products derived from this software
46 1.1 cgd * without specific prior written permission.
47 1.1 cgd *
48 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 cgd * SUCH DAMAGE.
59 1.1 cgd */
60 1.1 cgd
61 1.6 christos #include <sys/cdefs.h>
62 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
63 1.5 cgd #if 0
64 1.5 cgd static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93";
65 1.5 cgd #else
66 1.28 christos __RCSID("$NetBSD: getusershell.c,v 1.28 2011/10/15 23:00:01 christos Exp $");
67 1.5 cgd #endif
68 1.1 cgd #endif /* LIBC_SCCS and not lint */
69 1.1 cgd
70 1.7 jtc #include "namespace.h"
71 1.24 lukem #include "reentrant.h"
72 1.24 lukem
73 1.1 cgd #include <sys/param.h>
74 1.1 cgd #include <sys/file.h>
75 1.17 lukem
76 1.24 lukem #include <assert.h>
77 1.1 cgd #include <ctype.h>
78 1.17 lukem #include <errno.h>
79 1.13 lukem #include <nsswitch.h>
80 1.17 lukem #include <paths.h>
81 1.22 wiz #include <stdarg.h>
82 1.17 lukem #include <stdio.h>
83 1.1 cgd #include <stdlib.h>
84 1.13 lukem #include <string.h>
85 1.17 lukem #include <unistd.h>
86 1.17 lukem
87 1.13 lukem #ifdef HESIOD
88 1.13 lukem #include <hesiod.h>
89 1.13 lukem #endif
90 1.13 lukem #ifdef YP
91 1.13 lukem #include <rpc/rpc.h>
92 1.13 lukem #include <rpcsvc/ypclnt.h>
93 1.13 lukem #include <rpcsvc/yp_prot.h>
94 1.13 lukem #endif
95 1.7 jtc
96 1.7 jtc #ifdef __weak_alias
97 1.20 mycroft __weak_alias(endusershell,_endusershell)
98 1.20 mycroft __weak_alias(getusershell,_getusershell)
99 1.20 mycroft __weak_alias(setusershell,_setusershell)
100 1.7 jtc #endif
101 1.1 cgd
102 1.1 cgd /*
103 1.24 lukem * Local shells should NOT be added here.
104 1.24 lukem * They should be added in /etc/shells.
105 1.1 cgd */
106 1.24 lukem static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
107 1.24 lukem
108 1.24 lukem #ifdef _REENTRANT
109 1.24 lukem static mutex_t __shellmutex = MUTEX_INITIALIZER;
110 1.24 lukem #endif
111 1.24 lukem
112 1.24 lukem static char curshell[MAXPATHLEN + 2];
113 1.24 lukem
114 1.24 lukem static const char *const *curokshell = okshells;
115 1.24 lukem static int shellsfound = 0;
116 1.24 lukem
117 1.24 lukem /*
118 1.24 lukem * files methods
119 1.24 lukem */
120 1.1 cgd
121 1.24 lukem /* state shared between files methods */
122 1.24 lukem struct files_state {
123 1.24 lukem FILE *fp;
124 1.24 lukem };
125 1.24 lukem
126 1.24 lukem static struct files_state _files_state;
127 1.13 lukem
128 1.1 cgd
129 1.24 lukem static int
130 1.24 lukem _files_start(struct files_state *state)
131 1.1 cgd {
132 1.1 cgd
133 1.24 lukem _DIAGASSERT(state != NULL);
134 1.24 lukem
135 1.24 lukem if (state->fp == NULL) {
136 1.28 christos state->fp = fopen(_PATH_SHELLS, "re");
137 1.24 lukem if (state->fp == NULL)
138 1.24 lukem return NS_UNAVAIL;
139 1.24 lukem } else {
140 1.24 lukem rewind(state->fp);
141 1.24 lukem }
142 1.24 lukem return NS_SUCCESS;
143 1.1 cgd }
144 1.1 cgd
145 1.24 lukem static int
146 1.24 lukem _files_end(struct files_state *state)
147 1.1 cgd {
148 1.24 lukem
149 1.24 lukem _DIAGASSERT(state != NULL);
150 1.24 lukem
151 1.24 lukem if (state->fp) {
152 1.24 lukem (void) fclose(state->fp);
153 1.24 lukem state->fp = NULL;
154 1.24 lukem }
155 1.24 lukem return NS_SUCCESS;
156 1.1 cgd }
157 1.1 cgd
158 1.24 lukem /*ARGSUSED*/
159 1.24 lukem static int
160 1.24 lukem _files_setusershell(void *nsrv, void *nscb, va_list ap)
161 1.1 cgd {
162 1.1 cgd
163 1.24 lukem return _files_start(&_files_state);
164 1.1 cgd }
165 1.1 cgd
166 1.24 lukem /*ARGSUSED*/
167 1.24 lukem static int
168 1.24 lukem _files_endusershell(void *nsrv, void *nscb, va_list ap)
169 1.24 lukem {
170 1.13 lukem
171 1.24 lukem return _files_end(&_files_state);
172 1.24 lukem }
173 1.13 lukem
174 1.16 christos /*ARGSUSED*/
175 1.13 lukem static int
176 1.24 lukem _files_getusershell(void *nsrv, void *nscb, va_list ap)
177 1.1 cgd {
178 1.24 lukem char **retval = va_arg(ap, char **);
179 1.24 lukem
180 1.13 lukem char *sp, *cp;
181 1.24 lukem int rv;
182 1.13 lukem
183 1.24 lukem _DIAGASSERT(retval != NULL);
184 1.13 lukem
185 1.24 lukem *retval = NULL;
186 1.24 lukem if (_files_state.fp == NULL) { /* only start if file not open yet */
187 1.24 lukem rv = _files_start(&_files_state);
188 1.24 lukem if (rv != NS_SUCCESS)
189 1.24 lukem return rv;
190 1.24 lukem }
191 1.13 lukem
192 1.24 lukem while (fgets(curshell, sizeof(curshell) - 1, _files_state.fp) != NULL) {
193 1.24 lukem sp = cp = curshell;
194 1.1 cgd while (*cp != '#' && *cp != '/' && *cp != '\0')
195 1.1 cgd cp++;
196 1.1 cgd if (*cp == '#' || *cp == '\0')
197 1.1 cgd continue;
198 1.13 lukem sp = cp;
199 1.21 itohy while (!isspace((unsigned char) *cp) && *cp != '#'
200 1.21 itohy && *cp != '\0')
201 1.1 cgd cp++;
202 1.1 cgd *cp++ = '\0';
203 1.24 lukem *retval = sp;
204 1.24 lukem return NS_SUCCESS;
205 1.1 cgd }
206 1.24 lukem
207 1.24 lukem return NS_NOTFOUND;
208 1.13 lukem }
209 1.13 lukem
210 1.24 lukem
211 1.13 lukem #ifdef HESIOD
212 1.24 lukem /*
213 1.24 lukem * dns methods
214 1.24 lukem */
215 1.24 lukem
216 1.24 lukem /* state shared between dns methods */
217 1.24 lukem struct dns_state {
218 1.24 lukem void *context; /* Hesiod context */
219 1.24 lukem int num; /* shell index, -1 if no more */
220 1.24 lukem };
221 1.24 lukem
222 1.24 lukem static struct dns_state _dns_state;
223 1.24 lukem
224 1.24 lukem static int
225 1.24 lukem _dns_start(struct dns_state *state)
226 1.24 lukem {
227 1.24 lukem
228 1.24 lukem _DIAGASSERT(state != NULL);
229 1.24 lukem
230 1.24 lukem state->num = 0;
231 1.24 lukem if (state->context == NULL) { /* setup Hesiod */
232 1.24 lukem if (hesiod_init(&state->context) == -1)
233 1.24 lukem return NS_UNAVAIL;
234 1.24 lukem }
235 1.24 lukem
236 1.24 lukem return NS_SUCCESS;
237 1.24 lukem }
238 1.24 lukem
239 1.24 lukem static int
240 1.24 lukem _dns_end(struct dns_state *state)
241 1.24 lukem {
242 1.24 lukem
243 1.24 lukem _DIAGASSERT(state != NULL);
244 1.24 lukem
245 1.24 lukem state->num = 0;
246 1.24 lukem if (state->context) {
247 1.24 lukem hesiod_end(state->context);
248 1.24 lukem state->context = NULL;
249 1.24 lukem }
250 1.24 lukem return NS_SUCCESS;
251 1.24 lukem }
252 1.24 lukem
253 1.24 lukem /*ARGSUSED*/
254 1.24 lukem static int
255 1.24 lukem _dns_setusershell(void *nsrv, void *nscb, va_list ap)
256 1.24 lukem {
257 1.24 lukem
258 1.24 lukem return _dns_start(&_dns_state);
259 1.24 lukem }
260 1.13 lukem
261 1.16 christos /*ARGSUSED*/
262 1.13 lukem static int
263 1.24 lukem _dns_endusershell(void *nsrv, void *nscb, va_list ap)
264 1.24 lukem {
265 1.24 lukem
266 1.24 lukem return _dns_end(&_dns_state);
267 1.24 lukem }
268 1.24 lukem
269 1.24 lukem /*ARGSUSED*/
270 1.24 lukem static int
271 1.24 lukem _dns_getusershell(void *nsrv, void *nscb, va_list ap)
272 1.24 lukem {
273 1.24 lukem char **retval = va_arg(ap, char **);
274 1.24 lukem
275 1.24 lukem char shellname[] = "shells-NNNNNNNNNN";
276 1.24 lukem char **hp, *ep;
277 1.24 lukem int rv;
278 1.24 lukem
279 1.24 lukem _DIAGASSERT(retval != NULL);
280 1.24 lukem
281 1.24 lukem *retval = NULL;
282 1.24 lukem
283 1.24 lukem if (_dns_state.num == -1) /* exhausted search */
284 1.24 lukem return NS_NOTFOUND;
285 1.24 lukem
286 1.24 lukem if (_dns_state.context == NULL) {
287 1.24 lukem /* only start if Hesiod not setup */
288 1.24 lukem rv = _dns_start(&_dns_state);
289 1.24 lukem if (rv != NS_SUCCESS)
290 1.24 lukem return rv;
291 1.24 lukem }
292 1.24 lukem
293 1.24 lukem hp = NULL;
294 1.24 lukem rv = NS_NOTFOUND;
295 1.19 lukem
296 1.24 lukem /* find shells-NNN */
297 1.24 lukem snprintf(shellname, sizeof(shellname), "shells-%d", _dns_state.num);
298 1.24 lukem _dns_state.num++;
299 1.24 lukem
300 1.24 lukem hp = hesiod_resolve(_dns_state.context, shellname, "shells");
301 1.24 lukem if (hp == NULL) {
302 1.24 lukem if (errno == ENOENT)
303 1.24 lukem rv = NS_NOTFOUND;
304 1.24 lukem else
305 1.24 lukem rv = NS_UNAVAIL;
306 1.24 lukem } else {
307 1.24 lukem if ((ep = strchr(hp[0], '\n')) != NULL)
308 1.24 lukem *ep = '\0'; /* clear trailing \n */
309 1.24 lukem /* only use first result */
310 1.24 lukem strlcpy(curshell, hp[0], sizeof(curshell));
311 1.24 lukem *retval = curshell;
312 1.24 lukem rv = NS_SUCCESS;
313 1.13 lukem }
314 1.24 lukem
315 1.24 lukem if (hp)
316 1.24 lukem hesiod_free_list(_dns_state.context, hp);
317 1.24 lukem if (rv != NS_SUCCESS)
318 1.24 lukem _dns_state.num = -1; /* any failure halts search */
319 1.24 lukem return rv;
320 1.13 lukem }
321 1.24 lukem
322 1.13 lukem #endif /* HESIOD */
323 1.13 lukem
324 1.24 lukem
325 1.13 lukem #ifdef YP
326 1.24 lukem /*
327 1.24 lukem * nis methods
328 1.24 lukem */
329 1.24 lukem /* state shared between nis methods */
330 1.24 lukem struct nis_state {
331 1.24 lukem char *domain; /* NIS domain */
332 1.24 lukem int done; /* non-zero if search exhausted */
333 1.24 lukem char *current; /* current first/next match */
334 1.24 lukem int currentlen; /* length of _nis_current */
335 1.24 lukem };
336 1.24 lukem
337 1.24 lukem static struct nis_state _nis_state;
338 1.13 lukem
339 1.13 lukem static int
340 1.24 lukem _nis_start(struct nis_state *state)
341 1.24 lukem {
342 1.13 lukem
343 1.24 lukem _DIAGASSERT(state != NULL);
344 1.24 lukem
345 1.24 lukem state->done = 0;
346 1.24 lukem if (state->current) {
347 1.24 lukem free(state->current);
348 1.24 lukem state->current = NULL;
349 1.24 lukem }
350 1.24 lukem if (state->domain == NULL) { /* setup NIS */
351 1.24 lukem switch (yp_get_default_domain(&state->domain)) {
352 1.13 lukem case 0:
353 1.13 lukem break;
354 1.13 lukem case YPERR_RESRC:
355 1.24 lukem return NS_TRYAGAIN;
356 1.13 lukem default:
357 1.24 lukem return NS_UNAVAIL;
358 1.13 lukem }
359 1.13 lukem }
360 1.24 lukem return NS_SUCCESS;
361 1.24 lukem }
362 1.24 lukem
363 1.24 lukem static int
364 1.24 lukem _nis_end(struct nis_state *state)
365 1.24 lukem {
366 1.24 lukem
367 1.24 lukem _DIAGASSERT(state != NULL);
368 1.24 lukem
369 1.24 lukem if (state->domain)
370 1.24 lukem state->domain = NULL;
371 1.24 lukem state->done = 0;
372 1.24 lukem if (state->current)
373 1.24 lukem free(state->current);
374 1.24 lukem state->current = NULL;
375 1.24 lukem return NS_SUCCESS;
376 1.24 lukem }
377 1.24 lukem
378 1.24 lukem /*ARGSUSED*/
379 1.24 lukem static int
380 1.24 lukem _nis_setusershell(void *nsrv, void *nscb, va_list ap)
381 1.24 lukem {
382 1.24 lukem
383 1.24 lukem return _nis_start(&_nis_state);
384 1.24 lukem }
385 1.24 lukem
386 1.24 lukem /*ARGSUSED*/
387 1.24 lukem static int
388 1.24 lukem _nis_endusershell(void *nsrv, void *nscb, va_list ap)
389 1.24 lukem {
390 1.24 lukem
391 1.24 lukem return _nis_end(&_nis_state);
392 1.24 lukem }
393 1.24 lukem
394 1.24 lukem /*ARGSUSED*/
395 1.24 lukem static int
396 1.24 lukem _nis_getusershell(void *nsrv, void *nscb, va_list ap)
397 1.24 lukem {
398 1.24 lukem char **retval = va_arg(ap, char **);
399 1.24 lukem
400 1.24 lukem char *key, *data;
401 1.24 lukem int keylen, datalen, rv, nisr;
402 1.24 lukem
403 1.24 lukem _DIAGASSERT(retval != NULL);
404 1.24 lukem
405 1.24 lukem *retval = NULL;
406 1.24 lukem
407 1.24 lukem if (_nis_state.done) /* exhausted search */
408 1.24 lukem return NS_NOTFOUND;
409 1.24 lukem if (_nis_state.domain == NULL) {
410 1.24 lukem /* only start if NIS not setup */
411 1.24 lukem rv = _nis_start(&_nis_state);
412 1.24 lukem if (rv != NS_SUCCESS)
413 1.24 lukem return rv;
414 1.24 lukem }
415 1.13 lukem
416 1.24 lukem key = NULL;
417 1.24 lukem data = NULL;
418 1.24 lukem rv = NS_NOTFOUND;
419 1.24 lukem
420 1.24 lukem if (_nis_state.current) { /* already searching */
421 1.24 lukem nisr = yp_next(_nis_state.domain, "shells",
422 1.24 lukem _nis_state.current, _nis_state.currentlen,
423 1.24 lukem &key, &keylen, &data, &datalen);
424 1.24 lukem free(_nis_state.current);
425 1.24 lukem _nis_state.current = NULL;
426 1.24 lukem switch (nisr) {
427 1.24 lukem case 0:
428 1.24 lukem _nis_state.current = key;
429 1.24 lukem _nis_state.currentlen = keylen;
430 1.24 lukem key = NULL;
431 1.24 lukem break;
432 1.24 lukem case YPERR_NOMORE:
433 1.24 lukem rv = NS_NOTFOUND;
434 1.24 lukem goto nisent_out;
435 1.24 lukem default:
436 1.24 lukem rv = NS_UNAVAIL;
437 1.24 lukem goto nisent_out;
438 1.24 lukem }
439 1.24 lukem } else { /* new search */
440 1.24 lukem if (yp_first(_nis_state.domain, "shells",
441 1.24 lukem &_nis_state.current, &_nis_state.currentlen,
442 1.24 lukem &data, &datalen)) {
443 1.24 lukem rv = NS_UNAVAIL;
444 1.24 lukem goto nisent_out;
445 1.13 lukem }
446 1.13 lukem }
447 1.24 lukem
448 1.24 lukem data[datalen] = '\0'; /* clear trailing \n */
449 1.24 lukem strlcpy(curshell, data, sizeof(curshell));
450 1.24 lukem *retval = curshell;
451 1.24 lukem rv = NS_SUCCESS;
452 1.24 lukem
453 1.24 lukem nisent_out:
454 1.24 lukem if (key)
455 1.24 lukem free(key);
456 1.24 lukem if (data)
457 1.24 lukem free(data);
458 1.24 lukem if (rv != NS_SUCCESS) /* any failure halts search */
459 1.24 lukem _nis_state.done = 1;
460 1.24 lukem return rv;
461 1.13 lukem }
462 1.24 lukem
463 1.13 lukem #endif /* YP */
464 1.13 lukem
465 1.24 lukem
466 1.24 lukem /*
467 1.24 lukem * public functions
468 1.24 lukem */
469 1.24 lukem
470 1.24 lukem void
471 1.24 lukem endusershell(void)
472 1.24 lukem {
473 1.24 lukem static const ns_dtab dtab[] = {
474 1.24 lukem NS_FILES_CB(_files_endusershell, NULL)
475 1.24 lukem NS_DNS_CB(_dns_endusershell, NULL)
476 1.24 lukem NS_NIS_CB(_nis_endusershell, NULL)
477 1.26 christos NS_NULL_CB
478 1.24 lukem };
479 1.24 lukem
480 1.24 lukem mutex_lock(&__shellmutex);
481 1.24 lukem
482 1.24 lukem curokshell = okshells; /* reset okshells fallback state */
483 1.24 lukem shellsfound = 0;
484 1.24 lukem
485 1.24 lukem /* force all endusershell() methods */
486 1.24 lukem (void) nsdispatch(NULL, dtab, NSDB_SHELLS, "endusershell",
487 1.24 lukem __nsdefaultfiles_forceall);
488 1.24 lukem mutex_unlock(&__shellmutex);
489 1.24 lukem }
490 1.24 lukem
491 1.24 lukem __aconst char *
492 1.24 lukem getusershell(void)
493 1.13 lukem {
494 1.24 lukem int rv;
495 1.24 lukem __aconst char *retval;
496 1.24 lukem
497 1.15 lukem static const ns_dtab dtab[] = {
498 1.24 lukem NS_FILES_CB(_files_getusershell, NULL)
499 1.24 lukem NS_DNS_CB(_dns_getusershell, NULL)
500 1.24 lukem NS_NIS_CB(_nis_getusershell, NULL)
501 1.26 christos NS_NULL_CB
502 1.13 lukem };
503 1.24 lukem
504 1.24 lukem mutex_lock(&__shellmutex);
505 1.24 lukem
506 1.24 lukem retval = NULL;
507 1.24 lukem do {
508 1.24 lukem rv = nsdispatch(NULL, dtab, NSDB_SHELLS, "getusershell",
509 1.24 lukem __nsdefaultsrc, &retval);
510 1.24 lukem /* loop until failure or non-blank result */
511 1.24 lukem } while (rv == NS_SUCCESS && retval[0] == '\0');
512 1.24 lukem
513 1.24 lukem if (rv == NS_SUCCESS) {
514 1.24 lukem shellsfound++;
515 1.24 lukem } else if (shellsfound == 0) { /* no shells; fall back to okshells */
516 1.24 lukem if (curokshell != NULL) {
517 1.25 christos retval = __UNCONST(*curokshell);
518 1.24 lukem curokshell++;
519 1.24 lukem rv = NS_SUCCESS;
520 1.24 lukem }
521 1.13 lukem }
522 1.13 lukem
523 1.24 lukem mutex_unlock(&__shellmutex);
524 1.24 lukem return (rv == NS_SUCCESS) ? retval : NULL;
525 1.24 lukem }
526 1.24 lukem
527 1.24 lukem void
528 1.24 lukem setusershell(void)
529 1.24 lukem {
530 1.24 lukem static const ns_dtab dtab[] = {
531 1.24 lukem NS_FILES_CB(_files_setusershell, NULL)
532 1.24 lukem NS_DNS_CB(_dns_setusershell, NULL)
533 1.24 lukem NS_NIS_CB(_nis_setusershell, NULL)
534 1.26 christos NS_NULL_CB
535 1.24 lukem };
536 1.24 lukem
537 1.24 lukem mutex_lock(&__shellmutex);
538 1.24 lukem
539 1.24 lukem curokshell = okshells; /* reset okshells fallback state */
540 1.24 lukem shellsfound = 0;
541 1.24 lukem
542 1.24 lukem /* force all setusershell() methods */
543 1.24 lukem (void) nsdispatch(NULL, dtab, NSDB_SHELLS, "setusershell",
544 1.24 lukem __nsdefaultfiles_forceall);
545 1.24 lukem mutex_unlock(&__shellmutex);
546 1.1 cgd }
547