Compare commits

..
13 Commits
Author SHA1 Message Date
rofl0r 417c258d14 conf: speed up parsing 10x by using ragel if available
conf_regex.rl is generated from the output of conf_regex_print.c using
re2r (https://github.com/rofl0r/re2r).
if ragel is available on the build host, it is being used to generate
finite state machines from the regexes used by the config file parser
for an impressive speed boost, while only adding moderately to binary
size.
a stripped x86_64 tinyproxy binary compiled with -O2 is still only ~100KB.
2020-10-16 12:40:56 +01:00
rofl0r 3a920b7163 conf: add tool to print regex name/regex pairs as re2r input
this is currently not included in the build system and needs to be
compiled by hand.
2020-10-16 12:03:28 +01:00
rofl0r 42bb446c96 conf: shrink back RE_MAX_MATCHES to 16
with the IPv4 regex simplification from 22f059dc5e
we're back to max 15 match groups according to re2r analysis
(the most elaborate regex is the upstream one).
2020-10-16 11:58:48 +01:00
rofl0r dabfd1ad6c conf: remove pointless assert() statement 2020-10-15 22:39:46 +01:00
rofl0r ae4cbcabd1 conf: remove trailing whitespace via C code, not regex 2020-10-15 22:36:10 +01:00
rofl0r 22f059dc5e conf: simplify ipv4 regex
use one matching group rather than 3.
2020-10-12 20:05:06 +01:00
rofl0r 86379b4b66 conf: parse regexes case-sensitive
rather than treating everything as case insensitive, we explicitly
allow upper/lowercase where it makes sense.
2020-10-09 01:43:46 +01:00
rofl0r 57f932a33b conf: skip leading whitespace instead of adding it to each regex 2020-10-09 01:26:50 +01:00
rofl0r 173c5b66a7 conf: remove obsolete whitespace from regex start
we already deal with leading whitespace before a command in a manual
way before comparing keywords.
2020-10-09 01:04:44 +01:00
rofl0r 393e51ba45 conf: remove second instance of empty parens ERE group
likewise
2020-10-09 01:00:56 +01:00
rofl0r b07f7a8422 conf: remove empty parens group from regex
using an empty group () is not defined in the posix spec, and as such
"undefined behaviour", even though it happened to work with both GLIBC
and MUSL libc, as well as with oniguruma's POSIX compatibility API.

we used this idiom as a trick when refactoring the regex parsing,
in order not to change the match indices of all the handler functions,
ignorant that this is not explicitly allowed by the spec.

to make future refactoring easier, we introduce a MGROUP1 macro that's
added to each match group index, so we have only a single knob to turn
in case a similar change becomes necessary again.
2020-10-09 00:38:13 +01:00
rofl0r 3eb238634a conf: properly escape tab in whitespace class 2020-10-09 00:23:47 +01:00
rofl0r f1f3994d09 conf: factor out list of regex into separate header
this allows to include the regexes in another file and apply
transformations and experiments.
2020-10-09 00:22:14 +01:00
30 changed files with 1006 additions and 659 deletions

No files matched your search

@@ -1,20 +0,0 @@
---
name: New Issue, Bug report, Question
about: New Issue, Bug report, Question
title: ''
labels: ''
assignees: ''
---
# IMPORTANT NOTICE
Before filing an issue here PLEASE keep in mind that **tinyproxy 1.10.0 and older are no longer supported**.
Do not report issues with 1.10.0 or older, first try latest release 1.11.0, or even better, git master, and see whether the issue is already fixed.
## Tinyproxy version
State the tinyproxy version you're using; whether git master or 1.11.0 stable.
## Issue
Fill in your Issue text here.
A good issue report is detailed and includes full error messages from tinyproxy's output, not "X doesn't work".
-36
View File
@@ -1,36 +0,0 @@
name: CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: ./autogen.sh
- run: ./configure
- run: make
- run: make test
test-macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- run: brew install automake
- run: ./autogen.sh
- run: ./configure
- run: make
valgrind-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: install valgrind
run: sudo apt-get install --assume-yes valgrind
- run: ./autogen.sh
- run: ./configure --enable-debug --enable-transparent --enable-reverse
- run: make
- run: make test
- run: make valgrind-test
+1 -1
View File
@@ -1 +1 @@
1.11.0 1.11.0-rc1
+21 -7
View File
@@ -170,6 +170,17 @@ if test x"$debug_enabled" != x"yes" ; then
CFLAGS="-DNDEBUG $CFLAGS" CFLAGS="-DNDEBUG $CFLAGS"
fi fi
AS_ECHO_N(["checking to see if linker understands -z,defs... "])
LDFLAGS_OLD="-Wl $LDFLAGS"
LDFLAGS="-Wl,-z,defs $LDFLAGS"
AC_LINK_IFELSE([AC_LANG_PROGRAM()],
AS_ECHO("yes"),
AS_ECHO("no"); LDFLAGS="$LDFLAGS_OLD")
dnl
dnl Make sure we can actually handle the "--with-*" and "--enable-*" stuff.
dnl
dnl dnl
dnl Substitute the variables into the various Makefiles dnl Substitute the variables into the various Makefiles
dnl dnl
@@ -199,16 +210,19 @@ AH_TEMPLATE([HAVE_GPERF],
[Whether you have gperf installed for faster config parsing.]) [Whether you have gperf installed for faster config parsing.])
if test "x$GPERF" != "x" -a "x$GPERF" != "xno" ; then if test "x$GPERF" != "x" -a "x$GPERF" != "xno" ; then
AS_ECHO_N(["checking whether gperf is recent enough... "])
if "$GPERF" < src/conf-tokens.gperf >/dev/null 2>&1 ; then
AS_ECHO("yes")
AC_DEFINE(HAVE_GPERF) AC_DEFINE(HAVE_GPERF)
else
AM_CONDITIONAL(HAVE_GPERF, false)
AS_ECHO("no")
fi
fi fi
AC_PATH_PROG(RAGEL, ragel, no)
AM_CONDITIONAL(HAVE_RAGEL, test "x$RAGEL" != "x" -a "x$RAGEL" != "xno")
AH_TEMPLATE([HAVE_RAGEL],
[Whether you have ragel installed for faster config parsing.])
if test "x$RAGEL" != "x" -a "x$RAGEL" != "xno" ; then
AC_DEFINE(HAVE_RAGEL)
fi
AC_CONFIG_FILES([ AC_CONFIG_FILES([
Makefile Makefile
src/Makefile src/Makefile
-9
View File
@@ -58,8 +58,6 @@ only on one specific address.
This allows you to specify which address Tinyproxy will bind This allows you to specify which address Tinyproxy will bind
to for outgoing connections to web servers or upstream proxies. to for outgoing connections to web servers or upstream proxies.
This parameter may be specified multiple times, then Tinyproxy
will try all the specified addresses in order.
=item B<BindSame> =item B<BindSame>
@@ -293,11 +291,6 @@ If this boolean option is set to `Yes` or `On`, filtering is
performed for URLs rather than for domains. The default is to performed for URLs rather than for domains. The default is to
filter based on domains. filter based on domains.
Note that filtering for URLs works only in plain HTTP scenarios.
Since HTTPS has become ubiquitous during the last years, this
will only work on a tiny fraction of websites, so it is
recommended not to use this option.
=item B<FilterExtended> =item B<FilterExtended>
If this boolean option is set to `Yes`, then extended POSIX If this boolean option is set to `Yes`, then extended POSIX
@@ -316,8 +309,6 @@ The default filtering policy is to allow everything that is
not matched by a filtering rule. Setting `FilterDefaultDeny` not matched by a filtering rule. Setting `FilterDefaultDeny`
to `Yes` changes the policy do deny everything but the domains to `Yes` changes the policy do deny everything but the domains
or URLs matched by the filtering rules. or URLs matched by the filtering rules.
In other words, if set to `No` the Filter list acts as a
blacklist, if set to `Yes` as a whitelist.
=item B<Anonymous> =item B<Anonymous>
+12 -2
View File
@@ -24,7 +24,6 @@ AM_CPPFLAGS = \
-DLOCALSTATEDIR=\"${localstatedir}\" -DLOCALSTATEDIR=\"${localstatedir}\"
tinyproxy_SOURCES = \ tinyproxy_SOURCES = \
hostspec.c hostspec.h \
acl.c acl.h \ acl.c acl.h \
anonymous.c anonymous.h \ anonymous.c anonymous.h \
buffer.c buffer.h \ buffer.c buffer.h \
@@ -61,11 +60,22 @@ EXTRA_tinyproxy_SOURCES = filter.c filter.h \
tinyproxy_DEPENDENCIES = @ADDITIONAL_OBJECTS@ tinyproxy_DEPENDENCIES = @ADDITIONAL_OBJECTS@
tinyproxy_LDADD = @ADDITIONAL_OBJECTS@ -lpthread tinyproxy_LDADD = @ADDITIONAL_OBJECTS@ -lpthread
CLEANFILES =
if HAVE_GPERF if HAVE_GPERF
conf-tokens.c: conf-tokens-gperf.inc conf-tokens.c: conf-tokens-gperf.inc
conf-tokens-gperf.inc: conf-tokens.gperf conf-tokens-gperf.inc: conf-tokens.gperf
$(GPERF) $< > $@ $(GPERF) $< > $@
endif endif
EXTRA_DIST = conf-tokens.gperf if HAVE_RAGEL
conf.c: conf_regex.inc
conf_regex.inc: conf_regex.rl
$(RAGEL) $(RAGEL_FLAGS) -o $@ $<
CLEANFILES += conf_regex.inc
endif
EXTRA_DIST = conf-tokens.gperf conf_regex.rl
+128 -21
View File
@@ -29,7 +29,16 @@
#include "network.h" #include "network.h"
#include "sock.h" #include "sock.h"
#include "sblist.h" #include "sblist.h"
#include "hostspec.h"
#include <limits.h>
/* Define how long an IPv6 address is in bytes (128 bits, 16 bytes) */
#define IPV6_LEN 16
enum acl_type {
ACL_STRING,
ACL_NUMERIC
};
/* /*
* Hold the information about a particular access control. We store * Hold the information about a particular access control. We store
@@ -38,9 +47,66 @@
*/ */
struct acl_s { struct acl_s {
acl_access_t access; acl_access_t access;
struct hostspec h; enum acl_type type;
union {
char *string;
struct {
unsigned char network[IPV6_LEN];
unsigned char mask[IPV6_LEN];
} ip;
} address;
}; };
/*
* Fills in the netmask array given a numeric value.
*
* Returns:
* 0 on success
* -1 on failure (invalid mask value)
*
*/
static int
fill_netmask_array (char *bitmask_string, int v6,
unsigned char array[], size_t len)
{
unsigned int i;
unsigned long int mask;
char *endptr;
errno = 0; /* to distinguish success/failure after call */
mask = strtoul (bitmask_string, &endptr, 10);
/* check for various conversion errors */
if ((errno == ERANGE && mask == ULONG_MAX)
|| (errno != 0 && mask == 0) || (endptr == bitmask_string))
return -1;
if (v6 == 0) {
/* The mask comparison is done as an IPv6 address, so
* convert to a longer mask in the case of IPv4
* addresses. */
mask += 12 * 8;
}
/* check valid range for a bit mask */
if (mask > (8 * len))
return -1;
/* we have a valid range to fill in the array */
for (i = 0; i != len; ++i) {
if (mask >= 8) {
array[i] = 0xff;
mask -= 8;
} else if (mask > 0) {
array[i] = (unsigned char) (0xff << (8 - mask));
mask = 0;
} else {
array[i] = 0;
}
}
return 0;
}
/** /**
* If the access list has not been set up, create it. * If the access list has not been set up, create it.
@@ -72,6 +138,7 @@ int
insert_acl (char *location, acl_access_t access_type, acl_list_t *access_list) insert_acl (char *location, acl_access_t access_type, acl_list_t *access_list)
{ {
struct acl_s acl; struct acl_s acl;
char *mask, ip_dst[IPV6_LEN];
assert (location != NULL); assert (location != NULL);
@@ -83,11 +150,55 @@ insert_acl (char *location, acl_access_t access_type, acl_list_t *access_list)
*/ */
memset (&acl, 0, sizeof (struct acl_s)); memset (&acl, 0, sizeof (struct acl_s));
acl.access = access_type; acl.access = access_type;
if(hostspec_parse(location, &acl.h) || acl.h.type == HST_NONE)
return -1; if ((mask = strrchr(location, '/')))
*(mask++) = 0;
/*
* Check for a valid IP address (the simplest case) first.
*/
if (full_inet_pton (location, ip_dst) > 0) {
acl.type = ACL_NUMERIC;
memcpy (acl.address.ip.network, ip_dst, IPV6_LEN);
if(!mask) memset (acl.address.ip.mask, 0xff, IPV6_LEN);
else {
char dst[sizeof(struct in6_addr)];
int v6, i;
/* Check if the IP address before the netmask is
* an IPv6 address */
if (inet_pton(AF_INET6, location, dst) > 0)
v6 = 1;
else
v6 = 0;
if (fill_netmask_array
(mask, v6, &(acl.address.ip.mask[0]), IPV6_LEN)
< 0)
goto err;
for (i = 0; i < IPV6_LEN; i++)
acl.address.ip.network[i] = ip_dst[i] &
acl.address.ip.mask[i];
}
} else {
/* either bogus IP or hostname */
/* bogus ipv6 ? */
if (mask || strchr (location, ':'))
goto err;
/* In all likelihood a string */
acl.type = ACL_STRING;
acl.address.string = safestrdup (location);
if (!acl.address.string)
goto err;
}
if(!sblist_add(*access_list, &acl)) return -1; if(!sblist_add(*access_list, &acl)) return -1;
return 0; return 0;
err:;
/* restore mask for proper error message */
if(mask) *(--mask) = '/';
return -1;
} }
/* /*
@@ -108,7 +219,7 @@ acl_string_processing (struct acl_s *acl, const char *ip_address,
size_t test_length, match_length; size_t test_length, match_length;
char ipbuf[512]; char ipbuf[512];
assert (acl && acl->h.type == HST_STRING); assert (acl && acl->type == ACL_STRING);
assert (ip_address && strlen (ip_address) > 0); assert (ip_address && strlen (ip_address) > 0);
/* /*
@@ -116,11 +227,11 @@ acl_string_processing (struct acl_s *acl, const char *ip_address,
* do a string based test only; otherwise, we can do a reverse * do a string based test only; otherwise, we can do a reverse
* lookup test as well. * lookup test as well.
*/ */
if (acl->h.address.string[0] != '.') { if (acl->address.string[0] != '.') {
memset (&hints, 0, sizeof (struct addrinfo)); memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo (acl->h.address.string, NULL, &hints, &res) != 0) if (getaddrinfo (acl->address.string, NULL, &hints, &res) != 0)
goto STRING_TEST; goto STRING_TEST;
ressave = res; ressave = res;
@@ -154,7 +265,7 @@ STRING_TEST:
} }
test_length = strlen (string_addr); test_length = strlen (string_addr);
match_length = strlen (acl->h.address.string); match_length = strlen (acl->address.string);
/* /*
* If the string length is shorter than AC string, return a -1 so * If the string length is shorter than AC string, return a -1 so
@@ -165,7 +276,7 @@ STRING_TEST:
if (strcasecmp if (strcasecmp
(string_addr + (test_length - match_length), (string_addr + (test_length - match_length),
acl->h.address.string) == 0) { acl->address.string) == 0) {
if (acl->access == ACL_DENY) if (acl->access == ACL_DENY)
return 0; return 0;
else else
@@ -189,11 +300,11 @@ static int check_numeric_acl (const struct acl_s *acl, uint8_t addr[IPV6_LEN])
uint8_t x, y; uint8_t x, y;
int i; int i;
assert (acl && acl->h.type == HST_NUMERIC); assert (acl && acl->type == ACL_NUMERIC);
for (i = 0; i != IPV6_LEN; ++i) { for (i = 0; i != IPV6_LEN; ++i) {
x = addr[i] & acl->h.address.ip.mask[i]; x = addr[i] & acl->address.ip.mask[i];
y = acl->h.address.ip.network[i]; y = acl->address.ip.network[i];
/* If x and y don't match, the IP addresses don't match */ /* If x and y don't match, the IP addresses don't match */
if (x != y) if (x != y)
@@ -234,12 +345,12 @@ int check_acl (const char *ip, union sockaddr_union *addr, acl_list_t access_lis
for (i = 0; i < sblist_getsize (access_list); ++i) { for (i = 0; i < sblist_getsize (access_list); ++i) {
acl = sblist_get (access_list, i); acl = sblist_get (access_list, i);
switch (acl->h.type) { switch (acl->type) {
case HST_STRING: case ACL_STRING:
perm = acl_string_processing (acl, ip, addr, string_addr); perm = acl_string_processing (acl, ip, addr, string_addr);
break; break;
case HST_NUMERIC: case ACL_NUMERIC:
if (ip[0] == '\0') if (ip[0] == '\0')
continue; continue;
@@ -247,10 +358,6 @@ int check_acl (const char *ip, union sockaddr_union *addr, acl_list_t access_lis
? check_numeric_acl (acl, numeric_addr) ? check_numeric_acl (acl, numeric_addr)
: -1; : -1;
break; break;
case HST_NONE:
perm = -1;
break;
} }
/* /*
@@ -287,8 +394,8 @@ void flush_access_list (acl_list_t access_list)
*/ */
for (i = 0; i < sblist_getsize (access_list); ++i) { for (i = 0; i < sblist_getsize (access_list); ++i) {
acl = sblist_get (access_list, i); acl = sblist_get (access_list, i);
if (acl->h.type == HST_STRING) { if (acl->type == ACL_STRING) {
safefree (acl->h.address.string); safefree (acl->address.string);
} }
} }
-1
View File
@@ -68,7 +68,6 @@
# include <arpa/inet.h> # include <arpa/inet.h>
# include <grp.h> # include <grp.h>
# include <pwd.h> # include <pwd.h>
# include <limits.h>
/* rest - some oddball headers */ /* rest - some oddball headers */
#ifdef HAVE_VALUES_H #ifdef HAVE_VALUES_H
+91 -182
View File
@@ -40,47 +40,12 @@
#include "basicauth.h" #include "basicauth.h"
#include "conf-tokens.h" #include "conf-tokens.h"
#ifdef LINE_MAX
#define TP_LINE_MAX LINE_MAX
#else
#define TP_LINE_MAX 1024
#endif
/*
* The configuration directives are defined in the structure below. Each
* directive requires a regular expression to match against, and a
* function to call when the regex is matched.
*
* Below are defined certain constant regular expression strings that
* can (and likely should) be used when building the regex for the
* given directive.
*/
#define DIGIT "[0-9]"
#define SPACE "[ \t]"
#define WS SPACE "+"
#define STR "\"([^\"]+)\""
#define BOOL "(yes|on|no|off)"
#define INT "(()" DIGIT "+)"
#define ALNUM "([-a-z0-9._]+)"
#define USERNAME "([^:]*)"
#define PASSWORD "([^@]*)"
#define IP "((([0-9]{1,3})\\.){3}[0-9]{1,3})"
#define IPMASK "(" IP "(/" DIGIT "+)?)"
#define IPV6 "(" \
"(([0-9a-f:]{2,39}))|" \
"(([0-9a-f:]{0,29}:" IP "))" \
")"
#define IPV6MASK "(" IPV6 "(/" DIGIT "+)?)"
#define BEGIN "^" SPACE "*"
#define END SPACE "*$"
/* /*
* Limit the maximum number of substring matches to a reasonably high * Limit the maximum number of substring matches to a reasonably high
* number. Given the usual structure of the configuration file, sixteen * number. Given the usual structure of the configuration file, sixteen
* substring matches should be plenty. * substring matches should be plenty.
*/ */
#define RE_MAX_MATCHES 24 #define RE_MAX_MATCHES 16
#define CP_WARN(FMT, ...) \ #define CP_WARN(FMT, ...) \
log_message (LOG_WARNING, "line %lu: " FMT, lineno, __VA_ARGS__) log_message (LOG_WARNING, "line %lu: " FMT, lineno, __VA_ARGS__)
@@ -177,7 +142,14 @@ static void config_free_regex (void);
* do not follow the pattern above. This macro is for convenience * do not follow the pattern above. This macro is for convenience
* only. * only.
*/ */
#define STDCONF(d, re, func) [CD_ ## d] = { BEGIN "()" WS re END, func, NULL } #ifdef HAVE_RAGEL
#define RE2R_EXPORT static
#include "conf_regex.inc"
typedef int (*matchfunc)(const char*, const char*, size_t, regmatch_t[]);
#define STDCONF(d, re, func) [CD_ ## d] = { func, re2r_match_ ## d }
#else
#define STDCONF(d, re, func) [CD_ ## d] = { func, BEGIN re END, NULL }
#endif
/* /*
* Holds the regular expression used to match the configuration directive, * Holds the regular expression used to match the configuration directive,
@@ -186,73 +158,15 @@ static void config_free_regex (void);
* to be compiled one. * to be compiled one.
*/ */
struct { struct {
const char *re;
CONFFILE_HANDLER handler; CONFFILE_HANDLER handler;
#ifndef HAVE_RAGEL
const char *re;
regex_t *cre; regex_t *cre;
#else
matchfunc mf;
#endif
} directives[] = { } directives[] = {
/* string arguments */ #include "conf_regex.h"
STDCONF (logfile, STR, handle_logfile),
STDCONF (pidfile, STR, handle_pidfile),
STDCONF (anonymous, STR, handle_anonymous),
STDCONF (viaproxyname, STR, handle_viaproxyname),
STDCONF (defaulterrorfile, STR, handle_defaulterrorfile),
STDCONF (statfile, STR, handle_statfile),
STDCONF (stathost, STR, handle_stathost),
STDCONF (xtinyproxy, BOOL, handle_xtinyproxy),
/* boolean arguments */
STDCONF (syslog, BOOL, handle_syslog),
STDCONF (bindsame, BOOL, handle_bindsame),
STDCONF (disableviaheader, BOOL, handle_disableviaheader),
/* integer arguments */
STDCONF (port, INT, handle_port),
STDCONF (maxclients, INT, handle_maxclients),
STDCONF (maxspareservers, INT, handle_obsolete),
STDCONF (minspareservers, INT, handle_obsolete),
STDCONF (startservers, INT, handle_obsolete),
STDCONF (maxrequestsperchild, INT, handle_obsolete),
STDCONF (timeout, INT, handle_timeout),
STDCONF (connectport, INT, handle_connectport),
/* alphanumeric arguments */
STDCONF (user, ALNUM, handle_user),
STDCONF (group, ALNUM, handle_group),
/* ip arguments */
STDCONF (listen, "(" IP "|" IPV6 ")", handle_listen),
STDCONF (allow, "(" "(" IPMASK "|" IPV6MASK ")" "|" ALNUM ")",
handle_allow),
STDCONF (deny, "(" "(" IPMASK "|" IPV6MASK ")" "|" ALNUM ")",
handle_deny),
STDCONF (bind, "(" IP "|" IPV6 ")", handle_bind),
/* other */
STDCONF (basicauth, ALNUM WS ALNUM, handle_basicauth),
STDCONF (errorfile, INT WS STR, handle_errorfile),
STDCONF (addheader, STR WS STR, handle_addheader),
#ifdef FILTER_ENABLE
/* filtering */
STDCONF (filter, STR, handle_filter),
STDCONF (filterurls, BOOL, handle_filterurls),
STDCONF (filterextended, BOOL, handle_filterextended),
STDCONF (filterdefaultdeny, BOOL, handle_filterdefaultdeny),
STDCONF (filtercasesensitive, BOOL, handle_filtercasesensitive),
#endif
#ifdef REVERSE_SUPPORT
/* Reverse proxy arguments */
STDCONF (reversebaseurl, STR, handle_reversebaseurl),
STDCONF (reverseonly, BOOL, handle_reverseonly),
STDCONF (reversemagic, BOOL, handle_reversemagic),
STDCONF (reversepath, STR "(" WS STR ")?", handle_reversepath),
#endif
#ifdef UPSTREAM_SUPPORT
STDCONF (upstream,
"(" "(none)" WS STR ")|" \
"(" "(http|socks4|socks5)" WS \
"(" USERNAME /*username*/ ":" PASSWORD /*password*/ "@" ")?"
"(" IP "|" ALNUM ")"
":" INT "(" WS STR ")?" ")", handle_upstream),
#endif
/* loglevel */
STDCONF (loglevel, "(critical|error|warning|notice|connect|info)",
handle_loglevel)
}; };
const unsigned int ndirectives = sizeof (directives) / sizeof (directives[0]); const unsigned int ndirectives = sizeof (directives) / sizeof (directives[0]);
@@ -297,7 +211,6 @@ void free_config (struct config_s *conf)
safefree (conf->group); safefree (conf->group);
stringlist_free(conf->basicauth_list); stringlist_free(conf->basicauth_list);
stringlist_free(conf->listen_addrs); stringlist_free(conf->listen_addrs);
stringlist_free(conf->bind_addrs);
#ifdef FILTER_ENABLE #ifdef FILTER_ENABLE
safefree (conf->filter); safefree (conf->filter);
#endif /* FILTER_ENABLE */ #endif /* FILTER_ENABLE */
@@ -309,6 +222,7 @@ void free_config (struct config_s *conf)
free_upstream_list (conf->upstream_list); free_upstream_list (conf->upstream_list);
#endif /* UPSTREAM_SUPPORT */ #endif /* UPSTREAM_SUPPORT */
safefree (conf->pidpath); safefree (conf->pidpath);
safefree (conf->bind_address);
safefree (conf->via_proxy_name); safefree (conf->via_proxy_name);
if (conf->errorpages) { if (conf->errorpages) {
it = 0; it = 0;
@@ -345,23 +259,26 @@ config_init (void)
{ {
unsigned int i, r; unsigned int i, r;
(void) r;
for (i = 0; i != ndirectives; ++i) { for (i = 0; i != ndirectives; ++i) {
assert (!directives[i].cre);
if (!directives[i].handler) { if (!directives[i].handler) {
directives[i].handler = handle_disabled_feature; directives[i].handler = handle_disabled_feature;
continue; continue;
} }
#ifndef HAVE_RAGEL
directives[i].cre = (regex_t *) safemalloc (sizeof (regex_t)); directives[i].cre = (regex_t *) safemalloc (sizeof (regex_t));
if (!directives[i].cre) if (!directives[i].cre)
return -1; return -1;
r = regcomp (directives[i].cre, r = regcomp (directives[i].cre,
directives[i].re, directives[i].re,
REG_EXTENDED | REG_ICASE | REG_NEWLINE); REG_EXTENDED | REG_NEWLINE);
if (r) if (r)
return r; return r;
#endif
} }
atexit (config_free_regex); atexit (config_free_regex);
@@ -376,6 +293,7 @@ config_init (void)
static void static void
config_free_regex (void) config_free_regex (void)
{ {
#ifndef HAVE_RAGEL
unsigned int i; unsigned int i;
for (i = 0; i < ndirectives; i++) { for (i = 0; i < ndirectives; i++) {
@@ -385,6 +303,7 @@ config_free_regex (void)
directives[i].cre = NULL; directives[i].cre = NULL;
} }
} }
#endif
} }
/* /*
@@ -395,18 +314,25 @@ config_free_regex (void)
* Returns 0 if a match was found and successfully processed; otherwise, * Returns 0 if a match was found and successfully processed; otherwise,
* a negative number is returned. * a negative number is returned.
*/ */
static int check_match (struct config_s *conf, const char *line, static int check_match (struct config_s *conf,
const char *line, const char* lineend,
unsigned long lineno, enum config_directive cd) unsigned long lineno, enum config_directive cd)
{ {
regmatch_t match[RE_MAX_MATCHES]; regmatch_t match[RE_MAX_MATCHES];
unsigned int i = cd; unsigned int i = cd;
#ifndef HAVE_RAGEL
(void) lineend;
if (!directives[i].cre) if (!directives[i].cre)
return (*directives[i].handler) (conf, line, lineno, match); return (*directives[i].handler) (conf, line, lineno, match);
if (!regexec if (!regexec
(directives[i].cre, line, RE_MAX_MATCHES, match, 0)) (directives[i].cre, line, RE_MAX_MATCHES, match, 0))
return (*directives[i].handler) (conf, line, lineno, match); return (*directives[i].handler) (conf, line, lineno, match);
#else
if (!directives[i].mf(line, lineend, RE_MAX_MATCHES, match))
return (*directives[i].handler) (conf, line, lineno, match);
#endif
return -1; return -1;
} }
@@ -415,7 +341,7 @@ static int check_match (struct config_s *conf, const char *line,
*/ */
static int config_parse (struct config_s *conf, FILE * f) static int config_parse (struct config_s *conf, FILE * f)
{ {
char buffer[TP_LINE_MAX], *p, *q, c; char buffer[LINE_MAX], *p, *q;
const struct config_directive_entry *e; const struct config_directive_entry *e;
unsigned long lineno = 1; unsigned long lineno = 1;
@@ -426,11 +352,14 @@ static int config_parse (struct config_s *conf, FILE * f)
if(!*p) continue; if(!*p) continue;
q = p; q = p;
while(!isspace(*q))q++; while(!isspace(*q))q++;
c = *q;
*q = 0; *q = 0;
e = config_directive_find(p, strlen(p)); e = config_directive_find(p, strlen(p));
*q = c; ++q;
if (!e || e->value == CD_NIL || check_match (conf, q, lineno, e->value)) { while(isspace(*q))++q;
p = q;
while(*p && *p != '\n') ++p;
while(isspace(*p)) *(p--) = 0;
if (!e || e->value == CD_NIL || check_match (conf, q, ++p, lineno, e->value)) {
fprintf (stderr, "ERROR: Syntax error on line %lu\n", lineno); fprintf (stderr, "ERROR: Syntax error on line %lu\n", lineno);
return 1; return 1;
} }
@@ -511,7 +440,7 @@ int reload_config_file (const char *config_fname, struct config_s *conf)
goto done; goto done;
} }
if (!conf->user && !geteuid()) { if (!conf->user) {
log_message (LOG_WARNING, "You SHOULD set a UserName in the " log_message (LOG_WARNING, "You SHOULD set a UserName in the "
"config file. Using current user instead."); "config file. Using current user instead.");
} }
@@ -631,19 +560,21 @@ set_int_arg (unsigned int *var, const char *line, regmatch_t * match)
* *
***********************************************************************/ ***********************************************************************/
#define MGROUP1 -1
static HANDLE_FUNC (handle_logfile) static HANDLE_FUNC (handle_logfile)
{ {
return set_string_arg (&conf->logf_name, line, &match[2]); return set_string_arg (&conf->logf_name, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_pidfile) static HANDLE_FUNC (handle_pidfile)
{ {
return set_string_arg (&conf->pidpath, line, &match[2]); return set_string_arg (&conf->pidpath, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_anonymous) static HANDLE_FUNC (handle_anonymous)
{ {
char *arg = get_string_arg (line, &match[2]); char *arg = get_string_arg (line, &match[MGROUP1+2]);
if (!arg) if (!arg)
return -1; return -1;
@@ -659,7 +590,7 @@ static HANDLE_FUNC (handle_anonymous)
static HANDLE_FUNC (handle_viaproxyname) static HANDLE_FUNC (handle_viaproxyname)
{ {
int r = set_string_arg (&conf->via_proxy_name, line, &match[2]); int r = set_string_arg (&conf->via_proxy_name, line, &match[MGROUP1+2]);
if (r) if (r)
return r; return r;
@@ -671,7 +602,7 @@ static HANDLE_FUNC (handle_viaproxyname)
static HANDLE_FUNC (handle_disableviaheader) static HANDLE_FUNC (handle_disableviaheader)
{ {
int r = set_bool_arg (&conf->disable_viaheader, line, &match[2]); int r = set_bool_arg (&conf->disable_viaheader, line, &match[MGROUP1+2]);
if (r) { if (r) {
return r; return r;
@@ -684,17 +615,17 @@ static HANDLE_FUNC (handle_disableviaheader)
static HANDLE_FUNC (handle_defaulterrorfile) static HANDLE_FUNC (handle_defaulterrorfile)
{ {
return set_string_arg (&conf->errorpage_undef, line, &match[2]); return set_string_arg (&conf->errorpage_undef, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_statfile) static HANDLE_FUNC (handle_statfile)
{ {
return set_string_arg (&conf->statpage, line, &match[2]); return set_string_arg (&conf->statpage, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_stathost) static HANDLE_FUNC (handle_stathost)
{ {
int r = set_string_arg (&conf->stathost, line, &match[2]); int r = set_string_arg (&conf->stathost, line, &match[MGROUP1+2]);
if (r) if (r)
return r; return r;
@@ -705,7 +636,7 @@ static HANDLE_FUNC (handle_stathost)
static HANDLE_FUNC (handle_xtinyproxy) static HANDLE_FUNC (handle_xtinyproxy)
{ {
#ifdef XTINYPROXY_ENABLE #ifdef XTINYPROXY_ENABLE
return set_bool_arg (&conf->add_xtinyproxy, line, &match[2]); return set_bool_arg (&conf->add_xtinyproxy, line, &match[MGROUP1+2]);
#else #else
fprintf (stderr, fprintf (stderr,
"XTinyproxy NOT Enabled! Recompile with --enable-xtinyproxy\n"); "XTinyproxy NOT Enabled! Recompile with --enable-xtinyproxy\n");
@@ -715,12 +646,12 @@ static HANDLE_FUNC (handle_xtinyproxy)
static HANDLE_FUNC (handle_syslog) static HANDLE_FUNC (handle_syslog)
{ {
return set_bool_arg (&conf->syslog, line, &match[2]); return set_bool_arg (&conf->syslog, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_bindsame) static HANDLE_FUNC (handle_bindsame)
{ {
int r = set_bool_arg (&conf->bindsame, line, &match[2]); int r = set_bool_arg (&conf->bindsame, line, &match[MGROUP1+2]);
if (r) if (r)
return r; return r;
@@ -730,7 +661,7 @@ static HANDLE_FUNC (handle_bindsame)
static HANDLE_FUNC (handle_port) static HANDLE_FUNC (handle_port)
{ {
set_int_arg (&conf->port, line, &match[2]); set_int_arg (&conf->port, line, &match[MGROUP1+2]);
if (conf->port > 65535) { if (conf->port > 65535) {
fprintf (stderr, "Bad port number (%d) supplied for Port.\n", fprintf (stderr, "Bad port number (%d) supplied for Port.\n",
@@ -743,7 +674,7 @@ static HANDLE_FUNC (handle_port)
static HANDLE_FUNC (handle_maxclients) static HANDLE_FUNC (handle_maxclients)
{ {
set_int_arg (&conf->maxclients, line, &match[2]); set_int_arg (&conf->maxclients, line, &match[MGROUP1+2]);
return 0; return 0;
} }
@@ -756,24 +687,24 @@ static HANDLE_FUNC (handle_obsolete)
static HANDLE_FUNC (handle_timeout) static HANDLE_FUNC (handle_timeout)
{ {
return set_int_arg (&conf->idletimeout, line, &match[2]); return set_int_arg (&conf->idletimeout, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_connectport) static HANDLE_FUNC (handle_connectport)
{ {
add_connect_port_allowed (get_long_arg (line, &match[2]), add_connect_port_allowed (get_long_arg (line, &match[MGROUP1+2]),
&conf->connect_ports); &conf->connect_ports);
return 0; return 0;
} }
static HANDLE_FUNC (handle_user) static HANDLE_FUNC (handle_user)
{ {
return set_string_arg (&conf->user, line, &match[2]); return set_string_arg (&conf->user, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_group) static HANDLE_FUNC (handle_group)
{ {
return set_string_arg (&conf->group, line, &match[2]); return set_string_arg (&conf->group, line, &match[MGROUP1+2]);
} }
static void warn_invalid_address(char *arg, unsigned long lineno) { static void warn_invalid_address(char *arg, unsigned long lineno) {
@@ -782,7 +713,7 @@ static void warn_invalid_address(char *arg, unsigned long lineno) {
static HANDLE_FUNC (handle_allow) static HANDLE_FUNC (handle_allow)
{ {
char *arg = get_string_arg (line, &match[2]); char *arg = get_string_arg (line, &match[MGROUP1+2]);
if(insert_acl (arg, ACL_ALLOW, &conf->access_list) < 0) if(insert_acl (arg, ACL_ALLOW, &conf->access_list) < 0)
warn_invalid_address (arg, lineno); warn_invalid_address (arg, lineno);
@@ -792,7 +723,7 @@ static HANDLE_FUNC (handle_allow)
static HANDLE_FUNC (handle_deny) static HANDLE_FUNC (handle_deny)
{ {
char *arg = get_string_arg (line, &match[2]); char *arg = get_string_arg (line, &match[MGROUP1+2]);
if(insert_acl (arg, ACL_DENY, &conf->access_list) < 0) if(insert_acl (arg, ACL_DENY, &conf->access_list) < 0)
warn_invalid_address (arg, lineno); warn_invalid_address (arg, lineno);
@@ -802,33 +733,18 @@ static HANDLE_FUNC (handle_deny)
static HANDLE_FUNC (handle_bind) static HANDLE_FUNC (handle_bind)
{ {
char *arg = get_string_arg (line, &match[2]); int r = set_string_arg (&conf->bind_address, line, &match[MGROUP1+2]);
if (arg == NULL) {
return -1;
}
if (conf->bind_addrs == NULL) {
conf->bind_addrs = sblist_new(sizeof(char*), 16);
if (conf->bind_addrs == NULL) {
CP_WARN ("Could not create a list "
"of bind addresses.", "");
safefree(arg);
return -1;
}
}
sblist_add (conf->bind_addrs, &arg);
if (r)
return r;
log_message (LOG_INFO, log_message (LOG_INFO,
"Added bind address [%s] for outgoing connections.", arg); "Outgoing connections bound to IP %s", conf->bind_address);
return 0; return 0;
} }
static HANDLE_FUNC (handle_listen) static HANDLE_FUNC (handle_listen)
{ {
char *arg = get_string_arg (line, &match[2]); char *arg = get_string_arg (line, &match[MGROUP1+2]);
if (arg == NULL) { if (arg == NULL) {
return -1; return -1;
@@ -853,15 +769,8 @@ static HANDLE_FUNC (handle_listen)
static HANDLE_FUNC (handle_errorfile) static HANDLE_FUNC (handle_errorfile)
{ {
/* unsigned long int err = get_long_arg (line, &match[MGROUP1+2]);
* Because an integer is defined as ((0x)?[[:digit:]]+) _two_ char *page = get_string_arg (line, &match[MGROUP1+3]);
* match places are used. match[2] matches the full digit
* string, while match[3] matches only the "0x" part if
* present. This is why the "string" is located at
* match[4] (rather than the more intuitive match[3].
*/
unsigned long int err = get_long_arg (line, &match[2]);
char *page = get_string_arg (line, &match[4]);
if(add_new_errorpage (conf, page, err) < 0) { if(add_new_errorpage (conf, page, err) < 0) {
CP_WARN ("add_new_errorpage() failed: '%s'", page); CP_WARN ("add_new_errorpage() failed: '%s'", page);
@@ -872,8 +781,8 @@ static HANDLE_FUNC (handle_errorfile)
static HANDLE_FUNC (handle_addheader) static HANDLE_FUNC (handle_addheader)
{ {
char *name = get_string_arg (line, &match[2]); char *name = get_string_arg (line, &match[MGROUP1+2]);
char *value = get_string_arg (line, &match[3]); char *value = get_string_arg (line, &match[MGROUP1+3]);
http_header_t header; http_header_t header;
if (!conf->add_headers) { if (!conf->add_headers) {
@@ -914,7 +823,7 @@ static HANDLE_FUNC (handle_loglevel)
sizeof (log_levels) / sizeof (log_levels[0]); sizeof (log_levels) / sizeof (log_levels[0]);
unsigned int i; unsigned int i;
char *arg = get_string_arg (line, &match[2]); char *arg = get_string_arg (line, &match[MGROUP1+2]);
for (i = 0; i != nlevels; ++i) { for (i = 0; i != nlevels; ++i) {
if (!strcasecmp (arg, log_levels[i].string)) { if (!strcasecmp (arg, log_levels[i].string)) {
@@ -931,10 +840,10 @@ static HANDLE_FUNC (handle_loglevel)
static HANDLE_FUNC (handle_basicauth) static HANDLE_FUNC (handle_basicauth)
{ {
char *user, *pass; char *user, *pass;
user = get_string_arg(line, &match[2]); user = get_string_arg(line, &match[MGROUP1+2]);
if (!user) if (!user)
return -1; return -1;
pass = get_string_arg(line, &match[3]); pass = get_string_arg(line, &match[MGROUP1+3]);
if (!pass) { if (!pass) {
safefree (user); safefree (user);
return -1; return -1;
@@ -952,48 +861,48 @@ static HANDLE_FUNC (handle_basicauth)
#ifdef FILTER_ENABLE #ifdef FILTER_ENABLE
static HANDLE_FUNC (handle_filter) static HANDLE_FUNC (handle_filter)
{ {
return set_string_arg (&conf->filter, line, &match[2]); return set_string_arg (&conf->filter, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_filterurls) static HANDLE_FUNC (handle_filterurls)
{ {
return set_bool_arg (&conf->filter_url, line, &match[2]); return set_bool_arg (&conf->filter_url, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_filterextended) static HANDLE_FUNC (handle_filterextended)
{ {
return set_bool_arg (&conf->filter_extended, line, &match[2]); return set_bool_arg (&conf->filter_extended, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_filterdefaultdeny) static HANDLE_FUNC (handle_filterdefaultdeny)
{ {
assert (match[2].rm_so != -1); assert (match[MGROUP1+2].rm_so != -1);
if (get_bool_arg (line, &match[2])) if (get_bool_arg (line, &match[MGROUP1+2]))
filter_set_default_policy (FILTER_DEFAULT_DENY); filter_set_default_policy (FILTER_DEFAULT_DENY);
return 0; return 0;
} }
static HANDLE_FUNC (handle_filtercasesensitive) static HANDLE_FUNC (handle_filtercasesensitive)
{ {
return set_bool_arg (&conf->filter_casesensitive, line, &match[2]); return set_bool_arg (&conf->filter_casesensitive, line, &match[MGROUP1+2]);
} }
#endif #endif
#ifdef REVERSE_SUPPORT #ifdef REVERSE_SUPPORT
static HANDLE_FUNC (handle_reverseonly) static HANDLE_FUNC (handle_reverseonly)
{ {
return set_bool_arg (&conf->reverseonly, line, &match[2]); return set_bool_arg (&conf->reverseonly, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_reversemagic) static HANDLE_FUNC (handle_reversemagic)
{ {
return set_bool_arg (&conf->reversemagic, line, &match[2]); return set_bool_arg (&conf->reversemagic, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_reversebaseurl) static HANDLE_FUNC (handle_reversebaseurl)
{ {
return set_string_arg (&conf->reversebaseurl, line, &match[2]); return set_string_arg (&conf->reversebaseurl, line, &match[MGROUP1+2]);
} }
static HANDLE_FUNC (handle_reversepath) static HANDLE_FUNC (handle_reversepath)
@@ -1003,12 +912,12 @@ static HANDLE_FUNC (handle_reversepath)
*/ */
char *arg1, *arg2; char *arg1, *arg2;
arg1 = get_string_arg (line, &match[2]); arg1 = get_string_arg (line, &match[MGROUP1+2]);
if (!arg1) if (!arg1)
return -1; return -1;
if (match[4].rm_so != -1) { if (match[MGROUP1+4].rm_so != -1) {
arg2 = get_string_arg (line, &match[4]); arg2 = get_string_arg (line, &match[MGROUP1+4]);
if (!arg2) { if (!arg2) {
safefree (arg1); safefree (arg1);
return -1; return -1;
@@ -1049,12 +958,12 @@ static HANDLE_FUNC (handle_upstream)
enum proxy_type pt; enum proxy_type pt;
enum upstream_build_error ube; enum upstream_build_error ube;
if (match[3].rm_so != -1) { if (match[MGROUP1+3].rm_so != -1) {
tmp = get_string_arg (line, &match[3]); tmp = get_string_arg (line, &match[MGROUP1+3]);
if(!strcmp(tmp, "none")) { if(!strcmp(tmp, "none")) {
safefree(tmp); safefree(tmp);
if (match[4].rm_so == -1) return -1; if (match[MGROUP1+4].rm_so == -1) return -1;
domain = get_string_arg (line, &match[4]); domain = get_string_arg (line, &match[MGROUP1+4]);
if (!domain) if (!domain)
return -1; return -1;
ube = upstream_add (NULL, 0, domain, 0, 0, PT_NONE, &conf->upstream_list); ube = upstream_add (NULL, 0, domain, 0, 0, PT_NONE, &conf->upstream_list);
@@ -1063,7 +972,7 @@ static HANDLE_FUNC (handle_upstream)
} }
} }
mi = 6; mi = MGROUP1+6;
tmp = get_string_arg (line, &match[mi]); tmp = get_string_arg (line, &match[mi]);
pt = pt_from_string(tmp); pt = pt_from_string(tmp);
@@ -1081,10 +990,10 @@ static HANDLE_FUNC (handle_upstream)
ip = get_string_arg (line, &match[mi]); ip = get_string_arg (line, &match[mi]);
if (!ip) if (!ip)
return -1; return -1;
mi += 5; mi += 3;
port = (int) get_long_arg (line, &match[mi]); port = (int) get_long_arg (line, &match[mi]);
mi += 3; mi += 2;
if (match[mi].rm_so != -1) if (match[mi].rm_so != -1)
domain = get_string_arg (line, &match[mi]); domain = get_string_arg (line, &match[mi]);
+1 -1
View File
@@ -68,7 +68,7 @@ struct config_s {
#endif /* UPSTREAM_SUPPORT */ #endif /* UPSTREAM_SUPPORT */
char *pidpath; char *pidpath;
unsigned int idletimeout; unsigned int idletimeout;
sblist *bind_addrs; char *bind_address;
unsigned int bindsame; unsigned int bindsame;
/* /*
+93
View File
@@ -0,0 +1,93 @@
/*
* The configuration directives are defined in the structure below. Each
* directive requires a regular expression to match against, and a
* function to call when the regex is matched.
*
* Below are defined certain constant regular expression strings that
* can (and likely should) be used when building the regex for the
* given directive.
*/
#define DIGIT "[0-9]"
#define SPACE "[ \\t]"
#define WS SPACE "+"
#define STR "\"([^\"]+)\""
#define BOOL "([Yy][Ee][Ss]|[Oo][Nn]|[Nn][Oo]|[Oo][Ff][Ff])"
#define INT "(" DIGIT "+)"
#define ALNUM "([-A-Za-z0-9._]+)"
#define USERNAME "([^:]*)"
#define PASSWORD "([^@]*)"
#define IP "([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)"
#define IPMASK "(" IP "(/" DIGIT "+)?)"
#define IPV6 "(" \
"(([0-9a-fA-F:]{2,39}))|" \
"(([0-9a-fA-F:]{0,29}:" IP "))" \
")"
#define IPV6MASK "(" IPV6 "(/" DIGIT "+)?)"
#define BEGIN "^"
#define END "$"
STDCONF (logfile, STR, handle_logfile),
STDCONF (pidfile, STR, handle_pidfile),
STDCONF (anonymous, STR, handle_anonymous),
STDCONF (viaproxyname, STR, handle_viaproxyname),
STDCONF (defaulterrorfile, STR, handle_defaulterrorfile),
STDCONF (statfile, STR, handle_statfile),
STDCONF (stathost, STR, handle_stathost),
STDCONF (xtinyproxy, BOOL, handle_xtinyproxy),
/* boolean arguments */
STDCONF (syslog, BOOL, handle_syslog),
STDCONF (bindsame, BOOL, handle_bindsame),
STDCONF (disableviaheader, BOOL, handle_disableviaheader),
/* integer arguments */
STDCONF (port, INT, handle_port),
STDCONF (maxclients, INT, handle_maxclients),
STDCONF (maxspareservers, INT, handle_obsolete),
STDCONF (minspareservers, INT, handle_obsolete),
STDCONF (startservers, INT, handle_obsolete),
STDCONF (maxrequestsperchild, INT, handle_obsolete),
STDCONF (timeout, INT, handle_timeout),
STDCONF (connectport, INT, handle_connectport),
/* alphanumeric arguments */
STDCONF (user, ALNUM, handle_user),
STDCONF (group, ALNUM, handle_group),
/* ip arguments */
STDCONF (listen, "(" IP "|" IPV6 ")", handle_listen),
STDCONF (allow, "(" "(" IPMASK "|" IPV6MASK ")" "|" ALNUM ")",
handle_allow),
STDCONF (deny, "(" "(" IPMASK "|" IPV6MASK ")" "|" ALNUM ")",
handle_deny),
STDCONF (bind, "(" IP "|" IPV6 ")", handle_bind),
/* other */
STDCONF (basicauth, ALNUM WS ALNUM, handle_basicauth),
STDCONF (errorfile, INT WS STR, handle_errorfile),
STDCONF (addheader, STR WS STR, handle_addheader),
#ifdef FILTER_ENABLE
/* filtering */
STDCONF (filter, STR, handle_filter),
STDCONF (filterurls, BOOL, handle_filterurls),
STDCONF (filterextended, BOOL, handle_filterextended),
STDCONF (filterdefaultdeny, BOOL, handle_filterdefaultdeny),
STDCONF (filtercasesensitive, BOOL, handle_filtercasesensitive),
#endif
#ifdef REVERSE_SUPPORT
/* Reverse proxy arguments */
STDCONF (reversebaseurl, STR, handle_reversebaseurl),
STDCONF (reverseonly, BOOL, handle_reverseonly),
STDCONF (reversemagic, BOOL, handle_reversemagic),
STDCONF (reversepath, STR "(" WS STR ")?", handle_reversepath),
#endif
#ifdef UPSTREAM_SUPPORT
STDCONF (upstream,
"(" "(none)" WS STR ")|" \
"(" "(http|socks4|socks5)" WS \
"(" USERNAME /*username*/ ":" PASSWORD /*password*/ "@" ")?"
"(" IP "|" ALNUM ")"
":" INT "(" WS STR ")?" ")", handle_upstream),
#endif
/* loglevel */
STDCONF (loglevel, "([Cc]ritical|[Ee]rror|[Ww]arning|[Nn]otice|[Cc]onnect|[Ii]nfo)",
handle_loglevel)
+512
View File
@@ -0,0 +1,512 @@
/* automatically generated with re2r by rofl0r */
%%{
machine logfile;
action A1 { matches[1].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
main := '"'([^"]+) >A1 %E1 '"' ;
}%%
RE2R_EXPORT int re2r_match_logfile(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
RE2R_EXPORT int re2r_match_pidfile(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_anonymous(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_viaproxyname(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_defaulterrorfile(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_statfile(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_stathost(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
%%{
machine xtinyproxy;
action A1 { matches[1].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
main := ([Yy][Ee][Ss]|[Oo][Nn]|[Nn][Oo]|[Oo][Ff][Ff]) >A1 %E1 ;
}%%
RE2R_EXPORT int re2r_match_xtinyproxy(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
RE2R_EXPORT int re2r_match_syslog(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_bindsame(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_disableviaheader(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
%%{
machine port;
action A1 { matches[1].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
main := ([0-9]+) >A1 %E1 ;
}%%
RE2R_EXPORT int re2r_match_port(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
RE2R_EXPORT int re2r_match_maxclients(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_maxspareservers(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_minspareservers(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_startservers(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_maxrequestsperchild(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_timeout(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_connectport(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_port(p, pe, nmatch, matches);
}
%%{
machine user;
action A1 { matches[1].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
main := (('-'|[A-Za-z0-9._])+) >A1 %E1 ;
}%%
RE2R_EXPORT int re2r_match_user(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
RE2R_EXPORT int re2r_match_group(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_user(p, pe, nmatch, matches);
}
%%{
machine listen;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action A3 { matches[3].rm_so = p-start; }
action A4 { matches[4].rm_so = p-start; }
action A5 { matches[5].rm_so = p-start; }
action A6 { matches[6].rm_so = p-start; }
action A7 { matches[7].rm_so = p-start; }
action A8 { matches[8].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
action E3 { matches[3].rm_eo = p-start; }
action E4 { matches[4].rm_eo = p-start; }
action E5 { matches[5].rm_eo = p-start; }
action E6 { matches[6].rm_eo = p-start; }
action E7 { matches[7].rm_eo = p-start; }
action E8 { matches[8].rm_eo = p-start; }
main := (([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+) >A2 %E2 |((([0-9a-fA-F:]{2,39}) >A5 %E5 ) >A4 %E4 |(([0-9a-fA-F:]{0,29} ":" ([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+) >A8 %E8 ) >A7 %E7 ) >A6 %E6 ) >A3 %E3 ) >A1 %E1 ;
}%%
RE2R_EXPORT int re2r_match_listen(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=1,[3]=1,[4]=3,[5]=4,[6]=3,[7]=6,[8]=7,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
%%{
machine allow;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action A3 { matches[3].rm_so = p-start; }
action A4 { matches[4].rm_so = p-start; }
action A5 { matches[5].rm_so = p-start; }
action A6 { matches[6].rm_so = p-start; }
action A7 { matches[7].rm_so = p-start; }
action A8 { matches[8].rm_so = p-start; }
action A9 { matches[9].rm_so = p-start; }
action A10 { matches[10].rm_so = p-start; }
action A11 { matches[11].rm_so = p-start; }
action A12 { matches[12].rm_so = p-start; }
action A13 { matches[13].rm_so = p-start; }
action A14 { matches[14].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
action E3 { matches[3].rm_eo = p-start; }
action E4 { matches[4].rm_eo = p-start; }
action E5 { matches[5].rm_eo = p-start; }
action E6 { matches[6].rm_eo = p-start; }
action E7 { matches[7].rm_eo = p-start; }
action E8 { matches[8].rm_eo = p-start; }
action E9 { matches[9].rm_eo = p-start; }
action E10 { matches[10].rm_eo = p-start; }
action E11 { matches[11].rm_eo = p-start; }
action E12 { matches[12].rm_eo = p-start; }
action E13 { matches[13].rm_eo = p-start; }
action E14 { matches[14].rm_eo = p-start; }
main := (((([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+) >A4 %E4 ( "/" [0-9]+)? >A5 %E5 ) >A3 %E3 |(((([0-9a-fA-F:]{2,39}) >A9 %E9 ) >A8 %E8 |(([0-9a-fA-F:]{0,29} ":" ([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+) >A12 %E12 ) >A11 %E11 ) >A10 %E10 ) >A7 %E7 ( "/" [0-9]+)? >A13 %E13 ) >A6 %E6 ) >A2 %E2 |(('-'|[A-Za-z0-9._])+) >A14 %E14 ) >A1 %E1 ;
}%%
RE2R_EXPORT int re2r_match_allow(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=1,[3]=2,[4]=3,[5]=3,[6]=2,[7]=6,[8]=7,[9]=8,[10]=7,[11]=10,[12]=11,[13]=6,[14]=1,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
RE2R_EXPORT int re2r_match_deny(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_allow(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_bind(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_listen(p, pe, nmatch, matches);
}
%%{
machine basicauth;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
main := (('-'|[A-Za-z0-9._])+) >A1 %E1 [ \t]+(('-'|[A-Za-z0-9._])+) >A2 %E2 ;
}%%
RE2R_EXPORT int re2r_match_basicauth(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
%%{
machine errorfile;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
main := ([0-9]+) >A1 %E1 [ \t]+'"'([^"]+) >A2 %E2 '"' ;
}%%
RE2R_EXPORT int re2r_match_errorfile(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
%%{
machine addheader;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
main := '"'([^"]+) >A1 %E1 '"'[ \t]+'"'([^"]+) >A2 %E2 '"' ;
}%%
RE2R_EXPORT int re2r_match_addheader(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
RE2R_EXPORT int re2r_match_filter(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_filterurls(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_filterextended(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_filterdefaultdeny(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_filtercasesensitive(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_reversebaseurl(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_logfile(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_reverseonly(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
RE2R_EXPORT int re2r_match_reversemagic(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
return re2r_match_xtinyproxy(p, pe, nmatch, matches);
}
%%{
machine reversepath;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action A3 { matches[3].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
action E3 { matches[3].rm_eo = p-start; }
main := '"'([^"]+) >A1 %E1 '"'([ \t]+'"'([^"]+) >A3 %E3 '"')? >A2 %E2 ;
}%%
RE2R_EXPORT int re2r_match_reversepath(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=0,[3]=2,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
%%{
machine upstream;
action A1 { matches[1].rm_so = p-start; }
action A2 { matches[2].rm_so = p-start; }
action A3 { matches[3].rm_so = p-start; }
action A4 { matches[4].rm_so = p-start; }
action A5 { matches[5].rm_so = p-start; }
action A6 { matches[6].rm_so = p-start; }
action A7 { matches[7].rm_so = p-start; }
action A8 { matches[8].rm_so = p-start; }
action A9 { matches[9].rm_so = p-start; }
action A10 { matches[10].rm_so = p-start; }
action A11 { matches[11].rm_so = p-start; }
action A12 { matches[12].rm_so = p-start; }
action A13 { matches[13].rm_so = p-start; }
action A14 { matches[14].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
action E2 { matches[2].rm_eo = p-start; }
action E3 { matches[3].rm_eo = p-start; }
action E4 { matches[4].rm_eo = p-start; }
action E5 { matches[5].rm_eo = p-start; }
action E6 { matches[6].rm_eo = p-start; }
action E7 { matches[7].rm_eo = p-start; }
action E8 { matches[8].rm_eo = p-start; }
action E9 { matches[9].rm_eo = p-start; }
action E10 { matches[10].rm_eo = p-start; }
action E11 { matches[11].rm_eo = p-start; }
action E12 { matches[12].rm_eo = p-start; }
action E13 { matches[13].rm_eo = p-start; }
action E14 { matches[14].rm_eo = p-start; }
main := (( "none" ) >A2 %E2 [ \t]+'"'([^"]+) >A3 %E3 '"') >A1 %E1 |(( "http" | "socks4" | "socks5" ) >A5 %E5 [ \t]+(([^:]*) >A7 %E7 ":" ([^@]*) >A8 %E8 "@" )? >A6 %E6 (([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+) >A10 %E10 |(('-'|[A-Za-z0-9._])+) >A11 %E11 ) >A9 %E9 ":" ([0-9]+) >A12 %E12 ([ \t]+'"'([^"]+) >A14 %E14 '"')? >A13 %E13 ) >A4 %E4 ;
}%%
RE2R_EXPORT int re2r_match_upstream(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,[2]=1,[3]=1,[4]=0,[5]=4,[6]=4,[7]=6,[8]=6,[9]=4,[10]=9,[11]=9,[12]=4,[13]=4,[14]=13,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
%%{
machine loglevel;
action A1 { matches[1].rm_so = p-start; }
action E1 { matches[1].rm_eo = p-start; }
main := ([Cc] "ritical" |[Ee] "rror" |[Ww] "arning" |[Nn] "otice" |[Cc] "onnect" |[Ii] "nfo" ) >A1 %E1 ;
}%%
RE2R_EXPORT int re2r_match_loglevel(const char *p, const char* pe, size_t nmatch, regmatch_t matches[])
{
size_t i, cs;
int par;
static const unsigned char parents[] = {[0]=0,[1]=0,};
const char *start = p, *eof = pe;
%% write data nofinal noerror noentry;
for(i=0;i<nmatch;++i) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
%% write init;
%% write exec;
if(cs < %%{ write first_final; }%% ) return -1;
matches[0] = (regmatch_t){.rm_so = 0, .rm_eo = eof-start};
for(i=1;i<nmatch;++i) if(matches[i].rm_eo == -1) matches[i].rm_so = -1;
else if(matches[i].rm_so == matches[i].rm_eo) matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1};
else { par = i; while((par = parents[par])) if(matches[par].rm_eo == -1) { matches[i] = (regmatch_t){.rm_so = -1, .rm_eo = -1}; break; }}
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
/* this is a tool to print regexname regex pairs as input for re2r.
compile with gcc -I. src/conf_regex_print.c
*/
#include "config.h"
#include <stdio.h>
#define STDCONF(A, B, C) printf("%s %s\n", #A, B)
int main() {
#include "conf_regex.h"
;
}
+1 -2
View File
@@ -61,8 +61,7 @@ void filter_init (void)
fd = fopen (config->filter, "r"); fd = fopen (config->filter, "r");
if (!fd) { if (!fd) {
perror ("filter file"); return;
exit (EX_DATAERR);
} }
cflags = REG_NEWLINE | REG_NOSUB; cflags = REG_NEWLINE | REG_NOSUB;
-166
View File
@@ -1,166 +0,0 @@
#include "common.h"
#include "hostspec.h"
#include "heap.h"
#include "network.h"
/*
* Fills in the netmask array given a numeric value.
*
* Returns:
* 0 on success
* -1 on failure (invalid mask value)
*
*/
static int
fill_netmask_array (char *bitmask_string, int v6,
unsigned char array[], size_t len)
{
unsigned int i;
unsigned long int mask;
char *endptr;
errno = 0; /* to distinguish success/failure after call */
mask = strtoul (bitmask_string, &endptr, 10);
/* check for various conversion errors */
if ((errno == ERANGE && mask == ULONG_MAX)
|| (errno != 0 && mask == 0) || (endptr == bitmask_string))
return -1;
if (v6 == 0) {
/* The mask comparison is done as an IPv6 address, so
* convert to a longer mask in the case of IPv4
* addresses. */
mask += 12 * 8;
}
/* check valid range for a bit mask */
if (mask > (8 * len))
return -1;
/* we have a valid range to fill in the array */
for (i = 0; i != len; ++i) {
if (mask >= 8) {
array[i] = 0xff;
mask -= 8;
} else if (mask > 0) {
array[i] = (unsigned char) (0xff << (8 - mask));
mask = 0;
} else {
array[i] = 0;
}
}
return 0;
}
/* parse a location string containing either an ipv4/ipv4 + hostmask tuple
or a dnsname into a struct hostspec.
returns 0 on success, non-0 on error (might be memory allocation, bogus
ip address or mask).
*/
int hostspec_parse(char *location, struct hostspec *h) {
char *mask, ip_dst[IPV6_LEN];
h->type = HST_NONE;
if(!location) return 0;
memset(h, 0, sizeof(*h));
if ((mask = strrchr(location, '/')))
*(mask++) = 0;
/*
* Check for a valid IP address (the simplest case) first.
*/
if (full_inet_pton (location, ip_dst) > 0) {
h->type = HST_NUMERIC;
memcpy (h->address.ip.network, ip_dst, IPV6_LEN);
if(!mask) memset (h->address.ip.mask, 0xff, IPV6_LEN);
else {
char dst[sizeof(struct in6_addr)];
int v6, i;
/* Check if the IP address before the netmask is
* an IPv6 address */
if (inet_pton(AF_INET6, location, dst) > 0)
v6 = 1;
else
v6 = 0;
if (fill_netmask_array
(mask, v6, &(h->address.ip.mask[0]), IPV6_LEN)
< 0)
goto err;
for (i = 0; i < IPV6_LEN; i++)
h->address.ip.network[i] = ip_dst[i] &
h->address.ip.mask[i];
}
} else {
/* either bogus IP or hostname */
/* bogus ipv6 ? */
if (mask || strchr (location, ':'))
goto err;
/* In all likelihood a string */
h->type = HST_STRING;
h->address.string = safestrdup (location);
if (!h->address.string)
goto err;
}
/* restore mask */
if(mask) *(--mask) = '/';
return 0;
err:;
if(mask) *(--mask) = '/';
return -1;
}
static int string_match(const char *ip, const char *addrspec)
{
size_t test_length, match_length;
if(!strcasecmp(ip, addrspec)) return 1;
if(addrspec[0] != '.') return 0;
test_length = strlen (ip);
match_length = strlen (addrspec);
if (test_length < match_length) return 0;
return (strcasecmp
(ip + (test_length - match_length),
addrspec) == 0);
}
static int numeric_match(const uint8_t addr[], const struct hostspec *h)
{
uint8_t x, y;
int i;
for (i = 0; i != IPV6_LEN; ++i) {
x = addr[i] & h->address.ip.mask[i];
y = h->address.ip.network[i];
/* If x and y don't match, the IP addresses don't match */
if (x != y)
return 0;
}
return 1;
}
/* check whether ip matches hostspec.
return 1 on match, 0 on non-match */
int hostspec_match(const char *ip, const struct hostspec *h) {
int is_numeric_addr;
uint8_t numeric_addr[IPV6_LEN];
if (ip[0] == '\0') return 0;
is_numeric_addr = (full_inet_pton (ip, &numeric_addr) > 0);
switch (h->type) {
case HST_STRING:
if(is_numeric_addr) return 0;
return string_match (ip, h->address.string);
case HST_NUMERIC:
return numeric_match (numeric_addr, h);
case HST_NONE:
return 0;
}
return 0;
}
-26
View File
@@ -1,26 +0,0 @@
#ifndef HOSTSPEC_H
#define HOSTSPEC_H
#define IPV6_LEN 16
enum hostspec_type {
HST_NONE,
HST_STRING,
HST_NUMERIC,
};
struct hostspec {
enum hostspec_type type;
union {
char *string;
struct {
unsigned char network[IPV6_LEN];
unsigned char mask[IPV6_LEN];
} ip;
} address;
};
int hostspec_parse(char *domain, struct hostspec *h);
int hostspec_match(const char *ip, const struct hostspec *h);
#endif
+14 -32
View File
@@ -49,7 +49,6 @@ struct htab {
size_t mask; size_t mask;
size_t used; size_t used;
size_t seed; size_t seed;
size_t dead;
}; };
#define MINSIZE 8 #define MINSIZE 8
@@ -108,14 +107,14 @@ static int resize(struct htab *htab, size_t nel)
return 1; return 1;
} }
static struct elem *lookup(struct htab *htab, const char *key, size_t hash, size_t dead) static struct elem *lookup(struct htab *htab, const char *key, size_t hash)
{ {
size_t i, j; size_t i, j;
struct elem *e; struct elem *e;
for (i=hash,j=1; ; i+=j++) { for (i=hash,j=1; ; i+=j++) {
e = htab->elems + (i & htab->mask); e = htab->elems + (i & htab->mask);
if ((!e->item.key && (!e->hash || e->hash == dead)) || if (!e->item.key ||
(e->hash==hash && STRCMP(e->item.key, key)==0)) (e->hash==hash && STRCMP(e->item.key, key)==0))
break; break;
} }
@@ -139,67 +138,50 @@ void htab_destroy(struct htab *htab)
free(htab); free(htab);
} }
static struct elem *htab_find_elem(struct htab *htab, const char* key) static htab_entry *htab_find_item(struct htab *htab, const char* key)
{ {
size_t hash = keyhash(key, htab->seed); size_t hash = keyhash(key, htab->seed);
struct elem *e = lookup(htab, key, hash, 0); struct elem *e = lookup(htab, key, hash);
if (e->item.key) { if (e->item.key) {
return e; return &e->item;
} }
return 0; return 0;
} }
htab_value* htab_find(struct htab *htab, const char* key) htab_value* htab_find(struct htab *htab, const char* key)
{ {
struct elem *e = htab_find_elem(htab, key); htab_entry *i = htab_find_item(htab, key);
if(!e) return 0; if(i) return &i->data;
return &e->item.data; return 0;
}
htab_value* htab_find2(struct htab *htab, const char* key, char **saved_key)
{
struct elem *e = htab_find_elem(htab, key);
if(!e) return 0;
*saved_key = e->item.key;
return &e->item.data;
} }
int htab_delete(struct htab *htab, const char* key) int htab_delete(struct htab *htab, const char* key)
{ {
struct elem *e = htab_find_elem(htab, key); htab_entry *i = htab_find_item(htab, key);
if(!e) return 0; if(!i) return 0;
e->item.key = 0; i->key = 0;
e->hash = 0xdeadc0de;
--htab->used;
++htab->dead;
return 1; return 1;
} }
int htab_insert(struct htab *htab, char* key, htab_value value) int htab_insert(struct htab *htab, char* key, htab_value value)
{ {
size_t hash = keyhash(key, htab->seed), oh; size_t hash = keyhash(key, htab->seed);
struct elem *e = lookup(htab, key, hash, 0xdeadc0de); struct elem *e = lookup(htab, key, hash);
if(e->item.key) { if(e->item.key) {
/* it's not allowed to overwrite existing data */ /* it's not allowed to overwrite existing data */
return 0; return 0;
} }
oh = e->hash; /* save old hash in case it's tombstone marker */
e->item.key = key; e->item.key = key;
e->item.data = value; e->item.data = value;
e->hash = hash; e->hash = hash;
if (++htab->used + htab->dead > htab->mask - htab->mask/4) { if (++htab->used > htab->mask - htab->mask/4) {
if (!resize(htab, 2*htab->used)) { if (!resize(htab, 2*htab->used)) {
htab->used--; htab->used--;
e->item.key = 0; e->item.key = 0;
e->hash = oh;
return 0; return 0;
} }
htab->dead = 0;
} else if (oh == 0xdeadc0de) {
/* re-used tomb */
--htab->dead;
} }
return 1; return 1;
} }
-2
View File
@@ -14,8 +14,6 @@ typedef union htab_value {
struct htab * htab_create(size_t); struct htab * htab_create(size_t);
void htab_destroy(struct htab *); void htab_destroy(struct htab *);
htab_value* htab_find(struct htab *, const char* key); htab_value* htab_find(struct htab *, const char* key);
/* same as htab_find, but can retrieve the saved key (for freeing) */
htab_value* htab_find2(struct htab *htab, const char* key, char **saved_key);
int htab_insert(struct htab *, char*, htab_value); int htab_insert(struct htab *, char*, htab_value);
int htab_delete(struct htab *htab, const char* key); int htab_delete(struct htab *htab, const char* key);
size_t htab_next(struct htab *, size_t iterator, char** key, htab_value **v); size_t htab_next(struct htab *, size_t iterator, char** key, htab_value **v);
+19 -25
View File
@@ -20,9 +20,10 @@
* HTML error pages with variable substitution. * HTML error pages with variable substitution.
*/ */
#include "common.h" #include <regex.h>
#include "main.h" #include "main.h"
#include "common.h"
#include "buffer.h" #include "buffer.h"
#include "conns.h" #include "conns.h"
#include "heap.h" #include "heap.h"
@@ -31,8 +32,6 @@
#include "utils.h" #include "utils.h"
#include "conf.h" #include "conf.h"
#include <regex.h>
/* /*
* Add an error number -> filename mapping to the errorpages list. * Add an error number -> filename mapping to the errorpages list.
*/ */
@@ -133,21 +132,30 @@ send_html_file (FILE *infile, struct conn_s *connptr)
return 1; return 1;
} }
int send_http_headers ( int send_http_headers (struct conn_s *connptr, int code, const char *message)
struct conn_s *connptr, int code,
const char *message, const char *extra)
{ {
const char headers[] = const char headers[] =
"HTTP/1.%u %d %s\r\n" "HTTP/1.0 %d %s\r\n"
"Server: %s/%s\r\n" "Server: %s/%s\r\n"
"Content-Type: text/html\r\n" "Content-Type: text/html\r\n"
"%s" "%s"
"Connection: close\r\n" "\r\n"; "Connection: close\r\n" "\r\n";
const char p_auth_str[] =
"Proxy-Authenticate: Basic realm=\""
PACKAGE_NAME "\"\r\n";
const char w_auth_str[] =
"WWW-Authenticate: Basic realm=\""
PACKAGE_NAME "\"\r\n";
/* according to rfc7235, the 407 error must be accompanied by
a Proxy-Authenticate header field. */
const char *add = code == 407 ? p_auth_str : (code == 401 ? w_auth_str : "");
return (write_message (connptr->client_fd, headers, return (write_message (connptr->client_fd, headers,
connptr->protocol.major != 1 ? 0 : connptr->protocol.minor,
code, message, PACKAGE, VERSION, code, message, PACKAGE, VERSION,
extra)); add));
} }
/* /*
@@ -171,21 +179,8 @@ int send_http_error_message (struct conn_s *connptr)
"<p><em>Generated by %s version %s.</em></p>\n" "</body>\n" "<p><em>Generated by %s version %s.</em></p>\n" "</body>\n"
"</html>\n"; "</html>\n";
const char p_auth_str[] =
"Proxy-Authenticate: Basic realm=\""
PACKAGE_NAME "\"\r\n";
const char w_auth_str[] =
"WWW-Authenticate: Basic realm=\""
PACKAGE_NAME "\"\r\n";
/* according to rfc7235, the 407 error must be accompanied by
a Proxy-Authenticate header field. */
const char *add = connptr->error_number == 407 ? p_auth_str :
(connptr->error_number == 401 ? w_auth_str : "");
send_http_headers (connptr, connptr->error_number, send_http_headers (connptr, connptr->error_number,
connptr->error_string, add); connptr->error_string);
error_file = get_html_file (connptr->error_number); error_file = get_html_file (connptr->error_number);
if (!(infile = fopen (error_file, "r"))) { if (!(infile = fopen (error_file, "r"))) {
@@ -248,7 +243,6 @@ int add_standard_vars (struct conn_s *connptr)
char errnobuf[16]; char errnobuf[16];
char timebuf[30]; char timebuf[30];
time_t global_time; time_t global_time;
struct tm tm_buf;
snprintf (errnobuf, sizeof errnobuf, "%d", connptr->error_number); snprintf (errnobuf, sizeof errnobuf, "%d", connptr->error_number);
ADD_VAR_RET ("errno", errnobuf); ADD_VAR_RET ("errno", errnobuf);
@@ -264,7 +258,7 @@ int add_standard_vars (struct conn_s *connptr)
global_time = time (NULL); global_time = time (NULL);
strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT", strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT",
gmtime_r (&global_time, &tm_buf)); gmtime (&global_time));
add_error_variable (connptr, "date", timebuf); add_error_variable (connptr, "date", timebuf);
add_error_variable (connptr, "website", add_error_variable (connptr, "website",
+1 -1
View File
@@ -33,7 +33,7 @@ extern int add_error_variable (struct conn_s *connptr, const char *key,
const char *val); const char *val);
extern int send_html_file (FILE * infile, struct conn_s *connptr); extern int send_html_file (FILE * infile, struct conn_s *connptr);
extern int send_http_headers (struct conn_s *connptr, int code, extern int send_http_headers (struct conn_s *connptr, int code,
const char *message, const char *extra); const char *message);
extern int add_standard_vars (struct conn_s *connptr); extern int add_standard_vars (struct conn_s *connptr);
#endif /* !TINYPROXY_HTML_ERROR_H */ #endif /* !TINYPROXY_HTML_ERROR_H */
+2 -3
View File
@@ -232,7 +232,6 @@ int http_message_send (http_message_t msg, int fd)
char timebuf[30]; char timebuf[30];
time_t global_time; time_t global_time;
unsigned int i; unsigned int i;
struct tm tm_buf;
assert (is_http_message_valid (msg)); assert (is_http_message_valid (msg));
@@ -255,11 +254,11 @@ int http_message_send (http_message_t msg, int fd)
/* Output the date */ /* Output the date */
global_time = time (NULL); global_time = time (NULL);
strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT", strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT",
gmtime_r (&global_time, &tm_buf)); gmtime (&global_time));
write_message (fd, "Date: %s\r\n", timebuf); write_message (fd, "Date: %s\r\n", timebuf);
/* Output the content-length */ /* Output the content-length */
write_message (fd, "Content-length: %lu\r\n", (unsigned long) msg->body.length); write_message (fd, "Content-length: %u\r\n", msg->body.length);
/* Write the separator between the headers and body */ /* Write the separator between the headers and body */
safe_write (fd, "\r\n", 2); safe_write (fd, "\r\n", 2);
+3 -4
View File
@@ -109,7 +109,6 @@ void log_message (int level, const char *fmt, ...)
{ {
va_list args; va_list args;
struct timespec nowtime; struct timespec nowtime;
struct tm tm_buf;
char time_string[TIME_LENGTH]; char time_string[TIME_LENGTH];
char str[STRING_LENGTH]; char str[STRING_LENGTH];
@@ -178,11 +177,11 @@ void log_message (int level, const char *fmt, ...)
clock_gettime(CLOCK_REALTIME, &nowtime); clock_gettime(CLOCK_REALTIME, &nowtime);
/* Format is month day hour:minute:second (24 time) */ /* Format is month day hour:minute:second (24 time) */
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S", strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
localtime_r (&nowtime.tv_sec, &tm_buf)); localtime (&nowtime.tv_sec));
snprintf (str, STRING_LENGTH, "%-9s %s.%03lu [%ld]: ", snprintf (str, STRING_LENGTH, "%-9s %s.%03u [%ld]: ",
syslog_level[level], time_string, syslog_level[level], time_string,
(unsigned long) nowtime.tv_nsec/1000000ul, nowtime.tv_nsec/1000000u,
(long int) getpid ()); (long int) getpid ());
/* /*
+2 -7
View File
@@ -81,19 +81,14 @@ char* orderedmap_find(struct orderedmap *o, const char *key) {
int orderedmap_remove(struct orderedmap *o, const char *key) { int orderedmap_remove(struct orderedmap *o, const char *key) {
size_t i; size_t i;
char *lk; char *lk;
char *sk; htab_value *lv, *v = htab_find(o->map, key);
char **sv;
htab_value *lv, *v = htab_find2(o->map, key, &sk);
if(!v) return 0; if(!v) return 0;
sv = sblist_get(o->values, v->n); htab_delete(o->map, key);
free(*sv);
sblist_delete(o->values, v->n); sblist_delete(o->values, v->n);
i = 0; i = 0;
while((i = htab_next(o->map, i, &lk, &lv))) { while((i = htab_next(o->map, i, &lk, &lv))) {
if(lv->n > v->n) lv->n--; if(lv->n > v->n) lv->n--;
} }
htab_delete(o->map, key);
free(sk);
return 1; return 1;
} }
+11 -26
View File
@@ -268,34 +268,28 @@ establish_http_connection (struct conn_s *connptr, struct request_s *request)
/* host is an IPv6 address literal, so surround it with /* host is an IPv6 address literal, so surround it with
* [] */ * [] */
return write_message (connptr->server_fd, return write_message (connptr->server_fd,
"%s %s HTTP/1.%u\r\n" "%s %s HTTP/1.0\r\n"
"Host: [%s]%s\r\n" "Host: [%s]%s\r\n"
"Connection: close\r\n", "Connection: close\r\n",
request->method, request->path, request->method, request->path,
connptr->protocol.major != 1 ? 0 :
connptr->protocol.minor,
request->host, portbuff); request->host, portbuff);
} else if (connptr->upstream_proxy && } else if (connptr->upstream_proxy &&
connptr->upstream_proxy->type == PT_HTTP && connptr->upstream_proxy->type == PT_HTTP &&
connptr->upstream_proxy->ua.authstr) { connptr->upstream_proxy->ua.authstr) {
return write_message (connptr->server_fd, return write_message (connptr->server_fd,
"%s %s HTTP/1.%u\r\n" "%s %s HTTP/1.0\r\n"
"Host: %s%s\r\n" "Host: %s%s\r\n"
"Connection: close\r\n" "Connection: close\r\n"
"Proxy-Authorization: Basic %s\r\n", "Proxy-Authorization: Basic %s\r\n",
request->method, request->path, request->method, request->path,
connptr->protocol.major != 1 ? 0 :
connptr->protocol.minor,
request->host, portbuff, request->host, portbuff,
connptr->upstream_proxy->ua.authstr); connptr->upstream_proxy->ua.authstr);
} else { } else {
return write_message (connptr->server_fd, return write_message (connptr->server_fd,
"%s %s HTTP/1.%u\r\n" "%s %s HTTP/1.0\r\n"
"Host: %s%s\r\n" "Host: %s%s\r\n"
"Connection: close\r\n", "Connection: close\r\n",
request->method, request->path, request->method, request->path,
connptr->protocol.major != 1 ? 0 :
connptr->protocol.minor,
request->host, portbuff); request->host, portbuff);
} }
} }
@@ -389,17 +383,10 @@ BAD_REQUEST_ERROR:
* we'll be closing anyway. * we'll be closing anyway.
*/ */
char *reverse_url; char *reverse_url;
int reverse_status;
reverse_url = reverse_rewrite_url (connptr, hashofheaders, url, &reverse_status); reverse_url = reverse_rewrite_url (connptr, hashofheaders, url);
if (reverse_url != NULL) { if (reverse_url != NULL) {
if (reverse_status == 301) {
char buf[PATH_MAX];
snprintf (buf, sizeof buf, "Location: %s\r\n", reverse_url);
send_http_headers (connptr, 301, "Moved Permanently", buf);
goto fail;
}
safefree (url); safefree (url);
url = reverse_url; url = reverse_url;
} else if (config->reverseonly) { } else if (config->reverseonly) {
@@ -1244,6 +1231,7 @@ connect_to_upstream_proxy(struct conn_s *connptr, struct request_s *request)
unsigned short port; unsigned short port;
size_t ulen, passlen; size_t ulen, passlen;
struct hostent *host;
struct upstream *cur_upstream = connptr->upstream_proxy; struct upstream *cur_upstream = connptr->upstream_proxy;
ulen = cur_upstream->ua.user ? strlen(cur_upstream->ua.user) : 0; ulen = cur_upstream->ua.user ? strlen(cur_upstream->ua.user) : 0;
@@ -1260,13 +1248,10 @@ connect_to_upstream_proxy(struct conn_s *connptr, struct request_s *request)
buff[1] = 1; /* connect command */ buff[1] = 1; /* connect command */
port = htons(request->port); port = htons(request->port);
memcpy(&buff[2], &port, 2); /* dest port */ memcpy(&buff[2], &port, 2); /* dest port */
memcpy(&buff[4], "\0\0\0\1" /* socks4a fake ip */ host = gethostbyname(request->host);
"\0" /* user */, 5); memcpy(&buff[4], host->h_addr_list[0], 4); /* dest ip */
len = strlen(request->host); buff[8] = 0; /* user */
if(len>255) if (9 != safe_write(connptr->server_fd, buff, 9))
return -1;
memcpy(&buff[9], request->host, len+1);
if (9+len+1 != safe_write(connptr->server_fd, buff, 9+len+1))
return -1; return -1;
if (8 != safe_read(connptr->server_fd, buff, 8)) if (8 != safe_read(connptr->server_fd, buff, 8))
return -1; return -1;
@@ -1469,8 +1454,8 @@ get_request_entity(struct conn_s *connptr)
ret = -1; ret = -1;
} else { } else {
log_message (LOG_INFO, log_message (LOG_INFO,
"Read request entity of %ld bytes", "Read request entity of %d bytes",
(long) nread); nread);
ret = 0; ret = 0;
} }
} else { } else {
+10 -33
View File
@@ -34,7 +34,6 @@ void reversepath_add (const char *path, const char *url,
struct reversepath **reversepath_list) struct reversepath **reversepath_list)
{ {
struct reversepath *reverse; struct reversepath *reverse;
size_t l;
if (url == NULL) { if (url == NULL) {
log_message (LOG_WARNING, log_message (LOG_WARNING,
@@ -66,17 +65,8 @@ void reversepath_add (const char *path, const char *url,
if (!path) if (!path)
reverse->path = safestrdup ("/"); reverse->path = safestrdup ("/");
else { else
l = strlen (path); reverse->path = safestrdup (path);
if (l && path[l-1] == '/')
reverse->path = safestrdup (path);
else {
reverse->path = safemalloc (l + 2);
memcpy (reverse->path, path, l);
reverse->path[l] = '/';
reverse->path[l+1] = 0;
}
}
reverse->url = safestrdup (url); reverse->url = safestrdup (url);
@@ -93,16 +83,10 @@ void reversepath_add (const char *path, const char *url,
*/ */
struct reversepath *reversepath_get (char *url, struct reversepath *reverse) struct reversepath *reversepath_get (char *url, struct reversepath *reverse)
{ {
size_t l, lu, lp;
while (reverse) { while (reverse) {
lu = strlen (url); if (strstr (url, reverse->path) == url)
lp = strlen (reverse->path);
if ((
(l = lu) == lp-1 ||
(l = lp) <= lu
) &&
!memcmp(url, reverse->path, l))
return reverse; return reverse;
reverse = reverse->next; reverse = reverse->next;
} }
@@ -128,30 +112,23 @@ void free_reversepath_list (struct reversepath *reverse)
* Rewrite the URL for reverse proxying. * Rewrite the URL for reverse proxying.
*/ */
char *reverse_rewrite_url (struct conn_s *connptr, orderedmap hashofheaders, char *reverse_rewrite_url (struct conn_s *connptr, orderedmap hashofheaders,
char *url, int *status) char *url)
{ {
char *rewrite_url = NULL; char *rewrite_url = NULL;
char *cookie = NULL; char *cookie = NULL;
char *cookieval; char *cookieval;
struct reversepath *reverse = NULL; struct reversepath *reverse = NULL;
*status = 0;
/* Reverse requests always start with a slash */ /* Reverse requests always start with a slash */
if (*url == '/') { if (*url == '/') {
/* First try locating the reverse mapping by request url */ /* First try locating the reverse mapping by request url */
reverse = reversepath_get (url, config->reversepath_list); reverse = reversepath_get (url, config->reversepath_list);
if (reverse) { if (reverse) {
size_t lu = strlen (url); rewrite_url = (char *)
size_t lrp = strlen (reverse->path); safemalloc (strlen (url) + strlen (reverse->url) +
if (lrp > lu) { 1);
rewrite_url = safestrdup (reverse->path); strcpy (rewrite_url, reverse->url);
*status = 301; strcat (rewrite_url, url + strlen (reverse->path));
} else {
rewrite_url = safemalloc (
strlen (reverse->url) + lu + 1);
sprintf (rewrite_url, "%s%s", reverse->url, url + lrp);
}
} else if (config->reversemagic } else if (config->reversemagic
&& (cookie = orderedmap_find (hashofheaders, && (cookie = orderedmap_find (hashofheaders,
"cookie"))) { "cookie"))) {
+1 -2
View File
@@ -38,7 +38,6 @@ extern struct reversepath *reversepath_get (char *url,
struct reversepath *reverse); struct reversepath *reverse);
void free_reversepath_list (struct reversepath *reverse); void free_reversepath_list (struct reversepath *reverse);
extern char *reverse_rewrite_url (struct conn_s *connptr, extern char *reverse_rewrite_url (struct conn_s *connptr,
orderedmap hashofheaders, char *url, orderedmap hashofheaders, char *url);
int *status);
#endif #endif
+4 -25
View File
@@ -34,7 +34,6 @@
#include "text.h" #include "text.h"
#include "conf.h" #include "conf.h"
#include "loop.h" #include "loop.h"
#include "sblist.h"
/* /*
* Return a human readable error for getaddrinfo() and getnameinfo(). * Return a human readable error for getaddrinfo() and getnameinfo().
@@ -69,7 +68,7 @@ bind_socket (int sockfd, const char *addr, int family)
n = getaddrinfo (addr, NULL, &hints, &res); n = getaddrinfo (addr, NULL, &hints, &res);
if (n != 0) { if (n != 0) {
log_message (LOG_INFO, log_message (LOG_INFO,
"bind_socket: getaddrinfo failed for %s: %s", addr, get_gai_error (n)); "bind_socket: getaddrinfo failed for %s: ", addr, get_gai_error (n));
return -1; return -1;
} }
@@ -88,26 +87,6 @@ bind_socket (int sockfd, const char *addr, int family)
return sockfd; return sockfd;
} }
/**
* Try binding the given socket to supplied addresses, stopping when one succeeds.
*/
static int
bind_socket_list (int sockfd, sblist *addresses, int family)
{
size_t nb_addresses = sblist_getsize(addresses);
size_t i;
for (i = 0; i < nb_addresses; i++) {
const char *address = *(const char **)sblist_get(addresses, i);
if (bind_socket(sockfd, address, family) >= 0) {
log_message(LOG_INFO, "Bound to %s", address);
return 0;
}
}
return -1;
}
/* /*
* Open a connection to a remote host. It's been re-written to use * Open a connection to a remote host. It's been re-written to use
* the getaddrinfo() library function, which allows for a protocol * the getaddrinfo() library function, which allows for a protocol
@@ -155,9 +134,9 @@ int opensock (const char *host, int port, const char *bind_to)
close (sockfd); close (sockfd);
continue; /* can't bind, so try again */ continue; /* can't bind, so try again */
} }
} else if (config->bind_addrs) { } else if (config->bind_address) {
if (bind_socket_list (sockfd, config->bind_addrs, if (bind_socket (sockfd, config->bind_address,
res->ai_family) < 0) { res->ai_family) < 0) {
close (sockfd); close (sockfd);
continue; /* can't bind, so try again */ continue; /* can't bind, so try again */
} }
+1 -1
View File
@@ -122,7 +122,7 @@ err_minus_one:
add_error_variable (connptr, "deniedconns", denied); add_error_variable (connptr, "deniedconns", denied);
add_error_variable (connptr, "refusedconns", refused); add_error_variable (connptr, "refusedconns", refused);
add_standard_vars (connptr); add_standard_vars (connptr);
send_http_headers (connptr, 200, "Statistic requested", ""); send_http_headers (connptr, 200, "Statistic requested");
send_html_file (statfile, connptr); send_html_file (statfile, connptr);
fclose (statfile); fclose (statfile);
pthread_mutex_unlock(&stats_file_lock); pthread_mutex_unlock(&stats_file_lock);
+61 -21
View File
@@ -60,10 +60,11 @@ const char* upstream_build_error_string(enum upstream_build_error ube) {
/** /**
* Construct an upstream struct from input data. * Construct an upstream struct from input data.
*/ */
static struct upstream *upstream_build (const char *host, int port, char *domain, static struct upstream *upstream_build (const char *host, int port, const char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type, enum upstream_build_error *ube) proxy_type type, enum upstream_build_error *ube)
{ {
char *ptr;
struct upstream *up; struct upstream *up;
*ube = UBE_SUCCESS; *ube = UBE_SUCCESS;
@@ -74,8 +75,8 @@ static struct upstream *upstream_build (const char *host, int port, char *domain
} }
up->type = type; up->type = type;
up->target.type = HST_NONE; up->host = up->domain = up->ua.user = up->pass = NULL;
up->host = up->ua.user = up->pass = NULL; up->ip = up->mask = 0;
if (user) { if (user) {
if (type == PT_HTTP) { if (type == PT_HTTP) {
char b[BASE64ENC_BYTES((256+2)-1) + 1]; char b[BASE64ENC_BYTES((256+2)-1) + 1];
@@ -120,10 +121,30 @@ static struct upstream *upstream_build (const char *host, int port, char *domain
up->port = port; up->port = port;
} }
if (hostspec_parse(domain, &up->target) ptr = strchr (domain, '/');
|| up->target.type == HST_NONE) { if (ptr) {
*ube = UBE_NETMASK; struct in_addr addrstruct;
goto fail;
*ptr = '\0';
if (inet_aton (domain, &addrstruct) != 0) {
up->ip = ntohl (addrstruct.s_addr);
*ptr++ = '/';
if (strchr (ptr, '.')) {
if (inet_aton (ptr, &addrstruct) != 0)
up->mask =
ntohl (addrstruct.s_addr);
} else {
up->mask =
~((1 << (32 - atoi (ptr))) - 1);
}
up->ip = up->ip & up->mask;
} else {
*ube = UBE_NETMASK;
goto fail;
}
} else {
up->domain = safestrdup (domain);
} }
if (type == PT_NONE) if (type == PT_NONE)
@@ -139,8 +160,7 @@ fail:
safefree (up->ua.user); safefree (up->ua.user);
safefree (up->pass); safefree (up->pass);
safefree (up->host); safefree (up->host);
if(up->target.type == HST_STRING) safefree (up->domain);
safefree (up->target.address.string);
safefree (up); safefree (up);
return NULL; return NULL;
@@ -150,7 +170,7 @@ fail:
* Add an entry to the upstream list * Add an entry to the upstream list
*/ */
enum upstream_build_error upstream_add ( enum upstream_build_error upstream_add (
const char *host, int port, char *domain, const char *host, int port, const char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type, struct upstream **upstream_list) proxy_type type, struct upstream **upstream_list)
{ {
@@ -162,11 +182,11 @@ enum upstream_build_error upstream_add (
return ube; return ube;
} }
if (up->target.type == HST_NONE) { /* always add default to end */ if (!up->domain && !up->ip) { /* always add default to end */
struct upstream *tmp = *upstream_list; struct upstream *tmp = *upstream_list;
while (tmp) { while (tmp) {
if (tmp->target.type == HST_NONE) { if (!tmp->domain && !tmp->ip) {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Duplicate default upstream"); "Duplicate default upstream");
goto upstream_cleanup; goto upstream_cleanup;
@@ -189,8 +209,7 @@ enum upstream_build_error upstream_add (
upstream_cleanup: upstream_cleanup:
safefree (up->host); safefree (up->host);
if(up->target.type == HST_STRING) safefree (up->domain);
safefree (up->target.address.string);
safefree (up); safefree (up);
return ube; return ube;
@@ -201,12 +220,34 @@ upstream_cleanup:
*/ */
struct upstream *upstream_get (char *host, struct upstream *up) struct upstream *upstream_get (char *host, struct upstream *up)
{ {
while (up) { in_addr_t my_ip = INADDR_NONE;
if (up->target.type == HST_NONE)
break;
if (hostspec_match(host, &up->target)) while (up) {
break; if (up->domain) {
if (strcasecmp (host, up->domain) == 0)
break; /* exact match */
if (up->domain[0] == '.') {
char *dot = strchr (host, '.');
if (!dot && !up->domain[1])
break; /* local host matches "." */
while (dot && strcasecmp (dot, up->domain))
dot = strchr (dot + 1, '.');
if (dot)
break; /* subdomain match */
}
} else if (up->ip) {
if (my_ip == INADDR_NONE)
my_ip = ntohl (inet_addr (host));
if ((my_ip & up->mask) == up->ip)
break;
} else {
break; /* No domain or IP, default upstream */
}
up = up->next; up = up->next;
} }
@@ -228,8 +269,7 @@ void free_upstream_list (struct upstream *up)
while (up) { while (up) {
struct upstream *tmp = up; struct upstream *tmp = up;
up = up->next; up = up->next;
if(tmp->target.type == HST_STRING) safefree (tmp->domain);
safefree (tmp->target.address.string);
safefree (tmp->host); safefree (tmp->host);
safefree (tmp); safefree (tmp);
} }
+3 -3
View File
@@ -26,7 +26,6 @@
#define _TINYPROXY_UPSTREAM_H_ #define _TINYPROXY_UPSTREAM_H_
#include "common.h" #include "common.h"
#include "hostspec.h"
enum upstream_build_error { enum upstream_build_error {
UBE_SUCCESS = 0, UBE_SUCCESS = 0,
@@ -51,6 +50,7 @@ typedef enum proxy_type {
struct upstream { struct upstream {
struct upstream *next; struct upstream *next;
char *domain; /* optional */
char *host; char *host;
union { union {
char *user; char *user;
@@ -58,14 +58,14 @@ struct upstream {
} ua; } ua;
char *pass; char *pass;
int port; int port;
struct hostspec target; in_addr_t ip, mask;
proxy_type type; proxy_type type;
}; };
#ifdef UPSTREAM_SUPPORT #ifdef UPSTREAM_SUPPORT
const char *proxy_type_name(proxy_type type); const char *proxy_type_name(proxy_type type);
extern enum upstream_build_error upstream_add ( extern enum upstream_build_error upstream_add (
const char *host, int port, char *domain, const char *host, int port, const char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type, struct upstream **upstream_list); proxy_type type, struct upstream **upstream_list);
extern struct upstream *upstream_get (char *host, struct upstream *up); extern struct upstream *upstream_get (char *host, struct upstream *up);