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