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 | |
Bernhard Reutner-Fischer | 82b1429 | 2008-12-03 18:48:39 +0000 | [diff] [blame] | 16 | char* FAST_FUNC bb_ask_stdin(const char *prompt) |
| 17 | { |
| 18 | return bb_ask(STDIN_FILENO, 0, prompt); |
| 19 | } |
| 20 | char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt) |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 21 | { |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 22 | /* Was static char[BIGNUM] */ |
| 23 | enum { sizeof_passwd = 128 }; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 24 | |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 25 | char *passwd; |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 26 | char *ret; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 27 | int i; |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 28 | struct sigaction sa, oldsa; |
| 29 | struct termios tio, oldtio; |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 30 | |
Denys Vlasenko | d32fc64 | 2014-07-01 13:20:22 +0200 | [diff] [blame] | 31 | tcflush(fd, TCIFLUSH); |
| 32 | /* Was buggy: was printing prompt *before* flushing input, |
| 33 | * which was upsetting "expect" based scripts of some users. |
| 34 | */ |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 35 | fputs(prompt, stdout); |
| 36 | fflush_all(); |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 37 | |
| 38 | tcgetattr(fd, &oldtio); |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 39 | tio = oldtio; |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 40 | /* Switch off echo */ |
| 41 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); |
Denys Vlasenko | 557deb1 | 2010-02-03 12:17:06 +0100 | [diff] [blame] | 42 | tcsetattr(fd, TCSANOW, &tio); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 44 | memset(&sa, 0, sizeof(sa)); |
| 45 | /* sa.sa_flags = 0; - no SA_RESTART! */ |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 46 | /* SIGINT and SIGALRM will interrupt reads below */ |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 47 | sa.sa_handler = askpass_timeout; |
| 48 | sigaction(SIGINT, &sa, &oldsa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 49 | if (timeout) { |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 50 | sigaction_set(SIGALRM, &sa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 51 | alarm(timeout); |
| 52 | } |
| 53 | |
Denys Vlasenko | 02859aa | 2015-10-09 18:16:40 +0200 | [diff] [blame] | 54 | passwd = auto_string(xmalloc(sizeof_passwd)); |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 55 | ret = passwd; |
| 56 | i = 0; |
| 57 | while (1) { |
| 58 | int r = read(fd, &ret[i], 1); |
Jonathan Liu | b6dc13c | 2013-05-21 17:01:55 +0200 | [diff] [blame] | 59 | if ((i == 0 && r == 0) /* EOF (^D) with no password */ |
Denys Vlasenko | 2f094ae | 2018-04-07 15:02:20 +0200 | [diff] [blame^] | 60 | || r < 0 /* read is interrupted by timeout or ^C */ |
Jonathan Liu | b6dc13c | 2013-05-21 17:01:55 +0200 | [diff] [blame] | 61 | ) { |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 62 | ret = NULL; |
| 63 | break; |
| 64 | } |
| 65 | if (r == 0 /* EOF */ |
| 66 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ |
| 67 | || ++i == sizeof_passwd-1 /* line limit */ |
| 68 | ) { |
| 69 | ret[i] = '\0'; |
| 70 | break; |
| 71 | } |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (timeout) { |
| 75 | alarm(0); |
| 76 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 77 | sigaction_set(SIGINT, &oldsa); |
Denys Vlasenko | 557deb1 | 2010-02-03 12:17:06 +0100 | [diff] [blame] | 78 | tcsetattr(fd, TCSANOW, &oldtio); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 79 | bb_putchar('\n'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 80 | fflush_all(); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 81 | return ret; |
| 82 | } |