xref: /onnv-gate/usr/src/common/openssl/crypto/ui/ui_lib.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/ui/ui_lib.c -*- mode:C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
30Sstevel@tonic-gate  * project 2001.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    openssl-core@openssl.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <string.h>
60*2139Sjp161948 #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/e_os2.h>
620Sstevel@tonic-gate #include <openssl/buffer.h>
630Sstevel@tonic-gate #include <openssl/ui.h>
640Sstevel@tonic-gate #include <openssl/err.h>
650Sstevel@tonic-gate #include "ui_locl.h"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate IMPLEMENT_STACK_OF(UI_STRING_ST)
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static const UI_METHOD *default_UI_meth=NULL;
700Sstevel@tonic-gate 
UI_new(void)710Sstevel@tonic-gate UI *UI_new(void)
720Sstevel@tonic-gate 	{
730Sstevel@tonic-gate 	return(UI_new_method(NULL));
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 
UI_new_method(const UI_METHOD * method)760Sstevel@tonic-gate UI *UI_new_method(const UI_METHOD *method)
770Sstevel@tonic-gate 	{
780Sstevel@tonic-gate 	UI *ret;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	ret=(UI *)OPENSSL_malloc(sizeof(UI));
810Sstevel@tonic-gate 	if (ret == NULL)
820Sstevel@tonic-gate 		{
830Sstevel@tonic-gate 		UIerr(UI_F_UI_NEW_METHOD,ERR_R_MALLOC_FAILURE);
840Sstevel@tonic-gate 		return NULL;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	if (method == NULL)
870Sstevel@tonic-gate 		ret->meth=UI_get_default_method();
880Sstevel@tonic-gate 	else
890Sstevel@tonic-gate 		ret->meth=method;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	ret->strings=NULL;
920Sstevel@tonic-gate 	ret->user_data=NULL;
930Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data);
940Sstevel@tonic-gate 	return ret;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
free_string(UI_STRING * uis)970Sstevel@tonic-gate static void free_string(UI_STRING *uis)
980Sstevel@tonic-gate 	{
990Sstevel@tonic-gate 	if (uis->flags & OUT_STRING_FREEABLE)
1000Sstevel@tonic-gate 		{
1010Sstevel@tonic-gate 		OPENSSL_free((char *)uis->out_string);
1020Sstevel@tonic-gate 		switch(uis->type)
1030Sstevel@tonic-gate 			{
1040Sstevel@tonic-gate 		case UIT_BOOLEAN:
1050Sstevel@tonic-gate 			OPENSSL_free((char *)uis->_.boolean_data.action_desc);
1060Sstevel@tonic-gate 			OPENSSL_free((char *)uis->_.boolean_data.ok_chars);
1070Sstevel@tonic-gate 			OPENSSL_free((char *)uis->_.boolean_data.cancel_chars);
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 		default:
1100Sstevel@tonic-gate 			break;
1110Sstevel@tonic-gate 			}
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 	OPENSSL_free(uis);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
UI_free(UI * ui)1160Sstevel@tonic-gate void UI_free(UI *ui)
1170Sstevel@tonic-gate 	{
1180Sstevel@tonic-gate 	if (ui == NULL)
1190Sstevel@tonic-gate 		return;
1200Sstevel@tonic-gate 	sk_UI_STRING_pop_free(ui->strings,free_string);
1210Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_UI, ui, &ui->ex_data);
1220Sstevel@tonic-gate 	OPENSSL_free(ui);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 
allocate_string_stack(UI * ui)1250Sstevel@tonic-gate static int allocate_string_stack(UI *ui)
1260Sstevel@tonic-gate 	{
1270Sstevel@tonic-gate 	if (ui->strings == NULL)
1280Sstevel@tonic-gate 		{
1290Sstevel@tonic-gate 		ui->strings=sk_UI_STRING_new_null();
1300Sstevel@tonic-gate 		if (ui->strings == NULL)
1310Sstevel@tonic-gate 			{
1320Sstevel@tonic-gate 			return -1;
1330Sstevel@tonic-gate 			}
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	return 0;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
general_allocate_prompt(UI * ui,const char * prompt,int prompt_freeable,enum UI_string_types type,int input_flags,char * result_buf)1380Sstevel@tonic-gate static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt,
1390Sstevel@tonic-gate 	int prompt_freeable, enum UI_string_types type, int input_flags,
1400Sstevel@tonic-gate 	char *result_buf)
1410Sstevel@tonic-gate 	{
1420Sstevel@tonic-gate 	UI_STRING *ret = NULL;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (prompt == NULL)
1450Sstevel@tonic-gate 		{
1460Sstevel@tonic-gate 		UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,ERR_R_PASSED_NULL_PARAMETER);
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 	else if ((type == UIT_PROMPT || type == UIT_VERIFY
1490Sstevel@tonic-gate 			 || type == UIT_BOOLEAN) && result_buf == NULL)
1500Sstevel@tonic-gate 		{
1510Sstevel@tonic-gate 		UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,UI_R_NO_RESULT_BUFFER);
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 	else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING))))
1540Sstevel@tonic-gate 		{
1550Sstevel@tonic-gate 		ret->out_string=prompt;
1560Sstevel@tonic-gate 		ret->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0;
1570Sstevel@tonic-gate 		ret->input_flags=input_flags;
1580Sstevel@tonic-gate 		ret->type=type;
1590Sstevel@tonic-gate 		ret->result_buf=result_buf;
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 	return ret;
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
general_allocate_string(UI * ui,const char * prompt,int prompt_freeable,enum UI_string_types type,int input_flags,char * result_buf,int minsize,int maxsize,const char * test_buf)1640Sstevel@tonic-gate static int general_allocate_string(UI *ui, const char *prompt,
1650Sstevel@tonic-gate 	int prompt_freeable, enum UI_string_types type, int input_flags,
1660Sstevel@tonic-gate 	char *result_buf, int minsize, int maxsize, const char *test_buf)
1670Sstevel@tonic-gate 	{
1680Sstevel@tonic-gate 	int ret = -1;
1690Sstevel@tonic-gate 	UI_STRING *s = general_allocate_prompt(ui, prompt, prompt_freeable,
1700Sstevel@tonic-gate 		type, input_flags, result_buf);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (s)
1730Sstevel@tonic-gate 		{
1740Sstevel@tonic-gate 		if (allocate_string_stack(ui) >= 0)
1750Sstevel@tonic-gate 			{
1760Sstevel@tonic-gate 			s->_.string_data.result_minsize=minsize;
1770Sstevel@tonic-gate 			s->_.string_data.result_maxsize=maxsize;
1780Sstevel@tonic-gate 			s->_.string_data.test_buf=test_buf;
1790Sstevel@tonic-gate 			ret=sk_UI_STRING_push(ui->strings, s);
1800Sstevel@tonic-gate 			/* sk_push() returns 0 on error.  Let's addapt that */
1810Sstevel@tonic-gate 			if (ret <= 0) ret--;
1820Sstevel@tonic-gate 			}
1830Sstevel@tonic-gate 		else
1840Sstevel@tonic-gate 			free_string(s);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 	return ret;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
general_allocate_boolean(UI * ui,const char * prompt,const char * action_desc,const char * ok_chars,const char * cancel_chars,int prompt_freeable,enum UI_string_types type,int input_flags,char * result_buf)1890Sstevel@tonic-gate static int general_allocate_boolean(UI *ui,
1900Sstevel@tonic-gate 	const char *prompt, const char *action_desc,
1910Sstevel@tonic-gate 	const char *ok_chars, const char *cancel_chars,
1920Sstevel@tonic-gate 	int prompt_freeable, enum UI_string_types type, int input_flags,
1930Sstevel@tonic-gate 	char *result_buf)
1940Sstevel@tonic-gate 	{
1950Sstevel@tonic-gate 	int ret = -1;
1960Sstevel@tonic-gate 	UI_STRING *s;
1970Sstevel@tonic-gate 	const char *p;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (ok_chars == NULL)
2000Sstevel@tonic-gate 		{
2010Sstevel@tonic-gate 		UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	else if (cancel_chars == NULL)
2040Sstevel@tonic-gate 		{
2050Sstevel@tonic-gate 		UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER);
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	else
2080Sstevel@tonic-gate 		{
2090Sstevel@tonic-gate 		for(p = ok_chars; *p; p++)
2100Sstevel@tonic-gate 			{
2110Sstevel@tonic-gate 			if (strchr(cancel_chars, *p))
2120Sstevel@tonic-gate 				{
2130Sstevel@tonic-gate 				UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,
2140Sstevel@tonic-gate 					UI_R_COMMON_OK_AND_CANCEL_CHARACTERS);
2150Sstevel@tonic-gate 				}
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		s = general_allocate_prompt(ui, prompt, prompt_freeable,
2190Sstevel@tonic-gate 			type, input_flags, result_buf);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		if (s)
2220Sstevel@tonic-gate 			{
2230Sstevel@tonic-gate 			if (allocate_string_stack(ui) >= 0)
2240Sstevel@tonic-gate 				{
2250Sstevel@tonic-gate 				s->_.boolean_data.action_desc = action_desc;
2260Sstevel@tonic-gate 				s->_.boolean_data.ok_chars = ok_chars;
2270Sstevel@tonic-gate 				s->_.boolean_data.cancel_chars = cancel_chars;
2280Sstevel@tonic-gate 				ret=sk_UI_STRING_push(ui->strings, s);
2290Sstevel@tonic-gate 				/* sk_push() returns 0 on error.
2300Sstevel@tonic-gate 				   Let's addapt that */
2310Sstevel@tonic-gate 				if (ret <= 0) ret--;
2320Sstevel@tonic-gate 				}
2330Sstevel@tonic-gate 			else
2340Sstevel@tonic-gate 				free_string(s);
2350Sstevel@tonic-gate 			}
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 	return ret;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /* Returns the index to the place in the stack or -1 for error.  Uses a
2410Sstevel@tonic-gate    direct reference to the prompt.  */
UI_add_input_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize)2420Sstevel@tonic-gate int UI_add_input_string(UI *ui, const char *prompt, int flags,
2430Sstevel@tonic-gate 	char *result_buf, int minsize, int maxsize)
2440Sstevel@tonic-gate 	{
2450Sstevel@tonic-gate 	return general_allocate_string(ui, prompt, 0,
2460Sstevel@tonic-gate 		UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /* Same as UI_add_input_string(), excepts it takes a copy of the prompt */
UI_dup_input_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize)2500Sstevel@tonic-gate int UI_dup_input_string(UI *ui, const char *prompt, int flags,
2510Sstevel@tonic-gate 	char *result_buf, int minsize, int maxsize)
2520Sstevel@tonic-gate 	{
2530Sstevel@tonic-gate 	char *prompt_copy=NULL;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (prompt)
2560Sstevel@tonic-gate 		{
2570Sstevel@tonic-gate 		prompt_copy=BUF_strdup(prompt);
2580Sstevel@tonic-gate 		if (prompt_copy == NULL)
2590Sstevel@tonic-gate 			{
2600Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_INPUT_STRING,ERR_R_MALLOC_FAILURE);
2610Sstevel@tonic-gate 			return 0;
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	return general_allocate_string(ui, prompt_copy, 1,
2660Sstevel@tonic-gate 		UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
UI_add_verify_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize,const char * test_buf)2690Sstevel@tonic-gate int UI_add_verify_string(UI *ui, const char *prompt, int flags,
2700Sstevel@tonic-gate 	char *result_buf, int minsize, int maxsize, const char *test_buf)
2710Sstevel@tonic-gate 	{
2720Sstevel@tonic-gate 	return general_allocate_string(ui, prompt, 0,
2730Sstevel@tonic-gate 		UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
UI_dup_verify_string(UI * ui,const char * prompt,int flags,char * result_buf,int minsize,int maxsize,const char * test_buf)2760Sstevel@tonic-gate int UI_dup_verify_string(UI *ui, const char *prompt, int flags,
2770Sstevel@tonic-gate 	char *result_buf, int minsize, int maxsize, const char *test_buf)
2780Sstevel@tonic-gate 	{
2790Sstevel@tonic-gate 	char *prompt_copy=NULL;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if (prompt)
2820Sstevel@tonic-gate 		{
2830Sstevel@tonic-gate 		prompt_copy=BUF_strdup(prompt);
2840Sstevel@tonic-gate 		if (prompt_copy == NULL)
2850Sstevel@tonic-gate 			{
2860Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_VERIFY_STRING,ERR_R_MALLOC_FAILURE);
2870Sstevel@tonic-gate 			return -1;
2880Sstevel@tonic-gate 			}
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	return general_allocate_string(ui, prompt_copy, 1,
2920Sstevel@tonic-gate 		UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
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)2950Sstevel@tonic-gate int UI_add_input_boolean(UI *ui, const char *prompt, const char *action_desc,
2960Sstevel@tonic-gate 	const char *ok_chars, const char *cancel_chars,
2970Sstevel@tonic-gate 	int flags, char *result_buf)
2980Sstevel@tonic-gate 	{
2990Sstevel@tonic-gate 	return general_allocate_boolean(ui, prompt, action_desc,
3000Sstevel@tonic-gate 		ok_chars, cancel_chars, 0, UIT_BOOLEAN, flags, result_buf);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
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)3030Sstevel@tonic-gate int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc,
3040Sstevel@tonic-gate 	const char *ok_chars, const char *cancel_chars,
3050Sstevel@tonic-gate 	int flags, char *result_buf)
3060Sstevel@tonic-gate 	{
3070Sstevel@tonic-gate 	char *prompt_copy = NULL;
3080Sstevel@tonic-gate 	char *action_desc_copy = NULL;
3090Sstevel@tonic-gate 	char *ok_chars_copy = NULL;
3100Sstevel@tonic-gate 	char *cancel_chars_copy = NULL;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (prompt)
3130Sstevel@tonic-gate 		{
3140Sstevel@tonic-gate 		prompt_copy=BUF_strdup(prompt);
3150Sstevel@tonic-gate 		if (prompt_copy == NULL)
3160Sstevel@tonic-gate 			{
3170Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
3180Sstevel@tonic-gate 			goto err;
3190Sstevel@tonic-gate 			}
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (action_desc)
3230Sstevel@tonic-gate 		{
3240Sstevel@tonic-gate 		action_desc_copy=BUF_strdup(action_desc);
3250Sstevel@tonic-gate 		if (action_desc_copy == NULL)
3260Sstevel@tonic-gate 			{
3270Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
3280Sstevel@tonic-gate 			goto err;
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (ok_chars)
3330Sstevel@tonic-gate 		{
3340Sstevel@tonic-gate 		ok_chars_copy=BUF_strdup(ok_chars);
3350Sstevel@tonic-gate 		if (ok_chars_copy == NULL)
3360Sstevel@tonic-gate 			{
3370Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
3380Sstevel@tonic-gate 			goto err;
3390Sstevel@tonic-gate 			}
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if (cancel_chars)
3430Sstevel@tonic-gate 		{
3440Sstevel@tonic-gate 		cancel_chars_copy=BUF_strdup(cancel_chars);
3450Sstevel@tonic-gate 		if (cancel_chars_copy == NULL)
3460Sstevel@tonic-gate 			{
3470Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE);
3480Sstevel@tonic-gate 			goto err;
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return general_allocate_boolean(ui, prompt_copy, action_desc_copy,
3530Sstevel@tonic-gate 		ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags,
3540Sstevel@tonic-gate 		result_buf);
3550Sstevel@tonic-gate  err:
3560Sstevel@tonic-gate 	if (prompt_copy) OPENSSL_free(prompt_copy);
3570Sstevel@tonic-gate 	if (action_desc_copy) OPENSSL_free(action_desc_copy);
3580Sstevel@tonic-gate 	if (ok_chars_copy) OPENSSL_free(ok_chars_copy);
3590Sstevel@tonic-gate 	if (cancel_chars_copy) OPENSSL_free(cancel_chars_copy);
3600Sstevel@tonic-gate 	return -1;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
UI_add_info_string(UI * ui,const char * text)3630Sstevel@tonic-gate int UI_add_info_string(UI *ui, const char *text)
3640Sstevel@tonic-gate 	{
3650Sstevel@tonic-gate 	return general_allocate_string(ui, text, 0, UIT_INFO, 0, NULL, 0, 0,
3660Sstevel@tonic-gate 		NULL);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
UI_dup_info_string(UI * ui,const char * text)3690Sstevel@tonic-gate int UI_dup_info_string(UI *ui, const char *text)
3700Sstevel@tonic-gate 	{
3710Sstevel@tonic-gate 	char *text_copy=NULL;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if (text)
3740Sstevel@tonic-gate 		{
3750Sstevel@tonic-gate 		text_copy=BUF_strdup(text);
3760Sstevel@tonic-gate 		if (text_copy == NULL)
3770Sstevel@tonic-gate 			{
3780Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_INFO_STRING,ERR_R_MALLOC_FAILURE);
3790Sstevel@tonic-gate 			return -1;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL,
3840Sstevel@tonic-gate 		0, 0, NULL);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
UI_add_error_string(UI * ui,const char * text)3870Sstevel@tonic-gate int UI_add_error_string(UI *ui, const char *text)
3880Sstevel@tonic-gate 	{
3890Sstevel@tonic-gate 	return general_allocate_string(ui, text, 0, UIT_ERROR, 0, NULL, 0, 0,
3900Sstevel@tonic-gate 		NULL);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
UI_dup_error_string(UI * ui,const char * text)3930Sstevel@tonic-gate int UI_dup_error_string(UI *ui, const char *text)
3940Sstevel@tonic-gate 	{
3950Sstevel@tonic-gate 	char *text_copy=NULL;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (text)
3980Sstevel@tonic-gate 		{
3990Sstevel@tonic-gate 		text_copy=BUF_strdup(text);
4000Sstevel@tonic-gate 		if (text_copy == NULL)
4010Sstevel@tonic-gate 			{
4020Sstevel@tonic-gate 			UIerr(UI_F_UI_DUP_ERROR_STRING,ERR_R_MALLOC_FAILURE);
4030Sstevel@tonic-gate 			return -1;
4040Sstevel@tonic-gate 			}
4050Sstevel@tonic-gate 		}
4060Sstevel@tonic-gate 	return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL,
4070Sstevel@tonic-gate 		0, 0, NULL);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
UI_construct_prompt(UI * ui,const char * object_desc,const char * object_name)4100Sstevel@tonic-gate char *UI_construct_prompt(UI *ui, const char *object_desc,
4110Sstevel@tonic-gate 	const char *object_name)
4120Sstevel@tonic-gate 	{
4130Sstevel@tonic-gate 	char *prompt = NULL;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (ui->meth->ui_construct_prompt)
4160Sstevel@tonic-gate 		prompt = ui->meth->ui_construct_prompt(ui,
4170Sstevel@tonic-gate 			object_desc, object_name);
4180Sstevel@tonic-gate 	else
4190Sstevel@tonic-gate 		{
4200Sstevel@tonic-gate 		char prompt1[] = "Enter ";
4210Sstevel@tonic-gate 		char prompt2[] = " for ";
4220Sstevel@tonic-gate 		char prompt3[] = ":";
4230Sstevel@tonic-gate 		int len = 0;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		if (object_desc == NULL)
4260Sstevel@tonic-gate 			return NULL;
4270Sstevel@tonic-gate 		len = sizeof(prompt1) - 1 + strlen(object_desc);
4280Sstevel@tonic-gate 		if (object_name)
4290Sstevel@tonic-gate 			len += sizeof(prompt2) - 1 + strlen(object_name);
4300Sstevel@tonic-gate 		len += sizeof(prompt3) - 1;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		prompt = (char *)OPENSSL_malloc(len + 1);
4330Sstevel@tonic-gate 		BUF_strlcpy(prompt, prompt1, len + 1);
4340Sstevel@tonic-gate 		BUF_strlcat(prompt, object_desc, len + 1);
4350Sstevel@tonic-gate 		if (object_name)
4360Sstevel@tonic-gate 			{
4370Sstevel@tonic-gate 			BUF_strlcat(prompt, prompt2, len + 1);
4380Sstevel@tonic-gate 			BUF_strlcat(prompt, object_name, len + 1);
4390Sstevel@tonic-gate 			}
4400Sstevel@tonic-gate 		BUF_strlcat(prompt, prompt3, len + 1);
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 	return prompt;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
UI_add_user_data(UI * ui,void * user_data)4450Sstevel@tonic-gate void *UI_add_user_data(UI *ui, void *user_data)
4460Sstevel@tonic-gate 	{
4470Sstevel@tonic-gate 	void *old_data = ui->user_data;
4480Sstevel@tonic-gate 	ui->user_data = user_data;
4490Sstevel@tonic-gate 	return old_data;
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
UI_get0_user_data(UI * ui)4520Sstevel@tonic-gate void *UI_get0_user_data(UI *ui)
4530Sstevel@tonic-gate 	{
4540Sstevel@tonic-gate 	return ui->user_data;
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
UI_get0_result(UI * ui,int i)4570Sstevel@tonic-gate const char *UI_get0_result(UI *ui, int i)
4580Sstevel@tonic-gate 	{
4590Sstevel@tonic-gate 	if (i < 0)
4600Sstevel@tonic-gate 		{
4610Sstevel@tonic-gate 		UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_SMALL);
4620Sstevel@tonic-gate 		return NULL;
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 	if (i >= sk_UI_STRING_num(ui->strings))
4650Sstevel@tonic-gate 		{
4660Sstevel@tonic-gate 		UIerr(UI_F_UI_GET0_RESULT,UI_R_INDEX_TOO_LARGE);
4670Sstevel@tonic-gate 		return NULL;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 	return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i));
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
print_error(const char * str,size_t len,UI * ui)4720Sstevel@tonic-gate static int print_error(const char *str, size_t len, UI *ui)
4730Sstevel@tonic-gate 	{
4740Sstevel@tonic-gate 	UI_STRING uis;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	memset(&uis, 0, sizeof(uis));
4770Sstevel@tonic-gate 	uis.type = UIT_ERROR;
4780Sstevel@tonic-gate 	uis.out_string = str;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (ui->meth->ui_write_string
4810Sstevel@tonic-gate 		&& !ui->meth->ui_write_string(ui, &uis))
4820Sstevel@tonic-gate 		return -1;
4830Sstevel@tonic-gate 	return 0;
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
UI_process(UI * ui)4860Sstevel@tonic-gate int UI_process(UI *ui)
4870Sstevel@tonic-gate 	{
4880Sstevel@tonic-gate 	int i, ok=0;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (ui->meth->ui_open_session && !ui->meth->ui_open_session(ui))
4910Sstevel@tonic-gate 		return -1;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (ui->flags & UI_FLAG_PRINT_ERRORS)
4940Sstevel@tonic-gate 		ERR_print_errors_cb(
4950Sstevel@tonic-gate 			(int (*)(const char *, size_t, void *))print_error,
4960Sstevel@tonic-gate 			(void *)ui);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
4990Sstevel@tonic-gate 		{
5000Sstevel@tonic-gate 		if (ui->meth->ui_write_string
5010Sstevel@tonic-gate 			&& !ui->meth->ui_write_string(ui,
5020Sstevel@tonic-gate 				sk_UI_STRING_value(ui->strings, i)))
5030Sstevel@tonic-gate 			{
5040Sstevel@tonic-gate 			ok=-1;
5050Sstevel@tonic-gate 			goto err;
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	if (ui->meth->ui_flush)
5100Sstevel@tonic-gate 		switch(ui->meth->ui_flush(ui))
5110Sstevel@tonic-gate 			{
5120Sstevel@tonic-gate 		case -1: /* Interrupt/Cancel/something... */
5130Sstevel@tonic-gate 			ok = -2;
5140Sstevel@tonic-gate 			goto err;
5150Sstevel@tonic-gate 		case 0: /* Errors */
5160Sstevel@tonic-gate 			ok = -1;
5170Sstevel@tonic-gate 			goto err;
5180Sstevel@tonic-gate 		default: /* Success */
5190Sstevel@tonic-gate 			ok = 0;
5200Sstevel@tonic-gate 			break;
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	for(i=0; i<sk_UI_STRING_num(ui->strings); i++)
5240Sstevel@tonic-gate 		{
5250Sstevel@tonic-gate 		if (ui->meth->ui_read_string)
5260Sstevel@tonic-gate 			{
5270Sstevel@tonic-gate 			switch(ui->meth->ui_read_string(ui,
5280Sstevel@tonic-gate 				sk_UI_STRING_value(ui->strings, i)))
5290Sstevel@tonic-gate 				{
5300Sstevel@tonic-gate 			case -1: /* Interrupt/Cancel/something... */
5310Sstevel@tonic-gate 				ok = -2;
5320Sstevel@tonic-gate 				goto err;
5330Sstevel@tonic-gate 			case 0: /* Errors */
5340Sstevel@tonic-gate 				ok = -1;
5350Sstevel@tonic-gate 				goto err;
5360Sstevel@tonic-gate 			default: /* Success */
5370Sstevel@tonic-gate 				ok = 0;
5380Sstevel@tonic-gate 				break;
5390Sstevel@tonic-gate 				}
5400Sstevel@tonic-gate 			}
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate  err:
5430Sstevel@tonic-gate 	if (ui->meth->ui_close_session && !ui->meth->ui_close_session(ui))
5440Sstevel@tonic-gate 		return -1;
5450Sstevel@tonic-gate 	return ok;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
UI_ctrl(UI * ui,int cmd,long i,void * p,void (* f)(void))548*2139Sjp161948 int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void))
5490Sstevel@tonic-gate 	{
5500Sstevel@tonic-gate 	if (ui == NULL)
5510Sstevel@tonic-gate 		{
5520Sstevel@tonic-gate 		UIerr(UI_F_UI_CTRL,ERR_R_PASSED_NULL_PARAMETER);
5530Sstevel@tonic-gate 		return -1;
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 	switch(cmd)
5560Sstevel@tonic-gate 		{
5570Sstevel@tonic-gate 	case UI_CTRL_PRINT_ERRORS:
5580Sstevel@tonic-gate 		{
5590Sstevel@tonic-gate 		int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS);
5600Sstevel@tonic-gate 		if (i)
5610Sstevel@tonic-gate 			ui->flags |= UI_FLAG_PRINT_ERRORS;
5620Sstevel@tonic-gate 		else
5630Sstevel@tonic-gate 			ui->flags &= ~UI_FLAG_PRINT_ERRORS;
5640Sstevel@tonic-gate 		return save_flag;
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 	case UI_CTRL_IS_REDOABLE:
5670Sstevel@tonic-gate 		return !!(ui->flags & UI_FLAG_REDOABLE);
5680Sstevel@tonic-gate 	default:
5690Sstevel@tonic-gate 		break;
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 	UIerr(UI_F_UI_CTRL,UI_R_UNKNOWN_CONTROL_COMMAND);
5720Sstevel@tonic-gate 	return -1;
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
UI_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)5750Sstevel@tonic-gate int UI_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
5760Sstevel@tonic-gate 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
5770Sstevel@tonic-gate         {
5780Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI, argl, argp,
5790Sstevel@tonic-gate 				new_func, dup_func, free_func);
5800Sstevel@tonic-gate         }
5810Sstevel@tonic-gate 
UI_set_ex_data(UI * r,int idx,void * arg)5820Sstevel@tonic-gate int UI_set_ex_data(UI *r, int idx, void *arg)
5830Sstevel@tonic-gate 	{
5840Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
UI_get_ex_data(UI * r,int idx)5870Sstevel@tonic-gate void *UI_get_ex_data(UI *r, int idx)
5880Sstevel@tonic-gate 	{
5890Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&r->ex_data,idx));
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
UI_set_default_method(const UI_METHOD * meth)5920Sstevel@tonic-gate void UI_set_default_method(const UI_METHOD *meth)
5930Sstevel@tonic-gate 	{
5940Sstevel@tonic-gate 	default_UI_meth=meth;
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
UI_get_default_method(void)5970Sstevel@tonic-gate const UI_METHOD *UI_get_default_method(void)
5980Sstevel@tonic-gate 	{
5990Sstevel@tonic-gate 	if (default_UI_meth == NULL)
6000Sstevel@tonic-gate 		{
6010Sstevel@tonic-gate 		default_UI_meth=UI_OpenSSL();
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 	return default_UI_meth;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
UI_get_method(UI * ui)6060Sstevel@tonic-gate const UI_METHOD *UI_get_method(UI *ui)
6070Sstevel@tonic-gate 	{
6080Sstevel@tonic-gate 	return ui->meth;
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
UI_set_method(UI * ui,const UI_METHOD * meth)6110Sstevel@tonic-gate const UI_METHOD *UI_set_method(UI *ui, const UI_METHOD *meth)
6120Sstevel@tonic-gate 	{
6130Sstevel@tonic-gate 	ui->meth=meth;
6140Sstevel@tonic-gate 	return ui->meth;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 
UI_create_method(char * name)6180Sstevel@tonic-gate UI_METHOD *UI_create_method(char *name)
6190Sstevel@tonic-gate 	{
6200Sstevel@tonic-gate 	UI_METHOD *ui_method = (UI_METHOD *)OPENSSL_malloc(sizeof(UI_METHOD));
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if (ui_method)
623*2139Sjp161948 		{
6240Sstevel@tonic-gate 		memset(ui_method, 0, sizeof(*ui_method));
625*2139Sjp161948 		ui_method->name = BUF_strdup(name);
626*2139Sjp161948 		}
6270Sstevel@tonic-gate 	return ui_method;
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /* BIG FSCKING WARNING!!!!  If you use this on a statically allocated method
6310Sstevel@tonic-gate    (that is, it hasn't been allocated using UI_create_method(), you deserve
6320Sstevel@tonic-gate    anything Murphy can throw at you and more!  You have been warned. */
UI_destroy_method(UI_METHOD * ui_method)6330Sstevel@tonic-gate void UI_destroy_method(UI_METHOD *ui_method)
6340Sstevel@tonic-gate 	{
6350Sstevel@tonic-gate 	OPENSSL_free(ui_method->name);
6360Sstevel@tonic-gate 	ui_method->name = NULL;
6370Sstevel@tonic-gate 	OPENSSL_free(ui_method);
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
UI_method_set_opener(UI_METHOD * method,int (* opener)(UI * ui))6400Sstevel@tonic-gate int UI_method_set_opener(UI_METHOD *method, int (*opener)(UI *ui))
6410Sstevel@tonic-gate 	{
6420Sstevel@tonic-gate 	if (method)
6430Sstevel@tonic-gate 		{
6440Sstevel@tonic-gate 		method->ui_open_session = opener;
6450Sstevel@tonic-gate 		return 0;
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 	else
6480Sstevel@tonic-gate 		return -1;
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
UI_method_set_writer(UI_METHOD * method,int (* writer)(UI * ui,UI_STRING * uis))6510Sstevel@tonic-gate int UI_method_set_writer(UI_METHOD *method, int (*writer)(UI *ui, UI_STRING *uis))
6520Sstevel@tonic-gate 	{
6530Sstevel@tonic-gate 	if (method)
6540Sstevel@tonic-gate 		{
6550Sstevel@tonic-gate 		method->ui_write_string = writer;
6560Sstevel@tonic-gate 		return 0;
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 	else
6590Sstevel@tonic-gate 		return -1;
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 
UI_method_set_flusher(UI_METHOD * method,int (* flusher)(UI * ui))6620Sstevel@tonic-gate int UI_method_set_flusher(UI_METHOD *method, int (*flusher)(UI *ui))
6630Sstevel@tonic-gate 	{
6640Sstevel@tonic-gate 	if (method)
6650Sstevel@tonic-gate 		{
6660Sstevel@tonic-gate 		method->ui_flush = flusher;
6670Sstevel@tonic-gate 		return 0;
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 	else
6700Sstevel@tonic-gate 		return -1;
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
UI_method_set_reader(UI_METHOD * method,int (* reader)(UI * ui,UI_STRING * uis))6730Sstevel@tonic-gate int UI_method_set_reader(UI_METHOD *method, int (*reader)(UI *ui, UI_STRING *uis))
6740Sstevel@tonic-gate 	{
6750Sstevel@tonic-gate 	if (method)
6760Sstevel@tonic-gate 		{
6770Sstevel@tonic-gate 		method->ui_read_string = reader;
6780Sstevel@tonic-gate 		return 0;
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 	else
6810Sstevel@tonic-gate 		return -1;
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
UI_method_set_closer(UI_METHOD * method,int (* closer)(UI * ui))6840Sstevel@tonic-gate int UI_method_set_closer(UI_METHOD *method, int (*closer)(UI *ui))
6850Sstevel@tonic-gate 	{
6860Sstevel@tonic-gate 	if (method)
6870Sstevel@tonic-gate 		{
6880Sstevel@tonic-gate 		method->ui_close_session = closer;
6890Sstevel@tonic-gate 		return 0;
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 	else
6920Sstevel@tonic-gate 		return -1;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 
UI_method_get_opener(UI_METHOD * method)6950Sstevel@tonic-gate int (*UI_method_get_opener(UI_METHOD *method))(UI*)
6960Sstevel@tonic-gate 	{
6970Sstevel@tonic-gate 	if (method)
6980Sstevel@tonic-gate 		return method->ui_open_session;
6990Sstevel@tonic-gate 	else
7000Sstevel@tonic-gate 		return NULL;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
UI_method_get_writer(UI_METHOD * method)7030Sstevel@tonic-gate int (*UI_method_get_writer(UI_METHOD *method))(UI*,UI_STRING*)
7040Sstevel@tonic-gate 	{
7050Sstevel@tonic-gate 	if (method)
7060Sstevel@tonic-gate 		return method->ui_write_string;
7070Sstevel@tonic-gate 	else
7080Sstevel@tonic-gate 		return NULL;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
UI_method_get_flusher(UI_METHOD * method)7110Sstevel@tonic-gate int (*UI_method_get_flusher(UI_METHOD *method))(UI*)
7120Sstevel@tonic-gate 	{
7130Sstevel@tonic-gate 	if (method)
7140Sstevel@tonic-gate 		return method->ui_flush;
7150Sstevel@tonic-gate 	else
7160Sstevel@tonic-gate 		return NULL;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
UI_method_get_reader(UI_METHOD * method)7190Sstevel@tonic-gate int (*UI_method_get_reader(UI_METHOD *method))(UI*,UI_STRING*)
7200Sstevel@tonic-gate 	{
7210Sstevel@tonic-gate 	if (method)
7220Sstevel@tonic-gate 		return method->ui_read_string;
7230Sstevel@tonic-gate 	else
7240Sstevel@tonic-gate 		return NULL;
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 
UI_method_get_closer(UI_METHOD * method)7270Sstevel@tonic-gate int (*UI_method_get_closer(UI_METHOD *method))(UI*)
7280Sstevel@tonic-gate 	{
7290Sstevel@tonic-gate 	if (method)
7300Sstevel@tonic-gate 		return method->ui_close_session;
7310Sstevel@tonic-gate 	else
7320Sstevel@tonic-gate 		return NULL;
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 
UI_get_string_type(UI_STRING * uis)7350Sstevel@tonic-gate enum UI_string_types UI_get_string_type(UI_STRING *uis)
7360Sstevel@tonic-gate 	{
7370Sstevel@tonic-gate 	if (!uis)
7380Sstevel@tonic-gate 		return UIT_NONE;
7390Sstevel@tonic-gate 	return uis->type;
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
UI_get_input_flags(UI_STRING * uis)7420Sstevel@tonic-gate int UI_get_input_flags(UI_STRING *uis)
7430Sstevel@tonic-gate 	{
7440Sstevel@tonic-gate 	if (!uis)
7450Sstevel@tonic-gate 		return 0;
7460Sstevel@tonic-gate 	return uis->input_flags;
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
UI_get0_output_string(UI_STRING * uis)7490Sstevel@tonic-gate const char *UI_get0_output_string(UI_STRING *uis)
7500Sstevel@tonic-gate 	{
7510Sstevel@tonic-gate 	if (!uis)
7520Sstevel@tonic-gate 		return NULL;
7530Sstevel@tonic-gate 	return uis->out_string;
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 
UI_get0_action_string(UI_STRING * uis)7560Sstevel@tonic-gate const char *UI_get0_action_string(UI_STRING *uis)
7570Sstevel@tonic-gate 	{
7580Sstevel@tonic-gate 	if (!uis)
7590Sstevel@tonic-gate 		return NULL;
7600Sstevel@tonic-gate 	switch(uis->type)
7610Sstevel@tonic-gate 		{
7620Sstevel@tonic-gate 	case UIT_PROMPT:
7630Sstevel@tonic-gate 	case UIT_BOOLEAN:
7640Sstevel@tonic-gate 		return uis->_.boolean_data.action_desc;
7650Sstevel@tonic-gate 	default:
7660Sstevel@tonic-gate 		return NULL;
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
UI_get0_result_string(UI_STRING * uis)7700Sstevel@tonic-gate const char *UI_get0_result_string(UI_STRING *uis)
7710Sstevel@tonic-gate 	{
7720Sstevel@tonic-gate 	if (!uis)
7730Sstevel@tonic-gate 		return NULL;
7740Sstevel@tonic-gate 	switch(uis->type)
7750Sstevel@tonic-gate 		{
7760Sstevel@tonic-gate 	case UIT_PROMPT:
7770Sstevel@tonic-gate 	case UIT_VERIFY:
7780Sstevel@tonic-gate 		return uis->result_buf;
7790Sstevel@tonic-gate 	default:
7800Sstevel@tonic-gate 		return NULL;
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
UI_get0_test_string(UI_STRING * uis)7840Sstevel@tonic-gate const char *UI_get0_test_string(UI_STRING *uis)
7850Sstevel@tonic-gate 	{
7860Sstevel@tonic-gate 	if (!uis)
7870Sstevel@tonic-gate 		return NULL;
7880Sstevel@tonic-gate 	switch(uis->type)
7890Sstevel@tonic-gate 		{
7900Sstevel@tonic-gate 	case UIT_VERIFY:
7910Sstevel@tonic-gate 		return uis->_.string_data.test_buf;
7920Sstevel@tonic-gate 	default:
7930Sstevel@tonic-gate 		return NULL;
7940Sstevel@tonic-gate 		}
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
UI_get_result_minsize(UI_STRING * uis)7970Sstevel@tonic-gate int UI_get_result_minsize(UI_STRING *uis)
7980Sstevel@tonic-gate 	{
7990Sstevel@tonic-gate 	if (!uis)
8000Sstevel@tonic-gate 		return -1;
8010Sstevel@tonic-gate 	switch(uis->type)
8020Sstevel@tonic-gate 		{
8030Sstevel@tonic-gate 	case UIT_PROMPT:
8040Sstevel@tonic-gate 	case UIT_VERIFY:
8050Sstevel@tonic-gate 		return uis->_.string_data.result_minsize;
8060Sstevel@tonic-gate 	default:
8070Sstevel@tonic-gate 		return -1;
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
UI_get_result_maxsize(UI_STRING * uis)8110Sstevel@tonic-gate int UI_get_result_maxsize(UI_STRING *uis)
8120Sstevel@tonic-gate 	{
8130Sstevel@tonic-gate 	if (!uis)
8140Sstevel@tonic-gate 		return -1;
8150Sstevel@tonic-gate 	switch(uis->type)
8160Sstevel@tonic-gate 		{
8170Sstevel@tonic-gate 	case UIT_PROMPT:
8180Sstevel@tonic-gate 	case UIT_VERIFY:
8190Sstevel@tonic-gate 		return uis->_.string_data.result_maxsize;
8200Sstevel@tonic-gate 	default:
8210Sstevel@tonic-gate 		return -1;
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 
UI_set_result(UI * ui,UI_STRING * uis,const char * result)8250Sstevel@tonic-gate int UI_set_result(UI *ui, UI_STRING *uis, const char *result)
8260Sstevel@tonic-gate 	{
8270Sstevel@tonic-gate 	int l = strlen(result);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	ui->flags &= ~UI_FLAG_REDOABLE;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (!uis)
8320Sstevel@tonic-gate 		return -1;
8330Sstevel@tonic-gate 	switch (uis->type)
8340Sstevel@tonic-gate 		{
8350Sstevel@tonic-gate 	case UIT_PROMPT:
8360Sstevel@tonic-gate 	case UIT_VERIFY:
8370Sstevel@tonic-gate 		{
8380Sstevel@tonic-gate 		char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize)+1];
8390Sstevel@tonic-gate 		char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize)+1];
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		BIO_snprintf(number1, sizeof(number1), "%d",
8420Sstevel@tonic-gate 			uis->_.string_data.result_minsize);
8430Sstevel@tonic-gate 		BIO_snprintf(number2, sizeof(number2), "%d",
8440Sstevel@tonic-gate 			uis->_.string_data.result_maxsize);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		if (l < uis->_.string_data.result_minsize)
8470Sstevel@tonic-gate 			{
8480Sstevel@tonic-gate 			ui->flags |= UI_FLAG_REDOABLE;
8490Sstevel@tonic-gate 			UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_SMALL);
8500Sstevel@tonic-gate 			ERR_add_error_data(5,"You must type in ",
8510Sstevel@tonic-gate 				number1," to ",number2," characters");
8520Sstevel@tonic-gate 			return -1;
8530Sstevel@tonic-gate 			}
8540Sstevel@tonic-gate 		if (l > uis->_.string_data.result_maxsize)
8550Sstevel@tonic-gate 			{
8560Sstevel@tonic-gate 			ui->flags |= UI_FLAG_REDOABLE;
8570Sstevel@tonic-gate 			UIerr(UI_F_UI_SET_RESULT,UI_R_RESULT_TOO_LARGE);
8580Sstevel@tonic-gate 			ERR_add_error_data(5,"You must type in ",
8590Sstevel@tonic-gate 				number1," to ",number2," characters");
8600Sstevel@tonic-gate 			return -1;
8610Sstevel@tonic-gate 			}
8620Sstevel@tonic-gate 		}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		if (!uis->result_buf)
8650Sstevel@tonic-gate 			{
8660Sstevel@tonic-gate 			UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER);
8670Sstevel@tonic-gate 			return -1;
8680Sstevel@tonic-gate 			}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 		BUF_strlcpy(uis->result_buf, result,
8710Sstevel@tonic-gate 			    uis->_.string_data.result_maxsize + 1);
8720Sstevel@tonic-gate 		break;
8730Sstevel@tonic-gate 	case UIT_BOOLEAN:
8740Sstevel@tonic-gate 		{
8750Sstevel@tonic-gate 		const char *p;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		if (!uis->result_buf)
8780Sstevel@tonic-gate 			{
8790Sstevel@tonic-gate 			UIerr(UI_F_UI_SET_RESULT,UI_R_NO_RESULT_BUFFER);
8800Sstevel@tonic-gate 			return -1;
8810Sstevel@tonic-gate 			}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		uis->result_buf[0] = '\0';
8840Sstevel@tonic-gate 		for(p = result; *p; p++)
8850Sstevel@tonic-gate 			{
8860Sstevel@tonic-gate 			if (strchr(uis->_.boolean_data.ok_chars, *p))
8870Sstevel@tonic-gate 				{
8880Sstevel@tonic-gate 				uis->result_buf[0] =
8890Sstevel@tonic-gate 					uis->_.boolean_data.ok_chars[0];
8900Sstevel@tonic-gate 				break;
8910Sstevel@tonic-gate 				}
8920Sstevel@tonic-gate 			if (strchr(uis->_.boolean_data.cancel_chars, *p))
8930Sstevel@tonic-gate 				{
8940Sstevel@tonic-gate 				uis->result_buf[0] =
8950Sstevel@tonic-gate 					uis->_.boolean_data.cancel_chars[0];
8960Sstevel@tonic-gate 				break;
8970Sstevel@tonic-gate 				}
8980Sstevel@tonic-gate 			}
8990Sstevel@tonic-gate 	default:
9000Sstevel@tonic-gate 		break;
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 	return 0;
9040Sstevel@tonic-gate 	}
905