summaryrefslogtreecommitdiffstats
path: root/src/plugins/preauth/pkinit/pkinit_profile.c
blob: 1f7045aca8766d085dc11674c256d5fadf244397 (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
/*
 * COPYRIGHT (C) 2006,2007
 * THE REGENTS OF THE UNIVERSITY OF MICHIGAN
 * ALL RIGHTS RESERVED
 *
 * Permission is granted to use, copy, create derivative works
 * and redistribute this software and such derivative works
 * for any purpose, so long as the name of The University of
 * Michigan is not used in any advertising or publicity
 * pertaining to the use of distribution of this software
 * without specific, written prior authorization.  If the
 * above copyright notice or any other identification of the
 * University of Michigan is included in any copy of any
 * portion of this software, then the disclaimer below must
 * also be included.
 *
 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
 * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
 * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
 * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
 * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
 * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
 * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
 * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGES.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "k5-int.h"
#include "pkinit.h"

/*
 * Routines for handling profile [config file] options
 */

/* Forward prototypes */
static int _krb5_conf_boolean(const char *s);

/*
 * XXX
 * The following is duplicated verbatim from src/lib/krb5/krb/get_in_tkt.c,
 * which is duplicated from somewhere else. :-/
 * XXX
 */
static const char *const conf_yes[] = {
    "y", "yes", "true", "t", "1", "on",
    0,
};

static const char *const conf_no[] = {
    "n", "no", "false", "nil", "0", "off",
    0,
};

static int
_krb5_conf_boolean(const char *s)
{
    const char *const *p;

    for(p=conf_yes; *p; p++) {
	if (strcasecmp(*p,s) == 0)
	    return 1;
    }

    for(p=conf_no; *p; p++) {
	if (strcasecmp(*p,s) == 0)
	    return 0;
    }

    /* Default to "no" */
    return 0;
}

/*
 * XXX
 * End duplicated code from src/lib/krb5/krb/get_in_tkt.c
 * XXX
 */

/*
 * The following are based on krb5_libdefault_* functions in
 * src/lib/krb5/krb/get_in_tkt.c
 * N.B.  This assumes that context->default_realm has
 * already been established.
 */
krb5_error_code
pkinit_kdcdefault_strings(krb5_context context, const char *realmname,
			  const char *option, char ***ret_value)
{
    profile_t profile = NULL;
    const char *names[5];
    char **values = NULL;
    krb5_error_code retval;

    if (context == NULL)
	return KV5M_CONTEXT;

    profile = context->profile;

    if (realmname != NULL) {
	/*
	 * Try number one:
	 *
	 * [realms]
	 *	    REALM = {
	 *		option = <value>
	 *	    }
	 */

	names[0] = "realms";
	names[1] = realmname;
	names[2] = option;
	names[3] = 0;
	retval = profile_get_values(profile, names, &values);
	if (retval == 0 && values != NULL)
	    goto goodbye;
    }

    /*
     * Try number two:
     *
     * [kdcdefaults]
     *	    option = <value>
     */

    names[0] = "kdcdefaults";
    names[1] = option;
    names[2] = 0;
    retval = profile_get_values(profile, names, &values);
    if (retval == 0 && values != NULL)
	goto goodbye;

goodbye:
    if (values == NULL)
	retval = ENOENT;

    *ret_value = values;

    return retval;

}

krb5_error_code
pkinit_kdcdefault_string(krb5_context context, const char *realmname,
			 const char *option, char **ret_value)
{
    krb5_error_code retval;
    char **values = NULL;

    retval = pkinit_kdcdefault_strings(context, realmname, option, &values);
    if (retval)
	return retval;

    if (values[0] == NULL) {
	retval = ENOENT;
    } else {
	*ret_value = strdup(values[0]);
	if (*ret_value == NULL)
	    retval = ENOMEM;
    }

    profile_free_list(values);
    return retval;
}

krb5_error_code
pkinit_kdcdefault_boolean(krb5_context context, const char *realmname,
			  const char *option, int default_value, int *ret_value)
{
    char *string = NULL;
    krb5_error_code retval;

    retval = pkinit_kdcdefault_string(context, realmname, option, &string);

    if (retval == 0) {
	*ret_value = _krb5_conf_boolean(string);
	free(string);
    } else
	*ret_value = default_value;

    return 0;
}

krb5_error_code
pkinit_kdcdefault_integer(krb5_context context, const char *realmname,
			  const char *option, int default_value, int *ret_value)
{
    char *string = NULL;
    krb5_error_code retval;

    retval = pkinit_kdcdefault_string(context, realmname, option, &string);

    if (retval == 0) {
	char *endptr;
	long l;
	l = strtol(string, &endptr, 0);
	if (endptr == string)
	    *ret_value = default_value;
	else
	    *ret_value = l;
	free(string);
    } else
	*ret_value = default_value;

    return 0;
}


/*
 * krb5_libdefault_string() is defined as static in
 * src/lib/krb5/krb/get_in_tkt.c.  Create local versions of
 * krb5_libdefault_* functions here.  We need a libdefaults_strings()
 * function which is not currently supported there anyway.  Also,
 * add the ability to supply a default value for the boolean and
 * integer functions.
 */

krb5_error_code
pkinit_libdefault_strings(krb5_context context, const krb5_data *realm,
			  const char *option, char ***ret_value)
{
    profile_t profile;
    const char *names[5];
    char **values = NULL;
    krb5_error_code retval;
    char realmstr[1024];

    if (realm != NULL && realm->length > sizeof(realmstr)-1)
	return EINVAL;

    if (realm != NULL) {
	strncpy(realmstr, realm->data, realm->length);
	realmstr[realm->length] = '\0';
    }

    if (!context || (context->magic != KV5M_CONTEXT))
	return KV5M_CONTEXT;

    profile = context->profile;


    if (realm != NULL) {
	/*
	 * Try number one:
	 *
	 * [libdefaults]
	 *	  REALM = {
	 *		  option = <value>
	 *	  }
	 */

	names[0] = "libdefaults";
	names[1] = realmstr;
	names[2] = option;
	names[3] = 0;
	retval = profile_get_values(profile, names, &values);
	if (retval == 0 && values != NULL && values[0] != NULL)
	    goto goodbye;

	/*
	 * Try number two:
	 *
	 * [realms]
	 *	REALM = {
	 *		option = <value>
	 *	}
	 */

	names[0] = "realms";
	names[1] = realmstr;
	names[2] = option;
	names[3] = 0;
	retval = profile_get_values(profile, names, &values);
	if (retval == 0 && values != NULL && values[0] != NULL)
	    goto goodbye;
    }

    /*
     * Try number three:
     *
     * [libdefaults]
     *	      option = <value>
     */

    names[0] = "libdefaults";
    names[1] = option;
    names[2] = 0;
    retval = profile_get_values(profile, names, &values);
    if (retval == 0 && values != NULL && values[0] != NULL)
	goto goodbye;

goodbye:
    if (values == NULL)
	return ENOENT;

    *ret_value = values;

    return retval;
}

krb5_error_code
pkinit_libdefault_string(krb5_context context, const krb5_data *realm,
			 const char *option, char **ret_value)
{
    krb5_error_code retval;
    char **values = NULL;

    retval = pkinit_libdefault_strings(context, realm, option, &values);
    if (retval)
	return retval;

    if (values[0] == NULL) {
	retval = ENOENT;
    } else {
	*ret_value = strdup(values[0]);
	if (*ret_value == NULL)
	    retval = ENOMEM;
    }

    profile_free_list(values);
    return retval;
}

krb5_error_code
pkinit_libdefault_boolean(krb5_context context, const krb5_data *realm,
			  const char *option, int default_value,
			  int *ret_value)
{
    char *string = NULL;
    krb5_error_code retval;

    retval = pkinit_libdefault_string(context, realm, option, &string);

   if (retval == 0) {
	*ret_value = _krb5_conf_boolean(string);
	free(string);
    } else
	*ret_value = default_value;

    return 0;
}

krb5_error_code
pkinit_libdefault_integer(krb5_context context, const krb5_data *realm,
			  const char *option, int default_value,
			  int *ret_value)
{
    char *string = NULL;
    krb5_error_code retval;

    retval = pkinit_libdefault_string(context, realm, option, &string);

    if (retval == 0) {
	char *endptr;
	long l;
	l = strtol(string, &endptr, 0);
	if (endptr == string)
	    *ret_value = default_value;
	else
	    *ret_value = l;
	free(string);
    }

    return retval;
}