blob: 77c1bcd9566237f3304fad7a8963cb12f3ac3677 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Denys Vlasenko7449e182011-10-22 06:27:41 +020033 fputs(prompt, stdout);
34 fflush_all();
Bernhard Reutner-Fischer82b14292008-12-03 18:48:39 +000035 tcflush(fd, TCIFLUSH);
Denys Vlasenko7449e182011-10-22 06:27:41 +020036
37 tcgetattr(fd, &oldtio);
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000038 tio = oldtio;
Denys Vlasenko7449e182011-10-22 06:27:41 +020039#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
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000046 tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
Denys Vlasenko7449e182011-10-22 06:27:41 +020047#endif
48 /* Switch off echo */
49 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
Denys Vlasenko557deb12010-02-03 12:17:06 +010050 tcsetattr(fd, TCSANOW, &tio);
Eric Andersen6f9a7782004-05-01 01:27:30 +000051
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000052 memset(&sa, 0, sizeof(sa));
53 /* sa.sa_flags = 0; - no SA_RESTART! */
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010054 /* SIGINT and SIGALRM will interrupt reads below */
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000055 sa.sa_handler = askpass_timeout;
56 sigaction(SIGINT, &sa, &oldsa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000057 if (timeout) {
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000058 sigaction_set(SIGALRM, &sa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000059 alarm(timeout);
60 }
61
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010062 if (!passwd)
63 passwd = xmalloc(sizeof_passwd);
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010064 ret = passwd;
65 i = 0;
66 while (1) {
67 int r = read(fd, &ret[i], 1);
Jonathan Liub6dc13c2013-05-21 17:01:55 +020068 if ((i == 0 && r == 0) /* EOF (^D) with no password */
69 || r < 0
70 ) {
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010071 /* read is interrupted by timeout or ^C */
72 ret = NULL;
73 break;
74 }
75 if (r == 0 /* EOF */
76 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
77 || ++i == sizeof_passwd-1 /* line limit */
78 ) {
79 ret[i] = '\0';
80 break;
81 }
Eric Andersen6f9a7782004-05-01 01:27:30 +000082 }
83
84 if (timeout) {
85 alarm(0);
86 }
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000087 sigaction_set(SIGINT, &oldsa);
Denys Vlasenko557deb12010-02-03 12:17:06 +010088 tcsetattr(fd, TCSANOW, &oldtio);
Denis Vlasenko4daad902007-09-27 10:20:47 +000089 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +010090 fflush_all();
Eric Andersen6f9a7782004-05-01 01:27:30 +000091 return ret;
92}