blob: 5599c82ef982b8bf7e6aebb411f2b13ae585f225 [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
Bernhard Reutner-Fischer82b14292008-12-03 18:48:39 +000016char* FAST_FUNC bb_ask_stdin(const char *prompt)
17{
18 return bb_ask(STDIN_FILENO, 0, prompt);
19}
20char* FAST_FUNC bb_ask(const int fd, int timeout, const char *prompt)
Eric Andersen6f9a7782004-05-01 01:27:30 +000021{
Denis Vlasenko91e149a2007-06-18 10:35:06 +000022 /* Was static char[BIGNUM] */
23 enum { sizeof_passwd = 128 };
Denis Vlasenko6429aab2006-09-23 12:22:11 +000024
Denys Vlasenko02859aa2015-10-09 18:16:40 +020025 char *passwd;
Eric Andersen6f9a7782004-05-01 01:27:30 +000026 char *ret;
Denis Vlasenko6429aab2006-09-23 12:22:11 +000027 int i;
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000028 struct sigaction sa, oldsa;
29 struct termios tio, oldtio;
Eric Andersen6f9a7782004-05-01 01:27:30 +000030
Denys Vlasenkod32fc642014-07-01 13:20:22 +020031 tcflush(fd, TCIFLUSH);
32 /* Was buggy: was printing prompt *before* flushing input,
33 * which was upsetting "expect" based scripts of some users.
34 */
Denys Vlasenko7449e182011-10-22 06:27:41 +020035 fputs(prompt, stdout);
36 fflush_all();
Denys Vlasenko7449e182011-10-22 06:27:41 +020037
38 tcgetattr(fd, &oldtio);
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000039 tio = oldtio;
Denys Vlasenko7449e182011-10-22 06:27:41 +020040 /* Switch off echo */
41 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
Denys Vlasenko557deb12010-02-03 12:17:06 +010042 tcsetattr(fd, TCSANOW, &tio);
Eric Andersen6f9a7782004-05-01 01:27:30 +000043
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000044 memset(&sa, 0, sizeof(sa));
45 /* sa.sa_flags = 0; - no SA_RESTART! */
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010046 /* SIGINT and SIGALRM will interrupt reads below */
Denis Vlasenkoe5387a02007-10-20 19:20:22 +000047 sa.sa_handler = askpass_timeout;
48 sigaction(SIGINT, &sa, &oldsa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000049 if (timeout) {
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000050 sigaction_set(SIGALRM, &sa);
Eric Andersen6f9a7782004-05-01 01:27:30 +000051 alarm(timeout);
52 }
53
Denys Vlasenko02859aa2015-10-09 18:16:40 +020054 passwd = auto_string(xmalloc(sizeof_passwd));
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010055 ret = passwd;
56 i = 0;
57 while (1) {
58 int r = read(fd, &ret[i], 1);
Jonathan Liub6dc13c2013-05-21 17:01:55 +020059 if ((i == 0 && r == 0) /* EOF (^D) with no password */
Denys Vlasenko2f094ae2018-04-07 15:02:20 +020060 || r < 0 /* read is interrupted by timeout or ^C */
Jonathan Liub6dc13c2013-05-21 17:01:55 +020061 ) {
Denys Vlasenkoe936c6d2010-02-01 04:55:30 +010062 ret = NULL;
63 break;
64 }
65 if (r == 0 /* EOF */
66 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
67 || ++i == sizeof_passwd-1 /* line limit */
68 ) {
69 ret[i] = '\0';
70 break;
71 }
Eric Andersen6f9a7782004-05-01 01:27:30 +000072 }
73
74 if (timeout) {
75 alarm(0);
76 }
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000077 sigaction_set(SIGINT, &oldsa);
Denys Vlasenko557deb12010-02-03 12:17:06 +010078 tcsetattr(fd, 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}