2 * Kerberos v5 authentication and ticket-passing routines.
4 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
7 * Copyright (c) 2002 Daniel Kouril. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 RCSID("$OpenBSD: auth-krb5.c,v 1.7 2002/03/16 17:41:25 stevesk Exp $");
45 extern ServerOptions options;
48 krb5_init(void *context)
50 Authctxt *authctxt = (Authctxt *)context;
51 krb5_error_code problem;
52 static int cleanup_registered = 0;
54 if (authctxt->krb5_ctx == NULL) {
55 problem = krb5_init_context(&authctxt->krb5_ctx);
58 krb5_init_ets(authctxt->krb5_ctx);
60 if (!cleanup_registered) {
61 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
62 cleanup_registered = 1;
68 * Try krb5 authentication. server_user is passed for logging purposes
69 * only, in auth is received ticket, in client is returned principal
73 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client)
75 krb5_error_code problem;
76 krb5_principal server;
86 problem = krb5_init(authctxt);
90 problem = krb5_auth_con_init(authctxt->krb5_ctx,
91 &authctxt->krb5_auth_ctx);
95 fd = packet_get_connection_in();
96 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
97 authctxt->krb5_auth_ctx, &fd);
101 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
102 KRB5_NT_SRV_HST, &server);
106 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
107 auth, server, NULL, NULL, &ticket);
111 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
112 &authctxt->krb5_user);
116 /* if client wants mutual auth */
117 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
122 /* Check .k5login authorization now. */
123 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
124 authctxt->pw->pw_name))
128 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
131 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
132 packet_put_string((char *) reply.data, reply.length);
139 krb5_free_principal(authctxt->krb5_ctx, server);
141 krb5_free_ticket(authctxt->krb5_ctx, ticket);
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,
193 debug("Kerberos v5 TGT accepted (%s)", pname);
201 debug("Kerberos v5 TGT passing failed: %s",
202 krb5_get_err_text(authctxt->krb5_ctx, problem));
204 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
212 auth_krb5_password(Authctxt *authctxt, const char *password)
214 krb5_error_code problem;
216 if (authctxt->pw == NULL)
219 temporarily_use_uid(authctxt->pw);
221 problem = krb5_init(authctxt);
225 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
226 &authctxt->krb5_user);
230 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
231 &authctxt->krb5_fwd_ccache);
235 problem = krb5_cc_initialize(authctxt->krb5_ctx,
236 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
241 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
242 authctxt->krb5_fwd_ccache, password, 1, NULL);
243 temporarily_use_uid(authctxt->pw);
248 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
254 if (authctxt->krb5_ctx != NULL)
255 debug("Kerberos password authentication failed: %s",
256 krb5_get_err_text(authctxt->krb5_ctx, problem));
258 debug("Kerberos password authentication failed: %d",
261 krb5_cleanup_proc(authctxt);
263 if (options.kerberos_or_local_passwd)
272 krb5_cleanup_proc(void *context)
274 Authctxt *authctxt = (Authctxt *)context;
276 debug("krb5_cleanup_proc called");
277 if (authctxt->krb5_fwd_ccache) {
278 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
279 authctxt->krb5_fwd_ccache = NULL;
281 if (authctxt->krb5_user) {
282 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
283 authctxt->krb5_user = NULL;
285 if (authctxt->krb5_auth_ctx) {
286 krb5_auth_con_free(authctxt->krb5_ctx,
287 authctxt->krb5_auth_ctx);
288 authctxt->krb5_auth_ctx = NULL;
290 if (authctxt->krb5_ctx) {
291 krb5_free_context(authctxt->krb5_ctx);
292 authctxt->krb5_ctx = NULL;