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