1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/*
* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "kdc_locl.h"
#include <vis.h>
/*
*
*/
#undef __attribute__
#define __attribute__(x)
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_vaddreason(kdc_request_t r, const char *fmt, va_list ap)
__attribute__ ((__format__ (__printf__, 2, 0)))
{
heim_audit_vaddreason((heim_svc_req_desc)r, fmt, ap);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_addreason(kdc_request_t r, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 3)))
{
va_list ap;
va_start(ap, fmt);
heim_audit_vaddreason((heim_svc_req_desc)r, fmt, ap);
va_end(ap);
}
/*
* append_token adds a token which is optionally a kv-pair and it
* also optionally eats the whitespace. If k == NULL, then it's
* not a kv-pair.
*/
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_vaddkv(kdc_request_t r, int flags, const char *k,
const char *fmt, va_list ap)
__attribute__ ((__format__ (__printf__, 4, 0)))
{
heim_audit_vaddkv((heim_svc_req_desc)r, flags, k, fmt, ap);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_addkv(kdc_request_t r, int flags, const char *k,
const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 5)))
{
va_list ap;
va_start(ap, fmt);
heim_audit_vaddkv((heim_svc_req_desc)r, flags, k, fmt, ap);
va_end(ap);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_addkv_timediff(kdc_request_t r, const char *k,
const struct timeval *start,
const struct timeval *end)
{
heim_audit_addkv_timediff((heim_svc_req_desc)r,k, start, end);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_setkv_bool(kdc_request_t r, const char *k, krb5_boolean v)
{
heim_audit_setkv_bool((heim_svc_req_desc)r, k, (int)v);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_addkv_number(kdc_request_t r, const char *k, int64_t v)
{
heim_audit_addkv_number((heim_svc_req_desc)r, k, v);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_setkv_number(kdc_request_t r, const char *k, int64_t v)
{
heim_audit_setkv_number((heim_svc_req_desc)r, k, v);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_addkv_object(kdc_request_t r, const char *k, kdc_object_t obj)
{
heim_audit_addkv_object((heim_svc_req_desc)r, k, obj);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_setkv_object(kdc_request_t r, const char *k, kdc_object_t obj)
{
heim_audit_setkv_object((heim_svc_req_desc)r, k, obj);
}
KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
kdc_audit_getkv(kdc_request_t r, const char *k)
{
return heim_audit_getkv((heim_svc_req_desc)r, k);
}
/*
* Add up to 3 key value pairs to record HostAddresses from request body or
* PA-TGS ticket or whatever.
*/
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_audit_addaddrs(kdc_request_t r, HostAddresses *a, const char *key)
{
size_t i;
char buf[128];
if (a->len > 3) {
char numkey[32];
if (snprintf(numkey, sizeof(numkey), "num%s", key) >= sizeof(numkey))
numkey[31] = '\0';
kdc_audit_addkv(r, 0, numkey, "%llu", (unsigned long long)a->len);
}
for (i = 0; i < 3 && i < a->len; i++) {
if (krb5_print_address(&a->val[i], buf, sizeof(buf), NULL) == 0)
kdc_audit_addkv(r, 0, key, "%s", buf);
}
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
_kdc_audit_trail(kdc_request_t r, krb5_error_code ret)
{
const char *retname = NULL;
/* Get a symbolic name for some error codes */
#define CASE(x) case x : retname = #x; break
switch (ret ? ret : r->error_code) {
CASE(ENOMEM);
CASE(EACCES);
CASE(HDB_ERR_NOT_FOUND_HERE);
CASE(HDB_ERR_WRONG_REALM);
CASE(HDB_ERR_EXISTS);
CASE(HDB_ERR_KVNO_NOT_FOUND);
CASE(HDB_ERR_NOENTRY);
CASE(HDB_ERR_NO_MKEY);
CASE(KRB5KDC_ERR_BADOPTION);
CASE(KRB5KDC_ERR_CANNOT_POSTDATE);
CASE(KRB5KDC_ERR_CLIENT_NOTYET);
CASE(KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN);
CASE(KRB5KDC_ERR_ETYPE_NOSUPP);
CASE(KRB5KDC_ERR_KEY_EXPIRED);
CASE(KRB5KDC_ERR_NAME_EXP);
CASE(KRB5KDC_ERR_NEVER_VALID);
CASE(KRB5KDC_ERR_NONE);
CASE(KRB5KDC_ERR_NULL_KEY);
CASE(KRB5KDC_ERR_PADATA_TYPE_NOSUPP);
CASE(KRB5KDC_ERR_POLICY);
CASE(KRB5KDC_ERR_PREAUTH_FAILED);
CASE(KRB5KDC_ERR_PREAUTH_REQUIRED);
CASE(KRB5KDC_ERR_SERVER_NOMATCH);
CASE(KRB5KDC_ERR_SERVICE_EXP);
CASE(KRB5KDC_ERR_SERVICE_NOTYET);
CASE(KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN);
CASE(KRB5KDC_ERR_TRTYPE_NOSUPP);
CASE(KRB5KRB_AP_ERR_BADADDR);
CASE(KRB5KRB_AP_ERR_BADDIRECTION);
CASE(KRB5KRB_AP_ERR_BAD_INTEGRITY);
CASE(KRB5KRB_AP_ERR_BADKEYVER);
CASE(KRB5KRB_AP_ERR_BADMATCH);
CASE(KRB5KRB_AP_ERR_BADORDER);
CASE(KRB5KRB_AP_ERR_BADSEQ);
CASE(KRB5KRB_AP_ERR_BADVERSION);
CASE(KRB5KRB_AP_ERR_ILL_CR_TKT);
CASE(KRB5KRB_AP_ERR_INAPP_CKSUM);
CASE(KRB5KRB_AP_ERR_METHOD);
CASE(KRB5KRB_AP_ERR_MODIFIED);
CASE(KRB5KRB_AP_ERR_MSG_TYPE);
CASE(KRB5KRB_AP_ERR_MUT_FAIL);
CASE(KRB5KRB_AP_ERR_NOKEY);
CASE(KRB5KRB_AP_ERR_NOT_US);
CASE(KRB5KRB_AP_ERR_REPEAT);
CASE(KRB5KRB_AP_ERR_SKEW);
CASE(KRB5KRB_AP_ERR_TKT_EXPIRED);
CASE(KRB5KRB_AP_ERR_TKT_INVALID);
CASE(KRB5KRB_AP_ERR_TKT_NYV);
CASE(KRB5KRB_AP_ERR_V4_REPLY);
CASE(KRB5KRB_AP_PATH_NOT_ACCEPTED);
CASE(KRB5KRB_AP_WRONG_PRINC);
CASE(KRB5KRB_ERR_FIELD_TOOLONG);
CASE(KRB5KRB_ERR_GENERIC);
CASE(KRB5KRB_ERR_RESPONSE_TOO_BIG);
case 0:
retname = "SUCCESS";
break;
default:
retname = NULL;
break;
}
/* Let's save a few bytes */
#define PREFIX "KRB5KDC_"
if (retname && strncmp(PREFIX, retname, strlen(PREFIX)) == 0)
retname += strlen(PREFIX);
#undef PREFIX
heim_audit_trail((heim_svc_req_desc)r, ret, retname);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
krb5_kdc_update_time(struct timeval *tv)
{
if (tv == NULL)
gettimeofday(&_kdc_now, NULL);
else
_kdc_now = *tv;
}
KDC_LIB_FUNCTION struct timeval KDC_LIB_CALL
krb5_kdc_get_time(void)
{
return _kdc_now;
}
#define EXTEND_REQUEST_T(LHS, RHS) do { \
RHS = realloc(LHS, sizeof(*RHS)); \
if (!RHS) \
return krb5_enomem((LHS)->context); \
LHS = (void *)RHS; \
memset(((char *)LHS) + sizeof(*LHS), \
0x0, \
sizeof(*RHS) - sizeof(*LHS)); \
} while (0)
static krb5_error_code
kdc_as_req(kdc_request_t *rptr, int *claim)
{
astgs_request_t r;
krb5_error_code ret;
size_t len;
/* We must free things in the extensions */
EXTEND_REQUEST_T(*rptr, r);
ret = decode_AS_REQ(r->request.data, r->request.length, &r->req, &len);
if (ret)
return ret;
r->reqtype = "AS-REQ";
r->use_request_t = 1;
*claim = 1;
ret = _kdc_as_rep(r);
free_AS_REQ(&r->req);
return ret;
}
static krb5_error_code
kdc_tgs_req(kdc_request_t *rptr, int *claim)
{
astgs_request_t r;
krb5_error_code ret;
size_t len;
/* We must free things in the extensions */
EXTEND_REQUEST_T(*rptr, r);
ret = decode_TGS_REQ(r->request.data, r->request.length, &r->req, &len);
if (ret)
return ret;
r->reqtype = "TGS-REQ";
r->use_request_t = 1;
*claim = 1;
ret = _kdc_tgs_rep(r);
free_TGS_REQ(&r->req);
return ret;
}
#ifdef DIGEST
static krb5_error_code
kdc_digest(kdc_request_t *rptr, int *claim)
{
kdc_request_t r;
DigestREQ digestreq;
krb5_error_code ret;
size_t len;
r = *rptr;
ret = decode_DigestREQ(r->request.data, r->request.length,
&digestreq, &len);
if (ret)
return ret;
r->use_request_t = 0;
*claim = 1;
ret = _kdc_do_digest(r->context, r->config, &digestreq,
r->reply, r->from, r->addr);
free_DigestREQ(&digestreq);
return ret;
}
#endif
#ifdef KX509
static krb5_error_code
kdc_kx509(kdc_request_t *rptr, int *claim)
{
kx509_req_context r;
krb5_error_code ret;
/* We must free things in the extensions */
EXTEND_REQUEST_T(*rptr, r);
ret = _kdc_try_kx509_request(r);
if (ret)
return ret;
r->use_request_t = 1;
r->reqtype = "KX509";
*claim = 1;
return _kdc_do_kx509(r); /* Must clean up the req struct extensions */
}
#endif
static struct krb5_kdc_service services[] = {
{ KS_KRB5, "AS-REQ", kdc_as_req },
{ KS_KRB5, "TGS-REQ", kdc_tgs_req },
#ifdef DIGEST
{ 0, "DIGEST", kdc_digest },
#endif
#ifdef KX509
{ 0, "KX509", kdc_kx509 },
#endif
{ 0, NULL, NULL }
};
static int
process_request(krb5_context context,
krb5_kdc_configuration *config,
unsigned int krb5_only,
unsigned char *buf,
size_t len,
krb5_data *reply,
krb5_boolean *prependlength,
const char *from,
struct sockaddr *addr,
int datagram_reply)
{
kdc_request_t r;
krb5_error_code ret;
unsigned int i;
int claim = 0;
r = calloc(sizeof(*r), 1);
if (!r)
return krb5_enomem(context);
r->context = context;
r->hcontext = context->hcontext;
r->config = config;
r->logf = config->logf;
r->from = from;
r->addr = addr;
r->request.data = buf;
r->request.length = len;
r->datagram_reply = datagram_reply;
r->reply = reply;
r->kv = heim_dict_create(10);
r->attributes = heim_dict_create(1);
if (r->kv == NULL || r->attributes == NULL) {
heim_release(r->kv);
heim_release(r->attributes);
free(r);
return krb5_enomem(context);
}
gettimeofday(&r->tv_start, NULL);
for (i = 0; services[i].process != NULL; i++) {
if (krb5_only && (services[i].flags & KS_KRB5) == 0)
continue;
kdc_log(context, config, 7, "Probing for %s", services[i].name);
ret = (*services[i].process)(&r, &claim);
if (claim) {
if (prependlength && services[i].flags & KS_NO_LENGTH)
*prependlength = 0;
if (r->use_request_t) {
gettimeofday(&r->tv_end, NULL);
_kdc_audit_trail(r, ret);
free(r->cname);
free(r->sname);
free(r->e_text_buf);
krb5_data_free(&r->e_data);
}
heim_release(r->reason);
heim_release(r->kv);
heim_release(r->attributes);
free(r);
return ret;
}
}
heim_release(r->reason);
heim_release(r->kv);
heim_release(r->attributes);
free(r);
return -1;
}
/*
* handle the request in `buf, len', from `addr' (or `from' as a string),
* sending a reply in `reply'.
*/
KDC_LIB_FUNCTION int KDC_LIB_CALL
krb5_kdc_process_request(krb5_context context,
krb5_kdc_configuration *config,
unsigned char *buf,
size_t len,
krb5_data *reply,
krb5_boolean *prependlength,
const char *from,
struct sockaddr *addr,
int datagram_reply)
{
return process_request(context, config, 0, buf, len, reply, prependlength,
from, addr, datagram_reply);
}
/*
* handle the request in `buf, len', from `addr' (or `from' as a string),
* sending a reply in `reply'.
*
* This only processes krb5 requests
*/
KDC_LIB_FUNCTION int KDC_LIB_CALL
krb5_kdc_process_krb5_request(krb5_context context,
krb5_kdc_configuration *config,
unsigned char *buf,
size_t len,
krb5_data *reply,
const char *from,
struct sockaddr *addr,
int datagram_reply)
{
return process_request(context, config, 1, buf, len, reply, NULL,
from, addr, datagram_reply);
}
/*
*
*/
KDC_LIB_FUNCTION int KDC_LIB_CALL
krb5_kdc_save_request(krb5_context context,
const char *fn,
const unsigned char *buf,
size_t len,
const krb5_data *reply,
const struct sockaddr *sa)
{
krb5_storage *sp;
krb5_address a;
int fd = -1;
int ret = 0;
uint32_t t;
krb5_data d;
memset(&a, 0, sizeof(a));
d.data = rk_UNCONST(buf); /* do not free here */
d.length = len;
t = _kdc_now.tv_sec;
sp = krb5_storage_emem();
if (sp == NULL)
ret = krb5_enomem(context);
if (ret == 0)
ret = krb5_sockaddr2address(context, sa, &a);
if (ret == 0)
ret = krb5_store_uint32(sp, 1);
if (ret == 0)
ret = krb5_store_uint32(sp, t);
if (ret == 0)
ret = krb5_store_address(sp, a);
if (ret == 0)
ret = krb5_store_data(sp, d);
d.length = 0;
d.data = NULL;
if (ret == 0) {
Der_class cl;
Der_type ty;
unsigned int tag;
ret = der_get_tag (reply->data, reply->length,
&cl, &ty, &tag, NULL);
if (ret) {
ret = krb5_store_uint32(sp, 0xffffffff);
if (ret == 0)
ret = krb5_store_uint32(sp, 0xffffffff);
} else {
ret = krb5_store_uint32(sp, MAKE_TAG(cl, ty, 0));
if (ret == 0)
ret = krb5_store_uint32(sp, tag);
}
}
if (ret == 0)
ret = krb5_storage_to_data(sp, &d);
krb5_storage_free(sp);
sp = NULL;
/*
* We've got KDC concurrency, so we're going to try to do a single O_APPEND
* write(2). Hopefully we manage to write enough of the header that one
* can skip this request if it fails to write completely.
*/
if (ret == 0)
fd = open(fn, O_WRONLY|O_CREAT|O_APPEND, 0600);
if (fd < 0)
krb5_set_error_message(context, ret = errno, "Failed to open: %s", fn);
if (ret == 0) {
sp = krb5_storage_from_fd(fd);
if (sp == NULL)
krb5_set_error_message(context, ret = ENOMEM,
"Storage failed to open fd");
}
(void) close(fd);
if (ret == 0)
ret = krb5_store_data(sp, d);
krb5_free_address(context, &a);
/*
* krb5_storage_free() currently always returns 0, but for FDs it sets
* errno to whatever close() set it to if it failed.
*/
errno = 0;
if (ret == 0)
ret = krb5_storage_free(sp);
else
(void) krb5_storage_free(sp);
if (ret == 0 && errno)
ret = errno;
return ret;
}
KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
kdc_request_set_attribute(kdc_request_t r, kdc_object_t key, kdc_object_t value)
{
return heim_dict_set_value(r->attributes, key, value);
}
KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
kdc_request_get_attribute(kdc_request_t r, kdc_object_t key)
{
return heim_dict_get_value(r->attributes, key);
}
KDC_LIB_FUNCTION kdc_object_t KDC_LIB_CALL
kdc_request_copy_attribute(kdc_request_t r, kdc_object_t key)
{
return heim_dict_copy_value(r->attributes, key);
}
KDC_LIB_FUNCTION void KDC_LIB_CALL
kdc_request_delete_attribute(kdc_request_t r, kdc_object_t key)
{
heim_dict_delete_key(r->attributes, key);
}
|