2 * Copyright (c) 2005, PADL Software Pty Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of PADL Software nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 __RCSID("$Heimdal: acquire.c 22118 2007-12-03 21:44:00Z lha $"
38 static krb5_error_code
39 change_pw_and_update_keytab(krb5_context context
, kcm_ccache ccache
);
42 * Get a new ticket using a keytab/cached key and swap it into
43 * an existing redentials cache
47 kcm_ccache_acquire(krb5_context context
,
51 krb5_error_code ret
= 0;
53 krb5_const_realm realm
;
54 krb5_get_init_creds_opt opt
;
55 krb5_ccache_data ccdata
;
56 char *in_tkt_service
= NULL
;
59 memset(&cred
, 0, sizeof(cred
));
61 KCM_ASSERT_VALID(ccache
);
63 /* We need a cached key or keytab to acquire credentials */
64 if (ccache
->flags
& KCM_FLAGS_USE_CACHED_KEY
) {
65 if (ccache
->key
.keyblock
.keyvalue
.length
== 0)
67 "kcm_ccache_acquire: KCM_FLAGS_USE_CACHED_KEY without key");
68 } else if (ccache
->flags
& KCM_FLAGS_USE_KEYTAB
) {
69 if (ccache
->key
.keytab
== NULL
)
71 "kcm_ccache_acquire: KCM_FLAGS_USE_KEYTAB without keytab");
73 kcm_log(0, "Cannot acquire initial credentials for cache %s without key",
75 return KRB5_FCC_INTERNAL
;
78 HEIMDAL_MUTEX_lock(&ccache
->mutex
);
80 /* Fake up an internal ccache */
81 kcm_internal_ccache(context
, ccache
, &ccdata
);
83 /* Now, actually acquire the creds */
84 if (ccache
->server
!= NULL
) {
85 ret
= krb5_unparse_name(context
, ccache
->server
, &in_tkt_service
);
87 kcm_log(0, "Failed to unparse service principal name for cache %s: %s",
88 ccache
->name
, krb5_get_err_text(context
, ret
));
93 realm
= krb5_principal_get_realm(context
, ccache
->client
);
95 krb5_get_init_creds_opt_init(&opt
);
96 krb5_get_init_creds_opt_set_default_flags(context
, "kcm", realm
, &opt
);
97 if (ccache
->tkt_life
!= 0)
98 krb5_get_init_creds_opt_set_tkt_life(&opt
, ccache
->tkt_life
);
99 if (ccache
->renew_life
!= 0)
100 krb5_get_init_creds_opt_set_renew_life(&opt
, ccache
->renew_life
);
102 if (ccache
->flags
& KCM_FLAGS_USE_CACHED_KEY
) {
103 ret
= krb5_get_init_creds_keyblock(context
,
106 &ccache
->key
.keyblock
,
111 /* loosely based on lib/krb5/init_creds_pw.c */
113 ret
= krb5_get_init_creds_keytab(context
,
121 case KRB5KDC_ERR_KEY_EXPIRED
:
122 if (in_tkt_service
!= NULL
&&
123 strcmp(in_tkt_service
, "kadmin/changepw") == 0) {
127 ret
= change_pw_and_update_keytab(context
, ccache
);
140 kcm_log(0, "Failed to acquire credentials for cache %s: %s",
141 ccache
->name
, krb5_get_err_text(context
, ret
));
142 if (in_tkt_service
!= NULL
)
143 free(in_tkt_service
);
147 if (in_tkt_service
!= NULL
)
148 free(in_tkt_service
);
151 kcm_ccache_remove_creds_internal(context
, ccache
);
153 ret
= kcm_ccache_store_cred_internal(context
, ccache
, &cred
, 0, credp
);
155 kcm_log(0, "Failed to store credentials for cache %s: %s",
156 ccache
->name
, krb5_get_err_text(context
, ret
));
157 krb5_free_cred_contents(context
, &cred
);
162 HEIMDAL_MUTEX_unlock(&ccache
->mutex
);
167 static krb5_error_code
168 change_pw(krb5_context context
,
176 krb5_data result_code_string
;
177 krb5_data result_string
;
178 krb5_get_init_creds_opt options
;
180 memset(&cpw_cred
, 0, sizeof(cpw_cred
));
182 krb5_get_init_creds_opt_init(&options
);
183 krb5_get_init_creds_opt_set_tkt_life(&options
, 60);
184 krb5_get_init_creds_opt_set_forwardable(&options
, FALSE
);
185 krb5_get_init_creds_opt_set_proxiable(&options
, FALSE
);
187 krb5_data_zero(&result_code_string
);
188 krb5_data_zero(&result_string
);
190 ret
= krb5_get_init_creds_keytab(context
,
198 kcm_log(0, "Failed to acquire password change credentials "
199 "for principal %s: %s",
200 cpn
, krb5_get_err_text(context
, ret
));
204 ret
= krb5_set_password(context
,
212 kcm_log(0, "Failed to change password for principal %s: %s",
213 cpn
, krb5_get_err_text(context
, ret
));
218 kcm_log(0, "Failed to change password for principal %s: %.*s",
220 (int)result_string
.length
,
221 result_string
.length
> 0 ? (char *)result_string
.data
: "");
226 krb5_data_free(&result_string
);
227 krb5_data_free(&result_code_string
);
228 krb5_free_cred_contents(context
, &cpw_cred
);
233 struct kcm_keyseed_data
{
235 const char *password
;
238 static krb5_error_code
239 kcm_password_key_proc(krb5_context context
,
242 krb5_const_pointer keyseed
,
246 struct kcm_keyseed_data
*s
= (struct kcm_keyseed_data
*)keyseed
;
248 /* we may be called multiple times */
249 krb5_free_salt(context
, s
->salt
);
250 krb5_data_zero(&s
->salt
.saltvalue
);
253 s
->salt
.salttype
= salt
.salttype
;
255 ret
= krb5_data_copy(&s
->salt
.saltvalue
,
257 salt
.saltvalue
.length
);
261 *key
= (krb5_keyblock
*)malloc(sizeof(**key
));
266 ret
= krb5_string_to_key_salt(context
, etype
, s
->password
,
276 static krb5_error_code
277 get_salt_and_kvno(krb5_context context
,
279 krb5_enctype
*etypes
,
287 krb5_ccache_data ccdata
;
288 krb5_flags options
= 0;
290 struct kcm_keyseed_data s
;
292 memset(&creds
, 0, sizeof(creds
));
293 memset(&reply
, 0, sizeof(reply
));
296 s
.salt
.salttype
= (int)ETYPE_NULL
;
297 krb5_data_zero(&s
.salt
.saltvalue
);
300 kcm_internal_ccache(context
, ccache
, &ccdata
);
303 /* Do an AS-REQ to determine salt and key version number */
304 ret
= krb5_copy_principal(context
, ccache
->client
, &creds
.client
);
308 /* Yes, get a ticket to ourselves */
309 ret
= krb5_copy_principal(context
, ccache
->client
, &creds
.server
);
311 krb5_free_principal(context
, creds
.client
);
315 ret
= krb5_get_in_tkt(context
,
320 kcm_password_key_proc
,
328 kcm_log(0, "Failed to get self ticket for principal %s: %s",
329 cpn
, krb5_get_err_text(context
, ret
));
330 krb5_free_salt(context
, s
.salt
);
332 *salt
= s
.salt
; /* retrieve stashed salt */
333 if (reply
.kdc_rep
.enc_part
.kvno
!= NULL
)
334 *kvno
= *(reply
.kdc_rep
.enc_part
.kvno
);
336 /* ccache may have been modified but it will get trashed anyway */
338 krb5_free_cred_contents(context
, &creds
);
339 krb5_free_kdc_rep(context
, &reply
);
344 static krb5_error_code
345 update_keytab_entry(krb5_context context
,
355 krb5_keytab_entry entry
;
358 memset(&entry
, 0, sizeof(entry
));
360 pw
.data
= (char *)newpw
;
361 pw
.length
= strlen(newpw
);
363 ret
= krb5_string_to_key_data_salt(context
, etype
, pw
,
364 salt
, &entry
.keyblock
);
366 kcm_log(0, "String to key conversion failed for principal %s "
368 cpn
, etype
, krb5_get_err_text(context
, ret
));
373 ret
= krb5_copy_principal(context
, ccache
->client
,
376 kcm_log(0, "Failed to copy principal name %s: %s",
377 cpn
, krb5_get_err_text(context
, ret
));
381 ret
= krb5_parse_name(context
, spn
, &entry
.principal
);
383 kcm_log(0, "Failed to parse SPN alias %s: %s",
384 spn
, krb5_get_err_text(context
, ret
));
390 entry
.timestamp
= time(NULL
);
392 ret
= krb5_kt_add_entry(context
, ccache
->key
.keytab
, &entry
);
394 kcm_log(0, "Failed to update keytab for principal %s "
396 cpn
, etype
, krb5_get_err_text(context
, ret
));
399 krb5_kt_free_entry(context
, &entry
);
404 static krb5_error_code
405 update_keytab_entries(krb5_context context
,
407 krb5_enctype
*etypes
,
414 krb5_error_code ret
= 0;
417 for (i
= 0; etypes
[i
] != ETYPE_NULL
; i
++) {
418 ret
= update_keytab_entry(context
, ccache
, etypes
[i
],
419 cpn
, spn
, newpw
, salt
, kvno
);
428 generate_random_pw(krb5_context context
,
432 unsigned char x
[512], *p
;
435 memset(x
, 0, sizeof(x
));
436 krb5_generate_random_block(x
, sizeof(x
));
439 for (i
= 0; i
< bufsiz
; i
++) {
440 while (isprint(*p
) == 0)
443 if (p
- x
>= sizeof(x
)) {
444 krb5_generate_random_block(x
, sizeof(x
));
449 buf
[bufsiz
- 1] = '\0';
450 memset(x
, 0, sizeof(x
));
453 static krb5_error_code
454 change_pw_and_update_keytab(krb5_context context
,
461 krb5_enctype
*etypes
= NULL
;
466 krb5_data_zero(&salt
.saltvalue
);
468 ret
= krb5_unparse_name(context
, ccache
->client
, &cpn
);
470 kcm_log(0, "Failed to unparse name: %s",
471 krb5_get_err_text(context
, ret
));
475 ret
= krb5_get_default_in_tkt_etypes(context
, &etypes
);
477 kcm_log(0, "Failed to determine default encryption types: %s",
478 krb5_get_err_text(context
, ret
));
482 /* Generate a random password (there is no set keys protocol) */
483 generate_random_pw(context
, newpw
, sizeof(newpw
));
486 ret
= change_pw(context
, ccache
, cpn
, newpw
);
490 /* Do an AS-REQ to determine salt and key version number */
491 ret
= get_salt_and_kvno(context
, ccache
, etypes
, cpn
, newpw
,
494 kcm_log(0, "Failed to determine salting principal for principal %s: %s",
495 cpn
, krb5_get_err_text(context
, ret
));
499 /* Add canonical name */
500 ret
= update_keytab_entries(context
, ccache
, etypes
, cpn
,
501 NULL
, newpw
, salt
, kvno
);
505 /* Add SPN aliases, if any */
506 spns
= krb5_config_get_strings(context
, NULL
, "kcm",
507 "system_ccache", "spn_aliases", NULL
);
509 for (i
= 0; spns
[i
] != NULL
; i
++) {
510 ret
= update_keytab_entries(context
, ccache
, etypes
, cpn
,
511 spns
[i
], newpw
, salt
, kvno
);
517 kcm_log(0, "Changed expired password for principal %s in cache %s",
524 krb5_config_free_strings(spns
);
527 krb5_free_salt(context
, salt
);
528 memset(newpw
, 0, sizeof(newpw
));