2 * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "kadm5_locl.h"
39 * Set the keys of `ent' to the string-to-key of `password'
43 _kadm5_set_keys(kadm5_server_context
*context
,
51 ret
= hdb_generate_key_set_password(context
->context
,
53 password
, &keys
, &num_keys
);
57 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
59 ent
->keys
.len
= num_keys
;
61 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
63 if (krb5_config_get_bool_default(context
->context
, NULL
, FALSE
,
64 "kadmin", "save-password", NULL
))
66 ret
= hdb_entry_set_password(context
->context
, context
->db
,
76 * Set the keys of `ent' to (`n_key_data', `key_data')
80 _kadm5_set_keys2(kadm5_server_context
*context
,
83 krb5_key_data
*key_data
)
91 keys
= malloc (len
* sizeof(*keys
));
95 _kadm5_init_keys (keys
, len
);
97 for(i
= 0; i
< n_key_data
; i
++) {
99 keys
[i
].key
.keytype
= key_data
[i
].key_data_type
[0];
100 ret
= krb5_data_copy(&keys
[i
].key
.keyvalue
,
101 key_data
[i
].key_data_contents
[0],
102 key_data
[i
].key_data_length
[0]);
105 if(key_data
[i
].key_data_ver
== 2) {
108 salt
= calloc(1, sizeof(*salt
));
114 salt
->type
= key_data
[i
].key_data_type
[1];
115 krb5_data_copy(&salt
->salt
,
116 key_data
[i
].key_data_contents
[1],
117 key_data
[i
].key_data_length
[1]);
121 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
123 ent
->keys
.val
= keys
;
125 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
126 hdb_entry_clear_password(context
->context
, ent
);
130 _kadm5_free_keys (context
->context
, len
, keys
);
135 * Set the keys of `ent' to `n_keys, keys'
139 _kadm5_set_keys3(kadm5_server_context
*context
,
142 krb5_keyblock
*keyblocks
)
150 keys
= malloc (len
* sizeof(*keys
));
154 _kadm5_init_keys (keys
, len
);
156 for(i
= 0; i
< n_keys
; i
++) {
157 keys
[i
].mkvno
= NULL
;
158 ret
= krb5_copy_keyblock_contents (context
->context
,
165 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
167 ent
->keys
.val
= keys
;
169 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
170 hdb_entry_clear_password(context
->context
, ent
);
174 _kadm5_free_keys (context
->context
, len
, keys
);
183 is_des_key_p(int keytype
)
185 return keytype
== ETYPE_DES_CBC_CRC
||
186 keytype
== ETYPE_DES_CBC_MD4
||
187 keytype
== ETYPE_DES_CBC_MD5
;
192 * Set the keys of `ent' to random keys and return them in `n_keys'
197 _kadm5_set_keys_randomly (kadm5_server_context
*context
,
199 krb5_keyblock
**new_keys
,
202 krb5_keyblock
*kblock
= NULL
;
208 ret
= hdb_generate_key_set(context
->context
, ent
->principal
,
209 &keys
, &num_keys
, 1);
213 kblock
= malloc(num_keys
* sizeof(kblock
[0]));
214 if (kblock
== NULL
) {
216 _kadm5_free_keys (context
->context
, num_keys
, keys
);
219 memset(kblock
, 0, num_keys
* sizeof(kblock
[0]));
222 for (i
= 0; i
< num_keys
; i
++) {
225 * To make sure all des keys are the the same we generate only
226 * the first one and then copy key to all other des keys.
229 if (des_keyblock
!= -1 && is_des_key_p(keys
[i
].key
.keytype
)) {
230 ret
= krb5_copy_keyblock_contents (context
->context
,
231 &kblock
[des_keyblock
],
235 kblock
[i
].keytype
= keys
[i
].key
.keytype
;
237 ret
= krb5_generate_random_keyblock (context
->context
,
243 if (is_des_key_p(keys
[i
].key
.keytype
))
247 ret
= krb5_copy_keyblock_contents (context
->context
,
256 for (i
= 0; i
< num_keys
; ++i
)
257 krb5_free_keyblock_contents (context
->context
, &kblock
[i
]);
259 _kadm5_free_keys (context
->context
, num_keys
, keys
);
263 _kadm5_free_keys (context
->context
, ent
->keys
.len
, ent
->keys
.val
);
264 ent
->keys
.val
= keys
;
265 ent
->keys
.len
= num_keys
;
269 hdb_entry_set_pw_change_time(context
->context
, ent
, 0);
270 hdb_entry_clear_password(context
->context
, ent
);