c5e438ec |
1 | /* |
2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <assert.h> |
8 | #include <stdarg.h> |
9 | #include <unistd.h> |
10 | #include <fcntl.h> |
11 | #include <termios.h> |
12 | |
13 | /* More helpful version of the FD_SET macro, to also handle maxfd. */ |
14 | #define FD_SET_MAX(fd, max, set) do { \ |
15 | FD_SET(fd, &set); \ |
16 | if (max < fd + 1) max = fd + 1; \ |
17 | } while (0) |
18 | |
19 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
20 | #include "putty.h" |
21 | #include "storage.h" |
22 | #include "tree234.h" |
23 | |
24 | #define MAX_STDIN_BACKLOG 4096 |
25 | |
26 | void fatalbox(char *p, ...) |
27 | { |
28 | va_list ap; |
29 | fprintf(stderr, "FATAL ERROR: "); |
30 | va_start(ap, p); |
31 | vfprintf(stderr, p, ap); |
32 | va_end(ap); |
33 | fputc('\n', stderr); |
34 | cleanup_exit(1); |
35 | } |
36 | void modalfatalbox(char *p, ...) |
37 | { |
38 | va_list ap; |
39 | fprintf(stderr, "FATAL ERROR: "); |
40 | va_start(ap, p); |
41 | vfprintf(stderr, p, ap); |
42 | va_end(ap); |
43 | fputc('\n', stderr); |
44 | cleanup_exit(1); |
45 | } |
46 | void connection_fatal(void *frontend, char *p, ...) |
47 | { |
48 | va_list ap; |
49 | fprintf(stderr, "FATAL ERROR: "); |
50 | va_start(ap, p); |
51 | vfprintf(stderr, p, ap); |
52 | va_end(ap); |
53 | fputc('\n', stderr); |
54 | cleanup_exit(1); |
55 | } |
56 | void cmdline_error(char *p, ...) |
57 | { |
58 | va_list ap; |
59 | fprintf(stderr, "plink: "); |
60 | va_start(ap, p); |
61 | vfprintf(stderr, p, ap); |
62 | va_end(ap); |
63 | fputc('\n', stderr); |
64 | exit(1); |
65 | } |
66 | |
67 | struct termios orig_termios; |
68 | |
69 | static Backend *back; |
70 | static void *backhandle; |
71 | |
72 | char *x_get_default(char *key) |
73 | { |
74 | return NULL; /* this is a stub */ |
75 | } |
76 | int term_ldisc(Terminal *term, int mode) |
77 | { |
78 | return FALSE; |
79 | } |
80 | void ldisc_update(void *frontend, int echo, int edit) |
81 | { |
82 | /* Update stdin read mode to reflect changes in line discipline. */ |
83 | struct termios mode; |
84 | |
85 | mode = orig_termios; |
86 | |
87 | if (echo) |
88 | mode.c_lflag |= ECHO; |
89 | else |
90 | mode.c_lflag &= ~ECHO; |
91 | |
92 | if (edit) |
93 | mode.c_lflag |= ISIG | ICANON; |
94 | else |
95 | mode.c_lflag &= ~(ISIG | ICANON); |
96 | |
97 | tcsetattr(0, TCSANOW, &mode); |
98 | } |
99 | |
100 | void cleanup_termios(void) |
101 | { |
102 | tcsetattr(0, TCSANOW, &orig_termios); |
103 | } |
104 | |
105 | bufchain stdout_data, stderr_data; |
106 | |
107 | void try_output(int is_stderr) |
108 | { |
109 | bufchain *chain = (is_stderr ? &stderr_data : &stdout_data); |
110 | int fd = (is_stderr ? 2 : 1); |
111 | void *senddata; |
112 | int sendlen, ret; |
113 | |
d69a46c0 |
114 | if (bufchain_size(chain) == 0) |
115 | return; |
116 | |
c5e438ec |
117 | bufchain_prefix(chain, &senddata, &sendlen); |
118 | ret = write(fd, senddata, sendlen); |
119 | if (ret > 0) |
120 | bufchain_consume(chain, ret); |
121 | else if (ret < 0) { |
122 | perror(is_stderr ? "stderr: write" : "stdout: write"); |
123 | exit(1); |
124 | } |
125 | } |
126 | |
127 | int from_backend(void *frontend_handle, int is_stderr, char *data, int len) |
128 | { |
129 | int osize, esize; |
130 | |
131 | assert(len > 0); |
132 | |
133 | if (is_stderr) { |
134 | bufchain_add(&stderr_data, data, len); |
135 | try_output(1); |
136 | } else { |
137 | bufchain_add(&stdout_data, data, len); |
138 | try_output(0); |
139 | } |
140 | |
141 | osize = bufchain_size(&stdout_data); |
142 | esize = bufchain_size(&stderr_data); |
143 | |
144 | return osize + esize; |
145 | } |
146 | |
147 | /* |
148 | * Short description of parameters. |
149 | */ |
150 | static void usage(void) |
151 | { |
152 | printf("PuTTY Link: command-line connection utility\n"); |
153 | printf("%s\n", ver); |
154 | printf("Usage: plink [options] [user@]host [command]\n"); |
155 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
156 | printf("Options:\n"); |
157 | printf(" -v show verbose messages\n"); |
158 | printf(" -load sessname Load settings from saved session\n"); |
159 | printf(" -ssh -telnet -rlogin -raw\n"); |
160 | printf(" force use of a particular protocol (default SSH)\n"); |
161 | printf(" -P port connect to specified port\n"); |
162 | printf(" -l user connect with specified username\n"); |
163 | printf(" -m file read remote command(s) from file\n"); |
164 | printf(" -batch disable all interactive prompts\n"); |
165 | printf("The following options only apply to SSH connections:\n"); |
166 | printf(" -pw passw login with specified password\n"); |
167 | printf(" -L listen-port:host:port Forward local port to " |
168 | "remote address\n"); |
169 | printf(" -R listen-port:host:port Forward remote port to" |
170 | " local address\n"); |
171 | printf(" -X -x enable / disable X11 forwarding\n"); |
172 | printf(" -A -a enable / disable agent forwarding\n"); |
173 | printf(" -t -T enable / disable pty allocation\n"); |
174 | printf(" -1 -2 force use of particular protocol version\n"); |
175 | printf(" -C enable compression\n"); |
176 | printf(" -i key private key file for authentication\n"); |
177 | exit(1); |
178 | } |
179 | |
180 | int main(int argc, char **argv) |
181 | { |
182 | int sending; |
183 | int portnumber = -1; |
184 | int *sklist; |
185 | int socket; |
186 | int i, skcount, sksize, socketstate; |
187 | int connopen; |
188 | int exitcode; |
c5e438ec |
189 | void *ldisc; |
190 | |
191 | ssh_get_line = console_get_line; |
192 | |
193 | sklist = NULL; |
194 | skcount = sksize = 0; |
195 | /* |
196 | * Initialise port and protocol to sensible defaults. (These |
197 | * will be overridden by more or less anything.) |
198 | */ |
199 | default_protocol = PROT_SSH; |
200 | default_port = 22; |
201 | |
202 | flags = FLAG_STDERR; |
203 | /* |
204 | * Process the command line. |
205 | */ |
206 | do_defaults(NULL, &cfg); |
207 | default_protocol = cfg.protocol; |
208 | default_port = cfg.port; |
209 | { |
210 | /* |
211 | * Override the default protocol if PLINK_PROTOCOL is set. |
212 | */ |
213 | char *p = getenv("PLINK_PROTOCOL"); |
214 | int i; |
215 | if (p) { |
216 | for (i = 0; backends[i].backend != NULL; i++) { |
217 | if (!strcmp(backends[i].name, p)) { |
218 | default_protocol = cfg.protocol = backends[i].protocol; |
219 | default_port = cfg.port = |
220 | backends[i].backend->default_port; |
221 | break; |
222 | } |
223 | } |
224 | } |
225 | } |
226 | while (--argc) { |
227 | char *p = *++argv; |
228 | if (*p == '-') { |
229 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), 1); |
230 | if (ret == -2) { |
231 | fprintf(stderr, |
232 | "plink: option \"%s\" requires an argument\n", p); |
233 | } else if (ret == 2) { |
234 | --argc, ++argv; |
235 | } else if (ret == 1) { |
236 | continue; |
237 | } else if (!strcmp(p, "-batch")) { |
238 | console_batch_mode = 1; |
a0e5ed33 |
239 | } else if (!strcmp(p, "-o")) { |
240 | if (argc <= 1) |
241 | fprintf(stderr, |
242 | "plink: option \"-o\" requires an argument\n"); |
243 | else |
244 | --argc, provide_xrm_string(*++argv); |
c5e438ec |
245 | } |
246 | } else if (*p) { |
247 | if (!*cfg.host) { |
248 | char *q = p; |
a0e5ed33 |
249 | |
250 | do_defaults(NULL, &cfg); |
251 | |
c5e438ec |
252 | /* |
253 | * If the hostname starts with "telnet:", set the |
254 | * protocol to Telnet and process the string as a |
255 | * Telnet URL. |
256 | */ |
257 | if (!strncmp(q, "telnet:", 7)) { |
258 | char c; |
259 | |
260 | q += 7; |
261 | if (q[0] == '/' && q[1] == '/') |
262 | q += 2; |
263 | cfg.protocol = PROT_TELNET; |
264 | p = q; |
265 | while (*p && *p != ':' && *p != '/') |
266 | p++; |
267 | c = *p; |
268 | if (*p) |
269 | *p++ = '\0'; |
270 | if (c == ':') |
271 | cfg.port = atoi(p); |
272 | else |
273 | cfg.port = -1; |
274 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
275 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
276 | } else { |
277 | char *r; |
278 | /* |
279 | * Before we process the [user@]host string, we |
280 | * first check for the presence of a protocol |
281 | * prefix (a protocol name followed by ","). |
282 | */ |
283 | r = strchr(p, ','); |
284 | if (r) { |
285 | int i, j; |
286 | for (i = 0; backends[i].backend != NULL; i++) { |
287 | j = strlen(backends[i].name); |
288 | if (j == r - p && |
289 | !memcmp(backends[i].name, p, j)) { |
290 | default_protocol = cfg.protocol = |
291 | backends[i].protocol; |
292 | portnumber = |
293 | backends[i].backend->default_port; |
294 | p = r + 1; |
295 | break; |
296 | } |
297 | } |
298 | } |
299 | |
300 | /* |
301 | * Three cases. Either (a) there's a nonzero |
302 | * length string followed by an @, in which |
303 | * case that's user and the remainder is host. |
304 | * Or (b) there's only one string, not counting |
305 | * a potential initial @, and it exists in the |
306 | * saved-sessions database. Or (c) only one |
307 | * string and it _doesn't_ exist in the |
308 | * database. |
309 | */ |
310 | r = strrchr(p, '@'); |
311 | if (r == p) |
312 | p++, r = NULL; /* discount initial @ */ |
313 | if (r == NULL) { |
314 | /* |
315 | * One string. |
316 | */ |
317 | Config cfg2; |
318 | do_defaults(p, &cfg2); |
319 | if (cfg2.host[0] == '\0') { |
320 | /* No settings for this host; use defaults */ |
321 | strncpy(cfg.host, p, sizeof(cfg.host) - 1); |
322 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
323 | cfg.port = default_port; |
324 | } else { |
325 | cfg = cfg2; |
326 | cfg.remote_cmd_ptr = cfg.remote_cmd; |
327 | } |
328 | } else { |
329 | *r++ = '\0'; |
330 | strncpy(cfg.username, p, sizeof(cfg.username) - 1); |
331 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
332 | strncpy(cfg.host, r, sizeof(cfg.host) - 1); |
333 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
334 | cfg.port = default_port; |
335 | } |
336 | } |
337 | } else { |
338 | char *command; |
339 | int cmdlen, cmdsize; |
340 | cmdlen = cmdsize = 0; |
341 | command = NULL; |
342 | |
343 | while (argc) { |
344 | while (*p) { |
345 | if (cmdlen >= cmdsize) { |
346 | cmdsize = cmdlen + 512; |
347 | command = srealloc(command, cmdsize); |
348 | } |
349 | command[cmdlen++]=*p++; |
350 | } |
351 | if (cmdlen >= cmdsize) { |
352 | cmdsize = cmdlen + 512; |
353 | command = srealloc(command, cmdsize); |
354 | } |
355 | command[cmdlen++]=' '; /* always add trailing space */ |
356 | if (--argc) p = *++argv; |
357 | } |
358 | if (cmdlen) command[--cmdlen]='\0'; |
359 | /* change trailing blank to NUL */ |
360 | cfg.remote_cmd_ptr = command; |
361 | cfg.remote_cmd_ptr2 = NULL; |
362 | cfg.nopty = TRUE; /* command => no terminal */ |
363 | |
364 | break; /* done with cmdline */ |
365 | } |
366 | } |
367 | } |
368 | |
369 | if (!*cfg.host) { |
370 | usage(); |
371 | } |
372 | |
373 | /* |
374 | * Trim leading whitespace off the hostname if it's there. |
375 | */ |
376 | { |
377 | int space = strspn(cfg.host, " \t"); |
378 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
379 | } |
380 | |
381 | /* See if host is of the form user@host */ |
382 | if (cfg.host[0] != '\0') { |
383 | char *atsign = strchr(cfg.host, '@'); |
384 | /* Make sure we're not overflowing the user field */ |
385 | if (atsign) { |
386 | if (atsign - cfg.host < sizeof cfg.username) { |
387 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
388 | cfg.username[atsign - cfg.host] = '\0'; |
389 | } |
390 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
391 | } |
392 | } |
393 | |
394 | /* |
395 | * Perform command-line overrides on session configuration. |
396 | */ |
397 | cmdline_run_saved(); |
398 | |
399 | /* |
400 | * Trim a colon suffix off the hostname if it's there. |
401 | */ |
402 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
403 | |
404 | /* |
405 | * Remove any remaining whitespace from the hostname. |
406 | */ |
407 | { |
408 | int p1 = 0, p2 = 0; |
409 | while (cfg.host[p2] != '\0') { |
410 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
411 | cfg.host[p1] = cfg.host[p2]; |
412 | p1++; |
413 | } |
414 | p2++; |
415 | } |
416 | cfg.host[p1] = '\0'; |
417 | } |
418 | |
419 | if (!*cfg.remote_cmd_ptr) |
420 | flags |= FLAG_INTERACTIVE; |
421 | |
422 | /* |
423 | * Select protocol. This is farmed out into a table in a |
424 | * separate file to enable an ssh-free variant. |
425 | */ |
426 | { |
427 | int i; |
428 | back = NULL; |
429 | for (i = 0; backends[i].backend != NULL; i++) |
430 | if (backends[i].protocol == cfg.protocol) { |
431 | back = backends[i].backend; |
432 | break; |
433 | } |
434 | if (back == NULL) { |
435 | fprintf(stderr, |
436 | "Internal fault: Unsupported protocol found\n"); |
437 | return 1; |
438 | } |
439 | } |
440 | |
441 | /* |
442 | * Select port. |
443 | */ |
444 | if (portnumber != -1) |
445 | cfg.port = portnumber; |
446 | |
447 | sk_init(); |
448 | |
449 | /* |
450 | * Start up the connection. |
451 | */ |
a0e5ed33 |
452 | logctx = log_init(NULL); |
c5e438ec |
453 | { |
454 | char *error; |
455 | char *realhost; |
456 | /* nodelay is only useful if stdin is a terminal device */ |
457 | int nodelay = cfg.tcp_nodelay && isatty(0); |
458 | |
459 | error = back->init(NULL, &backhandle, cfg.host, cfg.port, |
460 | &realhost, nodelay); |
461 | if (error) { |
984992ef |
462 | fprintf(stderr, "Unable to open connection:\n%s\n", error); |
c5e438ec |
463 | return 1; |
464 | } |
c5e438ec |
465 | back->provide_logctx(backhandle, logctx); |
466 | ldisc = ldisc_create(NULL, back, backhandle, NULL); |
467 | sfree(realhost); |
468 | } |
469 | connopen = 1; |
470 | |
471 | /* |
472 | * Set up the initial console mode. We don't care if this call |
473 | * fails, because we know we aren't necessarily running in a |
474 | * console. |
475 | */ |
476 | tcgetattr(0, &orig_termios); |
477 | atexit(cleanup_termios); |
478 | ldisc_update(NULL, 1, 1); |
479 | sending = FALSE; |
480 | |
481 | while (1) { |
482 | fd_set rset, wset, xset; |
483 | int maxfd; |
484 | int rwx; |
485 | int ret; |
486 | |
487 | FD_ZERO(&rset); |
488 | FD_ZERO(&wset); |
489 | FD_ZERO(&xset); |
490 | maxfd = 0; |
491 | |
492 | if (connopen && !sending && |
493 | back->socket(backhandle) != NULL && |
494 | back->sendok(backhandle) && |
495 | back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
496 | /* If we're OK to send, then try to read from stdin. */ |
497 | FD_SET_MAX(0, maxfd, rset); |
498 | } |
499 | |
500 | if (bufchain_size(&stdout_data) > 0) { |
501 | /* If we have data for stdout, try to write to stdout. */ |
502 | FD_SET_MAX(1, maxfd, wset); |
503 | } |
504 | |
505 | if (bufchain_size(&stderr_data) > 0) { |
506 | /* If we have data for stderr, try to write to stderr. */ |
507 | FD_SET_MAX(2, maxfd, wset); |
508 | } |
509 | |
510 | /* Count the currently active sockets. */ |
511 | i = 0; |
512 | for (socket = first_socket(&socketstate, &rwx); socket >= 0; |
513 | socket = next_socket(&socketstate, &rwx)) i++; |
514 | |
515 | /* Expand the sklist buffer if necessary. */ |
516 | if (i > sksize) { |
517 | sksize = i + 16; |
518 | sklist = srealloc(sklist, sksize * sizeof(*sklist)); |
519 | } |
520 | |
521 | /* |
522 | * Add all currently open sockets to the select sets, and |
523 | * store them in sklist as well. |
524 | */ |
525 | skcount = 0; |
526 | for (socket = first_socket(&socketstate, &rwx); socket >= 0; |
527 | socket = next_socket(&socketstate, &rwx)) { |
528 | sklist[skcount++] = socket; |
529 | if (rwx & 1) |
530 | FD_SET_MAX(socket, maxfd, rset); |
531 | if (rwx & 2) |
532 | FD_SET_MAX(socket, maxfd, wset); |
533 | if (rwx & 4) |
534 | FD_SET_MAX(socket, maxfd, xset); |
535 | } |
536 | |
537 | ret = select(maxfd, &rset, &wset, &xset, NULL); |
538 | |
539 | if (ret < 0) { |
540 | perror("select"); |
541 | exit(1); |
542 | } |
543 | |
544 | for (i = 0; i < skcount; i++) { |
545 | socket = sklist[i]; |
56e5b2db |
546 | /* |
547 | * We must process exceptional notifications before |
548 | * ordinary readability ones, or we may go straight |
549 | * past the urgent marker. |
550 | */ |
551 | if (FD_ISSET(socket, &xset)) |
552 | select_result(socket, 4); |
c5e438ec |
553 | if (FD_ISSET(socket, &rset)) |
554 | select_result(socket, 1); |
555 | if (FD_ISSET(socket, &wset)) |
556 | select_result(socket, 2); |
c5e438ec |
557 | } |
558 | |
559 | if (FD_ISSET(0, &rset)) { |
560 | char buf[4096]; |
561 | int ret; |
562 | |
563 | if (connopen && back->socket(backhandle) != NULL) { |
564 | ret = read(0, buf, sizeof(buf)); |
565 | if (ret < 0) { |
566 | perror("stdin: read"); |
567 | exit(1); |
568 | } else if (ret == 0) { |
569 | back->special(backhandle, TS_EOF); |
570 | sending = FALSE; /* send nothing further after this */ |
571 | } else { |
572 | back->send(backhandle, buf, ret); |
573 | } |
574 | } |
575 | } |
576 | |
577 | if (FD_ISSET(1, &wset)) { |
578 | try_output(0); |
579 | } |
580 | |
581 | if (FD_ISSET(2, &wset)) { |
582 | try_output(1); |
583 | } |
584 | |
585 | if ((!connopen || back->socket(backhandle) == NULL) && |
586 | bufchain_size(&stdout_data) == 0 && |
587 | bufchain_size(&stderr_data) == 0) |
588 | break; /* we closed the connection */ |
589 | } |
590 | exitcode = back->exitcode(backhandle); |
591 | if (exitcode < 0) { |
592 | fprintf(stderr, "Remote process exit code unavailable\n"); |
593 | exitcode = 1; /* this is an error condition */ |
594 | } |
d9c40fd6 |
595 | cleanup_exit(exitcode); |
596 | return exitcode; /* shouldn't happen, but placates gcc */ |
c5e438ec |
597 | } |