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