26#include <client/get_password.h>
38# define TERMIO struct termios
42bool tty_password=
false;
43const std::string PASSWORD_SENTINEL(
"\0\0\0\0\0", 5);
51static void get_password(
char *to, uint32_t length,
int fd,
bool echo)
53 char *pos=to,*end=to+length;
58 if (read(fd,&tmp,1) != 1)
60 if (tmp ==
'\b' || (
int) tmp == 127)
66 fputs(
"\b \b",stderr);
73 if (tmp ==
'\n' || tmp ==
'\r' || tmp == 3)
75 if (iscntrl(tmp) || pos == end)
79 while (pos != to && isspace(pos[-1]) ==
' ')
86char *client_get_tty_password(
const char *opt_message)
91 if (isatty(fileno(stderr)))
93 fputs(opt_message ? opt_message :
"Enter password: ",stderr);
96 tcgetattr(fileno(stdin), &org);
98 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
101 tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
102 get_password(buff,
sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
103 tcsetattr(fileno(stdin), TCSADRAIN, &org);
104 if (isatty(fileno(stderr)))
110pair<string, string> parse_password_arg(
string s)
112 if (s.find(
"--password") == 0)
114 if (s ==
"--password")
118 return make_pair(
"password", PASSWORD_SENTINEL);
121 if (s.substr(10,3) ==
"=\"\"" || s.substr(10,3) ==
"=''")
124 return make_pair(
"password", PASSWORD_SENTINEL);
127 if(s.substr(10) ==
"=" && s.length() == 11)
130 return make_pair(
"password", PASSWORD_SENTINEL);
134 else if (s.find(
"-P") == 0)
140 return make_pair(
"password", PASSWORD_SENTINEL);
143 if (s.substr(2,2) ==
"\"\"" || s.substr(2,2) ==
"''")
146 return make_pair(
"password", PASSWORD_SENTINEL);
149 return make_pair(
string(
""),
string(
""));