2 * Kerberos v5 authentication and ticket-passing routines.
4 * $xFreeBSD: 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.9 2002/09/09 06:48:06 itojun Exp $");
32 RCSID("$FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.2.2.6 2003/02/03 17:31:06 des Exp $");
33 RCSID("$DragonFly: src/crypto/openssh/Attic/auth-krb5.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
47 #define krb5_get_err_text(context,code) error_message(code)
50 extern ServerOptions options;
53 krb5_init(void *context)
55 Authctxt *authctxt = (Authctxt *)context;
56 krb5_error_code problem;
57 static int cleanup_registered = 0;
59 if (authctxt->krb5_ctx == NULL) {
60 problem = krb5_init_context(&authctxt->krb5_ctx);
63 krb5_init_ets(authctxt->krb5_ctx);
65 if (!cleanup_registered) {
66 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
67 cleanup_registered = 1;
73 * Try krb5 authentication. server_user is passed for logging purposes
74 * only, in auth is received ticket, in client is returned principal
78 auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client, krb5_data *reply)
80 krb5_error_code problem;
81 krb5_principal server;
90 problem = krb5_init(authctxt);
94 problem = krb5_auth_con_init(authctxt->krb5_ctx,
95 &authctxt->krb5_auth_ctx);
99 fd = packet_get_connection_in();
101 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
102 authctxt->krb5_auth_ctx, &fd);
104 problem = krb5_auth_con_genaddrs(authctxt->krb5_ctx,
105 authctxt->krb5_auth_ctx,fd,
106 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
107 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
112 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
113 KRB5_NT_SRV_HST, &server);
117 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
118 auth, server, NULL, NULL, &ticket);
123 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
124 &authctxt->krb5_user);
126 problem = krb5_copy_principal(authctxt->krb5_ctx,
127 ticket->enc_part2->client,
128 &authctxt->krb5_user);
133 /* if client wants mutual auth */
134 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
139 /* Check .k5login authorization now. */
140 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
141 authctxt->pw->pw_name))
145 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
151 krb5_free_principal(authctxt->krb5_ctx, server);
153 krb5_free_ticket(authctxt->krb5_ctx, ticket);
154 if (!ret && reply->length) {
156 memset(reply, 0, sizeof(*reply));
160 if (authctxt->krb5_ctx != NULL)
161 debug("Kerberos v5 authentication failed: %s",
162 krb5_get_err_text(authctxt->krb5_ctx, problem));
164 debug("Kerberos v5 authentication failed: %d",
172 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
174 krb5_error_code problem;
175 krb5_ccache ccache = NULL;
179 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
182 temporarily_use_uid(authctxt->pw);
185 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
191 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
193 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
194 log("mkstemp(): %.100s", strerror(errno));
198 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
199 log("fchmod(): %.100s", strerror(errno));
205 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
211 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
212 authctxt->krb5_user);
217 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
222 problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
226 problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
231 authctxt->krb5_fwd_ccache = ccache;
234 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
236 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
241 debug("Kerberos v5 TGT accepted (%s)", pname);
249 debug("Kerberos v5 TGT passing failed: %s",
250 krb5_get_err_text(authctxt->krb5_ctx, problem));
252 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
260 auth_krb5_password(Authctxt *authctxt, const char *password)
264 krb5_principal server;
268 krb5_error_code problem;
270 if (authctxt->pw == NULL)
273 temporarily_use_uid(authctxt->pw);
275 problem = krb5_init(authctxt);
279 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
280 &authctxt->krb5_user);
285 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
286 &authctxt->krb5_fwd_ccache);
290 problem = krb5_cc_initialize(authctxt->krb5_ctx,
291 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
296 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
297 authctxt->krb5_fwd_ccache, password, 1, NULL);
298 temporarily_use_uid(authctxt->pw);
304 problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
305 authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
309 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
310 KRB5_NT_SRV_HST, &server);
315 problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
317 krb5_free_principal(authctxt->krb5_ctx, server);
318 temporarily_use_uid(authctxt->pw);
322 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
323 authctxt->pw->pw_name)) {
328 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
330 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
331 log("mkstemp(): %.100s", strerror(errno));
336 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
337 log("fchmod(): %.100s", strerror(errno));
344 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
348 problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
349 authctxt->krb5_user);
353 problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
359 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
365 if (authctxt->krb5_ctx != NULL && problem!=-1)
366 debug("Kerberos password authentication failed: %s",
367 krb5_get_err_text(authctxt->krb5_ctx, problem));
369 debug("Kerberos password authentication failed: %d",
372 krb5_cleanup_proc(authctxt);
374 if (options.kerberos_or_local_passwd)
383 krb5_cleanup_proc(void *context)
385 Authctxt *authctxt = (Authctxt *)context;
387 debug("krb5_cleanup_proc called");
388 if (authctxt->krb5_fwd_ccache) {
389 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
390 authctxt->krb5_fwd_ccache = NULL;
392 if (authctxt->krb5_user) {
393 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
394 authctxt->krb5_user = NULL;
396 if (authctxt->krb5_auth_ctx) {
397 krb5_auth_con_free(authctxt->krb5_ctx,
398 authctxt->krb5_auth_ctx);
399 authctxt->krb5_auth_ctx = NULL;
401 if (authctxt->krb5_ctx) {
402 krb5_free_context(authctxt->krb5_ctx);
403 authctxt->krb5_ctx = NULL;