summaryrefslogtreecommitdiffstats
path: root/third_party/heimdal/lib/hdb/print.c
blob: 7f2535881892c28bd5da7891b1e83acedfe2e8d7 (plain)
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
/*
 * Copyright (c) 1999-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 KTH 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 KTH AND ITS 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 KTH OR ITS 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 "hdb_locl.h"
#include <hex.h>
#include <ctype.h>

/*
   This is the present contents of a dump line. This might change at
   any time. Fields are separated by white space.

  principal
  keyblock
  	kvno
	keys...
		mkvno
		enctype
		keyvalue
		salt (- means use normal salt)
  creation date and principal
  modification date and principal
  principal valid from date (not used)
  principal valid end date (not used)
  principal key expires (not used)
  max ticket life
  max renewable life
  flags
  generation number
  */

/*
 * These utility functions return the number of bytes written or -1, and
 * they set an error in the context.
 */
static ssize_t
append_string(krb5_context context, krb5_storage *sp, const char *fmt, ...)
{
    ssize_t sz;
    char *s;
    int rc;
    va_list ap;
    va_start(ap, fmt);
    rc = vasprintf(&s, fmt, ap);
    va_end(ap);
    if(rc < 0) {
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return -1;
    }
    sz = krb5_storage_write(sp, s, strlen(s));
    free(s);
    return sz;
}

static krb5_error_code
append_hex(krb5_context context, krb5_storage *sp,
           int always_encode, int lower, krb5_data *data)
{
    ssize_t sz;
    int printable = 1;
    size_t i;
    char *p;

    p = data->data;
    if (!always_encode) {
        for (i = 0; i < data->length; i++) {
            if (!isalnum((unsigned char)p[i]) && p[i] != '.'){
                printable = 0;
                break;
            }
        }
    }
    if (printable && !always_encode)
	return append_string(context, sp, "\"%.*s\"",
			     data->length, data->data);
    sz = hex_encode(data->data, data->length, &p);
    if (sz == -1) return sz;
    if (lower)
        strlwr(p);
    sz = append_string(context, sp, "%s", p);
    free(p);
    return sz;
}

static char *
time2str(time_t t)
{
    static char buf[128];
    strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", gmtime(&t));
    return buf;
}

static ssize_t
append_event(krb5_context context, krb5_storage *sp, Event *ev)
{
    krb5_error_code ret;
    ssize_t sz;
    char *pr = NULL;
    if(ev == NULL)
	return append_string(context, sp, "- ");
    if (ev->principal != NULL) {
       ret = krb5_unparse_name(context, ev->principal, &pr);
       if (ret) return -1; /* krb5_unparse_name() sets error info */
    }
    sz = append_string(context, sp, "%s:%s ", time2str(ev->time),
                       pr ? pr : "UNKNOWN");
    free(pr);
    return sz;
}

#define KRB5_KDB_SALTTYPE_NORMAL        0
#define KRB5_KDB_SALTTYPE_V4            1
#define KRB5_KDB_SALTTYPE_NOREALM       2
#define KRB5_KDB_SALTTYPE_ONLYREALM     3
#define KRB5_KDB_SALTTYPE_SPECIAL       4
#define KRB5_KDB_SALTTYPE_AFS3          5

static ssize_t
append_mit_key(krb5_context context, krb5_storage *sp,
               krb5_const_principal princ,
               unsigned int kvno, Key *key)
{
    krb5_error_code ret;
    krb5_salt k5salt;
    ssize_t sz;
    size_t key_versions = key->salt ? 2 : 1;
    size_t decrypted_key_length;
    char buf[2];
    krb5_data keylenbytes;
    unsigned int salttype;

    sz = append_string(context, sp, "\t%u\t%u\t%d\t%d\t", key_versions, kvno,
                        key->key.keytype, key->key.keyvalue.length + 2);
    if (sz == -1) return sz;
    ret = krb5_enctype_keysize(context, key->key.keytype, &decrypted_key_length);
    if (ret) return -1; /* XXX we lose the error code */
    buf[0] = decrypted_key_length & 0xff;
    buf[1] = (decrypted_key_length & 0xff00) >> 8;
    keylenbytes.data = buf;
    keylenbytes.length = sizeof (buf);
    sz = append_hex(context, sp, 1, 1, &keylenbytes);
    if (sz == -1) return sz;
    sz = append_hex(context, sp, 1, 1, &key->key.keyvalue);
    if (!key->salt)
        return sz;

    /* Map salt to MIT KDB style */
    switch (key->salt->type) {
    case KRB5_PADATA_PW_SALT:

        /*
         * Compute normal salt and then see whether it matches the stored one
         */
        ret = krb5_get_pw_salt(context, princ, &k5salt);
        if (ret) return -1;
        if (k5salt.saltvalue.length == key->salt->salt.length &&
            memcmp(k5salt.saltvalue.data, key->salt->salt.data,
                   k5salt.saltvalue.length) == 0)
            salttype = KRB5_KDB_SALTTYPE_NORMAL; /* matches */
        else if (key->salt->salt.length == strlen(princ->realm) &&
                 memcmp(key->salt->salt.data, princ->realm,
                        key->salt->salt.length) == 0)
            salttype = KRB5_KDB_SALTTYPE_ONLYREALM; /* matches realm */
        else if (key->salt->salt.length ==
		 k5salt.saltvalue.length - strlen(princ->realm) &&
                 memcmp((char *)k5salt.saltvalue.data + strlen(princ->realm),
                        key->salt->salt.data, key->salt->salt.length) == 0)
            salttype = KRB5_KDB_SALTTYPE_NOREALM; /* matches w/o realm */
        else
            salttype = KRB5_KDB_SALTTYPE_NORMAL;  /* hope for best */

	break;

    case KRB5_PADATA_AFS3_SALT:
        salttype = KRB5_KDB_SALTTYPE_AFS3;
	break;

    default:
	return -1;
    }

    sz = append_string(context, sp, "\t%u\t%u\t", salttype,
                       key->salt->salt.length);
    if (sz == -1) return sz;
    return append_hex(context, sp, 1, 1, &key->salt->salt);
}

static krb5_error_code
entry2string_int (krb5_context context, krb5_storage *sp, hdb_entry *ent)
{
    char *p;
    size_t i;
    krb5_error_code ret;

    /* --- principal */
    ret = krb5_unparse_name(context, ent->principal, &p);
    if(ret)
	return ret;
    append_string(context, sp, "%s ", p);
    free(p);
    /* --- kvno */
    append_string(context, sp, "%d", ent->kvno);
    /* --- keys */
    for(i = 0; i < ent->keys.len; i++){
	/* --- mkvno, keytype */
	if(ent->keys.val[i].mkvno)
	    append_string(context, sp, ":%d:%d:",
			  *ent->keys.val[i].mkvno,
			  ent->keys.val[i].key.keytype);
	else
	    append_string(context, sp, "::%d:",
			  ent->keys.val[i].key.keytype);
	/* --- keydata */
	append_hex(context, sp, 0, 0, &ent->keys.val[i].key.keyvalue);
	append_string(context, sp, ":");
	/* --- salt */
	if(ent->keys.val[i].salt){
	    append_string(context, sp, "%u/", ent->keys.val[i].salt->type);
	    append_hex(context, sp, 0, 0, &ent->keys.val[i].salt->salt);
	}else
	    append_string(context, sp, "-");
    }
    append_string(context, sp, " ");
    /* --- created by */
    append_event(context, sp, &ent->created_by);
    /* --- modified by */
    append_event(context, sp, ent->modified_by);

    /* --- valid start */
    if(ent->valid_start)
	append_string(context, sp, "%s ", time2str(*ent->valid_start));
    else
	append_string(context, sp, "- ");

    /* --- valid end */
    if(ent->valid_end)
	append_string(context, sp, "%s ", time2str(*ent->valid_end));
    else
	append_string(context, sp, "- ");

    /* --- password ends */
    if(ent->pw_end)
	append_string(context, sp, "%s ", time2str(*ent->pw_end));
    else
	append_string(context, sp, "- ");

    /* --- max life */
    if(ent->max_life)
	append_string(context, sp, "%d ", *ent->max_life);
    else
	append_string(context, sp, "- ");

    /* --- max renewable life */
    if(ent->max_renew)
	append_string(context, sp, "%d ", *ent->max_renew);
    else
	append_string(context, sp, "- ");

    /* --- flags */
    append_string(context, sp, "%d ", HDBFlags2int(ent->flags));

    /* --- generation number */
    if(ent->generation) {
	append_string(context, sp, "%s:%d:%d ", time2str(ent->generation->time),
		      ent->generation->usec,
		      ent->generation->gen);
    } else
	append_string(context, sp, "- ");

    /* --- extensions */
    if(ent->extensions && ent->extensions->len > 0) {
	for(i = 0; i < ent->extensions->len; i++) {
	    void *d;
	    size_t size, sz = 0;

	    ASN1_MALLOC_ENCODE(HDB_extension, d, size,
			       &ent->extensions->val[i], &sz, ret);
	    if (ret) {
		krb5_clear_error_message(context);
		return ret;
	    }
	    if(size != sz)
		krb5_abortx(context, "internal asn.1 encoder error");

	    if (hex_encode(d, size, &p) < 0) {
		free(d);
		krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
		return ENOMEM;
	    }

	    free(d);
	    append_string(context, sp, "%s%s", p,
			  ent->extensions->len - 1 != i ? ":" : "");
	    free(p);
	}
    } else
	append_string(context, sp, "-");

    return 0;
}

#define KRB5_KDB_DISALLOW_POSTDATED     0x00000001
#define KRB5_KDB_DISALLOW_FORWARDABLE   0x00000002
#define KRB5_KDB_DISALLOW_TGT_BASED     0x00000004
#define KRB5_KDB_DISALLOW_RENEWABLE     0x00000008
#define KRB5_KDB_DISALLOW_PROXIABLE     0x00000010
#define KRB5_KDB_DISALLOW_DUP_SKEY      0x00000020
#define KRB5_KDB_DISALLOW_ALL_TIX       0x00000040
#define KRB5_KDB_REQUIRES_PRE_AUTH      0x00000080
#define KRB5_KDB_REQUIRES_HW_AUTH       0x00000100
#define KRB5_KDB_REQUIRES_PWCHANGE      0x00000200
#define KRB5_KDB_DISALLOW_SVR           0x00001000
#define KRB5_KDB_PWCHANGE_SERVICE       0x00002000
#define KRB5_KDB_SUPPORT_DESMD5         0x00004000
#define KRB5_KDB_NEW_PRINC              0x00008000

static int
flags_to_attr(HDBFlags flags)
{
    int a = 0;

    if (!flags.postdate)
        a |= KRB5_KDB_DISALLOW_POSTDATED;
    if (!flags.forwardable)
        a |= KRB5_KDB_DISALLOW_FORWARDABLE;
    if (flags.initial)
        a |= KRB5_KDB_DISALLOW_TGT_BASED;
    if (!flags.renewable)
        a |= KRB5_KDB_DISALLOW_RENEWABLE;
    if (!flags.proxiable)
        a |= KRB5_KDB_DISALLOW_PROXIABLE;
    if (flags.invalid)
        a |= KRB5_KDB_DISALLOW_ALL_TIX;
    if (flags.require_preauth)
        a |= KRB5_KDB_REQUIRES_PRE_AUTH;
    if (flags.require_hwauth)
        a |= KRB5_KDB_REQUIRES_HW_AUTH;
    if (!flags.server)
        a |= KRB5_KDB_DISALLOW_SVR;
    if (flags.change_pw)
        a |= KRB5_KDB_PWCHANGE_SERVICE;
    return a;
}

krb5_error_code
entry2mit_string_int(krb5_context context, krb5_storage *sp, hdb_entry *ent)
{
    krb5_error_code ret;
    ssize_t sz;
    size_t i, k;
    size_t num_tl_data = 0;
    size_t num_key_data = 0;
    char *p;
    HDB_Ext_KeySet *hist_keys = NULL;
    HDB_extension *extp;
    time_t last_pw_chg = 0;
    time_t exp = 0;
    time_t pwexp = 0;
    unsigned int max_life = 0;
    unsigned int max_renew = 0;

    if (ent->modified_by)
        num_tl_data++;

    ret = hdb_entry_get_pw_change_time(ent, &last_pw_chg);
    if (ret) return ret;
    if (last_pw_chg)
        num_tl_data++;

    extp = hdb_find_extension(ent, choice_HDB_extension_data_hist_keys);
    if (extp)
        hist_keys = &extp->data.u.hist_keys;

    for (i = 0; i < ent->keys.len;i++) {
        if (ent->keys.val[i].key.keytype == ETYPE_DES_CBC_MD4 ||
            ent->keys.val[i].key.keytype == ETYPE_DES_CBC_MD5)
            continue;
        num_key_data++;
    }
    if (hist_keys) {
        for (i = 0; i < hist_keys->len; i++) {
            /*
             * MIT uses the highest kvno as the current kvno instead of
             * tracking kvno separately, so we can't dump keysets with kvno
             * higher than the entry's kvno.
             */
            if (hist_keys->val[i].kvno >= ent->kvno)
                continue;
            for (k = 0; k < hist_keys->val[i].keys.len; k++) {
                if (ent->keys.val[k].key.keytype == ETYPE_DES_CBC_MD4 ||
                    ent->keys.val[k].key.keytype == ETYPE_DES_CBC_MD5)
                    continue;
                num_key_data++;
            }
        }
    }

    ret = krb5_unparse_name(context, ent->principal, &p);
    if (ret) return ret;
    sz = append_string(context, sp, "princ\t38\t%u\t%u\t%u\t0\t%s\t%d",
                       strlen(p), num_tl_data, num_key_data, p,
                       flags_to_attr(ent->flags));
    free(p);
    if (sz == -1) return ENOMEM;

    if (ent->max_life)
        max_life = *ent->max_life;
    if (ent->max_renew)
        max_renew = *ent->max_renew;
    if (ent->valid_end)
        exp = *ent->valid_end;
    if (ent->pw_end)
        pwexp = *ent->pw_end;

    sz = append_string(context, sp, "\t%u\t%u\t%u\t%u\t0\t0\t0",
                       max_life, max_renew, exp, pwexp);
    if (sz == -1) return ENOMEM;

    /* Dump TL data we know: last pw chg and modified_by */
#define mit_KRB5_TL_LAST_PWD_CHANGE     1
#define mit_KRB5_TL_MOD_PRINC           2
    if (last_pw_chg) {
        krb5_data d;
        time_t val;
        unsigned char *ptr;
        
        ptr = (unsigned char *)&last_pw_chg;
        val = ((unsigned long)ptr[3] << 24) | (ptr[2] << 16)
	    | (ptr[1] << 8) | ptr[0];
        d.data = &val;
        d.length = sizeof (last_pw_chg);
        sz = append_string(context, sp, "\t%u\t%u\t",
                           mit_KRB5_TL_LAST_PWD_CHANGE, d.length);
        if (sz == -1) return ENOMEM;
        sz = append_hex(context, sp, 1, 1, &d);
        if (sz == -1) return ENOMEM;
    }
    if (ent->modified_by) {
        krb5_data d;
        unsigned int val;
        size_t plen;
        unsigned char *ptr;
        char *modby_p;

        ptr = (unsigned char *)&ent->modified_by->time;
        val = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
        d.data = &val;
        d.length = sizeof (ent->modified_by->time);
        ret = krb5_unparse_name(context, ent->modified_by->principal, &modby_p);
        if (ret)
	    return ret;
        plen = strlen(modby_p);
        sz = append_string(context, sp, "\t%u\t%u\t",
                           mit_KRB5_TL_MOD_PRINC,
                           d.length + plen + 1 /* NULL counted */);
        if (sz == -1) {
	    free(modby_p);
	    return ENOMEM;
	}
        sz = append_hex(context, sp, 1, 1, &d);
        if (sz == -1) {
            free(modby_p);
            return ENOMEM;
        }
        d.data = modby_p;
        d.length = plen + 1;
        sz = append_hex(context, sp, 1, 1, &d);
        free(modby_p);
        if (sz == -1)
	    return ENOMEM;
    }
    /*
     * Dump keys (remembering to not include any with kvno higher than
     * the entry's because MIT doesn't track entry kvno separately from
     * the entry's keys -- max kvno is it)
     */
    for (i = 0; i < ent->keys.len; i++) {
        if (ent->keys.val[i].key.keytype == ETYPE_DES_CBC_MD4 ||
            ent->keys.val[i].key.keytype == ETYPE_DES_CBC_MD5)
            continue;
        sz = append_mit_key(context, sp, ent->principal, ent->kvno,
                            &ent->keys.val[i]);
        if (sz == -1) return ENOMEM;
    }
    for (i = 0; hist_keys && i < ent->kvno; i++) {
        size_t m;

        /* dump historical keys */
        for (k = 0; k < hist_keys->len; k++) {
            if (hist_keys->val[k].kvno != ent->kvno - i)
                continue;
            for (m = 0; m < hist_keys->val[k].keys.len; m++) {
                if (ent->keys.val[k].key.keytype == ETYPE_DES_CBC_MD4 ||
                    ent->keys.val[k].key.keytype == ETYPE_DES_CBC_MD5)
                    continue;
                sz = append_mit_key(context, sp, ent->principal,
                                    hist_keys->val[k].kvno,
                                    &hist_keys->val[k].keys.val[m]);
                if (sz == -1) return ENOMEM;
            }
        }
    }
    sz = append_string(context, sp, "\t-1;"); /* "extra data" */
    if (sz == -1) return ENOMEM;
    return 0;
}

krb5_error_code
hdb_entry2string(krb5_context context, hdb_entry *ent, char **str)
{
    krb5_error_code ret;
    krb5_data data;
    krb5_storage *sp;

    sp = krb5_storage_emem();
    if (sp == NULL) {
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return ENOMEM;
    }

    ret = entry2string_int(context, sp, ent);
    if (ret) {
	krb5_storage_free(sp);
	return ret;
    }

    krb5_storage_write(sp, "\0", 1);
    krb5_storage_to_data(sp, &data);
    krb5_storage_free(sp);
    *str = data.data;
    return 0;
}

/* print a hdb_entry to (FILE*)data; suitable for hdb_foreach */

krb5_error_code
hdb_print_entry(krb5_context context, HDB *db, hdb_entry *entry,
                void *data)
{
    struct hdb_print_entry_arg *parg = data;
    krb5_error_code ret;
    krb5_storage *sp;

    fflush(parg->out);
    sp = krb5_storage_from_fd(fileno(parg->out));
    if (sp == NULL) {
	krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
	return ENOMEM;
    }

    switch (parg->fmt) {
    case HDB_DUMP_HEIMDAL:
        ret = entry2string_int(context, sp, entry);
        break;
    case HDB_DUMP_MIT:
        ret = entry2mit_string_int(context, sp, entry);
        break;
    default:
        heim_abort("Only two dump formats supported: Heimdal and MIT");
    }
    if (ret) {
	krb5_storage_free(sp);
	return ret;
    }

    krb5_storage_write(sp, "\n", 1);
    krb5_storage_free(sp);
    return 0;
}