2 * PLink - a Windows command-line (stdin/stdout) variant of PuTTY.
\r
10 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
\r
12 #include "storage.h"
\r
13 #include "tree234.h"
\r
15 #define WM_AGENT_CALLBACK (WM_APP + 4)
\r
17 struct agent_callback {
\r
18 void (*callback)(void *, void *, int);
\r
24 void fatalbox(char *p, ...)
\r
27 char *stuff, morestuff[100];
\r
30 stuff = dupvprintf(p, ap);
\r
32 sprintf(morestuff, "%.70s Fatal Error", appname);
\r
33 MessageBox(GetParentHwnd(), stuff, morestuff, MB_ICONERROR | MB_OK);
\r
41 void modalfatalbox(char *p, ...)
\r
44 char *stuff, morestuff[100];
\r
47 stuff = dupvprintf(p, ap);
\r
49 sprintf(morestuff, "%.70s Fatal Error", appname);
\r
50 MessageBox(GetParentHwnd(), stuff, morestuff,
\r
51 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
\r
59 void connection_fatal(void *frontend, char *p, ...)
\r
62 char *stuff, morestuff[100];
\r
65 stuff = dupvprintf(p, ap);
\r
67 sprintf(morestuff, "%.70s Fatal Error", appname);
\r
68 MessageBox(GetParentHwnd(), stuff, morestuff,
\r
69 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
\r
77 void cmdline_error(char *p, ...)
\r
80 char *stuff, morestuff[100];
\r
83 stuff = dupvprintf(p, ap);
\r
85 sprintf(morestuff, "%.70s Command Line Error", appname);
\r
86 MessageBox(GetParentHwnd(), stuff, morestuff, MB_ICONERROR | MB_OK);
\r
91 HANDLE inhandle, outhandle, errhandle;
\r
92 struct handle *stdin_handle, *stdout_handle, *stderr_handle;
\r
93 DWORD orig_console_mode;
\r
98 static Backend *back;
\r
99 static void *backhandle;
\r
102 int term_ldisc(Terminal *term, int mode)
\r
106 void ldisc_update(void *frontend, int echo, int edit)
\r
108 /* Update stdin read mode to reflect changes in line discipline. */
\r
111 mode = ENABLE_PROCESSED_INPUT;
\r
113 mode = mode | ENABLE_ECHO_INPUT;
\r
115 mode = mode & ~ENABLE_ECHO_INPUT;
\r
117 mode = mode | ENABLE_LINE_INPUT;
\r
119 mode = mode & ~ENABLE_LINE_INPUT;
\r
120 SetConsoleMode(inhandle, mode);
\r
123 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
\r
125 int from_backend(void *frontend_handle, int is_stderr,
\r
126 const char *data, int len)
\r
129 handle_write(stderr_handle, data, len);
\r
131 handle_write(stdout_handle, data, len);
\r
134 return handle_backlog(stdout_handle) + handle_backlog(stderr_handle);
\r
137 int from_backend_untrusted(void *frontend_handle, const char *data, int len)
\r
140 * No "untrusted" output should get here (the way the code is
\r
141 * currently, it's all diverted by FLAG_STDERR).
\r
143 assert(!"Unexpected call to from_backend_untrusted()");
\r
144 return 0; /* not reached */
\r
147 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
\r
150 ret = cmdline_get_passwd_input(p, in, inlen);
\r
152 ret = console_get_userpass_input(p, in, inlen);
\r
156 static DWORD main_thread_id;
\r
158 void agent_schedule_callback(void (*callback)(void *, void *, int),
\r
159 void *callback_ctx, void *data, int len)
\r
161 struct agent_callback *c = snew(struct agent_callback);
\r
162 c->callback = callback;
\r
163 c->callback_ctx = callback_ctx;
\r
166 PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c);
\r
170 * Short description of parameters.
\r
172 static void usage(void)
\r
177 j += sprintf(buf+j, "PuTTY Link: command-line connection utility\n");
\r
178 j += sprintf(buf+j, "%s\n", ver);
\r
179 j += sprintf(buf+j, "Usage: plink [options] [user@]host [command]\n");
\r
180 j += sprintf(buf+j, " (\"host\" can also be a PuTTY saved session name)\n");
\r
181 j += sprintf(buf+j, "Options:\n");
\r
182 j += sprintf(buf+j, " -V print version information and exit\n");
\r
183 j += sprintf(buf+j, " -pgpfp print PGP key fingerprints and exit\n");
\r
184 j += sprintf(buf+j, " -v show verbose messages\n");
\r
185 j += sprintf(buf+j, " -load sessname Load settings from saved session\n");
\r
186 j += sprintf(buf+j, " -ssh -telnet -rlogin -raw\n");
\r
187 j += sprintf(buf+j, " force use of a particular protocol\n");
\r
188 j += sprintf(buf+j, " -P port connect to specified port\n");
\r
189 j += sprintf(buf+j, " -l user connect with specified username\n");
\r
190 j += sprintf(buf+j, "The following options only apply to SSH connections:\n");
\r
191 j += sprintf(buf+j, " -pw passw login with specified password\n");
\r
192 j += sprintf(buf+j, " -D [listen-IP:]listen-port\n");
\r
193 j += sprintf(buf+j, " Dynamic SOCKS-based port forwarding\n");
\r
194 j += sprintf(buf+j, " -L [listen-IP:]listen-port:host:port\n");
\r
195 j += sprintf(buf+j, " Forward local port to remote address\n");
\r
196 j += sprintf(buf+j, " -R [listen-IP:]listen-port:host:port\n");
\r
197 j += sprintf(buf+j, " Forward remote port to local address\n");
\r
198 j += sprintf(buf+j, " -X -x enable / disable X11 forwarding\n");
\r
199 j += sprintf(buf+j, " -A -a enable / disable agent forwarding\n");
\r
200 j += sprintf(buf+j, " -t -T enable / disable pty allocation\n");
\r
201 j += sprintf(buf+j, " -1 -2 force use of particular protocol version\n");
\r
202 j += sprintf(buf+j, " -4 -6 force use of IPv4 or IPv6\n");
\r
203 j += sprintf(buf+j, " -C enable compression\n");
\r
204 j += sprintf(buf+j, " -i key private key file for authentication\n");
\r
205 j += sprintf(buf+j, " -noagent disable use of Pageant\n");
\r
206 j += sprintf(buf+j, " -agent enable use of Pageant\n");
\r
207 j += sprintf(buf+j, " -m file read remote command(s) from file\n");
\r
208 j += sprintf(buf+j, " -s remote command is an SSH subsystem (SSH-2 only)\n");
\r
209 j += sprintf(buf+j, " -N don't start a shell/command (SSH-2 only)\n");
\r
210 j += sprintf(buf+j, " -nc host:port\n");
\r
211 j += sprintf(buf+j, " open tunnel in place of session (SSH-2 only)\n");
\r
212 MessageBox(NULL, buf, "TortoiseGitPlink", MB_ICONINFORMATION);
\r
216 static void version(void)
\r
218 printf("TortoiseGitPlink: %s\n", ver);
\r
222 char *do_select(SOCKET skt, int startup)
\r
226 events = (FD_CONNECT | FD_READ | FD_WRITE |
\r
227 FD_OOB | FD_CLOSE | FD_ACCEPT);
\r
231 if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
\r
232 switch (p_WSAGetLastError()) {
\r
234 return "Network is down";
\r
236 return "WSAEventSelect(): unknown error";
\r
242 int stdin_gotdata(struct handle *h, void *data, int len)
\r
246 * Special case: report read error.
\r
249 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -len, 0,
\r
250 buf, lenof(buf), NULL);
\r
251 buf[lenof(buf)-1] = '\0';
\r
252 if (buf[strlen(buf)-1] == '\n')
\r
253 buf[strlen(buf)-1] = '\0';
\r
254 fprintf(stderr, "Unable to read from standard input: %s\n", buf);
\r
257 noise_ultralight(len);
\r
258 if (connopen && back->connected(backhandle)) {
\r
260 return back->send(backhandle, data, len);
\r
262 back->special(backhandle, TS_EOF);
\r
269 void stdouterr_sent(struct handle *h, int new_backlog)
\r
271 if (new_backlog < 0) {
\r
273 * Special case: report write error.
\r
276 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -new_backlog, 0,
\r
277 buf, lenof(buf), NULL);
\r
278 buf[lenof(buf)-1] = '\0';
\r
279 if (buf[strlen(buf)-1] == '\n')
\r
280 buf[strlen(buf)-1] = '\0';
\r
281 fprintf(stderr, "Unable to write to standard %s: %s\n",
\r
282 (h == stdout_handle ? "output" : "error"), buf);
\r
285 if (connopen && back->connected(backhandle)) {
\r
286 back->unthrottle(backhandle, (handle_backlog(stdout_handle) +
\r
287 handle_backlog(stderr_handle)));
\r
291 int main(int argc, char **argv)
\r
294 int portnumber = -1;
\r
296 int skcount, sksize;
\r
299 int got_host = FALSE;
\r
300 int use_subsystem = 0;
\r
304 skcount = sksize = 0;
\r
306 * Initialise port and protocol to sensible defaults. (These
\r
307 * will be overridden by more or less anything.)
\r
309 default_protocol = PROT_SSH;
\r
312 flags = FLAG_STDERR;
\r
314 * Process the command line.
\r
316 do_defaults(NULL, &cfg);
\r
317 loaded_session = FALSE;
\r
318 default_protocol = cfg.protocol;
\r
319 default_port = cfg.port;
\r
323 * Override the default protocol if PLINK_PROTOCOL is set.
\r
325 char *p = getenv("PLINK_PROTOCOL");
\r
327 const Backend *b = backend_from_name(p);
\r
329 default_protocol = cfg.protocol = b->protocol;
\r
330 default_port = cfg.port = b->default_port;
\r
337 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
\r
341 "plink: option \"%s\" requires an argument\n", p);
\r
343 } else if (ret == 2) {
\r
345 } else if (ret == 1) {
\r
347 } else if (!strcmp(p, "-batch")) {
\r
348 // ignore and do not print an error message
\r
349 } else if (!strcmp(p, "-s")) {
\r
350 /* Save status to write to cfg later. */
\r
352 } else if (!strcmp(p, "-V")) {
\r
354 } else if (!strcmp(p, "-pgpfp")) {
\r
355 pgp_fingerprints();
\r
358 fprintf(stderr, "plink: unknown option \"%s\"\n", p);
\r
362 if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) {
\r
365 * If the hostname starts with "telnet:", set the
\r
366 * protocol to Telnet and process the string as a
\r
369 if (!strncmp(q, "telnet:", 7)) {
\r
373 if (q[0] == '/' && q[1] == '/')
\r
375 cfg.protocol = PROT_TELNET;
\r
377 while (*p && *p != ':' && *p != '/')
\r
383 cfg.port = atoi(p);
\r
386 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
\r
387 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
390 char *r, *user, *host;
\r
392 * Before we process the [user@]host string, we
\r
393 * first check for the presence of a protocol
\r
394 * prefix (a protocol name followed by ",").
\r
396 r = strchr(p, ',');
\r
400 b = backend_from_name(p);
\r
402 default_protocol = cfg.protocol = b->protocol;
\r
403 portnumber = b->default_port;
\r
409 * A nonzero length string followed by an @ is treated
\r
410 * as a username. (We discount an _initial_ @.) The
\r
411 * rest of the string (or the whole string if no @)
\r
412 * is treated as a session name and/or hostname.
\r
414 r = strrchr(p, '@');
\r
416 p++, r = NULL; /* discount initial @ */
\r
419 user = p, host = r;
\r
421 user = NULL, host = p;
\r
425 * Now attempt to load a saved session with the
\r
426 * same name as the hostname.
\r
430 do_defaults(host, &cfg2);
\r
431 if (loaded_session || !cfg_launchable(&cfg2)) {
\r
432 /* No settings for this host; use defaults */
\r
433 /* (or session was already loaded with -load) */
\r
434 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
\r
435 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
436 cfg.port = default_port;
\r
440 loaded_session = TRUE;
\r
445 /* Patch in specified username. */
\r
446 strncpy(cfg.username, user,
\r
447 sizeof(cfg.username) - 1);
\r
448 cfg.username[sizeof(cfg.username) - 1] = '\0';
\r
454 int cmdlen, cmdsize;
\r
455 cmdlen = cmdsize = 0;
\r
460 if (cmdlen >= cmdsize) {
\r
461 cmdsize = cmdlen + 512;
\r
462 command = sresize(command, cmdsize, char);
\r
464 command[cmdlen++]=*p++;
\r
466 if (cmdlen >= cmdsize) {
\r
467 cmdsize = cmdlen + 512;
\r
468 command = sresize(command, cmdsize, char);
\r
470 command[cmdlen++]=' '; /* always add trailing space */
\r
471 if (--argc) p = *++argv;
\r
473 if (cmdlen) command[--cmdlen]='\0';
\r
474 /* change trailing blank to NUL */
\r
475 cfg.remote_cmd_ptr = command;
\r
476 cfg.remote_cmd_ptr2 = NULL;
\r
477 cfg.nopty = TRUE; /* command => no terminal */
\r
479 break; /* done with cmdline */
\r
487 if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) {
\r
492 * Trim leading whitespace off the hostname if it's there.
\r
495 int space = strspn(cfg.host, " \t");
\r
496 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
\r
499 /* See if host is of the form user@host */
\r
500 if (cfg_launchable(&cfg)) {
\r
501 char *atsign = strrchr(cfg.host, '@');
\r
502 /* Make sure we're not overflowing the user field */
\r
504 if (atsign - cfg.host < sizeof cfg.username) {
\r
505 strncpy(cfg.username, cfg.host, atsign - cfg.host);
\r
506 cfg.username[atsign - cfg.host] = '\0';
\r
508 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
\r
513 * Perform command-line overrides on session configuration.
\r
515 cmdline_run_saved(&cfg);
\r
518 * Apply subsystem status.
\r
521 cfg.ssh_subsys = TRUE;
\r
524 * Trim a colon suffix off the hostname if it's there.
\r
526 cfg.host[strcspn(cfg.host, ":")] = '\0';
\r
529 * Remove any remaining whitespace from the hostname.
\r
532 int p1 = 0, p2 = 0;
\r
533 while (cfg.host[p2] != '\0') {
\r
534 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
\r
535 cfg.host[p1] = cfg.host[p2];
\r
540 cfg.host[p1] = '\0';
\r
543 if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd && !*cfg.ssh_nc_host)
\r
544 flags |= FLAG_INTERACTIVE;
\r
547 * Select protocol. This is farmed out into a table in a
\r
548 * separate file to enable an ssh-free variant.
\r
550 back = backend_from_proto(cfg.protocol);
\r
551 if (back == NULL) {
\r
553 "Internal fault: Unsupported protocol found\n");
\r
560 if (portnumber != -1)
\r
561 cfg.port = portnumber;
\r
564 if (p_WSAEventSelect == NULL) {
\r
565 fprintf(stderr, "Plink requires WinSock 2\n");
\r
569 logctx = log_init(NULL, &cfg);
\r
570 console_provide_logctx(logctx);
\r
573 * Start up the connection.
\r
575 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
\r
579 /* nodelay is only useful if stdin is a character device (console) */
\r
580 int nodelay = cfg.tcp_nodelay &&
\r
581 (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
\r
583 error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port,
\r
584 &realhost, nodelay, cfg.tcp_keepalives);
\r
586 fprintf(stderr, "Unable to open connection:\n%s", error);
\r
589 back->provide_logctx(backhandle, logctx);
\r
594 inhandle = GetStdHandle(STD_INPUT_HANDLE);
\r
595 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
\r
596 errhandle = GetStdHandle(STD_ERROR_HANDLE);
\r
599 * Turn off ECHO and LINE input modes. We don't care if this
\r
600 * call fails, because we know we aren't necessarily running in
\r
603 GetConsoleMode(inhandle, &orig_console_mode);
\r
604 SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
\r
607 * Pass the output handles to the handle-handling subsystem.
\r
608 * (The input one we leave until we're through the
\r
609 * authentication process.)
\r
611 stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL, 0);
\r
612 stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL, 0);
\r
614 main_thread_id = GetCurrentThreadId();
\r
618 now = GETTICKCOUNT();
\r
626 if (!sending && back->sendok(backhandle)) {
\r
627 stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL,
\r
632 if (run_timers(now, &next)) {
\r
633 ticks = next - GETTICKCOUNT();
\r
634 if (ticks < 0) ticks = 0; /* just in case */
\r
639 handles = handle_get_events(&nhandles);
\r
640 handles = sresize(handles, nhandles+1, HANDLE);
\r
641 handles[nhandles] = netevent;
\r
642 n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks,
\r
644 if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
\r
645 handle_got_event(handles[n - WAIT_OBJECT_0]);
\r
646 } else if (n == WAIT_OBJECT_0 + nhandles) {
\r
647 WSANETWORKEVENTS things;
\r
649 extern SOCKET first_socket(int *), next_socket(int *);
\r
650 extern int select_result(WPARAM, LPARAM);
\r
651 int i, socketstate;
\r
654 * We must not call select_result() for any socket
\r
655 * until we have finished enumerating within the tree.
\r
656 * This is because select_result() may close the socket
\r
657 * and modify the tree.
\r
659 /* Count the active sockets. */
\r
661 for (socket = first_socket(&socketstate);
\r
662 socket != INVALID_SOCKET;
\r
663 socket = next_socket(&socketstate)) i++;
\r
665 /* Expand the buffer if necessary. */
\r
668 sklist = sresize(sklist, sksize, SOCKET);
\r
671 /* Retrieve the sockets into sklist. */
\r
673 for (socket = first_socket(&socketstate);
\r
674 socket != INVALID_SOCKET;
\r
675 socket = next_socket(&socketstate)) {
\r
676 sklist[skcount++] = socket;
\r
679 /* Now we're done enumerating; go through the list. */
\r
680 for (i = 0; i < skcount; i++) {
\r
682 socket = sklist[i];
\r
683 wp = (WPARAM) socket;
\r
684 if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) {
\r
685 static const struct { int bit, mask; } eventtypes[] = {
\r
686 {FD_CONNECT_BIT, FD_CONNECT},
\r
687 {FD_READ_BIT, FD_READ},
\r
688 {FD_CLOSE_BIT, FD_CLOSE},
\r
689 {FD_OOB_BIT, FD_OOB},
\r
690 {FD_WRITE_BIT, FD_WRITE},
\r
691 {FD_ACCEPT_BIT, FD_ACCEPT},
\r
695 noise_ultralight(socket);
\r
696 noise_ultralight(things.lNetworkEvents);
\r
698 for (e = 0; e < lenof(eventtypes); e++)
\r
699 if (things.lNetworkEvents & eventtypes[e].mask) {
\r
701 int err = things.iErrorCode[eventtypes[e].bit];
\r
702 lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err);
\r
703 connopen &= select_result(wp, lp);
\r
707 } else if (n == WAIT_OBJECT_0 + nhandles + 1) {
\r
709 while (PeekMessage(&msg, INVALID_HANDLE_VALUE,
\r
710 WM_AGENT_CALLBACK, WM_AGENT_CALLBACK,
\r
712 struct agent_callback *c = (struct agent_callback *)msg.lParam;
\r
713 c->callback(c->callback_ctx, c->data, c->len);
\r
718 if (n == WAIT_TIMEOUT) {
\r
721 now = GETTICKCOUNT();
\r
727 handle_unthrottle(stdin_handle, back->sendbuffer(backhandle));
\r
729 if ((!connopen || !back->connected(backhandle)) &&
\r
730 handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0)
\r
731 break; /* we closed the connection */
\r
733 exitcode = back->exitcode(backhandle);
\r
734 if (exitcode < 0) {
\r
735 fprintf(stderr, "Remote process exit code unavailable\n");
\r
736 exitcode = 1; /* this is an error condition */
\r
738 cleanup_exit(exitcode);
\r
739 return 0; /* placate compiler warning */
\r
742 int WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
\r
744 main(__argc,__argv);
\r