1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/*
* $Header$
*
* Copyright 2008 Massachusetts Institute of Technology.
* All Rights Reserved.
*
* Export of this software from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
* distribute this software and its documentation for any purpose and
* without fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of M.I.T. not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*/
#ifndef LEAN_CLIENT
#include "kim_private.h"
// ---------------------------------------------------------------------------
static kim_error kim_ui_cli_read_string (kim_string *out_string,
kim_boolean in_hide_reply,
const char *in_format, ...)
{
kim_error err = KIM_NO_ERROR;
krb5_context k5context = NULL;
krb5_prompt prompts[1];
char prompt_string [BUFSIZ];
krb5_data reply_data;
char reply_string [BUFSIZ];
if (!err && !out_string) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !in_format ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err) {
err = krb5_init_context (&k5context);
}
if (!err) {
unsigned int count;
va_list args;
va_start (args, in_format);
count = vsnprintf (prompt_string, sizeof (prompt_string),
in_format, args);
va_end (args);
if (count > sizeof (prompt_string)) {
kim_debug_printf ("%s(): WARNING! Prompt should be %d characters\n",
__FUNCTION__, count);
prompt_string [sizeof (prompt_string) - 1] = '\0';
}
}
if (!err) {
/* Build the prompt structures */
prompts[0].prompt = prompt_string;
prompts[0].hidden = in_hide_reply;
prompts[0].reply = &reply_data;
prompts[0].reply->data = reply_string;
prompts[0].reply->length = sizeof (reply_string);
err = krb5_prompter_posix (k5context, NULL, NULL, NULL, 1, prompts);
if (err == KRB5_LIBOS_PWDINTR) { err = check_error (KIM_USER_CANCELED_ERR); }
}
if (!err) {
err = kim_string_create_from_buffer (out_string,
prompts[0].reply->data,
prompts[0].reply->length);
}
if (k5context) { krb5_free_context (k5context); }
return check_error (err);
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_init (kim_ui_context *io_context)
{
if (io_context) {
io_context->tcontext = NULL;
}
return KIM_NO_ERROR;
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_enter_identity (kim_ui_context *in_context,
kim_identity *out_identity)
{
kim_error err = KIM_NO_ERROR;
kim_string enter_identity_string = NULL;
kim_string identity_string = NULL;
if (!err && !out_identity) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err) {
err = kim_os_string_create_localized (&enter_identity_string,
"Please enter your Kerberos identity");
}
if (!err) {
err = kim_ui_cli_read_string (&identity_string,
0, enter_identity_string);
}
if (!err) {
err = kim_identity_create_from_string (out_identity, identity_string);
}
kim_string_free (&identity_string);
kim_string_free (&enter_identity_string);
return check_error (err);
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_select_identity (kim_ui_context *in_context,
kim_selection_hints in_hints,
kim_identity *out_identity)
{
kim_error err = KIM_NO_ERROR;
if (!err && !in_hints ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !out_identity) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err) {
err = kim_ui_cli_enter_identity (in_context, out_identity);
}
return check_error (err);
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_auth_prompt (kim_ui_context *in_context,
kim_identity in_identity,
kim_prompt_type in_type,
kim_boolean in_allow_save_reply,
kim_boolean in_hide_reply,
kim_string in_title,
kim_string in_message,
kim_string in_description,
char **out_reply,
kim_boolean *out_save_reply)
{
kim_error err = KIM_NO_ERROR;
if (!err && !in_identity) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !out_reply ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
/* in_title, in_message or in_description may be NULL */
if (!err) {
if (in_type == kim_prompt_type_password) {
kim_string enter_password_format = NULL;
kim_string identity_string = NULL;
err = kim_os_string_create_localized (&enter_password_format,
"Please enter the password for %s");
if (!err) {
err = kim_identity_get_display_string (in_identity,
&identity_string);
}
if (!err) {
err = kim_ui_cli_read_string ((kim_string *) out_reply,
1, enter_password_format,
identity_string);
}
kim_string_free (&identity_string);
kim_string_free (&enter_password_format);
} else {
krb5_context k5context = NULL;
krb5_prompt prompts[1];
krb5_data reply_data;
char reply_string [BUFSIZ];
prompts[0].prompt = (char *) in_description;
prompts[0].hidden = in_hide_reply;
prompts[0].reply = &reply_data;
prompts[0].reply->data = reply_string;
prompts[0].reply->length = sizeof (reply_string);
err = krb5_init_context (&k5context);
if (!err) {
err = krb5_prompter_posix (k5context, in_context, in_title,
in_message, 1, prompts);
if (err == KRB5_LIBOS_PWDINTR) { err = check_error (KIM_USER_CANCELED_ERR); }
}
if (!err) {
err = kim_string_create_from_buffer ((kim_string *) out_reply,
prompts[0].reply->data,
prompts[0].reply->length);
if (!err) {
/* always allow password saving */
*out_save_reply = (in_allow_save_reply &&
in_type == kim_prompt_type_password);
}
}
if (k5context) { krb5_free_context (k5context); }
}
}
return check_error (err);
}
/* ------------------------------------------------------------------------ */
static kim_error kim_ui_cli_ask_change_password (kim_string in_identity_string)
{
kim_error err = KIM_NO_ERROR;
kim_string ask_change_password = NULL;
kim_string answer_options = NULL;
kim_string yes = NULL;
kim_string no = NULL;
kim_string unknown_response = NULL;
kim_boolean done = 0;
kim_comparison no_comparison, yes_comparison;
if (!err) {
err = kim_os_string_create_localized (&ask_change_password,
"Your password has expired, would you like to change it? (yes/no)");
}
if (!err) {
err = kim_os_string_create_localized (&yes, "yes");
}
if (!err) {
err = kim_os_string_create_localized (&no, "no");
}
if (!err) {
err = kim_os_string_create_localized (&unknown_response,
"%s is not a response I understand. Please try again.");
}
while (!err && !done) {
kim_string answer = NULL;
err = kim_ui_cli_read_string (&answer, 0, ask_change_password);
if (!err) {
err = kim_os_string_compare (answer, no,
1 /* case insensitive */,
&no_comparison);
}
if (!err && kim_comparison_is_equal_to (no_comparison)) {
err = check_error (KIM_USER_CANCELED_ERR);
}
if (!err) {
err = kim_os_string_compare (answer, yes,
1 /* case insensitive */,
&yes_comparison);
}
if (!err) {
if (kim_comparison_is_equal_to (yes_comparison)) {
done = 1;
} else {
fprintf (stdout, unknown_response, answer);
fprintf (stdout, "\n");
}
}
kim_string_free (&answer);
}
kim_string_free (&ask_change_password);
kim_string_free (&answer_options);
kim_string_free (&yes);
kim_string_free (&no);
kim_string_free (&unknown_response);
return check_error (err);
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_change_password (kim_ui_context *in_context,
kim_identity in_identity,
kim_boolean in_old_password_expired,
char **out_old_password,
char **out_new_password,
char **out_verify_password)
{
kim_error err = KIM_NO_ERROR;
kim_string enter_old_password_format = NULL;
kim_string enter_new_password_format = NULL;
kim_string enter_verify_password_format = NULL;
kim_string identity_string = NULL;
kim_string old_password = NULL;
kim_string new_password = NULL;
kim_string verify_password = NULL;
kim_boolean done = 0;
if (!err && !in_identity ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !out_old_password ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !out_new_password ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !out_verify_password) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err) {
err = kim_identity_get_display_string (in_identity, &identity_string);
}
if (!err && in_old_password_expired) {
err = kim_ui_cli_ask_change_password (identity_string);
}
if (!err) {
err = kim_os_string_create_localized (&enter_old_password_format,
"Please enter the old password for %s");
}
if (!err) {
err = kim_os_string_create_localized (&enter_new_password_format,
"Please enter the new password for %s");
}
if (!err) {
err = kim_os_string_create_localized (&enter_verify_password_format,
"Verifying, please re-enter the new password for %s again");
}
while (!err && !done) {
kim_boolean was_prompted = 0; /* ignore because we always prompt */
kim_string_free (&old_password);
err = kim_ui_cli_read_string (&old_password,
1, enter_old_password_format,
identity_string);
if (!err) {
err = kim_credential_create_for_change_password ((kim_credential *) &in_context->tcontext,
in_identity,
old_password,
in_context,
&was_prompted);
}
if (err && err != KIM_USER_CANCELED_ERR) {
/* new creds failed, report error to user */
err = kim_ui_handle_kim_error (in_context, in_identity,
kim_ui_error_type_authentication,
err);
} else {
done = 1;
}
}
if (!err) {
err = kim_ui_cli_read_string (&new_password,
1, enter_new_password_format,
identity_string);
}
if (!err) {
err = kim_ui_cli_read_string (&verify_password,
1, enter_verify_password_format,
identity_string);
}
if (!err) {
*out_old_password = (char *) old_password;
old_password = NULL;
*out_new_password = (char *) new_password;
new_password = NULL;
*out_verify_password = (char *) verify_password;
verify_password = NULL;
}
kim_string_free (&old_password);
kim_string_free (&new_password);
kim_string_free (&verify_password);
kim_string_free (&identity_string);
kim_string_free (&enter_old_password_format);
kim_string_free (&enter_new_password_format);
kim_string_free (&enter_verify_password_format);
return check_error (err);
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_handle_error (kim_ui_context *in_context,
kim_identity in_identity,
kim_error in_error,
kim_string in_error_message,
kim_string in_error_description)
{
kim_error err = KIM_NO_ERROR;
if (!err && !in_error_message ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err && !in_error_description) { err = check_error (KIM_NULL_PARAMETER_ERR); }
if (!err) {
fprintf (stdout, "%s\n%s\n\n", in_error_message, in_error_description);
}
return check_error (err);
}
/* ------------------------------------------------------------------------ */
void kim_ui_cli_free_string (kim_ui_context *in_context,
char **io_string)
{
kim_string_free ((kim_string *) io_string);
}
/* ------------------------------------------------------------------------ */
kim_error kim_ui_cli_fini (kim_ui_context *io_context)
{
if (io_context) {
kim_credential_free ((kim_credential *) &io_context->tcontext);
}
return KIM_NO_ERROR;
}
#endif /* LEAN_CLIENT */
|