Handle picky windows RODC servers
[heimdal.git] / lib / krb5 / init_creds_pw.c
blob29b882d053117c878022ab4af49669a5915c3778
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
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
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
38 typedef struct krb5_get_init_creds_ctx {
39 KDCOptions flags;
40 krb5_creds cred;
41 krb5_addresses *addrs;
42 krb5_enctype *etypes;
43 krb5_preauthtype *pre_auth_types;
44 char *in_tkt_service;
45 unsigned nonce;
46 unsigned pk_nonce;
48 krb5_data req_buffer;
49 AS_REQ as_req;
50 int pa_counter;
52 /* password and keytab_data is freed on completion */
53 char *password;
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;
62 int ic_flags;
64 int used_pa_types;
65 #define USED_PKINIT 1
66 #define USED_PKINIT_W2K 2
67 #define USED_ENC_TS_GUESS 4
68 #define USED_ENC_TS_INFO 8
70 METHOD_DATA md;
71 KRB_ERROR error;
72 AS_REP as_rep;
73 EncKDCRepPart enc_part;
75 krb5_prompter_fct prompter;
76 void *prompter_data;
78 struct pa_info_data *ppaid;
80 } krb5_get_init_creds_ctx;
83 struct pa_info_data {
84 krb5_enctype etype;
85 krb5_salt salt;
86 krb5_data *s2kparams;
89 static void
90 free_paid(krb5_context context, struct pa_info_data *ppaid)
92 krb5_free_salt(context, ppaid->salt);
93 if (ppaid->s2kparams)
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,
101 krb5_keyblock **key)
103 krb5_error_code ret;
104 krb5_data password;
105 krb5_data opaque;
107 _krb5_debug(context, 5, "krb5_get_init_creds: using default_s2k_func");
109 password.data = rk_UNCONST(keyseed);
110 password.length = strlen(keyseed);
111 if (s2kparms)
112 opaque = *s2kparms;
113 else
114 krb5_data_zero(&opaque);
116 *key = malloc(sizeof(**key));
117 if (*key == NULL)
118 return ENOMEM;
119 ret = krb5_string_to_key_data_salt_opaque(context, type, password,
120 salt, opaque, *key);
121 if (ret) {
122 free(*key);
123 *key = NULL;
125 return ret;
128 static void
129 free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx)
131 if (ctx->etypes)
132 free(ctx->etypes);
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);
139 if (ctx->password) {
140 memset(ctx->password, 0, strlen(ctx->password));
141 free(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);
150 if (ctx->ppaid) {
151 free_paid(context, ctx->ppaid);
152 free(ctx->ppaid);
154 memset(ctx, 0, sizeof(*ctx));
157 static int
158 get_config_time (krb5_context context,
159 const char *realm,
160 const char *name,
161 int def)
163 int ret;
165 ret = krb5_config_get_time (context, NULL,
166 "realms",
167 realm,
168 name,
169 NULL);
170 if (ret >= 0)
171 return ret;
172 ret = krb5_config_get_time (context, NULL,
173 "libdefaults",
174 name,
175 NULL);
176 if (ret >= 0)
177 return ret;
178 return def;
181 static krb5_error_code
182 init_cred (krb5_context context,
183 krb5_creds *cred,
184 krb5_principal client,
185 krb5_deltat start_time,
186 krb5_get_init_creds_opt *options)
188 krb5_error_code ret;
189 int tmp;
190 krb5_timestamp now;
192 krb5_timeofday (context, &now);
194 memset (cred, 0, sizeof(*cred));
196 if (client)
197 krb5_copy_principal(context, client, &cred->client);
198 else {
199 ret = krb5_get_default_principal (context,
200 &cred->client);
201 if (ret)
202 goto out;
205 if (start_time)
206 cred->times.starttime = now + start_time;
208 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
209 tmp = options->tkt_life;
210 else
211 tmp = 10 * 60 * 60;
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;
219 return 0;
221 out:
222 krb5_free_cred_contents (context, cred);
223 return ret;
227 * Print a message (str) to the user about the expiration in `lr'
230 static void
231 report_expiration (krb5_context context,
232 krb5_prompter_fct prompter,
233 krb5_data *data,
234 const char *str,
235 time_t now)
237 char *p = NULL;
239 if (asprintf(&p, "%s%s", str, ctime(&now)) < 0 || p == NULL)
240 return;
241 (*prompter)(context, data, NULL, p, 0, NULL);
242 free(p);
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;
260 LastReq *lr;
261 krb5_boolean reported = FALSE;
262 krb5_timestamp sec;
263 time_t t;
264 size_t i;
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));
277 if (lre == NULL) {
278 krb5_set_error_message(context, ENOMEM,
279 N_("malloc: out of memory", ""));
280 return ENOMEM;
282 for (i = 0; i < lr->len; i++) {
283 lre[i] = calloc(1, sizeof(*lre[i]));
284 if (lre[i] == NULL)
285 break;
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++)
294 free(lre[i]);
295 free(lre);
299 * Now check if we should prompt the user
302 if (ctx->prompter == NULL)
303 return 0;
305 krb5_timeofday (context, &sec);
307 t = sec + get_config_time (context,
308 realm,
309 "warn_pwexpire",
310 7 * 24 * 60 * 60);
312 for (i = 0; i < lr->len; ++i) {
313 if (lr->val[i].lr_value <= t) {
314 switch (abs(lr->val[i].lr_type)) {
315 case LR_PW_EXPTIME :
316 report_expiration(context, ctx->prompter,
317 ctx->prompter_data,
318 "Your password will expire at ",
319 lr->val[i].lr_value);
320 reported = TRUE;
321 break;
322 case LR_ACCT_EXPTIME :
323 report_expiration(context, ctx->prompter,
324 ctx->prompter_data,
325 "Your account will expire at ",
326 lr->val[i].lr_value);
327 reported = TRUE;
328 break;
333 if (!reported
334 && ctx->enc_part.key_expiration
335 && *ctx->enc_part.key_expiration <= t) {
336 report_expiration(context, ctx->prompter,
337 ctx->prompter_data,
338 "Your password/account will expire at ",
339 *ctx->enc_part.key_expiration);
341 return 0;
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;
354 krb5_error_code ret;
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);
372 if (ret)
373 goto out;
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;
380 } else
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;
392 ctx->addrs = NULL;
393 ctx->etypes = NULL;
394 ctx->pre_auth_types = NULL;
396 ret = init_cred(context, &ctx->cred, client, start_time, options);
397 if (ret) {
398 if (default_opt)
399 krb5_get_init_creds_opt_free(context, default_opt);
400 return ret;
403 ret = krb5_init_creds_set_service(context, ctx, NULL);
404 if (ret)
405 goto out;
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;
413 if (start_time)
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;
424 #else
425 ctx->addrs = NULL;
426 #endif
427 break;
428 case KRB5_INIT_CREDS_TRISTATE_FALSE:
429 ctx->addrs = NULL;
430 break;
431 case KRB5_INIT_CREDS_TRISTATE_TRUE:
432 ctx->addrs = &no_addrs;
433 break;
436 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
437 if (ctx->etypes)
438 free(ctx->etypes);
440 etypes = malloc((options->etype_list_length + 1)
441 * sizeof(krb5_enctype));
442 if (etypes == NULL) {
443 ret = ENOMEM;
444 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
445 goto out;
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) {
456 ret = ENOMEM;
457 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
458 goto out;
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;
467 if (default_opt)
468 krb5_get_init_creds_opt_free(context, default_opt);
469 return 0;
470 out:
471 if (default_opt)
472 krb5_get_init_creds_opt_free(context, default_opt);
473 return ret;
476 static krb5_error_code
477 change_password (krb5_context context,
478 krb5_principal client,
479 const char *password,
480 char *newpw,
481 size_t newpw_sz,
482 krb5_prompter_fct prompter,
483 void *data,
484 krb5_get_init_creds_opt *old_options)
486 krb5_prompt prompts[2];
487 krb5_error_code ret;
488 krb5_creds cpw_cred;
489 char buf1[BUFSIZ], buf2[BUFSIZ];
490 krb5_data password_data[2];
491 int result_code;
492 krb5_data result_code_string;
493 krb5_data result_string;
494 char *p;
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);
500 if (ret)
501 return ret;
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,
514 &cpw_cred,
515 client,
516 password,
517 prompter,
518 data,
520 "kadmin/changepw",
521 options);
522 krb5_get_init_creds_opt_free(context, options);
523 if (ret)
524 goto out;
526 for(;;) {
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",
544 2, prompts);
545 if (ret) {
546 memset (buf1, 0, sizeof(buf1));
547 memset (buf2, 0, sizeof(buf2));
548 goto out;
551 if (strcmp (buf1, buf2) == 0)
552 break;
553 memset (buf1, 0, sizeof(buf1));
554 memset (buf2, 0, sizeof(buf2));
557 ret = krb5_set_password (context,
558 &cpw_cred,
559 buf1,
560 client,
561 &result_code,
562 &result_code_string,
563 &result_string);
564 if (ret)
565 goto out;
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)
571 ret = ENOMEM;
572 goto out;
575 /* return the result */
576 (*prompter) (context, data, NULL, p, 0, NULL);
578 free (p);
579 if (result_code == 0) {
580 strlcpy (newpw, buf1, newpw_sz);
581 ret = 0;
582 } else {
583 ret = ENOTTY;
584 krb5_set_error_message(context, ret,
585 N_("failed changing password", ""));
588 out:
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);
594 return ret;
598 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
599 krb5_keyblock_key_proc (krb5_context context,
600 krb5_keytype type,
601 krb5_data *salt,
602 krb5_const_pointer keyseed,
603 krb5_keyblock **key)
605 return krb5_copy_keyblock (context, keyseed, key);
612 static krb5_error_code
613 init_as_req (krb5_context context,
614 KDCOptions opts,
615 const krb5_creds *creds,
616 const krb5_addresses *addrs,
617 const krb5_enctype *etypes,
618 AS_REQ *a)
620 krb5_error_code ret;
622 memset(a, 0, sizeof(*a));
624 a->pvno = 5;
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) {
629 ret = ENOMEM;
630 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
631 goto fail;
633 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
634 if (a->req_body.sname == NULL) {
635 ret = ENOMEM;
636 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
637 goto fail;
640 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
641 if (ret)
642 goto fail;
643 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
644 if (ret)
645 goto fail;
647 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
648 if (ret)
649 goto fail;
651 if(creds->times.starttime) {
652 a->req_body.from = malloc(sizeof(*a->req_body.from));
653 if (a->req_body.from == NULL) {
654 ret = ENOMEM;
655 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
656 goto fail;
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) {
667 ret = ENOMEM;
668 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
669 goto fail;
671 *a->req_body.rtime = creds->times.renew_till;
673 a->req_body.nonce = 0;
674 ret = krb5_init_etype (context,
675 &a->req_body.etype.len,
676 &a->req_body.etype.val,
677 etypes);
678 if (ret)
679 goto fail;
682 * This means no addresses
685 if (addrs && addrs->len == 0) {
686 a->req_body.addresses = NULL;
687 } else {
688 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
689 if (a->req_body.addresses == NULL) {
690 ret = ENOMEM;
691 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
692 goto fail;
695 if (addrs)
696 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
697 else {
698 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
699 if(ret == 0 && a->req_body.addresses->len == 0) {
700 free(a->req_body.addresses);
701 a->req_body.addresses = NULL;
704 if (ret)
705 goto fail;
708 a->req_body.enc_authorization_data = NULL;
709 a->req_body.additional_tickets = NULL;
711 a->padata = NULL;
713 return 0;
714 fail:
715 free_AS_REQ(a);
716 memset(a, 0, sizeof(*a));
717 return ret;
721 static krb5_error_code
722 set_paid(struct pa_info_data *paid, krb5_context context,
723 krb5_enctype etype,
724 krb5_salttype salttype, void *salt_string, size_t salt_len,
725 krb5_data *s2kparams)
727 paid->etype = etype;
728 paid->salt.salttype = salttype;
729 paid->salt.saltvalue.data = malloc(salt_len + 1);
730 if (paid->salt.saltvalue.data == NULL) {
731 krb5_clear_error_message(context);
732 return ENOMEM;
734 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
735 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
736 paid->salt.saltvalue.length = salt_len;
737 if (s2kparams) {
738 krb5_error_code ret;
740 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
741 if (ret) {
742 krb5_clear_error_message(context);
743 krb5_free_salt(context, paid->salt);
744 return ret;
746 } else
747 paid->s2kparams = NULL;
749 return 0;
752 static struct pa_info_data *
753 pa_etype_info2(krb5_context context,
754 const krb5_principal client,
755 const AS_REQ *asreq,
756 struct pa_info_data *paid,
757 heim_octet_string *data)
759 krb5_error_code ret;
760 ETYPE_INFO2 e;
761 size_t sz;
762 int i, j;
764 memset(&e, 0, sizeof(e));
765 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
766 if (ret)
767 goto out;
768 if (e.len == 0)
769 goto out;
770 for (j = 0; j < asreq->req_body.etype.len; j++) {
771 for (i = 0; i < e.len; i++) {
772 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
773 krb5_salt salt;
774 if (e.val[i].salt == NULL)
775 ret = krb5_get_pw_salt(context, client, &salt);
776 else {
777 salt.saltvalue.data = *e.val[i].salt;
778 salt.saltvalue.length = strlen(*e.val[i].salt);
779 ret = 0;
781 if (ret == 0)
782 ret = set_paid(paid, context, e.val[i].etype,
783 KRB5_PW_SALT,
784 salt.saltvalue.data,
785 salt.saltvalue.length,
786 e.val[i].s2kparams);
787 if (e.val[i].salt == NULL)
788 krb5_free_salt(context, salt);
789 if (ret == 0) {
790 free_ETYPE_INFO2(&e);
791 return paid;
796 out:
797 free_ETYPE_INFO2(&e);
798 return NULL;
801 static struct pa_info_data *
802 pa_etype_info(krb5_context context,
803 const krb5_principal client,
804 const AS_REQ *asreq,
805 struct pa_info_data *paid,
806 heim_octet_string *data)
808 krb5_error_code ret;
809 ETYPE_INFO e;
810 size_t sz;
811 int i, j;
813 memset(&e, 0, sizeof(e));
814 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
815 if (ret)
816 goto out;
817 if (e.len == 0)
818 goto out;
819 for (j = 0; j < asreq->req_body.etype.len; j++) {
820 for (i = 0; i < e.len; i++) {
821 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
822 krb5_salt salt;
823 salt.salttype = KRB5_PW_SALT;
824 if (e.val[i].salt == NULL)
825 ret = krb5_get_pw_salt(context, client, &salt);
826 else {
827 salt.saltvalue = *e.val[i].salt;
828 ret = 0;
830 if (e.val[i].salttype)
831 salt.salttype = *e.val[i].salttype;
832 if (ret == 0) {
833 ret = set_paid(paid, context, e.val[i].etype,
834 salt.salttype,
835 salt.saltvalue.data,
836 salt.saltvalue.length,
837 NULL);
838 if (e.val[i].salt == NULL)
839 krb5_free_salt(context, salt);
841 if (ret == 0) {
842 free_ETYPE_INFO(&e);
843 return paid;
848 out:
849 free_ETYPE_INFO(&e);
850 return NULL;
853 static struct pa_info_data *
854 pa_pw_or_afs3_salt(krb5_context context,
855 const krb5_principal client,
856 const AS_REQ *asreq,
857 struct pa_info_data *paid,
858 heim_octet_string *data)
860 krb5_error_code ret;
861 if (paid->etype == ENCTYPE_NULL)
862 return NULL;
863 ret = set_paid(paid, context,
864 paid->etype,
865 paid->salt.salttype,
866 data->data,
867 data->length,
868 NULL);
869 if (ret)
870 return NULL;
871 return paid;
875 struct pa_info {
876 krb5_preauthtype type;
877 struct pa_info_data *(*salt_info)(krb5_context,
878 const krb5_principal,
879 const AS_REQ *,
880 struct pa_info_data *,
881 heim_octet_string *);
884 static struct pa_info pa_prefs[] = {
885 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
886 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
887 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
888 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
891 static PA_DATA *
892 find_pa_data(const METHOD_DATA *md, int type)
894 int i;
895 if (md == NULL)
896 return NULL;
897 for (i = 0; i < md->len; i++)
898 if (md->val[i].padata_type == type)
899 return &md->val[i];
900 return NULL;
903 static struct pa_info_data *
904 process_pa_info(krb5_context context,
905 const krb5_principal client,
906 const AS_REQ *asreq,
907 struct pa_info_data *paid,
908 METHOD_DATA *md)
910 struct pa_info_data *p = NULL;
911 int i;
913 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
914 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
915 if (pa == NULL)
916 continue;
917 paid->salt.salttype = pa_prefs[i].type;
918 p = (*pa_prefs[i].salt_info)(context, client, asreq,
919 paid, &pa->padata_value);
921 return p;
924 static krb5_error_code
925 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
926 krb5_enctype etype, krb5_keyblock *key)
928 PA_ENC_TS_ENC p;
929 unsigned char *buf;
930 size_t buf_size;
931 size_t len;
932 EncryptedData encdata;
933 krb5_error_code ret;
934 int32_t usec;
935 int usec2;
936 krb5_crypto crypto;
938 krb5_us_timeofday (context, &p.patimestamp, &usec);
939 usec2 = usec;
940 p.pausec = &usec2;
942 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
943 if (ret)
944 return ret;
945 if(buf_size != len)
946 krb5_abortx(context, "internal error in ASN.1 encoder");
948 ret = krb5_crypto_init(context, key, 0, &crypto);
949 if (ret) {
950 free(buf);
951 return ret;
953 ret = krb5_encrypt_EncryptedData(context,
954 crypto,
955 KRB5_KU_PA_ENC_TIMESTAMP,
956 buf,
957 len,
959 &encdata);
960 free(buf);
961 krb5_crypto_destroy(context, crypto);
962 if (ret)
963 return ret;
965 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
966 free_EncryptedData(&encdata);
967 if (ret)
968 return ret;
969 if(buf_size != len)
970 krb5_abortx(context, "internal error in ASN.1 encoder");
972 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
973 if (ret)
974 free(buf);
975 return ret;
978 static krb5_error_code
979 add_enc_ts_padata(krb5_context context,
980 METHOD_DATA *md,
981 krb5_principal client,
982 krb5_s2k_proc keyproc,
983 krb5_const_pointer keyseed,
984 krb5_enctype *enctypes,
985 unsigned netypes,
986 krb5_salt *salt,
987 krb5_data *s2kparams)
989 krb5_error_code ret;
990 krb5_salt salt2;
991 krb5_enctype *ep;
992 int i;
994 if(salt == NULL) {
995 /* default to standard salt */
996 ret = krb5_get_pw_salt (context, client, &salt2);
997 if (ret)
998 return ret;
999 salt = &salt2;
1001 if (!enctypes) {
1002 enctypes = context->etypes;
1003 netypes = 0;
1004 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
1005 netypes++;
1008 for (i = 0; i < netypes; ++i) {
1009 krb5_keyblock *key;
1011 _krb5_debug(context, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes[i]);
1013 ret = (*keyproc)(context, enctypes[i], keyseed,
1014 *salt, s2kparams, &key);
1015 if (ret)
1016 continue;
1017 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
1018 krb5_free_keyblock (context, key);
1019 if (ret)
1020 return ret;
1022 if(salt == &salt2)
1023 krb5_free_salt(context, salt2);
1024 return 0;
1027 static krb5_error_code
1028 pa_data_to_md_ts_enc(krb5_context context,
1029 const AS_REQ *a,
1030 const krb5_principal client,
1031 krb5_get_init_creds_ctx *ctx,
1032 struct pa_info_data *ppaid,
1033 METHOD_DATA *md)
1035 if (ctx->keyproc == NULL || ctx->keyseed == NULL)
1036 return 0;
1038 if (ppaid) {
1039 add_enc_ts_padata(context, md, client,
1040 ctx->keyproc, ctx->keyseed,
1041 &ppaid->etype, 1,
1042 &ppaid->salt, ppaid->s2kparams);
1043 } else {
1044 krb5_salt salt;
1046 _krb5_debug(context, 5, "krb5_get_init_creds: pa-info not found, guessing salt");
1048 /* make a v5 salted pa-data */
1049 add_enc_ts_padata(context, md, client,
1050 ctx->keyproc, ctx->keyseed,
1051 a->req_body.etype.val, a->req_body.etype.len,
1052 NULL, NULL);
1054 /* make a v4 salted pa-data */
1055 salt.salttype = KRB5_PW_SALT;
1056 krb5_data_zero(&salt.saltvalue);
1057 add_enc_ts_padata(context, md, client,
1058 ctx->keyproc, ctx->keyseed,
1059 a->req_body.etype.val, a->req_body.etype.len,
1060 &salt, NULL);
1062 return 0;
1065 static krb5_error_code
1066 pa_data_to_key_plain(krb5_context context,
1067 const krb5_principal client,
1068 krb5_get_init_creds_ctx *ctx,
1069 krb5_salt salt,
1070 krb5_data *s2kparams,
1071 krb5_enctype etype,
1072 krb5_keyblock **key)
1074 krb5_error_code ret;
1076 ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
1077 salt, s2kparams, key);
1078 return ret;
1082 static krb5_error_code
1083 pa_data_to_md_pkinit(krb5_context context,
1084 const AS_REQ *a,
1085 const krb5_principal client,
1086 int win2k,
1087 krb5_get_init_creds_ctx *ctx,
1088 METHOD_DATA *md)
1090 if (ctx->pk_init_ctx == NULL)
1091 return 0;
1092 #ifdef PKINIT
1093 return _krb5_pk_mk_padata(context,
1094 ctx->pk_init_ctx,
1095 ctx->ic_flags,
1096 win2k,
1097 &a->req_body,
1098 ctx->pk_nonce,
1099 md);
1100 #else
1101 krb5_set_error_message(context, EINVAL,
1102 N_("no support for PKINIT compiled in", ""));
1103 return EINVAL;
1104 #endif
1107 static krb5_error_code
1108 pa_data_add_pac_request(krb5_context context,
1109 krb5_get_init_creds_ctx *ctx,
1110 METHOD_DATA *md)
1112 size_t len, length;
1113 krb5_error_code ret;
1114 PA_PAC_REQUEST req;
1115 void *buf;
1117 switch (ctx->req_pac) {
1118 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1119 return 0; /* don't bother */
1120 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1121 req.include_pac = 1;
1122 break;
1123 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1124 req.include_pac = 0;
1127 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1128 &req, &len, ret);
1129 if (ret)
1130 return ret;
1131 if(len != length)
1132 krb5_abortx(context, "internal error in ASN.1 encoder");
1134 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1135 if (ret)
1136 free(buf);
1138 return 0;
1142 * Assumes caller always will free `out_md', even on error.
1145 static krb5_error_code
1146 process_pa_data_to_md(krb5_context context,
1147 const krb5_creds *creds,
1148 const AS_REQ *a,
1149 krb5_get_init_creds_ctx *ctx,
1150 METHOD_DATA *in_md,
1151 METHOD_DATA **out_md,
1152 krb5_prompter_fct prompter,
1153 void *prompter_data)
1155 krb5_error_code ret;
1157 ALLOC(*out_md, 1);
1158 if (*out_md == NULL) {
1159 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1160 return ENOMEM;
1162 (*out_md)->len = 0;
1163 (*out_md)->val = NULL;
1165 if (_krb5_have_debug(context, 5)) {
1166 unsigned i;
1167 _krb5_debug(context, 5, "KDC send %d patypes", in_md->len);
1168 for (i = 0; i < in_md->len; i++)
1169 _krb5_debug(context, 5, "KDC send PA-DATA type: %d", in_md->val[i].padata_type);
1173 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1174 * need to expose our password protecting our PKCS12 key.
1177 if (ctx->pk_init_ctx) {
1179 _krb5_debug(context, 5, "krb5_get_init_creds: "
1180 "prepareing PKINIT padata (%s)",
1181 (ctx->used_pa_types & USED_PKINIT_W2K) ? "win2k" : "ietf");
1183 if (ctx->used_pa_types & USED_PKINIT_W2K) {
1184 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1185 "Already tried pkinit, looping");
1186 return KRB5_GET_IN_TKT_LOOP;
1189 ret = pa_data_to_md_pkinit(context, a, creds->client,
1190 (ctx->used_pa_types & USED_PKINIT),
1191 ctx, *out_md);
1192 if (ret)
1193 return ret;
1195 if (ctx->used_pa_types & USED_PKINIT)
1196 ctx->used_pa_types |= USED_PKINIT_W2K;
1197 else
1198 ctx->used_pa_types |= USED_PKINIT;
1200 } else if (in_md->len != 0) {
1201 struct pa_info_data *paid, *ppaid;
1202 unsigned flag;
1204 paid = calloc(1, sizeof(*paid));
1206 paid->etype = ENCTYPE_NULL;
1207 ppaid = process_pa_info(context, creds->client, a, paid, in_md);
1209 if (ppaid)
1210 flag = USED_ENC_TS_INFO;
1211 else
1212 flag = USED_ENC_TS_GUESS;
1214 if (ctx->used_pa_types & flag) {
1215 if (ppaid)
1216 free_paid(context, ppaid);
1217 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1218 "Already tried ENC-TS-%s, looping",
1219 flag == USED_ENC_TS_INFO ? "info" : "guess");
1220 return KRB5_GET_IN_TKT_LOOP;
1223 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1225 ctx->used_pa_types |= flag;
1227 if (ppaid) {
1228 if (ctx->ppaid) {
1229 free_paid(context, ctx->ppaid);
1230 free(ctx->ppaid);
1232 ctx->ppaid = ppaid;
1233 } else
1234 free(paid);
1237 pa_data_add_pac_request(context, ctx, *out_md);
1239 if ((*out_md)->len == 0) {
1240 free(*out_md);
1241 *out_md = NULL;
1244 return 0;
1247 static krb5_error_code
1248 process_pa_data_to_key(krb5_context context,
1249 krb5_get_init_creds_ctx *ctx,
1250 krb5_creds *creds,
1251 AS_REQ *a,
1252 AS_REP *rep,
1253 const krb5_krbhst_info *hi,
1254 krb5_keyblock **key)
1256 struct pa_info_data paid, *ppaid = NULL;
1257 krb5_error_code ret;
1258 krb5_enctype etype;
1259 PA_DATA *pa;
1261 memset(&paid, 0, sizeof(paid));
1263 etype = rep->enc_part.etype;
1265 if (rep->padata) {
1266 paid.etype = etype;
1267 ppaid = process_pa_info(context, creds->client, a, &paid,
1268 rep->padata);
1270 if (ppaid == NULL)
1271 ppaid = ctx->ppaid;
1272 if (ppaid == NULL) {
1273 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1274 if (ret)
1275 return ret;
1276 paid.etype = etype;
1277 paid.s2kparams = NULL;
1278 ppaid = &paid;
1281 pa = NULL;
1282 if (rep->padata) {
1283 int idx = 0;
1284 pa = krb5_find_padata(rep->padata->val,
1285 rep->padata->len,
1286 KRB5_PADATA_PK_AS_REP,
1287 &idx);
1288 if (pa == NULL) {
1289 idx = 0;
1290 pa = krb5_find_padata(rep->padata->val,
1291 rep->padata->len,
1292 KRB5_PADATA_PK_AS_REP_19,
1293 &idx);
1296 if (pa && ctx->pk_init_ctx) {
1297 #ifdef PKINIT
1298 _krb5_debug(context, 5, "krb5_get_init_creds: using PKINIT");
1300 ret = _krb5_pk_rd_pa_reply(context,
1301 a->req_body.realm,
1302 ctx->pk_init_ctx,
1303 etype,
1305 ctx->pk_nonce,
1306 &ctx->req_buffer,
1308 key);
1309 #else
1310 ret = EINVAL;
1311 krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", ""));
1312 #endif
1313 } else if (ctx->keyseed) {
1314 _krb5_debug(context, 5, "krb5_get_init_creds: using keyproc");
1315 ret = pa_data_to_key_plain(context, creds->client, ctx,
1316 ppaid->salt, ppaid->s2kparams, etype, key);
1317 } else {
1318 ret = EINVAL;
1319 krb5_set_error_message(context, ret, N_("No usable pa data type", ""));
1322 free_paid(context, &paid);
1323 return ret;
1327 * Start a new context to get a new initial credential.
1329 * @param context A Kerberos 5 context.
1330 * @param client The Kerberos principal to get the credential for, if
1331 * NULL is given, the default principal is used as determined by
1332 * krb5_get_default_principal().
1333 * @param prompter
1334 * @param prompter_data
1335 * @param start_time the time the ticket should start to be valid or 0 for now.
1336 * @param options a options structure, can be NULL for default options.
1337 * @param rctx A new allocated free with krb5_init_creds_free().
1339 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1341 * @ingroup krb5_credential
1344 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1345 krb5_init_creds_init(krb5_context context,
1346 krb5_principal client,
1347 krb5_prompter_fct prompter,
1348 void *prompter_data,
1349 krb5_deltat start_time,
1350 krb5_get_init_creds_opt *options,
1351 krb5_init_creds_context *rctx)
1353 krb5_init_creds_context ctx;
1354 krb5_error_code ret;
1356 *rctx = NULL;
1358 ctx = calloc(1, sizeof(*ctx));
1359 if (ctx == NULL) {
1360 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1361 return ENOMEM;
1364 ret = get_init_creds_common(context, client, start_time, options, ctx);
1365 if (ret) {
1366 free(ctx);
1367 return ret;
1370 /* Set a new nonce. */
1371 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1372 ctx->nonce &= 0x7fffffff;
1373 /* XXX these just needs to be the same when using Windows PK-INIT */
1374 ctx->pk_nonce = ctx->nonce;
1376 ctx->prompter = prompter;
1377 ctx->prompter_data = prompter_data;
1379 *rctx = ctx;
1381 return ret;
1385 * Sets the service that the is requested. This call is only neede for
1386 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1388 * @param context a Kerberos 5 context.
1389 * @param ctx a krb5_init_creds_context context.
1390 * @param service the service given as a string, for example
1391 * "kadmind/admin". If NULL, the default krbtgt in the clients
1392 * realm is set.
1394 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1395 * @ingroup krb5_credential
1398 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1399 krb5_init_creds_set_service(krb5_context context,
1400 krb5_init_creds_context ctx,
1401 const char *service)
1403 krb5_const_realm client_realm;
1404 krb5_principal principal;
1405 krb5_error_code ret;
1407 client_realm = krb5_principal_get_realm (context, ctx->cred.client);
1409 if (service) {
1410 ret = krb5_parse_name (context, service, &principal);
1411 if (ret)
1412 return ret;
1413 krb5_principal_set_realm (context, principal, client_realm);
1414 } else {
1415 ret = krb5_make_principal(context, &principal,
1416 client_realm, KRB5_TGS_NAME, client_realm,
1417 NULL);
1418 if (ret)
1419 return ret;
1423 * This is for Windows RODC that are picky about what name type
1424 * the server principal have, and the really strange part is that
1425 * they are picky about the AS-REQ name type and not the TGS-REQ
1426 * later. Oh well.
1429 if (krb5_principal_is_krbtgt(context, principal))
1430 krb5_principal_set_type(context, principal, KRB5_NT_SRV_INST);
1432 krb5_free_principal(context, ctx->cred.server);
1433 ctx->cred.server = principal;
1435 return 0;
1439 * Sets the password that will use for the request.
1441 * @param context a Kerberos 5 context.
1442 * @param ctx ctx krb5_init_creds_context context.
1443 * @param password the password to use.
1445 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1446 * @ingroup krb5_credential
1449 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1450 krb5_init_creds_set_password(krb5_context context,
1451 krb5_init_creds_context ctx,
1452 const char *password)
1454 if (ctx->password) {
1455 memset(ctx->password, 0, strlen(ctx->password));
1456 free(ctx->password);
1458 if (password) {
1459 ctx->password = strdup(password);
1460 if (ctx->password == NULL) {
1461 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1462 return ENOMEM;
1464 ctx->keyseed = (void *) ctx->password;
1465 } else {
1466 ctx->keyseed = NULL;
1467 ctx->password = NULL;
1470 return 0;
1473 static krb5_error_code KRB5_CALLCONV
1474 keytab_key_proc(krb5_context context, krb5_enctype enctype,
1475 krb5_const_pointer keyseed,
1476 krb5_salt salt, krb5_data *s2kparms,
1477 krb5_keyblock **key)
1479 krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed);
1480 krb5_keytab keytab = args->keytab;
1481 krb5_principal principal = args->principal;
1482 krb5_error_code ret;
1483 krb5_keytab real_keytab;
1484 krb5_keytab_entry entry;
1486 if(keytab == NULL)
1487 krb5_kt_default(context, &real_keytab);
1488 else
1489 real_keytab = keytab;
1491 ret = krb5_kt_get_entry (context, real_keytab, principal,
1492 0, enctype, &entry);
1494 if (keytab == NULL)
1495 krb5_kt_close (context, real_keytab);
1497 if (ret)
1498 return ret;
1500 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
1501 krb5_kt_free_entry(context, &entry);
1502 return ret;
1507 * Set the keytab to use for authentication.
1509 * @param context a Kerberos 5 context.
1510 * @param ctx ctx krb5_init_creds_context context.
1511 * @param keytab the keytab to read the key from.
1513 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1514 * @ingroup krb5_credential
1517 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1518 krb5_init_creds_set_keytab(krb5_context context,
1519 krb5_init_creds_context ctx,
1520 krb5_keytab keytab)
1522 krb5_keytab_key_proc_args *a;
1523 krb5_keytab_entry entry;
1524 krb5_kt_cursor cursor;
1525 krb5_enctype *etypes = NULL;
1526 krb5_error_code ret;
1527 size_t netypes = 0;
1528 int kvno = 0;
1530 a = malloc(sizeof(*a));
1531 if (a == NULL) {
1532 krb5_set_error_message(context, ENOMEM,
1533 N_("malloc: out of memory", ""));
1534 return ENOMEM;
1537 a->principal = ctx->cred.client;
1538 a->keytab = keytab;
1540 ctx->keytab_data = a;
1541 ctx->keyseed = (void *)a;
1542 ctx->keyproc = keytab_key_proc;
1545 * We need to the KDC what enctypes we support for this keytab,
1546 * esp if the keytab is really a password based entry, then the
1547 * KDC might have more enctypes in the database then what we have
1548 * in the keytab.
1551 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1552 if(ret)
1553 goto out;
1555 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
1556 void *ptr;
1558 if (!krb5_principal_compare(context, entry.principal, ctx->cred.client))
1559 goto next;
1561 /* check if we ahve this kvno already */
1562 if (entry.vno > kvno) {
1563 /* remove old list of etype */
1564 if (etypes)
1565 free(etypes);
1566 etypes = NULL;
1567 netypes = 0;
1568 kvno = entry.vno;
1569 } else if (entry.vno != kvno)
1570 goto next;
1572 /* check if enctype is supported */
1573 if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0)
1574 goto next;
1576 /* add enctype to supported list */
1577 ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2));
1578 if (ptr == NULL)
1579 goto next;
1581 etypes = ptr;
1582 etypes[netypes] = entry.keyblock.keytype;
1583 etypes[netypes + 1] = ETYPE_NULL;
1584 netypes++;
1585 next:
1586 krb5_kt_free_entry(context, &entry);
1588 krb5_kt_end_seq_get(context, keytab, &cursor);
1590 if (etypes) {
1591 if (ctx->etypes)
1592 free(ctx->etypes);
1593 ctx->etypes = etypes;
1596 out:
1597 return 0;
1600 static krb5_error_code KRB5_CALLCONV
1601 keyblock_key_proc(krb5_context context, krb5_enctype enctype,
1602 krb5_const_pointer keyseed,
1603 krb5_salt salt, krb5_data *s2kparms,
1604 krb5_keyblock **key)
1606 return krb5_copy_keyblock (context, keyseed, key);
1609 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1610 krb5_init_creds_set_keyblock(krb5_context context,
1611 krb5_init_creds_context ctx,
1612 krb5_keyblock *keyblock)
1614 ctx->keyseed = (void *)keyblock;
1615 ctx->keyproc = keyblock_key_proc;
1617 return 0;
1621 * The core loop if krb5_get_init_creds() function family. Create the
1622 * packets and have the caller send them off to the KDC.
1624 * If the caller want all work been done for them, use
1625 * krb5_init_creds_get() instead.
1627 * @param context a Kerberos 5 context.
1628 * @param ctx ctx krb5_init_creds_context context.
1629 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
1630 * @param out reply to KDC.
1631 * @param hostinfo KDC address info, first round it can be NULL.
1632 * @param flags status of the round, if
1633 * KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round.
1635 * @return 0 for success, or an Kerberos 5 error code, see
1636 * krb5_get_error_message().
1638 * @ingroup krb5_credential
1641 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1642 krb5_init_creds_step(krb5_context context,
1643 krb5_init_creds_context ctx,
1644 krb5_data *in,
1645 krb5_data *out,
1646 krb5_krbhst_info *hostinfo,
1647 unsigned int *flags)
1649 krb5_error_code ret;
1650 size_t len;
1651 size_t size;
1653 krb5_data_zero(out);
1655 if (ctx->as_req.req_body.cname == NULL) {
1656 ret = init_as_req(context, ctx->flags, &ctx->cred,
1657 ctx->addrs, ctx->etypes, &ctx->as_req);
1658 if (ret) {
1659 free_init_creds_ctx(context, ctx);
1660 return ret;
1664 #define MAX_PA_COUNTER 10
1665 if (ctx->pa_counter > MAX_PA_COUNTER) {
1666 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1667 N_("Looping %d times while getting "
1668 "initial credentials", ""),
1669 ctx->pa_counter);
1670 return KRB5_GET_IN_TKT_LOOP;
1672 ctx->pa_counter++;
1674 _krb5_debug(context, 5, "krb5_get_init_creds: loop %d", ctx->pa_counter);
1676 /* Lets process the input packet */
1677 if (in && in->length) {
1678 krb5_kdc_rep rep;
1680 memset(&rep, 0, sizeof(rep));
1682 _krb5_debug(context, 5, "krb5_get_init_creds: processing input");
1684 ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
1685 if (ret == 0) {
1686 krb5_keyblock *key = NULL;
1687 unsigned eflags = EXTRACT_TICKET_AS_REQ | EXTRACT_TICKET_TIMESYNC;
1689 if (ctx->flags.canonicalize) {
1690 eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1691 eflags |= EXTRACT_TICKET_MATCH_REALM;
1693 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1694 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1696 ret = process_pa_data_to_key(context, ctx, &ctx->cred,
1697 &ctx->as_req, &rep.kdc_rep, hostinfo, &key);
1698 if (ret) {
1699 free_AS_REP(&rep.kdc_rep);
1700 goto out;
1703 _krb5_debug(context, 5, "krb5_get_init_creds: extracting ticket");
1705 ret = _krb5_extract_ticket(context,
1706 &rep,
1707 &ctx->cred,
1708 key,
1709 NULL,
1710 KRB5_KU_AS_REP_ENC_PART,
1711 NULL,
1712 ctx->nonce,
1713 eflags,
1714 NULL,
1715 NULL);
1716 krb5_free_keyblock(context, key);
1718 *flags = 0;
1720 if (ret == 0)
1721 ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
1723 free_AS_REP(&rep.kdc_rep);
1724 free_EncASRepPart(&rep.enc_part);
1726 return ret;
1728 } else {
1729 /* let's try to parse it as a KRB-ERROR */
1731 _krb5_debug(context, 5, "krb5_get_init_creds: got an error");
1733 free_KRB_ERROR(&ctx->error);
1735 ret = krb5_rd_error(context, in, &ctx->error);
1736 if(ret && in->length && ((char*)in->data)[0] == 4)
1737 ret = KRB5KRB_AP_ERR_V4_REPLY;
1738 if (ret) {
1739 _krb5_debug(context, 5, "krb5_get_init_creds: failed to read error");
1740 goto out;
1743 ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);
1745 _krb5_debug(context, 5, "krb5_get_init_creds: KRB-ERROR %d", ret);
1748 * If no preauth was set and KDC requires it, give it one
1749 * more try.
1752 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1754 free_METHOD_DATA(&ctx->md);
1755 memset(&ctx->md, 0, sizeof(ctx->md));
1757 if (ctx->error.e_data) {
1758 ret = decode_METHOD_DATA(ctx->error.e_data->data,
1759 ctx->error.e_data->length,
1760 &ctx->md,
1761 NULL);
1762 if (ret)
1763 krb5_set_error_message(context, ret,
1764 N_("Failed to decode METHOD-DATA", ""));
1765 } else {
1766 krb5_set_error_message(context, ret,
1767 N_("Preauth required but no preauth "
1768 "options send by KDC", ""));
1770 } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
1772 * Try adapt to timeskrew when we are using pre-auth, and
1773 * if there was a time skew, try again.
1775 krb5_set_real_time(context, ctx->error.stime, -1);
1776 if (context->kdc_sec_offset)
1777 ret = 0;
1779 _krb5_debug(context, 10, "init_creds: err skew updateing kdc offset to %d",
1780 context->kdc_sec_offset);
1782 ctx->used_pa_types = 0;
1784 } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
1785 /* client referal to a new realm */
1787 if (ctx->error.crealm == NULL) {
1788 krb5_set_error_message(context, ret,
1789 N_("Got a client referral, not but no realm", ""));
1790 goto out;
1792 _krb5_debug(context, 5,
1793 "krb5_get_init_creds: got referal to realm %s",
1794 *ctx->error.crealm);
1796 ret = krb5_principal_set_realm(context,
1797 ctx->cred.client,
1798 *ctx->error.crealm);
1800 ctx->used_pa_types = 0;
1802 if (ret)
1803 goto out;
1807 if (ctx->as_req.padata) {
1808 free_METHOD_DATA(ctx->as_req.padata);
1809 free(ctx->as_req.padata);
1810 ctx->as_req.padata = NULL;
1813 /* Set a new nonce. */
1814 ctx->as_req.req_body.nonce = ctx->nonce;
1816 /* fill_in_md_data */
1817 ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
1818 &ctx->md, &ctx->as_req.padata,
1819 ctx->prompter, ctx->prompter_data);
1820 if (ret)
1821 goto out;
1823 krb5_data_free(&ctx->req_buffer);
1825 ASN1_MALLOC_ENCODE(AS_REQ,
1826 ctx->req_buffer.data, ctx->req_buffer.length,
1827 &ctx->as_req, &len, ret);
1828 if (ret)
1829 goto out;
1830 if(len != ctx->req_buffer.length)
1831 krb5_abortx(context, "internal error in ASN.1 encoder");
1833 out->data = ctx->req_buffer.data;
1834 out->length = ctx->req_buffer.length;
1836 *flags = KRB5_INIT_CREDS_STEP_FLAG_CONTINUE;
1838 return 0;
1839 out:
1840 return ret;
1844 * Extract the newly acquired credentials from krb5_init_creds_context
1845 * context.
1847 * @param context A Kerberos 5 context.
1848 * @param ctx
1849 * @param cred credentials, free with krb5_free_cred_contents().
1851 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
1854 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1855 krb5_init_creds_get_creds(krb5_context context,
1856 krb5_init_creds_context ctx,
1857 krb5_creds *cred)
1859 return krb5_copy_creds_contents(context, &ctx->cred, cred);
1863 * Get the last error from the transaction.
1865 * @return Returns 0 or an error code
1867 * @ingroup krb5_credential
1870 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1871 krb5_init_creds_get_error(krb5_context context,
1872 krb5_init_creds_context ctx,
1873 KRB_ERROR *error)
1875 krb5_error_code ret;
1877 ret = copy_KRB_ERROR(&ctx->error, error);
1878 if (ret)
1879 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1881 return ret;
1885 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
1887 * @param context A Kerberos 5 context.
1888 * @param ctx The krb5_init_creds_context to free.
1890 * @ingroup krb5_credential
1893 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1894 krb5_init_creds_free(krb5_context context,
1895 krb5_init_creds_context ctx)
1897 free_init_creds_ctx(context, ctx);
1898 free(ctx);
1902 * Get new credentials as setup by the krb5_init_creds_context.
1904 * @param context A Kerberos 5 context.
1905 * @param ctx The krb5_init_creds_context to process.
1907 * @ingroup krb5_credential
1910 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1911 krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
1913 krb5_sendto_ctx stctx = NULL;
1914 krb5_krbhst_info *hostinfo = NULL;
1915 krb5_error_code ret;
1916 krb5_data in, out;
1917 unsigned int flags = 0;
1919 krb5_data_zero(&in);
1920 krb5_data_zero(&out);
1922 ret = krb5_sendto_ctx_alloc(context, &stctx);
1923 if (ret)
1924 goto out;
1925 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1927 while (1) {
1928 flags = 0;
1929 ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
1930 krb5_data_free(&in);
1931 if (ret)
1932 goto out;
1934 if ((flags & 1) == 0)
1935 break;
1937 ret = krb5_sendto_context (context, stctx, &out,
1938 ctx->cred.client->realm, &in);
1939 if (ret)
1940 goto out;
1944 out:
1945 if (stctx)
1946 krb5_sendto_ctx_free(context, stctx);
1948 return ret;
1952 * Get new credentials using password.
1954 * @ingroup krb5_credential
1958 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1959 krb5_get_init_creds_password(krb5_context context,
1960 krb5_creds *creds,
1961 krb5_principal client,
1962 const char *password,
1963 krb5_prompter_fct prompter,
1964 void *data,
1965 krb5_deltat start_time,
1966 const char *in_tkt_service,
1967 krb5_get_init_creds_opt *options)
1969 krb5_init_creds_context ctx;
1970 char buf[BUFSIZ];
1971 krb5_error_code ret;
1972 int chpw = 0;
1974 again:
1975 ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
1976 if (ret)
1977 goto out;
1979 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1980 if (ret)
1981 goto out;
1983 if (prompter != NULL && ctx->password == NULL && password == NULL) {
1984 krb5_prompt prompt;
1985 krb5_data password_data;
1986 char *p, *q;
1988 krb5_unparse_name (context, client, &p);
1989 asprintf (&q, "%s's Password: ", p);
1990 free (p);
1991 prompt.prompt = q;
1992 password_data.data = buf;
1993 password_data.length = sizeof(buf);
1994 prompt.hidden = 1;
1995 prompt.reply = &password_data;
1996 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1998 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1999 free (q);
2000 if (ret) {
2001 memset (buf, 0, sizeof(buf));
2002 ret = KRB5_LIBOS_PWDINTR;
2003 krb5_clear_error_message (context);
2004 goto out;
2006 password = password_data.data;
2009 if (password) {
2010 ret = krb5_init_creds_set_password(context, ctx, password);
2011 if (ret)
2012 goto out;
2015 ret = krb5_init_creds_get(context, ctx);
2017 if (ret == 0)
2018 process_last_request(context, options, ctx);
2021 if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
2022 char buf2[1024];
2024 /* try to avoid recursion */
2025 if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
2026 goto out;
2028 /* don't try to change password where then where none */
2029 if (prompter == NULL)
2030 goto out;
2032 ret = change_password (context,
2033 client,
2034 ctx->password,
2035 buf2,
2036 sizeof(buf),
2037 prompter,
2038 data,
2039 options);
2040 if (ret)
2041 goto out;
2042 chpw = 1;
2043 krb5_init_creds_free(context, ctx);
2044 goto again;
2047 out:
2048 if (ret == 0)
2049 krb5_init_creds_get_creds(context, ctx, creds);
2051 if (ctx)
2052 krb5_init_creds_free(context, ctx);
2054 memset(buf, 0, sizeof(buf));
2055 return ret;
2059 * Get new credentials using keyblock.
2061 * @ingroup krb5_credential
2064 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2065 krb5_get_init_creds_keyblock(krb5_context context,
2066 krb5_creds *creds,
2067 krb5_principal client,
2068 krb5_keyblock *keyblock,
2069 krb5_deltat start_time,
2070 const char *in_tkt_service,
2071 krb5_get_init_creds_opt *options)
2073 krb5_init_creds_context ctx;
2074 krb5_error_code ret;
2076 memset(creds, 0, sizeof(*creds));
2078 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2079 if (ret)
2080 goto out;
2082 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2083 if (ret)
2084 goto out;
2086 ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
2087 if (ret)
2088 goto out;
2090 ret = krb5_init_creds_get(context, ctx);
2092 if (ret == 0)
2093 process_last_request(context, options, ctx);
2095 out:
2096 if (ret == 0)
2097 krb5_init_creds_get_creds(context, ctx, creds);
2099 if (ctx)
2100 krb5_init_creds_free(context, ctx);
2102 return ret;
2106 * Get new credentials using keytab.
2108 * @ingroup krb5_credential
2111 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2112 krb5_get_init_creds_keytab(krb5_context context,
2113 krb5_creds *creds,
2114 krb5_principal client,
2115 krb5_keytab keytab,
2116 krb5_deltat start_time,
2117 const char *in_tkt_service,
2118 krb5_get_init_creds_opt *options)
2120 krb5_init_creds_context ctx;
2121 krb5_error_code ret;
2123 memset(creds, 0, sizeof(*creds));
2125 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2126 if (ret)
2127 goto out;
2129 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2130 if (ret)
2131 goto out;
2133 ret = krb5_init_creds_set_keytab(context, ctx, keytab);
2134 if (ret)
2135 goto out;
2137 ret = krb5_init_creds_get(context, ctx);
2138 if (ret == 0)
2139 process_last_request(context, options, ctx);
2141 out:
2142 if (ret == 0)
2143 krb5_init_creds_get_creds(context, ctx, creds);
2145 if (ctx)
2146 krb5_init_creds_free(context, ctx);
2148 return ret;