ui_lib.c 20 KB

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