/* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#include "ap_config.h"
#include "ap_mmn.h"
#include "httpd.h"
#include "http_config.h"
#include "http_connection.h"
#include "http_protocol.h"
#include "http_log.h"
#include "apr_strings.h"
#include "apr_lib.h"
#define APR_WANT_BYTEFUNC
#include "apr_want.h"
#include "apr_network_io.h"
module AP_MODULE_DECLARE_DATA remoteip_module;
typedef struct {
/** A proxy IP mask to match */
apr_ipsubnet_t *ip;
/** Flagged if internal, otherwise an external trusted proxy */
void *internal;
} remoteip_proxymatch_t;
typedef struct {
/** The header to retrieve a proxy-via ip list */
const char *header_name;
/** A header to record the proxied IP's
* (removed as the physical connection and
* from the proxy-via ip header value list)
*/
const char *proxies_header_name;
/** A list of trusted proxies, ideally configured
* with the most commonly encountered listed first
*/
apr_array_header_t *proxymatch_ip;
} remoteip_config_t;
typedef struct {
apr_sockaddr_t *remote_addr;
char *remote_ip;
/** The list of proxy ip's ignored as remote ip's */
const char *proxy_ips;
/** The remaining list of untrusted proxied remote ip's */
const char *proxied_remote;
} remoteip_req_t;
static void *create_remoteip_server_config(apr_pool_t *p, server_rec *s)
{
remoteip_config_t *config = apr_pcalloc(p, sizeof *config);
/* config->header_name = NULL;
* config->proxies_header_name = NULL;
*/
return config;
}
static void *merge_remoteip_server_config(apr_pool_t *p, void *globalv,
void *serverv)
{
remoteip_config_t *global = (remoteip_config_t *) globalv;
remoteip_config_t *server = (remoteip_config_t *) serverv;
remoteip_config_t *config;
config = (remoteip_config_t *) apr_palloc(p, sizeof(*config));
config->header_name = server->header_name
? server->header_name
: global->header_name;
config->proxies_header_name = server->proxies_header_name
? server->proxies_header_name
: global->proxies_header_name;
config->proxymatch_ip = server->proxymatch_ip
? server->proxymatch_ip
: global->proxymatch_ip;
return config;
}
static const char *header_name_set(cmd_parms *cmd, void *dummy,
const char *arg)
{
remoteip_config_t *config = ap_get_module_config(cmd->server->module_config,
&remoteip_module);
config->header_name = arg;
return NULL;
}
static const char *proxies_header_name_set(cmd_parms *cmd, void *dummy,
const char *arg)
{
remoteip_config_t *config = ap_get_module_config(cmd->server->module_config,
&remoteip_module);
config->proxies_header_name = arg;
return NULL;
}
/* Would be quite nice if APR exported this */
/* apr:network_io/unix/sockaddr.c */
static int looks_like_ip(const char *ipstr)
{
if (ap_strchr_c(ipstr, ':')) {
/* definitely not a hostname; assume it is intended to be an IPv6 address */
return 1;
}
/* simple IPv4 address string check */
while ((*ipstr == '.') || apr_isdigit(*ipstr))
ipstr++;
return (*ipstr == '\0');
}
static const char *proxies_set(cmd_parms *cmd, void *cfg,
const char *arg)
{
remoteip_config_t *config = ap_get_module_config(cmd->server->module_config,
&remoteip_module);
remoteip_proxymatch_t *match;
apr_status_t rv;
char *ip = apr_pstrdup(cmd->temp_pool, arg);
char *s = ap_strchr(ip, '/');
if (s) {
*s++ = '\0';
}
if (!config->proxymatch_ip) {
config->proxymatch_ip = apr_array_make(cmd->pool, 1, sizeof(*match));
}
match = (remoteip_proxymatch_t *) apr_array_push(config->proxymatch_ip);
match->internal = cmd->info;
if (looks_like_ip(ip)) {
/* Note s may be null, that's fine (explicit host) */
rv = apr_ipsubnet_create(&match->ip, ip, s, cmd->pool);
}
else
{
apr_sockaddr_t *temp_sa;
if (s) {
return apr_pstrcat(cmd->pool, "RemoteIP: Error parsing IP ", arg,
" the subnet /", s, " is invalid for ",
cmd->cmd->name, NULL);
}
rv = apr_sockaddr_info_get(&temp_sa, ip, APR_UNSPEC, 0,
APR_IPV4_ADDR_OK, cmd->temp_pool);
while (rv == APR_SUCCESS)
{
apr_sockaddr_ip_get(&ip, temp_sa);
rv = apr_ipsubnet_create(&match->ip, ip, NULL, cmd->pool);
if (!(temp_sa = temp_sa->next)) {
break;
}
match = (remoteip_proxymatch_t *)
apr_array_push(config->proxymatch_ip);
match->internal = cmd->info;
}
}
if (rv != APR_SUCCESS) {
char msgbuf[128];
apr_strerror(rv, msgbuf, sizeof(msgbuf));
return apr_pstrcat(cmd->pool, "RemoteIP: Error parsing IP ", arg,
" (", msgbuf, " error) for ", cmd->cmd->name, NULL);
}
return NULL;
}
static const char *proxylist_read(cmd_parms *cmd, void *cfg,
const char *filename)
{
char lbuf[MAX_STRING_LEN];
char *arg;
const char *args;
const char *errmsg;
ap_configfile_t *cfp;
apr_status_t rv;
filename = ap_server_root_relative(cmd->temp_pool, filename);
rv = ap_pcfg_openfile(&cfp, cmd->temp_pool, filename);
if (rv != APR_SUCCESS) {
return apr_psprintf(cmd->pool, "%s: Could not open file %s: %s",
cmd->cmd->name, filename,
apr_strerror(rv, lbuf, sizeof(lbuf)));
}
while (!(ap_cfg_getline(lbuf, MAX_STRING_LEN, cfp))) {
args = lbuf;
while (*(arg = ap_getword_conf(cmd->temp_pool, &args)) != '\0') {
if (*arg == '#' || *arg == '\0') {
break;
}
errmsg = proxies_set(cmd, cfg, arg);
if (errmsg) {
errmsg = apr_psprintf(cmd->pool, "%s at line %d of %s",
errmsg, cfp->line_number, filename);
return errmsg;
}
}
}
ap_cfg_closefile(cfp);
return NULL;
}
static int remoteip_modify_request(request_rec *r)
{
conn_rec *c = r->connection;
remoteip_config_t *config = (remoteip_config_t *)
ap_get_module_config(r->server->module_config, &remoteip_module);
remoteip_req_t *req = NULL;
apr_sockaddr_t *temp_sa;
apr_status_t rv;
char *remote;
char *proxy_ips = NULL;
char *parse_remote;
char *eos;
unsigned char *addrbyte;
void *internal = NULL;
if (!config->header_name) {
return DECLINED;
}
remote = (char *) apr_table_get(r->headers_in, config->header_name);
if (!remote) {
return OK;
}
remote