]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - ssh.c
Add SSH_STATE_CLOSED to prevent accidentally sending resizes down a
[PuTTY.git] / ssh.c
diff --git a/ssh.c b/ssh.c
index 8092c9850bf1295065f67e1b097b6278952c5b99..f29f87c4049bab665898ad532cc2e6bce84f23af 100644 (file)
--- a/ssh.c
+++ b/ssh.c
@@ -47,7 +47,8 @@ static char *savedhost;
 static enum {
     SSH_STATE_BEFORE_SIZE,
     SSH_STATE_INTERMED,
-    SSH_STATE_SESSION
+    SSH_STATE_SESSION,
+    SSH_STATE_CLOSED
 } ssh_state = SSH_STATE_BEFORE_SIZE;
 
 static int size_needed = FALSE;
@@ -174,8 +175,8 @@ static void s_wrpkt_start(int type, int len) {
     pktout.length = len-5;
     if (pktout.maxlen < biglen) {
        pktout.maxlen = biglen;
-       pktout.data = (pktout.data == NULL ? malloc(biglen) :
-                      realloc(pktout.data, biglen));
+       pktout.data = (pktout.data == NULL ? malloc(biglen+4) :
+                      realloc(pktout.data, biglen+4));
        if (!pktout.data)
            fatalbox("Out of memory");
     }
@@ -251,6 +252,7 @@ static int do_ssh_init(void) {
     sprintf(vstring, "SSH-%s-7.7.7\n",
            (strcmp(version, "1.5") <= 0 ? version : "1.5"));
     s_write(vstring, strlen(vstring));
+    return 1;
 }
 
 static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
@@ -433,30 +435,32 @@ static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
        }
     }
 
-    i = strlen(cfg.termtype);
-    s_wrpkt_start(10, i+5*4+1);
-    pktout.body[0] = (i >> 24) & 0xFF;
-    pktout.body[1] = (i >> 16) & 0xFF;
-    pktout.body[2] = (i >> 8) & 0xFF;
-    pktout.body[3] = i & 0xFF;
-    memcpy(pktout.body+4, cfg.termtype, i);
-    i += 4;
-    pktout.body[i++] = (rows >> 24) & 0xFF;
-    pktout.body[i++] = (rows >> 16) & 0xFF;
-    pktout.body[i++] = (rows >> 8) & 0xFF;
-    pktout.body[i++] = rows & 0xFF;
-    pktout.body[i++] = (cols >> 24) & 0xFF;
-    pktout.body[i++] = (cols >> 16) & 0xFF;
-    pktout.body[i++] = (cols >> 8) & 0xFF;
-    pktout.body[i++] = cols & 0xFF;
-    memset(pktout.body+i, 0, 9);       /* 0 pixwidth, 0 pixheight, 0.b endofopt */
-    s_wrpkt();
-    ssh_state = SSH_STATE_INTERMED;
-    do { crReturnV; } while (!ispkt);
-    if (pktin.type != 14 && pktin.type != 15) {
-       fatalbox("Protocol confusion");
-    } else if (pktin.type == 15) {
-       c_write("Server refused to allocate pty\r\n", 32);
+    if (!cfg.nopty) {
+        i = strlen(cfg.termtype);
+        s_wrpkt_start(10, i+5*4+1);
+        pktout.body[0] = (i >> 24) & 0xFF;
+        pktout.body[1] = (i >> 16) & 0xFF;
+        pktout.body[2] = (i >> 8) & 0xFF;
+        pktout.body[3] = i & 0xFF;
+        memcpy(pktout.body+4, cfg.termtype, i);
+        i += 4;
+        pktout.body[i++] = (rows >> 24) & 0xFF;
+        pktout.body[i++] = (rows >> 16) & 0xFF;
+        pktout.body[i++] = (rows >> 8) & 0xFF;
+        pktout.body[i++] = rows & 0xFF;
+        pktout.body[i++] = (cols >> 24) & 0xFF;
+        pktout.body[i++] = (cols >> 16) & 0xFF;
+        pktout.body[i++] = (cols >> 8) & 0xFF;
+        pktout.body[i++] = cols & 0xFF;
+        memset(pktout.body+i, 0, 9);       /* 0 pixwidth, 0 pixheight, 0.b endofopt */
+        s_wrpkt();
+        ssh_state = SSH_STATE_INTERMED;
+        do { crReturnV; } while (!ispkt);
+        if (pktin.type != 14 && pktin.type != 15) {
+            fatalbox("Protocol confusion");
+        } else if (pktin.type == 15) {
+            c_write("Server refused to allocate pty\r\n", 32);
+        }
     }
 
     s_wrpkt_start(12, 0);
@@ -475,7 +479,8 @@ static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
                    len = (len << 8) + pktin.body[i];
                c_write(pktin.body+4, len);
            } else if (pktin.type == 1) {
-               /* SSH_MSG_DISCONNECT: do nothing */
+               /* SSH_MSG_DISCONNECT */
+                ssh_state = SSH_STATE_CLOSED;
            } else if (pktin.type == 14) {
                /* SSH_MSG_SUCCESS: may be from EXEC_SHELL on some servers */
            } else if (pktin.type == 15) {
@@ -644,6 +649,7 @@ static int ssh_msg (WPARAM wParam, LPARAM lParam) {
        return 1;
       case FD_CLOSE:
        s = INVALID_SOCKET;
+        ssh_state = SSH_STATE_CLOSED;
        return 0;
     }
     return 1;                         /* shouldn't happen, but WTF */
@@ -665,22 +671,25 @@ static void ssh_send (char *buf, int len) {
 static void ssh_size(void) {
     switch (ssh_state) {
       case SSH_STATE_BEFORE_SIZE:
+      case SSH_STATE_CLOSED:
        break;                         /* do nothing */
       case SSH_STATE_INTERMED:
        size_needed = TRUE;            /* buffer for later */
        break;
       case SSH_STATE_SESSION:
-       s_wrpkt_start(11, 16);
-       pktout.body[0] = (rows >> 24) & 0xFF;
-       pktout.body[1] = (rows >> 16) & 0xFF;
-       pktout.body[2] = (rows >> 8) & 0xFF;
-       pktout.body[3] = rows & 0xFF;
-       pktout.body[4] = (cols >> 24) & 0xFF;
-       pktout.body[5] = (cols >> 16) & 0xFF;
-       pktout.body[6] = (cols >> 8) & 0xFF;
-       pktout.body[7] = cols & 0xFF;
-       memset(pktout.body+8, 0, 8);
-       s_wrpkt();
+        if (!cfg.nopty) {
+            s_wrpkt_start(11, 16);
+            pktout.body[0] = (rows >> 24) & 0xFF;
+            pktout.body[1] = (rows >> 16) & 0xFF;
+            pktout.body[2] = (rows >> 8) & 0xFF;
+            pktout.body[3] = rows & 0xFF;
+            pktout.body[4] = (cols >> 24) & 0xFF;
+            pktout.body[5] = (cols >> 16) & 0xFF;
+            pktout.body[6] = (cols >> 8) & 0xFF;
+            pktout.body[7] = cols & 0xFF;
+            memset(pktout.body+8, 0, 8);
+            s_wrpkt();
+        }
     }
 }