Compare commits

...
Author SHA1 Message Date
rofl0r b1497be081 Update new-issue--bug-report--question.md 2021-07-21 14:26:17 +01:00
rofl0r de35241981 Update new-issue--bug-report--question.md 2021-07-21 14:23:32 +01:00
rofl0r e7e98f044f Add an issue template
i'm tired of getting the same bug report for old tinyproxy versions over and over, and people not even stating which version they're using.
2021-07-21 14:14:03 +01:00
rofl0r 563978a3ea socks4 upstream: add safety check for hostname length 2021-06-25 02:55:22 +01:00
rofl0r 7ea9f80d3f fix segfault in socks4 upstream with unresolvable hostname
using a socks4 tor upstream with an .onion url resulted in
gethostbyname() returning NULL and a subsequent segfault.
not only did the code not check the return value of gethostbyname(),
that resolver API itself isn't threadsafe.

as pure SOCKS4 supports only IPv4 addresses, and the main SOCKS4
user to this date is tor, we just use SOCKS4a unconditionally and
pass the hostname to the proxy without trying to do any local name
resolving.

i suspect in 2021 almost all SOCKS4 proxy servers in existence use
SOCKS4a extension, but should i be wrong on this, i prefer issue
reports to show up and implement plain SOCKS4 fallback only when
i see it is actually used in practice.
2021-06-25 02:43:00 +01:00
rofl0r bc81b4d9e8 put an end to LINE_MAX issues
for some reason, getting this macro is really hard across platforms,
requiring either different feature test macros or even the right order
of included headers, and its usage caused several build failures in the
past. fix it once and for all by just using 1024 as max line length if
the macro can't be retrieved.

closes #382
2021-06-24 22:55:33 +01:00
rofl0r 558e3f748d configure.ac: remove unneeded "defs" linker flag
the flag was added in 753010f571 without
explanation, and according to my research it is used to make the linker
report undefined symbols when linking a shared library.
since we don't build any shared libs, this isn't needed at all, but
reportedly causes issues with cygwin (#382).
2021-06-23 16:59:55 +01:00
Alex Wied 7168a42624 Include limits.h to fix build on OSX 2021-05-13 01:15:13 +01:00
Michael Adam 60bf8b9e4d github actions: don't run "make test" on macos it currenctly fails
Signed-off-by: Michael Adam <obnox@samba.org>
2021-05-13 01:02:43 +01:00
Michael Adam 2db9a2a00f github actions: add macos tests
Signed-off-by: Michael Adam <obnox@samba.org>
2021-05-13 01:38:10 +02:00
Michael Adam 132a55cefb Fix github actions
Signed-off-by: Michael Adam <obnox@samba.org>
2021-05-12 22:50:15 +02:00
Michael Adam e81d7767d3 Add github actions workflow for new CI
Signed-off-by: Michael Adam <obnox@samba.org>
2021-05-12 22:39:30 +02:00
rofl0r 7af5b1a553 manpage: improve FilterDefaultDeny paragraph 2021-05-10 00:25:50 +01:00
rofl0r aeb7b19c53 conf: do not warn about missing user directive unless root
there's no point in printing a warning if the program is already started
as a restricted user.
2021-05-10 00:03:43 +01:00
rofl0r 9d815f69a4 filter: hard error when filter file doesn't exist 2021-05-09 23:41:49 +01:00
rofl0r cc47fbf1f7 manpage: URL-based filtering is no longer recommended 2021-05-09 17:53:52 +01:00
rofl0r a869e71ac3 add support for outgoing connections with HTTP/1.1
since there are numerous changes in HTTP/1.1, the proxyserver will
stick to using HTTP/1.0 for internal usage, however when a connection
is requested with HTTP/1.x from now on we will duplicate the minor revision
the client requested, because apparently some servers refuse to accept
HTTP/1.0

addresses #152.
2021-04-16 14:51:01 +01:00
rofl0r 979c737f9b make upstream site-spec ipv6 compatible, refactor acl code
the acl.c code parsing a site-spec has been factored out into a
new TU: hostspec. it was superior to the parsing code in
upstream.c in that it properly deals with both ipv4 and ipv6.

both upstream and acl now use the new code for parsing, and upstream
also for checking for a match.
acl.c still uses the old matching code as it has a lot of special case
code for specifications containing a hostname, and in case such
a spec is encountered, tries to do reverse name lookup to see if
a numeric ip matches that spec.

removing that code could break existing usecases, however since
that was never implemented for upstream nobody will miss it there.
2021-04-16 14:46:02 +01:00
rofl0r 2529597ea0 reverse: redirect if path without trailing slash is detected
if for example:

ReversePath = "/foo/"

and user requests "http://tinyproxy/foo" the common behaviour for HTTP
servers is to send a http 301 redirect to the correct url.
we now do the same.
2021-04-16 14:41:40 +01:00
rofl0r bc87de3482 Release 1.11.0 2021-04-16 13:47:21 +01:00
rofl0r 11a4f6c5cf reverse: ensure paths always end with a slash 2021-03-28 20:36:55 +01:00
rofl0r 64badd6b37 htab: prevent filling up of table with tombstones
as pointed out by @craigbarnes [0], using the latest fix for
the tombstone issue, it's possible to provoke a situation
that causes an endless loop when all free slots in the table
are filled up with tombstones and htab_find() is called.

therefore we need to account for those as well when deciding
if there's a need to call resize() so there's never more than
75% of the table used by either dead or live items.
the resize() serves as a rehash which gets rid of all deleted
entries, and it might cause the table size to shrink if
htab_insert() is called after a lot of items have been removed.

[0]: https://github.com/rofl0r/htab/issues/1#issuecomment-800094442

testcase:

    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "hsearch.h"

    #define HTAB_OOM_TEST
    #include "hsearch.c"

    static char *xstrdup(const char *str)
    {
        char *dup = strdup(str);
        assert(dup);
        return dup;
    }

    void utoa(unsigned number, char* buffer) {
            int lentest, len = 0, i, start = 0;

            lentest = number;
            do {
                    len++;
                    lentest /= 10;
            } while(lentest);
            buffer[start+len] = 0;
            do {
                    i = number % 10;
                    buffer[start+len - 1] = '0' + i;
                    number -= i;
                    len -= 1;
                    number /= 10;
            } while (number);
    }

    #define TESTSIZE 8
    #define KEEP 1

    static char* notorious[TESTSIZE];

    static void prep() {
    	srand(0);
    	char buf[16];
    	size_t filled = 0;
    	while(filled < TESTSIZE) {
    		utoa(rand(), buf);
    		size_t idx = keyhash(buf) & (TESTSIZE-1);
    		if(!notorious[idx]) {
    			notorious[idx] = xstrdup(buf);
    			++filled;
    		}
    	}
    }

    int main(void)
    {
    	struct htab *h = htab_create(TESTSIZE);
    	size_t i;
    	assert(h);

    	prep();
    	for(i=0; i<TESTSIZE; ++i) {
    		char *key = notorious[i];
    		printf("[%zu] = \"%s\"\n", i, key);
    		int r = htab_insert(h, key, HTV_N(42));
    		if(!r == 1) {
    			printf("element %zu couldn't be inserted\n", i);
    			break;
    		}
    		assert(r == 1);
    		// Ensure newly inserted entry can be found
    		assert(htab_find(h, key));
    		if(i >= KEEP) htab_delete(h, key);
    	}

    	htab_find(h, "looooop");

    	return 0;
    }
2021-03-28 20:33:17 +01:00
rofl0r 48860bbe26 refactor html-error so send_http_headers() can take extra arg
we already required an extra argument inside the headers sent
for 401 and 407 error responses, move those to sent_http_error_message()
and refactor send_http_headers() to always take the extra argument.
in calling sites where the extra arg isn't needed, use "".
2021-03-28 20:24:23 +01:00
rofl0r c4231e58bf orderedmap: fix memory leak when using orderedmap_remove()
closes #351
2021-03-14 16:06:10 +00:00
rofl0r 38934921c4 htab_delete(): fix failure to set tombstone
we can't just set an item's key to zero and be done with a deletion,
because this will break the item search chain.
a deleted item requires a special marker, also known as tombstone.
when searching for an item, all slots with a tombstone need to treated
as if they were in use, but when inserting an item such a slot needs
to be filled with the new item.

a common procedure is to rehash the table when the number of deleted
items crosses a certain threshold, though for simplicity we leave this
task to the resize() function which does the same thing anyway when
the hashtable grows.

this allows to fix the issue quite elegantly and with almost no
additional overhead, so we don't penalize applications that do very
few deletions.
2021-03-14 01:57:21 +00:00
rofl0r 4147e917d6 configure: check whether gperf is compatible
closes #337
2021-02-13 09:53:55 +00:00
rofl0r adad565c03 http-message: fix UB passing long to format string expecting int 2020-10-19 20:33:04 +01:00
rofl0r db5c0e99b4 reqs: fix UB passing ssize_t to format string expecting int 2020-10-19 20:30:10 +01:00
rofl0r 8ebbd50cb2 log: replace non-mt-safe localtime() with localtime_r() 2020-10-19 20:26:12 +01:00
rofl0r 732bdd0f56 replace usage of non-threadsafe gmtime() with gmtime_r()
the latter is a standard POSIX function too.
2020-10-19 20:21:26 +01:00
Anton Khirnov 3bb14e0440 Allow multiple Bind directives.
Try all the addresses specified with Bind in order. This is necessary
e.g. for maintaining IPv4+6 connectivity while still being restricted to
one interface.
2020-10-19 20:08:31 +01:00
Anton Khirnov 2b49ef0e0f sock: add missing format specifier to log_message() 2020-10-19 20:08:31 +01:00
rofl0r f7c616d2b9 log.c: fix format string args 2020-10-19 20:08:31 +01:00
rofl0r cc0a7eb9a2 html-error: move common.h inclusion back to top
this seems to cause an implicit declaration of snprintf() thanks to
feature test macro hell.
2020-10-19 20:08:31 +01:00
rofl0r e20aa221ff conf: move inclusion of common.h back to the start
otherwise the feature-test-macros won't kick in as they should.

should fix #329
2020-10-01 15:25:35 +01:00
rofl0r 8d27503cc3 acl: fix regression using ipv6 with netmask
introduced in 0ad8904b40

closes #327
2020-09-30 19:23:34 +01:00
rofl0r 3950a606a4 conf: only treat space and tab as whitespace
other characters in the [[:space:]] set can't possibly be encountered,
and this speeds up parsing by approximately 10%.
2020-09-30 05:31:56 +01:00
rofl0r a8944b93e7 conf: use [0-9] instead of [[:digit:]] for shorter re strings 2020-09-30 05:28:00 +01:00
rofl0r 960972865c print linenumber from all conf-emitted warnings 2020-09-30 05:21:26 +01:00
rofl0r f55c46eb39 log: print timestamps with millisecond precision
this allows easier time measurements for benchmarks.
2020-09-30 05:20:09 +01:00
rofl0r 10494cab8c change loglevel of "Not running as root" message to INFO
there's no reason to display this as warning.
2020-09-30 05:19:16 +01:00
rofl0r 4f1a1663ff conf: remove bogus support for hex literals
the INT regex macro supported a 0x prefix (used e.g. for port numbers),
however following that, only digits were accepted, and not the full
range of hexdigits. it's unlikely this was used, so remove it.

note that the () expression is kept, so we don't have to adjust match
number indices all over the place.
2020-09-30 05:14:57 +01:00
rofl0r 35c8edcf73 speed up build by only including regex.h where needed 2020-09-30 05:13:45 +01:00
28 changed files with 572 additions and 331 deletions

No files matched your search

@@ -0,0 +1,20 @@
---
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
@@ -0,0 +1,36 @@
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-rc1 1.11.0
+7 -11
View File
@@ -170,17 +170,6 @@ 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
@@ -210,7 +199,14 @@ 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_CONFIG_FILES([ AC_CONFIG_FILES([
+9
View File
@@ -58,6 +58,8 @@ 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>
@@ -291,6 +293,11 @@ 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
@@ -309,6 +316,8 @@ 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>
+1
View File
@@ -24,6 +24,7 @@ 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 \
+22 -142
View File
@@ -29,16 +29,7 @@
#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
@@ -47,66 +38,9 @@ enum acl_type {
*/ */
struct acl_s { struct acl_s {
acl_access_t access; acl_access_t access;
enum acl_type type; struct hostspec h;
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.
@@ -138,77 +72,19 @@ 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;
int ret;
char *p, ip_dst[IPV6_LEN];
assert (location != NULL); assert (location != NULL);
ret = init_access_list(access_list); if (init_access_list(access_list) != 0)
if (ret != 0) {
return -1; return -1;
}
/* /*
* Start populating the access control structure. * Start populating the access control structure.
*/ */
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;
* 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);
memset (acl.address.ip.mask, 0xff, IPV6_LEN);
} else {
int i;
/* bogus ipv6 ? */
if (strchr (location, ':'))
return -1;
/*
* At this point we're either a hostname or an
* IP address with a slash.
*/
p = strchr (location, '/');
if (p != NULL) {
char dst[sizeof(struct in6_addr)];
int v6;
/*
* We have a slash, so it's intended to be an
* IP address with mask
*/
*p = '\0';
if (full_inet_pton (location, ip_dst) <= 0)
return -1;
acl.type = ACL_NUMERIC;
/* 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
(p + 1, v6, &(acl.address.ip.mask[0]), IPV6_LEN)
< 0)
return -1;
for (i = 0; i < IPV6_LEN; i++)
acl.address.ip.network[i] = ip_dst[i] &
acl.address.ip.mask[i];
} else {
/* In all likelihood a string */
acl.type = ACL_STRING;
acl.address.string = safestrdup (location);
if (!acl.address.string)
return -1;
}
}
if(!sblist_add(*access_list, &acl)) return -1; if(!sblist_add(*access_list, &acl)) return -1;
return 0; return 0;
@@ -232,7 +108,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->type == ACL_STRING); assert (acl && acl->h.type == HST_STRING);
assert (ip_address && strlen (ip_address) > 0); assert (ip_address && strlen (ip_address) > 0);
/* /*
@@ -240,11 +116,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->address.string[0] != '.') { if (acl->h.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->address.string, NULL, &hints, &res) != 0) if (getaddrinfo (acl->h.address.string, NULL, &hints, &res) != 0)
goto STRING_TEST; goto STRING_TEST;
ressave = res; ressave = res;
@@ -278,7 +154,7 @@ STRING_TEST:
} }
test_length = strlen (string_addr); test_length = strlen (string_addr);
match_length = strlen (acl->address.string); match_length = strlen (acl->h.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
@@ -289,7 +165,7 @@ STRING_TEST:
if (strcasecmp if (strcasecmp
(string_addr + (test_length - match_length), (string_addr + (test_length - match_length),
acl->address.string) == 0) { acl->h.address.string) == 0) {
if (acl->access == ACL_DENY) if (acl->access == ACL_DENY)
return 0; return 0;
else else
@@ -313,11 +189,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->type == ACL_NUMERIC); assert (acl && acl->h.type == HST_NUMERIC);
for (i = 0; i != IPV6_LEN; ++i) { for (i = 0; i != IPV6_LEN; ++i) {
x = addr[i] & acl->address.ip.mask[i]; x = addr[i] & acl->h.address.ip.mask[i];
y = acl->address.ip.network[i]; y = acl->h.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)
@@ -358,12 +234,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->type) { switch (acl->h.type) {
case ACL_STRING: case HST_STRING:
perm = acl_string_processing (acl, ip, addr, string_addr); perm = acl_string_processing (acl, ip, addr, string_addr);
break; break;
case ACL_NUMERIC: case HST_NUMERIC:
if (ip[0] == '\0') if (ip[0] == '\0')
continue; continue;
@@ -371,6 +247,10 @@ 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;
} }
/* /*
@@ -407,8 +287,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->type == ACL_STRING) { if (acl->h.type == HST_STRING) {
safefree (acl->address.string); safefree (acl->h.address.string);
} }
} }
+1 -1
View File
@@ -68,7 +68,7 @@
# include <arpa/inet.h> # include <arpa/inet.h>
# include <grp.h> # include <grp.h>
# include <pwd.h> # include <pwd.h>
# include <regex.h> # include <limits.h>
/* rest - some oddball headers */ /* rest - some oddball headers */
#ifdef HAVE_VALUES_H #ifdef HAVE_VALUES_H
+52 -25
View File
@@ -24,6 +24,7 @@
*/ */
#include "common.h" #include "common.h"
#include <regex.h>
#include "conf.h" #include "conf.h"
#include "acl.h" #include "acl.h"
@@ -39,6 +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 * The configuration directives are defined in the structure below. Each
* directive requires a regular expression to match against, and a * directive requires a regular expression to match against, and a
@@ -48,23 +55,25 @@
* can (and likely should) be used when building the regex for the * can (and likely should) be used when building the regex for the
* given directive. * given directive.
*/ */
#define WS "[[:space:]]+" #define DIGIT "[0-9]"
#define SPACE "[ \t]"
#define WS SPACE "+"
#define STR "\"([^\"]+)\"" #define STR "\"([^\"]+)\""
#define BOOL "(yes|on|no|off)" #define BOOL "(yes|on|no|off)"
#define INT "((0x)?[[:digit:]]+)" #define INT "(()" DIGIT "+)"
#define ALNUM "([-a-z0-9._]+)" #define ALNUM "([-a-z0-9._]+)"
#define USERNAME "([^:]*)" #define USERNAME "([^:]*)"
#define PASSWORD "([^@]*)" #define PASSWORD "([^@]*)"
#define IP "((([0-9]{1,3})\\.){3}[0-9]{1,3})" #define IP "((([0-9]{1,3})\\.){3}[0-9]{1,3})"
#define IPMASK "(" IP "(/[[:digit:]]+)?)" #define IPMASK "(" IP "(/" DIGIT "+)?)"
#define IPV6 "(" \ #define IPV6 "(" \
"(([0-9a-f:]{2,39}))|" \ "(([0-9a-f:]{2,39}))|" \
"(([0-9a-f:]{0,29}:" IP "))" \ "(([0-9a-f:]{0,29}:" IP "))" \
")" ")"
#define IPV6MASK "(" IPV6 "(/[[:digit:]]+)?)" #define IPV6MASK "(" IPV6 "(/" DIGIT "+)?)"
#define BEGIN "^[[:space:]]*" #define BEGIN "^" SPACE "*"
#define END "[[: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
@@ -73,6 +82,9 @@
*/ */
#define RE_MAX_MATCHES 24 #define RE_MAX_MATCHES 24
#define CP_WARN(FMT, ...) \
log_message (LOG_WARNING, "line %lu: " FMT, lineno, __VA_ARGS__)
/* /*
* All configuration handling functions are REQUIRED to be defined * All configuration handling functions are REQUIRED to be defined
* with the same function template as below. * with the same function template as below.
@@ -285,6 +297,7 @@ 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 */
@@ -296,7 +309,6 @@ 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;
@@ -403,7 +415,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[LINE_MAX], *p, *q, c; char buffer[TP_LINE_MAX], *p, *q, c;
const struct config_directive_entry *e; const struct config_directive_entry *e;
unsigned long lineno = 1; unsigned long lineno = 1;
@@ -499,7 +511,7 @@ int reload_config_file (const char *config_fname, struct config_s *conf)
goto done; goto done;
} }
if (!conf->user) { if (!conf->user && !geteuid()) {
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.");
} }
@@ -637,9 +649,7 @@ static HANDLE_FUNC (handle_anonymous)
return -1; return -1;
if(anonymous_insert (conf, arg) < 0) { if(anonymous_insert (conf, arg) < 0) {
log_message (LOG_WARNING, CP_WARN ("anonymous_insert() failed: '%s'", arg);
"anonymous_insert() failed: '%s'",
arg);
safefree(arg); safefree(arg);
return -1; return -1;
} }
@@ -767,7 +777,7 @@ static HANDLE_FUNC (handle_group)
} }
static void warn_invalid_address(char *arg, unsigned long lineno) { static void warn_invalid_address(char *arg, unsigned long lineno) {
log_message (LOG_WARNING, "Invalid address %s on line %lu", arg, lineno); CP_WARN ("Invalid address %s", arg);
} }
static HANDLE_FUNC (handle_allow) static HANDLE_FUNC (handle_allow)
@@ -792,12 +802,27 @@ static HANDLE_FUNC (handle_deny)
static HANDLE_FUNC (handle_bind) static HANDLE_FUNC (handle_bind)
{ {
int r = set_string_arg (&conf->bind_address, line, &match[2]); char *arg = get_string_arg (line, &match[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,
"Outgoing connections bound to IP %s", conf->bind_address); "Added bind address [%s] for outgoing connections.", arg);
return 0; return 0;
} }
@@ -812,8 +837,8 @@ static HANDLE_FUNC (handle_listen)
if (conf->listen_addrs == NULL) { if (conf->listen_addrs == NULL) {
conf->listen_addrs = sblist_new(sizeof(char*), 16); conf->listen_addrs = sblist_new(sizeof(char*), 16);
if (conf->listen_addrs == NULL) { if (conf->listen_addrs == NULL) {
log_message(LOG_WARNING, "Could not create a list " CP_WARN ("Could not create a list "
"of listen addresses."); "of listen addresses.", "");
safefree(arg); safefree(arg);
return -1; return -1;
} }
@@ -839,9 +864,7 @@ static HANDLE_FUNC (handle_errorfile)
char *page = get_string_arg (line, &match[4]); char *page = get_string_arg (line, &match[4]);
if(add_new_errorpage (conf, page, err) < 0) { if(add_new_errorpage (conf, page, err) < 0) {
log_message (LOG_WARNING, CP_WARN ("add_new_errorpage() failed: '%s'", page);
"add_new_errorpage() failed: '%s'",
page);
safefree (page); safefree (page);
} }
return 0; return 0;
@@ -1024,6 +1047,7 @@ static HANDLE_FUNC (handle_upstream)
int port, mi; int port, mi;
char *domain = 0, *user = 0, *pass = 0, *tmp; char *domain = 0, *user = 0, *pass = 0, *tmp;
enum proxy_type pt; enum proxy_type pt;
enum upstream_build_error ube;
if (match[3].rm_so != -1) { if (match[3].rm_so != -1) {
tmp = get_string_arg (line, &match[3]); tmp = get_string_arg (line, &match[3]);
@@ -1033,9 +1057,9 @@ static HANDLE_FUNC (handle_upstream)
domain = get_string_arg (line, &match[4]); domain = get_string_arg (line, &match[4]);
if (!domain) if (!domain)
return -1; return -1;
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);
safefree (domain); safefree (domain);
return 0; goto check_err;
} }
} }
@@ -1065,13 +1089,16 @@ static HANDLE_FUNC (handle_upstream)
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]);
upstream_add (ip, port, domain, user, pass, pt, &conf->upstream_list); ube = upstream_add (ip, port, domain, user, pass, pt, &conf->upstream_list);
safefree (user); safefree (user);
safefree (pass); safefree (pass);
safefree (domain); safefree (domain);
safefree (ip); safefree (ip);
check_err:;
if(ube != UBE_SUCCESS)
CP_WARN("%s", upstream_build_error_string(ube));
return 0; return 0;
} }
+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;
char *bind_address; sblist *bind_addrs;
unsigned int bindsame; unsigned int bindsame;
/* /*
+3 -1
View File
@@ -24,6 +24,7 @@
#include "main.h" #include "main.h"
#include <regex.h>
#include "filter.h" #include "filter.h"
#include "heap.h" #include "heap.h"
#include "log.h" #include "log.h"
@@ -60,7 +61,8 @@ void filter_init (void)
fd = fopen (config->filter, "r"); fd = fopen (config->filter, "r");
if (!fd) { if (!fd) {
return; perror ("filter file");
exit (EX_DATAERR);
} }
cflags = REG_NEWLINE | REG_NOSUB; cflags = REG_NEWLINE | REG_NOSUB;
+166
View File
@@ -0,0 +1,166 @@
#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
@@ -0,0 +1,26 @@
#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
+32 -14
View File
@@ -49,6 +49,7 @@ 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
@@ -107,14 +108,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) static struct elem *lookup(struct htab *htab, const char *key, size_t hash, size_t dead)
{ {
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 || if ((!e->item.key && (!e->hash || e->hash == dead)) ||
(e->hash==hash && STRCMP(e->item.key, key)==0)) (e->hash==hash && STRCMP(e->item.key, key)==0))
break; break;
} }
@@ -138,50 +139,67 @@ void htab_destroy(struct htab *htab)
free(htab); free(htab);
} }
static htab_entry *htab_find_item(struct htab *htab, const char* key) static struct elem *htab_find_elem(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); struct elem *e = lookup(htab, key, hash, 0);
if (e->item.key) { if (e->item.key) {
return &e->item; return e;
} }
return 0; return 0;
} }
htab_value* htab_find(struct htab *htab, const char* key) htab_value* htab_find(struct htab *htab, const char* key)
{ {
htab_entry *i = htab_find_item(htab, key); struct elem *e = htab_find_elem(htab, key);
if(i) return &i->data; if(!e) return 0;
return 0; return &e->item.data;
}
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)
{ {
htab_entry *i = htab_find_item(htab, key); struct elem *e = htab_find_elem(htab, key);
if(!i) return 0; if(!e) return 0;
i->key = 0; e->item.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); size_t hash = keyhash(key, htab->seed), oh;
struct elem *e = lookup(htab, key, hash); struct elem *e = lookup(htab, key, hash, 0xdeadc0de);
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->mask - htab->mask/4) { if (++htab->used + htab->dead > 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,6 +14,8 @@ 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);
+25 -18
View File
@@ -20,9 +20,9 @@
* HTML error pages with variable substitution. * HTML error pages with variable substitution.
*/ */
#include "common.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,6 +31,8 @@
#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.
*/ */
@@ -131,30 +133,21 @@ send_html_file (FILE *infile, struct conn_s *connptr)
return 1; return 1;
} }
int send_http_headers (struct conn_s *connptr, int code, const char *message) int send_http_headers (
struct conn_s *connptr, int code,
const char *message, const char *extra)
{ {
const char headers[] = const char headers[] =
"HTTP/1.0 %d %s\r\n" "HTTP/1.%u %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,
add)); extra));
} }
/* /*
@@ -178,8 +171,21 @@ 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); connptr->error_string, add);
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"))) {
@@ -242,6 +248,7 @@ 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);
@@ -257,7 +264,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 (&global_time)); gmtime_r (&global_time, &tm_buf));
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 *message, const char *extra);
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 */
+3 -2
View File
@@ -232,6 +232,7 @@ 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));
@@ -254,11 +255,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 (&global_time)); gmtime_r (&global_time, &tm_buf));
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: %u\r\n", msg->body.length); write_message (fd, "Content-length: %lu\r\n", (unsigned long) 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);
+6 -4
View File
@@ -108,7 +108,8 @@ void set_log_level (int level)
void log_message (int level, const char *fmt, ...) void log_message (int level, const char *fmt, ...)
{ {
va_list args; va_list args;
time_t 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];
@@ -174,13 +175,14 @@ void log_message (int level, const char *fmt, ...)
} else { } else {
char *p; char *p;
nowtime = time (NULL); 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 (&nowtime)); localtime_r (&nowtime.tv_sec, &tm_buf));
snprintf (str, STRING_LENGTH, "%-9s %s [%ld]: ", snprintf (str, STRING_LENGTH, "%-9s %s.%03lu [%ld]: ",
syslog_level[level], time_string, syslog_level[level], time_string,
(unsigned long) nowtime.tv_nsec/1000000ul,
(long int) getpid ()); (long int) getpid ());
/* /*
+1 -1
View File
@@ -381,7 +381,7 @@ main (int argc, char **argv)
if (geteuid () == 0) if (geteuid () == 0)
change_user (argv[0]); change_user (argv[0]);
else else
log_message (LOG_WARNING, log_message (LOG_INFO,
"Not running as root, so not changing UID/GID."); "Not running as root, so not changing UID/GID.");
/* Create log file after we drop privileges */ /* Create log file after we drop privileges */
+7 -2
View File
@@ -81,14 +81,19 @@ 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;
htab_value *lv, *v = htab_find(o->map, key); char *sk;
char **sv;
htab_value *lv, *v = htab_find2(o->map, key, &sk);
if(!v) return 0; if(!v) return 0;
htab_delete(o->map, key); sv = sblist_get(o->values, v->n);
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;
} }
+26 -11
View File
@@ -268,28 +268,34 @@ 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.0\r\n" "%s %s HTTP/1.%u\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.0\r\n" "%s %s HTTP/1.%u\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.0\r\n" "%s %s HTTP/1.%u\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);
} }
} }
@@ -383,10 +389,17 @@ 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_url = reverse_rewrite_url (connptr, hashofheaders, url, &reverse_status);
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) {
@@ -1231,7 +1244,6 @@ 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;
@@ -1248,10 +1260,13 @@ 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 */
host = gethostbyname(request->host); memcpy(&buff[4], "\0\0\0\1" /* socks4a fake ip */
memcpy(&buff[4], host->h_addr_list[0], 4); /* dest ip */ "\0" /* user */, 5);
buff[8] = 0; /* user */ len = strlen(request->host);
if (9 != safe_write(connptr->server_fd, buff, 9)) if(len>255)
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;
@@ -1454,8 +1469,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 %d bytes", "Read request entity of %ld bytes",
nread); (long) nread);
ret = 0; ret = 0;
} }
} else { } else {
+33 -10
View File
@@ -34,6 +34,7 @@ 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,
@@ -65,8 +66,17 @@ void reversepath_add (const char *path, const char *url,
if (!path) if (!path)
reverse->path = safestrdup ("/"); reverse->path = safestrdup ("/");
else else {
reverse->path = safestrdup (path); l = strlen (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);
@@ -83,10 +93,16 @@ 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) {
if (strstr (url, reverse->path) == url) lu = strlen (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;
} }
@@ -112,23 +128,30 @@ 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) char *url, int *status)
{ {
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) {
rewrite_url = (char *) size_t lu = strlen (url);
safemalloc (strlen (url) + strlen (reverse->url) + size_t lrp = strlen (reverse->path);
1); if (lrp > lu) {
strcpy (rewrite_url, reverse->url); rewrite_url = safestrdup (reverse->path);
strcat (rewrite_url, url + strlen (reverse->path)); *status = 301;
} 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"))) {
+2 -1
View File
@@ -38,6 +38,7 @@ 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
+25 -4
View File
@@ -34,6 +34,7 @@
#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().
@@ -68,7 +69,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: ", addr, get_gai_error (n)); "bind_socket: getaddrinfo failed for %s: %s", addr, get_gai_error (n));
return -1; return -1;
} }
@@ -87,6 +88,26 @@ 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
@@ -134,9 +155,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_address) { } else if (config->bind_addrs) {
if (bind_socket (sockfd, config->bind_address, if (bind_socket_list (sockfd, config->bind_addrs,
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);
+48 -77
View File
@@ -43,34 +43,46 @@ proxy_type_name(proxy_type type)
} }
} }
const char* upstream_build_error_string(enum upstream_build_error ube) {
static const char *emap[] = {
[UBE_SUCCESS] = "",
[UBE_OOM] = "Unable to allocate memory in upstream_build()",
[UBE_USERLEN] = "User / pass in upstream config too long",
[UBE_EDOMAIN] = "Nonsense upstream none rule: empty domain",
[UBE_INVHOST] = "Nonsense upstream rule: invalid host or port",
[UBE_INVPARAMS] = "Nonsense upstream rule: invalid parameters",
[UBE_NETMASK] = "Nonsense upstream rule: failed to parse netmask",
};
return emap[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, const char *domain, static struct upstream *upstream_build (const char *host, int port, char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type) proxy_type type, enum upstream_build_error *ube)
{ {
char *ptr;
struct upstream *up; struct upstream *up;
*ube = UBE_SUCCESS;
up = (struct upstream *) safemalloc (sizeof (struct upstream)); up = (struct upstream *) safemalloc (sizeof (struct upstream));
if (!up) { if (!up) {
log_message (LOG_ERR, *ube = UBE_OOM;
"Unable to allocate memory in upstream_build()");
return NULL; return NULL;
} }
up->type = type; up->type = type;
up->host = up->domain = up->ua.user = up->pass = NULL; up->target.type = HST_NONE;
up->ip = up->mask = 0; up->host = up->ua.user = up->pass = NULL;
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];
ssize_t ret; ssize_t ret;
ret = basicauth_string(user, pass, b, sizeof b); ret = basicauth_string(user, pass, b, sizeof b);
if (ret == 0) { if (ret == 0) {
log_message (LOG_ERR, *ube = UBE_USERLEN;
"User / pass in upstream config too long");
return NULL; return NULL;
} }
up->ua.authstr = safestrdup (b); up->ua.authstr = safestrdup (b);
@@ -83,13 +95,11 @@ static struct upstream *upstream_build (const char *host, int port, const char *
if (domain == NULL) { if (domain == NULL) {
if (type == PT_NONE) { if (type == PT_NONE) {
e_nonedomain:; e_nonedomain:;
log_message (LOG_WARNING, *ube = UBE_EDOMAIN;
"Nonsense upstream none rule: empty domain");
goto fail; goto fail;
} }
if (!host || !host[0] || port < 1) { if (!host || !host[0] || port < 1) {
log_message (LOG_WARNING, *ube = UBE_INVHOST;
"Nonsense upstream rule: invalid host or port");
goto fail; goto fail;
} }
@@ -103,39 +113,17 @@ static struct upstream *upstream_build (const char *host, int port, const char *
if (!domain[0]) goto e_nonedomain; if (!domain[0]) goto e_nonedomain;
} else { } else {
if (!host || !host[0] || !domain[0]) { if (!host || !host[0] || !domain[0]) {
log_message (LOG_WARNING, *ube = UBE_INVPARAMS;
"Nonsense upstream rule: invalid parameters");
goto fail; goto fail;
} }
up->host = safestrdup (host); up->host = safestrdup (host);
up->port = port; up->port = port;
} }
ptr = strchr (domain, '/'); if (hostspec_parse(domain, &up->target)
if (ptr) { || up->target.type == HST_NONE) {
struct in_addr addrstruct; *ube = UBE_NETMASK;
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 {
log_message (LOG_WARNING,
"Nonsense upstream rule: failed to parse netmask");
goto fail;
}
} else {
up->domain = safestrdup (domain);
} }
if (type == PT_NONE) if (type == PT_NONE)
@@ -151,7 +139,8 @@ fail:
safefree (up->ua.user); safefree (up->ua.user);
safefree (up->pass); safefree (up->pass);
safefree (up->host); safefree (up->host);
safefree (up->domain); if(up->target.type == HST_STRING)
safefree (up->target.address.string);
safefree (up); safefree (up);
return NULL; return NULL;
@@ -160,22 +149,24 @@ fail:
/* /*
* Add an entry to the upstream list * Add an entry to the upstream list
*/ */
void upstream_add (const char *host, int port, const char *domain, enum upstream_build_error upstream_add (
const char *host, int port, 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)
{ {
struct upstream *up; struct upstream *up;
enum upstream_build_error ube;
up = upstream_build (host, port, domain, user, pass, type); up = upstream_build (host, port, domain, user, pass, type, &ube);
if (up == NULL) { if (up == NULL) {
return; return ube;
} }
if (!up->domain && !up->ip) { /* always add default to end */ if (up->target.type == HST_NONE) { /* always add default to end */
struct upstream *tmp = *upstream_list; struct upstream *tmp = *upstream_list;
while (tmp) { while (tmp) {
if (!tmp->domain && !tmp->ip) { if (tmp->target.type == HST_NONE) {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Duplicate default upstream"); "Duplicate default upstream");
goto upstream_cleanup; goto upstream_cleanup;
@@ -184,7 +175,7 @@ void upstream_add (const char *host, int port, const char *domain,
if (!tmp->next) { if (!tmp->next) {
up->next = NULL; up->next = NULL;
tmp->next = up; tmp->next = up;
return; return ube;
} }
tmp = tmp->next; tmp = tmp->next;
@@ -194,14 +185,15 @@ void upstream_add (const char *host, int port, const char *domain,
up->next = *upstream_list; up->next = *upstream_list;
*upstream_list = up; *upstream_list = up;
return; return ube;
upstream_cleanup: upstream_cleanup:
safefree (up->host); safefree (up->host);
safefree (up->domain); if(up->target.type == HST_STRING)
safefree (up->target.address.string);
safefree (up); safefree (up);
return; return ube;
} }
/* /*
@@ -209,34 +201,12 @@ upstream_cleanup:
*/ */
struct upstream *upstream_get (char *host, struct upstream *up) struct upstream *upstream_get (char *host, struct upstream *up)
{ {
in_addr_t my_ip = INADDR_NONE;
while (up) { while (up) {
if (up->domain) { if (up->target.type == HST_NONE)
if (strcasecmp (host, up->domain) == 0) break;
break; /* exact match */
if (up->domain[0] == '.') { if (hostspec_match(host, &up->target))
char *dot = strchr (host, '.'); break;
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;
} }
@@ -258,7 +228,8 @@ 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;
safefree (tmp->domain); if(tmp->target.type == HST_STRING)
safefree (tmp->target.address.string);
safefree (tmp->host); safefree (tmp->host);
safefree (tmp); safefree (tmp);
} }
+15 -3
View File
@@ -26,6 +26,17 @@
#define _TINYPROXY_UPSTREAM_H_ #define _TINYPROXY_UPSTREAM_H_
#include "common.h" #include "common.h"
#include "hostspec.h"
enum upstream_build_error {
UBE_SUCCESS = 0,
UBE_OOM,
UBE_USERLEN,
UBE_EDOMAIN,
UBE_INVHOST,
UBE_INVPARAMS,
UBE_NETMASK,
};
/* /*
* Even if upstream support is not compiled into tinyproxy, this * Even if upstream support is not compiled into tinyproxy, this
@@ -40,7 +51,6 @@ 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;
@@ -48,17 +58,19 @@ struct upstream {
} ua; } ua;
char *pass; char *pass;
int port; int port;
in_addr_t ip, mask; struct hostspec target;
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 void upstream_add (const char *host, int port, const char *domain, extern enum upstream_build_error upstream_add (
const char *host, int port, 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);
extern void free_upstream_list (struct upstream *up); extern void free_upstream_list (struct upstream *up);
extern const char* upstream_build_error_string(enum upstream_build_error);
#endif /* UPSTREAM_SUPPORT */ #endif /* UPSTREAM_SUPPORT */
#endif /* _TINYPROXY_UPSTREAM_H_ */ #endif /* _TINYPROXY_UPSTREAM_H_ */