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 $");
46 #define krb5_get_err_text(context,code) error_message(code)
49 extern ServerOptions options;
52 krb5_init(void *context)
54 Authctxt *authctxt = (Authctxt *)context;
55 krb5_error_code problem;
56 static int cleanup_registered = 0;
58 if (authctxt->krb5_ctx == NULL) {
59 problem = krb5_init_context(&authctxt->krb5_ctx);
62 krb5_init_ets(authctxt->krb5_ctx);
64 if (!cleanup_registered) {
65 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
66 cleanup_registered = 1;
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();
100 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
101 authctxt->krb5_auth_ctx, &fd);
103 problem = krb5_auth_con_genaddrs(authctxt->krb5_ctx,
104 authctxt->krb5_auth_ctx,fd,
105 KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR |
106 KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR);
111 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
112 KRB5_NT_SRV_HST, &server);
116 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
117 auth, server, NULL, NULL, &ticket);
122 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
123 &authctxt->krb5_user);
125 problem = krb5_copy_principal(authctxt->krb5_ctx,
126 ticket->enc_part2->client,
127 &authctxt->krb5_user);
132 /* if client wants mutual auth */
133 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
138 /* Check .k5login authorization now. */
139 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
140 authctxt->pw->pw_name))
144 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
150 krb5_free_principal(authctxt->krb5_ctx, server);
152 krb5_free_ticket(authctxt->krb5_ctx, ticket);
153 if (!ret && reply->length) {
155 memset(reply, 0, sizeof(*reply));
159 if (authctxt->krb5_ctx != NULL)
160 debug("Kerberos v5 authentication failed: %s",
161 krb5_get_err_text(authctxt->krb5_ctx, problem));
163 debug("Kerberos v5 authentication failed: %d",
171 auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
173 krb5_error_code problem;
174 krb5_ccache ccache = NULL;
178 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
181 temporarily_use_uid(authctxt->pw);
184 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
190 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
192 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
193 log("mkstemp(): %.100s", strerror(errno));
197 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
198 log("fchmod(): %.100s", strerror(errno));
204 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
210 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
211 authctxt->krb5_user);
216 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
221 problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
225 problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
230 authctxt->krb5_fwd_ccache = ccache;
233 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
235 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
240 debug("Kerberos v5 TGT accepted (%s)", pname);
248 debug("Kerberos v5 TGT passing failed: %s",
249 krb5_get_err_text(authctxt->krb5_ctx, problem));
251 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
259 auth_krb5_password(Authctxt *authctxt, const char *password)
263 krb5_principal server;
267 krb5_error_code problem;
269 if (authctxt->pw == NULL)
272 temporarily_use_uid(authctxt->pw);
274 problem = krb5_init(authctxt);
278 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
279 &authctxt->krb5_user);
284 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
285 &authctxt->krb5_fwd_ccache);
289 problem = krb5_cc_initialize(authctxt->krb5_ctx,
290 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
295 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
296 authctxt->krb5_fwd_ccache, password, 1, NULL);
297 temporarily_use_uid(authctxt->pw);
303 problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
304 authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
308 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
309 KRB5_NT_SRV_HST, &server);
314 problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
316 krb5_free_principal(authctxt->krb5_ctx, server);
317 temporarily_use_uid(authctxt->pw);
321 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
322 authctxt->pw->pw_name)) {
327 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
329 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
330 log("mkstemp(): %.100s", strerror(errno));
335 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
336 log("fchmod(): %.100s", strerror(errno));
343 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
347 problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
348 authctxt->krb5_user);
352 problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
358 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
364 if (authctxt->krb5_ctx != NULL && problem!=-1)
365 debug("Kerberos password authentication failed: %s",
366 krb5_get_err_text(authctxt->krb5_ctx, problem));
368 debug("Kerberos password authentication failed: %d",
371 krb5_cleanup_proc(authctxt);
373 if (options.kerberos_or_local_passwd)
382 krb5_cleanup_proc(void *context)
384 Authctxt *authctxt = (Authctxt *)context;
386 debug("krb5_cleanup_proc called");
387 if (authctxt->krb5_fwd_ccache) {
388 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
389 authctxt->krb5_fwd_ccache = NULL;
391 if (authctxt->krb5_user) {
392 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
393 authctxt->krb5_user = NULL;
395 if (authctxt->krb5_auth_ctx) {
396 krb5_auth_con_free(authctxt->krb5_ctx,
397 authctxt->krb5_auth_ctx);
398 authctxt->krb5_auth_ctx = NULL;
400 if (authctxt->krb5_ctx) {
401 krb5_free_context(authctxt->krb5_ctx);
402 authctxt->krb5_ctx = NULL;