summaryrefslogtreecommitdiffstats
path: root/daemons/ipa-kdb/ipa_kdb_mkey.c
blob: 1d208e3cc2f9a2aa4a7d1a55a5f61be6fc58b9b4 (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
/*
 * MIT Kerberos KDC database backend for FreeIPA
 *
 * Authors: Simo Sorce <ssorce@redhat.com>
 *
 * Copyright (C) 2011  Simo Sorce, Red Hat
 * see file 'COPYING' for use and warranty information
 *
 * This program is free software you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ipa_kdb.h"

static char *krbmkey_attrs[] = {
    "krbMKey",
    NULL
};

static krb5_error_code ipadb_fetch_master_key(struct ipadb_context *ipactx,
                                              krb5_keyblock *key,
                                              krb5_kvno *kvno)
{
    LDAPMessage *res = NULL;
    LDAPMessage *first;
    struct berval **vals = NULL;
    BerElement *be = NULL;
    krb5_error_code kerr;
    krb5_keyblock k;
    int mkvno;
    int ret;
    int i;

    if (!ipactx->lcontext) {
        ret = ipadb_get_connection(ipactx);
        if (ret != 0) {
            kerr = KRB5_KDB_SERVER_INTERNAL_ERR;
            goto done;
        }
    }

    be = ber_alloc_t(LBER_USE_DER);
    if (!be) {
        kerr = ENOMEM;
        goto done;
    }

    kerr = ipadb_simple_search(ipactx, ipactx->realm_base, LDAP_SCOPE_BASE,
                               "(krbMKey=*)", krbmkey_attrs, &res);
    if (kerr) {
        goto done;
    }

    first = ldap_first_entry(ipactx->lcontext, res);
    if (!first) {
        kerr = KRB5_KDB_NOENTRY;
        goto done;
    }

    mkvno = 0;
    k.contents = NULL;
    vals = ldap_get_values_len(ipactx->lcontext, first, "krbmkey");
    for (i = 0; vals[i]; i++) {
        struct berval *mkey;
        ber_tag_t tag;
        ber_int_t tvno;
        ber_int_t ttype;

        ber_init2(be, vals[i], LBER_USE_DER);

        tag = ber_scanf(be, "{i{iO}}", &tvno, &ttype, &mkey);
        if (tag == LBER_ERROR) {
            kerr = KRB5_KDB_SERVER_INTERNAL_ERR;
            goto done;
        }

        if (tvno > mkvno) {
            mkvno = tvno;
            k.enctype = ttype;
            k.length = mkey->bv_len;
            if (k.contents) {
                free(k.contents);
            }
            k.contents = malloc(k.length);
            if (!k.contents) {
                kerr = ENOMEM;
                goto done;
            }
            memcpy(k.contents, mkey->bv_val, k.length);
        }
        ber_bvfree(mkey);
    }

    if (mkvno == 0) {
        kerr = KRB5_KDB_NOENTRY;
        goto done;
    }

    *kvno = mkvno;
    key->magic = KV5M_KEYBLOCK;
    key->enctype = k.enctype;
    key->length = k.length;
    key->contents = k.contents;

    kerr = 0;

done:
    if (be) {
        ber_free(be, 0);
    }
    ldap_value_free_len(vals);
    ldap_msgfree(res);
    return kerr;
}

static krb5_error_code ipadb_store_master_key(struct ipadb_context *ipactx,
                                              krb5_keylist_node *cur_key)
{
    BerElement *be = NULL;
    struct berval mkey;
    ber_int_t tvno;
    ber_int_t ttype;
    LDAPMod **mods = NULL;
    krb5_error_code kerr;
    int ret;

    if (!ipactx->lcontext) {
        ret = ipadb_get_connection(ipactx);
        if (ret != 0) {
            kerr = KRB5_KDB_SERVER_INTERNAL_ERR;
            goto done;
        }
    }

    be = ber_alloc_t(LBER_USE_DER);
    if (!be) {
        kerr = ENOMEM;
        goto done;
    }


    tvno = cur_key->kvno;
    ttype = cur_key->keyblock.enctype;
    mkey.bv_len = cur_key->keyblock.length;
    mkey.bv_val = (void *)cur_key->keyblock.contents;

    ret = ber_printf(be, "{i{iO}}", tvno, ttype, &mkey);
    if (ret == -1) {
        kerr = KRB5_KDB_INTERNAL_ERROR;
        goto done;
    }

    mods = calloc(2, sizeof(LDAPMod *));
    if (!mods) {
        kerr = ENOMEM;
        goto done;
    }
    mods[0] = calloc(1, sizeof(LDAPMod));
    if (!mods[0]) {
        kerr = ENOMEM;
        goto done;
    }
    mods[0]->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
    mods[0]->mod_type = strdup("krbMKey");
    if (!mods[0]->mod_type) {
        kerr = ENOMEM;
        goto done;
    }
    mods[0]->mod_bvalues = calloc(2, sizeof(struct berval *));
    if (!mods[0]->mod_bvalues) {
        kerr = ENOMEM;
        goto done;
    }

    ret = ber_flatten(be, &mods[0]->mod_bvalues[0]);
    if (ret == -1) {
        kerr = KRB5_KDB_INTERNAL_ERROR;
        goto done;
    }

    kerr = ipadb_simple_modify(ipactx, ipactx->realm_base, mods);

    kerr = 0;

done:
    if (be) {
        ber_free(be, 1);
    }
    ldap_mods_free(mods, 1);
    return kerr;
}

static krb5_error_code ipadb_update_krbMKey(struct ipadb_context *ipactx,
                                            krb5_keylist_node *master_key,
                                            bool update)
{
    krb5_error_code kerr;
    LDAPMessage *res = NULL;

    if (update) {
        /* check if we need to update the master key in LDAP for
         * older replicas, if the attr is found, keep it updated */
        kerr = ipadb_simple_search(ipactx, ipactx->realm_base,
                                   LDAP_SCOPE_BASE, "(krbMKey=*)",
                                   krbmkey_attrs, &res);
        switch (kerr) {
        case 0:
            ldap_msgfree(res);
            break;
        case KRB5_KDB_NOENTRY:
            return 0;
        default:
            return kerr;
        }
    }

    /* FIXME: on domain level X we may want to not add the key */
    kerr = ipadb_store_master_key(ipactx, master_key);
    if (kerr) {
        com_err("IPA KDB driver", kerr,
                "while storing master key (version %d)",
                (int)master_key->kvno);
    }
    return kerr;
}

krb5_error_code ipadb_check_db_exists(struct ipadb_context *ipactx)
{
    if (ipactx->special_flags & IPA_SPECIAL_CREATE_STEP) return EEXIST;
    return 0;
}

/* See if we have a keytab, if not try to fetch the key from ldap, and
 * stash it for the future (upgrade path) */
krb5_error_code ipadb_check_master_key(struct ipadb_context *ipactx)
{
    krb5_error_code kerr;
    char *cmname = NULL;
    krb5_principal mname = NULL;
    krb5_keyblock mkey = { 0 };
    krb5_keylist_node *keylist;
    krb5_kvno mkvno;

    kerr = asprintf(&cmname, KRB5_KDB_M_NAME"@%s", ipactx->realm);
    if (kerr < 0) return ENOMEM;

    kerr = krb5_parse_name(ipactx->kcontext, cmname, &mname);
    if (kerr) goto done;

    kerr = krb5_db_fetch_mkey(ipactx->kcontext, mname, ENCTYPE_UNKNOWN,
                              0, 0, NULL, &mkvno, NULL, &mkey);
    if (kerr == 0) kerr = ipadb_check_db_exists(ipactx);
    if (kerr && (kerr != KRB5_KDB_CANTREAD_STORED)) goto done;

    if (kerr == KRB5_KDB_CANTREAD_STORED) {
        /* check if we have a key in LDAP, and stash it in the keytab */
        kerr = ipadb_fetch_master_key(ipactx, &mkey, &mkvno);
        if (kerr == 0) kerr = ipadb_check_db_exists(ipactx);
        if (kerr) {
            /* if we are setting up then lack of mkey is normal */
            if (ipactx->special_flags & IPA_SPECIAL_CREATE_STEP) kerr = 0;
            goto done;
        }

        kerr = krb5_db_store_master_key(ipactx->kcontext, NULL,
                                        mname, mkvno, &mkey, NULL);
    } else {
        /* if override is set do extra checks on ldap keytab so that it
         * is uploaded during initial setup if needed */
        if (ipactx->special_flags & IPA_SPECIAL_OVERRIDE_RESTRICTION) {
            krb5_keylist_node curkey = { mkey, mkvno, NULL };
            kerr = ipadb_update_krbMKey(ipactx, &curkey, false);
            if (kerr) goto done;
        }

        /* check if we have the latest kvno as current key. If not stash
         * the new keys in the keytab */
        kerr = krb5_db_fetch_mkey_list(ipactx->kcontext, mname, &mkey);
        if (kerr) goto done;

        keylist = krb5_db_mkey_list_alias(ipactx->kcontext);

        if (keylist->kvno > mkvno) {
            kerr = krb5_db_store_master_key_list(ipactx->kcontext, NULL,
                                                 mname, NULL);

            /* we do not consider it fatal if this fails on update */
            (void)ipadb_update_krbMKey(ipactx, keylist, true);
        }
    }

done:
    krb5_free_keyblock_contents(ipactx->kcontext, &mkey);
    krb5_free_principal(ipactx->kcontext, mname);
    free(cmname);
    return kerr;
}