1 /* $OpenBSD: auth-krb5.c,v 1.19 2006/08/03 03:34:41 deraadt Exp $ */
3 * Kerberos v5 authentication and ticket-passing routines.
5 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
8 * Copyright (c) 2002 Daniel Kouril. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
55 extern ServerOptions options
;
58 krb5_init(void *context
)
60 Authctxt
*authctxt
= (Authctxt
*)context
;
61 krb5_error_code problem
;
63 if (authctxt
->krb5_ctx
== NULL
) {
64 problem
= krb5_init_context(&authctxt
->krb5_ctx
);
72 auth_krb5_password(Authctxt
*authctxt
, const char *password
)
76 krb5_principal server
;
78 krb5_error_code problem
;
79 krb5_ccache ccache
= NULL
;
82 temporarily_use_uid(authctxt
->pw
);
84 problem
= krb5_init(authctxt
);
88 problem
= krb5_parse_name(authctxt
->krb5_ctx
, authctxt
->pw
->pw_name
,
89 &authctxt
->krb5_user
);
94 problem
= krb5_cc_gen_new(authctxt
->krb5_ctx
, &krb5_mcc_ops
, &ccache
);
98 problem
= krb5_cc_initialize(authctxt
->krb5_ctx
, ccache
,
105 problem
= krb5_verify_user(authctxt
->krb5_ctx
, authctxt
->krb5_user
,
106 ccache
, password
, 1, NULL
);
108 temporarily_use_uid(authctxt
->pw
);
113 problem
= krb5_cc_gen_new(authctxt
->krb5_ctx
, &krb5_fcc_ops
,
114 &authctxt
->krb5_fwd_ccache
);
118 problem
= krb5_cc_copy_cache(authctxt
->krb5_ctx
, ccache
,
119 authctxt
->krb5_fwd_ccache
);
120 krb5_cc_destroy(authctxt
->krb5_ctx
, ccache
);
126 problem
= krb5_get_init_creds_password(authctxt
->krb5_ctx
, &creds
,
127 authctxt
->krb5_user
, (char *)password
, NULL
, NULL
, 0, NULL
, NULL
);
131 problem
= krb5_sname_to_principal(authctxt
->krb5_ctx
, NULL
, NULL
,
132 KRB5_NT_SRV_HST
, &server
);
137 problem
= krb5_verify_init_creds(authctxt
->krb5_ctx
, &creds
, server
,
139 krb5_free_principal(authctxt
->krb5_ctx
, server
);
140 temporarily_use_uid(authctxt
->pw
);
144 if (!krb5_kuserok(authctxt
->krb5_ctx
, authctxt
->krb5_user
,
145 authctxt
->pw
->pw_name
)) {
150 problem
= ssh_krb5_cc_gen(authctxt
->krb5_ctx
, &authctxt
->krb5_fwd_ccache
);
154 problem
= krb5_cc_initialize(authctxt
->krb5_ctx
, authctxt
->krb5_fwd_ccache
,
155 authctxt
->krb5_user
);
159 problem
= krb5_cc_store_cred(authctxt
->krb5_ctx
, authctxt
->krb5_fwd_ccache
,
165 authctxt
->krb5_ticket_file
= (char *)krb5_cc_get_name(authctxt
->krb5_ctx
, authctxt
->krb5_fwd_ccache
);
167 len
= strlen(authctxt
->krb5_ticket_file
) + 6;
168 authctxt
->krb5_ccname
= xmalloc(len
);
169 snprintf(authctxt
->krb5_ccname
, len
, "FILE:%s",
170 authctxt
->krb5_ticket_file
);
174 do_pam_putenv("KRB5CCNAME", authctxt
->krb5_ccname
);
182 krb5_cc_destroy(authctxt
->krb5_ctx
, ccache
);
184 if (authctxt
->krb5_ctx
!= NULL
&& problem
!=-1)
185 debug("Kerberos password authentication failed: %s",
186 krb5_get_err_text(authctxt
->krb5_ctx
, problem
));
188 debug("Kerberos password authentication failed: %d",
191 krb5_cleanup_proc(authctxt
);
193 if (options
.kerberos_or_local_passwd
)
198 return (authctxt
->valid
? 1 : 0);
202 krb5_cleanup_proc(Authctxt
*authctxt
)
204 debug("krb5_cleanup_proc called");
205 if (authctxt
->krb5_fwd_ccache
) {
206 krb5_cc_destroy(authctxt
->krb5_ctx
, authctxt
->krb5_fwd_ccache
);
207 authctxt
->krb5_fwd_ccache
= NULL
;
209 if (authctxt
->krb5_user
) {
210 krb5_free_principal(authctxt
->krb5_ctx
, authctxt
->krb5_user
);
211 authctxt
->krb5_user
= NULL
;
213 if (authctxt
->krb5_ctx
) {
214 krb5_free_context(authctxt
->krb5_ctx
);
215 authctxt
->krb5_ctx
= NULL
;
221 ssh_krb5_cc_gen(krb5_context ctx
, krb5_ccache
*ccache
) {
226 ret
= snprintf(ccname
, sizeof(ccname
),
227 "FILE:/tmp/krb5cc_%d_XXXXXXXXXX", geteuid());
228 if (ret
< 0 || (size_t)ret
>= sizeof(ccname
))
231 old_umask
= umask(0177);
232 tmpfd
= mkstemp(ccname
+ strlen("FILE:"));
235 logit("mkstemp(): %.100s", strerror(errno
));
239 if (fchmod(tmpfd
,S_IRUSR
| S_IWUSR
) == -1) {
240 logit("fchmod(): %.100s", strerror(errno
));
246 return (krb5_cc_resolve(ctx
, ccname
, ccache
));
248 #endif /* !HEIMDAL */