forked from FreeTDS/freetds
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpass.c
318 lines (270 loc) · 7.98 KB
/
getpass.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* FreeTDS - Library of routines accessing Sybase and Microsoft databases
* Copyright (C) 2003, 2010 Craig A. Berry craigberry@mac.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include<config.h>
#include<dcdef.h>
#include<descrip.h>
#include<dvidef.h>
#include<iodef.h>
#include<lib$routines.h>
#include<libclidef.h>
#include<smg$routines.h>
#include<smgdef.h>
#include<ssdef.h>
#include<starlet.h>
#include<stsdef.h>
#include<syidef.h>
#include<ctype.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<unistd.h>
#include<freetds/replacements/readpassphrase.h>
#include"readline.h"
staticFILE*tds_rl_instream=NULL;
staticFILE*tds_rl_outstream=NULL;
/*
* A collection of assorted UNIXy input functions for VMS. The core
* functionality is provided by readpassphrase(), and the general
* requirements come mostly from Todd Miller's OpenBSD code but are
* completely reimplemented using native services.
*
* There are simple getpass() and readline() implementations wrapped
* around readpassphrase(), the main difference being that getpass()
* suppresses echoing whereas readline() enables echoing and suppresses
* timeouts. There is also an add_history() stub; echoed lines are
* already stored in the command recall buffer by SMG$READ_COMPOSED_LINE.
*/
#defineMY_PASSWORD_LEN 8192
#defineRECALL_SIZE 50 /* Lines in recall buffer. */
#defineDEFAULT_TIMEOUT 30 /* Seconds to wait for user input. */
/* Flags defined below are VMS-specific. Use the high byte to minimize
* potential conflicts.
*/
#defineRPP_TIMEOUT_OFF 0x01000000 /* Wait indefinitely for input. */
char*
readpassphrase(constchar*prompt, char*pbuf, size_tbuflen, intflags)
{
staticunsigned longkeyboard_id, keytable_id=0;
unsigned longctrl_mask, saved_ctrl_mask=0;
inttimeout_secs=0;
int*timeout_ptr=NULL;
unsigned longstatus=0;
unsigned shortiosb[4];
unsigned shortttchan, result_len=0, stdin_is_tty;
$DESCRIPTOR(ttdsc, "");
$DESCRIPTOR(pbuf_dsc, "");
$DESCRIPTOR(prompt_dsc, "");
char*retval=NULL;
char*myprompt=NULL;
charinput_fspec[MY_PASSWORD_LEN+1];
if (pbuf==NULL||buflen==0) {
errno=EINVAL;
returnNULL;
}
bzero(pbuf, buflen);
pbuf_dsc.dsc$a_pointer=pbuf;
pbuf_dsc.dsc$w_length=buflen-1;
/*
* If stdin is not a terminal and only reading from a terminal is allowed, we
* stop here.
*/
stdin_is_tty=isatty(fileno(stdin));
if (stdin_is_tty!=1&& (flags&RPP_REQUIRE_TTY)) {
errno=ENOTTY;
returnNULL;
}
/*
* We need the file or device associated with stdin in VMS format.
*/
if (fgetname(stdin, input_fspec, 1)) {
ttdsc.dsc$a_pointer= (char*)&input_fspec;
ttdsc.dsc$w_length=strlen(input_fspec);
} else {
errno=EMFILE;
returnNULL;
}
/*
* The prompt is expected to provide its own leading newline.
*/
myprompt=malloc(strlen(prompt) +1);
if (myprompt==NULL) {
errno=ENOMEM;
returnNULL;
}
sprintf(myprompt, "\n%s", prompt);
prompt_dsc.dsc$a_pointer=myprompt;
prompt_dsc.dsc$w_length=strlen(myprompt);
if (!(flags&RPP_ECHO_ON) && (stdin_is_tty)) {
/* Disable Ctrl-T and Ctrl-Y */
ctrl_mask=LIB$M_CLI_CTRLT | LIB$M_CLI_CTRLY;
status=LIB$DISABLE_CTRL(&ctrl_mask, &saved_ctrl_mask);
if (!$VMS_STATUS_SUCCESS(status)) {
errno=EVMSERR;
vaxc$errno=status;
free(myprompt);
returnNULL;
}
}
/*
* Unless timeouts are disabled, find out how long should we wait for input
* before giving up.
*/
if (!(flags&RPP_TIMEOUT_OFF)) {
unsigned longtmo_item=SYI$_LGI_PWD_TMO;
status=LIB$GETSYI(&tmo_item, &timeout_secs);
if (!$VMS_STATUS_SUCCESS(status))
timeout_secs=DEFAULT_TIMEOUT;
timeout_ptr=&timeout_secs;
}
if (!(flags&RPP_ECHO_ON) && (stdin_is_tty)) {
/*
* If we are suppressing echoing, get a line of input with $QIOW.
* Non-echoed lines are not stored for recall. (The same thing
* could be done with SMG but would require maintenance of a virtual
* display and pasteboard.)
*/
status=SYS$ASSIGN(&ttdsc, &ttchan, 0, 0, 0);
if ($VMS_STATUS_SUCCESS(status)) {
unsigned longqio_func=IO$_READPROMPT | IO$M_NOECHO | IO$M_PURGE;
if (!(flags&RPP_TIMEOUT_OFF))
qio_func |= IO$M_TIMED;
bzero(iosb, sizeof(iosb));
status=SYS$QIOW(0,
(unsigned long) ttchan,
qio_func, &iosb, 0, 0, pbuf, buflen-1, timeout_secs, 0, myprompt, strlen(myprompt));
if ($VMS_STATUS_SUCCESS(status)) {
status=iosb[0];
result_len=iosb[1]; /* bytes actually read */
}
(void) SYS$DASSGN(ttchan);
}
} else {
/*
* We are not suppressing echoing because echoing has been explicitly
* enabled and/or we are not reading from a terminal. In this case we
* use SMG, which will store commands for recall. The virtual keyboard
* and key table are static and will only be created if we haven't been
* here before.
*/
status=SS$_NORMAL;
if (keyboard_id==0) {
unsigned charrecall_size=RECALL_SIZE;
status=SMG$CREATE_VIRTUAL_KEYBOARD(&keyboard_id, &ttdsc, 0, 0, &recall_size);
}
if ($VMS_STATUS_SUCCESS(status) &&keytable_id==0) {
status=SMG$CREATE_KEY_TABLE(&keytable_id);
}
if ($VMS_STATUS_SUCCESS(status)) {
status=SMG$READ_COMPOSED_LINE(&keyboard_id,
&keytable_id, &pbuf_dsc, &prompt_dsc, &result_len, 0, 0, 0, timeout_ptr);
}
}
/*
* Process return value from SYS$QIOW or SMG$READ_COMPOSED_LINE.
*/
switch (status) {
caseSS$_TIMEOUT:
errno=ETIMEDOUT;
break;
caseSMG$_EOF:
if (result_len!=0) {
status=SS$_NORMAL;
}
/* fall through */
default:
if ($VMS_STATUS_SUCCESS(status)) {
inti;
if (flags&RPP_FORCELOWER) {
for (i=0; i<result_len; i++)
pbuf[i] =tolower(pbuf[i]);
}
if (flags&RPP_FORCEUPPER) {
for (i=0; i<result_len; i++)
pbuf[i] =toupper(pbuf[i]);
}
if (flags&RPP_SEVENBIT) {
for (i=0; i<result_len; i++)
pbuf[i] &= 0x7f;
}
pbuf[result_len] ='\0';
retval=pbuf;
} else {
errno=EVMSERR;
vaxc$errno=status;
}
} /* end switch */
free(myprompt);
if (!(flags&RPP_ECHO_ON) && (stdin_is_tty)) {
/*
* Reenable previous control processing.
*/
status=LIB$ENABLE_CTRL(&saved_ctrl_mask);
if (!$VMS_STATUS_SUCCESS(status)) {
errno=EVMSERR;
vaxc$errno=status;
returnNULL;
}
}
returnretval;
} /* getpassphrase */
staticcharpassbuf[MY_PASSWORD_LEN+1];
char*
getpass(constchar*prompt)
{
bzero(passbuf, sizeof(passbuf)); /* caller should do this again */
return (readpassphrase(prompt, passbuf, sizeof(passbuf), RPP_ECHO_OFF)
);
} /* getpass */
char*
readline(char*prompt)
{
char*buf=NULL, *s=NULL, *p=NULL, *newline=NULL;
if (tds_rl_instream==NULL)
s=readpassphrase((constchar*) prompt, passbuf, sizeof(passbuf),
RPP_ECHO_ON | RPP_TIMEOUT_OFF);
else
s=fgets(passbuf, sizeof(passbuf), tds_rl_instream);
if (s!=NULL) {
buf= (char*) malloc(strlen(s) +1);
strcpy(buf, s);
}
/* readline is documented to eat the newline. */
if (buf) {
newline=strchr(buf, '\n');
if (newline)
*newline='\0';
}
returnbuf;
} /* readline */
void
add_history(constchar*s)
{
}
FILE**
rl_instream_get_addr(void)
{
return&tds_rl_instream;
}
FILE**
rl_outstream_get_addr(void)
{
return&tds_rl_outstream;
}