Post-release destabilisation! Completely remove the struct type
[u/mdw/putty] / ssh.c
diff --git a/ssh.c b/ssh.c
index e1740f1..e3b8a33 100644 (file)
--- a/ssh.c
+++ b/ssh.c
@@ -13,6 +13,7 @@
 #include "tree234.h"
 #include "ssh.h"
 #ifndef NO_GSSAPI
+#include "sshgssc.h"
 #include "sshgss.h"
 #endif
 
@@ -194,6 +195,7 @@ static const char *const ssh2_disconnect_reasons[] = {
 #define BUG_SSH2_REKEY                           64
 #define BUG_SSH2_PK_SESSIONID                   128
 #define BUG_SSH2_MAXPKT                                256
+#define BUG_CHOKES_ON_SSH2_IGNORE               512
 
 /*
  * Codes for terminal modes.
@@ -542,7 +544,7 @@ static int ssh_comp_none_disable(void *handle)
     return 0;
 }
 const static struct ssh_compress ssh_comp_none = {
-    "none",
+    "none", NULL,
     ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block,
     ssh_comp_none_init, ssh_comp_none_cleanup, ssh_comp_none_block,
     ssh_comp_none_disable, NULL
@@ -588,6 +590,17 @@ struct ssh_channel {
      * A channel is completely finished with when all four bits are set.
      */
     int closes;
+
+    /*
+     * This flag indicates that a close is pending on the outgoing
+     * side of the channel: that is, wherever we're getting the data
+     * for this channel has sent us some data followed by EOF. We
+     * can't actually close the channel until we've finished sending
+     * the data, so we set this flag instead to remind us to
+     * initiate the closing process once our buffer is clear.
+     */
+    int pending_close;
+
     /*
      * True if this channel is causing the underlying connection to be
      * throttled.
@@ -871,12 +884,26 @@ struct ssh_tag {
     struct Packet *(*s_rdpkt) (Ssh ssh, unsigned char **data, int *datalen);
 
     /*
-     * We maintain a full _copy_ of a Config structure here, not
-     * merely a pointer to it. That way, when we're passed a new
-     * one for reconfiguration, we can check the differences and
-     * potentially reconfigure port forwardings etc in mid-session.
+     * We maintain our own copy of a Conf structure here. That way,
+     * when we're passed a new one for reconfiguration, we can check
+     * the differences and potentially reconfigure port forwardings
+     * etc in mid-session.
+     */
+    Conf *conf;
+
+    /*
+     * Values cached out of conf so as to avoid the tree234 lookup
+     * cost every time they're used.
+     */
+    int logomitdata;
+
+    /*
+     * Dynamically allocated username string created during SSH
+     * login. Stored in here rather than in the coroutine state so
+     * that it'll be reliably freed if we shut down the SSH session
+     * at some unexpected moment.
      */
-    Config cfg;
+    char *username;
 
     /*
      * Used to transfer data back from async callbacks.
@@ -928,6 +955,13 @@ struct ssh_tag {
      * Fully qualified host name, which we need if doing GSSAPI.
      */
     char *fullhostname;
+
+#ifndef NO_GSSAPI
+    /*
+     * GSSAPI libraries for this session.
+     */
+    struct ssh_gss_liblist *gsslibs;
+#endif
 };
 
 #define logevent(s) logevent(ssh->frontend, s)
@@ -958,13 +992,13 @@ static void logeventf(Ssh ssh, const char *fmt, ...)
 
 static void dont_log_password(Ssh ssh, struct Packet *pkt, int blanktype)
 {
-    if (ssh->cfg.logomitpass)
+    if (conf_get_int(ssh->conf, CONF_logomitpass))
        pkt->logmode = blanktype;
 }
 
 static void dont_log_data(Ssh ssh, struct Packet *pkt, int blanktype)
 {
-    if (ssh->cfg.logomitdata)
+    if (ssh->logomitdata)
        pkt->logmode = blanktype;
 }
 
@@ -973,26 +1007,27 @@ static void end_log_omission(Ssh ssh, struct Packet *pkt)
     pkt->logmode = PKTLOG_EMIT;
 }
 
-/* Helper function for common bits of parsing cfg.ttymodes. */
-static void parse_ttymodes(Ssh ssh, char *modes,
+/* Helper function for common bits of parsing ttymodes. */
+static void parse_ttymodes(Ssh ssh,
                           void (*do_mode)(void *data, char *mode, char *val),
                           void *data)
 {
-    while (*modes) {
-       char *t = strchr(modes, '\t');
-       char *m = snewn(t-modes+1, char);
-       char *val;
-       strncpy(m, modes, t-modes);
-       m[t-modes] = '\0';
-       if (*(t+1) == 'A')
-           val = get_ttymode(ssh->frontend, m);
+    char *key, *val;
+
+    for (val = conf_get_str_strs(ssh->conf, CONF_ttymodes, NULL, &key);
+        val != NULL;
+        val = conf_get_str_strs(ssh->conf, CONF_ttymodes, key, &key)) {
+       /*
+        * val[0] is either 'V', indicating that an explicit value
+        * follows it, or 'A' indicating that we should pass the
+        * value through from the local environment via get_ttymode.
+        */
+       if (val[0] == 'A')
+           val = get_ttymode(ssh->frontend, key);
        else
-           val = dupstr(t+2);
+           val++;                     /* skip the 'V' */
        if (val)
-           do_mode(data, m, val);
-       sfree(m);
-       sfree(val);
-       modes += strlen(modes) + 1;
+           do_mode(data, key, val);
     }
 }
 
@@ -1280,7 +1315,7 @@ static struct Packet *ssh1_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
     if (ssh->logctx) {
        int nblanks = 0;
        struct logblank_t blank;
-       if (ssh->cfg.logomitdata) {
+       if (ssh->logomitdata) {
            int do_blank = FALSE, blank_prefix = 0;
            /* "Session data" packets - omit the data field */
            if ((st->pktin->type == SSH1_SMSG_STDOUT_DATA) ||
@@ -1513,7 +1548,7 @@ static struct Packet *ssh2_rdpkt(Ssh ssh, unsigned char **data, int *datalen)
     if (ssh->logctx) {
        int nblanks = 0;
        struct logblank_t blank;
-       if (ssh->cfg.logomitdata) {
+       if (ssh->logomitdata) {
            int do_blank = FALSE, blank_prefix = 0;
            /* "Session data" packets - omit the data field */
            if (st->pktin->type == SSH2_MSG_CHANNEL_DATA) {
@@ -2011,7 +2046,8 @@ static void ssh2_pkt_defer_noqueue(Ssh ssh, struct Packet *pkt, int noignore)
 {
     int len;
     if (ssh->cscipher != NULL && (ssh->cscipher->flags & SSH_CIPHER_IS_CBC) &&
-       ssh->deferred_len == 0 && !noignore) {
+       ssh->deferred_len == 0 && !noignore &&
+       !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
        /*
         * Interpose an SSH_MSG_IGNORE to ensure that user data don't
         * get encrypted with a known IV.
@@ -2141,7 +2177,8 @@ static void ssh2_pkt_send_with_padding(Ssh ssh, struct Packet *pkt,
         * unavailable, we don't do this trick at all, because we
         * gain nothing by it.)
         */
-       if (ssh->cscipher) {
+       if (ssh->cscipher &&
+           !(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
            int stringlen, i;
 
            stringlen = (256 - ssh->deferred_len);
@@ -2395,8 +2432,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
      *    with SSH1_MSG_IGNOREs -- but this string never seems to change,
      *    so we can't distinguish them.
      */
-    if (ssh->cfg.sshbug_ignore1 == FORCE_ON ||
-       (ssh->cfg.sshbug_ignore1 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_ignore1) == AUTO &&
         (!strcmp(imp, "1.2.18") || !strcmp(imp, "1.2.19") ||
          !strcmp(imp, "1.2.20") || !strcmp(imp, "1.2.21") ||
          !strcmp(imp, "1.2.22") || !strcmp(imp, "Cisco-1.25") ||
@@ -2410,8 +2447,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version has SSH-1 ignore bug");
     }
 
-    if (ssh->cfg.sshbug_plainpw1 == FORCE_ON ||
-       (ssh->cfg.sshbug_plainpw1 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_plainpw1) == AUTO &&
         (!strcmp(imp, "Cisco-1.25") || !strcmp(imp, "OSU_1.4alpha3")))) {
        /*
         * These versions need a plain password sent; they can't
@@ -2422,8 +2459,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version needs a plain SSH-1 password");
     }
 
-    if (ssh->cfg.sshbug_rsa1 == FORCE_ON ||
-       (ssh->cfg.sshbug_rsa1 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_rsa1) == AUTO &&
         (!strcmp(imp, "Cisco-1.25")))) {
        /*
         * These versions apparently have no clue whatever about
@@ -2434,8 +2471,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version can't handle SSH-1 RSA authentication");
     }
 
-    if (ssh->cfg.sshbug_hmac2 == FORCE_ON ||
-       (ssh->cfg.sshbug_hmac2 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_hmac2) == AUTO &&
         !wc_match("* VShell", imp) &&
         (wc_match("2.1.0*", imp) || wc_match("2.0.*", imp) ||
          wc_match("2.2.0*", imp) || wc_match("2.3.0*", imp) ||
@@ -2447,8 +2484,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version has SSH-2 HMAC bug");
     }
 
-    if (ssh->cfg.sshbug_derivekey2 == FORCE_ON ||
-       (ssh->cfg.sshbug_derivekey2 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_derivekey2) == AUTO &&
         !wc_match("* VShell", imp) &&
         (wc_match("2.0.0*", imp) || wc_match("2.0.10*", imp) ))) {
        /*
@@ -2460,8 +2497,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version has SSH-2 key-derivation bug");
     }
 
-    if (ssh->cfg.sshbug_rsapad2 == FORCE_ON ||
-       (ssh->cfg.sshbug_rsapad2 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_rsapad2) == AUTO &&
         (wc_match("OpenSSH_2.[5-9]*", imp) ||
          wc_match("OpenSSH_3.[0-2]*", imp)))) {
        /*
@@ -2471,8 +2508,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version has SSH-2 RSA padding bug");
     }
 
-    if (ssh->cfg.sshbug_pksessid2 == FORCE_ON ||
-       (ssh->cfg.sshbug_pksessid2 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_pksessid2) == AUTO &&
         wc_match("OpenSSH_2.[0-2]*", imp))) {
        /*
         * These versions have the SSH-2 session-ID bug in
@@ -2482,8 +2519,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version has SSH-2 public-key-session-ID bug");
     }
 
-    if (ssh->cfg.sshbug_rekey2 == FORCE_ON ||
-       (ssh->cfg.sshbug_rekey2 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_rekey2) == AUTO &&
         (wc_match("DigiSSH_2.0", imp) ||
          wc_match("OpenSSH_2.[0-4]*", imp) ||
          wc_match("OpenSSH_2.5.[0-3]*", imp) ||
@@ -2498,8 +2535,8 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        logevent("We believe remote version has SSH-2 rekey bug");
     }
 
-    if (ssh->cfg.sshbug_maxpkt2 == FORCE_ON ||
-       (ssh->cfg.sshbug_maxpkt2 == AUTO &&
+    if (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == FORCE_ON ||
+       (conf_get_int(ssh->conf, CONF_sshbug_maxpkt2) == AUTO &&
         (wc_match("1.36_sshlib GlobalSCAPE", imp) ||
           wc_match("1.36 sshlib: GlobalScape", imp)))) {
        /*
@@ -2508,6 +2545,15 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        ssh->remote_bugs |= BUG_SSH2_MAXPKT;
        logevent("We believe remote version ignores SSH-2 maximum packet size");
     }
+
+    if (conf_get_int(ssh->conf, CONF_sshbug_ignore2) == FORCE_ON) {
+       /*
+        * Servers that don't support SSH2_MSG_IGNORE. Currently,
+        * none detected automatically.
+        */
+       ssh->remote_bugs |= BUG_CHOKES_ON_SSH2_IGNORE;
+       logevent("We believe remote version has SSH-2 ignore bug");
+    }
 }
 
 /*
@@ -2643,16 +2689,16 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
     /* Anything greater or equal to "1.99" means protocol 2 is supported. */
     s->proto2 = ssh_versioncmp(s->version, "1.99") >= 0;
 
-    if (ssh->cfg.sshprot == 0 && !s->proto1) {
+    if (conf_get_int(ssh->conf, CONF_sshprot) == 0 && !s->proto1) {
        bombout(("SSH protocol version 1 required by user but not provided by server"));
        crStop(0);
     }
-    if (ssh->cfg.sshprot == 3 && !s->proto2) {
+    if (conf_get_int(ssh->conf, CONF_sshprot) == 3 && !s->proto2) {
        bombout(("SSH protocol version 2 required by user but not provided by server"));
        crStop(0);
     }
 
-    if (s->proto2 && (ssh->cfg.sshprot >= 2 || !s->proto1))
+    if (s->proto2 && (conf_get_int(ssh->conf, CONF_sshprot) >= 2 || !s->proto1))
        ssh->version = 2;
     else
        ssh->version = 1;
@@ -2660,7 +2706,7 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
     logeventf(ssh, "Using SSH protocol version %d", ssh->version);
 
     /* Send the version string, if we haven't already */
-    if (ssh->cfg.sshprot != 3)
+    if (conf_get_int(ssh->conf, CONF_sshprot) != 3)
        ssh_send_verstring(ssh, s->version);
 
     if (ssh->version == 2) {
@@ -2692,7 +2738,7 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
 
     update_specials_menu(ssh->frontend);
     ssh->state = SSH_STATE_BEFORE_SIZE;
-    ssh->pinger = pinger_new(&ssh->cfg, &ssh_backend, ssh);
+    ssh->pinger = pinger_new(ssh->conf, &ssh_backend, ssh);
 
     sfree(s->vstring);
 
@@ -2831,6 +2877,7 @@ static int ssh_do_close(Ssh ssh, int notify_exit)
                x11_close(c->u.x11.s);
                break;
              case CHAN_SOCKDATA:
+             case CHAN_SOCKDATA_DORMANT:
                pfd_close(c->u.pfd.s);
                break;
            }
@@ -2853,6 +2900,8 @@ static int ssh_do_close(Ssh ssh, int notify_exit)
            del234(ssh->portfwds, pf); /* moving next one to index 0 */
            free_portfwd(pf);
        }
+       freetree234(ssh->portfwds);
+       ssh->portfwds = NULL;
     }
 
     return ret;
@@ -2942,11 +2991,14 @@ static const char *connect_to_host(Ssh ssh, char *host, int port,
 
     SockAddr addr;
     const char *err;
-
-    if (*ssh->cfg.loghost) {
+    char *loghost;
+    int addressfamily, sshprot;
+    
+    loghost = conf_get_str(ssh->conf, CONF_loghost);
+    if (*loghost) {
        char *colon;
 
-       ssh->savedhost = dupstr(ssh->cfg.loghost);
+       ssh->savedhost = dupstr(loghost);
        ssh->savedport = 22;           /* default ssh port */
 
        /*
@@ -2971,11 +3023,11 @@ static const char *connect_to_host(Ssh ssh, char *host, int port,
     /*
      * Try to find host.
      */
+    addressfamily = conf_get_int(ssh->conf, CONF_addressfamily);
     logeventf(ssh, "Looking up host \"%s\"%s", host,
-             (ssh->cfg.addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
-              (ssh->cfg.addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : "")));
-    addr = name_lookup(host, port, realhost, &ssh->cfg,
-                      ssh->cfg.addressfamily);
+             (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
+              (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : "")));
+    addr = name_lookup(host, port, realhost, ssh->conf, addressfamily);
     if ((err = sk_addr_error(addr)) != NULL) {
        sk_addr_free(addr);
        return err;
@@ -2987,7 +3039,7 @@ static const char *connect_to_host(Ssh ssh, char *host, int port,
      */
     ssh->fn = &fn_table;
     ssh->s = new_connection(addr, *realhost, port,
-                           0, 1, nodelay, keepalive, (Plug) ssh, &ssh->cfg);
+                           0, 1, nodelay, keepalive, (Plug) ssh, ssh->conf);
     if ((err = sk_socket_error(ssh->s)) != NULL) {
        ssh->s = NULL;
        notify_remote_exit(ssh->frontend);
@@ -2998,9 +3050,10 @@ static const char *connect_to_host(Ssh ssh, char *host, int port,
      * If the SSH version number's fixed, set it now, and if it's SSH-2,
      * send the version string too.
      */
-    if (ssh->cfg.sshprot == 0)
+    sshprot = conf_get_int(ssh->conf, CONF_sshprot);
+    if (sshprot == 0)
        ssh->version = 1;
-    if (ssh->cfg.sshprot == 3) {
+    if (sshprot == 3) {
        ssh->version = 2;
        ssh_send_verstring(ssh, NULL);
     }
@@ -3008,9 +3061,9 @@ static const char *connect_to_host(Ssh ssh, char *host, int port,
     /*
      * loghost, if configured, overrides realhost.
      */
-    if (*ssh->cfg.loghost) {
+    if (*loghost) {
        sfree(*realhost);
-       *realhost = dupstr(ssh->cfg.loghost);
+       *realhost = dupstr(loghost);
     }
 
     return NULL;
@@ -3175,7 +3228,6 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
        int tis_auth_refused, ccard_auth_refused;
        unsigned char session_id[16];
        int cipher_type;
-       char username[100];
        void *publickey_blob;
        int publickey_bloblen;
        char *publickey_comment;
@@ -3192,6 +3244,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
        char *commentp;
        int commentlen;
         int dlgret;
+       Filename *keyfile;
     };
     crState(do_ssh1_login_state);
 
@@ -3331,7 +3384,8 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
        char *cipher_string = NULL;
        int i;
        for (i = 0; !cipher_chosen && i < CIPHER_MAX; i++) {
-           int next_cipher = ssh->cfg.ssh_cipherlist[i];
+           int next_cipher = conf_get_int_int(ssh->conf,
+                                              CONF_ssh_cipherlist, i);
            if (next_cipher == CIPHER_WARN) {
                /* If/when we choose a cipher, warn about it */
                warn = 1;
@@ -3446,14 +3500,13 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
 
     fflush(stdout); /* FIXME eh? */
     {
-       if (!get_remote_username(&ssh->cfg, s->username,
-                                sizeof(s->username))) {
+       if ((ssh->username = get_remote_username(ssh->conf)) == NULL) {
            int ret; /* need not be kept over crReturn */
            s->cur_prompt = new_prompts(ssh->frontend);
            s->cur_prompt->to_server = TRUE;
            s->cur_prompt->name = dupstr("SSH login name");
-           add_prompt(s->cur_prompt, dupstr("login as: "), TRUE,
-                      lenof(s->username)); 
+           /* 512 is an arbitrary upper limit on username size */
+           add_prompt(s->cur_prompt, dupstr("login as: "), TRUE, 512);
            ret = get_userpass_input(s->cur_prompt, NULL, 0);
            while (ret < 0) {
                ssh->send_ok = 1;
@@ -3469,14 +3522,13 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
                ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE);
                crStop(0);
            }
-           memcpy(s->username, s->cur_prompt->prompts[0]->result,
-                  lenof(s->username));
+           ssh->username = dupstr(s->cur_prompt->prompts[0]->result);
            free_prompts(s->cur_prompt);
        }
 
-       send_packet(ssh, SSH1_CMSG_USER, PKT_STR, s->username, PKT_END);
+       send_packet(ssh, SSH1_CMSG_USER, PKT_STR, ssh->username, PKT_END);
        {
-           char *userlog = dupprintf("Sent username \"%s\"", s->username);
+           char *userlog = dupprintf("Sent username \"%s\"", ssh->username);
            logevent(userlog);
            if (flags & FLAG_INTERACTIVE &&
                (!((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)))) {
@@ -3499,24 +3551,25 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
     /*
      * Load the public half of any configured keyfile for later use.
      */
-    if (!filename_is_null(ssh->cfg.keyfile)) {
+    s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
+    if (!filename_is_null(*s->keyfile)) {
        int keytype;
        logeventf(ssh, "Reading private key file \"%.150s\"",
-                 filename_to_str(&ssh->cfg.keyfile));
-       keytype = key_type(&ssh->cfg.keyfile);
+                 filename_to_str(s->keyfile));
+       keytype = key_type(s->keyfile);
        if (keytype == SSH_KEYTYPE_SSH1) {
            const char *error;
-           if (rsakey_pubblob(&ssh->cfg.keyfile,
+           if (rsakey_pubblob(s->keyfile,
                               &s->publickey_blob, &s->publickey_bloblen,
                               &s->publickey_comment, &error)) {
-               s->publickey_encrypted = rsakey_encrypted(&ssh->cfg.keyfile,
+               s->publickey_encrypted = rsakey_encrypted(s->keyfile,
                                                          NULL);
            } else {
                char *msgbuf;
                logeventf(ssh, "Unable to load private key (%s)", error);
                msgbuf = dupprintf("Unable to load private key file "
                                   "\"%.150s\" (%s)\r\n",
-                                  filename_to_str(&ssh->cfg.keyfile),
+                                  filename_to_str(s->keyfile),
                                   error);
                c_write_str(ssh, msgbuf);
                sfree(msgbuf);
@@ -3528,7 +3581,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
                      key_type_to_str(keytype));
            msgbuf = dupprintf("Unable to use key file \"%.150s\""
                               " (%s)\r\n",
-                              filename_to_str(&ssh->cfg.keyfile),
+                              filename_to_str(s->keyfile),
                               key_type_to_str(keytype));
            c_write_str(ssh, msgbuf);
            sfree(msgbuf);
@@ -3540,7 +3593,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
     while (pktin->type == SSH1_SMSG_FAILURE) {
        s->pwpkt_type = SSH1_CMSG_AUTH_PASSWORD;
 
-       if (ssh->cfg.tryagent && agent_exists() && !s->tried_agent) {
+       if (conf_get_int(ssh->conf, CONF_tryagent) && agent_exists() && !s->tried_agent) {
            /*
             * Attempt RSA authentication using Pageant.
             */
@@ -3710,7 +3763,9 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
                sfree(s->response);
                if (s->publickey_blob && !s->tried_publickey)
                    logevent("Configured key file not in Pageant");
-           }
+           } else {
+                logevent("Failed to get reply from Pageant");
+            }
            if (s->authed)
                break;
        }
@@ -3722,8 +3777,9 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
            int got_passphrase; /* need not be kept over crReturn */
            if (flags & FLAG_VERBOSE)
                c_write_str(ssh, "Trying public key authentication.\r\n");
+           s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
            logeventf(ssh, "Trying public key \"%s\"",
-                     filename_to_str(&ssh->cfg.keyfile));
+                     filename_to_str(s->keyfile));
            s->tried_publickey = 1;
            got_passphrase = FALSE;
            while (!got_passphrase) {
@@ -3765,7 +3821,8 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
                /*
                 * Try decrypting key with passphrase.
                 */
-               ret = loadrsakey(&ssh->cfg.keyfile, &s->key, passphrase,
+               s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
+               ret = loadrsakey(s->keyfile, &s->key, passphrase,
                                 &error);
                if (passphrase) {
                    memset(passphrase, 0, strlen(passphrase));
@@ -3776,7 +3833,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
                    got_passphrase = TRUE;
                } else if (ret == 0) {
                    c_write_str(ssh, "Couldn't load private key from ");
-                   c_write_str(ssh, filename_to_str(&ssh->cfg.keyfile));
+                   c_write_str(ssh, filename_to_str(s->keyfile));
                    c_write_str(ssh, " (");
                    c_write_str(ssh, error);
                    c_write_str(ssh, ").\r\n");
@@ -3859,7 +3916,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
         */
        s->cur_prompt = new_prompts(ssh->frontend);
 
-       if (ssh->cfg.try_tis_auth &&
+       if (conf_get_int(ssh->conf, CONF_try_tis_auth) &&
            (s->supported_auths_mask & (1 << SSH1_AUTH_TIS)) &&
            !s->tis_auth_refused) {
            s->pwpkt_type = SSH1_CMSG_AUTH_TIS_RESPONSE;
@@ -3902,7 +3959,7 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
                sfree(instr_suf);
            }
        }
-       if (ssh->cfg.try_tis_auth &&
+       if (conf_get_int(ssh->conf, CONF_try_tis_auth) &&
            (s->supported_auths_mask & (1 << SSH1_AUTH_CCARD)) &&
            !s->ccard_auth_refused) {
            s->pwpkt_type = SSH1_CMSG_AUTH_CCARD_RESPONSE;
@@ -3952,8 +4009,8 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
            }
            s->cur_prompt->to_server = TRUE;
            s->cur_prompt->name = dupstr("SSH password");
-           add_prompt(s->cur_prompt, dupprintf("%.90s@%.90s's password: ",
-                                               s->username, ssh->savedhost),
+           add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
+                                               ssh->username, ssh->savedhost),
                       FALSE, SSH_MAX_PASSWORD_LEN);
        }
 
@@ -4138,7 +4195,7 @@ void sshfwd_close(struct ssh_channel *c)
     if (ssh->state == SSH_STATE_CLOSED)
        return;
 
-    if (c && !c->closes) {
+    if (!c->closes) {
        /*
         * If halfopen is true, we have sent
         * CHANNEL_OPEN for this channel, but it hasn't even been
@@ -4150,14 +4207,42 @@ void sshfwd_close(struct ssh_channel *c)
            if (ssh->version == 1) {
                send_packet(ssh, SSH1_MSG_CHANNEL_CLOSE, PKT_INT, c->remoteid,
                            PKT_END);
+               c->closes = 1;                 /* sent MSG_CLOSE */
            } else {
-               struct Packet *pktout;
-               pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
-               ssh2_pkt_adduint32(pktout, c->remoteid);
-               ssh2_pkt_send(ssh, pktout);
+               int bytes_to_send = bufchain_size(&c->v.v2.outbuffer);
+               if (bytes_to_send > 0) {
+                   /*
+                    * If we still have unsent data in our outgoing
+                    * buffer for this channel, we can't actually
+                    * initiate a close operation yet or that data
+                    * will be lost. Instead, set the pending_close
+                    * flag so that when we do clear the buffer
+                    * we'll start closing the channel.
+                    */
+                   char logmsg[160] = {'\0'};
+                   sprintf(
+                           logmsg,
+                           "Forwarded port pending to be closed : "
+                           "%d bytes remaining",
+                           bytes_to_send);
+                   logevent(logmsg);
+
+                   c->pending_close = TRUE;
+               } else {
+                   /*
+                    * No locally buffered data, so we can send the
+                    * close message immediately.
+                    */
+                   struct Packet *pktout;
+                   pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
+                   ssh2_pkt_adduint32(pktout, c->remoteid);
+                   ssh2_pkt_send(ssh, pktout);
+                   c->closes = 1;                     /* sent MSG_CLOSE */
+                   logevent("Nothing left to send, closing channel");
+               }
            }
        }
-       c->closes = 1;                 /* sent MSG_CLOSE */
+
        if (c->type == CHAN_X11) {
            c->u.x11.s = NULL;
            logevent("Forwarded X11 connection terminated");
@@ -4296,15 +4381,16 @@ static void ssh_rportfwd_succfail(Ssh ssh, struct Packet *pktin, void *ctx)
 
        rpf = del234(ssh->rportfwds, pf);
        assert(rpf == pf);
+       pf->pfrec->remote = NULL;
        free_rportfwd(pf);
     }
 }
 
-static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
+static void ssh_setup_portfwd(Ssh ssh, Conf *conf)
 {
-    const char *portfwd_strptr = cfg->portfwd;
     struct ssh_portfwd *epf;
     int i;
+    char *key, *val;
 
     if (!ssh->portfwds) {
        ssh->portfwds = newtree234(ssh_portcmp);
@@ -4322,80 +4408,34 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
            epf->status = DESTROY;
     }
 
-    while (*portfwd_strptr) {
+    for (val = conf_get_str_strs(conf, CONF_portfwd, NULL, &key);
+        val != NULL;
+        val = conf_get_str_strs(conf, CONF_portfwd, key, &key)) {
+       char *kp, *kp2, *vp, *vp2;
        char address_family, type;
        int sport,dport,sserv,dserv;
-       char sports[256], dports[256], saddr[256], host[256];
-       int n;
+       char *sports, *dports, *saddr, *host;
+
+       kp = key;
 
        address_family = 'A';
        type = 'L';
-       if (*portfwd_strptr == 'A' ||
-           *portfwd_strptr == '4' ||
-           *portfwd_strptr == '6')
-           address_family = *portfwd_strptr++;
-       if (*portfwd_strptr == 'L' ||
-           *portfwd_strptr == 'R' ||
-           *portfwd_strptr == 'D')
-           type = *portfwd_strptr++;
-
-       saddr[0] = '\0';
-
-       n = 0;
-       while (*portfwd_strptr && *portfwd_strptr != '\t') {
-           if (*portfwd_strptr == ':') {
-               /*
-                * We've seen a colon in the middle of the
-                * source port number. This means that
-                * everything we've seen until now is the
-                * source _address_, so we'll move it into
-                * saddr and start sports from the beginning
-                * again.
-                */
-               portfwd_strptr++;
-               sports[n] = '\0';
-               if (ssh->version == 1 && type == 'R') {
-                   logeventf(ssh, "SSH-1 cannot handle remote source address "
-                             "spec \"%s\"; ignoring", sports);
-               } else
-                   strcpy(saddr, sports);
-               n = 0;
-           }
-           if (n < lenof(sports)-1) sports[n++] = *portfwd_strptr++;
-       }
-       sports[n] = 0;
-       if (type != 'D') {
-           if (*portfwd_strptr == '\t')
-               portfwd_strptr++;
-           n = 0;
-           while (*portfwd_strptr && *portfwd_strptr != ':') {
-               if (n < lenof(host)-1) host[n++] = *portfwd_strptr++;
-           }
-           host[n] = 0;
-           if (*portfwd_strptr == ':')
-               portfwd_strptr++;
-           n = 0;
-           while (*portfwd_strptr) {
-               if (n < lenof(dports)-1) dports[n++] = *portfwd_strptr++;
-           }
-           dports[n] = 0;
-           portfwd_strptr++;
-           dport = atoi(dports);
-           dserv = 0;
-           if (dport == 0) {
-               dserv = 1;
-               dport = net_service_lookup(dports);
-               if (!dport) {
-                   logeventf(ssh, "Service lookup failed for destination"
-                             " port \"%s\"", dports);
-               }
-           }
+       if (*kp == 'A' || *kp == '4' || *kp == '6')
+           address_family = *kp++;
+       if (*kp == 'L' || *kp == 'R')
+           type = *kp++;
+
+       if ((kp2 = strchr(kp, ':')) != NULL) {
+           /*
+            * There's a colon in the middle of the source port
+            * string, which means that the part before it is
+            * actually a source address.
+            */
+           saddr = dupprintf("%.*s", (int)(kp2 - kp), kp);
+           sports = kp2+1;
        } else {
-           while (*portfwd_strptr) portfwd_strptr++;
-           host[0] = 0;
-           dports[0] = 0;
-           dport = dserv = -1;
-           portfwd_strptr++;          /* eat the NUL and move to next one */
+           saddr = NULL;
+           sports = kp;
        }
        sport = atoi(sports);
        sserv = 0;
@@ -4407,16 +4447,44 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
                          " port \"%s\"", sports);
            }
        }
+
+       if (type == 'L' && !strcmp(val, "D")) {
+            /* dynamic forwarding */
+           host = NULL;
+           dports = NULL;
+           dport = -1;
+           dserv = 0;
+            type = 'D';
+        } else {
+            /* ordinary forwarding */
+           vp = val;
+           vp2 = vp + strcspn(vp, ":");
+           host = dupprintf("%.*s", (int)(vp2 - vp), vp);
+           if (vp2)
+               vp2++;
+           dports = vp2;
+           dport = atoi(dports);
+           dserv = 0;
+           if (dport == 0) {
+               dserv = 1;
+               dport = net_service_lookup(dports);
+               if (!dport) {
+                   logeventf(ssh, "Service lookup failed for destination"
+                             " port \"%s\"", dports);
+               }
+           }
+       }
+
        if (sport && dport) {
            /* Set up a description of the source port. */
            struct ssh_portfwd *pfrec, *epfrec;
 
            pfrec = snew(struct ssh_portfwd);
            pfrec->type = type;
-           pfrec->saddr = *saddr ? dupstr(saddr) : NULL;
+           pfrec->saddr = saddr;
            pfrec->sserv = sserv ? dupstr(sports) : NULL;
            pfrec->sport = sport;
-           pfrec->daddr = *host ? dupstr(host) : NULL;
+           pfrec->daddr = host;
            pfrec->dserv = dserv ? dupstr(dports) : NULL;
            pfrec->dport = dport;
            pfrec->local = NULL;
@@ -4427,16 +4495,26 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
 
            epfrec = add234(ssh->portfwds, pfrec);
            if (epfrec != pfrec) {
+               if (epfrec->status == DESTROY) {
+                   /*
+                    * We already have a port forwarding up and running
+                    * with precisely these parameters. Hence, no need
+                    * to do anything; simply re-tag the existing one
+                    * as KEEP.
+                    */
+                   epfrec->status = KEEP;
+               }
                /*
-                * We already have a port forwarding with precisely
-                * these parameters. Hence, no need to do anything;
-                * simply tag the existing one as KEEP.
+                * Anything else indicates that there was a duplicate
+                * in our input, which we'll silently ignore.
                 */
-               epfrec->status = KEEP;
                free_portfwd(pfrec);
            } else {
                pfrec->status = CREATE;
            }
+       } else {
+           sfree(saddr);
+           sfree(host);
        }
     }
 
@@ -4465,6 +4543,8 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
            logeventf(ssh, "Cancelling %s", message);
            sfree(message);
 
+           /* epf->remote or epf->local may be NULL if setting up a
+            * forwarding failed. */
            if (epf->remote) {
                struct ssh_rportfwd *rpf = epf->remote;
                struct Packet *pktout;
@@ -4488,8 +4568,8 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
                    ssh2_pkt_addbool(pktout, 0);/* _don't_ want reply */
                    if (epf->saddr) {
                        ssh2_pkt_addstring(pktout, epf->saddr);
-                   } else if (ssh->cfg.rport_acceptall) {
-                       /* XXX: ssh->cfg.rport_acceptall may not represent
+                   } else if (conf_get_int(conf, CONF_rport_acceptall)) {
+                       /* XXX: rport_acceptall may not represent
                         * what was used to open the original connection,
                         * since it's reconfigurable. */
                        ssh2_pkt_addstring(pktout, "0.0.0.0");
@@ -4538,7 +4618,7 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
            if (epf->type == 'L') {
                const char *err = pfd_addforward(epf->daddr, epf->dport,
                                                 epf->saddr, epf->sport,
-                                                ssh, cfg,
+                                                ssh, conf,
                                                 &epf->local,
                                                 epf->addressfamily);
 
@@ -4550,7 +4630,7 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
            } else if (epf->type == 'D') {
                const char *err = pfd_addforward(NULL, -1,
                                                 epf->saddr, epf->sport,
-                                                ssh, cfg,
+                                                ssh, conf,
                                                 &epf->local,
                                                 epf->addressfamily);
 
@@ -4606,7 +4686,7 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
                        ssh2_pkt_addbool(pktout, 1);/* want reply */
                        if (epf->saddr) {
                            ssh2_pkt_addstring(pktout, epf->saddr);
-                       } else if (cfg->rport_acceptall) {
+                       } else if (conf_get_int(conf, CONF_rport_acceptall)) {
                            ssh2_pkt_addstring(pktout, "0.0.0.0");
                        } else {
                            ssh2_pkt_addstring(pktout, "127.0.0.1");
@@ -4662,7 +4742,7 @@ static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin)
        c->ssh = ssh;
 
        if (x11_init(&c->u.x11.s, ssh->x11disp, c,
-                    NULL, -1, &ssh->cfg) != NULL) {
+                    NULL, -1, ssh->conf) != NULL) {
            logevent("Opening X11 forward connection failed");
            sfree(c);
            send_packet(ssh, SSH1_MSG_CHANNEL_OPEN_FAILURE,
@@ -4674,6 +4754,7 @@ static void ssh1_smsg_x11_open(Ssh ssh, struct Packet *pktin)
            c->halfopen = FALSE;
            c->localid = alloc_channel_id(ssh);
            c->closes = 0;
+           c->pending_close = FALSE;
            c->throttling_conn = 0;
            c->type = CHAN_X11; /* identify channel type */
            add234(ssh->channels, c);
@@ -4703,6 +4784,7 @@ static void ssh1_smsg_agent_open(Ssh ssh, struct Packet *pktin)
        c->halfopen = FALSE;
        c->localid = alloc_channel_id(ssh);
        c->closes = 0;
+       c->pending_close = FALSE;
        c->throttling_conn = 0;
        c->type = CHAN_AGENT;   /* identify channel type */
        c->u.a.lensofar = 0;
@@ -4746,7 +4828,7 @@ static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin)
        logeventf(ssh, "Received remote port open request for %s:%d",
                  pf.dhost, port);
        e = pfd_newconnect(&c->u.pfd.s, pf.dhost, port,
-                          c, &ssh->cfg, pfp->pfrec->addressfamily);
+                          c, ssh->conf, pfp->pfrec->addressfamily);
        if (e != NULL) {
            logeventf(ssh, "Port open failed: %s", e);
            sfree(c);
@@ -4757,6 +4839,7 @@ static void ssh1_msg_port_open(Ssh ssh, struct Packet *pktin)
            c->halfopen = FALSE;
            c->localid = alloc_channel_id(ssh);
            c->closes = 0;
+           c->pending_close = FALSE;
            c->throttling_conn = 0;
            c->type = CHAN_SOCKDATA;    /* identify channel type */
            add234(ssh->channels, c);
@@ -4977,7 +5060,7 @@ static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,
     ssh->packet_dispatch[SSH1_MSG_CHANNEL_DATA] = ssh1_msg_channel_data;
     ssh->packet_dispatch[SSH1_SMSG_EXIT_STATUS] = ssh1_smsg_exit_status;
 
-    if (ssh->cfg.agentfwd && agent_exists()) {
+    if (conf_get_int(ssh->conf, CONF_agentfwd) && agent_exists()) {
        logevent("Requesting agent forwarding");
        send_packet(ssh, SSH1_CMSG_AGENT_REQUEST_FORWARDING, PKT_END);
        do {
@@ -4996,9 +5079,9 @@ static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,
        }
     }
 
-    if (ssh->cfg.x11_forward &&
-       (ssh->x11disp = x11_setup_display(ssh->cfg.x11_display,
-                                         ssh->cfg.x11_auth, &ssh->cfg))) {
+    if (conf_get_int(ssh->conf, CONF_x11_forward) &&
+       (ssh->x11disp = x11_setup_display(conf_get_str(ssh->conf, CONF_x11_display),
+                                         conf_get_int(ssh->conf, CONF_x11_auth), ssh->conf))) {
        logevent("Requesting X11 forwarding");
        /*
         * Note that while we blank the X authentication data here, we don't
@@ -5039,24 +5122,23 @@ static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,
        }
     }
 
-    ssh_setup_portfwd(ssh, &ssh->cfg);
+    ssh_setup_portfwd(ssh, ssh->conf);
     ssh->packet_dispatch[SSH1_MSG_PORT_OPEN] = ssh1_msg_port_open;
 
-    if (!ssh->cfg.nopty) {
+    if (!conf_get_int(ssh->conf, CONF_nopty)) {
        struct Packet *pkt;
        /* Unpick the terminal-speed string. */
        /* XXX perhaps we should allow no speeds to be sent. */
        ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */
-       sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed);
+       sscanf(conf_get_str(ssh->conf, CONF_termspeed), "%d,%d", &ssh->ospeed, &ssh->ispeed);
        /* Send the pty request. */
        pkt = ssh1_pkt_init(SSH1_CMSG_REQUEST_PTY);
-       ssh_pkt_addstring(pkt, ssh->cfg.termtype);
+       ssh_pkt_addstring(pkt, conf_get_str(ssh->conf, CONF_termtype));
        ssh_pkt_adduint32(pkt, ssh->term_height);
        ssh_pkt_adduint32(pkt, ssh->term_width);
        ssh_pkt_adduint32(pkt, 0); /* width in pixels */
        ssh_pkt_adduint32(pkt, 0); /* height in pixels */
-       parse_ttymodes(ssh, ssh->cfg.ttymodes,
-                      ssh1_send_ttymode, (void *)pkt);
+       parse_ttymodes(ssh, ssh1_send_ttymode, (void *)pkt);
        ssh_pkt_addbyte(pkt, SSH1_TTY_OP_ISPEED);
        ssh_pkt_adduint32(pkt, ssh->ispeed);
        ssh_pkt_addbyte(pkt, SSH1_TTY_OP_OSPEED);
@@ -5081,7 +5163,7 @@ static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,
        ssh->editing = ssh->echoing = 1;
     }
 
-    if (ssh->cfg.compression) {
+    if (conf_get_int(ssh->conf, CONF_compression)) {
        send_packet(ssh, SSH1_CMSG_REQUEST_COMPRESSION, PKT_INT, 6, PKT_END);
        do {
            crReturnV;
@@ -5109,12 +5191,11 @@ static void do_ssh1_connection(Ssh ssh, unsigned char *in, int inlen,
      * exists, we fall straight back to that.
      */
     {
-       char *cmd = ssh->cfg.remote_cmd_ptr;
-
-       if (!cmd) cmd = ssh->cfg.remote_cmd;
+       char *cmd = conf_get_str(ssh->conf, CONF_remote_cmd);
        
-       if (ssh->cfg.ssh_subsys && ssh->cfg.remote_cmd_ptr2) {
-           cmd = ssh->cfg.remote_cmd_ptr2;
+       if (conf_get_int(ssh->conf, CONF_ssh_subsys) &&
+           conf_get_str(ssh->conf, CONF_remote_cmd2)) {
+           cmd = conf_get_str(ssh->conf, CONF_remote_cmd2);
            ssh->fallback_cmd = TRUE;
        }
        if (*cmd)
@@ -5347,6 +5428,8 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
        int n_preferred_ciphers;
        const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX];
        const struct ssh_compress *preferred_comp;
+       int userauth_succeeded;     /* for delayed compression */
+       int pending_compression;
        int got_session_id, activated_authconn;
        struct Packet *pktout;
         int dlgret;
@@ -5362,6 +5445,8 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
     s->cscomp_tobe = s->sccomp_tobe = NULL;
 
     s->got_session_id = s->activated_authconn = FALSE;
+    s->userauth_succeeded = FALSE;
+    s->pending_compression = FALSE;
 
     /*
      * Be prepared to work around the buggy MAC problem.
@@ -5381,7 +5466,7 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
         */
        s->n_preferred_kex = 0;
        for (i = 0; i < KEX_MAX; i++) {
-           switch (ssh->cfg.ssh_kexlist[i]) {
+           switch (conf_get_int_int(ssh->conf, CONF_ssh_kexlist, i)) {
              case KEX_DHGEX:
                s->preferred_kex[s->n_preferred_kex++] =
                    &ssh_diffiehellman_gex;
@@ -5413,12 +5498,12 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
         */
        s->n_preferred_ciphers = 0;
        for (i = 0; i < CIPHER_MAX; i++) {
-           switch (ssh->cfg.ssh_cipherlist[i]) {
+           switch (conf_get_int_int(ssh->conf, CONF_ssh_cipherlist, i)) {
              case CIPHER_BLOWFISH:
                s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_blowfish;
                break;
              case CIPHER_DES:
-               if (ssh->cfg.ssh2_des_cbc) {
+               if (conf_get_int(ssh->conf, CONF_ssh2_des_cbc)) {
                    s->preferred_ciphers[s->n_preferred_ciphers++] = &ssh2_des;
                }
                break;
@@ -5444,7 +5529,7 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
        /*
         * Set up preferred compression.
         */
-       if (ssh->cfg.compression)
+       if (conf_get_int(ssh->conf, CONF_compression))
            s->preferred_comp = &ssh_zlib;
        else
            s->preferred_comp = &ssh_comp_none;
@@ -5526,26 +5611,32 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
            if (i < s->nmacs - 1)
                ssh2_pkt_addstring_str(s->pktout, ",");
        }
-       /* List client->server compression algorithms. */
-       ssh2_pkt_addstring_start(s->pktout);
-       assert(lenof(compressions) > 1);
-       ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name);
-       for (i = 0; i < lenof(compressions); i++) {
-           const struct ssh_compress *c = compressions[i];
-           if (c != s->preferred_comp) {
+       /* List client->server compression algorithms,
+        * then server->client compression algorithms. (We use the
+        * same set twice.) */
+       for (j = 0; j < 2; j++) {
+           ssh2_pkt_addstring_start(s->pktout);
+           assert(lenof(compressions) > 1);
+           /* Prefer non-delayed versions */
+           ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name);
+           /* We don't even list delayed versions of algorithms until
+            * they're allowed to be used, to avoid a race. See the end of
+            * this function. */
+           if (s->userauth_succeeded && s->preferred_comp->delayed_name) {
                ssh2_pkt_addstring_str(s->pktout, ",");
-               ssh2_pkt_addstring_str(s->pktout, c->name);
+               ssh2_pkt_addstring_str(s->pktout,
+                                      s->preferred_comp->delayed_name);
            }
-       }
-       /* List server->client compression algorithms. */
-       ssh2_pkt_addstring_start(s->pktout);
-       assert(lenof(compressions) > 1);
-       ssh2_pkt_addstring_str(s->pktout, s->preferred_comp->name);
-       for (i = 0; i < lenof(compressions); i++) {
-           const struct ssh_compress *c = compressions[i];
-           if (c != s->preferred_comp) {
-               ssh2_pkt_addstring_str(s->pktout, ",");
-               ssh2_pkt_addstring_str(s->pktout, c->name);
+           for (i = 0; i < lenof(compressions); i++) {
+               const struct ssh_compress *c = compressions[i];
+               if (c != s->preferred_comp) {
+                   ssh2_pkt_addstring_str(s->pktout, ",");
+                   ssh2_pkt_addstring_str(s->pktout, c->name);
+                   if (s->userauth_succeeded && c->delayed_name) {
+                       ssh2_pkt_addstring_str(s->pktout, ",");
+                       ssh2_pkt_addstring_str(s->pktout, c->delayed_name);
+                   }
+               }
            }
        }
        /* List client->server languages. Empty list. */
@@ -5694,6 +5785,13 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
            if (in_commasep_string(c->name, str, len)) {
                s->cscomp_tobe = c;
                break;
+           } else if (in_commasep_string(c->delayed_name, str, len)) {
+               if (s->userauth_succeeded) {
+                   s->cscomp_tobe = c;
+                   break;
+               } else {
+                   s->pending_compression = TRUE;  /* try this later */
+               }
            }
        }
        ssh_pkt_getstring(pktin, &str, &len);  /* server->client compression */
@@ -5703,8 +5801,19 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
            if (in_commasep_string(c->name, str, len)) {
                s->sccomp_tobe = c;
                break;
+           } else if (in_commasep_string(c->delayed_name, str, len)) {
+               if (s->userauth_succeeded) {
+                   s->sccomp_tobe = c;
+                   break;
+               } else {
+                   s->pending_compression = TRUE;  /* try this later */
+               }
            }
        }
+       if (s->pending_compression) {
+           logevent("Server supports delayed compression; "
+                    "will try this later");
+       }
        ssh_pkt_getstring(pktin, &str, &len);  /* client->server language */
        ssh_pkt_getstring(pktin, &str, &len);  /* server->client language */
        s->ignorepkt = ssh2_pkt_getbool(pktin) && !s->guessok;
@@ -6216,8 +6325,8 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
      */
     ssh->kex_in_progress = FALSE;
     ssh->last_rekey = GETTICKCOUNT();
-    if (ssh->cfg.ssh_rekey_time != 0)
-       ssh->next_rekey = schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC,
+    if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0)
+       ssh->next_rekey = schedule_timer(conf_get_int(ssh->conf, CONF_ssh_rekey_time)*60*TICKSPERSEC,
                                         ssh2_timer, ssh);
 
     /*
@@ -6240,19 +6349,52 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
      * start.
      * 
      * We _also_ go back to the start if we see pktin==NULL and
-     * inlen==-1, because this is a special signal meaning
+     * inlen negative, because this is a special signal meaning
      * `initiate client-driven rekey', and `in' contains a message
      * giving the reason for the rekey.
+     *
+     * inlen==-1 means always initiate a rekey;
+     * inlen==-2 means that userauth has completed successfully and
+     *   we should consider rekeying (for delayed compression).
      */
     while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) ||
-            (!pktin && inlen == -1))) {
+            (!pktin && inlen < 0))) {
         wait_for_rekey:
        crReturn(1);
     }
     if (pktin) {
        logevent("Server initiated key re-exchange");
     } else {
+       if (inlen == -2) {
+           /* 
+            * authconn has seen a USERAUTH_SUCCEEDED. Time to enable
+            * delayed compression, if it's available.
+            *
+            * draft-miller-secsh-compression-delayed-00 says that you
+            * negotiate delayed compression in the first key exchange, and
+            * both sides start compressing when the server has sent
+            * USERAUTH_SUCCESS. This has a race condition -- the server
+            * can't know when the client has seen it, and thus which incoming
+            * packets it should treat as compressed.
+            *
+            * Instead, we do the initial key exchange without offering the
+            * delayed methods, but note if the server offers them; when we
+            * get here, if a delayed method was available that was higher
+            * on our list than what we got, we initiate a rekey in which we
+            * _do_ list the delayed methods (and hopefully get it as a
+            * result). Subsequent rekeys will do the same.
+            */
+           assert(!s->userauth_succeeded); /* should only happen once */
+           s->userauth_succeeded = TRUE;
+           if (!s->pending_compression)
+               /* Can't see any point rekeying. */
+               goto wait_for_rekey;       /* this is utterly horrid */
+           /* else fall through to rekey... */
+           s->pending_compression = FALSE;
+       }
         /*
+        * Now we've decided to rekey.
+        *
          * Special case: if the server bug is set that doesn't
          * allow rekeying, we give a different log message and
          * continue waiting. (If such a server _initiates_ a rekey,
@@ -6265,12 +6407,12 @@ static int do_ssh2_transport(Ssh ssh, void *vin, int inlen,
              * hit the event log _too_ often. */
             ssh->outgoing_data_size = 0;
             ssh->incoming_data_size = 0;
-            if (ssh->cfg.ssh_rekey_time != 0) {
+            if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0) {
                 ssh->next_rekey =
-                    schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC,
+                    schedule_timer(conf_get_int(ssh->conf, CONF_ssh_rekey_time)*60*TICKSPERSEC,
                                    ssh2_timer, ssh);
             }
-            goto wait_for_rekey;       /* this is utterly horrid */
+            goto wait_for_rekey;       /* this is still utterly horrid */
         } else {
             logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in);
         }
@@ -6323,7 +6465,7 @@ static int ssh2_try_send(struct ssh_channel *c)
     return bufchain_size(&c->v.v2.outbuffer);
 }
 
-static void ssh2_try_send_and_unthrottle(struct ssh_channel *c)
+static void ssh2_try_send_and_unthrottle(Ssh ssh, struct ssh_channel *c)
 {
     int bufsize;
     if (c->closes)
@@ -6347,6 +6489,19 @@ static void ssh2_try_send_and_unthrottle(struct ssh_channel *c)
            break;
        }
     }
+
+    /*
+     * If we've emptied the channel's output buffer and there's a
+     * pending close event, start the channel-closing procedure.
+     */
+    if (c->pending_close && bufchain_size(&c->v.v2.outbuffer) == 0) {
+       struct Packet *pktout;
+       pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_CLOSE);
+       ssh2_pkt_adduint32(pktout, c->remoteid);
+       ssh2_pkt_send(ssh, pktout);
+       c->closes = 1;
+       c->pending_close = FALSE;
+    }
 }
 
 /*
@@ -6357,9 +6512,10 @@ static void ssh2_channel_init(struct ssh_channel *c)
     Ssh ssh = c->ssh;
     c->localid = alloc_channel_id(ssh);
     c->closes = 0;
+    c->pending_close = FALSE;
     c->throttling_conn = FALSE;
     c->v.v2.locwindow = c->v.v2.locmaxwin = c->v.v2.remlocwin =
-       ssh->cfg.ssh_simple ? OUR_V2_BIGWIN : OUR_V2_WINSIZE;
+       conf_get_int(ssh->conf, CONF_ssh_simple) ? OUR_V2_BIGWIN : OUR_V2_WINSIZE;
     c->v.v2.winadj_head = c->v.v2.winadj_tail = NULL;
     c->v.v2.throttle_state = UNTHROTTLED;
     bufchain_init(&c->v.v2.outbuffer);
@@ -6475,25 +6631,44 @@ static struct ssh_channel *ssh2_channel_msg(Ssh ssh, struct Packet *pktin)
     return c;
 }
 
+static int ssh2_handle_winadj_response(struct ssh_channel *c)
+{
+    struct winadj *wa = c->v.v2.winadj_head;
+    if (!wa)
+       return FALSE;
+    c->v.v2.winadj_head = wa->next;
+    c->v.v2.remlocwin += wa->size;
+    sfree(wa);
+    /*
+     * winadj messages are only sent when the window is fully open, so
+     * if we get an ack of one, we know any pending unthrottle is
+     * complete.
+     */
+    if (c->v.v2.throttle_state == UNTHROTTLING)
+       c->v.v2.throttle_state = UNTHROTTLED;
+    return TRUE;
+}
+
 static void ssh2_msg_channel_success(Ssh ssh, struct Packet *pktin)
 {
     /*
      * This should never get called.  All channel requests are either
-     * sent with want_reply false or are sent before this handler gets
-     * installed.
+     * sent with want_reply false, are sent before this handler gets
+     * installed, or are "winadj@putty" requests, which servers should
+     * never respond to with success.
+     *
+     * However, at least one server ("boks_sshd") is known to return
+     * SUCCESS for channel requests it's never heard of, such as
+     * "winadj@putty". Raised with foxt.com as bug 090916-090424, but
+     * for the sake of a quiet life, we handle it just the same as the
+     * expected FAILURE.
      */
     struct ssh_channel *c;
-    struct winadj *wa;
 
     c = ssh2_channel_msg(ssh, pktin);
     if (!c)
        return;
-    wa = c->v.v2.winadj_head;
-    if (wa)
-       ssh_disconnect(ssh, NULL, "Received SSH_MSG_CHANNEL_SUCCESS for "
-                      "\"winadj@putty.projects.tartarus.org\"",
-                      SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);
-    else
+    if (!ssh2_handle_winadj_response(c))
        ssh_disconnect(ssh, NULL,
                       "Received unsolicited SSH_MSG_CHANNEL_SUCCESS",
                       SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);
@@ -6508,28 +6683,14 @@ static void ssh2_msg_channel_failure(Ssh ssh, struct Packet *pktin)
      * installed.
      */
     struct ssh_channel *c;
-    struct winadj *wa;
 
     c = ssh2_channel_msg(ssh, pktin);
     if (!c)
        return;
-    wa = c->v.v2.winadj_head;
-    if (!wa) {
+    if (!ssh2_handle_winadj_response(c))
        ssh_disconnect(ssh, NULL,
                       "Received unsolicited SSH_MSG_CHANNEL_FAILURE",
                       SSH2_DISCONNECT_PROTOCOL_ERROR, FALSE);
-       return;
-    }
-    c->v.v2.winadj_head = wa->next;
-    c->v.v2.remlocwin += wa->size;
-    sfree(wa);
-    /*
-     * winadj messages are only sent when the window is fully open, so
-     * if we get an ack of one, we know any pending unthrottle is
-     * complete.
-     */
-    if (c->v.v2.throttle_state == UNTHROTTLING)
-       c->v.v2.throttle_state = UNTHROTTLED;
 }
 
 static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin)
@@ -6540,7 +6701,7 @@ static void ssh2_msg_channel_window_adjust(Ssh ssh, struct Packet *pktin)
        return;
     if (!c->closes) {
        c->v.v2.remwindow += ssh_pkt_getuint32(pktin);
-       ssh2_try_send_and_unthrottle(c);
+       ssh2_try_send_and_unthrottle(ssh, c);
     }
 }
 
@@ -6639,7 +6800,7 @@ static void ssh2_msg_channel_data(Ssh ssh, struct Packet *pktin)
         * throttle the whole channel.
         */
        if ((bufsize > c->v.v2.locmaxwin ||
-            (ssh->cfg.ssh_simple && bufsize > 0)) &&
+            (conf_get_int(ssh->conf, CONF_ssh_simple) && bufsize > 0)) &&
            !c->throttling_conn) {
            c->throttling_conn = 1;
            ssh_throttle_conn(ssh, +1);
@@ -6661,11 +6822,13 @@ static void ssh2_msg_channel_eof(Ssh ssh, struct Packet *pktin)
         * wrap up and close the channel ourselves.
         */
        x11_close(c->u.x11.s);
+       c->u.x11.s = NULL;
        sshfwd_close(c);
     } else if (c->type == CHAN_AGENT) {
        sshfwd_close(c);
     } else if (c->type == CHAN_SOCKDATA) {
        pfd_close(c->u.pfd.s);
+       c->u.pfd.s = NULL;
        sshfwd_close(c);
     }
 }
@@ -6712,7 +6875,7 @@ static void ssh2_msg_channel_close(Ssh ssh, struct Packet *pktin)
      * (This is only our termination condition if we're
      * not running in -N mode.)
      */
-    if (!ssh->cfg.ssh_no_shell && count234(ssh->channels) == 0) {
+    if (!conf_get_int(ssh->conf, CONF_ssh_no_shell) && count234(ssh->channels) == 0) {
        /*
         * We used to send SSH_MSG_DISCONNECT here,
         * because I'd believed that _every_ conforming
@@ -7027,7 +7190,7 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
        if (!ssh->X11_fwd_enabled)
            error = "X11 forwarding is not enabled";
        else if ((x11err = x11_init(&c->u.x11.s, ssh->x11disp, c,
-                                   addrstr, peerport, &ssh->cfg)) != NULL) {
+                                   addrstr, peerport, ssh->conf)) != NULL) {
            logeventf(ssh, "Local X11 connection failed: %s", x11err);
            error = "Unable to open an X11 connection";
        } else {
@@ -7054,7 +7217,7 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
            const char *e = pfd_newconnect(&c->u.pfd.s,
                                           realpf->dhost,
                                           realpf->dport, c,
-                                          &ssh->cfg,
+                                          ssh->conf,
                                           realpf->pfrec->addressfamily);
            logeventf(ssh, "Attempting to forward remote port to "
                      "%s:%d", realpf->dhost, realpf->dport);
@@ -7104,12 +7267,14 @@ static void ssh2_msg_channel_open(Ssh ssh, struct Packet *pktin)
 }
 
 /*
- * Buffer banner messages for later display at some convenient point.
+ * Buffer banner messages for later display at some convenient point,
+ * if we're going to display them.
  */
 static void ssh2_msg_userauth_banner(Ssh ssh, struct Packet *pktin)
 {
     /* Arbitrary limit to prevent unbounded inflation of buffer */
-    if (bufchain_size(&ssh->banner) <= 131072) {
+    if (conf_get_int(ssh->conf, CONF_ssh_show_banner) &&
+       bufchain_size(&ssh->banner) <= 131072) {
        char *banner = NULL;
        int size = 0;
        ssh_pkt_getstring(pktin, &banner, &size);
@@ -7163,10 +7328,10 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        int tried_gssapi;
 #endif
        int kbd_inter_refused;
-       int we_are_in;
+       int we_are_in, userauth_success;
        prompts_t *cur_prompt;
        int num_prompts;
-       char username[100];
+       char *username;
        char *password;
        int got_username;
        void *publickey_blob;
@@ -7185,7 +7350,9 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        int try_send;
        int num_env, env_left, env_ok;
        struct Packet *pktout;
+       Filename *keyfile;
 #ifndef NO_GSSAPI
+       struct ssh_gss_library *gsslib;
        Ssh_gss_ctx gss_ctx;
        Ssh_gss_buf gss_buf;
        Ssh_gss_buf gss_rcvtok, gss_sndtok;
@@ -7198,12 +7365,12 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     crBegin(ssh->do_ssh2_authconn_crstate);
 
     s->done_service_req = FALSE;
-    s->we_are_in = FALSE;
+    s->we_are_in = s->userauth_success = FALSE;
 #ifndef NO_GSSAPI
     s->tried_gssapi = FALSE;
 #endif
 
-    if (!ssh->cfg.ssh_no_userauth) {
+    if (!conf_get_int(ssh->conf, CONF_ssh_no_userauth)) {
        /*
         * Request userauth protocol, and await a response to it.
         */
@@ -7246,28 +7413,29 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
         * Load the public half of any configured public key file
         * for later use.
         */
-       if (!filename_is_null(ssh->cfg.keyfile)) {
+       s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
+       if (!filename_is_null(*s->keyfile)) {
            int keytype;
            logeventf(ssh, "Reading private key file \"%.150s\"",
-                     filename_to_str(&ssh->cfg.keyfile));
-           keytype = key_type(&ssh->cfg.keyfile);
+                     filename_to_str(s->keyfile));
+           keytype = key_type(s->keyfile);
            if (keytype == SSH_KEYTYPE_SSH2) {
                const char *error;
                s->publickey_blob =
-                   ssh2_userkey_loadpub(&ssh->cfg.keyfile,
+                   ssh2_userkey_loadpub(s->keyfile,
                                         &s->publickey_algorithm,
                                         &s->publickey_bloblen, 
                                         &s->publickey_comment, &error);
                if (s->publickey_blob) {
                    s->publickey_encrypted =
-                       ssh2_userkey_encrypted(&ssh->cfg.keyfile, NULL);
+                       ssh2_userkey_encrypted(s->keyfile, NULL);
                } else {
                    char *msgbuf;
                    logeventf(ssh, "Unable to load private key (%s)", 
                              error);
                    msgbuf = dupprintf("Unable to load private key file "
                                       "\"%.150s\" (%s)\r\n",
-                                      filename_to_str(&ssh->cfg.keyfile),
+                                      filename_to_str(s->keyfile),
                                       error);
                    c_write_str(ssh, msgbuf);
                    sfree(msgbuf);
@@ -7278,7 +7446,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                          key_type_to_str(keytype));
                msgbuf = dupprintf("Unable to use key file \"%.150s\""
                                   " (%s)\r\n",
-                                  filename_to_str(&ssh->cfg.keyfile),
+                                  filename_to_str(s->keyfile),
                                   key_type_to_str(keytype));
                c_write_str(ssh, msgbuf);
                sfree(msgbuf);
@@ -7293,7 +7461,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        s->nkeys = 0;
        s->agent_response = NULL;
        s->pkblob_in_agent = NULL;
-       if (ssh->cfg.tryagent && agent_exists()) {
+       if (conf_get_int(ssh->conf, CONF_tryagent) && agent_exists()) {
 
            void *r;
 
@@ -7345,6 +7513,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                        s->nkeys = 0;
                    }
                }
+           } else {
+                logevent("Failed to get reply from Pageant");
            }
        }
 
@@ -7374,26 +7544,24 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
      *    the username they will want to be able to get back and
      *    retype it!
      */
-    s->username[0] = '\0';
     s->got_username = FALSE;
     while (!s->we_are_in) {
        /*
         * Get a username.
         */
-       if (s->got_username && !ssh->cfg.change_username) {
+       if (s->got_username && !conf_get_int(ssh->conf, CONF_change_username)) {
            /*
             * We got a username last time round this loop, and
             * with change_username turned off we don't try to get
             * it again.
             */
-       } else if (!get_remote_username(&ssh->cfg, s->username,
-                                       sizeof(s->username))) {
+       } else if ((ssh->username = get_remote_username(ssh->conf)) == NULL) {
            int ret; /* need not be kept over crReturn */
            s->cur_prompt = new_prompts(ssh->frontend);
            s->cur_prompt->to_server = TRUE;
            s->cur_prompt->name = dupstr("SSH login name");
-           add_prompt(s->cur_prompt, dupstr("login as: "), TRUE,
-                      lenof(s->username)); 
+           /* 512 is an arbitrary limit :-( */
+           add_prompt(s->cur_prompt, dupstr("login as: "), TRUE, 512); 
            ret = get_userpass_input(s->cur_prompt, NULL, 0);
            while (ret < 0) {
                ssh->send_ok = 1;
@@ -7410,13 +7578,12 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                ssh_disconnect(ssh, "No username provided", NULL, 0, TRUE);
                crStopV;
            }
-           memcpy(s->username, s->cur_prompt->prompts[0]->result,
-                  lenof(s->username));
+           ssh->username = dupstr(s->cur_prompt->prompts[0]->result);
            free_prompts(s->cur_prompt);
        } else {
            char *stuff;
            if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
-               stuff = dupprintf("Using username \"%s\".\r\n", s->username);
+               stuff = dupprintf("Using username \"%s\".\r\n", ssh->username);
                c_write_str(ssh, stuff);
                sfree(stuff);
            }
@@ -7431,7 +7598,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        ssh->pkt_actx = SSH2_PKTCTX_NOAUTH;
 
        s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-       ssh2_pkt_addstring(s->pktout, s->username);
+       ssh2_pkt_addstring(s->pktout, ssh->username);
        ssh2_pkt_addstring(s->pktout, "ssh-connection");/* service requested */
        ssh2_pkt_addstring(s->pktout, "none");    /* method */
        ssh2_pkt_send(ssh, s->pktout);
@@ -7454,6 +7621,9 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        }
 
        while (1) {
+           char *methods = NULL;
+           int methlen = 0;
+
            /*
             * Wait for the result of the last authentication request.
             */
@@ -7485,7 +7655,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
            }
            if (pktin->type == SSH2_MSG_USERAUTH_SUCCESS) {
                logevent("Access granted");
-               s->we_are_in = TRUE;
+               s->we_are_in = s->userauth_success = TRUE;
                break;
            }
 
@@ -7503,8 +7673,6 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
             * helpfully try next.
             */
            if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
-               char *methods;
-               int methlen;
                ssh_pkt_getstring(pktin, &methods, &methlen);
                if (!ssh2_pkt_getbool(pktin)) {
                    /*
@@ -7542,7 +7710,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                        c_write_str(ssh, "Access denied\r\n");
                        logevent("Access denied");
                        if (s->type == AUTH_TYPE_PASSWORD &&
-                           ssh->cfg.change_username) {
+                           conf_get_int(ssh->conf, CONF_change_username)) {
                            /* XXX perhaps we should allow
                             * keyboard-interactive to do this too? */
                            s->we_are_in = FALSE;
@@ -7558,12 +7726,14 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                    in_commasep_string("publickey", methods, methlen);
                s->can_passwd =
                    in_commasep_string("password", methods, methlen);
-               s->can_keyb_inter = ssh->cfg.try_ki_auth &&
+               s->can_keyb_inter = conf_get_int(ssh->conf, CONF_try_ki_auth) &&
                    in_commasep_string("keyboard-interactive", methods, methlen);
-#ifndef NO_GSSAPI              
-               s->can_gssapi = ssh->cfg.try_gssapi_auth &&
-                 in_commasep_string("gssapi-with-mic", methods, methlen) &&
-                 ssh_gss_init();
+#ifndef NO_GSSAPI
+               if (!ssh->gsslibs)
+                   ssh->gsslibs = ssh_gss_setup(ssh->conf);
+               s->can_gssapi = conf_get_int(ssh->conf, CONF_try_gssapi_auth) &&
+                   in_commasep_string("gssapi-with-mic", methods, methlen) &&
+                   ssh->gsslibs->nlibraries > 0;
 #endif
            }
 
@@ -7594,7 +7764,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
 
                /* See if server will accept it */
                s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-               ssh2_pkt_addstring(s->pktout, s->username);
+               ssh2_pkt_addstring(s->pktout, ssh->username);
                ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                    /* service requested */
                ssh2_pkt_addstring(s->pktout, "publickey");
@@ -7629,7 +7799,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                     * Construct a SIGN_REQUEST.
                     */
                    s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-                   ssh2_pkt_addstring(s->pktout, s->username);
+                   ssh2_pkt_addstring(s->pktout, ssh->username);
                    ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                        /* service requested */
                    ssh2_pkt_addstring(s->pktout, "publickey");
@@ -7733,7 +7903,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                 * willing to accept it.
                 */
                s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-               ssh2_pkt_addstring(s->pktout, s->username);
+               ssh2_pkt_addstring(s->pktout, ssh->username);
                ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                /* service requested */
                ssh2_pkt_addstring(s->pktout, "publickey");     /* method */
@@ -7807,8 +7977,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                    /*
                     * Try decrypting the key.
                     */
-                   key = ssh2_load_userkey(&ssh->cfg.keyfile, passphrase,
-                                           &error);
+                   s->keyfile = conf_get_filename(ssh->conf, CONF_keyfile);
+                   key = ssh2_load_userkey(s->keyfile, passphrase, &error);
                    if (passphrase) {
                        /* burn the evidence */
                        memset(passphrase, 0, strlen(passphrase));
@@ -7841,7 +8011,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                     * Hallelujah. Generate a signature and send it.
                     */
                    s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-                   ssh2_pkt_addstring(s->pktout, s->username);
+                   ssh2_pkt_addstring(s->pktout, ssh->username);
                    ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                    /* service requested */
                    ssh2_pkt_addstring(s->pktout, "publickey");
@@ -7906,14 +8076,44 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                s->gotit = TRUE;
                ssh->pkt_actx = SSH2_PKTCTX_GSSAPI;
 
+               /*
+                * Pick the highest GSS library on the preference
+                * list.
+                */
+               {
+                   int i, j;
+                   s->gsslib = NULL;
+                   for (i = 0; i < ngsslibs; i++) {
+                       int want_id = conf_get_int_int(ssh->conf,
+                                                      CONF_ssh_gsslist, i);
+                       for (j = 0; j < ssh->gsslibs->nlibraries; j++)
+                           if (ssh->gsslibs->libraries[j].id == want_id) {
+                               s->gsslib = &ssh->gsslibs->libraries[j];
+                               goto got_gsslib;   /* double break */
+                           }
+                   }
+                   got_gsslib:
+                   /*
+                    * We always expect to have found something in
+                    * the above loop: we only came here if there
+                    * was at least one viable GSS library, and the
+                    * preference list should always mention
+                    * everything and only change the order.
+                    */
+                   assert(s->gsslib);
+               }
+
+               if (s->gsslib->gsslogmsg)
+                   logevent(s->gsslib->gsslogmsg);
+
                /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */
                s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-               ssh2_pkt_addstring(s->pktout, s->username);
+               ssh2_pkt_addstring(s->pktout, ssh->username);
                ssh2_pkt_addstring(s->pktout, "ssh-connection");
                ssh2_pkt_addstring(s->pktout, "gssapi-with-mic");
 
                /* add mechanism info */
-               ssh_gss_indicate_mech(&s->gss_buf);
+               s->gsslib->indicate_mech(s->gsslib, &s->gss_buf);
 
                /* number of GSSAPI mechanisms */
                ssh2_pkt_adduint32(s->pktout,1);
@@ -7949,8 +8149,9 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                }
 
                /* now start running */
-               s->gss_stat = ssh_gss_import_name(ssh->fullhostname,
-                                                 &s->gss_srv_name);
+               s->gss_stat = s->gsslib->import_name(s->gsslib,
+                                                    ssh->fullhostname,
+                                                    &s->gss_srv_name);
                if (s->gss_stat != SSH_GSS_OK) {
                    if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)
                        logevent("GSSAPI import name failed - Bad service name");
@@ -7960,11 +8161,11 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                }
 
                /* fetch TGT into GSS engine */
-               s->gss_stat = ssh_gss_acquire_cred(&s->gss_ctx);
+               s->gss_stat = s->gsslib->acquire_cred(s->gsslib, &s->gss_ctx);
 
                if (s->gss_stat != SSH_GSS_OK) {
                    logevent("GSSAPI authentication failed to get credentials");
-                   ssh_gss_release_name(&s->gss_srv_name);
+                   s->gsslib->release_name(s->gsslib, &s->gss_srv_name);
                    continue;
                }
 
@@ -7974,17 +8175,20 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
 
                /* now enter the loop */
                do {
-                   s->gss_stat = ssh_gss_init_sec_context(&s->gss_ctx,
-                                                          s->gss_srv_name,
-                                                          ssh->cfg.gssapifwd,
-                                                          &s->gss_rcvtok,
-                                                          &s->gss_sndtok);
+                   s->gss_stat = s->gsslib->init_sec_context
+                       (s->gsslib,
+                        &s->gss_ctx,
+                        s->gss_srv_name,
+                        conf_get_int(ssh->conf, CONF_gssapifwd),
+                        &s->gss_rcvtok,
+                        &s->gss_sndtok);
 
                    if (s->gss_stat!=SSH_GSS_S_COMPLETE &&
                        s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {
                        logevent("GSSAPI authentication initialisation failed");
 
-                       if (ssh_gss_display_status(s->gss_ctx,&s->gss_buf) == SSH_GSS_OK) {
+                       if (s->gsslib->display_status(s->gsslib, s->gss_ctx,
+                                                     &s->gss_buf) == SSH_GSS_OK) {
                            logevent(s->gss_buf.value);
                            sfree(s->gss_buf.value);
                        }
@@ -8001,7 +8205,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                        ssh_pkt_addstring_start(s->pktout);
                        ssh_pkt_addstring_data(s->pktout,s->gss_sndtok.value,s->gss_sndtok.length);
                        ssh2_pkt_send(ssh, s->pktout);
-                       ssh_gss_free_tok(&s->gss_sndtok);
+                       s->gsslib->free_tok(s->gsslib, &s->gss_sndtok);
                    }
 
                    if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {
@@ -8018,8 +8222,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
 
                if (s->gss_stat != SSH_GSS_OK) {
-                   ssh_gss_release_name(&s->gss_srv_name);
-                   ssh_gss_release_cred(&s->gss_ctx);
+                   s->gsslib->release_name(s->gsslib, &s->gss_srv_name);
+                   s->gsslib->release_cred(s->gsslib, &s->gss_ctx);
                    continue;
                }
                logevent("GSSAPI authentication loop finished OK");
@@ -8031,24 +8235,24 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                ssh_pkt_addstring_start(s->pktout);
                ssh_pkt_addstring_data(s->pktout, (char *)ssh->v2_session_id, ssh->v2_session_id_len);
                ssh_pkt_addbyte(s->pktout, SSH2_MSG_USERAUTH_REQUEST);
-               ssh_pkt_addstring(s->pktout, s->username);
+               ssh_pkt_addstring(s->pktout, ssh->username);
                ssh_pkt_addstring(s->pktout, "ssh-connection");
                ssh_pkt_addstring(s->pktout, "gssapi-with-mic");
 
                s->gss_buf.value = (char *)s->pktout->data + micoffset;
                s->gss_buf.length = s->pktout->length - micoffset;
 
-               ssh_gss_get_mic(s->gss_ctx, &s->gss_buf, &mic);
+               s->gsslib->get_mic(s->gsslib, s->gss_ctx, &s->gss_buf, &mic);
                s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_GSSAPI_MIC);
                ssh_pkt_addstring_start(s->pktout);
                ssh_pkt_addstring_data(s->pktout, mic.value, mic.length);
                ssh2_pkt_send(ssh, s->pktout);
-               ssh_gss_free_mic(&mic);
+               s->gsslib->free_mic(s->gsslib, &mic);
 
                s->gotit = FALSE;
 
-               ssh_gss_release_name(&s->gss_srv_name);
-               ssh_gss_release_cred(&s->gss_ctx);
+               s->gsslib->release_name(s->gsslib, &s->gss_srv_name);
+               s->gsslib->release_cred(s->gsslib, &s->gss_ctx);
                continue;
 #endif
            } else if (s->can_keyb_inter && !s->kbd_inter_refused) {
@@ -8062,7 +8266,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                ssh->pkt_actx = SSH2_PKTCTX_KBDINTER;
 
                s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-               ssh2_pkt_addstring(s->pktout, s->username);
+               ssh2_pkt_addstring(s->pktout, ssh->username);
                ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                        /* service requested */
                ssh2_pkt_addstring(s->pktout, "keyboard-interactive");
@@ -8217,8 +8421,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                s->cur_prompt = new_prompts(ssh->frontend);
                s->cur_prompt->to_server = TRUE;
                s->cur_prompt->name = dupstr("SSH password");
-               add_prompt(s->cur_prompt, dupprintf("%.90s@%.90s's password: ",
-                                                   s->username,
+               add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
+                                                   ssh->username,
                                                    ssh->savedhost),
                           FALSE, SSH_MAX_PASSWORD_LEN);
 
@@ -8258,7 +8462,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                 * people who find out how long their password is!
                 */
                s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-               ssh2_pkt_addstring(s->pktout, s->username);
+               ssh2_pkt_addstring(s->pktout, ssh->username);
                ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                        /* service requested */
                ssh2_pkt_addstring(s->pktout, "password");
@@ -8387,7 +8591,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                     * (see above for padding rationale)
                     */
                    s->pktout = ssh2_pkt_init(SSH2_MSG_USERAUTH_REQUEST);
-                   ssh2_pkt_addstring(s->pktout, s->username);
+                   ssh2_pkt_addstring(s->pktout, ssh->username);
                    ssh2_pkt_addstring(s->pktout, "ssh-connection");
                                                        /* service requested */
                    ssh2_pkt_addstring(s->pktout, "password");
@@ -8434,11 +8638,16 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                sfree(s->password);
 
            } else {
+               char *str = dupprintf("No supported authentication methods available"
+                                     " (server sent: %.*s)",
+                                     methlen, methods);
 
-               ssh_disconnect(ssh, NULL,
+               ssh_disconnect(ssh, str,
                               "No supported authentication methods available",
                               SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE,
                               FALSE);
+               sfree(str);
+
                crStopV;
 
            }
@@ -8455,6 +8664,20 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     if (s->agent_response)
        sfree(s->agent_response);
 
+    if (s->userauth_success) {
+       /*
+        * We've just received USERAUTH_SUCCESS, and we haven't sent any
+        * packets since. Signal the transport layer to consider enacting
+        * delayed compression.
+        *
+        * (Relying on we_are_in is not sufficient, as
+        * draft-miller-secsh-compression-delayed is quite clear that it
+        * triggers on USERAUTH_SUCCESS specifically, and we_are_in can
+        * become set for other reasons.)
+        */
+       do_ssh2_transport(ssh, "enabling delayed compression", -2, NULL);
+    }
+
     /*
      * Now the connection protocol has started, one way or another.
      */
@@ -8473,9 +8696,9 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     /*
      * Create the main session channel.
      */
-    if (ssh->cfg.ssh_no_shell) {
+    if (conf_get_int(ssh->conf, CONF_ssh_no_shell)) {
        ssh->mainchan = NULL;
-    } else if (*ssh->cfg.ssh_nc_host) {
+    } else if (*conf_get_str(ssh->conf, CONF_ssh_nc_host)) {
        /*
         * Just start a direct-tcpip channel and use it as the main
         * channel.
@@ -8485,14 +8708,15 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        ssh2_channel_init(ssh->mainchan);
        logeventf(ssh,
                  "Opening direct-tcpip channel to %s:%d in place of session",
-                 ssh->cfg.ssh_nc_host, ssh->cfg.ssh_nc_port);
+                 conf_get_str(ssh->conf, CONF_ssh_nc_host),
+                 conf_get_int(ssh->conf, CONF_ssh_nc_port));
        s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_OPEN);
        ssh2_pkt_addstring(s->pktout, "direct-tcpip");
        ssh2_pkt_adduint32(s->pktout, ssh->mainchan->localid);
        ssh2_pkt_adduint32(s->pktout, ssh->mainchan->v.v2.locwindow);/* our window size */
        ssh2_pkt_adduint32(s->pktout, OUR_V2_MAXPKT);      /* our max pkt size */
-       ssh2_pkt_addstring(s->pktout, ssh->cfg.ssh_nc_host);
-       ssh2_pkt_adduint32(s->pktout, ssh->cfg.ssh_nc_port);
+       ssh2_pkt_addstring(s->pktout, conf_get_str(ssh->conf, CONF_ssh_nc_host));
+       ssh2_pkt_adduint32(s->pktout, conf_get_int(ssh->conf, CONF_ssh_nc_port));
        /*
         * There's nothing meaningful to put in the originator
         * fields, but some servers insist on syntactically correct
@@ -8570,7 +8794,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     ssh->packet_dispatch[SSH2_MSG_CHANNEL_OPEN] =
        ssh2_msg_channel_open;
 
-    if (ssh->cfg.ssh_simple) {
+    if (ssh->mainchan && conf_get_int(ssh->conf, CONF_ssh_simple)) {
        /*
         * This message indicates to the server that we promise
         * not to try to run any other channel in parallel with
@@ -8587,9 +8811,9 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     /*
      * Potentially enable X11 forwarding.
      */
-    if (ssh->mainchan && !ssh->ncmode && ssh->cfg.x11_forward &&
-       (ssh->x11disp = x11_setup_display(ssh->cfg.x11_display,
-                                         ssh->cfg.x11_auth, &ssh->cfg))) {
+    if (ssh->mainchan && !ssh->ncmode && conf_get_int(ssh->conf, CONF_x11_forward) &&
+       (ssh->x11disp = x11_setup_display(conf_get_str(ssh->conf, CONF_x11_display),
+                                         conf_get_int(ssh->conf, CONF_x11_auth), ssh->conf))) {
        logevent("Requesting X11 forwarding");
        s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
        ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);
@@ -8628,12 +8852,12 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     /*
      * Enable port forwardings.
      */
-    ssh_setup_portfwd(ssh, &ssh->cfg);
+    ssh_setup_portfwd(ssh, ssh->conf);
 
     /*
      * Potentially enable agent forwarding.
      */
-    if (ssh->mainchan && !ssh->ncmode && ssh->cfg.agentfwd && agent_exists()) {
+    if (ssh->mainchan && !ssh->ncmode && conf_get_int(ssh->conf, CONF_agentfwd) && agent_exists()) {
        logevent("Requesting OpenSSH-style agent forwarding");
        s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
        ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);
@@ -8659,24 +8883,23 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
     /*
      * Now allocate a pty for the session.
      */
-    if (ssh->mainchan && !ssh->ncmode && !ssh->cfg.nopty) {
+    if (ssh->mainchan && !ssh->ncmode && !conf_get_int(ssh->conf, CONF_nopty)) {
        /* Unpick the terminal-speed string. */
        /* XXX perhaps we should allow no speeds to be sent. */
         ssh->ospeed = 38400; ssh->ispeed = 38400; /* last-resort defaults */
-       sscanf(ssh->cfg.termspeed, "%d,%d", &ssh->ospeed, &ssh->ispeed);
+       sscanf(conf_get_str(ssh->conf, CONF_termspeed), "%d,%d", &ssh->ospeed, &ssh->ispeed);
        /* Build the pty request. */
        s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
        ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid); /* recipient channel */
        ssh2_pkt_addstring(s->pktout, "pty-req");
        ssh2_pkt_addbool(s->pktout, 1);        /* want reply */
-       ssh2_pkt_addstring(s->pktout, ssh->cfg.termtype);
+       ssh2_pkt_addstring(s->pktout, conf_get_str(ssh->conf, CONF_termtype));
        ssh2_pkt_adduint32(s->pktout, ssh->term_width);
        ssh2_pkt_adduint32(s->pktout, ssh->term_height);
        ssh2_pkt_adduint32(s->pktout, 0);              /* pixel width */
        ssh2_pkt_adduint32(s->pktout, 0);              /* pixel height */
        ssh2_pkt_addstring_start(s->pktout);
-       parse_ttymodes(ssh, ssh->cfg.ttymodes,
-                      ssh2_send_ttymode, (void *)s->pktout);
+       parse_ttymodes(ssh, ssh2_send_ttymode, (void *)s->pktout);
        ssh2_pkt_addbyte(s->pktout, SSH2_TTY_OP_ISPEED);
        ssh2_pkt_adduint32(s->pktout, ssh->ispeed);
        ssh2_pkt_addbyte(s->pktout, SSH2_TTY_OP_OSPEED);
@@ -8709,63 +8932,57 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
      * Simplest thing here is to send all the requests at once, and
      * then wait for a whole bunch of successes or failures.
      */
-    if (ssh->mainchan && !ssh->ncmode && *ssh->cfg.environmt) {
-       char *e = ssh->cfg.environmt;
-       char *var, *varend, *val;
+    if (ssh->mainchan && !ssh->ncmode) {
+       char *key, *val;
 
        s->num_env = 0;
 
-       while (*e) {
-           var = e;
-           while (*e && *e != '\t') e++;
-           varend = e;
-           if (*e == '\t') e++;
-           val = e;
-           while (*e) e++;
-           e++;
-
+       for (val = conf_get_str_strs(ssh->conf, CONF_environmt, NULL, &key);
+            val != NULL;
+            val = conf_get_str_strs(ssh->conf, CONF_environmt, key, &key)) {
            s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
            ssh2_pkt_adduint32(s->pktout, ssh->mainchan->remoteid);
            ssh2_pkt_addstring(s->pktout, "env");
            ssh2_pkt_addbool(s->pktout, 1);            /* want reply */
-           ssh2_pkt_addstring_start(s->pktout);
-           ssh2_pkt_addstring_data(s->pktout, var, varend-var);
+           ssh2_pkt_addstring(s->pktout, key);
            ssh2_pkt_addstring(s->pktout, val);
            ssh2_pkt_send(ssh, s->pktout);
 
            s->num_env++;
        }
 
-       logeventf(ssh, "Sent %d environment variables", s->num_env);
+       if (s->num_env) {
+           logeventf(ssh, "Sent %d environment variables", s->num_env);
 
-       s->env_ok = 0;
-       s->env_left = s->num_env;
+           s->env_ok = 0;
+           s->env_left = s->num_env;
 
-       while (s->env_left > 0) {
-           crWaitUntilV(pktin);
+           while (s->env_left > 0) {
+               crWaitUntilV(pktin);
 
-           if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {
-               if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {
-                   bombout(("Unexpected response to environment request:"
-                            " packet type %d", pktin->type));
-                   crStopV;
+               if (pktin->type != SSH2_MSG_CHANNEL_SUCCESS) {
+                   if (pktin->type != SSH2_MSG_CHANNEL_FAILURE) {
+                       bombout(("Unexpected response to environment request:"
+                                " packet type %d", pktin->type));
+                       crStopV;
+                   }
+               } else {
+                   s->env_ok++;
                }
-           } else {
-               s->env_ok++;
-           }
 
-           s->env_left--;
-       }
+               s->env_left--;
+           }
 
-       if (s->env_ok == s->num_env) {
-           logevent("All environment variables successfully set");
-       } else if (s->env_ok == 0) {
-           logevent("All environment variables refused");
-           c_write_str(ssh, "Server refused to set environment variables\r\n");
-       } else {
-           logeventf(ssh, "%d environment variables refused",
-                     s->num_env - s->env_ok);
-           c_write_str(ssh, "Server refused to set all environment variables\r\n");
+           if (s->env_ok == s->num_env) {
+               logevent("All environment variables successfully set");
+           } else if (s->env_ok == 0) {
+               logevent("All environment variables refused");
+               c_write_str(ssh, "Server refused to set environment variables\r\n");
+           } else {
+               logeventf(ssh, "%d environment variables refused",
+                         s->num_env - s->env_ok);
+               c_write_str(ssh, "Server refused to set all environment variables\r\n");
+           }
        }
     }
 
@@ -8779,12 +8996,11 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
        char *cmd;
 
        if (ssh->fallback_cmd) {
-           subsys = ssh->cfg.ssh_subsys2;
-           cmd = ssh->cfg.remote_cmd_ptr2;
+           subsys = conf_get_int(ssh->conf, CONF_ssh_subsys2);
+           cmd = conf_get_str(ssh->conf, CONF_remote_cmd2);
        } else {
-           subsys = ssh->cfg.ssh_subsys;
-           cmd = ssh->cfg.remote_cmd_ptr;
-           if (!cmd) cmd = ssh->cfg.remote_cmd;
+           subsys = conf_get_int(ssh->conf, CONF_ssh_subsys);
+           cmd = conf_get_str(ssh->conf, CONF_remote_cmd);
        }
 
        s->pktout = ssh2_pkt_init(SSH2_MSG_CHANNEL_REQUEST);
@@ -8817,7 +9033,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
             * not, and if the fallback command exists, try falling
             * back to it before complaining.
             */
-           if (!ssh->fallback_cmd && ssh->cfg.remote_cmd_ptr2 != NULL) {
+           if (!ssh->fallback_cmd &&
+               *conf_get_str(ssh->conf, CONF_remote_cmd2)) {
                logevent("Primary command failed; attempting fallback");
                ssh->fallback_cmd = TRUE;
                continue;
@@ -8877,7 +9094,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
             * Try to send data on all channels if we can.
             */
            for (i = 0; NULL != (c = index234(ssh->channels, i)); i++)
-               ssh2_try_send_and_unthrottle(c);
+               ssh2_try_send_and_unthrottle(ssh, c);
        }
     }
 
@@ -8921,10 +9138,9 @@ static void ssh2_msg_debug(Ssh ssh, struct Packet *pktin)
     /* log the debug message */
     char *msg;
     int msglen;
-    int always_display;
 
-    /* XXX maybe we should actually take notice of this */
-    always_display = ssh2_pkt_getbool(pktin);
+    /* XXX maybe we should actually take notice of the return value */
+    ssh2_pkt_getbool(pktin);
     ssh_pkt_getstring(pktin, &msg, &msglen);
 
     logeventf(ssh, "Remote debug message: %.*s", msglen, msg);
@@ -9008,7 +9224,7 @@ static void ssh2_timer(void *ctx, long now)
     if (ssh->state == SSH_STATE_CLOSED)
        return;
 
-    if (!ssh->kex_in_progress && ssh->cfg.ssh_rekey_time != 0 &&
+    if (!ssh->kex_in_progress && conf_get_int(ssh->conf, CONF_ssh_rekey_time) != 0 &&
        now - ssh->next_rekey >= 0) {
        do_ssh2_transport(ssh, "timeout", -1, NULL);
     }
@@ -9049,21 +9265,26 @@ static void ssh2_protocol(Ssh ssh, void *vin, int inlen,
     }
 }
 
+static void ssh_cache_conf_values(Ssh ssh)
+{
+    ssh->logomitdata = conf_get_int(ssh->conf, CONF_logomitdata);
+}
+
 /*
  * Called to set up the connection.
  *
  * Returns an error message, or NULL on success.
  */
 static const char *ssh_init(void *frontend_handle, void **backend_handle,
-                           Config *cfg,
-                           char *host, int port, char **realhost, int nodelay,
-                           int keepalive)
+                           Conf *conf, char *host, int port, char **realhost,
+                           int nodelay, int keepalive)
 {
     const char *p;
     Ssh ssh;
 
     ssh = snew(struct ssh_tag);
-    ssh->cfg = *cfg;                  /* STRUCTURE COPY */
+    ssh->conf = conf_copy(conf);
+    ssh_cache_conf_values(ssh);
     ssh->version = 0;                 /* when not ready yet */
     ssh->s = NULL;
     ssh->cipher = NULL;
@@ -9125,6 +9346,7 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
     ssh->deferred_rekey_reason = NULL;
     bufchain_init(&ssh->queued_incoming_data);
     ssh->frozen = FALSE;
+    ssh->username = NULL;
 
     *backend_handle = ssh;
 
@@ -9134,8 +9356,8 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
 #endif
 
     ssh->frontend = frontend_handle;
-    ssh->term_width = ssh->cfg.width;
-    ssh->term_height = ssh->cfg.height;
+    ssh->term_width = conf_get_int(ssh->conf, CONF_width);
+    ssh->term_height = conf_get_int(ssh->conf, CONF_height);
 
     ssh->channels = NULL;
     ssh->rportfwds = NULL;
@@ -9156,9 +9378,14 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
 
     ssh->incoming_data_size = ssh->outgoing_data_size =
        ssh->deferred_data_size = 0L;
-    ssh->max_data_size = parse_blocksize(ssh->cfg.ssh_rekey_data);
+    ssh->max_data_size = parse_blocksize(conf_get_str(ssh->conf,
+                                                     CONF_ssh_rekey_data));
     ssh->kex_in_progress = FALSE;
 
+#ifndef NO_GSSAPI
+    ssh->gsslibs = NULL;
+#endif
+
     p = connect_to_host(ssh, host, port, realhost, nodelay, keepalive);
     if (p != NULL)
        return p;
@@ -9219,6 +9446,7 @@ static void ssh_free(void *handle)
                    x11_close(c->u.x11.s);
                break;
              case CHAN_SOCKDATA:
+             case CHAN_SOCKDATA_DORMANT:
                if (c->u.pfd.s != NULL)
                    pfd_close(c->u.pfd.s);
                break;
@@ -9231,7 +9459,7 @@ static void ssh_free(void *handle)
 
     if (ssh->rportfwds) {
        while ((pf = delpos234(ssh->rportfwds, 0)) != NULL)
-           sfree(pf);
+           free_rportfwd(pf);
        freetree234(ssh->rportfwds);
        ssh->rportfwds = NULL;
     }
@@ -9255,6 +9483,12 @@ static void ssh_free(void *handle)
     if (ssh->pinger)
        pinger_free(ssh->pinger);
     bufchain_clear(&ssh->queued_incoming_data);
+    sfree(ssh->username);
+    conf_free(ssh->conf);
+#ifndef NO_GSSAPI
+    if (ssh->gsslibs)
+       ssh_gss_cleanup(ssh->gsslibs);
+#endif
     sfree(ssh);
 
     random_unref();
@@ -9263,19 +9497,21 @@ static void ssh_free(void *handle)
 /*
  * Reconfigure the SSH backend.
  */
-static void ssh_reconfig(void *handle, Config *cfg)
+static void ssh_reconfig(void *handle, Conf *conf)
 {
     Ssh ssh = (Ssh) handle;
     char *rekeying = NULL, rekey_mandatory = FALSE;
     unsigned long old_max_data_size;
+    int i, rekey_time;
 
-    pinger_reconfig(ssh->pinger, &ssh->cfg, cfg);
+    pinger_reconfig(ssh->pinger, ssh->conf, conf);
     if (ssh->portfwds)
-       ssh_setup_portfwd(ssh, cfg);
+       ssh_setup_portfwd(ssh, conf);
 
-    if (ssh->cfg.ssh_rekey_time != cfg->ssh_rekey_time &&
-       cfg->ssh_rekey_time != 0) {
-       long new_next = ssh->last_rekey + cfg->ssh_rekey_time*60*TICKSPERSEC;
+    rekey_time = conf_get_int(conf, CONF_ssh_rekey_time);
+    if (conf_get_int(ssh->conf, CONF_ssh_rekey_time) != rekey_time &&
+       rekey_time != 0) {
+       long new_next = ssh->last_rekey + rekey_time*60*TICKSPERSEC;
        long now = GETTICKCOUNT();
 
        if (new_next - now < 0) {
@@ -9286,7 +9522,8 @@ static void ssh_reconfig(void *handle, Config *cfg)
     }
 
     old_max_data_size = ssh->max_data_size;
-    ssh->max_data_size = parse_blocksize(cfg->ssh_rekey_data);
+    ssh->max_data_size = parse_blocksize(conf_get_str(ssh->conf,
+                                                     CONF_ssh_rekey_data));
     if (old_max_data_size != ssh->max_data_size &&
        ssh->max_data_size != 0) {
        if (ssh->outgoing_data_size > ssh->max_data_size ||
@@ -9294,19 +9531,27 @@ static void ssh_reconfig(void *handle, Config *cfg)
            rekeying = "data limit lowered";
     }
 
-    if (ssh->cfg.compression != cfg->compression) {
+    if (conf_get_int(ssh->conf, CONF_compression) !=
+       conf_get_int(conf, CONF_compression)) {
        rekeying = "compression setting changed";
        rekey_mandatory = TRUE;
     }
 
-    if (ssh->cfg.ssh2_des_cbc != cfg->ssh2_des_cbc ||
-       memcmp(ssh->cfg.ssh_cipherlist, cfg->ssh_cipherlist,
-              sizeof(ssh->cfg.ssh_cipherlist))) {
+    for (i = 0; i < CIPHER_MAX; i++)
+       if (conf_get_int_int(ssh->conf, CONF_ssh_cipherlist, i) !=
+           conf_get_int_int(conf, CONF_ssh_cipherlist, i)) {
+       rekeying = "cipher settings changed";
+       rekey_mandatory = TRUE;
+    }
+    if (conf_get_int(ssh->conf, CONF_ssh2_des_cbc) !=
+       conf_get_int(conf, CONF_ssh2_des_cbc)) {
        rekeying = "cipher settings changed";
        rekey_mandatory = TRUE;
     }
 
-    ssh->cfg = *cfg;                  /* STRUCTURE COPY */
+    conf_free(ssh->conf);
+    ssh->conf = conf_copy(conf);
+    ssh_cache_conf_values(ssh);
 
     if (rekeying) {
        if (!ssh->kex_in_progress) {
@@ -9384,7 +9629,7 @@ static void ssh_size(void *handle, int width, int height)
        ssh->size_needed = TRUE;       /* buffer for later */
        break;
       case SSH_STATE_SESSION:
-       if (!ssh->cfg.nopty) {
+       if (!conf_get_int(ssh->conf, CONF_nopty)) {
            if (ssh->version == 1) {
                send_packet(ssh, SSH1_CMSG_WINDOW_SIZE,
                            PKT_INT, ssh->term_height,
@@ -9415,8 +9660,10 @@ static const struct telnet_special *ssh_get_specials(void *handle)
     static const struct telnet_special ssh1_ignore_special[] = {
        {"IGNORE message", TS_NOP}
     };
-    static const struct telnet_special ssh2_transport_specials[] = {
+    static const struct telnet_special ssh2_ignore_special[] = {
        {"IGNORE message", TS_NOP},
+    };
+    static const struct telnet_special ssh2_rekey_special[] = {
        {"Repeat key exchange", TS_REKEY},
     };
     static const struct telnet_special ssh2_session_specials[] = {
@@ -9441,7 +9688,8 @@ static const struct telnet_special *ssh_get_specials(void *handle)
        {NULL, TS_EXITMENU}
     };
     /* XXX review this length for any changes: */
-    static struct telnet_special ssh_specials[lenof(ssh2_transport_specials) +
+    static struct telnet_special ssh_specials[lenof(ssh2_ignore_special) +
+                                             lenof(ssh2_rekey_special) +
                                              lenof(ssh2_session_specials) +
                                              lenof(specials_end)];
     Ssh ssh = (Ssh) handle;
@@ -9460,7 +9708,10 @@ static const struct telnet_special *ssh_get_specials(void *handle)
        if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE))
            ADD_SPECIALS(ssh1_ignore_special);
     } else if (ssh->version == 2) {
-       ADD_SPECIALS(ssh2_transport_specials);
+       if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE))
+           ADD_SPECIALS(ssh2_ignore_special);
+       if (!(ssh->remote_bugs & BUG_SSH2_REKEY))
+           ADD_SPECIALS(ssh2_rekey_special);
        if (ssh->mainchan)
            ADD_SPECIALS(ssh2_session_specials);
     } /* else we're not ready yet */
@@ -9510,9 +9761,11 @@ static void ssh_special(void *handle, Telnet_Special code)
            if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH1_IGNORE))
                send_packet(ssh, SSH1_MSG_IGNORE, PKT_STR, "", PKT_END);
        } else {
-           pktout = ssh2_pkt_init(SSH2_MSG_IGNORE);
-           ssh2_pkt_addstring_start(pktout);
-           ssh2_pkt_send_noqueue(ssh, pktout);
+           if (!(ssh->remote_bugs & BUG_CHOKES_ON_SSH2_IGNORE)) {
+               pktout = ssh2_pkt_init(SSH2_MSG_IGNORE);
+               ssh2_pkt_addstring_start(pktout);
+               ssh2_pkt_send_noqueue(ssh, pktout);
+           }
        }
     } else if (code == TS_REKEY) {
        if (!ssh->kex_in_progress && ssh->version == 2) {
@@ -9600,7 +9853,7 @@ static void ssh_unthrottle(void *handle, int bufsize)
            ssh2_set_window(ssh->mainchan,
                            bufsize < ssh->mainchan->v.v2.locmaxwin ?
                            ssh->mainchan->v.v2.locmaxwin - bufsize : 0);
-           if (ssh->cfg.ssh_simple)
+           if (conf_get_int(ssh->conf, CONF_ssh_simple))
                buflimit = 0;
            else
                buflimit = ssh->mainchan->v.v2.locmaxwin;