ui_lib.c 20 KB

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