1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
9 #include <kadm5/admin.h>
11 #include "server_internal.h"
12 #ifdef USE_PASSWORD_SERVER
17 #include <krb5/kadm5_hook_plugin.h>
20 #include <valgrind/memcheck.h>
22 #define VALGRIND_CHECK_DEFINED(LVALUE) ((void)0)
25 extern krb5_principal master_princ;
26 extern krb5_principal hist_princ;
27 extern krb5_keyblock master_keyblock;
28 extern krb5_actkvno_node *active_mkey_list;
29 extern krb5_db_entry master_db;
31 static int decrypt_key_data(krb5_context context,
32 int n_key_data, krb5_key_data *key_data,
33 krb5_keyblock **keyblocks, int *n_keys);
35 static krb5_error_code
36 kadm5_copy_principal(krb5_context context, krb5_const_principal inprinc, krb5_principal *outprinc)
38 register krb5_principal tempprinc;
39 register int i, nelems;
41 tempprinc = (krb5_principal)krb5_db_alloc(context, NULL, sizeof(krb5_principal_data));
46 VALGRIND_CHECK_DEFINED(*inprinc);
47 *tempprinc = *inprinc;
49 nelems = (int) krb5_princ_size(context, inprinc);
50 tempprinc->data = krb5_db_alloc(context, NULL, nelems * sizeof(krb5_data));
51 if (tempprinc->data == 0) {
52 krb5_db_free(context, (char *)tempprinc);
56 for (i = 0; i < nelems; i++) {
57 unsigned int len = krb5_princ_component(context, inprinc, i)->length;
58 krb5_princ_component(context, tempprinc, i)->length = len;
59 if (((krb5_princ_component(context, tempprinc, i)->data =
60 krb5_db_alloc(context, NULL, len)) == 0) && len) {
62 krb5_db_free(context, krb5_princ_component(context, tempprinc, i)->data);
63 krb5_db_free (context, tempprinc->data);
64 krb5_db_free (context, tempprinc);
68 memcpy(krb5_princ_component(context, tempprinc, i)->data,
69 krb5_princ_component(context, inprinc, i)->data, len);
70 krb5_princ_component(context, tempprinc, i)->magic = KV5M_DATA;
73 tempprinc->realm.data =
74 krb5_db_alloc(context, NULL, tempprinc->realm.length = inprinc->realm.length);
75 if (!tempprinc->realm.data && tempprinc->realm.length) {
76 for (i = 0; i < nelems; i++)
77 krb5_db_free(context, krb5_princ_component(context, tempprinc, i)->data);
78 krb5_db_free(context, tempprinc->data);
79 krb5_db_free(context, tempprinc);
82 if (tempprinc->realm.length)
83 memcpy(tempprinc->realm.data, inprinc->realm.data,
84 inprinc->realm.length);
86 *outprinc = tempprinc;
91 kadm5_free_principal(krb5_context context, krb5_principal val)
93 register krb5_int32 i;
99 i = krb5_princ_size(context, val);
101 krb5_db_free(context, krb5_princ_component(context, val, i)->data);
102 krb5_db_free(context, val->data);
105 krb5_db_free(context, val->realm.data);
106 krb5_db_free(context, val);
110 * XXX Functions that ought to be in libkrb5.a, but aren't.
112 kadm5_ret_t krb5_copy_key_data_contents(context, from, to)
113 krb5_context context;
114 krb5_key_data *from, *to;
120 idx = (from->key_data_ver == 1 ? 1 : 2);
122 for (i = 0; i < idx; i++) {
123 if ( from->key_data_length[i] ) {
124 to->key_data_contents[i] = malloc(from->key_data_length[i]);
125 if (to->key_data_contents[i] == NULL) {
126 for (i = 0; i < idx; i++) {
127 if (to->key_data_contents[i]) {
128 memset(to->key_data_contents[i], 0,
129 to->key_data_length[i]);
130 free(to->key_data_contents[i]);
135 memcpy(to->key_data_contents[i], from->key_data_contents[i],
136 from->key_data_length[i]);
142 static krb5_tl_data *dup_tl_data(krb5_tl_data *tl)
146 n = (krb5_tl_data *) malloc(sizeof(krb5_tl_data));
149 n->tl_data_contents = malloc(tl->tl_data_length);
150 if (n->tl_data_contents == NULL) {
154 memcpy(n->tl_data_contents, tl->tl_data_contents, tl->tl_data_length);
155 n->tl_data_type = tl->tl_data_type;
156 n->tl_data_length = tl->tl_data_length;
157 n->tl_data_next = NULL;
161 /* This is in lib/kdb/kdb_cpw.c, but is static */
162 static void cleanup_key_data(context, count, data)
163 krb5_context context;
165 krb5_key_data * data;
169 for (i = 0; i < count; i++)
170 for (j = 0; j < data[i].key_data_ver; j++)
171 if (data[i].key_data_length[j])
172 krb5_db_free(context, data[i].key_data_contents[j]);
173 krb5_db_free(context, data);
177 * Set *passptr to NULL if the request looks like the first part of a krb5 1.6
178 * addprinc -randkey operation. The krb5 1.6 dummy password for these requests
179 * was invalid UTF-8, which runs afoul of the arcfour string-to-key.
182 check_1_6_dummy(kadm5_principal_ent_t entry, long mask,
183 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple, char **passptr)
186 char *password = *passptr;
188 /* Old-style randkey operations disallowed tickets to start. */
189 if (!(mask & KADM5_ATTRIBUTES) ||
190 !(entry->attributes & KRB5_KDB_DISALLOW_ALL_TIX))
193 /* The 1.6 dummy password was the octets 1..255. */
194 for (i = 0; (unsigned char) password[i] == i + 1; i++);
195 if (password[i] != '\0' || i != 255)
198 /* This will make the caller use a random password instead. */
203 kadm5_create_principal(void *server_handle,
204 kadm5_principal_ent_t entry, long mask,
208 kadm5_create_principal_3(server_handle, entry, mask,
212 kadm5_create_principal_3(void *server_handle,
213 kadm5_principal_ent_t entry, long mask,
214 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,
218 osa_princ_ent_rec adb;
219 kadm5_policy_ent_rec polent;
220 krb5_boolean have_polent = FALSE;
222 krb5_tl_data *tl_data_orig, *tl_data_tail;
224 kadm5_server_handle_t handle = server_handle;
225 krb5_keyblock *act_mkey;
228 CHECK_HANDLE(server_handle);
230 krb5_clear_error_message(handle->context);
232 check_1_6_dummy(entry, mask, n_ks_tuple, ks_tuple, &password);
235 * Argument sanity checking, and opening up the DB
237 if(!(mask & KADM5_PRINCIPAL) || (mask & KADM5_MOD_NAME) ||
238 (mask & KADM5_MOD_TIME) || (mask & KADM5_LAST_PWD_CHANGE) ||
239 (mask & KADM5_MKVNO) || (mask & KADM5_POLICY_CLR) ||
240 (mask & KADM5_AUX_ATTRIBUTES) || (mask & KADM5_KEY_DATA) ||
241 (mask & KADM5_LAST_SUCCESS) || (mask & KADM5_LAST_FAILED) ||
242 (mask & KADM5_FAIL_AUTH_COUNT))
243 return KADM5_BAD_MASK;
244 if((mask & ~ALL_PRINC_MASK))
245 return KADM5_BAD_MASK;
249 /* Use default keysalts if caller did not provide any. */
250 if (n_ks_tuple == 0) {
251 ks_tuple = handle->params.keysalts;
252 n_ks_tuple = handle->params.num_keysalts;
256 * Check to see if the principal exists
258 ret = kdb_get_entry(handle, entry->principal, &kdb, &adb);
261 case KADM5_UNK_PRINC:
264 kdb_free_entry(handle, kdb, &adb);
270 kdb = krb5_db_alloc(handle->context, NULL, sizeof(*kdb));
273 memset(kdb, 0, sizeof(*kdb));
274 memset(&adb, 0, sizeof(osa_princ_ent_rec));
277 * If a policy was specified, load it.
278 * If we can not find the one specified return an error
280 if ((mask & KADM5_POLICY)) {
281 if ((ret = kadm5_get_policy(handle->lhandle, entry->policy,
282 &polent)) != KADM5_OK) {
284 ret = KADM5_BAD_POLICY;
291 ret = passwd_check(handle, password, have_polent ? &polent : NULL,
297 * Start populating the various DB fields, using the
298 * "defaults" for fields that were not specified by the
301 if ((ret = krb5_timeofday(handle->context, &now)))
304 kdb->magic = KRB5_KDB_MAGIC_NUMBER;
305 kdb->len = KRB5_KDB_V1_BASE_LENGTH; /* gag me with a chainsaw */
307 if ((mask & KADM5_ATTRIBUTES))
308 kdb->attributes = entry->attributes;
310 kdb->attributes = handle->params.flags;
312 if ((mask & KADM5_MAX_LIFE))
313 kdb->max_life = entry->max_life;
315 kdb->max_life = handle->params.max_life;
317 if (mask & KADM5_MAX_RLIFE)
318 kdb->max_renewable_life = entry->max_renewable_life;
320 kdb->max_renewable_life = handle->params.max_rlife;
322 if ((mask & KADM5_PRINC_EXPIRE_TIME))
323 kdb->expiration = entry->princ_expire_time;
325 kdb->expiration = handle->params.expiration;
327 kdb->pw_expiration = 0;
329 if(polent.pw_max_life)
330 kdb->pw_expiration = now + polent.pw_max_life;
332 kdb->pw_expiration = 0;
334 if ((mask & KADM5_PW_EXPIRATION))
335 kdb->pw_expiration = entry->pw_expiration;
337 kdb->last_success = 0;
338 kdb->last_failed = 0;
339 kdb->fail_auth_count = 0;
341 /* this is kind of gross, but in order to free the tl data, I need
342 to free the entire kdb entry, and that will try to free the
345 if ((ret = kadm5_copy_principal(handle->context,
346 entry->principal, &(kdb->princ))))
349 if ((ret = krb5_dbe_update_last_pwd_change(handle->context, kdb, now)))
352 if (mask & KADM5_TL_DATA) {
353 /* splice entry->tl_data onto the front of kdb->tl_data */
354 tl_data_orig = kdb->tl_data;
355 for (tl_data_tail = entry->tl_data; tl_data_tail;
356 tl_data_tail = tl_data_tail->tl_data_next)
358 ret = krb5_dbe_update_tl_data(handle->context, kdb, tl_data_tail);
364 /* initialize the keys */
366 ret = krb5_dbe_find_act_mkey(handle->context, active_mkey_list, &act_kvno,
372 ret = krb5_dbe_cpw(handle->context, act_mkey, ks_tuple, n_ks_tuple,
373 password, (mask & KADM5_KVNO)?entry->kvno:1,
376 /* Null password means create with random key (new in 1.8). */
377 ret = krb5_dbe_crk(handle->context, &master_keyblock,
378 ks_tuple, n_ks_tuple, FALSE, kdb);
383 /* Record the master key VNO used to encrypt this entry's keys */
384 ret = krb5_dbe_update_mkvno(handle->context, kdb, act_kvno);
388 ret = k5_kadm5_hook_create(handle->context, handle->hook_handles,
389 KADM5_HOOK_STAGE_PRECOMMIT, entry, mask,
390 n_ks_tuple, ks_tuple, password);
394 /* populate the admin-server-specific fields. In the OV server,
395 this used to be in a separate database. Since there's already
396 marshalling code for the admin fields, to keep things simple,
397 I'm going to keep it, and make all the admin stuff occupy a
398 single tl_data record, */
400 adb.admin_history_kvno = INITIAL_HIST_KVNO;
402 adb.aux_attributes = KADM5_POLICY;
404 /* this does *not* need to be strdup'ed, because adb is xdr */
405 /* encoded in osa_adb_create_princ, and not ever freed */
407 adb.policy = entry->policy;
410 /* increment the policy ref count, if any */
413 polent.policy_refcnt++;
414 if ((ret = kadm5_modify_policy_internal(handle->lhandle, &polent,
420 /* In all cases key and the principal data is set, let the database provider know */
421 kdb->mask = mask | KADM5_KEY_DATA | KADM5_PRINCIPAL ;
423 /* store the new db entry */
424 ret = kdb_put_entry(handle, kdb, &adb);
429 /* decrement the policy ref count */
431 polent.policy_refcnt--;
433 * if this fails, there's nothing we can do anyway. the
434 * policy refcount wil be too high.
436 (void) kadm5_modify_policy_internal(handle->lhandle, &polent,
441 (void) k5_kadm5_hook_create(handle->context, handle->hook_handles,
442 KADM5_HOOK_STAGE_POSTCOMMIT, entry, mask,
443 n_ks_tuple, ks_tuple, password);
446 krb5_db_free_principal(handle->context, kdb);
448 (void) kadm5_free_policy_ent(handle->lhandle, &polent);
454 kadm5_delete_principal(void *server_handle, krb5_principal principal)
457 kadm5_policy_ent_rec polent;
459 osa_princ_ent_rec adb;
460 kadm5_server_handle_t handle = server_handle;
462 CHECK_HANDLE(server_handle);
464 krb5_clear_error_message(handle->context);
466 if (principal == NULL)
469 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
471 ret = k5_kadm5_hook_remove(handle->context, handle->hook_handles,
472 KADM5_HOOK_STAGE_PRECOMMIT, principal);
474 kdb_free_entry(handle, kdb, &adb);
478 if ((adb.aux_attributes & KADM5_POLICY)) {
479 if ((ret = kadm5_get_policy(handle->lhandle,
480 adb.policy, &polent))
482 polent.policy_refcnt--;
483 if ((ret = kadm5_modify_policy_internal(handle->lhandle, &polent,
486 (void) kadm5_free_policy_ent(handle->lhandle, &polent);
487 kdb_free_entry(handle, kdb, &adb);
491 if ((ret = kadm5_free_policy_ent(handle->lhandle, &polent))) {
492 kdb_free_entry(handle, kdb, &adb);
497 ret = kdb_delete_entry(handle, principal);
499 kdb_free_entry(handle, kdb, &adb);
502 (void) k5_kadm5_hook_remove(handle->context,
503 handle->hook_handles,
504 KADM5_HOOK_STAGE_POSTCOMMIT, principal);
510 kadm5_modify_principal(void *server_handle,
511 kadm5_principal_ent_t entry, long mask)
514 kadm5_policy_ent_rec npol, opol;
515 int have_npol = 0, have_opol = 0;
517 krb5_tl_data *tl_data_orig;
518 osa_princ_ent_rec adb;
519 kadm5_server_handle_t handle = server_handle;
521 CHECK_HANDLE(server_handle);
523 krb5_clear_error_message(handle->context);
525 if((mask & KADM5_PRINCIPAL) || (mask & KADM5_LAST_PWD_CHANGE) ||
526 (mask & KADM5_MOD_TIME) || (mask & KADM5_MOD_NAME) ||
527 (mask & KADM5_MKVNO) || (mask & KADM5_AUX_ATTRIBUTES) ||
528 (mask & KADM5_KEY_DATA) || (mask & KADM5_LAST_SUCCESS) ||
529 (mask & KADM5_LAST_FAILED))
530 return KADM5_BAD_MASK;
531 if((mask & ~ALL_PRINC_MASK))
532 return KADM5_BAD_MASK;
533 if((mask & KADM5_POLICY) && (mask & KADM5_POLICY_CLR))
534 return KADM5_BAD_MASK;
535 if(entry == (kadm5_principal_ent_t) NULL)
537 if (mask & KADM5_TL_DATA) {
538 tl_data_orig = entry->tl_data;
539 while (tl_data_orig) {
540 if (tl_data_orig->tl_data_type < 256)
541 return KADM5_BAD_TL_TYPE;
542 tl_data_orig = tl_data_orig->tl_data_next;
546 ret = kdb_get_entry(handle, entry->principal, &kdb, &adb);
551 * This is pretty much the same as create ...
554 if ((mask & KADM5_POLICY)) {
555 /* get the new policy */
556 ret = kadm5_get_policy(handle->lhandle, entry->policy, &npol);
560 ret = KADM5_BAD_POLICY;
562 case KADM5_UNK_POLICY:
563 case KADM5_BAD_POLICY:
564 ret = KADM5_UNK_POLICY;
571 /* if we already have a policy, get it to decrement the refcnt */
572 if(adb.aux_attributes & KADM5_POLICY) {
573 /* ... but not if the old and new are the same */
574 if(strcmp(adb.policy, entry->policy)) {
575 ret = kadm5_get_policy(handle->lhandle,
579 case KADM5_BAD_POLICY:
580 case KADM5_UNK_POLICY:
584 opol.policy_refcnt--;
590 npol.policy_refcnt++;
592 } else npol.policy_refcnt++;
594 /* set us up to use the new policy */
595 adb.aux_attributes |= KADM5_POLICY;
598 adb.policy = strdup(entry->policy);
600 /* set pw_max_life based on new policy */
601 if (npol.pw_max_life) {
602 ret = krb5_dbe_lookup_last_pwd_change(handle->context, kdb,
603 &(kdb->pw_expiration));
606 kdb->pw_expiration += npol.pw_max_life;
608 kdb->pw_expiration = 0;
612 if ((mask & KADM5_POLICY_CLR) &&
613 (adb.aux_attributes & KADM5_POLICY)) {
614 ret = kadm5_get_policy(handle->lhandle, adb.policy, &opol);
617 case KADM5_BAD_POLICY:
618 case KADM5_UNK_POLICY:
627 adb.aux_attributes &= ~KADM5_POLICY;
628 kdb->pw_expiration = 0;
629 opol.policy_refcnt--;
637 if (((mask & KADM5_POLICY) || (mask & KADM5_POLICY_CLR)) &&
640 kadm5_modify_policy_internal(handle->lhandle, &opol,
641 KADM5_REF_COUNT))) ||
644 kadm5_modify_policy_internal(handle->lhandle, &npol,
648 if ((mask & KADM5_ATTRIBUTES))
649 kdb->attributes = entry->attributes;
650 if ((mask & KADM5_MAX_LIFE))
651 kdb->max_life = entry->max_life;
652 if ((mask & KADM5_PRINC_EXPIRE_TIME))
653 kdb->expiration = entry->princ_expire_time;
654 if (mask & KADM5_PW_EXPIRATION)
655 kdb->pw_expiration = entry->pw_expiration;
656 if (mask & KADM5_MAX_RLIFE)
657 kdb->max_renewable_life = entry->max_renewable_life;
659 if((mask & KADM5_KVNO)) {
660 for (i = 0; i < kdb->n_key_data; i++)
661 kdb->key_data[i].key_data_kvno = entry->kvno;
664 if (mask & KADM5_TL_DATA) {
667 /* may have to change the version number of the API. Updates the list with the given tl_data rather than over-writting */
669 for (tl = entry->tl_data; tl;
670 tl = tl->tl_data_next)
672 ret = krb5_dbe_update_tl_data(handle->context, kdb, tl);
681 * Setting entry->fail_auth_count to 0 can be used to manually unlock
682 * an account. It is not possible to set fail_auth_count to any other
683 * value using kadmin.
685 if (mask & KADM5_FAIL_AUTH_COUNT) {
686 if (entry->fail_auth_count != 0) {
687 ret = KADM5_BAD_SERVER_PARAMS;
691 kdb->fail_auth_count = 0;
694 /* let the mask propagate to the database provider */
697 ret = k5_kadm5_hook_modify(handle->context, handle->hook_handles,
698 KADM5_HOOK_STAGE_PRECOMMIT, entry, mask);
702 ret = kdb_put_entry(handle, kdb, &adb);
704 (void) k5_kadm5_hook_modify(handle->context, handle->hook_handles,
705 KADM5_HOOK_STAGE_POSTCOMMIT, entry, mask);
710 ret2 = kadm5_free_policy_ent(handle->lhandle, &opol);
711 ret = ret ? ret : ret2;
714 ret2 = kadm5_free_policy_ent(handle->lhandle, &npol);
715 ret = ret ? ret : ret2;
717 kdb_free_entry(handle, kdb, &adb);
722 kadm5_rename_principal(void *server_handle,
723 krb5_principal source, krb5_principal target)
726 osa_princ_ent_rec adb;
728 kadm5_server_handle_t handle = server_handle;
730 krb5_data *salt = NULL;
732 CHECK_HANDLE(server_handle);
734 krb5_clear_error_message(handle->context);
736 if (source == NULL || target == NULL)
739 if ((ret = kdb_get_entry(handle, target, &kdb, &adb)) == 0) {
740 kdb_free_entry(handle, kdb, &adb);
744 if ((ret = kdb_get_entry(handle, source, &kdb, &adb)))
747 /* Transform salts as necessary. */
748 for (i = 0; i < kdb->n_key_data; i++) {
749 ret = krb5_dbe_compute_salt(handle->context, &kdb->key_data[i],
750 kdb->princ, &stype, &salt);
751 if (ret == KRB5_KDB_BAD_SALTTYPE)
752 ret = KADM5_NO_RENAME_SALT;
755 kdb->key_data[i].key_data_type[1] = KRB5_KDB_SALTTYPE_SPECIAL;
756 free(kdb->key_data[i].key_data_contents[1]);
757 kdb->key_data[i].key_data_contents[1] = (krb5_octet *)salt->data;
758 kdb->key_data[i].key_data_length[1] = salt->length;
759 kdb->key_data[i].key_data_ver = 2;
764 kadm5_free_principal(handle->context, kdb->princ);
765 ret = kadm5_copy_principal(handle->context, target, &kdb->princ);
767 kdb->princ = NULL; /* so freeing the dbe doesn't lose */
771 if ((ret = kdb_put_entry(handle, kdb, &adb)))
774 ret = kdb_delete_entry(handle, source);
777 krb5_free_data(handle->context, salt);
778 kdb_free_entry(handle, kdb, &adb);
783 kadm5_get_principal(void *server_handle, krb5_principal principal,
784 kadm5_principal_ent_t entry,
788 osa_princ_ent_rec adb;
789 krb5_error_code ret = 0;
792 kadm5_server_handle_t handle = server_handle;
794 CHECK_HANDLE(server_handle);
796 krb5_clear_error_message(handle->context);
799 * In version 1, all the defined fields are always returned.
800 * entry is a pointer to a kadm5_principal_ent_t_v1 that should be
801 * filled with allocated memory.
805 memset(entry, 0, sizeof(*entry));
807 if (principal == NULL)
810 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
813 if ((mask & KADM5_POLICY) &&
814 adb.policy && (adb.aux_attributes & KADM5_POLICY)) {
815 if ((entry->policy = strdup(adb.policy)) == NULL) {
821 if (mask & KADM5_AUX_ATTRIBUTES)
822 entry->aux_attributes = adb.aux_attributes;
824 if ((mask & KADM5_PRINCIPAL) &&
825 (ret = krb5_copy_principal(handle->context, kdb->princ,
826 &entry->principal))) {
830 if (mask & KADM5_PRINC_EXPIRE_TIME)
831 entry->princ_expire_time = kdb->expiration;
833 if ((mask & KADM5_LAST_PWD_CHANGE) &&
834 (ret = krb5_dbe_lookup_last_pwd_change(handle->context, kdb,
835 &(entry->last_pwd_change)))) {
839 if (mask & KADM5_PW_EXPIRATION)
840 entry->pw_expiration = kdb->pw_expiration;
841 if (mask & KADM5_MAX_LIFE)
842 entry->max_life = kdb->max_life;
844 /* this is a little non-sensical because the function returns two */
845 /* values that must be checked separately against the mask */
846 if ((mask & KADM5_MOD_NAME) || (mask & KADM5_MOD_TIME)) {
847 ret = krb5_dbe_lookup_mod_princ_data(handle->context, kdb,
854 if (! (mask & KADM5_MOD_TIME))
856 if (! (mask & KADM5_MOD_NAME)) {
857 krb5_free_principal(handle->context, entry->mod_name);
858 entry->mod_name = NULL;
862 if (mask & KADM5_ATTRIBUTES)
863 entry->attributes = kdb->attributes;
865 if (mask & KADM5_KVNO)
866 for (entry->kvno = 0, i=0; i<kdb->n_key_data; i++)
867 if ((krb5_kvno) kdb->key_data[i].key_data_kvno > entry->kvno)
868 entry->kvno = kdb->key_data[i].key_data_kvno;
870 if (mask & KADM5_MKVNO) {
871 ret = krb5_dbe_get_mkvno(handle->context, kdb, &entry->mkvno);
876 if (mask & KADM5_MAX_RLIFE)
877 entry->max_renewable_life = kdb->max_renewable_life;
878 if (mask & KADM5_LAST_SUCCESS)
879 entry->last_success = kdb->last_success;
880 if (mask & KADM5_LAST_FAILED)
881 entry->last_failed = kdb->last_failed;
882 if (mask & KADM5_FAIL_AUTH_COUNT)
883 entry->fail_auth_count = kdb->fail_auth_count;
884 if (mask & KADM5_TL_DATA) {
885 krb5_tl_data *tl, *tl2;
887 entry->tl_data = NULL;
891 if (tl->tl_data_type > 255) {
892 if ((tl2 = dup_tl_data(tl)) == NULL) {
896 tl2->tl_data_next = entry->tl_data;
897 entry->tl_data = tl2;
901 tl = tl->tl_data_next;
904 if (mask & KADM5_KEY_DATA) {
905 entry->n_key_data = kdb->n_key_data;
906 if(entry->n_key_data) {
907 entry->key_data = malloc(entry->n_key_data*sizeof(krb5_key_data));
908 if (entry->key_data == NULL) {
913 entry->key_data = NULL;
915 for (i = 0; i < entry->n_key_data; i++)
916 ret = krb5_copy_key_data_contents(handle->context,
918 &entry->key_data[i]);
926 if (ret && entry->principal) {
927 krb5_free_principal(handle->context, entry->principal);
928 entry->principal = NULL;
930 kdb_free_entry(handle, kdb, &adb);
936 * Function: check_pw_reuse
938 * Purpose: Check if a key appears in a list of keys, in order to
939 * enforce password history.
943 * context (r) the krb5 context
944 * hist_keyblock (r) the key that hist_key_data is
946 * n_new_key_data (r) length of new_key_data
947 * new_key_data (r) keys to check against
948 * pw_hist_data, encrypted in hist_keyblock
949 * n_pw_hist_data (r) length of pw_hist_data
950 * pw_hist_data (r) passwords to check new_key_data against
953 * For each new_key in new_key_data:
954 * decrypt new_key with the master_keyblock
955 * for each password in pw_hist_data:
956 * for each hist_key in password:
957 * decrypt hist_key with hist_keyblock
958 * compare the new_key and hist_key
960 * Returns krb5 errors, KADM5_PASS_RESUSE if a key in
961 * new_key_data is the same as a key in pw_hist_data, or 0.
964 check_pw_reuse(krb5_context context,
965 krb5_keyblock *hist_keyblock,
966 int n_new_key_data, krb5_key_data *new_key_data,
967 unsigned int n_pw_hist_data, osa_pw_hist_ent *pw_hist_data)
969 unsigned int x, y, z;
970 krb5_keyblock newkey, histkey;
973 assert (n_new_key_data >= 0);
974 for (x = 0; x < (unsigned) n_new_key_data; x++) {
975 ret = krb5_dbe_decrypt_key_data(context, NULL, &(new_key_data[x]),
979 for (y = 0; y < n_pw_hist_data; y++) {
980 for (z = 0; z < (unsigned int) pw_hist_data[y].n_key_data; z++) {
981 ret = krb5_dbe_decrypt_key_data(context, hist_keyblock,
982 &pw_hist_data[y].key_data[z],
987 if ((newkey.length == histkey.length) &&
988 (newkey.enctype == histkey.enctype) &&
989 (memcmp(newkey.contents, histkey.contents,
990 histkey.length) == 0)) {
991 krb5_free_keyblock_contents(context, &histkey);
992 krb5_free_keyblock_contents(context, &newkey);
994 return(KADM5_PASS_REUSE);
996 krb5_free_keyblock_contents(context, &histkey);
999 krb5_free_keyblock_contents(context, &newkey);
1006 * Function: create_history_entry
1008 * Purpose: Creates a password history entry from an array of
1013 * context (r) krb5_context to use
1014 * mkey (r) master keyblock to decrypt key data with
1015 * hist_key (r) history keyblock to encrypt key data with
1016 * n_key_data (r) number of elements in key_data
1017 * key_data (r) keys to add to the history entry
1018 * hist (w) history entry to fill in
1022 * hist->key_data is allocated to store n_key_data key_datas. Each
1023 * element of key_data is decrypted with master_keyblock, re-encrypted
1024 * in hist_key, and added to hist->key_data. hist->n_key_data is
1025 * set to n_key_data.
1028 int create_history_entry(krb5_context context,
1029 krb5_keyblock *hist_key, int n_key_data,
1030 krb5_key_data *key_data, osa_pw_hist_ent *hist)
1036 hist->key_data = (krb5_key_data*)malloc(n_key_data*sizeof(krb5_key_data));
1037 if (hist->key_data == NULL)
1039 memset(hist->key_data, 0, n_key_data*sizeof(krb5_key_data));
1041 for (i = 0; i < n_key_data; i++) {
1042 ret = krb5_dbe_decrypt_key_data(context, NULL, &key_data[i], &key,
1047 ret = krb5_dbe_encrypt_key_data(context, hist_key, &key, &salt,
1048 key_data[i].key_data_kvno,
1049 &hist->key_data[i]);
1053 krb5_free_keyblock_contents(context, &key);
1054 /* krb5_free_keysalt(context, &salt); */
1057 hist->n_key_data = n_key_data;
1062 void free_history_entry(krb5_context context, osa_pw_hist_ent *hist)
1066 for (i = 0; i < hist->n_key_data; i++)
1067 krb5_free_key_data_contents(context, &hist->key_data[i]);
1068 free(hist->key_data);
1072 * Function: add_to_history
1074 * Purpose: Adds a password to a principal's password history.
1078 * context (r) krb5_context to use
1079 * hist_kvno (r) kvno of current history key
1080 * adb (r/w) admin principal entry to add keys to
1081 * pol (r) adb's policy
1082 * pw (r) keys for the password to add to adb's key history
1086 * add_to_history adds a single password to adb's password history.
1087 * pw contains n_key_data keys in its key_data, in storage should be
1088 * allocated but not freed by the caller (XXX blech!).
1090 * This function maintains adb->old_keys as a circular queue. It
1091 * starts empty, and grows each time this function is called until it
1092 * is pol->pw_history_num items long. adb->old_key_len holds the
1093 * number of allocated entries in the array, and must therefore be [0,
1094 * pol->pw_history_num). adb->old_key_next is the index into the
1095 * array where the next element should be written, and must be [0,
1096 * adb->old_key_len).
1098 static kadm5_ret_t add_to_history(krb5_context context,
1099 krb5_kvno hist_kvno,
1100 osa_princ_ent_t adb,
1101 kadm5_policy_ent_t pol,
1102 osa_pw_hist_ent *pw)
1104 osa_pw_hist_ent *histp;
1106 unsigned int i, knext, nkeys;
1108 nhist = pol->pw_history_num;
1109 /* A history of 1 means just check the current password */
1113 if (adb->admin_history_kvno != hist_kvno) {
1114 /* The history key has changed since the last password change, so we
1115 * have to reset the password history. */
1116 free(adb->old_keys);
1117 adb->old_keys = NULL;
1118 adb->old_key_len = 0;
1119 adb->old_key_next = 0;
1120 adb->admin_history_kvno = hist_kvno;
1123 nkeys = adb->old_key_len;
1124 knext = adb->old_key_next;
1125 /* resize the adb->old_keys array if necessary */
1126 if (nkeys + 1 < nhist) {
1127 if (adb->old_keys == NULL) {
1128 adb->old_keys = (osa_pw_hist_ent *)
1129 malloc((nkeys + 1) * sizeof (osa_pw_hist_ent));
1131 adb->old_keys = (osa_pw_hist_ent *)
1132 realloc(adb->old_keys,
1133 (nkeys + 1) * sizeof (osa_pw_hist_ent));
1135 if (adb->old_keys == NULL)
1138 memset(&adb->old_keys[nkeys], 0, sizeof(osa_pw_hist_ent));
1139 nkeys = ++adb->old_key_len;
1141 * To avoid losing old keys, shift forward each entry after
1144 for (i = nkeys - 1; i > knext; i--) {
1145 adb->old_keys[i] = adb->old_keys[i - 1];
1147 memset(&adb->old_keys[knext], 0, sizeof(osa_pw_hist_ent));
1148 } else if (nkeys + 1 > nhist) {
1150 * The policy must have changed! Shrink the array.
1151 * Can't simply realloc() down, since it might be wrapped.
1152 * To understand the arithmetic below, note that we are
1153 * copying into new positions 0 .. N-1 from old positions
1154 * old_key_next-N .. old_key_next-1, modulo old_key_len,
1155 * where N = pw_history_num - 1 is the length of the
1156 * shortened list. Matt Crawford, FNAL
1159 * M = adb->old_key_len, N = pol->pw_history_num - 1
1161 * tmp[0] .. tmp[N-1] = old[(knext-N)%M] .. old[(knext-1)%M]
1166 tmp = (osa_pw_hist_ent *)
1167 malloc((nhist - 1) * sizeof (osa_pw_hist_ent));
1170 for (i = 0; i < nhist - 1; i++) {
1172 * Add nkeys once before taking remainder to avoid
1175 j = (i + nkeys + knext - (nhist - 1)) % nkeys;
1176 tmp[i] = adb->old_keys[j];
1178 /* Now free the ones we don't keep (the oldest ones) */
1179 for (i = 0; i < nkeys - (nhist - 1); i++) {
1180 j = (i + nkeys + knext) % nkeys;
1181 histp = &adb->old_keys[j];
1182 for (j = 0; j < histp->n_key_data; j++) {
1183 krb5_free_key_data_contents(context, &histp->key_data[j]);
1185 free(histp->key_data);
1187 free(adb->old_keys);
1188 adb->old_keys = tmp;
1189 nkeys = adb->old_key_len = nhist - 1;
1190 knext = adb->old_key_next = 0;
1194 * If nhist decreased since the last password change, and nkeys+1
1195 * is less than the previous nhist, it is possible for knext to
1196 * index into unallocated space. This condition would not be
1197 * caught by the resizing code above.
1199 if (knext + 1 > nkeys)
1200 knext = adb->old_key_next = 0;
1201 /* free the old pw history entry if it contains data */
1202 histp = &adb->old_keys[knext];
1203 for (i = 0; i < (unsigned int) histp->n_key_data; i++)
1204 krb5_free_key_data_contents(context, &histp->key_data[i]);
1205 free(histp->key_data);
1207 /* store the new entry */
1208 adb->old_keys[knext] = *pw;
1210 /* update the next pointer */
1211 if (++adb->old_key_next == nhist - 1)
1212 adb->old_key_next = 0;
1217 /* FIXME: don't use global variable for this */
1218 krb5_boolean use_password_server = 0;
1220 #ifdef USE_PASSWORD_SERVER
1222 kadm5_use_password_server (void)
1224 return use_password_server;
1229 kadm5_set_use_password_server (void)
1231 use_password_server = 1;
1234 #ifdef USE_PASSWORD_SERVER
1237 * kadm5_launch_task () runs a program (task_path) to synchronize the
1238 * Apple password server with the Kerberos database. Password server
1239 * programs can receive arguments on the command line (task_argv)
1240 * and a block of data via stdin (data_buffer).
1242 * Because a failure to communicate with the tool results in the
1243 * password server falling out of sync with the database,
1244 * kadm5_launch_task() always fails if it can't talk to the tool.
1248 kadm5_launch_task (krb5_context context,
1249 const char *task_path, char * const task_argv[],
1255 ret = pipe (data_pipe);
1260 pid_t pid = fork ();
1263 close (data_pipe[0]);
1264 close (data_pipe[1]);
1265 } else if (pid == 0) {
1268 if (dup2 (data_pipe[0], STDIN_FILENO) == -1)
1271 close (data_pipe[0]);
1272 close (data_pipe[1]);
1274 execv (task_path, task_argv);
1276 _exit (1); /* Fail if execv fails */
1283 close (data_pipe[0]);
1285 /* Write out the buffer to the child, add \n */
1287 if (krb5_net_write (context, data_pipe[1], buffer, strlen (buffer)) < 0
1288 || krb5_net_write (context, data_pipe[1], "\n", 1) < 0)
1290 /* kill the child to make sure waitpid() won't hang later */
1292 kill (pid, SIGKILL);
1295 close (data_pipe[1]);
1297 waitpid (pid, &status, 0);
1300 if (WIFEXITED (status)) {
1301 /* child read password and exited. Check the return value. */
1302 if ((WEXITSTATUS (status) != 0) && (WEXITSTATUS (status) != 252)) {
1303 ret = KRB5KDC_ERR_POLICY; /* password change rejected */
1306 /* child read password but crashed or was killed */
1307 ret = KRB5KRB_ERR_GENERIC; /* FIXME: better error */
1319 kadm5_chpass_principal(void *server_handle,
1320 krb5_principal principal, char *password)
1323 kadm5_chpass_principal_3(server_handle, principal, FALSE,
1328 kadm5_chpass_principal_3(void *server_handle,
1329 krb5_principal principal, krb5_boolean keepold,
1330 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,
1334 kadm5_policy_ent_rec pol;
1335 osa_princ_ent_rec adb;
1337 int ret, ret2, last_pwd, hist_added;
1339 kadm5_server_handle_t handle = server_handle;
1340 osa_pw_hist_ent hist;
1341 krb5_keyblock *act_mkey, hist_keyblock;
1342 krb5_kvno act_kvno, hist_kvno;
1344 CHECK_HANDLE(server_handle);
1346 krb5_clear_error_message(handle->context);
1349 memset(&hist, 0, sizeof(hist));
1350 memset(&hist_keyblock, 0, sizeof(hist_keyblock));
1352 if (principal == NULL || password == NULL)
1354 if ((krb5_principal_compare(handle->context,
1355 principal, hist_princ)) == TRUE)
1356 return KADM5_PROTECT_PRINCIPAL;
1358 /* Use default keysalts if caller did not provide any. */
1359 if (n_ks_tuple == 0) {
1360 ks_tuple = handle->params.keysalts;
1361 n_ks_tuple = handle->params.num_keysalts;
1364 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
1367 if ((adb.aux_attributes & KADM5_POLICY)) {
1368 if ((ret = kadm5_get_policy(handle->lhandle, adb.policy, &pol)))
1372 /* Create a password history entry before we change kdb's key_data. */
1373 ret = kdb_get_hist_key(handle, &hist_keyblock, &hist_kvno);
1376 ret = create_history_entry(handle->context, &hist_keyblock,
1377 kdb->n_key_data, kdb->key_data, &hist);
1382 if ((ret = passwd_check(handle, password, have_pol ? &pol : NULL,
1386 ret = krb5_dbe_find_act_mkey(handle->context, active_mkey_list, &act_kvno,
1391 ret = krb5_dbe_cpw(handle->context, act_mkey, ks_tuple, n_ks_tuple,
1392 password, 0 /* increment kvno */,
1397 ret = krb5_dbe_update_mkvno(handle->context, kdb, act_kvno);
1401 kdb->attributes &= ~KRB5_KDB_REQUIRES_PWCHANGE;
1403 ret = krb5_timeofday(handle->context, &now);
1407 if ((adb.aux_attributes & KADM5_POLICY)) {
1408 /* the policy was loaded before */
1410 ret = krb5_dbe_lookup_last_pwd_change(handle->context, kdb, &last_pwd);
1416 * The spec says this check is overridden if the caller has
1417 * modify privilege. The admin server therefore makes this
1418 * check itself (in chpass_principal_wrapper, misc.c). A
1419 * local caller implicitly has all authorization bits.
1421 if ((now - last_pwd) < pol.pw_min_life &&
1422 !(kdb->attributes & KRB5_KDB_REQUIRES_PWCHANGE)) {
1423 ret = KADM5_PASS_TOOSOON;
1428 ret = check_pw_reuse(handle->context, &hist_keyblock,
1429 kdb->n_key_data, kdb->key_data,
1434 if (pol.pw_history_num > 1) {
1435 /* If hist_kvno has changed since the last password change, we
1436 * can't check the history. */
1437 if (adb.admin_history_kvno == hist_kvno) {
1438 ret = check_pw_reuse(handle->context, &hist_keyblock,
1439 kdb->n_key_data, kdb->key_data,
1440 adb.old_key_len, adb.old_keys);
1445 ret = add_to_history(handle->context, hist_kvno, &adb, &pol,
1452 if (pol.pw_max_life)
1453 kdb->pw_expiration = now + pol.pw_max_life;
1455 kdb->pw_expiration = 0;
1457 kdb->pw_expiration = 0;
1460 #ifdef USE_PASSWORD_SERVER
1461 if (kadm5_use_password_server () &&
1462 (krb5_princ_size (handle->context, principal) == 1)) {
1463 krb5_data *princ = krb5_princ_component (handle->context, principal, 0);
1464 const char *path = "/usr/sbin/mkpassdb";
1465 char *argv[] = { "mkpassdb", "-setpassword", NULL, NULL };
1466 char *pstring = NULL;
1469 pstring = malloc ((princ->length + 1) * sizeof (char));
1470 if (pstring == NULL) { ret = ENOMEM; }
1474 memcpy (pstring, princ->data, princ->length);
1475 pstring [princ->length] = '\0';
1478 ret = kadm5_launch_task (handle->context, path, argv, password);
1481 if (pstring != NULL)
1489 ret = krb5_dbe_update_last_pwd_change(handle->context, kdb, now);
1493 /* unlock principal on this KDC */
1494 kdb->fail_auth_count = 0;
1496 /* key data and attributes changed, let the database provider know */
1497 kdb->mask = KADM5_KEY_DATA | KADM5_ATTRIBUTES |
1498 KADM5_FAIL_AUTH_COUNT;
1499 /* | KADM5_CPW_FUNCTION */
1501 ret = k5_kadm5_hook_chpass(handle->context, handle->hook_handles,
1502 KADM5_HOOK_STAGE_PRECOMMIT, principal, keepold,
1503 n_ks_tuple, ks_tuple, password);
1507 if ((ret = kdb_put_entry(handle, kdb, &adb)))
1510 (void) k5_kadm5_hook_chpass(handle->context, handle->hook_handles,
1511 KADM5_HOOK_STAGE_POSTCOMMIT, principal,
1512 keepold, n_ks_tuple, ks_tuple, password);
1515 if (!hist_added && hist.key_data)
1516 free_history_entry(handle->context, &hist);
1517 kdb_free_entry(handle, kdb, &adb);
1518 krb5_free_keyblock_contents(handle->context, &hist_keyblock);
1520 if (have_pol && (ret2 = kadm5_free_policy_ent(handle->lhandle, &pol))
1528 kadm5_randkey_principal(void *server_handle,
1529 krb5_principal principal,
1530 krb5_keyblock **keyblocks,
1534 kadm5_randkey_principal_3(server_handle, principal,
1539 kadm5_randkey_principal_3(void *server_handle,
1540 krb5_principal principal,
1541 krb5_boolean keepold,
1542 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,
1543 krb5_keyblock **keyblocks,
1547 osa_princ_ent_rec adb;
1549 kadm5_policy_ent_rec pol;
1550 int ret, last_pwd, have_pol = 0;
1551 kadm5_server_handle_t handle = server_handle;
1552 krb5_keyblock *act_mkey;
1557 CHECK_HANDLE(server_handle);
1559 /* Use default keysalts if caller did not provide any. */
1560 if (n_ks_tuple == 0) {
1561 ks_tuple = handle->params.keysalts;
1562 n_ks_tuple = handle->params.num_keysalts;
1565 krb5_clear_error_message(handle->context);
1567 if (principal == NULL)
1569 if (krb5_principal_compare(handle->context, principal, hist_princ)) {
1570 /* If changing the history entry, the new entry must have exactly one
1573 return KADM5_PROTECT_PRINCIPAL;
1577 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
1580 ret = krb5_dbe_find_act_mkey(handle->context, active_mkey_list, NULL,
1585 ret = krb5_dbe_crk(handle->context, act_mkey, ks_tuple, n_ks_tuple,
1590 kdb->attributes &= ~KRB5_KDB_REQUIRES_PWCHANGE;
1592 ret = krb5_timeofday(handle->context, &now);
1596 if ((adb.aux_attributes & KADM5_POLICY)) {
1597 if ((ret = kadm5_get_policy(handle->lhandle, adb.policy,
1602 ret = krb5_dbe_lookup_last_pwd_change(handle->context, kdb, &last_pwd);
1608 * The spec says this check is overridden if the caller has
1609 * modify privilege. The admin server therefore makes this
1610 * check itself (in chpass_principal_wrapper, misc.c). A
1611 * local caller implicitly has all authorization bits.
1613 if((now - last_pwd) < pol.pw_min_life &&
1614 !(kdb->attributes & KRB5_KDB_REQUIRES_PWCHANGE)) {
1615 ret = KADM5_PASS_TOOSOON;
1620 if (pol.pw_max_life)
1621 kdb->pw_expiration = now + pol.pw_max_life;
1623 kdb->pw_expiration = 0;
1625 kdb->pw_expiration = 0;
1628 ret = krb5_dbe_update_last_pwd_change(handle->context, kdb, now);
1632 /* unlock principal on this KDC */
1633 kdb->fail_auth_count = 0;
1636 ret = decrypt_key_data(handle->context,
1637 kdb->n_key_data, kdb->key_data,
1643 /* key data changed, let the database provider know */
1644 kdb->mask = KADM5_KEY_DATA | KADM5_FAIL_AUTH_COUNT;
1645 /* | KADM5_RANDKEY_USED */;
1647 ret = k5_kadm5_hook_chpass(handle->context, handle->hook_handles,
1648 KADM5_HOOK_STAGE_PRECOMMIT, principal, keepold,
1649 n_ks_tuple, ks_tuple, NULL);
1652 if ((ret = kdb_put_entry(handle, kdb, &adb)))
1655 (void) k5_kadm5_hook_chpass(handle->context, handle->hook_handles,
1656 KADM5_HOOK_STAGE_POSTCOMMIT, principal,
1657 keepold, n_ks_tuple, ks_tuple, NULL);
1660 kdb_free_entry(handle, kdb, &adb);
1662 kadm5_free_policy_ent(handle->lhandle, &pol);
1668 * kadm5_setv4key_principal:
1670 * Set only ONE key of the principal, removing all others. This key
1671 * must have the DES_CBC_CRC enctype and is entered as having the
1672 * krb4 salttype. This is to enable things like kadmind4 to work.
1675 kadm5_setv4key_principal(void *server_handle,
1676 krb5_principal principal,
1677 krb5_keyblock *keyblock)
1680 osa_princ_ent_rec adb;
1682 kadm5_policy_ent_rec pol;
1683 krb5_keysalt keysalt;
1684 int i, k, kvno, ret, have_pol = 0;
1688 kadm5_server_handle_t handle = server_handle;
1689 krb5_key_data tmp_key_data;
1690 krb5_keyblock *act_mkey;
1692 memset( &tmp_key_data, 0, sizeof(tmp_key_data));
1694 CHECK_HANDLE(server_handle);
1696 krb5_clear_error_message(handle->context);
1698 if (principal == NULL || keyblock == NULL)
1700 if (hist_princ && /* this will be NULL when initializing the databse */
1701 ((krb5_principal_compare(handle->context,
1702 principal, hist_princ)) == TRUE))
1703 return KADM5_PROTECT_PRINCIPAL;
1705 if (keyblock->enctype != ENCTYPE_DES_CBC_CRC)
1706 return KADM5_SETV4KEY_INVAL_ENCTYPE;
1708 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
1711 for (kvno = 0, i=0; i<kdb->n_key_data; i++)
1712 if (kdb->key_data[i].key_data_kvno > kvno)
1713 kvno = kdb->key_data[i].key_data_kvno;
1715 if (kdb->key_data != NULL)
1716 cleanup_key_data(handle->context, kdb->n_key_data, kdb->key_data);
1718 kdb->key_data = (krb5_key_data*)krb5_db_alloc(handle->context, NULL, sizeof(krb5_key_data));
1719 if (kdb->key_data == NULL)
1721 memset(kdb->key_data, 0, sizeof(krb5_key_data));
1722 kdb->n_key_data = 1;
1723 keysalt.type = KRB5_KDB_SALTTYPE_V4;
1724 /* XXX data.magic? */
1725 keysalt.data.length = 0;
1726 keysalt.data.data = NULL;
1728 ret = krb5_dbe_find_act_mkey(handle->context, active_mkey_list, NULL,
1733 /* use tmp_key_data as temporary location and reallocate later */
1734 ret = krb5_dbe_encrypt_key_data(handle->context, act_mkey, keyblock,
1735 &keysalt, kvno + 1, &tmp_key_data);
1740 for (k = 0; k < tmp_key_data.key_data_ver; k++) {
1741 kdb->key_data->key_data_type[k] = tmp_key_data.key_data_type[k];
1742 kdb->key_data->key_data_length[k] = tmp_key_data.key_data_length[k];
1743 if (tmp_key_data.key_data_contents[k]) {
1744 kdb->key_data->key_data_contents[k] = krb5_db_alloc(handle->context, NULL, tmp_key_data.key_data_length[k]);
1745 if (kdb->key_data->key_data_contents[k] == NULL) {
1746 cleanup_key_data(handle->context, kdb->n_key_data, kdb->key_data);
1747 kdb->key_data = NULL;
1748 kdb->n_key_data = 0;
1752 memcpy (kdb->key_data->key_data_contents[k], tmp_key_data.key_data_contents[k], tmp_key_data.key_data_length[k]);
1754 memset (tmp_key_data.key_data_contents[k], 0, tmp_key_data.key_data_length[k]);
1755 free (tmp_key_data.key_data_contents[k]);
1756 tmp_key_data.key_data_contents[k] = NULL;
1762 kdb->attributes &= ~KRB5_KDB_REQUIRES_PWCHANGE;
1764 ret = krb5_timeofday(handle->context, &now);
1768 if ((adb.aux_attributes & KADM5_POLICY)) {
1769 if ((ret = kadm5_get_policy(handle->lhandle, adb.policy,
1776 * The spec says this check is overridden if the caller has
1777 * modify privilege. The admin server therefore makes this
1778 * check itself (in chpass_principal_wrapper, misc.c). A
1779 * local caller implicitly has all authorization bits.
1781 if (ret = krb5_dbe_lookup_last_pwd_change(handle->context,
1784 if((now - last_pwd) < pol.pw_min_life &&
1785 !(kdb->attributes & KRB5_KDB_REQUIRES_PWCHANGE)) {
1786 ret = KADM5_PASS_TOOSOON;
1791 if (pol.pw_max_life)
1792 kdb->pw_expiration = now + pol.pw_max_life;
1794 kdb->pw_expiration = 0;
1796 kdb->pw_expiration = 0;
1799 ret = krb5_dbe_update_last_pwd_change(handle->context, kdb, now);
1803 /* unlock principal on this KDC */
1804 kdb->fail_auth_count = 0;
1806 if ((ret = kdb_put_entry(handle, kdb, &adb)))
1811 for (i = 0; i < tmp_key_data.key_data_ver; i++) {
1812 if (tmp_key_data.key_data_contents[i]) {
1813 memset (tmp_key_data.key_data_contents[i], 0, tmp_key_data.key_data_length[i]);
1814 free (tmp_key_data.key_data_contents[i]);
1818 kdb_free_entry(handle, kdb, &adb);
1820 kadm5_free_policy_ent(handle->lhandle, &pol);
1826 kadm5_setkey_principal(void *server_handle,
1827 krb5_principal principal,
1828 krb5_keyblock *keyblocks,
1832 kadm5_setkey_principal_3(server_handle, principal,
1838 kadm5_setkey_principal_3(void *server_handle,
1839 krb5_principal principal,
1840 krb5_boolean keepold,
1841 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,
1842 krb5_keyblock *keyblocks,
1846 osa_princ_ent_rec adb;
1848 kadm5_policy_ent_rec pol;
1849 krb5_key_data *old_key_data;
1851 int i, j, k, kvno, ret, have_pol = 0;
1855 kadm5_server_handle_t handle = server_handle;
1856 krb5_boolean similar;
1857 krb5_keysalt keysalt;
1858 krb5_key_data tmp_key_data;
1859 krb5_key_data *tptr;
1860 krb5_keyblock *act_mkey;
1862 CHECK_HANDLE(server_handle);
1864 krb5_clear_error_message(handle->context);
1866 if (principal == NULL || keyblocks == NULL)
1868 if (hist_princ && /* this will be NULL when initializing the databse */
1869 ((krb5_principal_compare(handle->context,
1870 principal, hist_princ)) == TRUE))
1871 return KADM5_PROTECT_PRINCIPAL;
1873 for (i = 0; i < n_keys; i++) {
1874 for (j = i+1; j < n_keys; j++) {
1875 if ((ret = krb5_c_enctype_compare(handle->context,
1876 keyblocks[i].enctype,
1877 keyblocks[j].enctype,
1882 if (ks_tuple[i].ks_salttype == ks_tuple[j].ks_salttype)
1883 return KADM5_SETKEY_DUP_ENCTYPES;
1885 return KADM5_SETKEY_DUP_ENCTYPES;
1890 if (n_ks_tuple && n_ks_tuple != n_keys)
1891 return KADM5_SETKEY3_ETYPE_MISMATCH;
1893 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
1896 for (kvno = 0, i=0; i<kdb->n_key_data; i++)
1897 if (kdb->key_data[i].key_data_kvno > kvno)
1898 kvno = kdb->key_data[i].key_data_kvno;
1901 old_key_data = kdb->key_data;
1902 n_old_keys = kdb->n_key_data;
1904 if (kdb->key_data != NULL)
1905 cleanup_key_data(handle->context, kdb->n_key_data, kdb->key_data);
1907 old_key_data = NULL;
1910 kdb->key_data = (krb5_key_data*)krb5_db_alloc(handle->context, NULL, (n_keys+n_old_keys)
1911 *sizeof(krb5_key_data));
1912 if (kdb->key_data == NULL) {
1917 memset(kdb->key_data, 0, (n_keys+n_old_keys)*sizeof(krb5_key_data));
1918 kdb->n_key_data = 0;
1920 for (i = 0; i < n_keys; i++) {
1922 keysalt.type = ks_tuple[i].ks_salttype;
1923 keysalt.data.length = 0;
1924 keysalt.data.data = NULL;
1925 if (ks_tuple[i].ks_enctype != keyblocks[i].enctype) {
1926 ret = KADM5_SETKEY3_ETYPE_MISMATCH;
1930 memset (&tmp_key_data, 0, sizeof(tmp_key_data));
1932 ret = krb5_dbe_find_act_mkey(handle->context, active_mkey_list, NULL,
1937 ret = krb5_dbe_encrypt_key_data(handle->context, act_mkey,
1939 n_ks_tuple ? &keysalt : NULL, kvno + 1,
1944 tptr = &kdb->key_data[i];
1945 tptr->key_data_ver = tmp_key_data.key_data_ver;
1946 tptr->key_data_kvno = tmp_key_data.key_data_kvno;
1947 for (k = 0; k < tmp_key_data.key_data_ver; k++) {
1948 tptr->key_data_type[k] = tmp_key_data.key_data_type[k];
1949 tptr->key_data_length[k] = tmp_key_data.key_data_length[k];
1950 if (tmp_key_data.key_data_contents[k]) {
1951 tptr->key_data_contents[k] = krb5_db_alloc(handle->context, NULL, tmp_key_data.key_data_length[k]);
1952 if (tptr->key_data_contents[k] == NULL) {
1954 for (i1 = k; i1 < tmp_key_data.key_data_ver; i1++) {
1955 if (tmp_key_data.key_data_contents[i1]) {
1956 memset (tmp_key_data.key_data_contents[i1], 0, tmp_key_data.key_data_length[i1]);
1957 free (tmp_key_data.key_data_contents[i1]);
1964 memcpy (tptr->key_data_contents[k], tmp_key_data.key_data_contents[k], tmp_key_data.key_data_length[k]);
1966 memset (tmp_key_data.key_data_contents[k], 0, tmp_key_data.key_data_length[k]);
1967 free (tmp_key_data.key_data_contents[k]);
1968 tmp_key_data.key_data_contents[k] = NULL;
1974 /* copy old key data if necessary */
1975 for (i = 0; i < n_old_keys; i++) {
1976 kdb->key_data[i+n_keys] = old_key_data[i];
1977 memset(&old_key_data[i], 0, sizeof (krb5_key_data));
1982 krb5_db_free(handle->context, old_key_data);
1984 /* assert(kdb->n_key_data == n_keys + n_old_keys) */
1985 kdb->attributes &= ~KRB5_KDB_REQUIRES_PWCHANGE;
1987 if ((ret = krb5_timeofday(handle->context, &now)))
1990 if ((adb.aux_attributes & KADM5_POLICY)) {
1991 if ((ret = kadm5_get_policy(handle->lhandle, adb.policy,
1998 * The spec says this check is overridden if the caller has
1999 * modify privilege. The admin server therefore makes this
2000 * check itself (in chpass_principal_wrapper, misc.c). A
2001 * local caller implicitly has all authorization bits.
2003 if (ret = krb5_dbe_lookup_last_pwd_change(handle->context,
2006 if((now - last_pwd) < pol.pw_min_life &&
2007 !(kdb->attributes & KRB5_KDB_REQUIRES_PWCHANGE)) {
2008 ret = KADM5_PASS_TOOSOON;
2013 if (pol.pw_max_life)
2014 kdb->pw_expiration = now + pol.pw_max_life;
2016 kdb->pw_expiration = 0;
2018 kdb->pw_expiration = 0;
2021 if ((ret = krb5_dbe_update_last_pwd_change(handle->context, kdb, now)))
2024 /* unlock principal on this KDC */
2025 kdb->fail_auth_count = 0;
2027 if ((ret = kdb_put_entry(handle, kdb, &adb)))
2032 kdb_free_entry(handle, kdb, &adb);
2034 kadm5_free_policy_ent(handle->lhandle, &pol);
2040 * Return the list of keys like kadm5_randkey_principal,
2041 * but don't modify the principal.
2044 kadm5_get_principal_keys(void *server_handle /* IN */,
2045 krb5_principal principal /* IN */,
2046 krb5_keyblock **keyblocks /* OUT */,
2047 int *n_keys /* OUT */)
2050 osa_princ_ent_rec adb;
2052 kadm5_server_handle_t handle = server_handle;
2057 CHECK_HANDLE(server_handle);
2059 if (principal == NULL)
2062 if ((ret = kdb_get_entry(handle, principal, &kdb, &adb)))
2066 ret = decrypt_key_data(handle->context,
2067 kdb->n_key_data, kdb->key_data,
2075 kdb_free_entry(handle, kdb, &adb);
2082 * Allocate an array of n_key_data krb5_keyblocks, fill in each
2083 * element with the results of decrypting the nth key in key_data,
2084 * and if n_keys is not NULL fill it in with the
2085 * number of keys decrypted.
2087 static int decrypt_key_data(krb5_context context,
2088 int n_key_data, krb5_key_data *key_data,
2089 krb5_keyblock **keyblocks, int *n_keys)
2091 krb5_keyblock *keys;
2094 keys = (krb5_keyblock *) malloc(n_key_data*sizeof(krb5_keyblock));
2097 memset(keys, 0, n_key_data*sizeof(krb5_keyblock));
2099 for (i = 0; i < n_key_data; i++) {
2100 ret = krb5_dbe_decrypt_key_data(context, NULL, &key_data[i], &keys[i],
2103 for (; i >= 0; i--) {
2104 if (keys[i].contents) {
2105 memset (keys[i].contents, 0, keys[i].length);
2106 free( keys[i].contents );
2110 memset(keys, 0, n_key_data*sizeof(krb5_keyblock));
2118 *n_keys = n_key_data;
2124 * Function: kadm5_decrypt_key
2126 * Purpose: Retrieves and decrypts a principal key.
2130 * server_handle (r) kadm5 handle
2131 * entry (r) principal retrieved with kadm5_get_principal
2132 * ktype (r) enctype to search for, or -1 to ignore
2133 * stype (r) salt type to search for, or -1 to ignore
2134 * kvno (r) kvno to search for, -1 for max, 0 for max
2135 * only if it also matches ktype and stype
2136 * keyblock (w) keyblock to fill in
2137 * keysalt (w) keysalt to fill in, or NULL
2138 * kvnop (w) kvno to fill in, or NULL
2140 * Effects: Searches the key_data array of entry, which must have been
2141 * retrived with kadm5_get_principal with the KADM5_KEY_DATA mask, to
2142 * find a key with a specified enctype, salt type, and kvno in a
2143 * principal entry. If not found, return ENOENT. Otherwise, decrypt
2144 * it with the master key, and return the key in keyblock, the salt
2145 * in salttype, and the key version number in kvno.
2147 * If ktype or stype is -1, it is ignored for the search. If kvno is
2148 * -1, ktype and stype are ignored and the key with the max kvno is
2149 * returned. If kvno is 0, only the key with the max kvno is returned
2150 * and only if it matches the ktype and stype; otherwise, ENOENT is
2153 kadm5_ret_t kadm5_decrypt_key(void *server_handle,
2154 kadm5_principal_ent_t entry, krb5_int32
2155 ktype, krb5_int32 stype, krb5_int32
2156 kvno, krb5_keyblock *keyblock,
2157 krb5_keysalt *keysalt, int *kvnop)
2159 kadm5_server_handle_t handle = server_handle;
2160 krb5_db_entry dbent;
2161 krb5_key_data *key_data;
2162 krb5_keyblock *mkey_ptr;
2165 CHECK_HANDLE(server_handle);
2167 if (entry->n_key_data == 0 || entry->key_data == NULL)
2170 /* find_enctype only uses these two fields */
2171 dbent.n_key_data = entry->n_key_data;
2172 dbent.key_data = entry->key_data;
2173 if ((ret = krb5_dbe_find_enctype(handle->context, &dbent, ktype,
2174 stype, kvno, &key_data)))
2177 /* find_mkey only uses this field */
2178 dbent.tl_data = entry->tl_data;
2179 if ((ret = krb5_dbe_find_mkey(handle->context, &dbent, &mkey_ptr))) {
2180 /* try refreshing master key list */
2181 /* XXX it would nice if we had the mkvno here for optimization */
2182 if (krb5_db_fetch_mkey_list(handle->context, master_princ,
2183 &master_keyblock) == 0) {
2184 if ((ret = krb5_dbe_find_mkey(handle->context, &dbent,
2193 if ((ret = krb5_dbe_decrypt_key_data(handle->context, NULL, key_data,
2194 keyblock, keysalt)))
2198 * Coerce the enctype of the output keyblock in case we got an
2199 * inexact match on the enctype; this behavior will go away when
2200 * the key storage architecture gets redesigned for 1.3.
2203 keyblock->enctype = ktype;
2206 *kvnop = key_data->key_data_kvno;
2212 kadm5_purgekeys(void *server_handle,
2213 krb5_principal principal,
2216 kadm5_server_handle_t handle = server_handle;
2219 osa_princ_ent_rec adb;
2220 krb5_key_data *old_keydata;
2224 CHECK_HANDLE(server_handle);
2226 if (principal == NULL)
2229 ret = kdb_get_entry(handle, principal, &kdb, &adb);
2233 if (keepkvno <= 0) {
2234 keepkvno = krb5_db_get_key_data_kvno(handle->context, kdb->n_key_data,
2238 old_keydata = kdb->key_data;
2239 n_old_keydata = kdb->n_key_data;
2240 kdb->n_key_data = 0;
2241 kdb->key_data = krb5_db_alloc(handle->context, NULL,
2242 n_old_keydata * sizeof(krb5_key_data));
2243 if (kdb->key_data == NULL) {
2247 memset(kdb->key_data, 0, n_old_keydata * sizeof(krb5_key_data));
2248 for (i = 0, j = 0; i < n_old_keydata; i++) {
2249 if (old_keydata[i].key_data_kvno < keepkvno)
2252 /* Alias the key_data_contents pointers; we null them out in the
2253 * source array immediately after. */
2254 kdb->key_data[j] = old_keydata[i];
2255 for (k = 0; k < old_keydata[i].key_data_ver; k++) {
2256 old_keydata[i].key_data_contents[k] = NULL;
2260 kdb->n_key_data = j;
2261 cleanup_key_data(handle->context, n_old_keydata, old_keydata);
2263 kdb->mask = KADM5_KEY_DATA;
2264 ret = kdb_put_entry(handle, kdb, &adb);
2269 kdb_free_entry(handle, kdb, &adb);
2274 kadm5_get_strings(void *server_handle, krb5_principal principal,
2275 krb5_string_attr **strings_out, int *count_out)
2277 kadm5_server_handle_t handle = server_handle;
2279 krb5_db_entry *kdb = NULL;
2281 *strings_out = NULL;
2283 CHECK_HANDLE(server_handle);
2284 if (principal == NULL)
2287 ret = kdb_get_entry(handle, principal, &kdb, NULL);
2291 ret = krb5_dbe_get_strings(handle->context, kdb, strings_out, count_out);
2292 kdb_free_entry(handle, kdb, NULL);
2297 kadm5_set_string(void *server_handle, krb5_principal principal,
2298 const char *key, const char *value)
2300 kadm5_server_handle_t handle = server_handle;
2303 osa_princ_ent_rec adb;
2305 CHECK_HANDLE(server_handle);
2306 if (principal == NULL || key == NULL)
2309 ret = kdb_get_entry(handle, principal, &kdb, &adb);
2313 ret = krb5_dbe_set_string(handle->context, kdb, key, value);
2317 kdb->mask = KADM5_TL_DATA;
2318 ret = kdb_put_entry(handle, kdb, &adb);
2321 kdb_free_entry(handle, kdb, &adb);