xref: /netbsd-src/usr.bin/su/su.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
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[] = "from: @(#)su.c	5.26 (Berkeley) 7/6/91";*/
42 static char rcsid[] = "$Id: su.c,v 1.7 1993/08/27 22:30:44 jtc Exp $";
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <syslog.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <pwd.h>
52 #include <grp.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <paths.h>
56 
57 #ifdef KERBEROS
58 #include <kerberosIV/des.h>
59 #include <kerberosIV/krb.h>
60 #include <netdb.h>
61 
62 #define	ARGSTR	"-Kflm"
63 
64 int use_kerberos = 1;
65 #else
66 #define	ARGSTR	"-flm"
67 #endif
68 
69 extern char *crypt();
70 int chshell();
71 
72 int
73 main(argc, argv)
74 	int argc;
75 	char **argv;
76 {
77 	extern char **environ;
78 	extern int errno, optind;
79 	register struct passwd *pwd;
80 	register char *p, **g;
81 	struct group *gr;
82 	uid_t ruid, getuid();
83 	int asme, ch, asthem, fastlogin, prio;
84 	enum { UNSET, YES, NO } iscsh = UNSET;
85 	char *user, *shell, *username, *cleanenv[2], **np;
86 	char shellbuf[MAXPATHLEN];
87 	char *getpass(), *getenv(), *getlogin(), *ontty();
88 
89 	asme = asthem = fastlogin = 0;
90 	while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
91 		switch((char)ch) {
92 #ifdef KERBEROS
93 		case 'K':
94 			use_kerberos = 0;
95 			break;
96 #endif
97 		case 'f':
98 			fastlogin = 1;
99 			break;
100 		case '-':
101 		case 'l':
102 			asme = 0;
103 			asthem = 1;
104 			break;
105 		case 'm':
106 			asme = 1;
107 			asthem = 0;
108 			break;
109 		case '?':
110 		default:
111 			(void)fprintf(stderr, "usage: su [%s] [login]\n",
112 			    ARGSTR);
113 			exit(1);
114 		}
115 	argv += optind;
116 
117 	errno = 0;
118 	prio = getpriority(PRIO_PROCESS, 0);
119 	if (errno)
120 		prio = 0;
121 	(void)setpriority(PRIO_PROCESS, 0, -2);
122 	openlog("su", LOG_CONS, 0);
123 
124 	/* get current login name and shell */
125 	ruid = getuid();
126 	username = getlogin();
127 	if (username == NULL || (pwd = getpwnam(username)) == NULL ||
128 	    pwd->pw_uid != ruid)
129 		pwd = getpwuid(ruid);
130 	if (pwd == NULL) {
131 		fprintf(stderr, "su: who are you?\n");
132 		exit(1);
133 	}
134 	username = strdup(pwd->pw_name);
135 	if (asme)
136 		if (pwd->pw_shell && *pwd->pw_shell)
137 			shell = strcpy(shellbuf,  pwd->pw_shell);
138 		else {
139 			shell = _PATH_BSHELL;
140 			iscsh = NO;
141 		}
142 
143 	/* get target login information, default to root */
144 	user = *argv ? *argv : "root";
145 	np = *argv ? argv : argv-1;
146 
147 	if ((pwd = getpwnam(user)) == NULL) {
148 		fprintf(stderr, "su: unknown login %s\n", user);
149 		exit(1);
150 	}
151 
152 	if (ruid) {
153 #ifdef KERBEROS
154 	    if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
155 #endif
156 	    {
157 		/* only allow those in group zero to su to root. */
158 		if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)))
159 			for (g = gr->gr_mem;; ++g) {
160 				if (!*g) {
161 					(void)fprintf(stderr,
162 			    "su: you are not in the correct group to su %s.\n",
163 					    user);
164 					exit(1);
165 				}
166 				if (!strcmp(username, *g))
167 					break;
168 		}
169 		/* if target requires a password, verify it */
170 		if (*pwd->pw_passwd) {
171 			p = getpass("Password:");
172 			if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
173 				fprintf(stderr, "Sorry\n");
174 				syslog(LOG_AUTH|LOG_WARNING,
175 					"BAD SU %s to %s%s", username,
176 					user, ontty());
177 				exit(1);
178 			}
179 		}
180 	    }
181 	}
182 
183 	if (asme) {
184 		/* if asme and non-standard target shell, must be root */
185 		if (!chshell(pwd->pw_shell) && ruid) {
186 			(void)fprintf(stderr,
187 				"su: permission denied (shell).\n");
188 			exit(1);
189 		}
190 	} else if (pwd->pw_shell && *pwd->pw_shell) {
191 		shell = pwd->pw_shell;
192 		iscsh = UNSET;
193 	} else {
194 		shell = _PATH_BSHELL;
195 		iscsh = NO;
196 	}
197 
198 	/* if we're forking a csh, we want to slightly muck the args */
199 	if (iscsh == UNSET) {
200 		if (p = rindex(shell, '/'))
201 			++p;
202 		else
203 			p = shell;
204 		iscsh = strcmp(p, "csh") ? NO : YES;
205 	}
206 
207 	/* set permissions */
208 	if (setgid(pwd->pw_gid) < 0) {
209 		perror("su: setgid");
210 		exit(1);
211 	}
212 	if (initgroups(user, pwd->pw_gid)) {
213 		(void)fprintf(stderr, "su: initgroups failed.\n");
214 		exit(1);
215 	}
216 	if (setuid(pwd->pw_uid) < 0) {
217 		perror("su: setuid");
218 		exit(1);
219 	}
220 
221 	if (!asme) {
222 		if (asthem) {
223 			p = getenv("TERM");
224 			cleanenv[0] = _PATH_DEFPATH;
225 			cleanenv[1] = NULL;
226 			environ = cleanenv;
227 			(void)setenv("TERM", p, 1);
228 			if (chdir(pwd->pw_dir) < 0) {
229 				fprintf(stderr, "su: no directory\n");
230 				exit(1);
231 			}
232 		}
233 		if (asthem || pwd->pw_uid)
234 			(void)setenv("USER", pwd->pw_name, 1);
235 		(void)setenv("HOME", pwd->pw_dir, 1);
236 		(void)setenv("SHELL", shell, 1);
237 	}
238 
239 	if (iscsh == YES) {
240 		if (fastlogin)
241 			*np-- = "-f";
242 		if (asme)
243 			*np-- = "-m";
244 	}
245 
246 	/* csh strips the first character... */
247 	*np = asthem ? "-su" : iscsh == YES ? "_su" : "su";
248 
249 	if (ruid != 0)
250 		syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
251 		    username, user, ontty());
252 
253 	(void)setpriority(PRIO_PROCESS, 0, prio);
254 
255 	execv(shell, np);
256 	(void)fprintf(stderr, "su: %s not found.\n", shell);
257 	exit(1);
258 }
259 
260 int
261 chshell(sh)
262 	char *sh;
263 {
264 	register char *cp;
265 	char *getusershell();
266 
267 	while ((cp = getusershell()) != NULL)
268 		if (!strcmp(cp, sh))
269 			return (1);
270 	return (0);
271 }
272 
273 char *
274 ontty()
275 {
276 	char *p, *ttyname();
277 	static char buf[MAXPATHLEN + 4];
278 
279 	buf[0] = 0;
280 	if (p = ttyname(STDERR_FILENO))
281 		sprintf(buf, " on %s", p);
282 	return (buf);
283 }
284 
285 #ifdef KERBEROS
286 kerberos(username, user, uid)
287 	char *username, *user;
288 	int uid;
289 {
290 	extern char *krb_err_txt[];
291 	KTEXT_ST ticket;
292 	AUTH_DAT authdata;
293 	struct hostent *hp;
294 	register char *p;
295 	int kerno;
296 	u_long faddr;
297 	char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
298 	char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
299 	char *ontty(), *krb_get_phost();
300 
301 	if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
302 		return (1);
303 	if (koktologin(username, lrealm, user) && !uid) {
304 		(void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
305 		return (1);
306 	}
307 	(void)sprintf(krbtkfile, "%s_%s_%d", TKT_ROOT, user, getuid());
308 
309 	(void)setenv("KRBTKFILE", krbtkfile, 1);
310 	(void)krb_set_tkt_string(krbtkfile);
311 	/*
312 	 * Set real as well as effective ID to 0 for the moment,
313 	 * to make the kerberos library do the right thing.
314 	 */
315 	if (setuid(0) < 0) {
316 		perror("su: setuid");
317 		return (1);
318 	}
319 
320 	/*
321 	 * Little trick here -- if we are su'ing to root,
322 	 * we need to get a ticket for "xxx.root", where xxx represents
323 	 * the name of the person su'ing.  Otherwise (non-root case),
324 	 * we need to get a ticket for "yyy.", where yyy represents
325 	 * the name of the person being su'd to, and the instance is null
326 	 *
327 	 * We should have a way to set the ticket lifetime,
328 	 * with a system default for root.
329 	 */
330 	kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
331 		(uid == 0 ? "root" : ""), lrealm,
332 	    	"krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
333 
334 	if (kerno != KSUCCESS) {
335 		if (kerno == KDC_PR_UNKNOWN) {
336 			fprintf(stderr, "principal unknown: %s.%s@%s\n",
337 				(uid == 0 ? username : user),
338 				(uid == 0 ? "root" : ""), lrealm);
339 			return (1);
340 		}
341 		(void)fprintf(stderr, "su: unable to su: %s\n",
342 		    krb_err_txt[kerno]);
343 		syslog(LOG_NOTICE|LOG_AUTH,
344 		    "BAD Kerberos SU: %s to %s%s: %s",
345 		    username, user, ontty(), krb_err_txt[kerno]);
346 		return (1);
347 	}
348 
349 	if (chown(krbtkfile, uid, -1) < 0) {
350 		perror("su: chown:");
351 		(void)unlink(krbtkfile);
352 		return (1);
353 	}
354 
355 	(void)setpriority(PRIO_PROCESS, 0, -2);
356 
357 	if (gethostname(hostname, sizeof(hostname)) == -1) {
358 		perror("su: gethostname");
359 		dest_tkt();
360 		return (1);
361 	}
362 
363 	(void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
364 	savehost[sizeof(savehost) - 1] = '\0';
365 
366 	kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
367 
368 	if (kerno == KDC_PR_UNKNOWN) {
369 		(void)fprintf(stderr, "Warning: TGT not verified.\n");
370 		syslog(LOG_NOTICE|LOG_AUTH,
371 		    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
372 		    username, user, ontty(), krb_err_txt[kerno],
373 		    "rcmd", savehost);
374 	} else if (kerno != KSUCCESS) {
375 		(void)fprintf(stderr, "Unable to use TGT: %s\n",
376 		    krb_err_txt[kerno]);
377 		syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
378 		    username, user, ontty(), krb_err_txt[kerno]);
379 		dest_tkt();
380 		return (1);
381 	} else {
382 		if (!(hp = gethostbyname(hostname))) {
383 			(void)fprintf(stderr, "su: can't get addr of %s\n",
384 			    hostname);
385 			dest_tkt();
386 			return (1);
387 		}
388 		(void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
389 
390 		if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
391 		    &authdata, "")) != KSUCCESS) {
392 			(void)fprintf(stderr,
393 			    "su: unable to verify rcmd ticket: %s\n",
394 			    krb_err_txt[kerno]);
395 			syslog(LOG_NOTICE|LOG_AUTH,
396 			    "failed su: %s to %s%s: %s", username,
397 			     user, ontty(), krb_err_txt[kerno]);
398 			dest_tkt();
399 			return (1);
400 		}
401 	}
402 	return (0);
403 }
404 
405 koktologin(name, realm, toname)
406 	char *name, *realm, *toname;
407 {
408 	register AUTH_DAT *kdata;
409 	AUTH_DAT kdata_st;
410 
411 	kdata = &kdata_st;
412 	bzero((caddr_t) kdata, sizeof(*kdata));
413 	(void)strcpy(kdata->pname, name);
414 	(void)strcpy(kdata->pinst,
415 	    ((strcmp(toname, "root") == 0) ? "root" : ""));
416 	(void)strcpy(kdata->prealm, realm);
417 	return (kuserok(kdata, toname));
418 }
419 #endif
420