blob: 308a6d5f9bad6ba997e0db05c5ba065a0fabc976 [file] [log] [blame]
Damien Miller964fed52001-09-25 12:58:23 +10001/*
2 * Kerberos v5 authentication and ticket-passing routines.
Ben Lindstrom6328ab32002-03-22 02:54:23 +00003 *
Damien Miller964fed52001-09-25 12:58:23 +10004 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
Damien Miller964fed52001-09-25 12:58:23 +10005 */
Ben Lindstromeacc71b2002-03-22 01:22:27 +00006/*
7 * Copyright (c) 2002 Daniel Kouril. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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.
28 */
Damien Miller964fed52001-09-25 12:58:23 +100029
30#include "includes.h"
Ben Lindstrom6328ab32002-03-22 02:54:23 +000031RCSID("$OpenBSD: auth-krb5.c,v 1.8 2002/03/19 10:49:35 markus Exp $");
Ben Lindstrom05764b92002-03-05 01:53:02 +000032
Damien Miller964fed52001-09-25 12:58:23 +100033#include "ssh.h"
34#include "ssh1.h"
35#include "packet.h"
36#include "xmalloc.h"
37#include "log.h"
38#include "servconf.h"
39#include "uidswap.h"
40#include "auth.h"
41
42#ifdef KRB5
43#include <krb5.h>
Damien Millerfd4c9ee2002-04-13 11:04:40 +100044#ifndef HEIMDAL
45#define krb5_get_err_text(context,code) error_message(code)
46#endif /* !HEIMDAL */
Damien Miller964fed52001-09-25 12:58:23 +100047
48extern ServerOptions options;
49
50static int
51krb5_init(void *context)
52{
53 Authctxt *authctxt = (Authctxt *)context;
54 krb5_error_code problem;
55 static int cleanup_registered = 0;
Damien Millerdb95e4e2002-02-13 16:56:44 +110056
Damien Miller964fed52001-09-25 12:58:23 +100057 if (authctxt->krb5_ctx == NULL) {
58 problem = krb5_init_context(&authctxt->krb5_ctx);
59 if (problem)
60 return (problem);
61 krb5_init_ets(authctxt->krb5_ctx);
62 }
63 if (!cleanup_registered) {
64 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
65 cleanup_registered = 1;
66 }
67 return (0);
68}
69
70/*
71 * Try krb5 authentication. server_user is passed for logging purposes
72 * only, in auth is received ticket, in client is returned principal
73 * from the ticket
74 */
Damien Millerdb95e4e2002-02-13 16:56:44 +110075int
Damien Miller964fed52001-09-25 12:58:23 +100076auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client)
77{
78 krb5_error_code problem;
79 krb5_principal server;
80 krb5_data reply;
81 krb5_ticket *ticket;
Damien Miller61b05cf2001-11-14 00:02:10 +110082 int fd, ret;
83
84 ret = 0;
Damien Miller964fed52001-09-25 12:58:23 +100085 server = NULL;
86 ticket = NULL;
87 reply.length = 0;
Damien Millerdb95e4e2002-02-13 16:56:44 +110088
Damien Miller964fed52001-09-25 12:58:23 +100089 problem = krb5_init(authctxt);
Damien Millerdb95e4e2002-02-13 16:56:44 +110090 if (problem)
Damien Miller964fed52001-09-25 12:58:23 +100091 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +110092
Damien Miller964fed52001-09-25 12:58:23 +100093 problem = krb5_auth_con_init(authctxt->krb5_ctx,
94 &authctxt->krb5_auth_ctx);
95 if (problem)
96 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +110097
Damien Miller964fed52001-09-25 12:58:23 +100098 fd = packet_get_connection_in();
Damien Millerfd4c9ee2002-04-13 11:04:40 +100099#ifdef HEIMDAL
Damien Miller964fed52001-09-25 12:58:23 +1000100 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
101 authctxt->krb5_auth_ctx, &fd);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000102#else
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);
107#endif
Damien Miller964fed52001-09-25 12:58:23 +1000108 if (problem)
109 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100110
Damien Miller964fed52001-09-25 12:58:23 +1000111 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL ,
112 KRB5_NT_SRV_HST, &server);
113 if (problem)
114 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100115
Damien Miller964fed52001-09-25 12:58:23 +1000116 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
117 auth, server, NULL, NULL, &ticket);
118 if (problem)
119 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100120
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000121#ifdef HEIMDAL
Damien Miller964fed52001-09-25 12:58:23 +1000122 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
123 &authctxt->krb5_user);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000124#else
125 problem = krb5_copy_principal(authctxt->krb5_ctx,
126 ticket->enc_part2->client,
127 &authctxt->krb5_user);
128#endif
Damien Miller964fed52001-09-25 12:58:23 +1000129 if (problem)
130 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100131
Damien Miller964fed52001-09-25 12:58:23 +1000132 /* if client wants mutual auth */
133 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
134 &reply);
135 if (problem)
136 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100137
Damien Miller964fed52001-09-25 12:58:23 +1000138 /* Check .k5login authorization now. */
139 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
140 authctxt->pw->pw_name))
141 goto err;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100142
Damien Miller964fed52001-09-25 12:58:23 +1000143 if (client)
144 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
145 client);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100146
Damien Miller964fed52001-09-25 12:58:23 +1000147 packet_start(SSH_SMSG_AUTH_KERBEROS_RESPONSE);
148 packet_put_string((char *) reply.data, reply.length);
149 packet_send();
150 packet_write_wait();
Damien Miller61b05cf2001-11-14 00:02:10 +1100151
152 ret = 1;
Damien Miller964fed52001-09-25 12:58:23 +1000153 err:
154 if (server)
155 krb5_free_principal(authctxt->krb5_ctx, server);
156 if (ticket)
157 krb5_free_ticket(authctxt->krb5_ctx, ticket);
158 if (reply.length)
159 xfree(reply.data);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100160
Ben Lindstromb8550282002-02-26 17:46:11 +0000161 if (problem) {
162 if (authctxt->krb5_ctx != NULL)
163 debug("Kerberos v5 authentication failed: %s",
164 krb5_get_err_text(authctxt->krb5_ctx, problem));
165 else
166 debug("Kerberos v5 authentication failed: %d",
167 problem);
168 }
Damien Miller61b05cf2001-11-14 00:02:10 +1100169
170 return (ret);
Damien Miller964fed52001-09-25 12:58:23 +1000171}
172
173int
174auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
175{
176 krb5_error_code problem;
177 krb5_ccache ccache = NULL;
178 char *pname;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000179 krb5_creds **creds;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100180
Damien Miller964fed52001-09-25 12:58:23 +1000181 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
182 return (0);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100183
Damien Miller964fed52001-09-25 12:58:23 +1000184 temporarily_use_uid(authctxt->pw);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100185
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000186#ifdef HEIMDAL
Damien Miller964fed52001-09-25 12:58:23 +1000187 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000188#else
189{
190 char ccname[40];
191 int tmpfd;
192
193 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
194
195 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
196 log("mkstemp(): %.100s", strerror(errno));
197 problem = errno;
198 goto fail;
199 }
200 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
201 log("fchmod(): %.100s", strerror(errno));
202 close(tmpfd);
203 problem = errno;
204 goto fail;
205 }
206 close(tmpfd);
207 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
208}
209#endif
Damien Miller964fed52001-09-25 12:58:23 +1000210 if (problem)
211 goto fail;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100212
Damien Miller964fed52001-09-25 12:58:23 +1000213 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
214 authctxt->krb5_user);
215 if (problem)
216 goto fail;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100217
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000218#ifdef HEIMDAL
Damien Miller964fed52001-09-25 12:58:23 +1000219 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
220 ccache, tgt);
221 if (problem)
222 goto fail;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000223#else
224 problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
225 tgt, &creds, NULL);
226 if (problem)
227 goto fail;
228 problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
229 if (problem)
230 goto fail;
231#endif
Damien Millerdb95e4e2002-02-13 16:56:44 +1100232
Damien Miller964fed52001-09-25 12:58:23 +1000233 authctxt->krb5_fwd_ccache = ccache;
234 ccache = NULL;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100235
Damien Miller964fed52001-09-25 12:58:23 +1000236 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100237
Damien Miller964fed52001-09-25 12:58:23 +1000238 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
239 &pname);
240 if (problem)
241 goto fail;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100242
Damien Miller964fed52001-09-25 12:58:23 +1000243 debug("Kerberos v5 TGT accepted (%s)", pname);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100244
Damien Miller964fed52001-09-25 12:58:23 +1000245 restore_uid();
Damien Millerdb95e4e2002-02-13 16:56:44 +1100246
Damien Miller964fed52001-09-25 12:58:23 +1000247 return (1);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100248
Damien Miller964fed52001-09-25 12:58:23 +1000249 fail:
250 if (problem)
251 debug("Kerberos v5 TGT passing failed: %s",
252 krb5_get_err_text(authctxt->krb5_ctx, problem));
253 if (ccache)
254 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100255
Damien Miller964fed52001-09-25 12:58:23 +1000256 restore_uid();
Damien Millerdb95e4e2002-02-13 16:56:44 +1100257
Damien Miller964fed52001-09-25 12:58:23 +1000258 return (0);
259}
260
261int
262auth_krb5_password(Authctxt *authctxt, const char *password)
263{
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000264#ifndef HEIMDAL
265 krb5_creds creds;
266 krb5_principal server;
267 char ccname[40];
268 int tmpfd;
269#endif
Damien Miller964fed52001-09-25 12:58:23 +1000270 krb5_error_code problem;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100271
Damien Miller964fed52001-09-25 12:58:23 +1000272 if (authctxt->pw == NULL)
273 return (0);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100274
Damien Miller964fed52001-09-25 12:58:23 +1000275 temporarily_use_uid(authctxt->pw);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100276
Damien Miller964fed52001-09-25 12:58:23 +1000277 problem = krb5_init(authctxt);
278 if (problem)
279 goto out;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100280
Damien Miller964fed52001-09-25 12:58:23 +1000281 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
282 &authctxt->krb5_user);
283 if (problem)
284 goto out;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100285
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000286#ifdef HEIMDAL
Damien Miller964fed52001-09-25 12:58:23 +1000287 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops,
288 &authctxt->krb5_fwd_ccache);
289 if (problem)
290 goto out;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100291
Damien Miller964fed52001-09-25 12:58:23 +1000292 problem = krb5_cc_initialize(authctxt->krb5_ctx,
293 authctxt->krb5_fwd_ccache, authctxt->krb5_user);
294 if (problem)
295 goto out;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100296
297 restore_uid();
Damien Miller964fed52001-09-25 12:58:23 +1000298 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
299 authctxt->krb5_fwd_ccache, password, 1, NULL);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100300 temporarily_use_uid(authctxt->pw);
301
Damien Miller964fed52001-09-25 12:58:23 +1000302 if (problem)
303 goto out;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100304
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000305#else
306 problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
307 authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
308 if (problem)
309 goto out;
310
311 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
312 KRB5_NT_SRV_HST, &server);
313 if (problem)
314 goto out;
315
316 restore_uid();
317 problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
318 NULL, NULL, NULL);
319 krb5_free_principal(authctxt->krb5_ctx, server);
320 temporarily_use_uid(authctxt->pw);
321 if (problem)
322 goto out;
323
324 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
325 authctxt->pw->pw_name)) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000326 problem = -1;
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000327 goto out;
328 }
329
330 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
331
332 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
333 log("mkstemp(): %.100s", strerror(errno));
334 problem = errno;
335 goto out;
336 }
337
338 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
339 log("fchmod(): %.100s", strerror(errno));
340 close(tmpfd);
341 problem = errno;
342 goto out;
343 }
344 close(tmpfd);
345
346 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
347 if (problem)
348 goto out;
349
350 problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
351 authctxt->krb5_user);
352 if (problem)
353 goto out;
354
355 problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
356 &creds);
357 if (problem)
358 goto out;
359#endif
360
Damien Miller964fed52001-09-25 12:58:23 +1000361 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100362
Damien Miller964fed52001-09-25 12:58:23 +1000363 out:
364 restore_uid();
Damien Millerdb95e4e2002-02-13 16:56:44 +1100365
Damien Miller964fed52001-09-25 12:58:23 +1000366 if (problem) {
Damien Millerfd4c9ee2002-04-13 11:04:40 +1000367 if (authctxt->krb5_ctx != NULL && problem!=-1)
Ben Lindstromb8550282002-02-26 17:46:11 +0000368 debug("Kerberos password authentication failed: %s",
369 krb5_get_err_text(authctxt->krb5_ctx, problem));
370 else
371 debug("Kerberos password authentication failed: %d",
372 problem);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100373
Damien Miller964fed52001-09-25 12:58:23 +1000374 krb5_cleanup_proc(authctxt);
Damien Millerdb95e4e2002-02-13 16:56:44 +1100375
Damien Miller964fed52001-09-25 12:58:23 +1000376 if (options.kerberos_or_local_passwd)
377 return (-1);
378 else
379 return (0);
380 }
381 return (1);
382}
383
384void
385krb5_cleanup_proc(void *context)
386{
387 Authctxt *authctxt = (Authctxt *)context;
Damien Millerdb95e4e2002-02-13 16:56:44 +1100388
Damien Miller964fed52001-09-25 12:58:23 +1000389 debug("krb5_cleanup_proc called");
390 if (authctxt->krb5_fwd_ccache) {
391 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
392 authctxt->krb5_fwd_ccache = NULL;
393 }
394 if (authctxt->krb5_user) {
395 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
396 authctxt->krb5_user = NULL;
397 }
398 if (authctxt->krb5_auth_ctx) {
399 krb5_auth_con_free(authctxt->krb5_ctx,
400 authctxt->krb5_auth_ctx);
401 authctxt->krb5_auth_ctx = NULL;
402 }
403 if (authctxt->krb5_ctx) {
404 krb5_free_context(authctxt->krb5_ctx);
405 authctxt->krb5_ctx = NULL;
406 }
407}
408
409#endif /* KRB5 */