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 |
| 4 | * I use a static buffer in this function. Plan accordingly. |
| 5 | * |
| 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 12 | |
| 13 | /* do nothing signal handler */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 14 | static void askpass_timeout(int UNUSED_PARAM ignore) |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 15 | { |
| 16 | } |
| 17 | |
Bernhard Reutner-Fischer | 82b1429 | 2008-12-03 18:48:39 +0000 | [diff] [blame] | 18 | char* FAST_FUNC bb_ask_stdin(const char *prompt) |
| 19 | { |
| 20 | return bb_ask(STDIN_FILENO, 0, prompt); |
| 21 | } |
| 22 | 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] | 23 | { |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 24 | /* Was static char[BIGNUM] */ |
| 25 | enum { sizeof_passwd = 128 }; |
| 26 | static char *passwd; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 28 | char *ret; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 29 | int i; |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 30 | struct sigaction sa, oldsa; |
| 31 | struct termios tio, oldtio; |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 32 | |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 33 | fputs(prompt, stdout); |
| 34 | fflush_all(); |
Bernhard Reutner-Fischer | 82b1429 | 2008-12-03 18:48:39 +0000 | [diff] [blame] | 35 | tcflush(fd, TCIFLUSH); |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 36 | |
| 37 | tcgetattr(fd, &oldtio); |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 38 | tio = oldtio; |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 39 | #if 0 |
| 40 | /* Switch off UPPERCASE->lowercase conversion (never used since 198x) |
| 41 | * and XON/XOFF (why we want to mess with this??) |
| 42 | */ |
| 43 | # ifndef IUCLC |
| 44 | # define IUCLC 0 |
| 45 | # endif |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 46 | tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); |
Denys Vlasenko | 7449e18 | 2011-10-22 06:27:41 +0200 | [diff] [blame] | 47 | #endif |
| 48 | /* Switch off echo */ |
| 49 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); |
Denys Vlasenko | 557deb1 | 2010-02-03 12:17:06 +0100 | [diff] [blame] | 50 | tcsetattr(fd, TCSANOW, &tio); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 52 | memset(&sa, 0, sizeof(sa)); |
| 53 | /* sa.sa_flags = 0; - no SA_RESTART! */ |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 54 | /* SIGINT and SIGALRM will interrupt reads below */ |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 55 | sa.sa_handler = askpass_timeout; |
| 56 | sigaction(SIGINT, &sa, &oldsa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 57 | if (timeout) { |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 58 | sigaction_set(SIGALRM, &sa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 59 | alarm(timeout); |
| 60 | } |
| 61 | |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 62 | if (!passwd) |
| 63 | passwd = xmalloc(sizeof_passwd); |
Denys Vlasenko | e936c6d | 2010-02-01 04:55:30 +0100 | [diff] [blame] | 64 | ret = passwd; |
| 65 | i = 0; |
| 66 | while (1) { |
| 67 | int r = read(fd, &ret[i], 1); |
| 68 | if (r < 0) { |
| 69 | /* read is interrupted by timeout or ^C */ |
| 70 | ret = NULL; |
| 71 | break; |
| 72 | } |
| 73 | if (r == 0 /* EOF */ |
| 74 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */ |
| 75 | || ++i == sizeof_passwd-1 /* line limit */ |
| 76 | ) { |
| 77 | ret[i] = '\0'; |
| 78 | break; |
| 79 | } |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (timeout) { |
| 83 | alarm(0); |
| 84 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 85 | sigaction_set(SIGINT, &oldsa); |
Denys Vlasenko | 557deb1 | 2010-02-03 12:17:06 +0100 | [diff] [blame] | 86 | tcsetattr(fd, TCSANOW, &oldtio); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 87 | bb_putchar('\n'); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 88 | fflush_all(); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 89 | return ret; |
| 90 | } |