home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / proxy / proxy_connect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  9.3 KB  |  286 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1996-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /* CONNECT method for Apache proxy */
  59.  
  60. #include "mod_proxy.h"
  61. #include "http_log.h"
  62. #include "http_main.h"
  63.  
  64. #ifdef HAVE_BSTRING_H
  65. #include <bstring.h>        /* for IRIX, FD_SET calls bzero() */
  66. #endif
  67.  
  68. DEF_Explain
  69.  
  70. /*  
  71.  * This handles Netscape CONNECT method secure proxy requests.
  72.  * A connection is opened to the specified host and data is
  73.  * passed through between the WWW site and the browser.
  74.  *
  75.  * This code is based on the INTERNET-DRAFT document
  76.  * "Tunneling SSL Through a WWW Proxy" currently at
  77.  * http://www.mcom.com/newsref/std/tunneling_ssl.html.
  78.  *
  79.  * If proxyhost and proxyport are set, we send a CONNECT to 
  80.  * the specified proxy..  
  81.  *
  82.  * FIXME: this is bad, because it does its own socket I/O
  83.  *        instead of using the I/O in buff.c.  However,
  84.  *        the I/O in buff.c blocks on reads, and because
  85.  *        this function doesn't know how much data will
  86.  *        be sent either way (or when) it can't use blocking
  87.  *        I/O.  This may be very implementation-specific
  88.  *        (to Linux).  Any suggestions?
  89.  * FIXME: this doesn't log the number of bytes sent, but
  90.  *        that may be okay, since the data is supposed to
  91.  *        be transparent. In fact, this doesn't log at all
  92.  *        yet. 8^)
  93.  * FIXME: doesn't check any headers initally sent from the
  94.  *        client.
  95.  * FIXME: should allow authentication, but hopefully the
  96.  *        generic proxy authentication is good enough.
  97.  * FIXME: no check for r->assbackwards, whatever that is.
  98.  */
  99.  
  100. static int
  101. allowed_port(proxy_server_conf *conf, int port)
  102. {
  103.     int i;
  104.     int *list = (int *) conf->allowed_connect_ports->elts;
  105.  
  106.     for(i = 0; i < conf->allowed_connect_ports->nelts; i++) {
  107.     if(port == list[i])
  108.         return 1;
  109.     }
  110.     return 0;
  111. }
  112.  
  113.  
  114. int ap_proxy_connect_handler(request_rec *r, cache_req *c, char *url,
  115.               const char *proxyhost, int proxyport)
  116. {
  117.     struct sockaddr_in server;
  118.     struct in_addr destaddr;
  119.     struct hostent server_hp;
  120.     const char *host, *err;
  121.     char *p;
  122.     int port, sock;
  123.     char buffer[HUGE_STRING_LEN];
  124.     int nbytes, i, j;
  125.     fd_set fds;
  126.  
  127.     void *sconf = r->server->module_config;
  128.     proxy_server_conf *conf =
  129.     (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
  130.     struct noproxy_entry *npent = (struct noproxy_entry *) conf->noproxies->elts;
  131.  
  132.     memset(&server, '\0', sizeof(server));
  133.     server.sin_family = AF_INET;
  134.  
  135.     /* Break the URL into host:port pairs */
  136.  
  137.     host = url;
  138.     p = strchr(url, ':');
  139.     if (p == NULL)
  140.     port = DEFAULT_HTTPS_PORT;
  141.     else {
  142.     port = atoi(p + 1);
  143.     *p = '\0';
  144.     }
  145.  
  146. /* check if ProxyBlock directive on this host */
  147.     destaddr.s_addr = ap_inet_addr(host);
  148.     for (i = 0; i < conf->noproxies->nelts; i++) {
  149.     if ((npent[i].name != NULL && strstr(host, npent[i].name) != NULL)
  150.         || destaddr.s_addr == npent[i].addr.s_addr || npent[i].name[0] == '*')
  151.         return ap_proxyerror(r, "Connect to remote machine blocked");
  152.     }
  153.  
  154.     /* Check if it is an allowed port */
  155.     if (conf->allowed_connect_ports->nelts == 0) {
  156.     /* Default setting if not overridden by AllowCONNECT */
  157.     switch (port) {
  158.         case DEFAULT_HTTPS_PORT:
  159.         case DEFAULT_SNEWS_PORT:
  160.         break;
  161.         default:
  162.         return HTTP_FORBIDDEN;
  163.     }
  164.     } else if(!allowed_port(conf, port))
  165.     return HTTP_FORBIDDEN;
  166.  
  167.     if (proxyhost) {
  168.     Explain2("CONNECT to remote proxy %s on port %d", proxyhost, proxyport);
  169.     }
  170.     else {
  171.     Explain2("CONNECT to %s on port %d", host, port);
  172.     }
  173.  
  174.     server.sin_port = (proxyport ? htons(proxyport) : htons(port));
  175.     err = ap_proxy_host2addr(proxyhost ? proxyhost : host, &server_hp);
  176.  
  177.     if (err != NULL)
  178.     return ap_proxyerror(r, err);    /* give up */
  179.  
  180.     sock = ap_psocket(r->pool, PF_INET, SOCK_STREAM, IPPROTO_TCP);
  181.     if (sock == -1) {
  182.     ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
  183.             "proxy: error creating socket");
  184.     return HTTP_INTERNAL_SERVER_ERROR;
  185.     }
  186.  
  187. #ifndef WIN32
  188.     if (sock >= FD_SETSIZE) {
  189.     ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, NULL,
  190.         "proxy_connect_handler: filedescriptor (%u) "
  191.         "larger than FD_SETSIZE (%u) "
  192.         "found, you probably need to rebuild Apache with a "
  193.         "larger FD_SETSIZE", sock, FD_SETSIZE);
  194.     ap_pclosesocket(r->pool, sock);
  195.     return HTTP_INTERNAL_SERVER_ERROR;
  196.     }
  197. #endif
  198.  
  199.     j = 0;
  200.     while (server_hp.h_addr_list[j] != NULL) {
  201.     memcpy(&server.sin_addr, server_hp.h_addr_list[j],
  202.            sizeof(struct in_addr));
  203.     i = ap_proxy_doconnect(sock, &server, r);
  204.     if (i == 0)
  205.         break;
  206.     j++;
  207.     }
  208.     if (i == -1) {
  209.     ap_pclosesocket(r->pool, sock);
  210.     return ap_proxyerror(r, ap_pstrcat(r->pool,
  211.                     "Could not connect to remote machine:<br>",
  212.                     strerror(errno), NULL));
  213.     }
  214.  
  215.     /* If we are connecting through a remote proxy, we need to pass
  216.      * the CONNECT request on to it.
  217.      */
  218.     if (proxyport) {
  219.     /* FIXME: We should not be calling write() directly, but we currently
  220.      * have no alternative.  Error checking ignored.  Also, we force
  221.      * a HTTP/1.0 request to keep things simple.
  222.      */
  223.     Explain0("Sending the CONNECT request to the remote proxy");
  224.     ap_snprintf(buffer, sizeof(buffer), "CONNECT %s HTTP/1.0" CRLF,
  225.             r->uri);
  226.     write(sock, buffer, strlen(buffer));
  227.     ap_snprintf(buffer, sizeof(buffer),
  228.             "Proxy-agent: %s" CRLF CRLF, ap_get_server_version());
  229.     write(sock, buffer, strlen(buffer));
  230.     }
  231.     else {
  232.     Explain0("Returning 200 OK Status");
  233.     ap_rvputs(r, "HTTP/1.0 200 Connection established" CRLF, NULL);
  234.     ap_rvputs(r, "Proxy-agent: ", ap_get_server_version(), CRLF CRLF, NULL);
  235.     ap_bflush(r->connection->client);
  236.     }
  237.  
  238.     while (1) {            /* Infinite loop until error (one side closes the connection) */
  239.     FD_ZERO(&fds);
  240.     FD_SET(sock, &fds);
  241.     FD_SET(r->connection->client->fd, &fds);
  242.  
  243.     Explain0("Going to sleep (select)");
  244.     i = ap_select((r->connection->client->fd > sock ?
  245.                r->connection->client->fd + 1 :
  246.                sock + 1), &fds, NULL, NULL, NULL);
  247.     Explain1("Woke from select(), i=%d", i);
  248.  
  249.     if (i) {
  250.         if (FD_ISSET(sock, &fds)) {
  251.         Explain0("sock was set");
  252.         if ((nbytes = read(sock, buffer, HUGE_STRING_LEN)) != 0) {
  253.             if (nbytes == -1)
  254.             break;
  255.             if (write(r->connection->client->fd, buffer, nbytes) == EOF)
  256.             break;
  257.             Explain1("Wrote %d bytes to client", nbytes);
  258.         }
  259.         else
  260.             break;
  261.         }
  262.         else if (FD_ISSET(r->connection->client->fd, &fds)) {
  263.         Explain0("client->fd was set");
  264.         if ((nbytes = read(r->connection->client->fd, buffer,
  265.                    HUGE_STRING_LEN)) != 0) {
  266.             if (nbytes == -1)
  267.             break;
  268.             if (write(sock, buffer, nbytes) == EOF)
  269.             break;
  270.             Explain1("Wrote %d bytes to server", nbytes);
  271.         }
  272.         else
  273.             break;
  274.         }
  275.         else
  276.         break;        /* Must be done waiting */
  277.     }
  278.     else
  279.         break;
  280.     }
  281.  
  282.     ap_pclosesocket(r->pool, sock);
  283.  
  284.     return OK;
  285. }
  286.