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