summaryrefslogtreecommitdiffstats
path: root/src/lib/kadm5/srv/svr_policy.c
blob: 0d79f86dcea6b8bcb7ee7908d37e4df546f7bfb3 (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
 *
 * $Header$
 */

#include        <sys/types.h>
#include        <kadm5/admin.h>
#include        "server_internal.h"
#include        <stdlib.h>
#include        <string.h>
#include        <errno.h>

#define MIN_PW_HISTORY  1
#define MIN_PW_CLASSES  1
#define MAX_PW_CLASSES  5
#define MIN_PW_LENGTH   1

/*
 * Function: kadm5_create_policy
 *
 * Purpose: Create Policies in the policy DB.
 *
 * Arguments:
 *      entry   (input) The policy entry to be written out to the DB.
 *      mask    (input) Specifies which fields in entry are to ge written out
 *                      and which get default values.
 *      <return value> 0 if successful otherwise an error code is returned.
 *
 * Requires:
 *      Entry must be a valid principal entry, and mask have a valid value.
 *
 * Effects:
 *      Verifies that mask does not specify that the refcount should
 *      be set as part of the creation, and calls
 *      kadm5_create_policy_internal.  If the refcount *is*
 *      specified, returns KADM5_BAD_MASK.
 */

kadm5_ret_t
kadm5_create_policy(void *server_handle,
                    kadm5_policy_ent_t entry, long mask)
{
    CHECK_HANDLE(server_handle);

    krb5_clear_error_message(((kadm5_server_handle_t)server_handle)->context);

    if (mask & KADM5_REF_COUNT)
        return KADM5_BAD_MASK;
    else
        return kadm5_create_policy_internal(server_handle, entry, mask);
}

/* Validate allowed_keysalts. */
static kadm5_ret_t
validate_allowed_keysalts(const char *allowed_keysalts)
{
    kadm5_ret_t ret;
    krb5_key_salt_tuple *ks_tuple = NULL;
    krb5_int32 n_ks_tuple = 0;

    if (strchr(allowed_keysalts, '\t') != NULL)
        return KADM5_BAD_KEYSALTS;
    ret = krb5_string_to_keysalts(allowed_keysalts, ",", ":.-", 0,
                                  &ks_tuple, &n_ks_tuple);
    free(ks_tuple);
    if (ret == EINVAL)
        return KADM5_BAD_KEYSALTS;
    return ret;
}

/*
 * Function: kadm5_create_policy_internal
 *
 * Purpose: Create Policies in the policy DB.
 *
 * Arguments:
 *      entry   (input) The policy entry to be written out to the DB.
 *      mask    (input) Specifies which fields in entry are to ge written out
 *                      and which get default values.
 *      <return value> 0 if successful otherwise an error code is returned.
 *
 * Requires:
 *      Entry must be a valid principal entry, and mask have a valid value.
 *
 * Effects:
 *      Writes the data to the database, and does a database sync if
 *      successful.
 *
 */

kadm5_ret_t
kadm5_create_policy_internal(void *server_handle,
                             kadm5_policy_ent_t entry, long mask)
{
    kadm5_server_handle_t handle = server_handle;
    osa_policy_ent_rec  pent;
    int                 ret;
    char                *p;

    CHECK_HANDLE(server_handle);

    if ((entry == (kadm5_policy_ent_t) NULL) || (entry->policy == NULL))
        return EINVAL;
    if(strlen(entry->policy) == 0)
        return KADM5_BAD_POLICY;
    if (!(mask & KADM5_POLICY))
        return KADM5_BAD_MASK;
    if ((mask & KADM5_POLICY_ALLOWED_KEYSALTS) &&
        entry->allowed_keysalts != NULL) {
        ret = validate_allowed_keysalts(entry->allowed_keysalts);
        if (ret)
            return ret;
    }

    memset(&pent, 0, sizeof(pent));
    pent.name = entry->policy;
    p = entry->policy;
    while(*p != '\0') {
        if(*p < ' ' || *p > '~')
            return KADM5_BAD_POLICY;
        else
            p++;
    }
    if (!(mask & KADM5_PW_MAX_LIFE))
        pent.pw_max_life = 0;
    else
        pent.pw_max_life = entry->pw_max_life;
    if (!(mask & KADM5_PW_MIN_LIFE))
        pent.pw_min_life = 0;
    else {
        if((mask & KADM5_PW_MAX_LIFE)) {
            if(entry->pw_min_life > entry->pw_max_life && entry->pw_max_life != 0)
                return KADM5_BAD_MIN_PASS_LIFE;
        }
        pent.pw_min_life = entry->pw_min_life;
    }
    if (!(mask & KADM5_PW_MIN_LENGTH))
        pent.pw_min_length = MIN_PW_LENGTH;
    else {
        if(entry->pw_min_length < MIN_PW_LENGTH)
            return KADM5_BAD_LENGTH;
        pent.pw_min_length = entry->pw_min_length;
    }
    if (!(mask & KADM5_PW_MIN_CLASSES))
        pent.pw_min_classes = MIN_PW_CLASSES;
    else {
        if(entry->pw_min_classes > MAX_PW_CLASSES || entry->pw_min_classes < MIN_PW_CLASSES)
            return KADM5_BAD_CLASS;
        pent.pw_min_classes = entry->pw_min_classes;
    }
    if (!(mask & KADM5_PW_HISTORY_NUM))
        pent.pw_history_num = MIN_PW_HISTORY;
    else {
        if(entry->pw_history_num < MIN_PW_HISTORY)
            return KADM5_BAD_HISTORY;
        else
            pent.pw_history_num = entry->pw_history_num;
    }
    if (!(mask & KADM5_REF_COUNT))
        pent.policy_refcnt = 0;
    else
        pent.policy_refcnt = entry->policy_refcnt;

    if (handle->api_version >= KADM5_API_VERSION_4) {
        if (!(mask & KADM5_POLICY_ATTRIBUTES))
            pent.attributes = 0;
        else
            pent.attributes = entry->attributes;
        if (!(mask & KADM5_POLICY_MAX_LIFE))
            pent.max_life = 0;
        else
            pent.max_life = entry->max_life;
        if (!(mask & KADM5_POLICY_MAX_RLIFE))
            pent.max_renewable_life = 0;
        else
            pent.max_renewable_life = entry->max_renewable_life;
        if (!(mask & KADM5_POLICY_ALLOWED_KEYSALTS))
            pent.allowed_keysalts = 0;
        else
            pent.allowed_keysalts = entry->allowed_keysalts;
        if (!(mask & KADM5_POLICY_TL_DATA)) {
            pent.n_tl_data = 0;
            pent.tl_data = NULL;
        } else {
            pent.n_tl_data = entry->n_tl_data;
            pent.tl_data = entry->tl_data;
        }
    }
    if (handle->api_version >= KADM5_API_VERSION_3) {
        if (!(mask & KADM5_PW_MAX_FAILURE))
            pent.pw_max_fail = 0;
        else
            pent.pw_max_fail = entry->pw_max_fail;
        if (!(mask & KADM5_PW_FAILURE_COUNT_INTERVAL))
            pent.pw_failcnt_interval = 0;
        else
            pent.pw_failcnt_interval = entry->pw_failcnt_interval;
        if (!(mask & KADM5_PW_LOCKOUT_DURATION))
            pent.pw_lockout_duration = 0;
        else
            pent.pw_lockout_duration = entry->pw_lockout_duration;
    }

    if ((ret = krb5_db_create_policy(handle->context, &pent)))
        return ret;
    else
        return KADM5_OK;
}

kadm5_ret_t
kadm5_delete_policy(void *server_handle, kadm5_policy_t name)
{
    kadm5_server_handle_t handle = server_handle;
    osa_policy_ent_t            entry;
    int                         ret;

    CHECK_HANDLE(server_handle);

    krb5_clear_error_message(handle->context);

    if(name == (kadm5_policy_t) NULL)
        return EINVAL;
    if(strlen(name) == 0)
        return KADM5_BAD_POLICY;
    ret = krb5_db_get_policy(handle->context, name, &entry);
    if (ret == KRB5_KDB_NOENTRY)
        return KADM5_UNK_POLICY;
    else if (ret)
        return ret;

    if(entry->policy_refcnt != 0) {
        krb5_db_free_policy(handle->context, entry);
        return KADM5_POLICY_REF;
    }
    krb5_db_free_policy(handle->context, entry);
    ret = krb5_db_delete_policy(handle->context, name);
    if (ret == KRB5_KDB_POLICY_REF)
        ret = KADM5_POLICY_REF;
    return (ret == 0) ? KADM5_OK : ret;
}

kadm5_ret_t
kadm5_modify_policy(void *server_handle,
                    kadm5_policy_ent_t entry, long mask)
{
    CHECK_HANDLE(server_handle);

    krb5_clear_error_message(((kadm5_server_handle_t)server_handle)->context);

    if (mask & KADM5_REF_COUNT)
        return KADM5_BAD_MASK;
    else
        return kadm5_modify_policy_internal(server_handle, entry, mask);
}

/* Allocate and form a TL data list of a desired size. */
static int
alloc_tl_data(krb5_int16 n_tl_data, krb5_tl_data **tldp)
{
    krb5_tl_data **tlp = tldp;
    int i;

    for (i = 0; i < n_tl_data; i++) {
        *tlp = calloc(1, sizeof(krb5_tl_data));
        if (*tlp == NULL)
            return ENOMEM; /* caller cleans up */
        memset(*tlp, 0, sizeof(krb5_tl_data));
        tlp = &((*tlp)->tl_data_next);
    }

    return 0;
}

static kadm5_ret_t
copy_tl_data(krb5_int16 n_tl_data, krb5_tl_data *tl_data,
             krb5_tl_data **out)
{
    kadm5_ret_t ret;
    krb5_tl_data *tl, *tl_new;

    if ((ret = alloc_tl_data(n_tl_data, out)))
        return ret; /* caller cleans up */

    tl = tl_data;
    tl_new = *out;
    for (; tl; tl = tl->tl_data_next, tl_new = tl_new->tl_data_next) {
        tl_new->tl_data_contents = malloc(tl->tl_data_length);
        if (tl_new->tl_data_contents == NULL)
            return ENOMEM;
        memcpy(tl_new->tl_data_contents, tl->tl_data_contents,
               tl->tl_data_length);
        tl_new->tl_data_type = tl->tl_data_type;
        tl_new->tl_data_length = tl->tl_data_length;
    }

    return 0;
}

kadm5_ret_t
kadm5_modify_policy_internal(void *server_handle,
                             kadm5_policy_ent_t entry, long mask)
{
    kadm5_server_handle_t    handle = server_handle;
    krb5_tl_data            *tl;
    osa_policy_ent_t         p;
    int                      ret;
    size_t                   len;

    CHECK_HANDLE(server_handle);

    if((entry == (kadm5_policy_ent_t) NULL) || (entry->policy == NULL))
        return EINVAL;
    if(strlen(entry->policy) == 0)
        return KADM5_BAD_POLICY;
    if((mask & KADM5_POLICY))
        return KADM5_BAD_MASK;
    if ((mask & KADM5_POLICY_ALLOWED_KEYSALTS) &&
        entry->allowed_keysalts != NULL) {
        ret = validate_allowed_keysalts(entry->allowed_keysalts);
        if (ret)
            return ret;
    }
    if ((mask & KADM5_POLICY_TL_DATA)) {
        tl = entry->tl_data;
        while (tl != NULL) {
            if (tl->tl_data_type < 256)
                return KADM5_BAD_TL_TYPE;
            tl = tl->tl_data_next;
        }
    }

    ret = krb5_db_get_policy(handle->context, entry->policy, &p);
    if (ret == KRB5_KDB_NOENTRY)
        return KADM5_UNK_POLICY;
    else if (ret)
        return ret;

    if ((mask & KADM5_PW_MAX_LIFE))
        p->pw_max_life = entry->pw_max_life;
    if ((mask & KADM5_PW_MIN_LIFE)) {
        if(entry->pw_min_life > p->pw_max_life && p->pw_max_life != 0)  {
            krb5_db_free_policy(handle->context, p);
            return KADM5_BAD_MIN_PASS_LIFE;
        }
        p->pw_min_life = entry->pw_min_life;
    }
    if ((mask & KADM5_PW_MIN_LENGTH)) {
        if(entry->pw_min_length < MIN_PW_LENGTH) {
            krb5_db_free_policy(handle->context, p);
            return KADM5_BAD_LENGTH;
        }
        p->pw_min_length = entry->pw_min_length;
    }
    if ((mask & KADM5_PW_MIN_CLASSES)) {
        if(entry->pw_min_classes > MAX_PW_CLASSES ||
           entry->pw_min_classes < MIN_PW_CLASSES) {
            krb5_db_free_policy(handle->context, p);
            return KADM5_BAD_CLASS;
        }
        p->pw_min_classes = entry->pw_min_classes;
    }
    if ((mask & KADM5_PW_HISTORY_NUM)) {
        if(entry->pw_history_num < MIN_PW_HISTORY) {
            krb5_db_free_policy(handle->context, p);
            return KADM5_BAD_HISTORY;
        }
        p->pw_history_num = entry->pw_history_num;
    }
    if ((mask & KADM5_REF_COUNT))
        p->policy_refcnt = entry->policy_refcnt;
    if (handle->api_version >= KADM5_API_VERSION_3) {
        if ((mask & KADM5_PW_MAX_FAILURE))
            p->pw_max_fail = entry->pw_max_fail;
        if ((mask & KADM5_PW_FAILURE_COUNT_INTERVAL))
            p->pw_failcnt_interval = entry->pw_failcnt_interval;
        if ((mask & KADM5_PW_LOCKOUT_DURATION))
            p->pw_lockout_duration = entry->pw_lockout_duration;
    }
    if (handle->api_version >= KADM5_API_VERSION_4) {
        if ((mask & KADM5_POLICY_ATTRIBUTES))
            p->attributes = entry->attributes;
        if ((mask & KADM5_POLICY_MAX_LIFE))
            p->max_life = entry->max_life;
        if ((mask & KADM5_POLICY_MAX_RLIFE))
            p->max_renewable_life = entry->max_renewable_life;
        if ((mask & KADM5_POLICY_ALLOWED_KEYSALTS)) {
            krb5_db_free(handle->context, p->allowed_keysalts);
            p->allowed_keysalts = NULL;
            if (entry->allowed_keysalts != NULL) {
                len = strlen(entry->allowed_keysalts) + 1;
                p->allowed_keysalts = krb5_db_alloc(handle->context, NULL,
                                                    len);
                if (p->allowed_keysalts == NULL) {
                    ret = ENOMEM;
                    goto cleanup;
                }
                memcpy(p->allowed_keysalts, entry->allowed_keysalts, len);
            }
        }
        if ((mask & KADM5_POLICY_TL_DATA)) {
            for (tl = entry->tl_data; tl != NULL; tl = tl->tl_data_next) {
                ret = krb5_db_update_tl_data(handle->context, &p->n_tl_data,
                                             &p->tl_data, tl);
                if (ret)
                    goto cleanup;
            }
        }
    }
    ret = krb5_db_put_policy(handle->context, p);

cleanup:
    krb5_db_free_policy(handle->context, p);
    return ret;
}

kadm5_ret_t
kadm5_get_policy(void *server_handle, kadm5_policy_t name,
                 kadm5_policy_ent_t entry)
{
    osa_policy_ent_t            t;
    kadm5_ret_t                 ret;
    kadm5_server_handle_t handle = server_handle;

    memset(entry, 0, sizeof(*entry));

    CHECK_HANDLE(server_handle);

    krb5_clear_error_message(handle->context);

    if (name == (kadm5_policy_t) NULL)
        return EINVAL;
    if(strlen(name) == 0)
        return KADM5_BAD_POLICY;
    ret = krb5_db_get_policy(handle->context, name, &t);
    if (ret == KRB5_KDB_NOENTRY)
        return KADM5_UNK_POLICY;
    else if (ret)
        return ret;

    if ((entry->policy = strdup(t->name)) == NULL) {
        ret = ENOMEM;
        goto cleanup;
    }
    entry->pw_min_life = t->pw_min_life;
    entry->pw_max_life = t->pw_max_life;
    entry->pw_min_length = t->pw_min_length;
    entry->pw_min_classes = t->pw_min_classes;
    entry->pw_history_num = t->pw_history_num;
    entry->policy_refcnt = t->policy_refcnt;
    if (handle->api_version >= KADM5_API_VERSION_3) {
        entry->pw_max_fail = t->pw_max_fail;
        entry->pw_failcnt_interval = t->pw_failcnt_interval;
        entry->pw_lockout_duration = t->pw_lockout_duration;
    }
    if (handle->api_version >= KADM5_API_VERSION_4) {
        entry->attributes = t->attributes;
        entry->max_life = t->max_life;
        entry->max_renewable_life = t->max_renewable_life;
        if (t->allowed_keysalts) {
            entry->allowed_keysalts = strdup(t->allowed_keysalts);
            if (!entry->allowed_keysalts) {
                ret = ENOMEM;
                goto cleanup;
            }
        }
        ret = copy_tl_data(t->n_tl_data, t->tl_data, &entry->tl_data);
        if (ret)
            goto cleanup;
        entry->n_tl_data = t->n_tl_data;
    }

    ret = 0;

cleanup:
    if (ret)
        kadm5_free_policy_ent(handle, entry);
    krb5_db_free_policy(handle->context, t);
    return ret;
}