2 /* $OpenBSD: auth-krb5.c,v 1.19 2006/08/03 03:34:41 deraadt Exp $ */
4 * Kerberos v5 authentication and ticket-passing routines.
6 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
9 * Copyright (c) 2002 Daniel Kouril. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 __RCSID("$NetBSD: auth-krb5.c,v 1.17 2006/09/28 21:22:14 christos Exp $");
34 #include <sys/types.h>
54 extern ServerOptions options
;
57 krb5_init(void *context
)
59 Authctxt
*authctxt
= (Authctxt
*)context
;
60 krb5_error_code problem
;
62 if (authctxt
->krb5_ctx
== NULL
) {
63 problem
= krb5_init_context(&authctxt
->krb5_ctx
);
66 krb5_init_ets(authctxt
->krb5_ctx
);
72 * Try krb5 authentication. server_user is passed for logging purposes
73 * only, in auth is received ticket, in client is returned principal
77 auth_krb5(Authctxt
*authctxt
, krb5_data
*auth
, char **client
, krb5_data
*reply
)
79 krb5_error_code problem
;
80 krb5_principal server
;
89 problem
= krb5_init(authctxt
);
93 problem
= krb5_auth_con_init(authctxt
->krb5_ctx
,
94 &authctxt
->krb5_auth_ctx
);
98 fd
= packet_get_connection_in();
99 problem
= krb5_auth_con_setaddrs_from_fd(authctxt
->krb5_ctx
,
100 authctxt
->krb5_auth_ctx
, &fd
);
104 problem
= krb5_sname_to_principal(authctxt
->krb5_ctx
, NULL
, NULL
,
105 KRB5_NT_SRV_HST
, &server
);
109 problem
= krb5_rd_req(authctxt
->krb5_ctx
, &authctxt
->krb5_auth_ctx
,
110 auth
, server
, NULL
, NULL
, &ticket
);
114 problem
= krb5_copy_principal(authctxt
->krb5_ctx
, ticket
->client
,
115 &authctxt
->krb5_user
);
119 /* if client wants mutual auth */
120 problem
= krb5_mk_rep(authctxt
->krb5_ctx
, authctxt
->krb5_auth_ctx
,
125 /* Check .k5login authorization now. */
126 if (!krb5_kuserok(authctxt
->krb5_ctx
, authctxt
->krb5_user
,
127 authctxt
->pw
->pw_name
))
131 krb5_unparse_name(authctxt
->krb5_ctx
, authctxt
->krb5_user
,
137 krb5_free_principal(authctxt
->krb5_ctx
, server
);
139 krb5_free_ticket(authctxt
->krb5_ctx
, ticket
);
140 if (!ret
&& reply
->length
) {
142 memset(reply
, 0, sizeof(*reply
));
146 if (authctxt
->krb5_ctx
!= NULL
)
147 debug("Kerberos v5 authentication failed: %s",
148 krb5_get_err_text(authctxt
->krb5_ctx
, problem
));
150 debug("Kerberos v5 authentication failed: %d",
158 auth_krb5_tgt(Authctxt
*authctxt
, krb5_data
*tgt
)
160 krb5_error_code problem
;
161 krb5_ccache ccache
= NULL
;
164 if (authctxt
->pw
== NULL
|| authctxt
->krb5_user
== NULL
)
167 temporarily_use_uid(authctxt
->pw
);
169 problem
= krb5_cc_gen_new(authctxt
->krb5_ctx
, &krb5_fcc_ops
, &ccache
);
173 problem
= krb5_cc_initialize(authctxt
->krb5_ctx
, ccache
,
174 authctxt
->krb5_user
);
178 problem
= krb5_rd_cred2(authctxt
->krb5_ctx
, authctxt
->krb5_auth_ctx
,
183 authctxt
->krb5_fwd_ccache
= ccache
;
186 authctxt
->krb5_ticket_file
= (char *)krb5_cc_get_name(authctxt
->krb5_ctx
, authctxt
->krb5_fwd_ccache
);
188 problem
= krb5_unparse_name(authctxt
->krb5_ctx
, authctxt
->krb5_user
,
195 do_pam_putenv("KRB5CCNAME", authctxt
->krb5_ticket_file
);
197 debug("Kerberos v5 TGT accepted (%s)", pname
);
205 debug("Kerberos v5 TGT passing failed: %s",
206 krb5_get_err_text(authctxt
->krb5_ctx
, problem
));
208 krb5_cc_destroy(authctxt
->krb5_ctx
, ccache
);
217 auth_krb5_password(Authctxt
*authctxt
, const char *password
)
219 krb5_error_code problem
;
220 krb5_ccache ccache
= NULL
;
222 temporarily_use_uid(authctxt
->pw
);
224 problem
= krb5_init(authctxt
);
228 problem
= krb5_parse_name(authctxt
->krb5_ctx
, authctxt
->pw
->pw_name
,
229 &authctxt
->krb5_user
);
233 problem
= krb5_cc_gen_new(authctxt
->krb5_ctx
, &krb5_mcc_ops
, &ccache
);
237 problem
= krb5_cc_initialize(authctxt
->krb5_ctx
, ccache
,
238 authctxt
->krb5_user
);
244 problem
= krb5_verify_user(authctxt
->krb5_ctx
, authctxt
->krb5_user
,
245 ccache
, password
, 1, NULL
);
247 temporarily_use_uid(authctxt
->pw
);
252 problem
= krb5_cc_gen_new(authctxt
->krb5_ctx
, &krb5_fcc_ops
,
253 &authctxt
->krb5_fwd_ccache
);
257 problem
= krb5_cc_copy_cache(authctxt
->krb5_ctx
, ccache
,
258 authctxt
->krb5_fwd_ccache
);
259 krb5_cc_destroy(authctxt
->krb5_ctx
, ccache
);
264 authctxt
->krb5_ticket_file
= (char *)krb5_cc_get_name(authctxt
->krb5_ctx
,
265 authctxt
->krb5_fwd_ccache
);
272 krb5_cc_destroy(authctxt
->krb5_ctx
, ccache
);
274 if (authctxt
->krb5_ctx
!= NULL
)
275 debug("Kerberos password authentication failed: %s",
276 krb5_get_err_text(authctxt
->krb5_ctx
, problem
));
278 debug("Kerberos password authentication failed: %d",
281 krb5_cleanup_proc(authctxt
);
283 if (options
.kerberos_or_local_passwd
)
288 return (authctxt
->valid
? 1 : 0);
292 krb5_cleanup_proc(Authctxt
*authctxt
)
294 debug("krb5_cleanup_proc called");
295 if (authctxt
->krb5_fwd_ccache
) {
296 krb5_cc_destroy(authctxt
->krb5_ctx
, authctxt
->krb5_fwd_ccache
);
297 authctxt
->krb5_fwd_ccache
= NULL
;
299 if (authctxt
->krb5_user
) {
300 krb5_free_principal(authctxt
->krb5_ctx
, authctxt
->krb5_user
);
301 authctxt
->krb5_user
= NULL
;
303 if (authctxt
->krb5_auth_ctx
) {
304 krb5_auth_con_free(authctxt
->krb5_ctx
,
305 authctxt
->krb5_auth_ctx
);
306 authctxt
->krb5_auth_ctx
= NULL
;
308 if (authctxt
->krb5_ctx
) {
309 krb5_free_context(authctxt
->krb5_ctx
);
310 authctxt
->krb5_ctx
= NULL
;