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