su.c revision 1.42 1 1.42 assar /* $NetBSD: su.c,v 1.42 2000/07/13 08:37:10 assar Exp $ */
2 1.12 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988 The Regents of the University of California.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.19 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.19 lukem __COPYRIGHT(
39 1.19 lukem "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
40 1.19 lukem All rights reserved.\n");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.12 christos #if 0
45 1.13 tls static char sccsid[] = "@(#)su.c 8.3 (Berkeley) 4/2/94";*/
46 1.12 christos #else
47 1.42 assar __RCSID("$NetBSD: su.c,v 1.42 2000/07/13 08:37:10 assar Exp $");
48 1.12 christos #endif
49 1.1 cgd #endif /* not lint */
50 1.1 cgd
51 1.1 cgd #include <sys/param.h>
52 1.1 cgd #include <sys/time.h>
53 1.1 cgd #include <sys/resource.h>
54 1.13 tls #include <err.h>
55 1.11 christos #include <errno.h>
56 1.17 lukem #include <grp.h>
57 1.17 lukem #include <paths.h>
58 1.17 lukem #include <pwd.h>
59 1.17 lukem #include <stdio.h>
60 1.27 wsanchez #ifdef SKEY
61 1.17 lukem #include <skey.h>
62 1.27 wsanchez #endif
63 1.7 jtc #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.17 lukem #include <syslog.h>
66 1.21 kleink #include <time.h>
67 1.17 lukem #include <tzfile.h>
68 1.1 cgd #include <unistd.h>
69 1.1 cgd
70 1.37 mjl #ifdef LOGIN_CAP
71 1.37 mjl #include <login_cap.h>
72 1.37 mjl #endif
73 1.37 mjl
74 1.1 cgd #ifdef KERBEROS
75 1.41 assar #include <des.h>
76 1.41 assar #include <krb.h>
77 1.1 cgd #include <netdb.h>
78 1.1 cgd
79 1.41 assar static int kerberos __P((char *, char *, int));
80 1.41 assar static int koktologin __P((char *, char *, char *));
81 1.41 assar
82 1.41 assar #endif
83 1.41 assar
84 1.41 assar #ifdef KERBEROS5
85 1.41 assar #include <krb5.h>
86 1.41 assar
87 1.41 assar static int kerberos5 __P((char *, char *, int));
88 1.41 assar
89 1.41 assar #endif
90 1.41 assar
91 1.41 assar #if defined(KERBEROS) || defined(KERBEROS5)
92 1.41 assar
93 1.37 mjl #define ARGSTRX "-Kflm"
94 1.1 cgd
95 1.1 cgd int use_kerberos = 1;
96 1.11 christos
97 1.1 cgd #else
98 1.37 mjl #define ARGSTRX "-flm"
99 1.1 cgd #endif
100 1.1 cgd
101 1.18 lukem #ifndef SUGROUP
102 1.18 lukem #define SUGROUP "wheel"
103 1.18 lukem #endif
104 1.18 lukem
105 1.37 mjl #ifdef LOGIN_CAP
106 1.37 mjl #define ARGSTR ARGSTRX "c:"
107 1.37 mjl #else
108 1.37 mjl #define ARGSTR ARGSTRX
109 1.37 mjl #endif
110 1.37 mjl
111 1.11 christos int main __P((int, char **));
112 1.11 christos
113 1.25 mycroft static int chshell __P((const char *));
114 1.11 christos static char *ontty __P((void));
115 1.11 christos
116 1.2 sef
117 1.7 jtc int
118 1.1 cgd main(argc, argv)
119 1.1 cgd int argc;
120 1.1 cgd char **argv;
121 1.1 cgd {
122 1.11 christos extern char *__progname;
123 1.1 cgd extern char **environ;
124 1.13 tls struct passwd *pwd;
125 1.17 lukem char *p;
126 1.1 cgd struct group *gr;
127 1.28 christos #ifdef BSD4_4
128 1.17 lukem struct timeval tp;
129 1.28 christos #endif
130 1.11 christos uid_t ruid;
131 1.1 cgd int asme, ch, asthem, fastlogin, prio;
132 1.1 cgd enum { UNSET, YES, NO } iscsh = UNSET;
133 1.35 christos char *user, *shell, *avshell, *username, **np;
134 1.37 mjl char *userpass, *class;
135 1.24 mrg char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
136 1.37 mjl time_t pw_warntime = _PASSWORD_WARNDAYS * SECSPERDAY;
137 1.37 mjl #ifdef LOGIN_CAP
138 1.37 mjl login_cap_t *lc;
139 1.37 mjl #endif
140 1.1 cgd
141 1.1 cgd asme = asthem = fastlogin = 0;
142 1.37 mjl shell = class = NULL;
143 1.19 lukem while ((ch = getopt(argc, argv, ARGSTR)) != -1)
144 1.1 cgd switch((char)ch) {
145 1.41 assar #if defined(KERBEROS) || defined(KERBEROS5)
146 1.1 cgd case 'K':
147 1.1 cgd use_kerberos = 0;
148 1.1 cgd break;
149 1.1 cgd #endif
150 1.37 mjl #ifdef LOGIN_CAP
151 1.37 mjl case 'c':
152 1.37 mjl class = optarg;
153 1.37 mjl break;
154 1.37 mjl #endif
155 1.1 cgd case 'f':
156 1.1 cgd fastlogin = 1;
157 1.1 cgd break;
158 1.1 cgd case '-':
159 1.1 cgd case 'l':
160 1.1 cgd asme = 0;
161 1.1 cgd asthem = 1;
162 1.1 cgd break;
163 1.1 cgd case 'm':
164 1.1 cgd asme = 1;
165 1.1 cgd asthem = 0;
166 1.1 cgd break;
167 1.1 cgd case '?':
168 1.1 cgd default:
169 1.11 christos (void)fprintf(stderr,
170 1.11 christos "Usage: %s [%s] [login [shell arguments]]\n",
171 1.11 christos __progname, ARGSTR);
172 1.1 cgd exit(1);
173 1.1 cgd }
174 1.1 cgd argv += optind;
175 1.30 christos
176 1.1 cgd errno = 0;
177 1.1 cgd prio = getpriority(PRIO_PROCESS, 0);
178 1.1 cgd if (errno)
179 1.1 cgd prio = 0;
180 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -2);
181 1.1 cgd openlog("su", LOG_CONS, 0);
182 1.1 cgd
183 1.1 cgd /* get current login name and shell */
184 1.1 cgd ruid = getuid();
185 1.1 cgd username = getlogin();
186 1.1 cgd if (username == NULL || (pwd = getpwnam(username)) == NULL ||
187 1.1 cgd pwd->pw_uid != ruid)
188 1.1 cgd pwd = getpwuid(ruid);
189 1.23 mrg if (pwd == NULL)
190 1.13 tls errx(1, "who are you?");
191 1.1 cgd username = strdup(pwd->pw_name);
192 1.30 christos userpass = strdup(pwd->pw_passwd);
193 1.30 christos if (username == NULL || userpass == NULL)
194 1.23 mrg err(1, "strdup");
195 1.1 cgd
196 1.37 mjl
197 1.26 ross if (asme) {
198 1.24 mrg if (pwd->pw_shell && *pwd->pw_shell) {
199 1.24 mrg shell = strncpy(shellbuf, pwd->pw_shell,
200 1.24 mrg sizeof(shellbuf) - 1);
201 1.24 mrg shellbuf[sizeof(shellbuf) - 1] = '\0';
202 1.24 mrg } else {
203 1.24 mrg shell = _PATH_BSHELL;
204 1.24 mrg iscsh = NO;
205 1.24 mrg }
206 1.26 ross }
207 1.1 cgd /* get target login information, default to root */
208 1.1 cgd user = *argv ? *argv : "root";
209 1.2 sef np = *argv ? argv : argv-1;
210 1.2 sef
211 1.23 mrg if ((pwd = getpwnam(user)) == NULL)
212 1.13 tls errx(1, "unknown login %s", user);
213 1.37 mjl
214 1.37 mjl #ifdef LOGIN_CAP
215 1.37 mjl /* force the usage of specified class */
216 1.37 mjl if (class) {
217 1.37 mjl if (ruid)
218 1.37 mjl errx(1, "Only root may use -c");
219 1.37 mjl
220 1.37 mjl pwd->pw_class = class;
221 1.37 mjl }
222 1.37 mjl lc = login_getclass(pwd->pw_class);
223 1.37 mjl
224 1.37 mjl pw_warntime = login_getcaptime(lc, "password-warn",
225 1.37 mjl _PASSWORD_WARNDAYS * SECSPERDAY,
226 1.37 mjl _PASSWORD_WARNDAYS * SECSPERDAY);
227 1.37 mjl #endif
228 1.37 mjl
229 1.24 mrg if (ruid
230 1.41 assar #ifdef KERBEROS5
231 1.41 assar && (!use_kerberos || kerberos5(username, user, pwd->pw_uid))
232 1.41 assar #endif
233 1.1 cgd #ifdef KERBEROS
234 1.24 mrg && (!use_kerberos || kerberos(username, user, pwd->pw_uid))
235 1.1 cgd #endif
236 1.24 mrg ) {
237 1.30 christos char *pass = pwd->pw_passwd;
238 1.30 christos int ok = pwd->pw_uid != 0;
239 1.30 christos char **g;
240 1.30 christos
241 1.34 kim #ifdef ROOTAUTH
242 1.34 kim /*
243 1.34 kim * Allow those in group rootauth to su to root, by supplying
244 1.34 kim * their own password.
245 1.34 kim */
246 1.34 kim if (!ok && (gr = getgrnam(ROOTAUTH)))
247 1.34 kim for (g = gr->gr_mem;; ++g) {
248 1.34 kim if (!*g) {
249 1.34 kim ok = 0;
250 1.34 kim break;
251 1.34 kim }
252 1.34 kim if (!strcmp(username, *g)) {
253 1.34 kim pass = userpass;
254 1.34 kim user = username;
255 1.34 kim ok = 1;
256 1.34 kim break;
257 1.34 kim }
258 1.34 kim }
259 1.34 kim #endif
260 1.24 mrg /*
261 1.24 mrg * Only allow those in group SUGROUP to su to root,
262 1.24 mrg * but only if that group has any members.
263 1.24 mrg * If SUGROUP has no members, allow anyone to su root
264 1.24 mrg */
265 1.33 abs if (!ok) {
266 1.33 abs if ( !(gr = getgrnam(SUGROUP)) || !*gr->gr_mem)
267 1.33 abs ok = 1;
268 1.33 abs else
269 1.33 abs for (g = gr->gr_mem; ; g++) {
270 1.33 abs if (*g == NULL) {
271 1.33 abs ok = 0;
272 1.33 abs break;
273 1.33 abs }
274 1.33 abs if (strcmp(username, *g) == 0) {
275 1.33 abs ok = 1;
276 1.33 abs break;
277 1.33 abs }
278 1.30 christos }
279 1.1 cgd }
280 1.30 christos if (!ok)
281 1.30 christos errx(1,
282 1.30 christos "you are not listed in the correct secondary group (%s) to su %s.",
283 1.30 christos SUGROUP, user);
284 1.1 cgd /* if target requires a password, verify it */
285 1.30 christos if (*pass) {
286 1.1 cgd p = getpass("Password:");
287 1.10 deraadt #ifdef SKEY
288 1.10 deraadt if (strcasecmp(p, "s/key") == 0) {
289 1.13 tls if (skey_haskey(user))
290 1.13 tls errx(1, "Sorry, you have no s/key.");
291 1.13 tls else {
292 1.10 deraadt if (skey_authenticate(user)) {
293 1.10 deraadt goto badlogin;
294 1.10 deraadt }
295 1.10 deraadt }
296 1.10 deraadt
297 1.10 deraadt } else
298 1.10 deraadt #endif
299 1.30 christos if (strcmp(pass, crypt(p, pass))) {
300 1.31 christos #ifdef SKEY
301 1.10 deraadt badlogin:
302 1.27 wsanchez #endif
303 1.1 cgd fprintf(stderr, "Sorry\n");
304 1.1 cgd syslog(LOG_AUTH|LOG_WARNING,
305 1.1 cgd "BAD SU %s to %s%s", username,
306 1.30 christos pwd->pw_name, ontty());
307 1.1 cgd exit(1);
308 1.1 cgd }
309 1.1 cgd }
310 1.1 cgd }
311 1.1 cgd
312 1.1 cgd if (asme) {
313 1.1 cgd /* if asme and non-standard target shell, must be root */
314 1.13 tls if (!chshell(pwd->pw_shell) && ruid)
315 1.13 tls errx(1,"permission denied (shell).");
316 1.1 cgd } else if (pwd->pw_shell && *pwd->pw_shell) {
317 1.1 cgd shell = pwd->pw_shell;
318 1.1 cgd iscsh = UNSET;
319 1.1 cgd } else {
320 1.1 cgd shell = _PATH_BSHELL;
321 1.1 cgd iscsh = NO;
322 1.1 cgd }
323 1.1 cgd
324 1.17 lukem if ((p = strrchr(shell, '/')) != NULL)
325 1.9 cgd avshell = p+1;
326 1.9 cgd else
327 1.9 cgd avshell = shell;
328 1.9 cgd
329 1.1 cgd /* if we're forking a csh, we want to slightly muck the args */
330 1.9 cgd if (iscsh == UNSET)
331 1.11 christos iscsh = strstr(avshell, "csh") ? YES : NO;
332 1.1 cgd
333 1.37 mjl #ifndef LOGIN_CAP /* This is done by setusercontext() */
334 1.1 cgd /* set permissions */
335 1.13 tls if (setgid(pwd->pw_gid) < 0)
336 1.13 tls err(1, "setgid");
337 1.13 tls if (initgroups(user, pwd->pw_gid))
338 1.13 tls errx(1, "initgroups failed");
339 1.13 tls if (setuid(pwd->pw_uid) < 0)
340 1.13 tls err(1, "setuid");
341 1.37 mjl #endif
342 1.1 cgd
343 1.1 cgd if (!asme) {
344 1.1 cgd if (asthem) {
345 1.1 cgd p = getenv("TERM");
346 1.35 christos /* Create an empty environment */
347 1.35 christos if ((environ = malloc(sizeof(char *))) == NULL)
348 1.36 drochner err(1, NULL);
349 1.35 christos environ[0] = NULL;
350 1.37 mjl #ifdef LOGIN_CAP
351 1.37 mjl if (setusercontext(lc,
352 1.37 mjl pwd, pwd->pw_uid, LOGIN_SETPATH))
353 1.37 mjl err(1, "setting user context");
354 1.37 mjl #else
355 1.8 mycroft (void)setenv("PATH", _PATH_DEFPATH, 1);
356 1.37 mjl #endif
357 1.11 christos if (p)
358 1.11 christos (void)setenv("TERM", p, 1);
359 1.13 tls if (chdir(pwd->pw_dir) < 0)
360 1.13 tls errx(1, "no directory");
361 1.37 mjl }
362 1.38 mjl
363 1.5 jtc if (asthem || pwd->pw_uid)
364 1.1 cgd (void)setenv("USER", pwd->pw_name, 1);
365 1.1 cgd (void)setenv("HOME", pwd->pw_dir, 1);
366 1.1 cgd (void)setenv("SHELL", shell, 1);
367 1.1 cgd }
368 1.39 abs (void)setenv("SU_FROM", username, 1);
369 1.1 cgd
370 1.12 christos if (iscsh == YES) {
371 1.12 christos if (fastlogin)
372 1.12 christos *np-- = "-f";
373 1.12 christos if (asme)
374 1.12 christos *np-- = "-m";
375 1.12 christos }
376 1.1 cgd
377 1.8 mycroft if (asthem) {
378 1.8 mycroft avshellbuf[0] = '-';
379 1.15 mrg (void)strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
380 1.8 mycroft avshell = avshellbuf;
381 1.8 mycroft } else if (iscsh == YES) {
382 1.8 mycroft /* csh strips the first character... */
383 1.8 mycroft avshellbuf[0] = '_';
384 1.15 mrg (void)strncpy(avshellbuf+1, avshell, sizeof(avshellbuf) - 2);
385 1.8 mycroft avshell = avshellbuf;
386 1.8 mycroft }
387 1.12 christos *np = avshell;
388 1.11 christos
389 1.28 christos #ifdef BSD4_4
390 1.17 lukem if (pwd->pw_change || pwd->pw_expire)
391 1.17 lukem (void)gettimeofday(&tp, (struct timezone *)NULL);
392 1.26 ross if (pwd->pw_change) {
393 1.17 lukem if (tp.tv_sec >= pwd->pw_change) {
394 1.17 lukem (void)printf("%s -- %s's password has expired.\n",
395 1.17 lukem (ruid ? "Sorry" : "Note"), user);
396 1.17 lukem if (ruid != 0)
397 1.17 lukem exit(1);
398 1.37 mjl } else if (pwd->pw_change - tp.tv_sec < pw_warntime)
399 1.17 lukem (void)printf("Warning: %s's password expires on %s",
400 1.17 lukem user, ctime(&pwd->pw_change));
401 1.26 ross }
402 1.26 ross if (pwd->pw_expire) {
403 1.17 lukem if (tp.tv_sec >= pwd->pw_expire) {
404 1.17 lukem (void)printf("%s -- %s's account has expired.\n",
405 1.17 lukem (ruid ? "Sorry" : "Note"), user);
406 1.17 lukem if (ruid != 0)
407 1.17 lukem exit(1);
408 1.17 lukem } else if (pwd->pw_expire - tp.tv_sec <
409 1.17 lukem _PASSWORD_WARNDAYS * SECSPERDAY)
410 1.17 lukem (void)printf("Warning: %s's account expires on %s",
411 1.17 lukem user, ctime(&pwd->pw_expire));
412 1.26 ross }
413 1.28 christos #endif
414 1.1 cgd if (ruid != 0)
415 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
416 1.30 christos username, pwd->pw_name, ontty());
417 1.1 cgd
418 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, prio);
419 1.37 mjl #ifdef LOGIN_CAP
420 1.37 mjl if (setusercontext(lc, pwd, pwd->pw_uid,
421 1.37 mjl (asthem ? (LOGIN_SETPRIORITY | LOGIN_SETUMASK) : 0) |
422 1.37 mjl LOGIN_SETRESOURCES | LOGIN_SETGROUP | LOGIN_SETUSER))
423 1.37 mjl err(1, "setting user context");
424 1.37 mjl #endif
425 1.1 cgd
426 1.12 christos execv(shell, np);
427 1.13 tls err(1, "%s", shell);
428 1.27 wsanchez /* NOTREACHED */
429 1.1 cgd }
430 1.11 christos
431 1.11 christos static int
432 1.1 cgd chshell(sh)
433 1.25 mycroft const char *sh;
434 1.1 cgd {
435 1.25 mycroft const char *cp;
436 1.1 cgd
437 1.1 cgd while ((cp = getusershell()) != NULL)
438 1.1 cgd if (!strcmp(cp, sh))
439 1.1 cgd return (1);
440 1.1 cgd return (0);
441 1.1 cgd }
442 1.1 cgd
443 1.11 christos static char *
444 1.1 cgd ontty()
445 1.1 cgd {
446 1.19 lukem char *p;
447 1.1 cgd static char buf[MAXPATHLEN + 4];
448 1.1 cgd
449 1.1 cgd buf[0] = 0;
450 1.17 lukem if ((p = ttyname(STDERR_FILENO)) != NULL)
451 1.15 mrg (void)snprintf(buf, sizeof buf, " on %s", p);
452 1.1 cgd return (buf);
453 1.1 cgd }
454 1.41 assar
455 1.41 assar #ifdef KERBEROS5
456 1.41 assar static int
457 1.41 assar kerberos5(username, user, uid)
458 1.41 assar char *username, *user;
459 1.41 assar int uid;
460 1.41 assar {
461 1.41 assar krb5_error_code ret;
462 1.41 assar krb5_context context;
463 1.41 assar krb5_principal princ = NULL;
464 1.41 assar krb5_ccache ccache, ccache2;
465 1.41 assar char *cc_name;
466 1.41 assar
467 1.41 assar ret = krb5_init_context(&context);
468 1.41 assar if (ret)
469 1.41 assar return (1);
470 1.41 assar
471 1.41 assar if (strcmp (user, "root") == 0)
472 1.41 assar ret = krb5_make_principal(context, &princ,
473 1.41 assar NULL, username, "root", NULL);
474 1.41 assar else
475 1.41 assar ret = krb5_make_principal(context, &princ,
476 1.42 assar NULL, user, NULL);
477 1.41 assar if (ret)
478 1.41 assar goto fail;
479 1.41 assar if (!krb5_kuserok(context, princ, user) && !uid) {
480 1.41 assar warnx ("kerberos5: not in %s's ACL.", user);
481 1.41 assar goto fail;
482 1.41 assar }
483 1.41 assar ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
484 1.41 assar if (ret)
485 1.41 assar goto fail;
486 1.41 assar ret = krb5_verify_user_lrealm(context, princ, ccache, NULL, TRUE,
487 1.41 assar NULL);
488 1.41 assar if (ret) {
489 1.41 assar krb5_cc_destroy(context, ccache);
490 1.41 assar switch (ret) {
491 1.41 assar case KRB5_LIBOS_PWDINTR :
492 1.41 assar break;
493 1.41 assar case KRB5KRB_AP_ERR_BAD_INTEGRITY:
494 1.41 assar case KRB5KRB_AP_ERR_MODIFIED:
495 1.41 assar krb5_warnx(context, "Password incorrect");
496 1.41 assar break;
497 1.41 assar default :
498 1.41 assar krb5_warn(context, ret, "krb5_verify_user");
499 1.41 assar break;
500 1.41 assar }
501 1.41 assar goto fail;
502 1.41 assar }
503 1.41 assar ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
504 1.41 assar if (ret) {
505 1.41 assar krb5_cc_destroy(context, ccache);
506 1.41 assar goto fail;
507 1.41 assar }
508 1.41 assar ret = krb5_cc_copy_cache(context, ccache, ccache2);
509 1.41 assar if (ret) {
510 1.41 assar krb5_cc_destroy(context, ccache);
511 1.41 assar krb5_cc_destroy(context, ccache2);
512 1.41 assar goto fail;
513 1.41 assar }
514 1.41 assar
515 1.41 assar asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
516 1.41 assar krb5_cc_get_name(context, ccache2));
517 1.41 assar setenv("KRB5CCNAME", cc_name, 1);
518 1.41 assar free(cc_name);
519 1.41 assar krb5_cc_close(context, ccache2);
520 1.41 assar krb5_cc_destroy(context, ccache);
521 1.41 assar return 0;
522 1.41 assar
523 1.41 assar fail:
524 1.41 assar if (princ != NULL)
525 1.41 assar krb5_free_principal (context, princ);
526 1.41 assar krb5_free_context (context);
527 1.41 assar return (1);
528 1.41 assar }
529 1.41 assar #endif
530 1.1 cgd
531 1.1 cgd #ifdef KERBEROS
532 1.11 christos static int
533 1.1 cgd kerberos(username, user, uid)
534 1.1 cgd char *username, *user;
535 1.1 cgd int uid;
536 1.1 cgd {
537 1.1 cgd KTEXT_ST ticket;
538 1.1 cgd AUTH_DAT authdata;
539 1.1 cgd struct hostent *hp;
540 1.1 cgd int kerno;
541 1.1 cgd u_long faddr;
542 1.1 cgd char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
543 1.22 mrg char hostname[MAXHOSTNAMELEN + 1], savehost[MAXHOSTNAMELEN + 1];
544 1.1 cgd
545 1.40 assar if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
546 1.1 cgd return (1);
547 1.1 cgd if (koktologin(username, lrealm, user) && !uid) {
548 1.13 tls warnx("kerberos: not in %s's ACL.", user);
549 1.1 cgd return (1);
550 1.1 cgd }
551 1.29 scottr (void)snprintf(krbtkfile, sizeof krbtkfile, "%s_%s_%d", TKT_ROOT,
552 1.15 mrg user, getuid());
553 1.1 cgd
554 1.1 cgd (void)setenv("KRBTKFILE", krbtkfile, 1);
555 1.1 cgd (void)krb_set_tkt_string(krbtkfile);
556 1.1 cgd /*
557 1.1 cgd * Set real as well as effective ID to 0 for the moment,
558 1.1 cgd * to make the kerberos library do the right thing.
559 1.1 cgd */
560 1.1 cgd if (setuid(0) < 0) {
561 1.13 tls warn("setuid");
562 1.1 cgd return (1);
563 1.1 cgd }
564 1.1 cgd
565 1.1 cgd /*
566 1.1 cgd * Little trick here -- if we are su'ing to root,
567 1.1 cgd * we need to get a ticket for "xxx.root", where xxx represents
568 1.1 cgd * the name of the person su'ing. Otherwise (non-root case),
569 1.1 cgd * we need to get a ticket for "yyy.", where yyy represents
570 1.1 cgd * the name of the person being su'd to, and the instance is null
571 1.1 cgd *
572 1.1 cgd * We should have a way to set the ticket lifetime,
573 1.1 cgd * with a system default for root.
574 1.1 cgd */
575 1.40 assar {
576 1.40 assar char prompt[128];
577 1.40 assar char passw[256];
578 1.40 assar
579 1.40 assar (void)snprintf (prompt, sizeof(prompt),
580 1.40 assar "%s's Password: ",
581 1.40 assar krb_unparse_name_long ((uid == 0 ? username : user),
582 1.40 assar (uid == 0 ? "root" : ""),
583 1.40 assar lrealm));
584 1.40 assar if (des_read_pw_string (passw, sizeof (passw), prompt, 0)) {
585 1.40 assar memset (passw, 0, sizeof (passw));
586 1.40 assar return (1);
587 1.40 assar }
588 1.40 assar if (strlen(passw) == 0)
589 1.40 assar return (1); /* Empty passwords are not allowed */
590 1.40 assar
591 1.40 assar kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
592 1.40 assar (uid == 0 ? "root" : ""), lrealm,
593 1.40 assar KRB_TICKET_GRANTING_TICKET,
594 1.40 assar lrealm,
595 1.40 assar DEFAULT_TKT_LIFE,
596 1.40 assar passw);
597 1.40 assar memset (passw, 0, strlen (passw));
598 1.40 assar }
599 1.1 cgd
600 1.1 cgd if (kerno != KSUCCESS) {
601 1.1 cgd if (kerno == KDC_PR_UNKNOWN) {
602 1.13 tls warnx("kerberos: principal unknown: %s.%s@%s",
603 1.1 cgd (uid == 0 ? username : user),
604 1.1 cgd (uid == 0 ? "root" : ""), lrealm);
605 1.1 cgd return (1);
606 1.1 cgd }
607 1.13 tls warnx("kerberos: unable to su: %s", krb_err_txt[kerno]);
608 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
609 1.1 cgd "BAD Kerberos SU: %s to %s%s: %s",
610 1.1 cgd username, user, ontty(), krb_err_txt[kerno]);
611 1.1 cgd return (1);
612 1.1 cgd }
613 1.1 cgd
614 1.1 cgd if (chown(krbtkfile, uid, -1) < 0) {
615 1.13 tls warn("chown");
616 1.1 cgd (void)unlink(krbtkfile);
617 1.1 cgd return (1);
618 1.1 cgd }
619 1.1 cgd
620 1.1 cgd (void)setpriority(PRIO_PROCESS, 0, -2);
621 1.1 cgd
622 1.1 cgd if (gethostname(hostname, sizeof(hostname)) == -1) {
623 1.13 tls warn("gethostname");
624 1.1 cgd dest_tkt();
625 1.1 cgd return (1);
626 1.1 cgd }
627 1.22 mrg hostname[sizeof(hostname) - 1] = '\0';
628 1.1 cgd
629 1.40 assar (void)strlcpy(savehost, krb_get_phost(hostname), sizeof(savehost));
630 1.1 cgd savehost[sizeof(savehost) - 1] = '\0';
631 1.1 cgd
632 1.1 cgd kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
633 1.1 cgd
634 1.1 cgd if (kerno == KDC_PR_UNKNOWN) {
635 1.13 tls warnx("Warning: TGT not verified.");
636 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
637 1.1 cgd "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
638 1.1 cgd username, user, ontty(), krb_err_txt[kerno],
639 1.1 cgd "rcmd", savehost);
640 1.1 cgd } else if (kerno != KSUCCESS) {
641 1.13 tls warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
642 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
643 1.1 cgd username, user, ontty(), krb_err_txt[kerno]);
644 1.1 cgd dest_tkt();
645 1.1 cgd return (1);
646 1.1 cgd } else {
647 1.1 cgd if (!(hp = gethostbyname(hostname))) {
648 1.13 tls warnx("can't get addr of %s", hostname);
649 1.1 cgd dest_tkt();
650 1.1 cgd return (1);
651 1.1 cgd }
652 1.13 tls memmove((char *)&faddr, (char *)hp->h_addr, sizeof(faddr));
653 1.1 cgd
654 1.1 cgd if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
655 1.1 cgd &authdata, "")) != KSUCCESS) {
656 1.13 tls warnx("kerberos: unable to verify rcmd ticket: %s\n",
657 1.1 cgd krb_err_txt[kerno]);
658 1.1 cgd syslog(LOG_NOTICE|LOG_AUTH,
659 1.1 cgd "failed su: %s to %s%s: %s", username,
660 1.1 cgd user, ontty(), krb_err_txt[kerno]);
661 1.1 cgd dest_tkt();
662 1.1 cgd return (1);
663 1.1 cgd }
664 1.1 cgd }
665 1.1 cgd return (0);
666 1.1 cgd }
667 1.1 cgd
668 1.11 christos static int
669 1.1 cgd koktologin(name, realm, toname)
670 1.1 cgd char *name, *realm, *toname;
671 1.1 cgd {
672 1.40 assar return krb_kuserok(name,
673 1.40 assar strcmp (toname, "root") == 0 ? "root" : "",
674 1.40 assar realm,
675 1.40 assar toname);
676 1.1 cgd }
677 1.1 cgd #endif
678