[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Ask for a password
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
| 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "libbb.h"
|
---|
| 11 |
|
---|
| 12 | /* do nothing signal handler */
|
---|
[2725] | 13 | static void askpass_timeout(int UNUSED_PARAM ignore)
|
---|
[821] | 14 | {
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[2725] | 17 | char* FAST_FUNC bb_ask_stdin(const char *prompt)
|
---|
[821] | 18 | {
|
---|
[2725] | 19 | return bb_ask(STDIN_FILENO, 0, prompt);
|
---|
| 20 | }
|
---|
| 21 | char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
|
---|
| 22 | {
|
---|
[1765] | 23 | /* Was static char[BIGNUM] */
|
---|
| 24 | enum { sizeof_passwd = 128 };
|
---|
| 25 |
|
---|
[3621] | 26 | char *passwd;
|
---|
[821] | 27 | char *ret;
|
---|
[1765] | 28 | int i;
|
---|
[2725] | 29 | struct sigaction sa, oldsa;
|
---|
| 30 | struct termios tio, oldtio;
|
---|
[821] | 31 |
|
---|
[3621] | 32 | tcflush(fd, TCIFLUSH);
|
---|
| 33 | /* Was buggy: was printing prompt *before* flushing input,
|
---|
| 34 | * which was upsetting "expect" based scripts of some users.
|
---|
| 35 | */
|
---|
[3232] | 36 | fputs(prompt, stdout);
|
---|
| 37 | fflush_all();
|
---|
| 38 |
|
---|
[2725] | 39 | tcgetattr(fd, &oldtio);
|
---|
| 40 | tio = oldtio;
|
---|
[3232] | 41 | #if 0
|
---|
| 42 | /* Switch off UPPERCASE->lowercase conversion (never used since 198x)
|
---|
| 43 | * and XON/XOFF (why we want to mess with this??)
|
---|
| 44 | */
|
---|
| 45 | # ifndef IUCLC
|
---|
| 46 | # define IUCLC 0
|
---|
| 47 | # endif
|
---|
| 48 | tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
|
---|
[2725] | 49 | #endif
|
---|
[3232] | 50 | /* Switch off echo */
|
---|
| 51 | tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
|
---|
[2725] | 52 | tcsetattr(fd, TCSANOW, &tio);
|
---|
[1765] | 53 |
|
---|
[2725] | 54 | memset(&sa, 0, sizeof(sa));
|
---|
| 55 | /* sa.sa_flags = 0; - no SA_RESTART! */
|
---|
| 56 | /* SIGINT and SIGALRM will interrupt reads below */
|
---|
| 57 | sa.sa_handler = askpass_timeout;
|
---|
| 58 | sigaction(SIGINT, &sa, &oldsa);
|
---|
[821] | 59 | if (timeout) {
|
---|
[2725] | 60 | sigaction_set(SIGALRM, &sa);
|
---|
[821] | 61 | alarm(timeout);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[3621] | 64 | passwd = auto_string(xmalloc(sizeof_passwd));
|
---|
[2725] | 65 | ret = passwd;
|
---|
| 66 | i = 0;
|
---|
| 67 | while (1) {
|
---|
| 68 | int r = read(fd, &ret[i], 1);
|
---|
[3621] | 69 | if ((i == 0 && r == 0) /* EOF (^D) with no password */
|
---|
| 70 | || r < 0
|
---|
| 71 | ) {
|
---|
[2725] | 72 | /* read is interrupted by timeout or ^C */
|
---|
| 73 | ret = NULL;
|
---|
| 74 | break;
|
---|
| 75 | }
|
---|
| 76 | if (r == 0 /* EOF */
|
---|
| 77 | || ret[i] == '\r' || ret[i] == '\n' /* EOL */
|
---|
| 78 | || ++i == sizeof_passwd-1 /* line limit */
|
---|
| 79 | ) {
|
---|
| 80 | ret[i] = '\0';
|
---|
| 81 | break;
|
---|
| 82 | }
|
---|
[821] | 83 | }
|
---|
| 84 |
|
---|
| 85 | if (timeout) {
|
---|
| 86 | alarm(0);
|
---|
| 87 | }
|
---|
[2725] | 88 | sigaction_set(SIGINT, &oldsa);
|
---|
| 89 | tcsetattr(fd, TCSANOW, &oldtio);
|
---|
| 90 | bb_putchar('\n');
|
---|
| 91 | fflush_all();
|
---|
[821] | 92 | return ret;
|
---|
| 93 | }
|
---|