2 * Copyright (c) 1999 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 * used to endorse or promote products derived from this software without
19 * specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
39 RCSID("$Id: su.c,v 1.26.2.1 2003/05/06 12:06:44 joda Exp $");
57 #include "crypto-headers.h"
70 #define _PATH_DEFPATH "/usr/bin:/bin"
74 #define _PATH_BSHELL "/bin/sh"
77 int kerberos_flag = 1;
81 char *kerberos_instance = "root";
87 struct getargs args[] = {
88 { "kerberos", 'K', arg_negative_flag, &kerberos_flag,
89 "don't use kerberos" },
90 { NULL, 'f', arg_flag, &csh_f_flag,
91 "don't read .cshrc" },
92 { "full", 'l', arg_flag, &full_login,
93 "simulate full login" },
94 { NULL, 'm', arg_flag, &env_flag,
95 "leave environment unmodified" },
96 { "instance", 'i', arg_string, &kerberos_instance,
97 "root instance to use" },
98 { "command", 'c', arg_string, &cmd,
99 "command to execute" },
100 { "help", 'h', arg_flag, &help_flag },
101 { "version", 0, arg_flag, &version_flag },
108 arg_printusage (args,
109 sizeof(args)/sizeof(*args),
111 "[login [shell arguments]]");
116 free_info(struct passwd *p)
125 static struct passwd*
126 dup_info(const struct passwd *pwd)
130 info = malloc(sizeof(*info));
133 info->pw_name = strdup(pwd->pw_name);
134 info->pw_passwd = strdup(pwd->pw_passwd);
135 info->pw_uid = pwd->pw_uid;
136 info->pw_gid = pwd->pw_gid;
137 info->pw_dir = strdup(pwd->pw_dir);
138 info->pw_shell = strdup(pwd->pw_shell);
139 if(info->pw_name == NULL || info->pw_passwd == NULL ||
140 info->pw_dir == NULL || info->pw_shell == NULL) {
147 #if defined(KRB4) || defined(KRB5)
152 #define TKT_ROOT "/tmp/tkt"
157 snprintf(tkfile, sizeof(tkfile), "%s_XXXXXX", TKT_ROOT);
158 fd = mkstemp(tkfile);
162 krb_set_tkt_string(tkfile);
168 static krb5_context context;
169 static krb5_ccache ccache;
172 krb5_verify(const struct passwd *login_info,
173 const struct passwd *su_info,
174 const char *kerberos_instance)
178 char *login_name = NULL;
180 #if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN)
181 login_name = getlogin();
183 ret = krb5_init_context (&context);
186 warnx("krb5_init_context failed: %d", ret);
191 if (login_name == NULL || strcmp (login_name, "root") == 0)
192 login_name = login_info->pw_name;
193 if (strcmp (su_info->pw_name, "root") == 0)
194 ret = krb5_make_principal(context, &p, NULL,
199 ret = krb5_make_principal(context, &p, NULL,
205 if(su_info->pw_uid != 0 || krb5_kuserok(context, p, su_info->pw_name)) {
206 ret = krb5_cc_gen_new(context, &krb5_mcc_ops, &ccache);
209 krb5_warn(context, ret, "krb5_cc_gen_new");
211 krb5_free_principal (context, p);
214 ret = krb5_verify_user_lrealm(context, p, ccache, NULL, TRUE, NULL);
215 krb5_free_principal (context, p);
217 krb5_cc_destroy(context, ccache);
219 case KRB5_LIBOS_PWDINTR :
221 case KRB5KRB_AP_ERR_BAD_INTEGRITY:
222 case KRB5KRB_AP_ERR_MODIFIED:
223 krb5_warnx(context, "Password incorrect");
226 krb5_warn(context, ret, "krb5_verify_user");
233 krb5_free_principal (context, p);
238 krb5_start_session(void)
244 ret = krb5_cc_gen_new(context, &krb5_fcc_ops, &ccache2);
246 krb5_cc_destroy(context, ccache);
250 ret = krb5_cc_copy_cache(context, ccache, ccache2);
252 asprintf(&cc_name, "%s:%s", krb5_cc_get_type(context, ccache2),
253 krb5_cc_get_name(context, ccache2));
254 esetenv("KRB5CCNAME", cc_name, 1);
256 /* we want to export this even if we don't directly support KRB4 */
258 esetenv("KRBTKFILE", tkfile, 1);
263 krb5_afslog(context, ccache2, NULL, NULL);
266 krb5_cc_close(context, ccache2);
267 krb5_cc_destroy(context, ccache);
275 krb_verify(const struct passwd *login_info,
276 const struct passwd *su_info,
277 const char *kerberos_instance)
280 char *login_name = NULL;
281 char *name, *instance, realm[REALM_SZ];
283 #if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN)
284 login_name = getlogin();
287 ret = krb_get_lrealm(realm, 1);
289 if (login_name == NULL || strcmp (login_name, "root") == 0)
290 login_name = login_info->pw_name;
291 if (strcmp (su_info->pw_name, "root") == 0) {
293 instance = (char*)kerberos_instance;
295 name = su_info->pw_name;
299 if(su_info->pw_uid != 0 ||
300 krb_kuserok(name, instance, realm, su_info->pw_name) == 0) {
305 krb_unparse_name_long (name, instance, realm));
306 if (des_read_pw_string (password, sizeof (password), prompt, 0)) {
307 memset (password, 0, sizeof (password));
312 if (strlen(password) == 0)
313 return (1); /* Empty passwords are not allowed */
315 setuid(geteuid()); /* need to run as root here */
316 ret = krb_verify_user(name, instance, realm, password,
317 KRB_VERIFY_SECURE, NULL);
318 memset(password, 0, sizeof(password));
321 warnx("%s", krb_get_err_text(ret));
324 chown (tkt_string(), su_info->pw_uid, su_info->pw_gid);
332 krb_start_session(void)
334 esetenv("KRBTKFILE", tkfile, 1);
337 if(k_hasafs() && k_setpag() == 0)
338 krb_afslog(NULL, NULL);
345 verify_unix(struct passwd *su)
351 if(su->pw_passwd != NULL && *su->pw_passwd != '\0') {
352 snprintf(prompt, sizeof(prompt), "%s's password: ", su->pw_name);
353 r = des_read_pw_string(pw_buf, sizeof(pw_buf), prompt, 0);
356 pw = crypt(pw_buf, su->pw_passwd);
357 memset(pw_buf, 0, sizeof(pw_buf));
358 if(strcmp(pw, su->pw_passwd) != 0)
365 main(int argc, char **argv)
369 struct passwd *su_info;
370 struct passwd *login_info;
377 int kerberos_error=1;
379 setprogname (argv[0]);
381 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))
384 for (i=0; i < optind; i++)
385 if (strcmp(argv[i], "-") == 0) {
399 su_user = argv[optind++];
401 pwd = k_getpwnam(su_user);
403 errx (1, "unknown login %s", su_user);
404 if (pwd->pw_uid == 0 && strcmp ("root", su_user) != 0) {
405 syslog (LOG_ALERT, "NIS attack, user %s has uid 0", su_user);
406 errx (1, "unknown login %s", su_user);
408 su_info = dup_info(pwd);
410 errx (1, "malloc: out of memory");
412 pwd = getpwuid(getuid());
414 errx(1, "who are you?");
415 login_info = dup_info(pwd);
416 if (login_info == NULL)
417 errx (1, "malloc: out of memory");
419 shell = login_info->pw_shell;
421 shell = su_info->pw_shell;
422 if(shell == NULL || *shell == '\0')
423 shell = _PATH_BSHELL;
427 if(kerberos_flag && ok == 0 &&
428 (kerberos_error=krb5_verify(login_info, su_info, kerberos_instance)) == 0)
432 if(kerberos_flag && ok == 0 &&
433 (kerberos_error = krb_verify(login_info, su_info, kerberos_instance)) == 0)
437 if(ok == 0 && login_info->pw_uid && verify_unix(su_info) != 0) {
446 sp = getspnam(su_info->pw_name);
448 today = time(0)/(24L * 60 * 60);
449 if (sp->sp_expire > 0) {
450 if (today >= sp->sp_expire) {
451 if (login_info->pw_uid)
452 errx(1,"Your account has expired.");
454 printf("Your account has expired.");
456 else if (sp->sp_expire - today < 14)
457 printf("Your account will expire in %d days.\n",
458 (int)(sp->sp_expire - today));
460 if (sp->sp_max > 0) {
461 if (today >= sp->sp_lstchg + sp->sp_max) {
462 if (login_info->pw_uid)
463 errx(1,"Your password has expired. Choose a new one.");
465 printf("Your password has expired. Choose a new one.");
467 else if (today >= sp->sp_lstchg + sp->sp_max - sp->sp_warn)
468 printf("Your account will expire in %d days.\n",
469 (int)(sp->sp_lstchg + sp->sp_max -today));
475 char *tty = ttyname (STDERR_FILENO);
476 syslog (LOG_NOTICE | LOG_AUTH, tty ? "%s to %s" : "%s to %s on %s",
477 login_info->pw_name, su_info->pw_name, tty);
483 char *t = getenv ("TERM");
485 environ = malloc (10 * sizeof (char *));
489 esetenv ("PATH", _PATH_DEFPATH, 1);
491 esetenv ("TERM", t, 1);
492 if (chdir (su_info->pw_dir) < 0)
493 errx (1, "no directory");
495 if (full_login || su_info->pw_uid)
496 esetenv ("USER", su_info->pw_name, 1);
497 esetenv("HOME", su_info->pw_dir, 1);
498 esetenv("SHELL", shell, 1);
506 p = strrchr(shell, '/');
512 if (strcmp(p, "csh") != 0)
515 args = malloc(((cmd ? 2 : 0) + 1 + argc - optind + 1 + csh_f_flag) * sizeof(*args));
520 asprintf(&args[i++], "-%s", p);
531 for (argv += optind; *argv; ++argv)
535 if(setgid(su_info->pw_gid) < 0)
537 if (initgroups (su_info->pw_name, su_info->pw_gid) < 0)
538 err (1, "initgroups");
539 if(setuid(su_info->pw_uid) < 0
540 || (su_info->pw_uid != 0 && setuid(0) == 0))
545 krb5_start_session();