2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 typedef struct krb5_get_init_creds_ctx {
41 krb5_addresses *addrs;
43 krb5_preauthtype *pre_auth_types;
52 /* password and keytab_data is freed on completion */
54 krb5_keytab_key_proc_args *keytab_data;
56 krb5_pointer *keyseed;
57 krb5_s2k_proc keyproc;
59 krb5_get_init_creds_tristate req_pac;
61 krb5_pk_init_ctx pk_init_ctx;
66 #define USED_PKINIT_W2K 2
67 #define USED_ENC_TS_GUESS 4
68 #define USED_ENC_TS_INFO 8
73 EncKDCRepPart enc_part;
75 krb5_prompter_fct prompter;
78 struct pa_info_data *ppaid;
80 } krb5_get_init_creds_ctx;
90 free_paid(krb5_context context, struct pa_info_data *ppaid)
92 krb5_free_salt(context, ppaid->salt);
94 krb5_free_data(context, ppaid->s2kparams);
97 static krb5_error_code KRB5_CALLCONV
98 default_s2k_func(krb5_context context, krb5_enctype type,
99 krb5_const_pointer keyseed,
100 krb5_salt salt, krb5_data *s2kparms,
107 _krb5_debug(context, 5, "krb5_get_init_creds: using default_s2k_func");
109 password.data = rk_UNCONST(keyseed);
110 password.length = strlen(keyseed);
114 krb5_data_zero(&opaque);
116 *key = malloc(sizeof(**key));
119 ret = krb5_string_to_key_data_salt_opaque(context, type, password,
129 free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx)
133 if (ctx->pre_auth_types)
134 free (ctx->pre_auth_types);
135 if (ctx->in_tkt_service)
136 free(ctx->in_tkt_service);
137 if (ctx->keytab_data)
138 free(ctx->keytab_data);
140 memset(ctx->password, 0, strlen(ctx->password));
143 krb5_data_free(&ctx->req_buffer);
144 krb5_free_cred_contents(context, &ctx->cred);
145 free_METHOD_DATA(&ctx->md);
146 free_AS_REP(&ctx->as_rep);
147 free_EncKDCRepPart(&ctx->enc_part);
148 free_KRB_ERROR(&ctx->error);
149 free_AS_REQ(&ctx->as_req);
151 free_paid(context, ctx->ppaid);
154 memset(ctx, 0, sizeof(*ctx));
158 get_config_time (krb5_context context,
165 ret = krb5_config_get_time (context, NULL,
172 ret = krb5_config_get_time (context, NULL,
181 static krb5_error_code
182 init_cred (krb5_context context,
184 krb5_principal client,
185 krb5_deltat start_time,
186 krb5_get_init_creds_opt *options)
192 krb5_timeofday (context, &now);
194 memset (cred, 0, sizeof(*cred));
197 krb5_copy_principal(context, client, &cred->client);
199 ret = krb5_get_default_principal (context,
206 cred->times.starttime = now + start_time;
208 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
209 tmp = options->tkt_life;
212 cred->times.endtime = now + tmp;
214 if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE) &&
215 options->renew_life > 0) {
216 cred->times.renew_till = now + options->renew_life;
222 krb5_free_cred_contents (context, cred);
227 * Print a message (str) to the user about the expiration in `lr'
231 report_expiration (krb5_context context,
232 krb5_prompter_fct prompter,
239 if (asprintf(&p, "%s%s", str, ctime(&now)) < 0 || p == NULL)
241 (*prompter)(context, data, NULL, p, 0, NULL);
246 * Check the context, and in the case there is a expiration warning,
247 * use the prompter to print the warning.
249 * @param context A Kerberos 5 context.
250 * @param options An GIC options structure
251 * @param ctx The krb5_init_creds_context check for expiration.
254 static krb5_error_code
255 process_last_request(krb5_context context,
256 krb5_get_init_creds_opt *options,
257 krb5_init_creds_context ctx)
259 krb5_const_realm realm;
261 krb5_boolean reported = FALSE;
267 * First check if there is a API consumer.
270 realm = krb5_principal_get_realm (context, ctx->cred.client);
271 lr = &ctx->enc_part.last_req;
273 if (options && options->opt_private && options->opt_private->lr.func) {
274 krb5_last_req_entry **lre;
276 lre = calloc(lr->len + 1, sizeof(**lre));
278 krb5_set_error_message(context, ENOMEM,
279 N_("malloc: out of memory", ""));
282 for (i = 0; i < lr->len; i++) {
283 lre[i] = calloc(1, sizeof(*lre[i]));
286 lre[i]->lr_type = lr->val[i].lr_type;
287 lre[i]->value = lr->val[i].lr_value;
290 (*options->opt_private->lr.func)(context, lre,
291 options->opt_private->lr.ctx);
293 for (i = 0; i < lr->len; i++)
299 * Now check if we should prompt the user
302 if (ctx->prompter == NULL)
305 krb5_timeofday (context, &sec);
307 t = sec + get_config_time (context,
312 for (i = 0; i < lr->len; ++i) {
313 if (lr->val[i].lr_value <= t) {
314 switch (abs(lr->val[i].lr_type)) {
316 report_expiration(context, ctx->prompter,
318 "Your password will expire at ",
319 lr->val[i].lr_value);
322 case LR_ACCT_EXPTIME :
323 report_expiration(context, ctx->prompter,
325 "Your account will expire at ",
326 lr->val[i].lr_value);
334 && ctx->enc_part.key_expiration
335 && *ctx->enc_part.key_expiration <= t) {
336 report_expiration(context, ctx->prompter,
338 "Your password/account will expire at ",
339 *ctx->enc_part.key_expiration);
344 static krb5_addresses no_addrs = { 0, NULL };
346 static krb5_error_code
347 get_init_creds_common(krb5_context context,
348 krb5_principal client,
349 krb5_deltat start_time,
350 krb5_get_init_creds_opt *options,
351 krb5_init_creds_context ctx)
353 krb5_get_init_creds_opt *default_opt = NULL;
355 krb5_enctype *etypes;
356 krb5_preauthtype *pre_auth_types;
358 memset(ctx, 0, sizeof(*ctx));
360 if (options == NULL) {
361 const char *realm = krb5_principal_get_realm(context, client);
363 krb5_get_init_creds_opt_alloc (context, &default_opt);
364 options = default_opt;
365 krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options);
368 if (options->opt_private) {
369 if (options->opt_private->password) {
370 ret = krb5_init_creds_set_password(context, ctx,
371 options->opt_private->password);
376 ctx->keyproc = options->opt_private->key_proc;
377 ctx->req_pac = options->opt_private->req_pac;
378 ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
379 ctx->ic_flags = options->opt_private->flags;
381 ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;
383 if (ctx->keyproc == NULL)
384 ctx->keyproc = default_s2k_func;
386 /* Enterprise name implicitly turns on canonicalize */
387 if ((ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE) ||
388 krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL)
389 ctx->flags.canonicalize = 1;
391 ctx->pre_auth_types = NULL;
394 ctx->pre_auth_types = NULL;
396 ret = init_cred(context, &ctx->cred, client, start_time, options);
399 krb5_get_init_creds_opt_free(context, default_opt);
403 ret = krb5_init_creds_set_service(context, ctx, NULL);
407 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
408 ctx->flags.forwardable = options->forwardable;
410 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
411 ctx->flags.proxiable = options->proxiable;
414 ctx->flags.postdated = 1;
415 if (ctx->cred.times.renew_till)
416 ctx->flags.renewable = 1;
417 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
418 ctx->addrs = options->address_list;
419 } else if (options->opt_private) {
420 switch (options->opt_private->addressless) {
421 case KRB5_INIT_CREDS_TRISTATE_UNSET:
422 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
423 ctx->addrs = &no_addrs;
428 case KRB5_INIT_CREDS_TRISTATE_FALSE:
431 case KRB5_INIT_CREDS_TRISTATE_TRUE:
432 ctx->addrs = &no_addrs;
436 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
440 etypes = malloc((options->etype_list_length + 1)
441 * sizeof(krb5_enctype));
442 if (etypes == NULL) {
444 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
447 memcpy (etypes, options->etype_list,
448 options->etype_list_length * sizeof(krb5_enctype));
449 etypes[options->etype_list_length] = ETYPE_NULL;
450 ctx->etypes = etypes;
452 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
453 pre_auth_types = malloc((options->preauth_list_length + 1)
454 * sizeof(krb5_preauthtype));
455 if (pre_auth_types == NULL) {
457 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
460 memcpy (pre_auth_types, options->preauth_list,
461 options->preauth_list_length * sizeof(krb5_preauthtype));
462 pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
463 ctx->pre_auth_types = pre_auth_types;
465 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
466 ctx->flags.request_anonymous = options->anonymous;
468 krb5_get_init_creds_opt_free(context, default_opt);
472 krb5_get_init_creds_opt_free(context, default_opt);
476 static krb5_error_code
477 change_password (krb5_context context,
478 krb5_principal client,
479 const char *password,
482 krb5_prompter_fct prompter,
484 krb5_get_init_creds_opt *old_options)
486 krb5_prompt prompts[2];
489 char buf1[BUFSIZ], buf2[BUFSIZ];
490 krb5_data password_data[2];
492 krb5_data result_code_string;
493 krb5_data result_string;
495 krb5_get_init_creds_opt *options;
497 memset (&cpw_cred, 0, sizeof(cpw_cred));
499 ret = krb5_get_init_creds_opt_alloc(context, &options);
502 krb5_get_init_creds_opt_set_tkt_life (options, 60);
503 krb5_get_init_creds_opt_set_forwardable (options, FALSE);
504 krb5_get_init_creds_opt_set_proxiable (options, FALSE);
505 if (old_options && old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
506 krb5_get_init_creds_opt_set_preauth_list (options,
507 old_options->preauth_list,
508 old_options->preauth_list_length);
510 krb5_data_zero (&result_code_string);
511 krb5_data_zero (&result_string);
513 ret = krb5_get_init_creds_password (context,
522 krb5_get_init_creds_opt_free(context, options);
527 password_data[0].data = buf1;
528 password_data[0].length = sizeof(buf1);
530 prompts[0].hidden = 1;
531 prompts[0].prompt = "New password: ";
532 prompts[0].reply = &password_data[0];
533 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
535 password_data[1].data = buf2;
536 password_data[1].length = sizeof(buf2);
538 prompts[1].hidden = 1;
539 prompts[1].prompt = "Repeat new password: ";
540 prompts[1].reply = &password_data[1];
541 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
543 ret = (*prompter) (context, data, NULL, "Changing password",
546 memset (buf1, 0, sizeof(buf1));
547 memset (buf2, 0, sizeof(buf2));
551 if (strcmp (buf1, buf2) == 0)
553 memset (buf1, 0, sizeof(buf1));
554 memset (buf2, 0, sizeof(buf2));
557 ret = krb5_set_password (context,
566 if (asprintf(&p, "%s: %.*s\n",
567 result_code ? "Error" : "Success",
568 (int)result_string.length,
569 result_string.length > 0 ? (char*)result_string.data : "") < 0)
575 /* return the result */
576 (*prompter) (context, data, NULL, p, 0, NULL);
579 if (result_code == 0) {
580 strlcpy (newpw, buf1, newpw_sz);
584 krb5_set_error_message(context, ret,
585 N_("failed changing password", ""));
589 memset (buf1, 0, sizeof(buf1));
590 memset (buf2, 0, sizeof(buf2));
591 krb5_data_free (&result_string);
592 krb5_data_free (&result_code_string);
593 krb5_free_cred_contents (context, &cpw_cred);
598 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
599 krb5_keyblock_key_proc (krb5_context context,
602 krb5_const_pointer keyseed,
605 return krb5_copy_keyblock (context, keyseed, key);
612 static krb5_error_code
613 init_as_req (krb5_context context,
615 const krb5_creds *creds,
616 const krb5_addresses *addrs,
617 const krb5_enctype *etypes,
622 memset(a, 0, sizeof(*a));
625 a->msg_type = krb_as_req;
626 a->req_body.kdc_options = opts;
627 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
628 if (a->req_body.cname == NULL) {
630 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
633 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
634 if (a->req_body.sname == NULL) {
636 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
640 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
643 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
647 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
651 if(creds->times.starttime) {
652 a->req_body.from = malloc(sizeof(*a->req_body.from));
653 if (a->req_body.from == NULL) {
655 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
658 *a->req_body.from = creds->times.starttime;
660 if(creds->times.endtime){
661 ALLOC(a->req_body.till, 1);
662 *a->req_body.till = creds->times.endtime;
664 if(creds->times.renew_till){
665 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
666 if (a->req_body.rtime == NULL) {
668 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
671 *a->req_body.rtime = creds->times.renew_till;
673 a->req_body.nonce = 0;
674 ret = _krb5_init_etype(context,
676 &a->req_body.etype.len,
677 &a->req_body.etype.val,
683 * This means no addresses
686 if (addrs && addrs->len == 0) {
687 a->req_body.addresses = NULL;
689 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
690 if (a->req_body.addresses == NULL) {
692 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
697 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
699 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
700 if(ret == 0 && a->req_body.addresses->len == 0) {
701 free(a->req_body.addresses);
702 a->req_body.addresses = NULL;
709 a->req_body.enc_authorization_data = NULL;
710 a->req_body.additional_tickets = NULL;
717 memset(a, 0, sizeof(*a));
722 static krb5_error_code
723 set_paid(struct pa_info_data *paid, krb5_context context,
725 krb5_salttype salttype, void *salt_string, size_t salt_len,
726 krb5_data *s2kparams)
729 paid->salt.salttype = salttype;
730 paid->salt.saltvalue.data = malloc(salt_len + 1);
731 if (paid->salt.saltvalue.data == NULL) {
732 krb5_clear_error_message(context);
735 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
736 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
737 paid->salt.saltvalue.length = salt_len;
741 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
743 krb5_clear_error_message(context);
744 krb5_free_salt(context, paid->salt);
748 paid->s2kparams = NULL;
753 static struct pa_info_data *
754 pa_etype_info2(krb5_context context,
755 const krb5_principal client,
757 struct pa_info_data *paid,
758 heim_octet_string *data)
765 memset(&e, 0, sizeof(e));
766 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
771 for (j = 0; j < asreq->req_body.etype.len; j++) {
772 for (i = 0; i < e.len; i++) {
773 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
775 if (e.val[i].salt == NULL)
776 ret = krb5_get_pw_salt(context, client, &salt);
778 salt.saltvalue.data = *e.val[i].salt;
779 salt.saltvalue.length = strlen(*e.val[i].salt);
783 ret = set_paid(paid, context, e.val[i].etype,
786 salt.saltvalue.length,
788 if (e.val[i].salt == NULL)
789 krb5_free_salt(context, salt);
791 free_ETYPE_INFO2(&e);
798 free_ETYPE_INFO2(&e);
802 static struct pa_info_data *
803 pa_etype_info(krb5_context context,
804 const krb5_principal client,
806 struct pa_info_data *paid,
807 heim_octet_string *data)
814 memset(&e, 0, sizeof(e));
815 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
820 for (j = 0; j < asreq->req_body.etype.len; j++) {
821 for (i = 0; i < e.len; i++) {
822 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
824 salt.salttype = KRB5_PW_SALT;
825 if (e.val[i].salt == NULL)
826 ret = krb5_get_pw_salt(context, client, &salt);
828 salt.saltvalue = *e.val[i].salt;
831 if (e.val[i].salttype)
832 salt.salttype = *e.val[i].salttype;
834 ret = set_paid(paid, context, e.val[i].etype,
837 salt.saltvalue.length,
839 if (e.val[i].salt == NULL)
840 krb5_free_salt(context, salt);
854 static struct pa_info_data *
855 pa_pw_or_afs3_salt(krb5_context context,
856 const krb5_principal client,
858 struct pa_info_data *paid,
859 heim_octet_string *data)
862 if (paid->etype == ENCTYPE_NULL)
864 ret = set_paid(paid, context,
877 krb5_preauthtype type;
878 struct pa_info_data *(*salt_info)(krb5_context,
879 const krb5_principal,
881 struct pa_info_data *,
882 heim_octet_string *);
885 static struct pa_info pa_prefs[] = {
886 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
887 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
888 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
889 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
893 find_pa_data(const METHOD_DATA *md, unsigned type)
898 for (i = 0; i < md->len; i++)
899 if (md->val[i].padata_type == type)
904 static struct pa_info_data *
905 process_pa_info(krb5_context context,
906 const krb5_principal client,
908 struct pa_info_data *paid,
911 struct pa_info_data *p = NULL;
914 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
915 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
918 paid->salt.salttype = (krb5_salttype)pa_prefs[i].type;
919 p = (*pa_prefs[i].salt_info)(context, client, asreq,
920 paid, &pa->padata_value);
925 static krb5_error_code
926 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
927 krb5_enctype etype, krb5_keyblock *key)
933 EncryptedData encdata;
939 krb5_us_timeofday (context, &p.patimestamp, &usec);
943 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
947 krb5_abortx(context, "internal error in ASN.1 encoder");
949 ret = krb5_crypto_init(context, key, 0, &crypto);
954 ret = krb5_encrypt_EncryptedData(context,
956 KRB5_KU_PA_ENC_TIMESTAMP,
962 krb5_crypto_destroy(context, crypto);
966 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
967 free_EncryptedData(&encdata);
971 krb5_abortx(context, "internal error in ASN.1 encoder");
973 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
979 static krb5_error_code
980 add_enc_ts_padata(krb5_context context,
982 krb5_principal client,
983 krb5_s2k_proc keyproc,
984 krb5_const_pointer keyseed,
985 krb5_enctype *enctypes,
988 krb5_data *s2kparams)
996 /* default to standard salt */
997 ret = krb5_get_pw_salt (context, client, &salt2);
1003 enctypes = context->etypes;
1005 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
1009 for (i = 0; i < netypes; ++i) {
1012 _krb5_debug(context, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes[i]);
1014 ret = (*keyproc)(context, enctypes[i], keyseed,
1015 *salt, s2kparams, &key);
1018 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
1019 krb5_free_keyblock (context, key);
1024 krb5_free_salt(context, salt2);
1028 static krb5_error_code
1029 pa_data_to_md_ts_enc(krb5_context context,
1031 const krb5_principal client,
1032 krb5_get_init_creds_ctx *ctx,
1033 struct pa_info_data *ppaid,
1036 if (ctx->keyproc == NULL || ctx->keyseed == NULL)
1040 add_enc_ts_padata(context, md, client,
1041 ctx->keyproc, ctx->keyseed,
1043 &ppaid->salt, ppaid->s2kparams);
1047 _krb5_debug(context, 5, "krb5_get_init_creds: pa-info not found, guessing salt");
1049 /* make a v5 salted pa-data */
1050 add_enc_ts_padata(context, md, client,
1051 ctx->keyproc, ctx->keyseed,
1052 a->req_body.etype.val, a->req_body.etype.len,
1055 /* make a v4 salted pa-data */
1056 salt.salttype = KRB5_PW_SALT;
1057 krb5_data_zero(&salt.saltvalue);
1058 add_enc_ts_padata(context, md, client,
1059 ctx->keyproc, ctx->keyseed,
1060 a->req_body.etype.val, a->req_body.etype.len,
1066 static krb5_error_code
1067 pa_data_to_key_plain(krb5_context context,
1068 const krb5_principal client,
1069 krb5_get_init_creds_ctx *ctx,
1071 krb5_data *s2kparams,
1073 krb5_keyblock **key)
1075 krb5_error_code ret;
1077 ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
1078 salt, s2kparams, key);
1083 static krb5_error_code
1084 pa_data_to_md_pkinit(krb5_context context,
1086 const krb5_principal client,
1088 krb5_get_init_creds_ctx *ctx,
1091 if (ctx->pk_init_ctx == NULL)
1094 return _krb5_pk_mk_padata(context,
1102 krb5_set_error_message(context, EINVAL,
1103 N_("no support for PKINIT compiled in", ""));
1108 static krb5_error_code
1109 pa_data_add_pac_request(krb5_context context,
1110 krb5_get_init_creds_ctx *ctx,
1113 size_t len = 0, length;
1114 krb5_error_code ret;
1118 switch (ctx->req_pac) {
1119 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1120 return 0; /* don't bother */
1121 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1122 req.include_pac = 1;
1124 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1125 req.include_pac = 0;
1128 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1133 krb5_abortx(context, "internal error in ASN.1 encoder");
1135 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1143 * Assumes caller always will free `out_md', even on error.
1146 static krb5_error_code
1147 process_pa_data_to_md(krb5_context context,
1148 const krb5_creds *creds,
1150 krb5_get_init_creds_ctx *ctx,
1152 METHOD_DATA **out_md,
1153 krb5_prompter_fct prompter,
1154 void *prompter_data)
1156 krb5_error_code ret;
1159 if (*out_md == NULL) {
1160 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1164 (*out_md)->val = NULL;
1166 if (_krb5_have_debug(context, 5)) {
1168 _krb5_debug(context, 5, "KDC send %d patypes", in_md->len);
1169 for (i = 0; i < in_md->len; i++)
1170 _krb5_debug(context, 5, "KDC send PA-DATA type: %d", in_md->val[i].padata_type);
1174 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1175 * need to expose our password protecting our PKCS12 key.
1178 if (ctx->pk_init_ctx) {
1180 _krb5_debug(context, 5, "krb5_get_init_creds: "
1181 "prepareing PKINIT padata (%s)",
1182 (ctx->used_pa_types & USED_PKINIT_W2K) ? "win2k" : "ietf");
1184 if (ctx->used_pa_types & USED_PKINIT_W2K) {
1185 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1186 "Already tried pkinit, looping");
1187 return KRB5_GET_IN_TKT_LOOP;
1190 ret = pa_data_to_md_pkinit(context, a, creds->client,
1191 (ctx->used_pa_types & USED_PKINIT),
1196 if (ctx->used_pa_types & USED_PKINIT)
1197 ctx->used_pa_types |= USED_PKINIT_W2K;
1199 ctx->used_pa_types |= USED_PKINIT;
1201 } else if (in_md->len != 0) {
1202 struct pa_info_data *paid, *ppaid;
1205 paid = calloc(1, sizeof(*paid));
1207 paid->etype = ENCTYPE_NULL;
1208 ppaid = process_pa_info(context, creds->client, a, paid, in_md);
1211 flag = USED_ENC_TS_INFO;
1213 flag = USED_ENC_TS_GUESS;
1215 if (ctx->used_pa_types & flag) {
1217 free_paid(context, ppaid);
1218 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1219 "Already tried ENC-TS-%s, looping",
1220 flag == USED_ENC_TS_INFO ? "info" : "guess");
1221 return KRB5_GET_IN_TKT_LOOP;
1224 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1226 ctx->used_pa_types |= flag;
1230 free_paid(context, ctx->ppaid);
1238 pa_data_add_pac_request(context, ctx, *out_md);
1240 if ((*out_md)->len == 0) {
1248 static krb5_error_code
1249 process_pa_data_to_key(krb5_context context,
1250 krb5_get_init_creds_ctx *ctx,
1254 const krb5_krbhst_info *hi,
1255 krb5_keyblock **key)
1257 struct pa_info_data paid, *ppaid = NULL;
1258 krb5_error_code ret;
1262 memset(&paid, 0, sizeof(paid));
1264 etype = rep->enc_part.etype;
1268 ppaid = process_pa_info(context, creds->client, a, &paid,
1273 if (ppaid == NULL) {
1274 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1278 paid.s2kparams = NULL;
1285 pa = krb5_find_padata(rep->padata->val,
1287 KRB5_PADATA_PK_AS_REP,
1291 pa = krb5_find_padata(rep->padata->val,
1293 KRB5_PADATA_PK_AS_REP_19,
1297 if (pa && ctx->pk_init_ctx) {
1299 _krb5_debug(context, 5, "krb5_get_init_creds: using PKINIT");
1301 ret = _krb5_pk_rd_pa_reply(context,
1312 krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", ""));
1314 } else if (ctx->keyseed) {
1315 _krb5_debug(context, 5, "krb5_get_init_creds: using keyproc");
1316 ret = pa_data_to_key_plain(context, creds->client, ctx,
1317 ppaid->salt, ppaid->s2kparams, etype, key);
1320 krb5_set_error_message(context, ret, N_("No usable pa data type", ""));
1323 free_paid(context, &paid);
1328 * Start a new context to get a new initial credential.
1330 * @param context A Kerberos 5 context.
1331 * @param client The Kerberos principal to get the credential for, if
1332 * NULL is given, the default principal is used as determined by
1333 * krb5_get_default_principal().
1335 * @param prompter_data
1336 * @param start_time the time the ticket should start to be valid or 0 for now.
1337 * @param options a options structure, can be NULL for default options.
1338 * @param rctx A new allocated free with krb5_init_creds_free().
1340 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1342 * @ingroup krb5_credential
1345 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1346 krb5_init_creds_init(krb5_context context,
1347 krb5_principal client,
1348 krb5_prompter_fct prompter,
1349 void *prompter_data,
1350 krb5_deltat start_time,
1351 krb5_get_init_creds_opt *options,
1352 krb5_init_creds_context *rctx)
1354 krb5_init_creds_context ctx;
1355 krb5_error_code ret;
1359 ctx = calloc(1, sizeof(*ctx));
1361 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1365 ret = get_init_creds_common(context, client, start_time, options, ctx);
1371 /* Set a new nonce. */
1372 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1373 ctx->nonce &= 0x7fffffff;
1374 /* XXX these just needs to be the same when using Windows PK-INIT */
1375 ctx->pk_nonce = ctx->nonce;
1377 ctx->prompter = prompter;
1378 ctx->prompter_data = prompter_data;
1386 * Sets the service that the is requested. This call is only neede for
1387 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1389 * @param context a Kerberos 5 context.
1390 * @param ctx a krb5_init_creds_context context.
1391 * @param service the service given as a string, for example
1392 * "kadmind/admin". If NULL, the default krbtgt in the clients
1395 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1396 * @ingroup krb5_credential
1399 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1400 krb5_init_creds_set_service(krb5_context context,
1401 krb5_init_creds_context ctx,
1402 const char *service)
1404 krb5_const_realm client_realm;
1405 krb5_principal principal;
1406 krb5_error_code ret;
1408 client_realm = krb5_principal_get_realm (context, ctx->cred.client);
1411 ret = krb5_parse_name (context, service, &principal);
1414 krb5_principal_set_realm (context, principal, client_realm);
1416 ret = krb5_make_principal(context, &principal,
1417 client_realm, KRB5_TGS_NAME, client_realm,
1424 * This is for Windows RODC that are picky about what name type
1425 * the server principal have, and the really strange part is that
1426 * they are picky about the AS-REQ name type and not the TGS-REQ
1430 if (krb5_principal_is_krbtgt(context, principal))
1431 krb5_principal_set_type(context, principal, KRB5_NT_SRV_INST);
1433 krb5_free_principal(context, ctx->cred.server);
1434 ctx->cred.server = principal;
1440 * Sets the password that will use for the request.
1442 * @param context a Kerberos 5 context.
1443 * @param ctx ctx krb5_init_creds_context context.
1444 * @param password the password to use.
1446 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1447 * @ingroup krb5_credential
1450 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1451 krb5_init_creds_set_password(krb5_context context,
1452 krb5_init_creds_context ctx,
1453 const char *password)
1455 if (ctx->password) {
1456 memset(ctx->password, 0, strlen(ctx->password));
1457 free(ctx->password);
1460 ctx->password = strdup(password);
1461 if (ctx->password == NULL) {
1462 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1465 ctx->keyseed = (void *) ctx->password;
1467 ctx->keyseed = NULL;
1468 ctx->password = NULL;
1474 static krb5_error_code KRB5_CALLCONV
1475 keytab_key_proc(krb5_context context, krb5_enctype enctype,
1476 krb5_const_pointer keyseed,
1477 krb5_salt salt, krb5_data *s2kparms,
1478 krb5_keyblock **key)
1480 krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed);
1481 krb5_keytab keytab = args->keytab;
1482 krb5_principal principal = args->principal;
1483 krb5_error_code ret;
1484 krb5_keytab real_keytab;
1485 krb5_keytab_entry entry;
1488 krb5_kt_default(context, &real_keytab);
1490 real_keytab = keytab;
1492 ret = krb5_kt_get_entry (context, real_keytab, principal,
1493 0, enctype, &entry);
1495 ret = krb5_copy_keyblock(context, &entry.keyblock, key);
1496 krb5_kt_free_entry(context, &entry);
1500 krb5_kt_close (context, real_keytab);
1506 * Set the keytab to use for authentication.
1508 * @param context a Kerberos 5 context.
1509 * @param ctx ctx krb5_init_creds_context context.
1510 * @param keytab the keytab to read the key from.
1512 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1513 * @ingroup krb5_credential
1516 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1517 krb5_init_creds_set_keytab(krb5_context context,
1518 krb5_init_creds_context ctx,
1521 krb5_keytab_key_proc_args *a;
1522 krb5_keytab_entry entry;
1523 krb5_kt_cursor cursor;
1524 krb5_enctype *etypes = NULL;
1525 krb5_error_code ret;
1529 a = malloc(sizeof(*a));
1531 krb5_set_error_message(context, ENOMEM,
1532 N_("malloc: out of memory", ""));
1536 a->principal = ctx->cred.client;
1539 ctx->keytab_data = a;
1540 ctx->keyseed = (void *)a;
1541 ctx->keyproc = keytab_key_proc;
1544 * We need to the KDC what enctypes we support for this keytab,
1545 * esp if the keytab is really a password based entry, then the
1546 * KDC might have more enctypes in the database then what we have
1550 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1554 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
1557 if (!krb5_principal_compare(context, entry.principal, ctx->cred.client))
1560 /* check if we ahve this kvno already */
1561 if (entry.vno > kvno) {
1562 /* remove old list of etype */
1568 } else if (entry.vno != kvno)
1571 /* check if enctype is supported */
1572 if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0)
1575 /* add enctype to supported list */
1576 ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2));
1581 etypes[netypes] = entry.keyblock.keytype;
1582 etypes[netypes + 1] = ETYPE_NULL;
1585 krb5_kt_free_entry(context, &entry);
1587 krb5_kt_end_seq_get(context, keytab, &cursor);
1592 ctx->etypes = etypes;
1599 static krb5_error_code KRB5_CALLCONV
1600 keyblock_key_proc(krb5_context context, krb5_enctype enctype,
1601 krb5_const_pointer keyseed,
1602 krb5_salt salt, krb5_data *s2kparms,
1603 krb5_keyblock **key)
1605 return krb5_copy_keyblock (context, keyseed, key);
1608 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1609 krb5_init_creds_set_keyblock(krb5_context context,
1610 krb5_init_creds_context ctx,
1611 krb5_keyblock *keyblock)
1613 ctx->keyseed = (void *)keyblock;
1614 ctx->keyproc = keyblock_key_proc;
1620 * The core loop if krb5_get_init_creds() function family. Create the
1621 * packets and have the caller send them off to the KDC.
1623 * If the caller want all work been done for them, use
1624 * krb5_init_creds_get() instead.
1626 * @param context a Kerberos 5 context.
1627 * @param ctx ctx krb5_init_creds_context context.
1628 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
1629 * @param out reply to KDC.
1630 * @param hostinfo KDC address info, first round it can be NULL.
1631 * @param flags status of the round, if
1632 * KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round.
1634 * @return 0 for success, or an Kerberos 5 error code, see
1635 * krb5_get_error_message().
1637 * @ingroup krb5_credential
1640 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1641 krb5_init_creds_step(krb5_context context,
1642 krb5_init_creds_context ctx,
1645 krb5_krbhst_info *hostinfo,
1646 unsigned int *flags)
1648 krb5_error_code ret;
1652 krb5_data_zero(out);
1654 if (ctx->as_req.req_body.cname == NULL) {
1655 ret = init_as_req(context, ctx->flags, &ctx->cred,
1656 ctx->addrs, ctx->etypes, &ctx->as_req);
1658 free_init_creds_ctx(context, ctx);
1663 #define MAX_PA_COUNTER 10
1664 if (ctx->pa_counter > MAX_PA_COUNTER) {
1665 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1666 N_("Looping %d times while getting "
1667 "initial credentials", ""),
1669 return KRB5_GET_IN_TKT_LOOP;
1673 _krb5_debug(context, 5, "krb5_get_init_creds: loop %d", ctx->pa_counter);
1675 /* Lets process the input packet */
1676 if (in && in->length) {
1679 memset(&rep, 0, sizeof(rep));
1681 _krb5_debug(context, 5, "krb5_get_init_creds: processing input");
1683 ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
1685 krb5_keyblock *key = NULL;
1686 unsigned eflags = EXTRACT_TICKET_AS_REQ | EXTRACT_TICKET_TIMESYNC;
1688 if (ctx->flags.canonicalize) {
1689 eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1690 eflags |= EXTRACT_TICKET_MATCH_REALM;
1692 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1693 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1695 ret = process_pa_data_to_key(context, ctx, &ctx->cred,
1696 &ctx->as_req, &rep.kdc_rep, hostinfo, &key);
1698 free_AS_REP(&rep.kdc_rep);
1702 _krb5_debug(context, 5, "krb5_get_init_creds: extracting ticket");
1704 ret = _krb5_extract_ticket(context,
1709 KRB5_KU_AS_REP_ENC_PART,
1715 krb5_free_keyblock(context, key);
1720 ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
1722 free_AS_REP(&rep.kdc_rep);
1723 free_EncASRepPart(&rep.enc_part);
1728 /* let's try to parse it as a KRB-ERROR */
1730 _krb5_debug(context, 5, "krb5_get_init_creds: got an error");
1732 free_KRB_ERROR(&ctx->error);
1734 ret = krb5_rd_error(context, in, &ctx->error);
1735 if(ret && in->length && ((char*)in->data)[0] == 4)
1736 ret = KRB5KRB_AP_ERR_V4_REPLY;
1738 _krb5_debug(context, 5, "krb5_get_init_creds: failed to read error");
1742 ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);
1744 _krb5_debug(context, 5, "krb5_get_init_creds: KRB-ERROR %d", ret);
1747 * If no preauth was set and KDC requires it, give it one
1751 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1753 free_METHOD_DATA(&ctx->md);
1754 memset(&ctx->md, 0, sizeof(ctx->md));
1756 if (ctx->error.e_data) {
1757 ret = decode_METHOD_DATA(ctx->error.e_data->data,
1758 ctx->error.e_data->length,
1762 krb5_set_error_message(context, ret,
1763 N_("Failed to decode METHOD-DATA", ""));
1765 krb5_set_error_message(context, ret,
1766 N_("Preauth required but no preauth "
1767 "options send by KDC", ""));
1769 } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
1771 * Try adapt to timeskrew when we are using pre-auth, and
1772 * if there was a time skew, try again.
1774 krb5_set_real_time(context, ctx->error.stime, -1);
1775 if (context->kdc_sec_offset)
1778 _krb5_debug(context, 10, "init_creds: err skew updateing kdc offset to %d",
1779 context->kdc_sec_offset);
1781 ctx->used_pa_types = 0;
1783 } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
1784 /* client referal to a new realm */
1786 if (ctx->error.crealm == NULL) {
1787 krb5_set_error_message(context, ret,
1788 N_("Got a client referral, not but no realm", ""));
1791 _krb5_debug(context, 5,
1792 "krb5_get_init_creds: got referal to realm %s",
1793 *ctx->error.crealm);
1795 ret = krb5_principal_set_realm(context,
1797 *ctx->error.crealm);
1799 ctx->used_pa_types = 0;
1806 if (ctx->as_req.padata) {
1807 free_METHOD_DATA(ctx->as_req.padata);
1808 free(ctx->as_req.padata);
1809 ctx->as_req.padata = NULL;
1812 /* Set a new nonce. */
1813 ctx->as_req.req_body.nonce = ctx->nonce;
1815 /* fill_in_md_data */
1816 ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
1817 &ctx->md, &ctx->as_req.padata,
1818 ctx->prompter, ctx->prompter_data);
1822 krb5_data_free(&ctx->req_buffer);
1824 ASN1_MALLOC_ENCODE(AS_REQ,
1825 ctx->req_buffer.data, ctx->req_buffer.length,
1826 &ctx->as_req, &len, ret);
1829 if(len != ctx->req_buffer.length)
1830 krb5_abortx(context, "internal error in ASN.1 encoder");
1832 out->data = ctx->req_buffer.data;
1833 out->length = ctx->req_buffer.length;
1835 *flags = KRB5_INIT_CREDS_STEP_FLAG_CONTINUE;
1843 * Extract the newly acquired credentials from krb5_init_creds_context
1846 * @param context A Kerberos 5 context.
1848 * @param cred credentials, free with krb5_free_cred_contents().
1850 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
1853 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1854 krb5_init_creds_get_creds(krb5_context context,
1855 krb5_init_creds_context ctx,
1858 return krb5_copy_creds_contents(context, &ctx->cred, cred);
1862 * Get the last error from the transaction.
1864 * @return Returns 0 or an error code
1866 * @ingroup krb5_credential
1869 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1870 krb5_init_creds_get_error(krb5_context context,
1871 krb5_init_creds_context ctx,
1874 krb5_error_code ret;
1876 ret = copy_KRB_ERROR(&ctx->error, error);
1878 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1884 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
1886 * @param context A Kerberos 5 context.
1887 * @param ctx The krb5_init_creds_context to free.
1889 * @ingroup krb5_credential
1892 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1893 krb5_init_creds_free(krb5_context context,
1894 krb5_init_creds_context ctx)
1896 free_init_creds_ctx(context, ctx);
1901 * Get new credentials as setup by the krb5_init_creds_context.
1903 * @param context A Kerberos 5 context.
1904 * @param ctx The krb5_init_creds_context to process.
1906 * @ingroup krb5_credential
1909 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1910 krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
1912 krb5_sendto_ctx stctx = NULL;
1913 krb5_krbhst_info *hostinfo = NULL;
1914 krb5_error_code ret;
1916 unsigned int flags = 0;
1918 krb5_data_zero(&in);
1919 krb5_data_zero(&out);
1921 ret = krb5_sendto_ctx_alloc(context, &stctx);
1924 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1928 ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
1929 krb5_data_free(&in);
1933 if ((flags & 1) == 0)
1936 ret = krb5_sendto_context (context, stctx, &out,
1937 ctx->cred.client->realm, &in);
1945 krb5_sendto_ctx_free(context, stctx);
1951 * Get new credentials using password.
1953 * @ingroup krb5_credential
1957 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1958 krb5_get_init_creds_password(krb5_context context,
1960 krb5_principal client,
1961 const char *password,
1962 krb5_prompter_fct prompter,
1964 krb5_deltat start_time,
1965 const char *in_tkt_service,
1966 krb5_get_init_creds_opt *options)
1968 krb5_init_creds_context ctx;
1970 krb5_error_code ret;
1974 ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
1978 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1982 if (prompter != NULL && ctx->password == NULL && password == NULL) {
1984 krb5_data password_data;
1987 krb5_unparse_name (context, client, &p);
1988 asprintf (&q, "%s's Password: ", p);
1991 password_data.data = buf;
1992 password_data.length = sizeof(buf);
1994 prompt.reply = &password_data;
1995 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1997 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
2000 memset (buf, 0, sizeof(buf));
2001 ret = KRB5_LIBOS_PWDINTR;
2002 krb5_clear_error_message (context);
2005 password = password_data.data;
2009 ret = krb5_init_creds_set_password(context, ctx, password);
2014 ret = krb5_init_creds_get(context, ctx);
2017 process_last_request(context, options, ctx);
2020 if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
2023 /* try to avoid recursion */
2024 if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
2027 /* don't try to change password where then where none */
2028 if (prompter == NULL)
2031 ret = change_password (context,
2042 krb5_init_creds_free(context, ctx);
2048 krb5_init_creds_get_creds(context, ctx, creds);
2051 krb5_init_creds_free(context, ctx);
2053 memset(buf, 0, sizeof(buf));
2058 * Get new credentials using keyblock.
2060 * @ingroup krb5_credential
2063 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2064 krb5_get_init_creds_keyblock(krb5_context context,
2066 krb5_principal client,
2067 krb5_keyblock *keyblock,
2068 krb5_deltat start_time,
2069 const char *in_tkt_service,
2070 krb5_get_init_creds_opt *options)
2072 krb5_init_creds_context ctx;
2073 krb5_error_code ret;
2075 memset(creds, 0, sizeof(*creds));
2077 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2081 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2085 ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
2089 ret = krb5_init_creds_get(context, ctx);
2092 process_last_request(context, options, ctx);
2096 krb5_init_creds_get_creds(context, ctx, creds);
2099 krb5_init_creds_free(context, ctx);
2105 * Get new credentials using keytab.
2107 * @ingroup krb5_credential
2110 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2111 krb5_get_init_creds_keytab(krb5_context context,
2113 krb5_principal client,
2115 krb5_deltat start_time,
2116 const char *in_tkt_service,
2117 krb5_get_init_creds_opt *options)
2119 krb5_init_creds_context ctx;
2120 krb5_error_code ret;
2122 memset(creds, 0, sizeof(*creds));
2124 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2128 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2132 ret = krb5_init_creds_set_keytab(context, ctx, keytab);
2136 ret = krb5_init_creds_get(context, ctx);
2138 process_last_request(context, options, ctx);
2142 krb5_init_creds_get_creds(context, ctx, creds);
2145 krb5_init_creds_free(context, ctx);