[821] | 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 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 9 | */
|
---|
| 10 |
|
---|
| 11 | #include "libbb.h"
|
---|
| 12 |
|
---|
| 13 | /* do nothing signal handler */
|
---|
[2725] | 14 | static void askpass_timeout(int UNUSED_PARAM ignore)
|
---|
[821] | 15 | {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[2725] | 18 | char* FAST_FUNC bb_ask_stdin(const char *prompt)
|
---|
[821] | 19 | {
|
---|
[2725] | 20 | return bb_ask(STDIN_FILENO, 0, prompt);
|
---|
| 21 | }
|
---|
| 22 | char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
|
---|
| 23 | {
|
---|
[1765] | 24 | /* Was static char[BIGNUM] */
|
---|
| 25 | enum { sizeof_passwd = 128 };
|
---|
| 26 | static char *passwd;
|
---|
| 27 |
|
---|
[821] | 28 | char *ret;
|
---|
[1765] | 29 | int i;
|
---|
[2725] | 30 | struct sigaction sa, oldsa;
|
---|
| 31 | struct termios tio, oldtio;
|
---|
[821] | 32 |
|
---|
[3232] | 33 | fputs(prompt, stdout);
|
---|
| 34 | fflush_all();
|
---|
| 35 | tcflush(fd, TCIFLUSH);
|
---|
| 36 |
|
---|
[2725] | 37 | tcgetattr(fd, &oldtio);
|
---|
| 38 | tio = oldtio;
|
---|
[3232] | 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
|
---|
| 46 | tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
|
---|
[2725] | 47 | #endif
|
---|
[3232] | 48 | /* Switch off echo */
|
---|
| 49 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
|
---|
[2725] | 50 | tcsetattr(fd, TCSANOW, &tio);
|
---|
[1765] | 51 |
|
---|
[2725] | 52 | memset(&sa, 0, sizeof(sa));
|
---|
| 53 | /* sa.sa_flags = 0; - no SA_RESTART! */
|
---|
| 54 | /* SIGINT and SIGALRM will interrupt reads below */
|
---|
| 55 | sa.sa_handler = askpass_timeout;
|
---|
| 56 | sigaction(SIGINT, &sa, &oldsa);
|
---|
[821] | 57 | if (timeout) {
|
---|
[2725] | 58 | sigaction_set(SIGALRM, &sa);
|
---|
[821] | 59 | alarm(timeout);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[2725] | 62 | if (!passwd)
|
---|
| 63 | passwd = xmalloc(sizeof_passwd);
|
---|
| 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 | }
|
---|
[821] | 80 | }
|
---|
| 81 |
|
---|
| 82 | if (timeout) {
|
---|
| 83 | alarm(0);
|
---|
| 84 | }
|
---|
[2725] | 85 | sigaction_set(SIGINT, &oldsa);
|
---|
| 86 | tcsetattr(fd, TCSANOW, &oldtio);
|
---|
| 87 | bb_putchar('\n');
|
---|
| 88 | fflush_all();
|
---|
[821] | 89 | return ret;
|
---|
| 90 | }
|
---|