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