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 | |
| 20 | char *bb_askpass(int timeout, const char * prompt) |
| 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; |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 28 | struct sigaction sa; |
| 29 | struct termios old, new; |
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 | |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 35 | tcgetattr(STDIN_FILENO, &old); |
Rob Landley | e422af6 | 2005-12-12 07:02:15 +0000 | [diff] [blame] | 36 | tcflush(STDIN_FILENO, TCIFLUSH); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 37 | |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 38 | fputs(prompt, stdout); |
| 39 | fflush(stdout); |
| 40 | |
| 41 | tcgetattr(STDIN_FILENO, &new); |
| 42 | new.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); |
| 43 | new.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); |
| 44 | tcsetattr(STDIN_FILENO, TCSANOW, &new); |
| 45 | |
| 46 | if (timeout) { |
| 47 | sa.sa_flags = 0; |
| 48 | sa.sa_handler = askpass_timeout; |
| 49 | sigaction(SIGALRM, &sa, NULL); |
| 50 | alarm(timeout); |
| 51 | } |
| 52 | |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 53 | ret = NULL; |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 54 | /* On timeout, read will hopefully be interrupted by SIGALRM, |
| 55 | * and we return NULL */ |
| 56 | if (read(STDIN_FILENO, passwd, sizeof_passwd-1) > 0) { |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 57 | ret = passwd; |
| 58 | i = 0; |
| 59 | /* Last byte is guaranteed to be 0 |
| 60 | (read did not overwrite it) */ |
| 61 | do { |
| 62 | if (passwd[i] == '\r' || passwd[i] == '\n') |
Denis Vlasenko | ab24e18 | 2006-11-30 16:41:15 +0000 | [diff] [blame] | 63 | passwd[i] = '\0'; |
Denis Vlasenko | 6429aab | 2006-09-23 12:22:11 +0000 | [diff] [blame] | 64 | } while (passwd[i++]); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (timeout) { |
| 68 | alarm(0); |
| 69 | } |
| 70 | |
| 71 | tcsetattr(STDIN_FILENO, TCSANOW, &old); |
Denis Vlasenko | 91e149a | 2007-06-18 10:35:06 +0000 | [diff] [blame] | 72 | putchar('\n'); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 73 | fflush(stdout); |
| 74 | return ret; |
| 75 | } |