+++ /dev/null
-/*\r
- * PLink - a command-line (stdin/stdout) variant of PuTTY.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <errno.h>\r
-#include <assert.h>\r
-#include <stdarg.h>\r
-#include <signal.h>\r
-#include <unistd.h>\r
-#include <fcntl.h>\r
-#include <termios.h>\r
-#include <pwd.h>\r
-#include <sys/ioctl.h>\r
-#include <sys/time.h>\r
-#ifndef HAVE_NO_SYS_SELECT_H\r
-#include <sys/select.h>\r
-#endif\r
-\r
-#define PUTTY_DO_GLOBALS /* actually _define_ globals */\r
-#include "putty.h"\r
-#include "storage.h"\r
-#include "tree234.h"\r
-\r
-#define MAX_STDIN_BACKLOG 4096\r
-\r
-void *logctx;\r
-\r
-static struct termios orig_termios;\r
-\r
-void fatalbox(char *p, ...)\r
-{\r
- struct termios cf;\r
- va_list ap;\r
- premsg(&cf);\r
- fprintf(stderr, "FATAL ERROR: ");\r
- va_start(ap, p);\r
- vfprintf(stderr, p, ap);\r
- va_end(ap);\r
- fputc('\n', stderr);\r
- postmsg(&cf);\r
- if (logctx) {\r
- log_free(logctx);\r
- logctx = NULL;\r
- }\r
- cleanup_exit(1);\r
-}\r
-void modalfatalbox(char *p, ...)\r
-{\r
- struct termios cf;\r
- va_list ap;\r
- premsg(&cf);\r
- fprintf(stderr, "FATAL ERROR: ");\r
- va_start(ap, p);\r
- vfprintf(stderr, p, ap);\r
- va_end(ap);\r
- fputc('\n', stderr);\r
- postmsg(&cf);\r
- if (logctx) {\r
- log_free(logctx);\r
- logctx = NULL;\r
- }\r
- cleanup_exit(1);\r
-}\r
-void connection_fatal(void *frontend, char *p, ...)\r
-{\r
- struct termios cf;\r
- va_list ap;\r
- premsg(&cf);\r
- fprintf(stderr, "FATAL ERROR: ");\r
- va_start(ap, p);\r
- vfprintf(stderr, p, ap);\r
- va_end(ap);\r
- fputc('\n', stderr);\r
- postmsg(&cf);\r
- if (logctx) {\r
- log_free(logctx);\r
- logctx = NULL;\r
- }\r
- cleanup_exit(1);\r
-}\r
-void cmdline_error(char *p, ...)\r
-{\r
- struct termios cf;\r
- va_list ap;\r
- premsg(&cf);\r
- fprintf(stderr, "plink: ");\r
- va_start(ap, p);\r
- vfprintf(stderr, p, ap);\r
- va_end(ap);\r
- fputc('\n', stderr);\r
- postmsg(&cf);\r
- exit(1);\r
-}\r
-\r
-static int local_tty = FALSE; /* do we have a local tty? */\r
-\r
-static Backend *back;\r
-static void *backhandle;\r
-static Config cfg;\r
-\r
-/*\r
- * Default settings that are specific to pterm.\r
- */\r
-char *platform_default_s(const char *name)\r
-{\r
- if (!strcmp(name, "TermType"))\r
- return dupstr(getenv("TERM"));\r
- if (!strcmp(name, "UserName"))\r
- return get_username();\r
- if (!strcmp(name, "SerialLine"))\r
- return dupstr("/dev/ttyS0");\r
- return NULL;\r
-}\r
-\r
-int platform_default_i(const char *name, int def)\r
-{\r
- if (!strcmp(name, "TermWidth") ||\r
- !strcmp(name, "TermHeight")) {\r
- struct winsize size;\r
- if (ioctl(STDIN_FILENO, TIOCGWINSZ, (void *)&size) >= 0)\r
- return (!strcmp(name, "TermWidth") ? size.ws_col : size.ws_row);\r
- }\r
- return def;\r
-}\r
-\r
-FontSpec platform_default_fontspec(const char *name)\r
-{\r
- FontSpec ret;\r
- *ret.name = '\0';\r
- return ret;\r
-}\r
-\r
-Filename platform_default_filename(const char *name)\r
-{\r
- Filename ret;\r
- if (!strcmp(name, "LogFileName"))\r
- strcpy(ret.path, "putty.log");\r
- else\r
- *ret.path = '\0';\r
- return ret;\r
-}\r
-\r
-char *x_get_default(const char *key)\r
-{\r
- return NULL; /* this is a stub */\r
-}\r
-int term_ldisc(Terminal *term, int mode)\r
-{\r
- return FALSE;\r
-}\r
-void ldisc_update(void *frontend, int echo, int edit)\r
-{\r
- /* Update stdin read mode to reflect changes in line discipline. */\r
- struct termios mode;\r
-\r
- if (!local_tty) return;\r
-\r
- mode = orig_termios;\r
-\r
- if (echo)\r
- mode.c_lflag |= ECHO;\r
- else\r
- mode.c_lflag &= ~ECHO;\r
-\r
- if (edit) {\r
- mode.c_iflag |= ICRNL;\r
- mode.c_lflag |= ISIG | ICANON;\r
- mode.c_oflag |= OPOST;\r
- } else {\r
- mode.c_iflag &= ~ICRNL;\r
- mode.c_lflag &= ~(ISIG | ICANON);\r
- mode.c_oflag &= ~OPOST;\r
- /* Solaris sets these to unhelpful values */\r
- mode.c_cc[VMIN] = 1;\r
- mode.c_cc[VTIME] = 0;\r
- /* FIXME: perhaps what we do with IXON/IXOFF should be an\r
- * argument to ldisc_update(), to allow implementation of SSH-2\r
- * "xon-xoff" and Rlogin's equivalent? */\r
- mode.c_iflag &= ~IXON;\r
- mode.c_iflag &= ~IXOFF;\r
- }\r
- /* \r
- * Mark parity errors and (more important) BREAK on input. This\r
- * is more complex than it need be because POSIX-2001 suggests\r
- * that escaping of valid 0xff in the input stream is dependent on\r
- * IGNPAR being clear even though marking of BREAK isn't. NetBSD\r
- * 2.0 goes one worse and makes it dependent on INPCK too. We\r
- * deal with this by forcing these flags into a useful state and\r
- * then faking the state in which we found them in from_tty() if\r
- * we get passed a parity or framing error.\r
- */\r
- mode.c_iflag = (mode.c_iflag | INPCK | PARMRK) & ~IGNPAR;\r
-\r
- tcsetattr(STDIN_FILENO, TCSANOW, &mode);\r
-}\r
-\r
-/* Helper function to extract a special character from a termios. */\r
-static char *get_ttychar(struct termios *t, int index)\r
-{\r
- cc_t c = t->c_cc[index];\r
-#if defined(_POSIX_VDISABLE)\r
- if (c == _POSIX_VDISABLE)\r
- return dupprintf("");\r
-#endif\r
- return dupprintf("^<%d>", c);\r
-}\r
-\r
-char *get_ttymode(void *frontend, const char *mode)\r
-{\r
- /*\r
- * Propagate appropriate terminal modes from the local terminal,\r
- * if any.\r
- */\r
- if (!local_tty) return NULL;\r
-\r
-#define GET_CHAR(ourname, uxname) \\r
- do { \\r
- if (strcmp(mode, ourname) == 0) \\r
- return get_ttychar(&orig_termios, uxname); \\r
- } while(0)\r
-#define GET_BOOL(ourname, uxname, uxmemb, transform) \\r
- do { \\r
- if (strcmp(mode, ourname) == 0) { \\r
- int b = (orig_termios.uxmemb & uxname) != 0; \\r
- transform; \\r
- return dupprintf("%d", b); \\r
- } \\r
- } while (0)\r
-\r
- /*\r
- * Modes that want to be the same on all terminal devices involved.\r
- */\r
- /* All the special characters supported by SSH */\r
-#if defined(VINTR)\r
- GET_CHAR("INTR", VINTR);\r
-#endif\r
-#if defined(VQUIT)\r
- GET_CHAR("QUIT", VQUIT);\r
-#endif\r
-#if defined(VERASE)\r
- GET_CHAR("ERASE", VERASE);\r
-#endif\r
-#if defined(VKILL)\r
- GET_CHAR("KILL", VKILL);\r
-#endif\r
-#if defined(VEOF)\r
- GET_CHAR("EOF", VEOF);\r
-#endif\r
-#if defined(VEOL)\r
- GET_CHAR("EOL", VEOL);\r
-#endif\r
-#if defined(VEOL2)\r
- GET_CHAR("EOL2", VEOL2);\r
-#endif\r
-#if defined(VSTART)\r
- GET_CHAR("START", VSTART);\r
-#endif\r
-#if defined(VSTOP)\r
- GET_CHAR("STOP", VSTOP);\r
-#endif\r
-#if defined(VSUSP)\r
- GET_CHAR("SUSP", VSUSP);\r
-#endif\r
-#if defined(VDSUSP)\r
- GET_CHAR("DSUSP", VDSUSP);\r
-#endif\r
-#if defined(VREPRINT)\r
- GET_CHAR("REPRINT", VREPRINT);\r
-#endif\r
-#if defined(VWERASE)\r
- GET_CHAR("WERASE", VWERASE);\r
-#endif\r
-#if defined(VLNEXT)\r
- GET_CHAR("LNEXT", VLNEXT);\r
-#endif\r
-#if defined(VFLUSH)\r
- GET_CHAR("FLUSH", VFLUSH);\r
-#endif\r
-#if defined(VSWTCH)\r
- GET_CHAR("SWTCH", VSWTCH);\r
-#endif\r
-#if defined(VSTATUS)\r
- GET_CHAR("STATUS", VSTATUS);\r
-#endif\r
-#if defined(VDISCARD)\r
- GET_CHAR("DISCARD", VDISCARD);\r
-#endif\r
- /* Modes that "configure" other major modes. These should probably be\r
- * considered as user preferences. */\r
- /* Configuration of ICANON */\r
-#if defined(ECHOK)\r
- GET_BOOL("ECHOK", ECHOK, c_lflag, );\r
-#endif\r
-#if defined(ECHOKE)\r
- GET_BOOL("ECHOKE", ECHOKE, c_lflag, );\r
-#endif\r
-#if defined(ECHOE)\r
- GET_BOOL("ECHOE", ECHOE, c_lflag, );\r
-#endif\r
-#if defined(ECHONL)\r
- GET_BOOL("ECHONL", ECHONL, c_lflag, );\r
-#endif\r
-#if defined(XCASE)\r
- GET_BOOL("XCASE", XCASE, c_lflag, );\r
-#endif\r
- /* Configuration of ECHO */\r
-#if defined(ECHOCTL)\r
- GET_BOOL("ECHOCTL", ECHOCTL, c_lflag, );\r
-#endif\r
- /* Configuration of IXON/IXOFF */\r
-#if defined(IXANY)\r
- GET_BOOL("IXANY", IXANY, c_iflag, );\r
-#endif\r
- /* Configuration of OPOST */\r
-#if defined(OLCUC)\r
- GET_BOOL("OLCUC", OLCUC, c_oflag, );\r
-#endif\r
-#if defined(ONLCR)\r
- GET_BOOL("ONLCR", ONLCR, c_oflag, );\r
-#endif\r
-#if defined(OCRNL)\r
- GET_BOOL("OCRNL", OCRNL, c_oflag, );\r
-#endif\r
-#if defined(ONOCR)\r
- GET_BOOL("ONOCR", ONOCR, c_oflag, );\r
-#endif\r
-#if defined(ONLRET)\r
- GET_BOOL("ONLRET", ONLRET, c_oflag, );\r
-#endif\r
-\r
- /*\r
- * Modes that want to be set in only one place, and that we have\r
- * squashed locally.\r
- */\r
-#if defined(ISIG)\r
- GET_BOOL("ISIG", ISIG, c_lflag, );\r
-#endif\r
-#if defined(ICANON)\r
- GET_BOOL("ICANON", ICANON, c_lflag, );\r
-#endif\r
-#if defined(ECHO)\r
- GET_BOOL("ECHO", ECHO, c_lflag, );\r
-#endif\r
-#if defined(IXON)\r
- GET_BOOL("IXON", IXON, c_iflag, );\r
-#endif\r
-#if defined(IXOFF)\r
- GET_BOOL("IXOFF", IXOFF, c_iflag, );\r
-#endif\r
-#if defined(OPOST)\r
- GET_BOOL("OPOST", OPOST, c_oflag, );\r
-#endif\r
-\r
- /*\r
- * We do not propagate the following modes:\r
- * - Parity/serial settings, which are a local affair and don't\r
- * make sense propagated over SSH's 8-bit byte-stream.\r
- * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD\r
- * - Things that want to be enabled in one place that we don't\r
- * squash locally.\r
- * IUCLC\r
- * - Status bits.\r
- * PENDIN\r
- * - Things I don't know what to do with. (FIXME)\r
- * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN\r
- * INLCR IGNCR ICRNL\r
- */\r
-\r
-#undef GET_CHAR\r
-#undef GET_BOOL\r
-\r
- /* Fall through to here for unrecognised names, or ones that are\r
- * unsupported on this platform */\r
- return NULL;\r
-}\r
-\r
-void cleanup_termios(void)\r
-{\r
- if (local_tty)\r
- tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);\r
-}\r
-\r
-bufchain stdout_data, stderr_data;\r
-\r
-int try_output(int is_stderr)\r
-{\r
- bufchain *chain = (is_stderr ? &stderr_data : &stdout_data);\r
- int fd = (is_stderr ? STDERR_FILENO : STDOUT_FILENO);\r
- void *senddata;\r
- int sendlen, ret, fl;\r
-\r
- if (bufchain_size(chain) == 0)\r
- return bufchain_size(&stdout_data) + bufchain_size(&stderr_data);\r
-\r
- fl = fcntl(fd, F_GETFL);\r
- if (fl != -1 && !(fl & O_NONBLOCK))\r
- fcntl(fd, F_SETFL, fl | O_NONBLOCK);\r
- do {\r
- bufchain_prefix(chain, &senddata, &sendlen);\r
- ret = write(fd, senddata, sendlen);\r
- if (ret > 0)\r
- bufchain_consume(chain, ret);\r
- } while (ret == sendlen && bufchain_size(chain) != 0);\r
- if (fl != -1 && !(fl & O_NONBLOCK))\r
- fcntl(fd, F_SETFL, fl);\r
- if (ret < 0 && errno != EAGAIN) {\r
- perror(is_stderr ? "stderr: write" : "stdout: write");\r
- exit(1);\r
- }\r
- return bufchain_size(&stdout_data) + bufchain_size(&stderr_data);\r
-}\r
-\r
-int from_backend(void *frontend_handle, int is_stderr,\r
- const char *data, int len)\r
-{\r
- if (is_stderr) {\r
- bufchain_add(&stderr_data, data, len);\r
- return try_output(TRUE);\r
- } else {\r
- bufchain_add(&stdout_data, data, len);\r
- return try_output(FALSE);\r
- }\r
-}\r
-\r
-int from_backend_untrusted(void *frontend_handle, const char *data, int len)\r
-{\r
- /*\r
- * No "untrusted" output should get here (the way the code is\r
- * currently, it's all diverted by FLAG_STDERR).\r
- */\r
- assert(!"Unexpected call to from_backend_untrusted()");\r
- return 0; /* not reached */\r
-}\r
-\r
-int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)\r
-{\r
- int ret;\r
- ret = cmdline_get_passwd_input(p, in, inlen);\r
- if (ret == -1)\r
- ret = console_get_userpass_input(p, in, inlen);\r
- return ret;\r
-}\r
-\r
-/*\r
- * Handle data from a local tty in PARMRK format.\r
- */\r
-static void from_tty(void *vbuf, unsigned len)\r
-{\r
- char *p, *q, *end, *buf = vbuf;\r
- static enum {NORMAL, FF, FF00} state = NORMAL;\r
-\r
- p = buf; end = buf + len;\r
- while (p < end) {\r
- switch (state) {\r
- case NORMAL:\r
- if (*p == '\xff') {\r
- p++;\r
- state = FF;\r
- } else {\r
- q = memchr(p, '\xff', end - p);\r
- if (q == NULL) q = end;\r
- back->send(backhandle, p, q - p);\r
- p = q;\r
- }\r
- break;\r
- case FF:\r
- if (*p == '\xff') {\r
- back->send(backhandle, p, 1);\r
- p++;\r
- state = NORMAL;\r
- } else if (*p == '\0') {\r
- p++;\r
- state = FF00;\r
- } else abort();\r
- break;\r
- case FF00:\r
- if (*p == '\0') {\r
- back->special(backhandle, TS_BRK);\r
- } else {\r
- /* \r
- * Pretend that PARMRK wasn't set. This involves\r
- * faking what INPCK and IGNPAR would have done if\r
- * we hadn't overridden them. Unfortunately, we\r
- * can't do this entirely correctly because INPCK\r
- * distinguishes between framing and parity\r
- * errors, but PARMRK format represents both in\r
- * the same way. We assume that parity errors are\r
- * more common than framing errors, and hence\r
- * treat all input errors as being subject to\r
- * INPCK.\r
- */\r
- if (orig_termios.c_iflag & INPCK) {\r
- /* If IGNPAR is set, we throw away the character. */\r
- if (!(orig_termios.c_iflag & IGNPAR)) {\r
- /* PE/FE get passed on as NUL. */\r
- *p = 0;\r
- back->send(backhandle, p, 1);\r
- }\r
- } else {\r
- /* INPCK not set. Assume we got a parity error. */\r
- back->send(backhandle, p, 1);\r
- }\r
- }\r
- p++;\r
- state = NORMAL;\r
- }\r
- }\r
-}\r
-\r
-int signalpipe[2];\r
-\r
-void sigwinch(int signum)\r
-{\r
- if (write(signalpipe[1], "x", 1) <= 0)\r
- /* not much we can do about it */;\r
-}\r
-\r
-/*\r
- * In Plink our selects are synchronous, so these functions are\r
- * empty stubs.\r
- */\r
-int uxsel_input_add(int fd, int rwx) { return 0; }\r
-void uxsel_input_remove(int id) { }\r
-\r
-/*\r
- * Short description of parameters.\r
- */\r
-static void usage(void)\r
-{\r
- printf("PuTTY Link: command-line connection utility\n");\r
- printf("%s\n", ver);\r
- printf("Usage: plink [options] [user@]host [command]\n");\r
- printf(" (\"host\" can also be a PuTTY saved session name)\n");\r
- printf("Options:\n");\r
- printf(" -V print version information and exit\n");\r
- printf(" -pgpfp print PGP key fingerprints and exit\n");\r
- printf(" -v show verbose messages\n");\r
- printf(" -load sessname Load settings from saved session\n");\r
- printf(" -ssh -telnet -rlogin -raw -serial\n");\r
- printf(" force use of a particular protocol\n");\r
- printf(" -P port connect to specified port\n");\r
- printf(" -l user connect with specified username\n");\r
- printf(" -batch disable all interactive prompts\n");\r
- printf("The following options only apply to SSH connections:\n");\r
- printf(" -pw passw login with specified password\n");\r
- printf(" -D [listen-IP:]listen-port\n");\r
- printf(" Dynamic SOCKS-based port forwarding\n");\r
- printf(" -L [listen-IP:]listen-port:host:port\n");\r
- printf(" Forward local port to remote address\n");\r
- printf(" -R [listen-IP:]listen-port:host:port\n");\r
- printf(" Forward remote port to local address\n");\r
- printf(" -X -x enable / disable X11 forwarding\n");\r
- printf(" -A -a enable / disable agent forwarding\n");\r
- printf(" -t -T enable / disable pty allocation\n");\r
- printf(" -1 -2 force use of particular protocol version\n");\r
- printf(" -4 -6 force use of IPv4 or IPv6\n");\r
- printf(" -C enable compression\n");\r
- printf(" -i key private key file for authentication\n");\r
- printf(" -noagent disable use of Pageant\n");\r
- printf(" -agent enable use of Pageant\n");\r
- printf(" -m file read remote command(s) from file\n");\r
- printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");\r
- printf(" -N don't start a shell/command (SSH-2 only)\n");\r
- printf(" -nc host:port\n");\r
- printf(" open tunnel in place of session (SSH-2 only)\n");\r
- printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");\r
- printf(" Specify the serial configuration (serial only)\n");\r
- exit(1);\r
-}\r
-\r
-static void version(void)\r
-{\r
- printf("plink: %s\n", ver);\r
- exit(1);\r
-}\r
-\r
-int main(int argc, char **argv)\r
-{\r
- int sending;\r
- int portnumber = -1;\r
- int *fdlist;\r
- int fd;\r
- int i, fdcount, fdsize, fdstate;\r
- int connopen;\r
- int exitcode;\r
- int errors;\r
- int use_subsystem = 0;\r
- int got_host = FALSE;\r
- long now;\r
-\r
- fdlist = NULL;\r
- fdcount = fdsize = 0;\r
- /*\r
- * Initialise port and protocol to sensible defaults. (These\r
- * will be overridden by more or less anything.)\r
- */\r
- default_protocol = PROT_SSH;\r
- default_port = 22;\r
-\r
- flags = FLAG_STDERR | FLAG_STDERR_TTY;\r
-\r
- stderr_tty_init();\r
- /*\r
- * Process the command line.\r
- */\r
- do_defaults(NULL, &cfg);\r
- loaded_session = FALSE;\r
- default_protocol = cfg.protocol;\r
- default_port = cfg.port;\r
- errors = 0;\r
- {\r
- /*\r
- * Override the default protocol if PLINK_PROTOCOL is set.\r
- */\r
- char *p = getenv("PLINK_PROTOCOL");\r
- if (p) {\r
- const Backend *b = backend_from_name(p);\r
- if (b) {\r
- default_protocol = cfg.protocol = b->protocol;\r
- default_port = cfg.port = b->default_port;\r
- }\r
- }\r
- }\r
- while (--argc) {\r
- char *p = *++argv;\r
- if (*p == '-') {\r
- int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),\r
- 1, &cfg);\r
- if (ret == -2) {\r
- fprintf(stderr,\r
- "plink: option \"%s\" requires an argument\n", p);\r
- errors = 1;\r
- } else if (ret == 2) {\r
- --argc, ++argv;\r
- } else if (ret == 1) {\r
- continue;\r
- } else if (!strcmp(p, "-batch")) {\r
- console_batch_mode = 1;\r
- } else if (!strcmp(p, "-s")) {\r
- /* Save status to write to cfg later. */\r
- use_subsystem = 1;\r
- } else if (!strcmp(p, "-V")) {\r
- version();\r
- } else if (!strcmp(p, "-pgpfp")) {\r
- pgp_fingerprints();\r
- exit(1);\r
- } else if (!strcmp(p, "-o")) {\r
- if (argc <= 1) {\r
- fprintf(stderr,\r
- "plink: option \"-o\" requires an argument\n");\r
- errors = 1;\r
- } else {\r
- --argc;\r
- provide_xrm_string(*++argv);\r
- }\r
- } else {\r
- fprintf(stderr, "plink: unknown option \"%s\"\n", p);\r
- errors = 1;\r
- }\r
- } else if (*p) {\r
- if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) {\r
- char *q = p;\r
-\r
- /*\r
- * If the hostname starts with "telnet:", set the\r
- * protocol to Telnet and process the string as a\r
- * Telnet URL.\r
- */\r
- if (!strncmp(q, "telnet:", 7)) {\r
- char c;\r
-\r
- q += 7;\r
- if (q[0] == '/' && q[1] == '/')\r
- q += 2;\r
- cfg.protocol = PROT_TELNET;\r
- p = q;\r
- while (*p && *p != ':' && *p != '/')\r
- p++;\r
- c = *p;\r
- if (*p)\r
- *p++ = '\0';\r
- if (c == ':')\r
- cfg.port = atoi(p);\r
- else\r
- cfg.port = -1;\r
- strncpy(cfg.host, q, sizeof(cfg.host) - 1);\r
- cfg.host[sizeof(cfg.host) - 1] = '\0';\r
- got_host = TRUE;\r
- } else {\r
- char *r, *user, *host;\r
- /*\r
- * Before we process the [user@]host string, we\r
- * first check for the presence of a protocol\r
- * prefix (a protocol name followed by ",").\r
- */\r
- r = strchr(p, ',');\r
- if (r) {\r
- const Backend *b;\r
- *r = '\0';\r
- b = backend_from_name(p);\r
- if (b) {\r
- default_protocol = cfg.protocol = b->protocol;\r
- portnumber = b->default_port;\r
- }\r
- p = r + 1;\r
- }\r
-\r
- /*\r
- * A nonzero length string followed by an @ is treated\r
- * as a username. (We discount an _initial_ @.) The\r
- * rest of the string (or the whole string if no @)\r
- * is treated as a session name and/or hostname.\r
- */\r
- r = strrchr(p, '@');\r
- if (r == p)\r
- p++, r = NULL; /* discount initial @ */\r
- if (r) {\r
- *r++ = '\0';\r
- user = p, host = r;\r
- } else {\r
- user = NULL, host = p;\r
- }\r
-\r
- /*\r
- * Now attempt to load a saved session with the\r
- * same name as the hostname.\r
- */\r
- {\r
- Config cfg2;\r
- do_defaults(host, &cfg2);\r
- if (loaded_session || !cfg_launchable(&cfg2)) {\r
- /* No settings for this host; use defaults */\r
- /* (or session was already loaded with -load) */\r
- strncpy(cfg.host, host, sizeof(cfg.host) - 1);\r
- cfg.host[sizeof(cfg.host) - 1] = '\0';\r
- cfg.port = default_port;\r
- got_host = TRUE;\r
- } else {\r
- cfg = cfg2;\r
- loaded_session = TRUE;\r
- }\r
- }\r
-\r
- if (user) {\r
- /* Patch in specified username. */\r
- strncpy(cfg.username, user,\r
- sizeof(cfg.username) - 1);\r
- cfg.username[sizeof(cfg.username) - 1] = '\0';\r
- }\r
-\r
- }\r
- } else {\r
- char *command;\r
- int cmdlen, cmdsize;\r
- cmdlen = cmdsize = 0;\r
- command = NULL;\r
-\r
- while (argc) {\r
- while (*p) {\r
- if (cmdlen >= cmdsize) {\r
- cmdsize = cmdlen + 512;\r
- command = sresize(command, cmdsize, char);\r
- }\r
- command[cmdlen++]=*p++;\r
- }\r
- if (cmdlen >= cmdsize) {\r
- cmdsize = cmdlen + 512;\r
- command = sresize(command, cmdsize, char);\r
- }\r
- command[cmdlen++]=' '; /* always add trailing space */\r
- if (--argc) p = *++argv;\r
- }\r
- if (cmdlen) command[--cmdlen]='\0';\r
- /* change trailing blank to NUL */\r
- cfg.remote_cmd_ptr = command;\r
- cfg.remote_cmd_ptr2 = NULL;\r
- cfg.nopty = TRUE; /* command => no terminal */\r
-\r
- break; /* done with cmdline */\r
- }\r
- }\r
- }\r
-\r
- if (errors)\r
- return 1;\r
-\r
- if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) {\r
- usage();\r
- }\r
-\r
- /*\r
- * Trim leading whitespace off the hostname if it's there.\r
- */\r
- {\r
- int space = strspn(cfg.host, " \t");\r
- memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);\r
- }\r
-\r
- /* See if host is of the form user@host */\r
- if (cfg.host[0] != '\0') {\r
- char *atsign = strrchr(cfg.host, '@');\r
- /* Make sure we're not overflowing the user field */\r
- if (atsign) {\r
- if (atsign - cfg.host < sizeof cfg.username) {\r
- strncpy(cfg.username, cfg.host, atsign - cfg.host);\r
- cfg.username[atsign - cfg.host] = '\0';\r
- }\r
- memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));\r
- }\r
- }\r
-\r
- /*\r
- * Perform command-line overrides on session configuration.\r
- */\r
- cmdline_run_saved(&cfg);\r
-\r
- /*\r
- * Apply subsystem status.\r
- */\r
- if (use_subsystem)\r
- cfg.ssh_subsys = TRUE;\r
-\r
- /*\r
- * Trim a colon suffix off the hostname if it's there.\r
- */\r
- cfg.host[strcspn(cfg.host, ":")] = '\0';\r
-\r
- /*\r
- * Remove any remaining whitespace from the hostname.\r
- */\r
- {\r
- int p1 = 0, p2 = 0;\r
- while (cfg.host[p2] != '\0') {\r
- if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {\r
- cfg.host[p1] = cfg.host[p2];\r
- p1++;\r
- }\r
- p2++;\r
- }\r
- cfg.host[p1] = '\0';\r
- }\r
-\r
- if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd && !*cfg.ssh_nc_host)\r
- flags |= FLAG_INTERACTIVE;\r
-\r
- /*\r
- * Select protocol. This is farmed out into a table in a\r
- * separate file to enable an ssh-free variant.\r
- */\r
- back = backend_from_proto(cfg.protocol);\r
- if (back == NULL) {\r
- fprintf(stderr,\r
- "Internal fault: Unsupported protocol found\n");\r
- return 1;\r
- }\r
-\r
- /*\r
- * Select port.\r
- */\r
- if (portnumber != -1)\r
- cfg.port = portnumber;\r
-\r
- /*\r
- * Set up the pipe we'll use to tell us about SIGWINCH.\r
- */\r
- if (pipe(signalpipe) < 0) {\r
- perror("pipe");\r
- exit(1);\r
- }\r
- putty_signal(SIGWINCH, sigwinch);\r
-\r
- sk_init();\r
- uxsel_init();\r
-\r
- /*\r
- * Unix Plink doesn't provide any way to add forwardings after the\r
- * connection is set up, so if there are none now, we can safely set\r
- * the "simple" flag.\r
- */\r
- if (cfg.protocol == PROT_SSH && !cfg.x11_forward && !cfg.agentfwd &&\r
- cfg.portfwd[0] == '\0' && cfg.portfwd[1] == '\0')\r
- cfg.ssh_simple = TRUE;\r
- /*\r
- * Start up the connection.\r
- */\r
- logctx = log_init(NULL, &cfg);\r
- console_provide_logctx(logctx);\r
- {\r
- const char *error;\r
- char *realhost;\r
- /* nodelay is only useful if stdin is a terminal device */\r
- int nodelay = cfg.tcp_nodelay && isatty(0);\r
-\r
- error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port,\r
- &realhost, nodelay, cfg.tcp_keepalives);\r
- if (error) {\r
- fprintf(stderr, "Unable to open connection:\n%s\n", error);\r
- return 1;\r
- }\r
- back->provide_logctx(backhandle, logctx);\r
- ldisc_create(&cfg, NULL, back, backhandle, NULL);\r
- sfree(realhost);\r
- }\r
- connopen = 1;\r
-\r
- /*\r
- * Set up the initial console mode. We don't care if this call\r
- * fails, because we know we aren't necessarily running in a\r
- * console.\r
- */\r
- local_tty = (tcgetattr(STDIN_FILENO, &orig_termios) == 0);\r
- atexit(cleanup_termios);\r
- ldisc_update(NULL, 1, 1);\r
- sending = FALSE;\r
- now = GETTICKCOUNT();\r
-\r
- while (1) {\r
- fd_set rset, wset, xset;\r
- int maxfd;\r
- int rwx;\r
- int ret;\r
-\r
- FD_ZERO(&rset);\r
- FD_ZERO(&wset);\r
- FD_ZERO(&xset);\r
- maxfd = 0;\r
-\r
- FD_SET_MAX(signalpipe[0], maxfd, rset);\r
-\r
- if (connopen && !sending &&\r
- back->connected(backhandle) &&\r
- back->sendok(backhandle) &&\r
- back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {\r
- /* If we're OK to send, then try to read from stdin. */\r
- FD_SET_MAX(STDIN_FILENO, maxfd, rset);\r
- }\r
-\r
- if (bufchain_size(&stdout_data) > 0) {\r
- /* If we have data for stdout, try to write to stdout. */\r
- FD_SET_MAX(STDOUT_FILENO, maxfd, wset);\r
- }\r
-\r
- if (bufchain_size(&stderr_data) > 0) {\r
- /* If we have data for stderr, try to write to stderr. */\r
- FD_SET_MAX(STDERR_FILENO, maxfd, wset);\r
- }\r
-\r
- /* Count the currently active fds. */\r
- i = 0;\r
- for (fd = first_fd(&fdstate, &rwx); fd >= 0;\r
- fd = next_fd(&fdstate, &rwx)) i++;\r
-\r
- /* Expand the fdlist buffer if necessary. */\r
- if (i > fdsize) {\r
- fdsize = i + 16;\r
- fdlist = sresize(fdlist, fdsize, int);\r
- }\r
-\r
- /*\r
- * Add all currently open fds to the select sets, and store\r
- * them in fdlist as well.\r
- */\r
- fdcount = 0;\r
- for (fd = first_fd(&fdstate, &rwx); fd >= 0;\r
- fd = next_fd(&fdstate, &rwx)) {\r
- fdlist[fdcount++] = fd;\r
- if (rwx & 1)\r
- FD_SET_MAX(fd, maxfd, rset);\r
- if (rwx & 2)\r
- FD_SET_MAX(fd, maxfd, wset);\r
- if (rwx & 4)\r
- FD_SET_MAX(fd, maxfd, xset);\r
- }\r
-\r
- do {\r
- long next, ticks;\r
- struct timeval tv, *ptv;\r
-\r
- if (run_timers(now, &next)) {\r
- ticks = next - GETTICKCOUNT();\r
- if (ticks < 0) ticks = 0; /* just in case */\r
- tv.tv_sec = ticks / 1000;\r
- tv.tv_usec = ticks % 1000 * 1000;\r
- ptv = &tv;\r
- } else {\r
- ptv = NULL;\r
- }\r
- ret = select(maxfd, &rset, &wset, &xset, ptv);\r
- if (ret == 0)\r
- now = next;\r
- else {\r
- long newnow = GETTICKCOUNT();\r
- /*\r
- * Check to see whether the system clock has\r
- * changed massively during the select.\r
- */\r
- if (newnow - now < 0 || newnow - now > next - now) {\r
- /*\r
- * If so, look at the elapsed time in the\r
- * select and use it to compute a new\r
- * tickcount_offset.\r
- */\r
- long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;\r
- /* So we'd like GETTICKCOUNT to have returned othernow,\r
- * but instead it return newnow. Hence ... */\r
- tickcount_offset += othernow - newnow;\r
- now = othernow;\r
- } else {\r
- now = newnow;\r
- }\r
- }\r
- } while (ret < 0 && errno == EINTR);\r
-\r
- if (ret < 0) {\r
- perror("select");\r
- exit(1);\r
- }\r
-\r
- for (i = 0; i < fdcount; i++) {\r
- fd = fdlist[i];\r
- /*\r
- * We must process exceptional notifications before\r
- * ordinary readability ones, or we may go straight\r
- * past the urgent marker.\r
- */\r
- if (FD_ISSET(fd, &xset))\r
- select_result(fd, 4);\r
- if (FD_ISSET(fd, &rset))\r
- select_result(fd, 1);\r
- if (FD_ISSET(fd, &wset))\r
- select_result(fd, 2);\r
- }\r
-\r
- if (FD_ISSET(signalpipe[0], &rset)) {\r
- char c[1];\r
- struct winsize size;\r
- if (read(signalpipe[0], c, 1) <= 0)\r
- /* ignore error */;\r
- /* ignore its value; it'll be `x' */\r
- if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0)\r
- back->size(backhandle, size.ws_col, size.ws_row);\r
- }\r
-\r
- if (FD_ISSET(STDIN_FILENO, &rset)) {\r
- char buf[4096];\r
- int ret;\r
-\r
- if (connopen && back->connected(backhandle)) {\r
- ret = read(STDIN_FILENO, buf, sizeof(buf));\r
- if (ret < 0) {\r
- perror("stdin: read");\r
- exit(1);\r
- } else if (ret == 0) {\r
- back->special(backhandle, TS_EOF);\r
- sending = FALSE; /* send nothing further after this */\r
- } else {\r
- if (local_tty)\r
- from_tty(buf, ret);\r
- else\r
- back->send(backhandle, buf, ret);\r
- }\r
- }\r
- }\r
-\r
- if (FD_ISSET(STDOUT_FILENO, &wset)) {\r
- back->unthrottle(backhandle, try_output(FALSE));\r
- }\r
-\r
- if (FD_ISSET(STDERR_FILENO, &wset)) {\r
- back->unthrottle(backhandle, try_output(TRUE));\r
- }\r
-\r
- if ((!connopen || !back->connected(backhandle)) &&\r
- bufchain_size(&stdout_data) == 0 &&\r
- bufchain_size(&stderr_data) == 0)\r
- break; /* we closed the connection */\r
- }\r
- exitcode = back->exitcode(backhandle);\r
- if (exitcode < 0) {\r
- fprintf(stderr, "Remote process exit code unavailable\n");\r
- exitcode = 1; /* this is an error condition */\r
- }\r
- cleanup_exit(exitcode);\r
- return exitcode; /* shouldn't happen, but placates gcc */\r
-}\r