ui_lib.c 24 KB

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