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