From a21cd7e3ed97041af3bb4c2c124f2474b78574ad Mon Sep 17 00:00:00 2001 From: Mukund Sivaraman Date: Fri, 7 Aug 2009 03:42:53 +0530 Subject: Rename tinyproxy.[ch] to main.[ch] --- src/Makefile.am | 2 +- src/acl.c | 2 +- src/anonymous.c | 2 +- src/buffer.c | 2 +- src/child.c | 2 +- src/common.h | 2 +- src/conffile.c | 2 +- src/conns.c | 2 +- src/conns.h | 2 +- src/daemon.c | 2 +- src/filter.c | 2 +- src/hashmap.c | 2 +- src/heap.c | 2 +- src/html-error.c | 2 +- src/log.c | 2 +- src/main.c | 476 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.h | 109 +++++++++++ src/network.c | 2 +- src/reqs.c | 2 +- src/reverse-proxy.c | 2 +- src/sock.c | 2 +- src/stats.c | 2 +- src/text.c | 2 +- src/tinyproxy.c | 476 ------------------------------------------------ src/tinyproxy.h | 109 ----------- src/transparent-proxy.c | 2 +- src/utils.c | 2 +- src/vector.c | 2 +- 28 files changed, 609 insertions(+), 609 deletions(-) create mode 100644 src/main.c create mode 100644 src/main.h delete mode 100644 src/tinyproxy.c delete mode 100644 src/tinyproxy.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 4b616e0..2d11ddc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,7 +36,7 @@ tinyproxy_SOURCES = \ sock.c sock.h \ stats.c stats.h \ text.c text.h \ - tinyproxy.c tinyproxy.h \ + main.c main.h \ utils.c utils.h \ vector.c vector.h diff --git a/src/acl.c b/src/acl.c index aec6773..ece859e 100644 --- a/src/acl.c +++ b/src/acl.c @@ -21,7 +21,7 @@ * which is then used to compare incoming connections. */ -#include "tinyproxy.h" +#include "main.h" #include "acl.h" #include "heap.h" diff --git a/src/anonymous.c b/src/anonymous.c index bfd2dc6..928abb8 100644 --- a/src/anonymous.c +++ b/src/anonymous.c @@ -20,7 +20,7 @@ * when the anonymous feature is turned on. */ -#include "tinyproxy.h" +#include "main.h" #include "anonymous.h" #include "hashmap.h" diff --git a/src/buffer.c b/src/buffer.c index 796e20a..d158ca0 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -25,7 +25,7 @@ * the ends of an array. :) */ -#include "tinyproxy.h" +#include "main.h" #include "buffer.h" #include "heap.h" diff --git a/src/child.c b/src/child.c index b540f3c..bb4110c 100644 --- a/src/child.c +++ b/src/child.c @@ -20,7 +20,7 @@ * processing incoming connections. */ -#include "tinyproxy.h" +#include "main.h" #include "child.h" #include "daemon.h" diff --git a/src/common.h b/src/common.h index 551d0f9..cf72071 100644 --- a/src/common.h +++ b/src/common.h @@ -17,7 +17,7 @@ */ /* This file groups all the headers required throughout the tinyproxy - * system. All this information use to be in the "tinyproxy.h" header, + * system. All this information use to be in the "main.h" header, * but various other "libraries" in the program need the same information, * without the tinyproxy specific defines. */ diff --git a/src/conffile.c b/src/conffile.c index a3eea02..888ddce 100644 --- a/src/conffile.c +++ b/src/conffile.c @@ -22,7 +22,7 @@ * add new directives to. Who knows if I'm right though. */ -#include "tinyproxy.h" +#include "main.h" #include "conffile.h" diff --git a/src/conns.c b/src/conns.c index 433f59a..3dc0575 100644 --- a/src/conns.c +++ b/src/conns.c @@ -22,7 +22,7 @@ * connection structure definition. */ -#include "tinyproxy.h" +#include "main.h" #include "buffer.h" #include "conns.h" diff --git a/src/conns.h b/src/conns.h index ae1fcaa..f28c2c1 100644 --- a/src/conns.h +++ b/src/conns.h @@ -21,7 +21,7 @@ #ifndef TINYPROXY_CONNS_H #define TINYPROXY_CONNS_H -#include "tinyproxy.h" +#include "main.h" #include "hashmap.h" /* diff --git a/src/daemon.c b/src/daemon.c index 9950842..17d41b5 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -21,7 +21,7 @@ * a function to portably set a signal handler. */ -#include "tinyproxy.h" +#include "main.h" #include "daemon.h" diff --git a/src/filter.c b/src/filter.c index 30642ed..d74ebb7 100644 --- a/src/filter.c +++ b/src/filter.c @@ -22,7 +22,7 @@ * pointed at by DEFAULT_FILTER. */ -#include "tinyproxy.h" +#include "main.h" #include "filter.h" #include "heap.h" diff --git a/src/hashmap.c b/src/hashmap.c index f8b181d..2c8327c 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -25,7 +25,7 @@ * don't try to free the data, or realloc the memory. :) */ -#include "tinyproxy.h" +#include "main.h" #include "hashmap.h" #include "heap.h" diff --git a/src/heap.c b/src/heap.c index c0c768b..b0a0200 100644 --- a/src/heap.c +++ b/src/heap.c @@ -23,7 +23,7 @@ * format of the log message is standardized. */ -#include "tinyproxy.h" +#include "main.h" #include "heap.h" #include "text.h" diff --git a/src/html-error.c b/src/html-error.c index 8c95469..09116a6 100644 --- a/src/html-error.c +++ b/src/html-error.c @@ -20,7 +20,7 @@ * HTML error pages with variable substitution. */ -#include "tinyproxy.h" +#include "main.h" #include "common.h" #include "buffer.h" diff --git a/src/log.c b/src/log.c index ca42c43..51f7c2d 100644 --- a/src/log.c +++ b/src/log.c @@ -21,7 +21,7 @@ * or the syslog daemon. Not much to it... */ -#include "tinyproxy.h" +#include "main.h" #include "heap.h" #include "log.h" diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..05b3dd0 --- /dev/null +++ b/src/main.c @@ -0,0 +1,476 @@ +/* tinyproxy - A fast light-weight HTTP proxy + * Copyright (C) 1998 Steven Young + * Copyright (C) 1998-2002 Robert James Kaes + * Copyright (C) 2000 Chris Lightfoot + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* The initialize routine. Basically sets up all the initial stuff (logfile, + * listening socket, config options, etc.) and then sits there and loops + * over the new connections until the daemon is closed. Also has additional + * functions to handle the "user friendly" aspects of a program (usage, + * stats, etc.) Like any good program, most of the work is actually done + * elsewhere. + */ + +#include "main.h" + +#include "anonymous.h" +#include "buffer.h" +#include "conffile.h" +#include "daemon.h" +#include "heap.h" +#include "filter.h" +#include "child.h" +#include "log.h" +#include "reqs.h" +#include "sock.h" +#include "stats.h" +#include "utils.h" + +RETSIGTYPE takesig (int sig); + +/* + * Global Structures + */ +struct config_s config; +float load = 0.00; +unsigned int received_sighup = FALSE; /* boolean */ +unsigned int processed_config_file = FALSE; /* boolean */ + +/* + * Handle a signal + */ +RETSIGTYPE +takesig (int sig) +{ + pid_t pid; + int status; + + switch (sig) + { + case SIGHUP: + received_sighup = TRUE; + break; + + case SIGTERM: + config.quit = TRUE; + break; + + case SIGCHLD: + while ((pid = waitpid (-1, &status, WNOHANG)) > 0); + break; + } + + return; +} + +/* + * Display the version information for the user. + */ +static void +display_version (void) +{ + printf ("%s %s (%s)\n", PACKAGE, VERSION, TARGET_SYSTEM); +} + +/* + * Display the copyright and license for this program. + */ +static void +display_license (void) +{ + display_version (); + + printf ("\ + Copyright 1998 Steven Young (sdyoung@well.com)\n\ + Copyright 1998-2002 Robert James Kaes (rjkaes@users.sourceforge.net)\n\ + Copyright 1999 George Talusan (gstalusan@uwaterloo.ca)\n\ + Copyright 2000 Chris Lightfoot (chris@ex-parrot.com)\n\ +\n\ + This program is free software; you can redistribute it and/or modify\n\ + it under the terms of the GNU General Public License as published by\n\ + the Free Software Foundation; either version 2, or (at your option)\n\ + any later version.\n\ +\n\ + This program is distributed in the hope that it will be useful,\n\ + but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ + GNU General Public License for more details.\n\ +\n\ + You should have received a copy of the GNU General Public License\n\ + along with this program; if not, write to the Free Software\n\ + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.\n"); +} + +/* + * Display usage to the user. + */ +static void +display_usage (void) +{ + printf ("Usage: %s [options]\n", PACKAGE); + printf ("\ +Options:\n\ + -d Operate in DEBUG mode.\n\ + -c FILE Use an alternate configuration file.\n\ + -h Display this usage information.\n\ + -l Display the license.\n\ + -v Display the version number.\n"); + + /* Display the modes compiled into tinyproxy */ + printf ("\nFeatures compiled in:\n"); +#ifdef XTINYPROXY_ENABLE + printf (" XTinyproxy header\n"); +#endif /* XTINYPROXY */ +#ifdef FILTER_ENABLE + printf (" Filtering\n"); +#endif /* FILTER_ENABLE */ +#ifndef NDEBUG + printf (" Debugging code\n"); +#endif /* NDEBUG */ +#ifdef TRANSPARENT_PROXY + printf (" Transparent proxy support\n"); +#endif /* TRANSPARENT_PROXY */ +#ifdef REVERSE_SUPPORT + printf (" Reverse proxy support\n"); +#endif /* REVERSE_SUPPORT */ +} + +static int +get_id (char *str) +{ + char *tstr; + + if (str == NULL) + return -1; + + tstr = str; + while (*tstr != 0) + { + if (!isdigit (*tstr)) + return -1; + tstr++; + } + + return atoi (str); +} + +int +main (int argc, char **argv) +{ + int optch; + unsigned int godaemon = TRUE; /* boolean */ + struct passwd *thisuser = NULL; + struct group *thisgroup = NULL; + FILE *config_file; + + /* Only allow u+rw bits. This may be required for some versions + * of glibc so that mkstemp() doesn't make us vulnerable. + */ + umask (0177); + + /* Default configuration file location */ + config.config_file = DEFAULT_CONF_FILE; + + /* + * Process the various options + */ + while ((optch = getopt (argc, argv, "c:vldh")) != EOF) + { + switch (optch) + { + case 'v': + display_version (); + exit (EX_OK); + case 'l': + display_license (); + exit (EX_OK); + case 'd': + godaemon = FALSE; + break; + case 'c': + config.config_file = safestrdup (optarg); + if (!config.config_file) + { + fprintf (stderr, "%s: Could not allocate memory.\n", argv[0]); + exit (EX_SOFTWARE); + } + break; + case 'h': + default: + display_usage (); + exit (EX_OK); + } + } + + log_message (LOG_INFO, "Initializing " PACKAGE " ..."); + + /* + * Make sure the HTML error pages array is NULL to begin with. + * (FIXME: Should have a better API for all this) + */ + config.errorpages = NULL; + + /* + * Read in the settings from the config file. + */ + config_file = fopen (config.config_file, "r"); + if (!config_file) + { + fprintf (stderr, + "%s: Could not open configuration file \"%s\".\n", + argv[0], config.config_file); + exit (EX_SOFTWARE); + } + if (config_compile () || config_parse (&config, config_file)) + { + fprintf (stderr, + "Unable to parse configuration file. Not starting.\n"); + exit (EX_SOFTWARE); + } + fclose (config_file); + + /* + * Write to a user supplied log file if it's defined. This + * will override using the syslog even if syslog is defined. + */ + if (config.logf_name) + { + if (open_log_file (config.logf_name) < 0) + { + fprintf (stderr, "%s: Could not create log file.\n", argv[0]); + exit (EX_SOFTWARE); + } + config.syslog = FALSE; /* disable syslog */ + } + else if (config.syslog) + { + if (godaemon == TRUE) + openlog ("tinyproxy", LOG_PID, LOG_DAEMON); + else + openlog ("tinyproxy", LOG_PID, LOG_USER); + } + else + { + fprintf (stderr, + "%s: Either define a logfile or enable syslog logging.\n", + argv[0]); + exit (EX_SOFTWARE); + } + + processed_config_file = TRUE; + send_stored_logs (); + + /* + * Set the default values if they were not set in the config file. + */ + if (config.port == 0) + { + fprintf (stderr, + "%s: You MUST set a Port in the configuration file.\n", + argv[0]); + exit (EX_SOFTWARE); + } + if (!config.stathost) + { + log_message (LOG_INFO, "Setting stathost to \"%s\".", DEFAULT_STATHOST); + config.stathost = DEFAULT_STATHOST; + } + if (!config.user) + { + log_message (LOG_WARNING, + "You SHOULD set a UserName in the configuration file. " + "Using current user instead."); + } + if (config.idletimeout == 0) + { + log_message (LOG_WARNING, + "Invalid idle time setting. Only values greater than zero " + "allowed; therefore setting idle timeout to %u seconds.", + MAX_IDLE_TIME); + config.idletimeout = MAX_IDLE_TIME; + } + + init_stats (); + + /* + * If ANONYMOUS is turned on, make sure that Content-Length is + * in the list of allowed headers, since it is required in a + * HTTP/1.0 request. Also add the Content-Type header since it goes + * hand in hand with Content-Length. + * - rjkaes + */ + if (is_anonymous_enabled ()) + { + anonymous_insert ("Content-Length"); + anonymous_insert ("Content-Type"); + } + + if (godaemon == TRUE) + makedaemon (); + + if (config.pidpath) + { + if (pidfile_create (config.pidpath) < 0) + { + fprintf (stderr, "%s: Could not create PID file.\n", argv[0]); + exit (EX_OSERR); + } + } + + if (set_signal_handler (SIGPIPE, SIG_IGN) == SIG_ERR) + { + fprintf (stderr, "%s: Could not set the \"SIGPIPE\" signal.\n", + argv[0]); + exit (EX_OSERR); + } +#ifdef FILTER_ENABLE + if (config.filter) + filter_init (); +#endif /* FILTER_ENABLE */ + + /* + * Start listening on the selected port. + */ + if (child_listening_sock (config.port) < 0) + { + fprintf (stderr, "%s: Could not create listening socket.\n", argv[0]); + exit (EX_OSERR); + } + + /* + * Switch to a different user. + */ + if (geteuid () == 0) + { + if (config.group && strlen (config.group) > 0) + { + int gid = get_id (config.group); + if (gid < 0) + { + thisgroup = getgrnam (config.group); + if (!thisgroup) + { + fprintf (stderr, + "%s: Unable to find " + "group \"%s\".\n", argv[0], config.group); + exit (EX_NOUSER); + } + gid = thisgroup->gr_gid; + } + if (setgid (gid) < 0) + { + fprintf (stderr, + "%s: Unable to change to " + "group \"%s\".\n", argv[0], config.group); + exit (EX_CANTCREAT); + } + log_message (LOG_INFO, "Now running as group \"%s\".", + config.group); + } + if (config.user && strlen (config.user) > 0) + { + int uid = get_id (config.user); + if (uid < 0) + { + thisuser = getpwnam (config.user); + if (!thisuser) + { + fprintf (stderr, + "%s: Unable to find " + "user \"%s\".\n", argv[0], config.user); + exit (EX_NOUSER); + } + uid = thisuser->pw_uid; + } + if (setuid (uid) < 0) + { + fprintf (stderr, + "%s: Unable to change to user \"%s\".\n", + argv[0], config.user); + exit (EX_CANTCREAT); + } + log_message (LOG_INFO, "Now running as user \"%s\".", config.user); + } + } + else + { + log_message (LOG_WARNING, + "Not running as root, so not changing UID/GID."); + } + + if (child_pool_create () < 0) + { + fprintf (stderr, "%s: Could not create the pool of children.\n", argv[0]); + exit (EX_SOFTWARE); + } + + /* + * These signals are only for the parent process. + */ + log_message (LOG_INFO, "Setting the various signals."); + if (set_signal_handler (SIGCHLD, takesig) == SIG_ERR) + { + fprintf (stderr, "%s: Could not set the \"SIGCHLD\" signal.\n", + argv[0]); + exit (EX_OSERR); + } + if (set_signal_handler (SIGTERM, takesig) == SIG_ERR) + { + fprintf (stderr, "%s: Could not set the \"SIGTERM\" signal.\n", + argv[0]); + exit (EX_OSERR); + } + if (set_signal_handler (SIGHUP, takesig) == SIG_ERR) + { + fprintf (stderr, "%s: Could not set the \"SIGHUP\" signal.\n", argv[0]); + exit (EX_OSERR); + } + + /* + * Start the main loop. + */ + log_message (LOG_INFO, "Starting main loop. Accepting connections."); + + child_main_loop (); + + log_message (LOG_INFO, "Shutting down."); + + child_kill_children (); + child_close_sock (); + + /* + * Remove the PID file. + */ + if (unlink (config.pidpath) < 0) + { + log_message (LOG_WARNING, + "Could not remove PID file \"%s\": %s.", + config.pidpath, strerror (errno)); + } +#ifdef FILTER_ENABLE + if (config.filter) + filter_destroy (); +#endif /* FILTER_ENABLE */ + + if (config.syslog) + closelog (); + else + close_log_file (); + + exit (EX_OK); +} diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..e508aa0 --- /dev/null +++ b/src/main.h @@ -0,0 +1,109 @@ +/* tinyproxy - A fast light-weight HTTP proxy + * Copyright (C) 1998 Steven Young + * Copyright (C) 1999 Robert James Kaes + * + * 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 of the License, 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* See 'main.c' for detailed information. */ + +#ifndef __MAIN_H__ +#define __MAIN_H__ + +#include "common.h" +#include "hashmap.h" + +/* Global variables for the main controls of the program */ +#define MAXBUFFSIZE ((size_t)(1024 * 96)) /* Max size of buffer */ +#define MAX_IDLE_TIME (60 * 10) /* 10 minutes of no activity */ + +/* + * Even if upstream support is not compiled into tinyproxy, this + * structure still needs to be defined. + */ +struct upstream +{ + struct upstream *next; + char *domain; /* optional */ + char *host; + int port; + in_addr_t ip, mask; +}; + +/* + * Hold all the configuration time information. + */ +struct config_s +{ + char *logf_name; + char *config_file; + unsigned int syslog; /* boolean */ + int port; + char *stathost; + unsigned int quit; /* boolean */ + char *user; + char *group; + char *ipAddr; +#ifdef FILTER_ENABLE + char *filter; + unsigned int filter_url; /* boolean */ + unsigned int filter_extended; /* boolean */ + unsigned int filter_casesensitive; /* boolean */ +#endif /* FILTER_ENABLE */ +#ifdef XTINYPROXY_ENABLE + char *my_domain; +#endif +#ifdef REVERSE_SUPPORT + struct reversepath *reversepath_list; + unsigned int reverseonly; /* boolean */ + unsigned int reversemagic; /* boolean */ + char *reversebaseurl; +#endif +#ifdef UPSTREAM_SUPPORT + struct upstream *upstream_list; +#endif /* UPSTREAM_SUPPORT */ + char *pidpath; + unsigned int idletimeout; + char *bind_address; + unsigned int bindsame; + + /* + * The configured name to use in the HTTP "Via" header field. + */ + char *via_proxy_name; + + /* + * Error page support. Map error numbers to file paths. + */ + hashmap_t errorpages; + + /* + * Error page to be displayed if appropriate page cannot be located + * in the errorpages structure. + */ + char *errorpage_undef; + + /* + * The HTML statistics page. + */ + char *statpage; +}; + +/* Global Structures used in the program */ +extern struct config_s config; +extern unsigned int received_sighup; /* boolean */ +extern unsigned int processed_config_file; /* boolean */ + +#endif /* __MAIN_H__ */ diff --git a/src/network.c b/src/network.c index fe65a15..22dfadf 100644 --- a/src/network.c +++ b/src/network.c @@ -23,7 +23,7 @@ * to write an arbitrary amount of data to the network. */ -#include "tinyproxy.h" +#include "main.h" #include "heap.h" #include "network.h" diff --git a/src/reqs.c b/src/reqs.c index 0fc4778..1950a5b 100644 --- a/src/reqs.c +++ b/src/reqs.c @@ -25,7 +25,7 @@ * and then relays the bytes between the two. */ -#include "tinyproxy.h" +#include "main.h" #include "acl.h" #include "anonymous.h" diff --git a/src/reverse-proxy.c b/src/reverse-proxy.c index d4ca7eb..8d97538 100644 --- a/src/reverse-proxy.c +++ b/src/reverse-proxy.c @@ -18,7 +18,7 @@ /* Allow tinyproxy to be used as a reverse proxy. */ -#include "tinyproxy.h" +#include "main.h" #include "reverse-proxy.h" #include "conns.h" diff --git a/src/sock.c b/src/sock.c index 8da0abf..5673d46 100644 --- a/src/sock.c +++ b/src/sock.c @@ -25,7 +25,7 @@ * actually is. */ -#include "tinyproxy.h" +#include "main.h" #include "log.h" #include "heap.h" diff --git a/src/stats.c b/src/stats.c index 3c3b0d9..6abd4bd 100644 --- a/src/stats.c +++ b/src/stats.c @@ -25,7 +25,7 @@ * and the switch statement in update_stats(). */ -#include "tinyproxy.h" +#include "main.h" #include "log.h" #include "heap.h" diff --git a/src/text.c b/src/text.c index 44ee558..1536eac 100644 --- a/src/text.c +++ b/src/text.c @@ -22,7 +22,7 @@ * with the standard C string library. */ -#include "tinyproxy.h" +#include "main.h" #include "text.h" diff --git a/src/tinyproxy.c b/src/tinyproxy.c deleted file mode 100644 index b5a214d..0000000 --- a/src/tinyproxy.c +++ /dev/null @@ -1,476 +0,0 @@ -/* tinyproxy - A fast light-weight HTTP proxy - * Copyright (C) 1998 Steven Young - * Copyright (C) 1998-2002 Robert James Kaes - * Copyright (C) 2000 Chris Lightfoot - * - * 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 of the License, 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. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* The initialize routine. Basically sets up all the initial stuff (logfile, - * listening socket, config options, etc.) and then sits there and loops - * over the new connections until the daemon is closed. Also has additional - * functions to handle the "user friendly" aspects of a program (usage, - * stats, etc.) Like any good program, most of the work is actually done - * elsewhere. - */ - -#include "tinyproxy.h" - -#include "anonymous.h" -#include "buffer.h" -#include "conffile.h" -#include "daemon.h" -#include "heap.h" -#include "filter.h" -#include "child.h" -#include "log.h" -#include "reqs.h" -#include "sock.h" -#include "stats.h" -#include "utils.h" - -RETSIGTYPE takesig (int sig); - -/* - * Global Structures - */ -struct config_s config; -float load = 0.00; -unsigned int received_sighup = FALSE; /* boolean */ -unsigned int processed_config_file = FALSE; /* boolean */ - -/* - * Handle a signal - */ -RETSIGTYPE -takesig (int sig) -{ - pid_t pid; - int status; - - switch (sig) - { - case SIGHUP: - received_sighup = TRUE; - break; - - case SIGTERM: - config.quit = TRUE; - break; - - case SIGCHLD: - while ((pid = waitpid (-1, &status, WNOHANG)) > 0); - break; - } - - return; -} - -/* - * Display the version information for the user. - */ -static void -display_version (void) -{ - printf ("%s %s (%s)\n", PACKAGE, VERSION, TARGET_SYSTEM); -} - -/* - * Display the copyright and license for this program. - */ -static void -display_license (void) -{ - display_version (); - - printf ("\ - Copyright 1998 Steven Young (sdyoung@well.com)\n\ - Copyright 1998-2002 Robert James Kaes (rjkaes@users.sourceforge.net)\n\ - Copyright 1999 George Talusan (gstalusan@uwaterloo.ca)\n\ - Copyright 2000 Chris Lightfoot (chris@ex-parrot.com)\n\ -\n\ - This program is free software; you can redistribute it and/or modify\n\ - it under the terms of the GNU General Public License as published by\n\ - the Free Software Foundation; either version 2, or (at your option)\n\ - any later version.\n\ -\n\ - This program is distributed in the hope that it will be useful,\n\ - but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ - GNU General Public License for more details.\n\ -\n\ - You should have received a copy of the GNU General Public License\n\ - along with this program; if not, write to the Free Software\n\ - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.\n"); -} - -/* - * Display usage to the user. - */ -static void -display_usage (void) -{ - printf ("Usage: %s [options]\n", PACKAGE); - printf ("\ -Options:\n\ - -d Operate in DEBUG mode.\n\ - -c FILE Use an alternate configuration file.\n\ - -h Display this usage information.\n\ - -l Display the license.\n\ - -v Display the version number.\n"); - - /* Display the modes compiled into tinyproxy */ - printf ("\nFeatures compiled in:\n"); -#ifdef XTINYPROXY_ENABLE - printf (" XTinyproxy header\n"); -#endif /* XTINYPROXY */ -#ifdef FILTER_ENABLE - printf (" Filtering\n"); -#endif /* FILTER_ENABLE */ -#ifndef NDEBUG - printf (" Debugging code\n"); -#endif /* NDEBUG */ -#ifdef TRANSPARENT_PROXY - printf (" Transparent proxy support\n"); -#endif /* TRANSPARENT_PROXY */ -#ifdef REVERSE_SUPPORT - printf (" Reverse proxy support\n"); -#endif /* REVERSE_SUPPORT */ -} - -static int -get_id (char *str) -{ - char *tstr; - - if (str == NULL) - return -1; - - tstr = str; - while (*tstr != 0) - { - if (!isdigit (*tstr)) - return -1; - tstr++; - } - - return atoi (str); -} - -int -main (int argc, char **argv) -{ - int optch; - unsigned int godaemon = TRUE; /* boolean */ - struct passwd *thisuser = NULL; - struct group *thisgroup = NULL; - FILE *config_file; - - /* Only allow u+rw bits. This may be required for some versions - * of glibc so that mkstemp() doesn't make us vulnerable. - */ - umask (0177); - - /* Default configuration file location */ - config.config_file = DEFAULT_CONF_FILE; - - /* - * Process the various options - */ - while ((optch = getopt (argc, argv, "c:vldh")) != EOF) - { - switch (optch) - { - case 'v': - display_version (); - exit (EX_OK); - case 'l': - display_license (); - exit (EX_OK); - case 'd': - godaemon = FALSE; - break; - case 'c': - config.config_file = safestrdup (optarg); - if (!config.config_file) - { - fprintf (stderr, "%s: Could not allocate memory.\n", argv[0]); - exit (EX_SOFTWARE); - } - break; - case 'h': - default: - display_usage (); - exit (EX_OK); - } - } - - log_message (LOG_INFO, "Initializing " PACKAGE " ..."); - - /* - * Make sure the HTML error pages array is NULL to begin with. - * (FIXME: Should have a better API for all this) - */ - config.errorpages = NULL; - - /* - * Read in the settings from the config file. - */ - config_file = fopen (config.config_file, "r"); - if (!config_file) - { - fprintf (stderr, - "%s: Could not open configuration file \"%s\".\n", - argv[0], config.config_file); - exit (EX_SOFTWARE); - } - if (config_compile () || config_parse (&config, config_file)) - { - fprintf (stderr, - "Unable to parse configuration file. Not starting.\n"); - exit (EX_SOFTWARE); - } - fclose (config_file); - - /* - * Write to a user supplied log file if it's defined. This - * will override using the syslog even if syslog is defined. - */ - if (config.logf_name) - { - if (open_log_file (config.logf_name) < 0) - { - fprintf (stderr, "%s: Could not create log file.\n", argv[0]); - exit (EX_SOFTWARE); - } - config.syslog = FALSE; /* disable syslog */ - } - else if (config.syslog) - { - if (godaemon == TRUE) - openlog ("tinyproxy", LOG_PID, LOG_DAEMON); - else - openlog ("tinyproxy", LOG_PID, LOG_USER); - } - else - { - fprintf (stderr, - "%s: Either define a logfile or enable syslog logging.\n", - argv[0]); - exit (EX_SOFTWARE); - } - - processed_config_file = TRUE; - send_stored_logs (); - - /* - * Set the default values if they were not set in the config file. - */ - if (config.port == 0) - { - fprintf (stderr, - "%s: You MUST set a Port in the configuration file.\n", - argv[0]); - exit (EX_SOFTWARE); - } - if (!config.stathost) - { - log_message (LOG_INFO, "Setting stathost to \"%s\".", DEFAULT_STATHOST); - config.stathost = DEFAULT_STATHOST; - } - if (!config.user) - { - log_message (LOG_WARNING, - "You SHOULD set a UserName in the configuration file. " - "Using current user instead."); - } - if (config.idletimeout == 0) - { - log_message (LOG_WARNING, - "Invalid idle time setting. Only values greater than zero " - "allowed; therefore setting idle timeout to %u seconds.", - MAX_IDLE_TIME); - config.idletimeout = MAX_IDLE_TIME; - } - - init_stats (); - - /* - * If ANONYMOUS is turned on, make sure that Content-Length is - * in the list of allowed headers, since it is required in a - * HTTP/1.0 request. Also add the Content-Type header since it goes - * hand in hand with Content-Length. - * - rjkaes - */ - if (is_anonymous_enabled ()) - { - anonymous_insert ("Content-Length"); - anonymous_insert ("Content-Type"); - } - - if (godaemon == TRUE) - makedaemon (); - - if (config.pidpath) - { - if (pidfile_create (config.pidpath) < 0) - { - fprintf (stderr, "%s: Could not create PID file.\n", argv[0]); - exit (EX_OSERR); - } - } - - if (set_signal_handler (SIGPIPE, SIG_IGN) == SIG_ERR) - { - fprintf (stderr, "%s: Could not set the \"SIGPIPE\" signal.\n", - argv[0]); - exit (EX_OSERR); - } -#ifdef FILTER_ENABLE - if (config.filter) - filter_init (); -#endif /* FILTER_ENABLE */ - - /* - * Start listening on the selected port. - */ - if (child_listening_sock (config.port) < 0) - { - fprintf (stderr, "%s: Could not create listening socket.\n", argv[0]); - exit (EX_OSERR); - } - - /* - * Switch to a different user. - */ - if (geteuid () == 0) - { - if (config.group && strlen (config.group) > 0) - { - int gid = get_id (config.group); - if (gid < 0) - { - thisgroup = getgrnam (config.group); - if (!thisgroup) - { - fprintf (stderr, - "%s: Unable to find " - "group \"%s\".\n", argv[0], config.group); - exit (EX_NOUSER); - } - gid = thisgroup->gr_gid; - } - if (setgid (gid) < 0) - { - fprintf (stderr, - "%s: Unable to change to " - "group \"%s\".\n", argv[0], config.group); - exit (EX_CANTCREAT); - } - log_message (LOG_INFO, "Now running as group \"%s\".", - config.group); - } - if (config.user && strlen (config.user) > 0) - { - int uid = get_id (config.user); - if (uid < 0) - { - thisuser = getpwnam (config.user); - if (!thisuser) - { - fprintf (stderr, - "%s: Unable to find " - "user \"%s\".\n", argv[0], config.user); - exit (EX_NOUSER); - } - uid = thisuser->pw_uid; - } - if (setuid (uid) < 0) - { - fprintf (stderr, - "%s: Unable to change to user \"%s\".\n", - argv[0], config.user); - exit (EX_CANTCREAT); - } - log_message (LOG_INFO, "Now running as user \"%s\".", config.user); - } - } - else - { - log_message (LOG_WARNING, - "Not running as root, so not changing UID/GID."); - } - - if (child_pool_create () < 0) - { - fprintf (stderr, "%s: Could not create the pool of children.\n", argv[0]); - exit (EX_SOFTWARE); - } - - /* - * These signals are only for the parent process. - */ - log_message (LOG_INFO, "Setting the various signals."); - if (set_signal_handler (SIGCHLD, takesig) == SIG_ERR) - { - fprintf (stderr, "%s: Could not set the \"SIGCHLD\" signal.\n", - argv[0]); - exit (EX_OSERR); - } - if (set_signal_handler (SIGTERM, takesig) == SIG_ERR) - { - fprintf (stderr, "%s: Could not set the \"SIGTERM\" signal.\n", - argv[0]); - exit (EX_OSERR); - } - if (set_signal_handler (SIGHUP, takesig) == SIG_ERR) - { - fprintf (stderr, "%s: Could not set the \"SIGHUP\" signal.\n", argv[0]); - exit (EX_OSERR); - } - - /* - * Start the main loop. - */ - log_message (LOG_INFO, "Starting main loop. Accepting connections."); - - child_main_loop (); - - log_message (LOG_INFO, "Shutting down."); - - child_kill_children (); - child_close_sock (); - - /* - * Remove the PID file. - */ - if (unlink (config.pidpath) < 0) - { - log_message (LOG_WARNING, - "Could not remove PID file \"%s\": %s.", - config.pidpath, strerror (errno)); - } -#ifdef FILTER_ENABLE - if (config.filter) - filter_destroy (); -#endif /* FILTER_ENABLE */ - - if (config.syslog) - closelog (); - else - close_log_file (); - - exit (EX_OK); -} diff --git a/src/tinyproxy.h b/src/tinyproxy.h deleted file mode 100644 index 2d4a187..0000000 --- a/src/tinyproxy.h +++ /dev/null @@ -1,109 +0,0 @@ -/* tinyproxy - A fast light-weight HTTP proxy - * Copyright (C) 1998 Steven Young - * Copyright (C) 1999 Robert James Kaes - * - * 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 of the License, 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. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -/* See 'tinyproxy.c' for detailed information. */ - -#ifndef TINYPROXY_TINYPROXY_H -#define TINYPROXY_TINYPROXY_H - -#include "common.h" -#include "hashmap.h" - -/* Global variables for the main controls of the program */ -#define MAXBUFFSIZE ((size_t)(1024 * 96)) /* Max size of buffer */ -#define MAX_IDLE_TIME (60 * 10) /* 10 minutes of no activity */ - -/* - * Even if upstream support is not compiled into tinyproxy, this - * structure still needs to be defined. - */ -struct upstream -{ - struct upstream *next; - char *domain; /* optional */ - char *host; - int port; - in_addr_t ip, mask; -}; - -/* - * Hold all the configuration time information. - */ -struct config_s -{ - char *logf_name; - char *config_file; - unsigned int syslog; /* boolean */ - int port; - char *stathost; - unsigned int quit; /* boolean */ - char *user; - char *group; - char *ipAddr; -#ifdef FILTER_ENABLE - char *filter; - unsigned int filter_url; /* boolean */ - unsigned int filter_extended; /* boolean */ - unsigned int filter_casesensitive; /* boolean */ -#endif /* FILTER_ENABLE */ -#ifdef XTINYPROXY_ENABLE - char *my_domain; -#endif -#ifdef REVERSE_SUPPORT - struct reversepath *reversepath_list; - unsigned int reverseonly; /* boolean */ - unsigned int reversemagic; /* boolean */ - char *reversebaseurl; -#endif -#ifdef UPSTREAM_SUPPORT - struct upstream *upstream_list; -#endif /* UPSTREAM_SUPPORT */ - char *pidpath; - unsigned int idletimeout; - char *bind_address; - unsigned int bindsame; - - /* - * The configured name to use in the HTTP "Via" header field. - */ - char *via_proxy_name; - - /* - * Error page support. Map error numbers to file paths. - */ - hashmap_t errorpages; - - /* - * Error page to be displayed if appropriate page cannot be located - * in the errorpages structure. - */ - char *errorpage_undef; - - /* - * The HTML statistics page. - */ - char *statpage; -}; - -/* Global Structures used in the program */ -extern struct config_s config; -extern unsigned int received_sighup; /* boolean */ -extern unsigned int processed_config_file; /* boolean */ - -#endif diff --git a/src/transparent-proxy.c b/src/transparent-proxy.c index a3c4ee8..7318697 100644 --- a/src/transparent-proxy.c +++ b/src/transparent-proxy.c @@ -23,7 +23,7 @@ * traffic to tinyproxy for this to work properly. */ -#include "tinyproxy.h" +#include "main.h" #include "transparent-proxy.h" #include "conns.h" diff --git a/src/utils.c b/src/utils.c index b657e65..fc65af0 100644 --- a/src/utils.c +++ b/src/utils.c @@ -23,7 +23,7 @@ * it, so it's in here. */ -#include "tinyproxy.h" +#include "main.h" #include "conns.h" #include "heap.h" diff --git a/src/vector.c b/src/vector.c index 944c38b..77f3955 100644 --- a/src/vector.c +++ b/src/vector.c @@ -21,7 +21,7 @@ * vector.) */ -#include "tinyproxy.h" +#include "main.h" #include "heap.h" #include "vector.h" -- cgit v1.2.3