1 /* $NetBSD: getusershell.c,v 1.18 1999/04/18 02:04:05 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 #if 0 39 static char sccsid[] = "@(#)getusershell.c 8.1 (Berkeley) 6/4/93"; 40 #else 41 __RCSID("$NetBSD: getusershell.c,v 1.18 1999/04/18 02:04:05 lukem Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 #include <sys/param.h> 47 #include <sys/file.h> 48 49 #include <ctype.h> 50 #include <errno.h> 51 #include <nsswitch.h> 52 #include <paths.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <stringlist.h> 57 #include <unistd.h> 58 59 #ifdef __STDC__ 60 #include <stdarg.h> 61 #else 62 #include <varargs.h> 63 #endif 64 65 #ifdef HESIOD 66 #include <hesiod.h> 67 #endif 68 #ifdef YP 69 #include <rpc/rpc.h> 70 #include <rpcsvc/ypclnt.h> 71 #include <rpcsvc/yp_prot.h> 72 #endif 73 74 #ifdef __weak_alias 75 __weak_alias(endusershell,_endusershell); 76 __weak_alias(getusershell,_getusershell); 77 __weak_alias(setusershell,_setusershell); 78 #endif 79 80 /* 81 * Local shells should NOT be added here. They should be added in 82 * /etc/shells. 83 */ 84 85 static const char *const okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL }; 86 static const char *const *curshell; 87 static StringList *sl; 88 89 static const char *const *initshells __P((void)); 90 91 /* 92 * Get a list of shells from "shells" nsswitch database 93 */ 94 __aconst char * 95 getusershell() 96 { 97 __aconst char *ret; 98 99 if (curshell == NULL) 100 curshell = initshells(); 101 /*LINTED*/ 102 ret = (__aconst char *)*curshell; 103 if (ret != NULL) 104 curshell++; 105 return (ret); 106 } 107 108 void 109 endusershell() 110 { 111 if (sl) 112 sl_free(sl, 1); 113 sl = NULL; 114 curshell = NULL; 115 } 116 117 void 118 setusershell() 119 { 120 121 curshell = initshells(); 122 } 123 124 125 static int _local_initshells __P((void *, void *, va_list)); 126 127 /*ARGSUSED*/ 128 static int 129 _local_initshells(rv, cb_data, ap) 130 void *rv; 131 void *cb_data; 132 va_list ap; 133 { 134 char *sp, *cp; 135 FILE *fp; 136 char line[MAXPATHLEN + 2]; 137 138 if (sl) 139 sl_free(sl, 1); 140 sl = sl_init(); 141 142 if ((fp = fopen(_PATH_SHELLS, "r")) == NULL) 143 return NS_UNAVAIL; 144 145 sp = cp = line; 146 while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) { 147 while (*cp != '#' && *cp != '/' && *cp != '\0') 148 cp++; 149 if (*cp == '#' || *cp == '\0') 150 continue; 151 sp = cp; 152 while (!isspace(*cp) && *cp != '#' && *cp != '\0') 153 cp++; 154 *cp++ = '\0'; 155 sl_add(sl, strdup(sp)); 156 } 157 (void)fclose(fp); 158 return NS_SUCCESS; 159 } 160 161 #ifdef HESIOD 162 static int _dns_initshells __P((void *, void *, va_list)); 163 164 /*ARGSUSED*/ 165 static int 166 _dns_initshells(rv, cb_data, ap) 167 void *rv; 168 void *cb_data; 169 va_list ap; 170 { 171 char shellname[] = "shells-XXXXX"; 172 int hsindex, hpi, r; 173 char **hp; 174 void *context; 175 176 if (sl) 177 sl_free(sl, 1); 178 sl = sl_init(); 179 r = NS_UNAVAIL; 180 if (hesiod_init(&context) == -1) 181 return (r); 182 183 for (hsindex = 0; ; hsindex++) { 184 snprintf(shellname, sizeof(shellname)-1, "shells-%d", hsindex); 185 hp = hesiod_resolve(context, shellname, "shells"); 186 if (hp == NULL) { 187 if (errno == ENOENT) { 188 if (hsindex == 0) 189 r = NS_NOTFOUND; 190 else 191 r = NS_SUCCESS; 192 } 193 break; 194 } else { 195 for (hpi = 0; hp[hpi]; hpi++) 196 sl_add(sl, hp[hpi]); 197 free(hp); 198 } 199 } 200 hesiod_end(context); 201 return (r); 202 } 203 #endif /* HESIOD */ 204 205 #ifdef YP 206 static int _nis_initshells __P((void *, void *, va_list)); 207 208 /*ARGSUSED*/ 209 static int 210 _nis_initshells(rv, cb_data, ap) 211 void *rv; 212 void *cb_data; 213 va_list ap; 214 { 215 static char *ypdomain; 216 217 if (sl) 218 sl_free(sl, 1); 219 sl = sl_init(); 220 221 if (ypdomain == NULL) { 222 switch (yp_get_default_domain(&ypdomain)) { 223 case 0: 224 break; 225 case YPERR_RESRC: 226 return NS_TRYAGAIN; 227 default: 228 return NS_UNAVAIL; 229 } 230 } 231 232 for (;;) { 233 char *ypcur = NULL; 234 int ypcurlen = 0; /* XXX: GCC */ 235 char *key, *data; 236 int keylen, datalen; 237 int r; 238 239 key = data = NULL; 240 if (ypcur) { 241 r = yp_next(ypdomain, "shells", ypcur, ypcurlen, 242 &key, &keylen, &data, &datalen); 243 free(ypcur); 244 switch (r) { 245 case 0: 246 break; 247 case YPERR_NOMORE: 248 free(key); 249 free(data); 250 return NS_SUCCESS; 251 default: 252 free(key); 253 free(data); 254 return NS_UNAVAIL; 255 } 256 ypcur = key; 257 ypcurlen = keylen; 258 } else { 259 if (yp_first(ypdomain, "shells", &ypcur, 260 &ypcurlen, &data, &datalen)) { 261 free(data); 262 return NS_UNAVAIL; 263 } 264 } 265 data[datalen] = '\0'; /* clear trailing \n */ 266 sl_add(sl, data); 267 } 268 } 269 #endif /* YP */ 270 271 static const char *const * 272 initshells() 273 { 274 static const ns_dtab dtab[] = { 275 NS_FILES_CB(_local_initshells, NULL) 276 NS_DNS_CB(_dns_initshells, NULL) 277 NS_NIS_CB(_nis_initshells, NULL) 278 { 0 } 279 }; 280 if (sl) 281 sl_free(sl, 1); 282 sl = sl_init(); 283 284 if (nsdispatch(NULL, dtab, NSDB_SHELLS, "initshells", __nsdefaultsrc) 285 != NS_SUCCESS) { 286 if (sl) 287 sl_free(sl, 1); 288 sl = NULL; 289 return (okshells); 290 } 291 sl_add(sl, NULL); 292 293 return (const char *const *)(sl->sl_str); 294 } 295