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 | |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 11 | #include <termios.h> |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 12 | |
| 13 | #include "libbb.h" |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 14 | |
| 15 | /* do nothing signal handler */ |
Bernhard Reutner-Fischer | 20f4000 | 2006-01-30 17:17:14 +0000 | [diff] [blame] | 16 | static void askpass_timeout(int ATTRIBUTE_UNUSED ignore) |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 20 | char *bb_askpass(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 }; |
| 24 | static char *passwd; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 25 | |
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 | |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 31 | if (!passwd) |
| 32 | passwd = xmalloc(sizeof_passwd); |
| 33 | memset(passwd, 0, sizeof_passwd); |
| 34 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 35 | tcgetattr(STDIN_FILENO, &oldtio); |
Rob Landley | e422af6 | 2005-12-12 07:02:15 +0000 | [diff] [blame] | 36 | tcflush(STDIN_FILENO, TCIFLUSH); |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 37 | tio = oldtio; |
| 38 | tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); |
| 39 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); |
| 40 | tcsetattr(STDIN_FILENO, TCSANOW, &tio); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 42 | memset(&sa, 0, sizeof(sa)); |
| 43 | /* sa.sa_flags = 0; - no SA_RESTART! */ |
| 44 | /* SIGINT and SIGALRM will interrupt read below */ |
| 45 | sa.sa_handler = askpass_timeout; |
| 46 | sigaction(SIGINT, &sa, &oldsa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 47 | if (timeout) { |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 48 | sigaction_set(SIGALRM, &sa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 49 | alarm(timeout); |
| 50 | } |
| 51 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 52 | fputs(prompt, stdout); |
| 53 | fflush(stdout); |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 54 | ret = NULL; |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 55 | /* On timeout or Ctrl-C, read will hopefully be interrupted, |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 56 | * and we return NULL */ |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 57 | if (read(STDIN_FILENO, passwd, sizeof_passwd - 1) > 0) { |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 58 | ret = passwd; |
| 59 | i = 0; |
| 60 | /* Last byte is guaranteed to be 0 |
| 61 | (read did not overwrite it) */ |
| 62 | do { |
| 63 | if (passwd[i] == '\r' || passwd[i] == '\n') |
Denis Vlasenko | ab24e18 | 2006-11-30 16:41:15 +0000 | [diff] [blame] | 64 | passwd[i] = '\0'; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 65 | } while (passwd[i++]); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | if (timeout) { |
| 69 | alarm(0); |
| 70 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 71 | sigaction_set(SIGINT, &oldsa); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | e5387a0 | 2007-10-20 19:20:22 +0000 | [diff] [blame] | 73 | tcsetattr(STDIN_FILENO, TCSANOW, &oldtio); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 74 | bb_putchar('\n'); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 75 | fflush(stdout); |
| 76 | return ret; |
| 77 | } |