26 #include <client/get_password.h>
36 #include <sys/ioctl.h>
39 # define TERMIO struct termios
43 # define TERMIO struct termio
46 # define TERMIO struct sgttyb
52 bool tty_password=
false;
53 const std::string PASSWORD_SENTINEL(
"\0\0\0\0\0", 5);
61 static void get_password(
char *to, uint32_t length,
int fd,
bool echo)
63 char *pos=to,*end=to+length;
68 if (read(fd,&tmp,1) != 1)
70 if (tmp ==
'\b' || (
int) tmp == 127)
76 fputs(
"\b \b",stderr);
83 if (tmp ==
'\n' || tmp ==
'\r' || tmp == 3)
85 if (iscntrl(tmp) || pos == end)
89 while (pos != to && isspace(pos[-1]) ==
' ')
96 char *client_get_tty_password(
const char *opt_message)
101 if (isatty(fileno(stderr)))
103 fputs(opt_message ? opt_message :
"Enter password: ",stderr);
106 # if defined(HAVE_TERMIOS_H)
107 tcgetattr(fileno(stdin), &org);
109 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
112 tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
113 get_password(buff,
sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
114 tcsetattr(fileno(stdin), TCSADRAIN, &org);
115 # elif defined(HAVE_TERMIO_H)
116 ioctl(fileno(stdin), (
int) TCGETA, &org);
118 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
121 ioctl(fileno(stdin),(
int) TCSETA, &tmp);
122 get_password(buff,
sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
123 ioctl(fileno(stdin),(
int) TCSETA, &org);
125 gtty(fileno(stdin), &org);
127 tmp.sg_flags &= ~ECHO;
129 stty(fileno(stdin), &tmp);
130 get_password(buff,
sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
131 stty(fileno(stdin), &org);
133 if (isatty(fileno(stderr)))
139 pair<string, string> parse_password_arg(
string s)
141 if (s.find(
"--password") == 0)
143 if (s ==
"--password")
147 return make_pair(
"password", PASSWORD_SENTINEL);
150 if (s.substr(10,3) ==
"=\"\"" || s.substr(10,3) ==
"=''")
153 return make_pair(
"password", PASSWORD_SENTINEL);
156 if(s.substr(10) ==
"=" && s.length() == 11)
159 return make_pair(
"password", PASSWORD_SENTINEL);
163 else if (s.find(
"-P") == 0)
169 return make_pair(
"password", PASSWORD_SENTINEL);
172 if (s.substr(2,2) ==
"\"\"" || s.substr(2,2) ==
"''")
175 return make_pair(
"password", PASSWORD_SENTINEL);
178 return make_pair(
string(
""),
string(
""));