mod_proxy_connect.c revision 9bec939825399ac2816ea0d912d2e3c3b2ed91f4
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Unless required by applicable law or agreed to in writing, software
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * distributed under the License is distributed on an "AS IS" BASIS,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * See the License for the specific language governing permissions and
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * limitations under the License.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* CONNECT method for Apache proxy */
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholesmodule AP_MODULE_DECLARE_DATA proxy_connect_module;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * This handles Netscape CONNECT method secure proxy requests.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * A connection is opened to the specified host and data is
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * passed through between the WWW site and the browser.
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick * This code is based on the INTERNET-DRAFT document
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * "Tunneling SSL Through a WWW Proxy" currently at
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * http://www.mcom.com/newsref/std/tunneling_ssl.html.
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * If proxyhost and proxyport are set, we send a CONNECT to
5c0419d51818eb02045cf923a9fe456127a44c60wrowe * the specified proxy..
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * FIXME: this doesn't log the number of bytes sent, but
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * that may be okay, since the data is supposed to
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * be transparent. In fact, this doesn't log at all
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * yet. 8^)
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * FIXME: doesn't check any headers initally sent from the
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * FIXME: should allow authentication, but hopefully the
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * generic proxy authentication is good enough.
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * FIXME: no check for r->assbackwards, whatever that is.
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sftypedef struct {
bede2929837dfd23863ad4b39199c63126566d61jortontypedef struct {
0f60998368b493f90120180a93fc2e1e74490872covenerstatic void *create_config(apr_pool_t *p, server_rec *s)
0f60998368b493f90120180a93fc2e1e74490872covener connect_conf *c = apr_pcalloc(p, sizeof(connect_conf));
0f60998368b493f90120180a93fc2e1e74490872covener c->allowed_connect_ports = apr_array_make(p, 10, sizeof(port_range));
87587593f1a53030e840acc0dec6cc881022ea40covenerstatic void *merge_config(apr_pool_t *p, void *basev, void *overridesv)
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener connect_conf *c = apr_pcalloc(p, sizeof(connect_conf));
a81c0c1ae464b2063a21b45f80c9da8d89bb840ecovener connect_conf *overrides = (connect_conf *) overridesv;
0568280364eb026393be492ebc732795c4934643jorton * Set the ports CONNECT can use
0568280364eb026393be492ebc732795c4934643jortonstatic const char *
0568280364eb026393be492ebc732795c4934643jorton set_allowed_ports(cmd_parms *parms, void *dummy, const char *arg)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_get_module_config(s->module_config, &proxy_connect_module);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes const char *p = arg;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes return "AllowCONNECT: port numbers must be numeric";
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "Cannot parse '%s' as port number", p);
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes New = apr_array_push(conf->allowed_connect_ports);
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrinstatic int allowed_port(connect_conf *conf, int port)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes port_range *list = (port_range *) conf->allowed_connect_ports->elts;
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes if (apr_is_empty_array(conf->allowed_connect_ports)){
95b6fe1346805e1731e6e97c15d569c73be22cf7minfrin for (i = 0; i < conf->allowed_connect_ports->nelts; i++) {
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* canonicalise CONNECT URLs. */
482f676c6c19b1c5bb5cca04dad11509c1da3a4cwrowestatic int proxy_connect_canon(request_rec *r, char *url)
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes/* read available data (in blocks of CONN_BLKSZ) from c_i and copy to c_o */
f43b67c5a9d29b572eac916f8335cedc80c908bebnicholesstatic int proxy_connect_transfer(request_rec *r, conn_rec *c_i, conn_rec *c_o,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim rv = ap_get_brigade(c_i->input_filters, bb, AP_MODE_READBYTES,
8113dac419143273351446c3ad653f3fe5ba5cfdwrowe "proxy: CONNECT: error on %s - ap_pass_brigade",
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes "proxy: CONNECT: error on %s - ap_get_brigade",
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf/* CONNECT handler */
fa123db15501821e36e513afa78e839775ad2800covenerstatic int proxy_connect_handler(request_rec *r, proxy_worker *worker,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_get_module_config(r->server->module_config, &proxy_connect_module);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_bucket_brigade *bb = apr_brigade_create(p, c->bucket_alloc);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener apr_socket_t *client_socket = ap_get_conn_socket(c);
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* is this for us? */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes * Step One: Determine Who To Connect To
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener * Break up the URL to determine the host to connect to
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener /* we break the URL into host, port, uri */
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener if (APR_SUCCESS != apr_uri_parse_hostinfo(p, url, &uri)) {
4e9c24785b525d2956e6e381015c0f2bd0a72f4bcovener "proxy: CONNECT: connecting %s to %s:%d", url, uri.hostname,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim /* do a DNS lookup for the destination host */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim err = apr_sockaddr_info_get(&uri_addr, uri.hostname, APR_UNSPEC, uri.port,
b08925593f214f621161742925dcf074a8047e0acovener /* are we connecting directly, or via a proxy? */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim err = apr_sockaddr_info_get(&connect_addr, proxyname, APR_UNSPEC,
55e2e59e5910072e51c870afc68b0907f41a28e0sf "proxy: CONNECT: connecting to remote proxy %s on port %d",
55e2e59e5910072e51c870afc68b0907f41a28e0sf /* check if ProxyBlock directive on this host */
55e2e59e5910072e51c870afc68b0907f41a28e0sf "Connect to remote machine blocked");
55e2e59e5910072e51c870afc68b0907f41a28e0sf /* Check if it is an allowed port */
55e2e59e5910072e51c870afc68b0907f41a28e0sf "Connect to remote machine blocked");
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * Step Two: Make the Connection
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * We have determined who to connect to. Now make the connection.
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* get all the possible IP addresses for the destname and loop through them
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * until we get a successful connection
513b324e774c559b579896df131fd7c8471ed529rederpj * At this point we have a list of one or more IP addresses of
513b324e774c559b579896df131fd7c8471ed529rederpj * the machine to connect to. If configured, reorder this
513b324e774c559b579896df131fd7c8471ed529rederpj * list so that the "best candidate" is first try. "best
513b324e774c559b579896df131fd7c8471ed529rederpj * candidate" could mean the least loaded server, the fastest
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes * responding server, whatever.
02fd88c85a9850109753b87612955ad372de1575sf * For now we do nothing, ie we get DNS round robin.
d5b12fe8ae917e654a33247fd4e59dc9e75170aebnicholes failed = ap_proxy_connect_to_backend(&sock, "CONNECT", connect_addr,
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* handle a permanent error from the above loop */
307219eca940aa30b873bfd68a44484dd3d3fa88covener /* setup polling for connection */
707f6d077f73cc948deead8df5b40ea42c1eaa78covener "proxy: CONNECT: setting up poll()");
707f6d077f73cc948deead8df5b40ea42c1eaa78covener if ((rv = apr_pollset_create(&pollset, 2, r->pool, 0)) != APR_SUCCESS) {
707f6d077f73cc948deead8df5b40ea42c1eaa78covener "proxy: CONNECT: error apr_pollset_create()");
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* Add client side to the poll */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes /* Add the server side to the poll */
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Step Three: Send the Request
54d22ed1c429b903b029bbd62621f11a9e286137minfrin * Send the HTTP/1.1 CONNECT request to the remote server
55e2e59e5910072e51c870afc68b0907f41a28e0sf backconn = ap_run_create_connection(c->pool, r->server, sock,
55e2e59e5910072e51c870afc68b0907f41a28e0sf /* peer reset */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf "proxy: an error occurred creating a new connection "
b08925593f214f621161742925dcf074a8047e0acovener "proxy: CONNECT: pre_connection setup failed (%d)", rc);
5f3e4e06f8e23597d2f95e2c2cff1116c522488fcovener "proxy: CONNECT: connection complete to %pI (%s)",
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf /* If we are connecting through a remote proxy, we need to pass
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * the CONNECT request on to it.
9ad7b260be233be7d7b5576979825cac72e15498rederpj /* FIXME: Error checking ignored.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener ap_log_error(APLOG_MARK, APLOG_TRACE2, 0, r->server,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener "proxy: CONNECT: sending the CONNECT request"
707f6d077f73cc948deead8df5b40ea42c1eaa78covener " to the remote proxy");
707f6d077f73cc948deead8df5b40ea42c1eaa78covener "Proxy-agent: %s" CRLF CRLF, ap_get_server_banner());
60284a9f9158baa60cc8ab4a69066404b1dcae7acovener ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
707f6d077f73cc948deead8df5b40ea42c1eaa78covener "proxy: CONNECT: Returning 200 OK Status");
707f6d077f73cc948deead8df5b40ea42c1eaa78covener /* This is safer code, but it doesn't work yet. I'm leaving it
707f6d077f73cc948deead8df5b40ea42c1eaa78covener * here so that I can fix it later.
707f6d077f73cc948deead8df5b40ea42c1eaa78covener apr_table_set(r->headers_out, "Proxy-agent: %s", ap_get_server_banner());
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf "proxy: CONNECT: setting up poll()");
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Step Four: Handle Data Transfer
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * Handle two way transfer of data over the socket (this is a tunnel).
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf /* we are now acting as a tunnel - the input/output filter stacks should
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf * not contain any non-connection filters.
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf/* r->sent_bodyct = 1;*/
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf while (1) { /* Infinite loop until error (one side closes the connection) */
ab86c68ce36c715e93f403dde41d0b9c1522c8b0sf if ((rv = apr_pollset_poll(pollset, -1, &pollcnt, &signalled))
141e1368614dc7564e1627671361b01b4869b491bnicholes ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: CONNECT: error apr_poll()");
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: CONNECT: sock was readable");
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: CONNECT: err/hup on backconn");
4be9c459920a7c1cfe62d654327dae5c4bb6b284sf "proxy: CONNECT: client was readable");
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes rv = proxy_connect_transfer(r, c, backconn, bb, "client");
96ebb616bbf4ac2a422cc5d9770c9ad07ccecdc0covener "proxy: CONNECT: unknown socket in pollset");
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim "proxy: CONNECT: finished with poll() - cleaning up");
7dbf29be626018bc389ef94c1846aeac4b72633bsf * Step Five: Clean Up
7dbf29be626018bc389ef94c1846aeac4b72633bsf * Close the socket and clean up
7dbf29be626018bc389ef94c1846aeac4b72633bsf proxy_hook_scheme_handler(proxy_connect_handler, NULL, NULL, APR_HOOK_MIDDLE);
7dbf29be626018bc389ef94c1846aeac4b72633bsf proxy_hook_canon_handler(proxy_connect_canon, NULL, NULL, APR_HOOK_MIDDLE);
7dbf29be626018bc389ef94c1846aeac4b72633bsf AP_INIT_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
783874b86bfe13d7a4fe0922f344a3779cdccea3covener "A list of ports or port ranges which CONNECT may connect to"),
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes create_config, /* create per-server config structure */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes merge_config, /* merge per-server config structures */
d330a801b1e5d63a4b8b4fd431542ad0903fd71bbnicholes ap_proxy_connect_register_hook /* register hooks */