insert _FLAG into the name
[heimdal.git] / lib / krb5 / init_creds_pw.c
blobec954da0c680745adef89eb0de882e75ff58145f
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
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;
239 asprintf (&p, "%s%s", str, ctime(&now));
240 (*prompter) (context, data, NULL, p, 0, NULL);
241 free (p);
245 * Check the context, and in the case there is a expiration warning,
246 * use the prompter to print the warning.
248 * @param context A Kerberos 5 context.
249 * @param options An GIC options structure
250 * @param ctx The krb5_init_creds_context check for expiration.
253 static krb5_error_code
254 process_last_request(krb5_context context,
255 krb5_get_init_creds_opt *options,
256 krb5_init_creds_context ctx)
258 krb5_const_realm realm;
259 LastReq *lr;
260 krb5_boolean reported = FALSE;
261 krb5_timestamp sec;
262 time_t t;
263 size_t i;
266 * First check if there is a API consumer.
269 realm = krb5_principal_get_realm (context, ctx->cred.client);
270 lr = &ctx->enc_part.last_req;
272 if (options && options->opt_private && options->opt_private->lr.func) {
273 krb5_last_req_entry **lre;
275 lre = calloc(lr->len + 1, sizeof(**lre));
276 if (lre == NULL) {
277 krb5_set_error_message(context, ENOMEM,
278 N_("malloc: out of memory", ""));
279 return ENOMEM;
281 for (i = 0; i < lr->len; i++) {
282 lre[i] = calloc(1, sizeof(*lre[i]));
283 if (lre[i] == NULL)
284 break;
285 lre[i]->lr_type = lr->val[i].lr_type;
286 lre[i]->value = lr->val[i].lr_value;
289 (*options->opt_private->lr.func)(context, lre,
290 options->opt_private->lr.ctx);
292 for (i = 0; i < lr->len; i++)
293 free(lre[i]);
294 free(lre);
298 * Now check if we should prompt the user
301 if (ctx->prompter == NULL)
302 return 0;
304 krb5_timeofday (context, &sec);
306 t = sec + get_config_time (context,
307 realm,
308 "warn_pwexpire",
309 7 * 24 * 60 * 60);
311 for (i = 0; i < lr->len; ++i) {
312 if (lr->val[i].lr_value <= t) {
313 switch (abs(lr->val[i].lr_type)) {
314 case LR_PW_EXPTIME :
315 report_expiration(context, ctx->prompter,
316 ctx->prompter_data,
317 "Your password will expire at ",
318 lr->val[i].lr_value);
319 reported = TRUE;
320 break;
321 case LR_ACCT_EXPTIME :
322 report_expiration(context, ctx->prompter,
323 ctx->prompter_data,
324 "Your account will expire at ",
325 lr->val[i].lr_value);
326 reported = TRUE;
327 break;
332 if (!reported
333 && ctx->enc_part.key_expiration
334 && *ctx->enc_part.key_expiration <= t) {
335 report_expiration(context, ctx->prompter,
336 ctx->prompter_data,
337 "Your password/account will expire at ",
338 *ctx->enc_part.key_expiration);
340 return 0;
343 static krb5_addresses no_addrs = { 0, NULL };
345 static krb5_error_code
346 get_init_creds_common(krb5_context context,
347 krb5_principal client,
348 krb5_deltat start_time,
349 krb5_get_init_creds_opt *options,
350 krb5_init_creds_context ctx)
352 krb5_get_init_creds_opt *default_opt = NULL;
353 krb5_error_code ret;
354 krb5_enctype *etypes;
355 krb5_preauthtype *pre_auth_types;
357 memset(ctx, 0, sizeof(*ctx));
359 if (options == NULL) {
360 const char *realm = krb5_principal_get_realm(context, client);
362 krb5_get_init_creds_opt_alloc (context, &default_opt);
363 options = default_opt;
364 krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options);
367 if (options->opt_private) {
368 if (options->opt_private->password) {
369 ret = krb5_init_creds_set_password(context, ctx,
370 options->opt_private->password);
371 if (ret)
372 goto out;
375 ctx->keyproc = options->opt_private->key_proc;
376 ctx->req_pac = options->opt_private->req_pac;
377 ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
378 ctx->ic_flags = options->opt_private->flags;
379 } else
380 ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;
382 if (ctx->keyproc == NULL)
383 ctx->keyproc = default_s2k_func;
385 /* Enterprise name implicitly turns on canonicalize */
386 if ((ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE) ||
387 krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL)
388 ctx->flags.canonicalize = 1;
390 ctx->pre_auth_types = NULL;
391 ctx->addrs = NULL;
392 ctx->etypes = NULL;
393 ctx->pre_auth_types = NULL;
395 ret = init_cred(context, &ctx->cred, client, start_time, options);
396 if (ret) {
397 if (default_opt)
398 krb5_get_init_creds_opt_free(context, default_opt);
399 return ret;
402 ret = krb5_init_creds_set_service(context, ctx, NULL);
403 if (ret)
404 goto out;
406 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
407 ctx->flags.forwardable = options->forwardable;
409 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
410 ctx->flags.proxiable = options->proxiable;
412 if (start_time)
413 ctx->flags.postdated = 1;
414 if (ctx->cred.times.renew_till)
415 ctx->flags.renewable = 1;
416 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
417 ctx->addrs = options->address_list;
418 } else if (options->opt_private) {
419 switch (options->opt_private->addressless) {
420 case KRB5_INIT_CREDS_TRISTATE_UNSET:
421 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
422 ctx->addrs = &no_addrs;
423 #else
424 ctx->addrs = NULL;
425 #endif
426 break;
427 case KRB5_INIT_CREDS_TRISTATE_FALSE:
428 ctx->addrs = NULL;
429 break;
430 case KRB5_INIT_CREDS_TRISTATE_TRUE:
431 ctx->addrs = &no_addrs;
432 break;
435 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
436 if (ctx->etypes)
437 free(ctx->etypes);
439 etypes = malloc((options->etype_list_length + 1)
440 * sizeof(krb5_enctype));
441 if (etypes == NULL) {
442 ret = ENOMEM;
443 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
444 goto out;
446 memcpy (etypes, options->etype_list,
447 options->etype_list_length * sizeof(krb5_enctype));
448 etypes[options->etype_list_length] = ETYPE_NULL;
449 ctx->etypes = etypes;
451 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
452 pre_auth_types = malloc((options->preauth_list_length + 1)
453 * sizeof(krb5_preauthtype));
454 if (pre_auth_types == NULL) {
455 ret = ENOMEM;
456 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
457 goto out;
459 memcpy (pre_auth_types, options->preauth_list,
460 options->preauth_list_length * sizeof(krb5_preauthtype));
461 pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
462 ctx->pre_auth_types = pre_auth_types;
464 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
465 ctx->flags.request_anonymous = options->anonymous;
466 if (default_opt)
467 krb5_get_init_creds_opt_free(context, default_opt);
468 return 0;
469 out:
470 if (default_opt)
471 krb5_get_init_creds_opt_free(context, default_opt);
472 return ret;
475 static krb5_error_code
476 change_password (krb5_context context,
477 krb5_principal client,
478 const char *password,
479 char *newpw,
480 size_t newpw_sz,
481 krb5_prompter_fct prompter,
482 void *data,
483 krb5_get_init_creds_opt *old_options)
485 krb5_prompt prompts[2];
486 krb5_error_code ret;
487 krb5_creds cpw_cred;
488 char buf1[BUFSIZ], buf2[BUFSIZ];
489 krb5_data password_data[2];
490 int result_code;
491 krb5_data result_code_string;
492 krb5_data result_string;
493 char *p;
494 krb5_get_init_creds_opt *options;
496 memset (&cpw_cred, 0, sizeof(cpw_cred));
498 ret = krb5_get_init_creds_opt_alloc(context, &options);
499 if (ret)
500 return ret;
501 krb5_get_init_creds_opt_set_tkt_life (options, 60);
502 krb5_get_init_creds_opt_set_forwardable (options, FALSE);
503 krb5_get_init_creds_opt_set_proxiable (options, FALSE);
504 if (old_options && old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
505 krb5_get_init_creds_opt_set_preauth_list (options,
506 old_options->preauth_list,
507 old_options->preauth_list_length);
509 krb5_data_zero (&result_code_string);
510 krb5_data_zero (&result_string);
512 ret = krb5_get_init_creds_password (context,
513 &cpw_cred,
514 client,
515 password,
516 prompter,
517 data,
519 "kadmin/changepw",
520 options);
521 krb5_get_init_creds_opt_free(context, options);
522 if (ret)
523 goto out;
525 for(;;) {
526 password_data[0].data = buf1;
527 password_data[0].length = sizeof(buf1);
529 prompts[0].hidden = 1;
530 prompts[0].prompt = "New password: ";
531 prompts[0].reply = &password_data[0];
532 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
534 password_data[1].data = buf2;
535 password_data[1].length = sizeof(buf2);
537 prompts[1].hidden = 1;
538 prompts[1].prompt = "Repeat new password: ";
539 prompts[1].reply = &password_data[1];
540 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
542 ret = (*prompter) (context, data, NULL, "Changing password",
543 2, prompts);
544 if (ret) {
545 memset (buf1, 0, sizeof(buf1));
546 memset (buf2, 0, sizeof(buf2));
547 goto out;
550 if (strcmp (buf1, buf2) == 0)
551 break;
552 memset (buf1, 0, sizeof(buf1));
553 memset (buf2, 0, sizeof(buf2));
556 ret = krb5_set_password (context,
557 &cpw_cred,
558 buf1,
559 client,
560 &result_code,
561 &result_code_string,
562 &result_string);
563 if (ret)
564 goto out;
565 asprintf (&p, "%s: %.*s\n",
566 result_code ? "Error" : "Success",
567 (int)result_string.length,
568 result_string.length > 0 ? (char*)result_string.data : "");
570 /* return the result */
571 (*prompter) (context, data, NULL, p, 0, NULL);
573 free (p);
574 if (result_code == 0) {
575 strlcpy (newpw, buf1, newpw_sz);
576 ret = 0;
577 } else {
578 ret = ENOTTY;
579 krb5_set_error_message(context, ret,
580 N_("failed changing password", ""));
583 out:
584 memset (buf1, 0, sizeof(buf1));
585 memset (buf2, 0, sizeof(buf2));
586 krb5_data_free (&result_string);
587 krb5_data_free (&result_code_string);
588 krb5_free_cred_contents (context, &cpw_cred);
589 return ret;
593 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
594 krb5_keyblock_key_proc (krb5_context context,
595 krb5_keytype type,
596 krb5_data *salt,
597 krb5_const_pointer keyseed,
598 krb5_keyblock **key)
600 return krb5_copy_keyblock (context, keyseed, key);
607 static krb5_error_code
608 init_as_req (krb5_context context,
609 KDCOptions opts,
610 const krb5_creds *creds,
611 const krb5_addresses *addrs,
612 const krb5_enctype *etypes,
613 AS_REQ *a)
615 krb5_error_code ret;
617 memset(a, 0, sizeof(*a));
619 a->pvno = 5;
620 a->msg_type = krb_as_req;
621 a->req_body.kdc_options = opts;
622 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
623 if (a->req_body.cname == NULL) {
624 ret = ENOMEM;
625 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
626 goto fail;
628 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
629 if (a->req_body.sname == NULL) {
630 ret = ENOMEM;
631 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
632 goto fail;
635 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
636 if (ret)
637 goto fail;
638 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
639 if (ret)
640 goto fail;
642 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
643 if (ret)
644 goto fail;
646 if(creds->times.starttime) {
647 a->req_body.from = malloc(sizeof(*a->req_body.from));
648 if (a->req_body.from == NULL) {
649 ret = ENOMEM;
650 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
651 goto fail;
653 *a->req_body.from = creds->times.starttime;
655 if(creds->times.endtime){
656 ALLOC(a->req_body.till, 1);
657 *a->req_body.till = creds->times.endtime;
659 if(creds->times.renew_till){
660 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
661 if (a->req_body.rtime == NULL) {
662 ret = ENOMEM;
663 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
664 goto fail;
666 *a->req_body.rtime = creds->times.renew_till;
668 a->req_body.nonce = 0;
669 ret = krb5_init_etype (context,
670 &a->req_body.etype.len,
671 &a->req_body.etype.val,
672 etypes);
673 if (ret)
674 goto fail;
677 * This means no addresses
680 if (addrs && addrs->len == 0) {
681 a->req_body.addresses = NULL;
682 } else {
683 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
684 if (a->req_body.addresses == NULL) {
685 ret = ENOMEM;
686 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
687 goto fail;
690 if (addrs)
691 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
692 else {
693 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
694 if(ret == 0 && a->req_body.addresses->len == 0) {
695 free(a->req_body.addresses);
696 a->req_body.addresses = NULL;
699 if (ret)
700 goto fail;
703 a->req_body.enc_authorization_data = NULL;
704 a->req_body.additional_tickets = NULL;
706 a->padata = NULL;
708 return 0;
709 fail:
710 free_AS_REQ(a);
711 memset(a, 0, sizeof(*a));
712 return ret;
716 static krb5_error_code
717 set_paid(struct pa_info_data *paid, krb5_context context,
718 krb5_enctype etype,
719 krb5_salttype salttype, void *salt_string, size_t salt_len,
720 krb5_data *s2kparams)
722 paid->etype = etype;
723 paid->salt.salttype = salttype;
724 paid->salt.saltvalue.data = malloc(salt_len + 1);
725 if (paid->salt.saltvalue.data == NULL) {
726 krb5_clear_error_message(context);
727 return ENOMEM;
729 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
730 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
731 paid->salt.saltvalue.length = salt_len;
732 if (s2kparams) {
733 krb5_error_code ret;
735 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
736 if (ret) {
737 krb5_clear_error_message(context);
738 krb5_free_salt(context, paid->salt);
739 return ret;
741 } else
742 paid->s2kparams = NULL;
744 return 0;
747 static struct pa_info_data *
748 pa_etype_info2(krb5_context context,
749 const krb5_principal client,
750 const AS_REQ *asreq,
751 struct pa_info_data *paid,
752 heim_octet_string *data)
754 krb5_error_code ret;
755 ETYPE_INFO2 e;
756 size_t sz;
757 int i, j;
759 memset(&e, 0, sizeof(e));
760 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
761 if (ret)
762 goto out;
763 if (e.len == 0)
764 goto out;
765 for (j = 0; j < asreq->req_body.etype.len; j++) {
766 for (i = 0; i < e.len; i++) {
767 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
768 krb5_salt salt;
769 if (e.val[i].salt == NULL)
770 ret = krb5_get_pw_salt(context, client, &salt);
771 else {
772 salt.saltvalue.data = *e.val[i].salt;
773 salt.saltvalue.length = strlen(*e.val[i].salt);
774 ret = 0;
776 if (ret == 0)
777 ret = set_paid(paid, context, e.val[i].etype,
778 KRB5_PW_SALT,
779 salt.saltvalue.data,
780 salt.saltvalue.length,
781 e.val[i].s2kparams);
782 if (e.val[i].salt == NULL)
783 krb5_free_salt(context, salt);
784 if (ret == 0) {
785 free_ETYPE_INFO2(&e);
786 return paid;
791 out:
792 free_ETYPE_INFO2(&e);
793 return NULL;
796 static struct pa_info_data *
797 pa_etype_info(krb5_context context,
798 const krb5_principal client,
799 const AS_REQ *asreq,
800 struct pa_info_data *paid,
801 heim_octet_string *data)
803 krb5_error_code ret;
804 ETYPE_INFO e;
805 size_t sz;
806 int i, j;
808 memset(&e, 0, sizeof(e));
809 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
810 if (ret)
811 goto out;
812 if (e.len == 0)
813 goto out;
814 for (j = 0; j < asreq->req_body.etype.len; j++) {
815 for (i = 0; i < e.len; i++) {
816 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
817 krb5_salt salt;
818 salt.salttype = KRB5_PW_SALT;
819 if (e.val[i].salt == NULL)
820 ret = krb5_get_pw_salt(context, client, &salt);
821 else {
822 salt.saltvalue = *e.val[i].salt;
823 ret = 0;
825 if (e.val[i].salttype)
826 salt.salttype = *e.val[i].salttype;
827 if (ret == 0) {
828 ret = set_paid(paid, context, e.val[i].etype,
829 salt.salttype,
830 salt.saltvalue.data,
831 salt.saltvalue.length,
832 NULL);
833 if (e.val[i].salt == NULL)
834 krb5_free_salt(context, salt);
836 if (ret == 0) {
837 free_ETYPE_INFO(&e);
838 return paid;
843 out:
844 free_ETYPE_INFO(&e);
845 return NULL;
848 static struct pa_info_data *
849 pa_pw_or_afs3_salt(krb5_context context,
850 const krb5_principal client,
851 const AS_REQ *asreq,
852 struct pa_info_data *paid,
853 heim_octet_string *data)
855 krb5_error_code ret;
856 if (paid->etype == ENCTYPE_NULL)
857 return NULL;
858 ret = set_paid(paid, context,
859 paid->etype,
860 paid->salt.salttype,
861 data->data,
862 data->length,
863 NULL);
864 if (ret)
865 return NULL;
866 return paid;
870 struct pa_info {
871 krb5_preauthtype type;
872 struct pa_info_data *(*salt_info)(krb5_context,
873 const krb5_principal,
874 const AS_REQ *,
875 struct pa_info_data *,
876 heim_octet_string *);
879 static struct pa_info pa_prefs[] = {
880 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
881 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
882 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
883 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
886 static PA_DATA *
887 find_pa_data(const METHOD_DATA *md, int type)
889 int i;
890 if (md == NULL)
891 return NULL;
892 for (i = 0; i < md->len; i++)
893 if (md->val[i].padata_type == type)
894 return &md->val[i];
895 return NULL;
898 static struct pa_info_data *
899 process_pa_info(krb5_context context,
900 const krb5_principal client,
901 const AS_REQ *asreq,
902 struct pa_info_data *paid,
903 METHOD_DATA *md)
905 struct pa_info_data *p = NULL;
906 int i;
908 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
909 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
910 if (pa == NULL)
911 continue;
912 paid->salt.salttype = pa_prefs[i].type;
913 p = (*pa_prefs[i].salt_info)(context, client, asreq,
914 paid, &pa->padata_value);
916 return p;
919 static krb5_error_code
920 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
921 krb5_enctype etype, krb5_keyblock *key)
923 PA_ENC_TS_ENC p;
924 unsigned char *buf;
925 size_t buf_size;
926 size_t len;
927 EncryptedData encdata;
928 krb5_error_code ret;
929 int32_t usec;
930 int usec2;
931 krb5_crypto crypto;
933 krb5_us_timeofday (context, &p.patimestamp, &usec);
934 usec2 = usec;
935 p.pausec = &usec2;
937 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
938 if (ret)
939 return ret;
940 if(buf_size != len)
941 krb5_abortx(context, "internal error in ASN.1 encoder");
943 ret = krb5_crypto_init(context, key, 0, &crypto);
944 if (ret) {
945 free(buf);
946 return ret;
948 ret = krb5_encrypt_EncryptedData(context,
949 crypto,
950 KRB5_KU_PA_ENC_TIMESTAMP,
951 buf,
952 len,
954 &encdata);
955 free(buf);
956 krb5_crypto_destroy(context, crypto);
957 if (ret)
958 return ret;
960 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
961 free_EncryptedData(&encdata);
962 if (ret)
963 return ret;
964 if(buf_size != len)
965 krb5_abortx(context, "internal error in ASN.1 encoder");
967 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
968 if (ret)
969 free(buf);
970 return ret;
973 static krb5_error_code
974 add_enc_ts_padata(krb5_context context,
975 METHOD_DATA *md,
976 krb5_principal client,
977 krb5_s2k_proc keyproc,
978 krb5_const_pointer keyseed,
979 krb5_enctype *enctypes,
980 unsigned netypes,
981 krb5_salt *salt,
982 krb5_data *s2kparams)
984 krb5_error_code ret;
985 krb5_salt salt2;
986 krb5_enctype *ep;
987 int i;
989 if(salt == NULL) {
990 /* default to standard salt */
991 ret = krb5_get_pw_salt (context, client, &salt2);
992 if (ret)
993 return ret;
994 salt = &salt2;
996 if (!enctypes) {
997 enctypes = context->etypes;
998 netypes = 0;
999 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
1000 netypes++;
1003 for (i = 0; i < netypes; ++i) {
1004 krb5_keyblock *key;
1006 _krb5_debug(context, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes[i]);
1008 ret = (*keyproc)(context, enctypes[i], keyseed,
1009 *salt, s2kparams, &key);
1010 if (ret)
1011 continue;
1012 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
1013 krb5_free_keyblock (context, key);
1014 if (ret)
1015 return ret;
1017 if(salt == &salt2)
1018 krb5_free_salt(context, salt2);
1019 return 0;
1022 static krb5_error_code
1023 pa_data_to_md_ts_enc(krb5_context context,
1024 const AS_REQ *a,
1025 const krb5_principal client,
1026 krb5_get_init_creds_ctx *ctx,
1027 struct pa_info_data *ppaid,
1028 METHOD_DATA *md)
1030 if (ctx->keyproc == NULL || ctx->keyseed == NULL)
1031 return 0;
1033 if (ppaid) {
1034 add_enc_ts_padata(context, md, client,
1035 ctx->keyproc, ctx->keyseed,
1036 &ppaid->etype, 1,
1037 &ppaid->salt, ppaid->s2kparams);
1038 } else {
1039 krb5_salt salt;
1041 _krb5_debug(context, 5, "krb5_get_init_creds: pa-info not found, guessing salt");
1043 /* make a v5 salted pa-data */
1044 add_enc_ts_padata(context, md, client,
1045 ctx->keyproc, ctx->keyseed,
1046 a->req_body.etype.val, a->req_body.etype.len,
1047 NULL, NULL);
1049 /* make a v4 salted pa-data */
1050 salt.salttype = KRB5_PW_SALT;
1051 krb5_data_zero(&salt.saltvalue);
1052 add_enc_ts_padata(context, md, client,
1053 ctx->keyproc, ctx->keyseed,
1054 a->req_body.etype.val, a->req_body.etype.len,
1055 &salt, NULL);
1057 return 0;
1060 static krb5_error_code
1061 pa_data_to_key_plain(krb5_context context,
1062 const krb5_principal client,
1063 krb5_get_init_creds_ctx *ctx,
1064 krb5_salt salt,
1065 krb5_data *s2kparams,
1066 krb5_enctype etype,
1067 krb5_keyblock **key)
1069 krb5_error_code ret;
1071 ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
1072 salt, s2kparams, key);
1073 return ret;
1077 static krb5_error_code
1078 pa_data_to_md_pkinit(krb5_context context,
1079 const AS_REQ *a,
1080 const krb5_principal client,
1081 int win2k,
1082 krb5_get_init_creds_ctx *ctx,
1083 METHOD_DATA *md)
1085 if (ctx->pk_init_ctx == NULL)
1086 return 0;
1087 #ifdef PKINIT
1088 return _krb5_pk_mk_padata(context,
1089 ctx->pk_init_ctx,
1090 ctx->ic_flags,
1091 win2k,
1092 &a->req_body,
1093 ctx->pk_nonce,
1094 md);
1095 #else
1096 krb5_set_error_message(context, EINVAL,
1097 N_("no support for PKINIT compiled in", ""));
1098 return EINVAL;
1099 #endif
1102 static krb5_error_code
1103 pa_data_add_pac_request(krb5_context context,
1104 krb5_get_init_creds_ctx *ctx,
1105 METHOD_DATA *md)
1107 size_t len, length;
1108 krb5_error_code ret;
1109 PA_PAC_REQUEST req;
1110 void *buf;
1112 switch (ctx->req_pac) {
1113 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1114 return 0; /* don't bother */
1115 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1116 req.include_pac = 1;
1117 break;
1118 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1119 req.include_pac = 0;
1122 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1123 &req, &len, ret);
1124 if (ret)
1125 return ret;
1126 if(len != length)
1127 krb5_abortx(context, "internal error in ASN.1 encoder");
1129 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1130 if (ret)
1131 free(buf);
1133 return 0;
1137 * Assumes caller always will free `out_md', even on error.
1140 static krb5_error_code
1141 process_pa_data_to_md(krb5_context context,
1142 const krb5_creds *creds,
1143 const AS_REQ *a,
1144 krb5_get_init_creds_ctx *ctx,
1145 METHOD_DATA *in_md,
1146 METHOD_DATA **out_md,
1147 krb5_prompter_fct prompter,
1148 void *prompter_data)
1150 krb5_error_code ret;
1152 ALLOC(*out_md, 1);
1153 if (*out_md == NULL) {
1154 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1155 return ENOMEM;
1157 (*out_md)->len = 0;
1158 (*out_md)->val = NULL;
1160 if (_krb5_have_debug(context, 5)) {
1161 unsigned i;
1162 _krb5_debug(context, 5, "KDC send %d patypes", in_md->len);
1163 for (i = 0; i < in_md->len; i++)
1164 _krb5_debug(context, 5, "KDC send PA-DATA type: %d", in_md->val[i].padata_type);
1168 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1169 * need to expose our password protecting our PKCS12 key.
1172 if (ctx->pk_init_ctx) {
1174 _krb5_debug(context, 5, "krb5_get_init_creds: "
1175 "prepareing PKINIT padata (%s)",
1176 (ctx->used_pa_types & USED_PKINIT_W2K) ? "win2k" : "ietf");
1178 if (ctx->used_pa_types & USED_PKINIT_W2K) {
1179 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1180 "Already tried pkinit, looping");
1181 return KRB5_GET_IN_TKT_LOOP;
1184 ret = pa_data_to_md_pkinit(context, a, creds->client,
1185 (ctx->used_pa_types & USED_PKINIT),
1186 ctx, *out_md);
1187 if (ret)
1188 return ret;
1190 if (ctx->used_pa_types & USED_PKINIT)
1191 ctx->used_pa_types |= USED_PKINIT_W2K;
1192 else
1193 ctx->used_pa_types |= USED_PKINIT;
1195 } else if (in_md->len != 0) {
1196 struct pa_info_data *paid, *ppaid;
1197 unsigned flag;
1199 paid = calloc(1, sizeof(*paid));
1201 paid->etype = ENCTYPE_NULL;
1202 ppaid = process_pa_info(context, creds->client, a, paid, in_md);
1204 if (ppaid)
1205 flag = USED_ENC_TS_INFO;
1206 else
1207 flag = USED_ENC_TS_GUESS;
1209 if (ctx->used_pa_types & flag) {
1210 if (ppaid)
1211 free_paid(context, ppaid);
1212 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1213 "Already tried ENC-TS-%s, looping",
1214 flag == USED_ENC_TS_INFO ? "info" : "guess");
1215 return KRB5_GET_IN_TKT_LOOP;
1218 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1220 ctx->used_pa_types |= flag;
1222 if (ppaid) {
1223 if (ctx->ppaid) {
1224 free_paid(context, ctx->ppaid);
1225 free(ctx->ppaid);
1227 ctx->ppaid = ppaid;
1228 } else
1229 free(paid);
1232 pa_data_add_pac_request(context, ctx, *out_md);
1234 if ((*out_md)->len == 0) {
1235 free(*out_md);
1236 *out_md = NULL;
1239 return 0;
1242 static krb5_error_code
1243 process_pa_data_to_key(krb5_context context,
1244 krb5_get_init_creds_ctx *ctx,
1245 krb5_creds *creds,
1246 AS_REQ *a,
1247 AS_REP *rep,
1248 const krb5_krbhst_info *hi,
1249 krb5_keyblock **key)
1251 struct pa_info_data paid, *ppaid = NULL;
1252 krb5_error_code ret;
1253 krb5_enctype etype;
1254 PA_DATA *pa;
1256 memset(&paid, 0, sizeof(paid));
1258 etype = rep->enc_part.etype;
1260 if (rep->padata) {
1261 paid.etype = etype;
1262 ppaid = process_pa_info(context, creds->client, a, &paid,
1263 rep->padata);
1265 if (ppaid == NULL)
1266 ppaid = ctx->ppaid;
1267 if (ppaid == NULL) {
1268 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1269 if (ret)
1270 return ret;
1271 paid.etype = etype;
1272 paid.s2kparams = NULL;
1273 ppaid = &paid;
1276 pa = NULL;
1277 if (rep->padata) {
1278 int idx = 0;
1279 pa = krb5_find_padata(rep->padata->val,
1280 rep->padata->len,
1281 KRB5_PADATA_PK_AS_REP,
1282 &idx);
1283 if (pa == NULL) {
1284 idx = 0;
1285 pa = krb5_find_padata(rep->padata->val,
1286 rep->padata->len,
1287 KRB5_PADATA_PK_AS_REP_19,
1288 &idx);
1291 if (pa && ctx->pk_init_ctx) {
1292 #ifdef PKINIT
1293 _krb5_debug(context, 5, "krb5_get_init_creds: using PKINIT");
1295 ret = _krb5_pk_rd_pa_reply(context,
1296 a->req_body.realm,
1297 ctx->pk_init_ctx,
1298 etype,
1300 ctx->pk_nonce,
1301 &ctx->req_buffer,
1303 key);
1304 #else
1305 ret = EINVAL;
1306 krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", ""));
1307 #endif
1308 } else if (ctx->keyseed) {
1309 _krb5_debug(context, 5, "krb5_get_init_creds: using keyproc");
1310 ret = pa_data_to_key_plain(context, creds->client, ctx,
1311 ppaid->salt, ppaid->s2kparams, etype, key);
1312 } else {
1313 ret = EINVAL;
1314 krb5_set_error_message(context, ret, N_("No usable pa data type", ""));
1317 free_paid(context, &paid);
1318 return ret;
1322 * Start a new context to get a new initial credential.
1324 * @param context A Kerberos 5 context.
1325 * @param client The Kerberos principal to get the credential for, if
1326 * NULL is given, the default principal is used as determined by
1327 * krb5_get_default_principal().
1328 * @param prompter
1329 * @param prompter_data
1330 * @param start_time the time the ticket should start to be valid or 0 for now.
1331 * @param options a options structure, can be NULL for default options.
1332 * @param rctx A new allocated free with krb5_init_creds_free().
1334 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1336 * @ingroup krb5_credential
1339 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1340 krb5_init_creds_init(krb5_context context,
1341 krb5_principal client,
1342 krb5_prompter_fct prompter,
1343 void *prompter_data,
1344 krb5_deltat start_time,
1345 krb5_get_init_creds_opt *options,
1346 krb5_init_creds_context *rctx)
1348 krb5_init_creds_context ctx;
1349 krb5_error_code ret;
1351 *rctx = NULL;
1353 ctx = calloc(1, sizeof(*ctx));
1354 if (ctx == NULL) {
1355 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1356 return ENOMEM;
1359 ret = get_init_creds_common(context, client, start_time, options, ctx);
1360 if (ret) {
1361 free(ctx);
1362 return ret;
1365 /* Set a new nonce. */
1366 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1367 ctx->nonce &= 0x7fffffff;
1368 /* XXX these just needs to be the same when using Windows PK-INIT */
1369 ctx->pk_nonce = ctx->nonce;
1371 ctx->prompter = prompter;
1372 ctx->prompter_data = prompter_data;
1374 *rctx = ctx;
1376 return ret;
1380 * Sets the service that the is requested. This call is only neede for
1381 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1383 * @param context a Kerberos 5 context.
1384 * @param ctx a krb5_init_creds_context context.
1385 * @param service the service given as a string, for example
1386 * "kadmind/admin". If NULL, the default krbtgt in the clients
1387 * realm is set.
1389 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1390 * @ingroup krb5_credential
1393 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1394 krb5_init_creds_set_service(krb5_context context,
1395 krb5_init_creds_context ctx,
1396 const char *service)
1398 krb5_const_realm client_realm;
1399 krb5_principal principal;
1400 krb5_error_code ret;
1402 client_realm = krb5_principal_get_realm (context, ctx->cred.client);
1404 if (service) {
1405 ret = krb5_parse_name (context, service, &principal);
1406 if (ret)
1407 return ret;
1408 krb5_principal_set_realm (context, principal, client_realm);
1409 } else {
1410 ret = krb5_make_principal(context, &principal,
1411 client_realm, KRB5_TGS_NAME, client_realm,
1412 NULL);
1413 if (ret)
1414 return ret;
1416 krb5_free_principal(context, ctx->cred.server);
1417 ctx->cred.server = principal;
1419 return 0;
1423 * Sets the password that will use for the request.
1425 * @param context a Kerberos 5 context.
1426 * @param ctx ctx krb5_init_creds_context context.
1427 * @param password the password to use.
1429 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1430 * @ingroup krb5_credential
1433 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1434 krb5_init_creds_set_password(krb5_context context,
1435 krb5_init_creds_context ctx,
1436 const char *password)
1438 if (ctx->password) {
1439 memset(ctx->password, 0, strlen(ctx->password));
1440 free(ctx->password);
1442 if (password) {
1443 ctx->password = strdup(password);
1444 if (ctx->password == NULL) {
1445 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1446 return ENOMEM;
1448 ctx->keyseed = (void *) ctx->password;
1449 } else {
1450 ctx->keyseed = NULL;
1451 ctx->password = NULL;
1454 return 0;
1457 static krb5_error_code
1458 keytab_key_proc(krb5_context context, krb5_enctype enctype,
1459 krb5_const_pointer keyseed,
1460 krb5_salt salt, krb5_data *s2kparms,
1461 krb5_keyblock **key)
1463 krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed);
1464 krb5_keytab keytab = args->keytab;
1465 krb5_principal principal = args->principal;
1466 krb5_error_code ret;
1467 krb5_keytab real_keytab;
1468 krb5_keytab_entry entry;
1470 if(keytab == NULL)
1471 krb5_kt_default(context, &real_keytab);
1472 else
1473 real_keytab = keytab;
1475 ret = krb5_kt_get_entry (context, real_keytab, principal,
1476 0, enctype, &entry);
1478 if (keytab == NULL)
1479 krb5_kt_close (context, real_keytab);
1481 if (ret)
1482 return ret;
1484 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
1485 krb5_kt_free_entry(context, &entry);
1486 return ret;
1491 * Set the keytab to use for authentication.
1493 * @param context a Kerberos 5 context.
1494 * @param ctx ctx krb5_init_creds_context context.
1495 * @param keytab the keytab to read the key from.
1497 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1498 * @ingroup krb5_credential
1501 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1502 krb5_init_creds_set_keytab(krb5_context context,
1503 krb5_init_creds_context ctx,
1504 krb5_keytab keytab)
1506 krb5_keytab_key_proc_args *a;
1507 krb5_keytab_entry entry;
1508 krb5_kt_cursor cursor;
1509 krb5_enctype *etypes = NULL;
1510 krb5_error_code ret;
1511 size_t netypes = 0;
1512 int kvno = 0;
1514 a = malloc(sizeof(*a));
1515 if (a == NULL) {
1516 krb5_set_error_message(context, ENOMEM,
1517 N_("malloc: out of memory", ""));
1518 return ENOMEM;
1521 a->principal = ctx->cred.client;
1522 a->keytab = keytab;
1524 ctx->keytab_data = a;
1525 ctx->keyseed = (void *)a;
1526 ctx->keyproc = keytab_key_proc;
1529 * We need to the KDC what enctypes we support for this keytab,
1530 * esp if the keytab is really a password based entry, then the
1531 * KDC might have more enctypes in the database then what we have
1532 * in the keytab.
1535 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1536 if(ret)
1537 goto out;
1539 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
1540 void *ptr;
1542 if (!krb5_principal_compare(context, entry.principal, ctx->cred.client))
1543 goto next;
1545 /* check if we ahve this kvno already */
1546 if (entry.vno > kvno) {
1547 /* remove old list of etype */
1548 if (etypes)
1549 free(etypes);
1550 etypes = NULL;
1551 netypes = 0;
1552 kvno = entry.vno;
1553 } else if (entry.vno != kvno)
1554 goto next;
1556 /* check if enctype is supported */
1557 if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0)
1558 goto next;
1560 /* add enctype to supported list */
1561 ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2));
1562 if (ptr == NULL)
1563 goto next;
1565 etypes = ptr;
1566 etypes[netypes] = entry.keyblock.keytype;
1567 etypes[netypes + 1] = ETYPE_NULL;
1568 netypes++;
1569 next:
1570 krb5_kt_free_entry(context, &entry);
1572 krb5_kt_end_seq_get(context, keytab, &cursor);
1574 if (etypes) {
1575 if (ctx->etypes)
1576 free(ctx->etypes);
1577 ctx->etypes = etypes;
1580 out:
1581 return 0;
1584 static krb5_error_code
1585 keyblock_key_proc(krb5_context context, krb5_enctype enctype,
1586 krb5_const_pointer keyseed,
1587 krb5_salt salt, krb5_data *s2kparms,
1588 krb5_keyblock **key)
1590 return krb5_copy_keyblock (context, keyseed, key);
1593 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1594 krb5_init_creds_set_keyblock(krb5_context context,
1595 krb5_init_creds_context ctx,
1596 krb5_keyblock *keyblock)
1598 ctx->keyseed = (void *)keyblock;
1599 ctx->keyproc = keyblock_key_proc;
1601 return 0;
1605 * The core loop if krb5_get_init_creds() function family. Create the
1606 * packets and have the caller send them off to the KDC.
1608 * If the caller want all work been done for them, use
1609 * krb5_init_creds_get() instead.
1611 * @param context a Kerberos 5 context.
1612 * @param ctx ctx krb5_init_creds_context context.
1613 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
1614 * @param out reply to KDC.
1615 * @param hostinfo KDC address info, first round it can be NULL.
1616 * @param flags status of the round, if
1617 * KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round.
1619 * @return 0 for success, or an Kerberos 5 error code, see
1620 * krb5_get_error_message().
1622 * @ingroup krb5_credential
1625 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1626 krb5_init_creds_step(krb5_context context,
1627 krb5_init_creds_context ctx,
1628 krb5_data *in,
1629 krb5_data *out,
1630 krb5_krbhst_info *hostinfo,
1631 unsigned int *flags)
1633 krb5_error_code ret;
1634 size_t len;
1635 size_t size;
1637 krb5_data_zero(out);
1639 if (ctx->as_req.req_body.cname == NULL) {
1640 ret = init_as_req(context, ctx->flags, &ctx->cred,
1641 ctx->addrs, ctx->etypes, &ctx->as_req);
1642 if (ret) {
1643 free_init_creds_ctx(context, ctx);
1644 return ret;
1648 #define MAX_PA_COUNTER 10
1649 if (ctx->pa_counter > MAX_PA_COUNTER) {
1650 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1651 N_("Looping %d times while getting "
1652 "initial credentials", ""),
1653 ctx->pa_counter);
1654 return KRB5_GET_IN_TKT_LOOP;
1656 ctx->pa_counter++;
1658 _krb5_debug(context, 5, "krb5_get_init_creds: loop %d", ctx->pa_counter);
1660 /* Lets process the input packet */
1661 if (in && in->length) {
1662 krb5_kdc_rep rep;
1664 memset(&rep, 0, sizeof(rep));
1666 _krb5_debug(context, 5, "krb5_get_init_creds: processing input");
1668 ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
1669 if (ret == 0) {
1670 krb5_keyblock *key = NULL;
1671 unsigned eflags = EXTRACT_TICKET_AS_REQ | EXTRACT_TICKET_TIMESYNC;
1673 if (ctx->flags.canonicalize) {
1674 eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1675 eflags |= EXTRACT_TICKET_MATCH_REALM;
1677 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1678 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1680 ret = process_pa_data_to_key(context, ctx, &ctx->cred,
1681 &ctx->as_req, &rep.kdc_rep, hostinfo, &key);
1682 if (ret) {
1683 free_AS_REP(&rep.kdc_rep);
1684 goto out;
1687 _krb5_debug(context, 5, "krb5_get_init_creds: extracting ticket");
1689 ret = _krb5_extract_ticket(context,
1690 &rep,
1691 &ctx->cred,
1692 key,
1693 NULL,
1694 KRB5_KU_AS_REP_ENC_PART,
1695 NULL,
1696 ctx->nonce,
1697 eflags,
1698 NULL,
1699 NULL);
1700 krb5_free_keyblock(context, key);
1702 *flags = 0;
1704 if (ret == 0)
1705 ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
1707 free_AS_REP(&rep.kdc_rep);
1708 free_EncASRepPart(&rep.enc_part);
1710 return ret;
1712 } else {
1713 /* let's try to parse it as a KRB-ERROR */
1715 _krb5_debug(context, 5, "krb5_get_init_creds: got an error");
1717 free_KRB_ERROR(&ctx->error);
1719 ret = krb5_rd_error(context, in, &ctx->error);
1720 if(ret && in->length && ((char*)in->data)[0] == 4)
1721 ret = KRB5KRB_AP_ERR_V4_REPLY;
1722 if (ret) {
1723 _krb5_debug(context, 5, "krb5_get_init_creds: failed to read error");
1724 goto out;
1727 ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);
1729 _krb5_debug(context, 5, "krb5_get_init_creds: KRB-ERROR %d", ret);
1732 * If no preauth was set and KDC requires it, give it one
1733 * more try.
1736 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1738 free_METHOD_DATA(&ctx->md);
1739 memset(&ctx->md, 0, sizeof(ctx->md));
1741 if (ctx->error.e_data) {
1742 ret = decode_METHOD_DATA(ctx->error.e_data->data,
1743 ctx->error.e_data->length,
1744 &ctx->md,
1745 NULL);
1746 if (ret)
1747 krb5_set_error_message(context, ret,
1748 N_("Failed to decode METHOD-DATA", ""));
1749 } else {
1750 krb5_set_error_message(context, ret,
1751 N_("Preauth required but no preauth "
1752 "options send by KDC", ""));
1754 } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
1756 * Try adapt to timeskrew when we are using pre-auth, and
1757 * if there was a time skew, try again.
1759 krb5_set_real_time(context, ctx->error.stime, -1);
1760 if (context->kdc_sec_offset)
1761 ret = 0;
1763 _krb5_debug(context, 10, "init_creds: err skew updateing kdc offset to %d",
1764 context->kdc_sec_offset);
1766 ctx->used_pa_types = 0;
1768 } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
1769 /* client referal to a new realm */
1771 if (ctx->error.crealm == NULL) {
1772 krb5_set_error_message(context, ret,
1773 N_("Got a client referral, not but no realm", ""));
1774 goto out;
1776 _krb5_debug(context, 5,
1777 "krb5_get_init_creds: got referal to realm %s",
1778 *ctx->error.crealm);
1780 ret = krb5_principal_set_realm(context,
1781 ctx->cred.client,
1782 *ctx->error.crealm);
1784 ctx->used_pa_types = 0;
1786 if (ret)
1787 goto out;
1791 if (ctx->as_req.padata) {
1792 free_METHOD_DATA(ctx->as_req.padata);
1793 free(ctx->as_req.padata);
1794 ctx->as_req.padata = NULL;
1797 /* Set a new nonce. */
1798 ctx->as_req.req_body.nonce = ctx->nonce;
1800 /* fill_in_md_data */
1801 ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
1802 &ctx->md, &ctx->as_req.padata,
1803 ctx->prompter, ctx->prompter_data);
1804 if (ret)
1805 goto out;
1807 krb5_data_free(&ctx->req_buffer);
1809 ASN1_MALLOC_ENCODE(AS_REQ,
1810 ctx->req_buffer.data, ctx->req_buffer.length,
1811 &ctx->as_req, &len, ret);
1812 if (ret)
1813 goto out;
1814 if(len != ctx->req_buffer.length)
1815 krb5_abortx(context, "internal error in ASN.1 encoder");
1817 out->data = ctx->req_buffer.data;
1818 out->length = ctx->req_buffer.length;
1820 *flags = KRB5_INIT_CREDS_STEP_FLAG_CONTINUE;
1822 return 0;
1823 out:
1824 return ret;
1828 * Extract the newly acquired credentials from krb5_init_creds_context
1829 * context.
1831 * @param context A Kerberos 5 context.
1832 * @param ctx
1833 * @param cred credentials, free with krb5_free_cred_contents().
1835 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
1838 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1839 krb5_init_creds_get_creds(krb5_context context,
1840 krb5_init_creds_context ctx,
1841 krb5_creds *cred)
1843 return krb5_copy_creds_contents(context, &ctx->cred, cred);
1847 * Get the last error from the transaction.
1849 * @return Returns 0 or an error code
1851 * @ingroup krb5_credential
1854 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1855 krb5_init_creds_get_error(krb5_context context,
1856 krb5_init_creds_context ctx,
1857 KRB_ERROR *error)
1859 krb5_error_code ret;
1861 ret = copy_KRB_ERROR(&ctx->error, error);
1862 if (ret)
1863 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1865 return ret;
1869 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
1871 * @param context A Kerberos 5 context.
1872 * @param ctx The krb5_init_creds_context to free.
1874 * @ingroup krb5_credential
1877 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1878 krb5_init_creds_free(krb5_context context,
1879 krb5_init_creds_context ctx)
1881 free_init_creds_ctx(context, ctx);
1882 free(ctx);
1886 * Get new credentials as setup by the krb5_init_creds_context.
1888 * @param context A Kerberos 5 context.
1889 * @param ctx The krb5_init_creds_context to process.
1891 * @ingroup krb5_credential
1894 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1895 krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
1897 krb5_sendto_ctx stctx = NULL;
1898 krb5_krbhst_info *hostinfo = NULL;
1899 krb5_error_code ret;
1900 krb5_data in, out;
1901 unsigned int flags = 0;
1903 krb5_data_zero(&in);
1904 krb5_data_zero(&out);
1906 ret = krb5_sendto_ctx_alloc(context, &stctx);
1907 if (ret)
1908 goto out;
1909 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1911 while (1) {
1912 flags = 0;
1913 ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
1914 krb5_data_free(&in);
1915 if (ret)
1916 goto out;
1918 if ((flags & 1) == 0)
1919 break;
1921 ret = krb5_sendto_context (context, stctx, &out,
1922 ctx->cred.client->realm, &in);
1923 if (ret)
1924 goto out;
1928 out:
1929 if (stctx)
1930 krb5_sendto_ctx_free(context, stctx);
1932 return ret;
1936 * Get new credentials using password.
1938 * @ingroup krb5_credential
1942 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1943 krb5_get_init_creds_password(krb5_context context,
1944 krb5_creds *creds,
1945 krb5_principal client,
1946 const char *password,
1947 krb5_prompter_fct prompter,
1948 void *data,
1949 krb5_deltat start_time,
1950 const char *in_tkt_service,
1951 krb5_get_init_creds_opt *options)
1953 krb5_init_creds_context ctx;
1954 char buf[BUFSIZ];
1955 krb5_error_code ret;
1956 int chpw = 0;
1958 again:
1959 ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
1960 if (ret)
1961 goto out;
1963 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1964 if (ret)
1965 goto out;
1967 if (prompter != NULL && ctx->password == NULL && password == NULL) {
1968 krb5_prompt prompt;
1969 krb5_data password_data;
1970 char *p, *q;
1972 krb5_unparse_name (context, client, &p);
1973 asprintf (&q, "%s's Password: ", p);
1974 free (p);
1975 prompt.prompt = q;
1976 password_data.data = buf;
1977 password_data.length = sizeof(buf);
1978 prompt.hidden = 1;
1979 prompt.reply = &password_data;
1980 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1982 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1983 free (q);
1984 if (ret) {
1985 memset (buf, 0, sizeof(buf));
1986 ret = KRB5_LIBOS_PWDINTR;
1987 krb5_clear_error_message (context);
1988 goto out;
1990 password = password_data.data;
1993 if (password) {
1994 ret = krb5_init_creds_set_password(context, ctx, password);
1995 if (ret)
1996 goto out;
1999 ret = krb5_init_creds_get(context, ctx);
2001 if (ret == 0)
2002 process_last_request(context, options, ctx);
2005 if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
2006 char buf2[1024];
2008 /* try to avoid recursion */
2009 if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
2010 goto out;
2012 /* don't try to change password where then where none */
2013 if (prompter == NULL)
2014 goto out;
2016 ret = change_password (context,
2017 client,
2018 ctx->password,
2019 buf2,
2020 sizeof(buf),
2021 prompter,
2022 data,
2023 options);
2024 if (ret)
2025 goto out;
2026 chpw = 1;
2027 krb5_init_creds_free(context, ctx);
2028 goto again;
2031 out:
2032 if (ret == 0)
2033 krb5_init_creds_get_creds(context, ctx, creds);
2035 if (ctx)
2036 krb5_init_creds_free(context, ctx);
2038 memset(buf, 0, sizeof(buf));
2039 return ret;
2043 * Get new credentials using keyblock.
2045 * @ingroup krb5_credential
2048 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2049 krb5_get_init_creds_keyblock(krb5_context context,
2050 krb5_creds *creds,
2051 krb5_principal client,
2052 krb5_keyblock *keyblock,
2053 krb5_deltat start_time,
2054 const char *in_tkt_service,
2055 krb5_get_init_creds_opt *options)
2057 krb5_init_creds_context ctx;
2058 krb5_error_code ret;
2060 memset(creds, 0, sizeof(*creds));
2062 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2063 if (ret)
2064 goto out;
2066 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2067 if (ret)
2068 goto out;
2070 ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
2071 if (ret)
2072 goto out;
2074 ret = krb5_init_creds_get(context, ctx);
2076 if (ret == 0)
2077 process_last_request(context, options, ctx);
2079 out:
2080 if (ret == 0)
2081 krb5_init_creds_get_creds(context, ctx, creds);
2083 if (ctx)
2084 krb5_init_creds_free(context, ctx);
2086 return ret;
2090 * Get new credentials using keytab.
2092 * @ingroup krb5_credential
2095 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2096 krb5_get_init_creds_keytab(krb5_context context,
2097 krb5_creds *creds,
2098 krb5_principal client,
2099 krb5_keytab keytab,
2100 krb5_deltat start_time,
2101 const char *in_tkt_service,
2102 krb5_get_init_creds_opt *options)
2104 krb5_init_creds_context ctx;
2105 krb5_error_code ret;
2107 memset(creds, 0, sizeof(*creds));
2109 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2110 if (ret)
2111 goto out;
2113 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2114 if (ret)
2115 goto out;
2117 ret = krb5_init_creds_set_keytab(context, ctx, keytab);
2118 if (ret)
2119 goto out;
2121 ret = krb5_init_creds_get(context, ctx);
2122 if (ret == 0)
2123 process_last_request(context, options, ctx);
2125 out:
2126 if (ret == 0)
2127 krb5_init_creds_get_creds(context, ctx, creds);
2129 if (ctx)
2130 krb5_init_creds_free(context, ctx);
2132 return ret;