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) 35 static 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$"); 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 68 static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; 69 static const char *const *curshell; 70 static StringList *sl; 71 72 static const char *const *initshells(void); 73 74 /* 75 * Get a list of shells from "shells" nsswitch database 76 */ 77 char * 78 getusershell(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 91 void 92 endusershell(void) 93 { 94 if (sl) 95 sl_free(sl, 1); 96 sl = NULL; 97 curshell = NULL; 98 } 99 100 void 101 setusershell(void) 102 { 103 104 curshell = initshells(); 105 } 106 107 108 static int _local_initshells(void *, void *, va_list); 109 110 /*ARGSUSED*/ 111 static int 112 _local_initshells(rv, cb_data, ap) 113 void *rv; 114 void *cb_data; 115 va_list ap; 116 { 117 char *sp, *cp; 118 FILE *fp; 119 char line[MAXPATHLEN + 2]; 120 121 if (sl) 122 sl_free(sl, 1); 123 sl = sl_init(); 124 125 if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) 126 return NS_UNAVAIL; 127 128 sp = cp = line; 129 while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { 130 while (*cp != '#' && *cp != '/' && *cp != '\0') 131 cp++; 132 if (*cp == '#' || *cp == '\0') 133 continue; 134 sp = cp; 135 while (!isspace(*cp) && *cp != '#' && *cp != '\0') 136 cp++; 137 *cp++ = '\0'; 138 sl_add(sl, strdup(sp)); 139 } 140 (void)fclose(fp); 141 return NS_SUCCESS; 142 } 143 144 #ifdef HESIOD 145 static int _dns_initshells(void *, void *, va_list); 146 147 /*ARGSUSED*/ 148 static int 149 _dns_initshells(rv, cb_data, ap) 150 void *rv; 151 void *cb_data; 152 va_list ap; 153 { 154 char shellname[] = "shells-XXXXX"; 155 int hsindex, hpi, r; 156 char **hp; 157 void *context; 158 159 if (sl) 160 sl_free(sl, 1); 161 sl = sl_init(); 162 r = NS_UNAVAIL; 163 if (hesiod_init(&context) == -1) 164 return (r); 165 166 for (hsindex = 0; ; hsindex++) { 167 snprintf(shellname, sizeof(shellname)-1, "shells-%d", hsindex); 168 hp = hesiod_resolve(context, shellname, "shells"); 169 if (hp == NULL) { 170 if (errno == ENOENT) { 171 if (hsindex == 0) 172 r = NS_NOTFOUND; 173 else 174 r = NS_SUCCESS; 175 } 176 break; 177 } else { 178 for (hpi = 0; hp[hpi]; hpi++) 179 sl_add(sl, hp[hpi]); 180 free(hp); 181 } 182 } 183 hesiod_end(context); 184 return (r); 185 } 186 #endif /* HESIOD */ 187 188 #ifdef YP 189 static int _nis_initshells(void *, void *, va_list); 190 191 /*ARGSUSED*/ 192 static int 193 _nis_initshells(rv, cb_data, ap) 194 void *rv; 195 void *cb_data; 196 va_list ap; 197 { 198 static char *ypdomain; 199 200 if (sl) 201 sl_free(sl, 1); 202 sl = sl_init(); 203 204 if (ypdomain == NULL) { 205 switch (yp_get_default_domain(&ypdomain)) { 206 case 0: 207 break; 208 case YPERR_RESRC: 209 return NS_TRYAGAIN; 210 default: 211 return NS_UNAVAIL; 212 } 213 } 214 215 for (;;) { 216 char *ypcur = NULL; 217 int ypcurlen = 0; /* XXX: GCC */ 218 char *key, *data; 219 int keylen, datalen; 220 int r; 221 222 key = data = NULL; 223 if (ypcur) { 224 r = yp_next(ypdomain, "shells", ypcur, ypcurlen, 225 &key, &keylen, &data, &datalen); 226 free(ypcur); 227 switch (r) { 228 case 0: 229 break; 230 case YPERR_NOMORE: 231 free(key); 232 free(data); 233 return NS_SUCCESS; 234 default: 235 free(key); 236 free(data); 237 return NS_UNAVAIL; 238 } 239 ypcur = key; 240 ypcurlen = keylen; 241 } else { 242 if (yp_first(ypdomain, "shells", &ypcur, 243 &ypcurlen, &data, &datalen)) { 244 free(data); 245 return NS_UNAVAIL; 246 } 247 } 248 data[datalen] = '\0'; /* clear trailing \n */ 249 sl_add(sl, data); 250 } 251 } 252 #endif /* YP */ 253 254 static const char *const * 255 initshells() 256 { 257 static const ns_dtab dtab[] = { 258 NS_FILES_CB(_local_initshells, NULL) 259 NS_DNS_CB(_dns_initshells, NULL) 260 NS_NIS_CB(_nis_initshells, NULL) 261 { 0 } 262 }; 263 if (sl) 264 sl_free(sl, 1); 265 sl = sl_init(); 266 267 if (nsdispatch(NULL, dtab, NSDB_SHELLS, "initshells", __nsdefaultsrc) 268 != NS_SUCCESS) { 269 if (sl) 270 sl_free(sl, 1); 271 sl = NULL; 272 return (okshells); 273 } 274 sl_add(sl, NULL); 275 276 return (const char *const *)(sl->sl_str); 277 } 278