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