summaryrefslogtreecommitdiffstats
path: root/src/lib/kadm/alt_prof.c
blob: caf5abf394cbe2a42a78e08a645ab45fc386854d (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
/*
 * lib/kadm/alt_prof.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.
 *
 */

/*
 * alt_prof.c - Implement alternate profile file handling.
 */
#include "k5-int.h"
#include "adm.h"
#include <stdio.h>

/*
 * krb5_aprof_init()	- Initialize alternate profile context.
 *
 * Parameters:
 *	fname		- default file name of the profile.
 *	envname		- environment variable name which can override fname.
 *	acontextp	- Pointer to opaque context for alternate profile.
 *
 * Returns:
 *	error codes from profile_init()
 */
krb5_error_code
krb5_aprof_init(fname, envname, acontextp)
    char		*fname;
    char		*envname;
    krb5_pointer	*acontextp;
{
    krb5_error_code	kret;
    const char		*namelist[2];
    profile_t		profile;
    
    namelist[1] = (char *) NULL;
    profile = (profile_t) NULL;
    if (envname) {
	if (namelist[0] = getenv(envname)) {
	    if (!(kret = profile_init(namelist, &profile))) {
		*acontextp = (krb5_pointer) profile;
		return(0);
	    }
	}
    }
    namelist[0] = fname;
    profile = (profile_t) NULL;
    if (!(kret = profile_init(namelist, &profile))) {
	*acontextp = (krb5_pointer) profile;
	return(0);
    }
    return(kret);
}

/*
 * krb5_aprof_getvals()	- Get values from alternate profile.
 *
 * Parameters:
 *	acontext	- opaque context for alternate profile.
 *	hierarchy	- hierarchy of value to retrieve.
 *	retdata		- Returned data values.
 *
 * Returns:
 * 	error codes from profile_get_values()
 */
krb5_error_code
krb5_aprof_getvals(acontext, hierarchy, retdata)
    krb5_pointer	acontext;
    const char		**hierarchy;
    char		***retdata;
{
    return(profile_get_values((profile_t) acontext,
			      hierarchy,
			      retdata));
}

/*
 * krb5_aprof_get_deltat()	- Get a delta time value from the alternate
 *				  profile.
 *
 * Parameters:
 *	acontext		- opaque context for alternate profile.
 *	hierarchy		- hierarchy of value to retrieve.
 *	uselast			- if true, use last value, otherwise use
 *				  first value found.
 *	deltatp			- returned delta time value.
 *
 * Returns:
 * 	error codes from profile_get_values()
 *	EINVAL			- Invalid syntax.
 *
 * Valid formats are:
 *	<days>-<hours>:<minutes>:<seconds>
 *	<days>d <hours>h <minutes>m <seconds>s
 *	<hours>:<minutes>:<seconds>
 *	<hours>h <minutes>m <seconds>s
 *	<hours>:<minutes>
 *	<hours>h <minutes>m
 *	<seconds>
 */
krb5_error_code
krb5_aprof_get_deltat(acontext, hierarchy, uselast, deltatp)
    krb5_pointer	acontext;
    const char		**hierarchy;
    krb5_boolean	uselast;
    krb5_deltat		*deltatp;
{
    krb5_error_code	kret;
    char		**values;
    char		*valp;
    int			index;
    krb5_boolean	found;
    int			days, hours, minutes, seconds;
    krb5_deltat		dt;

    if (!(kret = krb5_aprof_getvals(acontext, hierarchy, &values))) {
	index = 0;
	if (uselast) {
	    for (index=0; values[index]; index++);
	    index--;
	}
	valp = values[index];
	days = hours = minutes = seconds = 0;
	found = 0;

	/*
	 * Blast our way through potential syntaxes until we find a match.
	 */
	if (sscanf(valp, "%d-%d:%d:%d", &days, &hours, &minutes, &seconds)
	    == 4)
	    found = 1;
	else if (sscanf(valp, "%dd %dh %dm %ds",
			&days, &hours, &minutes, &seconds) == 4)
	    found = 1;
	else if (sscanf(valp, "%d:%d:%d", &hours, &minutes, &seconds) == 3) {
	    found = 1;
	    days = 0;
	}
	else if (sscanf(valp, "%dh %dm %ds", &hours, &minutes, &seconds)
		 == 3) {
	    found = 1;
	    days = 0;
	}
	else if (sscanf(valp, "%d:%d", &hours, &minutes) == 2) {
	    found = 1;
	    days = seconds = 0;
	}
	else if (sscanf(valp, "%dh %dm", &hours, &minutes) == 2) {
	    found = 1;
	    days = seconds = 0;
	}
	else if (sscanf(valp, "%d", &seconds) == 1) {
	    found = 1;
	    days = hours = minutes = 0;
	}

	/* If found, calculate the delta value */
	if (found) {
	    dt = days;
	    dt *= 24;
	    dt += hours;
	    dt *= 60;
	    dt += minutes;
	    dt *= 60;
	    dt += seconds;
	    *deltatp = dt;
	}
	else
	    kret = EINVAL;

	/* Free the string storage */
	for (index=0; values[index]; index++)
	    krb5_xfree(values[index]);
	krb5_xfree(values);
    }
    return(kret);
}

/*
 * krb5_aprof_get_string()	- Get a string value from the alternate
 *				  profile.
 *
 * Parameters:
 *	acontext		- opaque context for alternate profile.
 *	hierarchy		- hierarchy of value to retrieve.
 *	uselast			- if true, use last value, otherwise use
 *				  first value found.
 *	stringp			- returned string value.
 *
 * Returns:
 * 	error codes from profile_get_values()
 */
krb5_error_code
krb5_aprof_get_string(acontext, hierarchy, uselast, stringp)
    krb5_pointer	acontext;
    const char		**hierarchy;
    krb5_boolean	uselast;
    char		**stringp;
{
    krb5_error_code	kret;
    char		**values;
    int			index, i;

    if (!(kret = krb5_aprof_getvals(acontext, hierarchy, &values))) {
	index = 0;
	if (uselast) {
	    for (index=0; values[index]; index++);
	    index--;
	}

	*stringp = values[index];

	/* Free the string storage */
	for (i=0; values[i]; i++)
	    if (i != index)
		krb5_xfree(values[i]);
	krb5_xfree(values);
    }
    return(kret);
}

/*
 * krb5_aprof_get_int32()	- Get a 32-bit integer value from the alternate
 *				  profile.
 *
 * Parameters:
 *	acontext		- opaque context for alternate profile.
 *	hierarchy		- hierarchy of value to retrieve.
 *	uselast			- if true, use last value, otherwise use
 *				  first value found.
 *	intp			- returned 32-bit integer value.
 *
 * Returns:
 * 	error codes from profile_get_values()
 *	EINVAL			- value is not an integer
 */
krb5_error_code
krb5_aprof_get_int32(acontext, hierarchy, uselast, intp)
    krb5_pointer	acontext;
    const char		**hierarchy;
    krb5_boolean	uselast;
    krb5_int32		*intp;
{
    krb5_error_code	kret;
    char		**values;
    int			index;

    if (!(kret = krb5_aprof_getvals(acontext, hierarchy, &values))) {
	index = 0;
	if (uselast) {
	    for (index=0; values[index]; index++);
	    index--;
	}

	if (sscanf(values[index], "%d", intp) != 1)
	    kret = EINVAL;

	/* Free the string storage */
	for (index=0; values[index]; index++)
	    krb5_xfree(values[index]);
	krb5_xfree(values);
    }
    return(kret);
}

/*
 * krb5_aprof_finish()	- Finish alternate profile context.
 *
 * Parameter:
 *	acontext	- opaque context for alternate profile.
 *
 * Returns:
 *	0 on success, something else on failure.
 */
krb5_error_code
krb5_aprof_finish(acontext)
    krb5_pointer	acontext;
{
    profile_release(acontext);
}

/*
 * krb5_read_realm_params()	- Read per-realm parameters from KDC
 *				  alternate profile.
 */
krb5_error_code
krb5_read_realm_params(kcontext, realm, kdcprofile, kdcenv, rparamp)
    krb5_context	kcontext;
    char		*realm;
    char		*kdcprofile;
    char		*kdcenv;
    krb5_realm_params	**rparamp;
{
    char		*filename;
    char		*envname;
    char		*lrealm;
    krb5_pointer	aprofile;
    krb5_realm_params	*rparams;
    const char		*hierarchy[4];
    char		*svalue;
    krb5_int32		ivalue;
    krb5_deltat		dtvalue;

    krb5_error_code	kret;

    filename = (kdcprofile) ? kdcprofile : DEFAULT_KDC_PROFILE;
    envname = (kdcenv) ? kdcenv : KDC_PROFILE_ENV;
    rparams = (krb5_realm_params *) NULL;
    kret = 0;
    if (!realm)
	kret = krb5_get_default_realm(kcontext, &lrealm);
    else
	lrealm = strdup(realm);
    if (!kret && !(kret = krb5_aprof_init(filename, envname, &aprofile))) {
	if (rparams =
	    (krb5_realm_params *) malloc(sizeof(krb5_realm_params))) {

	    /* Initialize realm parameters */
	    memset((char *) rparams, 0, sizeof(krb5_realm_params));

	    /* Get the value of the per-realm profile */
	    hierarchy[0] = "realms";
	    hierarchy[1] = lrealm;
	    hierarchy[2] = "profile";
	    hierarchy[3] = (char *) NULL;
	    if (!krb5_aprof_get_string(aprofile, hierarchy, TRUE, &svalue)) {
		const char *filenames[2];

		/*
		 * XXX this knows too much about krb5 contexts.
		 */
		filenames[0] = svalue;
		filenames[1] = (char *) NULL;
		if (kcontext->profile)
		    profile_release(kcontext->profile);
		if (!(kret = profile_init(filenames, &kcontext->profile)))
		    rparams->realm_profile = svalue;
		else
		    krb5_xfree(svalue);
	    }

	    /* Get the value for the database */
	    hierarchy[2] = "database_name";
	    if (!krb5_aprof_get_string(aprofile, hierarchy, TRUE, &svalue))
		rparams->realm_dbname = svalue;

	    /* Get the value for the KDC primary port */
	    hierarchy[2] = "port";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_kdc_pport = ivalue;
		rparams->realm_kdc_pport_valid = 1;
	    }
	    
	    /* Get the value for the KDC secondary port */
	    hierarchy[2] = "secondary_port";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_kdc_sport = ivalue;
		rparams->realm_kdc_sport_valid = 1;
	    }
	    
	    /* Get the value for the kadmind port */
	    hierarchy[2] = "kadmind_port";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_kadmind_port = ivalue;
		rparams->realm_kadmind_port_valid = 1;
	    }
	    
	    /* Get the value for the master key name */
	    hierarchy[2] = "master_key_name";
	    if (!krb5_aprof_get_string(aprofile, hierarchy, TRUE, &svalue))
		rparams->realm_mkey_name = svalue;
	    
	    /* Get the value for the master key type */
	    hierarchy[2] = "master_key_type";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_keytype = ivalue;
		rparams->realm_keytype_valid = 1;
	    }
	    
	    /* Get the value for the encryption type */
	    hierarchy[2] = "encryption_type";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_enctype = ivalue;
		rparams->realm_enctype_valid = 1;
	    }
	    
	    /* Get the value for the stashfile */
	    hierarchy[2] = "key_stash_file";
	    if (!krb5_aprof_get_string(aprofile, hierarchy, TRUE, &svalue))
		rparams->realm_stash_file = svalue;
	    
	    /* Get the value for maximum ticket lifetime. */
	    hierarchy[2] = "max_life";
	    if (!krb5_aprof_get_deltat(aprofile, hierarchy, TRUE, &dtvalue)) {
		rparams->realm_max_life = dtvalue;
		rparams->realm_max_life_valid = 1;
	    }
	    
	    /* Get the value for maximum renewable ticket lifetime. */
	    hierarchy[2] = "max_renewable_life";
	    if (!krb5_aprof_get_deltat(aprofile, hierarchy, TRUE, &dtvalue)) {
		rparams->realm_max_rlife = dtvalue;
		rparams->realm_max_rlife_valid = 1;
	    }
	    
	    /* Get the value for the default principal expiration */
	    hierarchy[2] = "default_principal_expiration";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_expiration = (krb5_timestamp) ivalue;
		rparams->realm_expiration_valid = 1;
	    }
	    
	    /* Get the value for the default principal flags */
	    hierarchy[2] = "default_principal_flags";
	    if (!krb5_aprof_get_int32(aprofile, hierarchy, TRUE, &ivalue)) {
		rparams->realm_flags = (krb5_flags) ivalue;
		rparams->realm_flags_valid = 1;
	    }
	}
	krb5_aprof_finish(aprofile);
    }
    *rparamp = rparams;
    return(kret);
}

/*
 * krb5_free_realm_params()	- Free data allocated by above.
 */
krb5_error_code
krb5_free_realm_params(kcontext, rparams)
    krb5_context	kcontext;
    krb5_realm_params	*rparams;
{
    if (rparams) {
	if (rparams->realm_profile)
	    krb5_xfree(rparams->realm_profile);
	if (rparams->realm_dbname)
	    krb5_xfree(rparams->realm_dbname);
	if (rparams->realm_mkey_name)
	    krb5_xfree(rparams->realm_mkey_name);
	if (rparams->realm_stash_file)
	    krb5_xfree(rparams->realm_stash_file);
	krb5_xfree(rparams);
    }
    return(0);
}