Home | History | Annotate | Line # | Download | only in su
su.c revision 1.62
      1  1.62      manu /*	$NetBSD: su.c,v 1.62 2005/01/08 22:16:23 manu Exp $	*/
      2  1.12  christos 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1988 The Regents of the University of California.
      5   1.1       cgd  * 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.56       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.19     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.19     lukem __COPYRIGHT(
     35  1.19     lukem     "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     36  1.19     lukem  All rights reserved.\n");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40  1.12  christos #if 0
     41  1.13       tls static char sccsid[] = "@(#)su.c	8.3 (Berkeley) 4/2/94";*/
     42  1.12  christos #else
     43  1.62      manu __RCSID("$NetBSD: su.c,v 1.62 2005/01/08 22:16:23 manu Exp $");
     44  1.12  christos #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/time.h>
     49  1.59      manu #ifdef USE_PAM
     50  1.59      manu #include <sys/wait.h>
     51  1.59      manu #endif
     52   1.1       cgd #include <sys/resource.h>
     53  1.13       tls #include <err.h>
     54  1.11  christos #include <errno.h>
     55  1.17     lukem #include <grp.h>
     56  1.17     lukem #include <paths.h>
     57  1.17     lukem #include <pwd.h>
     58  1.17     lukem #include <stdio.h>
     59  1.27  wsanchez #ifdef SKEY
     60  1.17     lukem #include <skey.h>
     61  1.27  wsanchez #endif
     62   1.7       jtc #include <stdlib.h>
     63   1.1       cgd #include <string.h>
     64  1.17     lukem #include <syslog.h>
     65  1.21    kleink #include <time.h>
     66  1.17     lukem #include <tzfile.h>
     67   1.1       cgd #include <unistd.h>
     68   1.1       cgd 
     69  1.59      manu #ifdef USE_PAM
     70  1.59      manu #include <security/pam_appl.h>
     71  1.59      manu #include <security/openpam.h>   /* for openpam_ttyconv() */
     72  1.59      manu 
     73  1.59      manu static pam_handle_t *pamh;
     74  1.59      manu static struct pam_conv pamc;
     75  1.59      manu 
     76  1.60  christos #define fatal(x) do { \
     77  1.60  christos 	if (pam_err == PAM_SUCCESS) \
     78  1.60  christos 		pam_end(pamh, pam_err); \
     79  1.60  christos 	err x;  \
     80  1.60  christos } while (/*CONSTCOND*/ 0)
     81  1.60  christos #define fatalx(x) do { \
     82  1.60  christos 	if (pam_err == PAM_SUCCESS) \
     83  1.60  christos 		pam_end(pamh, pam_err); \
     84  1.60  christos 	errx x; \
     85  1.60  christos } while (/*CONSTCOND*/ 0)
     86  1.60  christos 
     87  1.59      manu #else
     88  1.59      manu #define fatal(x) err x
     89  1.59      manu #define fatalx(x) errx x
     90  1.59      manu 
     91  1.59      manu #endif
     92  1.59      manu 
     93  1.37       mjl #ifdef LOGIN_CAP
     94  1.37       mjl #include <login_cap.h>
     95  1.37       mjl #endif
     96  1.37       mjl 
     97   1.1       cgd #ifdef KERBEROS
     98  1.41     assar #include <des.h>
     99  1.41     assar #include <krb.h>
    100   1.1       cgd #include <netdb.h>
    101   1.1       cgd 
    102  1.41     assar static int kerberos __P((char *, char *, int));
    103  1.41     assar static int koktologin __P((char *, char *, char *));
    104  1.41     assar 
    105  1.41     assar #endif
    106  1.41     assar 
    107  1.41     assar #ifdef KERBEROS5
    108  1.41     assar #include <krb5.h>
    109  1.41     assar 
    110  1.41     assar static int kerberos5 __P((char *, char *, int));
    111  1.41     assar 
    112  1.41     assar #endif
    113  1.41     assar 
    114  1.41     assar #if defined(KERBEROS) || defined(KERBEROS5)
    115  1.41     assar 
    116  1.54      jmmv #define	ARGSTRX	"-Kdflm"
    117   1.1       cgd 
    118   1.1       cgd int use_kerberos = 1;
    119  1.11  christos 
    120   1.1       cgd #else
    121  1.54      jmmv #define	ARGSTRX	"-dflm"
    122   1.1       cgd #endif
    123   1.1       cgd 
    124  1.57  christos #ifndef	SU_GROUP
    125  1.57  christos #define	SU_GROUP	"wheel"
    126  1.18     lukem #endif
    127  1.18     lukem 
    128  1.37       mjl #ifdef LOGIN_CAP
    129  1.37       mjl #define ARGSTR	ARGSTRX "c:"
    130  1.37       mjl #else
    131  1.37       mjl #define ARGSTR ARGSTRX
    132  1.37       mjl #endif
    133  1.37       mjl 
    134  1.11  christos int main __P((int, char **));
    135  1.11  christos 
    136  1.25   mycroft static int chshell __P((const char *));
    137  1.11  christos static char *ontty __P((void));
    138  1.46       sjg static int check_ingroup __P((int, const char *, const char *, int));
    139  1.11  christos 
    140   1.2       sef 
    141   1.7       jtc int
    142   1.1       cgd main(argc, argv)
    143   1.1       cgd 	int argc;
    144   1.1       cgd 	char **argv;
    145   1.1       cgd {
    146   1.1       cgd 	extern char **environ;
    147  1.13       tls 	struct passwd *pwd;
    148  1.17     lukem 	char *p;
    149  1.28  christos #ifdef BSD4_4
    150  1.17     lukem 	struct timeval tp;
    151  1.28  christos #endif
    152  1.11  christos 	uid_t ruid;
    153  1.54      jmmv 	int asme, ch, asthem, fastlogin, prio, gohome;
    154   1.1       cgd 	enum { UNSET, YES, NO } iscsh = UNSET;
    155  1.35  christos 	char *user, *shell, *avshell, *username, **np;
    156  1.37       mjl 	char *userpass, *class;
    157  1.24       mrg 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    158  1.37       mjl 	time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
    159  1.37       mjl #ifdef LOGIN_CAP
    160  1.37       mjl 	login_cap_t *lc;
    161  1.37       mjl #endif
    162  1.59      manu #ifdef USE_PAM
    163  1.59      manu 	char **pam_envlist, **pam_env;
    164  1.59      manu 	char hostname[MAXHOSTNAMELEN];
    165  1.59      manu 	const char **usernamep;
    166  1.59      manu 	char *tty;
    167  1.59      manu 	int pam_err, status;
    168  1.59      manu 	pid_t pid;
    169  1.60  christos #ifdef notdef
    170  1.60  christos 	extern int _openpam_debug;
    171  1.60  christos 	_openpam_debug = 1;
    172  1.60  christos #endif
    173  1.59      manu #endif
    174   1.1       cgd 
    175   1.1       cgd 	asme = asthem = fastlogin = 0;
    176  1.54      jmmv 	gohome = 1;
    177  1.37       mjl 	shell = class = NULL;
    178  1.19     lukem 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    179   1.1       cgd 		switch((char)ch) {
    180  1.41     assar #if defined(KERBEROS) || defined(KERBEROS5)
    181   1.1       cgd 		case 'K':
    182   1.1       cgd 			use_kerberos = 0;
    183   1.1       cgd 			break;
    184   1.1       cgd #endif
    185  1.37       mjl #ifdef LOGIN_CAP
    186  1.37       mjl 		case 'c':
    187  1.37       mjl 			class = optarg;
    188  1.37       mjl 			break;
    189  1.37       mjl #endif
    190  1.54      jmmv 		case 'd':
    191  1.54      jmmv 			asme = 0;
    192  1.54      jmmv 			asthem = 1;
    193  1.54      jmmv 			gohome = 0;
    194  1.54      jmmv 			break;
    195   1.1       cgd 		case 'f':
    196   1.1       cgd 			fastlogin = 1;
    197   1.1       cgd 			break;
    198   1.1       cgd 		case '-':
    199   1.1       cgd 		case 'l':
    200   1.1       cgd 			asme = 0;
    201   1.1       cgd 			asthem = 1;
    202   1.1       cgd 			break;
    203   1.1       cgd 		case 'm':
    204   1.1       cgd 			asme = 1;
    205   1.1       cgd 			asthem = 0;
    206   1.1       cgd 			break;
    207   1.1       cgd 		case '?':
    208   1.1       cgd 		default:
    209  1.11  christos 			(void)fprintf(stderr,
    210  1.58      jmmv 			    "usage: %s [%s] [login [shell arguments]]\n",
    211  1.47       cgd 			    getprogname(), ARGSTR);
    212   1.1       cgd 			exit(1);
    213   1.1       cgd 		}
    214   1.1       cgd 	argv += optind;
    215  1.30  christos 
    216  1.44       erh 	/* Lower the priority so su runs faster */
    217   1.1       cgd 	errno = 0;
    218   1.1       cgd 	prio = getpriority(PRIO_PROCESS, 0);
    219   1.1       cgd 	if (errno)
    220   1.1       cgd 		prio = 0;
    221  1.44       erh 	if (prio > -2)
    222  1.44       erh 		(void)setpriority(PRIO_PROCESS, 0, -2);
    223  1.45     lukem 	openlog("su", 0, LOG_AUTH);
    224   1.1       cgd 
    225   1.1       cgd 	/* get current login name and shell */
    226   1.1       cgd 	ruid = getuid();
    227   1.1       cgd 	username = getlogin();
    228  1.60  christos 	/* get target login information, default to root */
    229  1.60  christos 	user = *argv ? *argv : "root";
    230  1.59      manu 
    231  1.59      manu #ifdef USE_PAM
    232  1.59      manu 	pamc.conv = &openpam_ttyconv;
    233  1.60  christos 	pam_start("su", user, &pamc, &pamh);
    234  1.59      manu 
    235  1.59      manu 	/* Fill in hostname, username and tty */
    236  1.59      manu 	if ((pam_err = pam_set_item(pamh, PAM_RUSER, username)) != PAM_SUCCESS)
    237  1.59      manu 		goto pam_failed;
    238  1.59      manu 
    239  1.59      manu 	gethostname(hostname, sizeof(hostname));
    240  1.59      manu 	if ((pam_err = pam_set_item(pamh, PAM_RHOST, hostname)) != PAM_SUCCESS)
    241  1.59      manu 		goto pam_failed;
    242  1.59      manu 
    243  1.59      manu 	tty = ttyname(STDERR_FILENO);
    244  1.59      manu 	if ((pam_err = pam_set_item(pamh, PAM_TTY, tty)) != PAM_SUCCESS)
    245  1.59      manu 		goto pam_failed;
    246  1.59      manu 
    247  1.59      manu 	/* authentication */
    248  1.59      manu 	if ((pam_err = pam_authenticate(pamh, 0)) != PAM_SUCCESS)
    249  1.59      manu 		goto pam_failed;
    250  1.59      manu 
    251  1.59      manu 	if ((pam_err = pam_acct_mgmt(pamh, 0)) == PAM_NEW_AUTHTOK_REQD)
    252  1.59      manu 		pam_err = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
    253  1.59      manu 	if (pam_err != PAM_SUCCESS)
    254  1.59      manu 		goto pam_failed;
    255  1.59      manu 
    256  1.59      manu 	/* Get user credentials */
    257  1.59      manu 	if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS)
    258  1.59      manu 		goto pam_failed;
    259  1.59      manu 
    260  1.59      manu 	/* Open the session */
    261  1.59      manu 	if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS)
    262  1.59      manu 		goto pam_failed;
    263  1.59      manu 
    264  1.59      manu 	/* New user name? */
    265  1.59      manu 	usernamep = (const char **)&username;
    266  1.59      manu 	pam_err = pam_get_item(pamh, PAM_USER, (const void **)usernamep);
    267  1.59      manu 	if (pam_err != PAM_SUCCESS)
    268  1.59      manu 		goto pam_failed;
    269  1.59      manu 
    270  1.59      manu pam_failed:
    271  1.62      manu 	/*
    272  1.62      manu 	 * If PAM is broken, fallback to plain old authentication.
    273  1.62      manu 	 * Do not do that on authentication errors.
    274  1.62      manu 	 */
    275  1.62      manu 	switch(pam_err) {
    276  1.62      manu 	case PAM_SUCCESS:
    277  1.62      manu 		break;
    278  1.62      manu 
    279  1.62      manu 	case PAM_ABORT:
    280  1.62      manu 	case PAM_BUF_ERR:
    281  1.62      manu 	case PAM_SYMBOL_ERR:
    282  1.62      manu 	case PAM_SYSTEM_ERR:
    283  1.62      manu 		warnx("PAM failed: %s", pam_strerror(pamh, pam_err));
    284  1.62      manu 		warnx("fallback to plain old authentication");
    285  1.59      manu 		pam_end(pamh, pam_err);
    286  1.59      manu 		username = getlogin();
    287  1.62      manu 		break;
    288  1.62      manu 
    289  1.62      manu 	default:
    290  1.62      manu 		fatalx((1, "Sorry: %s\n", pam_strerror(pamh, pam_err)));
    291  1.62      manu 		break;
    292  1.59      manu 	}
    293  1.59      manu 
    294  1.59      manu 	/*
    295  1.59      manu 	 * Now any failure should use fatal/fatalx instead of err/errx
    296  1.59      manu 	 * so that pam_end() is called on errors.
    297  1.59      manu 	 */
    298  1.59      manu #endif
    299   1.1       cgd 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    300   1.1       cgd 	    pwd->pw_uid != ruid)
    301   1.1       cgd 		pwd = getpwuid(ruid);
    302  1.23       mrg 	if (pwd == NULL)
    303  1.59      manu 		fatalx((1, "who are you?"));
    304  1.59      manu 
    305   1.1       cgd 	username = strdup(pwd->pw_name);
    306  1.30  christos 	userpass = strdup(pwd->pw_passwd);
    307  1.30  christos 	if (username == NULL || userpass == NULL)
    308  1.59      manu 		fatal((1, "strdup"));
    309   1.1       cgd 
    310  1.59      manu #ifdef USE_PAM
    311  1.59      manu 	/* Get PAM environment */
    312  1.59      manu 	if ((pam_err == PAM_SUCCESS) &&
    313  1.59      manu 	    ((pam_envlist = pam_getenvlist(pamh)) != NULL)) {
    314  1.59      manu 		for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env) {
    315  1.59      manu 			putenv(*pam_env);
    316  1.59      manu 			free(*pam_env);
    317  1.59      manu 		}
    318  1.59      manu 		free(pam_envlist);
    319  1.59      manu 	}
    320  1.59      manu #endif
    321  1.37       mjl 
    322  1.26      ross 	if (asme) {
    323  1.24       mrg 		if (pwd->pw_shell && *pwd->pw_shell) {
    324  1.50    itojun 			strlcpy(shellbuf, pwd->pw_shell, sizeof(shellbuf));
    325  1.50    itojun 			shell = shellbuf;
    326  1.24       mrg 		} else {
    327  1.24       mrg 			shell = _PATH_BSHELL;
    328  1.24       mrg 			iscsh = NO;
    329  1.24       mrg 		}
    330  1.26      ross 	}
    331   1.2       sef 	np = *argv ? argv : argv-1;
    332   1.2       sef 
    333  1.23       mrg 	if ((pwd = getpwnam(user)) == NULL)
    334  1.59      manu 		fatalx((1, "unknown login %s", user));
    335  1.37       mjl 
    336  1.37       mjl #ifdef LOGIN_CAP
    337  1.37       mjl 	/* force the usage of specified class */
    338  1.37       mjl 	if (class) {
    339  1.37       mjl 		if (ruid)
    340  1.59      manu 			fatalx((1, "Only root may use -c"));
    341  1.37       mjl 
    342  1.37       mjl 		pwd->pw_class = class;
    343  1.37       mjl 	}
    344  1.37       mjl 	lc = login_getclass(pwd->pw_class);
    345  1.37       mjl 
    346  1.37       mjl 	pw_warntime = login_getcaptime(lc, "password-warn",
    347  1.37       mjl                                     _PASSWORD_WARNDAYS * SECSPERDAY,
    348  1.37       mjl                                     _PASSWORD_WARNDAYS * SECSPERDAY);
    349  1.37       mjl #endif
    350  1.37       mjl 
    351  1.24       mrg 	if (ruid
    352  1.61  christos #ifdef USE_PAM
    353  1.61  christos 	    && (pam_err != PAM_SUCCESS)
    354  1.61  christos #endif
    355  1.41     assar #ifdef KERBEROS5
    356  1.41     assar 	    && (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
    357  1.41     assar #endif
    358   1.1       cgd #ifdef KERBEROS
    359  1.24       mrg 	    && (!use_kerberos || kerberos(username, user, pwd->pw_uid))
    360   1.1       cgd #endif
    361  1.24       mrg 	    ) {
    362  1.30  christos 		char *pass = pwd->pw_passwd;
    363  1.30  christos 		int ok = pwd->pw_uid != 0;
    364  1.30  christos 
    365  1.57  christos #ifdef SU_ROOTAUTH
    366  1.34       kim 		/*
    367  1.34       kim 		 * Allow those in group rootauth to su to root, by supplying
    368  1.34       kim 		 * their own password.
    369  1.34       kim 		 */
    370  1.46       sjg 		if (!ok) {
    371  1.57  christos 			if ((ok = check_ingroup(-1, SU_ROOTAUTH, username, 0))) {
    372  1.46       sjg 				pass = userpass;
    373  1.46       sjg 				user = username;
    374  1.34       kim 			}
    375  1.46       sjg 		}
    376  1.34       kim #endif
    377  1.24       mrg 		/*
    378  1.57  christos 		 * Only allow those in group SU_GROUP to su to root,
    379  1.24       mrg 		 * but only if that group has any members.
    380  1.57  christos 		 * If SU_GROUP has no members, allow anyone to su root
    381  1.24       mrg 		 */
    382  1.33       abs 		if (!ok) {
    383  1.57  christos 			ok = check_ingroup(-1, SU_GROUP, username, 1);
    384   1.1       cgd 		}
    385  1.30  christos 		if (!ok)
    386  1.59      manu 			fatalx((1,
    387  1.30  christos 	    "you are not listed in the correct secondary group (%s) to su %s.",
    388  1.59      manu 					    SU_GROUP, user));
    389   1.1       cgd 		/* if target requires a password, verify it */
    390  1.30  christos 		if (*pass) {
    391   1.1       cgd 			p = getpass("Password:");
    392  1.10   deraadt #ifdef SKEY
    393  1.10   deraadt 			if (strcasecmp(p, "s/key") == 0) {
    394  1.59      manu 				if (skey_haskey(user)) {
    395  1.59      manu 					fatalx((1,
    396  1.59      manu 					    "Sorry, you have no s/key."));
    397  1.59      manu 				} else {
    398  1.10   deraadt 					if (skey_authenticate(user)) {
    399  1.10   deraadt 						goto badlogin;
    400  1.10   deraadt 					}
    401  1.10   deraadt 				}
    402  1.10   deraadt 
    403  1.10   deraadt 			} else
    404  1.10   deraadt #endif
    405  1.30  christos 			if (strcmp(pass, crypt(p, pass))) {
    406  1.31  christos #ifdef SKEY
    407  1.10   deraadt badlogin:
    408  1.27  wsanchez #endif
    409   1.1       cgd 				fprintf(stderr, "Sorry\n");
    410  1.45     lukem 				syslog(LOG_WARNING,
    411   1.1       cgd 					"BAD SU %s to %s%s", username,
    412  1.30  christos 					pwd->pw_name, ontty());
    413   1.1       cgd 				exit(1);
    414   1.1       cgd 			}
    415   1.1       cgd 		}
    416   1.1       cgd 	}
    417   1.1       cgd 
    418   1.1       cgd 	if (asme) {
    419   1.1       cgd 		/* if asme and non-standard target shell, must be root */
    420  1.13       tls 		if (!chshell(pwd->pw_shell) && ruid)
    421  1.59      manu 			fatalx((1,"permission denied (shell)."));
    422   1.1       cgd 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    423   1.1       cgd 		shell = pwd->pw_shell;
    424   1.1       cgd 		iscsh = UNSET;
    425   1.1       cgd 	} else {
    426   1.1       cgd 		shell = _PATH_BSHELL;
    427   1.1       cgd 		iscsh = NO;
    428   1.1       cgd 	}
    429   1.1       cgd 
    430  1.17     lukem 	if ((p = strrchr(shell, '/')) != NULL)
    431   1.9       cgd 		avshell = p+1;
    432   1.9       cgd 	else
    433   1.9       cgd 		avshell = shell;
    434   1.9       cgd 
    435   1.1       cgd 	/* if we're forking a csh, we want to slightly muck the args */
    436   1.9       cgd 	if (iscsh == UNSET)
    437  1.11  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    438   1.1       cgd 
    439  1.59      manu #ifdef USE_PAM
    440  1.59      manu 	/*
    441  1.60  christos 	 * If PAM is in use, we must release PAM resources and close
    442  1.60  christos 	 * the session after su child exits. That requires a fork now,
    443  1.59      manu 	 * before we drop the root privs (needed for PAM)
    444  1.59      manu 	 */
    445  1.59      manu 	if (pam_err == PAM_SUCCESS) {
    446  1.59      manu 		switch(pid = fork()) {
    447  1.59      manu 		case -1:
    448  1.59      manu 			fatal((1, "fork"));
    449  1.59      manu 			break;
    450  1.59      manu 		case 0:		/* child */
    451  1.59      manu 			break;
    452  1.59      manu 		default:	/* parent */
    453  1.59      manu 			waitpid(pid, &status, 0);
    454  1.59      manu 			pam_err = pam_close_session(pamh, 0);
    455  1.59      manu 			pam_end(pamh, pam_err);
    456  1.59      manu 
    457  1.59      manu 			exit(WEXITSTATUS(status));
    458  1.59      manu 			break;
    459  1.59      manu 		}
    460  1.59      manu 	}
    461  1.59      manu 
    462  1.59      manu 	/*
    463  1.59      manu 	 * Everything here happens in the child, it should not
    464  1.59      manu 	 * do any PAM operation anymore (don't use fatal/fatalx)
    465  1.59      manu 	 */
    466  1.59      manu #endif
    467   1.1       cgd 	/* set permissions */
    468  1.44       erh #ifdef LOGIN_CAP
    469  1.44       erh 	if (setusercontext(lc, pwd, pwd->pw_uid,
    470  1.44       erh 	    (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
    471  1.44       erh 	    LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
    472  1.44       erh 		err(1, "setting user context");
    473  1.44       erh #else
    474  1.13       tls 	if (setgid(pwd->pw_gid) < 0)
    475  1.13       tls 		err(1, "setgid");
    476  1.13       tls 	if (initgroups(user, pwd->pw_gid))
    477  1.13       tls 		errx(1, "initgroups failed");
    478  1.13       tls 	if (setuid(pwd->pw_uid) < 0)
    479  1.13       tls 		err(1, "setuid");
    480  1.37       mjl #endif
    481   1.1       cgd 
    482   1.1       cgd 	if (!asme) {
    483   1.1       cgd 		if (asthem) {
    484   1.1       cgd 			p = getenv("TERM");
    485  1.35  christos 			/* Create an empty environment */
    486  1.35  christos 			if ((environ = malloc(sizeof(char *))) == NULL)
    487  1.48    simonb 				err(1, NULL);
    488  1.35  christos 			environ[0] = NULL;
    489  1.37       mjl #ifdef LOGIN_CAP
    490  1.44       erh 			if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETPATH))
    491  1.37       mjl 				err(1, "setting user context");
    492  1.37       mjl #else
    493   1.8   mycroft 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    494  1.37       mjl #endif
    495  1.11  christos 			if (p)
    496  1.11  christos 				(void)setenv("TERM", p, 1);
    497  1.54      jmmv 			if (gohome && chdir(pwd->pw_dir) < 0)
    498  1.13       tls 				errx(1, "no directory");
    499  1.37       mjl 		}
    500  1.38       mjl 
    501   1.5       jtc 		if (asthem || pwd->pw_uid)
    502   1.1       cgd 			(void)setenv("USER", pwd->pw_name, 1);
    503   1.1       cgd 		(void)setenv("HOME", pwd->pw_dir, 1);
    504   1.1       cgd 		(void)setenv("SHELL", shell, 1);
    505   1.1       cgd 	}
    506  1.39       abs 	(void)setenv("SU_FROM", username, 1);
    507   1.1       cgd 
    508  1.12  christos 	if (iscsh == YES) {
    509  1.12  christos 		if (fastlogin)
    510  1.12  christos 			*np-- = "-f";
    511  1.12  christos 		if (asme)
    512  1.12  christos 			*np-- = "-m";
    513  1.52  christos 	} else {
    514  1.53   mycroft 		if (fastlogin)
    515  1.53   mycroft 			unsetenv("ENV");
    516  1.12  christos 	}
    517   1.1       cgd 
    518   1.8   mycroft 	if (asthem) {
    519   1.8   mycroft 		avshellbuf[0] = '-';
    520  1.50    itojun 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    521   1.8   mycroft 		avshell = avshellbuf;
    522   1.8   mycroft 	} else if (iscsh == YES) {
    523   1.8   mycroft 		/* csh strips the first character... */
    524   1.8   mycroft 		avshellbuf[0] = '_';
    525  1.50    itojun 		(void)strlcpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 1);
    526   1.8   mycroft 		avshell = avshellbuf;
    527   1.8   mycroft 	}
    528  1.12  christos 	*np = avshell;
    529  1.11  christos 
    530  1.28  christos #ifdef BSD4_4
    531  1.17     lukem 	if (pwd->pw_change || pwd->pw_expire)
    532  1.17     lukem 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    533  1.26      ross 	if (pwd->pw_change) {
    534  1.17     lukem 		if (tp.tv_sec >= pwd->pw_change) {
    535  1.17     lukem 			(void)printf("%s -- %s's password has expired.\n",
    536  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    537  1.17     lukem 			if (ruid != 0)
    538  1.17     lukem 				exit(1);
    539  1.37       mjl 		} else if (pwd->pw_change - tp.tv_sec < pw_warntime)
    540  1.17     lukem 			(void)printf("Warning: %s's password expires on %s",
    541  1.17     lukem 				     user, ctime(&pwd->pw_change));
    542  1.26      ross 	}
    543  1.26      ross 	if (pwd->pw_expire) {
    544  1.17     lukem 		if (tp.tv_sec >= pwd->pw_expire) {
    545  1.17     lukem 			(void)printf("%s -- %s's account has expired.\n",
    546  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    547  1.17     lukem 			if (ruid != 0)
    548  1.17     lukem 				exit(1);
    549  1.17     lukem 		} else if (pwd->pw_expire - tp.tv_sec <
    550  1.17     lukem 		    _PASSWORD_WARNDAYS * SECSPERDAY)
    551  1.17     lukem 			(void)printf("Warning: %s's account expires on %s",
    552  1.17     lukem 				     user, ctime(&pwd->pw_expire));
    553  1.26      ross  	}
    554  1.28  christos #endif
    555   1.1       cgd 	if (ruid != 0)
    556  1.45     lukem 		syslog(LOG_NOTICE, "%s to %s%s",
    557  1.30  christos 		    username, pwd->pw_name, ontty());
    558   1.1       cgd 
    559  1.44       erh 	/* Raise our priority back to what we had before */
    560   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, prio);
    561   1.1       cgd 
    562  1.12  christos 	execv(shell, np);
    563  1.13       tls 	err(1, "%s", shell);
    564  1.27  wsanchez         /* NOTREACHED */
    565   1.1       cgd }
    566  1.11  christos 
    567  1.11  christos static int
    568   1.1       cgd chshell(sh)
    569  1.25   mycroft 	const char *sh;
    570   1.1       cgd {
    571  1.25   mycroft 	const char *cp;
    572   1.1       cgd 
    573  1.55       jrf 	setusershell();
    574   1.1       cgd 	while ((cp = getusershell()) != NULL)
    575   1.1       cgd 		if (!strcmp(cp, sh))
    576   1.1       cgd 			return (1);
    577   1.1       cgd 	return (0);
    578   1.1       cgd }
    579   1.1       cgd 
    580  1.11  christos static char *
    581   1.1       cgd ontty()
    582   1.1       cgd {
    583  1.19     lukem 	char *p;
    584   1.1       cgd 	static char buf[MAXPATHLEN + 4];
    585   1.1       cgd 
    586   1.1       cgd 	buf[0] = 0;
    587  1.17     lukem 	if ((p = ttyname(STDERR_FILENO)) != NULL)
    588  1.15       mrg 		(void)snprintf(buf, sizeof buf, " on %s", p);
    589   1.1       cgd 	return (buf);
    590   1.1       cgd }
    591  1.41     assar 
    592  1.41     assar #ifdef KERBEROS5
    593  1.41     assar static int
    594  1.41     assar kerberos5(username, user, uid)
    595  1.41     assar 	char *username, *user;
    596  1.41     assar 	int uid;
    597  1.41     assar {
    598  1.41     assar 	krb5_error_code ret;
    599  1.41     assar 	krb5_context context;
    600  1.41     assar 	krb5_principal princ = NULL;
    601  1.41     assar 	krb5_ccache ccache, ccache2;
    602  1.41     assar 	char *cc_name;
    603  1.43     assar 	const char *filename;
    604  1.41     assar 
    605  1.41     assar 	ret = krb5_init_context(&context);
    606  1.41     assar 	if (ret)
    607  1.41     assar 		return (1);
    608  1.41     assar 
    609  1.41     assar 	if (strcmp (user, "root") == 0)
    610  1.41     assar 		ret = krb5_make_principal(context, &princ,
    611  1.41     assar 					  NULL, username, "root", NULL);
    612  1.41     assar 	else
    613  1.41     assar 		ret = krb5_make_principal(context, &princ,
    614  1.42     assar 					  NULL, user, NULL);
    615  1.41     assar 	if (ret)
    616  1.41     assar 		goto fail;
    617  1.41     assar 	if (!krb5_kuserok(context, princ, user) && !uid) {
    618  1.41     assar 		warnx ("kerberos5: not in %s's ACL.", user);
    619  1.41     assar 		goto fail;
    620  1.41     assar 	}
    621  1.41     assar 	ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
    622  1.41     assar 	if (ret)
    623  1.41     assar 		goto fail;
    624  1.41     assar 	ret = krb5_verify_user_lrealm(context, princ, ccache, NULL, TRUE,
    625  1.41     assar 				      NULL);
    626  1.41     assar 	if (ret) {
    627  1.41     assar 		krb5_cc_destroy(context, ccache);
    628  1.41     assar 		switch (ret) {
    629  1.41     assar 		case KRB5_LIBOS_PWDINTR :
    630  1.41     assar 			break;
    631  1.41     assar 		case KRB5KRB_AP_ERR_BAD_INTEGRITY:
    632  1.41     assar 		case KRB5KRB_AP_ERR_MODIFIED:
    633  1.41     assar 			krb5_warnx(context, "Password incorrect");
    634  1.41     assar 			break;
    635  1.41     assar 		default :
    636  1.41     assar 			krb5_warn(context, ret, "krb5_verify_user");
    637  1.41     assar 			break;
    638  1.41     assar 		}
    639  1.41     assar 		goto fail;
    640  1.41     assar 	}
    641  1.41     assar 	ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
    642  1.41     assar 	if (ret) {
    643  1.41     assar 		krb5_cc_destroy(context, ccache);
    644  1.41     assar 		goto fail;
    645  1.41     assar 	}
    646  1.41     assar 	ret = krb5_cc_copy_cache(context, ccache, ccache2);
    647  1.41     assar 	if (ret) {
    648  1.41     assar 		krb5_cc_destroy(context, ccache);
    649  1.41     assar 		krb5_cc_destroy(context, ccache2);
    650  1.41     assar 		goto fail;
    651  1.41     assar 	}
    652  1.41     assar 
    653  1.43     assar 	filename = krb5_cc_get_name(context, ccache2);
    654  1.41     assar 	asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
    655  1.43     assar 		 filename);
    656  1.43     assar 	if (chown (filename, uid, -1) < 0) {
    657  1.43     assar 		warn("chown %s", filename);
    658  1.43     assar 		free(cc_name);
    659  1.43     assar 		krb5_cc_destroy(context, ccache);
    660  1.43     assar 		krb5_cc_destroy(context, ccache2);
    661  1.43     assar 		goto fail;
    662  1.43     assar 	}
    663  1.43     assar 
    664  1.41     assar 	setenv("KRB5CCNAME", cc_name, 1);
    665  1.41     assar 	free(cc_name);
    666  1.41     assar 	krb5_cc_close(context, ccache2);
    667  1.41     assar 	krb5_cc_destroy(context, ccache);
    668  1.43     assar 	return (0);
    669  1.41     assar 
    670  1.41     assar  fail:
    671  1.41     assar 	if (princ != NULL)
    672  1.41     assar 		krb5_free_principal (context, princ);
    673  1.41     assar 	krb5_free_context (context);
    674  1.41     assar 	return (1);
    675  1.41     assar }
    676  1.41     assar #endif
    677   1.1       cgd 
    678   1.1       cgd #ifdef KERBEROS
    679  1.11  christos static int
    680   1.1       cgd kerberos(username, user, uid)
    681   1.1       cgd 	char *username, *user;
    682   1.1       cgd 	int uid;
    683   1.1       cgd {
    684   1.1       cgd 	KTEXT_ST ticket;
    685   1.1       cgd 	AUTH_DAT authdata;
    686   1.1       cgd 	struct hostent *hp;
    687   1.1       cgd 	int kerno;
    688   1.1       cgd 	u_long faddr;
    689   1.1       cgd 	char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
    690  1.22       mrg 	char hostname[MAXHOSTNAMELEN + 1], savehost[MAXHOSTNAMELEN + 1];
    691   1.1       cgd 
    692  1.40     assar 	if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
    693   1.1       cgd 		return (1);
    694   1.1       cgd 	if (koktologin(username, lrealm, user) && !uid) {
    695  1.13       tls 		warnx("kerberos: not in %s's ACL.", user);
    696   1.1       cgd 		return (1);
    697   1.1       cgd 	}
    698  1.29    scottr 	(void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
    699  1.15       mrg 	    user, getuid());
    700   1.1       cgd 
    701   1.1       cgd 	(void)setenv("KRBTKFILE", krbtkfile, 1);
    702   1.1       cgd 	(void)krb_set_tkt_string(krbtkfile);
    703   1.1       cgd 	/*
    704   1.1       cgd 	 * Set real as well as effective ID to 0 for the moment,
    705   1.1       cgd 	 * to make the kerberos library do the right thing.
    706   1.1       cgd 	 */
    707   1.1       cgd 	if (setuid(0) < 0) {
    708  1.13       tls 		warn("setuid");
    709   1.1       cgd 		return (1);
    710   1.1       cgd 	}
    711   1.1       cgd 
    712   1.1       cgd 	/*
    713   1.1       cgd 	 * Little trick here -- if we are su'ing to root,
    714   1.1       cgd 	 * we need to get a ticket for "xxx.root", where xxx represents
    715   1.1       cgd 	 * the name of the person su'ing.  Otherwise (non-root case),
    716   1.1       cgd 	 * we need to get a ticket for "yyy.", where yyy represents
    717   1.1       cgd 	 * the name of the person being su'd to, and the instance is null
    718   1.1       cgd 	 *
    719   1.1       cgd 	 * We should have a way to set the ticket lifetime,
    720   1.1       cgd 	 * with a system default for root.
    721   1.1       cgd 	 */
    722  1.40     assar 	{
    723  1.40     assar 		char prompt[128];
    724  1.40     assar 		char passw[256];
    725  1.40     assar 
    726  1.40     assar 		(void)snprintf (prompt, sizeof(prompt),
    727  1.40     assar 			  "%s's Password: ",
    728  1.40     assar 			  krb_unparse_name_long ((uid == 0 ? username : user),
    729  1.40     assar 						 (uid == 0 ? "root" : ""),
    730  1.40     assar 						 lrealm));
    731  1.40     assar 		if (des_read_pw_string (passw, sizeof (passw), prompt, 0)) {
    732  1.40     assar 			memset (passw, 0, sizeof (passw));
    733  1.40     assar 			return (1);
    734  1.40     assar 		}
    735  1.40     assar 		if (strlen(passw) == 0)
    736  1.40     assar 			return (1); /* Empty passwords are not allowed */
    737  1.40     assar 
    738  1.40     assar 		kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
    739  1.40     assar 					  (uid == 0 ? "root" : ""), lrealm,
    740  1.40     assar 					  KRB_TICKET_GRANTING_TICKET,
    741  1.40     assar 					  lrealm,
    742  1.40     assar 					  DEFAULT_TKT_LIFE,
    743  1.40     assar 					  passw);
    744  1.40     assar 		memset (passw, 0, strlen (passw));
    745  1.40     assar 	}
    746   1.1       cgd 
    747   1.1       cgd 	if (kerno != KSUCCESS) {
    748   1.1       cgd 		if (kerno == KDC_PR_UNKNOWN) {
    749  1.13       tls 			warnx("kerberos: principal unknown: %s.%s@%s",
    750   1.1       cgd 				(uid == 0 ? username : user),
    751   1.1       cgd 				(uid == 0 ? "root" : ""), lrealm);
    752   1.1       cgd 			return (1);
    753   1.1       cgd 		}
    754  1.13       tls 		warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
    755  1.45     lukem 		syslog(LOG_WARNING,
    756   1.1       cgd 		    "BAD Kerberos SU: %s to %s%s: %s",
    757   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    758   1.1       cgd 		return (1);
    759   1.1       cgd 	}
    760   1.1       cgd 
    761   1.1       cgd 	if (chown(krbtkfile, uid, -1) < 0) {
    762  1.13       tls 		warn("chown");
    763   1.1       cgd 		(void)unlink(krbtkfile);
    764   1.1       cgd 		return (1);
    765   1.1       cgd 	}
    766   1.1       cgd 
    767   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
    768   1.1       cgd 
    769   1.1       cgd 	if (gethostname(hostname, sizeof(hostname)) == -1) {
    770  1.13       tls 		warn("gethostname");
    771   1.1       cgd 		dest_tkt();
    772   1.1       cgd 		return (1);
    773   1.1       cgd 	}
    774  1.22       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    775   1.1       cgd 
    776  1.40     assar 	(void)strlcpy(savehost, krb_get_phost(hostname), sizeof(savehost));
    777   1.1       cgd 	savehost[sizeof(savehost) - 1] = '\0';
    778   1.1       cgd 
    779   1.1       cgd 	kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
    780   1.1       cgd 
    781   1.1       cgd 	if (kerno == KDC_PR_UNKNOWN) {
    782  1.13       tls 		warnx("Warning: TGT not verified.");
    783  1.45     lukem 		syslog(LOG_WARNING,
    784   1.1       cgd 		    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
    785   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno],
    786   1.1       cgd 		    "rcmd", savehost);
    787   1.1       cgd 	} else if (kerno != KSUCCESS) {
    788  1.13       tls 		warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
    789  1.45     lukem 		syslog(LOG_WARNING, "failed su: %s to %s%s: %s",
    790   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    791   1.1       cgd 		dest_tkt();
    792   1.1       cgd 		return (1);
    793   1.1       cgd 	} else {
    794   1.1       cgd 		if (!(hp = gethostbyname(hostname))) {
    795  1.13       tls 			warnx("can't get addr of %s", hostname);
    796   1.1       cgd 			dest_tkt();
    797   1.1       cgd 			return (1);
    798   1.1       cgd 		}
    799  1.13       tls 		memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
    800   1.1       cgd 
    801   1.1       cgd 		if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
    802   1.1       cgd 		    &authdata, "")) != KSUCCESS) {
    803  1.49    itojun 			warnx("kerberos: unable to verify rcmd ticket: %s",
    804   1.1       cgd 			    krb_err_txt[kerno]);
    805  1.45     lukem 			syslog(LOG_WARNING,
    806   1.1       cgd 			    "failed su: %s to %s%s: %s", username,
    807   1.1       cgd 			     user, ontty(), krb_err_txt[kerno]);
    808   1.1       cgd 			dest_tkt();
    809   1.1       cgd 			return (1);
    810   1.1       cgd 		}
    811   1.1       cgd 	}
    812   1.1       cgd 	return (0);
    813   1.1       cgd }
    814   1.1       cgd 
    815  1.11  christos static int
    816   1.1       cgd koktologin(name, realm, toname)
    817   1.1       cgd 	char *name, *realm, *toname;
    818   1.1       cgd {
    819  1.40     assar 	return krb_kuserok(name,
    820  1.40     assar 			   strcmp (toname, "root") == 0 ? "root" : "",
    821  1.40     assar 			   realm,
    822  1.40     assar 			   toname);
    823   1.1       cgd }
    824   1.1       cgd #endif
    825  1.46       sjg 
    826  1.46       sjg static int
    827  1.46       sjg check_ingroup (gid, gname, user, ifempty)
    828  1.46       sjg 	int gid;
    829  1.46       sjg 	const char *gname;
    830  1.46       sjg 	const char *user;
    831  1.46       sjg 	int ifempty;
    832  1.46       sjg {
    833  1.46       sjg 	struct group *gr;
    834  1.46       sjg 	char **g;
    835  1.46       sjg #ifdef SU_INDIRECT_GROUP
    836  1.46       sjg 	char **gr_mem;
    837  1.46       sjg 	int n = 0;
    838  1.46       sjg 	int i = 0;
    839  1.46       sjg #endif
    840  1.46       sjg 	int ok = 0;
    841  1.46       sjg 
    842  1.46       sjg 	if (gname == NULL)
    843  1.46       sjg 		gr = getgrgid((gid_t) gid);
    844  1.46       sjg 	else
    845  1.46       sjg 		gr = getgrnam(gname);
    846  1.46       sjg 
    847  1.46       sjg 	/*
    848  1.46       sjg 	 * XXX we are relying on the fact that we only set ifempty when
    849  1.57  christos 	 * calling to check for SU_GROUP and that is the only time a
    850  1.46       sjg 	 * missing group is acceptable.
    851  1.46       sjg 	 */
    852  1.46       sjg 	if (gr == NULL)
    853  1.46       sjg 		return ifempty;
    854  1.46       sjg 	if (!*gr->gr_mem)		/* empty */
    855  1.46       sjg 		return ifempty;
    856  1.46       sjg 
    857  1.46       sjg 	/*
    858  1.46       sjg 	 * Ok, first see if user is in gr_mem
    859  1.46       sjg 	 */
    860  1.46       sjg 	for (g = gr->gr_mem; *g; ++g) {
    861  1.46       sjg 		if (strcmp(*g, user) == 0)
    862  1.46       sjg 			return 1;	/* ok */
    863  1.46       sjg #ifdef SU_INDIRECT_GROUP
    864  1.46       sjg 		++n;			/* count them */
    865  1.46       sjg #endif
    866  1.46       sjg 	}
    867  1.46       sjg #ifdef SU_INDIRECT_GROUP
    868  1.46       sjg 	/*
    869  1.46       sjg 	 * No.
    870  1.46       sjg 	 * Now we need to duplicate the gr_mem list, and recurse for
    871  1.46       sjg 	 * each member to see if it is a group, and if so whether user is
    872  1.46       sjg 	 * in it.
    873  1.46       sjg 	 */
    874  1.46       sjg 	gr_mem = malloc((n + 1) * sizeof (char *));
    875  1.46       sjg 	for  (g = gr->gr_mem, i = 0; *g; ++g) {
    876  1.51    itojun 		gr_mem[i] = strdup(*g);
    877  1.51    itojun 		if (!gr_mem[i])
    878  1.51    itojun 			err(1, "strdup");
    879  1.51    itojun 		i++;
    880  1.46       sjg 	}
    881  1.46       sjg 	gr_mem[i++] = NULL;
    882  1.46       sjg 
    883  1.46       sjg 	for  (g = gr_mem; ok == 0 && *g; ++g) {
    884  1.46       sjg 		/*
    885  1.46       sjg 		 * If we get this far we don't accept empty/missing groups.
    886  1.46       sjg 		 */
    887  1.46       sjg 		ok = check_ingroup(-1, *g, user, 0);
    888  1.46       sjg 	}
    889  1.46       sjg 	for  (g = gr_mem; *g; ++g) {
    890  1.46       sjg 		free(*g);
    891  1.46       sjg 	}
    892  1.46       sjg 	free(gr_mem);
    893  1.46       sjg #endif
    894  1.46       sjg 	return ok;
    895  1.46       sjg }
    896