mod_proxy.c revision 7708bd70088b64148d7d78fd84ede43ced63c713
252N/A/* ====================================================================
252N/A * The Apache Software License, Version 1.1
252N/A *
252N/A * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
252N/A * reserved.
252N/A *
252N/A * Redistribution and use in source and binary forms, with or without
252N/A * modification, are permitted provided that the following conditions
252N/A * are met:
252N/A *
252N/A * 1. Redistributions of source code must retain the above copyright
252N/A * notice, this list of conditions and the following disclaimer.
252N/A *
252N/A * 2. Redistributions in binary form must reproduce the above copyright
252N/A * notice, this list of conditions and the following disclaimer in
252N/A * the documentation and/or other materials provided with the
252N/A * distribution.
252N/A *
252N/A * 3. The end-user documentation included with the redistribution,
252N/A * if any, must include the following acknowledgment:
252N/A * "This product includes software developed by the
252N/A * Apache Software Foundation (http://www.apache.org/)."
252N/A * Alternately, this acknowledgment may appear in the software itself,
252N/A * if and wherever such third-party acknowledgments normally appear.
252N/A *
252N/A * 4. The names "Apache" and "Apache Software Foundation" must
252N/A * not be used to endorse or promote products derived from this
252N/A * software without prior written permission. For written
252N/A * permission, please contact apache@apache.org.
252N/A *
252N/A * 5. Products derived from this software may not be called "Apache",
252N/A * nor may "Apache" appear in their name, without prior written
252N/A * permission of the Apache Software Foundation.
252N/A *
252N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
252N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
252N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
252N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
252N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
252N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
252N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
252N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
252N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
252N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252N/A * SUCH DAMAGE.
252N/A * ====================================================================
252N/A *
252N/A * This software consists of voluntary contributions made by many
252N/A * individuals on behalf of the Apache Software Foundation. For more
252N/A * information on the Apache Software Foundation, please see
252N/A * <http://www.apache.org/>.
252N/A *
252N/A * Portions of this software are based upon public domain software
252N/A * originally written at the National Center for Supercomputing Applications,
252N/A * University of Illinois, Urbana-Champaign.
252N/A */
252N/A
252N/A#define CORE_PRIVATE
252N/A
252N/A#include "mod_proxy.h"
252N/A
252N/A/*
252N/A * A Web proxy module. Stages:
252N/A *
252N/A * translate_name: set filename to proxy:<URL>
252N/A * type_checker: set type to PROXY_MAGIC_TYPE if filename begins proxy:
252N/A * fix_ups: convert the URL stored in the filename to the
252N/A * canonical form.
252N/A * handler: handle proxy requests
252N/A */
252N/A
252N/A/* -------------------------------------------------------------- */
252N/A/* Translate the URL into a 'filename' */
252N/A
252N/Astatic int alias_match(const char *uri, const char *alias_fakename)
252N/A{
252N/A const char *end_fakename = alias_fakename + strlen(alias_fakename);
252N/A const char *aliasp = alias_fakename, *urip = uri;
252N/A
252N/A while (aliasp < end_fakename) {
252N/A if (*aliasp == '/') {
252N/A /* any number of '/' in the alias matches any number in
252N/A * the supplied URI, but there must be at least one...
252N/A */
252N/A if (*urip != '/')
252N/A return 0;
252N/A
252N/A while (*aliasp == '/')
252N/A ++aliasp;
252N/A while (*urip == '/')
252N/A ++urip;
252N/A }
252N/A else {
252N/A /* Other characters are compared literally */
252N/A if (*urip++ != *aliasp++)
252N/A return 0;
252N/A }
252N/A }
252N/A
252N/A /* Check last alias path component matched all the way */
252N/A
252N/A if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
252N/A return 0;
252N/A
252N/A /* Return number of characters from URI which matched (may be
252N/A * greater than length of alias, since we may have matched
252N/A * doubled slashes)
252N/A */
252N/A
252N/A return urip - uri;
252N/A}
252N/A
252N/A/* Detect if an absoluteURI should be proxied or not. Note that we
252N/A * have to do this during this phase because later phases are
252N/A * "short-circuiting"... i.e. translate_names will end when the first
252N/A * module returns OK. So for example, if the request is something like:
252N/A *
252N/A * GET http://othervhost/cgi-bin/printenv HTTP/1.0
252N/A *
252N/A * mod_alias will notice the /cgi-bin part and ScriptAlias it and
252N/A * short-circuit the proxy... just because of the ordering in the
252N/A * configuration file.
252N/A */
252N/Astatic int proxy_detect(request_rec *r)
252N/A{
252N/A void *sconf = r->server->module_config;
252N/A proxy_server_conf *conf;
252N/A
252N/A conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
252N/A
252N/A if (conf->req && r->parsed_uri.scheme) {
252N/A /* but it might be something vhosted */
252N/A if (!(r->parsed_uri.hostname
252N/A && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))
252N/A && ap_matches_request_vhost(r, r->parsed_uri.hostname,
252N/A r->parsed_uri.port_str ? r->parsed_uri.port : ap_default_port(r)))) {
252N/A r->proxyreq = PROXYREQ_PROXY;
252N/A r->uri = r->unparsed_uri;
252N/A r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
252N/A r->handler = "proxy-server";
252N/A }
252N/A }
252N/A /* We need special treatment for CONNECT proxying: it has no scheme part */
252N/A else if (conf->req && r->method_number == M_CONNECT
252N/A && r->parsed_uri.hostname
252N/A && r->parsed_uri.port_str) {
252N/A r->proxyreq = PROXYREQ_PROXY;
252N/A r->uri = r->unparsed_uri;
252N/A r->filename = apr_pstrcat(r->pool, "proxy:", r->uri, NULL);
252N/A r->handler = "proxy-server";
252N/A }
252N/A return DECLINED;
252N/A}
252N/A
252N/Astatic int proxy_trans(request_rec *r)
252N/A{
252N/A void *sconf = r->server->module_config;
252N/A proxy_server_conf *conf =
252N/A (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
252N/A int i, len;
252N/A struct proxy_alias *ent = (struct proxy_alias *) conf->aliases->elts;
252N/A
252N/A if (r->proxyreq) {
252N/A /* someone has already set up the proxy, it was possibly ourselves
252N/A * in proxy_detect
252N/A */
252N/A return OK;
252N/A }
252N/A
252N/A /* XXX: since r->uri has been manipulated already we're not really
252N/A * compliant with RFC1945 at this point. But this probably isn't
252N/A * an issue because this is a hybrid proxy/origin server.
252N/A */
252N/A
252N/A for (i = 0; i < conf->aliases->nelts; i++) {
252N/A len = alias_match(r->uri, ent[i].fake);
252N/A
252N/A if (len > 0) {
252N/A r->filename = apr_pstrcat(r->pool, "proxy:", ent[i].real,
252N/A r->uri + len, NULL);
252N/A r->handler = "proxy-server";
252N/A r->proxyreq = PROXYREQ_REVERSE;
252N/A return OK;
252N/A }
252N/A }
252N/A return DECLINED;
252N/A}
252N/A
252N/A/* -------------------------------------------------------------- */
252N/A/* Fixup the filename */
252N/A
252N/A/*
252N/A * Canonicalise the URL
252N/A */
252N/Astatic int proxy_fixup(request_rec *r)
252N/A{
252N/A char *url, *p;
252N/A
252N/A if (!r->proxyreq || strncmp(r->filename, "proxy:", 6) != 0)
252N/A return DECLINED;
252N/A
252N/A url = &r->filename[6];
252N/A
252N/A/* canonicalise each specific scheme */
252N/A if (strncasecmp(url, "http:", 5) == 0)
252N/A return ap_proxy_http_canon(r, url + 5, "http", DEFAULT_HTTP_PORT);
252N/A else if (strncasecmp(url, "ftp:", 4) == 0)
252N/A return ap_proxy_ftp_canon(r, url + 4);
252N/A
252N/A p = strchr(url, ':');
252N/A if (p == NULL || p == url)
252N/A return HTTP_BAD_REQUEST;
252N/A
252N/A return OK; /* otherwise; we've done the best we can */
252N/A}
252N/A
252N/A/* Send a redirection if the request contains a hostname which is not */
252N/A/* fully qualified, i.e. doesn't have a domain name appended. Some proxy */
252N/A/* servers like Netscape's allow this and access hosts from the local */
252N/A/* domain in this case. I think it is better to redirect to a FQDN, since */
252N/A/* these will later be found in the bookmarks files. */
252N/A/* The "ProxyDomain" directive determines what domain will be appended */
252N/Astatic int proxy_needsdomain(request_rec *r, const char *url, const char *domain)
252N/A{
252N/A char *nuri;
252N/A const char *ref;
252N/A
252N/A /* We only want to worry about GETs */
252N/A if (!r->proxyreq || r->method_number != M_GET || !r->parsed_uri.hostname)
252N/A return DECLINED;
252N/A
252N/A /* If host does contain a dot already, or it is "localhost", decline */
252N/A if (strchr(r->parsed_uri.hostname, '.') != NULL
252N/A || strcasecmp(r->parsed_uri.hostname, "localhost") == 0)
252N/A return DECLINED; /* host name has a dot already */
252N/A
252N/A ref = apr_table_get(r->headers_in, "Referer");
252N/A
252N/A /* Reassemble the request, but insert the domain after the host name */
252N/A /* Note that the domain name always starts with a dot */
252N/A r->parsed_uri.hostname = apr_pstrcat(r->pool, r->parsed_uri.hostname,
252N/A domain, NULL);
252N/A nuri = ap_unparse_uri_components(r->pool,
252N/A &r->parsed_uri,
252N/A UNP_REVEALPASSWORD);
252N/A
252N/A apr_table_set(r->headers_out, "Location", nuri);
252N/A ap_log_rerror(APLOG_MARK, APLOG_INFO|APLOG_NOERRNO, 0, r,
252N/A "Domain missing: %s sent to %s%s%s", r->uri,
252N/A ap_unparse_uri_components(r->pool, &r->parsed_uri,
252N/A UNP_OMITUSERINFO),
252N/A ref ? " from " : "", ref ? ref : "");
252N/A
252N/A return HTTP_MOVED_PERMANENTLY;
252N/A}
252N/A
252N/A/* -------------------------------------------------------------- */
252N/A/* Invoke handler */
252N/A
252N/Astatic int proxy_handler(request_rec *r)
252N/A{
252N/A char *url, *scheme, *p;
252N/A const char *p2;
252N/A void *sconf = r->server->module_config;
252N/A proxy_server_conf *conf = (proxy_server_conf *)
252N/A ap_get_module_config(sconf, &proxy_module);
252N/A apr_array_header_t *proxies = conf->proxies;
252N/A struct proxy_remote *ents = (struct proxy_remote *) proxies->elts;
252N/A int i, rc;
252N/A int direct_connect = 0;
252N/A const char *maxfwd_str;
252N/A const char *pragma, *auth, *imstr;
252N/A
252N/A if (!r->proxyreq || strncmp(r->filename, "proxy:", 6) != 0)
252N/A return DECLINED;
252N/A
252N/A if ((r->method_number == M_TRACE || r->method_number == M_OPTIONS) &&
252N/A (maxfwd_str = apr_table_get(r->headers_in, "Max-Forwards")) != NULL) {
252N/A long maxfwd = strtol(maxfwd_str, NULL, 10);
252N/A if (maxfwd < 1) {
252N/A switch (r->method_number) {
252N/A case M_TRACE: {
252N/A int access_status;
252N/A r->proxyreq = PROXYREQ_NONE;
252N/A if ((access_status = ap_send_http_trace(r)))
252N/A ap_die(access_status, r);
252N/A else
252N/A ap_finalize_request_protocol(r);
252N/A return OK;
252N/A }
252N/A case M_OPTIONS: {
252N/A int access_status;
252N/A r->proxyreq = PROXYREQ_NONE;
252N/A if ((access_status = ap_send_http_options(r)))
252N/A ap_die(access_status, r);
252N/A else
252N/A ap_finalize_request_protocol(r);
252N/A return OK;
252N/A }
252N/A }
252N/A }
252N/A apr_table_setn(r->headers_in, "Max-Forwards",
252N/A apr_psprintf(r->pool, "%ld", (maxfwd > 0) ? maxfwd-1 : 0));
252N/A }
252N/A
252N/A if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)))
252N/A return rc;
252N/A
252N/A url = r->filename + 6;
252N/A p = strchr(url, ':');
252N/A if (p == NULL)
252N/A return HTTP_BAD_REQUEST;
252N/A
252N/A pragma = apr_table_get(r->headers_in, "Pragma");
252N/A auth = apr_table_get(r->headers_in, "Authorization");
252N/A imstr = apr_table_get(r->headers_in, "If-Modified-Since");
252N/A
252N/A ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL,
252N/A "Request for %s, pragma=%s, auth=%s, imstr=%s", url,
252N/A pragma, auth, imstr);
252N/A
252N/A /* If the host doesn't have a domain name, add one and redirect. */
252N/A if (conf->domain != NULL) {
252N/A rc = proxy_needsdomain(r, url, conf->domain);
252N/A if (ap_is_HTTP_REDIRECT(rc))
252N/A return HTTP_MOVED_PERMANENTLY;
252N/A }
252N/A
252N/A *p = '\0';
252N/A scheme = apr_pstrdup(r->pool, url);
252N/A *p = ':';
252N/A
252N/A /* Check URI's destination host against NoProxy hosts */
252N/A /* Bypass ProxyRemote server lookup if configured as NoProxy */
252N/A /* we only know how to handle communication to a proxy via http */
252N/A /*if (strcasecmp(scheme, "http") == 0) */
252N/A {
252N/A int ii;
252N/A struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
252N/A
252N/A for (direct_connect = ii = 0; ii < conf->dirconn->nelts && !direct_connect; ii++) {
252N/A direct_connect = list[ii].matcher(&list[ii], r);
252N/A }
252N/A#if DEBUGGING
252N/A ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, r,
252N/A (direct_connect) ? "NoProxy for %s" : "UseProxy for %s",
252N/A r->uri);
252N/A#endif
252N/A }
252N/A
252N/A/* firstly, try a proxy, unless a NoProxy directive is active */
252N/A
252N/A if (!direct_connect)
252N/A for (i = 0; i < proxies->nelts; i++) {
252N/A p2 = ap_strchr_c(ents[i].scheme, ':'); /* is it a partial URL? */
252N/A if (strcmp(ents[i].scheme, "*") == 0 ||
252N/A (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
252N/A (p2 != NULL &&
252N/A strncasecmp(url, ents[i].scheme, strlen(ents[i].scheme)) == 0)) {
252N/A /* CONNECT is a special method that bypasses the normal
252N/A * proxy code.
252N/A */
252N/A if (r->method_number == M_CONNECT)
252N/A rc = ap_proxy_connect_handler(r, url, ents[i].hostname,
252N/A ents[i].port);
252N/A/* we only know how to handle communication to a proxy via http */
252N/A else if (strcasecmp(ents[i].protocol, "http") == 0)
252N/A rc = ap_proxy_http_handler(r, url, ents[i].hostname,
252N/A ents[i].port);
252N/A else
252N/A rc = DECLINED;
252N/A
252N/A /* an error or success */
252N/A if (rc != DECLINED && rc != HTTP_BAD_GATEWAY)
252N/A return rc;
252N/A /* we failed to talk to the upstream proxy */
252N/A }
252N/A }
252N/A
252N/A/* otherwise, try it direct */
252N/A/* N.B. what if we're behind a firewall, where we must use a proxy or
252N/A * give up??
252N/A */
252N/A /* handle the scheme */
252N/A if (r->method_number == M_CONNECT)
252N/A return ap_proxy_connect_handler(r, url, NULL, 0);
252N/A if (strcasecmp(scheme, "http") == 0)
252N/A return ap_proxy_http_handler(r, url, NULL, 0);
252N/A if (strcasecmp(scheme, "ftp") == 0)
252N/A return ap_proxy_ftp_handler(r, url);
252N/A else {
252N/A ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server,
252N/A "Neither CONNECT, HTTP or FTP for %s",
252N/A r->uri);
252N/A return HTTP_FORBIDDEN;
252N/A }
252N/A}
252N/A
252N/A/* -------------------------------------------------------------- */
252N/A/* Setup configurable data */
252N/A
252N/Astatic void * create_proxy_config(apr_pool_t *p, server_rec *s)
252N/A{
252N/A proxy_server_conf *ps = ap_pcalloc(p, sizeof(proxy_server_conf));
252N/A
252N/A ps->proxies = ap_make_array(p, 10, sizeof(struct proxy_remote));
252N/A ps->aliases = ap_make_array(p, 10, sizeof(struct proxy_alias));
252N/A ps->raliases = ap_make_array(p, 10, sizeof(struct proxy_alias));
252N/A ps->noproxies = ap_make_array(p, 10, sizeof(struct noproxy_entry));
252N/A ps->dirconn = ap_make_array(p, 10, sizeof(struct dirconn_entry));
252N/A ps->allowed_connect_ports = ap_make_array(p, 10, sizeof(int));
252N/A ps->client_socket = NULL;
252N/A ps->domain = NULL;
252N/A ps->viaopt = via_off; /* initially backward compatible with 1.3.1 */
252N/A ps->viaopt_set = 0; /* 0 means default */
252N/A ps->req = 0;
252N/A ps->req_set = 0;
252N/A ps->recv_buffer_size = 0; /* this default was left unset for some reason */
252N/A ps->recv_buffer_size_set = 0;
252N/A
252N/A return ps;
252N/A}
252N/A
252N/Astatic void * merge_proxy_config(apr_pool_t *p, void *basev, void *overridesv)
252N/A{
252N/A proxy_server_conf *ps = ap_pcalloc(p, sizeof(proxy_server_conf));
252N/A proxy_server_conf *base = (proxy_server_conf *) basev;
252N/A proxy_server_conf *overrides = (proxy_server_conf *) overridesv;
252N/A
252N/A ps->proxies = ap_append_arrays(p, base->proxies, overrides->proxies);
252N/A ps->aliases = ap_append_arrays(p, base->aliases, overrides->aliases);
252N/A ps->raliases = ap_append_arrays(p, base->raliases, overrides->raliases);
252N/A ps->noproxies = ap_append_arrays(p, base->noproxies, overrides->noproxies);
252N/A ps->dirconn = ap_append_arrays(p, base->dirconn, overrides->dirconn);
252N/A ps->allowed_connect_ports = ap_append_arrays(p, base->allowed_connect_ports, overrides->allowed_connect_ports);
252N/A
252N/A ps->domain = (overrides->domain == NULL) ? base->domain : overrides->domain;
252N/A ps->viaopt = (overrides->viaopt_set == 0) ? base->viaopt : overrides->viaopt;
252N/A ps->req = (overrides->req_set == 0) ? base->req : overrides->req;
252N/A ps->recv_buffer_size = (overrides->recv_buffer_size_set == 0) ? base->recv_buffer_size : overrides->recv_buffer_size;
252N/A
252N/A return ps;
252N/A}
252N/A
252N/Astatic const char *
252N/A add_proxy(cmd_parms *cmd, void *dummy, const char *f1, const char *r1)
252N/A{
252N/A server_rec *s = cmd->server;
252N/A proxy_server_conf *conf =
252N/A (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
252N/A struct proxy_remote *new;
252N/A char *p, *q;
252N/A char *r, *f, *scheme;
252N/A int port;
252N/A
252N/A r = apr_pstrdup(cmd->pool, r1);
252N/A scheme = apr_pstrdup(cmd->pool, r1);
252N/A f = apr_pstrdup(cmd->pool, f1);
252N/A p = strchr(r, ':');
252N/A if (p == NULL || p[1] != '/' || p[2] != '/' || p[3] == '\0') {
252N/A return "ProxyRemote: Bad syntax for a remote proxy server";
252N/A }
252N/A else {
252N/A scheme[p-r] = 0;
252N/A }
252N/A q = strchr(p + 3, ':');
252N/A if (q != NULL) {
252N/A if (sscanf(q + 1, "%u", &port) != 1 || port > 65535)
252N/A return "ProxyRemote: Bad syntax for a remote proxy server (bad port number)";
252N/A *q = '\0';
252N/A }
252N/A else
252N/A port = -1;
252N/A *p = '\0';
252N/A if (strchr(f, ':') == NULL)
252N/A ap_str_tolower(f); /* lowercase scheme */
252N/A ap_str_tolower(p + 3); /* lowercase hostname */
252N/A
252N/A if (port == -1) {
252N/A port = ap_default_port_for_scheme(scheme);
252N/A }
252N/A
252N/A new = apr_array_push(conf->proxies);
252N/A new->scheme = f;
252N/A new->protocol = r;
252N/A new->hostname = p + 3;
252N/A new->port = port;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char *
252N/A add_pass(cmd_parms *cmd, void *dummy, const char *f, const char *r)
252N/A{
252N/A server_rec *s = cmd->server;
252N/A proxy_server_conf *conf =
252N/A (proxy_server_conf *) ap_get_module_config(s->module_config, &proxy_module);
252N/A struct proxy_alias *new;
252N/A
252N/A new = apr_array_push(conf->aliases);
252N/A new->fake = f;
252N/A new->real = r;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char *
252N/A add_pass_reverse(cmd_parms *cmd, void *dummy, const char *f, const char *r)
252N/A{
252N/A server_rec *s = cmd->server;
252N/A proxy_server_conf *conf;
252N/A struct proxy_alias *new;
252N/A
252N/A conf = (proxy_server_conf *)ap_get_module_config(s->module_config,
252N/A &proxy_module);
252N/A new = apr_array_push(conf->raliases);
252N/A new->fake = f;
252N/A new->real = r;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char *
252N/A set_proxy_exclude(cmd_parms *parms, void *dummy, const char *arg)
252N/A{
252N/A server_rec *s = parms->server;
252N/A proxy_server_conf *conf =
252N/A ap_get_module_config(s->module_config, &proxy_module);
252N/A struct noproxy_entry *new;
252N/A struct noproxy_entry *list = (struct noproxy_entry *) conf->noproxies->elts;
252N/A struct apr_sockaddr_t *addr;
252N/A int found = 0;
252N/A int i;
252N/A
252N/A /* Don't duplicate entries */
252N/A for (i = 0; i < conf->noproxies->nelts; i++) {
252N/A if (apr_strnatcasecmp(arg, list[i].name) == 0) { /* ignore case for host names */
252N/A found = 1;
252N/A }
252N/A }
252N/A
252N/A if (!found) {
252N/A new = apr_array_push(conf->noproxies);
252N/A new->name = arg;
252N/A if (APR_SUCCESS == apr_sockaddr_info_get(&addr, new->name, APR_UNSPEC, 0, 0, parms->pool)) {
252N/A new->addr = addr;
252N/A }
252N/A else {
252N/A new->addr = NULL;
252N/A }
252N/A }
252N/A return NULL;
252N/A}
252N/A
252N/A/*
252N/A * Set the ports CONNECT can use
252N/A */
252N/Astatic const char *
252N/A set_allowed_ports(cmd_parms *parms, void *dummy, const char *arg)
252N/A{
252N/A server_rec *s = parms->server;
252N/A proxy_server_conf *conf =
252N/A ap_get_module_config(s->module_config, &proxy_module);
252N/A int *New;
252N/A
252N/A if (!apr_isdigit(arg[0]))
252N/A return "AllowCONNECT: port number must be numeric";
252N/A
252N/A New = apr_array_push(conf->allowed_connect_ports);
252N/A *New = atoi(arg);
252N/A return NULL;
252N/A}
252N/A
252N/A/* Similar to set_proxy_exclude(), but defining directly connected hosts,
252N/A * which should never be accessed via the configured ProxyRemote servers
252N/A */
252N/Astatic const char *
252N/A set_proxy_dirconn(cmd_parms *parms, void *dummy, const char *arg)
252N/A{
252N/A server_rec *s = parms->server;
252N/A proxy_server_conf *conf =
252N/A ap_get_module_config(s->module_config, &proxy_module);
252N/A struct dirconn_entry *New;
252N/A struct dirconn_entry *list = (struct dirconn_entry *) conf->dirconn->elts;
252N/A int found = 0;
252N/A int i;
252N/A
252N/A /* Don't duplicate entries */
252N/A for (i = 0; i < conf->dirconn->nelts; i++) {
252N/A if (strcasecmp(arg, list[i].name) == 0)
252N/A found = 1;
252N/A }
252N/A
252N/A if (!found) {
252N/A New = apr_array_push(conf->dirconn);
252N/A New->name = apr_pstrdup(parms->pool, arg);
252N/A New->hostentry = NULL;
252N/A
252N/A if (ap_proxy_is_ipaddr(New, parms->pool)) {
252N/A#if DEBUGGING
252N/A ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
252N/A "Parsed addr %s", inet_ntoa(New->addr));
252N/A ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
252N/A "Parsed mask %s", inet_ntoa(New->mask));
252N/A#endif
252N/A }
252N/A else if (ap_proxy_is_domainname(New, parms->pool)) {
252N/A ap_str_tolower(New->name);
252N/A#if DEBUGGING
252N/A ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
252N/A "Parsed domain %s", New->name);
252N/A#endif
252N/A }
252N/A else if (ap_proxy_is_hostname(New, parms->pool)) {
252N/A ap_str_tolower(New->name);
252N/A#if DEBUGGING
252N/A ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
252N/A "Parsed host %s", New->name);
252N/A#endif
252N/A }
252N/A else {
252N/A ap_proxy_is_word(New, parms->pool);
252N/A#if DEBUGGING
252N/A fprintf(stderr, "Parsed word %s\n", New->name);
252N/A#endif
252N/A }
252N/A }
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char *
252N/A set_proxy_domain(cmd_parms *parms, void *dummy, const char *arg)
252N/A{
252N/A proxy_server_conf *psf =
252N/A ap_get_module_config(parms->server->module_config, &proxy_module);
252N/A
252N/A if (arg[0] != '.')
252N/A return "ProxyDomain: domain name must start with a dot.";
252N/A
252N/A psf->domain = arg;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char *
252N/A set_proxy_req(cmd_parms *parms, void *dummy, int flag)
252N/A{
252N/A proxy_server_conf *psf =
252N/A ap_get_module_config(parms->server->module_config, &proxy_module);
252N/A
252N/A psf->req = flag;
252N/A psf->req_set = 1;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char *
252N/A set_recv_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
252N/A{
252N/A proxy_server_conf *psf =
252N/A ap_get_module_config(parms->server->module_config, &proxy_module);
252N/A int s = atoi(arg);
252N/A if (s < 512 && s != 0) {
252N/A return "ProxyReceiveBufferSize must be >= 512 bytes, or 0 for system default.";
252N/A }
252N/A
252N/A psf->recv_buffer_size = s;
252N/A psf->recv_buffer_size_set = 1;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const char*
252N/A set_via_opt(cmd_parms *parms, void *dummy, const char *arg)
252N/A{
252N/A proxy_server_conf *psf =
252N/A ap_get_module_config(parms->server->module_config, &proxy_module);
252N/A
252N/A if (strcasecmp(arg, "Off") == 0)
252N/A psf->viaopt = via_off;
252N/A else if (strcasecmp(arg, "On") == 0)
252N/A psf->viaopt = via_on;
252N/A else if (strcasecmp(arg, "Block") == 0)
252N/A psf->viaopt = via_block;
252N/A else if (strcasecmp(arg, "Full") == 0)
252N/A psf->viaopt = via_full;
252N/A else {
252N/A return "ProxyVia must be one of: "
252N/A "off | on | full | block";
252N/A }
252N/A
252N/A psf->viaopt_set = 1;
252N/A return NULL;
252N/A}
252N/A
252N/Astatic const command_rec proxy_cmds[] =
252N/A{
252N/A AP_INIT_FLAG("ProxyRequests", set_proxy_req, NULL, RSRC_CONF,
252N/A "on if the true proxy requests should be accepted"),
252N/A AP_INIT_TAKE2("ProxyRemote", add_proxy, NULL, RSRC_CONF,
252N/A "a scheme, partial URL or '*' and a proxy server"),
252N/A AP_INIT_TAKE2("ProxyPass", add_pass, NULL, RSRC_CONF,
252N/A "a virtual path and a URL"),
252N/A AP_INIT_TAKE2("ProxyPassReverse", add_pass_reverse, NULL, RSRC_CONF,
252N/A "a virtual path and a URL for reverse proxy behaviour"),
252N/A AP_INIT_ITERATE("ProxyBlock", set_proxy_exclude, NULL, RSRC_CONF,
252N/A "A list of names, hosts or domains to which the proxy will not connect"),
252N/A AP_INIT_TAKE1("ProxyReceiveBufferSize", set_recv_buffer_size, NULL, RSRC_CONF,
252N/A "Receive buffer size for outgoing HTTP and FTP connections in bytes"),
252N/A AP_INIT_ITERATE("NoProxy", set_proxy_dirconn, NULL, RSRC_CONF,
252N/A "A list of domains, hosts, or subnets to which the proxy will connect directly"),
252N/A AP_INIT_TAKE1("ProxyDomain", set_proxy_domain, NULL, RSRC_CONF,
252N/A "The default intranet domain name (in absence of a domain in the URL)"),
252N/A AP_INIT_ITERATE("AllowCONNECT", set_allowed_ports, NULL, RSRC_CONF,
252N/A "A list of ports which CONNECT may connect to"),
252N/A AP_INIT_TAKE1("ProxyVia", set_via_opt, NULL, RSRC_CONF,
252N/A "Configure Via: proxy header header to one of: on | off | block | full"),
252N/A {NULL}
252N/A};
252N/A
252N/Astatic void register_hooks(apr_pool_t *p)
252N/A{
252N/A /* handler */
252N/A ap_hook_handler(proxy_handler, NULL, NULL, APR_HOOK_FIRST);
252N/A /* filename-to-URI translation */
252N/A ap_hook_translate_name(proxy_trans, NULL, NULL, APR_HOOK_FIRST);
252N/A /* filters */
252N/A ap_register_output_filter("PROXY_SEND_DIR", ap_proxy_send_dir_filter, AP_FTYPE_CONNECTION);
252N/A ap_register_output_filter("PROXY_NULL", ap_proxy_null_filter, AP_FTYPE_NETWORK);
252N/A /* fixups */
252N/A ap_hook_fixups(proxy_fixup, NULL, NULL, APR_HOOK_FIRST);
252N/A /* post read_request handling */
252N/A ap_hook_post_read_request(proxy_detect, NULL, NULL, APR_HOOK_FIRST);
252N/A}
252N/A
252N/Amodule AP_MODULE_DECLARE_DATA proxy_module =
252N/A{
252N/A STANDARD20_MODULE_STUFF,
252N/A NULL, /* create per-directory config structure */
252N/A NULL, /* merge per-directory config structures */
252N/A create_proxy_config, /* create per-server config structure */
252N/A merge_proxy_config, /* merge per-server config structures */
252N/A proxy_cmds, /* command table */
252N/A register_hooks
252N/A};
252N/A