928.1.1
by Eric Day
Started client changes. |
1 |
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Sun Microsystems, Inc.
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
8 |
* the Free Software Foundation; version 2 of the License.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 |
*/
|
|
19 |
||
20 |
/*
|
|
21 |
** Ask for a password from tty
|
|
22 |
** This is an own file to avoid conflicts with curses
|
|
23 |
*/
|
|
24 |
||
1241.9.1
by Monty Taylor
Removed global.h. Fixed all the headers. |
25 |
#include "config.h" |
994.2.4
by Monty Taylor
Blast. Fixed some make distcheck issues. |
26 |
#include "client/get_password.h" |
928.1.1
by Eric Day
Started client changes. |
27 |
|
1627.2.2
by Monty Taylor
Moved password parsing code into get_password.cc. |
28 |
#include <string> |
29 |
||
928.1.1
by Eric Day
Started client changes. |
30 |
#include <string.h> |
31 |
#include <stdio.h> |
|
32 |
#include <stdlib.h> |
|
33 |
#include <ctype.h> |
|
34 |
#include <unistd.h> |
|
35 |
||
36 |
#include <sys/ioctl.h> |
|
37 |
#ifdef HAVE_TERMIOS_H /* For tty-password */ |
|
38 |
# include <termios.h>
|
|
39 |
# define TERMIO struct termios
|
|
40 |
#else
|
|
41 |
# ifdef HAVE_TERMIO_H /* For tty-password */ |
|
42 |
# include <termio.h>
|
|
43 |
# define TERMIO struct termio
|
|
44 |
# else
|
|
45 |
# include <sgtty.h>
|
|
46 |
# define TERMIO struct sgttyb
|
|
47 |
# endif
|
|
48 |
#endif
|
|
49 |
||
1627.2.2
by Monty Taylor
Moved password parsing code into get_password.cc. |
50 |
using namespace std; |
51 |
||
52 |
bool tty_password= false; |
|
53 |
const std::string PASSWORD_SENTINEL("\0\0\0\0\0", 5); |
|
54 |
||
928.1.1
by Eric Day
Started client changes. |
55 |
/*
|
56 |
Can't use fgets, because readline will get confused
|
|
57 |
length is max number of chars in to, not counting \0
|
|
58 |
to will not include the eol characters.
|
|
59 |
*/
|
|
60 |
||
61 |
static void get_password(char *to, uint32_t length,int fd, bool echo) |
|
62 |
{
|
|
63 |
char *pos=to,*end=to+length; |
|
64 |
||
65 |
for (;;) |
|
66 |
{
|
|
67 |
char tmp; |
|
68 |
if (read(fd,&tmp,1) != 1) |
|
69 |
break; |
|
70 |
if (tmp == '\b' || (int) tmp == 127) |
|
71 |
{
|
|
72 |
if (pos != to) |
|
73 |
{
|
|
74 |
if (echo) |
|
75 |
{
|
|
76 |
fputs("\b \b",stdout); |
|
77 |
fflush(stdout); |
|
78 |
}
|
|
79 |
pos--; |
|
80 |
continue; |
|
81 |
}
|
|
82 |
}
|
|
83 |
if (tmp == '\n' || tmp == '\r' || tmp == 3) |
|
84 |
break; |
|
85 |
if (iscntrl(tmp) || pos == end) |
|
86 |
continue; |
|
87 |
if (echo) |
|
88 |
{
|
|
89 |
fputc('*',stdout); |
|
90 |
fflush(stdout); |
|
91 |
}
|
|
92 |
*(pos++) = tmp; |
|
93 |
}
|
|
94 |
while (pos != to && isspace(pos[-1]) == ' ') |
|
95 |
pos--; /* Allow dummy space at end */ |
|
96 |
*pos=0; |
|
97 |
return; |
|
98 |
}
|
|
99 |
||
100 |
||
101 |
char *client_get_tty_password(const char *opt_message) |
|
102 |
{
|
|
103 |
TERMIO org,tmp; |
|
104 |
char buff[80]; |
|
105 |
||
106 |
if (isatty(fileno(stdout))) |
|
107 |
{
|
|
108 |
fputs(opt_message ? opt_message : "Enter password: ",stdout); |
|
109 |
fflush(stdout); |
|
110 |
}
|
|
111 |
# if defined(HAVE_TERMIOS_H)
|
|
112 |
tcgetattr(fileno(stdin), &org); |
|
113 |
tmp = org; |
|
114 |
tmp.c_lflag &= ~(ECHO | ISIG | ICANON); |
|
115 |
tmp.c_cc[VMIN] = 1; |
|
116 |
tmp.c_cc[VTIME] = 0; |
|
117 |
tcsetattr(fileno(stdin), TCSADRAIN, &tmp); |
|
118 |
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout))); |
|
119 |
tcsetattr(fileno(stdin), TCSADRAIN, &org); |
|
120 |
# elif defined(HAVE_TERMIO_H)
|
|
121 |
ioctl(fileno(stdin), (int) TCGETA, &org); |
|
122 |
tmp=org; |
|
123 |
tmp.c_lflag &= ~(ECHO | ISIG | ICANON); |
|
124 |
tmp.c_cc[VMIN] = 1; |
|
125 |
tmp.c_cc[VTIME]= 0; |
|
126 |
ioctl(fileno(stdin),(int) TCSETA, &tmp); |
|
127 |
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout))); |
|
128 |
ioctl(fileno(stdin),(int) TCSETA, &org); |
|
129 |
# else
|
|
130 |
gtty(fileno(stdin), &org); |
|
131 |
tmp=org; |
|
132 |
tmp.sg_flags &= ~ECHO; |
|
133 |
tmp.sg_flags |= RAW; |
|
134 |
stty(fileno(stdin), &tmp); |
|
135 |
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout))); |
|
136 |
stty(fileno(stdin), &org); |
|
137 |
# endif
|
|
138 |
if (isatty(fileno(stdout))) |
|
139 |
fputc('\n',stdout); |
|
140 |
||
141 |
return strdup(buff); |
|
142 |
}
|
|
1627.2.2
by Monty Taylor
Moved password parsing code into get_password.cc. |
143 |
|
144 |
pair<string, string> parse_password_arg(string s) |
|
145 |
{
|
|
146 |
if (s.find("--password") == 0) |
|
147 |
{
|
|
148 |
if (s == "--password") |
|
149 |
{
|
|
150 |
tty_password= true; |
|
151 |
//check if no argument is passed.
|
|
152 |
return make_pair("password", PASSWORD_SENTINEL); |
|
153 |
}
|
|
154 |
||
155 |
if (s.substr(10,3) == "=\"\"" || s.substr(10,3) == "=''") |
|
156 |
{
|
|
157 |
// Check if --password="" or --password=''
|
|
158 |
return make_pair("password", PASSWORD_SENTINEL); |
|
159 |
}
|
|
160 |
||
161 |
if(s.substr(10) == "=" && s.length() == 11) |
|
162 |
{
|
|
163 |
// check if --password= and return a default value
|
|
164 |
return make_pair("password", PASSWORD_SENTINEL); |
|
165 |
}
|
|
166 |
}
|
|
167 |
||
168 |
return make_pair(string(""), string("")); |
|
169 |
}
|
|
170 |