Home | History | Annotate | Line # | Download | only in su
su.c revision 1.11
      1   1.1       cgd /*
      2   1.1       cgd  * Copyright (c) 1988 The Regents of the University of California.
      3   1.1       cgd  * All rights reserved.
      4   1.1       cgd  *
      5   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      6   1.1       cgd  * modification, are permitted provided that the following conditions
      7   1.1       cgd  * are met:
      8   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
      9   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     10   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     12   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     13   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     14   1.1       cgd  *    must display the following acknowledgement:
     15   1.1       cgd  *	This product includes software developed by the University of
     16   1.1       cgd  *	California, Berkeley and its contributors.
     17   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     18   1.1       cgd  *    may be used to endorse or promote products derived from this software
     19   1.1       cgd  *    without specific prior written permission.
     20   1.1       cgd  *
     21   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1       cgd  * SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34   1.1       cgd #ifndef lint
     35   1.1       cgd char copyright[] =
     36   1.1       cgd "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
     37   1.1       cgd  All rights reserved.\n";
     38   1.1       cgd #endif /* not lint */
     39   1.1       cgd 
     40   1.1       cgd #ifndef lint
     41   1.6   mycroft /*static char sccsid[] = "from: @(#)su.c	5.26 (Berkeley) 7/6/91";*/
     42  1.11  christos static char rcsid[] = "$Id: su.c,v 1.11 1996/10/12 23:54:39 christos Exp $";
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46   1.1       cgd #include <sys/time.h>
     47   1.1       cgd #include <sys/resource.h>
     48   1.1       cgd #include <syslog.h>
     49   1.1       cgd #include <stdio.h>
     50  1.11  christos #include <errno.h>
     51   1.7       jtc #include <stdlib.h>
     52   1.1       cgd #include <pwd.h>
     53   1.1       cgd #include <grp.h>
     54   1.1       cgd #include <string.h>
     55   1.1       cgd #include <unistd.h>
     56   1.1       cgd #include <paths.h>
     57   1.1       cgd 
     58   1.1       cgd #ifdef KERBEROS
     59   1.1       cgd #include <kerberosIV/des.h>
     60   1.1       cgd #include <kerberosIV/krb.h>
     61   1.1       cgd #include <netdb.h>
     62   1.1       cgd 
     63   1.1       cgd #define	ARGSTR	"-Kflm"
     64   1.1       cgd 
     65   1.1       cgd int use_kerberos = 1;
     66  1.11  christos 
     67  1.11  christos static int kerberos __P((char *, char *, int));
     68  1.11  christos static int koktologin __P((char *, char *, char *));
     69  1.11  christos 
     70   1.1       cgd #else
     71   1.1       cgd #define	ARGSTR	"-flm"
     72   1.1       cgd #endif
     73   1.1       cgd 
     74  1.11  christos 
     75  1.11  christos int main __P((int, char **));
     76  1.11  christos 
     77  1.11  christos static int chshell __P((char *));
     78  1.11  christos static char *catarg __P((char *, const char *));
     79  1.11  christos static char *ontty __P((void));
     80  1.11  christos 
     81   1.2       sef 
     82   1.7       jtc int
     83   1.1       cgd main(argc, argv)
     84   1.1       cgd 	int argc;
     85   1.1       cgd 	char **argv;
     86   1.1       cgd {
     87  1.11  christos 	extern char *__progname;
     88   1.1       cgd 	extern char **environ;
     89   1.1       cgd 	register struct passwd *pwd;
     90   1.1       cgd 	register char *p, **g;
     91  1.11  christos 	char *shargv[10];	/* shell [-m] [-f] [-c "command"] NULL */
     92  1.11  christos 	int shargc;
     93   1.1       cgd 	struct group *gr;
     94  1.11  christos 	uid_t ruid;
     95   1.1       cgd 	int asme, ch, asthem, fastlogin, prio;
     96   1.1       cgd 	enum { UNSET, YES, NO } iscsh = UNSET;
     97   1.8   mycroft 	char *user, *shell, *avshell, *username, *cleanenv[10], **np;
     98   1.8   mycroft 	char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
     99   1.1       cgd 
    100   1.1       cgd 	asme = asthem = fastlogin = 0;
    101   1.1       cgd 	while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
    102   1.1       cgd 		switch((char)ch) {
    103   1.1       cgd #ifdef KERBEROS
    104   1.1       cgd 		case 'K':
    105   1.1       cgd 			use_kerberos = 0;
    106   1.1       cgd 			break;
    107   1.1       cgd #endif
    108   1.1       cgd 		case 'f':
    109   1.1       cgd 			fastlogin = 1;
    110   1.1       cgd 			break;
    111   1.1       cgd 		case '-':
    112   1.1       cgd 		case 'l':
    113   1.1       cgd 			asme = 0;
    114   1.1       cgd 			asthem = 1;
    115   1.1       cgd 			break;
    116   1.1       cgd 		case 'm':
    117   1.1       cgd 			asme = 1;
    118   1.1       cgd 			asthem = 0;
    119   1.1       cgd 			break;
    120   1.1       cgd 		case '?':
    121   1.1       cgd 		default:
    122  1.11  christos 			(void)fprintf(stderr,
    123  1.11  christos 			    "Usage: %s [%s] [login [shell arguments]]\n",
    124  1.11  christos 			    __progname, ARGSTR);
    125   1.1       cgd 			exit(1);
    126   1.1       cgd 		}
    127   1.1       cgd 	argv += optind;
    128   1.1       cgd 
    129   1.1       cgd 	errno = 0;
    130   1.1       cgd 	prio = getpriority(PRIO_PROCESS, 0);
    131   1.1       cgd 	if (errno)
    132   1.1       cgd 		prio = 0;
    133   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
    134   1.1       cgd 	openlog("su", LOG_CONS, 0);
    135   1.1       cgd 
    136   1.1       cgd 	/* get current login name and shell */
    137   1.1       cgd 	ruid = getuid();
    138   1.1       cgd 	username = getlogin();
    139   1.1       cgd 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
    140   1.1       cgd 	    pwd->pw_uid != ruid)
    141   1.1       cgd 		pwd = getpwuid(ruid);
    142   1.1       cgd 	if (pwd == NULL) {
    143   1.1       cgd 		fprintf(stderr, "su: who are you?\n");
    144   1.1       cgd 		exit(1);
    145   1.1       cgd 	}
    146   1.1       cgd 	username = strdup(pwd->pw_name);
    147   1.1       cgd 	if (asme)
    148   1.1       cgd 		if (pwd->pw_shell && *pwd->pw_shell)
    149   1.1       cgd 			shell = strcpy(shellbuf,  pwd->pw_shell);
    150   1.1       cgd 		else {
    151   1.1       cgd 			shell = _PATH_BSHELL;
    152   1.1       cgd 			iscsh = NO;
    153   1.1       cgd 		}
    154   1.1       cgd 
    155   1.1       cgd 	/* get target login information, default to root */
    156   1.1       cgd 	user = *argv ? *argv : "root";
    157   1.2       sef 	np = *argv ? argv : argv-1;
    158   1.2       sef 
    159   1.1       cgd 	if ((pwd = getpwnam(user)) == NULL) {
    160   1.1       cgd 		fprintf(stderr, "su: unknown login %s\n", user);
    161   1.1       cgd 		exit(1);
    162   1.1       cgd 	}
    163   1.1       cgd 
    164   1.1       cgd 	if (ruid) {
    165   1.1       cgd #ifdef KERBEROS
    166   1.1       cgd 	    if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
    167   1.1       cgd #endif
    168   1.1       cgd 	    {
    169   1.1       cgd 		/* only allow those in group zero to su to root. */
    170   1.1       cgd 		if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)))
    171   1.1       cgd 			for (g = gr->gr_mem;; ++g) {
    172   1.1       cgd 				if (!*g) {
    173   1.1       cgd 					(void)fprintf(stderr,
    174   1.1       cgd 			    "su: you are not in the correct group to su %s.\n",
    175   1.1       cgd 					    user);
    176   1.1       cgd 					exit(1);
    177   1.1       cgd 				}
    178   1.1       cgd 				if (!strcmp(username, *g))
    179   1.1       cgd 					break;
    180   1.1       cgd 		}
    181   1.1       cgd 		/* if target requires a password, verify it */
    182   1.1       cgd 		if (*pwd->pw_passwd) {
    183   1.1       cgd 			p = getpass("Password:");
    184  1.10   deraadt #ifdef SKEY
    185  1.10   deraadt 			if (strcasecmp(p, "s/key") == 0) {
    186  1.10   deraadt 				if (skey_haskey(user)) {
    187  1.10   deraadt 					fprintf(stderr, "Sorry, you have no s/key.\n");
    188  1.10   deraadt 					exit(1);
    189  1.10   deraadt 				} else {
    190  1.10   deraadt 					if (skey_authenticate(user)) {
    191  1.10   deraadt 						goto badlogin;
    192  1.10   deraadt 					}
    193  1.10   deraadt 				}
    194  1.10   deraadt 
    195  1.10   deraadt 			} else
    196  1.10   deraadt #endif
    197   1.1       cgd 			if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
    198  1.10   deraadt badlogin:
    199   1.1       cgd 				fprintf(stderr, "Sorry\n");
    200   1.1       cgd 				syslog(LOG_AUTH|LOG_WARNING,
    201   1.1       cgd 					"BAD SU %s to %s%s", username,
    202   1.1       cgd 					user, ontty());
    203   1.1       cgd 				exit(1);
    204   1.1       cgd 			}
    205   1.1       cgd 		}
    206   1.1       cgd 	    }
    207   1.1       cgd 	}
    208   1.1       cgd 
    209   1.1       cgd 	if (asme) {
    210   1.1       cgd 		/* if asme and non-standard target shell, must be root */
    211   1.1       cgd 		if (!chshell(pwd->pw_shell) && ruid) {
    212   1.1       cgd 			(void)fprintf(stderr,
    213   1.1       cgd 				"su: permission denied (shell).\n");
    214   1.1       cgd 			exit(1);
    215   1.1       cgd 		}
    216   1.1       cgd 	} else if (pwd->pw_shell && *pwd->pw_shell) {
    217   1.1       cgd 		shell = pwd->pw_shell;
    218   1.1       cgd 		iscsh = UNSET;
    219   1.1       cgd 	} else {
    220   1.1       cgd 		shell = _PATH_BSHELL;
    221   1.1       cgd 		iscsh = NO;
    222   1.1       cgd 	}
    223   1.1       cgd 
    224   1.9       cgd 	if (p = rindex(shell, '/'))
    225   1.9       cgd 		avshell = p+1;
    226   1.9       cgd 	else
    227   1.9       cgd 		avshell = shell;
    228   1.9       cgd 
    229   1.1       cgd 	/* if we're forking a csh, we want to slightly muck the args */
    230   1.9       cgd 	if (iscsh == UNSET)
    231  1.11  christos 		iscsh = strstr(avshell, "csh") ? YES : NO;
    232   1.1       cgd 
    233   1.1       cgd 	/* set permissions */
    234   1.1       cgd 	if (setgid(pwd->pw_gid) < 0) {
    235   1.1       cgd 		perror("su: setgid");
    236   1.1       cgd 		exit(1);
    237   1.1       cgd 	}
    238   1.1       cgd 	if (initgroups(user, pwd->pw_gid)) {
    239   1.1       cgd 		(void)fprintf(stderr, "su: initgroups failed.\n");
    240   1.1       cgd 		exit(1);
    241   1.1       cgd 	}
    242   1.1       cgd 	if (setuid(pwd->pw_uid) < 0) {
    243   1.1       cgd 		perror("su: setuid");
    244   1.1       cgd 		exit(1);
    245   1.1       cgd 	}
    246   1.1       cgd 
    247   1.1       cgd 	if (!asme) {
    248   1.1       cgd 		if (asthem) {
    249   1.1       cgd 			p = getenv("TERM");
    250   1.8   mycroft 			cleanenv[0] = NULL;
    251   1.1       cgd 			environ = cleanenv;
    252   1.8   mycroft 			(void)setenv("PATH", _PATH_DEFPATH, 1);
    253  1.11  christos 			if (p)
    254  1.11  christos 				(void)setenv("TERM", p, 1);
    255   1.1       cgd 			if (chdir(pwd->pw_dir) < 0) {
    256   1.1       cgd 				fprintf(stderr, "su: no directory\n");
    257   1.1       cgd 				exit(1);
    258   1.1       cgd 			}
    259   1.1       cgd 		}
    260   1.5       jtc 		if (asthem || pwd->pw_uid)
    261   1.1       cgd 			(void)setenv("USER", pwd->pw_name, 1);
    262   1.1       cgd 		(void)setenv("HOME", pwd->pw_dir, 1);
    263   1.1       cgd 		(void)setenv("SHELL", shell, 1);
    264   1.1       cgd 	}
    265   1.1       cgd 
    266  1.11  christos 	shargc = 0;
    267   1.1       cgd 
    268   1.8   mycroft 	if (asthem) {
    269   1.8   mycroft 		avshellbuf[0] = '-';
    270   1.8   mycroft 		strcpy(avshellbuf+1, avshell);
    271   1.8   mycroft 		avshell = avshellbuf;
    272   1.8   mycroft 	} else if (iscsh == YES) {
    273   1.8   mycroft 		/* csh strips the first character... */
    274   1.8   mycroft 		avshellbuf[0] = '_';
    275   1.8   mycroft 		strcpy(avshellbuf+1, avshell);
    276   1.8   mycroft 		avshell = avshellbuf;
    277   1.8   mycroft 	}
    278  1.11  christos 	shargv[shargc++] = avshell;
    279  1.11  christos 
    280  1.11  christos 	if (iscsh == YES) {
    281  1.11  christos 		if (fastlogin)
    282  1.11  christos 			shargv[shargc++] = "-f";
    283  1.11  christos 		if (asme)
    284  1.11  christos 			shargv[shargc++] = "-m";
    285  1.11  christos 	}
    286  1.11  christos 
    287  1.11  christos 	/* if passed any additional arguments, shells need a "-c" */
    288  1.11  christos 	if (*++np) {
    289  1.11  christos 		char *arg = NULL;
    290  1.11  christos 		shargv[shargc++] = "-c";
    291  1.11  christos 		while (*np)
    292  1.11  christos 			arg = catarg(arg, *np++);
    293  1.11  christos 		shargv[shargc++] = arg;
    294  1.11  christos 	}
    295  1.11  christos 
    296  1.11  christos 	shargv[shargc] = NULL;
    297   1.8   mycroft 
    298   1.1       cgd 	if (ruid != 0)
    299   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
    300   1.1       cgd 		    username, user, ontty());
    301   1.1       cgd 
    302   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, prio);
    303   1.1       cgd 
    304  1.11  christos 	execv(shell, shargv);
    305   1.1       cgd 	(void)fprintf(stderr, "su: %s not found.\n", shell);
    306   1.1       cgd 	exit(1);
    307   1.1       cgd }
    308   1.1       cgd 
    309  1.11  christos static char *
    310  1.11  christos catarg(buf, arg)
    311  1.11  christos 	char *buf;
    312  1.11  christos 	const char *arg;
    313  1.11  christos {
    314  1.11  christos 	char *cp;
    315  1.11  christos 
    316  1.11  christos 	if (buf && *buf) {
    317  1.11  christos 		size_t i = strlen(buf) + strlen(arg) + 1 + 1;
    318  1.11  christos 		if ((cp = malloc(i)) == NULL)
    319  1.11  christos 			err(1, "%s", "");
    320  1.11  christos 		(void)snprintf(cp, i, "%s %s", buf, arg);
    321  1.11  christos 	}
    322  1.11  christos 	else
    323  1.11  christos 		if ((cp = strdup(arg)) == NULL)
    324  1.11  christos 			err(1, "%s", "");
    325  1.11  christos 	if (buf)
    326  1.11  christos 		free(buf);
    327  1.11  christos 
    328  1.11  christos 	return cp;
    329  1.11  christos }
    330  1.11  christos 
    331  1.11  christos 
    332  1.11  christos static int
    333   1.1       cgd chshell(sh)
    334   1.1       cgd 	char *sh;
    335   1.1       cgd {
    336   1.1       cgd 	register char *cp;
    337   1.1       cgd 	char *getusershell();
    338   1.1       cgd 
    339   1.1       cgd 	while ((cp = getusershell()) != NULL)
    340   1.1       cgd 		if (!strcmp(cp, sh))
    341   1.1       cgd 			return (1);
    342   1.1       cgd 	return (0);
    343   1.1       cgd }
    344   1.1       cgd 
    345  1.11  christos static char *
    346   1.1       cgd ontty()
    347   1.1       cgd {
    348   1.1       cgd 	char *p, *ttyname();
    349   1.1       cgd 	static char buf[MAXPATHLEN + 4];
    350   1.1       cgd 
    351   1.1       cgd 	buf[0] = 0;
    352   1.1       cgd 	if (p = ttyname(STDERR_FILENO))
    353   1.1       cgd 		sprintf(buf, " on %s", p);
    354   1.1       cgd 	return (buf);
    355   1.1       cgd }
    356   1.1       cgd 
    357   1.1       cgd #ifdef KERBEROS
    358  1.11  christos static int
    359   1.1       cgd kerberos(username, user, uid)
    360   1.1       cgd 	char *username, *user;
    361   1.1       cgd 	int uid;
    362   1.1       cgd {
    363   1.1       cgd 	KTEXT_ST ticket;
    364   1.1       cgd 	AUTH_DAT authdata;
    365   1.1       cgd 	struct hostent *hp;
    366   1.1       cgd 	register char *p;
    367   1.1       cgd 	int kerno;
    368   1.1       cgd 	u_long faddr;
    369   1.1       cgd 	char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
    370   1.1       cgd 	char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
    371   1.1       cgd 	char *ontty(), *krb_get_phost();
    372   1.1       cgd 
    373   1.1       cgd 	if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
    374   1.1       cgd 		return (1);
    375   1.1       cgd 	if (koktologin(username, lrealm, user) && !uid) {
    376   1.1       cgd 		(void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
    377   1.1       cgd 		return (1);
    378   1.1       cgd 	}
    379   1.1       cgd 	(void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid());
    380   1.1       cgd 
    381   1.1       cgd 	(void)setenv("KRBTKFILE", krbtkfile, 1);
    382   1.1       cgd 	(void)krb_set_tkt_string(krbtkfile);
    383   1.1       cgd 	/*
    384   1.1       cgd 	 * Set real as well as effective ID to 0 for the moment,
    385   1.1       cgd 	 * to make the kerberos library do the right thing.
    386   1.1       cgd 	 */
    387   1.1       cgd 	if (setuid(0) < 0) {
    388   1.1       cgd 		perror("su: setuid");
    389   1.1       cgd 		return (1);
    390   1.1       cgd 	}
    391   1.1       cgd 
    392   1.1       cgd 	/*
    393   1.1       cgd 	 * Little trick here -- if we are su'ing to root,
    394   1.1       cgd 	 * we need to get a ticket for "xxx.root", where xxx represents
    395   1.1       cgd 	 * the name of the person su'ing.  Otherwise (non-root case),
    396   1.1       cgd 	 * we need to get a ticket for "yyy.", where yyy represents
    397   1.1       cgd 	 * the name of the person being su'd to, and the instance is null
    398   1.1       cgd 	 *
    399   1.1       cgd 	 * We should have a way to set the ticket lifetime,
    400   1.1       cgd 	 * with a system default for root.
    401   1.1       cgd 	 */
    402   1.1       cgd 	kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
    403   1.1       cgd 		(uid == 0 ? "root" : ""), lrealm,
    404   1.1       cgd 	    	"krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
    405   1.1       cgd 
    406   1.1       cgd 	if (kerno != KSUCCESS) {
    407   1.1       cgd 		if (kerno == KDC_PR_UNKNOWN) {
    408   1.1       cgd 			fprintf(stderr, "principal unknown: %s.%s@%s\n",
    409   1.1       cgd 				(uid == 0 ? username : user),
    410   1.1       cgd 				(uid == 0 ? "root" : ""), lrealm);
    411   1.1       cgd 			return (1);
    412   1.1       cgd 		}
    413   1.1       cgd 		(void)fprintf(stderr, "su: unable to su: %s\n",
    414   1.1       cgd 		    krb_err_txt[kerno]);
    415   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH,
    416   1.1       cgd 		    "BAD Kerberos SU: %s to %s%s: %s",
    417   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    418   1.1       cgd 		return (1);
    419   1.1       cgd 	}
    420   1.1       cgd 
    421   1.1       cgd 	if (chown(krbtkfile, uid, -1) < 0) {
    422   1.1       cgd 		perror("su: chown:");
    423   1.1       cgd 		(void)unlink(krbtkfile);
    424   1.1       cgd 		return (1);
    425   1.1       cgd 	}
    426   1.1       cgd 
    427   1.1       cgd 	(void)setpriority(PRIO_PROCESS, 0, -2);
    428   1.1       cgd 
    429   1.1       cgd 	if (gethostname(hostname, sizeof(hostname)) == -1) {
    430   1.1       cgd 		perror("su: gethostname");
    431   1.1       cgd 		dest_tkt();
    432   1.1       cgd 		return (1);
    433   1.1       cgd 	}
    434   1.1       cgd 
    435   1.1       cgd 	(void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
    436   1.1       cgd 	savehost[sizeof(savehost) - 1] = '\0';
    437   1.1       cgd 
    438   1.1       cgd 	kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
    439   1.1       cgd 
    440   1.1       cgd 	if (kerno == KDC_PR_UNKNOWN) {
    441   1.1       cgd 		(void)fprintf(stderr, "Warning: TGT not verified.\n");
    442   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH,
    443   1.1       cgd 		    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
    444   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno],
    445   1.1       cgd 		    "rcmd", savehost);
    446   1.1       cgd 	} else if (kerno != KSUCCESS) {
    447   1.1       cgd 		(void)fprintf(stderr, "Unable to use TGT: %s\n",
    448   1.1       cgd 		    krb_err_txt[kerno]);
    449   1.1       cgd 		syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
    450   1.1       cgd 		    username, user, ontty(), krb_err_txt[kerno]);
    451   1.1       cgd 		dest_tkt();
    452   1.1       cgd 		return (1);
    453   1.1       cgd 	} else {
    454   1.1       cgd 		if (!(hp = gethostbyname(hostname))) {
    455   1.1       cgd 			(void)fprintf(stderr, "su: can't get addr of %s\n",
    456   1.1       cgd 			    hostname);
    457   1.1       cgd 			dest_tkt();
    458   1.1       cgd 			return (1);
    459   1.1       cgd 		}
    460   1.1       cgd 		(void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
    461   1.1       cgd 
    462   1.1       cgd 		if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
    463   1.1       cgd 		    &authdata, "")) != KSUCCESS) {
    464   1.1       cgd 			(void)fprintf(stderr,
    465   1.1       cgd 			    "su: unable to verify rcmd ticket: %s\n",
    466   1.1       cgd 			    krb_err_txt[kerno]);
    467   1.1       cgd 			syslog(LOG_NOTICE|LOG_AUTH,
    468   1.1       cgd 			    "failed su: %s to %s%s: %s", username,
    469   1.1       cgd 			     user, ontty(), krb_err_txt[kerno]);
    470   1.1       cgd 			dest_tkt();
    471   1.1       cgd 			return (1);
    472   1.1       cgd 		}
    473   1.1       cgd 	}
    474   1.1       cgd 	return (0);
    475   1.1       cgd }
    476   1.1       cgd 
    477  1.11  christos static int
    478   1.1       cgd koktologin(name, realm, toname)
    479   1.1       cgd 	char *name, *realm, *toname;
    480   1.1       cgd {
    481   1.1       cgd 	register AUTH_DAT *kdata;
    482   1.1       cgd 	AUTH_DAT kdata_st;
    483   1.1       cgd 
    484   1.1       cgd 	kdata = &kdata_st;
    485   1.1       cgd 	bzero((caddr_t) kdata, sizeof(*kdata));
    486   1.1       cgd 	(void)strcpy(kdata->pname, name);
    487   1.1       cgd 	(void)strcpy(kdata->pinst,
    488   1.1       cgd 	    ((strcmp(toname, "root") == 0) ? "root" : ""));
    489   1.1       cgd 	(void)strcpy(kdata->prealm, realm);
    490   1.1       cgd 	return (kuserok(kdata, toname));
    491   1.1       cgd }
    492   1.1       cgd #endif
    493