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