Home | History | Annotate | Line # | Download | only in gen
getusershell.c revision 1.5.10.4
      1  1.5.10.4  lukem /*	$NetBSD: getusershell.c,v 1.5.10.4 1997/05/27 07:56:15 lukem Exp $	*/
      2       1.5    cgd 
      3       1.1    cgd /*
      4       1.5    cgd  * Copyright (c) 1985, 1993
      5       1.5    cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.5.10.1  lukem  * Portions Copyright (c) 1997
      7  1.5.10.1  lukem  *	Luke Mewburn <lukem (at) netbsd.org>. All rights reserved.
      8       1.1    cgd  *
      9       1.1    cgd  * Redistribution and use in source and binary forms, with or without
     10       1.1    cgd  * modification, are permitted provided that the following conditions
     11       1.1    cgd  * are met:
     12       1.1    cgd  * 1. Redistributions of source code must retain the above copyright
     13       1.1    cgd  *    notice, this list of conditions and the following disclaimer.
     14       1.1    cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1    cgd  *    notice, this list of conditions and the following disclaimer in the
     16       1.1    cgd  *    documentation and/or other materials provided with the distribution.
     17       1.1    cgd  * 3. All advertising materials mentioning features or use of this software
     18       1.1    cgd  *    must display the following acknowledgement:
     19       1.1    cgd  *	This product includes software developed by the University of
     20       1.1    cgd  *	California, Berkeley and its contributors.
     21       1.1    cgd  * 4. Neither the name of the University nor the names of its contributors
     22       1.1    cgd  *    may be used to endorse or promote products derived from this software
     23       1.1    cgd  *    without specific prior written permission.
     24       1.1    cgd  *
     25       1.1    cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26       1.1    cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27       1.1    cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28       1.1    cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29       1.1    cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30       1.1    cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31       1.1    cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32       1.1    cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33       1.1    cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34       1.1    cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35       1.1    cgd  * SUCH DAMAGE.
     36       1.1    cgd  */
     37       1.1    cgd 
     38       1.1    cgd #if defined(LIBC_SCCS) && !defined(lint)
     39       1.5    cgd #if 0
     40       1.5    cgd static char sccsid[] = "@(#)getusershell.c	8.1 (Berkeley) 6/4/93";
     41       1.5    cgd #else
     42  1.5.10.4  lukem static char rcsid[] = "$NetBSD: getusershell.c,v 1.5.10.4 1997/05/27 07:56:15 lukem Exp $";
     43       1.5    cgd #endif
     44       1.1    cgd #endif /* LIBC_SCCS and not lint */
     45       1.1    cgd 
     46       1.1    cgd #include <sys/param.h>
     47       1.1    cgd #include <sys/file.h>
     48       1.5    cgd #include <stdio.h>
     49       1.1    cgd #include <ctype.h>
     50  1.5.10.1  lukem #include <nsswitch.h>
     51       1.1    cgd #include <stdlib.h>
     52       1.1    cgd #include <unistd.h>
     53       1.5    cgd #include <paths.h>
     54  1.5.10.1  lukem #include <string.h>
     55  1.5.10.1  lukem #include <stringlist.h>
     56  1.5.10.1  lukem #ifdef HESIOD
     57  1.5.10.1  lukem #include <hesiod.h>
     58  1.5.10.1  lukem #endif
     59  1.5.10.1  lukem #ifdef YP
     60  1.5.10.1  lukem #include <rpc/rpc.h>
     61  1.5.10.1  lukem #include <rpcsvc/ypclnt.h>
     62  1.5.10.1  lukem #include <rpcsvc/yp_prot.h>
     63  1.5.10.1  lukem #endif
     64       1.1    cgd 
     65       1.1    cgd /*
     66       1.5    cgd  * Local shells should NOT be added here.  They should be added in
     67       1.5    cgd  * /etc/shells.
     68       1.1    cgd  */
     69       1.1    cgd 
     70       1.5    cgd static char *okshells[] = { _PATH_BSHELL, _PATH_CSHELL, NULL };
     71  1.5.10.4  lukem 
     72  1.5.10.4  lukem static char		**curshell;
     73  1.5.10.4  lukem static char		**initshells __P((void));
     74  1.5.10.4  lukem static StringList	 *sl;
     75       1.1    cgd 
     76       1.1    cgd /*
     77  1.5.10.1  lukem  * Get a list of shells from "shells" nsswitch database
     78       1.1    cgd  */
     79       1.1    cgd char *
     80       1.1    cgd getusershell()
     81       1.1    cgd {
     82       1.1    cgd 	char *ret;
     83       1.1    cgd 
     84       1.1    cgd 	if (curshell == NULL)
     85       1.1    cgd 		curshell = initshells();
     86       1.1    cgd 	ret = *curshell;
     87       1.1    cgd 	if (ret != NULL)
     88       1.1    cgd 		curshell++;
     89       1.1    cgd 	return (ret);
     90       1.1    cgd }
     91       1.1    cgd 
     92       1.1    cgd void
     93       1.1    cgd endusershell()
     94       1.1    cgd {
     95  1.5.10.4  lukem 	if (sl)
     96  1.5.10.4  lukem 		sl_free(sl, 1);
     97  1.5.10.1  lukem 	sl = NULL;
     98       1.1    cgd 	curshell = NULL;
     99       1.1    cgd }
    100       1.1    cgd 
    101       1.1    cgd void
    102       1.1    cgd setusershell()
    103       1.1    cgd {
    104       1.1    cgd 
    105       1.1    cgd 	curshell = initshells();
    106       1.1    cgd }
    107       1.1    cgd 
    108  1.5.10.1  lukem static int
    109  1.5.10.1  lukem _local_initshells(rv, cb_data, ap)
    110  1.5.10.1  lukem 	void	*rv;
    111  1.5.10.1  lukem 	void	*cb_data;
    112  1.5.10.1  lukem 	va_list	 ap;
    113       1.1    cgd {
    114  1.5.10.4  lukem 	char	*sp, *cp;
    115  1.5.10.4  lukem 	FILE	*fp;
    116  1.5.10.4  lukem 	char	 line[MAXPATHLEN + 2];
    117  1.5.10.1  lukem 
    118  1.5.10.4  lukem 	if (sl)
    119  1.5.10.4  lukem 		sl_free(sl, 1);
    120  1.5.10.1  lukem 	sl = sl_init();
    121       1.1    cgd 
    122       1.5    cgd 	if ((fp = fopen(_PATH_SHELLS, "r")) == NULL)
    123  1.5.10.1  lukem 		return NS_UNAVAIL;
    124  1.5.10.1  lukem 
    125  1.5.10.1  lukem 	sp = cp = line;
    126       1.1    cgd 	while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
    127       1.1    cgd 		while (*cp != '#' && *cp != '/' && *cp != '\0')
    128       1.1    cgd 			cp++;
    129       1.1    cgd 		if (*cp == '#' || *cp == '\0')
    130       1.1    cgd 			continue;
    131  1.5.10.1  lukem 		sp = cp;
    132       1.1    cgd 		while (!isspace(*cp) && *cp != '#' && *cp != '\0')
    133       1.1    cgd 			cp++;
    134       1.1    cgd 		*cp++ = '\0';
    135  1.5.10.1  lukem 		sl_add(sl, strdup(sp));
    136       1.1    cgd 	}
    137       1.1    cgd 	(void)fclose(fp);
    138  1.5.10.1  lukem 	return NS_SUCCESS;
    139  1.5.10.1  lukem }
    140  1.5.10.1  lukem 
    141  1.5.10.1  lukem #ifdef HESIOD
    142  1.5.10.1  lukem static int
    143  1.5.10.1  lukem _dns_initshells(rv, cb_data, ap)
    144  1.5.10.1  lukem 	void	*rv;
    145  1.5.10.1  lukem 	void	*cb_data;
    146  1.5.10.1  lukem 	va_list	 ap;
    147  1.5.10.1  lukem {
    148  1.5.10.1  lukem 	char	  shellname[] = "shells-XXXXX";
    149  1.5.10.1  lukem 	int	  hsindex, hpi;
    150  1.5.10.1  lukem 	char	**hp;
    151  1.5.10.1  lukem 
    152  1.5.10.4  lukem 	if (sl)
    153  1.5.10.4  lukem 		sl_free(sl, 1);
    154  1.5.10.1  lukem 	sl = sl_init();
    155  1.5.10.1  lukem 
    156  1.5.10.1  lukem 	for (hsindex = 0; ; hsindex++) {
    157  1.5.10.1  lukem 		snprintf(shellname, sizeof(shellname)-1, "shells-%d", hsindex);
    158  1.5.10.1  lukem 		hp = hes_resolve(shellname, "shells");
    159  1.5.10.1  lukem 		if (hp == NULL) {
    160  1.5.10.1  lukem 			switch(hes_error()) {
    161  1.5.10.1  lukem 			case HES_ER_OK:
    162  1.5.10.1  lukem 				break;
    163  1.5.10.1  lukem 			case HES_ER_NOTFOUND:
    164  1.5.10.1  lukem 				if (hsindex == 0)
    165  1.5.10.1  lukem 					return NS_NOTFOUND;
    166  1.5.10.1  lukem 				return NS_SUCCESS;
    167  1.5.10.1  lukem 			default:
    168  1.5.10.1  lukem 				return NS_UNAVAIL;
    169  1.5.10.1  lukem 			}
    170  1.5.10.1  lukem 		} else {
    171  1.5.10.1  lukem 			for (hpi = 0; hp[hpi]; hpi++)
    172  1.5.10.1  lukem 				sl_add(sl, hp[hpi]);
    173  1.5.10.1  lukem 			free(hp);
    174  1.5.10.1  lukem 		}
    175  1.5.10.1  lukem 	}
    176  1.5.10.1  lukem 	return NS_SUCCESS;
    177  1.5.10.1  lukem }
    178  1.5.10.1  lukem #endif /* HESIOD */
    179  1.5.10.1  lukem 
    180  1.5.10.1  lukem #ifdef YP
    181  1.5.10.1  lukem static int
    182  1.5.10.1  lukem _nis_initshells(rv, cb_data, ap)
    183  1.5.10.1  lukem 	void	*rv;
    184  1.5.10.1  lukem 	void	*cb_data;
    185  1.5.10.1  lukem 	va_list	 ap;
    186  1.5.10.1  lukem {
    187  1.5.10.4  lukem 	static char *ypdomain;
    188  1.5.10.1  lukem 
    189  1.5.10.4  lukem 	if (sl)
    190  1.5.10.4  lukem 		sl_free(sl, 1);
    191  1.5.10.1  lukem 	sl = sl_init();
    192  1.5.10.1  lukem 
    193  1.5.10.1  lukem 	if (ypdomain == NULL) {
    194  1.5.10.1  lukem 		switch (yp_get_default_domain(&ypdomain)) {
    195  1.5.10.1  lukem 		case 0:
    196  1.5.10.1  lukem 			break;
    197  1.5.10.1  lukem 		case YPERR_RESRC:
    198  1.5.10.1  lukem 			return NS_TRYAGAIN;
    199  1.5.10.1  lukem 		default:
    200  1.5.10.1  lukem 			return NS_UNAVAIL;
    201  1.5.10.1  lukem 		}
    202  1.5.10.1  lukem 	}
    203  1.5.10.1  lukem 
    204  1.5.10.1  lukem 	for (;;) {
    205  1.5.10.1  lukem 		char	*ypcur = NULL;
    206  1.5.10.1  lukem 		int	 ypcurlen;
    207  1.5.10.1  lukem 		char	*key, *data;
    208  1.5.10.1  lukem 		int	 keylen, datalen;
    209  1.5.10.1  lukem 		int	 r;
    210  1.5.10.1  lukem 
    211  1.5.10.3  lukem 		key = data = NULL;
    212  1.5.10.1  lukem 		if (ypcur) {
    213  1.5.10.1  lukem 			r = yp_next(ypdomain, "shells", ypcur, ypcurlen,
    214  1.5.10.1  lukem 					&key, &keylen, &data, &datalen);
    215  1.5.10.1  lukem 			free(ypcur);
    216  1.5.10.1  lukem 			switch (r) {
    217  1.5.10.1  lukem 			case 0:
    218  1.5.10.1  lukem 				break;
    219  1.5.10.1  lukem 			case YPERR_NOMORE:
    220  1.5.10.1  lukem 				free(key);
    221  1.5.10.1  lukem 				free(data);
    222  1.5.10.1  lukem 				return NS_SUCCESS;
    223  1.5.10.1  lukem 			default:
    224  1.5.10.1  lukem 				free(key);
    225  1.5.10.1  lukem 				free(data);
    226  1.5.10.1  lukem 				return NS_UNAVAIL;
    227  1.5.10.1  lukem 			}
    228  1.5.10.1  lukem 			ypcur = key;
    229  1.5.10.1  lukem 			ypcurlen = keylen;
    230  1.5.10.1  lukem 		} else {
    231  1.5.10.1  lukem 			if (yp_first(ypdomain, "shells", &ypcur,
    232  1.5.10.1  lukem 				    &ypcurlen, &data, &datalen)) {
    233  1.5.10.1  lukem 				free(data);
    234  1.5.10.1  lukem 				return NS_UNAVAIL;
    235  1.5.10.1  lukem 			}
    236  1.5.10.1  lukem 		}
    237  1.5.10.1  lukem 		data[datalen] = '\0';		/* clear trailing \n */
    238  1.5.10.1  lukem 		sl_add(sl, data);
    239  1.5.10.1  lukem 	}
    240  1.5.10.1  lukem 	return NS_SUCCESS;
    241  1.5.10.1  lukem }
    242  1.5.10.1  lukem #endif /* YP */
    243  1.5.10.1  lukem 
    244  1.5.10.1  lukem static char **
    245  1.5.10.1  lukem initshells()
    246  1.5.10.1  lukem {
    247  1.5.10.1  lukem 	static ns_dtab dtab;
    248  1.5.10.1  lukem 
    249  1.5.10.3  lukem 	if (dtab[NS_FILES].cb == NULL) {
    250  1.5.10.3  lukem 		NS_FILES_CB(dtab, _local_initshells, NULL);
    251  1.5.10.3  lukem 		NS_DNS_CB(dtab, _dns_initshells, NULL);
    252  1.5.10.3  lukem 		NS_NIS_CB(dtab, _nis_initshells, NULL);
    253  1.5.10.3  lukem 	}
    254  1.5.10.1  lukem 
    255  1.5.10.4  lukem 	if (sl)
    256  1.5.10.4  lukem 		sl_free(sl, 1);
    257  1.5.10.1  lukem 	sl = sl_init();
    258  1.5.10.1  lukem 
    259  1.5.10.1  lukem 	if (nsdispatch(NULL, dtab, NSDB_SHELLS) != NS_SUCCESS) {
    260  1.5.10.4  lukem 		if (sl)
    261  1.5.10.4  lukem 			sl_free(sl, 1);
    262  1.5.10.1  lukem 		sl = NULL;
    263  1.5.10.1  lukem 		return (okshells);
    264  1.5.10.1  lukem 	}
    265  1.5.10.1  lukem 	sl_add(sl, NULL);
    266  1.5.10.1  lukem 
    267  1.5.10.1  lukem 	return (sl->sl_str);
    268       1.1    cgd }
    269