2 * Copyright (c) 1999 - 2008 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. */
56 #include "crypto-headers.h"
67 #if !HAVE_DECL_ENVIRON
68 extern char **environ
;
71 int kerberos_flag
= 1;
75 char *kerberos_instance
= "root";
81 struct getargs args
[] = {
82 { "kerberos", 'K', arg_negative_flag
, &kerberos_flag
,
83 "don't use kerberos" },
84 { NULL
, 'f', arg_flag
, &csh_f_flag
,
85 "don't read .cshrc" },
86 { "full", 'l', arg_flag
, &full_login
,
87 "simulate full login" },
88 { NULL
, 'm', arg_flag
, &env_flag
,
89 "leave environment unmodified" },
90 { "instance", 'i', arg_string
, &kerberos_instance
,
91 "root instance to use" },
92 { "command", 'c', arg_string
, &cmd
,
93 "command to execute" },
94 { "help", 'h', arg_flag
, &help_flag
},
95 { "version", 0, arg_flag
, &version_flag
},
102 arg_printusage (args
,
103 sizeof(args
)/sizeof(*args
),
105 "[login [shell arguments]]");
110 free_info(struct passwd
*p
)
119 static struct passwd
*
120 dup_info(const struct passwd
*pwd
)
124 info
= malloc(sizeof(*info
));
127 info
->pw_name
= strdup(pwd
->pw_name
);
128 info
->pw_passwd
= strdup(pwd
->pw_passwd
);
129 info
->pw_uid
= pwd
->pw_uid
;
130 info
->pw_gid
= pwd
->pw_gid
;
131 info
->pw_dir
= strdup(pwd
->pw_dir
);
132 info
->pw_shell
= strdup(pwd
->pw_shell
);
133 if(info
->pw_name
== NULL
|| info
->pw_passwd
== NULL
||
134 info
->pw_dir
== NULL
|| info
->pw_shell
== NULL
) {
142 static krb5_context context
;
143 static krb5_ccache ccache
;
146 krb5_verify(const struct passwd
*login_info
,
147 const struct passwd
*su_info
,
148 const char *kerberos_instance
)
152 krb5_realm
*realms
, *r
;
153 char *login_name
= NULL
;
156 #if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN)
157 login_name
= getlogin();
159 ret
= krb5_init_context (&context
);
162 warnx("krb5_init_context failed: %d", ret
);
167 ret
= krb5_get_default_realms(context
, &realms
);
171 /* Check all local realms */
172 for (r
= realms
; *r
!= NULL
&& !user_ok
; r
++) {
174 if (login_name
== NULL
|| strcmp (login_name
, "root") == 0)
175 login_name
= login_info
->pw_name
;
176 if (strcmp (su_info
->pw_name
, "root") == 0)
177 ret
= krb5_make_principal(context
, &p
, *r
,
182 ret
= krb5_make_principal(context
, &p
, *r
,
186 krb5_free_host_realm(context
, realms
);
190 /* if we are su-ing too root, check with krb5_kuserok */
191 if (su_info
->pw_uid
== 0 && !krb5_kuserok(context
, p
, su_info
->pw_name
))
194 ret
= krb5_cc_new_unique(context
, krb5_cc_type_memory
, NULL
, &ccache
);
196 krb5_free_host_realm(context
, realms
);
197 krb5_free_principal (context
, p
);
200 ret
= krb5_verify_user(context
, p
, ccache
, NULL
, TRUE
, NULL
);
201 krb5_free_principal (context
, p
);
206 case KRB5_LIBOS_PWDINTR
:
207 krb5_cc_destroy(context
, ccache
);
209 case KRB5KRB_AP_ERR_BAD_INTEGRITY
:
210 case KRB5KRB_AP_ERR_MODIFIED
:
211 krb5_cc_destroy(context
, ccache
);
212 krb5_warnx(context
, "Password incorrect");
215 krb5_cc_destroy(context
, ccache
);
216 krb5_warn(context
, ret
, "krb5_verify_user");
220 krb5_free_host_realm(context
, realms
);
227 krb5_start_session(void)
233 ret
= krb5_cc_new_unique(context
, krb5_cc_type_file
, NULL
, &ccache2
);
235 krb5_cc_destroy(context
, ccache
);
239 ret
= krb5_cc_copy_cache(context
, ccache
, ccache2
);
241 krb5_cc_destroy(context
, ccache
);
242 krb5_cc_destroy(context
, ccache2
);
246 ret
= asprintf(&cc_name
, "%s:%s", krb5_cc_get_type(context
, ccache2
),
247 krb5_cc_get_name(context
, ccache2
));
249 krb5_cc_destroy(context
, ccache
);
250 krb5_cc_destroy(context
, ccache2
);
251 errx(1, "malloc - out of memory");
253 esetenv("KRB5CCNAME", cc_name
, 1);
258 krb5_afslog(context
, ccache2
, NULL
, NULL
);
261 krb5_cc_close(context
, ccache2
);
262 krb5_cc_destroy(context
, ccache
);
268 #define GROUP_MEMBER 0
269 #define GROUP_MISSING 1
270 #define GROUP_EMPTY 2
271 #define GROUP_NOT_MEMBER 3
274 group_member_p(const char *group
, const char *user
)
280 return GROUP_MISSING
;
281 if(g
->gr_mem
[0] == NULL
)
283 for(i
= 0; g
->gr_mem
[i
] != NULL
; i
++)
284 if(strcmp(user
, g
->gr_mem
[i
]) == 0)
286 return GROUP_NOT_MEMBER
;
290 verify_unix(struct passwd
*login
, struct passwd
*su
)
296 if(su
->pw_passwd
!= NULL
&& *su
->pw_passwd
!= '\0') {
297 snprintf(prompt
, sizeof(prompt
), "%s's password: ", su
->pw_name
);
298 r
= UI_UTIL_read_pw_string(pw_buf
, sizeof(pw_buf
), prompt
, 0);
301 pw
= crypt(pw_buf
, su
->pw_passwd
);
302 memset(pw_buf
, 0, sizeof(pw_buf
));
303 if(strcmp(pw
, su
->pw_passwd
) != 0) {
304 syslog (LOG_ERR
| LOG_AUTH
, "%s to %s: incorrect password",
305 login
->pw_name
, su
->pw_name
);
309 /* if su:ing to root, check membership of group wheel or root; if
310 that group doesn't exist, or is empty, allow anyone to su
312 if(su
->pw_uid
== 0) {
314 #define ROOT_GROUP "wheel"
316 int gs
= group_member_p(ROOT_GROUP
, login
->pw_name
);
317 if(gs
== GROUP_NOT_MEMBER
) {
318 syslog (LOG_ERR
| LOG_AUTH
, "%s to %s: not in group %s",
319 login
->pw_name
, su
->pw_name
, ROOT_GROUP
);
328 main(int argc
, char **argv
)
332 struct passwd
*su_info
;
333 struct passwd
*login_info
;
341 setprogname (argv
[0]);
343 if(getarg(args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
, &optind
))
346 for (i
=0; i
< optind
; i
++)
347 if (strcmp(argv
[i
], "-") == 0) {
361 su_user
= argv
[optind
++];
363 if (!issuid() && getuid() != 0)
364 warnx("Not setuid and you are not root, expect this to fail");
366 pwd
= k_getpwnam(su_user
);
368 errx (1, "unknown login %s", su_user
);
369 if (pwd
->pw_uid
== 0 && strcmp ("root", su_user
) != 0) {
370 syslog (LOG_ALERT
, "NIS attack, user %s has uid 0", su_user
);
371 errx (1, "unknown login %s", su_user
);
373 su_info
= dup_info(pwd
);
375 errx (1, "malloc: out of memory");
377 pwd
= getpwuid(getuid());
379 errx(1, "who are you?");
380 login_info
= dup_info(pwd
);
381 if (login_info
== NULL
)
382 errx (1, "malloc: out of memory");
384 shell
= login_info
->pw_shell
;
386 shell
= su_info
->pw_shell
;
387 if(shell
== NULL
|| *shell
== '\0')
388 shell
= _PATH_BSHELL
;
392 if(kerberos_flag
&& ok
== 0 &&
393 krb5_verify(login_info
, su_info
, kerberos_instance
) == 0)
397 if(ok
== 0 && login_info
->pw_uid
&& verify_unix(login_info
, su_info
) != 0) {
406 sp
= getspnam(su_info
->pw_name
);
408 today
= time(0)/(24L * 60 * 60);
409 if (sp
->sp_expire
> 0) {
410 if (today
>= sp
->sp_expire
) {
411 if (login_info
->pw_uid
)
412 errx(1,"Your account has expired.");
414 printf("Your account has expired.");
416 else if (sp
->sp_expire
- today
< 14)
417 printf("Your account will expire in %d days.\n",
418 (int)(sp
->sp_expire
- today
));
420 if (sp
->sp_max
> 0) {
421 if (today
>= sp
->sp_lstchg
+ sp
->sp_max
) {
422 if (login_info
->pw_uid
)
423 errx(1,"Your password has expired. Choose a new one.");
425 printf("Your password has expired. Choose a new one.");
427 else if (today
>= sp
->sp_lstchg
+ sp
->sp_max
- sp
->sp_warn
)
428 printf("Your account will expire in %d days.\n",
429 (int)(sp
->sp_lstchg
+ sp
->sp_max
-today
));
435 char *tty
= ttyname (STDERR_FILENO
);
436 syslog (LOG_NOTICE
| LOG_AUTH
, tty
? "%s to %s on %s" : "%s to %s",
437 login_info
->pw_name
, su_info
->pw_name
, tty
);
443 char *t
= getenv ("TERM");
444 char **newenv
= NULL
;
447 i
= read_environment(_PATH_ETC_ENVIRONMENT
, &newenv
);
449 environ
= malloc ((10 + i
) * sizeof (char *));
454 for (j
= 0; j
< i
; j
++) {
455 char *p
= strchr(newenv
[j
], '=');
457 errx(1, "enviroment '%s' missing '='", newenv
[j
]);
459 esetenv (newenv
[j
], p
, 1);
463 esetenv ("PATH", _PATH_DEFPATH
, 1);
465 esetenv ("TERM", t
, 1);
466 if (chdir (su_info
->pw_dir
) < 0)
467 errx (1, "no directory");
469 if (full_login
|| su_info
->pw_uid
)
470 esetenv ("USER", su_info
->pw_name
, 1);
471 esetenv("HOME", su_info
->pw_dir
, 1);
472 esetenv("SHELL", shell
, 1);
480 p
= strrchr(shell
, '/');
486 if (strcmp(p
, "csh") != 0)
489 args
= malloc(((cmd
? 2 : 0) + 1 + argc
- optind
+ 1 + csh_f_flag
) * sizeof(*args
));
494 if (asprintf(&args
[i
++], "-%s", p
) == -1)
506 for (argv
+= optind
; *argv
; ++argv
)
510 if(setgid(su_info
->pw_gid
) < 0)
512 if (initgroups (su_info
->pw_name
, su_info
->pw_gid
) < 0)
513 err (1, "initgroups");
514 if(setuid(su_info
->pw_uid
) < 0
515 || (su_info
->pw_uid
!= 0 && setuid(0) == 0))
520 krb5_start_session();
522 execve(shell
, args
, environ
);