| 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 Config cfg; |
| 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\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 | exit(1); |
| 197 | } |
| 198 | |
| 199 | static void version(void) |
| 200 | { |
| 201 | printf("plink: %s\n", ver); |
| 202 | exit(1); |
| 203 | } |
| 204 | |
| 205 | char *do_select(SOCKET skt, int startup) |
| 206 | { |
| 207 | int events; |
| 208 | if (startup) { |
| 209 | events = (FD_CONNECT | FD_READ | FD_WRITE | |
| 210 | FD_OOB | FD_CLOSE | FD_ACCEPT); |
| 211 | } else { |
| 212 | events = 0; |
| 213 | } |
| 214 | if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { |
| 215 | switch (p_WSAGetLastError()) { |
| 216 | case WSAENETDOWN: |
| 217 | return "Network is down"; |
| 218 | default: |
| 219 | return "WSAEventSelect(): unknown error"; |
| 220 | } |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | int stdin_gotdata(struct handle *h, void *data, int len) |
| 226 | { |
| 227 | if (len < 0) { |
| 228 | /* |
| 229 | * Special case: report read error. |
| 230 | */ |
| 231 | char buf[4096]; |
| 232 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -len, 0, |
| 233 | buf, lenof(buf), NULL); |
| 234 | buf[lenof(buf)-1] = '\0'; |
| 235 | if (buf[strlen(buf)-1] == '\n') |
| 236 | buf[strlen(buf)-1] = '\0'; |
| 237 | fprintf(stderr, "Unable to read from standard input: %s\n", buf); |
| 238 | cleanup_exit(0); |
| 239 | } |
| 240 | noise_ultralight(len); |
| 241 | if (connopen && back->connected(backhandle)) { |
| 242 | if (len > 0) { |
| 243 | return back->send(backhandle, data, len); |
| 244 | } else { |
| 245 | back->special(backhandle, TS_EOF); |
| 246 | return 0; |
| 247 | } |
| 248 | } else |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | void stdouterr_sent(struct handle *h, int new_backlog) |
| 253 | { |
| 254 | if (new_backlog < 0) { |
| 255 | /* |
| 256 | * Special case: report write error. |
| 257 | */ |
| 258 | char buf[4096]; |
| 259 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -new_backlog, 0, |
| 260 | buf, lenof(buf), NULL); |
| 261 | buf[lenof(buf)-1] = '\0'; |
| 262 | if (buf[strlen(buf)-1] == '\n') |
| 263 | buf[strlen(buf)-1] = '\0'; |
| 264 | fprintf(stderr, "Unable to write to standard %s: %s\n", |
| 265 | (h == stdout_handle ? "output" : "error"), buf); |
| 266 | cleanup_exit(0); |
| 267 | } |
| 268 | if (connopen && back->connected(backhandle)) { |
| 269 | back->unthrottle(backhandle, (handle_backlog(stdout_handle) + |
| 270 | handle_backlog(stderr_handle))); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | int main(int argc, char **argv) |
| 275 | { |
| 276 | int sending; |
| 277 | int portnumber = -1; |
| 278 | SOCKET *sklist; |
| 279 | int skcount, sksize; |
| 280 | int exitcode; |
| 281 | int errors; |
| 282 | int use_subsystem = 0; |
| 283 | long now, next; |
| 284 | |
| 285 | sklist = NULL; |
| 286 | skcount = sksize = 0; |
| 287 | /* |
| 288 | * Initialise port and protocol to sensible defaults. (These |
| 289 | * will be overridden by more or less anything.) |
| 290 | */ |
| 291 | default_protocol = PROT_SSH; |
| 292 | default_port = 22; |
| 293 | |
| 294 | flags = FLAG_STDERR; |
| 295 | /* |
| 296 | * Process the command line. |
| 297 | */ |
| 298 | do_defaults(NULL, &cfg); |
| 299 | loaded_session = FALSE; |
| 300 | default_protocol = cfg.protocol; |
| 301 | default_port = cfg.port; |
| 302 | errors = 0; |
| 303 | { |
| 304 | /* |
| 305 | * Override the default protocol if PLINK_PROTOCOL is set. |
| 306 | */ |
| 307 | char *p = getenv("PLINK_PROTOCOL"); |
| 308 | int i; |
| 309 | if (p) { |
| 310 | const Backend *b = backend_from_name(p); |
| 311 | if (b) { |
| 312 | default_protocol = cfg.protocol = b->protocol; |
| 313 | default_port = cfg.port = b->default_port; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | while (--argc) { |
| 318 | char *p = *++argv; |
| 319 | if (*p == '-') { |
| 320 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
| 321 | 1, &cfg); |
| 322 | if (ret == -2) { |
| 323 | fprintf(stderr, |
| 324 | "plink: option \"%s\" requires an argument\n", p); |
| 325 | errors = 1; |
| 326 | } else if (ret == 2) { |
| 327 | --argc, ++argv; |
| 328 | } else if (ret == 1) { |
| 329 | continue; |
| 330 | } else if (!strcmp(p, "-batch")) { |
| 331 | console_batch_mode = 1; |
| 332 | } else if (!strcmp(p, "-s")) { |
| 333 | /* Save status to write to cfg later. */ |
| 334 | use_subsystem = 1; |
| 335 | } else if (!strcmp(p, "-V")) { |
| 336 | version(); |
| 337 | } else if (!strcmp(p, "-pgpfp")) { |
| 338 | pgp_fingerprints(); |
| 339 | exit(1); |
| 340 | } else { |
| 341 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
| 342 | errors = 1; |
| 343 | } |
| 344 | } else if (*p) { |
| 345 | if (!cfg_launchable(&cfg)) { |
| 346 | char *q = p; |
| 347 | /* |
| 348 | * If the hostname starts with "telnet:", set the |
| 349 | * protocol to Telnet and process the string as a |
| 350 | * Telnet URL. |
| 351 | */ |
| 352 | if (!strncmp(q, "telnet:", 7)) { |
| 353 | char c; |
| 354 | |
| 355 | q += 7; |
| 356 | if (q[0] == '/' && q[1] == '/') |
| 357 | q += 2; |
| 358 | cfg.protocol = PROT_TELNET; |
| 359 | p = q; |
| 360 | while (*p && *p != ':' && *p != '/') |
| 361 | p++; |
| 362 | c = *p; |
| 363 | if (*p) |
| 364 | *p++ = '\0'; |
| 365 | if (c == ':') |
| 366 | cfg.port = atoi(p); |
| 367 | else |
| 368 | cfg.port = -1; |
| 369 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
| 370 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
| 371 | } else { |
| 372 | char *r, *user, *host; |
| 373 | /* |
| 374 | * Before we process the [user@]host string, we |
| 375 | * first check for the presence of a protocol |
| 376 | * prefix (a protocol name followed by ","). |
| 377 | */ |
| 378 | r = strchr(p, ','); |
| 379 | if (r) { |
| 380 | const Backend *b; |
| 381 | *r = '\0'; |
| 382 | b = backend_from_name(p); |
| 383 | if (b) { |
| 384 | default_protocol = cfg.protocol = b->protocol; |
| 385 | portnumber = b->default_port; |
| 386 | } |
| 387 | p = r + 1; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * A nonzero length string followed by an @ is treated |
| 392 | * as a username. (We discount an _initial_ @.) The |
| 393 | * rest of the string (or the whole string if no @) |
| 394 | * is treated as a session name and/or hostname. |
| 395 | */ |
| 396 | r = strrchr(p, '@'); |
| 397 | if (r == p) |
| 398 | p++, r = NULL; /* discount initial @ */ |
| 399 | if (r) { |
| 400 | *r++ = '\0'; |
| 401 | user = p, host = r; |
| 402 | } else { |
| 403 | user = NULL, host = p; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Now attempt to load a saved session with the |
| 408 | * same name as the hostname. |
| 409 | */ |
| 410 | { |
| 411 | Config cfg2; |
| 412 | do_defaults(host, &cfg2); |
| 413 | if (loaded_session || !cfg_launchable(&cfg2)) { |
| 414 | /* No settings for this host; use defaults */ |
| 415 | /* (or session was already loaded with -load) */ |
| 416 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
| 417 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
| 418 | cfg.port = default_port; |
| 419 | } else { |
| 420 | cfg = cfg2; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (user) { |
| 425 | /* Patch in specified username. */ |
| 426 | strncpy(cfg.username, user, |
| 427 | sizeof(cfg.username) - 1); |
| 428 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
| 429 | } |
| 430 | |
| 431 | } |
| 432 | } else { |
| 433 | char *command; |
| 434 | int cmdlen, cmdsize; |
| 435 | cmdlen = cmdsize = 0; |
| 436 | command = NULL; |
| 437 | |
| 438 | while (argc) { |
| 439 | while (*p) { |
| 440 | if (cmdlen >= cmdsize) { |
| 441 | cmdsize = cmdlen + 512; |
| 442 | command = sresize(command, cmdsize, char); |
| 443 | } |
| 444 | command[cmdlen++]=*p++; |
| 445 | } |
| 446 | if (cmdlen >= cmdsize) { |
| 447 | cmdsize = cmdlen + 512; |
| 448 | command = sresize(command, cmdsize, char); |
| 449 | } |
| 450 | command[cmdlen++]=' '; /* always add trailing space */ |
| 451 | if (--argc) p = *++argv; |
| 452 | } |
| 453 | if (cmdlen) command[--cmdlen]='\0'; |
| 454 | /* change trailing blank to NUL */ |
| 455 | cfg.remote_cmd_ptr = command; |
| 456 | cfg.remote_cmd_ptr2 = NULL; |
| 457 | cfg.nopty = TRUE; /* command => no terminal */ |
| 458 | |
| 459 | break; /* done with cmdline */ |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (errors) |
| 465 | return 1; |
| 466 | |
| 467 | if (!cfg_launchable(&cfg)) { |
| 468 | usage(); |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Trim leading whitespace off the hostname if it's there. |
| 473 | */ |
| 474 | { |
| 475 | int space = strspn(cfg.host, " \t"); |
| 476 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
| 477 | } |
| 478 | |
| 479 | /* See if host is of the form user@host */ |
| 480 | if (cfg_launchable(&cfg)) { |
| 481 | char *atsign = strrchr(cfg.host, '@'); |
| 482 | /* Make sure we're not overflowing the user field */ |
| 483 | if (atsign) { |
| 484 | if (atsign - cfg.host < sizeof cfg.username) { |
| 485 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
| 486 | cfg.username[atsign - cfg.host] = '\0'; |
| 487 | } |
| 488 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Perform command-line overrides on session configuration. |
| 494 | */ |
| 495 | cmdline_run_saved(&cfg); |
| 496 | |
| 497 | /* |
| 498 | * Apply subsystem status. |
| 499 | */ |
| 500 | if (use_subsystem) |
| 501 | cfg.ssh_subsys = TRUE; |
| 502 | |
| 503 | /* |
| 504 | * Trim a colon suffix off the hostname if it's there. |
| 505 | */ |
| 506 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
| 507 | |
| 508 | /* |
| 509 | * Remove any remaining whitespace from the hostname. |
| 510 | */ |
| 511 | { |
| 512 | int p1 = 0, p2 = 0; |
| 513 | while (cfg.host[p2] != '\0') { |
| 514 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
| 515 | cfg.host[p1] = cfg.host[p2]; |
| 516 | p1++; |
| 517 | } |
| 518 | p2++; |
| 519 | } |
| 520 | cfg.host[p1] = '\0'; |
| 521 | } |
| 522 | |
| 523 | if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd && !*cfg.ssh_nc_host) |
| 524 | flags |= FLAG_INTERACTIVE; |
| 525 | |
| 526 | /* |
| 527 | * Select protocol. This is farmed out into a table in a |
| 528 | * separate file to enable an ssh-free variant. |
| 529 | */ |
| 530 | back = backend_from_proto(cfg.protocol); |
| 531 | if (back == NULL) { |
| 532 | fprintf(stderr, |
| 533 | "Internal fault: Unsupported protocol found\n"); |
| 534 | return 1; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Select port. |
| 539 | */ |
| 540 | if (portnumber != -1) |
| 541 | cfg.port = portnumber; |
| 542 | |
| 543 | sk_init(); |
| 544 | if (p_WSAEventSelect == NULL) { |
| 545 | fprintf(stderr, "Plink requires WinSock 2\n"); |
| 546 | return 1; |
| 547 | } |
| 548 | |
| 549 | logctx = log_init(NULL, &cfg); |
| 550 | console_provide_logctx(logctx); |
| 551 | |
| 552 | /* |
| 553 | * Start up the connection. |
| 554 | */ |
| 555 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 556 | { |
| 557 | const char *error; |
| 558 | char *realhost; |
| 559 | /* nodelay is only useful if stdin is a character device (console) */ |
| 560 | int nodelay = cfg.tcp_nodelay && |
| 561 | (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR); |
| 562 | |
| 563 | error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, |
| 564 | &realhost, nodelay, cfg.tcp_keepalives); |
| 565 | if (error) { |
| 566 | fprintf(stderr, "Unable to open connection:\n%s", error); |
| 567 | return 1; |
| 568 | } |
| 569 | back->provide_logctx(backhandle, logctx); |
| 570 | sfree(realhost); |
| 571 | } |
| 572 | connopen = 1; |
| 573 | |
| 574 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
| 575 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 576 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
| 577 | |
| 578 | /* |
| 579 | * Turn off ECHO and LINE input modes. We don't care if this |
| 580 | * call fails, because we know we aren't necessarily running in |
| 581 | * a console. |
| 582 | */ |
| 583 | GetConsoleMode(inhandle, &orig_console_mode); |
| 584 | SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT); |
| 585 | |
| 586 | /* |
| 587 | * Pass the output handles to the handle-handling subsystem. |
| 588 | * (The input one we leave until we're through the |
| 589 | * authentication process.) |
| 590 | */ |
| 591 | stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL, 0); |
| 592 | stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL, 0); |
| 593 | |
| 594 | main_thread_id = GetCurrentThreadId(); |
| 595 | |
| 596 | sending = FALSE; |
| 597 | |
| 598 | now = GETTICKCOUNT(); |
| 599 | |
| 600 | while (1) { |
| 601 | int nhandles; |
| 602 | HANDLE *handles; |
| 603 | int n; |
| 604 | DWORD ticks; |
| 605 | |
| 606 | if (!sending && back->sendok(backhandle)) { |
| 607 | stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL, |
| 608 | 0); |
| 609 | sending = TRUE; |
| 610 | } |
| 611 | |
| 612 | if (run_timers(now, &next)) { |
| 613 | ticks = next - GETTICKCOUNT(); |
| 614 | if (ticks < 0) ticks = 0; /* just in case */ |
| 615 | } else { |
| 616 | ticks = INFINITE; |
| 617 | } |
| 618 | |
| 619 | handles = handle_get_events(&nhandles); |
| 620 | handles = sresize(handles, nhandles+1, HANDLE); |
| 621 | handles[nhandles] = netevent; |
| 622 | n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks, |
| 623 | QS_POSTMESSAGE); |
| 624 | if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) { |
| 625 | handle_got_event(handles[n - WAIT_OBJECT_0]); |
| 626 | } else if (n == WAIT_OBJECT_0 + nhandles) { |
| 627 | WSANETWORKEVENTS things; |
| 628 | SOCKET socket; |
| 629 | extern SOCKET first_socket(int *), next_socket(int *); |
| 630 | extern int select_result(WPARAM, LPARAM); |
| 631 | int i, socketstate; |
| 632 | |
| 633 | /* |
| 634 | * We must not call select_result() for any socket |
| 635 | * until we have finished enumerating within the tree. |
| 636 | * This is because select_result() may close the socket |
| 637 | * and modify the tree. |
| 638 | */ |
| 639 | /* Count the active sockets. */ |
| 640 | i = 0; |
| 641 | for (socket = first_socket(&socketstate); |
| 642 | socket != INVALID_SOCKET; |
| 643 | socket = next_socket(&socketstate)) i++; |
| 644 | |
| 645 | /* Expand the buffer if necessary. */ |
| 646 | if (i > sksize) { |
| 647 | sksize = i + 16; |
| 648 | sklist = sresize(sklist, sksize, SOCKET); |
| 649 | } |
| 650 | |
| 651 | /* Retrieve the sockets into sklist. */ |
| 652 | skcount = 0; |
| 653 | for (socket = first_socket(&socketstate); |
| 654 | socket != INVALID_SOCKET; |
| 655 | socket = next_socket(&socketstate)) { |
| 656 | sklist[skcount++] = socket; |
| 657 | } |
| 658 | |
| 659 | /* Now we're done enumerating; go through the list. */ |
| 660 | for (i = 0; i < skcount; i++) { |
| 661 | WPARAM wp; |
| 662 | socket = sklist[i]; |
| 663 | wp = (WPARAM) socket; |
| 664 | if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) { |
| 665 | static const struct { int bit, mask; } eventtypes[] = { |
| 666 | {FD_CONNECT_BIT, FD_CONNECT}, |
| 667 | {FD_READ_BIT, FD_READ}, |
| 668 | {FD_CLOSE_BIT, FD_CLOSE}, |
| 669 | {FD_OOB_BIT, FD_OOB}, |
| 670 | {FD_WRITE_BIT, FD_WRITE}, |
| 671 | {FD_ACCEPT_BIT, FD_ACCEPT}, |
| 672 | }; |
| 673 | int e; |
| 674 | |
| 675 | noise_ultralight(socket); |
| 676 | noise_ultralight(things.lNetworkEvents); |
| 677 | |
| 678 | for (e = 0; e < lenof(eventtypes); e++) |
| 679 | if (things.lNetworkEvents & eventtypes[e].mask) { |
| 680 | LPARAM lp; |
| 681 | int err = things.iErrorCode[eventtypes[e].bit]; |
| 682 | lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err); |
| 683 | connopen &= select_result(wp, lp); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } else if (n == WAIT_OBJECT_0 + nhandles + 1) { |
| 688 | MSG msg; |
| 689 | while (PeekMessage(&msg, INVALID_HANDLE_VALUE, |
| 690 | WM_AGENT_CALLBACK, WM_AGENT_CALLBACK, |
| 691 | PM_REMOVE)) { |
| 692 | struct agent_callback *c = (struct agent_callback *)msg.lParam; |
| 693 | c->callback(c->callback_ctx, c->data, c->len); |
| 694 | sfree(c); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (n == WAIT_TIMEOUT) { |
| 699 | now = next; |
| 700 | } else { |
| 701 | now = GETTICKCOUNT(); |
| 702 | } |
| 703 | |
| 704 | sfree(handles); |
| 705 | |
| 706 | if (sending) |
| 707 | handle_unthrottle(stdin_handle, back->sendbuffer(backhandle)); |
| 708 | |
| 709 | if ((!connopen || !back->connected(backhandle)) && |
| 710 | handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0) |
| 711 | break; /* we closed the connection */ |
| 712 | } |
| 713 | exitcode = back->exitcode(backhandle); |
| 714 | if (exitcode < 0) { |
| 715 | fprintf(stderr, "Remote process exit code unavailable\n"); |
| 716 | exitcode = 1; /* this is an error condition */ |
| 717 | } |
| 718 | cleanup_exit(exitcode); |
| 719 | return 0; /* placate compiler warning */ |
| 720 | } |