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