summaryrefslogtreecommitdiffstats
path: root/src/lib/kadm/adm_kw_dec.c
blob: 35097383bc2a0701596620ed9fecfc494628d878 (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
/*
 * lib/kadm/adm_kw_dec.c
 *
 * Copyright 1995 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 */

/*
 * adm_kw_dec.c	- routines to decode keyword-value pairs.
 */
#include "k5-int.h"
#include "adm.h"
#include "adm_proto.h"

#define	char2int(c)	((c) - '0')

/*
 * keyword_value()	- Find index of keyword value if keyword is present.
 *
 * If a value is required, then the index of the keyword value is returned,
 * otherwise the index of the first character past the end of the keyword
 * string is returned.
 */
static off_t
keyword_value(dataentp, keyword, value_req)
    krb5_data		*dataentp;
    char		*keyword;
    krb5_boolean	value_req;
{
    off_t	len_req;

    len_req = strlen(keyword);
    if (value_req)
	len_req++;
    if ((dataentp->length >= len_req) &&
	(!strncmp(dataentp->data, keyword, strlen(keyword))) &&
	(!value_req || (dataentp->data[strlen(keyword)] == '=')))
	return(len_req);
    else
	return(-1);
}

/*
 * decode_kw_string()	- Decode a keyword=<string> pair and return the
 *			  string value if the pair is present.
 *
 * Note: successful callers must free the string storage.
 */
static krb5_error_code
decode_kw_string(dataentp, keyword, stringp)
    krb5_data	*dataentp;
    char	*keyword;
    char	**stringp;
{
    krb5_error_code	kret;
    off_t		valueoff;
    size_t		len2copy;

    kret = ENOENT;
    if ((valueoff = keyword_value(dataentp, keyword, 1)) >= 0) {
	kret = ENOMEM;
	len2copy = (size_t) ((off_t) dataentp->length - valueoff);
	*stringp = (char *) malloc(len2copy+1);
	if (*stringp) {
	    strncpy(*stringp, &dataentp->data[valueoff], len2copy);
	    (*stringp)[len2copy] = '\0';
	    kret = 0;
	}
    }
    return(kret);
}

/*
 * decode_kw_integer()	- Decode a keyword=<integer> pair and return the value
 *			  if the pair is present.
 */
static krb5_error_code
decode_kw_integer(dataentp, keyword, uintp)
    krb5_data	*dataentp;
    char	*keyword;
    krb5_ui_4	*uintp;
{
    krb5_error_code	kret;
    off_t		voff;
    size_t		len2copy;

    kret = ENOENT;
    if ((voff = keyword_value(dataentp, keyword, 1)) >= 0) {
	kret = EINVAL;
	len2copy = (size_t) ((off_t) dataentp->length - voff);
	if (len2copy == sizeof(krb5_ui_4)) {
	    *uintp = (((krb5_int32) ((unsigned char) dataentp->data[voff])
		       << 24) +
		      ((krb5_int32) ((unsigned char) dataentp->data[voff+1])
		       << 16) +
		      ((krb5_int32) ((unsigned char) dataentp->data[voff+2])
		       << 8) +
		      ((krb5_int32) ((unsigned char) dataentp->data[voff+3])));
	    kret = 0;
	}
    }
    return(kret);
}

/*
 * decode_kw_gentime()	- Decode a keyword=<general-time> pair and return the
 *			  time result if the pair is present.
 *
 * XXX - this knows too much about how Kerberos time is encoded.
 */
static krb5_error_code
decode_kw_gentime(dataentp, keyword, gtimep)
    krb5_data		*dataentp;
    char		*keyword;
    krb5_timestamp	*gtimep;
{
    krb5_error_code	kret;
    char		*timestring;
    struct tm		tval;
    time_t		temp_time;

    memset((char *) &tval, 0, sizeof(tval));
    timestring = (char *) NULL;
    if (!(kret = decode_kw_string(dataentp, keyword, &timestring))) {
	kret = EINVAL;
	if ((strlen(timestring) == 15) &&
	    (timestring[14] == 'Z')) {
	    tval.tm_year = 1000*char2int(timestring[0]) +
		100*char2int(timestring[1]) +
		    10*char2int(timestring[2]) +
			char2int(timestring[3]) - 1900;
	    tval.tm_mon = 10*char2int(timestring[4]) +
		char2int(timestring[5]) - 1;
	    tval.tm_mday = 10*char2int(timestring[6]) +
		char2int(timestring[7]);
	    tval.tm_hour = 10*char2int(timestring[8]) +
		char2int(timestring[9]);
	    tval.tm_min = 10*char2int(timestring[10]) +
		char2int(timestring[11]);
	    tval.tm_sec = 10*char2int(timestring[12]) +
		char2int(timestring[13]);
	    tval.tm_isdst = -1;
	    temp_time = gmt_mktime(&tval);
	    if (temp_time >= 0) {
		kret = 0;
		*gtimep = (krb5_timestamp) temp_time;
	    }
	}
	free(timestring);
    }
    return(kret);
}

/*
 * decode_kw_tagged()	- Decode a keyword=<taglist>...<data> list and return
 *			  the values of the tags and the data if the list is
 *			  present.
 */
static krb5_error_code
decode_kw_tagged(dataentp, keyword, ntags, taglist, lenp, datap)
    krb5_data		*dataentp;
    char		*keyword;
    krb5_int32		ntags;
    krb5_int32		*taglist;
    size_t		*lenp;
    krb5_octet		**datap;
{
    krb5_error_code	kret;
    off_t		valueoff;
    size_t		len2copy;
    unsigned char	*cp, *ep;
    int			i;

    kret = ENOENT;
    if ((valueoff = keyword_value(dataentp, keyword, 1)) >= 0) {
	/*
	 * Blast through the tags.
	 */
	kret = 0;
	cp = (unsigned char *) &dataentp->data[valueoff];
	ep = (unsigned char *) &dataentp->data[dataentp->length];
	for (i=0; i<ntags; i++) {
	    if (&cp[sizeof(krb5_int32)] > ep) {
		kret = EINVAL;
		break;
	    }
	    taglist[i] = (((krb5_int32) ((unsigned char) cp[0]) << 24) +
			  ((krb5_int32) ((unsigned char) cp[1]) << 16) +
			  ((krb5_int32) ((unsigned char) cp[2]) << 8) +
			  ((krb5_int32) ((unsigned char) cp[3])));
	    cp += sizeof(krb5_int32);
	}
	if (!kret) {
	    /*
	     * If we were successful, copy out the remaining bytes for value.
	     */
	    len2copy = (size_t) (ep - cp);
	    if (len2copy &&
		(*datap = (krb5_octet *) malloc(len2copy+1))) {
		memcpy(*datap, cp, len2copy);
		(*datap)[len2copy] = '\0';
	    }
	    if (len2copy && !*datap)
		kret = ENOMEM;
	    else
		*lenp = len2copy;
	}
    }
    return(kret);
}

#if !defined(_MSDOS) && !defined(_WIN32) && !defined(_MACINTOSH)
/*
 * krb5_adm_proto_to_dbent()	- Convert external attribute list into a
 *				  database entry.
 *
 * Scan through the keyword=value pairs in "data" until either the end of
 * the list (as determined from "nent") is reached, or an error occurs.
 * Return a mask of attributes which are set in "validp", the actual
 * attribute values in "dbentp" and "pwordp" if a password is specified.
 *
 * Successful callers must allocate the storage for "validp", "dbentp" and
 * must free the storage allocated for "pwordp" if a password is specified
 * and free the storage allocated for "validp->mod_name" if a modifier name
 * is specified.
 */
krb5_error_code
krb5_adm_proto_to_dbent(kcontext, nent, data, validp, dbentp, pwordp)
    krb5_context	kcontext;	/* Kerberos context	*/ /* In */
    krb5_int32		nent;		/* Number of components	*/ /* In */
    krb5_data		*data;		/* Component list	*/ /* In */
    krb5_ui_4		*validp;	/* Valid bitmask	*/ /* Out */
    krb5_db_entry	*dbentp;	/* Database entry	*/ /* Out */
    char		**pwordp;	/* Password string	*/ /* Out */
{
    int			i;
    krb5_error_code	retval;
    krb5_ui_4		parsed_mask;
    krb5_int32		taglist[4];
    size_t		data_length;
    krb5_octet		*tagged_data;
    struct key_tag_correlator {
	krb5_int32	key_tag;
	int		key_data_index;
    } *correlators, *correlation;
    int			ncorrelations;

    /* Initialize */
    retval = 0;
    parsed_mask = 0;
    *pwordp = (char *) NULL;
    correlators = (struct key_tag_correlator *) NULL;
    ncorrelations = 0;

    /* Loop through all the specified keyword=value pairs. */
    for (i=0; i<nent; i++) {
	/* Check for password */
	if (!(retval = decode_kw_string(&data[i],
					KRB5_ADM_KW_PASSWORD,
					pwordp))) {
	    parsed_mask |= KRB5_ADM_M_PASSWORD;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for maximum lifetime */
	if (!(retval = decode_kw_integer(&data[i],
					 KRB5_ADM_KW_MAXLIFE,
					 (krb5_ui_4 *) &dbentp->max_life))) {
	    parsed_mask |= KRB5_ADM_M_MAXLIFE;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for maximum renewable lifetime */
	if (!(retval = decode_kw_integer(&data[i],
					 KRB5_ADM_KW_MAXRENEWLIFE,
					 (krb5_ui_4 *)
					 &dbentp->max_renewable_life))) {
	    parsed_mask |= KRB5_ADM_M_MAXRENEWLIFE;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for principal expiration */
	if (!(retval = decode_kw_gentime(&data[i],
					 KRB5_ADM_KW_EXPIRATION,
					 &dbentp->expiration))) {
	    parsed_mask |= KRB5_ADM_M_EXPIRATION;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for password expiration */
	if (!(retval = decode_kw_gentime(&data[i],
					 KRB5_ADM_KW_PWEXPIRATION,
					 &dbentp->pw_expiration))) {
	    parsed_mask |= KRB5_ADM_M_PWEXPIRATION;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* random key - value optional */
	if (keyword_value(&data[i],
			  KRB5_ADM_KW_RANDOMKEY,
			  0) >= 0) {
	    krb5_ui_4	value;

	    if (retval = decode_kw_integer(&data[i],
					   KRB5_ADM_KW_RANDOMKEY,
					   &value)) {
		value = 1;
		retval = 0;
	    }
	    if (value)
		parsed_mask |= KRB5_ADM_M_RANDOMKEY;
	    else
		parsed_mask &= ~KRB5_ADM_M_RANDOMKEY;
	    continue;
	}

	/* Check for flags */
	if (!(retval = decode_kw_integer(&data[i],
					 KRB5_ADM_KW_FLAGS,
					 (krb5_ui_4 *) &dbentp->attributes))) {
	    parsed_mask |= KRB5_ADM_M_FLAGS;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for last successful password entry */
	if (!(retval = decode_kw_gentime(&data[i],
					 KRB5_ADM_KW_LASTSUCCESS,
					 &dbentp->last_success))) {
	    parsed_mask |= KRB5_ADM_M_LASTSUCCESS;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for last failed entry */
	if (!(retval = decode_kw_gentime(&data[i],
					 KRB5_ADM_KW_LASTFAILED,
					 &dbentp->last_failed))) {
	    parsed_mask |= KRB5_ADM_M_LASTFAILED;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for failure count */
	if (!(retval = decode_kw_integer(&data[i],
					 KRB5_ADM_KW_FAILCOUNT,
					 (krb5_ui_4 *)
					 &dbentp->fail_auth_count))) {
	    parsed_mask |= KRB5_ADM_M_FAILCOUNT;
	    continue;
	}
	else {
	    if (retval != ENOENT)
		break;
	}

	/* Check for auxiliary data */
	if (!(retval = decode_kw_tagged(&data[i],
					KRB5_ADM_KW_AUXDATA,
					1,
					taglist,
					&data_length,
					&tagged_data))) {
	    krb5_tl_data	**fixupp;
	    krb5_tl_data	*tl_data, *new_tl;

	    /*
	     * We've got a tagged data value here.  We've got to do a little
	     * work to put it in the right place.  First, find the right place.
	     */
	    fixupp = &dbentp->tl_data;
	    for (tl_data = dbentp->tl_data;
		 tl_data; 
		 tl_data = tl_data->tl_data_next)
		fixupp = &tl_data->tl_data_next;

	    /* Get memory */
	    if (new_tl = (krb5_tl_data *) malloc(sizeof(krb5_tl_data))) {
		/* Fill in the supplied values */
		new_tl->tl_data_type = (krb5_int16) taglist[0];
		new_tl->tl_data_length = (krb5_int16) data_length;
		new_tl->tl_data_contents = tagged_data;

		/* Link in the right place */
		new_tl->tl_data_next= *fixupp;
		*fixupp = new_tl;

		/* Update counters and flags */
		dbentp->n_tl_data++;
		parsed_mask |= KRB5_ADM_M_AUXDATA;
	    }
	    else {
		retval = ENOMEM;
		break;
	    }
	    continue;
	}
	else {
	    if ((retval != ENOENT) && (retval != EINVAL))
		break;
	}

	/* Check for key data */
	if (!(retval = decode_kw_tagged(&data[i],
					KRB5_ADM_KW_KEYDATA,
					3,
					taglist,
					&data_length,
					&tagged_data))) {
	    krb5_boolean	corr_found;
	    int			cindex, kindex;
	    krb5_key_data	*kdata;

	    /*
	     * See if we already have a correlation betwen our key-tag and
	     * an index into the key table.
	     */
	    corr_found = 0;
	    for (cindex = 0; cindex < ncorrelations; cindex++) {
		if (correlators[cindex].key_tag == taglist[0]) {
		    correlation = &correlators[cindex];
		    corr_found = 1;
		    break;
		}
	    }

	    /* If not, then we had better make one up */
	    if (!corr_found) {
		/* Get a new list */
		if (correlation = (struct key_tag_correlator *)
		    malloc((ncorrelations+1)*
			   sizeof(struct key_tag_correlator))) {
		    /* Save the contents of the old one. */
		    if (ncorrelations) {
			memcpy(correlation, correlators,
			       ncorrelations*
			       sizeof(struct key_tag_correlator));
			/* Free the old one */
			free(correlators);
		    }
		    /* Point us at the new relation */
		    correlators = correlation;
		    correlation = &correlators[ncorrelations];
		    ncorrelations++;
		    correlation->key_tag = taglist[0];
		    /* Make a new key data entry */
		    if (kdata = (krb5_key_data *)
			malloc((dbentp->n_key_data+1)*sizeof(krb5_key_data))) {
			/* Copy the old list */
			if (dbentp->n_key_data) {
			    memcpy(kdata, dbentp->key_data,
				   dbentp->n_key_data*sizeof(krb5_key_data));
			    free(dbentp->key_data);
			}
			dbentp->key_data = kdata;
			correlation->key_data_index = dbentp->n_key_data;
			memset(&kdata[dbentp->n_key_data], 0,
			       sizeof(krb5_key_data));
			kdata[dbentp->n_key_data].key_data_ver = 1;
			dbentp->n_key_data++;
			corr_found = 1;
		    }
		    else
			retval = ENOMEM;
		}
		else
		    retval = ENOMEM;
	    }

	    /* Check to see if we either found a correlation or made one */
	    if (corr_found) {
		/* Special case for key version number */
		if (taglist[1] == -1) {
		    dbentp->key_data[correlation->key_data_index].
			key_data_kvno = taglist[2];
		}
		else {
		    dbentp->key_data[correlation->key_data_index].
			key_data_type[taglist[1]] = taglist[2];
		    dbentp->key_data[correlation->key_data_index].
			key_data_length[taglist[1]] = (krb5_int16) data_length;
		    dbentp->key_data[correlation->key_data_index].
			key_data_contents[taglist[1]] = tagged_data;
		}
		parsed_mask |= KRB5_ADM_M_KEYDATA;
	    }
	    else
		break;
	    continue;
	}
	else {
	    if ((retval != ENOENT) && (retval != EINVAL))
		break;
	}

	/* Check for extra data */
	if (!(retval = decode_kw_tagged(&data[i],
					KRB5_ADM_KW_EXTRADATA,
					0,
					taglist,
					&data_length,
					&dbentp->e_data))) {
	    dbentp->e_length = (krb5_int16) data_length;
	    parsed_mask |= KRB5_ADM_M_EXTRADATA;
	    continue;
	}
	else {
	    if ((retval != ENOENT) && (retval != EINVAL))
		break;
	}

	/* If we fall through here, we've got something unrecognized */
	if (retval) {
	    retval = EINVAL;
	    break;
	}
    }

    if (retval) {
	if (*pwordp) {
	    memset(*pwordp, 0, strlen(*pwordp));
	    free(*pwordp);
	    *pwordp = (char *) NULL;
	}
	parsed_mask = 0;
    }
    if (correlators)
	free(correlators);
    *validp |= parsed_mask;
    return(retval);
}
#endif