ui_lib.c 25 KB

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