xref: /netbsd-src/lib/libc/gen/getusershell.c (revision c5e820cae412164fcbee52f470436200af5358ea)
1*c5e820caSchristos /*	$NetBSD: getusershell.c,v 1.29 2012/03/13 21:13:36 christos Exp $	*/
2a3045235Slukem 
3a3045235Slukem /*-
4a3045235Slukem  * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
5a3045235Slukem  * All rights reserved.
6a3045235Slukem  *
7a3045235Slukem  * This code is derived from software contributed to The NetBSD Foundation
8a3045235Slukem  * by Luke Mewburn.
9a3045235Slukem  *
10a3045235Slukem  * Redistribution and use in source and binary forms, with or without
11a3045235Slukem  * modification, are permitted provided that the following conditions
12a3045235Slukem  * are met:
13a3045235Slukem  * 1. Redistributions of source code must retain the above copyright
14a3045235Slukem  *    notice, this list of conditions and the following disclaimer.
15a3045235Slukem  * 2. Redistributions in binary form must reproduce the above copyright
16a3045235Slukem  *    notice, this list of conditions and the following disclaimer in the
17a3045235Slukem  *    documentation and/or other materials provided with the distribution.
18a3045235Slukem  *
19a3045235Slukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a3045235Slukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a3045235Slukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a3045235Slukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a3045235Slukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a3045235Slukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a3045235Slukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a3045235Slukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a3045235Slukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a3045235Slukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a3045235Slukem  * POSSIBILITY OF SUCH DAMAGE.
30a3045235Slukem  */
31b585e843Scgd 
3261f28255Scgd /*
33b585e843Scgd  * Copyright (c) 1985, 1993
34b585e843Scgd  *	The Regents of the University of California.  All rights reserved.
3561f28255Scgd  *
3661f28255Scgd  * Redistribution and use in source and binary forms, with or without
3761f28255Scgd  * modification, are permitted provided that the following conditions
3861f28255Scgd  * are met:
3961f28255Scgd  * 1. Redistributions of source code must retain the above copyright
4061f28255Scgd  *    notice, this list of conditions and the following disclaimer.
4161f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
4261f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
4361f28255Scgd  *    documentation and/or other materials provided with the distribution.
44eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
4561f28255Scgd  *    may be used to endorse or promote products derived from this software
4661f28255Scgd  *    without specific prior written permission.
4761f28255Scgd  *
4861f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4961f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5061f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5161f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5261f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5361f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5461f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5561f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5661f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5761f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5861f28255Scgd  * SUCH DAMAGE.
5961f28255Scgd  */
6061f28255Scgd 
6126cc2d4fSchristos #include <sys/cdefs.h>
6261f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
63b585e843Scgd #if 0
64b585e843Scgd static char sccsid[] = "@(#)getusershell.c	8.1 (Berkeley) 6/4/93";
65b585e843Scgd #else
66*c5e820caSchristos __RCSID("$NetBSD: getusershell.c,v 1.29 2012/03/13 21:13:36 christos Exp $");
67b585e843Scgd #endif
6861f28255Scgd #endif /* LIBC_SCCS and not lint */
6961f28255Scgd 
7043fa6fe3Sjtc #include "namespace.h"
71a3045235Slukem #include "reentrant.h"
72a3045235Slukem 
7361f28255Scgd #include <sys/param.h>
7461f28255Scgd #include <sys/file.h>
75147dea10Slukem 
76a3045235Slukem #include <assert.h>
77b585e843Scgd #include <ctype.h>
78147dea10Slukem #include <errno.h>
790eb8645eSlukem #include <nsswitch.h>
80b585e843Scgd #include <paths.h>
81077b67efSwiz #include <stdarg.h>
82147dea10Slukem #include <stdio.h>
83147dea10Slukem #include <stdlib.h>
840eb8645eSlukem #include <string.h>
85147dea10Slukem #include <unistd.h>
86147dea10Slukem 
870eb8645eSlukem #ifdef HESIOD
880eb8645eSlukem #include <hesiod.h>
890eb8645eSlukem #endif
900eb8645eSlukem #ifdef YP
910eb8645eSlukem #include <rpc/rpc.h>
920eb8645eSlukem #include <rpcsvc/ypclnt.h>
930eb8645eSlukem #include <rpcsvc/yp_prot.h>
940eb8645eSlukem #endif
9561f28255Scgd 
9643fa6fe3Sjtc #ifdef __weak_alias
9760549036Smycroft __weak_alias(endusershell,_endusershell)
9860549036Smycroft __weak_alias(getusershell,_getusershell)
9960549036Smycroft __weak_alias(setusershell,_setusershell)
10043fa6fe3Sjtc #endif
10143fa6fe3Sjtc 
10261f28255Scgd /*
103a3045235Slukem  * Local shells should NOT be added here.
104a3045235Slukem  * They should be added in /etc/shells.
10561f28255Scgd  */
1064024f736Smycroft static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
1070eb8645eSlukem 
108a3045235Slukem #ifdef _REENTRANT
109a3045235Slukem static mutex_t __shellmutex = MUTEX_INITIALIZER;
110a3045235Slukem #endif
111a3045235Slukem 
112a3045235Slukem static char		  curshell[MAXPATHLEN + 2];
113a3045235Slukem 
114a3045235Slukem static const char *const *curokshell = okshells;
115a3045235Slukem static int		  shellsfound = 0;
11661f28255Scgd 
11761f28255Scgd 		/*
118a3045235Slukem 		 *	files methods
11961f28255Scgd 		 */
12061f28255Scgd 
121a3045235Slukem 	/* state shared between files methods */
122a3045235Slukem struct files_state {
123a3045235Slukem 	FILE	*fp;
124a3045235Slukem };
12561f28255Scgd 
126a3045235Slukem static struct files_state _files_state;
12761f28255Scgd 
128a3045235Slukem 
129a3045235Slukem static int
_files_start(struct files_state * state)130a3045235Slukem _files_start(struct files_state *state)
13161f28255Scgd {
13261f28255Scgd 
133a3045235Slukem 	_DIAGASSERT(state != NULL);
134a3045235Slukem 
135a3045235Slukem 	if (state->fp == NULL) {
1369292cfb2Schristos 		state->fp = fopen(_PATH_SHELLS, "re");
137a3045235Slukem 		if (state->fp == NULL)
138a3045235Slukem 			return NS_UNAVAIL;
139a3045235Slukem 	} else {
140a3045235Slukem 		rewind(state->fp);
141a3045235Slukem 	}
142a3045235Slukem 	return NS_SUCCESS;
14361f28255Scgd }
14461f28255Scgd 
145a3045235Slukem static int
_files_end(struct files_state * state)146a3045235Slukem _files_end(struct files_state *state)
147a3045235Slukem {
14861f28255Scgd 
149a3045235Slukem 	_DIAGASSERT(state != NULL);
150a3045235Slukem 
151a3045235Slukem 	if (state->fp) {
152a3045235Slukem 		(void) fclose(state->fp);
153a3045235Slukem 		state->fp = NULL;
154a3045235Slukem 	}
155a3045235Slukem 	return NS_SUCCESS;
156a3045235Slukem }
1570eb8645eSlukem 
15878427083Schristos /*ARGSUSED*/
1590eb8645eSlukem static int
_files_setusershell(void * nsrv,void * nscb,va_list ap)160a3045235Slukem _files_setusershell(void *nsrv, void *nscb, va_list ap)
1610eb8645eSlukem {
162a3045235Slukem 
163a3045235Slukem 	return _files_start(&_files_state);
164a3045235Slukem }
165a3045235Slukem 
166a3045235Slukem /*ARGSUSED*/
167a3045235Slukem static int
_files_endusershell(void * nsrv,void * nscb,va_list ap)168a3045235Slukem _files_endusershell(void *nsrv, void *nscb, va_list ap)
169a3045235Slukem {
170a3045235Slukem 
171a3045235Slukem 	return _files_end(&_files_state);
172a3045235Slukem }
173a3045235Slukem 
174a3045235Slukem /*ARGSUSED*/
175a3045235Slukem static int
_files_getusershell(void * nsrv,void * nscb,va_list ap)176a3045235Slukem _files_getusershell(void *nsrv, void *nscb, va_list ap)
177a3045235Slukem {
178a3045235Slukem 	char	**retval = va_arg(ap, char **);
179a3045235Slukem 
1800eb8645eSlukem 	char	*sp, *cp;
181a3045235Slukem 	int	 rv;
1820eb8645eSlukem 
183a3045235Slukem 	_DIAGASSERT(retval != NULL);
1840eb8645eSlukem 
185a3045235Slukem 	*retval = NULL;
186a3045235Slukem 	if (_files_state.fp == NULL) {	/* only start if file not open yet */
187a3045235Slukem 		rv = _files_start(&_files_state);
188a3045235Slukem 		if (rv != NS_SUCCESS)
189a3045235Slukem 			return rv;
190a3045235Slukem 	}
1910eb8645eSlukem 
192*c5e820caSchristos 	while (fgets(curshell, (int)sizeof(curshell) - 1, _files_state.fp)
193*c5e820caSchristos 	    != NULL) {
194a3045235Slukem 		sp = cp = curshell;
19561f28255Scgd 		while (*cp != '#' && *cp != '/' && *cp != '\0')
19661f28255Scgd 			cp++;
19761f28255Scgd 		if (*cp == '#' || *cp == '\0')
19861f28255Scgd 			continue;
1990eb8645eSlukem 		sp = cp;
2007daefc5aSitohy 		while (!isspace((unsigned char) *cp) && *cp != '#'
2017daefc5aSitohy 		    && *cp != '\0')
20261f28255Scgd 			cp++;
20361f28255Scgd 		*cp++ = '\0';
204a3045235Slukem 		*retval = sp;
205a3045235Slukem 		return NS_SUCCESS;
20661f28255Scgd 	}
207a3045235Slukem 
208a3045235Slukem 	return NS_NOTFOUND;
2090eb8645eSlukem }
2100eb8645eSlukem 
211a3045235Slukem 
2120eb8645eSlukem #ifdef HESIOD
213a3045235Slukem 		/*
214a3045235Slukem 		 *	dns methods
215a3045235Slukem 		 */
216a3045235Slukem 
217a3045235Slukem 	/* state shared between dns methods */
218a3045235Slukem struct dns_state {
219a3045235Slukem 	void	*context;		/* Hesiod context */
220a3045235Slukem 	int	 num;			/* shell index, -1 if no more */
221a3045235Slukem };
222a3045235Slukem 
223a3045235Slukem static struct dns_state		_dns_state;
224a3045235Slukem 
225a3045235Slukem static int
_dns_start(struct dns_state * state)226a3045235Slukem _dns_start(struct dns_state *state)
227a3045235Slukem {
228a3045235Slukem 
229a3045235Slukem 	_DIAGASSERT(state != NULL);
230a3045235Slukem 
231a3045235Slukem 	state->num = 0;
232a3045235Slukem 	if (state->context == NULL) {			/* setup Hesiod */
233a3045235Slukem 		if (hesiod_init(&state->context) == -1)
234a3045235Slukem 			return NS_UNAVAIL;
235a3045235Slukem 	}
236a3045235Slukem 
237a3045235Slukem 	return NS_SUCCESS;
238a3045235Slukem }
239a3045235Slukem 
240a3045235Slukem static int
_dns_end(struct dns_state * state)241a3045235Slukem _dns_end(struct dns_state *state)
242a3045235Slukem {
243a3045235Slukem 
244a3045235Slukem 	_DIAGASSERT(state != NULL);
245a3045235Slukem 
246a3045235Slukem 	state->num = 0;
247a3045235Slukem 	if (state->context) {
248a3045235Slukem 		hesiod_end(state->context);
249a3045235Slukem 		state->context = NULL;
250a3045235Slukem 	}
251a3045235Slukem 	return NS_SUCCESS;
252a3045235Slukem }
2530eb8645eSlukem 
25478427083Schristos /*ARGSUSED*/
2550eb8645eSlukem static int
_dns_setusershell(void * nsrv,void * nscb,va_list ap)256a3045235Slukem _dns_setusershell(void *nsrv, void *nscb, va_list ap)
2570eb8645eSlukem {
2580eb8645eSlukem 
259a3045235Slukem 	return _dns_start(&_dns_state);
260a3045235Slukem }
26156a2dec5Slukem 
262a3045235Slukem /*ARGSUSED*/
263a3045235Slukem static int
_dns_endusershell(void * nsrv,void * nscb,va_list ap)264a3045235Slukem _dns_endusershell(void *nsrv, void *nscb, va_list ap)
265a3045235Slukem {
2660eb8645eSlukem 
267a3045235Slukem 	return _dns_end(&_dns_state);
268a3045235Slukem }
269a3045235Slukem 
270a3045235Slukem /*ARGSUSED*/
271a3045235Slukem static int
_dns_getusershell(void * nsrv,void * nscb,va_list ap)272a3045235Slukem _dns_getusershell(void *nsrv, void *nscb, va_list ap)
273a3045235Slukem {
274a3045235Slukem 	char	**retval = va_arg(ap, char **);
275a3045235Slukem 
276a3045235Slukem 	char	  shellname[] = "shells-NNNNNNNNNN";
277a3045235Slukem 	char	**hp, *ep;
278a3045235Slukem 	int	  rv;
279a3045235Slukem 
280a3045235Slukem 	_DIAGASSERT(retval != NULL);
281a3045235Slukem 
282a3045235Slukem 	*retval = NULL;
283a3045235Slukem 
284a3045235Slukem 	if (_dns_state.num == -1)			/* exhausted search */
285a3045235Slukem 		return NS_NOTFOUND;
286a3045235Slukem 
287a3045235Slukem 	if (_dns_state.context == NULL) {
288a3045235Slukem 			/* only start if Hesiod not setup */
289a3045235Slukem 		rv = _dns_start(&_dns_state);
290a3045235Slukem 		if (rv != NS_SUCCESS)
291a3045235Slukem 			return rv;
292a3045235Slukem 	}
293a3045235Slukem 
294a3045235Slukem 	hp = NULL;
295a3045235Slukem 	rv = NS_NOTFOUND;
296a3045235Slukem 
297a3045235Slukem 							/* find shells-NNN */
298a3045235Slukem 	snprintf(shellname, sizeof(shellname), "shells-%d", _dns_state.num);
299a3045235Slukem 	_dns_state.num++;
300a3045235Slukem 
301a3045235Slukem 	hp = hesiod_resolve(_dns_state.context, shellname, "shells");
3020eb8645eSlukem 	if (hp == NULL) {
303a3045235Slukem 		if (errno == ENOENT)
304a3045235Slukem 			rv = NS_NOTFOUND;
305147dea10Slukem 		else
306a3045235Slukem 			rv = NS_UNAVAIL;
3070eb8645eSlukem 	} else {
308a3045235Slukem 		if ((ep = strchr(hp[0], '\n')) != NULL)
309a3045235Slukem 			*ep = '\0';			/* clear trailing \n */
310a3045235Slukem 						/* only use first result */
311a3045235Slukem 		strlcpy(curshell, hp[0], sizeof(curshell));
312a3045235Slukem 		*retval = curshell;
313a3045235Slukem 		rv = NS_SUCCESS;
314a3045235Slukem 	}
31556a2dec5Slukem 
316a3045235Slukem 	if (hp)
317a3045235Slukem 		hesiod_free_list(_dns_state.context, hp);
318a3045235Slukem 	if (rv != NS_SUCCESS)
319a3045235Slukem 		_dns_state.num = -1;		/* any failure halts search */
320a3045235Slukem 	return rv;
32156a2dec5Slukem }
322a3045235Slukem 
3230eb8645eSlukem #endif /* HESIOD */
3240eb8645eSlukem 
325a3045235Slukem 
3260eb8645eSlukem #ifdef YP
327a3045235Slukem 		/*
328a3045235Slukem 		 *	nis methods
329a3045235Slukem 		 */
330a3045235Slukem 	/* state shared between nis methods */
331a3045235Slukem struct nis_state {
332a3045235Slukem 	char		*domain;	/* NIS domain */
333a3045235Slukem 	int		 done;		/* non-zero if search exhausted */
334a3045235Slukem 	char		*current;	/* current first/next match */
335a3045235Slukem 	int		 currentlen;	/* length of _nis_current */
336a3045235Slukem };
3370eb8645eSlukem 
338a3045235Slukem static struct nis_state		_nis_state;
339a3045235Slukem 
3400eb8645eSlukem static int
_nis_start(struct nis_state * state)341a3045235Slukem _nis_start(struct nis_state *state)
3420eb8645eSlukem {
3430eb8645eSlukem 
344a3045235Slukem 	_DIAGASSERT(state != NULL);
3450eb8645eSlukem 
346a3045235Slukem 	state->done = 0;
347a3045235Slukem 	if (state->current) {
348a3045235Slukem 		free(state->current);
349a3045235Slukem 		state->current = NULL;
350a3045235Slukem 	}
351a3045235Slukem 	if (state->domain == NULL) {			/* setup NIS */
352a3045235Slukem 		switch (yp_get_default_domain(&state->domain)) {
3530eb8645eSlukem 		case 0:
3540eb8645eSlukem 			break;
3550eb8645eSlukem 		case YPERR_RESRC:
356a3045235Slukem 			return NS_TRYAGAIN;
3570eb8645eSlukem 		default:
358a3045235Slukem 			return NS_UNAVAIL;
3590eb8645eSlukem 		}
3600eb8645eSlukem 	}
361a3045235Slukem 	return NS_SUCCESS;
362a3045235Slukem }
3630eb8645eSlukem 
364a3045235Slukem static int
_nis_end(struct nis_state * state)365a3045235Slukem _nis_end(struct nis_state *state)
366a3045235Slukem {
367a3045235Slukem 
368a3045235Slukem 	_DIAGASSERT(state != NULL);
369a3045235Slukem 
370a3045235Slukem 	if (state->domain)
371a3045235Slukem 		state->domain = NULL;
372a3045235Slukem 	state->done = 0;
373a3045235Slukem 	if (state->current)
374a3045235Slukem 		free(state->current);
375a3045235Slukem 	state->current = NULL;
376a3045235Slukem 	return NS_SUCCESS;
377a3045235Slukem }
378a3045235Slukem 
379a3045235Slukem /*ARGSUSED*/
380a3045235Slukem static int
_nis_setusershell(void * nsrv,void * nscb,va_list ap)381a3045235Slukem _nis_setusershell(void *nsrv, void *nscb, va_list ap)
382a3045235Slukem {
383a3045235Slukem 
384a3045235Slukem 	return _nis_start(&_nis_state);
385a3045235Slukem }
386a3045235Slukem 
387a3045235Slukem /*ARGSUSED*/
388a3045235Slukem static int
_nis_endusershell(void * nsrv,void * nscb,va_list ap)389a3045235Slukem _nis_endusershell(void *nsrv, void *nscb, va_list ap)
390a3045235Slukem {
391a3045235Slukem 
392a3045235Slukem 	return _nis_end(&_nis_state);
393a3045235Slukem }
394a3045235Slukem 
395a3045235Slukem /*ARGSUSED*/
396a3045235Slukem static int
_nis_getusershell(void * nsrv,void * nscb,va_list ap)397a3045235Slukem _nis_getusershell(void *nsrv, void *nscb, va_list ap)
398a3045235Slukem {
399a3045235Slukem 	char	**retval = va_arg(ap, char **);
400a3045235Slukem 
4010eb8645eSlukem 	char	*key, *data;
402a3045235Slukem 	int	keylen, datalen, rv, nisr;
4030eb8645eSlukem 
404a3045235Slukem 	_DIAGASSERT(retval != NULL);
405a3045235Slukem 
406a3045235Slukem 	*retval = NULL;
407a3045235Slukem 
408a3045235Slukem 	if (_nis_state.done)				/* exhausted search */
409a3045235Slukem 		return NS_NOTFOUND;
410a3045235Slukem 	if (_nis_state.domain == NULL) {
411a3045235Slukem 					/* only start if NIS not setup */
412a3045235Slukem 		rv = _nis_start(&_nis_state);
413a3045235Slukem 		if (rv != NS_SUCCESS)
414a3045235Slukem 			return rv;
415a3045235Slukem 	}
416a3045235Slukem 
417a3045235Slukem 	key = NULL;
418a3045235Slukem 	data = NULL;
419a3045235Slukem 	rv = NS_NOTFOUND;
420a3045235Slukem 
421a3045235Slukem 	if (_nis_state.current) {			/* already searching */
422a3045235Slukem 		nisr = yp_next(_nis_state.domain, "shells",
423a3045235Slukem 		    _nis_state.current, _nis_state.currentlen,
4240eb8645eSlukem 		    &key, &keylen, &data, &datalen);
425a3045235Slukem 		free(_nis_state.current);
426a3045235Slukem 		_nis_state.current = NULL;
427a3045235Slukem 		switch (nisr) {
4280eb8645eSlukem 		case 0:
429a3045235Slukem 			_nis_state.current = key;
430a3045235Slukem 			_nis_state.currentlen = keylen;
431a3045235Slukem 			key = NULL;
4320eb8645eSlukem 			break;
4330eb8645eSlukem 		case YPERR_NOMORE:
434a3045235Slukem 			rv = NS_NOTFOUND;
435a3045235Slukem 			goto nisent_out;
4360eb8645eSlukem 		default:
437a3045235Slukem 			rv = NS_UNAVAIL;
438a3045235Slukem 			goto nisent_out;
4390eb8645eSlukem 		}
440a3045235Slukem 	} else {					/* new search */
441a3045235Slukem 		if (yp_first(_nis_state.domain, "shells",
442a3045235Slukem 		    &_nis_state.current, &_nis_state.currentlen,
443a3045235Slukem 		    &data, &datalen)) {
444a3045235Slukem 			rv = NS_UNAVAIL;
445a3045235Slukem 			goto nisent_out;
4460eb8645eSlukem 		}
4470eb8645eSlukem 	}
448a3045235Slukem 
4490eb8645eSlukem 	data[datalen] = '\0';				/* clear trailing \n */
450a3045235Slukem 	strlcpy(curshell, data, sizeof(curshell));
451a3045235Slukem 	*retval = curshell;
452a3045235Slukem 	rv = NS_SUCCESS;
453a3045235Slukem 
454a3045235Slukem  nisent_out:
455a3045235Slukem 	if (key)
456a3045235Slukem 		free(key);
457a3045235Slukem 	if (data)
458a3045235Slukem 		free(data);
459a3045235Slukem 	if (rv != NS_SUCCESS)			/* any failure halts search */
460a3045235Slukem 		_nis_state.done = 1;
461a3045235Slukem 	return rv;
4620eb8645eSlukem }
463a3045235Slukem 
4640eb8645eSlukem #endif /* YP */
4650eb8645eSlukem 
466a3045235Slukem 
467a3045235Slukem 		/*
468a3045235Slukem 		 *	public functions
469a3045235Slukem 		 */
470a3045235Slukem 
471a3045235Slukem void
endusershell(void)472a3045235Slukem endusershell(void)
4730eb8645eSlukem {
474b247a8ecSlukem 	static const ns_dtab dtab[] = {
475a3045235Slukem 		NS_FILES_CB(_files_endusershell, NULL)
476a3045235Slukem 		NS_DNS_CB(_dns_endusershell, NULL)
477a3045235Slukem 		NS_NIS_CB(_nis_endusershell, NULL)
478ce2c90c7Schristos 		NS_NULL_CB
4790eb8645eSlukem 	};
4800eb8645eSlukem 
481a3045235Slukem 	mutex_lock(&__shellmutex);
482a3045235Slukem 
483a3045235Slukem 	curokshell = okshells;		/* reset okshells fallback state */
484a3045235Slukem 	shellsfound = 0;
485a3045235Slukem 
486a3045235Slukem 					/* force all endusershell() methods */
487a3045235Slukem 	(void) nsdispatch(NULL, dtab, NSDB_SHELLS, "endusershell",
488a3045235Slukem 	    __nsdefaultfiles_forceall);
489a3045235Slukem 	mutex_unlock(&__shellmutex);
4900eb8645eSlukem }
4910eb8645eSlukem 
492a3045235Slukem __aconst char *
getusershell(void)493a3045235Slukem getusershell(void)
494a3045235Slukem {
495a3045235Slukem 	int		 rv;
496a3045235Slukem 	__aconst char	*retval;
497a3045235Slukem 
498a3045235Slukem 	static const ns_dtab dtab[] = {
499a3045235Slukem 		NS_FILES_CB(_files_getusershell, NULL)
500a3045235Slukem 		NS_DNS_CB(_dns_getusershell, NULL)
501a3045235Slukem 		NS_NIS_CB(_nis_getusershell, NULL)
502ce2c90c7Schristos 		NS_NULL_CB
503a3045235Slukem 	};
504a3045235Slukem 
505a3045235Slukem 	mutex_lock(&__shellmutex);
506a3045235Slukem 
507a3045235Slukem 	retval = NULL;
508a3045235Slukem 	do {
509a3045235Slukem 		rv = nsdispatch(NULL, dtab, NSDB_SHELLS, "getusershell",
510a3045235Slukem 		    __nsdefaultsrc, &retval);
511a3045235Slukem 				/* loop until failure or non-blank result */
512a3045235Slukem 	} while (rv == NS_SUCCESS && retval[0] == '\0');
513a3045235Slukem 
514a3045235Slukem 	if (rv == NS_SUCCESS) {
515a3045235Slukem 		shellsfound++;
516a3045235Slukem 	} else if (shellsfound == 0) {	/* no shells; fall back to okshells */
517a3045235Slukem 		if (curokshell != NULL) {
51803256c6eSchristos 			retval = __UNCONST(*curokshell);
519a3045235Slukem 			curokshell++;
520a3045235Slukem 			rv = NS_SUCCESS;
521a3045235Slukem 		}
522a3045235Slukem 	}
523a3045235Slukem 
524a3045235Slukem 	mutex_unlock(&__shellmutex);
525a3045235Slukem 	return (rv == NS_SUCCESS) ? retval : NULL;
526a3045235Slukem }
527a3045235Slukem 
528a3045235Slukem void
setusershell(void)529a3045235Slukem setusershell(void)
530a3045235Slukem {
531a3045235Slukem 	static const ns_dtab dtab[] = {
532a3045235Slukem 		NS_FILES_CB(_files_setusershell, NULL)
533a3045235Slukem 		NS_DNS_CB(_dns_setusershell, NULL)
534a3045235Slukem 		NS_NIS_CB(_nis_setusershell, NULL)
535ce2c90c7Schristos 		NS_NULL_CB
536a3045235Slukem 	};
537a3045235Slukem 
538a3045235Slukem 	mutex_lock(&__shellmutex);
539a3045235Slukem 
540a3045235Slukem 	curokshell = okshells;		/* reset okshells fallback state */
541a3045235Slukem 	shellsfound = 0;
542a3045235Slukem 
543a3045235Slukem 					/* force all setusershell() methods */
544a3045235Slukem 	(void) nsdispatch(NULL, dtab, NSDB_SHELLS, "setusershell",
545a3045235Slukem 	    __nsdefaultfiles_forceall);
546a3045235Slukem 	mutex_unlock(&__shellmutex);
54761f28255Scgd }
548