added RSYNC_PROXY support from Stephen Rothwell. This allows access to
authorAndrew Tridgell <tridge@samba.org>
Tue, 13 Apr 1999 03:53:30 +0000 (03:53 +0000)
committerAndrew Tridgell <tridge@samba.org>
Tue, 13 Apr 1999 03:53:30 +0000 (03:53 +0000)
rsync servers via a web proxy (useful for getting through firewalls)

rsync.yo
socket.c

index 2ba99f8..459fb44 100644 (file)
--- a/rsync.yo
+++ b/rsync.yo
@@ -139,6 +139,11 @@ It is also possible to use rsync without using rsh or ssh as the
 transport. In this case you will connect to a remote rsync server
 running on TCP port 873. 
 
+You may establish the connetcion via a web proxy by setting the
+environment variable RSYNC_PROXY to a hostname:port pair pointing to
+your web proxy. Note that your web proxy must allow proxying to port
+873, this must be configured in your proxy servers ruleset.
+
 Using rsync in this way is the same as using it with rsh or ssh except
 that:
 
@@ -727,6 +732,10 @@ dit(bf(RSYNC_RSH)) The RSYNC_RSH environment variable allows you to
 override the default shell used as the transport for rsync. This can
 be used instead of the -e option.
 
+dit(bf(RSYNC_PROXY)) The RSYNC_PROXY environment variable allows you to
+redirect your rsync client to use a web proxy when connecting to a
+rsync daemon. You should set RSYNC_PROXY to a hostname:port pair.
+
 dit(bf(RSYNC_PASSWORD)) Setting RSYNC_PASSWORD to the required
 password allows you to run authenticated rsync connections to a rsync
 daemon without user intervention. Note that this does not supply a
index e7e98a6..eb25d8c 100644 (file)
--- a/socket.c
+++ b/socket.c
 
 #include "rsync.h"
 
+
+/* establish a proxy connection on an open socket to a web roxy by using the CONNECT
+   method */
+static int establish_proxy_connection(int fd, char *host, int port)
+{
+       char buffer[1024];
+       char *cp;
+
+       slprintf(buffer, sizeof(buffer), "CONNECT %s:%d HTTP/1.0\r\n\r\n", host, port);
+       if (write(fd, buffer, strlen(buffer)) != strlen(buffer)) {
+               rprintf(FERROR, "failed to write to proxy - %s\n",
+                       strerror(errno));
+               return -1;
+       }
+
+       for (cp = buffer; cp < &buffer[sizeof(buffer) - 1]; cp++) {
+               if (read(fd, cp, 1) != 1) {
+                       rprintf(FERROR, "failed to read from proxy\n");
+                       return -1;
+               }
+               if (*cp == '\n')
+                       break;
+       }
+
+       if (*cp != '\n')
+               cp++;
+       *cp-- = '\0';
+       if (*cp == '\r')
+               *cp = '\0';
+       if (strncmp(buffer, "HTTP/", 5) != 0) {
+               rprintf(FERROR, "bad response from proxy - %s\n",
+                       buffer);
+               return -1;
+       }
+       for (cp = &buffer[5]; isdigit(*cp) || (*cp == '.'); cp++)
+               ;
+       while (*cp == ' ')
+               cp++;
+       if (*cp != '2') {
+               rprintf(FERROR, "bad response from proxy - %s\n",
+                       buffer);
+               return -1;
+       }
+       /* throw away the rest of the HTTP header */
+       while (1) {
+               for (cp = buffer; cp < &buffer[sizeof(buffer) - 1];
+                    cp++) {
+                       if (read(fd, cp, 1) != 1) {
+                               rprintf(FERROR, "failed to read from proxy\n");
+                               return -1;
+                       }
+                       if (*cp == '\n')
+                               break;
+               }
+               if ((cp > buffer) && (*cp == '\n'))
+                       cp--;
+               if ((cp == buffer) && ((*cp == '\n') || (*cp == '\r')))
+                       break;
+       }
+       return 0;
+}
+
+
 /* open a socket to a tcp remote host with the specified port 
-   based on code from Warren */
+   based on code from Warren
+   proxy support by Stephen Rothwell */
 int open_socket_out(char *host, int port)
 {
        int type = SOCK_STREAM;
        struct sockaddr_in sock_out;
        int res;
        struct hostent *hp;
-  
+       char *h;
+       unsigned p;
+       int proxied = 0;
+       char buffer[1024];
+       char *cp;
+
+       /* if we have a RSYNC_PROXY env variable then redirect our connetcion via a web proxy
+          at the given address. The format is hostname:port */
+       h = getenv("RSYNC_PROXY");
+       proxied = (h != NULL) && (*h != '\0');
+
+       if (proxied) {
+               strlcpy(buffer, h, sizeof(buffer));
+               cp = strchr(buffer, ':');
+               if (cp == NULL) {
+                       rprintf(FERROR, "invalid proxy specification\n");
+                       return -1;
+               }
+               *cp++ = '\0';
+               p = atoi(cp);
+               h = buffer;
+       } else {
+               h = host;
+               p = port;
+       }
 
        res = socket(PF_INET, type, 0);
        if (res == -1) {
                return -1;
        }
 
-       hp = gethostbyname(host);
+       hp = gethostbyname(h);
        if (!hp) {
-               rprintf(FERROR,"unknown host: %s\n", host);
+               rprintf(FERROR,"unknown host: %s\n", h);
+               close(res);
                return -1;
        }
 
        memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
-       sock_out.sin_port = htons(port);
+       sock_out.sin_port = htons(p);
        sock_out.sin_family = PF_INET;
 
        if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
+               rprintf(FERROR,"failed to connect to %s - %s\n", h, strerror(errno));
+               close(res);
+               return -1;
+       }
+
+       if (proxied && establish_proxy_connection(res, host, port) != 0) {
                close(res);
-               rprintf(FERROR,"failed to connect to %s - %s\n", host, strerror(errno));
                return -1;
        }