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