File: config.c

package info (click to toggle)
krb5-strength 3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,668 kB
  • ctags: 876
  • sloc: sh: 11,907; ansic: 8,234; perl: 1,208; makefile: 167
file content (378 lines) | stat: -rw-r--r-- 10,590 bytes parent folder | download
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
/*
 * Retrieve configuration settings from krb5.conf.
 *
 * Provided here are functions to retrieve boolean, numeric, and string
 * settings from krb5.conf.  This wraps the somewhat awkward
 * krb5_appdefaults_* functions.
 *
 * Written by Russ Allbery <eagle@eyrie.org>
 * Copyright 2013
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * See LICENSE for licensing terms.
 */

#include <config.h>
#include <portable/krb5.h>
#include <portable/system.h>

#include <ctype.h>
#include <errno.h>

#include <plugin/internal.h>
#include <util/macros.h>

/* The representation of the realm differs between MIT and Kerberos. */
#ifdef HAVE_KRB5_REALM
typedef krb5_realm realm_type;
#else
typedef krb5_data *realm_type;
#endif


/*
 * Obtain the default realm and translate it into the format required by
 * krb5_appdefault_*.  This is obnoxious for MIT Kerberos, which returns the
 * default realm as a string but expects the realm as a krb5_data type when
 * calling krb5_appdefault_*.
 */
#ifdef HAVE_KRB5_REALM

static realm_type
default_realm(krb5_context ctx)
{
    krb5_error_code code;
    realm_type realm;

    code = krb5_get_default_realm(ctx, &realm);
    if (code != 0)
        realm = NULL;
    return realm;
}

#else /* !HAVE_KRB5_REALM */

static realm_type
default_realm(krb5_context ctx)
{
    char *realm = NULL;
    krb5_error_code code;
    krb5_data *realm_data;

    realm_data = calloc(1, sizeof(krb5_data));
    if (realm_data == NULL)
        return NULL;
    code = krb5_get_default_realm(ctx, &realm);
    if (code != 0) {
        free(realm);
        return NULL;
    }
    realm_data->magic = KV5M_DATA;
    realm_data->data = strdup(realm);
    if (realm_data->data == NULL) {
        free(realm_data);
        krb5_free_default_realm(ctx, realm);
        return NULL;
    }
    realm_data->length = strlen(realm);
    krb5_free_default_realm(ctx, realm);
    return realm_data;
}

#endif /* !HAVE_KRB5_REALM */


/*
 * Free the default realm data in whatever form it was generated for the calls
 * to krb5_appdefault_*.
 */
#ifdef HAVE_KRB5_REALM

static void
free_default_realm(krb5_context ctx UNUSED, realm_type realm)
{
    krb5_free_default_realm(ctx, realm);
}

#else /* !HAVE_KRB5_REALM */

static void
free_default_realm(krb5_context ctx UNUSED, realm_type realm)
{
    free(realm->data);
    free(realm);
}

#endif /* !HAVE_KRB5_REALM */


/*
 * Helper function to parse a number.  Takes the string to parse, the unsigned
 * int in which to store the number, and the pointer to set to the first
 * invalid character after the number.  Returns true if a number could be
 * successfully parsed and false otherwise.
 */
static bool
parse_number(const char *string, unsigned long *result, const char **end)
{
    unsigned long value;

    errno = 0;
    value = strtoul(string, (char **) end, 10);
    if (errno != 0 || *end == string)
        return false;
    *result = value;
    return true;
}


/*
 * Load a boolean option from Kerberos appdefaults.  Takes the Kerberos
 * context, the option, and the result location.
 */
void
strength_config_boolean(krb5_context ctx, const char *opt, bool *result)
{
    realm_type realm;
    int tmp;

    /*
     * The MIT version of krb5_appdefault_boolean takes an int * and the
     * Heimdal version takes a krb5_boolean *, so hope that Heimdal always
     * defines krb5_boolean to int or this will require more portability work.
     */
    realm = default_realm(ctx);
    krb5_appdefault_boolean(ctx, "krb5-strength", realm, opt, *result, &tmp);
    *result = tmp;
    free_default_realm(ctx, realm);
}


/*
 * Parse a single class specification.  Currently, this assumes that the class
 * specification is a comma-separated list of required classes, and those
 * classes are required for any length of password.  This will be enhanced
 * later.
 */
static krb5_error_code
parse_class(krb5_context ctx, const char *spec, struct class_rule **rule)
{
    struct vector *classes = NULL;
    size_t i;
    krb5_error_code code;
    const char *end;
    bool okay;

    /* Create the basic rule structure. */
    *rule = calloc(1, sizeof(struct class_rule));
    if (*rule == NULL)
        return strength_error_system(ctx, "cannot allocate memory");

    /*
     * If the rule starts with a digit, it starts with a range of affected
     * password lengths.  Parse that range.
     */
    if (isdigit((unsigned char) *spec)) {
        okay = parse_number(spec, &(*rule)->min, &end);
        if (okay)
            okay = (*end == '-');
        if (okay)
            okay = parse_number(end + 1, &(*rule)->max, &end);
        if (okay)
            okay = (*end == ':');
        if (okay)
            spec = end + 1;
        else {
            code = strength_error_config(ctx, "bad character class requirement"
                                         " in configuration: %s", spec);
            goto fail;
        }
    }

    /* Parse the required classes into a vector. */
    classes = strength_vector_split_multi(spec, ",", NULL);
    if (classes == NULL) {
        code = strength_error_system(ctx, "cannot allocate memory");
        goto fail;
    }

    /*
     * Walk the list of required classes and set our flags, diagnosing an
     * unknown character class.
     */
    for (i = 0; i < classes->count; i++) {
        if (strcmp(classes->strings[i], "upper") == 0)
            (*rule)->upper = true;
        else if (strcmp(classes->strings[i], "lower") == 0)
            (*rule)->lower = true;
        else if (strcmp(classes->strings[i], "digit") == 0)
            (*rule)->digit = true;
        else if (strcmp(classes->strings[i], "symbol") == 0)
            (*rule)->symbol = true;
        else {
            code = strength_error_config(ctx, "unknown character class %s",
                                         classes->strings[i]);
            goto fail;
        }
    }
    strength_vector_free(classes);
    return 0;

fail:
    strength_vector_free(classes);
    free(*rule);
    *rule = NULL;
    return code;
}


/*
 * Parse character class requirements from Kerberos appdefaults.  Takes the
 * Kerberos context, the option, and the place to store the linked list of
 * class requirements.
 */
krb5_error_code
strength_config_classes(krb5_context ctx, const char *opt,
                        struct class_rule **result)
{
    struct vector *config = NULL;
    struct class_rule *rules, *last, *tmp;
    krb5_error_code code;
    size_t i;

    /* Get the basic configuration as a list. */
    code = strength_config_list(ctx, opt, &config);
    if (code != 0)
        return code;
    if (config == NULL || config->count == 0) {
        *result = NULL;
        return 0;
    }

    /* Each word in the list will be a class rule. */
    code = parse_class(ctx, config->strings[0], &rules);
    if (code != 0 || rules == NULL)
        goto fail;
    last = rules;
    for (i = 1; i < config->count; i++) {
        code = parse_class(ctx, config->strings[i], &last->next);
        if (code != 0 || last->next == NULL)
            goto fail;
        last = last->next;
    }

    /* Success.  Free the vector and return the results. */
    strength_vector_free(config);
    *result = rules;
    return 0;

fail:
    last = rules;
    while (last != NULL) {
        tmp = last;
        last = last->next;
        free(tmp);
    }
    strength_vector_free(config);
    return code;
}


/*
 * Load a list option from Kerberos appdefaults.  Takes the Kerberos context,
 * the option, and the result location.  The option is read as a string and
 * the split on spaces and tabs into a list.
 *
 * This requires an annoying workaround because one cannot specify a default
 * value of NULL with MIT Kerberos, since MIT Kerberos unconditionally calls
 * strdup on the default value.  There's also no way to determine if memory
 * allocation failed while parsing or while setting the default value.
 */
krb5_error_code
strength_config_list(krb5_context ctx, const char *opt,
                     struct vector **result)
{
    realm_type realm;
    char *value = NULL;

    /* Obtain the string from [appdefaults]. */
    realm = default_realm(ctx);
    krb5_appdefault_string(ctx, "krb5-strength", realm, opt, "", &value);
    free_default_realm(ctx, realm);

    /* If we got something back, store it in result. */
    if (value != NULL) {
        if (value[0] != '\0') {
            *result = strength_vector_split_multi(value, " \t", *result);
            if (*result == NULL)
                return strength_error_system(ctx, "cannot allocate memory");
        }
        krb5_free_string(ctx, value);
    }
    return 0;
}


/*
 * Load a number option from Kerberos appdefaults.  Takes the Kerberos
 * context, the option, and the result location.  The native interface doesn't
 * support numbers, so we actually read a string and then convert.
 */
void
strength_config_number(krb5_context ctx, const char *opt, long *result)
{
    realm_type realm;
    char *tmp = NULL;
    char *end;
    long value;

    /* Obtain the setting in string form from [appdefaults]. */
    realm = default_realm(ctx);
    krb5_appdefault_string(ctx, "krb5-strength", realm, opt, "", &tmp);
    free_default_realm(ctx, realm);

    /*
     * If we found anything, convert it to a number.  Currently, we ignore
     * errors here.
     */
    if (tmp != NULL && tmp[0] != '\0') {
        errno = 0;
        value = strtol(tmp, &end, 10);
        if (errno == 0 && *end == '\0')
            *result = value;
    }
    if (tmp != NULL)
        krb5_free_string(ctx, tmp);
}


/*
 * Load a string option from Kerberos appdefaults.  Takes the Kerberos
 * context, the option, and the result location.
 *
 * This requires an annoying workaround because one cannot specify a default
 * value of NULL with MIT Kerberos, since MIT Kerberos unconditionally calls
 * strdup on the default value.  There's also no way to determine if memory
 * allocation failed while parsing or while setting the default value, so we
 * don't return an error code.
 */
void
strength_config_string(krb5_context ctx, const char *opt, char **result)
{
    realm_type realm;
    char *value = NULL;

    /* Obtain the string from [appdefaults]. */
    realm = default_realm(ctx);
    krb5_appdefault_string(ctx, "krb5-strength", realm, opt, "", &value);
    free_default_realm(ctx, realm);

    /* If we got something back, store it in result. */
    if (value != NULL) {
        if (value[0] != '\0') {
            free(*result);
            *result = strdup(value);
        }
        krb5_free_string(ctx, value);
    }
}