getusershell.c revision 112407
1/*
2 * Copyright (c) 1985, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)getusershell.c	8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37/*	$NetBSD: getusershell.c,v 1.17 1999/01/25 01:09:34 lukem Exp $	*/
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/lib/libc/gen/getusershell.c 112407 2003-03-19 14:17:24Z robert $");
40
41#include <sys/param.h>
42#include <sys/file.h>
43
44#include <ctype.h>
45#include <errno.h>
46#include <nsswitch.h>
47#include <paths.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <stringlist.h>
52#include <unistd.h>
53
54#ifdef HESIOD
55#include <hesiod.h>
56#endif
57#ifdef YP
58#include <rpc/rpc.h>
59#include <rpcsvc/ypclnt.h>
60#include <rpcsvc/yp_prot.h>
61#endif
62
63/*
64 * Local shells should NOT be added here.  They should be added in
65 * /etc/shells.
66 */
67
68static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
69static const char *const *curshell;
70static StringList	 *sl;
71
72static const char *const *initshells(void);
73
74/*
75 * Get a list of shells from "shells" nsswitch database
76 */
77char *
78getusershell(void)
79{
80	char *ret;
81
82	if (curshell == NULL)
83		curshell = initshells();
84	/*LINTED*/
85	ret = (char *)*curshell;
86	if (ret != NULL)
87		curshell++;
88	return (ret);
89}
90
91void
92endusershell(void)
93{
94	if (sl) {
95		sl_free(sl, 1);
96		sl = NULL;
97	}
98	curshell = NULL;
99}
100
101void
102setusershell(void)
103{
104
105	curshell = initshells();
106}
107
108
109static int	_local_initshells(void *, void *, va_list);
110
111/*ARGSUSED*/
112static int
113_local_initshells(rv, cb_data, ap)
114	void	*rv;
115	void	*cb_data;
116	va_list	 ap;
117{
118	char	*sp, *cp;
119	FILE	*fp;
120	char	 line[MAXPATHLEN + 2];
121
122	if (sl)
123		sl_free(sl, 1);
124	sl = sl_init();
125
126	if ((fp = fopen(_PATH_SHELLS, "r")) == NULL)
127		return NS_UNAVAIL;
128
129	sp = cp = line;
130	while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
131		while (*cp != '#' && *cp != '/' && *cp != '\0')
132			cp++;
133		if (*cp == '#' || *cp == '\0')
134			continue;
135		sp = cp;
136		while (!isspace(*cp) && *cp != '#' && *cp != '\0')
137			cp++;
138		*cp++ = '\0';
139		sl_add(sl, strdup(sp));
140	}
141	(void)fclose(fp);
142	return NS_SUCCESS;
143}
144
145#ifdef HESIOD
146static int	_dns_initshells(void *, void *, va_list);
147
148/*ARGSUSED*/
149static int
150_dns_initshells(rv, cb_data, ap)
151	void	*rv;
152	void	*cb_data;
153	va_list	 ap;
154{
155	char	  shellname[] = "shells-XXXXX";
156	int	  hsindex, hpi, r;
157	char	**hp;
158	void	 *context;
159
160	if (sl)
161		sl_free(sl, 1);
162	sl = sl_init();
163	r = NS_UNAVAIL;
164	if (hesiod_init(&context) == -1)
165		return (r);
166
167	for (hsindex = 0; ; hsindex++) {
168		snprintf(shellname, sizeof(shellname)-1, "shells-%d", hsindex);
169		hp = hesiod_resolve(context, shellname, "shells");
170		if (hp == NULL) {
171			if (errno == ENOENT) {
172				if (hsindex == 0)
173					r = NS_NOTFOUND;
174				else
175					r = NS_SUCCESS;
176			}
177			break;
178		} else {
179			for (hpi = 0; hp[hpi]; hpi++)
180				sl_add(sl, hp[hpi]);
181			free(hp);
182		}
183	}
184	hesiod_end(context);
185	return (r);
186}
187#endif /* HESIOD */
188
189#ifdef YP
190static int	_nis_initshells(void *, void *, va_list);
191
192/*ARGSUSED*/
193static int
194_nis_initshells(rv, cb_data, ap)
195	void	*rv;
196	void	*cb_data;
197	va_list	 ap;
198{
199	static char *ypdomain;
200	char	*key, *data;
201	char	*lastkey;
202	int	 keylen, datalen;
203	int	 r;
204
205	if (sl)
206		sl_free(sl, 1);
207	sl = sl_init();
208
209	if (ypdomain == NULL) {
210		switch (yp_get_default_domain(&ypdomain)) {
211		case 0:
212			break;
213		case YPERR_RESRC:
214			return NS_TRYAGAIN;
215		default:
216			return NS_UNAVAIL;
217		}
218	}
219
220	/*
221	 * `key' and `data' point to strings dynamically allocated by
222	 * the yp_... functions.
223	 * `data' is directly put into the stringlist of shells.
224	 */
225	key = data = NULL;
226	if (yp_first(ypdomain, "shells", &key, &keylen, &data, &datalen))
227		return NS_UNAVAIL;
228	do {
229		data[datalen] = '\0';		/* clear trailing \n */
230		sl_add(sl, data);
231
232		lastkey = key;
233		r = yp_next(ypdomain, "shells", lastkey, keylen,
234		    &key, &keylen, &data, &datalen);
235		free(lastkey);
236	} while (r == 0);
237
238	if (r == YPERR_NOMORE) {
239		/*
240		 * `data' and `key' ought to be NULL - do not try to free them.
241		 */
242		return NS_SUCCESS;
243	}
244
245	return NS_UNAVAIL;
246}
247#endif /* YP */
248
249static const char *const *
250initshells()
251{
252	static const ns_dtab dtab[] = {
253		NS_FILES_CB(_local_initshells, NULL)
254		NS_DNS_CB(_dns_initshells, NULL)
255		NS_NIS_CB(_nis_initshells, NULL)
256		{ 0 }
257	};
258	if (sl)
259		sl_free(sl, 1);
260	sl = sl_init();
261
262	if (nsdispatch(NULL, dtab, NSDB_SHELLS, "initshells", __nsdefaultsrc)
263	    != NS_SUCCESS) {
264		if (sl)
265			sl_free(sl, 1);
266		sl = NULL;
267		return (okshells);
268	}
269	sl_add(sl, NULL);
270
271	return (const char *const *)(sl->sl_str);
272}
273