Home | History | Annotate | Line # | Download | only in su
su.c revision 1.30
      1  1.30  christos /*	$NetBSD: su.c,v 1.30 1999/03/15 08:05:07 christos 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.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.19     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.19     lukem __COPYRIGHT(
     39  1.19     lukem     "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     40  1.19     lukem  All rights reserved.\n");
     41   1.1       cgd #endif /* not lint */
     42   1.1       cgd 
     43   1.1       cgd #ifndef lint
     44  1.12  christos #if 0
     45  1.13       tls static char sccsid[] = "@(#)su.c	8.3 (Berkeley) 4/2/94";*/
     46  1.12  christos #else
     47  1.30  christos __RCSID("$NetBSD: su.c,v 1.30 1999/03/15 08:05:07 christos Exp $");
     48  1.12  christos #endif
     49   1.1       cgd #endif /* not lint */
     50   1.1       cgd 
     51   1.1       cgd #include <sys/param.h>
     52   1.1       cgd #include <sys/time.h>
     53   1.1       cgd #include <sys/resource.h>
     54  1.13       tls #include <err.h>
     55  1.11  christos #include <errno.h>
     56  1.17     lukem #include <grp.h>
     57  1.17     lukem #include <paths.h>
     58  1.17     lukem #include <pwd.h>
     59  1.30  christos #ifdef SHADOWPASSWORDS
     60  1.30  christos #include <shadow.h>
     61  1.30  christos #endif
     62  1.17     lukem #include <stdio.h>
     63  1.27  wsanchez #ifdef SKEY
     64  1.17     lukem #include <skey.h>
     65  1.27  wsanchez #endif
     66   1.7       jtc #include <stdlib.h>
     67   1.1       cgd #include <string.h>
     68  1.17     lukem #include <syslog.h>
     69  1.21    kleink #include <time.h>
     70  1.17     lukem #include <tzfile.h>
     71   1.1       cgd #include <unistd.h>
     72   1.1       cgd 
     73   1.1       cgd #ifdef KERBEROS
     74   1.1       cgd #include <kerberosIV/des.h>
     75   1.1       cgd #include <kerberosIV/krb.h>
     76   1.1       cgd #include <netdb.h>
     77   1.1       cgd 
     78   1.1       cgd #define	ARGSTR	"-Kflm"
     79   1.1       cgd 
     80   1.1       cgd int use_kerberos = 1;
     81  1.11  christos 
     82  1.11  christos static int kerberos __P((char *, char *, int));
     83  1.11  christos static int koktologin __P((char *, char *, char *));
     84  1.11  christos 
     85   1.1       cgd #else
     86   1.1       cgd #define	ARGSTR	"-flm"
     87   1.1       cgd #endif
     88   1.1       cgd 
     89  1.18     lukem #ifndef	SUGROUP
     90  1.18     lukem #define	SUGROUP	"wheel"
     91  1.18     lukem #endif
     92  1.18     lukem 
     93  1.11  christos int main __P((int, char **));
     94  1.11  christos 
     95  1.25   mycroft static int chshell __P((const char *));
     96  1.11  christos static char *ontty __P((void));
     97  1.11  christos 
     98   1.2       sef 
     99   1.7       jtc int
    100   1.1       cgd main(argc, argv)
    101   1.1       cgd 	int argc;
    102   1.1       cgd 	char **argv;
    103   1.1       cgd {
    104  1.11  christos 	extern char *__progname;
    105   1.1       cgd 	extern char **environ;
    106  1.13       tls 	struct passwd *pwd;
    107  1.30  christos #ifdef SHADOWPASSWORDS
    108  1.30  christos 	struct spwd *shapwd;
    109  1.30  christos #endif
    110  1.17     lukem 	char *p;
    111   1.1       cgd 	struct group *gr;
    112  1.28  christos #ifdef BSD4_4
    113  1.17     lukem 	struct timeval tp;
    114  1.28  christos #endif
    115  1.11  christos 	uid_t ruid;
    116   1.1       cgd 	int asme, ch, asthem, fastlogin, prio;
    117   1.1       cgd 	enum { UNSET, YES, NO } iscsh = UNSET;
    118   1.8   mycroft 	char *user, *shell, *avshell, *username, *cleanenv[10], **np;
    119  1.30  christos 	char *userpass;
    120  1.24       mrg 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
    121   1.1       cgd 
    122   1.1       cgd 	asme = asthem = fastlogin = 0;
    123  1.17     lukem 	shell = NULL;
    124  1.19     lukem 	while ((ch = getopt(argc, argv, ARGSTR)) != -1)
    125   1.1       cgd 		switch((char)ch) {
    126   1.1       cgd #ifdef KERBEROS
    127   1.1       cgd 		case 'K':
    128   1.1       cgd 			use_kerberos = 0;
    129   1.1       cgd 			break;
    130   1.1       cgd #endif
    131   1.1       cgd 		case 'f':
    132   1.1       cgd 			fastlogin = 1;
    133   1.1       cgd 			break;
    134   1.1       cgd 		case '-':
    135   1.1       cgd 		case 'l':
    136   1.1       cgd 			asme = 0;
    137   1.1       cgd 			asthem = 1;
    138   1.1       cgd 			break;
    139   1.1       cgd 		case 'm':
    140   1.1       cgd 			asme = 1;
    141   1.1       cgd 			asthem = 0;
    142   1.1       cgd 			break;
    143   1.1       cgd 		case '?':
    144   1.1       cgd 		default:
    145  1.11  christos 			(void)fprintf(stderr,
    146  1.11  christos 			    "Usage: %s [%s] [login [shell arguments]]\n",
    147  1.11  christos 			    __progname, ARGSTR);
    148   1.1       cgd 			exit(1);
    149   1.1       cgd 		}
    150   1.1       cgd 	argv += optind;
    151   1.1       cgd 
    152  1.30  christos #ifndef BSD4_4
    153  1.30  christos 	if (*argv && strcmp(*argv, "-") == 0) {
    154  1.30  christos 		asme = 0;
    155  1.30  christos 		asthem = 1;
    156  1.30  christos 		argv++;
    157  1.30  christos 	}
    158  1.30  christos #endif
    159  1.30  christos 
    160   1.1       cgd 	errno = 0;
    161   1.1       cgd 	prio = getpriority(PRIO_PROCESS, 0);
    162   1.1       cgd 	if (errno)
    163   1.1       cgd 		prio = 0;
    164   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
    165   1.1       cgd 	openlog("su", LOG_CONS, 0);
    166   1.1       cgd 
    167   1.1       cgd 	/* get current login name and shell */
    168   1.1       cgd 	ruid = getuid();
    169   1.1       cgd 	username = getlogin();
    170   1.1       cgd 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    171   1.1       cgd 	    pwd->pw_uid != ruid)
    172   1.1       cgd 		pwd = getpwuid(ruid);
    173  1.23       mrg 	if (pwd == NULL)
    174  1.13       tls 		errx(1, "who are you?");
    175   1.1       cgd 	username = strdup(pwd->pw_name);
    176  1.30  christos 	userpass = strdup(pwd->pw_passwd);
    177  1.30  christos 	if (username == NULL || userpass == NULL)
    178  1.23       mrg 		err(1, "strdup");
    179   1.1       cgd 
    180  1.26      ross 	if (asme) {
    181  1.24       mrg 		if (pwd->pw_shell && *pwd->pw_shell) {
    182  1.24       mrg 			shell = strncpy(shellbuf, pwd->pw_shell,
    183  1.24       mrg 			    sizeof(shellbuf) - 1);
    184  1.24       mrg 			shellbuf[sizeof(shellbuf) - 1] = '\0';
    185  1.24       mrg 		} else {
    186  1.24       mrg 			shell = _PATH_BSHELL;
    187  1.24       mrg 			iscsh = NO;
    188  1.24       mrg 		}
    189  1.26      ross 	}
    190   1.1       cgd 	/* get target login information, default to root */
    191   1.1       cgd 	user = *argv ? *argv : "root";
    192   1.2       sef 	np = *argv ? argv : argv-1;
    193   1.2       sef 
    194  1.23       mrg 	if ((pwd = getpwnam(user)) == NULL)
    195  1.13       tls 		errx(1, "unknown login %s", user);
    196   1.1       cgd 
    197  1.24       mrg 	if (ruid
    198   1.1       cgd #ifdef KERBEROS
    199  1.24       mrg 	    && (!use_kerberos || kerberos(username, user, pwd->pw_uid))
    200   1.1       cgd #endif
    201  1.24       mrg 	    ) {
    202  1.30  christos 		char *pass = pwd->pw_passwd;
    203  1.30  christos 		int ok = pwd->pw_uid != 0;
    204  1.30  christos 		char **g;
    205  1.30  christos 
    206  1.24       mrg 		/*
    207  1.24       mrg 		 * Only allow those in group SUGROUP to su to root,
    208  1.24       mrg 		 * but only if that group has any members.
    209  1.24       mrg 		 * If SUGROUP has no members, allow anyone to su root
    210  1.24       mrg 		 */
    211  1.30  christos 		if (!ok &&
    212  1.18     lukem 		    (gr = getgrnam(SUGROUP)) && *gr->gr_mem) {
    213  1.18     lukem 
    214  1.18     lukem 			for (g = gr->gr_mem; ; g++) {
    215  1.30  christos 				if (*g == NULL) {
    216  1.30  christos 					ok = 0;
    217  1.30  christos 					break;
    218  1.30  christos 				}
    219  1.30  christos 				if (strcmp(username, *g) == 0) {
    220  1.30  christos 					ok = 1;
    221  1.17     lukem 					break;
    222  1.30  christos 				}
    223  1.18     lukem 			}
    224   1.1       cgd 		}
    225  1.30  christos #ifdef ROOTAUTH
    226  1.30  christos 		/*
    227  1.30  christos 		 * Allow those in group rootauth to su to root, by supplying
    228  1.30  christos 		 * their own password.
    229  1.30  christos 		 */
    230  1.30  christos 		if (!ok && (gr = getgrnam(ROOTAUTH)))
    231  1.30  christos 			for (g = gr->gr_mem;; ++g) {
    232  1.30  christos 				if (!*g) {
    233  1.30  christos 					ok = 0;
    234  1.30  christos 					break;
    235  1.30  christos 				}
    236  1.30  christos 				if (!strcmp(username, *g)) {
    237  1.30  christos 					pass = userpass;
    238  1.30  christos 					user = username;
    239  1.30  christos 					ok = 1;
    240  1.30  christos 					break;
    241  1.30  christos 				}
    242  1.30  christos 			}
    243  1.30  christos #endif
    244  1.30  christos 		if (!ok)
    245  1.30  christos 			errx(1,
    246  1.30  christos 	    "you are not listed in the correct secondary group (%s) to su %s.",
    247  1.30  christos 					    SUGROUP, user);
    248   1.1       cgd 		/* if target requires a password, verify it */
    249  1.30  christos 		if (*pass) {
    250   1.1       cgd 			p = getpass("Password:");
    251  1.10   deraadt #ifdef SKEY
    252  1.10   deraadt 			if (strcasecmp(p, "s/key") == 0) {
    253  1.13       tls 				if (skey_haskey(user))
    254  1.13       tls 					errx(1, "Sorry, you have no s/key.");
    255  1.13       tls 				else {
    256  1.10   deraadt 					if (skey_authenticate(user)) {
    257  1.10   deraadt 						goto badlogin;
    258  1.10   deraadt 					}
    259  1.10   deraadt 				}
    260  1.10   deraadt 
    261  1.10   deraadt 			} else
    262  1.10   deraadt #endif
    263  1.30  christos #ifdef SHADOWPASSWORDS
    264  1.30  christos 			if (strcmp(pass, "x") == 0) {
    265  1.30  christos 				if ((shapwd = getspnam(user)) == NULL)
    266  1.30  christos 					errx(1,
    267  1.30  christos 				    "Could not find shadow passwd entry for %s",
    268  1.30  christos 					    user);
    269  1.30  christos 				if (strcmp(shapwd->sp_pwdp,
    270  1.30  christos 				    crypt(p, shapwd->sp_pwdp))) {
    271  1.30  christos 					goto badlogin;
    272  1.30  christos 				}
    273  1.30  christos 			} else
    274  1.30  christos #endif
    275  1.30  christos 			if (strcmp(pass, crypt(p, pass))) {
    276  1.30  christos #if defined(SKEY) || defined(SHADOWPASSWORDS)
    277  1.10   deraadt badlogin:
    278  1.27  wsanchez #endif
    279   1.1       cgd 				fprintf(stderr, "Sorry\n");
    280   1.1       cgd 				syslog(LOG_AUTH|LOG_WARNING,
    281   1.1       cgd 					"BAD SU %s to %s%s", username,
    282  1.30  christos 					pwd->pw_name, ontty());
    283   1.1       cgd 				exit(1);
    284   1.1       cgd 			}
    285   1.1       cgd 		}
    286   1.1       cgd 	}
    287   1.1       cgd 
    288   1.1       cgd 	if (asme) {
    289   1.1       cgd 		/* if asme and non-standard target shell, must be root */
    290  1.13       tls 		if (!chshell(pwd->pw_shell) && ruid)
    291  1.13       tls 			errx(1,"permission denied (shell).");
    292   1.1       cgd 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    293   1.1       cgd 		shell = pwd->pw_shell;
    294   1.1       cgd 		iscsh = UNSET;
    295   1.1       cgd 	} else {
    296   1.1       cgd 		shell = _PATH_BSHELL;
    297   1.1       cgd 		iscsh = NO;
    298   1.1       cgd 	}
    299   1.1       cgd 
    300  1.17     lukem 	if ((p = strrchr(shell, '/')) != NULL)
    301   1.9       cgd 		avshell = p+1;
    302   1.9       cgd 	else
    303   1.9       cgd 		avshell = shell;
    304   1.9       cgd 
    305   1.1       cgd 	/* if we're forking a csh, we want to slightly muck the args */
    306   1.9       cgd 	if (iscsh == UNSET)
    307  1.11  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    308   1.1       cgd 
    309   1.1       cgd 	/* set permissions */
    310  1.13       tls 	if (setgid(pwd->pw_gid) < 0)
    311  1.13       tls 		err(1, "setgid");
    312  1.13       tls 	if (initgroups(user, pwd->pw_gid))
    313  1.13       tls 		errx(1, "initgroups failed");
    314  1.13       tls 	if (setuid(pwd->pw_uid) < 0)
    315  1.13       tls 		err(1, "setuid");
    316   1.1       cgd 
    317   1.1       cgd 	if (!asme) {
    318   1.1       cgd 		if (asthem) {
    319   1.1       cgd 			p = getenv("TERM");
    320   1.8   mycroft 			cleanenv[0] = NULL;
    321   1.1       cgd 			environ = cleanenv;
    322   1.8   mycroft 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    323  1.11  christos 			if (p)
    324  1.11  christos 				(void)setenv("TERM", p, 1);
    325  1.13       tls 			if (chdir(pwd->pw_dir) < 0)
    326  1.13       tls 				errx(1, "no directory");
    327   1.1       cgd 		}
    328   1.5       jtc 		if (asthem || pwd->pw_uid)
    329   1.1       cgd 			(void)setenv("USER", pwd->pw_name, 1);
    330   1.1       cgd 		(void)setenv("HOME", pwd->pw_dir, 1);
    331   1.1       cgd 		(void)setenv("SHELL", shell, 1);
    332   1.1       cgd 	}
    333   1.1       cgd 
    334  1.12  christos 	if (iscsh == YES) {
    335  1.12  christos 		if (fastlogin)
    336  1.12  christos 			*np-- = "-f";
    337  1.12  christos 		if (asme)
    338  1.12  christos 			*np-- = "-m";
    339  1.12  christos 	}
    340   1.1       cgd 
    341   1.8   mycroft 	if (asthem) {
    342   1.8   mycroft 		avshellbuf[0] = '-';
    343  1.15       mrg 		(void)strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
    344   1.8   mycroft 		avshell = avshellbuf;
    345   1.8   mycroft 	} else if (iscsh == YES) {
    346   1.8   mycroft 		/* csh strips the first character... */
    347   1.8   mycroft 		avshellbuf[0] = '_';
    348  1.15       mrg 		(void)strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
    349   1.8   mycroft 		avshell = avshellbuf;
    350   1.8   mycroft 	}
    351  1.12  christos 	*np = avshell;
    352  1.11  christos 
    353  1.28  christos #ifdef BSD4_4
    354  1.17     lukem 	if (pwd->pw_change || pwd->pw_expire)
    355  1.17     lukem 		(void)gettimeofday(&tp, (struct timezone *)NULL);
    356  1.26      ross 	if (pwd->pw_change) {
    357  1.17     lukem 		if (tp.tv_sec >= pwd->pw_change) {
    358  1.17     lukem 			(void)printf("%s -- %s's password has expired.\n",
    359  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    360  1.17     lukem 			if (ruid != 0)
    361  1.17     lukem 				exit(1);
    362  1.17     lukem 		} else if (pwd->pw_change - tp.tv_sec <
    363  1.17     lukem 		    _PASSWORD_WARNDAYS * SECSPERDAY)
    364  1.17     lukem 			(void)printf("Warning: %s's password expires on %s",
    365  1.17     lukem 				     user, ctime(&pwd->pw_change));
    366  1.26      ross 	}
    367  1.26      ross 	if (pwd->pw_expire) {
    368  1.17     lukem 		if (tp.tv_sec >= pwd->pw_expire) {
    369  1.17     lukem 			(void)printf("%s -- %s's account has expired.\n",
    370  1.17     lukem 				     (ruid ? "Sorry" : "Note"), user);
    371  1.17     lukem 			if (ruid != 0)
    372  1.17     lukem 				exit(1);
    373  1.17     lukem 		} else if (pwd->pw_expire - tp.tv_sec <
    374  1.17     lukem 		    _PASSWORD_WARNDAYS * SECSPERDAY)
    375  1.17     lukem 			(void)printf("Warning: %s's account expires on %s",
    376  1.17     lukem 				     user, ctime(&pwd->pw_expire));
    377  1.26      ross  	}
    378  1.28  christos #endif
    379   1.1       cgd 	if (ruid != 0)
    380   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
    381  1.30  christos 		    username, pwd->pw_name, ontty());
    382   1.1       cgd 
    383   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, prio);
    384   1.1       cgd 
    385  1.12  christos 	execv(shell, np);
    386  1.13       tls 	err(1, "%s", shell);
    387  1.27  wsanchez         /* NOTREACHED */
    388   1.1       cgd }
    389  1.11  christos 
    390  1.11  christos static int
    391   1.1       cgd chshell(sh)
    392  1.25   mycroft 	const char *sh;
    393   1.1       cgd {
    394  1.25   mycroft 	const char *cp;
    395   1.1       cgd 
    396   1.1       cgd 	while ((cp = getusershell()) != NULL)
    397   1.1       cgd 		if (!strcmp(cp, sh))
    398   1.1       cgd 			return (1);
    399   1.1       cgd 	return (0);
    400   1.1       cgd }
    401   1.1       cgd 
    402  1.11  christos static char *
    403   1.1       cgd ontty()
    404   1.1       cgd {
    405  1.19     lukem 	char *p;
    406   1.1       cgd 	static char buf[MAXPATHLEN + 4];
    407   1.1       cgd 
    408   1.1       cgd 	buf[0] = 0;
    409  1.17     lukem 	if ((p = ttyname(STDERR_FILENO)) != NULL)
    410  1.15       mrg 		(void)snprintf(buf, sizeof buf, " on %s", p);
    411   1.1       cgd 	return (buf);
    412   1.1       cgd }
    413   1.1       cgd 
    414   1.1       cgd #ifdef KERBEROS
    415  1.11  christos static int
    416   1.1       cgd kerberos(username, user, uid)
    417   1.1       cgd 	char *username, *user;
    418   1.1       cgd 	int uid;
    419   1.1       cgd {
    420   1.1       cgd 	KTEXT_ST ticket;
    421   1.1       cgd 	AUTH_DAT authdata;
    422   1.1       cgd 	struct hostent *hp;
    423   1.1       cgd 	int kerno;
    424   1.1       cgd 	u_long faddr;
    425   1.1       cgd 	char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
    426  1.22       mrg 	char hostname[MAXHOSTNAMELEN + 1], savehost[MAXHOSTNAMELEN + 1];
    427   1.1       cgd 
    428  1.29    scottr 	if (krb_get_lrealm(lrealm, 1) != KSUCCESS ||
    429  1.29    scottr 	    strcmp(lrealm, KRB_REALM) == 0)
    430   1.1       cgd 		return (1);
    431   1.1       cgd 	if (koktologin(username, lrealm, user) && !uid) {
    432  1.13       tls 		warnx("kerberos: not in %s's ACL.", user);
    433   1.1       cgd 		return (1);
    434   1.1       cgd 	}
    435  1.29    scottr 	(void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
    436  1.15       mrg 	    user, getuid());
    437   1.1       cgd 
    438   1.1       cgd 	(void)setenv("KRBTKFILE", krbtkfile, 1);
    439   1.1       cgd 	(void)krb_set_tkt_string(krbtkfile);
    440   1.1       cgd 	/*
    441   1.1       cgd 	 * Set real as well as effective ID to 0 for the moment,
    442   1.1       cgd 	 * to make the kerberos library do the right thing.
    443   1.1       cgd 	 */
    444   1.1       cgd 	if (setuid(0) < 0) {
    445  1.13       tls 		warn("setuid");
    446   1.1       cgd 		return (1);
    447   1.1       cgd 	}
    448   1.1       cgd 
    449   1.1       cgd 	/*
    450   1.1       cgd 	 * Little trick here -- if we are su'ing to root,
    451   1.1       cgd 	 * we need to get a ticket for "xxx.root", where xxx represents
    452   1.1       cgd 	 * the name of the person su'ing.  Otherwise (non-root case),
    453   1.1       cgd 	 * we need to get a ticket for "yyy.", where yyy represents
    454   1.1       cgd 	 * the name of the person being su'd to, and the instance is null
    455   1.1       cgd 	 *
    456   1.1       cgd 	 * We should have a way to set the ticket lifetime,
    457   1.1       cgd 	 * with a system default for root.
    458   1.1       cgd 	 */
    459   1.1       cgd 	kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
    460   1.1       cgd 		(uid == 0 ? "root" : ""), lrealm,
    461  1.17     lukem 		"krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
    462   1.1       cgd 
    463   1.1       cgd 	if (kerno != KSUCCESS) {
    464   1.1       cgd 		if (kerno == KDC_PR_UNKNOWN) {
    465  1.13       tls 			warnx("kerberos: principal unknown: %s.%s@%s",
    466   1.1       cgd 				(uid == 0 ? username : user),
    467   1.1       cgd 				(uid == 0 ? "root" : ""), lrealm);
    468   1.1       cgd 			return (1);
    469   1.1       cgd 		}
    470  1.13       tls 		warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
    471   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH,
    472   1.1       cgd 		    "BAD Kerberos SU: %s to %s%s: %s",
    473   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    474   1.1       cgd 		return (1);
    475   1.1       cgd 	}
    476   1.1       cgd 
    477   1.1       cgd 	if (chown(krbtkfile, uid, -1) < 0) {
    478  1.13       tls 		warn("chown");
    479   1.1       cgd 		(void)unlink(krbtkfile);
    480   1.1       cgd 		return (1);
    481   1.1       cgd 	}
    482   1.1       cgd 
    483   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
    484   1.1       cgd 
    485   1.1       cgd 	if (gethostname(hostname, sizeof(hostname)) == -1) {
    486  1.13       tls 		warn("gethostname");
    487   1.1       cgd 		dest_tkt();
    488   1.1       cgd 		return (1);
    489   1.1       cgd 	}
    490  1.22       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    491   1.1       cgd 
    492   1.1       cgd 	(void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
    493   1.1       cgd 	savehost[sizeof(savehost) - 1] = '\0';
    494   1.1       cgd 
    495   1.1       cgd 	kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
    496   1.1       cgd 
    497   1.1       cgd 	if (kerno == KDC_PR_UNKNOWN) {
    498  1.13       tls 		warnx("Warning: TGT not verified.");
    499   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH,
    500   1.1       cgd 		    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
    501   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno],
    502   1.1       cgd 		    "rcmd", savehost);
    503   1.1       cgd 	} else if (kerno != KSUCCESS) {
    504  1.13       tls 		warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
    505   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
    506   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    507   1.1       cgd 		dest_tkt();
    508   1.1       cgd 		return (1);
    509   1.1       cgd 	} else {
    510   1.1       cgd 		if (!(hp = gethostbyname(hostname))) {
    511  1.13       tls 			warnx("can't get addr of %s", hostname);
    512   1.1       cgd 			dest_tkt();
    513   1.1       cgd 			return (1);
    514   1.1       cgd 		}
    515  1.13       tls 		memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
    516   1.1       cgd 
    517   1.1       cgd 		if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
    518   1.1       cgd 		    &authdata, "")) != KSUCCESS) {
    519  1.13       tls 			warnx("kerberos: unable to verify rcmd ticket: %s\n",
    520   1.1       cgd 			    krb_err_txt[kerno]);
    521   1.1       cgd 			syslog(LOG_NOTICE|LOG_AUTH,
    522   1.1       cgd 			    "failed su: %s to %s%s: %s", username,
    523   1.1       cgd 			     user, ontty(), krb_err_txt[kerno]);
    524   1.1       cgd 			dest_tkt();
    525   1.1       cgd 			return (1);
    526   1.1       cgd 		}
    527   1.1       cgd 	}
    528   1.1       cgd 	return (0);
    529   1.1       cgd }
    530   1.1       cgd 
    531  1.11  christos static int
    532   1.1       cgd koktologin(name, realm, toname)
    533   1.1       cgd 	char *name, *realm, *toname;
    534   1.1       cgd {
    535  1.19     lukem 	AUTH_DAT *kdata;
    536   1.1       cgd 	AUTH_DAT kdata_st;
    537   1.1       cgd 
    538   1.1       cgd 	kdata = &kdata_st;
    539  1.13       tls 	memset((char *)kdata, 0, sizeof(*kdata));
    540  1.15       mrg 	(void)strncpy(kdata->pname, name, sizeof(kdata->pname) - 1);
    541  1.16  explorer 	(void)strncpy(kdata->pinst,
    542  1.15       mrg 	    ((strcmp(toname, "root") == 0) ? "root" : ""), sizeof(kdata->pinst) - 1);
    543  1.16  explorer 	(void)strncpy(kdata->prealm, realm, sizeof(kdata->prealm) - 1);
    544   1.1       cgd 	return (kuserok(kdata, toname));
    545   1.1       cgd }
    546   1.1       cgd #endif
    547