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.11 2003/07/16 15:02:06 markus Exp $");
46 extern ServerOptions options;
49 krb5_init(void *context)
51 Authctxt *authctxt = (Authctxt *)context;
52 krb5_error_code problem;
53 static int cleanup_registered = 0;
55 if (authctxt->krb5_ctx == NULL) {
56 problem = krb5_init_context(&authctxt->krb5_ctx);
59 krb5_init_ets(authctxt->krb5_ctx);
61 if (!cleanup_registered) {
62 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
63 cleanup_registered = 1;
69 * Try krb5 authentication. server_user is passed for logging purposes
70 * only, in auth is received ticket, in client is returned principal
74 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client, krb5_data *reply)
76 krb5_error_code problem;
77 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();
97 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
98 authctxt->krb5_auth_ctx, &fd);
100 problem = krb5_auth_con_genaddrs(authctxt->krb5_ctx,
101 authctxt->krb5_auth_ctx,fd,
102 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
103 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
108 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
109 KRB5_NT_SRV_HST, &server);
113 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
114 auth, server, NULL, NULL, &ticket);
119 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
120 &authctxt->krb5_user);
122 problem = krb5_copy_principal(authctxt->krb5_ctx,
123 ticket->enc_part2->client,
124 &authctxt->krb5_user);
129 /* if client wants mutual auth */
130 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
135 /* Check .k5login authorization now. */
136 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
137 authctxt->pw->pw_name))
141 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
147 krb5_free_principal(authctxt->krb5_ctx, server);
149 krb5_free_ticket(authctxt->krb5_ctx, ticket);
150 if (!ret && reply->length) {
152 memset(reply, 0, sizeof(*reply));
156 if (authctxt->krb5_ctx != NULL)
157 debug("Kerberos v5 authentication failed: %s",
158 krb5_get_err_text(authctxt->krb5_ctx, problem));
160 debug("Kerberos v5 authentication failed: %d",
168 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
170 krb5_error_code problem;
171 krb5_ccache ccache = NULL;
175 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
178 temporarily_use_uid(authctxt->pw);
181 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
187 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
189 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
190 logit("mkstemp(): %.100s", strerror(errno));
194 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
195 logit("fchmod(): %.100s", strerror(errno));
201 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
207 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
208 authctxt->krb5_user);
213 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
218 problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
222 problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
227 authctxt->krb5_fwd_ccache = ccache;
230 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
232 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
237 debug("Kerberos v5 TGT accepted (%s)", pname);
245 debug("Kerberos v5 TGT passing failed: %s",
246 krb5_get_err_text(authctxt->krb5_ctx, problem));
248 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
256 auth_krb5_password(Authctxt *authctxt, const char *password)
260 krb5_principal server;
264 krb5_error_code problem;
265 krb5_ccache ccache = NULL;
267 if (authctxt->pw == NULL)
270 temporarily_use_uid(authctxt->pw);
272 problem = krb5_init(authctxt);
276 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
277 &authctxt->krb5_user);
282 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, &ccache);
286 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
287 authctxt->krb5_user);
293 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
294 ccache, password, 1, NULL);
296 temporarily_use_uid(authctxt->pw);
300 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops,
301 &authctxt->krb5_fwd_ccache);
305 problem = krb5_cc_copy_cache(authctxt->krb5_ctx, ccache,
306 authctxt->krb5_fwd_ccache);
307 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
313 problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
314 authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
318 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
319 KRB5_NT_SRV_HST, &server);
324 problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
326 krb5_free_principal(authctxt->krb5_ctx, server);
327 temporarily_use_uid(authctxt->pw);
331 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
332 authctxt->pw->pw_name)) {
337 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
339 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
340 logit("mkstemp(): %.100s", strerror(errno));
345 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
346 logit("fchmod(): %.100s", strerror(errno));
353 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
357 problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
358 authctxt->krb5_user);
362 problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
368 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
375 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
377 if (authctxt->krb5_ctx != NULL && problem!=-1)
378 debug("Kerberos password authentication failed: %s",
379 krb5_get_err_text(authctxt->krb5_ctx, problem));
381 debug("Kerberos password authentication failed: %d",
384 krb5_cleanup_proc(authctxt);
386 if (options.kerberos_or_local_passwd)
395 krb5_cleanup_proc(void *context)
397 Authctxt *authctxt = (Authctxt *)context;
399 debug("krb5_cleanup_proc called");
400 if (authctxt->krb5_fwd_ccache) {
401 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
402 authctxt->krb5_fwd_ccache = NULL;
404 if (authctxt->krb5_user) {
405 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
406 authctxt->krb5_user = NULL;
408 if (authctxt->krb5_auth_ctx) {
409 krb5_auth_con_free(authctxt->krb5_ctx,
410 authctxt->krb5_auth_ctx);
411 authctxt->krb5_auth_ctx = NULL;
413 if (authctxt->krb5_ctx) {
414 krb5_free_context(authctxt->krb5_ctx);
415 authctxt->krb5_ctx = NULL;