OSDN Git Service

Add PuTTY 0.61 to contrib directory.
[ffftp/ffftp.git] / contrib / putty / PORTFWD.C
diff --git a/contrib/putty/PORTFWD.C b/contrib/putty/PORTFWD.C
new file mode 100644 (file)
index 0000000..e5874a6
--- /dev/null
@@ -0,0 +1,556 @@
+/*\r
+ * SSH port forwarding.\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+\r
+#include "putty.h"\r
+#include "ssh.h"\r
+\r
+#ifndef FALSE\r
+#define FALSE 0\r
+#endif\r
+#ifndef TRUE\r
+#define TRUE 1\r
+#endif\r
+\r
+struct PFwdPrivate {\r
+    const struct plug_function_table *fn;\r
+    /* the above variable absolutely *must* be the first in this structure */\r
+    void *c;                          /* (channel) data used by ssh.c */\r
+    void *backhandle;                 /* instance of SSH backend itself */\r
+    /* Note that backhandle need not be filled in if c is non-NULL */\r
+    Socket s;\r
+    int throttled, throttle_override;\r
+    int ready;\r
+    /*\r
+     * `dynamic' does double duty. It's set to 0 for an ordinary\r
+     * forwarded port, and nonzero for SOCKS-style dynamic port\r
+     * forwarding; but it also represents the state of the SOCKS\r
+     * exchange.\r
+     */\r
+    int dynamic;\r
+    /*\r
+     * `hostname' and `port' are the real hostname and port, once\r
+     * we know what we're connecting to; they're unused for this\r
+     * purpose while conducting a local SOCKS exchange, which means\r
+     * we can also use them as a buffer and pointer for reading\r
+     * data from the SOCKS client.\r
+     */\r
+    char hostname[256+8];\r
+    int port;\r
+    /*\r
+     * When doing dynamic port forwarding, we can receive\r
+     * connection data before we are actually able to send it; so\r
+     * we may have to temporarily hold some in a dynamically\r
+     * allocated buffer here.\r
+     */\r
+    void *buffer;\r
+    int buflen;\r
+};\r
+\r
+static void pfd_log(Plug plug, int type, SockAddr addr, int port,\r
+                   const char *error_msg, int error_code)\r
+{\r
+    /* we have to dump these since we have no interface to logging.c */\r
+}\r
+\r
+static int pfd_closing(Plug plug, const char *error_msg, int error_code,\r
+                      int calling_back)\r
+{\r
+    struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;\r
+\r
+    /*\r
+     * We have no way to communicate down the forwarded connection,\r
+     * so if an error occurred on the socket, we just ignore it\r
+     * and treat it like a proper close.\r
+     */\r
+    if (pr->c)\r
+       sshfwd_close(pr->c);\r
+    pfd_close(pr->s);\r
+    return 1;\r
+}\r
+\r
+static int pfd_receive(Plug plug, int urgent, char *data, int len)\r
+{\r
+    struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;\r
+    if (pr->dynamic) {\r
+       while (len--) {\r
+           /*\r
+            * Throughout SOCKS negotiation, "hostname" is re-used as a\r
+            * random protocol buffer with "port" storing the length.\r
+            */ \r
+           if (pr->port >= lenof(pr->hostname)) {\r
+               /* Request too long. */\r
+               if ((pr->dynamic >> 12) == 4) {\r
+                   /* Send back a SOCKS 4 error before closing. */\r
+                   char data[8];\r
+                   memset(data, 0, sizeof(data));\r
+                   data[1] = 91;      /* generic `request rejected' */\r
+                   sk_write(pr->s, data, 8);\r
+               }\r
+               pfd_close(pr->s);\r
+               return 1;\r
+           }\r
+           pr->hostname[pr->port++] = *data++;\r
+\r
+           /*\r
+            * Now check what's in the buffer to see if it's a\r
+            * valid and complete message in the SOCKS exchange.\r
+            */\r
+           if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 4) &&\r
+               pr->hostname[0] == 4) {\r
+               /*\r
+                * SOCKS 4.\r
+                */\r
+               if (pr->dynamic == 1)\r
+                   pr->dynamic = 0x4000;\r
+               if (pr->port < 2) continue;/* don't have command code yet */\r
+               if (pr->hostname[1] != 1) {\r
+                   /* Not CONNECT. */\r
+                   /* Send back a SOCKS 4 error before closing. */\r
+                   char data[8];\r
+                   memset(data, 0, sizeof(data));\r
+                   data[1] = 91;      /* generic `request rejected' */\r
+                   sk_write(pr->s, data, 8);\r
+                   pfd_close(pr->s);\r
+                   return 1;\r
+               }\r
+               if (pr->port <= 8) continue; /* haven't started user/hostname */\r
+               if (pr->hostname[pr->port-1] != 0)\r
+                   continue;          /* haven't _finished_ user/hostname */\r
+               /*\r
+                * Now we have a full SOCKS 4 request. Check it to\r
+                * see if it's a SOCKS 4A request.\r
+                */\r
+               if (pr->hostname[4] == 0 && pr->hostname[5] == 0 &&\r
+                   pr->hostname[6] == 0 && pr->hostname[7] != 0) {\r
+                   /*\r
+                    * It's SOCKS 4A. So if we haven't yet\r
+                    * collected the host name, we should continue\r
+                    * waiting for data in order to do so; if we\r
+                    * have, we can go ahead.\r
+                    */\r
+                   int len;\r
+                   if (pr->dynamic == 0x4000) {\r
+                       pr->dynamic = 0x4001;\r
+                       pr->port = 8;      /* reset buffer to overwrite name */\r
+                       continue;\r
+                   }\r
+                   pr->hostname[0] = 0;   /* reply version code */\r
+                   pr->hostname[1] = 90;   /* request granted */\r
+                   sk_write(pr->s, pr->hostname, 8);\r
+                   len= pr->port - 8;\r
+                   pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);\r
+                   memmove(pr->hostname, pr->hostname + 8, len);\r
+                   goto connect;\r
+               } else {\r
+                   /*\r
+                    * It's SOCKS 4, which means we should format\r
+                    * the IP address into the hostname string and\r
+                    * then just go.\r
+                    */\r
+                   pr->hostname[0] = 0;   /* reply version code */\r
+                   pr->hostname[1] = 90;   /* request granted */\r
+                   sk_write(pr->s, pr->hostname, 8);\r
+                   pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);\r
+                   sprintf(pr->hostname, "%d.%d.%d.%d",\r
+                           (unsigned char)pr->hostname[4],\r
+                           (unsigned char)pr->hostname[5],\r
+                           (unsigned char)pr->hostname[6],\r
+                           (unsigned char)pr->hostname[7]);\r
+                   goto connect;\r
+               }\r
+           }\r
+\r
+           if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 5) &&\r
+               pr->hostname[0] == 5) {\r
+               /*\r
+                * SOCKS 5.\r
+                */\r
+               if (pr->dynamic == 1)\r
+                   pr->dynamic = 0x5000;\r
+\r
+               if (pr->dynamic == 0x5000) {\r
+                   int i, method;\r
+                   char data[2];\r
+                   /*\r
+                    * We're receiving a set of method identifiers.\r
+                    */\r
+                   if (pr->port < 2) continue;/* no method count yet */\r
+                   if (pr->port < 2 + (unsigned char)pr->hostname[1])\r
+                       continue;      /* no methods yet */\r
+                   method = 0xFF;     /* invalid */\r
+                   for (i = 0; i < (unsigned char)pr->hostname[1]; i++)\r
+                       if (pr->hostname[2+i] == 0) {\r
+                           method = 0;/* no auth */\r
+                           break;\r
+                       }\r
+                   data[0] = 5;\r
+                   data[1] = method;\r
+                   sk_write(pr->s, data, 2);\r
+                   pr->dynamic = 0x5001;\r
+                   pr->port = 0;      /* re-empty the buffer */\r
+                   continue;\r
+               }\r
+\r
+               if (pr->dynamic == 0x5001) {\r
+                   /*\r
+                    * We're receiving a SOCKS request.\r
+                    */\r
+                   unsigned char reply[10]; /* SOCKS5 atyp=1 reply */\r
+                   int atype, alen = 0;\r
+\r
+                   /*\r
+                    * Pre-fill reply packet.\r
+                    * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0\r
+                    * (atyp=1) in the reply; if we succeed, we don't know\r
+                    * the right answers, and if we fail, they should be\r
+                    * ignored.\r
+                    */\r
+                   memset(reply, 0, lenof(reply));\r
+                   reply[0] = 5; /* VER */\r
+                   reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */\r
+\r
+                   if (pr->port < 6) continue;\r
+                   atype = (unsigned char)pr->hostname[3];\r
+                   if (atype == 1)    /* IPv4 address */\r
+                       alen = 4;\r
+                   if (atype == 4)    /* IPv6 address */\r
+                       alen = 16;\r
+                   if (atype == 3)    /* domain name has leading length */\r
+                       alen = 1 + (unsigned char)pr->hostname[4];\r
+                   if (pr->port < 6 + alen) continue;\r
+                   if (pr->hostname[1] != 1 || pr->hostname[2] != 0) {\r
+                       /* Not CONNECT or reserved field nonzero - error */\r
+                       reply[1] = 1;   /* generic failure */\r
+                       sk_write(pr->s, (char *) reply, lenof(reply));\r
+                       pfd_close(pr->s);\r
+                       return 1;\r
+                   }\r
+                   /*\r
+                    * Now we have a viable connect request. Switch\r
+                    * on atype.\r
+                    */\r
+                   pr->port = GET_16BIT_MSB_FIRST(pr->hostname+4+alen);\r
+                   if (atype == 1) {\r
+                       /* REP=0 (success) already */\r
+                       sk_write(pr->s, (char *) reply, lenof(reply));\r
+                       sprintf(pr->hostname, "%d.%d.%d.%d",\r
+                               (unsigned char)pr->hostname[4],\r
+                               (unsigned char)pr->hostname[5],\r
+                               (unsigned char)pr->hostname[6],\r
+                               (unsigned char)pr->hostname[7]);\r
+                       goto connect;\r
+                   } else if (atype == 3) {\r
+                       /* REP=0 (success) already */\r
+                       sk_write(pr->s, (char *) reply, lenof(reply));\r
+                       memmove(pr->hostname, pr->hostname + 5, alen-1);\r
+                       pr->hostname[alen-1] = '\0';\r
+                       goto connect;\r
+                   } else {\r
+                       /*\r
+                        * Unknown address type. (FIXME: support IPv6!)\r
+                        */\r
+                       reply[1] = 8;   /* atype not supported */\r
+                       sk_write(pr->s, (char *) reply, lenof(reply));\r
+                       pfd_close(pr->s);\r
+                       return 1;\r
+                   }\r
+               }\r
+           }\r
+\r
+           /*\r
+            * If we get here without either having done `continue'\r
+            * or `goto connect', it must be because there is no\r
+            * sensible interpretation of what's in our buffer. So\r
+            * close the connection rudely.\r
+            */\r
+           pfd_close(pr->s);\r
+           return 1;\r
+       }\r
+       return 1;\r
+\r
+       /*\r
+        * We come here when we're ready to make an actual\r
+        * connection.\r
+        */\r
+       connect:\r
+\r
+       /*\r
+        * Freeze the socket until the SSH server confirms the\r
+        * connection.\r
+        */\r
+       sk_set_frozen(pr->s, 1);\r
+\r
+       pr->c = new_sock_channel(pr->backhandle, pr->s);\r
+       if (pr->c == NULL) {\r
+           pfd_close(pr->s);\r
+           return 1;\r
+       } else {\r
+           /* asks to forward to the specified host/port for this */\r
+           ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");\r
+       }\r
+       pr->dynamic = 0;\r
+\r
+       /*\r
+        * If there's any data remaining in our current buffer,\r
+        * save it to be sent on pfd_confirm().\r
+        */\r
+       if (len > 0) {\r
+           pr->buffer = snewn(len, char);\r
+           memcpy(pr->buffer, data, len);\r
+           pr->buflen = len;\r
+       }\r
+    }\r
+    if (pr->ready) {\r
+       if (sshfwd_write(pr->c, data, len) > 0) {\r
+           pr->throttled = 1;\r
+           sk_set_frozen(pr->s, 1);\r
+       }\r
+    }\r
+    return 1;\r
+}\r
+\r
+static void pfd_sent(Plug plug, int bufsize)\r
+{\r
+    struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;\r
+\r
+    if (pr->c)\r
+       sshfwd_unthrottle(pr->c, bufsize);\r
+}\r
+\r
+/*\r
+ * Called when receiving a PORT OPEN from the server\r
+ */\r
+const char *pfd_newconnect(Socket *s, char *hostname, int port,\r
+                          void *c, const Config *cfg, int addressfamily)\r
+{\r
+    static const struct plug_function_table fn_table = {\r
+       pfd_log,\r
+       pfd_closing,\r
+       pfd_receive,\r
+       pfd_sent,\r
+       NULL\r
+    };\r
+\r
+    SockAddr addr;\r
+    const char *err;\r
+    char *dummy_realhost;\r
+    struct PFwdPrivate *pr;\r
+\r
+    /*\r
+     * Try to find host.\r
+     */\r
+    addr = name_lookup(hostname, port, &dummy_realhost, cfg, addressfamily);\r
+    if ((err = sk_addr_error(addr)) != NULL) {\r
+       sk_addr_free(addr);\r
+       return err;\r
+    }\r
+\r
+    /*\r
+     * Open socket.\r
+     */\r
+    pr = snew(struct PFwdPrivate);\r
+    pr->buffer = NULL;\r
+    pr->fn = &fn_table;\r
+    pr->throttled = pr->throttle_override = 0;\r
+    pr->ready = 1;\r
+    pr->c = c;\r
+    pr->backhandle = NULL;            /* we shouldn't need this */\r
+    pr->dynamic = 0;\r
+\r
+    pr->s = *s = new_connection(addr, dummy_realhost, port,\r
+                               0, 1, 0, 0, (Plug) pr, cfg);\r
+    if ((err = sk_socket_error(*s)) != NULL) {\r
+       sfree(pr);\r
+       return err;\r
+    }\r
+\r
+    sk_set_private_ptr(*s, pr);\r
+    return NULL;\r
+}\r
+\r
+/*\r
+ called when someone connects to the local port\r
+ */\r
+\r
+static int pfd_accepting(Plug p, OSSocket sock)\r
+{\r
+    static const struct plug_function_table fn_table = {\r
+       pfd_log,\r
+       pfd_closing,\r
+       pfd_receive,\r
+       pfd_sent,\r
+       NULL\r
+    };\r
+    struct PFwdPrivate *pr, *org;\r
+    Socket s;\r
+    const char *err;\r
+\r
+    org = (struct PFwdPrivate *)p;\r
+    pr = snew(struct PFwdPrivate);\r
+    pr->buffer = NULL;\r
+    pr->fn = &fn_table;\r
+\r
+    pr->c = NULL;\r
+    pr->backhandle = org->backhandle;\r
+\r
+    pr->s = s = sk_register(sock, (Plug) pr);\r
+    if ((err = sk_socket_error(s)) != NULL) {\r
+       sfree(pr);\r
+       return err != NULL;\r
+    }\r
+\r
+    sk_set_private_ptr(s, pr);\r
+\r
+    pr->throttled = pr->throttle_override = 0;\r
+    pr->ready = 0;\r
+\r
+    if (org->dynamic) {\r
+       pr->dynamic = 1;\r
+       pr->port = 0;                  /* "hostname" buffer is so far empty */\r
+       sk_set_frozen(s, 0);           /* we want to receive SOCKS _now_! */\r
+    } else {\r
+       pr->dynamic = 0;\r
+       strcpy(pr->hostname, org->hostname);\r
+       pr->port = org->port;   \r
+       pr->c = new_sock_channel(org->backhandle, s);\r
+\r
+       if (pr->c == NULL) {\r
+           sfree(pr);\r
+           return 1;\r
+       } else {\r
+           /* asks to forward to the specified host/port for this */\r
+           ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");\r
+       }\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+\r
+/* Add a new forwarding from port -> desthost:destport\r
+ sets up a listener on the local machine on (srcaddr:)port\r
+ */\r
+const char *pfd_addforward(char *desthost, int destport, char *srcaddr,\r
+                          int port, void *backhandle, const Config *cfg,\r
+                          void **sockdata, int address_family)\r
+{\r
+    static const struct plug_function_table fn_table = {\r
+       pfd_log,\r
+       pfd_closing,\r
+       pfd_receive,                   /* should not happen... */\r
+       pfd_sent,                      /* also should not happen */\r
+       pfd_accepting\r
+    };\r
+\r
+    const char *err;\r
+    struct PFwdPrivate *pr;\r
+    Socket s;\r
+\r
+    /*\r
+     * Open socket.\r
+     */\r
+    pr = snew(struct PFwdPrivate);\r
+    pr->buffer = NULL;\r
+    pr->fn = &fn_table;\r
+    pr->c = NULL;\r
+    if (desthost) {\r
+       strcpy(pr->hostname, desthost);\r
+       pr->port = destport;\r
+       pr->dynamic = 0;\r
+    } else\r
+       pr->dynamic = 1;\r
+    pr->throttled = pr->throttle_override = 0;\r
+    pr->ready = 0;\r
+    pr->backhandle = backhandle;\r
+\r
+    pr->s = s = new_listener(srcaddr, port, (Plug) pr,\r
+                            !cfg->lport_acceptall, cfg, address_family);\r
+    if ((err = sk_socket_error(s)) != NULL) {\r
+       sfree(pr);\r
+       return err;\r
+    }\r
+\r
+    sk_set_private_ptr(s, pr);\r
+\r
+    *sockdata = (void *)s;\r
+\r
+    return NULL;\r
+}\r
+\r
+void pfd_close(Socket s)\r
+{\r
+    struct PFwdPrivate *pr;\r
+\r
+    if (!s)\r
+       return;\r
+\r
+    pr = (struct PFwdPrivate *) sk_get_private_ptr(s);\r
+\r
+    sfree(pr->buffer);\r
+    sfree(pr);\r
+\r
+    sk_close(s);\r
+}\r
+\r
+/*\r
+ * Terminate a listener.\r
+ */\r
+void pfd_terminate(void *sv)\r
+{\r
+    pfd_close((Socket)sv);\r
+}\r
+\r
+void pfd_unthrottle(Socket s)\r
+{\r
+    struct PFwdPrivate *pr;\r
+    if (!s)\r
+       return;\r
+    pr = (struct PFwdPrivate *) sk_get_private_ptr(s);\r
+\r
+    pr->throttled = 0;\r
+    sk_set_frozen(s, pr->throttled || pr->throttle_override);\r
+}\r
+\r
+void pfd_override_throttle(Socket s, int enable)\r
+{\r
+    struct PFwdPrivate *pr;\r
+    if (!s)\r
+       return;\r
+    pr = (struct PFwdPrivate *) sk_get_private_ptr(s);\r
+\r
+    pr->throttle_override = enable;\r
+    sk_set_frozen(s, pr->throttled || pr->throttle_override);\r
+}\r
+\r
+/*\r
+ * Called to send data down the raw connection.\r
+ */\r
+int pfd_send(Socket s, char *data, int len)\r
+{\r
+    if (s == NULL)\r
+       return 0;\r
+    return sk_write(s, data, len);\r
+}\r
+\r
+\r
+void pfd_confirm(Socket s)\r
+{\r
+    struct PFwdPrivate *pr;\r
+\r
+    if (s == NULL)\r
+       return;\r
+\r
+    pr = (struct PFwdPrivate *) sk_get_private_ptr(s);\r
+    pr->ready = 1;\r
+    sk_set_frozen(s, 0);\r
+    sk_write(s, NULL, 0);\r
+    if (pr->buffer) {\r
+       sshfwd_write(pr->c, pr->buffer, pr->buflen);\r
+       sfree(pr->buffer);\r
+       pr->buffer = NULL;\r
+    }\r
+}\r