Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Ask for a password |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 8 | */ |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 10 | |
| 11 | /* do nothing signal handler */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12 | static void askpass_timeout(int UNUSED_PARAM ignore) |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 13 | { |
| 14 | } |
| 15 | |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 16 | char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt) |
Bernhard Reutner-Fischer | 82b1429 | 2008-12-03 18:48:39 +0000 | [diff] [blame] | 17 | { |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 18 | #define MAX_LINE 0xfff |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 19 | char *ret; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 20 | int i; |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 21 | struct sigaction sa, oldsa; |
| 22 | struct termios tio, oldtio; |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 23 | |
Denys Vlasenko | d32fc64 | 2014-07-01 13:20:22 +0200 | [diff] [blame] | 24 | tcflush(fd, TCIFLUSH); |
| 25 | /* Was buggy: was printing prompt *before* flushing input, |
| 26 | * which was upsetting "expect" based scripts of some users. |
| 27 | */ |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 28 | fputs_stdout(prompt); |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 29 | fflush_all(); |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 30 | |
| 31 | tcgetattr(fd, &oldtio); |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 32 | tio = oldtio; |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 33 | /* Switch off echo. ECHOxyz meaning: |
| 34 | * ECHO echo input chars |
| 35 | * ECHOE echo BS-SP-BS on erase character |
| 36 | * ECHOK echo kill char specially, not as ^c (ECHOKE controls how exactly) |
| 37 | * ECHOKE erase all input via BS-SP-BS on kill char (else go to next line) |
| 38 | * ECHOCTL Echo ctrl chars as ^c (else echo verbatim: |
| 39 | * e.g. up arrow emits "ESC-something" and thus moves cursor up!) |
| 40 | * ECHONL Echo NL even if ECHO is not set |
| 41 | * ECHOPRT On erase, echo erased chars |
| 42 | * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen] |
| 43 | */ |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 44 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); |
Denys Vlasenko | 557deb1 | 2010-02-03 12:17:06 +0100 | [diff] [blame] | 45 | tcsetattr(fd, TCSANOW, &tio); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 47 | memset(&sa, 0, sizeof(sa)); |
| 48 | /* sa.sa_flags = 0; - no SA_RESTART! */ |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 49 | /* SIGINT and SIGALRM will interrupt reads below */ |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 50 | sa.sa_handler = askpass_timeout; |
| 51 | sigaction(SIGINT, &sa, &oldsa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 52 | if (timeout) { |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 53 | sigaction_set(SIGALRM, &sa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 54 | alarm(timeout); |
| 55 | } |
| 56 | |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 57 | ret = NULL; |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 58 | i = 0; |
| 59 | while (1) { |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 60 | int r; |
| 61 | |
| 62 | /* User input is uber-slow, no need to optimize reallocs. |
| 63 | * Grow it on every char. |
| 64 | */ |
| 65 | ret = xrealloc(ret, i + 2); |
| 66 | r = read(fd, &ret[i], 1); |
| 67 | |
Jonathan Liu | b6dc13c | 2013-05-21 17:01:55 +0200 | [diff] [blame] | 68 | if ((i == 0 && r == 0) /* EOF (^D) with no password */ |
Denys Vlasenko | 2f094ae | 2018-04-07 15:02:20 +0200 | [diff] [blame] | 69 | || r < 0 /* read is interrupted by timeout or ^C */ |
Jonathan Liu | b6dc13c | 2013-05-21 17:01:55 +0200 | [diff] [blame] | 70 | ) { |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 71 | ret[i] = '\0'; /* paranoia */ |
| 72 | nuke_str(ret); /* paranoia */ |
| 73 | free(ret); |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 74 | ret = NULL; |
| 75 | break; |
| 76 | } |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 77 | |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 78 | if (r == 0 /* EOF */ |
| 79 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 80 | || ++i == MAX_LINE /* line limit */ |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 81 | ) { |
| 82 | ret[i] = '\0'; |
| 83 | break; |
| 84 | } |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (timeout) { |
| 88 | alarm(0); |
| 89 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 90 | sigaction_set(SIGINT, &oldsa); |
Denys Vlasenko | 557deb1 | 2010-02-03 12:17:06 +0100 | [diff] [blame] | 91 | tcsetattr(fd, TCSANOW, &oldtio); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 92 | bb_putchar('\n'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 93 | fflush_all(); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 94 | return ret; |
| 95 | } |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 96 | char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt) |
| 97 | { |
| 98 | return bb_ask_noecho(STDIN_FILENO, 0, prompt); |
| 99 | } |