2 Unix SMB/CIFS implementation.
4 Kerberos utility functions
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
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/>.
26 * @brief Kerberos keytab utility functions
31 #include "system/kerberos.h"
32 #include "auth/credentials/credentials.h"
33 #include "auth/kerberos/kerberos.h"
34 #include "auth/kerberos/kerberos_util.h"
35 #include "auth/kerberos/kerberos_srv_keytab.h"
37 static void keytab_principals_free(krb5_context context,
38 uint32_t num_principals,
43 for (i = 0; i < num_principals; i++) {
44 krb5_free_principal(context, set[i]);
48 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
49 uint32_t num_principals,
50 krb5_principal *principals,
51 krb5_principal salt_princ,
53 const char *password_s,
55 krb5_enctype *enctypes,
57 const char **error_string)
64 password.data = discard_const_p(char, password_s);
65 password.length = strlen(password_s);
67 for (i = 0; enctypes[i]; i++) {
68 krb5_keytab_entry entry;
72 ret = smb_krb5_create_key_from_string(context,
79 *error_string = talloc_strdup(parent_ctx,
80 "Failed to create key from string");
86 for (p = 0; p < num_principals; p++) {
88 entry.principal = principals[p];
89 ret = krb5_kt_add_entry(context, keytab, &entry);
91 char *k5_error_string =
92 smb_get_krb5_error_message(context,
94 krb5_unparse_name(context,
95 principals[p], &unparsed);
96 *error_string = talloc_asprintf(parent_ctx,
97 "Failed to add enctype %d entry for "
98 "%s(kvno %d) to keytab: %s\n",
99 (int)enctypes[i], unparsed,
100 kvno, k5_error_string);
103 talloc_free(k5_error_string);
104 krb5_free_keyblock_contents(context,
105 KRB5_KT_KEY(&entry));
109 DEBUG(5, ("Added key (kvno %d) to keytab (enctype %d)\n",
110 kvno, (int)enctypes[i]));
112 krb5_free_keyblock_contents(context, KRB5_KT_KEY(&entry));
117 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
118 const char *saltPrincipal,
120 const char *new_secret,
121 const char *old_secret,
122 uint32_t supp_enctypes,
123 uint32_t num_principals,
124 krb5_principal *principals,
125 krb5_context context,
128 const char **perror_string)
131 krb5_principal salt_princ = NULL;
132 krb5_enctype *enctypes;
134 const char *error_string = NULL;
137 /* There is no password here, so nothing to do */
141 mem_ctx = talloc_new(parent_ctx);
143 *perror_string = talloc_strdup(parent_ctx,
144 "unable to allocate tmp_ctx for create_keytab");
148 /* The salt used to generate these entries may be different however,
150 ret = krb5_parse_name(context, saltPrincipal, &salt_princ);
152 *perror_string = smb_get_krb5_error_message(context,
155 talloc_free(mem_ctx);
159 ret = ms_suptypes_to_ietf_enctypes(mem_ctx, supp_enctypes, &enctypes);
161 *perror_string = talloc_asprintf(parent_ctx,
162 "create_keytab: generating list of "
163 "encryption types failed (%s)\n",
164 smb_get_krb5_error_message(context,
169 ret = keytab_add_keys(mem_ctx,
172 salt_princ, kvno, new_secret,
173 context, enctypes, keytab, &error_string);
175 *perror_string = talloc_steal(parent_ctx, error_string);
179 if (old_secret && add_old && kvno != 0) {
180 ret = keytab_add_keys(mem_ctx,
183 salt_princ, kvno - 1, old_secret,
184 context, enctypes, keytab, &error_string);
186 *perror_string = talloc_steal(parent_ctx, error_string);
191 krb5_free_principal(context, salt_princ);
192 talloc_free(mem_ctx);
197 * @brief Update a Kerberos keytab and removes any obsolete keytab entries.
199 * If the keytab does not exist, this function will create one.
201 * @param[in] parent_ctx Talloc memory context
202 * @param[in] context Kerberos context
203 * @param[in] keytab_name Keytab to open
204 * @param[in] samAccountName User account to update
205 * @param[in] realm Kerberos realm
206 * @param[in] SPNs Service principal names to update
207 * @param[in] num_SPNs Length of SPNs
208 * @param[in] saltPrincipal Salt used for AES encryption.
209 * Required, unless delete_all_kvno is set.
210 * @param[in] old_secret Old password
211 * @param[in] new_secret New password
212 * @param[in] kvno Current key version number
213 * @param[in] supp_enctypes msDS-SupportedEncryptionTypes bit-field
214 * @param[in] delete_all_kvno Removes all obsolete entries, without
215 * recreating the keytab.
216 * @param[out] _keytab If supplied, returns the keytab
217 * @param[out] perror_string Error string on failure
219 * @return 0 on success, errno on failure
221 krb5_error_code smb_krb5_update_keytab(TALLOC_CTX *parent_ctx,
222 krb5_context context,
223 const char *keytab_name,
224 const char *samAccountName,
228 const char *saltPrincipal,
229 const char *new_secret,
230 const char *old_secret,
232 uint32_t supp_enctypes,
233 bool delete_all_kvno,
234 krb5_keytab *_keytab,
235 const char **perror_string)
237 krb5_keytab keytab = NULL;
239 bool found_previous = false;
240 TALLOC_CTX *tmp_ctx = NULL;
241 krb5_principal *principals = NULL;
242 uint32_t num_principals = 0;
244 const char *error_string = NULL;
246 if (keytab_name == NULL) {
250 ret = krb5_kt_resolve(context, keytab_name, &keytab);
252 *perror_string = smb_get_krb5_error_message(context,
257 DEBUG(5, ("Opened keytab %s\n", keytab_name));
259 tmp_ctx = talloc_new(parent_ctx);
261 *perror_string = talloc_strdup(parent_ctx,
262 "Failed to allocate memory context");
267 upper_realm = strupper_talloc(tmp_ctx, realm);
268 if (upper_realm == NULL) {
269 *perror_string = talloc_strdup(parent_ctx,
270 "Cannot allocate memory to upper case realm");
275 ret = smb_krb5_create_principals_array(tmp_ctx,
285 *perror_string = talloc_asprintf(parent_ctx,
286 "Failed to load principals from ldb message: %s\n",
291 ret = smb_krb5_remove_obsolete_keytab_entries(tmp_ctx,
300 *perror_string = talloc_asprintf(parent_ctx,
301 "Failed to remove old principals from keytab: %s\n",
306 if (!delete_all_kvno) {
307 /* Create a new keytab. If during the cleanout we found
308 * entries for kvno -1, then don't try and duplicate them.
309 * Otherwise, add kvno, and kvno -1 */
310 if (saltPrincipal == NULL) {
311 *perror_string = talloc_strdup(parent_ctx,
312 "No saltPrincipal provided");
317 ret = create_keytab(tmp_ctx,
319 kvno, new_secret, old_secret,
324 found_previous ? false : true,
327 *perror_string = talloc_steal(parent_ctx, error_string);
331 if (ret == 0 && _keytab != NULL) {
332 /* caller wants the keytab handle back */
337 keytab_principals_free(context, num_principals, principals);
338 if (ret != 0 || _keytab == NULL) {
339 krb5_kt_close(context, keytab);
341 talloc_free(tmp_ctx);
346 * @brief Wrapper around smb_krb5_update_keytab() for creating an in-memory keytab
348 * @param[in] parent_ctx Talloc memory context
349 * @param[in] context Kerberos context
350 * @param[in] new_secret New password
351 * @param[in] samAccountName User account to update
352 * @param[in] realm Kerberos realm
353 * @param[in] salt_principal Salt used for AES encryption.
354 * Required, unless delete_all_kvno is set.
355 * @param[in] kvno Current key version number
356 * @param[out] keytab If supplied, returns the keytab
357 * @param[out] keytab_name Returns the created keytab name
359 * @return 0 on success, errno on failure
361 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
362 krb5_context context,
363 const char *new_secret,
364 const char *samAccountName,
366 const char *salt_principal,
369 const char **keytab_name)
372 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
373 const char *rand_string;
374 const char *error_string = NULL;
379 rand_string = generate_random_str(mem_ctx, 16);
381 talloc_free(mem_ctx);
385 *keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s", rand_string);
386 if (*keytab_name == NULL) {
387 talloc_free(mem_ctx);
391 ret = smb_krb5_update_keytab(mem_ctx, context,
392 *keytab_name, samAccountName, realm,
393 NULL, 0, salt_principal, new_secret, NULL,
395 false, keytab, &error_string);
397 talloc_steal(parent_ctx, *keytab_name);
399 DEBUG(0, ("Failed to create in-memory keytab: %s\n",
403 talloc_free(mem_ctx);