busybox/libbb/bb_askpass.c
<<
>>
Prefs
   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 *
   7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   8 */
   9#include "libbb.h"
  10
  11/* do nothing signal handler */
  12static void askpass_timeout(int UNUSED_PARAM ignore)
  13{
  14}
  15
  16char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt)
  17{
  18#define MAX_LINE 0xfff
  19        char *ret;
  20        int i;
  21        struct sigaction sa, oldsa;
  22        struct termios tio, oldtio;
  23
  24        tcflush(fd, TCIFLUSH);
  25        /* Was buggy: was printing prompt *before* flushing input,
  26         * which was upsetting "expect" based scripts of some users.
  27         */
  28        fputs(prompt, stdout);
  29        fflush_all();
  30
  31        tcgetattr(fd, &oldtio);
  32        tio = oldtio;
  33        /* Switch off echo. ECHOxyz meaning:
  34         * ECHO    echo input chars
  35         * ECHOE   echo BS-SP-BS on erase character
  36         * ECHOK   echo kill char specially, not as ^c (ECHOKE controls how exactly)
  37         * ECHOKE  erase all input via BS-SP-BS on kill char (else go to next line)
  38         * ECHOCTL Echo ctrl chars as ^c (else echo verbatim:
  39         *         e.g. up arrow emits "ESC-something" and thus moves cursor up!)
  40         * ECHONL  Echo NL even if ECHO is not set
  41         * ECHOPRT On erase, echo erased chars
  42         *         [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen]
  43         */
  44        tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
  45        tcsetattr(fd, TCSANOW, &tio);
  46
  47        memset(&sa, 0, sizeof(sa));
  48        /* sa.sa_flags = 0; - no SA_RESTART! */
  49        /* SIGINT and SIGALRM will interrupt reads below */
  50        sa.sa_handler = askpass_timeout;
  51        sigaction(SIGINT, &sa, &oldsa);
  52        if (timeout) {
  53                sigaction_set(SIGALRM, &sa);
  54                alarm(timeout);
  55        }
  56
  57        ret = NULL;
  58        i = 0;
  59        while (1) {
  60                int r;
  61
  62                /* User input is uber-slow, no need to optimize reallocs.
  63                 * Grow it on every char.
  64                 */
  65                ret = xrealloc(ret, i + 2);
  66                r = read(fd, &ret[i], 1);
  67
  68                if ((i == 0 && r == 0) /* EOF (^D) with no password */
  69                 || r < 0 /* read is interrupted by timeout or ^C */
  70                ) {
  71                        ret[i] = '\0'; /* paranoia */
  72                        nuke_str(ret); /* paranoia */
  73                        free(ret);
  74                        ret = NULL;
  75                        break;
  76                }
  77
  78                if (r == 0 /* EOF */
  79                 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
  80                 || ++i == MAX_LINE /* line limit */
  81                ) {
  82                        ret[i] = '\0';
  83                        break;
  84                }
  85        }
  86
  87        if (timeout) {
  88                alarm(0);
  89        }
  90        sigaction_set(SIGINT, &oldsa);
  91        tcsetattr(fd, TCSANOW, &oldtio);
  92        bb_putchar('\n');
  93        fflush_all();
  94        return ret;
  95}
  96char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt)
  97{
  98        return bb_ask_noecho(STDIN_FILENO, 0, prompt);
  99}
 100