su.c revision 1.21 1 /* $NetBSD: su.c,v 1.21 1998/04/02 11:13:33 kleink 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.21 1998/04/02 11:13:33 kleink 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], savehost[MAXHOSTNAMELEN];
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
422 (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
423 savehost[sizeof(savehost) - 1] = '\0';
424
425 kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
426
427 if (kerno == KDC_PR_UNKNOWN) {
428 warnx("Warning: TGT not verified.");
429 syslog(LOG_NOTICE|LOG_AUTH,
430 "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
431 username, user, ontty(), krb_err_txt[kerno],
432 "rcmd", savehost);
433 } else if (kerno != KSUCCESS) {
434 warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
435 syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
436 username, user, ontty(), krb_err_txt[kerno]);
437 dest_tkt();
438 return (1);
439 } else {
440 if (!(hp = gethostbyname(hostname))) {
441 warnx("can't get addr of %s", hostname);
442 dest_tkt();
443 return (1);
444 }
445 memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
446
447 if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
448 &authdata, "")) != KSUCCESS) {
449 warnx("kerberos: unable to verify rcmd ticket: %s\n",
450 krb_err_txt[kerno]);
451 syslog(LOG_NOTICE|LOG_AUTH,
452 "failed su: %s to %s%s: %s", username,
453 user, ontty(), krb_err_txt[kerno]);
454 dest_tkt();
455 return (1);
456 }
457 }
458 return (0);
459 }
460
461 static int
462 koktologin(name, realm, toname)
463 char *name, *realm, *toname;
464 {
465 AUTH_DAT *kdata;
466 AUTH_DAT kdata_st;
467
468 kdata = &kdata_st;
469 memset((char *)kdata, 0, sizeof(*kdata));
470 (void)strncpy(kdata->pname, name, sizeof(kdata->pname) - 1);
471 (void)strncpy(kdata->pinst,
472 ((strcmp(toname, "root") == 0) ? "root" : ""), sizeof(kdata->pinst) - 1);
473 (void)strncpy(kdata->prealm, realm, sizeof(kdata->prealm) - 1);
474 return (kuserok(kdata, toname));
475 }
476 #endif
477