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