Home | History | Annotate | Line # | Download | only in gen
getusershell.c revision 1.5.10.1
      1 /*	$NetBSD: getusershell.c,v 1.5.10.1 1997/05/23 21:19:19 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.1 1997/05/23 21:19:19 lukem Exp $";
     43 #endif
     44 #endif /* LIBC_SCCS and not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <stdio.h>
     50 #include <ctype.h>
     51 #include <nsswitch.h>
     52 #include <stdlib.h>
     53 #include <unistd.h>
     54 #include <paths.h>
     55 #include <string.h>
     56 #include <stringlist.h>
     57 #ifdef HESIOD
     58 #include <hesiod.h>
     59 #endif
     60 #ifdef YP
     61 #include <rpc/rpc.h>
     62 #include <rpcsvc/ypclnt.h>
     63 #include <rpcsvc/yp_prot.h>
     64 #endif
     65 
     66 /*
     67  * Local shells should NOT be added here.  They should be added in
     68  * /etc/shells.
     69  */
     70 
     71 static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
     72 static char	**curshell;
     73 static char **initshells __P((void));
     74 static struct 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 	sl_free(sl, 1);
     96 	sl = NULL;
     97 	curshell = NULL;
     98 }
     99 
    100 void
    101 setusershell()
    102 {
    103 
    104 	curshell = initshells();
    105 }
    106 
    107 static int
    108 _local_initshells(rv, cb_data, ap)
    109 	void	*rv;
    110 	void	*cb_data;
    111 	va_list	 ap;
    112 {
    113 	register char	*sp, *cp;
    114 	register FILE *fp;
    115 	struct stat statb;
    116 	char		 line[MAXPATHLEN + 2];
    117 
    118 	sl_free(sl, 1);
    119 	sl = sl_init();
    120 
    121 	if ((fp = fopen(_PATH_SHELLS, "r")) == NULL)
    122 		return NS_UNAVAIL;
    123 	if (fstat(fileno(fp), &statb) == -1) {
    124 		(void)fclose(fp);
    125 		return NS_UNAVAIL;
    126 	}
    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
    146 _dns_initshells(rv, cb_data, ap)
    147 	void	*rv;
    148 	void	*cb_data;
    149 	va_list	 ap;
    150 {
    151 	char	  shellname[] = "shells-XXXXX";
    152 	int	  hsindex, hpi;
    153 	char	**hp;
    154 
    155 	sl_free(sl, 1);
    156 	sl = sl_init();
    157 
    158 	for (hsindex = 0; ; hsindex++) {
    159 		snprintf(shellname, sizeof(shellname)-1, "shells-%d", hsindex);
    160 		hp = hes_resolve(shellname, "shells");
    161 		if (hp == NULL) {
    162 			switch(hes_error()) {
    163 			case HES_ER_OK:
    164 				break;
    165 			case HES_ER_NOTFOUND:
    166 				if (hsindex == 0)
    167 					return NS_NOTFOUND;
    168 				return NS_SUCCESS;
    169 			default:
    170 				return NS_UNAVAIL;
    171 			}
    172 		} else {
    173 			for (hpi = 0; hp[hpi]; hpi++)
    174 				sl_add(sl, hp[hpi]);
    175 			free(hp);
    176 		}
    177 	}
    178 	return NS_SUCCESS;
    179 }
    180 #endif /* HESIOD */
    181 
    182 #ifdef YP
    183 static int
    184 _nis_initshells(rv, cb_data, ap)
    185 	void	*rv;
    186 	void	*cb_data;
    187 	va_list	 ap;
    188 {
    189 	static char	*ypdomain;
    190 
    191 	sl_free(sl, 1);
    192 	sl = sl_init();
    193 
    194 	if (ypdomain == NULL) {
    195 		switch (yp_get_default_domain(&ypdomain)) {
    196 		case 0:
    197 			break;
    198 		case YPERR_RESRC:
    199 			return NS_TRYAGAIN;
    200 		default:
    201 			return NS_UNAVAIL;
    202 		}
    203 	}
    204 
    205 	for (;;) {
    206 		char	*ypcur = NULL;
    207 		int	 ypcurlen;
    208 		char	*key, *data;
    209 		int	 keylen, datalen;
    210 		int	 r;
    211 
    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 	NS_FILES_CB(dtab, _local_initshells, NULL);
    250 	NS_DNS_CB(dtab, _dns_initshells, NULL);
    251 	NS_NIS_CB(dtab, _nis_initshells, NULL);
    252 
    253 	sl_free(sl, 1);
    254 	sl = sl_init();
    255 
    256 	if (nsdispatch(NULL, dtab, NSDB_SHELLS) != NS_SUCCESS) {
    257 		sl_free(sl, 1);
    258 		sl = NULL;
    259 		return (okshells);
    260 	}
    261 	sl_add(sl, NULL);
    262 
    263 	return (sl->sl_str);
    264 }
    265