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