blob: f9b918cecef643a070d35a33d6d7aec88f25524d [file] [log] [blame]
Eric Andersen6f9a7782004-05-01 01:27:30 +00001/* 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-Fischer20f40002006-01-30 17:17:14 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen6f9a7782004-05-01 01:27:30 +00009 */
10
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000011#include "libbb.h"
Eric Andersen6f9a7782004-05-01 01:27:30 +000012
13/* do nothing signal handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000014static void askpass_timeout(int UNUSED_PARAM ignore)
Eric Andersen6f9a7782004-05-01 01:27:30 +000015{
16}
17
Bernhard Reutner-Fischer82b14292008-12-03 18:48:39 +000018char* FAST_FUNC bb_ask_stdin(const char *prompt)
19{
20 return bb_ask(STDIN_FILENO, 0, prompt);
21}
22char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
Eric Andersen6f9a7782004-05-01 01:27:30 +000023{
Denis Vlasenko91e149a2007-06-18 10:35:06 +000024 /* Was static char[BIGNUM] */
25 enum { sizeof_passwd = 128 };
26 static char *passwd;
Denis Vlasenko6429aab2006-09-23 12:22:11 +000027
Eric Andersen6f9a7782004-05-01 01:27:30 +000028 char *ret;
Denis Vlasenko6429aab2006-09-23 12:22:11 +000029 int i;
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000030 struct sigaction sa, oldsa;
31 struct termios tio, oldtio;
Eric Andersen6f9a7782004-05-01 01:27:30 +000032
Denis Vlasenko91e149a2007-06-18 10:35:06 +000033 if (!passwd)
34 passwd = xmalloc(sizeof_passwd);
35 memset(passwd, 0, sizeof_passwd);
36
Bernhard Reutner-Fischer82b14292008-12-03 18:48:39 +000037 tcgetattr(fd, &oldtio);
38 tcflush(fd, TCIFLUSH);
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000039 tio = oldtio;
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020040#ifndef IUCLC
41# define IUCLC 0
42#endif
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000043 tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
44 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
Denis Vlasenko202ac502008-11-05 13:20:58 +000045 tcsetattr_stdin_TCSANOW(&tio);
Eric Andersen6f9a7782004-05-01 01:27:30 +000046
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000047 memset(&sa, 0, sizeof(sa));
48 /* sa.sa_flags = 0; - no SA_RESTART! */
49 /* SIGINT and SIGALRM will interrupt read below */
50 sa.sa_handler = askpass_timeout;
51 sigaction(SIGINT, &sa, &oldsa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000052 if (timeout) {
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000053 sigaction_set(SIGALRM, &sa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000054 alarm(timeout);
55 }
56
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000057 fputs(prompt, stdout);
Denys Vlasenko8131eea2009-11-02 14:19:51 +010058 fflush_all();
Denis Vlasenko6429aab2006-09-23 12:22:11 +000059 ret = NULL;
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000060 /* On timeout or Ctrl-C, read will hopefully be interrupted,
Denis Vlasenko91e149a2007-06-18 10:35:06 +000061 * and we return NULL */
Bernhard Reutner-Fischer82b14292008-12-03 18:48:39 +000062 if (read(fd, passwd, sizeof_passwd - 1) > 0) {
Denis Vlasenko6429aab2006-09-23 12:22:11 +000063 ret = passwd;
64 i = 0;
65 /* Last byte is guaranteed to be 0
66 (read did not overwrite it) */
67 do {
68 if (passwd[i] == '\r' || passwd[i] == '\n')
Denis Vlasenkoab24e182006-11-30 16:41:15 +000069 passwd[i] = '\0';
Denis Vlasenko6429aab2006-09-23 12:22:11 +000070 } while (passwd[i++]);
Eric Andersen6f9a7782004-05-01 01:27:30 +000071 }
72
73 if (timeout) {
74 alarm(0);
75 }
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000076 sigaction_set(SIGINT, &oldsa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000077
Denis Vlasenko202ac502008-11-05 13:20:58 +000078 tcsetattr_stdin_TCSANOW(&oldtio);
Denis Vlasenko4daad902007-09-27 10:20:47 +000079 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +010080 fflush_all();
Eric Andersen6f9a7782004-05-01 01:27:30 +000081 return ret;
82}