mod_proxy_connect.c revision 36ef8f77bffe75d1aa327882be1b5bdbe2ff567a
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* CONNECT method for Apache proxy */
#include "mod_proxy.h"
#include "apr_poll.h"
#define CONN_BLKSZ AP_IOBUFSIZE
/*
* This handles Netscape CONNECT method secure proxy requests.
* A connection is opened to the specified host and data is
* passed through between the WWW site and the browser.
*
* This code is based on the INTERNET-DRAFT document
* "Tunneling SSL Through a WWW Proxy" currently at
*
* If proxyhost and proxyport are set, we send a CONNECT to
* the specified proxy..
*
* FIXME: this doesn't log the number of bytes sent, but
* that may be okay, since the data is supposed to
* be transparent. In fact, this doesn't log at all
* yet. 8^)
* FIXME: doesn't check any headers initally sent from the
* client.
* FIXME: should allow authentication, but hopefully the
* generic proxy authentication is good enough.
* FIXME: no check for r->assbackwards, whatever that is.
*/
typedef struct {
} connect_conf;
typedef struct {
int first;
int last;
} port_range;
{
return c;
}
{
return c;
}
/*
* Set the ports CONNECT can use
*/
static const char *
{
connect_conf *conf =
char *endptr;
const char *p = arg;
if (!apr_isdigit(arg[0]))
return "AllowCONNECT: port numbers must be numeric";
if (*endptr == '-') {
p = endptr + 1;
}
else {
}
"Cannot parse '%s' as port number", p);
}
return NULL;
}
{
int i;
return port == APR_URI_HTTPS_DEFAULT_PORT
|| port == APR_URI_SNEWS_DEFAULT_PORT;
}
return 1;
}
return 0;
}
/* canonicalise CONNECT URLs. */
{
if (r->method_number != M_CONNECT) {
return DECLINED;
}
"proxy: CONNECT: canonicalising URL %s", url);
return OK;
}
/* read available data (in blocks of CONN_BLKSZ) from c_i and copy to c_o */
{
int rv;
#ifdef DEBUGGING
#endif
do {
if (rv == APR_SUCCESS) {
return APR_EPIPE;
if (APR_BRIGADE_EMPTY(bb))
break;
#ifdef DEBUGGING
len = -1;
"proxy: CONNECT: read %" APR_OFF_T_FMT
#endif
if (rv == APR_SUCCESS) {
}
else {
"proxy: CONNECT: error on %s - ap_pass_brigade",
name);
}
} else if (!APR_STATUS_IS_EAGAIN(rv)) {
"proxy: CONNECT: error on %s - ap_get_brigade",
name);
}
} while (rv == APR_SUCCESS);
if (APR_STATUS_IS_EAGAIN(rv)) {
rv = APR_SUCCESS;
}
return rv;
}
/* CONNECT handler */
{
apr_pool_t *p = r->pool;
conn_rec *c = r->connection;
char buffer[HUGE_STRING_LEN];
int client_error = 0;
const apr_pollfd_t *signalled;
const char *connectname;
int connectport = 0;
/* is this for us? */
if (r->method_number != M_CONNECT) {
"proxy: CONNECT: declining URL %s", url);
return DECLINED;
}
"proxy: CONNECT: serving URL %s", url);
/*
* Step One: Determine Who To Connect To
*
* Break up the URL to determine the host to connect to
*/
/* we break the URL into host, port, uri */
return ap_proxyerror(r, HTTP_BAD_REQUEST,
NULL));
}
/* do a DNS lookup for the destination host */
0, p);
if (APR_SUCCESS != err) {
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
apr_pstrcat(p, "DNS lookup failure for: ",
}
/* are we connecting directly, or via a proxy? */
if (proxyname) {
proxyport, 0, p);
}
else {
}
"proxy: CONNECT: connecting to remote proxy %s on port %d",
/* check if ProxyBlock directive on this host */
return ap_proxyerror(r, HTTP_FORBIDDEN,
"Connect to remote machine blocked");
}
/* Check if it is an allowed port */
return ap_proxyerror(r, HTTP_FORBIDDEN,
"Connect to remote machine blocked");
}
/*
* Step Two: Make the Connection
*
* We have determined who to connect to. Now make the connection.
*/
/* get all the possible IP addresses for the destname and loop through them
* until we get a successful connection
*/
if (APR_SUCCESS != err) {
return ap_proxyerror(r, HTTP_BAD_GATEWAY,
apr_pstrcat(p, "DNS lookup failure for: ",
connectname, NULL));
}
/*
* At this point we have a list of one or more IP addresses of
* the machine to connect to. If configured, reorder this
* list so that the "best candidate" is first try. "best
* candidate" could mean the least loaded server, the fastest
* responding server, whatever.
*
* For now we do nothing, ie we get DNS round robin.
* XXX FIXME
*/
connectname, conf, r);
/* handle a permanent error from the above loop */
if (failed) {
if (proxyname) {
return DECLINED;
}
else {
return HTTP_SERVICE_UNAVAILABLE;
}
}
/* setup polling for connection */
"proxy: CONNECT: setting up poll()");
"proxy: CONNECT: error apr_pollset_create()");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Add client side to the poll */
/* Add the server side to the poll */
/*
* Step Three: Send the Request
*
* Send the HTTP/1.1 CONNECT request to the remote server
*/
if (!backconn) {
/* peer reset */
"proxy: an error occurred creating a new connection "
return HTTP_INTERNAL_SERVER_ERROR;
}
"proxy: CONNECT: pre_connection setup failed (%d)", rc);
return HTTP_INTERNAL_SERVER_ERROR;
}
"proxy: CONNECT: connection complete to %pI (%s)",
/* If we are connecting through a remote proxy, we need to pass
* the CONNECT request on to it.
*/
if (proxyport) {
/* FIXME: Error checking ignored.
*/
"proxy: CONNECT: sending the CONNECT request"
" to the remote proxy");
}
else {
"proxy: CONNECT: Returning 200 OK Status");
"HTTP/1.0 200 Connection Established" CRLF);
#if 0
/* This is safer code, but it doesn't work yet. I'm leaving it
* here so that I can fix it later.
*/
r->header_only = 1;
ap_rflush(r);
#endif
}
"proxy: CONNECT: setting up poll()");
/*
* Step Four: Handle Data Transfer
*
* Handle two way transfer of data over the socket (this is a tunnel).
*/
* not contain any non-connection filters.
*/
r->output_filters = c->output_filters;
r->proto_output_filters = c->output_filters;
r->input_filters = c->input_filters;
r->proto_input_filters = c->input_filters;
/* r->sent_bodyct = 1;*/
while (1) { /* Infinite loop until error (one side closes the connection) */
!= APR_SUCCESS) {
return HTTP_INTERNAL_SERVER_ERROR;
}
#ifdef DEBUGGING
"proxy: CONNECT: woke from poll(), i=%d", pollcnt);
#endif
if (pollevent & APR_POLLIN) {
#ifdef DEBUGGING
"proxy: CONNECT: sock was readable");
#endif
}
else if ((pollevent & APR_POLLERR)
|| (pollevent & APR_POLLHUP)) {
}
if (rv != APR_SUCCESS)
client_error = 1;
}
if (pollevent & APR_POLLIN) {
#ifdef DEBUGGING
"proxy: CONNECT: client was readable");
#endif
}
}
else {
"proxy: CONNECT: unknown socket in pollset");
}
}
if (rv != APR_SUCCESS) {
break;
}
}
"proxy: CONNECT: finished with poll() - cleaning up");
/*
* Step Five: Clean Up
*
* Close the socket and clean up
*/
if (client_error)
else
c->aborted = 1;
return OK;
}
static void ap_proxy_connect_register_hook(apr_pool_t *p)
{
}
static const command_rec cmds[] =
{
"A list of ports or port ranges which CONNECT may connect to"),
{NULL}
};
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
create_config, /* create per-server config structure */
merge_config, /* merge per-server config structures */
cmds, /* command apr_table_t */
ap_proxy_connect_register_hook /* register hooks */
};