2 * Copyright (c) 1997 - 2003 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.55.2.1 2004/08/30 23:21:07 lha Exp $");
39 get_config_time (krb5_context context,
46 ret = krb5_config_get_time (context, NULL,
53 ret = krb5_config_get_time (context, NULL,
62 static krb5_error_code
63 init_cred (krb5_context context,
65 krb5_principal client,
66 krb5_deltat start_time,
67 const char *in_tkt_service,
68 krb5_get_init_creds_opt *options)
71 krb5_realm *client_realm;
75 krb5_timeofday (context, &now);
77 memset (cred, 0, sizeof(*cred));
80 krb5_copy_principal(context, client, &cred->client);
82 ret = krb5_get_default_principal (context,
88 client_realm = krb5_princ_realm (context, cred->client);
91 cred->times.starttime = now + start_time;
93 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
94 tmp = options->tkt_life;
97 cred->times.endtime = now + tmp;
99 if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE) &&
100 options->renew_life > 0) {
101 cred->times.renew_till = now + options->renew_life;
104 if (in_tkt_service) {
105 krb5_realm server_realm;
107 ret = krb5_parse_name (context, in_tkt_service, &cred->server);
110 server_realm = strdup (*client_realm);
111 free (*krb5_princ_realm(context, cred->server));
112 krb5_princ_set_realm (context, cred->server, &server_realm);
114 ret = krb5_make_principal(context, &cred->server,
115 *client_realm, KRB5_TGS_NAME, *client_realm,
123 krb5_free_creds_contents (context, cred);
128 * Print a message (str) to the user about the expiration in `lr'
132 report_expiration (krb5_context context,
133 krb5_prompter_fct prompter,
140 asprintf (&p, "%s%s", str, ctime(&time));
141 (*prompter) (context, data, NULL, p, 0, NULL);
146 * Parse the last_req data and show it to the user if it's interesting
150 print_expire (krb5_context context,
153 krb5_prompter_fct prompter,
157 LastReq *lr = &rep->enc_part.last_req;
160 krb5_boolean reported = FALSE;
162 krb5_timeofday (context, &sec);
164 t = sec + get_config_time (context,
169 for (i = 0; i < lr->len; ++i) {
170 if (lr->val[i].lr_value <= t) {
171 switch (abs(lr->val[i].lr_type)) {
173 report_expiration(context, prompter, data,
174 "Your password will expire at ",
175 lr->val[i].lr_value);
178 case LR_ACCT_EXPTIME :
179 report_expiration(context, prompter, data,
180 "Your account will expire at ",
181 lr->val[i].lr_value);
189 && rep->enc_part.key_expiration
190 && *rep->enc_part.key_expiration <= t) {
191 report_expiration(context, prompter, data,
192 "Your password/account will expire at ",
193 *rep->enc_part.key_expiration);
197 static krb5_error_code
198 get_init_creds_common(krb5_context context,
200 krb5_principal client,
201 krb5_deltat start_time,
202 const char *in_tkt_service,
203 krb5_get_init_creds_opt *options,
204 krb5_addresses **addrs,
205 krb5_enctype **etypes,
207 krb5_preauthtype **pre_auth_types,
208 krb5_kdc_flags *flags)
211 krb5_realm *client_realm;
212 krb5_get_init_creds_opt default_opt;
214 if (options == NULL) {
215 krb5_get_init_creds_opt_init (&default_opt);
216 options = &default_opt;
219 ret = init_cred (context, cred, client, start_time,
220 in_tkt_service, options);
224 client_realm = krb5_princ_realm (context, cred->client);
228 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
229 flags->b.forwardable = options->forwardable;
231 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
232 flags->b.proxiable = options->proxiable;
235 flags->b.postdated = 1;
236 if (cred->times.renew_till)
237 flags->b.renewable = 1;
238 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST)
239 *addrs = options->address_list;
240 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
241 *etypes = malloc((options->etype_list_length + 1)
242 * sizeof(krb5_enctype));
243 if (*etypes == NULL) {
244 krb5_set_error_string(context, "malloc: out of memory");
247 memcpy (*etypes, options->etype_list,
248 options->etype_list_length * sizeof(krb5_enctype));
249 (*etypes)[options->etype_list_length] = ETYPE_NULL;
251 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
252 *pre_auth_types = malloc((options->preauth_list_length + 1)
253 * sizeof(krb5_preauthtype));
254 if (*pre_auth_types == NULL) {
255 krb5_set_error_string(context, "malloc: out of memory");
258 memcpy (*pre_auth_types, options->preauth_list,
259 options->preauth_list_length * sizeof(krb5_preauthtype));
260 (*pre_auth_types)[options->preauth_list_length] = KRB5_PADATA_NONE;
262 if (options->flags & KRB5_GET_INIT_CREDS_OPT_SALT)
264 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
265 flags->b.request_anonymous = options->anonymous;
269 static krb5_error_code
270 change_password (krb5_context context,
271 krb5_principal client,
272 const char *password,
275 krb5_prompter_fct prompter,
277 krb5_get_init_creds_opt *old_options)
279 krb5_prompt prompts[2];
282 char buf1[BUFSIZ], buf2[BUFSIZ];
283 krb5_data password_data[2];
285 krb5_data result_code_string;
286 krb5_data result_string;
288 krb5_get_init_creds_opt options;
290 memset (&cpw_cred, 0, sizeof(cpw_cred));
292 krb5_get_init_creds_opt_init (&options);
293 krb5_get_init_creds_opt_set_tkt_life (&options, 60);
294 krb5_get_init_creds_opt_set_forwardable (&options, FALSE);
295 krb5_get_init_creds_opt_set_proxiable (&options, FALSE);
296 if (old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
297 krb5_get_init_creds_opt_set_preauth_list (&options,
298 old_options->preauth_list,
299 old_options->preauth_list_length);
301 krb5_data_zero (&result_code_string);
302 krb5_data_zero (&result_string);
304 ret = krb5_get_init_creds_password (context,
317 password_data[0].data = buf1;
318 password_data[0].length = sizeof(buf1);
320 prompts[0].hidden = 1;
321 prompts[0].prompt = "New password: ";
322 prompts[0].reply = &password_data[0];
323 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
325 password_data[1].data = buf2;
326 password_data[1].length = sizeof(buf2);
328 prompts[1].hidden = 1;
329 prompts[1].prompt = "Repeat new password: ";
330 prompts[1].reply = &password_data[1];
331 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
333 ret = (*prompter) (context, data, NULL, "Changing password",
336 memset (buf1, 0, sizeof(buf1));
337 memset (buf2, 0, sizeof(buf2));
341 if (strcmp (buf1, buf2) == 0)
343 memset (buf1, 0, sizeof(buf1));
344 memset (buf2, 0, sizeof(buf2));
347 ret = krb5_change_password (context,
355 asprintf (&p, "%s: %.*s\n",
356 result_code ? "Error" : "Success",
357 (int)result_string.length,
358 (char*)result_string.data);
360 ret = (*prompter) (context, data, NULL, p, 0, NULL);
362 if (result_code == 0) {
363 strlcpy (newpw, buf1, newpw_sz);
366 krb5_set_error_string (context, "failed changing password");
371 memset (buf1, 0, sizeof(buf1));
372 memset (buf2, 0, sizeof(buf2));
373 krb5_data_free (&result_string);
374 krb5_data_free (&result_code_string);
375 krb5_free_creds_contents (context, &cpw_cred);
380 krb5_get_init_creds_password(krb5_context context,
382 krb5_principal client,
383 const char *password,
384 krb5_prompter_fct prompter,
386 krb5_deltat start_time,
387 const char *in_tkt_service,
388 krb5_get_init_creds_opt *options)
391 krb5_kdc_flags flags;
392 krb5_addresses *addrs = NULL;
393 krb5_enctype *etypes = NULL;
394 krb5_preauthtype *pre_auth_types = NULL;
395 krb5_creds this_cred;
396 krb5_kdc_rep kdc_reply;
398 krb5_data password_data;
401 memset(&kdc_reply, 0, sizeof(kdc_reply));
403 ret = get_init_creds_common(context, creds, client, start_time,
404 in_tkt_service, options,
405 &addrs, &etypes, &this_cred, &pre_auth_types,
410 if (password == NULL) {
414 krb5_unparse_name (context, this_cred.client, &p);
415 asprintf (&q, "%s's Password: ", p);
418 password_data.data = buf;
419 password_data.length = sizeof(buf);
421 prompt.reply = &password_data;
422 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
424 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
427 memset (buf, 0, sizeof(buf));
428 ret = KRB5_LIBOS_PWDINTR;
429 krb5_clear_error_string (context);
432 password = password_data.data;
437 memset(&kdc_reply, 0, sizeof(kdc_reply));
438 ret = krb5_get_in_cred (context,
444 krb5_password_key_proc,
454 case KRB5KDC_ERR_KEY_EXPIRED :
455 /* try to avoid recursion */
457 if (prompter == NULL)
460 krb5_clear_error_string (context);
462 if (in_tkt_service != NULL
463 && strcmp (in_tkt_service, "kadmin/changepw") == 0)
466 ret = change_password (context,
484 print_expire (context,
485 krb5_princ_realm (context, this_cred.client),
490 memset (buf, 0, sizeof(buf));
492 krb5_free_kdc_rep (context, &kdc_reply);
494 free (pre_auth_types);
496 if (ret == 0 && creds)
499 krb5_free_creds_contents (context, &this_cred);
504 krb5_keyblock_key_proc (krb5_context context,
507 krb5_const_pointer keyseed,
510 return krb5_copy_keyblock (context, keyseed, key);
514 krb5_get_init_creds_keytab(krb5_context context,
516 krb5_principal client,
518 krb5_deltat start_time,
519 const char *in_tkt_service,
520 krb5_get_init_creds_opt *options)
523 krb5_kdc_flags flags;
524 krb5_addresses *addrs = NULL;
525 krb5_enctype *etypes = NULL;
526 krb5_preauthtype *pre_auth_types = NULL;
527 krb5_creds this_cred;
528 krb5_keytab_key_proc_args *a;
530 ret = get_init_creds_common(context, creds, client, start_time,
531 in_tkt_service, options,
532 &addrs, &etypes, &this_cred, &pre_auth_types,
537 a = malloc (sizeof(*a));
539 krb5_set_error_string(context, "malloc: out of memory");
543 a->principal = this_cred.client;
546 ret = krb5_get_in_cred (context,
552 krb5_keytab_key_proc,
562 free (pre_auth_types);
567 krb5_free_creds_contents (context, &this_cred);
571 free (pre_auth_types);
573 krb5_free_creds_contents (context, &this_cred);