X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/39934deb5202149f98198c111a35c21cb4d0d0f8..5e60ed724b574699b6101c5500466af23c0a5aea:/unix/uxplink.c diff --git a/unix/uxplink.c b/unix/uxplink.c index 6f17b5a2..2505cb2a 100644 --- a/unix/uxplink.c +++ b/unix/uxplink.c @@ -13,6 +13,7 @@ #include #include #include +#include #define PUTTY_DO_GLOBALS /* actually _define_ globals */ #include "putty.h" @@ -62,7 +63,8 @@ void cmdline_error(char *p, ...) exit(1); } -struct termios orig_termios; +static int local_tty = 0; /* do we have a local tty? */ +static struct termios orig_termios; static Backend *back; static void *backhandle; @@ -121,6 +123,8 @@ void ldisc_update(void *frontend, int echo, int edit) /* Update stdin read mode to reflect changes in line discipline. */ struct termios mode; + if (!local_tty) return; + mode = orig_termios; if (echo) @@ -128,17 +132,211 @@ void ldisc_update(void *frontend, int echo, int edit) else mode.c_lflag &= ~ECHO; - if (edit) + if (edit) { + mode.c_iflag |= ICRNL; mode.c_lflag |= ISIG | ICANON; - else + mode.c_oflag |= OPOST; + } else { + mode.c_iflag &= ~ICRNL; mode.c_lflag &= ~(ISIG | ICANON); + mode.c_oflag &= ~OPOST; + /* Solaris sets these to unhelpful values */ + mode.c_cc[VMIN] = 1; + mode.c_cc[VTIME] = 0; + /* FIXME: perhaps what we do with IXON/IXOFF should be an + * argument to ldisc_update(), to allow implementation of SSH-2 + * "xon-xoff" and Rlogin's equivalent? */ + mode.c_iflag &= ~IXON; + mode.c_iflag &= ~IXOFF; + } tcsetattr(0, TCSANOW, &mode); } +/* Helper function to extract a special character from a termios. */ +static char *get_ttychar(struct termios *t, int index) +{ + cc_t c = t->c_cc[index]; +#if defined(_POSIX_VDISABLE) + if (c == _POSIX_VDISABLE) + return dupprintf(""); +#endif + return dupprintf("^<%d>", c); +} + +char *get_ttymode(void *frontend, const char *mode) +{ + /* + * Propagate appropriate terminal modes from the local terminal, + * if any. + */ + if (!local_tty) return NULL; + +#define GET_CHAR(ourname, uxname) \ + do { \ + if (strcmp(mode, ourname) == 0) \ + return get_ttychar(&orig_termios, uxname); \ + } while(0) +#define GET_BOOL(ourname, uxname, uxmemb, transform) \ + do { \ + if (strcmp(mode, ourname) == 0) { \ + int b = (orig_termios.uxmemb & uxname) != 0; \ + transform; \ + return dupprintf("%d", b); \ + } \ + } while (0) + + /* + * Modes that want to be the same on all terminal devices involved. + */ + /* All the special characters supported by SSH */ +#if defined(VINTR) + GET_CHAR("INTR", VINTR); +#endif +#if defined(VQUIT) + GET_CHAR("QUIT", VQUIT); +#endif +#if defined(VERASE) + GET_CHAR("ERASE", VERASE); +#endif +#if defined(VKILL) + GET_CHAR("KILL", VKILL); +#endif +#if defined(VEOF) + GET_CHAR("EOF", VEOF); +#endif +#if defined(VEOL) + GET_CHAR("EOL", VEOL); +#endif +#if defined(VEOL2) + GET_CHAR("EOL2", VEOL2); +#endif +#if defined(VSTART) + GET_CHAR("START", VSTART); +#endif +#if defined(VSTOP) + GET_CHAR("STOP", VSTOP); +#endif +#if defined(VSUSP) + GET_CHAR("SUSP", VSUSP); +#endif +#if defined(VDSUSP) + GET_CHAR("DSUSP", VDSUSP); +#endif +#if defined(VREPRINT) + GET_CHAR("REPRINT", VREPRINT); +#endif +#if defined(VWERASE) + GET_CHAR("WERASE", VWERASE); +#endif +#if defined(VLNEXT) + GET_CHAR("LNEXT", VLNEXT); +#endif +#if defined(VFLUSH) + GET_CHAR("FLUSH", VFLUSH); +#endif +#if defined(VSWTCH) + GET_CHAR("SWTCH", VSWTCH); +#endif +#if defined(VSTATUS) + GET_CHAR("STATUS", VSTATUS); +#endif +#if defined(VDISCARD) + GET_CHAR("DISCARD", VDISCARD); +#endif + /* Modes that "configure" other major modes. These should probably be + * considered as user preferences. */ + /* Configuration of ICANON */ +#if defined(ECHOK) + GET_BOOL("ECHOK", ECHOK, c_lflag, ); +#endif +#if defined(ECHOKE) + GET_BOOL("ECHOKE", ECHOKE, c_lflag, ); +#endif +#if defined(ECHOE) + GET_BOOL("ECHOE", ECHOE, c_lflag, ); +#endif +#if defined(ECHONL) + GET_BOOL("ECHONL", ECHONL, c_lflag, ); +#endif +#if defined(XCASE) + GET_BOOL("XCASE", XCASE, c_lflag, ); +#endif + /* Configuration of ECHO */ +#if defined(ECHOCTL) + GET_BOOL("ECHOCTL", ECHOCTL, c_lflag, ); +#endif + /* Configuration of IXON/IXOFF */ +#if defined(IXANY) + GET_BOOL("IXANY", IXANY, c_iflag, ); +#endif + /* Configuration of OPOST */ +#if defined(OLCUC) + GET_BOOL("OLCUC", OLCUC, c_oflag, ); +#endif +#if defined(ONLCR) + GET_BOOL("ONLCR", ONLCR, c_oflag, ); +#endif +#if defined(OCRNL) + GET_BOOL("OCRNL", OCRNL, c_oflag, ); +#endif +#if defined(ONOCR) + GET_BOOL("ONOCR", ONOCR, c_oflag, ); +#endif +#if defined(ONLCR) + GET_BOOL("ONLRET", ONLRET, c_oflag, ); +#endif + + /* + * Modes that want to be set in only one place, and that we have + * squashed locally. + */ +#if defined(ISIG) + GET_BOOL("ISIG", ISIG, c_lflag, ); +#endif +#if defined(ICANON) + GET_BOOL("ICANON", ICANON, c_lflag, ); +#endif +#if defined(ECHO) + GET_BOOL("ECHO", ECHO, c_lflag, ); +#endif +#if defined(IXON) + GET_BOOL("IXON", IXON, c_iflag, ); +#endif +#if defined(IXOFF) + GET_BOOL("IXOFF", IXOFF, c_iflag, ); +#endif +#if defined(OPOST) + GET_BOOL("OPOST", OPOST, c_oflag, ); +#endif + + /* + * We do not propagate the following modes: + * - Parity/serial settings, which are a local affair and don't + * make sense propagated over SSH's 8-bit byte-stream. + * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD + * - Things that want to be enabled in one place that we don't + * squash locally. + * IUCLC + * - Status bits. + * PENDIN + * - Things I don't know what to do with. (FIXME) + * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN + * INLCR IGNCR ICRNL + */ + +#undef GET_CHAR +#undef GET_BOOL + + /* Fall through to here for unrecognised names, or ones that are + * unsupported on this platform */ + return NULL; +} + void cleanup_termios(void) { - tcsetattr(0, TCSANOW, &orig_termios); + if (local_tty) + tcsetattr(0, TCSANOW, &orig_termios); } bufchain stdout_data, stderr_data; @@ -206,14 +404,14 @@ static void usage(void) printf("Usage: plink [options] [user@]host [command]\n"); printf(" (\"host\" can also be a PuTTY saved session name)\n"); printf("Options:\n"); - printf(" -V print version information\n"); + printf(" -V print version information and exit\n"); + printf(" -pgpfp print PGP key fingerprints and exit\n"); printf(" -v show verbose messages\n"); printf(" -load sessname Load settings from saved session\n"); printf(" -ssh -telnet -rlogin -raw\n"); printf(" force use of a particular protocol\n"); printf(" -P port connect to specified port\n"); printf(" -l user connect with specified username\n"); - printf(" -m file read remote command(s) from file\n"); printf(" -batch disable all interactive prompts\n"); printf("The following options only apply to SSH connections:\n"); printf(" -pw passw login with specified password\n"); @@ -227,8 +425,10 @@ static void usage(void) printf(" -A -a enable / disable agent forwarding\n"); printf(" -t -T enable / disable pty allocation\n"); printf(" -1 -2 force use of particular protocol version\n"); + printf(" -4 -6 force use of IPv4 or IPv6\n"); printf(" -C enable compression\n"); printf(" -i key private key file for authentication\n"); + printf(" -m file read remote command(s) from file\n"); printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); printf(" -N don't start a shell/command (SSH-2 only)\n"); exit(1); @@ -311,6 +511,9 @@ int main(int argc, char **argv) use_subsystem = 1; } else if (!strcmp(p, "-V")) { version(); + } else if (!strcmp(p, "-pgpfp")) { + pgp_fingerprints(); + exit(1); } else if (!strcmp(p, "-o")) { if (argc <= 1) { fprintf(stderr, @@ -409,8 +612,6 @@ int main(int argc, char **argv) cfg.port = default_port; } else { cfg = cfg2; - /* Ick: patch up internal pointer after copy */ - cfg.remote_cmd_ptr = cfg.remote_cmd; } } @@ -513,7 +714,7 @@ int main(int argc, char **argv) cfg.host[p1] = '\0'; } - if (!*cfg.remote_cmd_ptr) + if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd) flags |= FLAG_INTERACTIVE; /* @@ -581,7 +782,7 @@ int main(int argc, char **argv) * fails, because we know we aren't necessarily running in a * console. */ - tcgetattr(0, &orig_termios); + local_tty = (tcgetattr(0, &orig_termios) == 0); atexit(cleanup_termios); ldisc_update(NULL, 1, 1); sending = FALSE; @@ -661,8 +862,27 @@ int main(int argc, char **argv) ret = select(maxfd, &rset, &wset, &xset, ptv); if (ret == 0) now = next; - else - now = GETTICKCOUNT(); + else { + long newnow = GETTICKCOUNT(); + /* + * Check to see whether the system clock has + * changed massively during the select. + */ + if (newnow - now < 0 || newnow - now > next - now) { + /* + * If so, look at the elapsed time in the + * select and use it to compute a new + * tickcount_offset. + */ + long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000; + /* So we'd like GETTICKCOUNT to have returned othernow, + * but instead it return newnow. Hence ... */ + tickcount_offset += othernow - newnow; + now = othernow; + } else { + now = newnow; + } + } } while (ret < 0 && errno == EINTR); if (ret < 0) {