1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* kdc/do_as_req.c */
3 /*
4 * Portions Copyright (C) 2007 Apple Inc.
5 * Copyright 1990, 1991, 2007, 2008, 2009, 2013, 2014 by the
6 * Massachusetts Institute of Technology. All Rights Reserved.
7 *
8 * Export of this software from the United States of America may
9 * require a specific license from the United States Government.
10 * It is the responsibility of any person or organization contemplating
11 * export to obtain such a license before exporting.
12 *
13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14 * distribute this software and its documentation for any purpose and
15 * without fee is hereby granted, provided that the above copyright
16 * notice appear in all copies and that both that copyright notice and
17 * this permission notice appear in supporting documentation, and that
18 * the name of M.I.T. not be used in advertising or publicity pertaining
19 * to distribution of the software without specific, written prior
20 * permission. Furthermore if you modify this software you must label
21 * your software as modified software and not distribute it in such a
22 * fashion that it might be confused with the original M.I.T. software.
23 * M.I.T. makes no representations about the suitability of
24 * this software for any purpose. It is provided "as is" without express
25 * or implied warranty.
26 *
27 *
28 * KDC Routines to deal with AS_REQ's
29 */
30 /*
31 * Copyright (c) 2006-2008, Novell, Inc.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions are met:
36 *
37 * * Redistributions of source code must retain the above copyright notice,
38 * this list of conditions and the following disclaimer.
39 * * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * * The copyright holder's name is not used to endorse or promote products
43 * derived from this software without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 * POSSIBILITY OF SUCH DAMAGE.
56 */
57
58 #include "k5-int.h"
59 #include "com_err.h"
60
61 #include <syslog.h>
62 #ifdef HAVE_NETINET_IN_H
63 #include <sys/types.h>
64 #include <netinet/in.h>
65 #ifndef hpux
66 #include <arpa/inet.h>
67 #endif /* hpux */
68 #endif /* HAVE_NETINET_IN_H */
69
70 #include "kdc_util.h"
71 #include "kdc_audit.h"
72 #include "policy.h"
73 #include <kadm5/admin.h>
74 #include "adm_proto.h"
75 #include "extern.h"
76
77 static krb5_error_code
78 prepare_error_as(struct kdc_request_state *, krb5_kdc_req *, krb5_db_entry *,
79 krb5_keyblock *, int, krb5_pa_data **, krb5_boolean,
80 krb5_principal, krb5_data **, const char *);
81
82 /* Determine the key-expiration value according to RFC 4120 section 5.4.2. */
83 static krb5_timestamp
get_key_exp(krb5_db_entry * entry)84 get_key_exp(krb5_db_entry *entry)
85 {
86 if (entry->expiration == 0)
87 return entry->pw_expiration;
88 if (entry->pw_expiration == 0)
89 return entry->expiration;
90 return ts_min(entry->expiration, entry->pw_expiration);
91 }
92
93 /*
94 * Find the key in client for the most preferred enctype in req_enctypes. Fill
95 * in *kb_out with the decrypted keyblock (which the caller must free) and set
96 * *kd_out to an alias to that key data entry. Set *kd_out to NULL and leave
97 * *kb_out zeroed if no key is found for any of the requested enctypes.
98 * kb_out->enctype may differ from the enctype of *kd_out for DES enctypes; in
99 * this case, kb_out->enctype is the requested enctype used to match the key
100 * data entry.
101 */
102 static krb5_error_code
select_client_key(krb5_context context,krb5_db_entry * client,krb5_enctype * req_enctypes,int n_req_enctypes,krb5_keyblock * kb_out,krb5_key_data ** kd_out)103 select_client_key(krb5_context context, krb5_db_entry *client,
104 krb5_enctype *req_enctypes, int n_req_enctypes,
105 krb5_keyblock *kb_out, krb5_key_data **kd_out)
106 {
107 krb5_error_code ret;
108 krb5_key_data *kd;
109 krb5_enctype etype;
110 int i;
111
112 memset(kb_out, 0, sizeof(*kb_out));
113 *kd_out = NULL;
114
115 for (i = 0; i < n_req_enctypes; i++) {
116 etype = req_enctypes[i];
117 if (!krb5_c_valid_enctype(etype))
118 continue;
119 if (krb5_dbe_find_enctype(context, client, etype, -1, 0, &kd) == 0) {
120 /* Decrypt the client key data and set its enctype to the request
121 * enctype (which may differ from the key data enctype for DES). */
122 ret = krb5_dbe_decrypt_key_data(context, NULL, kd, kb_out, NULL);
123 if (ret)
124 return ret;
125 kb_out->enctype = etype;
126 *kd_out = kd;
127 return 0;
128 }
129 }
130 return 0;
131 }
132
133 static krb5_error_code
lookup_client(krb5_context context,krb5_kdc_req * req,unsigned int flags,krb5_db_entry ** entry_out)134 lookup_client(krb5_context context, krb5_kdc_req *req, unsigned int flags,
135 krb5_db_entry **entry_out)
136 {
137 krb5_pa_data *pa;
138 krb5_data cert;
139
140 *entry_out = NULL;
141 pa = krb5int_find_pa_data(context, req->padata, KRB5_PADATA_S4U_X509_USER);
142 if (pa != NULL && pa->length != 0 &&
143 req->client->type == KRB5_NT_X500_PRINCIPAL) {
144 cert = make_data(pa->contents, pa->length);
145 return krb5_db_get_s4u_x509_principal(context, &cert, req->client,
146 flags, entry_out);
147 } else {
148 return krb5_db_get_principal(context, req->client, flags, entry_out);
149 }
150 }
151
152 struct as_req_state {
153 loop_respond_fn respond;
154 void *arg;
155
156 krb5_principal_data client_princ;
157 krb5_enc_tkt_part enc_tkt_reply;
158 krb5_enc_kdc_rep_part reply_encpart;
159 krb5_ticket ticket_reply;
160 krb5_keyblock local_tgt_key;
161 krb5_keyblock server_keyblock;
162 krb5_keyblock client_keyblock;
163 krb5_db_entry *client;
164 krb5_db_entry *server;
165 krb5_db_entry *local_tgt;
166 krb5_db_entry *local_tgt_storage;
167 krb5_key_data *client_key;
168 krb5_kdc_req *request;
169 struct krb5_kdcpreauth_rock_st rock;
170 const char *status;
171 krb5_pa_data **e_data;
172 krb5_boolean typed_e_data;
173 krb5_kdc_rep reply;
174 krb5_timestamp kdc_time;
175 krb5_keyblock session_key;
176 unsigned int c_flags;
177 krb5_data *req_pkt;
178 krb5_data *inner_body;
179 struct kdc_request_state *rstate;
180 char *sname, *cname;
181 void *pa_context;
182 const krb5_fulladdr *local_addr;
183 const krb5_fulladdr *remote_addr;
184 krb5_data **auth_indicators;
185
186 krb5_error_code preauth_err;
187
188 kdc_realm_t *active_realm;
189 krb5_audit_state *au_state;
190 };
191
192 static void
finish_process_as_req(struct as_req_state * state,krb5_error_code errcode)193 finish_process_as_req(struct as_req_state *state, krb5_error_code errcode)
194 {
195 krb5_key_data *server_key;
196 krb5_keyblock *as_encrypting_key = NULL;
197 krb5_data *response = NULL;
198 const char *emsg = 0;
199 int did_log = 0;
200 loop_respond_fn oldrespond;
201 void *oldarg;
202 kdc_realm_t *kdc_active_realm = state->active_realm;
203 krb5_audit_state *au_state = state->au_state;
204
205 assert(state);
206 oldrespond = state->respond;
207 oldarg = state->arg;
208
209 if (errcode)
210 goto egress;
211
212 au_state->stage = ENCR_REP;
213
214 state->ticket_reply.enc_part2 = &state->enc_tkt_reply;
215
216 errcode = check_kdcpolicy_as(kdc_context, state->request, state->client,
217 state->server, state->auth_indicators,
218 state->kdc_time, &state->enc_tkt_reply.times,
219 &state->status);
220 if (errcode)
221 goto egress;
222
223 /*
224 * Find the server key
225 */
226 if ((errcode = krb5_dbe_find_enctype(kdc_context, state->server,
227 -1, /* ignore keytype */
228 -1, /* Ignore salttype */
229 0, /* Get highest kvno */
230 &server_key))) {
231 state->status = "FINDING_SERVER_KEY";
232 goto egress;
233 }
234
235 /*
236 * Convert server->key into a real key
237 * (it may be encrypted in the database)
238 *
239 * server_keyblock is later used to generate auth data signatures
240 */
241 if ((errcode = krb5_dbe_decrypt_key_data(kdc_context, NULL,
242 server_key,
243 &state->server_keyblock,
244 NULL))) {
245 state->status = "DECRYPT_SERVER_KEY";
246 goto egress;
247 }
248
249 /* Start assembling the response */
250 state->reply.msg_type = KRB5_AS_REP;
251 state->reply.client = state->enc_tkt_reply.client; /* post canonization */
252 state->reply.ticket = &state->ticket_reply;
253 state->reply_encpart.session = &state->session_key;
254 if ((errcode = fetch_last_req_info(state->client,
255 &state->reply_encpart.last_req)))
256 goto egress;
257 state->reply_encpart.nonce = state->request->nonce;
258 state->reply_encpart.key_exp = get_key_exp(state->client);
259 state->reply_encpart.flags = state->enc_tkt_reply.flags;
260 state->reply_encpart.server = state->ticket_reply.server;
261 state->reply_encpart.times = state->enc_tkt_reply.times;
262 state->reply_encpart.caddrs = state->enc_tkt_reply.caddrs;
263 state->reply_encpart.enc_padata = NULL;
264
265 /* Fetch the padata info to be returned (do this before
266 * authdata to handle possible replacement of reply key
267 */
268 errcode = return_padata(kdc_context, &state->rock, state->req_pkt,
269 state->request, &state->reply,
270 &state->client_keyblock, &state->pa_context);
271 if (errcode) {
272 state->status = "KDC_RETURN_PADATA";
273 goto egress;
274 }
275
276 /* If we didn't find a client long-term key and no preauth mechanism
277 * replaced the reply key, error out now. */
278 if (state->client_keyblock.enctype == ENCTYPE_NULL) {
279 state->status = "CANT_FIND_CLIENT_KEY";
280 errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
281 goto egress;
282 }
283
284 errcode = handle_authdata(kdc_context, state->c_flags, state->client,
285 state->server, NULL, state->local_tgt,
286 &state->local_tgt_key, &state->client_keyblock,
287 &state->server_keyblock, NULL, state->req_pkt,
288 state->request, NULL, NULL, NULL,
289 &state->auth_indicators, &state->enc_tkt_reply);
290 if (errcode) {
291 krb5_klog_syslog(LOG_INFO, _("AS_REQ : handle_authdata (%d)"),
292 errcode);
293 state->status = "HANDLE_AUTHDATA";
294 goto egress;
295 }
296
297 errcode = check_indicators(kdc_context, state->server,
298 state->auth_indicators);
299 if (errcode) {
300 state->status = "HIGHER_AUTHENTICATION_REQUIRED";
301 goto egress;
302 }
303
304 errcode = krb5_encrypt_tkt_part(kdc_context, &state->server_keyblock,
305 &state->ticket_reply);
306 if (errcode)
307 goto egress;
308
309 errcode = kau_make_tkt_id(kdc_context, &state->ticket_reply,
310 &au_state->tkt_out_id);
311 if (errcode)
312 goto egress;
313
314 state->ticket_reply.enc_part.kvno = server_key->key_data_kvno;
315 errcode = kdc_fast_response_handle_padata(state->rstate,
316 state->request,
317 &state->reply,
318 state->client_keyblock.enctype);
319 if (errcode)
320 goto egress;
321
322 /* now encode/encrypt the response */
323
324 state->reply.enc_part.enctype = state->client_keyblock.enctype;
325
326 errcode = kdc_fast_handle_reply_key(state->rstate, &state->client_keyblock,
327 &as_encrypting_key);
328 if (errcode)
329 goto egress;
330 errcode = return_enc_padata(kdc_context, state->req_pkt, state->request,
331 as_encrypting_key, state->server,
332 &state->reply_encpart, FALSE);
333 if (errcode) {
334 state->status = "KDC_RETURN_ENC_PADATA";
335 goto egress;
336 }
337
338 if (kdc_fast_hide_client(state->rstate))
339 state->reply.client = (krb5_principal)krb5_anonymous_principal();
340 errcode = krb5_encode_kdc_rep(kdc_context, KRB5_AS_REP,
341 &state->reply_encpart, 0,
342 as_encrypting_key,
343 &state->reply, &response);
344 if (state->client_key != NULL)
345 state->reply.enc_part.kvno = state->client_key->key_data_kvno;
346 if (errcode)
347 goto egress;
348
349 /* these parts are left on as a courtesy from krb5_encode_kdc_rep so we
350 can use them in raw form if needed. But, we don't... */
351 memset(state->reply.enc_part.ciphertext.data, 0,
352 state->reply.enc_part.ciphertext.length);
353 free(state->reply.enc_part.ciphertext.data);
354
355 log_as_req(kdc_context, state->local_addr, state->remote_addr,
356 state->request, &state->reply, state->client, state->cname,
357 state->server, state->sname, state->kdc_time, 0, 0, 0);
358 did_log = 1;
359
360 egress:
361 if (errcode != 0 && state->status == NULL)
362 state->status = "UNKNOWN_REASON";
363
364 au_state->status = state->status;
365 au_state->reply = &state->reply;
366 kau_as_req(kdc_context,
367 (errcode || state->preauth_err) ? FALSE : TRUE, au_state);
368 kau_free_kdc_req(au_state);
369
370 free_padata_context(kdc_context, state->pa_context);
371 if (as_encrypting_key)
372 krb5_free_keyblock(kdc_context, as_encrypting_key);
373 if (errcode)
374 emsg = krb5_get_error_message(kdc_context, errcode);
375
376 if (state->status) {
377 log_as_req(kdc_context, state->local_addr, state->remote_addr,
378 state->request, &state->reply, state->client,
379 state->cname, state->server, state->sname, state->kdc_time,
380 state->status, errcode, emsg);
381 did_log = 1;
382 }
383 if (errcode) {
384 if (state->status == 0) {
385 state->status = emsg;
386 }
387 if (errcode != KRB5KDC_ERR_DISCARD) {
388 errcode -= ERROR_TABLE_BASE_krb5;
389 if (errcode < 0 || errcode > KRB_ERR_MAX)
390 errcode = KRB_ERR_GENERIC;
391
392 errcode = prepare_error_as(state->rstate, state->request,
393 state->local_tgt, &state->local_tgt_key,
394 errcode, state->e_data,
395 state->typed_e_data,
396 ((state->client != NULL) ?
397 state->client->princ : NULL),
398 &response, state->status);
399 state->status = 0;
400 }
401 }
402
403 if (emsg)
404 krb5_free_error_message(kdc_context, emsg);
405 if (state->enc_tkt_reply.authorization_data != NULL)
406 krb5_free_authdata(kdc_context,
407 state->enc_tkt_reply.authorization_data);
408 if (state->local_tgt_key.contents != NULL)
409 krb5_free_keyblock_contents(kdc_context, &state->local_tgt_key);
410 if (state->server_keyblock.contents != NULL)
411 krb5_free_keyblock_contents(kdc_context, &state->server_keyblock);
412 if (state->client_keyblock.contents != NULL)
413 krb5_free_keyblock_contents(kdc_context, &state->client_keyblock);
414 if (state->reply.padata != NULL)
415 krb5_free_pa_data(kdc_context, state->reply.padata);
416 if (state->reply_encpart.enc_padata)
417 krb5_free_pa_data(kdc_context, state->reply_encpart.enc_padata);
418
419 if (state->cname != NULL)
420 free(state->cname);
421 if (state->sname != NULL)
422 free(state->sname);
423 krb5_db_free_principal(kdc_context, state->client);
424 krb5_db_free_principal(kdc_context, state->server);
425 krb5_db_free_principal(kdc_context, state->local_tgt_storage);
426 if (state->session_key.contents != NULL)
427 krb5_free_keyblock_contents(kdc_context, &state->session_key);
428 if (state->ticket_reply.enc_part.ciphertext.data != NULL) {
429 memset(state->ticket_reply.enc_part.ciphertext.data , 0,
430 state->ticket_reply.enc_part.ciphertext.length);
431 free(state->ticket_reply.enc_part.ciphertext.data);
432 }
433
434 krb5_free_pa_data(kdc_context, state->e_data);
435 krb5_free_data(kdc_context, state->inner_body);
436 kdc_free_rstate(state->rstate);
437 krb5_free_kdc_req(kdc_context, state->request);
438 k5_free_data_ptr_list(state->auth_indicators);
439 assert(did_log != 0);
440
441 free(state);
442 (*oldrespond)(oldarg, errcode, response);
443 }
444
445 static void
finish_missing_required_preauth(void * arg)446 finish_missing_required_preauth(void *arg)
447 {
448 struct as_req_state *state = (struct as_req_state *)arg;
449
450 finish_process_as_req(state, state->preauth_err);
451 }
452
453 static void
finish_preauth(void * arg,krb5_error_code code)454 finish_preauth(void *arg, krb5_error_code code)
455 {
456 struct as_req_state *state = arg;
457 krb5_error_code real_code = code;
458
459 if (code) {
460 if (vague_errors)
461 code = KRB5KRB_ERR_GENERIC;
462 state->status = "PREAUTH_FAILED";
463 if (real_code == KRB5KDC_ERR_PREAUTH_FAILED) {
464 state->preauth_err = code;
465 get_preauth_hint_list(state->request, &state->rock, &state->e_data,
466 finish_missing_required_preauth, state);
467 return;
468 }
469 } else {
470 /*
471 * Final check before handing out ticket: If the client requires
472 * preauthentication, verify that the proper kind of
473 * preauthentication was carried out.
474 */
475 state->status = missing_required_preauth(state->client, state->server,
476 &state->enc_tkt_reply);
477 if (state->status) {
478 state->preauth_err = KRB5KDC_ERR_PREAUTH_REQUIRED;
479 get_preauth_hint_list(state->request, &state->rock, &state->e_data,
480 finish_missing_required_preauth, state);
481 return;
482 }
483 }
484
485 finish_process_as_req(state, code);
486 }
487
488 /*ARGSUSED*/
489 void
process_as_req(krb5_kdc_req * request,krb5_data * req_pkt,const krb5_fulladdr * local_addr,const krb5_fulladdr * remote_addr,kdc_realm_t * kdc_active_realm,verto_ctx * vctx,loop_respond_fn respond,void * arg)490 process_as_req(krb5_kdc_req *request, krb5_data *req_pkt,
491 const krb5_fulladdr *local_addr,
492 const krb5_fulladdr *remote_addr, kdc_realm_t *kdc_active_realm,
493 verto_ctx *vctx, loop_respond_fn respond, void *arg)
494 {
495 krb5_error_code errcode;
496 unsigned int s_flags = 0;
497 krb5_data encoded_req_body;
498 krb5_enctype useenctype;
499 struct as_req_state *state;
500 krb5_audit_state *au_state = NULL;
501
502 state = k5alloc(sizeof(*state), &errcode);
503 if (state == NULL) {
504 (*respond)(arg, errcode, NULL);
505 return;
506 }
507 state->respond = respond;
508 state->arg = arg;
509 state->request = request;
510 state->req_pkt = req_pkt;
511 state->local_addr = local_addr;
512 state->remote_addr = remote_addr;
513 state->active_realm = kdc_active_realm;
514
515 errcode = kdc_make_rstate(kdc_active_realm, &state->rstate);
516 if (errcode != 0) {
517 (*respond)(arg, errcode, NULL);
518 free(state);
519 return;
520 }
521
522 /* Initialize audit state. */
523 errcode = kau_init_kdc_req(kdc_context, state->request, remote_addr,
524 &au_state);
525 if (errcode) {
526 (*respond)(arg, errcode, NULL);
527 kdc_free_rstate(state->rstate);
528 free(state);
529 return;
530 }
531 state->au_state = au_state;
532
533 if (state->request->msg_type != KRB5_AS_REQ) {
534 state->status = "VALIDATE_MESSAGE_TYPE";
535 errcode = KRB5_BADMSGTYPE;
536 goto errout;
537 }
538
539 /* Seed the audit trail with the request ID and basic information. */
540 kau_as_req(kdc_context, TRUE, au_state);
541
542 errcode = krb5_timeofday(kdc_context, &state->kdc_time);
543 if (errcode)
544 goto errout;
545
546 if (fetch_asn1_field((unsigned char *) req_pkt->data,
547 1, 4, &encoded_req_body) != 0) {
548 errcode = ASN1_BAD_ID;
549 goto errout;
550 }
551 errcode = kdc_find_fast(&state->request, &encoded_req_body, NULL, NULL,
552 state->rstate, &state->inner_body);
553 if (errcode) {
554 state->status = "FIND_FAST";
555 goto errout;
556 }
557 if (state->inner_body == NULL) {
558 /* Not a FAST request; copy the encoded request body. */
559 errcode = krb5_copy_data(kdc_context, &encoded_req_body,
560 &state->inner_body);
561 if (errcode)
562 goto errout;
563 }
564 au_state->request = state->request;
565 state->rock.request = state->request;
566 state->rock.inner_body = state->inner_body;
567 state->rock.rstate = state->rstate;
568 state->rock.vctx = vctx;
569 state->rock.auth_indicators = &state->auth_indicators;
570 state->rock.send_freshness_token = FALSE;
571 if (!state->request->client) {
572 state->status = "NULL_CLIENT";
573 errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
574 goto errout;
575 }
576 if ((errcode = krb5_unparse_name(kdc_context,
577 state->request->client,
578 &state->cname)))
579 goto errout;
580 limit_string(state->cname);
581
582 if (!state->request->server) {
583 state->status = "NULL_SERVER";
584 errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
585 goto errout;
586 }
587 if ((errcode = krb5_unparse_name(kdc_context,
588 state->request->server,
589 &state->sname)))
590 goto errout;
591 limit_string(state->sname);
592
593 /*
594 * We set KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY as a hint
595 * to the backend to return naming information in lieu
596 * of cross realm TGS entries.
597 */
598 setflag(state->c_flags, KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY);
599
600 if (isflagset(state->request->kdc_options, KDC_OPT_CANONICALIZE)) {
601 setflag(state->c_flags, KRB5_KDB_FLAG_CANONICALIZE);
602 }
603 if (include_pac_p(kdc_context, state->request)) {
604 setflag(state->c_flags, KRB5_KDB_FLAG_INCLUDE_PAC);
605 }
606 errcode = lookup_client(kdc_context, state->request, state->c_flags,
607 &state->client);
608 if (errcode == KRB5_KDB_CANTLOCK_DB)
609 errcode = KRB5KDC_ERR_SVC_UNAVAILABLE;
610 if (errcode == KRB5_KDB_NOENTRY) {
611 state->status = "CLIENT_NOT_FOUND";
612 if (vague_errors)
613 errcode = KRB5KRB_ERR_GENERIC;
614 else
615 errcode = KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
616 goto errout;
617 } else if (errcode) {
618 state->status = "LOOKING_UP_CLIENT";
619 goto errout;
620 }
621 state->rock.client = state->client;
622
623 /*
624 * If the backend returned a principal that is not in the local
625 * realm, then we need to refer the client to that realm.
626 */
627 if (!is_local_principal(kdc_active_realm, state->client->princ)) {
628 /* Entry is a referral to another realm */
629 state->status = "REFERRAL";
630 au_state->cl_realm = &state->client->princ->realm;
631 errcode = KRB5KDC_ERR_WRONG_REALM;
632 goto errout;
633 }
634
635 au_state->stage = SRVC_PRINC;
636
637 s_flags = 0;
638 if (isflagset(state->request->kdc_options, KDC_OPT_CANONICALIZE)) {
639 setflag(s_flags, KRB5_KDB_FLAG_CANONICALIZE);
640 }
641 errcode = krb5_db_get_principal(kdc_context, state->request->server,
642 s_flags, &state->server);
643 if (errcode == KRB5_KDB_CANTLOCK_DB)
644 errcode = KRB5KDC_ERR_SVC_UNAVAILABLE;
645 if (errcode == KRB5_KDB_NOENTRY) {
646 state->status = "SERVER_NOT_FOUND";
647 errcode = KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN;
648 goto errout;
649 } else if (errcode) {
650 state->status = "LOOKING_UP_SERVER";
651 goto errout;
652 }
653
654 errcode = get_local_tgt(kdc_context, &state->request->server->realm,
655 state->server, &state->local_tgt,
656 &state->local_tgt_storage, &state->local_tgt_key);
657 if (errcode) {
658 state->status = "GET_LOCAL_TGT";
659 goto errout;
660 }
661 state->rock.local_tgt = state->local_tgt;
662
663 au_state->stage = VALIDATE_POL;
664
665 if ((errcode = validate_as_request(kdc_active_realm,
666 state->request, *state->client,
667 *state->server, state->kdc_time,
668 &state->status, &state->e_data))) {
669 errcode += ERROR_TABLE_BASE_krb5;
670 goto errout;
671 }
672
673 au_state->stage = ISSUE_TKT;
674
675 /*
676 * Select the keytype for the ticket session key.
677 */
678 if ((useenctype = select_session_keytype(kdc_active_realm, state->server,
679 state->request->nktypes,
680 state->request->ktype)) == 0) {
681 /* unsupported ktype */
682 state->status = "BAD_ENCRYPTION_TYPE";
683 errcode = KRB5KDC_ERR_ETYPE_NOSUPP;
684 goto errout;
685 }
686
687 if ((errcode = krb5_c_make_random_key(kdc_context, useenctype,
688 &state->session_key)))
689 goto errout;
690
691 /*
692 * Canonicalization is only effective if we are issuing a TGT
693 * (the intention is to allow support for Windows "short" realm
694 * aliases, nothing more).
695 */
696 if (isflagset(s_flags, KRB5_KDB_FLAG_CANONICALIZE) &&
697 krb5_is_tgs_principal(state->request->server) &&
698 krb5_is_tgs_principal(state->server->princ)) {
699 state->ticket_reply.server = state->server->princ;
700 } else {
701 state->ticket_reply.server = state->request->server;
702 }
703
704 /* Copy options that request the corresponding ticket flags. */
705 state->enc_tkt_reply.flags = get_ticket_flags(state->request->kdc_options,
706 state->client, state->server,
707 NULL);
708 state->enc_tkt_reply.times.authtime = state->kdc_time;
709
710 /*
711 * It should be noted that local policy may affect the
712 * processing of any of these flags. For example, some
713 * realms may refuse to issue renewable tickets
714 */
715
716 state->enc_tkt_reply.session = &state->session_key;
717 if (isflagset(state->c_flags, KRB5_KDB_FLAG_CANONICALIZE)) {
718 state->client_princ = *(state->client->princ);
719 } else {
720 state->client_princ = *(state->request->client);
721 /* The realm is always canonicalized */
722 state->client_princ.realm = state->client->princ->realm;
723 }
724 state->enc_tkt_reply.client = &state->client_princ;
725 state->enc_tkt_reply.transited.tr_type = KRB5_DOMAIN_X500_COMPRESS;
726 state->enc_tkt_reply.transited.tr_contents = empty_string;
727
728 if (isflagset(state->request->kdc_options, KDC_OPT_POSTDATED))
729 state->enc_tkt_reply.times.starttime = state->request->from;
730 else
731 state->enc_tkt_reply.times.starttime = state->kdc_time;
732
733 kdc_get_ticket_endtime(kdc_active_realm,
734 state->enc_tkt_reply.times.starttime,
735 kdc_infinity, state->request->till, state->client,
736 state->server, &state->enc_tkt_reply.times.endtime);
737
738 kdc_get_ticket_renewtime(kdc_active_realm, state->request, NULL,
739 state->client, state->server,
740 &state->enc_tkt_reply);
741
742 /*
743 * starttime is optional, and treated as authtime if not present.
744 * so we can nuke it if it matches
745 */
746 if (state->enc_tkt_reply.times.starttime ==
747 state->enc_tkt_reply.times.authtime)
748 state->enc_tkt_reply.times.starttime = 0;
749
750 state->enc_tkt_reply.caddrs = state->request->addresses;
751 state->enc_tkt_reply.authorization_data = 0;
752
753 /* If anonymous requests are being used, adjust the realm of the client
754 * principal. */
755 if (isflagset(state->request->kdc_options, KDC_OPT_REQUEST_ANONYMOUS)) {
756 if (!krb5_principal_compare_any_realm(kdc_context,
757 state->request->client,
758 krb5_anonymous_principal())) {
759 errcode = KRB5KDC_ERR_BADOPTION;
760 /* Anonymous requested but anonymous principal not used.*/
761 state->status = "VALIDATE_ANONYMOUS_PRINCIPAL";
762 goto errout;
763 }
764 krb5_free_principal(kdc_context, state->request->client);
765 state->request->client = NULL;
766 errcode = krb5_copy_principal(kdc_context, krb5_anonymous_principal(),
767 &state->request->client);
768 if (errcode)
769 goto errout;
770 state->enc_tkt_reply.client = state->request->client;
771 setflag(state->client->attributes, KRB5_KDB_REQUIRES_PRE_AUTH);
772 }
773
774 errcode = select_client_key(kdc_context, state->client,
775 state->request->ktype, state->request->nktypes,
776 &state->client_keyblock, &state->client_key);
777 if (errcode) {
778 state->status = "DECRYPT_CLIENT_KEY";
779 goto errout;
780 }
781 if (state->client_key != NULL) {
782 state->rock.client_key = state->client_key;
783 state->rock.client_keyblock = &state->client_keyblock;
784 }
785
786 errcode = kdc_fast_read_cookie(kdc_context, state->rstate, state->request,
787 state->local_tgt, &state->local_tgt_key);
788 if (errcode) {
789 state->status = "READ_COOKIE";
790 goto errout;
791 }
792
793 /*
794 * Check the preauthentication if it is there.
795 */
796 if (state->request->padata) {
797 check_padata(kdc_context, &state->rock, state->req_pkt,
798 state->request, &state->enc_tkt_reply, &state->pa_context,
799 &state->e_data, &state->typed_e_data, finish_preauth,
800 state);
801 } else
802 finish_preauth(state, 0);
803 return;
804
805 errout:
806 finish_process_as_req(state, errcode);
807 }
808
809 static krb5_error_code
prepare_error_as(struct kdc_request_state * rstate,krb5_kdc_req * request,krb5_db_entry * local_tgt,krb5_keyblock * local_tgt_key,int error,krb5_pa_data ** e_data_in,krb5_boolean typed_e_data,krb5_principal canon_client,krb5_data ** response,const char * status)810 prepare_error_as(struct kdc_request_state *rstate, krb5_kdc_req *request,
811 krb5_db_entry *local_tgt, krb5_keyblock *local_tgt_key,
812 int error, krb5_pa_data **e_data_in,
813 krb5_boolean typed_e_data, krb5_principal canon_client,
814 krb5_data **response, const char *status)
815 {
816 krb5_error errpkt;
817 krb5_error_code retval;
818 krb5_data *scratch = NULL, *e_data_asn1 = NULL, *fast_edata = NULL;
819 krb5_pa_data **e_data = NULL, *cookie = NULL;
820 kdc_realm_t *kdc_active_realm = rstate->realm_data;
821 size_t count;
822
823 errpkt.magic = KV5M_ERROR;
824
825 if (e_data_in != NULL) {
826 /* Add a PA-FX-COOKIE to e_data_in. e_data is a shallow copy
827 * containing aliases. */
828 for (count = 0; e_data_in[count] != NULL; count++);
829 e_data = calloc(count + 2, sizeof(*e_data));
830 if (e_data == NULL)
831 return ENOMEM;
832 memcpy(e_data, e_data_in, count * sizeof(*e_data));
833 retval = kdc_fast_make_cookie(kdc_context, rstate, local_tgt,
834 local_tgt_key, request->client,
835 &cookie);
836 e_data[count] = cookie;
837 }
838
839 errpkt.ctime = 0;
840 errpkt.cusec = 0;
841
842 retval = krb5_us_timeofday(kdc_context, &errpkt.stime, &errpkt.susec);
843 if (retval)
844 goto cleanup;
845 errpkt.error = error;
846 errpkt.server = request->server;
847 errpkt.client = (error == KDC_ERR_WRONG_REALM) ? canon_client :
848 request->client;
849 errpkt.text = string2data((char *)status);
850
851 if (e_data != NULL) {
852 if (typed_e_data)
853 retval = encode_krb5_typed_data(e_data, &e_data_asn1);
854 else
855 retval = encode_krb5_padata_sequence(e_data, &e_data_asn1);
856 if (retval)
857 goto cleanup;
858 errpkt.e_data = *e_data_asn1;
859 } else
860 errpkt.e_data = empty_data();
861
862 retval = kdc_fast_handle_error(kdc_context, rstate, request, e_data,
863 &errpkt, &fast_edata);
864 if (retval)
865 goto cleanup;
866 if (fast_edata != NULL)
867 errpkt.e_data = *fast_edata;
868
869 scratch = k5alloc(sizeof(*scratch), &retval);
870 if (scratch == NULL)
871 goto cleanup;
872 if (kdc_fast_hide_client(rstate) && errpkt.client != NULL)
873 errpkt.client = (krb5_principal)krb5_anonymous_principal();
874 retval = krb5_mk_error(kdc_context, &errpkt, scratch);
875 if (retval)
876 goto cleanup;
877
878 *response = scratch;
879 scratch = NULL;
880
881 cleanup:
882 krb5_free_data(kdc_context, fast_edata);
883 krb5_free_data(kdc_context, e_data_asn1);
884 free(scratch);
885 free(e_data);
886 if (cookie != NULL)
887 free(cookie->contents);
888 free(cookie);
889 return retval;
890 }
891