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; |
86256dc6 |
189 | int errors; |
c5e438ec |
190 | void *ldisc; |
191 | |
192 | ssh_get_line = console_get_line; |
193 | |
194 | sklist = NULL; |
195 | skcount = sksize = 0; |
196 | /* |
197 | * Initialise port and protocol to sensible defaults. (These |
198 | * will be overridden by more or less anything.) |
199 | */ |
200 | default_protocol = PROT_SSH; |
201 | default_port = 22; |
202 | |
203 | flags = FLAG_STDERR; |
204 | /* |
205 | * Process the command line. |
206 | */ |
207 | do_defaults(NULL, &cfg); |
208 | default_protocol = cfg.protocol; |
209 | default_port = cfg.port; |
86256dc6 |
210 | errors = 0; |
c5e438ec |
211 | { |
212 | /* |
213 | * Override the default protocol if PLINK_PROTOCOL is set. |
214 | */ |
215 | char *p = getenv("PLINK_PROTOCOL"); |
216 | int i; |
217 | if (p) { |
218 | for (i = 0; backends[i].backend != NULL; i++) { |
219 | if (!strcmp(backends[i].name, p)) { |
220 | default_protocol = cfg.protocol = backends[i].protocol; |
221 | default_port = cfg.port = |
222 | backends[i].backend->default_port; |
223 | break; |
224 | } |
225 | } |
226 | } |
227 | } |
228 | while (--argc) { |
229 | char *p = *++argv; |
230 | if (*p == '-') { |
231 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), 1); |
232 | if (ret == -2) { |
233 | fprintf(stderr, |
234 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
235 | errors = 1; |
c5e438ec |
236 | } else if (ret == 2) { |
237 | --argc, ++argv; |
238 | } else if (ret == 1) { |
239 | continue; |
240 | } else if (!strcmp(p, "-batch")) { |
241 | console_batch_mode = 1; |
a0e5ed33 |
242 | } else if (!strcmp(p, "-o")) { |
86256dc6 |
243 | if (argc <= 1) { |
a0e5ed33 |
244 | fprintf(stderr, |
245 | "plink: option \"-o\" requires an argument\n"); |
86256dc6 |
246 | errors = 1; |
247 | } else { |
248 | --argc; |
249 | provide_xrm_string(*++argv); |
250 | } |
251 | } else { |
252 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
253 | errors = 1; |
c5e438ec |
254 | } |
255 | } else if (*p) { |
256 | if (!*cfg.host) { |
257 | char *q = p; |
a0e5ed33 |
258 | |
259 | do_defaults(NULL, &cfg); |
260 | |
c5e438ec |
261 | /* |
262 | * If the hostname starts with "telnet:", set the |
263 | * protocol to Telnet and process the string as a |
264 | * Telnet URL. |
265 | */ |
266 | if (!strncmp(q, "telnet:", 7)) { |
267 | char c; |
268 | |
269 | q += 7; |
270 | if (q[0] == '/' && q[1] == '/') |
271 | q += 2; |
272 | cfg.protocol = PROT_TELNET; |
273 | p = q; |
274 | while (*p && *p != ':' && *p != '/') |
275 | p++; |
276 | c = *p; |
277 | if (*p) |
278 | *p++ = '\0'; |
279 | if (c == ':') |
280 | cfg.port = atoi(p); |
281 | else |
282 | cfg.port = -1; |
283 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
284 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
285 | } else { |
286 | char *r; |
287 | /* |
288 | * Before we process the [user@]host string, we |
289 | * first check for the presence of a protocol |
290 | * prefix (a protocol name followed by ","). |
291 | */ |
292 | r = strchr(p, ','); |
293 | if (r) { |
294 | int i, j; |
295 | for (i = 0; backends[i].backend != NULL; i++) { |
296 | j = strlen(backends[i].name); |
297 | if (j == r - p && |
298 | !memcmp(backends[i].name, p, j)) { |
299 | default_protocol = cfg.protocol = |
300 | backends[i].protocol; |
301 | portnumber = |
302 | backends[i].backend->default_port; |
303 | p = r + 1; |
304 | break; |
305 | } |
306 | } |
307 | } |
308 | |
309 | /* |
310 | * Three cases. Either (a) there's a nonzero |
311 | * length string followed by an @, in which |
312 | * case that's user and the remainder is host. |
313 | * Or (b) there's only one string, not counting |
314 | * a potential initial @, and it exists in the |
315 | * saved-sessions database. Or (c) only one |
316 | * string and it _doesn't_ exist in the |
317 | * database. |
318 | */ |
319 | r = strrchr(p, '@'); |
320 | if (r == p) |
321 | p++, r = NULL; /* discount initial @ */ |
322 | if (r == NULL) { |
323 | /* |
324 | * One string. |
325 | */ |
326 | Config cfg2; |
327 | do_defaults(p, &cfg2); |
328 | if (cfg2.host[0] == '\0') { |
329 | /* No settings for this host; use defaults */ |
330 | strncpy(cfg.host, p, sizeof(cfg.host) - 1); |
331 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
332 | cfg.port = default_port; |
333 | } else { |
334 | cfg = cfg2; |
335 | cfg.remote_cmd_ptr = cfg.remote_cmd; |
336 | } |
337 | } else { |
338 | *r++ = '\0'; |
339 | strncpy(cfg.username, p, sizeof(cfg.username) - 1); |
340 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
341 | strncpy(cfg.host, r, sizeof(cfg.host) - 1); |
342 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
343 | cfg.port = default_port; |
344 | } |
345 | } |
346 | } else { |
347 | char *command; |
348 | int cmdlen, cmdsize; |
349 | cmdlen = cmdsize = 0; |
350 | command = NULL; |
351 | |
352 | while (argc) { |
353 | while (*p) { |
354 | if (cmdlen >= cmdsize) { |
355 | cmdsize = cmdlen + 512; |
356 | command = srealloc(command, cmdsize); |
357 | } |
358 | command[cmdlen++]=*p++; |
359 | } |
360 | if (cmdlen >= cmdsize) { |
361 | cmdsize = cmdlen + 512; |
362 | command = srealloc(command, cmdsize); |
363 | } |
364 | command[cmdlen++]=' '; /* always add trailing space */ |
365 | if (--argc) p = *++argv; |
366 | } |
367 | if (cmdlen) command[--cmdlen]='\0'; |
368 | /* change trailing blank to NUL */ |
369 | cfg.remote_cmd_ptr = command; |
370 | cfg.remote_cmd_ptr2 = NULL; |
371 | cfg.nopty = TRUE; /* command => no terminal */ |
372 | |
373 | break; /* done with cmdline */ |
374 | } |
375 | } |
376 | } |
377 | |
86256dc6 |
378 | if (errors) |
379 | return 1; |
380 | |
c5e438ec |
381 | if (!*cfg.host) { |
382 | usage(); |
383 | } |
384 | |
385 | /* |
386 | * Trim leading whitespace off the hostname if it's there. |
387 | */ |
388 | { |
389 | int space = strspn(cfg.host, " \t"); |
390 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
391 | } |
392 | |
393 | /* See if host is of the form user@host */ |
394 | if (cfg.host[0] != '\0') { |
395 | char *atsign = strchr(cfg.host, '@'); |
396 | /* Make sure we're not overflowing the user field */ |
397 | if (atsign) { |
398 | if (atsign - cfg.host < sizeof cfg.username) { |
399 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
400 | cfg.username[atsign - cfg.host] = '\0'; |
401 | } |
402 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
403 | } |
404 | } |
405 | |
406 | /* |
407 | * Perform command-line overrides on session configuration. |
408 | */ |
409 | cmdline_run_saved(); |
410 | |
411 | /* |
412 | * Trim a colon suffix off the hostname if it's there. |
413 | */ |
414 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
415 | |
416 | /* |
417 | * Remove any remaining whitespace from the hostname. |
418 | */ |
419 | { |
420 | int p1 = 0, p2 = 0; |
421 | while (cfg.host[p2] != '\0') { |
422 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
423 | cfg.host[p1] = cfg.host[p2]; |
424 | p1++; |
425 | } |
426 | p2++; |
427 | } |
428 | cfg.host[p1] = '\0'; |
429 | } |
430 | |
431 | if (!*cfg.remote_cmd_ptr) |
432 | flags |= FLAG_INTERACTIVE; |
433 | |
434 | /* |
435 | * Select protocol. This is farmed out into a table in a |
436 | * separate file to enable an ssh-free variant. |
437 | */ |
438 | { |
439 | int i; |
440 | back = NULL; |
441 | for (i = 0; backends[i].backend != NULL; i++) |
442 | if (backends[i].protocol == cfg.protocol) { |
443 | back = backends[i].backend; |
444 | break; |
445 | } |
446 | if (back == NULL) { |
447 | fprintf(stderr, |
448 | "Internal fault: Unsupported protocol found\n"); |
449 | return 1; |
450 | } |
451 | } |
452 | |
453 | /* |
454 | * Select port. |
455 | */ |
456 | if (portnumber != -1) |
457 | cfg.port = portnumber; |
458 | |
459 | sk_init(); |
460 | |
461 | /* |
462 | * Start up the connection. |
463 | */ |
a0e5ed33 |
464 | logctx = log_init(NULL); |
c5e438ec |
465 | { |
466 | char *error; |
467 | char *realhost; |
468 | /* nodelay is only useful if stdin is a terminal device */ |
469 | int nodelay = cfg.tcp_nodelay && isatty(0); |
470 | |
471 | error = back->init(NULL, &backhandle, cfg.host, cfg.port, |
472 | &realhost, nodelay); |
473 | if (error) { |
984992ef |
474 | fprintf(stderr, "Unable to open connection:\n%s\n", error); |
c5e438ec |
475 | return 1; |
476 | } |
c5e438ec |
477 | back->provide_logctx(backhandle, logctx); |
478 | ldisc = ldisc_create(NULL, back, backhandle, NULL); |
479 | sfree(realhost); |
480 | } |
481 | connopen = 1; |
482 | |
483 | /* |
484 | * Set up the initial console mode. We don't care if this call |
485 | * fails, because we know we aren't necessarily running in a |
486 | * console. |
487 | */ |
488 | tcgetattr(0, &orig_termios); |
489 | atexit(cleanup_termios); |
490 | ldisc_update(NULL, 1, 1); |
491 | sending = FALSE; |
492 | |
493 | while (1) { |
494 | fd_set rset, wset, xset; |
495 | int maxfd; |
496 | int rwx; |
497 | int ret; |
498 | |
499 | FD_ZERO(&rset); |
500 | FD_ZERO(&wset); |
501 | FD_ZERO(&xset); |
502 | maxfd = 0; |
503 | |
504 | if (connopen && !sending && |
505 | back->socket(backhandle) != NULL && |
506 | back->sendok(backhandle) && |
507 | back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
508 | /* If we're OK to send, then try to read from stdin. */ |
509 | FD_SET_MAX(0, maxfd, rset); |
510 | } |
511 | |
512 | if (bufchain_size(&stdout_data) > 0) { |
513 | /* If we have data for stdout, try to write to stdout. */ |
514 | FD_SET_MAX(1, maxfd, wset); |
515 | } |
516 | |
517 | if (bufchain_size(&stderr_data) > 0) { |
518 | /* If we have data for stderr, try to write to stderr. */ |
519 | FD_SET_MAX(2, maxfd, wset); |
520 | } |
521 | |
522 | /* Count the currently active sockets. */ |
523 | i = 0; |
524 | for (socket = first_socket(&socketstate, &rwx); socket >= 0; |
525 | socket = next_socket(&socketstate, &rwx)) i++; |
526 | |
527 | /* Expand the sklist buffer if necessary. */ |
528 | if (i > sksize) { |
529 | sksize = i + 16; |
530 | sklist = srealloc(sklist, sksize * sizeof(*sklist)); |
531 | } |
532 | |
533 | /* |
534 | * Add all currently open sockets to the select sets, and |
535 | * store them in sklist as well. |
536 | */ |
537 | skcount = 0; |
538 | for (socket = first_socket(&socketstate, &rwx); socket >= 0; |
539 | socket = next_socket(&socketstate, &rwx)) { |
540 | sklist[skcount++] = socket; |
541 | if (rwx & 1) |
542 | FD_SET_MAX(socket, maxfd, rset); |
543 | if (rwx & 2) |
544 | FD_SET_MAX(socket, maxfd, wset); |
545 | if (rwx & 4) |
546 | FD_SET_MAX(socket, maxfd, xset); |
547 | } |
548 | |
549 | ret = select(maxfd, &rset, &wset, &xset, NULL); |
550 | |
551 | if (ret < 0) { |
552 | perror("select"); |
553 | exit(1); |
554 | } |
555 | |
556 | for (i = 0; i < skcount; i++) { |
557 | socket = sklist[i]; |
56e5b2db |
558 | /* |
559 | * We must process exceptional notifications before |
560 | * ordinary readability ones, or we may go straight |
561 | * past the urgent marker. |
562 | */ |
563 | if (FD_ISSET(socket, &xset)) |
564 | select_result(socket, 4); |
c5e438ec |
565 | if (FD_ISSET(socket, &rset)) |
566 | select_result(socket, 1); |
567 | if (FD_ISSET(socket, &wset)) |
568 | select_result(socket, 2); |
c5e438ec |
569 | } |
570 | |
571 | if (FD_ISSET(0, &rset)) { |
572 | char buf[4096]; |
573 | int ret; |
574 | |
575 | if (connopen && back->socket(backhandle) != NULL) { |
576 | ret = read(0, buf, sizeof(buf)); |
577 | if (ret < 0) { |
578 | perror("stdin: read"); |
579 | exit(1); |
580 | } else if (ret == 0) { |
581 | back->special(backhandle, TS_EOF); |
582 | sending = FALSE; /* send nothing further after this */ |
583 | } else { |
584 | back->send(backhandle, buf, ret); |
585 | } |
586 | } |
587 | } |
588 | |
589 | if (FD_ISSET(1, &wset)) { |
590 | try_output(0); |
591 | } |
592 | |
593 | if (FD_ISSET(2, &wset)) { |
594 | try_output(1); |
595 | } |
596 | |
597 | if ((!connopen || back->socket(backhandle) == NULL) && |
598 | bufchain_size(&stdout_data) == 0 && |
599 | bufchain_size(&stderr_data) == 0) |
600 | break; /* we closed the connection */ |
601 | } |
602 | exitcode = back->exitcode(backhandle); |
603 | if (exitcode < 0) { |
604 | fprintf(stderr, "Remote process exit code unavailable\n"); |
605 | exitcode = 1; /* this is an error condition */ |
606 | } |
d9c40fd6 |
607 | cleanup_exit(exitcode); |
608 | return exitcode; /* shouldn't happen, but placates gcc */ |
c5e438ec |
609 | } |