]> andersk Git - openssh.git/blame - auth-krb5.c
- (dtucker) OpenBSD CVS Sync
[openssh.git] / auth-krb5.c
CommitLineData
aa9f6a6e 1/*
2 * Kerberos v5 authentication and ticket-passing routines.
762715ce 3 *
aa9f6a6e 4 * $FreeBSD: src/crypto/openssh/auth-krb5.c,v 1.6 2001/02/13 16:58:04 assar Exp $
aa9f6a6e 5 */
bb15f28b 6/*
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 */
aa9f6a6e 29
30#include "includes.h"
78e43412 31RCSID("$OpenBSD: auth-krb5.c,v 1.11 2003/07/16 15:02:06 markus Exp $");
93c3b6de 32
aa9f6a6e 33#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
751092f9 43
aa9f6a6e 44#include <krb5.h>
12928e80 45#ifndef HEIMDAL
46#define krb5_get_err_text(context,code) error_message(code)
47#endif /* !HEIMDAL */
aa9f6a6e 48
49extern ServerOptions options;
50
51static int
52krb5_init(void *context)
53{
54 Authctxt *authctxt = (Authctxt *)context;
55 krb5_error_code problem;
56 static int cleanup_registered = 0;
a0105740 57
aa9f6a6e 58 if (authctxt->krb5_ctx == NULL) {
59 problem = krb5_init_context(&authctxt->krb5_ctx);
60 if (problem)
61 return (problem);
62 krb5_init_ets(authctxt->krb5_ctx);
63 }
64 if (!cleanup_registered) {
65 fatal_add_cleanup(krb5_cleanup_proc, authctxt);
66 cleanup_registered = 1;
67 }
68 return (0);
69}
70
71/*
72 * Try krb5 authentication. server_user is passed for logging purposes
73 * only, in auth is received ticket, in client is returned principal
74 * from the ticket
75 */
a0105740 76int
696f6bef 77auth_krb5(Authctxt *authctxt, krb5_data *auth, char **client, krb5_data *reply)
aa9f6a6e 78{
79 krb5_error_code problem;
80 krb5_principal server;
aa9f6a6e 81 krb5_ticket *ticket;
ede8cea6 82 int fd, ret;
83
84 ret = 0;
aa9f6a6e 85 server = NULL;
86 ticket = NULL;
696f6bef 87 reply->length = 0;
a0105740 88
aa9f6a6e 89 problem = krb5_init(authctxt);
a0105740 90 if (problem)
aa9f6a6e 91 goto err;
a0105740 92
aa9f6a6e 93 problem = krb5_auth_con_init(authctxt->krb5_ctx,
94 &authctxt->krb5_auth_ctx);
95 if (problem)
96 goto err;
a0105740 97
aa9f6a6e 98 fd = packet_get_connection_in();
12928e80 99#ifdef HEIMDAL
aa9f6a6e 100 problem = krb5_auth_con_setaddrs_from_fd(authctxt->krb5_ctx,
101 authctxt->krb5_auth_ctx, &fd);
12928e80 102#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
aa9f6a6e 108 if (problem)
109 goto err;
a0105740 110
b77a87e5 111 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
aa9f6a6e 112 KRB5_NT_SRV_HST, &server);
113 if (problem)
114 goto err;
a0105740 115
aa9f6a6e 116 problem = krb5_rd_req(authctxt->krb5_ctx, &authctxt->krb5_auth_ctx,
117 auth, server, NULL, NULL, &ticket);
118 if (problem)
119 goto err;
a0105740 120
12928e80 121#ifdef HEIMDAL
aa9f6a6e 122 problem = krb5_copy_principal(authctxt->krb5_ctx, ticket->client,
123 &authctxt->krb5_user);
12928e80 124#else
125 problem = krb5_copy_principal(authctxt->krb5_ctx,
126 ticket->enc_part2->client,
127 &authctxt->krb5_user);
128#endif
aa9f6a6e 129 if (problem)
130 goto err;
a0105740 131
aa9f6a6e 132 /* if client wants mutual auth */
133 problem = krb5_mk_rep(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
696f6bef 134 reply);
aa9f6a6e 135 if (problem)
136 goto err;
a0105740 137
aa9f6a6e 138 /* Check .k5login authorization now. */
139 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
140 authctxt->pw->pw_name))
141 goto err;
a0105740 142
aa9f6a6e 143 if (client)
144 krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
145 client);
a0105740 146
ede8cea6 147 ret = 1;
aa9f6a6e 148 err:
149 if (server)
150 krb5_free_principal(authctxt->krb5_ctx, server);
151 if (ticket)
152 krb5_free_ticket(authctxt->krb5_ctx, ticket);
696f6bef 153 if (!ret && reply->length) {
154 xfree(reply->data);
155 memset(reply, 0, sizeof(*reply));
156 }
a0105740 157
c12337d9 158 if (problem) {
159 if (authctxt->krb5_ctx != NULL)
160 debug("Kerberos v5 authentication failed: %s",
161 krb5_get_err_text(authctxt->krb5_ctx, problem));
162 else
163 debug("Kerberos v5 authentication failed: %d",
164 problem);
165 }
ede8cea6 166
167 return (ret);
aa9f6a6e 168}
169
170int
171auth_krb5_tgt(Authctxt *authctxt, krb5_data *tgt)
172{
173 krb5_error_code problem;
174 krb5_ccache ccache = NULL;
175 char *pname;
12928e80 176 krb5_creds **creds;
a0105740 177
aa9f6a6e 178 if (authctxt->pw == NULL || authctxt->krb5_user == NULL)
179 return (0);
a0105740 180
aa9f6a6e 181 temporarily_use_uid(authctxt->pw);
a0105740 182
12928e80 183#ifdef HEIMDAL
aa9f6a6e 184 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops, &ccache);
12928e80 185#else
186{
187 char ccname[40];
188 int tmpfd;
189
190 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
191
192 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
bbe88b6d 193 logit("mkstemp(): %.100s", strerror(errno));
12928e80 194 problem = errno;
195 goto fail;
196 }
197 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
bbe88b6d 198 logit("fchmod(): %.100s", strerror(errno));
12928e80 199 close(tmpfd);
200 problem = errno;
201 goto fail;
202 }
203 close(tmpfd);
204 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &ccache);
205}
206#endif
aa9f6a6e 207 if (problem)
208 goto fail;
a0105740 209
aa9f6a6e 210 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
211 authctxt->krb5_user);
212 if (problem)
213 goto fail;
a0105740 214
12928e80 215#ifdef HEIMDAL
aa9f6a6e 216 problem = krb5_rd_cred2(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
217 ccache, tgt);
218 if (problem)
219 goto fail;
12928e80 220#else
221 problem = krb5_rd_cred(authctxt->krb5_ctx, authctxt->krb5_auth_ctx,
222 tgt, &creds, NULL);
223 if (problem)
224 goto fail;
225 problem = krb5_cc_store_cred(authctxt->krb5_ctx, ccache, *creds);
226 if (problem)
227 goto fail;
228#endif
a0105740 229
aa9f6a6e 230 authctxt->krb5_fwd_ccache = ccache;
231 ccache = NULL;
a0105740 232
aa9f6a6e 233 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
a0105740 234
aa9f6a6e 235 problem = krb5_unparse_name(authctxt->krb5_ctx, authctxt->krb5_user,
236 &pname);
237 if (problem)
238 goto fail;
a0105740 239
aa9f6a6e 240 debug("Kerberos v5 TGT accepted (%s)", pname);
a0105740 241
aa9f6a6e 242 restore_uid();
a0105740 243
aa9f6a6e 244 return (1);
a0105740 245
aa9f6a6e 246 fail:
247 if (problem)
248 debug("Kerberos v5 TGT passing failed: %s",
249 krb5_get_err_text(authctxt->krb5_ctx, problem));
250 if (ccache)
251 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
a0105740 252
aa9f6a6e 253 restore_uid();
a0105740 254
aa9f6a6e 255 return (0);
256}
257
258int
259auth_krb5_password(Authctxt *authctxt, const char *password)
260{
12928e80 261#ifndef HEIMDAL
262 krb5_creds creds;
263 krb5_principal server;
264 char ccname[40];
265 int tmpfd;
266#endif
aa9f6a6e 267 krb5_error_code problem;
78e43412 268 krb5_ccache ccache = NULL;
a0105740 269
aa9f6a6e 270 if (authctxt->pw == NULL)
271 return (0);
a0105740 272
aa9f6a6e 273 temporarily_use_uid(authctxt->pw);
a0105740 274
aa9f6a6e 275 problem = krb5_init(authctxt);
276 if (problem)
277 goto out;
a0105740 278
aa9f6a6e 279 problem = krb5_parse_name(authctxt->krb5_ctx, authctxt->pw->pw_name,
280 &authctxt->krb5_user);
281 if (problem)
282 goto out;
a0105740 283
12928e80 284#ifdef HEIMDAL
78e43412 285 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_mcc_ops, &ccache);
aa9f6a6e 286 if (problem)
287 goto out;
a0105740 288
78e43412 289 problem = krb5_cc_initialize(authctxt->krb5_ctx, ccache,
290 authctxt->krb5_user);
aa9f6a6e 291 if (problem)
292 goto out;
a0105740 293
294 restore_uid();
78e43412 295
aa9f6a6e 296 problem = krb5_verify_user(authctxt->krb5_ctx, authctxt->krb5_user,
78e43412 297 ccache, password, 1, NULL);
298
a0105740 299 temporarily_use_uid(authctxt->pw);
300
aa9f6a6e 301 if (problem)
302 goto out;
78e43412 303 problem = krb5_cc_gen_new(authctxt->krb5_ctx, &krb5_fcc_ops,
304 &authctxt->krb5_fwd_ccache);
305 if (problem)
306 goto out;
307
308 problem = krb5_cc_copy_cache(authctxt->krb5_ctx, ccache,
309 authctxt->krb5_fwd_ccache);
310 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
311 ccache = NULL;
312 if (problem)
313 goto out;
a0105740 314
12928e80 315#else
316 problem = krb5_get_init_creds_password(authctxt->krb5_ctx, &creds,
317 authctxt->krb5_user, (char *)password, NULL, NULL, 0, NULL, NULL);
318 if (problem)
319 goto out;
320
321 problem = krb5_sname_to_principal(authctxt->krb5_ctx, NULL, NULL,
322 KRB5_NT_SRV_HST, &server);
323 if (problem)
324 goto out;
325
326 restore_uid();
327 problem = krb5_verify_init_creds(authctxt->krb5_ctx, &creds, server,
328 NULL, NULL, NULL);
329 krb5_free_principal(authctxt->krb5_ctx, server);
330 temporarily_use_uid(authctxt->pw);
331 if (problem)
332 goto out;
333
334 if (!krb5_kuserok(authctxt->krb5_ctx, authctxt->krb5_user,
335 authctxt->pw->pw_name)) {
875ec275 336 problem = -1;
12928e80 337 goto out;
338 }
339
340 snprintf(ccname,sizeof(ccname),"FILE:/tmp/krb5cc_%d_XXXXXX",geteuid());
341
342 if ((tmpfd = mkstemp(ccname+strlen("FILE:")))==-1) {
bbe88b6d 343 logit("mkstemp(): %.100s", strerror(errno));
12928e80 344 problem = errno;
345 goto out;
346 }
347
348 if (fchmod(tmpfd,S_IRUSR | S_IWUSR) == -1) {
bbe88b6d 349 logit("fchmod(): %.100s", strerror(errno));
12928e80 350 close(tmpfd);
351 problem = errno;
352 goto out;
353 }
354 close(tmpfd);
355
356 problem = krb5_cc_resolve(authctxt->krb5_ctx, ccname, &authctxt->krb5_fwd_ccache);
357 if (problem)
358 goto out;
359
360 problem = krb5_cc_initialize(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
361 authctxt->krb5_user);
362 if (problem)
363 goto out;
364
365 problem= krb5_cc_store_cred(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache,
366 &creds);
367 if (problem)
368 goto out;
369#endif
370
aa9f6a6e 371 authctxt->krb5_ticket_file = (char *)krb5_cc_get_name(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
a0105740 372
aa9f6a6e 373 out:
374 restore_uid();
a0105740 375
aa9f6a6e 376 if (problem) {
78e43412 377 if (ccache)
378 krb5_cc_destroy(authctxt->krb5_ctx, ccache);
379
12928e80 380 if (authctxt->krb5_ctx != NULL && problem!=-1)
c12337d9 381 debug("Kerberos password authentication failed: %s",
382 krb5_get_err_text(authctxt->krb5_ctx, problem));
383 else
384 debug("Kerberos password authentication failed: %d",
385 problem);
a0105740 386
aa9f6a6e 387 krb5_cleanup_proc(authctxt);
a0105740 388
aa9f6a6e 389 if (options.kerberos_or_local_passwd)
390 return (-1);
391 else
392 return (0);
393 }
394 return (1);
395}
396
397void
398krb5_cleanup_proc(void *context)
399{
400 Authctxt *authctxt = (Authctxt *)context;
a0105740 401
aa9f6a6e 402 debug("krb5_cleanup_proc called");
403 if (authctxt->krb5_fwd_ccache) {
404 krb5_cc_destroy(authctxt->krb5_ctx, authctxt->krb5_fwd_ccache);
405 authctxt->krb5_fwd_ccache = NULL;
406 }
407 if (authctxt->krb5_user) {
408 krb5_free_principal(authctxt->krb5_ctx, authctxt->krb5_user);
409 authctxt->krb5_user = NULL;
410 }
411 if (authctxt->krb5_auth_ctx) {
412 krb5_auth_con_free(authctxt->krb5_ctx,
413 authctxt->krb5_auth_ctx);
414 authctxt->krb5_auth_ctx = NULL;
415 }
416 if (authctxt->krb5_ctx) {
417 krb5_free_context(authctxt->krb5_ctx);
418 authctxt->krb5_ctx = NULL;
419 }
420}
421
422#endif /* KRB5 */
This page took 0.347829 seconds and 5 git commands to generate.