2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
36 RCSID("$Id: init_creds_pw.c,v 1.47 2001/05/14 06:14:48 assar Exp $");
39 get_config_time (krb5_context context,
46 ret = krb5_config_get_time (context, NULL,
53 ret = krb5_config_get_time (context, NULL,
63 get_config_bool (krb5_context context,
67 return krb5_config_get_bool (context,
73 || krb5_config_get_bool (context,
80 static krb5_error_code
81 init_cred (krb5_context context,
83 krb5_principal client,
84 krb5_deltat start_time,
85 const char *in_tkt_service,
86 krb5_get_init_creds_opt *options)
89 krb5_realm *client_realm;
93 krb5_timeofday (context, &now);
95 memset (cred, 0, sizeof(*cred));
98 krb5_copy_principal(context, client, &cred->client);
100 ret = krb5_get_default_principal (context,
106 client_realm = krb5_princ_realm (context, cred->client);
109 cred->times.starttime = now + start_time;
111 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
112 tmp = options->tkt_life;
114 tmp = get_config_time (context,
118 cred->times.endtime = now + tmp;
121 if (options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE)
122 tmp = options->renew_life;
124 tmp = get_config_time (context,
129 cred->times.renew_till = now + tmp;
131 if (in_tkt_service) {
132 krb5_realm server_realm;
134 ret = krb5_parse_name (context, in_tkt_service, &cred->server);
137 server_realm = strdup (*client_realm);
138 free (cred->server->realm);
139 krb5_princ_set_realm (context, cred->server, &server_realm);
141 ret = krb5_make_principal(context, &cred->server,
142 *client_realm, KRB5_TGS_NAME, *client_realm,
150 krb5_free_creds_contents (context, cred);
155 * Parse the last_req data and show it to the user if it's interesting
159 print_expire (krb5_context context,
162 krb5_prompter_fct prompter,
166 LastReq *lr = &rep->enc_part.last_req;
170 krb5_timeofday (context, &sec);
172 t = sec + get_config_time (context,
177 for (i = 0; i < lr->len; ++i) {
178 if (abs(lr->val[i].lr_type) == LR_PW_EXPTIME
179 && lr->val[i].lr_value <= t) {
181 time_t tmp = lr->val[i].lr_value;
183 asprintf (&p, "Your password will expire at %s", ctime(&tmp));
184 (*prompter) (context, data, NULL, p, 0, NULL);
190 if (rep->enc_part.key_expiration
191 && *rep->enc_part.key_expiration <= t) {
193 time_t t = *rep->enc_part.key_expiration;
195 asprintf (&p, "Your password/account will expire at %s", ctime(&t));
196 (*prompter) (context, data, NULL, p, 0, NULL);
201 static krb5_error_code
202 get_init_creds_common(krb5_context context,
204 krb5_principal client,
205 krb5_deltat start_time,
206 const char *in_tkt_service,
207 krb5_get_init_creds_opt *options,
208 krb5_addresses **addrs,
209 krb5_enctype **etypes,
211 krb5_preauthtype **pre_auth_types,
212 krb5_kdc_flags *flags)
215 krb5_realm *client_realm;
216 krb5_get_init_creds_opt default_opt;
218 if (options == NULL) {
219 krb5_get_init_creds_opt_init (&default_opt);
220 options = &default_opt;
223 ret = init_cred (context, cred, client, start_time,
224 in_tkt_service, options);
228 client_realm = krb5_princ_realm (context, cred->client);
232 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
233 flags->b.forwardable = options->forwardable;
235 flags->b.forwardable = get_config_bool (context,
239 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
240 flags->b.proxiable = options->proxiable;
242 flags->b.proxiable = get_config_bool (context,
247 flags->b.postdated = 1;
248 if (cred->times.renew_till)
249 flags->b.renewable = 1;
250 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST)
251 *addrs = options->address_list;
252 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
253 *etypes = malloc((options->etype_list_length + 1)
254 * sizeof(krb5_enctype));
255 if (*etypes == NULL) {
256 krb5_set_error_string(context, "malloc: out of memory");
259 memcpy (*etypes, options->etype_list,
260 options->etype_list_length * sizeof(krb5_enctype));
261 (*etypes)[options->etype_list_length] = ETYPE_NULL;
263 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
264 *pre_auth_types = malloc((options->preauth_list_length + 1)
265 * sizeof(krb5_preauthtype));
266 if (*pre_auth_types == NULL) {
267 krb5_set_error_string(context, "malloc: out of memory");
270 memcpy (*pre_auth_types, options->preauth_list,
271 options->preauth_list_length * sizeof(krb5_preauthtype));
272 (*pre_auth_types)[options->preauth_list_length] = KRB5_PADATA_NONE;
274 if (options->flags & KRB5_GET_INIT_CREDS_OPT_SALT)
276 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
277 flags->b.request_anonymous = options->anonymous;
281 static krb5_error_code
282 change_password (krb5_context context,
283 krb5_principal client,
284 const char *password,
287 krb5_prompter_fct prompter,
289 krb5_get_init_creds_opt *old_options)
291 krb5_prompt prompts[2];
294 char buf1[BUFSIZ], buf2[BUFSIZ];
295 krb5_data password_data;
297 krb5_data result_code_string;
298 krb5_data result_string;
300 krb5_get_init_creds_opt options;
302 memset (&cpw_cred, 0, sizeof(cpw_cred));
304 krb5_get_init_creds_opt_init (&options);
305 krb5_get_init_creds_opt_set_tkt_life (&options, 60);
306 krb5_get_init_creds_opt_set_forwardable (&options, FALSE);
307 krb5_get_init_creds_opt_set_proxiable (&options, FALSE);
308 if (old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
309 krb5_get_init_creds_opt_set_preauth_list (&options,
310 old_options->preauth_list,
311 old_options->preauth_list_length);
313 krb5_data_zero (&result_code_string);
314 krb5_data_zero (&result_string);
316 ret = krb5_get_init_creds_password (context,
329 password_data.data = buf1;
330 password_data.length = sizeof(buf1);
332 prompts[0].hidden = 1;
333 prompts[0].prompt = "New password: ";
334 prompts[0].reply = &password_data;
335 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
337 password_data.data = buf2;
338 password_data.length = sizeof(buf2);
340 prompts[1].hidden = 1;
341 prompts[1].prompt = "Repeat new password: ";
342 prompts[1].reply = &password_data;
343 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
345 ret = (*prompter) (context, data, NULL, "Changing password",
348 memset (buf1, 0, sizeof(buf1));
349 memset (buf2, 0, sizeof(buf2));
353 if (strcmp (buf1, buf2) == 0)
355 memset (buf1, 0, sizeof(buf1));
356 memset (buf2, 0, sizeof(buf2));
359 ret = krb5_change_password (context,
367 asprintf (&p, "%s: %.*s\n",
368 result_code ? "Error" : "Success",
369 (int)result_string.length,
370 (char*)result_string.data);
372 ret = (*prompter) (context, data, NULL, p, 0, NULL);
374 if (result_code == 0) {
375 strlcpy (newpw, buf1, newpw_sz);
378 krb5_set_error_string (context, "failed changing password");
383 memset (buf1, 0, sizeof(buf1));
384 memset (buf2, 0, sizeof(buf2));
385 krb5_data_free (&result_string);
386 krb5_data_free (&result_code_string);
387 krb5_free_creds_contents (context, &cpw_cred);
392 krb5_get_init_creds_password(krb5_context context,
394 krb5_principal client,
395 const char *password,
396 krb5_prompter_fct prompter,
398 krb5_deltat start_time,
399 const char *in_tkt_service,
400 krb5_get_init_creds_opt *options)
403 krb5_kdc_flags flags;
404 krb5_addresses *addrs = NULL;
405 krb5_enctype *etypes = NULL;
406 krb5_preauthtype *pre_auth_types = NULL;
407 krb5_creds this_cred;
408 krb5_kdc_rep kdc_reply;
410 krb5_data password_data;
413 ret = get_init_creds_common(context, creds, client, start_time,
414 in_tkt_service, options,
415 &addrs, &etypes, &this_cred, &pre_auth_types,
420 if (password == NULL) {
424 krb5_unparse_name (context, this_cred.client, &p);
425 asprintf (&prompt.prompt, "%s's Password: ", p);
427 password_data.data = buf;
428 password_data.length = sizeof(buf);
430 prompt.reply = &password_data;
431 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
433 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
434 free (prompt.prompt);
436 memset (buf, 0, sizeof(buf));
437 ret = KRB5_LIBOS_PWDINTR;
438 krb5_clear_error_string (context);
441 password = password_data.data;
446 memset(&kdc_reply, 0, sizeof(kdc_reply));
447 ret = krb5_get_in_cred (context,
453 krb5_password_key_proc,
463 case KRB5KDC_ERR_KEY_EXPIRED :
464 /* try to avoid recursion */
466 krb5_clear_error_string (context);
468 if (in_tkt_service != NULL
469 && strcmp (in_tkt_service, "kadmin/changepw") == 0)
472 ret = change_password (context,
490 print_expire (context,
491 krb5_princ_realm (context, this_cred.client),
496 memset (buf, 0, sizeof(buf));
498 krb5_free_kdc_rep (context, &kdc_reply);
500 free (pre_auth_types);
502 if (ret == 0 && creds)
505 krb5_free_creds_contents (context, &this_cred);
510 krb5_keyblock_key_proc (krb5_context context,
513 krb5_const_pointer keyseed,
516 return krb5_copy_keyblock (context, keyseed, key);
520 krb5_get_init_creds_keytab(krb5_context context,
522 krb5_principal client,
524 krb5_deltat start_time,
525 const char *in_tkt_service,
526 krb5_get_init_creds_opt *options)
529 krb5_kdc_flags flags;
530 krb5_addresses *addrs = NULL;
531 krb5_enctype *etypes = NULL;
532 krb5_preauthtype *pre_auth_types = NULL;
533 krb5_creds this_cred;
534 krb5_keytab_key_proc_args *a;
536 ret = get_init_creds_common(context, creds, client, start_time,
537 in_tkt_service, options,
538 &addrs, &etypes, &this_cred, &pre_auth_types,
543 a = malloc (sizeof(*a));
545 krb5_set_error_string(context, "malloc: out of memory");
549 a->principal = this_cred.client;
552 ret = krb5_get_in_cred (context,
558 krb5_keytab_key_proc,
566 free (pre_auth_types);
571 krb5_free_creds_contents (context, &this_cred);
575 free (pre_auth_types);
577 krb5_free_creds_contents (context, &this_cred);