1 /* $NetBSD: getusershell.c,v 1.17 1999/01/25 01:09:34 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1985, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 static char rcsid[] = 39 "$FreeBSD$"; 40 #endif /* LIBC_SCCS and not lint */ 41 42 #include <sys/param.h> 43 #include <sys/file.h> 44 45 #include <ctype.h> 46 #include <errno.h> 47 #include <nsswitch.h> 48 #include <paths.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <stringlist.h> 53 #include <unistd.h> 54 55 #ifdef HESIOD 56 #include <hesiod.h> 57 #endif 58 #ifdef YP 59 #include <rpc/rpc.h> 60 #include <rpcsvc/ypclnt.h> 61 #include <rpcsvc/yp_prot.h> 62 #endif 63 64 /* 65 * Local shells should NOT be added here. They should be added in 66 * /etc/shells. 67 */ 68 69 static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; 70 static const char *const *curshell; 71 static StringList *sl; 72 73 static const char *const *initshells __P((void)); 74 75 /* 76 * Get a list of shells from "shells" nsswitch database 77 */ 78 char * 79 getusershell(void) 80 { 81 char *ret; 82 83 if (curshell == NULL) 84 curshell = initshells(); 85 /*LINTED*/ 86 ret = (char *)*curshell; 87 if (ret != NULL) 88 curshell++; 89 return (ret); 90 } 91 92 void 93 endusershell(void) 94 { 95 if (sl) 96 sl_free(sl, 1); 97 sl = NULL; 98 curshell = NULL; 99 } 100 101 void 102 setusershell(void) 103 { 104 105 curshell = initshells(); 106 } 107 108 109 static int _local_initshells __P((void *, void *, va_list)); 110 111 /*ARGSUSED*/ 112 static 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 146 static int _dns_initshells __P((void *, void *, va_list)); 147 148 /*ARGSUSED*/ 149 static 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 190 static int _nis_initshells __P((void *, void *, va_list)); 191 192 /*ARGSUSED*/ 193 static 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 201 if (sl) 202 sl_free(sl, 1); 203 sl = sl_init(); 204 205 if (ypdomain == NULL) { 206 switch (yp_get_default_domain(&ypdomain)) { 207 case 0: 208 break; 209 case YPERR_RESRC: 210 return NS_TRYAGAIN; 211 default: 212 return NS_UNAVAIL; 213 } 214 } 215 216 for (;;) { 217 char *ypcur = NULL; 218 int ypcurlen = 0; /* XXX: GCC */ 219 char *key, *data; 220 int keylen, datalen; 221 int r; 222 223 key = data = NULL; 224 if (ypcur) { 225 r = yp_next(ypdomain, "shells", ypcur, ypcurlen, 226 &key, &keylen, &data, &datalen); 227 free(ypcur); 228 switch (r) { 229 case 0: 230 break; 231 case YPERR_NOMORE: 232 free(key); 233 free(data); 234 return NS_SUCCESS; 235 default: 236 free(key); 237 free(data); 238 return NS_UNAVAIL; 239 } 240 ypcur = key; 241 ypcurlen = keylen; 242 } else { 243 if (yp_first(ypdomain, "shells", &ypcur, 244 &ypcurlen, &data, &datalen)) { 245 free(data); 246 return NS_UNAVAIL; 247 } 248 } 249 data[datalen] = '\0'; /* clear trailing \n */ 250 sl_add(sl, data); 251 } 252 } 253 #endif /* YP */ 254 255 static const char *const * 256 initshells() 257 { 258 static const ns_dtab dtab[] = { 259 NS_FILES_CB(_local_initshells, NULL) 260 NS_DNS_CB(_dns_initshells, NULL) 261 NS_NIS_CB(_nis_initshells, NULL) 262 { 0 } 263 }; 264 if (sl) 265 sl_free(sl, 1); 266 sl = sl_init(); 267 268 if (nsdispatch(NULL, dtab, NSDB_SHELLS, "initshells", __nsdefaultsrc) 269 != NS_SUCCESS) { 270 if (sl) 271 sl_free(sl, 1); 272 sl = NULL; 273 return (okshells); 274 } 275 sl_add(sl, NULL); 276 277 return (const char *const *)(sl->sl_str); 278 } 279