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