int UI_ctrl(UI *ui, int cmd, long i, void *p, void (*f)(void)) { if (ui == NULL) { UIerr(UI_F_UI_CTRL,ERR_R_PASSED_NULL_PARAMETER); return -1; } switch(cmd) { case UI_CTRL_PRINT_ERRORS: { int save_flag = !!(ui->flags & UI_FLAG_PRINT_ERRORS); if (i) ui->flags |= UI_FLAG_PRINT_ERRORS; else ui->flags &= ~UI_FLAG_PRINT_ERRORS; return save_flag; } case UI_CTRL_IS_REDOABLE: return !!(ui->flags & UI_FLAG_REDOABLE); default: break; } UIerr(UI_F_UI_CTRL,UI_R_UNKNOWN_CONTROL_COMMAND); return -1; }
static UI_STRING *general_allocate_prompt(UI *ui, const char *prompt, int prompt_freeable, enum UI_string_types type, int input_flags, char *result_buf) { UI_STRING *ret = NULL; if (prompt == NULL) { UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,ERR_R_PASSED_NULL_PARAMETER); } else if ((type == UIT_PROMPT || type == UIT_VERIFY || type == UIT_BOOLEAN) && result_buf == NULL) { UIerr(UI_F_GENERAL_ALLOCATE_PROMPT,UI_R_NO_RESULT_BUFFER); } else if ((ret = (UI_STRING *)OPENSSL_malloc(sizeof(UI_STRING)))) { ret->out_string=prompt; ret->flags=prompt_freeable ? OUT_STRING_FREEABLE : 0; ret->input_flags=input_flags; ret->type=type; ret->result_buf=result_buf; } return ret; }
UI *UI_new_method(const UI_METHOD *method) { UI *ret = OPENSSL_zalloc(sizeof(*ret)); if (ret == NULL) { UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE); return NULL; } ret->lock = CRYPTO_THREAD_lock_new(); if (ret->lock == NULL) { UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE); OPENSSL_free(ret); return NULL; } if (method == NULL) method = UI_get_default_method(); if (method == NULL) method = UI_null(); ret->meth = method; if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data)) { OPENSSL_free(ret); return NULL; } return ret; }
int UI_dup_input_boolean(UI *ui, const char *prompt, const char *action_desc, const char *ok_chars, const char *cancel_chars, int flags, char *result_buf) { char *prompt_copy = NULL; char *action_desc_copy = NULL; char *ok_chars_copy = NULL; char *cancel_chars_copy = NULL; if (prompt) { prompt_copy=BUF_strdup(prompt); if (prompt_copy == NULL) { UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); goto err; } } if (action_desc) { action_desc_copy=BUF_strdup(action_desc); if (action_desc_copy == NULL) { UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); goto err; } } if (ok_chars) { ok_chars_copy=BUF_strdup(ok_chars); if (ok_chars_copy == NULL) { UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); goto err; } } if (cancel_chars) { cancel_chars_copy=BUF_strdup(cancel_chars); if (cancel_chars_copy == NULL) { UIerr(UI_F_UI_DUP_INPUT_BOOLEAN,ERR_R_MALLOC_FAILURE); goto err; } } return general_allocate_boolean(ui, prompt_copy, action_desc_copy, ok_chars_copy, cancel_chars_copy, 1, UIT_BOOLEAN, flags, result_buf); err: if (prompt_copy) OPENSSL_free(prompt_copy); if (action_desc_copy) OPENSSL_free(action_desc_copy); if (ok_chars_copy) OPENSSL_free(ok_chars_copy); if (cancel_chars_copy) OPENSSL_free(cancel_chars_copy); return -1; }
const char *UI_get0_result(UI *ui, int i) { if (i < 0) { UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_SMALL); return NULL; } if (i >= sk_UI_STRING_num(ui->strings)) { UIerr(UI_F_UI_GET0_RESULT, UI_R_INDEX_TOO_LARGE); return NULL; } return UI_get0_result_string(sk_UI_STRING_value(ui->strings, i)); }
int UI_get_result_length(UI *ui, int i) { if (i < 0) { UIerr(UI_F_UI_GET_RESULT_LENGTH, UI_R_INDEX_TOO_SMALL); return -1; } if (i >= sk_UI_STRING_num(ui->strings)) { UIerr(UI_F_UI_GET_RESULT_LENGTH, UI_R_INDEX_TOO_LARGE); return -1; } return UI_get_result_string_length(sk_UI_STRING_value(ui->strings, i)); }
static int general_allocate_boolean(UI *ui, const char *prompt, const char *action_desc, const char *ok_chars, const char *cancel_chars, int prompt_freeable, enum UI_string_types type, int input_flags, char *result_buf) { int ret = -1; UI_STRING *s; const char *p; if (ok_chars == NULL) { UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER); } else if (cancel_chars == NULL) { UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN,ERR_R_PASSED_NULL_PARAMETER); } else { for(p = ok_chars; *p; p++) { if (strchr(cancel_chars, *p)) { UIerr(UI_F_GENERAL_ALLOCATE_BOOLEAN, UI_R_COMMON_OK_AND_CANCEL_CHARACTERS); } } s = general_allocate_prompt(ui, prompt, prompt_freeable, type, input_flags, result_buf); if (s) { if (allocate_string_stack(ui) >= 0) { s->_.boolean_data.action_desc = action_desc; s->_.boolean_data.ok_chars = ok_chars; s->_.boolean_data.cancel_chars = cancel_chars; ret=sk_UI_STRING_push(ui->strings, s); /* sk_push() returns 0 on error. Let's addapt that */ if (ret <= 0) ret--; } else free_string(s); } } return ret; }
int UI_set_result(UI *ui, UI_STRING *uis, const char *result) { #if 0 /* * This is placed here solely to preserve UI_F_UI_SET_RESULT * To be removed for OpenSSL 1.2.0 */ UIerr(UI_F_UI_SET_RESULT, ERR_R_DISABLED); #endif return UI_set_result_ex(ui, uis, result, strlen(result)); }
int UI_dup_user_data(UI *ui, void *user_data) { void *duplicate = NULL; if (ui->meth->ui_duplicate_data == NULL || ui->meth->ui_destroy_data == NULL) { UIerr(UI_F_UI_DUP_USER_DATA, UI_R_USER_DATA_DUPLICATION_UNSUPPORTED); return -1; } duplicate = ui->meth->ui_duplicate_data(ui, user_data); if (duplicate == NULL) { UIerr(UI_F_UI_DUP_USER_DATA, ERR_R_MALLOC_FAILURE); return -1; } (void)UI_add_user_data(ui, duplicate); ui->flags |= UI_FLAG_DUPL_DATA; return 0; }
int UI_dup_error_string(UI *ui, const char *text) { char *text_copy = NULL; if (text != NULL) { text_copy = OPENSSL_strdup(text); if (text_copy == NULL) { UIerr(UI_F_UI_DUP_ERROR_STRING, ERR_R_MALLOC_FAILURE); return -1; } } return general_allocate_string(ui, text_copy, 1, UIT_ERROR, 0, NULL, 0, 0, NULL); }
UI_METHOD *UI_create_method(const char *name) { UI_METHOD *ui_method = OPENSSL_zalloc(sizeof(*ui_method)); if (ui_method != NULL) { ui_method->name = OPENSSL_strdup(name); if (ui_method->name == NULL) { OPENSSL_free(ui_method); UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE); return NULL; } } return ui_method; }
int UI_dup_info_string(UI *ui, const char *text) { char *text_copy = NULL; if (text) { text_copy = BUF_strdup(text); if (text_copy == NULL) { UIerr(UI_F_UI_DUP_INFO_STRING, ERR_R_MALLOC_FAILURE); return -1; } } return general_allocate_string(ui, text_copy, 1, UIT_INFO, 0, NULL, 0, 0, NULL); }
UI *UI_new_method(const UI_METHOD *method) { UI *ret = OPENSSL_zalloc(sizeof(*ret)); if (ret == NULL) { UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE); return NULL; } if (method == NULL) ret->meth = UI_get_default_method(); else ret->meth = method; CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data); return ret; }
int UI_dup_verify_string(UI *ui, const char *prompt, int flags, char *result_buf, int minsize, int maxsize, const char *test_buf) { char *prompt_copy = NULL; if (prompt) { prompt_copy = strdup(prompt); if (prompt_copy == NULL) { UIerr(UI_F_UI_DUP_VERIFY_STRING, ERR_R_MALLOC_FAILURE); return -1; } } return general_allocate_string(ui, prompt_copy, 1, UIT_VERIFY, flags, result_buf, minsize, maxsize, test_buf); }
/* Same as UI_add_input_string(), excepts it takes a copy of the prompt */ int UI_dup_input_string(UI *ui, const char *prompt, int flags, char *result_buf, int minsize, int maxsize) { char *prompt_copy = NULL; if (prompt) { prompt_copy = strdup(prompt); if (prompt_copy == NULL) { UIerr(UI_F_UI_DUP_INPUT_STRING, ERR_R_MALLOC_FAILURE); return 0; } } return general_allocate_string(ui, prompt_copy, 1, UIT_PROMPT, flags, result_buf, minsize, maxsize, NULL); }
UI_METHOD *UI_create_method(const char *name) { UI_METHOD *ui_method = NULL; if ((ui_method = OPENSSL_zalloc(sizeof(*ui_method))) == NULL || (ui_method->name = OPENSSL_strdup(name)) == NULL || !CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI_METHOD, ui_method, &ui_method->ex_data)) { if (ui_method) OPENSSL_free(ui_method->name); OPENSSL_free(ui_method); UIerr(UI_F_UI_CREATE_METHOD, ERR_R_MALLOC_FAILURE); return NULL; } return ui_method; }
static int noecho_console(UI *ui) { #ifdef TTY_FLAGS memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig)); tty_new.TTY_FLAGS &= ~ECHO; #endif #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) return 0; #endif #ifdef OPENSSL_SYS_VMS if (is_a_tty) { tty_new[0] = tty_orig[0]; tty_new[1] = tty_orig[1] | TT$M_NOECHO; tty_new[2] = tty_orig[2]; status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0, 0); if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) { char tmp_num[2][12]; BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X", status); BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X", iosb.iosb$w_value); UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR); ERR_add_error_data(5, "status=", tmp_num[0], ",", "iosb.iosb$w_value=", tmp_num[1]); return 0; } } #endif #if defined(_WIN32) && !defined(_WIN32_WCE) if (is_a_tty) { tty_new = tty_orig; tty_new &= ~ENABLE_ECHO_INPUT; SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new); } #endif return 1; }
UI * UI_new_method(const UI_METHOD *method) { UI *ret; ret = malloc(sizeof(UI)); if (ret == NULL) { UIerr(UI_F_UI_NEW_METHOD, ERR_R_MALLOC_FAILURE); return NULL; } if (method == NULL) ret->meth = UI_get_default_method(); else ret->meth = method; ret->strings = NULL; ret->user_data = NULL; ret->flags = 0; CRYPTO_new_ex_data(CRYPTO_EX_INDEX_UI, ret, &ret->ex_data); return ret; }
static int close_console(UI *ui) { if (tty_in != stdin) fclose(tty_in); if (tty_out != stderr) fclose(tty_out); #ifdef OPENSSL_SYS_VMS status = sys$dassgn(channel); if (status != SS$_NORMAL) { char tmp_num[12]; BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status); UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR); ERR_add_error_data(2, "status=", tmp_num); return 0; } #endif CRYPTO_THREAD_unlock(ui->lock); return 1; }
int UI_set_result_ex(UI *ui, UI_STRING *uis, const char *result, int len) { ui->flags &= ~UI_FLAG_REDOABLE; switch (uis->type) { case UIT_PROMPT: case UIT_VERIFY: { char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1]; char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1]; BIO_snprintf(number1, sizeof(number1), "%d", uis->_.string_data.result_minsize); BIO_snprintf(number2, sizeof(number2), "%d", uis->_.string_data.result_maxsize); if (len < uis->_.string_data.result_minsize) { ui->flags |= UI_FLAG_REDOABLE; UIerr(UI_F_UI_SET_RESULT_EX, UI_R_RESULT_TOO_SMALL); ERR_add_error_data(5, "You must type in ", number1, " to ", number2, " characters"); return -1; } if (len > uis->_.string_data.result_maxsize) { ui->flags |= UI_FLAG_REDOABLE; UIerr(UI_F_UI_SET_RESULT_EX, UI_R_RESULT_TOO_LARGE); ERR_add_error_data(5, "You must type in ", number1, " to ", number2, " characters"); return -1; } } if (uis->result_buf == NULL) { UIerr(UI_F_UI_SET_RESULT_EX, UI_R_NO_RESULT_BUFFER); return -1; } memcpy(uis->result_buf, result, len); if (len <= uis->_.string_data.result_maxsize) uis->result_buf[len] = '\0'; uis->result_len = len; break; case UIT_BOOLEAN: { const char *p; if (uis->result_buf == NULL) { UIerr(UI_F_UI_SET_RESULT_EX, UI_R_NO_RESULT_BUFFER); return -1; } uis->result_buf[0] = '\0'; for (p = result; *p; p++) { if (strchr(uis->_.boolean_data.ok_chars, *p)) { uis->result_buf[0] = uis->_.boolean_data.ok_chars[0]; break; } if (strchr(uis->_.boolean_data.cancel_chars, *p)) { uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0]; break; } } } case UIT_NONE: case UIT_INFO: case UIT_ERROR: break; } return 0; }
int UI_process(UI *ui) { int i, ok = 0; const char *state = "processing"; if (ui->meth->ui_open_session != NULL && ui->meth->ui_open_session(ui) <= 0) { state = "opening session"; ok = -1; goto err; } if (ui->flags & UI_FLAG_PRINT_ERRORS) ERR_print_errors_cb((int (*)(const char *, size_t, void *)) print_error, (void *)ui); for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { if (ui->meth->ui_write_string != NULL && (ui->meth->ui_write_string(ui, sk_UI_STRING_value(ui->strings, i)) <= 0)) { state = "writing strings"; ok = -1; goto err; } } if (ui->meth->ui_flush != NULL) switch (ui->meth->ui_flush(ui)) { case -1: /* Interrupt/Cancel/something... */ ok = -2; goto err; case 0: /* Errors */ state = "flushing"; ok = -1; goto err; default: /* Success */ ok = 0; break; } for (i = 0; i < sk_UI_STRING_num(ui->strings); i++) { if (ui->meth->ui_read_string != NULL) { switch (ui->meth->ui_read_string(ui, sk_UI_STRING_value(ui->strings, i))) { case -1: /* Interrupt/Cancel/something... */ ok = -2; goto err; case 0: /* Errors */ state = "reading strings"; ok = -1; goto err; default: /* Success */ ok = 0; break; } } } state = NULL; err: if (ui->meth->ui_close_session != NULL && ui->meth->ui_close_session(ui) <= 0) { if (state == NULL) state = "closing session"; ok = -1; } if (ok == -1) { UIerr(UI_F_UI_PROCESS, UI_R_PROCESSING_ERROR); ERR_add_error_data(2, "while ", state); } return ok; }
int UI_set_result(UI *ui, UI_STRING *uis, const char *result) { int l = strlen(result); ui->flags &= ~UI_FLAG_REDOABLE; if (!uis) return -1; switch (uis->type) { case UIT_PROMPT: case UIT_VERIFY: if (l < uis->_.string_data.result_minsize) { ui->flags |= UI_FLAG_REDOABLE; UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL); ERR_asprintf_error_data ("You must type in %d to %d characters", uis->_.string_data.result_minsize, uis->_.string_data.result_maxsize); return -1; } if (l > uis->_.string_data.result_maxsize) { ui->flags |= UI_FLAG_REDOABLE; UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE); ERR_asprintf_error_data ("You must type in %d to %d characters", uis->_.string_data.result_minsize, uis->_.string_data.result_maxsize); return -1; } if (!uis->result_buf) { UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); return -1; } strlcpy(uis->result_buf, result, uis->_.string_data.result_maxsize + 1); break; case UIT_BOOLEAN: { const char *p; if (!uis->result_buf) { UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); return -1; } uis->result_buf[0] = '\0'; for (p = result; *p; p++) { if (strchr(uis->_.boolean_data.ok_chars, *p)) { uis->result_buf[0] = uis->_.boolean_data.ok_chars[0]; break; } if (strchr(uis->_.boolean_data.cancel_chars, *p)) { uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0]; break; } } default: break; } } return 0; }
/* Internal functions to open, handle and close a channel to the console. */ static int open_console(UI *ui) { CRYPTO_THREAD_write_lock(ui->lock); is_a_tty = 1; #if defined(OPENSSL_SYS_VXWORKS) tty_in = stdin; tty_out = stderr; #elif defined(_WIN32) && !defined(_WIN32_WCE) if ((tty_out = fopen("conout$", "w")) == NULL) tty_out = stderr; if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) { tty_in = stdin; } else { is_a_tty = 0; if ((tty_in = fopen("conin$", "r")) == NULL) tty_in = stdin; } #else # ifdef OPENSSL_SYS_MSDOS # define DEV_TTY "con" # else # define DEV_TTY "/dev/tty" # endif if ((tty_in = fopen(DEV_TTY, "r")) == NULL) tty_in = stdin; if ((tty_out = fopen(DEV_TTY, "w")) == NULL) tty_out = stderr; #endif #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS) if (TTY_get(fileno(tty_in), &tty_orig) == -1) { # ifdef ENOTTY if (errno == ENOTTY) is_a_tty = 0; else # endif # ifdef EINVAL /* * Ariel Glenn [email protected] reports that solaris can return * EINVAL instead. This should be ok */ if (errno == EINVAL) is_a_tty = 0; else # endif # ifdef ENODEV /* * MacOS X returns ENODEV (Operation not supported by device), * which seems appropriate. */ if (errno == ENODEV) is_a_tty = 0; else # endif { char tmp_num[10]; BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno); UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE); ERR_add_error_data(2, "errno=", tmp_num); return 0; } } #endif #ifdef OPENSSL_SYS_VMS status = sys$assign(&terminal, &channel, 0, 0); /* if there isn't a TT device, something is very wrong */ if (status != SS$_NORMAL) { char tmp_num[12]; BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status); UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR); ERR_add_error_data(2, "status=", tmp_num); return 0; } status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0, 0, 0); /* If IO$_SENSEMODE doesn't work, this is not a terminal device */ if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) is_a_tty = 0; #endif return 1; }
int UI_set_result(UI *ui, UI_STRING *uis, const char *result) { int l = strlen(result); ui->flags &= ~UI_FLAG_REDOABLE; if (!uis) return -1; switch (uis->type) { case UIT_PROMPT: case UIT_VERIFY: { char number1[DECIMAL_SIZE(uis->_.string_data.result_minsize) + 1]; char number2[DECIMAL_SIZE(uis->_.string_data.result_maxsize) + 1]; BIO_snprintf(number1, sizeof(number1), "%d", uis->_.string_data.result_minsize); BIO_snprintf(number2, sizeof(number2), "%d", uis->_.string_data.result_maxsize); if (l < uis->_.string_data.result_minsize) { ui->flags |= UI_FLAG_REDOABLE; UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_SMALL); ERR_add_error_data(5, "You must type in ", number1, " to ", number2, " characters"); return -1; } if (l > uis->_.string_data.result_maxsize) { ui->flags |= UI_FLAG_REDOABLE; UIerr(UI_F_UI_SET_RESULT, UI_R_RESULT_TOO_LARGE); ERR_add_error_data(5, "You must type in ", number1, " to ", number2, " characters"); return -1; } } if (!uis->result_buf) { UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); return -1; } BUF_strlcpy(uis->result_buf, result, uis->_.string_data.result_maxsize + 1); break; case UIT_BOOLEAN: { const char *p; if (!uis->result_buf) { UIerr(UI_F_UI_SET_RESULT, UI_R_NO_RESULT_BUFFER); return -1; } uis->result_buf[0] = '\0'; for (p = result; *p; p++) { if (strchr(uis->_.boolean_data.ok_chars, *p)) { uis->result_buf[0] = uis->_.boolean_data.ok_chars[0]; break; } if (strchr(uis->_.boolean_data.cancel_chars, *p)) { uis->result_buf[0] = uis->_.boolean_data.cancel_chars[0]; break; } } } default: break; } return 0; }