blob: 2dcead35ab2149d47de280f562259f81edf67f4e [file] [log] [blame]
Eric Andersen6f9a7782004-05-01 01:27:30 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Ask for a password
Eric Andersen6f9a7782004-05-01 01:27:30 +00004 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen6f9a7782004-05-01 01:27:30 +00008 */
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +00009#include "libbb.h"
Eric Andersen6f9a7782004-05-01 01:27:30 +000010
11/* do nothing signal handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000012static void askpass_timeout(int UNUSED_PARAM ignore)
Eric Andersen6f9a7782004-05-01 01:27:30 +000013{
14}
15
Denys Vlasenko17058a02018-04-07 15:50:30 +020016char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt)
Bernhard Reutner-Fischer82b14292008-12-03 18:48:39 +000017{
Denys Vlasenko17058a02018-04-07 15:50:30 +020018#define MAX_LINE 0xfff
Eric Andersen6f9a7782004-05-01 01:27:30 +000019 char *ret;
Denis Vlasenko6429aab2006-09-23 12:22:11 +000020 int i;
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000021 struct sigaction sa, oldsa;
22 struct termios tio, oldtio;
Eric Andersen6f9a7782004-05-01 01:27:30 +000023
Denys Vlasenkod32fc642014-07-01 13:20:22 +020024 tcflush(fd, TCIFLUSH);
25 /* Was buggy: was printing prompt *before* flushing input,
26 * which was upsetting "expect" based scripts of some users.
27 */
Denys Vlasenko7449e182011-10-22 06:27:41 +020028 fputs(prompt, stdout);
29 fflush_all();
Denys Vlasenko7449e182011-10-22 06:27:41 +020030
31 tcgetattr(fd, &oldtio);
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000032 tio = oldtio;
Denys Vlasenko17058a02018-04-07 15:50:30 +020033 /* 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 */
Denys Vlasenko7449e182011-10-22 06:27:41 +020044 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
Denys Vlasenko557deb12010-02-03 12:17:06 +010045 tcsetattr(fd, 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! */
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010049 /* SIGINT and SIGALRM will interrupt reads below */
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000050 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
Denys Vlasenko17058a02018-04-07 15:50:30 +020057 ret = NULL;
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010058 i = 0;
59 while (1) {
Denys Vlasenko17058a02018-04-07 15:50:30 +020060 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
Jonathan Liub6dc13c2013-05-21 17:01:55 +020068 if ((i == 0 && r == 0) /* EOF (^D) with no password */
Denys Vlasenko2f094ae2018-04-07 15:02:20 +020069 || r < 0 /* read is interrupted by timeout or ^C */
Jonathan Liub6dc13c2013-05-21 17:01:55 +020070 ) {
Denys Vlasenko17058a02018-04-07 15:50:30 +020071 ret[i] = '\0'; /* paranoia */
72 nuke_str(ret); /* paranoia */
73 free(ret);
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010074 ret = NULL;
75 break;
76 }
Denys Vlasenko17058a02018-04-07 15:50:30 +020077
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010078 if (r == 0 /* EOF */
79 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
Denys Vlasenko17058a02018-04-07 15:50:30 +020080 || ++i == MAX_LINE /* line limit */
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010081 ) {
82 ret[i] = '\0';
83 break;
84 }
Eric Andersen6f9a7782004-05-01 01:27:30 +000085 }
86
87 if (timeout) {
88 alarm(0);
89 }
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000090 sigaction_set(SIGINT, &oldsa);
Denys Vlasenko557deb12010-02-03 12:17:06 +010091 tcsetattr(fd, TCSANOW, &oldtio);
Denis Vlasenko4daad902007-09-27 10:20:47 +000092 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +010093 fflush_all();
Eric Andersen6f9a7782004-05-01 01:27:30 +000094 return ret;
95}
Denys Vlasenko17058a02018-04-07 15:50:30 +020096char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt)
97{
98 return bb_ask_noecho(STDIN_FILENO, 0, prompt);
99}