diff options
Diffstat (limited to '')
-rw-r--r-- | configure.ac | 3 | ||||
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/conffile.c | 3 | ||||
-rw-r--r-- | src/reqs.c | 155 | ||||
-rw-r--r-- | src/reqs.h | 3 | ||||
-rw-r--r-- | src/reverse_proxy.c | 154 | ||||
-rw-r--r-- | src/reverse_proxy.h | 36 | ||||
-rw-r--r-- | src/tinyproxy.h | 12 |
8 files changed, 214 insertions, 157 deletions
diff --git a/configure.ac b/configure.ac index 83d727d..2acbeb0 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -dnl $Id: configure.ac,v 2.69 2004-08-24 18:43:17 rjkaes Exp $ +dnl $Id: configure.ac,v 2.70 2005-08-16 04:03:18 rjkaes Exp $ dnl Devlopers, please strive to achieve this order: dnl @@ -130,6 +130,7 @@ TP_ARG_ENABLE(reverse, [Enable reverse proxying (default is NO)] no) if test x"$reverse_enabled" = x"yes"; then + ADDITIONAL_OBJECTS="$ADDITIONAL_OBJECTS reverse_proxy.o" AC_DEFINE(REVERSE_SUPPORT) fi diff --git a/src/Makefile.am b/src/Makefile.am index becee65..3904cd3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -# $Id: Makefile.am,v 1.22 2005-07-12 17:39:40 rjkaes Exp $ +# $Id: Makefile.am,v 1.23 2005-08-16 04:03:19 rjkaes Exp $ # # Copyright (C) 2000 Robert James Kaes (rjkaes@users.sourceforge.net) # @@ -40,7 +40,8 @@ tinyproxy_SOURCES = \ utils.c utils.h \ vector.c vector.h -EXTRA_tinyproxy_SOURCES = filter.c filter.h grammar.h +EXTRA_tinyproxy_SOURCES = filter.c filter.h \ + reverse_proxy.c reverse_proxy.h tinyproxy_DEPENDENCIES = @ADDITIONAL_OBJECTS@ tinyproxy_LDADD = @ADDITIONAL_OBJECTS@ diff --git a/src/conffile.c b/src/conffile.c index 453942f..1730998 100644 --- a/src/conffile.c +++ b/src/conffile.c @@ -1,4 +1,4 @@ -/* $Id: conffile.c,v 1.5 2005-08-15 03:54:31 rjkaes Exp $ +/* $Id: conffile.c,v 1.6 2005-08-16 04:03:19 rjkaes Exp $ * * Parses the configuration file and sets up the config_s structure for * use by the application. This file replaces the old grammar.y and @@ -30,6 +30,7 @@ #include "htmlerror.h" #include "log.h" #include "reqs.h" +#include "reverse_proxy.h" /* * The configuration directives are defined in the structure below. Each @@ -1,4 +1,4 @@ -/* $Id: reqs.c,v 1.120 2005-08-15 03:54:31 rjkaes Exp $ +/* $Id: reqs.c,v 1.121 2005-08-16 04:03:19 rjkaes Exp $ * * This is where all the work in tinyproxy is actually done. Incoming * connections have a new child created for them. The child then @@ -6,7 +6,7 @@ * and then relays the bytes between the two. * * Copyright (C) 1998 Steven Young - * Copyright (C) 1999-2004 Robert James Kaes (rjkaes@users.sourceforge.net) + * Copyright (C) 1999-2005 Robert James Kaes (rjkaes@users.sourceforge.net) * Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com) * Copyright (C) 2002 Petr Lampa (lampa@fit.vutbr.cz) * @@ -39,6 +39,7 @@ #include "text.h" #include "utils.h" #include "vector.h" +#include "reverse_proxy.h" /* * Maximum length of a HTTP line @@ -497,75 +498,6 @@ upstream_get(char *host) } #endif -#ifdef REVERSE_SUPPORT -/* - * Add entry to the reversepath list - */ -void -reversepath_add(const char *path, const char *url) -{ - struct reversepath *reverse; - - if (url == NULL) { - log_message(LOG_WARNING, - "Illegal reverse proxy rule: missing url"); - return; - } - - if (!strstr(url, "://")) { - log_message(LOG_WARNING, - "Skipping reverse proxy rule: '%s' is not a valid url", - url); - return; - } - - if (path && *path != '/') { - log_message(LOG_WARNING, - "Skipping reverse proxy rule: path '%s' doesn't start with a /", - path); - return; - } - - if (!(reverse = safemalloc(sizeof(struct reversepath)))) { - log_message(LOG_ERR, - "Unable to allocate memory in reversepath_add()"); - return; - } - - if (!path) - reverse->path = safestrdup("/"); - else - reverse->path = safestrdup(path); - - reverse->url = safestrdup(url); - - reverse->next = config.reversepath_list; - config.reversepath_list = reverse; - - log_message(LOG_INFO, - "Added reverse proxy rule: %s -> %s", reverse->path, - reverse->url); -} - -/* - * Check if a request url is in the reversepath list - */ -static struct reversepath * -reversepath_get(char *url) -{ - struct reversepath *reverse = config.reversepath_list; - - while (reverse) { - if (strstr(url, reverse->path) == url) - return reverse; - - reverse = reverse->next; - } - - return NULL; -} -#endif - /* * Create a connection for HTTP connections. */ @@ -616,16 +548,7 @@ process_request(struct conn_s *connptr, hashmap_t hashofheaders) { char *url; struct request_s *request; - -#ifdef REVERSE_SUPPORT - char *rewrite_url = NULL; - char *cookie = NULL; - char *cookieval; - struct reversepath *reverse; -#endif - int ret; - size_t request_len; /* NULL out all the fields so frees don't cause segfaults. */ @@ -698,72 +621,24 @@ process_request(struct conn_s *connptr, hashmap_t hashofheaders) return NULL; } #ifdef REVERSE_SUPPORT - /* - * Reverse proxy URL rewriting. - */ if (config.reversepath_list != NULL) { - /* Reverse requests always start with a slash */ - if (*url == '/') { - /* First try locating the reverse mapping by request url */ - reverse = reversepath_get(url); - if (reverse) { - rewrite_url = safemalloc(strlen(url) + - strlen(reverse->url) + - 1); - strcpy(rewrite_url, reverse->url); - strcat(rewrite_url, - url + strlen(reverse->path)); - } else if (config.reversemagic - && hashmap_entry_by_key(hashofheaders, - "cookie", - (void **)&cookie) > - 0) { - - /* No match - try the magical tracking cookie next */ - if ((cookieval = - strstr(cookie, REVERSE_COOKIE "=")) - && (reverse = - reversepath_get(cookieval + - strlen(REVERSE_COOKIE) + - 1))) { - - rewrite_url = safemalloc(strlen(url) + - strlen - (reverse-> - url) + 1); - strcpy(rewrite_url, reverse->url); - strcat(rewrite_url, url + 1); - - log_message(LOG_INFO, - "Magical tracking cookie says: %s", - reverse->path); - } - } - } + /* + * Rewrite the URL based on the reverse path. After calling + * reverse_rewrite_url "url" can be freed since we either + * have the newly rewritten URL, or something failed and + * we'll be closing anyway. + */ + char *reverse_url; - /* Forward proxy support off and no reverse path match found */ - if (config.reverseonly && !rewrite_url) { - log_message(LOG_ERR, "Bad request"); - indicate_http_error(connptr, 400, "Bad Request", - "detail", - "Request has an invalid URL", "url", - url, NULL); + reverse_url = reverse_rewrite_url(connptr, hashofheaders, url); + safefree(url); - safefree(url); + if (!reverse_url) { free_request_struct(request); - return NULL; + } else { + url = reverse_url; } - - log_message(LOG_CONN, "Rewriting URL: %s -> %s", - url, rewrite_url); - - safefree(url); - url = rewrite_url; - - /* Store reverse path so that the magical tracking cookie can be set */ - if (config.reversemagic) - connptr->reversepath = safestrdup(reverse->path); } #endif @@ -1,4 +1,4 @@ -/* $Id: reqs.h,v 1.6 2005-07-12 17:39:44 rjkaes Exp $ +/* $Id: reqs.h,v 1.7 2005-08-16 04:03:19 rjkaes Exp $ * * See 'reqs.c' for a detailed description. * @@ -22,6 +22,5 @@ extern void handle_connection(int fd); extern void add_connect_port_allowed(int port); extern void upstream_add(const char *host, int port, const char *domain); -extern void reversepath_add(const char *path, const char *url); #endif diff --git a/src/reverse_proxy.c b/src/reverse_proxy.c new file mode 100644 index 0000000..40f15f4 --- /dev/null +++ b/src/reverse_proxy.c @@ -0,0 +1,154 @@ +/* $Id: reverse_proxy.c,v 1.1 2005-08-16 04:03:19 rjkaes Exp $ + * + * Allow tinyproxy to be used as a reverse proxy. + * + * Copyright (C) 1999-2005 Robert James Kaes (rjkaes@users.sourceforge.net) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +#include "tinyproxy.h" +#include "reverse_proxy.h" + +#include "conns.h" +#include "heap.h" +#include "htmlerror.h" +#include "log.h" + +/* + * Add entry to the reversepath list + */ +void +reversepath_add(const char *path, const char *url) +{ + struct reversepath *reverse; + + if (url == NULL) { + log_message(LOG_WARNING, + "Illegal reverse proxy rule: missing url"); + return; + } + + if (!strstr(url, "://")) { + log_message(LOG_WARNING, + "Skipping reverse proxy rule: '%s' is not a valid url", + url); + return; + } + + if (path && *path != '/') { + log_message(LOG_WARNING, + "Skipping reverse proxy rule: path '%s' doesn't start with a /", + path); + return; + } + + if (!(reverse = safemalloc(sizeof(struct reversepath)))) { + log_message(LOG_ERR, + "Unable to allocate memory in reversepath_add()"); + return; + } + + if (!path) + reverse->path = safestrdup("/"); + else + reverse->path = safestrdup(path); + + reverse->url = safestrdup(url); + + reverse->next = config.reversepath_list; + config.reversepath_list = reverse; + + log_message(LOG_INFO, + "Added reverse proxy rule: %s -> %s", reverse->path, + reverse->url); +} + +/* + * Check if a request url is in the reversepath list + */ +struct reversepath * +reversepath_get(char *url) +{ + struct reversepath *reverse = config.reversepath_list; + + while (reverse) { + if (strstr(url, reverse->path) == url) + return reverse; + + reverse = reverse->next; + } + + return NULL; +} + +/* + * Rewrite the URL for reverse proxying. + */ +char * +reverse_rewrite_url(struct conn_s *connptr, hashmap_t hashofheaders, char *url) +{ + char *rewrite_url = NULL; + char *cookie = NULL; + char *cookieval; + struct reversepath *reverse; + + /* Reverse requests always start with a slash */ + if (*url == '/') { + /* First try locating the reverse mapping by request url */ + reverse = reversepath_get(url); + if (reverse) { + rewrite_url = safemalloc(strlen(url) + + strlen(reverse->url) + 1); + strcpy(rewrite_url, reverse->url); + strcat(rewrite_url, url + strlen(reverse->path)); + } else if (config.reversemagic + && hashmap_entry_by_key(hashofheaders, + "cookie", + (void **)&cookie) > 0) { + + /* No match - try the magical tracking cookie next */ + if ((cookieval = strstr(cookie, REVERSE_COOKIE "=")) + && (reverse = + reversepath_get(cookieval + + strlen(REVERSE_COOKIE) + 1))) { + + rewrite_url = safemalloc(strlen(url) + + strlen + (reverse->url) + 1); + strcpy(rewrite_url, reverse->url); + strcat(rewrite_url, url + 1); + + log_message(LOG_INFO, + "Magical tracking cookie says: %s", + reverse->path); + } + } + } + + /* Forward proxy support off and no reverse path match found */ + if (config.reverseonly && !rewrite_url) { + log_message(LOG_ERR, "Bad request"); + indicate_http_error(connptr, 400, "Bad Request", + "detail", + "Request has an invalid URL", "url", + url, NULL); + return NULL; + } + + log_message(LOG_CONN, "Rewriting URL: %s -> %s", url, rewrite_url); + + /* Store reverse path so that the magical tracking cookie can be set */ + if (config.reversemagic) + connptr->reversepath = safestrdup(reverse->path); + + return rewrite_url; +} diff --git a/src/reverse_proxy.h b/src/reverse_proxy.h new file mode 100644 index 0000000..f27a9de --- /dev/null +++ b/src/reverse_proxy.h @@ -0,0 +1,36 @@ +/* $Id: reverse_proxy.h,v 1.1 2005-08-16 04:03:19 rjkaes Exp $ + * + * See 'reverse_proxy.c' for a detailed description. + * + * Copyright (C) 2005 Robert James Kaes (rjkaes@users.sourceforge.net) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + */ + +#ifndef TINYPROXY_REVERSE_PROXY_H +#define TINYPROXY_REVERSE_PROXY_H + +#include "conns.h" + +struct reversepath { + struct reversepath *next; + char *path; + char *url; +}; + +#define REVERSE_COOKIE "yummy_magical_cookie" + +extern void reversepath_add(const char *path, const char *url); +extern struct reversepath *reversepath_get(char *url); +extern char *reverse_rewrite_url(struct conn_s *connptr, + hashmap_t hashofheaders, char *url); + +#endif diff --git a/src/tinyproxy.h b/src/tinyproxy.h index 4cb091c..5e14857 100644 --- a/src/tinyproxy.h +++ b/src/tinyproxy.h @@ -1,4 +1,4 @@ -/* $Id: tinyproxy.h,v 1.47 2005-08-15 03:54:31 rjkaes Exp $ +/* $Id: tinyproxy.h,v 1.48 2005-08-16 04:03:19 rjkaes Exp $ * * See 'tinyproxy.c' for a detailed description. * @@ -38,16 +38,6 @@ struct upstream { in_addr_t ip, mask; }; -#ifdef REVERSE_SUPPORT -struct reversepath { - struct reversepath *next; - char *path; - char *url; -}; - -#define REVERSE_COOKIE "yummy_magical_cookie" -#endif - /* * Hold all the configuration time information. */ |