Handle picky windows RODC servers
[heimdal.git] / lib / krb5 / init_creds_pw.c
blobdf300380eca71949ffb94af511697079e4b07c56
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 typedef struct krb5_get_init_creds_ctx {
37 KDCOptions flags;
38 krb5_creds cred;
39 krb5_addresses *addrs;
40 krb5_enctype *etypes;
41 krb5_preauthtype *pre_auth_types;
42 char *in_tkt_service;
43 unsigned nonce;
44 unsigned pk_nonce;
46 krb5_data req_buffer;
47 AS_REQ as_req;
48 int pa_counter;
50 /* password and keytab_data is freed on completion */
51 char *password;
52 krb5_keytab_key_proc_args *keytab_data;
54 krb5_pointer *keyseed;
55 krb5_s2k_proc keyproc;
57 krb5_get_init_creds_tristate req_pac;
59 krb5_pk_init_ctx pk_init_ctx;
60 int ic_flags;
62 METHOD_DATA md;
63 KRB_ERROR error;
64 AS_REP as_rep;
65 EncKDCRepPart enc_part;
67 krb5_prompter_fct prompter;
68 void *prompter_data;
70 } krb5_get_init_creds_ctx;
72 static krb5_error_code
73 default_s2k_func(krb5_context context, krb5_enctype type,
74 krb5_const_pointer keyseed,
75 krb5_salt salt, krb5_data *s2kparms,
76 krb5_keyblock **key)
78 krb5_error_code ret;
79 krb5_data password;
80 krb5_data opaque;
82 password.data = rk_UNCONST(keyseed);
83 password.length = strlen(keyseed);
84 if (s2kparms)
85 opaque = *s2kparms;
86 else
87 krb5_data_zero(&opaque);
89 *key = malloc(sizeof(**key));
90 if (*key == NULL)
91 return ENOMEM;
92 ret = krb5_string_to_key_data_salt_opaque(context, type, password,
93 salt, opaque, *key);
94 if (ret) {
95 free(*key);
96 *key = NULL;
98 return ret;
101 static void
102 free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx)
104 if (ctx->etypes)
105 free(ctx->etypes);
106 if (ctx->pre_auth_types)
107 free (ctx->pre_auth_types);
108 if (ctx->in_tkt_service)
109 free(ctx->in_tkt_service);
110 if (ctx->keytab_data)
111 free(ctx->keytab_data);
112 krb5_data_free(&ctx->req_buffer);
113 krb5_free_cred_contents(context, &ctx->cred);
114 free_METHOD_DATA(&ctx->md);
115 free_AS_REP(&ctx->as_rep);
116 free_EncKDCRepPart(&ctx->enc_part);
117 free_KRB_ERROR(&ctx->error);
118 free_AS_REQ(&ctx->as_req);
119 memset(ctx, 0, sizeof(*ctx));
122 static int
123 get_config_time (krb5_context context,
124 const char *realm,
125 const char *name,
126 int def)
128 int ret;
130 ret = krb5_config_get_time (context, NULL,
131 "realms",
132 realm,
133 name,
134 NULL);
135 if (ret >= 0)
136 return ret;
137 ret = krb5_config_get_time (context, NULL,
138 "libdefaults",
139 name,
140 NULL);
141 if (ret >= 0)
142 return ret;
143 return def;
146 static krb5_error_code
147 init_cred (krb5_context context,
148 krb5_creds *cred,
149 krb5_principal client,
150 krb5_deltat start_time,
151 krb5_get_init_creds_opt *options)
153 krb5_error_code ret;
154 int tmp;
155 krb5_timestamp now;
157 krb5_timeofday (context, &now);
159 memset (cred, 0, sizeof(*cred));
161 if (client)
162 krb5_copy_principal(context, client, &cred->client);
163 else {
164 ret = krb5_get_default_principal (context,
165 &cred->client);
166 if (ret)
167 goto out;
170 if (start_time)
171 cred->times.starttime = now + start_time;
173 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
174 tmp = options->tkt_life;
175 else
176 tmp = 10 * 60 * 60;
177 cred->times.endtime = now + tmp;
179 if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE) &&
180 options->renew_life > 0) {
181 cred->times.renew_till = now + options->renew_life;
184 return 0;
186 out:
187 krb5_free_cred_contents (context, cred);
188 return ret;
192 * Print a message (str) to the user about the expiration in `lr'
195 static void
196 report_expiration (krb5_context context,
197 krb5_prompter_fct prompter,
198 krb5_data *data,
199 const char *str,
200 time_t now)
202 char *p;
204 asprintf (&p, "%s%s", str, ctime(&now));
205 (*prompter) (context, data, NULL, p, 0, NULL);
206 free (p);
210 * Check the context, and in the case there is a expiration warning,
211 * use the prompter to print the warning.
213 * @param context A Kerberos 5 context.
214 * @param options An GIC options structure
215 * @param ctx The krb5_init_creds_context check for expiration.
218 static krb5_error_code
219 process_last_request(krb5_context context,
220 krb5_get_init_creds_opt *options,
221 krb5_init_creds_context ctx)
223 krb5_const_realm realm;
224 LastReq *lr;
225 krb5_boolean reported = FALSE;
226 krb5_timestamp sec;
227 time_t t;
228 size_t i;
231 * First check if there is a API consumer.
234 realm = krb5_principal_get_realm (context, ctx->cred.client);
235 lr = &ctx->enc_part.last_req;
237 if (options && options->opt_private && options->opt_private->lr.func) {
238 krb5_last_req_entry **lre;
240 lre = calloc(lr->len + 1, sizeof(**lre));
241 if (lre == NULL) {
242 krb5_set_error_message(context, ENOMEM,
243 N_("malloc: out of memory", ""));
244 return ENOMEM;
246 for (i = 0; i < lr->len; i++) {
247 lre[i] = calloc(1, sizeof(*lre[i]));
248 if (lre[i] == NULL)
249 break;
250 lre[i]->lr_type = lr->val[i].lr_type;
251 lre[i]->value = lr->val[i].lr_value;
254 (*options->opt_private->lr.func)(context, lre,
255 options->opt_private->lr.ctx);
257 for (i = 0; i < lr->len; i++)
258 free(lre[i]);
259 free(lre);
263 * Now check if we should prompt the user
266 if (ctx->prompter == NULL)
267 return 0;
269 krb5_timeofday (context, &sec);
271 t = sec + get_config_time (context,
272 realm,
273 "warn_pwexpire",
274 7 * 24 * 60 * 60);
276 for (i = 0; i < lr->len; ++i) {
277 if (lr->val[i].lr_value <= t) {
278 switch (abs(lr->val[i].lr_type)) {
279 case LR_PW_EXPTIME :
280 report_expiration(context, ctx->prompter,
281 ctx->prompter_data,
282 "Your password will expire at ",
283 lr->val[i].lr_value);
284 reported = TRUE;
285 break;
286 case LR_ACCT_EXPTIME :
287 report_expiration(context, ctx->prompter,
288 ctx->prompter_data,
289 "Your account will expire at ",
290 lr->val[i].lr_value);
291 reported = TRUE;
292 break;
297 if (!reported
298 && ctx->enc_part.key_expiration
299 && *ctx->enc_part.key_expiration <= t) {
300 report_expiration(context, ctx->prompter,
301 ctx->prompter_data,
302 "Your password/account will expire at ",
303 *ctx->enc_part.key_expiration);
305 return 0;
308 static krb5_addresses no_addrs = { 0, NULL };
310 static krb5_error_code
311 get_init_creds_common(krb5_context context,
312 krb5_principal client,
313 krb5_deltat start_time,
314 krb5_get_init_creds_opt *options,
315 krb5_init_creds_context ctx)
317 krb5_get_init_creds_opt *default_opt = NULL;
318 krb5_error_code ret;
319 krb5_enctype *etypes;
320 krb5_preauthtype *pre_auth_types;
322 memset(ctx, 0, sizeof(*ctx));
324 if (options == NULL) {
325 const char *realm = krb5_principal_get_realm(context, client);
327 krb5_get_init_creds_opt_alloc (context, &default_opt);
328 options = default_opt;
329 krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options);
332 if (options->opt_private) {
333 if (options->opt_private->password) {
334 ret = krb5_init_creds_set_password(context, ctx,
335 options->opt_private->password);
336 if (ret)
337 goto out;
340 ctx->keyproc = options->opt_private->key_proc;
341 ctx->req_pac = options->opt_private->req_pac;
342 ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
343 ctx->ic_flags = options->opt_private->flags;
344 } else
345 ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;
347 if (ctx->keyproc == NULL)
348 ctx->keyproc = default_s2k_func;
350 /* Enterprise name implicitly turns on canonicalize */
351 if ((ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE) ||
352 krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL)
353 ctx->flags.canonicalize = 1;
355 ctx->pre_auth_types = NULL;
356 ctx->addrs = NULL;
357 ctx->etypes = NULL;
358 ctx->pre_auth_types = NULL;
360 ret = init_cred(context, &ctx->cred, client, start_time, options);
361 if (ret) {
362 if (default_opt)
363 krb5_get_init_creds_opt_free(context, default_opt);
364 return ret;
367 ret = krb5_init_creds_set_service(context, ctx, NULL);
368 if (ret)
369 goto out;
371 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
372 ctx->flags.forwardable = options->forwardable;
374 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
375 ctx->flags.proxiable = options->proxiable;
377 if (start_time)
378 ctx->flags.postdated = 1;
379 if (ctx->cred.times.renew_till)
380 ctx->flags.renewable = 1;
381 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
382 ctx->addrs = options->address_list;
383 } else if (options->opt_private) {
384 switch (options->opt_private->addressless) {
385 case KRB5_INIT_CREDS_TRISTATE_UNSET:
386 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
387 ctx->addrs = &no_addrs;
388 #else
389 ctx->addrs = NULL;
390 #endif
391 break;
392 case KRB5_INIT_CREDS_TRISTATE_FALSE:
393 ctx->addrs = NULL;
394 break;
395 case KRB5_INIT_CREDS_TRISTATE_TRUE:
396 ctx->addrs = &no_addrs;
397 break;
400 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
401 if (ctx->etypes)
402 free(ctx->etypes);
404 etypes = malloc((options->etype_list_length + 1)
405 * sizeof(krb5_enctype));
406 if (etypes == NULL) {
407 ret = ENOMEM;
408 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
409 goto out;
411 memcpy (etypes, options->etype_list,
412 options->etype_list_length * sizeof(krb5_enctype));
413 etypes[options->etype_list_length] = ETYPE_NULL;
414 ctx->etypes = etypes;
416 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
417 pre_auth_types = malloc((options->preauth_list_length + 1)
418 * sizeof(krb5_preauthtype));
419 if (pre_auth_types == NULL) {
420 ret = ENOMEM;
421 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
422 goto out;
424 memcpy (pre_auth_types, options->preauth_list,
425 options->preauth_list_length * sizeof(krb5_preauthtype));
426 pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
427 ctx->pre_auth_types = pre_auth_types;
429 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
430 ctx->flags.request_anonymous = options->anonymous;
431 if (default_opt)
432 krb5_get_init_creds_opt_free(context, default_opt);
433 return 0;
434 out:
435 if (default_opt)
436 krb5_get_init_creds_opt_free(context, default_opt);
437 return ret;
440 static krb5_error_code
441 change_password (krb5_context context,
442 krb5_principal client,
443 const char *password,
444 char *newpw,
445 size_t newpw_sz,
446 krb5_prompter_fct prompter,
447 void *data,
448 krb5_get_init_creds_opt *old_options)
450 krb5_prompt prompts[2];
451 krb5_error_code ret;
452 krb5_creds cpw_cred;
453 char buf1[BUFSIZ], buf2[BUFSIZ];
454 krb5_data password_data[2];
455 int result_code;
456 krb5_data result_code_string;
457 krb5_data result_string;
458 char *p;
459 krb5_get_init_creds_opt *options;
461 memset (&cpw_cred, 0, sizeof(cpw_cred));
463 ret = krb5_get_init_creds_opt_alloc(context, &options);
464 if (ret)
465 return ret;
466 krb5_get_init_creds_opt_set_tkt_life (options, 60);
467 krb5_get_init_creds_opt_set_forwardable (options, FALSE);
468 krb5_get_init_creds_opt_set_proxiable (options, FALSE);
469 if (old_options && old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
470 krb5_get_init_creds_opt_set_preauth_list (options,
471 old_options->preauth_list,
472 old_options->preauth_list_length);
474 krb5_data_zero (&result_code_string);
475 krb5_data_zero (&result_string);
477 ret = krb5_get_init_creds_password (context,
478 &cpw_cred,
479 client,
480 password,
481 prompter,
482 data,
484 "kadmin/changepw",
485 options);
486 krb5_get_init_creds_opt_free(context, options);
487 if (ret)
488 goto out;
490 for(;;) {
491 password_data[0].data = buf1;
492 password_data[0].length = sizeof(buf1);
494 prompts[0].hidden = 1;
495 prompts[0].prompt = "New password: ";
496 prompts[0].reply = &password_data[0];
497 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
499 password_data[1].data = buf2;
500 password_data[1].length = sizeof(buf2);
502 prompts[1].hidden = 1;
503 prompts[1].prompt = "Repeat new password: ";
504 prompts[1].reply = &password_data[1];
505 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
507 ret = (*prompter) (context, data, NULL, "Changing password",
508 2, prompts);
509 if (ret) {
510 memset (buf1, 0, sizeof(buf1));
511 memset (buf2, 0, sizeof(buf2));
512 goto out;
515 if (strcmp (buf1, buf2) == 0)
516 break;
517 memset (buf1, 0, sizeof(buf1));
518 memset (buf2, 0, sizeof(buf2));
521 ret = krb5_set_password (context,
522 &cpw_cred,
523 buf1,
524 client,
525 &result_code,
526 &result_code_string,
527 &result_string);
528 if (ret)
529 goto out;
530 asprintf (&p, "%s: %.*s\n",
531 result_code ? "Error" : "Success",
532 (int)result_string.length,
533 result_string.length > 0 ? (char*)result_string.data : "");
535 /* return the result */
536 (*prompter) (context, data, NULL, p, 0, NULL);
538 free (p);
539 if (result_code == 0) {
540 strlcpy (newpw, buf1, newpw_sz);
541 ret = 0;
542 } else {
543 ret = ENOTTY;
544 krb5_set_error_message(context, ret,
545 N_("failed changing password", ""));
548 out:
549 memset (buf1, 0, sizeof(buf1));
550 memset (buf2, 0, sizeof(buf2));
551 krb5_data_free (&result_string);
552 krb5_data_free (&result_code_string);
553 krb5_free_cred_contents (context, &cpw_cred);
554 return ret;
558 krb5_error_code KRB5_LIB_FUNCTION
559 krb5_keyblock_key_proc (krb5_context context,
560 krb5_keytype type,
561 krb5_data *salt,
562 krb5_const_pointer keyseed,
563 krb5_keyblock **key)
565 return krb5_copy_keyblock (context, keyseed, key);
572 static krb5_error_code
573 init_as_req (krb5_context context,
574 KDCOptions opts,
575 const krb5_creds *creds,
576 const krb5_addresses *addrs,
577 const krb5_enctype *etypes,
578 AS_REQ *a)
580 krb5_error_code ret;
582 memset(a, 0, sizeof(*a));
584 a->pvno = 5;
585 a->msg_type = krb_as_req;
586 a->req_body.kdc_options = opts;
587 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
588 if (a->req_body.cname == NULL) {
589 ret = ENOMEM;
590 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
591 goto fail;
593 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
594 if (a->req_body.sname == NULL) {
595 ret = ENOMEM;
596 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
597 goto fail;
600 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
601 if (ret)
602 goto fail;
603 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
604 if (ret)
605 goto fail;
607 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
608 if (ret)
609 goto fail;
611 if(creds->times.starttime) {
612 a->req_body.from = malloc(sizeof(*a->req_body.from));
613 if (a->req_body.from == NULL) {
614 ret = ENOMEM;
615 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
616 goto fail;
618 *a->req_body.from = creds->times.starttime;
620 if(creds->times.endtime){
621 ALLOC(a->req_body.till, 1);
622 *a->req_body.till = creds->times.endtime;
624 if(creds->times.renew_till){
625 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
626 if (a->req_body.rtime == NULL) {
627 ret = ENOMEM;
628 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
629 goto fail;
631 *a->req_body.rtime = creds->times.renew_till;
633 a->req_body.nonce = 0;
634 ret = krb5_init_etype (context,
635 &a->req_body.etype.len,
636 &a->req_body.etype.val,
637 etypes);
638 if (ret)
639 goto fail;
642 * This means no addresses
645 if (addrs && addrs->len == 0) {
646 a->req_body.addresses = NULL;
647 } else {
648 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
649 if (a->req_body.addresses == NULL) {
650 ret = ENOMEM;
651 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
652 goto fail;
655 if (addrs)
656 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
657 else {
658 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
659 if(ret == 0 && a->req_body.addresses->len == 0) {
660 free(a->req_body.addresses);
661 a->req_body.addresses = NULL;
664 if (ret)
665 goto fail;
668 a->req_body.enc_authorization_data = NULL;
669 a->req_body.additional_tickets = NULL;
671 a->padata = NULL;
673 return 0;
674 fail:
675 free_AS_REQ(a);
676 memset(a, 0, sizeof(*a));
677 return ret;
680 struct pa_info_data {
681 krb5_enctype etype;
682 krb5_salt salt;
683 krb5_data *s2kparams;
686 static void
687 free_paid(krb5_context context, struct pa_info_data *ppaid)
689 krb5_free_salt(context, ppaid->salt);
690 if (ppaid->s2kparams)
691 krb5_free_data(context, ppaid->s2kparams);
695 static krb5_error_code
696 set_paid(struct pa_info_data *paid, krb5_context context,
697 krb5_enctype etype,
698 krb5_salttype salttype, void *salt_string, size_t salt_len,
699 krb5_data *s2kparams)
701 paid->etype = etype;
702 paid->salt.salttype = salttype;
703 paid->salt.saltvalue.data = malloc(salt_len + 1);
704 if (paid->salt.saltvalue.data == NULL) {
705 krb5_clear_error_message(context);
706 return ENOMEM;
708 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
709 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
710 paid->salt.saltvalue.length = salt_len;
711 if (s2kparams) {
712 krb5_error_code ret;
714 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
715 if (ret) {
716 krb5_clear_error_message(context);
717 krb5_free_salt(context, paid->salt);
718 return ret;
720 } else
721 paid->s2kparams = NULL;
723 return 0;
726 static struct pa_info_data *
727 pa_etype_info2(krb5_context context,
728 const krb5_principal client,
729 const AS_REQ *asreq,
730 struct pa_info_data *paid,
731 heim_octet_string *data)
733 krb5_error_code ret;
734 ETYPE_INFO2 e;
735 size_t sz;
736 int i, j;
738 memset(&e, 0, sizeof(e));
739 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
740 if (ret)
741 goto out;
742 if (e.len == 0)
743 goto out;
744 for (j = 0; j < asreq->req_body.etype.len; j++) {
745 for (i = 0; i < e.len; i++) {
746 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
747 krb5_salt salt;
748 if (e.val[i].salt == NULL)
749 ret = krb5_get_pw_salt(context, client, &salt);
750 else {
751 salt.saltvalue.data = *e.val[i].salt;
752 salt.saltvalue.length = strlen(*e.val[i].salt);
753 ret = 0;
755 if (ret == 0)
756 ret = set_paid(paid, context, e.val[i].etype,
757 KRB5_PW_SALT,
758 salt.saltvalue.data,
759 salt.saltvalue.length,
760 e.val[i].s2kparams);
761 if (e.val[i].salt == NULL)
762 krb5_free_salt(context, salt);
763 if (ret == 0) {
764 free_ETYPE_INFO2(&e);
765 return paid;
770 out:
771 free_ETYPE_INFO2(&e);
772 return NULL;
775 static struct pa_info_data *
776 pa_etype_info(krb5_context context,
777 const krb5_principal client,
778 const AS_REQ *asreq,
779 struct pa_info_data *paid,
780 heim_octet_string *data)
782 krb5_error_code ret;
783 ETYPE_INFO e;
784 size_t sz;
785 int i, j;
787 memset(&e, 0, sizeof(e));
788 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
789 if (ret)
790 goto out;
791 if (e.len == 0)
792 goto out;
793 for (j = 0; j < asreq->req_body.etype.len; j++) {
794 for (i = 0; i < e.len; i++) {
795 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
796 krb5_salt salt;
797 salt.salttype = KRB5_PW_SALT;
798 if (e.val[i].salt == NULL)
799 ret = krb5_get_pw_salt(context, client, &salt);
800 else {
801 salt.saltvalue = *e.val[i].salt;
802 ret = 0;
804 if (e.val[i].salttype)
805 salt.salttype = *e.val[i].salttype;
806 if (ret == 0) {
807 ret = set_paid(paid, context, e.val[i].etype,
808 salt.salttype,
809 salt.saltvalue.data,
810 salt.saltvalue.length,
811 NULL);
812 if (e.val[i].salt == NULL)
813 krb5_free_salt(context, salt);
815 if (ret == 0) {
816 free_ETYPE_INFO(&e);
817 return paid;
822 out:
823 free_ETYPE_INFO(&e);
824 return NULL;
827 static struct pa_info_data *
828 pa_pw_or_afs3_salt(krb5_context context,
829 const krb5_principal client,
830 const AS_REQ *asreq,
831 struct pa_info_data *paid,
832 heim_octet_string *data)
834 krb5_error_code ret;
835 if (paid->etype == ENCTYPE_NULL)
836 return NULL;
837 ret = set_paid(paid, context,
838 paid->etype,
839 paid->salt.salttype,
840 data->data,
841 data->length,
842 NULL);
843 if (ret)
844 return NULL;
845 return paid;
849 struct pa_info {
850 krb5_preauthtype type;
851 struct pa_info_data *(*salt_info)(krb5_context,
852 const krb5_principal,
853 const AS_REQ *,
854 struct pa_info_data *,
855 heim_octet_string *);
858 static struct pa_info pa_prefs[] = {
859 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
860 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
861 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
862 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
865 static PA_DATA *
866 find_pa_data(const METHOD_DATA *md, int type)
868 int i;
869 if (md == NULL)
870 return NULL;
871 for (i = 0; i < md->len; i++)
872 if (md->val[i].padata_type == type)
873 return &md->val[i];
874 return NULL;
877 static struct pa_info_data *
878 process_pa_info(krb5_context context,
879 const krb5_principal client,
880 const AS_REQ *asreq,
881 struct pa_info_data *paid,
882 METHOD_DATA *md)
884 struct pa_info_data *p = NULL;
885 int i;
887 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
888 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
889 if (pa == NULL)
890 continue;
891 paid->salt.salttype = pa_prefs[i].type;
892 p = (*pa_prefs[i].salt_info)(context, client, asreq,
893 paid, &pa->padata_value);
895 return p;
898 static krb5_error_code
899 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
900 krb5_enctype etype, krb5_keyblock *key)
902 PA_ENC_TS_ENC p;
903 unsigned char *buf;
904 size_t buf_size;
905 size_t len;
906 EncryptedData encdata;
907 krb5_error_code ret;
908 int32_t usec;
909 int usec2;
910 krb5_crypto crypto;
912 krb5_us_timeofday (context, &p.patimestamp, &usec);
913 usec2 = usec;
914 p.pausec = &usec2;
916 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
917 if (ret)
918 return ret;
919 if(buf_size != len)
920 krb5_abortx(context, "internal error in ASN.1 encoder");
922 ret = krb5_crypto_init(context, key, 0, &crypto);
923 if (ret) {
924 free(buf);
925 return ret;
927 ret = krb5_encrypt_EncryptedData(context,
928 crypto,
929 KRB5_KU_PA_ENC_TIMESTAMP,
930 buf,
931 len,
933 &encdata);
934 free(buf);
935 krb5_crypto_destroy(context, crypto);
936 if (ret)
937 return ret;
939 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
940 free_EncryptedData(&encdata);
941 if (ret)
942 return ret;
943 if(buf_size != len)
944 krb5_abortx(context, "internal error in ASN.1 encoder");
946 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
947 if (ret)
948 free(buf);
949 return ret;
952 static krb5_error_code
953 add_enc_ts_padata(krb5_context context,
954 METHOD_DATA *md,
955 krb5_principal client,
956 krb5_s2k_proc keyproc,
957 krb5_const_pointer keyseed,
958 krb5_enctype *enctypes,
959 unsigned netypes,
960 krb5_salt *salt,
961 krb5_data *s2kparams)
963 krb5_error_code ret;
964 krb5_salt salt2;
965 krb5_enctype *ep;
966 int i;
968 if(salt == NULL) {
969 /* default to standard salt */
970 ret = krb5_get_pw_salt (context, client, &salt2);
971 if (ret)
972 return ret;
973 salt = &salt2;
975 if (!enctypes) {
976 enctypes = context->etypes;
977 netypes = 0;
978 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
979 netypes++;
982 for (i = 0; i < netypes; ++i) {
983 krb5_keyblock *key;
985 ret = (*keyproc)(context, enctypes[i], keyseed,
986 *salt, s2kparams, &key);
987 if (ret)
988 continue;
989 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
990 krb5_free_keyblock (context, key);
991 if (ret)
992 return ret;
994 if(salt == &salt2)
995 krb5_free_salt(context, salt2);
996 return 0;
999 static krb5_error_code
1000 pa_data_to_md_ts_enc(krb5_context context,
1001 const AS_REQ *a,
1002 const krb5_principal client,
1003 krb5_get_init_creds_ctx *ctx,
1004 struct pa_info_data *ppaid,
1005 METHOD_DATA *md)
1007 if (ctx->keyproc == NULL || ctx->keyseed == NULL)
1008 return 0;
1010 if (ppaid) {
1011 add_enc_ts_padata(context, md, client,
1012 ctx->keyproc, ctx->keyseed,
1013 &ppaid->etype, 1,
1014 &ppaid->salt, ppaid->s2kparams);
1015 } else {
1016 krb5_salt salt;
1018 /* make a v5 salted pa-data */
1019 add_enc_ts_padata(context, md, client,
1020 ctx->keyproc, ctx->keyseed,
1021 a->req_body.etype.val, a->req_body.etype.len,
1022 NULL, NULL);
1024 /* make a v4 salted pa-data */
1025 salt.salttype = KRB5_PW_SALT;
1026 krb5_data_zero(&salt.saltvalue);
1027 add_enc_ts_padata(context, md, client,
1028 ctx->keyproc, ctx->keyseed,
1029 a->req_body.etype.val, a->req_body.etype.len,
1030 &salt, NULL);
1032 return 0;
1035 static krb5_error_code
1036 pa_data_to_key_plain(krb5_context context,
1037 const krb5_principal client,
1038 krb5_get_init_creds_ctx *ctx,
1039 krb5_salt salt,
1040 krb5_data *s2kparams,
1041 krb5_enctype etype,
1042 krb5_keyblock **key)
1044 krb5_error_code ret;
1046 ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
1047 salt, s2kparams, key);
1048 return ret;
1052 static krb5_error_code
1053 pa_data_to_md_pkinit(krb5_context context,
1054 const AS_REQ *a,
1055 const krb5_principal client,
1056 krb5_get_init_creds_ctx *ctx,
1057 METHOD_DATA *md)
1059 if (ctx->pk_init_ctx == NULL)
1060 return 0;
1061 #ifdef PKINIT
1062 return _krb5_pk_mk_padata(context,
1063 ctx->pk_init_ctx,
1064 &a->req_body,
1065 ctx->pk_nonce,
1066 md);
1067 #else
1068 krb5_set_error_message(context, EINVAL,
1069 N_("no support for PKINIT compiled in", ""));
1070 return EINVAL;
1071 #endif
1074 static krb5_error_code
1075 pa_data_add_pac_request(krb5_context context,
1076 krb5_get_init_creds_ctx *ctx,
1077 METHOD_DATA *md)
1079 size_t len, length;
1080 krb5_error_code ret;
1081 PA_PAC_REQUEST req;
1082 void *buf;
1084 switch (ctx->req_pac) {
1085 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1086 return 0; /* don't bother */
1087 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1088 req.include_pac = 1;
1089 break;
1090 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1091 req.include_pac = 0;
1094 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1095 &req, &len, ret);
1096 if (ret)
1097 return ret;
1098 if(len != length)
1099 krb5_abortx(context, "internal error in ASN.1 encoder");
1101 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1102 if (ret)
1103 free(buf);
1105 return 0;
1109 * Assumes caller always will free `out_md', even on error.
1112 static krb5_error_code
1113 process_pa_data_to_md(krb5_context context,
1114 const krb5_creds *creds,
1115 const AS_REQ *a,
1116 krb5_get_init_creds_ctx *ctx,
1117 METHOD_DATA *in_md,
1118 METHOD_DATA **out_md,
1119 krb5_prompter_fct prompter,
1120 void *prompter_data)
1122 krb5_error_code ret;
1124 ALLOC(*out_md, 1);
1125 if (*out_md == NULL) {
1126 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1127 return ENOMEM;
1129 (*out_md)->len = 0;
1130 (*out_md)->val = NULL;
1133 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1134 * need to expose our password protecting our PKCS12 key.
1137 if (ctx->pk_init_ctx) {
1139 ret = pa_data_to_md_pkinit(context, a, creds->client, ctx, *out_md);
1140 if (ret)
1141 return ret;
1143 } else if (in_md->len != 0) {
1144 struct pa_info_data paid, *ppaid;
1146 memset(&paid, 0, sizeof(paid));
1148 paid.etype = ENCTYPE_NULL;
1149 ppaid = process_pa_info(context, creds->client, a, &paid, in_md);
1151 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1152 if (ppaid)
1153 free_paid(context, ppaid);
1156 pa_data_add_pac_request(context, ctx, *out_md);
1158 if ((*out_md)->len == 0) {
1159 free(*out_md);
1160 *out_md = NULL;
1163 return 0;
1166 static krb5_error_code
1167 process_pa_data_to_key(krb5_context context,
1168 krb5_get_init_creds_ctx *ctx,
1169 krb5_creds *creds,
1170 AS_REQ *a,
1171 AS_REP *rep,
1172 const krb5_krbhst_info *hi,
1173 krb5_keyblock **key)
1175 struct pa_info_data paid, *ppaid = NULL;
1176 krb5_error_code ret;
1177 krb5_enctype etype;
1178 PA_DATA *pa;
1180 memset(&paid, 0, sizeof(paid));
1182 etype = rep->enc_part.etype;
1184 if (rep->padata) {
1185 paid.etype = etype;
1186 ppaid = process_pa_info(context, creds->client, a, &paid,
1187 rep->padata);
1189 if (ppaid == NULL) {
1190 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1191 if (ret)
1192 return ret;
1193 paid.etype = etype;
1194 paid.s2kparams = NULL;
1197 pa = NULL;
1198 if (rep->padata) {
1199 int idx = 0;
1200 pa = krb5_find_padata(rep->padata->val,
1201 rep->padata->len,
1202 KRB5_PADATA_PK_AS_REP,
1203 &idx);
1204 if (pa == NULL) {
1205 idx = 0;
1206 pa = krb5_find_padata(rep->padata->val,
1207 rep->padata->len,
1208 KRB5_PADATA_PK_AS_REP_19,
1209 &idx);
1212 if (pa && ctx->pk_init_ctx) {
1213 #ifdef PKINIT
1214 ret = _krb5_pk_rd_pa_reply(context,
1215 a->req_body.realm,
1216 ctx->pk_init_ctx,
1217 etype,
1219 ctx->pk_nonce,
1220 &ctx->req_buffer,
1222 key);
1223 #else
1224 ret = EINVAL;
1225 krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", ""));
1226 #endif
1227 } else if (ctx->keyseed)
1228 ret = pa_data_to_key_plain(context, creds->client, ctx,
1229 paid.salt, paid.s2kparams, etype, key);
1230 else {
1231 ret = EINVAL;
1232 krb5_set_error_message(context, ret, N_("No usable pa data type", ""));
1235 free_paid(context, &paid);
1236 return ret;
1240 * Start a new context to get a new initial credential.
1242 * @param context A Kerberos 5 context.
1243 * @param client The Kerberos principal to get the credential for, if
1244 * NULL is given, the default principal is used as determined by
1245 * krb5_get_default_principal().
1246 * @param prompter
1247 * @param prompter_data
1248 * @param start_time the time the ticket should start to be valid or 0 for now.
1249 * @param options a options structure, can be NULL for default options.
1250 * @param rctx A new allocated free with krb5_init_creds_free().
1252 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1254 * @ingroup krb5_credential
1257 krb5_error_code KRB5_LIB_FUNCTION
1258 krb5_init_creds_init(krb5_context context,
1259 krb5_principal client,
1260 krb5_prompter_fct prompter,
1261 void *prompter_data,
1262 krb5_deltat start_time,
1263 krb5_get_init_creds_opt *options,
1264 krb5_init_creds_context *rctx)
1266 krb5_init_creds_context ctx;
1267 krb5_error_code ret;
1269 *rctx = NULL;
1271 ctx = calloc(1, sizeof(*ctx));
1272 if (ctx == NULL) {
1273 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1274 return ENOMEM;
1277 ret = get_init_creds_common(context, client, start_time, options, ctx);
1278 if (ret) {
1279 free(ctx);
1280 return ret;
1283 /* Set a new nonce. */
1284 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1285 ctx->nonce &= 0x7fffffff;
1286 /* XXX these just needs to be the same when using Windows PK-INIT */
1287 ctx->pk_nonce = ctx->nonce;
1289 ctx->prompter = prompter;
1290 ctx->prompter_data = prompter_data;
1292 *rctx = ctx;
1294 return ret;
1298 * Sets the service that the is requested. This call is only neede for
1299 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1301 * @param context a Kerberos 5 context.
1302 * @param ctx a krb5_init_creds_context context.
1303 * @param service the service given as a string, for example
1304 * "kadmind/admin". If NULL, the default krbtgt in the clients
1305 * realm is set.
1307 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1308 * @ingroup krb5_credential
1311 krb5_error_code KRB5_LIB_FUNCTION
1312 krb5_init_creds_set_service(krb5_context context,
1313 krb5_init_creds_context ctx,
1314 const char *service)
1316 krb5_const_realm client_realm;
1317 krb5_principal principal;
1318 krb5_error_code ret;
1320 client_realm = krb5_principal_get_realm (context, ctx->cred.client);
1322 if (service) {
1323 ret = krb5_parse_name (context, service, &principal);
1324 if (ret)
1325 return ret;
1326 krb5_principal_set_realm (context, principal, client_realm);
1327 } else {
1328 ret = krb5_make_principal(context, &principal,
1329 client_realm, KRB5_TGS_NAME, client_realm,
1330 NULL);
1331 if (ret)
1332 return ret;
1336 * This is for Windows RODC that are picky about what name type
1337 * the server principal have, and the really strange part is that
1338 * they are picky about the AS-REQ name type and not the TGS-REQ
1339 * later. Oh well.
1342 if (krb5_principal_is_krbtgt(context, principal))
1343 krb5_principal_set_type(context, principal, KRB5_NT_SRV_INST);
1345 krb5_free_principal(context, ctx->cred.server);
1346 ctx->cred.server = principal;
1348 return 0;
1352 * Sets the password that will use for the request.
1354 * @param context a Kerberos 5 context.
1355 * @param ctx ctx krb5_init_creds_context context.
1356 * @param password the password to use.
1358 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1359 * @ingroup krb5_credential
1362 krb5_error_code KRB5_LIB_FUNCTION
1363 krb5_init_creds_set_password(krb5_context context,
1364 krb5_init_creds_context ctx,
1365 const char *password)
1367 if (ctx->password)
1368 memset(ctx->password, 0, strlen(ctx->password));
1369 if (password) {
1370 ctx->password = strdup(password);
1371 if (ctx->password == NULL) {
1372 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1373 return ENOMEM;
1375 ctx->keyseed = (void *) ctx->password;
1376 } else {
1377 ctx->keyseed = NULL;
1378 ctx->password = NULL;
1381 return 0;
1384 static krb5_error_code
1385 keytab_key_proc(krb5_context context, krb5_enctype enctype,
1386 krb5_const_pointer keyseed,
1387 krb5_salt salt, krb5_data *s2kparms,
1388 krb5_keyblock **key)
1390 krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed);
1391 krb5_keytab keytab = args->keytab;
1392 krb5_principal principal = args->principal;
1393 krb5_error_code ret;
1394 krb5_keytab real_keytab;
1395 krb5_keytab_entry entry;
1397 if(keytab == NULL)
1398 krb5_kt_default(context, &real_keytab);
1399 else
1400 real_keytab = keytab;
1402 ret = krb5_kt_get_entry (context, real_keytab, principal,
1403 0, enctype, &entry);
1405 if (keytab == NULL)
1406 krb5_kt_close (context, real_keytab);
1408 if (ret)
1409 return ret;
1411 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
1412 krb5_kt_free_entry(context, &entry);
1413 return ret;
1418 * Set the keytab to use for authentication.
1420 * @param context a Kerberos 5 context.
1421 * @param ctx ctx krb5_init_creds_context context.
1422 * @param keytab the keytab to read the key from.
1424 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1425 * @ingroup krb5_credential
1428 krb5_error_code KRB5_LIB_FUNCTION
1429 krb5_init_creds_set_keytab(krb5_context context,
1430 krb5_init_creds_context ctx,
1431 krb5_keytab keytab)
1433 krb5_keytab_key_proc_args *a;
1434 krb5_keytab_entry entry;
1435 krb5_kt_cursor cursor;
1436 krb5_enctype *etypes = NULL;
1437 krb5_error_code ret;
1438 size_t netypes = 0;
1439 int kvno = 0;
1441 a = malloc(sizeof(*a));
1442 if (a == NULL) {
1443 krb5_set_error_message(context, ENOMEM,
1444 N_("malloc: out of memory", ""));
1445 return ENOMEM;
1448 a->principal = ctx->cred.client;
1449 a->keytab = keytab;
1451 ctx->keytab_data = a;
1452 ctx->keyseed = (void *)a;
1453 ctx->keyproc = keytab_key_proc;
1456 * We need to the KDC what enctypes we support for this keytab,
1457 * esp if the keytab is really a password based entry, then the
1458 * KDC might have more enctypes in the database then what we have
1459 * in the keytab.
1462 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1463 if(ret)
1464 goto out;
1466 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
1467 void *ptr;
1469 if (!krb5_principal_compare(context, entry.principal, ctx->cred.client))
1470 goto next;
1472 /* check if we ahve this kvno already */
1473 if (entry.vno > kvno) {
1474 /* remove old list of etype */
1475 if (etypes)
1476 free(etypes);
1477 etypes = NULL;
1478 netypes = 0;
1479 kvno = entry.vno;
1480 } else if (entry.vno != kvno)
1481 goto next;
1483 /* check if enctype is supported */
1484 if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0)
1485 goto next;
1487 /* add enctype to supported list */
1488 ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2));
1489 if (ptr == NULL)
1490 goto next;
1492 etypes = ptr;
1493 etypes[netypes] = entry.keyblock.keytype;
1494 etypes[netypes + 1] = ETYPE_NULL;
1495 netypes++;
1496 next:
1497 krb5_kt_free_entry(context, &entry);
1499 krb5_kt_end_seq_get(context, keytab, &cursor);
1501 if (etypes) {
1502 if (ctx->etypes)
1503 free(ctx->etypes);
1504 ctx->etypes = etypes;
1507 out:
1508 return 0;
1511 static krb5_error_code
1512 keyblock_key_proc(krb5_context context, krb5_enctype enctype,
1513 krb5_const_pointer keyseed,
1514 krb5_salt salt, krb5_data *s2kparms,
1515 krb5_keyblock **key)
1517 return krb5_copy_keyblock (context, keyseed, key);
1520 krb5_error_code KRB5_LIB_FUNCTION
1521 krb5_init_creds_set_keyblock(krb5_context context,
1522 krb5_init_creds_context ctx,
1523 krb5_keyblock *keyblock)
1525 ctx->keyseed = (void *)keyblock;
1526 ctx->keyproc = keyblock_key_proc;
1528 return 0;
1532 * The core loop if krb5_get_init_creds() function family. Create the
1533 * packets and have the caller send them off to the KDC.
1535 * If the caller want all work been done for them, use
1536 * krb5_init_creds_get() instead.
1538 * @param context a Kerberos 5 context.
1539 * @param ctx ctx krb5_init_creds_context context.
1540 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
1541 * @param out reply to KDC.
1542 * @param hostinfo KDC address info, first round it can be NULL.
1543 * @param flags status of the round, if 1 is set, continue one more round.
1545 * @return 0 for success, or an Kerberos 5 error code, see
1546 * krb5_get_error_message().
1548 * @ingroup krb5_credential
1551 krb5_error_code KRB5_LIB_FUNCTION
1552 krb5_init_creds_step(krb5_context context,
1553 krb5_init_creds_context ctx,
1554 krb5_data *in,
1555 krb5_data *out,
1556 krb5_krbhst_info *hostinfo,
1557 unsigned int *flags)
1559 krb5_error_code ret;
1560 size_t len;
1561 size_t size;
1563 krb5_data_zero(out);
1565 if (ctx->as_req.req_body.cname == NULL) {
1566 ret = init_as_req(context, ctx->flags, &ctx->cred,
1567 ctx->addrs, ctx->etypes, &ctx->as_req);
1568 if (ret) {
1569 free_init_creds_ctx(context, ctx);
1570 return ret;
1574 #define MAX_PA_COUNTER 10
1575 if (ctx->pa_counter > MAX_PA_COUNTER) {
1576 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1577 N_("Looping %d times while getting "
1578 "initial credentials", ""),
1579 ctx->pa_counter);
1580 return KRB5_GET_IN_TKT_LOOP;
1582 ctx->pa_counter++;
1584 /* Lets process the input packet */
1585 if (in && in->length) {
1586 krb5_kdc_rep rep;
1588 memset(&rep, 0, sizeof(rep));
1590 ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
1591 if (ret == 0) {
1592 krb5_keyblock *key = NULL;
1593 unsigned eflags = EXTRACT_TICKET_AS_REQ;
1595 if (ctx->flags.canonicalize) {
1596 eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1597 eflags |= EXTRACT_TICKET_MATCH_REALM;
1599 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1600 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1602 ret = process_pa_data_to_key(context, ctx, &ctx->cred,
1603 &ctx->as_req, &rep.kdc_rep, hostinfo, &key);
1604 if (ret) {
1605 free_AS_REP(&rep.kdc_rep);
1606 goto out;
1609 ret = _krb5_extract_ticket(context,
1610 &rep,
1611 &ctx->cred,
1612 key,
1613 NULL,
1614 KRB5_KU_AS_REP_ENC_PART,
1615 NULL,
1616 ctx->nonce,
1617 eflags,
1618 NULL,
1619 NULL);
1620 krb5_free_keyblock(context, key);
1622 *flags = 0;
1624 if (ret == 0)
1625 ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
1627 free_AS_REP(&rep.kdc_rep);
1628 free_EncASRepPart(&rep.enc_part);
1630 return ret;
1632 } else {
1633 /* let's try to parse it as a KRB-ERROR */
1635 free_KRB_ERROR(&ctx->error);
1637 ret = krb5_rd_error(context, in, &ctx->error);
1638 if(ret && in->length && ((char*)in->data)[0] == 4)
1639 ret = KRB5KRB_AP_ERR_V4_REPLY;
1640 if (ret)
1641 goto out;
1643 ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);
1646 * If no preauth was set and KDC requires it, give it one
1647 * more try.
1650 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1652 free_METHOD_DATA(&ctx->md);
1653 memset(&ctx->md, 0, sizeof(ctx->md));
1655 if (ctx->error.e_data) {
1656 ret = decode_METHOD_DATA(ctx->error.e_data->data,
1657 ctx->error.e_data->length,
1658 &ctx->md,
1659 NULL);
1660 if (ret)
1661 krb5_set_error_message(context, ret,
1662 N_("Failed to decode METHOD-DATA", ""));
1663 } else {
1664 krb5_set_error_message(context, ret,
1665 N_("Preauth required but no preauth "
1666 "options send by KDC", ""));
1668 } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
1670 * Try adapt to timeskrew when we are using pre-auth, and
1671 * if there was a time skew, try again.
1673 krb5_set_real_time(context, ctx->error.stime, -1);
1674 if (context->kdc_sec_offset)
1675 ret = 0;
1676 } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
1677 /* client referal to a new realm */
1678 if (ctx->error.crealm == NULL) {
1679 krb5_set_error_message(context, ret,
1680 N_("Got a client referral, not but no realm", ""));
1681 goto out;
1683 ret = krb5_principal_set_realm(context,
1684 ctx->cred.client,
1685 *ctx->error.crealm);
1687 if (ret)
1688 goto out;
1692 if (ctx->as_req.padata) {
1693 free_METHOD_DATA(ctx->as_req.padata);
1694 free(ctx->as_req.padata);
1695 ctx->as_req.padata = NULL;
1698 /* Set a new nonce. */
1699 ctx->as_req.req_body.nonce = ctx->nonce;
1701 /* fill_in_md_data */
1702 ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
1703 &ctx->md, &ctx->as_req.padata,
1704 ctx->prompter, ctx->prompter_data);
1705 if (ret)
1706 goto out;
1708 krb5_data_free(&ctx->req_buffer);
1710 ASN1_MALLOC_ENCODE(AS_REQ,
1711 ctx->req_buffer.data, ctx->req_buffer.length,
1712 &ctx->as_req, &len, ret);
1713 if (ret)
1714 goto out;
1715 if(len != ctx->req_buffer.length)
1716 krb5_abortx(context, "internal error in ASN.1 encoder");
1718 out->data = ctx->req_buffer.data;
1719 out->length = ctx->req_buffer.length;
1721 *flags = 1;
1723 return 0;
1724 out:
1725 return ret;
1729 * Extract the newly acquired credentials from krb5_init_creds_context
1730 * context.
1732 * @param context A Kerberos 5 context.
1733 * @param ctx
1734 * @param cred credentials, free with krb5_free_cred_contents().
1736 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
1739 krb5_error_code KRB5_LIB_FUNCTION
1740 krb5_init_creds_get_creds(krb5_context context,
1741 krb5_init_creds_context ctx,
1742 krb5_creds *cred)
1744 return krb5_copy_creds_contents(context, &ctx->cred, cred);
1748 * Get the last error from the transaction.
1750 * @return Returns 0 or an error code
1752 * @ingroup krb5_credential
1755 krb5_error_code KRB5_LIB_FUNCTION
1756 krb5_init_creds_get_error(krb5_context context,
1757 krb5_init_creds_context ctx,
1758 KRB_ERROR *error)
1760 krb5_error_code ret;
1762 ret = copy_KRB_ERROR(&ctx->error, error);
1763 if (ret)
1764 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1766 return ret;
1770 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
1772 * @param context A Kerberos 5 context.
1773 * @param ctx The krb5_init_creds_context to free.
1775 * @ingroup krb5_credential
1778 void KRB5_LIB_FUNCTION
1779 krb5_init_creds_free(krb5_context context,
1780 krb5_init_creds_context ctx)
1782 free_init_creds_ctx(context, ctx);
1783 free(ctx);
1787 * Get new credentials as setup by the krb5_init_creds_context.
1789 * @param context A Kerberos 5 context.
1790 * @param ctx The krb5_init_creds_context to process.
1792 * @ingroup krb5_credential
1795 krb5_error_code KRB5_LIB_FUNCTION
1796 krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
1798 krb5_sendto_ctx stctx = NULL;
1799 krb5_krbhst_info *hostinfo = NULL;
1800 krb5_error_code ret;
1801 krb5_data in, out;
1802 unsigned int flags = 0;
1804 krb5_data_zero(&in);
1805 krb5_data_zero(&out);
1807 ret = krb5_sendto_ctx_alloc(context, &stctx);
1808 if (ret)
1809 goto out;
1810 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1812 while (1) {
1813 flags = 0;
1814 ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
1815 krb5_data_free(&in);
1816 if (ret)
1817 goto out;
1819 if ((flags & 1) == 0)
1820 break;
1822 ret = krb5_sendto_context (context, stctx, &out,
1823 ctx->cred.client->realm, &in);
1824 if (ret)
1825 goto out;
1829 out:
1830 if (stctx)
1831 krb5_sendto_ctx_free(context, stctx);
1833 return ret;
1837 * Get new credentials using password.
1839 * @ingroup krb5_credential
1843 krb5_error_code KRB5_LIB_FUNCTION
1844 krb5_get_init_creds_password(krb5_context context,
1845 krb5_creds *creds,
1846 krb5_principal client,
1847 const char *password,
1848 krb5_prompter_fct prompter,
1849 void *data,
1850 krb5_deltat start_time,
1851 const char *in_tkt_service,
1852 krb5_get_init_creds_opt *options)
1854 krb5_init_creds_context ctx;
1855 char buf[BUFSIZ];
1856 krb5_error_code ret;
1857 int chpw = 0;
1859 again:
1860 ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
1861 if (ret)
1862 goto out;
1864 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1865 if (ret)
1866 goto out;
1868 if (prompter != NULL && ctx->password == NULL && password == NULL) {
1869 krb5_prompt prompt;
1870 krb5_data password_data;
1871 char *p, *q;
1873 krb5_unparse_name (context, client, &p);
1874 asprintf (&q, "%s's Password: ", p);
1875 free (p);
1876 prompt.prompt = q;
1877 password_data.data = buf;
1878 password_data.length = sizeof(buf);
1879 prompt.hidden = 1;
1880 prompt.reply = &password_data;
1881 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1883 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1884 free (q);
1885 if (ret) {
1886 memset (buf, 0, sizeof(buf));
1887 ret = KRB5_LIBOS_PWDINTR;
1888 krb5_clear_error_message (context);
1889 goto out;
1891 password = password_data.data;
1894 if (password) {
1895 ret = krb5_init_creds_set_password(context, ctx, password);
1896 if (ret)
1897 goto out;
1900 ret = krb5_init_creds_get(context, ctx);
1902 if (ret == 0)
1903 process_last_request(context, options, ctx);
1906 if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
1907 char buf[1024];
1909 /* try to avoid recursion */
1910 if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
1911 goto out;
1913 /* don't try to change password where then where none */
1914 if (prompter == NULL)
1915 goto out;
1917 ret = change_password (context,
1918 client,
1919 ctx->password,
1920 buf,
1921 sizeof(buf),
1922 prompter,
1923 data,
1924 options);
1925 if (ret)
1926 goto out;
1927 chpw = 1;
1928 krb5_init_creds_free(context, ctx);
1929 goto again;
1932 out:
1933 if (ret == 0)
1934 krb5_init_creds_get_creds(context, ctx, creds);
1936 if (ctx)
1937 krb5_init_creds_free(context, ctx);
1939 memset(buf, 0, sizeof(buf));
1940 return ret;
1944 * Get new credentials using keyblock.
1946 * @ingroup krb5_credential
1949 krb5_error_code KRB5_LIB_FUNCTION
1950 krb5_get_init_creds_keyblock(krb5_context context,
1951 krb5_creds *creds,
1952 krb5_principal client,
1953 krb5_keyblock *keyblock,
1954 krb5_deltat start_time,
1955 const char *in_tkt_service,
1956 krb5_get_init_creds_opt *options)
1958 krb5_init_creds_context ctx;
1959 krb5_error_code ret;
1961 memset(creds, 0, sizeof(*creds));
1963 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
1964 if (ret)
1965 goto out;
1967 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1968 if (ret)
1969 goto out;
1971 ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
1972 if (ret)
1973 goto out;
1975 ret = krb5_init_creds_get(context, ctx);
1977 if (ret == 0)
1978 process_last_request(context, options, ctx);
1980 out:
1981 if (ret == 0)
1982 krb5_init_creds_get_creds(context, ctx, creds);
1984 if (ctx)
1985 krb5_init_creds_free(context, ctx);
1987 return ret;
1991 * Get new credentials using keytab.
1993 * @ingroup krb5_credential
1996 krb5_error_code KRB5_LIB_FUNCTION
1997 krb5_get_init_creds_keytab(krb5_context context,
1998 krb5_creds *creds,
1999 krb5_principal client,
2000 krb5_keytab keytab,
2001 krb5_deltat start_time,
2002 const char *in_tkt_service,
2003 krb5_get_init_creds_opt *options)
2005 krb5_init_creds_context ctx;
2006 krb5_error_code ret;
2008 memset(creds, 0, sizeof(*creds));
2010 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2011 if (ret)
2012 goto out;
2014 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2015 if (ret)
2016 goto out;
2018 ret = krb5_init_creds_set_keytab(context, ctx, keytab);
2019 if (ret)
2020 goto out;
2022 ret = krb5_init_creds_get(context, ctx);
2023 if (ret == 0)
2024 process_last_request(context, options, ctx);
2026 out:
2027 if (ret == 0)
2028 krb5_init_creds_get_creds(context, ctx, creds);
2030 if (ctx)
2031 krb5_init_creds_free(context, ctx);
2033 return ret;