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