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