c5e438ec |
1 | /* |
2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
5673d44e |
7 | #include <errno.h> |
c5e438ec |
8 | #include <assert.h> |
9 | #include <stdarg.h> |
5673d44e |
10 | #include <signal.h> |
c5e438ec |
11 | #include <unistd.h> |
12 | #include <fcntl.h> |
13 | #include <termios.h> |
5a9eb105 |
14 | #include <pwd.h> |
15 | #include <sys/ioctl.h> |
2a6848cc |
16 | #include <sys/select.h> |
c5e438ec |
17 | |
c5e438ec |
18 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
19 | #include "putty.h" |
20 | #include "storage.h" |
21 | #include "tree234.h" |
22 | |
23 | #define MAX_STDIN_BACKLOG 4096 |
24 | |
25 | void fatalbox(char *p, ...) |
26 | { |
27 | va_list ap; |
28 | fprintf(stderr, "FATAL ERROR: "); |
29 | va_start(ap, p); |
30 | vfprintf(stderr, p, ap); |
31 | va_end(ap); |
32 | fputc('\n', stderr); |
33 | cleanup_exit(1); |
34 | } |
35 | void modalfatalbox(char *p, ...) |
36 | { |
37 | va_list ap; |
38 | fprintf(stderr, "FATAL ERROR: "); |
39 | va_start(ap, p); |
40 | vfprintf(stderr, p, ap); |
41 | va_end(ap); |
42 | fputc('\n', stderr); |
43 | cleanup_exit(1); |
44 | } |
45 | void connection_fatal(void *frontend, char *p, ...) |
46 | { |
47 | va_list ap; |
48 | fprintf(stderr, "FATAL ERROR: "); |
49 | va_start(ap, p); |
50 | vfprintf(stderr, p, ap); |
51 | va_end(ap); |
52 | fputc('\n', stderr); |
53 | cleanup_exit(1); |
54 | } |
55 | void cmdline_error(char *p, ...) |
56 | { |
57 | va_list ap; |
58 | fprintf(stderr, "plink: "); |
59 | va_start(ap, p); |
60 | vfprintf(stderr, p, ap); |
61 | va_end(ap); |
62 | fputc('\n', stderr); |
63 | exit(1); |
64 | } |
65 | |
c6ccd5c2 |
66 | static int local_tty = 0; /* do we have a local tty? */ |
67 | static struct termios orig_termios; |
c5e438ec |
68 | |
69 | static Backend *back; |
70 | static void *backhandle; |
3ea863a3 |
71 | static Config cfg; |
c5e438ec |
72 | |
5a9eb105 |
73 | /* |
74 | * Default settings that are specific to pterm. |
75 | */ |
c85623f9 |
76 | char *platform_default_s(const char *name) |
5a9eb105 |
77 | { |
5a9eb105 |
78 | if (!strcmp(name, "TermType")) |
799dfcfa |
79 | return dupstr(getenv("TERM")); |
80 | if (!strcmp(name, "UserName")) |
81 | return get_username(); |
5a9eb105 |
82 | return NULL; |
83 | } |
84 | |
c85623f9 |
85 | int platform_default_i(const char *name, int def) |
5a9eb105 |
86 | { |
87 | if (!strcmp(name, "TermWidth") || |
88 | !strcmp(name, "TermHeight")) { |
89 | struct winsize size; |
90 | if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0) |
91 | return (!strcmp(name, "TermWidth") ? size.ws_col : size.ws_row); |
92 | } |
93 | return def; |
94 | } |
95 | |
9a30e26b |
96 | FontSpec platform_default_fontspec(const char *name) |
97 | { |
98 | FontSpec ret; |
99 | *ret.name = '\0'; |
100 | return ret; |
101 | } |
102 | |
103 | Filename platform_default_filename(const char *name) |
104 | { |
105 | Filename ret; |
106 | if (!strcmp(name, "LogFileName")) |
107 | strcpy(ret.path, "putty.log"); |
108 | else |
109 | *ret.path = '\0'; |
110 | return ret; |
111 | } |
112 | |
c85623f9 |
113 | char *x_get_default(const char *key) |
c5e438ec |
114 | { |
115 | return NULL; /* this is a stub */ |
116 | } |
117 | int term_ldisc(Terminal *term, int mode) |
118 | { |
119 | return FALSE; |
120 | } |
121 | void ldisc_update(void *frontend, int echo, int edit) |
122 | { |
123 | /* Update stdin read mode to reflect changes in line discipline. */ |
124 | struct termios mode; |
125 | |
c6ccd5c2 |
126 | if (!local_tty) return; |
127 | |
c5e438ec |
128 | mode = orig_termios; |
129 | |
130 | if (echo) |
131 | mode.c_lflag |= ECHO; |
132 | else |
133 | mode.c_lflag &= ~ECHO; |
134 | |
8cdf0e5f |
135 | if (edit) { |
136 | mode.c_iflag |= ICRNL; |
c5e438ec |
137 | mode.c_lflag |= ISIG | ICANON; |
e8269d30 |
138 | mode.c_oflag |= OPOST; |
8cdf0e5f |
139 | } else { |
140 | mode.c_iflag &= ~ICRNL; |
c5e438ec |
141 | mode.c_lflag &= ~(ISIG | ICANON); |
e8269d30 |
142 | mode.c_oflag &= ~OPOST; |
c6ccd5c2 |
143 | /* Solaris sets these to unhelpful values */ |
259d0428 |
144 | mode.c_cc[VMIN] = 1; |
145 | mode.c_cc[VTIME] = 0; |
c6ccd5c2 |
146 | /* FIXME: perhaps what we do with IXON/IXOFF should be an |
147 | * argument to ldisc_update(), to allow implementation of SSH-2 |
148 | * "xon-xoff" and Rlogin's equivalent? */ |
149 | mode.c_iflag &= ~IXON; |
150 | mode.c_iflag &= ~IXOFF; |
8cdf0e5f |
151 | } |
d0cb8c0c |
152 | /* |
153 | * Mark parity errors and (more important) BREAK on input. This |
154 | * is more complex than it need be because POSIX-2001 suggests |
155 | * that escaping of valid 0xff in the input stream is dependent on |
156 | * IGNPAR being clear even though marking of BREAK isn't. NetBSD |
157 | * 2.0 goes one worse and makes it dependent on INPCK too. We |
158 | * deal with this by forcing these flags into a useful state and |
159 | * then faking the state in which we found them in from_tty() if |
160 | * we get passed a parity or framing error. |
161 | */ |
162 | mode.c_iflag = (mode.c_iflag | INPCK | PARMRK) & ~IGNPAR; |
c5e438ec |
163 | |
164 | tcsetattr(0, TCSANOW, &mode); |
165 | } |
166 | |
c6ccd5c2 |
167 | /* Helper function to extract a special character from a termios. */ |
168 | static char *get_ttychar(struct termios *t, int index) |
169 | { |
170 | cc_t c = t->c_cc[index]; |
171 | #if defined(_POSIX_VDISABLE) |
172 | if (c == _POSIX_VDISABLE) |
173 | return dupprintf(""); |
174 | #endif |
175 | return dupprintf("^<%d>", c); |
176 | } |
177 | |
178 | char *get_ttymode(void *frontend, const char *mode) |
179 | { |
180 | /* |
181 | * Propagate appropriate terminal modes from the local terminal, |
182 | * if any. |
183 | */ |
184 | if (!local_tty) return NULL; |
185 | |
186 | #define GET_CHAR(ourname, uxname) \ |
187 | do { \ |
188 | if (strcmp(mode, ourname) == 0) \ |
189 | return get_ttychar(&orig_termios, uxname); \ |
190 | } while(0) |
191 | #define GET_BOOL(ourname, uxname, uxmemb, transform) \ |
192 | do { \ |
193 | if (strcmp(mode, ourname) == 0) { \ |
194 | int b = (orig_termios.uxmemb & uxname) != 0; \ |
195 | transform; \ |
196 | return dupprintf("%d", b); \ |
197 | } \ |
198 | } while (0) |
199 | |
200 | /* |
201 | * Modes that want to be the same on all terminal devices involved. |
202 | */ |
203 | /* All the special characters supported by SSH */ |
204 | #if defined(VINTR) |
205 | GET_CHAR("INTR", VINTR); |
206 | #endif |
207 | #if defined(VQUIT) |
208 | GET_CHAR("QUIT", VQUIT); |
209 | #endif |
210 | #if defined(VERASE) |
211 | GET_CHAR("ERASE", VERASE); |
212 | #endif |
213 | #if defined(VKILL) |
214 | GET_CHAR("KILL", VKILL); |
215 | #endif |
216 | #if defined(VEOF) |
217 | GET_CHAR("EOF", VEOF); |
218 | #endif |
219 | #if defined(VEOL) |
220 | GET_CHAR("EOL", VEOL); |
221 | #endif |
222 | #if defined(VEOL2) |
223 | GET_CHAR("EOL2", VEOL2); |
224 | #endif |
225 | #if defined(VSTART) |
226 | GET_CHAR("START", VSTART); |
227 | #endif |
228 | #if defined(VSTOP) |
229 | GET_CHAR("STOP", VSTOP); |
230 | #endif |
231 | #if defined(VSUSP) |
232 | GET_CHAR("SUSP", VSUSP); |
233 | #endif |
234 | #if defined(VDSUSP) |
235 | GET_CHAR("DSUSP", VDSUSP); |
236 | #endif |
237 | #if defined(VREPRINT) |
238 | GET_CHAR("REPRINT", VREPRINT); |
239 | #endif |
240 | #if defined(VWERASE) |
241 | GET_CHAR("WERASE", VWERASE); |
242 | #endif |
243 | #if defined(VLNEXT) |
244 | GET_CHAR("LNEXT", VLNEXT); |
245 | #endif |
246 | #if defined(VFLUSH) |
247 | GET_CHAR("FLUSH", VFLUSH); |
248 | #endif |
249 | #if defined(VSWTCH) |
250 | GET_CHAR("SWTCH", VSWTCH); |
251 | #endif |
252 | #if defined(VSTATUS) |
253 | GET_CHAR("STATUS", VSTATUS); |
254 | #endif |
255 | #if defined(VDISCARD) |
256 | GET_CHAR("DISCARD", VDISCARD); |
257 | #endif |
258 | /* Modes that "configure" other major modes. These should probably be |
259 | * considered as user preferences. */ |
260 | /* Configuration of ICANON */ |
261 | #if defined(ECHOK) |
262 | GET_BOOL("ECHOK", ECHOK, c_lflag, ); |
263 | #endif |
264 | #if defined(ECHOKE) |
265 | GET_BOOL("ECHOKE", ECHOKE, c_lflag, ); |
266 | #endif |
267 | #if defined(ECHOE) |
268 | GET_BOOL("ECHOE", ECHOE, c_lflag, ); |
269 | #endif |
270 | #if defined(ECHONL) |
271 | GET_BOOL("ECHONL", ECHONL, c_lflag, ); |
272 | #endif |
273 | #if defined(XCASE) |
274 | GET_BOOL("XCASE", XCASE, c_lflag, ); |
275 | #endif |
276 | /* Configuration of ECHO */ |
277 | #if defined(ECHOCTL) |
278 | GET_BOOL("ECHOCTL", ECHOCTL, c_lflag, ); |
279 | #endif |
280 | /* Configuration of IXON/IXOFF */ |
281 | #if defined(IXANY) |
282 | GET_BOOL("IXANY", IXANY, c_iflag, ); |
283 | #endif |
e8269d30 |
284 | /* Configuration of OPOST */ |
7016cde4 |
285 | #if defined(OLCUC) |
286 | GET_BOOL("OLCUC", OLCUC, c_oflag, ); |
287 | #endif |
e8269d30 |
288 | #if defined(ONLCR) |
289 | GET_BOOL("ONLCR", ONLCR, c_oflag, ); |
290 | #endif |
291 | #if defined(OCRNL) |
292 | GET_BOOL("OCRNL", OCRNL, c_oflag, ); |
293 | #endif |
294 | #if defined(ONOCR) |
295 | GET_BOOL("ONOCR", ONOCR, c_oflag, ); |
296 | #endif |
20923089 |
297 | #if defined(ONLRET) |
e8269d30 |
298 | GET_BOOL("ONLRET", ONLRET, c_oflag, ); |
299 | #endif |
c6ccd5c2 |
300 | |
301 | /* |
302 | * Modes that want to be set in only one place, and that we have |
303 | * squashed locally. |
304 | */ |
305 | #if defined(ISIG) |
306 | GET_BOOL("ISIG", ISIG, c_lflag, ); |
307 | #endif |
308 | #if defined(ICANON) |
309 | GET_BOOL("ICANON", ICANON, c_lflag, ); |
310 | #endif |
311 | #if defined(ECHO) |
312 | GET_BOOL("ECHO", ECHO, c_lflag, ); |
313 | #endif |
314 | #if defined(IXON) |
315 | GET_BOOL("IXON", IXON, c_iflag, ); |
316 | #endif |
317 | #if defined(IXOFF) |
318 | GET_BOOL("IXOFF", IXOFF, c_iflag, ); |
319 | #endif |
e8269d30 |
320 | #if defined(OPOST) |
321 | GET_BOOL("OPOST", OPOST, c_oflag, ); |
322 | #endif |
c6ccd5c2 |
323 | |
324 | /* |
325 | * We do not propagate the following modes: |
326 | * - Parity/serial settings, which are a local affair and don't |
327 | * make sense propagated over SSH's 8-bit byte-stream. |
328 | * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD |
329 | * - Things that want to be enabled in one place that we don't |
330 | * squash locally. |
7016cde4 |
331 | * IUCLC |
c6ccd5c2 |
332 | * - Status bits. |
333 | * PENDIN |
334 | * - Things I don't know what to do with. (FIXME) |
e8269d30 |
335 | * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN |
336 | * INLCR IGNCR ICRNL |
c6ccd5c2 |
337 | */ |
338 | |
339 | #undef GET_CHAR |
340 | #undef GET_BOOL |
341 | |
342 | /* Fall through to here for unrecognised names, or ones that are |
343 | * unsupported on this platform */ |
344 | return NULL; |
345 | } |
346 | |
c5e438ec |
347 | void cleanup_termios(void) |
348 | { |
c6ccd5c2 |
349 | if (local_tty) |
350 | tcsetattr(0, TCSANOW, &orig_termios); |
c5e438ec |
351 | } |
352 | |
353 | bufchain stdout_data, stderr_data; |
354 | |
355 | void try_output(int is_stderr) |
356 | { |
357 | bufchain *chain = (is_stderr ? &stderr_data : &stdout_data); |
358 | int fd = (is_stderr ? 2 : 1); |
359 | void *senddata; |
360 | int sendlen, ret; |
361 | |
d69a46c0 |
362 | if (bufchain_size(chain) == 0) |
363 | return; |
364 | |
c5e438ec |
365 | bufchain_prefix(chain, &senddata, &sendlen); |
366 | ret = write(fd, senddata, sendlen); |
367 | if (ret > 0) |
368 | bufchain_consume(chain, ret); |
369 | else if (ret < 0) { |
370 | perror(is_stderr ? "stderr: write" : "stdout: write"); |
371 | exit(1); |
372 | } |
373 | } |
374 | |
9fab77dc |
375 | int from_backend(void *frontend_handle, int is_stderr, |
376 | const char *data, int len) |
c5e438ec |
377 | { |
378 | int osize, esize; |
379 | |
c5e438ec |
380 | if (is_stderr) { |
381 | bufchain_add(&stderr_data, data, len); |
382 | try_output(1); |
383 | } else { |
384 | bufchain_add(&stdout_data, data, len); |
385 | try_output(0); |
386 | } |
387 | |
388 | osize = bufchain_size(&stdout_data); |
389 | esize = bufchain_size(&stderr_data); |
390 | |
391 | return osize + esize; |
392 | } |
393 | |
d0cb8c0c |
394 | /* |
395 | * Handle data from a local tty in PARMRK format. |
396 | */ |
397 | static void from_tty(void *buf, unsigned len) |
398 | { |
399 | char *p, *q, *end; |
400 | static enum {NORMAL, FF, FF00} state = NORMAL; |
401 | |
402 | p = buf; end = buf + len; |
403 | while (p < end) { |
404 | switch (state) { |
405 | case NORMAL: |
406 | if (*p == '\xff') { |
407 | p++; |
408 | state = FF; |
409 | } else { |
410 | q = memchr(p, '\xff', end - p); |
411 | if (q == NULL) q = end; |
412 | back->send(backhandle, p, q - p); |
413 | p = q; |
414 | } |
415 | break; |
416 | case FF: |
417 | if (*p == '\xff') { |
418 | back->send(backhandle, p, 1); |
419 | p++; |
420 | state = NORMAL; |
421 | } else if (*p == '\0') { |
422 | p++; |
423 | state = FF00; |
424 | } else abort(); |
425 | break; |
426 | case FF00: |
427 | if (*p == '\0') { |
428 | back->special(backhandle, TS_BRK); |
429 | } else { |
430 | /* |
431 | * Pretend that PARMRK wasn't set. This involves |
432 | * faking what INPCK and IGNPAR would have done if |
433 | * we hadn't overridden them. Unfortunately, we |
434 | * can't do this entirely correctly because INPCK |
435 | * distinguishes between framing and parity |
436 | * errors, but PARMRK format represents both in |
437 | * the same way. We assume that parity errors are |
438 | * more common than framing errors, and hence |
439 | * treat all input errors as being subject to |
440 | * INPCK. |
441 | */ |
442 | if (orig_termios.c_iflag & INPCK) { |
443 | /* If IGNPAR is set, we throw away the character. */ |
444 | if (!(orig_termios.c_iflag & IGNPAR)) { |
445 | /* PE/FE get passed on as NUL. */ |
446 | *p = 0; |
447 | back->send(backhandle, p, 1); |
448 | } |
449 | } else { |
450 | /* INPCK not set. Assume we got a parity error. */ |
451 | back->send(backhandle, p, 1); |
452 | } |
453 | } |
454 | p++; |
455 | state = NORMAL; |
456 | } |
457 | } |
458 | } |
459 | |
5673d44e |
460 | int signalpipe[2]; |
461 | |
462 | void sigwinch(int signum) |
463 | { |
464 | write(signalpipe[1], "x", 1); |
465 | } |
466 | |
c5e438ec |
467 | /* |
74aca06d |
468 | * In Plink our selects are synchronous, so these functions are |
469 | * empty stubs. |
470 | */ |
471 | int uxsel_input_add(int fd, int rwx) { return 0; } |
472 | void uxsel_input_remove(int id) { } |
473 | |
474 | /* |
c5e438ec |
475 | * Short description of parameters. |
476 | */ |
477 | static void usage(void) |
478 | { |
479 | printf("PuTTY Link: command-line connection utility\n"); |
480 | printf("%s\n", ver); |
481 | printf("Usage: plink [options] [user@]host [command]\n"); |
482 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
483 | printf("Options:\n"); |
2285d016 |
484 | printf(" -V print version information and exit\n"); |
485 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
c5e438ec |
486 | printf(" -v show verbose messages\n"); |
487 | printf(" -load sessname Load settings from saved session\n"); |
488 | printf(" -ssh -telnet -rlogin -raw\n"); |
afd4d0d2 |
489 | printf(" force use of a particular protocol\n"); |
c5e438ec |
490 | printf(" -P port connect to specified port\n"); |
491 | printf(" -l user connect with specified username\n"); |
c5e438ec |
492 | printf(" -batch disable all interactive prompts\n"); |
493 | printf("The following options only apply to SSH connections:\n"); |
494 | printf(" -pw passw login with specified password\n"); |
dbe6c525 |
495 | printf(" -D [listen-IP:]listen-port\n"); |
496 | printf(" Dynamic SOCKS-based port forwarding\n"); |
497 | printf(" -L [listen-IP:]listen-port:host:port\n"); |
498 | printf(" Forward local port to remote address\n"); |
499 | printf(" -R [listen-IP:]listen-port:host:port\n"); |
500 | printf(" Forward remote port to local address\n"); |
c5e438ec |
501 | printf(" -X -x enable / disable X11 forwarding\n"); |
502 | printf(" -A -a enable / disable agent forwarding\n"); |
503 | printf(" -t -T enable / disable pty allocation\n"); |
504 | printf(" -1 -2 force use of particular protocol version\n"); |
05581745 |
505 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
c5e438ec |
506 | printf(" -C enable compression\n"); |
507 | printf(" -i key private key file for authentication\n"); |
54018d95 |
508 | printf(" -m file read remote command(s) from file\n"); |
09bdfcbb |
509 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
b72c366d |
510 | printf(" -N don't start a shell/command (SSH-2 only)\n"); |
dc108ebc |
511 | exit(1); |
512 | } |
513 | |
514 | static void version(void) |
515 | { |
516 | printf("plink: %s\n", ver); |
c5e438ec |
517 | exit(1); |
518 | } |
519 | |
520 | int main(int argc, char **argv) |
521 | { |
522 | int sending; |
523 | int portnumber = -1; |
0ff9ea38 |
524 | int *fdlist; |
525 | int fd; |
526 | int i, fdcount, fdsize, fdstate; |
c5e438ec |
527 | int connopen; |
528 | int exitcode; |
86256dc6 |
529 | int errors; |
09bdfcbb |
530 | int use_subsystem = 0; |
b51259f6 |
531 | void *ldisc, *logctx; |
39934deb |
532 | long now; |
c5e438ec |
533 | |
534 | ssh_get_line = console_get_line; |
535 | |
0ff9ea38 |
536 | fdlist = NULL; |
537 | fdcount = fdsize = 0; |
c5e438ec |
538 | /* |
539 | * Initialise port and protocol to sensible defaults. (These |
540 | * will be overridden by more or less anything.) |
541 | */ |
542 | default_protocol = PROT_SSH; |
543 | default_port = 22; |
544 | |
545 | flags = FLAG_STDERR; |
546 | /* |
547 | * Process the command line. |
548 | */ |
549 | do_defaults(NULL, &cfg); |
18e62ad8 |
550 | loaded_session = FALSE; |
c5e438ec |
551 | default_protocol = cfg.protocol; |
552 | default_port = cfg.port; |
86256dc6 |
553 | errors = 0; |
c5e438ec |
554 | { |
555 | /* |
556 | * Override the default protocol if PLINK_PROTOCOL is set. |
557 | */ |
558 | char *p = getenv("PLINK_PROTOCOL"); |
559 | int i; |
560 | if (p) { |
561 | for (i = 0; backends[i].backend != NULL; i++) { |
562 | if (!strcmp(backends[i].name, p)) { |
563 | default_protocol = cfg.protocol = backends[i].protocol; |
564 | default_port = cfg.port = |
565 | backends[i].backend->default_port; |
566 | break; |
567 | } |
568 | } |
569 | } |
570 | } |
571 | while (--argc) { |
572 | char *p = *++argv; |
573 | if (*p == '-') { |
9b41d3a8 |
574 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
575 | 1, &cfg); |
c5e438ec |
576 | if (ret == -2) { |
577 | fprintf(stderr, |
578 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
579 | errors = 1; |
c5e438ec |
580 | } else if (ret == 2) { |
581 | --argc, ++argv; |
582 | } else if (ret == 1) { |
583 | continue; |
584 | } else if (!strcmp(p, "-batch")) { |
585 | console_batch_mode = 1; |
09bdfcbb |
586 | } else if (!strcmp(p, "-s")) { |
587 | /* Save status to write to cfg later. */ |
588 | use_subsystem = 1; |
dc108ebc |
589 | } else if (!strcmp(p, "-V")) { |
590 | version(); |
2285d016 |
591 | } else if (!strcmp(p, "-pgpfp")) { |
592 | pgp_fingerprints(); |
593 | exit(1); |
a0e5ed33 |
594 | } else if (!strcmp(p, "-o")) { |
86256dc6 |
595 | if (argc <= 1) { |
a0e5ed33 |
596 | fprintf(stderr, |
597 | "plink: option \"-o\" requires an argument\n"); |
86256dc6 |
598 | errors = 1; |
599 | } else { |
600 | --argc; |
601 | provide_xrm_string(*++argv); |
602 | } |
603 | } else { |
604 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
605 | errors = 1; |
c5e438ec |
606 | } |
607 | } else if (*p) { |
608 | if (!*cfg.host) { |
609 | char *q = p; |
a0e5ed33 |
610 | |
611 | do_defaults(NULL, &cfg); |
612 | |
c5e438ec |
613 | /* |
614 | * If the hostname starts with "telnet:", set the |
615 | * protocol to Telnet and process the string as a |
616 | * Telnet URL. |
617 | */ |
618 | if (!strncmp(q, "telnet:", 7)) { |
619 | char c; |
620 | |
621 | q += 7; |
622 | if (q[0] == '/' && q[1] == '/') |
623 | q += 2; |
624 | cfg.protocol = PROT_TELNET; |
625 | p = q; |
626 | while (*p && *p != ':' && *p != '/') |
627 | p++; |
628 | c = *p; |
629 | if (*p) |
630 | *p++ = '\0'; |
631 | if (c == ':') |
632 | cfg.port = atoi(p); |
633 | else |
634 | cfg.port = -1; |
635 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
636 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
637 | } else { |
3608528b |
638 | char *r, *user, *host; |
c5e438ec |
639 | /* |
640 | * Before we process the [user@]host string, we |
641 | * first check for the presence of a protocol |
642 | * prefix (a protocol name followed by ","). |
643 | */ |
644 | r = strchr(p, ','); |
645 | if (r) { |
646 | int i, j; |
647 | for (i = 0; backends[i].backend != NULL; i++) { |
648 | j = strlen(backends[i].name); |
649 | if (j == r - p && |
650 | !memcmp(backends[i].name, p, j)) { |
651 | default_protocol = cfg.protocol = |
652 | backends[i].protocol; |
653 | portnumber = |
654 | backends[i].backend->default_port; |
655 | p = r + 1; |
656 | break; |
657 | } |
658 | } |
659 | } |
660 | |
661 | /* |
3608528b |
662 | * A nonzero length string followed by an @ is treated |
663 | * as a username. (We discount an _initial_ @.) The |
664 | * rest of the string (or the whole string if no @) |
665 | * is treated as a session name and/or hostname. |
c5e438ec |
666 | */ |
667 | r = strrchr(p, '@'); |
668 | if (r == p) |
669 | p++, r = NULL; /* discount initial @ */ |
3608528b |
670 | if (r) { |
671 | *r++ = '\0'; |
672 | user = p, host = r; |
673 | } else { |
674 | user = NULL, host = p; |
675 | } |
676 | |
677 | /* |
678 | * Now attempt to load a saved session with the |
679 | * same name as the hostname. |
680 | */ |
681 | { |
c5e438ec |
682 | Config cfg2; |
3608528b |
683 | do_defaults(host, &cfg2); |
18e62ad8 |
684 | if (loaded_session || cfg2.host[0] == '\0') { |
c5e438ec |
685 | /* No settings for this host; use defaults */ |
18e62ad8 |
686 | /* (or session was already loaded with -load) */ |
3608528b |
687 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
c5e438ec |
688 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
689 | cfg.port = default_port; |
690 | } else { |
691 | cfg = cfg2; |
c5e438ec |
692 | } |
3608528b |
693 | } |
694 | |
695 | if (user) { |
696 | /* Patch in specified username. */ |
697 | strncpy(cfg.username, user, |
698 | sizeof(cfg.username) - 1); |
c5e438ec |
699 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
c5e438ec |
700 | } |
3608528b |
701 | |
c5e438ec |
702 | } |
703 | } else { |
704 | char *command; |
705 | int cmdlen, cmdsize; |
706 | cmdlen = cmdsize = 0; |
707 | command = NULL; |
708 | |
709 | while (argc) { |
710 | while (*p) { |
711 | if (cmdlen >= cmdsize) { |
712 | cmdsize = cmdlen + 512; |
3d88e64d |
713 | command = sresize(command, cmdsize, char); |
c5e438ec |
714 | } |
715 | command[cmdlen++]=*p++; |
716 | } |
717 | if (cmdlen >= cmdsize) { |
718 | cmdsize = cmdlen + 512; |
3d88e64d |
719 | command = sresize(command, cmdsize, char); |
c5e438ec |
720 | } |
721 | command[cmdlen++]=' '; /* always add trailing space */ |
722 | if (--argc) p = *++argv; |
723 | } |
724 | if (cmdlen) command[--cmdlen]='\0'; |
725 | /* change trailing blank to NUL */ |
726 | cfg.remote_cmd_ptr = command; |
727 | cfg.remote_cmd_ptr2 = NULL; |
728 | cfg.nopty = TRUE; /* command => no terminal */ |
729 | |
730 | break; /* done with cmdline */ |
731 | } |
732 | } |
733 | } |
734 | |
86256dc6 |
735 | if (errors) |
736 | return 1; |
737 | |
c5e438ec |
738 | if (!*cfg.host) { |
739 | usage(); |
740 | } |
741 | |
742 | /* |
743 | * Trim leading whitespace off the hostname if it's there. |
744 | */ |
745 | { |
746 | int space = strspn(cfg.host, " \t"); |
747 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
748 | } |
749 | |
750 | /* See if host is of the form user@host */ |
751 | if (cfg.host[0] != '\0') { |
5dd103a8 |
752 | char *atsign = strrchr(cfg.host, '@'); |
c5e438ec |
753 | /* Make sure we're not overflowing the user field */ |
754 | if (atsign) { |
755 | if (atsign - cfg.host < sizeof cfg.username) { |
756 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
757 | cfg.username[atsign - cfg.host] = '\0'; |
758 | } |
759 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
760 | } |
761 | } |
762 | |
763 | /* |
764 | * Perform command-line overrides on session configuration. |
765 | */ |
9b41d3a8 |
766 | cmdline_run_saved(&cfg); |
c5e438ec |
767 | |
768 | /* |
09bdfcbb |
769 | * Apply subsystem status. |
770 | */ |
771 | if (use_subsystem) |
772 | cfg.ssh_subsys = TRUE; |
773 | |
774 | /* |
c5e438ec |
775 | * Trim a colon suffix off the hostname if it's there. |
776 | */ |
777 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
778 | |
779 | /* |
780 | * Remove any remaining whitespace from the hostname. |
781 | */ |
782 | { |
783 | int p1 = 0, p2 = 0; |
784 | while (cfg.host[p2] != '\0') { |
785 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
786 | cfg.host[p1] = cfg.host[p2]; |
787 | p1++; |
788 | } |
789 | p2++; |
790 | } |
791 | cfg.host[p1] = '\0'; |
792 | } |
793 | |
a79e1969 |
794 | if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd) |
c5e438ec |
795 | flags |= FLAG_INTERACTIVE; |
796 | |
797 | /* |
798 | * Select protocol. This is farmed out into a table in a |
799 | * separate file to enable an ssh-free variant. |
800 | */ |
801 | { |
802 | int i; |
803 | back = NULL; |
804 | for (i = 0; backends[i].backend != NULL; i++) |
805 | if (backends[i].protocol == cfg.protocol) { |
806 | back = backends[i].backend; |
807 | break; |
808 | } |
809 | if (back == NULL) { |
810 | fprintf(stderr, |
811 | "Internal fault: Unsupported protocol found\n"); |
812 | return 1; |
813 | } |
814 | } |
815 | |
816 | /* |
817 | * Select port. |
818 | */ |
819 | if (portnumber != -1) |
820 | cfg.port = portnumber; |
821 | |
5673d44e |
822 | /* |
823 | * Set up the pipe we'll use to tell us about SIGWINCH. |
824 | */ |
825 | if (pipe(signalpipe) < 0) { |
826 | perror("pipe"); |
827 | exit(1); |
828 | } |
829 | putty_signal(SIGWINCH, sigwinch); |
830 | |
c5e438ec |
831 | sk_init(); |
0ff9ea38 |
832 | uxsel_init(); |
c5e438ec |
833 | |
834 | /* |
835 | * Start up the connection. |
836 | */ |
c229ef97 |
837 | logctx = log_init(NULL, &cfg); |
b51259f6 |
838 | console_provide_logctx(logctx); |
c5e438ec |
839 | { |
cbe2d68f |
840 | const char *error; |
c5e438ec |
841 | char *realhost; |
842 | /* nodelay is only useful if stdin is a terminal device */ |
843 | int nodelay = cfg.tcp_nodelay && isatty(0); |
844 | |
86916870 |
845 | error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, |
79bf227b |
846 | &realhost, nodelay, cfg.tcp_keepalives); |
c5e438ec |
847 | if (error) { |
984992ef |
848 | fprintf(stderr, "Unable to open connection:\n%s\n", error); |
c5e438ec |
849 | return 1; |
850 | } |
c5e438ec |
851 | back->provide_logctx(backhandle, logctx); |
fe5634f6 |
852 | ldisc = ldisc_create(&cfg, NULL, back, backhandle, NULL); |
c5e438ec |
853 | sfree(realhost); |
854 | } |
855 | connopen = 1; |
856 | |
857 | /* |
858 | * Set up the initial console mode. We don't care if this call |
859 | * fails, because we know we aren't necessarily running in a |
860 | * console. |
861 | */ |
c6ccd5c2 |
862 | local_tty = (tcgetattr(0, &orig_termios) == 0); |
c5e438ec |
863 | atexit(cleanup_termios); |
864 | ldisc_update(NULL, 1, 1); |
865 | sending = FALSE; |
39934deb |
866 | now = GETTICKCOUNT(); |
c5e438ec |
867 | |
868 | while (1) { |
869 | fd_set rset, wset, xset; |
870 | int maxfd; |
871 | int rwx; |
872 | int ret; |
873 | |
874 | FD_ZERO(&rset); |
875 | FD_ZERO(&wset); |
876 | FD_ZERO(&xset); |
877 | maxfd = 0; |
878 | |
5673d44e |
879 | FD_SET_MAX(signalpipe[0], maxfd, rset); |
880 | |
c5e438ec |
881 | if (connopen && !sending && |
882 | back->socket(backhandle) != NULL && |
883 | back->sendok(backhandle) && |
884 | back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
885 | /* If we're OK to send, then try to read from stdin. */ |
886 | FD_SET_MAX(0, maxfd, rset); |
887 | } |
888 | |
889 | if (bufchain_size(&stdout_data) > 0) { |
890 | /* If we have data for stdout, try to write to stdout. */ |
891 | FD_SET_MAX(1, maxfd, wset); |
892 | } |
893 | |
894 | if (bufchain_size(&stderr_data) > 0) { |
895 | /* If we have data for stderr, try to write to stderr. */ |
896 | FD_SET_MAX(2, maxfd, wset); |
897 | } |
898 | |
0ff9ea38 |
899 | /* Count the currently active fds. */ |
c5e438ec |
900 | i = 0; |
0ff9ea38 |
901 | for (fd = first_fd(&fdstate, &rwx); fd >= 0; |
902 | fd = next_fd(&fdstate, &rwx)) i++; |
c5e438ec |
903 | |
0ff9ea38 |
904 | /* Expand the fdlist buffer if necessary. */ |
905 | if (i > fdsize) { |
906 | fdsize = i + 16; |
907 | fdlist = sresize(fdlist, fdsize, int); |
c5e438ec |
908 | } |
909 | |
910 | /* |
0ff9ea38 |
911 | * Add all currently open fds to the select sets, and store |
912 | * them in fdlist as well. |
c5e438ec |
913 | */ |
0ff9ea38 |
914 | fdcount = 0; |
915 | for (fd = first_fd(&fdstate, &rwx); fd >= 0; |
916 | fd = next_fd(&fdstate, &rwx)) { |
917 | fdlist[fdcount++] = fd; |
c5e438ec |
918 | if (rwx & 1) |
0ff9ea38 |
919 | FD_SET_MAX(fd, maxfd, rset); |
c5e438ec |
920 | if (rwx & 2) |
0ff9ea38 |
921 | FD_SET_MAX(fd, maxfd, wset); |
c5e438ec |
922 | if (rwx & 4) |
0ff9ea38 |
923 | FD_SET_MAX(fd, maxfd, xset); |
c5e438ec |
924 | } |
925 | |
5673d44e |
926 | do { |
39934deb |
927 | long next, ticks; |
928 | struct timeval tv, *ptv; |
929 | |
930 | if (run_timers(now, &next)) { |
931 | ticks = next - GETTICKCOUNT(); |
932 | if (ticks < 0) ticks = 0; /* just in case */ |
933 | tv.tv_sec = ticks / 1000; |
934 | tv.tv_usec = ticks % 1000 * 1000; |
935 | ptv = &tv; |
936 | } else { |
937 | ptv = NULL; |
938 | } |
939 | ret = select(maxfd, &rset, &wset, &xset, ptv); |
940 | if (ret == 0) |
941 | now = next; |
2ac3322e |
942 | else { |
943 | long newnow = GETTICKCOUNT(); |
944 | /* |
945 | * Check to see whether the system clock has |
946 | * changed massively during the select. |
947 | */ |
948 | if (newnow - now < 0 || newnow - now > next - now) { |
949 | /* |
950 | * If so, look at the elapsed time in the |
951 | * select and use it to compute a new |
952 | * tickcount_offset. |
953 | */ |
954 | long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000; |
955 | /* So we'd like GETTICKCOUNT to have returned othernow, |
956 | * but instead it return newnow. Hence ... */ |
957 | tickcount_offset += othernow - newnow; |
958 | now = othernow; |
959 | } else { |
960 | now = newnow; |
961 | } |
962 | } |
5673d44e |
963 | } while (ret < 0 && errno == EINTR); |
c5e438ec |
964 | |
965 | if (ret < 0) { |
966 | perror("select"); |
967 | exit(1); |
968 | } |
969 | |
0ff9ea38 |
970 | for (i = 0; i < fdcount; i++) { |
971 | fd = fdlist[i]; |
56e5b2db |
972 | /* |
973 | * We must process exceptional notifications before |
974 | * ordinary readability ones, or we may go straight |
975 | * past the urgent marker. |
976 | */ |
0ff9ea38 |
977 | if (FD_ISSET(fd, &xset)) |
978 | select_result(fd, 4); |
979 | if (FD_ISSET(fd, &rset)) |
980 | select_result(fd, 1); |
981 | if (FD_ISSET(fd, &wset)) |
982 | select_result(fd, 2); |
c5e438ec |
983 | } |
984 | |
5673d44e |
985 | if (FD_ISSET(signalpipe[0], &rset)) { |
986 | char c[1]; |
987 | struct winsize size; |
988 | read(signalpipe[0], c, 1); /* ignore its value; it'll be `x' */ |
989 | if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0) |
990 | back->size(backhandle, size.ws_col, size.ws_row); |
991 | } |
992 | |
c5e438ec |
993 | if (FD_ISSET(0, &rset)) { |
994 | char buf[4096]; |
995 | int ret; |
996 | |
997 | if (connopen && back->socket(backhandle) != NULL) { |
998 | ret = read(0, buf, sizeof(buf)); |
999 | if (ret < 0) { |
1000 | perror("stdin: read"); |
1001 | exit(1); |
1002 | } else if (ret == 0) { |
1003 | back->special(backhandle, TS_EOF); |
1004 | sending = FALSE; /* send nothing further after this */ |
1005 | } else { |
d0cb8c0c |
1006 | if (local_tty) |
1007 | from_tty(buf, ret); |
1008 | else |
1009 | back->send(backhandle, buf, ret); |
c5e438ec |
1010 | } |
1011 | } |
1012 | } |
1013 | |
1014 | if (FD_ISSET(1, &wset)) { |
1015 | try_output(0); |
1016 | } |
1017 | |
1018 | if (FD_ISSET(2, &wset)) { |
1019 | try_output(1); |
1020 | } |
1021 | |
1022 | if ((!connopen || back->socket(backhandle) == NULL) && |
1023 | bufchain_size(&stdout_data) == 0 && |
1024 | bufchain_size(&stderr_data) == 0) |
1025 | break; /* we closed the connection */ |
1026 | } |
1027 | exitcode = back->exitcode(backhandle); |
1028 | if (exitcode < 0) { |
1029 | fprintf(stderr, "Remote process exit code unavailable\n"); |
1030 | exitcode = 1; /* this is an error condition */ |
1031 | } |
d9c40fd6 |
1032 | cleanup_exit(exitcode); |
1033 | return exitcode; /* shouldn't happen, but placates gcc */ |
c5e438ec |
1034 | } |