From a2dfb1b27e34b75b81e81537ba8711f42d3f7f06 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sun, 27 Jan 2019 02:40:31 +0100 Subject: ideviceactivation: Mask input for secure input fields --- tools/ideviceactivation.c | 63 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/tools/ideviceactivation.c b/tools/ideviceactivation.c index a930d5d..93e6717 100644 --- a/tools/ideviceactivation.c +++ b/tools/ideviceactivation.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,13 @@ #include #include +#ifdef WIN32 +#include +#include +#else +#include +#endif + static void print_usage(int argc, char **argv) { char *name = NULL; @@ -58,6 +66,48 @@ static void print_usage(int argc, char **argv) printf("Homepage: \n"); } +#ifdef WIN32 +#define BS_CC '\b' +#define my_getch getch +#else +#define BS_CC 0x7f +static int my_getch(void) +{ + struct termios oldt, newt; + int ch; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + ch = getchar(); + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + return ch; +} +#endif + +static void get_user_input(char *buf, int maxlen, int secure) +{ + int len = 0; + int c; + + while ((c = my_getch())) { + if ((c == '\r') || (c == '\n')) { + break; + } else if (isprint(c)) { + if (len < maxlen-1) + buf[len++] = c; + fputc((secure) ? '*' : c, stdout); + } else if (c == BS_CC) { + if (len > 0) { + fputs("\b \b", stdout); + len--; + } + } + } + fputs("\n", stdout); + buf[len] = 0; +} + int main(int argc, char *argv[]) { idevice_t device = NULL; @@ -461,9 +511,18 @@ int main(int argc, char *argv[]) if (idevice_activation_response_field_requires_input(response, field_key)) { idevice_activation_response_get_label(response, field_key, &field_label); if (interactive) { - printf("input %s: ", field_label ? field_label : field_key); + char *field_placeholder = NULL; + int secure = idevice_activation_response_field_secure_input(response, field_key); + idevice_activation_response_get_placeholder(response, field_key, &field_placeholder); + printf("input %s", field_label ? field_label : field_key); + if (field_placeholder) { + printf(" (%s)", field_placeholder); + free(field_placeholder); + } + printf(": "); + fflush(stdout); fflush(stdin); - scanf("%1023s", input); + get_user_input(input, 1023, secure); } else { fprintf(stderr, "Server requires input for '%s' but we're not running interactively.\n", field_label ? field_label : field_key); strcpy(input, ""); -- cgit v1.1-32-gdbae