]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ui/ui_lib.c
d2732f5bf2bfc69ff7e9910adcabc811f65b3f91
[thirdparty/openssl.git] / crypto / ui / ui_lib.c
1 /* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */
2 /*
3 * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
4 * 2001.
5 */
6 /* ====================================================================
7 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60 #include <string.h>
61 #include "cryptlib.h"
62 #include <openssl/e_os2.h>
63 #include <openssl/buffer.h>
64 #include <openssl/ui.h>
65 #include <openssl/err.h>
66 #include "ui_locl.h"
67
68 static const UI_METHOD *default_UI_meth = NULL;
69
70 UI *UI_new(void)
71 {
72 return (UI_new_method(NULL));
73 }
74
75 UI *UI_new_method(const UI_METHOD *method)
76 {
77 UI *ret = OPENSSL_malloc(sizeof(UI));
78
79 if (ret == NULL) {
80 UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE);
81 return NULL;
82 }
83 if (method == NULL)
84 ret->meth = UI_get_default_method();
85 else
86 ret->meth = method;
87
88 ret->strings = NULL;
89 ret->user_data = NULL;
90 ret->flags = 0;
91 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
92 return ret;
93 }
94
95 static void free_string(UI_STRING *uis)
96 {
97 if (uis->flags & OUT_STRING_FREEABLE) {
98 OPENSSL_free((char *)uis->out_string);
99 switch (uis->type) {
100 case UIT_BOOLEAN:
101 OPENSSL_free((char *)uis->_.boolean_data.action_desc);
102 OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
103 OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
104 break;
105 default:
106 break;
107 }
108 }
109 OPENSSL_free(uis);
110 }
111
112 void UI_free(UI *ui)
113 {
114 if (ui == NULL)
115 return;
116 sk_UI_STRING_pop_free(ui->strings, free_string);
117 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
118 OPENSSL_free(ui);
119 }
120
121 static int allocate_string_stack(UI *ui)
122 {
123 if (ui->strings == NULL) {
124 ui->strings = sk_UI_STRING_new_null();
125 if (ui->strings == NULL) {
126 return -1;
127 }
128 }
129 return 0;
130 }
131
132 static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
133 int prompt_freeable,
134 enum UI_string_types type,
135 int input_flags, char *result_buf)
136 {
137 UI_STRING *ret = NULL;
138
139 if (prompt == NULL) {
140 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, ERR_R_PASSED_NULL_PARAMETER);
141 } else if ((type == UIT_PROMPT || type == UIT_VERIFY
142 || type == UIT_BOOLEAN) && result_buf == NULL) {
143 UIerr(UI_F_GENERAL_ALLOCATE_PROMPT, UI_R_NO_RESULT_BUFFER);
144 } else if ((ret = OPENSSL_malloc(sizeof(UI_STRING)))) {
145 ret->out_string = prompt;
146 ret->flags = prompt_freeable ? OUT_STRING_FREEABLE : 0;
147 ret->input_flags = input_flags;
148 ret->type = type;
149 ret->result_buf = result_buf;
150 }
151 return ret;
152 }
153
154 static int general_allocate_string(UI *ui, const char *prompt,
155 int prompt_freeable,
156 enum UI_string_types type, int input_flags,
157 char *result_buf, int minsize, int maxsize,
158 const char *test_buf)
159 {
160 int ret = -1;
161 UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
162 type, input_flags, result_buf);
163
164 if (s) {
165 if (allocate_string_stack(ui) >= 0) {
166 s->_.string_data.result_minsize = minsize;
167 s->_.string_data.result_maxsize = maxsize;
168 s->_.string_data.test_buf = test_buf;
169 ret = sk_UI_STRING_push(ui->strings, s);
170 /* sk_push() returns 0 on error. Let's addapt that */
171 if (ret <= 0)
172 ret--;
173 } else
174 free_string(s);
175 }
176 return ret;
177 }
178
179 static int general_allocate_boolean(UI *ui,
180 const char *prompt,
181 const char *action_desc,
182 const char *ok_chars,
183 const char *cancel_chars,
184 int prompt_freeable,
185 enum UI_string_types type,
186 int input_flags, char *result_buf)
187 {
188 int ret = -1;
189 UI_STRING *s;
190 const char *p;
191
192 if (ok_chars == NULL) {
193 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
194 } else if (cancel_chars == NULL) {
195 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, ERR_R_PASSED_NULL_PARAMETER);
196 } else {
197 for (p = ok_chars; *p; p++) {
198 if (strchr(cancel_chars, *p)) {
199 UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
200 UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
201 }
202 }
203
204 s = general_allocate_prompt(ui, prompt, prompt_freeable,
205 type, input_flags, result_buf);
206
207 if (s) {
208 if (allocate_string_stack(ui) >= 0) {
209 s->_.boolean_data.action_desc = action_desc;
210 s->_.boolean_data.ok_chars = ok_chars;
211 s->_.boolean_data.cancel_chars = cancel_chars;
212 ret = sk_UI_STRING_push(ui->strings, s);
213 /*
214 * sk_push() returns 0 on error. Let's addapt that
215 */
216 if (ret <= 0)
217 ret--;
218 } else
219 free_string(s);
220 }
221 }
222 return ret;
223 }
224
225 /*
226 * Returns the index to the place in the stack or -1 for error. Uses a
227 * direct reference to the prompt.
228 */
229 int UI_add_input_string(UI *ui, const char *prompt, int flags,
230 char *result_buf, int minsize, int maxsize)
231 {
232 return general_allocate_string(ui, prompt, 0,
233 UIT_PROMPT, flags, result_buf, minsize,
234 maxsize, NULL);
235 }
236
237 /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
238 int UI_dup_input_string(UI *ui, const char *prompt, int flags,
239 char *result_buf, int minsize, int maxsize)
240 {
241 char *prompt_copy = NULL;
242
243 if (prompt) {
244 prompt_copy = BUF_strdup(prompt);
245 if (prompt_copy == NULL) {
246 UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE);
247 return 0;
248 }
249 }
250
251 return general_allocate_string(ui, prompt_copy, 1,
252 UIT_PROMPT, flags, result_buf, minsize,
253 maxsize, NULL);
254 }
255
256 int UI_add_verify_string(UI *ui, const char *prompt, int flags,
257 char *result_buf, int minsize, int maxsize,
258 const char *test_buf)
259 {
260 return general_allocate_string(ui, prompt, 0,
261 UIT_VERIFY, flags, result_buf, minsize,
262 maxsize, test_buf);
263 }
264
265 int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
266 char *result_buf, int minsize, int maxsize,
267 const char *test_buf)
268 {
269 char *prompt_copy = NULL;
270
271 if (prompt) {
272 prompt_copy = BUF_strdup(prompt);
273 if (prompt_copy == NULL) {
274 UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE);
275 return -1;
276 }
277 }
278
279 return general_allocate_string(ui, prompt_copy, 1,
280 UIT_VERIFY, flags, result_buf, minsize,
281 maxsize, test_buf);
282 }
283
284 int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
285 const char *ok_chars, const char *cancel_chars,
286 int flags, char *result_buf)
287 {
288 return general_allocate_boolean(ui, prompt, action_desc,
289 ok_chars, cancel_chars, 0, UIT_BOOLEAN,
290 flags, result_buf);
291 }
292
293 int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
294 const char *ok_chars, const char *cancel_chars,
295 int flags, char *result_buf)
296 {
297 char *prompt_copy = NULL;
298 char *action_desc_copy = NULL;
299 char *ok_chars_copy = NULL;
300 char *cancel_chars_copy = NULL;
301
302 if (prompt) {
303 prompt_copy = BUF_strdup(prompt);
304 if (prompt_copy == NULL) {
305 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
306 goto err;
307 }
308 }
309
310 if (action_desc) {
311 action_desc_copy = BUF_strdup(action_desc);
312 if (action_desc_copy == NULL) {
313 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
314 goto err;
315 }
316 }
317
318 if (ok_chars) {
319 ok_chars_copy = BUF_strdup(ok_chars);
320 if (ok_chars_copy == NULL) {
321 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
322 goto err;
323 }
324 }
325
326 if (cancel_chars) {
327 cancel_chars_copy = BUF_strdup(cancel_chars);
328 if (cancel_chars_copy == NULL) {
329 UIerr(UI_F_UI_DUP_INPUT_BOOLEAN, ERR_R_MALLOC_FAILURE);
330 goto err;
331 }
332 }
333
334 return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
335 ok_chars_copy, cancel_chars_copy, 1,
336 UIT_BOOLEAN, flags, result_buf);
337 err:
338 if (prompt_copy)
339 OPENSSL_free(prompt_copy);
340 if (action_desc_copy)
341 OPENSSL_free(action_desc_copy);
342 if (ok_chars_copy)
343 OPENSSL_free(ok_chars_copy);
344 if (cancel_chars_copy)
345 OPENSSL_free(cancel_chars_copy);
346 return -1;
347 }
348
349 int UI_add_info_string(UI *ui, const char *text)
350 {
351 return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
352 NULL);
353 }
354
355 int UI_dup_info_string(UI *ui, const char *text)
356 {
357 char *text_copy = NULL;
358
359 if (text) {
360 text_copy = BUF_strdup(text);
361 if (text_copy == NULL) {
362 UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE);
363 return -1;
364 }
365 }
366
367 return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
368 0, 0, NULL);
369 }
370
371 int UI_add_error_string(UI *ui, const char *text)
372 {
373 return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
374 NULL);
375 }
376
377 int UI_dup_error_string(UI *ui, const char *text)
378 {
379 char *text_copy = NULL;
380
381 if (text) {
382 text_copy = BUF_strdup(text);
383 if (text_copy == NULL) {
384 UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE);
385 return -1;
386 }
387 }
388 return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
389 0, 0, NULL);
390 }
391
392 char *UI_construct_prompt(UI *ui, const char *object_desc,
393 const char *object_name)
394 {
395 char *prompt = NULL;
396
397 if (ui->meth->ui_construct_prompt)
398 prompt = ui->meth->ui_construct_prompt(ui, object_desc, object_name);
399 else {
400 char prompt1[] = "Enter ";
401 char prompt2[] = " for ";
402 char prompt3[] = ":";
403 int len = 0;
404
405 if (object_desc == NULL)
406 return NULL;
407 len = sizeof(prompt1) - 1 + strlen(object_desc);
408 if (object_name)
409 len += sizeof(prompt2) - 1 + strlen(object_name);
410 len += sizeof(prompt3) - 1;
411
412 prompt = OPENSSL_malloc(len + 1);
413 if (prompt == NULL)
414 return NULL;
415 BUF_strlcpy(prompt, prompt1, len + 1);
416 BUF_strlcat(prompt, object_desc, len + 1);
417 if (object_name) {
418 BUF_strlcat(prompt, prompt2, len + 1);
419 BUF_strlcat(prompt, object_name, len + 1);
420 }
421 BUF_strlcat(prompt, prompt3, len + 1);
422 }
423 return prompt;
424 }
425
426 void *UI_add_user_data(UI *ui, void *user_data)
427 {
428 void *old_data = ui->user_data;
429 ui->user_data = user_data;
430 return old_data;
431 }
432
433 void *UI_get0_user_data(UI *ui)
434 {
435 return ui->user_data;
436 }
437
438 const char *UI_get0_result(UI *ui, int i)
439 {
440 if (i < 0) {
441 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL);
442 return NULL;
443 }
444 if (i >= sk_UI_STRING_num(ui->strings)) {
445 UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE);
446 return NULL;
447 }
448 return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
449 }
450
451 static int print_error(const char *str, size_t len, UI *ui)
452 {
453 UI_STRING uis;
454
455 memset(&uis, 0, sizeof(uis));
456 uis.type = UIT_ERROR;
457 uis.out_string = str;
458
459 if (ui->meth->ui_write_string && !ui->meth->ui_write_string(ui, &uis))
460 return -1;
461 return 0;
462 }
463
464 int UI_process(UI *ui)
465 {
466 int i, ok = 0;
467
468 if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
469 return -1;
470
471 if (ui->flags & UI_FLAG_PRINT_ERRORS)
472 ERR_print_errors_cb((int (*)(const char *, size_t, void *))
473 print_error, (void *)ui);
474
475 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
476 if (ui->meth->ui_write_string
477 && !ui->meth->ui_write_string(ui,
478 sk_UI_STRING_value(ui->strings, i)))
479 {
480 ok = -1;
481 goto err;
482 }
483 }
484
485 if (ui->meth->ui_flush)
486 switch (ui->meth->ui_flush(ui)) {
487 case -1: /* Interrupt/Cancel/something... */
488 ok = -2;
489 goto err;
490 case 0: /* Errors */
491 ok = -1;
492 goto err;
493 default: /* Success */
494 ok = 0;
495 break;
496 }
497
498 for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) {
499 if (ui->meth->ui_read_string) {
500 switch (ui->meth->ui_read_string(ui,
501 sk_UI_STRING_value(ui->strings,
502 i))) {
503 case -1: /* Interrupt/Cancel/something... */
504 ok = -2;
505 goto err;
506 case 0: /* Errors */
507 ok = -1;
508 goto err;
509 default: /* Success */
510 ok = 0;
511 break;
512 }
513 }
514 }
515 err:
516 if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
517 return -1;
518 return ok;
519 }
520
521 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f) (void))
522 {
523 if (ui == NULL) {
524 UIerr(UI_F_UI_CTRL, ERR_R_PASSED_NULL_PARAMETER);
525 return -1;
526 }
527 switch (cmd) {
528 case UI_CTRL_PRINT_ERRORS:
529 {
530 int save_flag = ! !(ui->flags & UI_FLAG_PRINT_ERRORS);
531 if (i)
532 ui->flags |= UI_FLAG_PRINT_ERRORS;
533 else
534 ui->flags &= ~UI_FLAG_PRINT_ERRORS;
535 return save_flag;
536 }
537 case UI_CTRL_IS_REDOABLE:
538 return ! !(ui->flags & UI_FLAG_REDOABLE);
539 default:
540 break;
541 }
542 UIerr(UI_F_UI_CTRL, UI_R_UNKNOWN_CONTROL_COMMAND);
543 return -1;
544 }
545
546 int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
547 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
548 {
549 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
550 new_func, dup_func, free_func);
551 }
552
553 int UI_set_ex_data(UI *r, int idx, void *arg)
554 {
555 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
556 }
557
558 void *UI_get_ex_data(UI *r, int idx)
559 {
560 return (CRYPTO_get_ex_data(&r->ex_data, idx));
561 }
562
563 void UI_set_default_method(const UI_METHOD *meth)
564 {
565 default_UI_meth = meth;
566 }
567
568 const UI_METHOD *UI_get_default_method(void)
569 {
570 if (default_UI_meth == NULL) {
571 default_UI_meth = UI_OpenSSL();
572 }
573 return default_UI_meth;
574 }
575
576 const UI_METHOD *UI_get_method(UI *ui)
577 {
578 return ui->meth;
579 }
580
581 const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
582 {
583 ui->meth = meth;
584 return ui->meth;
585 }
586
587 UI_METHOD *UI_create_method(char *name)
588 {
589 UI_METHOD *ui_method = OPENSSL_malloc(sizeof(UI_METHOD));
590
591 if (ui_method) {
592 memset(ui_method, 0, sizeof(*ui_method));
593 ui_method->name = BUF_strdup(name);
594 }
595 return ui_method;
596 }
597
598 /*
599 * BIG FSCKING WARNING!!!! If you use this on a statically allocated method
600 * (that is, it hasn't been allocated using UI_create_method(), you deserve
601 * anything Murphy can throw at you and more! You have been warned.
602 */
603 void UI_destroy_method(UI_METHOD *ui_method)
604 {
605 OPENSSL_free(ui_method->name);
606 ui_method->name = NULL;
607 OPENSSL_free(ui_method);
608 }
609
610 int UI_method_set_opener(UI_METHOD *method, int (*opener) (UI *ui))
611 {
612 if (method) {
613 method->ui_open_session = opener;
614 return 0;
615 } else
616 return -1;
617 }
618
619 int UI_method_set_writer(UI_METHOD *method,
620 int (*writer) (UI *ui, UI_STRING *uis))
621 {
622 if (method) {
623 method->ui_write_string = writer;
624 return 0;
625 } else
626 return -1;
627 }
628
629 int UI_method_set_flusher(UI_METHOD *method, int (*flusher) (UI *ui))
630 {
631 if (method) {
632 method->ui_flush = flusher;
633 return 0;
634 } else
635 return -1;
636 }
637
638 int UI_method_set_reader(UI_METHOD *method,
639 int (*reader) (UI *ui, UI_STRING *uis))
640 {
641 if (method) {
642 method->ui_read_string = reader;
643 return 0;
644 } else
645 return -1;
646 }
647
648 int UI_method_set_closer(UI_METHOD *method, int (*closer) (UI *ui))
649 {
650 if (method) {
651 method->ui_close_session = closer;
652 return 0;
653 } else
654 return -1;
655 }
656
657 int UI_method_set_prompt_constructor(UI_METHOD *method,
658 char *(*prompt_constructor) (UI *ui,
659 const char
660 *object_desc,
661 const char
662 *object_name))
663 {
664 if (method) {
665 method->ui_construct_prompt = prompt_constructor;
666 return 0;
667 } else
668 return -1;
669 }
670
671 int (*UI_method_get_opener(UI_METHOD *method)) (UI *) {
672 if (method)
673 return method->ui_open_session;
674 else
675 return NULL;
676 }
677
678 int (*UI_method_get_writer(UI_METHOD *method)) (UI *, UI_STRING *) {
679 if (method)
680 return method->ui_write_string;
681 else
682 return NULL;
683 }
684
685 int (*UI_method_get_flusher(UI_METHOD *method)) (UI *) {
686 if (method)
687 return method->ui_flush;
688 else
689 return NULL;
690 }
691
692 int (*UI_method_get_reader(UI_METHOD *method)) (UI *, UI_STRING *) {
693 if (method)
694 return method->ui_read_string;
695 else
696 return NULL;
697 }
698
699 int (*UI_method_get_closer(UI_METHOD *method)) (UI *) {
700 if (method)
701 return method->ui_close_session;
702 else
703 return NULL;
704 }
705
706 char *(*UI_method_get_prompt_constructor(UI_METHOD *method)) (UI *,
707 const char *,
708 const char *) {
709 if (method)
710 return method->ui_construct_prompt;
711 else
712 return NULL;
713 }
714
715 enum UI_string_types UI_get_string_type(UI_STRING *uis)
716 {
717 if (!uis)
718 return UIT_NONE;
719 return uis->type;
720 }
721
722 int UI_get_input_flags(UI_STRING *uis)
723 {
724 if (!uis)
725 return 0;
726 return uis->input_flags;
727 }
728
729 const char *UI_get0_output_string(UI_STRING *uis)
730 {
731 if (!uis)
732 return NULL;
733 return uis->out_string;
734 }
735
736 const char *UI_get0_action_string(UI_STRING *uis)
737 {
738 if (!uis)
739 return NULL;
740 switch (uis->type) {
741 case UIT_PROMPT:
742 case UIT_BOOLEAN:
743 return uis->_.boolean_data.action_desc;
744 default:
745 return NULL;
746 }
747 }
748
749 const char *UI_get0_result_string(UI_STRING *uis)
750 {
751 if (!uis)
752 return NULL;
753 switch (uis->type) {
754 case UIT_PROMPT:
755 case UIT_VERIFY:
756 return uis->result_buf;
757 default:
758 return NULL;
759 }
760 }
761
762 const char *UI_get0_test_string(UI_STRING *uis)
763 {
764 if (!uis)
765 return NULL;
766 switch (uis->type) {
767 case UIT_VERIFY:
768 return uis->_.string_data.test_buf;
769 default:
770 return NULL;
771 }
772 }
773
774 int UI_get_result_minsize(UI_STRING *uis)
775 {
776 if (!uis)
777 return -1;
778 switch (uis->type) {
779 case UIT_PROMPT:
780 case UIT_VERIFY:
781 return uis->_.string_data.result_minsize;
782 default:
783 return -1;
784 }
785 }
786
787 int UI_get_result_maxsize(UI_STRING *uis)
788 {
789 if (!uis)
790 return -1;
791 switch (uis->type) {
792 case UIT_PROMPT:
793 case UIT_VERIFY:
794 return uis->_.string_data.result_maxsize;
795 default:
796 return -1;
797 }
798 }
799
800 int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
801 {
802 int l = strlen(result);
803
804 ui->flags &= ~UI_FLAG_REDOABLE;
805
806 if (!uis)
807 return -1;
808 switch (uis->type) {
809 case UIT_PROMPT:
810 case UIT_VERIFY:
811 {
812 char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1];
813 char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1];
814
815 BIO_snprintf(number1, sizeof(number1), "%d",
816 uis->_.string_data.result_minsize);
817 BIO_snprintf(number2, sizeof(number2), "%d",
818 uis->_.string_data.result_maxsize);
819
820 if (l < uis->_.string_data.result_minsize) {
821 ui->flags |= UI_FLAG_REDOABLE;
822 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL);
823 ERR_add_error_data(5, "You must type in ",
824 number1, " to ", number2, " characters");
825 return -1;
826 }
827 if (l > uis->_.string_data.result_maxsize) {
828 ui->flags |= UI_FLAG_REDOABLE;
829 UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE);
830 ERR_add_error_data(5, "You must type in ",
831 number1, " to ", number2, " characters");
832 return -1;
833 }
834 }
835
836 if (!uis->result_buf) {
837 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
838 return -1;
839 }
840
841 BUF_strlcpy(uis->result_buf, result,
842 uis->_.string_data.result_maxsize + 1);
843 break;
844 case UIT_BOOLEAN:
845 {
846 const char *p;
847
848 if (!uis->result_buf) {
849 UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER);
850 return -1;
851 }
852
853 uis->result_buf[0] = '\0';
854 for (p = result; *p; p++) {
855 if (strchr(uis->_.boolean_data.ok_chars, *p)) {
856 uis->result_buf[0] = uis->_.boolean_data.ok_chars[0];
857 break;
858 }
859 if (strchr(uis->_.boolean_data.cancel_chars, *p)) {
860 uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0];
861 break;
862 }
863 }
864 }
865 default:
866 break;
867 }
868 return 0;
869 }