| 1 | /* |
| 2 | * PLink - a Windows command-line (stdin/stdout) variant of PuTTY. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <assert.h> |
| 8 | #include <stdarg.h> |
| 9 | |
| 10 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
| 11 | #include "putty.h" |
| 12 | #include "storage.h" |
| 13 | #include "tree234.h" |
| 14 | |
| 15 | #define WM_AGENT_CALLBACK (WM_APP + 4) |
| 16 | |
| 17 | struct agent_callback { |
| 18 | void (*callback)(void *, void *, int); |
| 19 | void *callback_ctx; |
| 20 | void *data; |
| 21 | int len; |
| 22 | }; |
| 23 | |
| 24 | void fatalbox(char *p, ...) |
| 25 | { |
| 26 | va_list ap; |
| 27 | fprintf(stderr, "FATAL ERROR: "); |
| 28 | va_start(ap, p); |
| 29 | vfprintf(stderr, p, ap); |
| 30 | va_end(ap); |
| 31 | fputc('\n', stderr); |
| 32 | if (logctx) { |
| 33 | log_free(logctx); |
| 34 | logctx = NULL; |
| 35 | } |
| 36 | cleanup_exit(1); |
| 37 | } |
| 38 | void modalfatalbox(char *p, ...) |
| 39 | { |
| 40 | va_list ap; |
| 41 | fprintf(stderr, "FATAL ERROR: "); |
| 42 | va_start(ap, p); |
| 43 | vfprintf(stderr, p, ap); |
| 44 | va_end(ap); |
| 45 | fputc('\n', stderr); |
| 46 | if (logctx) { |
| 47 | log_free(logctx); |
| 48 | logctx = NULL; |
| 49 | } |
| 50 | cleanup_exit(1); |
| 51 | } |
| 52 | void connection_fatal(void *frontend, char *p, ...) |
| 53 | { |
| 54 | va_list ap; |
| 55 | fprintf(stderr, "FATAL ERROR: "); |
| 56 | va_start(ap, p); |
| 57 | vfprintf(stderr, p, ap); |
| 58 | va_end(ap); |
| 59 | fputc('\n', stderr); |
| 60 | if (logctx) { |
| 61 | log_free(logctx); |
| 62 | logctx = NULL; |
| 63 | } |
| 64 | cleanup_exit(1); |
| 65 | } |
| 66 | void cmdline_error(char *p, ...) |
| 67 | { |
| 68 | va_list ap; |
| 69 | fprintf(stderr, "plink: "); |
| 70 | va_start(ap, p); |
| 71 | vfprintf(stderr, p, ap); |
| 72 | va_end(ap); |
| 73 | fputc('\n', stderr); |
| 74 | exit(1); |
| 75 | } |
| 76 | |
| 77 | HANDLE inhandle, outhandle, errhandle; |
| 78 | struct handle *stdin_handle, *stdout_handle, *stderr_handle; |
| 79 | DWORD orig_console_mode; |
| 80 | int connopen; |
| 81 | |
| 82 | WSAEVENT netevent; |
| 83 | |
| 84 | static Backend *back; |
| 85 | static void *backhandle; |
| 86 | static Conf *conf; |
| 87 | |
| 88 | int term_ldisc(Terminal *term, int mode) |
| 89 | { |
| 90 | return FALSE; |
| 91 | } |
| 92 | void ldisc_update(void *frontend, int echo, int edit) |
| 93 | { |
| 94 | /* Update stdin read mode to reflect changes in line discipline. */ |
| 95 | DWORD mode; |
| 96 | |
| 97 | mode = ENABLE_PROCESSED_INPUT; |
| 98 | if (echo) |
| 99 | mode = mode | ENABLE_ECHO_INPUT; |
| 100 | else |
| 101 | mode = mode & ~ENABLE_ECHO_INPUT; |
| 102 | if (edit) |
| 103 | mode = mode | ENABLE_LINE_INPUT; |
| 104 | else |
| 105 | mode = mode & ~ENABLE_LINE_INPUT; |
| 106 | SetConsoleMode(inhandle, mode); |
| 107 | } |
| 108 | |
| 109 | char *get_ttymode(void *frontend, const char *mode) { return NULL; } |
| 110 | |
| 111 | int from_backend(void *frontend_handle, int is_stderr, |
| 112 | const char *data, int len) |
| 113 | { |
| 114 | if (is_stderr) { |
| 115 | handle_write(stderr_handle, data, len); |
| 116 | } else { |
| 117 | handle_write(stdout_handle, data, len); |
| 118 | } |
| 119 | |
| 120 | return handle_backlog(stdout_handle) + handle_backlog(stderr_handle); |
| 121 | } |
| 122 | |
| 123 | int from_backend_untrusted(void *frontend_handle, const char *data, int len) |
| 124 | { |
| 125 | /* |
| 126 | * No "untrusted" output should get here (the way the code is |
| 127 | * currently, it's all diverted by FLAG_STDERR). |
| 128 | */ |
| 129 | assert(!"Unexpected call to from_backend_untrusted()"); |
| 130 | return 0; /* not reached */ |
| 131 | } |
| 132 | |
| 133 | int get_userpass_input(prompts_t *p, unsigned char *in, int inlen) |
| 134 | { |
| 135 | int ret; |
| 136 | ret = cmdline_get_passwd_input(p, in, inlen); |
| 137 | if (ret == -1) |
| 138 | ret = console_get_userpass_input(p, in, inlen); |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static DWORD main_thread_id; |
| 143 | |
| 144 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
| 145 | void *callback_ctx, void *data, int len) |
| 146 | { |
| 147 | struct agent_callback *c = snew(struct agent_callback); |
| 148 | c->callback = callback; |
| 149 | c->callback_ctx = callback_ctx; |
| 150 | c->data = data; |
| 151 | c->len = len; |
| 152 | PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Short description of parameters. |
| 157 | */ |
| 158 | static void usage(void) |
| 159 | { |
| 160 | printf("PuTTY Link: command-line connection utility\n"); |
| 161 | printf("%s\n", ver); |
| 162 | printf("Usage: plink [options] [user@]host [command]\n"); |
| 163 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
| 164 | printf("Options:\n"); |
| 165 | printf(" -V print version information and exit\n"); |
| 166 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
| 167 | printf(" -v show verbose messages\n"); |
| 168 | printf(" -load sessname Load settings from saved session\n"); |
| 169 | printf(" -ssh -telnet -rlogin -raw -serial\n"); |
| 170 | printf(" force use of a particular protocol\n"); |
| 171 | printf(" -P port connect to specified port\n"); |
| 172 | printf(" -l user connect with specified username\n"); |
| 173 | printf(" -batch disable all interactive prompts\n"); |
| 174 | printf("The following options only apply to SSH connections:\n"); |
| 175 | printf(" -pw passw login with specified password\n"); |
| 176 | printf(" -D [listen-IP:]listen-port\n"); |
| 177 | printf(" Dynamic SOCKS-based port forwarding\n"); |
| 178 | printf(" -L [listen-IP:]listen-port:host:port\n"); |
| 179 | printf(" Forward local port to remote address\n"); |
| 180 | printf(" -R [listen-IP:]listen-port:host:port\n"); |
| 181 | printf(" Forward remote port to local address\n"); |
| 182 | printf(" -X -x enable / disable X11 forwarding\n"); |
| 183 | printf(" -A -a enable / disable agent forwarding\n"); |
| 184 | printf(" -t -T enable / disable pty allocation\n"); |
| 185 | printf(" -1 -2 force use of particular protocol version\n"); |
| 186 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
| 187 | printf(" -C enable compression\n"); |
| 188 | printf(" -i key private key file for authentication\n"); |
| 189 | printf(" -noagent disable use of Pageant\n"); |
| 190 | printf(" -agent enable use of Pageant\n"); |
| 191 | printf(" -m file read remote command(s) from file\n"); |
| 192 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
| 193 | printf(" -N don't start a shell/command (SSH-2 only)\n"); |
| 194 | printf(" -nc host:port\n"); |
| 195 | printf(" open tunnel in place of session (SSH-2 only)\n"); |
| 196 | printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n"); |
| 197 | printf(" Specify the serial configuration (serial only)\n"); |
| 198 | exit(1); |
| 199 | } |
| 200 | |
| 201 | static void version(void) |
| 202 | { |
| 203 | printf("plink: %s\n", ver); |
| 204 | exit(1); |
| 205 | } |
| 206 | |
| 207 | char *do_select(SOCKET skt, int startup) |
| 208 | { |
| 209 | int events; |
| 210 | if (startup) { |
| 211 | events = (FD_CONNECT | FD_READ | FD_WRITE | |
| 212 | FD_OOB | FD_CLOSE | FD_ACCEPT); |
| 213 | } else { |
| 214 | events = 0; |
| 215 | } |
| 216 | if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { |
| 217 | switch (p_WSAGetLastError()) { |
| 218 | case WSAENETDOWN: |
| 219 | return "Network is down"; |
| 220 | default: |
| 221 | return "WSAEventSelect(): unknown error"; |
| 222 | } |
| 223 | } |
| 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | int stdin_gotdata(struct handle *h, void *data, int len) |
| 228 | { |
| 229 | if (len < 0) { |
| 230 | /* |
| 231 | * Special case: report read error. |
| 232 | */ |
| 233 | char buf[4096]; |
| 234 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -len, 0, |
| 235 | buf, lenof(buf), NULL); |
| 236 | buf[lenof(buf)-1] = '\0'; |
| 237 | if (buf[strlen(buf)-1] == '\n') |
| 238 | buf[strlen(buf)-1] = '\0'; |
| 239 | fprintf(stderr, "Unable to read from standard input: %s\n", buf); |
| 240 | cleanup_exit(0); |
| 241 | } |
| 242 | noise_ultralight(len); |
| 243 | if (connopen && back->connected(backhandle)) { |
| 244 | if (len > 0) { |
| 245 | return back->send(backhandle, data, len); |
| 246 | } else { |
| 247 | back->special(backhandle, TS_EOF); |
| 248 | return 0; |
| 249 | } |
| 250 | } else |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | void stdouterr_sent(struct handle *h, int new_backlog) |
| 255 | { |
| 256 | if (new_backlog < 0) { |
| 257 | /* |
| 258 | * Special case: report write error. |
| 259 | */ |
| 260 | char buf[4096]; |
| 261 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -new_backlog, 0, |
| 262 | buf, lenof(buf), NULL); |
| 263 | buf[lenof(buf)-1] = '\0'; |
| 264 | if (buf[strlen(buf)-1] == '\n') |
| 265 | buf[strlen(buf)-1] = '\0'; |
| 266 | fprintf(stderr, "Unable to write to standard %s: %s\n", |
| 267 | (h == stdout_handle ? "output" : "error"), buf); |
| 268 | cleanup_exit(0); |
| 269 | } |
| 270 | if (connopen && back->connected(backhandle)) { |
| 271 | back->unthrottle(backhandle, (handle_backlog(stdout_handle) + |
| 272 | handle_backlog(stderr_handle))); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | int main(int argc, char **argv) |
| 277 | { |
| 278 | int sending; |
| 279 | int portnumber = -1; |
| 280 | SOCKET *sklist; |
| 281 | int skcount, sksize; |
| 282 | int exitcode; |
| 283 | int errors; |
| 284 | int got_host = FALSE; |
| 285 | int use_subsystem = 0; |
| 286 | long now, next; |
| 287 | |
| 288 | sklist = NULL; |
| 289 | skcount = sksize = 0; |
| 290 | /* |
| 291 | * Initialise port and protocol to sensible defaults. (These |
| 292 | * will be overridden by more or less anything.) |
| 293 | */ |
| 294 | default_protocol = PROT_SSH; |
| 295 | default_port = 22; |
| 296 | |
| 297 | flags = FLAG_STDERR; |
| 298 | /* |
| 299 | * Process the command line. |
| 300 | */ |
| 301 | conf = conf_new(); |
| 302 | do_defaults(NULL, conf); |
| 303 | loaded_session = FALSE; |
| 304 | default_protocol = conf_get_int(conf, CONF_protocol); |
| 305 | default_port = conf_get_int(conf, CONF_port); |
| 306 | errors = 0; |
| 307 | { |
| 308 | /* |
| 309 | * Override the default protocol if PLINK_PROTOCOL is set. |
| 310 | */ |
| 311 | char *p = getenv("PLINK_PROTOCOL"); |
| 312 | if (p) { |
| 313 | const Backend *b = backend_from_name(p); |
| 314 | if (b) { |
| 315 | default_protocol = b->protocol; |
| 316 | default_port = b->default_port; |
| 317 | conf_set_int(conf, CONF_protocol, default_protocol); |
| 318 | conf_set_int(conf, CONF_port, default_port); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | while (--argc) { |
| 323 | char *p = *++argv; |
| 324 | if (*p == '-') { |
| 325 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
| 326 | 1, conf); |
| 327 | if (ret == -2) { |
| 328 | fprintf(stderr, |
| 329 | "plink: option \"%s\" requires an argument\n", p); |
| 330 | errors = 1; |
| 331 | } else if (ret == 2) { |
| 332 | --argc, ++argv; |
| 333 | } else if (ret == 1) { |
| 334 | continue; |
| 335 | } else if (!strcmp(p, "-batch")) { |
| 336 | console_batch_mode = 1; |
| 337 | } else if (!strcmp(p, "-s")) { |
| 338 | /* Save status to write to conf later. */ |
| 339 | use_subsystem = 1; |
| 340 | } else if (!strcmp(p, "-V")) { |
| 341 | version(); |
| 342 | } else if (!strcmp(p, "-pgpfp")) { |
| 343 | pgp_fingerprints(); |
| 344 | exit(1); |
| 345 | } else { |
| 346 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
| 347 | errors = 1; |
| 348 | } |
| 349 | } else if (*p) { |
| 350 | if (!conf_launchable(conf) || !(got_host || loaded_session)) { |
| 351 | char *q = p; |
| 352 | /* |
| 353 | * If the hostname starts with "telnet:", set the |
| 354 | * protocol to Telnet and process the string as a |
| 355 | * Telnet URL. |
| 356 | */ |
| 357 | if (!strncmp(q, "telnet:", 7)) { |
| 358 | char c; |
| 359 | |
| 360 | q += 7; |
| 361 | if (q[0] == '/' && q[1] == '/') |
| 362 | q += 2; |
| 363 | conf_set_int(conf, CONF_protocol, PROT_TELNET); |
| 364 | p = q; |
| 365 | while (*p && *p != ':' && *p != '/') |
| 366 | p++; |
| 367 | c = *p; |
| 368 | if (*p) |
| 369 | *p++ = '\0'; |
| 370 | if (c == ':') |
| 371 | conf_set_int(conf, CONF_port, atoi(p)); |
| 372 | else |
| 373 | conf_set_int(conf, CONF_port, -1); |
| 374 | conf_set_str(conf, CONF_host, q); |
| 375 | got_host = TRUE; |
| 376 | } else { |
| 377 | char *r, *user, *host; |
| 378 | /* |
| 379 | * Before we process the [user@]host string, we |
| 380 | * first check for the presence of a protocol |
| 381 | * prefix (a protocol name followed by ","). |
| 382 | */ |
| 383 | r = strchr(p, ','); |
| 384 | if (r) { |
| 385 | const Backend *b; |
| 386 | *r = '\0'; |
| 387 | b = backend_from_name(p); |
| 388 | if (b) { |
| 389 | default_protocol = b->protocol; |
| 390 | conf_set_int(conf, CONF_protocol, |
| 391 | default_protocol); |
| 392 | portnumber = b->default_port; |
| 393 | } |
| 394 | p = r + 1; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * A nonzero length string followed by an @ is treated |
| 399 | * as a username. (We discount an _initial_ @.) The |
| 400 | * rest of the string (or the whole string if no @) |
| 401 | * is treated as a session name and/or hostname. |
| 402 | */ |
| 403 | r = strrchr(p, '@'); |
| 404 | if (r == p) |
| 405 | p++, r = NULL; /* discount initial @ */ |
| 406 | if (r) { |
| 407 | *r++ = '\0'; |
| 408 | user = p, host = r; |
| 409 | } else { |
| 410 | user = NULL, host = p; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Now attempt to load a saved session with the |
| 415 | * same name as the hostname. |
| 416 | */ |
| 417 | { |
| 418 | Conf *conf2 = conf_new(); |
| 419 | do_defaults(host, conf2); |
| 420 | if (loaded_session || !conf_launchable(conf2)) { |
| 421 | /* No settings for this host; use defaults */ |
| 422 | /* (or session was already loaded with -load) */ |
| 423 | conf_set_str(conf, CONF_host, host); |
| 424 | conf_set_int(conf, CONF_port, default_port); |
| 425 | got_host = TRUE; |
| 426 | } else { |
| 427 | conf_copy_into(conf, conf2); |
| 428 | loaded_session = TRUE; |
| 429 | } |
| 430 | conf_free(conf2); |
| 431 | } |
| 432 | |
| 433 | if (user) { |
| 434 | /* Patch in specified username. */ |
| 435 | conf_set_str(conf, CONF_username, user); |
| 436 | } |
| 437 | |
| 438 | } |
| 439 | } else { |
| 440 | char *command; |
| 441 | int cmdlen, cmdsize; |
| 442 | cmdlen = cmdsize = 0; |
| 443 | command = NULL; |
| 444 | |
| 445 | while (argc) { |
| 446 | while (*p) { |
| 447 | if (cmdlen >= cmdsize) { |
| 448 | cmdsize = cmdlen + 512; |
| 449 | command = sresize(command, cmdsize, char); |
| 450 | } |
| 451 | command[cmdlen++]=*p++; |
| 452 | } |
| 453 | if (cmdlen >= cmdsize) { |
| 454 | cmdsize = cmdlen + 512; |
| 455 | command = sresize(command, cmdsize, char); |
| 456 | } |
| 457 | command[cmdlen++]=' '; /* always add trailing space */ |
| 458 | if (--argc) p = *++argv; |
| 459 | } |
| 460 | if (cmdlen) command[--cmdlen]='\0'; |
| 461 | /* change trailing blank to NUL */ |
| 462 | conf_set_str(conf, CONF_remote_cmd, command); |
| 463 | conf_set_str(conf, CONF_remote_cmd2, ""); |
| 464 | conf_set_int(conf, CONF_nopty, TRUE); /* command => no tty */ |
| 465 | |
| 466 | break; /* done with cmdline */ |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (errors) |
| 472 | return 1; |
| 473 | |
| 474 | if (!conf_launchable(conf) || !(got_host || loaded_session)) { |
| 475 | usage(); |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Muck about with the hostname in various ways. |
| 480 | */ |
| 481 | { |
| 482 | char *hostbuf = dupstr(conf_get_str(conf, CONF_host)); |
| 483 | char *host = hostbuf; |
| 484 | char *p, *q; |
| 485 | |
| 486 | /* |
| 487 | * Trim leading whitespace. |
| 488 | */ |
| 489 | host += strspn(host, " \t"); |
| 490 | |
| 491 | /* |
| 492 | * See if host is of the form user@host, and separate out |
| 493 | * the username if so. |
| 494 | */ |
| 495 | if (host[0] != '\0') { |
| 496 | char *atsign = strrchr(host, '@'); |
| 497 | if (atsign) { |
| 498 | *atsign = '\0'; |
| 499 | conf_set_str(conf, CONF_username, host); |
| 500 | host = atsign + 1; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Trim off a colon suffix if it's there. |
| 506 | */ |
| 507 | host[strcspn(host, ":")] = '\0'; |
| 508 | |
| 509 | /* |
| 510 | * Remove any remaining whitespace. |
| 511 | */ |
| 512 | p = hostbuf; |
| 513 | q = host; |
| 514 | while (*q) { |
| 515 | if (*q != ' ' && *q != '\t') |
| 516 | *p++ = *q; |
| 517 | q++; |
| 518 | } |
| 519 | *p = '\0'; |
| 520 | |
| 521 | conf_set_str(conf, CONF_host, hostbuf); |
| 522 | sfree(hostbuf); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Perform command-line overrides on session configuration. |
| 527 | */ |
| 528 | cmdline_run_saved(conf); |
| 529 | |
| 530 | /* |
| 531 | * Apply subsystem status. |
| 532 | */ |
| 533 | if (use_subsystem) |
| 534 | conf_set_int(conf, CONF_ssh_subsys, TRUE); |
| 535 | |
| 536 | if (!*conf_get_str(conf, CONF_remote_cmd) && |
| 537 | !*conf_get_str(conf, CONF_remote_cmd2) && |
| 538 | !*conf_get_str(conf, CONF_ssh_nc_host)) |
| 539 | flags |= FLAG_INTERACTIVE; |
| 540 | |
| 541 | /* |
| 542 | * Select protocol. This is farmed out into a table in a |
| 543 | * separate file to enable an ssh-free variant. |
| 544 | */ |
| 545 | back = backend_from_proto(conf_get_int(conf, CONF_protocol)); |
| 546 | if (back == NULL) { |
| 547 | fprintf(stderr, |
| 548 | "Internal fault: Unsupported protocol found\n"); |
| 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Select port. |
| 554 | */ |
| 555 | if (portnumber != -1) |
| 556 | conf_set_int(conf, CONF_port, portnumber); |
| 557 | |
| 558 | sk_init(); |
| 559 | if (p_WSAEventSelect == NULL) { |
| 560 | fprintf(stderr, "Plink requires WinSock 2\n"); |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | logctx = log_init(NULL, conf); |
| 565 | console_provide_logctx(logctx); |
| 566 | |
| 567 | /* |
| 568 | * Start up the connection. |
| 569 | */ |
| 570 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 571 | { |
| 572 | const char *error; |
| 573 | char *realhost; |
| 574 | /* nodelay is only useful if stdin is a character device (console) */ |
| 575 | int nodelay = conf_get_int(conf, CONF_tcp_nodelay) && |
| 576 | (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR); |
| 577 | |
| 578 | error = back->init(NULL, &backhandle, conf, |
| 579 | conf_get_str(conf, CONF_host), |
| 580 | conf_get_int(conf, CONF_port), |
| 581 | &realhost, nodelay, |
| 582 | conf_get_int(conf, CONF_tcp_keepalives)); |
| 583 | if (error) { |
| 584 | fprintf(stderr, "Unable to open connection:\n%s", error); |
| 585 | return 1; |
| 586 | } |
| 587 | back->provide_logctx(backhandle, logctx); |
| 588 | sfree(realhost); |
| 589 | } |
| 590 | connopen = 1; |
| 591 | |
| 592 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
| 593 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 594 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
| 595 | |
| 596 | /* |
| 597 | * Turn off ECHO and LINE input modes. We don't care if this |
| 598 | * call fails, because we know we aren't necessarily running in |
| 599 | * a console. |
| 600 | */ |
| 601 | GetConsoleMode(inhandle, &orig_console_mode); |
| 602 | SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT); |
| 603 | |
| 604 | /* |
| 605 | * Pass the output handles to the handle-handling subsystem. |
| 606 | * (The input one we leave until we're through the |
| 607 | * authentication process.) |
| 608 | */ |
| 609 | stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL, 0); |
| 610 | stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL, 0); |
| 611 | |
| 612 | main_thread_id = GetCurrentThreadId(); |
| 613 | |
| 614 | sending = FALSE; |
| 615 | |
| 616 | now = GETTICKCOUNT(); |
| 617 | |
| 618 | while (1) { |
| 619 | int nhandles; |
| 620 | HANDLE *handles; |
| 621 | int n; |
| 622 | DWORD ticks; |
| 623 | |
| 624 | if (!sending && back->sendok(backhandle)) { |
| 625 | stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL, |
| 626 | 0); |
| 627 | sending = TRUE; |
| 628 | } |
| 629 | |
| 630 | if (run_timers(now, &next)) { |
| 631 | ticks = next - GETTICKCOUNT(); |
| 632 | if (ticks < 0) ticks = 0; /* just in case */ |
| 633 | } else { |
| 634 | ticks = INFINITE; |
| 635 | } |
| 636 | |
| 637 | handles = handle_get_events(&nhandles); |
| 638 | handles = sresize(handles, nhandles+1, HANDLE); |
| 639 | handles[nhandles] = netevent; |
| 640 | n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks, |
| 641 | QS_POSTMESSAGE); |
| 642 | if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) { |
| 643 | handle_got_event(handles[n - WAIT_OBJECT_0]); |
| 644 | } else if (n == WAIT_OBJECT_0 + nhandles) { |
| 645 | WSANETWORKEVENTS things; |
| 646 | SOCKET socket; |
| 647 | extern SOCKET first_socket(int *), next_socket(int *); |
| 648 | extern int select_result(WPARAM, LPARAM); |
| 649 | int i, socketstate; |
| 650 | |
| 651 | /* |
| 652 | * We must not call select_result() for any socket |
| 653 | * until we have finished enumerating within the tree. |
| 654 | * This is because select_result() may close the socket |
| 655 | * and modify the tree. |
| 656 | */ |
| 657 | /* Count the active sockets. */ |
| 658 | i = 0; |
| 659 | for (socket = first_socket(&socketstate); |
| 660 | socket != INVALID_SOCKET; |
| 661 | socket = next_socket(&socketstate)) i++; |
| 662 | |
| 663 | /* Expand the buffer if necessary. */ |
| 664 | if (i > sksize) { |
| 665 | sksize = i + 16; |
| 666 | sklist = sresize(sklist, sksize, SOCKET); |
| 667 | } |
| 668 | |
| 669 | /* Retrieve the sockets into sklist. */ |
| 670 | skcount = 0; |
| 671 | for (socket = first_socket(&socketstate); |
| 672 | socket != INVALID_SOCKET; |
| 673 | socket = next_socket(&socketstate)) { |
| 674 | sklist[skcount++] = socket; |
| 675 | } |
| 676 | |
| 677 | /* Now we're done enumerating; go through the list. */ |
| 678 | for (i = 0; i < skcount; i++) { |
| 679 | WPARAM wp; |
| 680 | socket = sklist[i]; |
| 681 | wp = (WPARAM) socket; |
| 682 | if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) { |
| 683 | static const struct { int bit, mask; } eventtypes[] = { |
| 684 | {FD_CONNECT_BIT, FD_CONNECT}, |
| 685 | {FD_READ_BIT, FD_READ}, |
| 686 | {FD_CLOSE_BIT, FD_CLOSE}, |
| 687 | {FD_OOB_BIT, FD_OOB}, |
| 688 | {FD_WRITE_BIT, FD_WRITE}, |
| 689 | {FD_ACCEPT_BIT, FD_ACCEPT}, |
| 690 | }; |
| 691 | int e; |
| 692 | |
| 693 | noise_ultralight(socket); |
| 694 | noise_ultralight(things.lNetworkEvents); |
| 695 | |
| 696 | for (e = 0; e < lenof(eventtypes); e++) |
| 697 | if (things.lNetworkEvents & eventtypes[e].mask) { |
| 698 | LPARAM lp; |
| 699 | int err = things.iErrorCode[eventtypes[e].bit]; |
| 700 | lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err); |
| 701 | connopen &= select_result(wp, lp); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | } else if (n == WAIT_OBJECT_0 + nhandles + 1) { |
| 706 | MSG msg; |
| 707 | while (PeekMessage(&msg, INVALID_HANDLE_VALUE, |
| 708 | WM_AGENT_CALLBACK, WM_AGENT_CALLBACK, |
| 709 | PM_REMOVE)) { |
| 710 | struct agent_callback *c = (struct agent_callback *)msg.lParam; |
| 711 | c->callback(c->callback_ctx, c->data, c->len); |
| 712 | sfree(c); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (n == WAIT_TIMEOUT) { |
| 717 | now = next; |
| 718 | } else { |
| 719 | now = GETTICKCOUNT(); |
| 720 | } |
| 721 | |
| 722 | sfree(handles); |
| 723 | |
| 724 | if (sending) |
| 725 | handle_unthrottle(stdin_handle, back->sendbuffer(backhandle)); |
| 726 | |
| 727 | if ((!connopen || !back->connected(backhandle)) && |
| 728 | handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0) |
| 729 | break; /* we closed the connection */ |
| 730 | } |
| 731 | exitcode = back->exitcode(backhandle); |
| 732 | if (exitcode < 0) { |
| 733 | fprintf(stderr, "Remote process exit code unavailable\n"); |
| 734 | exitcode = 1; /* this is an error condition */ |
| 735 | } |
| 736 | cleanup_exit(exitcode); |
| 737 | return 0; /* placate compiler warning */ |
| 738 | } |