| File: | daemons/ipa-kdb/ipa_kdb_mkey.c |
| Warning: | line 214, column 5 Value stored to 'kerr' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * MIT Kerberos KDC database backend for FreeIPA |
| 3 | * |
| 4 | * Authors: Simo Sorce <ssorce@redhat.com> |
| 5 | * |
| 6 | * Copyright (C) 2011 Simo Sorce, Red Hat |
| 7 | * see file 'COPYING' for use and warranty information |
| 8 | * |
| 9 | * This program is free software you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation, either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include "ipa_kdb.h" |
| 24 | |
| 25 | static char *krbmkey_attrs[] = { |
| 26 | "krbMKey", |
| 27 | NULL((void*)0) |
| 28 | }; |
| 29 | |
| 30 | krb5_error_code ipadb_fetch_master_key(krb5_context kcontext, |
| 31 | krb5_principal mname, |
| 32 | krb5_keyblock *key, |
| 33 | krb5_kvno *kvno, |
| 34 | char *db_args) |
| 35 | { |
| 36 | struct ipadb_context *ipactx; |
| 37 | LDAPMessage *res = NULL((void*)0); |
| 38 | LDAPMessage *first; |
| 39 | struct berval **vals = NULL((void*)0); |
| 40 | BerElement *be = NULL((void*)0); |
| 41 | krb5_error_code kerr; |
| 42 | krb5_keyblock k; |
| 43 | int mkvno; |
| 44 | int ret; |
| 45 | int i; |
| 46 | |
| 47 | ipactx = ipadb_get_context(kcontext); |
| 48 | if (!ipactx) { |
| 49 | return KRB5_KDB_DBNOTINITED(-1780008435L); |
| 50 | } |
| 51 | |
| 52 | if (!ipactx->lcontext) { |
| 53 | ret = ipadb_get_connection(ipactx); |
| 54 | if (ret != 0) { |
| 55 | kerr = KRB5_KDB_SERVER_INTERNAL_ERR(-1780008413L); |
| 56 | goto done; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | be = ber_alloc_t(LBER_USE_DER0x01); |
| 61 | if (!be) { |
| 62 | kerr = ENOMEM12; |
| 63 | goto done; |
| 64 | } |
| 65 | |
| 66 | kerr = ipadb_simple_search(ipactx, ipactx->realm_base, LDAP_SCOPE_BASE((ber_int_t) 0x0000), |
| 67 | "(krbMKey=*)", krbmkey_attrs, &res); |
| 68 | if (kerr) { |
| 69 | goto done; |
| 70 | } |
| 71 | |
| 72 | first = ldap_first_entry(ipactx->lcontext, res); |
| 73 | if (!first) { |
| 74 | kerr = KRB5_KDB_NOENTRY(-1780008443L); |
| 75 | goto done; |
| 76 | } |
| 77 | |
| 78 | mkvno = 0; |
| 79 | k.contents = NULL((void*)0); |
| 80 | vals = ldap_get_values_len(ipactx->lcontext, first, "krbmkey"); |
| 81 | for (i = 0; vals[i]; i++) { |
| 82 | struct berval *mkey; |
| 83 | ber_tag_t tag; |
| 84 | ber_int_t tvno; |
| 85 | ber_int_t ttype; |
| 86 | |
| 87 | ber_init2(be, vals[i], LBER_USE_DER0x01); |
| 88 | |
| 89 | tag = ber_scanf(be, "{i{iO}}", &tvno, &ttype, &mkey); |
| 90 | if (tag == LBER_ERROR((ber_tag_t) -1)) { |
| 91 | kerr = KRB5_KDB_SERVER_INTERNAL_ERR(-1780008413L); |
| 92 | goto done; |
| 93 | } |
| 94 | |
| 95 | if (tvno > mkvno) { |
| 96 | mkvno = tvno; |
| 97 | k.enctype = ttype; |
| 98 | k.length = mkey->bv_len; |
| 99 | if (k.contents) { |
| 100 | free(k.contents); |
| 101 | } |
| 102 | k.contents = malloc(k.length); |
| 103 | if (!k.contents) { |
| 104 | kerr = ENOMEM12; |
| 105 | goto done; |
| 106 | } |
| 107 | memcpy(k.contents, mkey->bv_val, k.length); |
| 108 | } |
| 109 | ber_bvfree(mkey); |
| 110 | } |
| 111 | |
| 112 | if (mkvno == 0) { |
| 113 | kerr = KRB5_KDB_NOENTRY(-1780008443L); |
| 114 | goto done; |
| 115 | } |
| 116 | |
| 117 | *kvno = mkvno; |
| 118 | key->magic = KV5M_KEYBLOCK(-1760647421L); |
| 119 | key->enctype = k.enctype; |
| 120 | key->length = k.length; |
| 121 | key->contents = k.contents; |
| 122 | |
| 123 | kerr = 0; |
| 124 | |
| 125 | done: |
| 126 | if (be) { |
| 127 | ber_free(be, 0); |
| 128 | } |
| 129 | ldap_value_free_len(vals); |
| 130 | ldap_msgfree(res); |
| 131 | return kerr; |
| 132 | } |
| 133 | |
| 134 | krb5_error_code ipadb_store_master_key_list(krb5_context kcontext, |
| 135 | char *db_arg, |
| 136 | krb5_principal mname, |
| 137 | krb5_keylist_node *keylist, |
| 138 | char *master_pwd) |
| 139 | { |
| 140 | struct ipadb_context *ipactx; |
| 141 | BerElement *be = NULL((void*)0); |
| 142 | krb5_keyblock k = { 0, 0, 0, NULL((void*)0) }; |
| 143 | struct berval mkey; |
| 144 | ber_int_t tvno; |
| 145 | ber_int_t ttype; |
| 146 | LDAPMod **mods = NULL((void*)0); |
| 147 | krb5_error_code kerr; |
| 148 | int ret; |
| 149 | |
| 150 | ipactx = ipadb_get_context(kcontext); |
| 151 | if (!ipactx) { |
| 152 | return KRB5_KDB_DBNOTINITED(-1780008435L); |
| 153 | } |
| 154 | |
| 155 | /* we support storing only one key for now */ |
| 156 | if (!keylist || keylist->next) { |
| 157 | return EINVAL22; |
| 158 | } |
| 159 | |
| 160 | if (!ipactx->lcontext) { |
| 161 | ret = ipadb_get_connection(ipactx); |
| 162 | if (ret != 0) { |
| 163 | kerr = KRB5_KDB_SERVER_INTERNAL_ERR(-1780008413L); |
| 164 | goto done; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | be = ber_alloc_t(LBER_USE_DER0x01); |
| 169 | if (!be) { |
| 170 | kerr = ENOMEM12; |
| 171 | goto done; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | tvno = keylist->kvno; |
| 176 | ttype = keylist->keyblock.enctype; |
| 177 | mkey.bv_len = keylist->keyblock.length; |
| 178 | mkey.bv_val = (void *)keylist->keyblock.contents; |
| 179 | |
| 180 | ret = ber_printf(be, "{i{iO}}", tvno, ttype, &mkey); |
| 181 | if (ret == -1) { |
| 182 | kerr = KRB5_KDB_INTERNAL_ERROR(-1780008411L); |
| 183 | goto done; |
| 184 | } |
| 185 | |
| 186 | mods = calloc(2, sizeof(LDAPMod *)); |
| 187 | if (!mods) { |
| 188 | kerr = ENOMEM12; |
| 189 | goto done; |
| 190 | } |
| 191 | mods[0] = calloc(1, sizeof(LDAPMod)); |
| 192 | if (!mods[0]) { |
| 193 | kerr = ENOMEM12; |
| 194 | goto done; |
| 195 | } |
| 196 | mods[0]->mod_op = LDAP_MOD_ADD(0x0000) | LDAP_MOD_BVALUES(0x0080); |
| 197 | mods[0]->mod_type = strdup("krbMKey"); |
| 198 | if (!mods[0]->mod_type) { |
| 199 | kerr = ENOMEM12; |
| 200 | goto done; |
| 201 | } |
| 202 | mods[0]->mod_bvaluesmod_vals.modv_bvals = calloc(2, sizeof(struct berval *)); |
| 203 | if (!mods[0]->mod_bvaluesmod_vals.modv_bvals) { |
| 204 | kerr = ENOMEM12; |
| 205 | goto done; |
| 206 | } |
| 207 | |
| 208 | ret = ber_flatten(be, &mods[0]->mod_bvaluesmod_vals.modv_bvals[0]); |
| 209 | if (ret == -1) { |
| 210 | kerr = KRB5_KDB_INTERNAL_ERROR(-1780008411L); |
| 211 | goto done; |
| 212 | } |
| 213 | |
| 214 | kerr = ipadb_simple_modify(ipactx, ipactx->realm_base, mods); |
Value stored to 'kerr' is never read | |
| 215 | |
| 216 | kerr = 0; |
| 217 | |
| 218 | done: |
| 219 | if (be) { |
| 220 | ber_free(be, 1); |
| 221 | } |
| 222 | krb5_free_keyblock_contents(kcontext, &k); |
| 223 | ldap_mods_free(mods, 1); |
| 224 | return kerr; |
| 225 | } |