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