su.c revision 1.16 1 /* $NetBSD: su.c,v 1.16 1997/03/04 00:21:13 explorer 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 #ifndef lint
37 char copyright[] =
38 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
39 All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";*/
45 #else
46 static char rcsid[] = "$NetBSD: su.c,v 1.16 1997/03/04 00:21:13 explorer Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/resource.h>
53 #include <syslog.h>
54 #include <stdio.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <stdlib.h>
58 #include <pwd.h>
59 #include <grp.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <paths.h>
63
64 #ifdef KERBEROS
65 #include <kerberosIV/des.h>
66 #include <kerberosIV/krb.h>
67 #include <netdb.h>
68
69 #define ARGSTR "-Kflm"
70
71 int use_kerberos = 1;
72
73 static int kerberos __P((char *, char *, int));
74 static int koktologin __P((char *, char *, char *));
75
76 #else
77 #define ARGSTR "-flm"
78 #endif
79
80
81 int main __P((int, char **));
82
83 static int chshell __P((char *));
84 static char *ontty __P((void));
85
86
87 int
88 main(argc, argv)
89 int argc;
90 char **argv;
91 {
92 extern char *__progname;
93 extern char **environ;
94 struct passwd *pwd;
95 char *p, **g;
96 struct group *gr;
97 uid_t ruid;
98 int asme, ch, asthem, fastlogin, prio;
99 enum { UNSET, YES, NO } iscsh = UNSET;
100 char *user, *shell, *avshell, *username, *cleanenv[10], **np;
101 char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
102
103 asme = asthem = fastlogin = 0;
104 while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
105 switch((char)ch) {
106 #ifdef KERBEROS
107 case 'K':
108 use_kerberos = 0;
109 break;
110 #endif
111 case 'f':
112 fastlogin = 1;
113 break;
114 case '-':
115 case 'l':
116 asme = 0;
117 asthem = 1;
118 break;
119 case 'm':
120 asme = 1;
121 asthem = 0;
122 break;
123 case '?':
124 default:
125 (void)fprintf(stderr,
126 "Usage: %s [%s] [login [shell arguments]]\n",
127 __progname, ARGSTR);
128 exit(1);
129 }
130 argv += optind;
131
132 errno = 0;
133 prio = getpriority(PRIO_PROCESS, 0);
134 if (errno)
135 prio = 0;
136 (void)setpriority(PRIO_PROCESS, 0, -2);
137 openlog("su", LOG_CONS, 0);
138
139 /* get current login name and shell */
140 ruid = getuid();
141 username = getlogin();
142 if (username == NULL || (pwd = getpwnam(username)) == NULL ||
143 pwd->pw_uid != ruid)
144 pwd = getpwuid(ruid);
145 if (pwd == NULL) {
146 errx(1, "who are you?");
147 }
148 username = strdup(pwd->pw_name);
149 if (asme)
150 if (pwd->pw_shell && *pwd->pw_shell)
151 shell = strncpy(shellbuf, pwd->pw_shell,
152 sizeof(shellbuf) + 1);
153 else {
154 shell = _PATH_BSHELL;
155 iscsh = NO;
156 }
157
158 /* get target login information, default to root */
159 user = *argv ? *argv : "root";
160 np = *argv ? argv : argv-1;
161
162 if ((pwd = getpwnam(user)) == NULL) {
163 errx(1, "unknown login %s", user);
164 }
165
166 if (ruid) {
167 #ifdef KERBEROS
168 if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
169 #endif
170 {
171 /* only allow those in group zero to su to root, if group
172 * zero has any members. */
173 if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)) &&
174 *gr->gr_mem) {
175 for (g = gr->gr_mem;; ++g) {
176 if (!*g)
177 errx(1,
178 "you are not in the correct group to su %s.",
179 user);
180 if (!strcmp(username, *g))
181 break;
182 }
183 }
184 /* if target requires a password, verify it */
185 if (*pwd->pw_passwd) {
186 p = getpass("Password:");
187 #ifdef SKEY
188 if (strcasecmp(p, "s/key") == 0) {
189 if (skey_haskey(user))
190 errx(1, "Sorry, you have no s/key.");
191 else {
192 if (skey_authenticate(user)) {
193 goto badlogin;
194 }
195 }
196
197 } else
198 #endif
199 if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
200 badlogin:
201 fprintf(stderr, "Sorry\n");
202 syslog(LOG_AUTH|LOG_WARNING,
203 "BAD SU %s to %s%s", username,
204 user, ontty());
205 exit(1);
206 }
207 }
208 }
209 }
210
211 if (asme) {
212 /* if asme and non-standard target shell, must be root */
213 if (!chshell(pwd->pw_shell) && ruid)
214 errx(1,"permission denied (shell).");
215 } else if (pwd->pw_shell && *pwd->pw_shell) {
216 shell = pwd->pw_shell;
217 iscsh = UNSET;
218 } else {
219 shell = _PATH_BSHELL;
220 iscsh = NO;
221 }
222
223 if (p = strrchr(shell, '/'))
224 avshell = p+1;
225 else
226 avshell = shell;
227
228 /* if we're forking a csh, we want to slightly muck the args */
229 if (iscsh == UNSET)
230 iscsh = strstr(avshell, "csh") ? YES : NO;
231
232 /* set permissions */
233 if (setgid(pwd->pw_gid) < 0)
234 err(1, "setgid");
235 if (initgroups(user, pwd->pw_gid))
236 errx(1, "initgroups failed");
237 if (setuid(pwd->pw_uid) < 0)
238 err(1, "setuid");
239
240 if (!asme) {
241 if (asthem) {
242 p = getenv("TERM");
243 cleanenv[0] = NULL;
244 environ = cleanenv;
245 (void)setenv("PATH", _PATH_DEFPATH, 1);
246 if (p)
247 (void)setenv("TERM", p, 1);
248 if (chdir(pwd->pw_dir) < 0)
249 errx(1, "no directory");
250 }
251 if (asthem || pwd->pw_uid)
252 (void)setenv("USER", pwd->pw_name, 1);
253 (void)setenv("HOME", pwd->pw_dir, 1);
254 (void)setenv("SHELL", shell, 1);
255 }
256
257 if (iscsh == YES) {
258 if (fastlogin)
259 *np-- = "-f";
260 if (asme)
261 *np-- = "-m";
262 }
263
264 if (asthem) {
265 avshellbuf[0] = '-';
266 (void)strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
267 avshell = avshellbuf;
268 } else if (iscsh == YES) {
269 /* csh strips the first character... */
270 avshellbuf[0] = '_';
271 (void)strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
272 avshell = avshellbuf;
273 }
274 *np = avshell;
275
276 if (ruid != 0)
277 syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
278 username, user, ontty());
279
280 (void)setpriority(PRIO_PROCESS, 0, prio);
281
282 execv(shell, np);
283 err(1, "%s", shell);
284 }
285
286 static int
287 chshell(sh)
288 char *sh;
289 {
290 char *cp;
291 char *getusershell();
292
293 while ((cp = getusershell()) != NULL)
294 if (!strcmp(cp, sh))
295 return (1);
296 return (0);
297 }
298
299 static char *
300 ontty()
301 {
302 char *p, *ttyname();
303 static char buf[MAXPATHLEN + 4];
304
305 buf[0] = 0;
306 if (p = ttyname(STDERR_FILENO))
307 (void)snprintf(buf, sizeof buf, " on %s", p);
308 return (buf);
309 }
310
311 #ifdef KERBEROS
312 static int
313 kerberos(username, user, uid)
314 char *username, *user;
315 int uid;
316 {
317 KTEXT_ST ticket;
318 AUTH_DAT authdata;
319 struct hostent *hp;
320 register char *p;
321 int kerno;
322 u_long faddr;
323 char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
324 char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
325 char *ontty(), *krb_get_phost();
326
327 if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
328 return (1);
329 if (koktologin(username, lrealm, user) && !uid) {
330 warnx("kerberos: not in %s's ACL.", user);
331 return (1);
332 }
333 (void)(void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
334 user, getuid());
335
336 (void)setenv("KRBTKFILE", krbtkfile, 1);
337 (void)krb_set_tkt_string(krbtkfile);
338 /*
339 * Set real as well as effective ID to 0 for the moment,
340 * to make the kerberos library do the right thing.
341 */
342 if (setuid(0) < 0) {
343 warn("setuid");
344 return (1);
345 }
346
347 /*
348 * Little trick here -- if we are su'ing to root,
349 * we need to get a ticket for "xxx.root", where xxx represents
350 * the name of the person su'ing. Otherwise (non-root case),
351 * we need to get a ticket for "yyy.", where yyy represents
352 * the name of the person being su'd to, and the instance is null
353 *
354 * We should have a way to set the ticket lifetime,
355 * with a system default for root.
356 */
357 kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
358 (uid == 0 ? "root" : ""), lrealm,
359 "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
360
361 if (kerno != KSUCCESS) {
362 if (kerno == KDC_PR_UNKNOWN) {
363 warnx("kerberos: principal unknown: %s.%s@%s",
364 (uid == 0 ? username : user),
365 (uid == 0 ? "root" : ""), lrealm);
366 return (1);
367 }
368 warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
369 syslog(LOG_NOTICE|LOG_AUTH,
370 "BAD Kerberos SU: %s to %s%s: %s",
371 username, user, ontty(), krb_err_txt[kerno]);
372 return (1);
373 }
374
375 if (chown(krbtkfile, uid, -1) < 0) {
376 warn("chown");
377 (void)unlink(krbtkfile);
378 return (1);
379 }
380
381 (void)setpriority(PRIO_PROCESS, 0, -2);
382
383 if (gethostname(hostname, sizeof(hostname)) == -1) {
384 warn("gethostname");
385 dest_tkt();
386 return (1);
387 }
388
389 (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
390 savehost[sizeof(savehost) - 1] = '\0';
391
392 kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
393
394 if (kerno == KDC_PR_UNKNOWN) {
395 warnx("Warning: TGT not verified.");
396 syslog(LOG_NOTICE|LOG_AUTH,
397 "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
398 username, user, ontty(), krb_err_txt[kerno],
399 "rcmd", savehost);
400 } else if (kerno != KSUCCESS) {
401 warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
402 syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
403 username, user, ontty(), krb_err_txt[kerno]);
404 dest_tkt();
405 return (1);
406 } else {
407 if (!(hp = gethostbyname(hostname))) {
408 warnx("can't get addr of %s", hostname);
409 dest_tkt();
410 return (1);
411 }
412 memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
413
414 if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
415 &authdata, "")) != KSUCCESS) {
416 warnx("kerberos: unable to verify rcmd ticket: %s\n",
417 krb_err_txt[kerno]);
418 syslog(LOG_NOTICE|LOG_AUTH,
419 "failed su: %s to %s%s: %s", username,
420 user, ontty(), krb_err_txt[kerno]);
421 dest_tkt();
422 return (1);
423 }
424 }
425 return (0);
426 }
427
428 static int
429 koktologin(name, realm, toname)
430 char *name, *realm, *toname;
431 {
432 register AUTH_DAT *kdata;
433 AUTH_DAT kdata_st;
434
435 kdata = &kdata_st;
436 memset((char *)kdata, 0, sizeof(*kdata));
437 (void)strncpy(kdata->pname, name, sizeof(kdata->pname) - 1);
438 (void)strncpy(kdata->pinst,
439 ((strcmp(toname, "root") == 0) ? "root" : ""), sizeof(kdata->pinst) - 1);
440 (void)strncpy(kdata->prealm, realm, sizeof(kdata->prealm) - 1);
441 return (kuserok(kdata, toname));
442 }
443 #endif
444