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