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 | * |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 33 | if (!passwd) |
| 34 | passwd = xmalloc(sizeof_passwd); |
| 35 | memset(passwd, 0, sizeof_passwd); |
| 36 | |
Bernhard Reutner-Fischer | 82b1429 | 2008-12-03 18:48:39 +0000 | [diff] [blame] | 37 | tcgetattr(fd, &oldtio); |
| 38 | tcflush(fd, TCIFLUSH); |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 39 | tio = oldtio; |
| 40 | tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); |
| 41 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 42 | tcsetattr_stdin_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! */ |
| 46 | /* SIGINT and SIGALRM will interrupt read below */ |
| 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 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 54 | fputs(prompt, stdout); |
| 55 | fflush(stdout); |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 56 | ret = NULL; |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 57 | /* On timeout or Ctrl-C, read will hopefully be interrupted, |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 58 | * and we return NULL */ |
Bernhard Reutner-Fischer | 82b1429 | 2008-12-03 18:48:39 +0000 | [diff] [blame] | 59 | if (read(fd, passwd, sizeof_passwd - 1) > 0) { |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 60 | ret = passwd; |
| 61 | i = 0; |
| 62 | /* Last byte is guaranteed to be 0 |
| 63 | (read did not overwrite it) */ |
| 64 | do { |
| 65 | if (passwd[i] == '\r' || passwd[i] == '\n') |
Denis Vlasenko | ab24e18 | 2006-11-30 16:41:15 +0000 | [diff] [blame] | 66 | passwd[i] = '\0'; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 67 | } while (passwd[i++]); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (timeout) { |
| 71 | alarm(0); |
| 72 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 73 | sigaction_set(SIGINT, &oldsa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 75 | tcsetattr_stdin_TCSANOW(&oldtio); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 76 | bb_putchar('\n'); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 77 | fflush(stdout); |
| 78 | return ret; |
| 79 | } |