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