Servers announcing themselves as `Sun_SSH_1.0' apparently cannot
[u/mdw/putty] / ssh.c
diff --git a/ssh.c b/ssh.c
index 163b2ea..f4b6154 100644 (file)
--- a/ssh.c
+++ b/ssh.c
  */
 #define SSH2_PKTCTX_DHGROUP          0x0001
 #define SSH2_PKTCTX_DHGEX            0x0002
+#define SSH2_PKTCTX_KEX_MASK         0x000F
 #define SSH2_PKTCTX_PUBLICKEY        0x0010
 #define SSH2_PKTCTX_PASSWORD         0x0020
 #define SSH2_PKTCTX_KBDINTER         0x0040
@@ -162,7 +163,7 @@ static const char *const ssh2_disconnect_reasons[] = {
 #define BUG_CHOKES_ON_RSA                        8
 #define BUG_SSH2_RSA_PADDING                    16
 #define BUG_SSH2_DERIVEKEY                       32
-/* 64 was BUG_SSH2_DH_GEX, now spare */
+#define BUG_SSH2_REKEY                           64
 #define BUG_SSH2_PK_SESSIONID                   128
 
 #define translate(x) if (type == x) return #x
@@ -520,12 +521,14 @@ struct ssh_portfwd {
     int type;
     unsigned sport, dport;
     char *saddr, *daddr;
+    char *sserv, *dserv;
     struct ssh_rportfwd *remote;
     int addressfamily;
     void *local;
 };
 #define free_portfwd(pf) ( \
-    ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr)) : (void)0 ), sfree(pf) )
+    ((pf) ? (sfree((pf)->saddr), sfree((pf)->daddr), \
+            sfree((pf)->sserv), sfree((pf)->dserv)) : (void)0 ), sfree(pf) )
 
 struct Packet {
     long length;
@@ -748,6 +751,7 @@ struct ssh_tag {
     unsigned long max_data_size;
     int kex_in_progress;
     long next_rekey, last_rekey;
+    char *deferred_rekey_reason;    /* points to STATIC string; don't free */
 };
 
 #define logevent(s) logevent(ssh->frontend, s)
@@ -1735,8 +1739,7 @@ static void ssh2_pkt_send_noqueue(Ssh ssh, struct Packet *pkt)
     if (!ssh->kex_in_progress &&
        ssh->max_data_size != 0 &&
        ssh->outgoing_data_size > ssh->max_data_size)
-       do_ssh2_transport(ssh, "Initiating key re-exchange "
-                         "(too much data sent)", -1, NULL);
+       do_ssh2_transport(ssh, "too much data sent", -1, NULL);
 
     ssh_free_packet(pkt);
 }
@@ -1826,8 +1829,7 @@ static void ssh_pkt_defersend(Ssh ssh)
     if (!ssh->kex_in_progress &&
        ssh->max_data_size != 0 &&
        ssh->outgoing_data_size > ssh->max_data_size)
-       do_ssh2_transport(ssh, "Initiating key re-exchange "
-                         "(too much data sent)", -1, NULL);
+       do_ssh2_transport(ssh, "too much data sent", -1, NULL);
     ssh->deferred_data_size = 0;
 }
 
@@ -2134,6 +2136,16 @@ static void ssh_detect_bugs(Ssh ssh, char *vstring)
        ssh->remote_bugs |= BUG_SSH2_PK_SESSIONID;
        logevent("We believe remote version has SSH2 public-key-session-ID bug");
     }
+
+    if (ssh->cfg.sshbug_rekey2 == FORCE_ON ||
+       (ssh->cfg.sshbug_rekey2 == AUTO &&
+        wc_match("Sun_SSH_1.0", imp))) {
+       /*
+        * These versions have the SSH2 ignore-rekey bug.
+        */
+       ssh->remote_bugs |= BUG_SSH2_REKEY;
+       logevent("We believe remote version has SSH2 ignore-rekey bug");
+    }
 }
 
 /*
@@ -2831,8 +2843,11 @@ static int do_ssh1_login(Ssh ssh, unsigned char *in, int inlen,
        }
 
        /* Warn about chosen cipher if necessary. */
-       if (warn)
+       if (warn) {
+            sk_set_frozen(ssh->s, 1);
            askalg(ssh->frontend, "cipher", cipher_string);
+            sk_set_frozen(ssh->s, 0);
+        }
     }
 
     switch (s->cipher_type) {
@@ -3784,8 +3799,10 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
            pfrec = snew(struct ssh_portfwd);
            pfrec->type = type;
            pfrec->saddr = *saddr ? dupstr(saddr) : NULL;
+           pfrec->sserv = sserv ? dupstr(sports) : NULL;
            pfrec->sport = sport;
            pfrec->daddr = *host ? dupstr(host) : NULL;
+           pfrec->dserv = dserv ? dupstr(dports) : NULL;
            pfrec->dport = dport;
            pfrec->local = NULL;
            pfrec->remote = NULL;
@@ -3882,14 +3899,22 @@ static void ssh_setup_portfwd(Ssh ssh, const Config *cfg)
     for (i = 0; (epf = index234(ssh->portfwds, i)) != NULL; i++)
        if (epf->status == CREATE) {
            char *sportdesc, *dportdesc;
-           sportdesc = dupprintf("%s%s%d",
+           sportdesc = dupprintf("%s%s%s%s%d%s",
                                  epf->saddr ? epf->saddr : "",
                                  epf->saddr ? ":" : "",
-                                 epf->sport);
+                                 epf->sserv ? epf->sserv : "",
+                                 epf->sserv ? "(" : "",
+                                 epf->sport,
+                                 epf->sserv ? ")" : "");
            if (epf->type == 'D') {
                dportdesc = NULL;
            } else {
-               dportdesc = dupprintf("%s:%d", epf->daddr, epf->dport);
+               dportdesc = dupprintf("%s:%s%s%d%s",
+                                     epf->daddr,
+                                     epf->dserv ? epf->dserv : "",
+                                     epf->dserv ? "(" : "",
+                                     epf->dport,
+                                     epf->dserv ? ")" : "");
            }
 
            if (epf->type == 'L') {
@@ -4637,7 +4662,7 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
        int n_preferred_ciphers;
        const struct ssh2_ciphers *preferred_ciphers[CIPHER_MAX];
        const struct ssh_compress *preferred_comp;
-       int first_kex;
+       int got_session_id, activated_authconn;
        struct Packet *pktout;
     };
     crState(do_ssh2_transport_state);
@@ -4648,10 +4673,21 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
     s->csmac_tobe = s->scmac_tobe = NULL;
     s->cscomp_tobe = s->sccomp_tobe = NULL;
 
-    s->first_kex = 1;
+    s->got_session_id = s->activated_authconn = FALSE;
+
+    /*
+     * Be prepared to work around the buggy MAC problem.
+     */
+    if (ssh->remote_bugs & BUG_SSH2_HMAC)
+       s->maclist = buggymacs, s->nmacs = lenof(buggymacs);
+    else
+       s->maclist = macs, s->nmacs = lenof(macs);
 
+  begin_key_exchange:
+    ssh->pkt_ctx &= ~SSH2_PKTCTX_KEX_MASK;
     {
-       int i;
+       int i, j, commalist_started;
+
        /*
         * Set up the preferred key exchange. (NULL => warn below here)
         */
@@ -4679,10 +4715,7 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
                break;
            }
        }
-    }
 
-    {
-       int i;
        /*
         * Set up the preferred ciphers. (NULL => warn below here)
         */
@@ -4712,27 +4745,14 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
                break;
            }
        }
-    }
-
-    /*
-     * Set up preferred compression.
-     */
-    if (ssh->cfg.compression)
-       s->preferred_comp = &ssh_zlib;
-    else
-       s->preferred_comp = &ssh_comp_none;
-
-    /*
-     * Be prepared to work around the buggy MAC problem.
-     */
-    if (ssh->remote_bugs & BUG_SSH2_HMAC)
-       s->maclist = buggymacs, s->nmacs = lenof(buggymacs);
-    else
-       s->maclist = macs, s->nmacs = lenof(macs);
 
-  begin_key_exchange:
-    {
-       int i, j, commalist_started;
+       /*
+        * Set up preferred compression.
+        */
+       if (ssh->cfg.compression)
+           s->preferred_comp = &ssh_zlib;
+       else
+           s->preferred_comp = &ssh_comp_none;
 
        /*
         * Enable queueing of outgoing auth- or connection-layer
@@ -4882,9 +4902,12 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
                ssh->kex = k;
            }
            if (ssh->kex) {
-               if (s->warn)
+               if (s->warn) {
+                    sk_set_frozen(ssh->s, 1);
                    askalg(ssh->frontend, "key-exchange algorithm",
                           ssh->kex->name);
+                    sk_set_frozen(ssh->s, 0);
+                }
                break;
            }
        }
@@ -4915,9 +4938,12 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
                }
            }
            if (s->cscipher_tobe) {
-               if (s->warn)
+               if (s->warn) {
+                    sk_set_frozen(ssh->s, 1);
                    askalg(ssh->frontend, "client-to-server cipher",
                           s->cscipher_tobe->name);
+                    sk_set_frozen(ssh->s, 0);
+                }
                break;
            }
        }
@@ -4942,9 +4968,12 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
                }
            }
            if (s->sccipher_tobe) {
-               if (s->warn)
+               if (s->warn) {
+                    sk_set_frozen(ssh->s, 1);
                    askalg(ssh->frontend, "server-to-client cipher",
                           s->sccipher_tobe->name);
+                    sk_set_frozen(ssh->s, 0);
+                }
                break;
            }
        }
@@ -5101,10 +5130,12 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
      */
     s->keystr = ssh->hostkey->fmtkey(s->hkey);
     s->fingerprint = ssh->hostkey->fingerprint(s->hkey);
+    sk_set_frozen(ssh->s, 1);
     verify_ssh_host_key(ssh->frontend,
                        ssh->savedhost, ssh->savedport, ssh->hostkey->keytype,
                        s->keystr, s->fingerprint);
-    if (s->first_kex) {                       /* don't bother logging this in rekeys */
+    sk_set_frozen(ssh->s, 0);
+    if (!s->got_session_id) {     /* don't bother logging this in rekeys */
        logevent("Host key fingerprint is:");
        logevent(s->fingerprint);
     }
@@ -5117,9 +5148,11 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
      * the session id, used in session key construction and
      * authentication.
      */
-    if (s->first_kex)
+    if (!s->got_session_id) {
        memcpy(ssh->v2_session_id, s->exchange_hash,
               sizeof(s->exchange_hash));
+       s->got_session_id = TRUE;
+    }
 
     /*
      * Send SSH2_MSG_NEWKEYS.
@@ -5237,14 +5270,25 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
     }
 
     /*
-     * Key exchange is over. Schedule a timer for our next rekey.
+     * Key exchange is over. Loop straight back round if we have a
+     * deferred rekey reason.
+     */
+    if (ssh->deferred_rekey_reason) {
+       logevent(ssh->deferred_rekey_reason);
+       pktin = NULL;
+       ssh->deferred_rekey_reason = NULL;
+       goto begin_key_exchange;
+    }
+
+    /*
+     * Otherwise, schedule a timer for our next rekey.
      */
     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,
                                         ssh2_timer, ssh);
-    
+
     /*
      * If this is the first key exchange phase, we must pass the
      * SSH2_MSG_NEWKEYS packet to the next layer, not because it
@@ -5253,10 +5297,10 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
      * exchange phases, we don't pass SSH2_MSG_NEWKEYS on, because
      * it would only confuse the layer above.
      */
-    if (!s->first_kex) {
+    if (s->activated_authconn) {
        crReturn(1);
     }
-    s->first_kex = 0;
+    s->activated_authconn = TRUE;
 
     /*
      * Now we're encrypting. Begin returning 1 to the protocol main
@@ -5271,12 +5315,35 @@ static int do_ssh2_transport(Ssh ssh, unsigned char *in, int inlen,
      */
     while (!((pktin && pktin->type == SSH2_MSG_KEXINIT) ||
             (!pktin && inlen == -1))) {
+        wait_for_rekey:
        crReturn(1);
     }
     if (pktin) {
        logevent("Server initiated key re-exchange");
     } else {
-       logevent((char *)in);
+        /*
+         * 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,
+         * we process it anyway!)
+         */
+        if ((ssh->remote_bugs & BUG_SSH2_REKEY)) {
+            logeventf(ssh, "Server bug prevents key re-exchange (%s)",
+                      (char *)in);
+            /* Reset the counters, so that at least this message doesn't
+             * hit the event log _too_ often. */
+            ssh->outgoing_data_size = 0;
+            ssh->incoming_data_size = 0;
+            if (ssh->cfg.ssh_rekey_time != 0) {
+                ssh->next_rekey =
+                    schedule_timer(ssh->cfg.ssh_rekey_time*60*TICKSPERSEC,
+                                   ssh2_timer, ssh);
+            }
+            goto wait_for_rekey;       /* this is utterly horrid */
+        } else {
+            logeventf(ssh, "Initiating key re-exchange (%s)", (char *)in);
+            logevent((char *)in);
+        }
     }
     goto begin_key_exchange;
 
@@ -5901,8 +5968,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
        } type;
        int gotit, need_pw, can_pubkey, can_passwd, can_keyb_inter;
-       int tried_pubkey_config, tried_agent, tried_keyb_inter;
-       int kbd_inter_running;
+       int tried_pubkey_config, tried_agent;
+       int kbd_inter_running, kbd_inter_refused;
        int we_are_in;
        int num_prompts, curr_prompt, echo;
        char username[100];
@@ -6031,8 +6098,8 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
 
        s->tried_pubkey_config = FALSE;
        s->tried_agent = FALSE;
-       s->tried_keyb_inter = FALSE;
        s->kbd_inter_running = FALSE;
+       s->kbd_inter_refused = FALSE;
        /* Load the pub half of ssh->cfg.keyfile so we notice if it's in Pageant */
        if (!filename_is_null(ssh->cfg.keyfile)) {
            int keytype;
@@ -6099,6 +6166,10 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                 */
                if (!s->gotit)
                    s->curr_prompt = 0;
+           } else if (pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) {
+               /* FIXME: perhaps we should support this? */
+               bombout(("PASSWD_CHANGEREQ not yet supported"));
+               crStopV;
            } else if (pktin->type != SSH2_MSG_USERAUTH_FAILURE) {
                bombout(("Strange packet received during authentication: type %d",
                         pktin->type));
@@ -6423,10 +6494,10 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                }
            }
 
-           if (!s->method && s->can_keyb_inter && !s->tried_keyb_inter) {
+           if (!s->method && s->can_keyb_inter && !s->kbd_inter_refused &&
+               !s->kbd_inter_running) {
                s->method = AUTH_KEYBOARD_INTERACTIVE;
                s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
-               s->tried_keyb_inter = TRUE;
 
                ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK;
                ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER;
@@ -6445,6 +6516,7 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
                        s->gotit = TRUE;
                    logevent("Keyboard-interactive authentication refused");
                    s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;
+                   s->kbd_inter_refused = TRUE; /* don't try it again */
                    continue;
                }
 
@@ -6455,7 +6527,6 @@ static void do_ssh2_authconn(Ssh ssh, unsigned char *in, int inlen,
            if (s->kbd_inter_running) {
                s->method = AUTH_KEYBOARD_INTERACTIVE;
                s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
-               s->tried_keyb_inter = TRUE;
 
                ssh->pkt_ctx &= ~SSH2_PKTCTX_AUTH_MASK;
                ssh->pkt_ctx |= SSH2_PKTCTX_KBDINTER;
@@ -7246,8 +7317,7 @@ static void ssh2_timer(void *ctx, long now)
 
     if (!ssh->kex_in_progress && ssh->cfg.ssh_rekey_time != 0 &&
        now - ssh->next_rekey >= 0) {
-       do_ssh2_transport(ssh, "Initiating key re-exchange (timeout)",
-                         -1, NULL);
+       do_ssh2_transport(ssh, "timeout", -1, NULL);
     }
 }
 
@@ -7262,8 +7332,7 @@ static void ssh2_protocol(Ssh ssh, unsigned char *in, int inlen,
        if (!ssh->kex_in_progress &&
            ssh->max_data_size != 0 &&
            ssh->incoming_data_size > ssh->max_data_size)
-           do_ssh2_transport(ssh, "Initiating key re-exchange "
-                             "(too much data received)", -1, NULL);
+           do_ssh2_transport(ssh, "too much data received", -1, NULL);
     }
 
     if (pktin && ssh->packet_dispatch[pktin->type]) {
@@ -7354,6 +7423,7 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
     ssh->queuelen = ssh->queuesize = 0;
     ssh->queueing = FALSE;
     ssh->qhead = ssh->qtail = NULL;
+    ssh->deferred_rekey_reason = NULL;
 
     *backend_handle = ssh;
 
@@ -7491,7 +7561,7 @@ static void ssh_free(void *handle)
 static void ssh_reconfig(void *handle, Config *cfg)
 {
     Ssh ssh = (Ssh) handle;
-    char *rekeying = NULL;
+    char *rekeying = NULL, rekey_mandatory = FALSE;
     unsigned long old_max_data_size;
 
     pinger_reconfig(ssh->pinger, &ssh->cfg, cfg);
@@ -7503,7 +7573,7 @@ static void ssh_reconfig(void *handle, Config *cfg)
        long now = GETTICKCOUNT();
 
        if (new_next - now < 0) {
-           rekeying = "Initiating key re-exchange (timeout shortened)";
+           rekeying = "timeout shortened";
        } else {
            ssh->next_rekey = schedule_timer(new_next - now, ssh2_timer, ssh);
        }
@@ -7515,14 +7585,30 @@ static void ssh_reconfig(void *handle, Config *cfg)
        ssh->max_data_size != 0) {
        if (ssh->outgoing_data_size > ssh->max_data_size ||
            ssh->incoming_data_size > ssh->max_data_size)
-           rekeying = "Initiating key re-exchange (data limit lowered)";
+           rekeying = "data limit lowered";
+    }
+
+    if (ssh->cfg.compression != cfg->compression) {
+       rekeying = "compression setting changed";
+       rekey_mandatory = TRUE;
     }
 
-    if (rekeying && !ssh->kex_in_progress) {
-       do_ssh2_transport(ssh, rekeying, -1, NULL);
+    if (ssh->cfg.ssh2_des_cbc != cfg->ssh2_des_cbc ||
+       memcmp(ssh->cfg.ssh_cipherlist, cfg->ssh_cipherlist,
+              sizeof(ssh->cfg.ssh_cipherlist))) {
+       rekeying = "cipher settings changed";
+       rekey_mandatory = TRUE;
     }
 
     ssh->cfg = *cfg;                  /* STRUCTURE COPY */
+
+    if (rekeying) {
+       if (!ssh->kex_in_progress) {
+           do_ssh2_transport(ssh, rekeying, -1, NULL);
+       } else if (rekey_mandatory) {
+           ssh->deferred_rekey_reason = rekeying;
+       }
+    }
 }
 
 /*
@@ -7723,8 +7809,7 @@ static void ssh_special(void *handle, Telnet_Special code)
        }
     } else if (code == TS_REKEY) {
        if (!ssh->kex_in_progress && ssh->version == 2) {
-           do_ssh2_transport(ssh, "Initiating key re-exchange at"
-                             " user request", -1, NULL);
+           do_ssh2_transport(ssh, "at user request", -1, NULL);
        }
     } else if (code == TS_BRK) {
        if (ssh->state == SSH_STATE_CLOSED