12dc4ec0 |
1 | /* |
2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
3 | */ |
4 | |
5 | #include <winsock2.h> |
6 | #include <windows.h> |
7 | #include <stdio.h> |
8 | #include <stdarg.h> |
9 | |
10 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
11 | #include "putty.h" |
12 | |
13 | void fatalbox (char *p, ...) { |
14 | va_list ap; |
15 | fprintf(stderr, "FATAL ERROR: ", p); |
16 | va_start(ap, p); |
17 | vfprintf(stderr, p, ap); |
18 | va_end(ap); |
19 | fputc('\n', stderr); |
20 | WSACleanup(); |
21 | exit(1); |
22 | } |
8d5de777 |
23 | void connection_fatal (char *p, ...) { |
24 | va_list ap; |
25 | fprintf(stderr, "FATAL ERROR: ", p); |
26 | va_start(ap, p); |
27 | vfprintf(stderr, p, ap); |
28 | va_end(ap); |
29 | fputc('\n', stderr); |
30 | WSACleanup(); |
31 | exit(1); |
32 | } |
12dc4ec0 |
33 | |
34 | HANDLE outhandle; |
6f34e365 |
35 | DWORD orig_console_mode; |
36 | |
37 | void begin_session(void) { |
38 | if (!cfg.ldisc_term) |
39 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); |
40 | else |
41 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), orig_console_mode); |
42 | } |
12dc4ec0 |
43 | |
44 | void term_out(void) |
45 | { |
46 | int reap; |
47 | DWORD ret; |
12dc4ec0 |
48 | reap = 0; |
49 | while (reap < inbuf_head) { |
50 | if (!WriteFile(outhandle, inbuf+reap, inbuf_head-reap, &ret, NULL)) |
51 | return; /* give up in panic */ |
52 | reap += ret; |
53 | } |
54 | inbuf_head = 0; |
55 | } |
56 | |
57 | struct input_data { |
58 | DWORD len; |
59 | char buffer[4096]; |
60 | HANDLE event; |
61 | }; |
62 | |
67779be7 |
63 | static int get_password(const char *prompt, char *str, int maxlen) |
64 | { |
65 | HANDLE hin, hout; |
66 | DWORD savemode, i; |
67 | |
68 | #if 0 /* this allows specifying a password some other way */ |
69 | if (password) { |
70 | static int tried_once = 0; |
71 | |
72 | if (tried_once) { |
73 | return 0; |
74 | } else { |
75 | strncpy(str, password, maxlen); |
76 | str[maxlen-1] = '\0'; |
77 | tried_once = 1; |
78 | return 1; |
79 | } |
80 | } |
81 | #endif |
82 | |
83 | hin = GetStdHandle(STD_INPUT_HANDLE); |
84 | hout = GetStdHandle(STD_OUTPUT_HANDLE); |
85 | if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) { |
86 | fprintf(stderr, "Cannot get standard input/output handles"); |
87 | return 0; |
88 | } |
89 | |
90 | GetConsoleMode(hin, &savemode); |
91 | SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | |
92 | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); |
93 | |
94 | WriteFile(hout, prompt, strlen(prompt), &i, NULL); |
95 | ReadFile(hin, str, maxlen-1, &i, NULL); |
96 | |
97 | SetConsoleMode(hin, savemode); |
98 | |
99 | if ((int)i > maxlen) i = maxlen-1; else i = i - 2; |
100 | str[i] = '\0'; |
101 | |
102 | WriteFile(hout, "\r\n", 2, &i, NULL); |
103 | |
104 | return 1; |
105 | } |
106 | |
12dc4ec0 |
107 | int WINAPI stdin_read_thread(void *param) { |
108 | struct input_data *idata = (struct input_data *)param; |
109 | HANDLE inhandle; |
110 | |
111 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
112 | |
113 | while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer), |
114 | &idata->len, NULL)) { |
115 | SetEvent(idata->event); |
116 | } |
117 | |
118 | idata->len = 0; |
119 | SetEvent(idata->event); |
120 | |
121 | return 0; |
122 | } |
123 | |
124 | int main(int argc, char **argv) { |
125 | WSADATA wsadata; |
126 | WORD winsock_ver; |
127 | WSAEVENT netevent, stdinevent; |
128 | HANDLE handles[2]; |
129 | SOCKET socket; |
130 | DWORD threadid; |
131 | struct input_data idata; |
132 | int sending; |
133 | |
67779be7 |
134 | ssh_get_password = get_password; |
135 | |
136 | flags = FLAG_STDERR; |
12dc4ec0 |
137 | /* |
138 | * Process the command line. |
139 | */ |
140 | default_protocol = DEFAULT_PROTOCOL; |
141 | default_port = DEFAULT_PORT; |
142 | do_defaults(NULL); |
143 | while (--argc) { |
144 | char *p = *++argv; |
145 | if (*p == '-') { |
146 | if (!strcmp(p, "-ssh")) { |
147 | default_protocol = cfg.protocol = PROT_SSH; |
148 | default_port = cfg.port = 22; |
67779be7 |
149 | } else if (!strcmp(p, "-v")) { |
150 | flags |= FLAG_VERBOSE; |
12dc4ec0 |
151 | } else if (!strcmp(p, "-log")) { |
152 | logfile = "putty.log"; |
153 | } |
154 | } else if (*p) { |
155 | if (!*cfg.host) { |
156 | char *q = p; |
157 | /* |
158 | * If the hostname starts with "telnet:", set the |
159 | * protocol to Telnet and process the string as a |
160 | * Telnet URL. |
161 | */ |
162 | if (!strncmp(q, "telnet:", 7)) { |
163 | char c; |
164 | |
165 | q += 7; |
166 | if (q[0] == '/' && q[1] == '/') |
167 | q += 2; |
168 | cfg.protocol = PROT_TELNET; |
169 | p = q; |
170 | while (*p && *p != ':' && *p != '/') p++; |
171 | c = *p; |
172 | if (*p) |
173 | *p++ = '\0'; |
174 | if (c == ':') |
175 | cfg.port = atoi(p); |
176 | else |
177 | cfg.port = -1; |
178 | strncpy (cfg.host, q, sizeof(cfg.host)-1); |
179 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
180 | } else { |
181 | /* |
182 | * Three cases. Either (a) there's a nonzero |
183 | * length string followed by an @, in which |
184 | * case that's user and the remainder is host. |
185 | * Or (b) there's only one string, not counting |
186 | * a potential initial @, and it exists in the |
187 | * saved-sessions database. Or (c) only one |
188 | * string and it _doesn't_ exist in the |
189 | * database. |
190 | */ |
191 | char *r = strrchr(p, '@'); |
192 | if (r == p) p++, r = NULL; /* discount initial @ */ |
193 | if (r == NULL) { |
194 | /* |
195 | * One string. |
196 | */ |
197 | do_defaults (p); |
198 | if (cfg.host[0] == '\0') { |
199 | /* No settings for this host; use defaults */ |
200 | strncpy(cfg.host, p, sizeof(cfg.host)-1); |
201 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
202 | cfg.port = 22; |
203 | } |
204 | } else { |
205 | *r++ = '\0'; |
206 | strncpy(cfg.username, p, sizeof(cfg.username)-1); |
207 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
208 | strncpy(cfg.host, r, sizeof(cfg.host)-1); |
209 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
210 | cfg.port = 22; |
211 | } |
212 | } |
213 | } else { |
214 | int len = sizeof(cfg.remote_cmd) - 1; |
215 | char *cp = cfg.remote_cmd; |
216 | int len2; |
217 | |
218 | strncpy(cp, p, len); cp[len] = '\0'; |
219 | len2 = strlen(cp); len -= len2; cp += len2; |
220 | while (--argc) { |
221 | if (len > 0) |
222 | len--, *cp++ = ' '; |
223 | strncpy(cp, *++argv, len); cp[len] = '\0'; |
224 | len2 = strlen(cp); len -= len2; cp += len2; |
225 | } |
226 | cfg.nopty = TRUE; /* command => no terminal */ |
227 | cfg.ldisc_term = TRUE; /* use stdin like a line buffer */ |
228 | break; /* done with cmdline */ |
229 | } |
230 | } |
231 | } |
232 | |
67779be7 |
233 | if (!*cfg.remote_cmd) |
234 | flags |= FLAG_INTERACTIVE; |
235 | |
12dc4ec0 |
236 | /* |
237 | * Select protocol. This is farmed out into a table in a |
238 | * separate file to enable an ssh-free variant. |
239 | */ |
240 | { |
241 | int i; |
242 | back = NULL; |
243 | for (i = 0; backends[i].backend != NULL; i++) |
244 | if (backends[i].protocol == cfg.protocol) { |
245 | back = backends[i].backend; |
246 | break; |
247 | } |
248 | if (back == NULL) { |
249 | fprintf(stderr, "Internal fault: Unsupported protocol found\n"); |
250 | return 1; |
251 | } |
252 | } |
253 | |
254 | /* |
255 | * Initialise WinSock. |
256 | */ |
257 | winsock_ver = MAKEWORD(2, 0); |
258 | if (WSAStartup(winsock_ver, &wsadata)) { |
259 | MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error", |
260 | MB_OK | MB_ICONEXCLAMATION); |
261 | return 1; |
262 | } |
263 | if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) { |
264 | MessageBox(NULL, "WinSock version is incompatible with 2.0", |
265 | "WinSock Error", MB_OK | MB_ICONEXCLAMATION); |
266 | WSACleanup(); |
267 | return 1; |
268 | } |
269 | |
270 | /* |
271 | * Start up the connection. |
272 | */ |
273 | { |
274 | char *error; |
275 | char *realhost; |
276 | |
277 | error = back->init (NULL, cfg.host, cfg.port, &realhost); |
278 | if (error) { |
279 | fprintf(stderr, "Unable to open connection:\n%s", error); |
280 | return 1; |
281 | } |
282 | } |
283 | |
284 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
285 | stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
286 | |
6f34e365 |
287 | GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &orig_console_mode); |
288 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); |
12dc4ec0 |
289 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
290 | |
291 | /* |
292 | * Now we must send the back end oodles of stuff. |
293 | */ |
294 | socket = back->socket(); |
295 | /* |
296 | * Turn off ECHO and LINE input modes. We don't care if this |
297 | * call fails, because we know we aren't necessarily running in |
298 | * a console. |
299 | */ |
300 | WSAEventSelect(socket, netevent, FD_READ | FD_CLOSE); |
301 | handles[0] = netevent; |
302 | handles[1] = stdinevent; |
303 | sending = FALSE; |
304 | while (1) { |
305 | int n; |
306 | n = WaitForMultipleObjects(2, handles, FALSE, INFINITE); |
307 | if (n == 0) { |
308 | WSANETWORKEVENTS things; |
309 | if (!WSAEnumNetworkEvents(socket, netevent, &things)) { |
310 | if (things.lNetworkEvents & FD_READ) |
311 | back->msg(0, FD_READ); |
312 | if (things.lNetworkEvents & FD_CLOSE) { |
313 | back->msg(0, FD_CLOSE); |
314 | break; |
315 | } |
316 | } |
317 | term_out(); |
318 | if (!sending && back->sendok()) { |
319 | /* |
320 | * Create a separate thread to read from stdin. |
321 | * This is a total pain, but I can't find another |
322 | * way to do it: |
323 | * |
324 | * - an overlapped ReadFile or ReadFileEx just |
325 | * doesn't happen; we get failure from |
326 | * ReadFileEx, and ReadFile blocks despite being |
327 | * given an OVERLAPPED structure. Perhaps we |
328 | * can't do overlapped reads on consoles. WHY |
329 | * THE HELL NOT? |
330 | * |
331 | * - WaitForMultipleObjects(netevent, console) |
332 | * doesn't work, because it signals the console |
333 | * when _anything_ happens, including mouse |
334 | * motions and other things that don't cause |
335 | * data to be readable - so we're back to |
336 | * ReadFile blocking. |
337 | */ |
338 | idata.event = stdinevent; |
339 | if (!CreateThread(NULL, 0, stdin_read_thread, |
340 | &idata, 0, &threadid)) { |
341 | fprintf(stderr, "Unable to create second thread\n"); |
342 | exit(1); |
343 | } |
344 | sending = TRUE; |
345 | } |
346 | } else if (n == 1) { |
347 | if (idata.len > 0) { |
348 | back->send(idata.buffer, idata.len); |
349 | } else { |
350 | back->special(TS_EOF); |
351 | } |
352 | } |
353 | } |
354 | WSACleanup(); |
355 | return 0; |
356 | } |