diff options
author | Mukund Sivaraman <muks@banu.com> | 2008-12-08 13:39:44 +0000 |
---|---|---|
committer | Mukund Sivaraman <muks@banu.com> | 2008-12-08 13:39:44 +0000 |
commit | 024b317de09a7d426973ffa16e4781ec75aaef35 (patch) | |
tree | 6994f5266e93773333bfa99fa24489ac139452a3 /src | |
parent | 6e6f992f8cbc01ce4d6840d1a020f18846c37847 (diff) | |
download | tinyproxy-024b317de09a7d426973ffa16e4781ec75aaef35.tar.gz tinyproxy-024b317de09a7d426973ffa16e4781ec75aaef35.zip |
Convert tabs to spaces
Diffstat (limited to '')
-rw-r--r-- | src/acl.c | 146 | ||||
-rw-r--r-- | src/acl.h | 2 | ||||
-rw-r--r-- | src/anonymous.c | 2 | ||||
-rw-r--r-- | src/buffer.c | 101 | ||||
-rw-r--r-- | src/buffer.h | 2 | ||||
-rw-r--r-- | src/child.c | 220 | ||||
-rw-r--r-- | src/common.h | 10 | ||||
-rw-r--r-- | src/conffile.c | 58 | ||||
-rw-r--r-- | src/conns.c | 6 | ||||
-rw-r--r-- | src/conns.h | 4 | ||||
-rw-r--r-- | src/daemon.c | 4 | ||||
-rw-r--r-- | src/filter.c | 176 | ||||
-rw-r--r-- | src/hashmap.c | 116 | ||||
-rw-r--r-- | src/hashmap.h | 10 | ||||
-rw-r--r-- | src/heap.c | 8 | ||||
-rw-r--r-- | src/heap.h | 8 | ||||
-rw-r--r-- | src/html-error.c | 120 | ||||
-rw-r--r-- | src/html-error.h | 4 | ||||
-rw-r--r-- | src/http-message.c | 10 | ||||
-rw-r--r-- | src/http-message.h | 10 | ||||
-rw-r--r-- | src/log.c | 34 | ||||
-rw-r--r-- | src/log.h | 2 | ||||
-rw-r--r-- | src/network.c | 96 | ||||
-rw-r--r-- | src/reqs.c | 841 | ||||
-rw-r--r-- | src/reverse-proxy.c | 69 | ||||
-rw-r--r-- | src/reverse-proxy.h | 2 | ||||
-rw-r--r-- | src/sock.c | 50 | ||||
-rw-r--r-- | src/stats.c | 20 | ||||
-rw-r--r-- | src/stats.h | 10 | ||||
-rw-r--r-- | src/text.c | 2 | ||||
-rw-r--r-- | src/tinyproxy.c | 206 | ||||
-rw-r--r-- | src/tinyproxy.h | 28 | ||||
-rw-r--r-- | src/transparent-proxy.c | 51 | ||||
-rw-r--r-- | src/transparent-proxy.h | 6 | ||||
-rw-r--r-- | src/utils.c | 94 | ||||
-rw-r--r-- | src/utils.h | 4 | ||||
-rw-r--r-- | src/vector.h | 4 |
37 files changed, 1262 insertions, 1274 deletions
@@ -72,13 +72,13 @@ static vector_t access_list = NULL; */ inline static int fill_netmask_array (char *bitmask_string, unsigned char array[], - unsigned int len) + unsigned int len) { unsigned int i; long int mask; char *endptr; - errno = 0; /* to distinguish success/failure after call */ + errno = 0; /* to distinguish success/failure after call */ mask = strtol (bitmask_string, &endptr, 10); /* check for various conversion errors */ @@ -94,19 +94,19 @@ fill_netmask_array (char *bitmask_string, unsigned char array[], for (i = 0; i != len; ++i) { if (mask >= 8) - { - array[i] = 0xff; - mask -= 8; - } + { + array[i] = 0xff; + mask -= 8; + } else if (mask > 0) - { - array[i] = (unsigned char) (0xff << (8 - mask)); - mask = 0; - } + { + array[i] = (unsigned char) (0xff << (8 - mask)); + mask = 0; + } else - { - array[i] = 0; - } + { + array[i] = 0; + } } return 0; @@ -138,10 +138,10 @@ insert_acl (char *location, acl_access_t access_type) { access_list = vector_create (); if (!access_list) - { - log_message (LOG_ERR, "Unable to allocate memory for access list"); - return -1; - } + { + log_message (LOG_ERR, "Unable to allocate memory for access list"); + return -1; + } } /* @@ -167,30 +167,30 @@ insert_acl (char *location, acl_access_t access_type) */ p = strchr (location, '/'); if (p != NULL) - { - /* - * 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; - memcpy (acl.address.ip.octet, ip_dst, IPV6_LEN); - - if (fill_netmask_array (p + 1, &(acl.address.ip.mask[0]), IPV6_LEN) - < 0) - return -1; - } + { + /* + * 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; + memcpy (acl.address.ip.octet, ip_dst, IPV6_LEN); + + if (fill_netmask_array (p + 1, &(acl.address.ip.mask[0]), IPV6_LEN) + < 0) + return -1; + } else - { - /* In all likelihood a string */ - acl.type = ACL_STRING; - acl.address.string = safestrdup (location); - if (!acl.address.string) - return -1; - } + { + /* In all likelihood a string */ + acl.type = ACL_STRING; + acl.address.string = safestrdup (location); + if (!acl.address.string) + return -1; + } } /* @@ -212,7 +212,7 @@ insert_acl (char *location, acl_access_t access_type) */ static int acl_string_processing (struct acl_s *acl, - const char *ip_address, const char *string_address) + const char *ip_address, const char *string_address) { int match; struct addrinfo hints, *res, *ressave; @@ -234,31 +234,31 @@ acl_string_processing (struct acl_s *acl, hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo (acl->address.string, NULL, &hints, &res) != 0) - goto STRING_TEST; + goto STRING_TEST; ressave = res; match = FALSE; do - { - get_ip_string (res->ai_addr, ipbuf, sizeof (ipbuf)); - if (strcmp (ip_address, ipbuf) == 0) - { - match = TRUE; - break; - } - } + { + get_ip_string (res->ai_addr, ipbuf, sizeof (ipbuf)); + if (strcmp (ip_address, ipbuf) == 0) + { + match = TRUE; + break; + } + } while ((res = res->ai_next) != NULL); freeaddrinfo (ressave); if (match) - { - if (acl->access == ACL_DENY) - return 0; - else - return 1; - } + { + if (acl->access == ACL_DENY) + return 0; + else + return 1; + } } STRING_TEST: @@ -277,9 +277,9 @@ STRING_TEST: acl->address.string) == 0) { if (acl->access == ACL_DENY) - return 0; + return 0; else - return 1; + return 1; } /* Indicate that no tests succeeded, so skip to next control. */ @@ -313,7 +313,7 @@ check_numeric_acl (const struct acl_s *acl, const char *ip) /* If x and y don't match, the IP addresses don't match */ if (x != y) - return 0; + return 0; } /* The addresses match, return the permission */ @@ -348,32 +348,32 @@ check_acl (int fd, const char *ip, const char *host) { acl = vector_getentry (access_list, i, NULL); switch (acl->type) - { - case ACL_STRING: - perm = acl_string_processing (acl, ip, host); - break; - - case ACL_NUMERIC: - if (ip[0] == '\0') - continue; - perm = check_numeric_acl (acl, ip); - break; - } + { + case ACL_STRING: + perm = acl_string_processing (acl, ip, host); + break; + + case ACL_NUMERIC: + if (ip[0] == '\0') + continue; + perm = check_numeric_acl (acl, ip); + break; + } /* * Check the return value too see if the IP address is * allowed or denied. */ if (perm == 0) - break; + break; else if (perm == 1) - return perm; + return perm; } /* * Deny all connections by default. */ log_message (LOG_NOTICE, "Unauthorized connection from \"%s\" [%s].", - host, ip); + host, ip); return 0; } @@ -26,6 +26,6 @@ typedef enum extern int insert_acl (char *location, acl_access_t access_type); extern int check_acl (int fd, const char *ip_address, - const char *string_address); + const char *string_address); #endif diff --git a/src/anonymous.c b/src/anonymous.c index dd16421..87dfaf5 100644 --- a/src/anonymous.c +++ b/src/anonymous.c @@ -65,7 +65,7 @@ anonymous_insert (char *s) { anonymous_map = hashmap_create (32); if (!anonymous_map) - return -1; + return -1; } if (hashmap_search (anonymous_map, s) > 0) diff --git a/src/buffer.c b/src/buffer.c index ec19e59..4df30aa 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -36,10 +36,10 @@ struct bufline_s { - unsigned char *string; /* the actual string of data */ - struct bufline_s *next; /* pointer to next in linked list */ - size_t length; /* length of the string of data */ - size_t pos; /* start sending from this offset */ + unsigned char *string; /* the actual string of data */ + struct bufline_s *next; /* pointer to next in linked list */ + size_t length; /* length of the string of data */ + size_t pos; /* start sending from this offset */ }; /* @@ -48,9 +48,9 @@ struct bufline_s */ struct buffer_s { - struct bufline_s *head; /* top of the buffer */ - struct bufline_s *tail; /* bottom of the buffer */ - size_t size; /* total size of the buffer */ + struct bufline_s *head; /* top of the buffer */ + struct bufline_s *tail; /* bottom of the buffer */ + size_t size; /* total size of the buffer */ }; /* @@ -244,40 +244,40 @@ read_buffer (int fd, struct buffer_s * buffptr) if (bytesin > 0) { if (add_to_buffer (buffptr, buffer, bytesin) < 0) - { - log_message (LOG_ERR, "readbuff: add_to_buffer() error."); - bytesin = -1; - } + { + log_message (LOG_ERR, "readbuff: add_to_buffer() error."); + bytesin = -1; + } } else { if (bytesin == 0) - { - /* connection was closed by client */ - bytesin = -1; - } + { + /* connection was closed by client */ + bytesin = -1; + } else - { - switch (errno) - { + { + switch (errno) + { #ifdef EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #else # ifdef EAGAIN - case EAGAIN: + case EAGAIN: # endif #endif - case EINTR: - bytesin = 0; - break; - default: - log_message (LOG_ERR, - "readbuff: recv() error \"%s\" on file descriptor %d", - strerror (errno), fd); - bytesin = -1; - break; - } - } + case EINTR: + bytesin = 0; + break; + default: + log_message (LOG_ERR, + "readbuff: recv() error \"%s\" on file descriptor %d", + strerror (errno), fd); + bytesin = -1; + break; + } + } } safefree (buffer); @@ -306,41 +306,40 @@ write_buffer (int fd, struct buffer_s * buffptr) bytessent = send (fd, line->string + line->pos, line->length - line->pos, - MSG_NOSIGNAL); + MSG_NOSIGNAL); if (bytessent >= 0) { /* bytes sent, adjust buffer */ line->pos += bytessent; if (line->pos == line->length) - free_line (remove_from_buffer (buffptr)); + free_line (remove_from_buffer (buffptr)); return bytessent; } else { switch (errno) - { + { #ifdef EWOULDBLOCK - case EWOULDBLOCK: + case EWOULDBLOCK: #else # ifdef EAGAIN - case EAGAIN: + case EAGAIN: # endif #endif - case EINTR: - return 0; - case ENOBUFS: - case ENOMEM: - log_message (LOG_ERR, - "writebuff: write() error [NOBUFS/NOMEM] \"%s\" on " - "file descriptor %d", - strerror (errno), fd); - return 0; - default: - log_message (LOG_ERR, - "writebuff: write() error \"%s\" on file descriptor %d", - strerror (errno), fd); - return -1; - } + case EINTR: + return 0; + case ENOBUFS: + case ENOMEM: + log_message (LOG_ERR, + "writebuff: write() error [NOBUFS/NOMEM] \"%s\" on " + "file descriptor %d", strerror (errno), fd); + return 0; + default: + log_message (LOG_ERR, + "writebuff: write() error \"%s\" on file descriptor %d", + strerror (errno), fd); + return -1; + } } } diff --git a/src/buffer.h b/src/buffer.h index ff1167f..67571e0 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -32,7 +32,7 @@ extern size_t buffer_size (struct buffer_s *buffptr); * Add a new line to the given buffer. The data IS copied into the structure. */ extern int add_to_buffer (struct buffer_s *buffptr, unsigned char *data, - size_t length); + size_t length); extern ssize_t read_buffer (int fd, struct buffer_s *buffptr); extern ssize_t write_buffer (int fd, struct buffer_s *buffptr); diff --git a/src/child.c b/src/child.c index a2735d1..716743d 100644 --- a/src/child.c +++ b/src/child.c @@ -58,7 +58,7 @@ static struct child_config_s int maxspareservers, minspareservers, startservers; } child_config; -static unsigned int *servers_waiting; /* servers waiting for a connection */ +static unsigned int *servers_waiting; /* servers waiting for a connection */ /* * Lock/Unlock the "servers_waiting" variable so that two children cannot @@ -108,9 +108,9 @@ _child_lock_wait (void) while ((rc = fcntl (lock_fd, F_SETLKW, &lock_it)) < 0) { if (errno == EINTR) - continue; + continue; else - return; + return; } } @@ -202,26 +202,26 @@ child_main (struct child_s *ptr) * want to use the GDB debugger. */ if (getenv ("TINYPROXY_DEBUG")) - { - /* Pause for 10 seconds to allow us to connect debugger */ - fprintf (stderr, - "Process has accepted connection: %ld\n", - (long int) ptr->tid); - sleep (10); - fprintf (stderr, "Continuing process: %ld\n", (long int) ptr->tid); - } + { + /* Pause for 10 seconds to allow us to connect debugger */ + fprintf (stderr, + "Process has accepted connection: %ld\n", + (long int) ptr->tid); + sleep (10); + fprintf (stderr, "Continuing process: %ld\n", (long int) ptr->tid); + } #endif /* * Make sure no error occurred... */ if (connfd < 0) - { - log_message (LOG_ERR, - "Accept returned an error (%s) ... retrying.", - strerror (errno)); - continue; - } + { + log_message (LOG_ERR, + "Accept returned an error (%s) ... retrying.", + strerror (errno)); + continue; + } ptr->status = T_CONNECTED; @@ -231,38 +231,37 @@ child_main (struct child_s *ptr) ptr->connects++; if (child_config.maxrequestsperchild != 0) - { - DEBUG2 ("%u connections so far...", ptr->connects); - - if (ptr->connects == child_config.maxrequestsperchild) - { - log_message (LOG_NOTICE, - "Child has reached MaxRequestsPerChild (%u). " - "Killing child.", - ptr->connects); - break; - } - } + { + DEBUG2 ("%u connections so far...", ptr->connects); + + if (ptr->connects == child_config.maxrequestsperchild) + { + log_message (LOG_NOTICE, + "Child has reached MaxRequestsPerChild (%u). " + "Killing child.", ptr->connects); + break; + } + } SERVER_COUNT_LOCK (); if (*servers_waiting > child_config.maxspareservers) - { - /* - * There are too many spare children, kill ourself - * off. - */ - log_message (LOG_NOTICE, - "Waiting servers (%d) exceeds MaxSpareServers (%d). " - "Killing child.", - *servers_waiting, child_config.maxspareservers); - SERVER_COUNT_UNLOCK (); - - break; - } + { + /* + * There are too many spare children, kill ourself + * off. + */ + log_message (LOG_NOTICE, + "Waiting servers (%d) exceeds MaxSpareServers (%d). " + "Killing child.", + *servers_waiting, child_config.maxspareservers); + SERVER_COUNT_UNLOCK (); + + break; + } else - { - SERVER_COUNT_UNLOCK (); - } + { + SERVER_COUNT_UNLOCK (); + } SERVER_INC (); } @@ -283,7 +282,7 @@ child_make (struct child_s *ptr) pid_t pid; if ((pid = fork ()) > 0) - return pid; /* parent */ + return pid; /* parent */ /* * Reset the SIGNALS so that the child can be reaped. @@ -292,7 +291,7 @@ child_make (struct child_s *ptr) set_signal_handler (SIGTERM, SIG_DFL); set_signal_handler (SIGHUP, SIG_DFL); - child_main (ptr); /* never returns */ + child_main (ptr); /* never returns */ return -1; } @@ -312,20 +311,20 @@ child_pool_create (void) if (child_config.maxclients == 0) { log_message (LOG_ERR, - "child_pool_create: \"MaxClients\" must be " - "greater than zero."); + "child_pool_create: \"MaxClients\" must be " + "greater than zero."); return -1; } if (child_config.startservers == 0) { log_message (LOG_ERR, - "child_pool_create: \"StartServers\" must be " - "greater than zero."); + "child_pool_create: \"StartServers\" must be " + "greater than zero."); return -1; } child_ptr = calloc_shared_memory (child_config.maxclients, - sizeof (struct child_s)); + sizeof (struct child_s)); if (!child_ptr) { log_message (LOG_ERR, "Could not allocate memory for children."); @@ -349,9 +348,8 @@ child_pool_create (void) if (child_config.startservers > child_config.maxclients) { log_message (LOG_WARNING, - "Can not start more than \"MaxClients\" servers. " - "Starting %u servers instead.", - child_config.maxclients); + "Can not start more than \"MaxClients\" servers. " + "Starting %u servers instead.", child_config.maxclients); child_config.startservers = child_config.maxclients; } @@ -364,25 +362,25 @@ child_pool_create (void) for (i = 0; i != child_config.startservers; i++) { DEBUG2 ("Trying to create child %d of %d", i + 1, - child_config.startservers); + child_config.startservers); child_ptr[i].status = T_WAITING; child_ptr[i].tid = child_make (&child_ptr[i]); if (child_ptr[i].tid < 0) - { - log_message (LOG_WARNING, - "Could not create child number %d of %d", - i, child_config.startservers); - return -1; - } + { + log_message (LOG_WARNING, + "Could not create child number %d of %d", + i, child_config.startservers); + return -1; + } else - { - log_message (LOG_INFO, - "Creating child number %d of %d ...", - i + 1, child_config.startservers); + { + log_message (LOG_INFO, + "Creating child number %d of %d ...", + i + 1, child_config.startservers); - SERVER_INC (); - } + SERVER_INC (); + } } log_message (LOG_INFO, "Finished creating all children."); @@ -402,62 +400,62 @@ child_main_loop (void) while (1) { if (config.quit) - return; + return; /* If there are not enough spare servers, create more */ SERVER_COUNT_LOCK (); if (*servers_waiting < child_config.minspareservers) - { - log_message (LOG_NOTICE, - "Waiting servers (%d) is less than MinSpareServers (%d). " - "Creating new child.", - *servers_waiting, child_config.minspareservers); - - SERVER_COUNT_UNLOCK (); - - for (i = 0; i != child_config.maxclients; i++) - { - if (child_ptr[i].status == T_EMPTY) - { - child_ptr[i].status = T_WAITING; - child_ptr[i].tid = child_make (&child_ptr[i]); - if (child_ptr[i].tid < 0) - { - log_message (LOG_NOTICE, "Could not create child"); - - child_ptr[i].status = T_EMPTY; - break; - } - - SERVER_INC (); - - break; - } - } - } + { + log_message (LOG_NOTICE, + "Waiting servers (%d) is less than MinSpareServers (%d). " + "Creating new child.", + *servers_waiting, child_config.minspareservers); + + SERVER_COUNT_UNLOCK (); + + for (i = 0; i != child_config.maxclients; i++) + { + if (child_ptr[i].status == T_EMPTY) + { + child_ptr[i].status = T_WAITING; + child_ptr[i].tid = child_make (&child_ptr[i]); + if (child_ptr[i].tid < 0) + { + log_message (LOG_NOTICE, "Could not create child"); + + child_ptr[i].status = T_EMPTY; + break; + } + + SERVER_INC (); + + break; + } + } + } else - { - SERVER_COUNT_UNLOCK (); - } + { + SERVER_COUNT_UNLOCK (); + } sleep (5); /* Handle log rotation if it was requested */ if (received_sighup) - { - truncate_log_file (); + { + truncate_log_file (); #ifdef FILTER_ENABLE - if (config.filter) - { - filter_destroy (); - filter_init (); - } - log_message (LOG_NOTICE, "Re-reading filter file."); + if (config.filter) + { + filter_destroy (); + filter_init (); + } + log_message (LOG_NOTICE, "Re-reading filter file."); #endif /* FILTER_ENABLE */ - received_sighup = FALSE; - } + received_sighup = FALSE; + } } } @@ -472,7 +470,7 @@ child_kill_children (void) for (i = 0; i != child_config.maxclients; i++) { if (child_ptr[i].status != T_EMPTY) - kill (child_ptr[i].tid, SIGTERM); + kill (child_ptr[i].tid, SIGTERM); } } diff --git a/src/common.h b/src/common.h index ab71322..551d0f9 100644 --- a/src/common.h +++ b/src/common.h @@ -173,13 +173,13 @@ # define MSG_NOSIGNAL (0) #endif -#ifndef SHUT_RD /* these three Posix.1g names are quite new */ -# define SHUT_RD 0 /* shutdown for reading */ -# define SHUT_WR 1 /* shutdown for writing */ -# define SHUT_RDWR 2 /* shutdown for reading and writing */ +#ifndef SHUT_RD /* these three Posix.1g names are quite new */ +# define SHUT_RD 0 /* shutdown for reading */ +# define SHUT_WR 1 /* shutdown for writing */ +# define SHUT_RDWR 2 /* shutdown for reading and writing */ #endif -#define MAXLISTEN 1024 /* Max number of connections */ +#define MAXLISTEN 1024 /* Max number of connections */ /* * SunOS doesn't have INADDR_NONE defined. diff --git a/src/conffile.c b/src/conffile.c index c123293..6510555 100644 --- a/src/conffile.c +++ b/src/conffile.c @@ -67,7 +67,7 @@ * with the same function template as below. */ typedef int (*CONFFILE_HANDLER) (struct config_s *, const char *, - regmatch_t[]); + regmatch_t[]); /* * Define the pattern used by any directive handling function. The @@ -92,7 +92,7 @@ static HANDLE_FUNC (handle_nop) { return 0; -} /* do nothing function */ +} /* do nothing function */ static HANDLE_FUNC (handle_allow); static HANDLE_FUNC (handle_anonymous); @@ -228,7 +228,7 @@ struct #endif /* loglevel */ STDCONF ("loglevel", "(critical|error|warning|notice|connect|info)", - handle_loglevel)}; + handle_loglevel)}; const unsigned int ndirectives = sizeof (directives) / sizeof (directives[0]); /* @@ -249,12 +249,12 @@ config_compile (void) directives[i].cre = safemalloc (sizeof (regex_t)); if (!directives[i].cre) - return -1; + return -1; r = regcomp (directives[i].cre, - directives[i].re, REG_EXTENDED | REG_ICASE | REG_NEWLINE); + directives[i].re, REG_EXTENDED | REG_ICASE | REG_NEWLINE); if (r) - return r; + return r; } return 0; } @@ -279,7 +279,7 @@ check_match (struct config_s *conf, const char *line) { assert (directives[i].cre); if (!regexec (directives[i].cre, line, RE_MAX_MATCHES, match, 0)) - return (*directives[i].handler) (conf, line, match); + return (*directives[i].handler) (conf, line, match); } return -1; @@ -291,16 +291,16 @@ check_match (struct config_s *conf, const char *line) int config_parse (struct config_s *conf, FILE * f) { - char buffer[1024]; /* 1KB lines should be plenty */ + char buffer[1024]; /* 1KB lines should be plenty */ unsigned long lineno = 1; while (fgets (buffer, sizeof (buffer), f)) { if (check_match (conf, buffer)) - { - printf ("Syntax error on line %ld\n", lineno); - return 1; - } + { + printf ("Syntax error on line %ld\n", lineno); + return 1; + } ++lineno; } return 0; @@ -440,7 +440,7 @@ HANDLE_FUNC (handle_viaproxyname) if (r) return r; log_message (LOG_INFO, - "Setting \"Via\" header proxy to %s", conf->via_proxy_name); + "Setting \"Via\" header proxy to %s", conf->via_proxy_name); return 0; } @@ -474,7 +474,7 @@ HANDLE_FUNC (handle_xtinyproxy) return set_string_arg (&conf->my_domain, line, &match[2]); #else fprintf (stderr, - "XTinyproxy NOT Enabled! Recompile with --enable-xtinyproxy\n"); + "XTinyproxy NOT Enabled! Recompile with --enable-xtinyproxy\n"); return 1; #endif } @@ -596,11 +596,11 @@ HANDLE_FUNC (handle_bind) if (r) return r; log_message (LOG_INFO, - "Outgoing connections bound to IP %s", conf->bind_address); + "Outgoing connections bound to IP %s", conf->bind_address); return 0; #else fprintf (stderr, - "\"Bind\" cannot be used with transparent support enabled.\n"); + "\"Bind\" cannot be used with transparent support enabled.\n"); return 1; #endif } @@ -663,11 +663,11 @@ HANDLE_FUNC (handle_loglevel) for (i = 0; i != nlevels; ++i) { if (!strcasecmp (arg, log_levels[i].string)) - { - set_log_level (log_levels[i].level); - safefree (arg); - return 0; - } + { + set_log_level (log_levels[i].level); + safefree (arg); + return 0; + } } safefree (arg); @@ -745,10 +745,10 @@ HANDLE_FUNC (handle_reversepath) { arg2 = get_string_arg (line, &match[3]); if (!arg2) - { - safefree (arg1); - return -1; - } + { + safefree (arg1); + return -1; + } reversepath_add (arg1, arg2); safefree (arg1); safefree (arg2); @@ -779,10 +779,10 @@ HANDLE_FUNC (handle_upstream) { domain = get_string_arg (line, &match[9]); if (domain) - { - upstream_add (ip, port, domain); - safefree (domain); - } + { + upstream_add (ip, port, domain); + safefree (domain); + } } else { diff --git a/src/conns.c b/src/conns.c index 6c04383..433f59a 100644 --- a/src/conns.c +++ b/src/conns.c @@ -32,7 +32,7 @@ struct conn_s * initialize_conn (int client_fd, const char *ipaddr, const char *string_addr, - const char *sock_ipaddr) + const char *sock_ipaddr) { struct conn_s *connptr; struct buffer_s *cbuffer, *sbuffer; @@ -110,11 +110,11 @@ destroy_conn (struct conn_s *connptr) if (connptr->client_fd != -1) if (close (connptr->client_fd) < 0) log_message (LOG_INFO, "Client (%d) close message: %s", - connptr->client_fd, strerror (errno)); + connptr->client_fd, strerror (errno)); if (connptr->server_fd != -1) if (close (connptr->server_fd) < 0) log_message (LOG_INFO, "Server (%d) close message: %s", - connptr->server_fd, strerror (errno)); + connptr->server_fd, strerror (errno)); if (connptr->cbuffer) delete_buffer (connptr->cbuffer); diff --git a/src/conns.h b/src/conns.h index 6c2f365..ae1fcaa 100644 --- a/src/conns.h +++ b/src/conns.h @@ -95,8 +95,8 @@ struct conn_s * Functions for the creation and destruction of a connection structure. */ extern struct conn_s *initialize_conn (int client_fd, const char *ipaddr, - const char *string_addr, - const char *sock_ipaddr); + const char *string_addr, + const char *sock_ipaddr); extern void destroy_conn (struct conn_s *connptr); #endif diff --git a/src/daemon.c b/src/daemon.c index fc39b42..9950842 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -70,13 +70,13 @@ set_signal_handler (int signo, signal_func * func) if (signo == SIGALRM) { #ifdef SA_INTERRUPT - act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */ + act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */ #endif } else { #ifdef SA_RESTART - act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */ + act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */ #endif } diff --git a/src/filter.c b/src/filter.c index 06346c3..c4415b2 100644 --- a/src/filter.c +++ b/src/filter.c @@ -60,75 +60,75 @@ filter_init (void) { fd = fopen (config.filter, "r"); if (fd) - { - p = NULL; - - cflags = REG_NEWLINE | REG_NOSUB; - if (config.filter_extended) - cflags |= REG_EXTENDED; - if (!config.filter_casesensitive) - cflags |= REG_ICASE; - - while (fgets (buf, FILTER_BUFFER_LEN, fd)) - { - /* - * Remove any trailing white space and - * comments. - */ - s = buf; - while (*s) - { - if (isspace ((unsigned char) *s)) - break; - if (*s == '#') - { - /* - * If the '#' char is preceeded by - * an escape, it's not a comment - * string. - */ - if (s == buf || *(s - 1) != '\\') - break; - } - ++s; - } - *s = '\0'; - - /* skip leading whitespace */ - s = buf; - while (*s && isspace ((unsigned char) *s)) - s++; - - /* skip blank lines and comments */ - if (*s == '\0') - continue; - - if (!p) /* head of list */ - fl = p = safecalloc (1, sizeof (struct filter_list)); - else - { /* next entry */ - p->next = safecalloc (1, sizeof (struct filter_list)); - p = p->next; - } - - p->pat = safestrdup (s); - p->cpat = safemalloc (sizeof (regex_t)); - if ((err = regcomp (p->cpat, p->pat, cflags)) != 0) - { - fprintf (stderr, "Bad regex in %s: %s\n", - config.filter, p->pat); - exit (EX_DATAERR); - } - } - if (ferror (fd)) - { - perror ("fgets"); - exit (EX_DATAERR); - } - fclose (fd); - - already_init = 1; - } + { + p = NULL; + + cflags = REG_NEWLINE | REG_NOSUB; + if (config.filter_extended) + cflags |= REG_EXTENDED; + if (!config.filter_casesensitive) + cflags |= REG_ICASE; + + while (fgets (buf, FILTER_BUFFER_LEN, fd)) + { + /* + * Remove any trailing white space and + * comments. + */ + s = buf; + while (*s) + { + if (isspace ((unsigned char) *s)) + break; + if (*s == '#') + { + /* + * If the '#' char is preceeded by + * an escape, it's not a comment + * string. + */ + if (s == buf || *(s - 1) != '\\') + break; + } + ++s; + } + *s = '\0'; + + /* skip leading whitespace */ + s = buf; + while (*s && isspace ((unsigned char) *s)) + s++; + + /* skip blank lines and comments */ + if (*s == '\0') + continue; + + if (!p) /* head of list */ + fl = p = safecalloc (1, sizeof (struct filter_list)); + else + { /* next entry */ + p->next = safecalloc (1, sizeof (struct filter_list)); + p = p->next; + } + + p->pat = safestrdup (s); + p->cpat = safemalloc (sizeof (regex_t)); + if ((err = regcomp (p->cpat, p->pat, cflags)) != 0) + { + fprintf (stderr, "Bad regex in %s: %s\n", + config.filter, p->pat); + exit (EX_DATAERR); + } + } + if (ferror (fd)) + { + perror ("fgets"); + exit (EX_DATAERR); + } + fclose (fd); + + already_init = 1; + } } } @@ -141,13 +141,13 @@ filter_destroy (void) if (already_init) { for (p = q = fl; p; p = q) - { - regfree (p->cpat); - safefree (p->cpat); - safefree (p->pat); - q = p->next; - safefree (p); - } + { + regfree (p->cpat); + safefree (p->cpat); + safefree (p->pat); + q = p->next; + safefree (p); + } fl = NULL; already_init = 0; } @@ -168,12 +168,12 @@ filter_domain (const char *host) result = regexec (p->cpat, host, (size_t) 0, (regmatch_t *) 0, 0); if (result == 0) - { - if (default_policy == FILTER_DEFAULT_ALLOW) - return 1; - else - return 0; - } + { + if (default_policy == FILTER_DEFAULT_ALLOW) + return 1; + else + return 0; + } } COMMON_EXIT: @@ -198,12 +198,12 @@ filter_url (const char *url) result = regexec (p->cpat, url, (size_t) 0, (regmatch_t *) 0, 0); if (result == 0) - { - if (default_policy == FILTER_DEFAULT_ALLOW) - return 1; - else - return 0; - } + { + if (default_policy == FILTER_DEFAULT_ALLOW) + return 1; + else + return 0; + } } COMMON_EXIT: diff --git a/src/hashmap.c b/src/hashmap.c index 764c9a6..f8b181d 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -172,9 +172,9 @@ hashmap_delete (hashmap_t map) for (i = 0; i != map->size; i++) { if (map->buckets[i].head != NULL) - { - delete_hashbucket (&map->buckets[i]); - } + { + delete_hashbucket (&map->buckets[i]); + } } safefree (map->buckets); @@ -329,16 +329,16 @@ hashmap_find (hashmap_t map, const char *key) ptr = map->buckets[i].head; while (ptr) - { - if (strcasecmp (ptr->key, key) == 0) - { - /* Found it, so return the current count */ - return iter; - } - - iter++; - ptr = ptr->next; - } + { + if (strcasecmp (ptr->key, key) == 0) + { + /* Found it, so return the current count */ + return iter; + } + + iter++; + ptr = ptr->next; + } } return iter; @@ -352,7 +352,7 @@ hashmap_find (hashmap_t map, const char *key) */ ssize_t hashmap_return_entry (hashmap_t map, hashmap_iter iter, char **key, - void **data) + void **data) { unsigned int i; struct hashentry_s *ptr; @@ -371,18 +371,18 @@ hashmap_return_entry (hashmap_t map, hashmap_iter iter, char **key, { ptr = map->buckets[i].head; while (ptr) - { - if (count == iter) - { - /* This is the data so return it */ - *key = ptr->key; - *data = ptr->data; - return ptr->len; - } - - ptr = ptr->next; - count++; - } + { + if (count == iter) + { + /* This is the data so return it */ + *key = ptr->key; + *data = ptr->data; + return ptr->len; + } + + ptr = ptr->next; + count++; + } } return -EFAULT; @@ -415,7 +415,7 @@ hashmap_search (hashmap_t map, const char *key) while (ptr) { if (strcasecmp (ptr->key, key) == 0) - ++count; + ++count; /* This entry didn't contain the key; move to the next one */ ptr = ptr->next; @@ -450,10 +450,10 @@ hashmap_entry_by_key (hashmap_t map, const char *key, void **data) while (ptr) { if (strcasecmp (ptr->key, key) == 0) - { - *data = ptr->data; - return ptr->len; - } + { + *data = ptr->data; + return ptr->len; + } ptr = ptr->next; } @@ -487,33 +487,33 @@ hashmap_remove (hashmap_t map, const char *key) while (ptr) { if (strcasecmp (ptr->key, key) == 0) - { - /* - * Found the data, now need to remove everything - * and update the hashmap. - */ - next = ptr->next; - - if (ptr->prev) - ptr->prev->next = ptr->next; - if (ptr->next) - ptr->next->prev = ptr->prev; - - if (map->buckets[hash].head == ptr) - map->buckets[hash].head = ptr->next; - if (map->buckets[hash].tail == ptr) - map->buckets[hash].tail = ptr->prev; - - safefree (ptr->key); - safefree (ptr->data); - safefree (ptr); - - ++deleted; - --map->end_iterator; - - ptr = next; - continue; - } + { + /* + * Found the data, now need to remove everything + * and update the hashmap. + */ + next = ptr->next; + + if (ptr->prev) + ptr->prev->next = ptr->next; + if (ptr->next) + ptr->next->prev = ptr->prev; + + if (map->buckets[hash].head == ptr) + map->buckets[hash].head = ptr->next; + if (map->buckets[hash].tail == ptr) + map->buckets[hash].tail = ptr->prev; + + safefree (ptr->key); + safefree (ptr->data); + safefree (ptr); + + ++deleted; + --map->end_iterator; + + ptr = next; + continue; + } /* This entry didn't contain the key; move to the next one */ ptr = ptr->next; diff --git a/src/hashmap.h b/src/hashmap.h index 946b619..9563277 100644 --- a/src/hashmap.h +++ b/src/hashmap.h @@ -52,7 +52,7 @@ extern "C" * 0 upon successful insert */ extern int hashmap_insert (hashmap_t map, const char *key, - const void *data, size_t len); + const void *data, size_t len); /* * Get an iterator to the first entry. @@ -88,7 +88,7 @@ extern "C" * negative upon error */ extern ssize_t hashmap_return_entry (hashmap_t map, hashmap_iter iter, - char **key, void **data); + char **key, void **data); /* * Get the first entry (assuming there is more than one) for a particular @@ -99,7 +99,7 @@ extern "C" * length of data for the entry */ extern ssize_t hashmap_entry_by_key (hashmap_t map, const char *key, - void **data); + void **data); /* * Searches for _any_ occurrances of "key" within the hashmap and returns the @@ -123,5 +123,5 @@ extern "C" #if defined(__cplusplus) } -#endif /* C++ */ -#endif /* _HASHMAP_H */ +#endif /* C++ */ +#endif /* _HASHMAP_H */ @@ -29,7 +29,7 @@ void * debugging_calloc (size_t nmemb, size_t size, const char *file, - unsigned long line) + unsigned long line) { void *ptr; @@ -38,7 +38,7 @@ debugging_calloc (size_t nmemb, size_t size, const char *file, ptr = calloc (nmemb, size); fprintf (stderr, "{calloc: %p:%zu x %zu} %s:%lu\n", ptr, nmemb, size, file, - line); + line); return ptr; } @@ -56,7 +56,7 @@ debugging_malloc (size_t size, const char *file, unsigned long line) void * debugging_realloc (void *ptr, size_t size, const char *file, - unsigned long line) + unsigned long line) { void *newptr; @@ -64,7 +64,7 @@ debugging_realloc (void *ptr, size_t size, const char *file, newptr = realloc (ptr, size); fprintf (stderr, "{realloc: %p -> %p:%zu} %s:%lu\n", ptr, newptr, size, - file, line); + file, line); return newptr; } @@ -27,14 +27,14 @@ #ifndef NDEBUG extern void *debugging_calloc (size_t nmemb, size_t size, const char *file, - unsigned long line); + unsigned long line); extern void *debugging_malloc (size_t size, const char *file, - unsigned long line); + unsigned long line); extern void debugging_free (void *ptr, const char *file, unsigned long line); extern void *debugging_realloc (void *ptr, size_t size, const char *file, - unsigned long line); + unsigned long line); extern char *debugging_strdup (const char *s, const char *file, - unsigned long line); + unsigned long line); # define safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__) # define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__) diff --git a/src/html-error.c b/src/html-error.c index f5a8003..8c95469 100644 --- a/src/html-error.c +++ b/src/html-error.c @@ -33,7 +33,7 @@ /* * Add an error number -> filename mapping to the errorpages list. */ -#define ERRORNUM_BUFSIZE 8 /* this is more than required */ +#define ERRORNUM_BUFSIZE 8 /* this is more than required */ #define ERRPAGES_BUCKETCOUNT 16 int @@ -48,7 +48,7 @@ add_new_errorpage (char *filepath, unsigned int errornum) snprintf (errornbuf, ERRORNUM_BUFSIZE, "%u", errornum); if (hashmap_insert (config.errorpages, errornbuf, - filepath, strlen (filepath) + 1) < 0) + filepath, strlen (filepath) + 1) < 0) return (-1); return (0); @@ -78,7 +78,7 @@ get_html_file (unsigned int errornum) return (config.errorpage_undef); if (hashmap_return_entry (config.errorpages, result_iter, - &key, (void **) &val) < 0) + &key, (void **) &val) < 0) return (config.errorpage_undef); return (val); @@ -100,7 +100,7 @@ lookup_variable (struct conn_s *connptr, char *varname) return (NULL); if (hashmap_return_entry (connptr->error_variables, result_iter, - &key, (void **) &data) < 0) + &key, (void **) &data) < 0) return (NULL); return (data); @@ -121,51 +121,51 @@ send_html_file (FILE * infile, struct conn_s *connptr) while (fgets (inbuf, HTML_BUFSIZE, infile) != NULL) { for (p = inbuf; *p; p++) - { - switch (*p) - { - case '}': - if (in_variable) - { - *p = '\0'; - if (!(varval = lookup_variable (connptr, varstart))) - varval = "(unknown)"; - writeret = write_message (connptr->client_fd, "%s", varval); - if (writeret) - return (writeret); - in_variable = 0; - } - else - { - writeret = write_message (connptr->client_fd, "%c", *p); - if (writeret) - return (writeret); - } - break; - case '{': - /* a {{ will print a single {. If we are NOT - * already in a { variable, then proceed with - * setup. If we ARE already in a { variable, - * this code will fallthrough to the code that - * just dumps a character to the client fd. - */ - if (!in_variable) - { - varstart = p + 1; - in_variable++; - } - else - in_variable = 0; - default: - if (!in_variable) - { - writeret = write_message (connptr->client_fd, "%c", *p); - if (writeret) - return (writeret); - } - - } - } + { + switch (*p) + { + case '}': + if (in_variable) + { + *p = '\0'; + if (!(varval = lookup_variable (connptr, varstart))) + varval = "(unknown)"; + writeret = write_message (connptr->client_fd, "%s", varval); + if (writeret) + return (writeret); + in_variable = 0; + } + else + { + writeret = write_message (connptr->client_fd, "%c", *p); + if (writeret) + return (writeret); + } + break; + case '{': + /* a {{ will print a single {. If we are NOT + * already in a { variable, then proceed with + * setup. If we ARE already in a { variable, + * this code will fallthrough to the code that + * just dumps a character to the client fd. + */ + if (!in_variable) + { + varstart = p + 1; + in_variable++; + } + else + in_variable = 0; + default: + if (!in_variable) + { + writeret = write_message (connptr->client_fd, "%c", *p); + if (writeret) + return (writeret); + } + + } + } in_variable = 0; } return (0); @@ -180,7 +180,7 @@ send_http_headers (struct conn_s *connptr, int code, char *message) "Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n"; return (write_message (connptr->client_fd, headers, - code, message, PACKAGE, VERSION)); + code, message, PACKAGE, VERSION)); } /* @@ -211,10 +211,10 @@ send_http_error_message (struct conn_s *connptr) { char *detail = lookup_variable (connptr, "detail"); return (write_message (connptr->client_fd, fallback_error, - connptr->error_number, - connptr->error_string, - connptr->error_string, - detail, PACKAGE, VERSION)); + connptr->error_number, + connptr->error_string, + connptr->error_string, + detail, PACKAGE, VERSION)); } ret = send_html_file (infile, connptr); @@ -236,7 +236,7 @@ add_error_variable (struct conn_s *connptr, char *key, char *val) return (-1); return hashmap_insert (connptr->error_variables, key, val, - strlen (val) + 1); + strlen (val) + 1); } #define ADD_VAR_RET(x, y) \ @@ -272,7 +272,7 @@ add_standard_vars (struct conn_s *connptr) global_time = time (NULL); strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT", - gmtime (&global_time)); + gmtime (&global_time)); add_error_variable (connptr, "date", timebuf); add_error_variable (connptr, "website", "http://www.banu.com/tinyproxy/"); @@ -298,10 +298,10 @@ indicate_http_error (struct conn_s *connptr, int number, char *message, ...) val = va_arg (ap, char *); if (add_error_variable (connptr, key, val) == -1) - { - va_end (ap); - return (-1); - } + { + va_end (ap); + return (-1); + } } connptr->error_number = number; diff --git a/src/html-error.h b/src/html-error.h index 280101c..b1ace83 100644 --- a/src/html-error.h +++ b/src/html-error.h @@ -27,11 +27,11 @@ struct conn_s; extern int add_new_errorpage (char *filepath, unsigned int errornum); extern int send_http_error_message (struct conn_s *connptr); extern int indicate_http_error (struct conn_s *connptr, int number, - char *message, ...); + char *message, ...); extern int add_error_variable (struct conn_s *connptr, char *key, char *val); extern int send_html_file (FILE * infile, struct conn_s *connptr); extern int send_http_headers (struct conn_s *connptr, int code, - char *message); + char *message); extern int add_standard_vars (struct conn_s *connptr); #endif /* !TINYPROXY_HTML_ERROR_H */ diff --git a/src/http-message.c b/src/http-message.c index 2cd5a4d..d6ab81e 100644 --- a/src/http-message.c +++ b/src/http-message.c @@ -144,7 +144,7 @@ http_message_destroy (http_message_t msg) */ int http_message_set_response (http_message_t msg, - int response_code, const char *response_string) + int response_code, const char *response_string) { /* Check for valid arguments */ if (msg == NULL) @@ -207,11 +207,11 @@ http_message_add_headers (http_message_t msg, char **headers, int num_headers) { new_headers = safecalloc (msg->headers.total * 2, sizeof (char *)); if (new_headers == NULL) - return -ENOMEM; + return -ENOMEM; /* Copy the array */ for (i = 0; i != msg->headers.used; ++i) - new_headers[i] = msg->headers.strings[i]; + new_headers[i] = msg->headers.strings[i]; /* Remove the old array and replace it with the new array */ safefree (msg->headers.strings); @@ -251,7 +251,7 @@ http_message_send (http_message_t msg, int fd) /* Write the response line */ write_message (fd, "HTTP/1.0 %d %s\r\n", - msg->response.code, msg->response.string); + msg->response.code, msg->response.string); /* Go through all the headers */ for (i = 0; i != msg->headers.used; ++i) @@ -260,7 +260,7 @@ http_message_send (http_message_t msg, int fd) /* Output the date */ global_time = time (NULL); strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT", - gmtime (&global_time)); + gmtime (&global_time)); write_message (fd, "Date: %s\r\n", timebuf); /* Output the content-length */ diff --git a/src/http-message.h b/src/http-message.h index b83df11..d36146f 100644 --- a/src/http-message.h +++ b/src/http-message.h @@ -59,7 +59,7 @@ typedef struct http_message_s *http_message_t; /* Initialize the internal structure of the HTTP message */ extern http_message_t http_message_create (int response_code, - const char *response_string); + const char *response_string); /* Free up an _internal_ resources */ extern int http_message_destroy (http_message_t msg); @@ -76,10 +76,10 @@ extern int http_message_send (http_message_t msg, int fd); * add a new set of headers. */ extern int http_message_set_body (http_message_t msg, - const char *body, size_t len); + const char *body, size_t len); extern int http_message_set_response (http_message_t msg, - int response_code, - const char *response_string); + int response_code, + const char *response_string); /* * Set the headers for this HTTP message. Each string must be NUL ('\0') @@ -88,6 +88,6 @@ extern int http_message_set_response (http_message_t msg, * sent. */ extern int http_message_add_headers (http_message_t msg, - char **headers, int num_headers); + char **headers, int num_headers); #endif /* _TINYPROXY_HTTP_MESSAGE_H_ */ @@ -118,12 +118,12 @@ log_message (int level, char *fmt, ...) if (log_level == LOG_CONN) { if (level == LOG_INFO) - return; + return; } else if (log_level == LOG_INFO) { if (level > LOG_INFO && level != LOG_CONN) - return; + return; } else if (level > log_level) return; @@ -145,21 +145,21 @@ log_message (int level, char *fmt, ...) char *entry_buffer; if (!log_message_storage) - { - log_message_storage = vector_create (); - if (!log_message_storage) - goto out; - } + { + log_message_storage = vector_create (); + if (!log_message_storage) + goto out; + } vsnprintf (str, STRING_LENGTH, fmt, args); entry_buffer = safemalloc (strlen (str) + 6); if (!entry_buffer) - goto out; + goto out; sprintf (entry_buffer, "%d %s", level, str); vector_append (log_message_storage, entry_buffer, - strlen (entry_buffer) + 1); + strlen (entry_buffer) + 1); safefree (entry_buffer); goto out; @@ -180,10 +180,10 @@ log_message (int level, char *fmt, ...) nowtime = time (NULL); /* Format is month day hour:minute:second (24 time) */ strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S", - localtime (&nowtime)); + localtime (&nowtime)); snprintf (str, STRING_LENGTH, "%-9s %s [%ld]: ", - syslog_level[level], time_string, (long int) getpid ()); + syslog_level[level], time_string, (long int) getpid ()); assert (log_file_fd >= 0); @@ -223,14 +223,14 @@ send_stored_logs (void) #ifdef NDEBUG if (log_level == LOG_CONN && level == LOG_INFO) - continue; + continue; else if (log_level == LOG_INFO) - { - if (level > LOG_INFO && level != LOG_CONN) - continue; - } + { + if (level > LOG_INFO && level != LOG_CONN) + continue; + } else if (level > log_level) - continue; + continue; #endif log_message (level, ptr); @@ -87,7 +87,7 @@ # define LOG_DEBUG 7 #endif -#define LOG_CONN 8 /* extra to log connections without the INFO stuff */ +#define LOG_CONN 8 /* extra to log connections without the INFO stuff */ /* * Use this for debugging. The format is specific: diff --git a/src/network.c b/src/network.c index 7b4bb52..fe65a15 100644 --- a/src/network.c +++ b/src/network.c @@ -49,15 +49,15 @@ safe_write (int fd, const char *buffer, size_t count) len = send (fd, buffer, bytestosend, MSG_NOSIGNAL); if (len < 0) - { - if (errno == EINTR) - continue; - else - return -errno; - } + { + if (errno == EINTR) + continue; + else + return -errno; + } if (len == bytestosend) - break; + break; buffer += len; bytestosend -= len; @@ -94,7 +94,7 @@ int write_message (int fd, const char *fmt, ...) { ssize_t n; - size_t size = (1024 * 8); /* start with 8 KB and go from there */ + size_t size = (1024 * 8); /* start with 8 KB and go from there */ char *buf, *tmpbuf; va_list ap; @@ -109,23 +109,23 @@ write_message (int fd, const char *fmt, ...) /* If that worked, break out so we can send the buffer */ if (n > -1 && n < size) - break; + break; /* Else, try again with more space */ if (n > -1) - /* precisely what is needed (glibc2.1) */ - size = n + 1; + /* precisely what is needed (glibc2.1) */ + size = n + 1; else - /* twice the old size (glibc2.0) */ - size *= 2; + /* twice the old size (glibc2.0) */ + size *= 2; if ((tmpbuf = saferealloc (buf, size)) == NULL) - { - safefree (buf); - return -1; - } + { + safefree (buf); + return -1; + } else - buf = tmpbuf; + buf = tmpbuf; } if (safe_write (fd, buf, n) < 0) @@ -178,13 +178,13 @@ readline (int fd, char **whole_buffer) { ret = recv (fd, buffer, SEGMENT_LEN, MSG_PEEK); if (ret <= 0) - goto CLEANUP; + goto CLEANUP; ptr = memchr (buffer, '\n', ret); if (ptr) - diff = ptr - buffer + 1; + diff = ptr - buffer + 1; else - diff = ret; + diff = ret; whole_buffer_len += diff; @@ -193,33 +193,33 @@ readline (int fd, char **whole_buffer) * get to more than MAXIMUM_BUFFER_LENGTH close. */ if (whole_buffer_len > MAXIMUM_BUFFER_LENGTH) - { - ret = -ERANGE; - goto CLEANUP; - } + { + ret = -ERANGE; + goto CLEANUP; + } line_ptr->data = safemalloc (diff); if (!line_ptr->data) - { - ret = -ENOMEM; - goto CLEANUP; - } + { + ret = -ENOMEM; + goto CLEANUP; + } recv (fd, line_ptr->data, diff, 0); line_ptr->len = diff; if (ptr) - { - line_ptr->next = NULL; - break; - } + { + line_ptr->next = NULL; + break; + } line_ptr->next = safecalloc (sizeof (struct read_lines_s), 1); if (!line_ptr->next) - { - ret = -ENOMEM; - goto CLEANUP; - } + { + ret = -ENOMEM; + goto CLEANUP; + } line_ptr = line_ptr->next; } @@ -237,7 +237,7 @@ readline (int fd, char **whole_buffer) while (line_ptr) { memcpy (*whole_buffer + whole_buffer_len, line_ptr->data, - line_ptr->len); + line_ptr->len); whole_buffer_len += line_ptr->len; line_ptr = line_ptr->next; @@ -250,7 +250,7 @@ CLEANUP: { line_ptr = first_line->next; if (first_line->data) - safefree (first_line->data); + safefree (first_line->data); safefree (first_line); first_line = line_ptr; } @@ -269,23 +269,23 @@ get_ip_string (struct sockaddr *sa, char *buf, size_t buflen) assert (sa != NULL); assert (buf != NULL); assert (buflen != 0); - buf[0] = '\0'; /* start with an empty string */ + buf[0] = '\0'; /* start with an empty string */ switch (sa->sa_family) { case AF_INET: { - struct sockaddr_in *sa_in = (struct sockaddr_in *) sa; + struct sockaddr_in *sa_in = (struct sockaddr_in *) sa; - inet_ntop (AF_INET, &sa_in->sin_addr, buf, buflen); - break; + inet_ntop (AF_INET, &sa_in->sin_addr, buf, buflen); + break; } case AF_INET6: { - struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *) sa; + struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *) sa; - inet_ntop (AF_INET6, &sa_in6->sin6_addr, buf, buflen); - break; + inet_ntop (AF_INET6, &sa_in6->sin6_addr, buf, buflen); + break; } default: /* no valid family */ @@ -305,7 +305,7 @@ get_ip_string (struct sockaddr *sa, char *buf, size_t buflen) int full_inet_pton (const char *ip, void *dst) { - char buf[24], tmp[24]; /* IPv4->IPv6 = ::FFFF:xxx.xxx.xxx.xxx\0 */ + char buf[24], tmp[24]; /* IPv4->IPv6 = ::FFFF:xxx.xxx.xxx.xxx\0 */ int n; assert (ip != NULL && strlen (ip) != 0); @@ -339,6 +339,6 @@ full_inet_pton (const char *ip, void *dst) * full string. */ snprintf (buf, sizeof (buf), "::ffff:%s", - inet_ntop (AF_INET, dst, tmp, sizeof (tmp))); + inet_ntop (AF_INET, dst, tmp, sizeof (tmp))); return inet_pton (AF_INET6, buf, dst); } @@ -89,15 +89,15 @@ add_connect_port_allowed (int port) { ports_allowed_by_connect = vector_create (); if (!ports_allowed_by_connect) - { - log_message (LOG_WARNING, - "Could not create a list of allowed CONNECT ports"); - return; - } + { + log_message (LOG_WARNING, + "Could not create a list of allowed CONNECT ports"); + return; + } } log_message (LOG_INFO, "Adding Port [%d] to the list allowed by CONNECT", - port); + port); vector_append (ports_allowed_by_connect, (void **) &port, sizeof (port)); } @@ -124,7 +124,7 @@ check_allowed_connect_ports (int port) { data = vector_getentry (ports_allowed_by_connect, i, NULL); if (data && *data == port) - return 1; + return 1; } return 0; @@ -145,9 +145,8 @@ retry: if (len <= 0) { log_message (LOG_ERR, - "read_request_line: Client (file descriptor: %d) " - "closed socket before read.", - connptr->client_fd); + "read_request_line: Client (file descriptor: %d) " + "closed socket before read.", connptr->client_fd); return -1; } @@ -167,7 +166,7 @@ retry: } log_message (LOG_CONN, "Request (file descriptor %d): %s", - connptr->client_fd, connptr->request_line); + connptr->client_fd, connptr->request_line); return 0; } @@ -232,7 +231,7 @@ strip_return_port (char *host) return 0; *ptr1++ = '\0'; - if (sscanf (ptr1, "%d", &port) != 1) /* one conversion required */ + if (sscanf (ptr1, "%d", &port) != 1) /* one conversion required */ return 0; return port; } @@ -335,94 +334,94 @@ upstream_add (const char *host, int port, const char *domain) if (domain == NULL) { if (!host || host[0] == '\0' || port < 1) - { - log_message (LOG_WARNING, - "Nonsense upstream rule: invalid host or port"); - goto upstream_cleanup; - } + { + log_message (LOG_WARNING, + "Nonsense upstream rule: invalid host or port"); + goto upstream_cleanup; + } up->host = safestrdup (host); up->port = port; log_message (LOG_INFO, "Added upstream %s:%d for [default]", - host, port); + host, port); } else if (host == NULL) { if (!domain || domain[0] == '\0') - { - log_message (LOG_WARNING, - "Nonsense no-upstream rule: empty domain"); - goto upstream_cleanup; - } + { + log_message (LOG_WARNING, + "Nonsense no-upstream rule: empty domain"); + goto upstream_cleanup; + } ptr = strchr (domain, '/'); if (ptr) - { - struct in_addr addrstruct; - - *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); - } - } - } + { + struct in_addr addrstruct; + + *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); + } + } + } else - { - up->domain = safestrdup (domain); - } + { + up->domain = safestrdup (domain); + } log_message (LOG_INFO, "Added no-upstream for %s", domain); } else { if (!host || host[0] == '\0' || port < 1 || !domain || domain == '\0') - { - log_message (LOG_WARNING, - "Nonsense upstream rule: invalid parameters"); - goto upstream_cleanup; - } + { + log_message (LOG_WARNING, + "Nonsense upstream rule: invalid parameters"); + goto upstream_cleanup; + } up->host = safestrdup (host); up->port = port; up->domain = safestrdup (domain); log_message (LOG_INFO, "Added upstream %s:%d for %s", - host, port, domain); + host, port, domain); } if (!up->domain && !up->ip) - { /* always add default to end */ + { /* always add default to end */ struct upstream *tmp = config.upstream_list; while (tmp) - { - if (!tmp->domain && !tmp->ip) - { - log_message (LOG_WARNING, "Duplicate default upstream"); - goto upstream_cleanup; - } - - if (!tmp->next) - { - up->next = NULL; - tmp->next = up; - return; - } - - tmp = tmp->next; - } + { + if (!tmp->domain && !tmp->ip) + { + log_message (LOG_WARNING, "Duplicate default upstream"); + goto upstream_cleanup; + } + + if (!tmp->next) + { + up->next = NULL; + tmp->next = up; + return; + } + + tmp = tmp->next; + } } up->next = config.upstream_list; @@ -451,36 +450,36 @@ upstream_get (char *host) while (up) { if (up->domain) - { - if (strcasecmp (host, up->domain) == 0) - break; /* exact match */ + { + if (strcasecmp (host, up->domain) == 0) + break; /* exact match */ - if (up->domain[0] == '.') - { - char *dot = strchr (host, '.'); + if (up->domain[0] == '.') + { + char *dot = strchr (host, '.'); - if (!dot && !up->domain[1]) - break; /* local host matches "." */ + if (!dot && !up->domain[1]) + break; /* local host matches "." */ - while (dot && strcasecmp (dot, up->domain)) - dot = strchr (dot + 1, '.'); + while (dot && strcasecmp (dot, up->domain)) + dot = strchr (dot + 1, '.'); - if (dot) - break; /* subdomain match */ - } - } + if (dot) + break; /* subdomain match */ + } + } else if (up->ip) - { - if (my_ip == INADDR_NONE) - my_ip = ntohl (inet_addr (host)); + { + if (my_ip == INADDR_NONE) + my_ip = ntohl (inet_addr (host)); - if ((my_ip & up->mask) == up->ip) - break; - } + if ((my_ip & up->mask) == up->ip) + break; + } else - { - break; /* No domain or IP, default upstream */ - } + { + break; /* No domain or IP, default upstream */ + } up = up->next; } @@ -490,7 +489,7 @@ upstream_get (char *host) if (up) log_message (LOG_INFO, "Found proxy %s:%d for %s", - up->host, up->port, host); + up->host, up->port, host); else log_message (LOG_INFO, "No proxy for %s", host); @@ -513,11 +512,11 @@ establish_http_connection (struct conn_s *connptr, struct request_s *request) portbuff[0] = '\0'; return write_message (connptr->server_fd, - "%s %s HTTP/1.0\r\n" - "Host: %s%s\r\n" - "Connection: close\r\n", - request->method, request->path, - request->host, portbuff); + "%s %s HTTP/1.0\r\n" + "Host: %s%s\r\n" + "Connection: close\r\n", + request->method, request->path, + request->host, portbuff); } /* @@ -534,9 +533,9 @@ static inline int send_ssl_response (struct conn_s *connptr) { return write_message (connptr->client_fd, - "%s\r\n" - "%s\r\n" - "\r\n", SSL_CONNECTION_RESPONSE, PROXY_AGENT); + "%s\r\n" + "%s\r\n" + "\r\n", SSL_CONNECTION_RESPONSE, PROXY_AGENT); } /* @@ -571,7 +570,7 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) } ret = sscanf (connptr->request_line, "%[^ ] %[^ ] %[^ ]", - request->method, url, request->protocol); + request->method, url, request->protocol); if (ret == 2 && !strcasecmp (request->method, "GET")) { request->protocol[0] = 0; @@ -587,24 +586,24 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) * structure. */ ret = sscanf (request->protocol + 5, "%u.%u", - &connptr->protocol.major, &connptr->protocol.minor); + &connptr->protocol.major, &connptr->protocol.minor); /* * If the conversion doesn't succeed, drop down below and * send the error to the user. */ if (ret != 2) - goto BAD_REQUEST_ERROR; + goto BAD_REQUEST_ERROR; } else { BAD_REQUEST_ERROR: log_message (LOG_ERR, - "process_request: Bad Request on file descriptor %d", - connptr->client_fd); + "process_request: Bad Request on file descriptor %d", + connptr->client_fd); indicate_http_error (connptr, 400, "Bad Request", - "detail", "Request has an invalid format", - "url", url, NULL); + "detail", "Request has an invalid format", + "url", url, NULL); safefree (url); free_request_struct (request); @@ -615,11 +614,11 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) if (!url) { log_message (LOG_ERR, - "process_request: Null URL on file descriptor %d", - connptr->client_fd); + "process_request: Null URL on file descriptor %d", + connptr->client_fd); indicate_http_error (connptr, 400, "Bad Request", - "detail", "Request has an empty URL", - "url", url, NULL); + "detail", "Request has an empty URL", + "url", url, NULL); safefree (url); free_request_struct (request); @@ -641,14 +640,14 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) safefree (url); if (!reverse_url) - { - free_request_struct (request); - return NULL; - } + { + free_request_struct (request); + return NULL; + } else - { - url = reverse_url; - } + { + url = reverse_url; + } } #endif @@ -658,47 +657,47 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) char *skipped_type = strstr (url, "//") + 2; if (extract_http_url (skipped_type, request) < 0) - { - indicate_http_error (connptr, 400, "Bad Request", - "detail", "Could not parse URL", - "url", url, NULL); + { + indicate_http_error (connptr, 400, "Bad Request", + "detail", "Could not parse URL", + "url", url, NULL); - safefree (url); - free_request_struct (request); + safefree (url); + free_request_struct (request); - return NULL; - } + return NULL; + } } else if (strcmp (request->method, "CONNECT") == 0) { if (extract_ssl_url (url, request) < 0) - { - indicate_http_error (connptr, 400, "Bad Request", - "detail", "Could not parse URL", - "url", url, NULL); + { + indicate_http_error (connptr, 400, "Bad Request", + "detail", "Could not parse URL", + "url", url, NULL); - safefree (url); - free_request_struct (request); + safefree (url); + free_request_struct (request); - return NULL; - } + return NULL; + } /* Verify that the port in the CONNECT method is allowed */ if (!check_allowed_connect_ports (request->port)) - { - indicate_http_error (connptr, 403, "Access violation", - "detail", - "The CONNECT method not allowed " - "with the port you tried to use.", - "url", url, NULL); - log_message (LOG_INFO, - "Refused CONNECT method on port %d", request->port); + { + indicate_http_error (connptr, 403, "Access violation", + "detail", + "The CONNECT method not allowed " + "with the port you tried to use.", + "url", url, NULL); + log_message (LOG_INFO, + "Refused CONNECT method on port %d", request->port); - safefree (url); - free_request_struct (request); + safefree (url); + free_request_struct (request); - return NULL; - } + return NULL; + } connptr->connect_method = TRUE; } @@ -706,19 +705,19 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) { #ifdef TRANSPARENT_PROXY if (!do_transparent_proxy - (connptr, hashofheaders, request, &config, url)) - { - safefree (url); - free_request_struct (request); - return NULL; - } + (connptr, hashofheaders, request, &config, url)) + { + safefree (url); + free_request_struct (request); + return NULL; + } #else indicate_http_error (connptr, 501, "Not Implemented", - "detail", - "Unknown method or unsupported protocol.", "url", - url, NULL); + "detail", + "Unknown method or unsupported protocol.", "url", + url, NULL); log_message (LOG_INFO, "Unknown method (%s) or protocol (%s)", - request->method, url); + request->method, url); safefree (url); free_request_struct (request); return NULL; @@ -733,32 +732,32 @@ process_request (struct conn_s *connptr, hashmap_t hashofheaders) if (config.filter) { if (config.filter_url) - ret = filter_url (url); + ret = filter_url (url); else - ret = filter_domain (request->host); + ret = filter_domain (request->host); if (ret) - { - update_stats (STAT_DENIED); - - if (config.filter_url) - log_message (LOG_NOTICE, - "Proxying refused on filtered url \"%s\"", url); - else - log_message (LOG_NOTICE, - "Proxying refused on filtered domain \"%s\"", - request->host); - - indicate_http_error (connptr, 403, "Filtered", - "detail", - "The request you made has been filtered", - "url", url, NULL); - - safefree (url); - free_request_struct (request); - - return NULL; - } + { + update_stats (STAT_DENIED); + + if (config.filter_url) + log_message (LOG_NOTICE, + "Proxying refused on filtered url \"%s\"", url); + else + log_message (LOG_NOTICE, + "Proxying refused on filtered domain \"%s\"", + request->host); + + indicate_http_error (connptr, 403, "Filtered", + "detail", + "The request you made has been filtered", + "url", url, NULL); + + safefree (url); + free_request_struct (request); + + return NULL; + } } #endif @@ -799,13 +798,13 @@ pull_client_data (struct conn_s *connptr, long int length) { len = safe_read (connptr->client_fd, buffer, min (MAXBUFFSIZE, length)); if (len <= 0) - goto ERROR_EXIT; + goto ERROR_EXIT; if (!connptr->error_variables) - { - if (safe_write (connptr->server_fd, buffer, len) < 0) - goto ERROR_EXIT; - } + { + if (safe_write (connptr->server_fd, buffer, len) < 0) + goto ERROR_EXIT; + } length -= len; } @@ -845,7 +844,7 @@ add_xtinyproxy_header (struct conn_s *connptr) { assert (connptr && connptr->server_fd >= 0); return write_message (connptr->server_fd, - "X-Tinyproxy: %s\r\n", connptr->client_ip_addr); + "X-Tinyproxy: %s\r\n", connptr->client_ip_addr); } #endif /* XTINYPROXY */ @@ -884,7 +883,7 @@ get_all_headers (int fd, hashmap_t hashofheaders) { char *header; ssize_t len; - unsigned int double_cgi = FALSE; /* boolean */ + unsigned int double_cgi = FALSE; /* boolean */ assert (fd >= 0); assert (hashofheaders != NULL); @@ -892,20 +891,20 @@ get_all_headers (int fd, hashmap_t hashofheaders) for (;;) { if ((len = readline (fd, &header)) <= 0) - { - safefree (header); - return -1; - } + { + safefree (header); + return -1; + } /* * If we received just a CR LF on a line, the headers are * finished. */ if (CHECK_CRLF (header, len)) - { - safefree (header); - return 0; - } + { + safefree (header); + return 0; + } /* * BUG FIX: The following code detects a "Double CGI" @@ -919,19 +918,19 @@ get_all_headers (int fd, hashmap_t hashofheaders) * FIXME: Might need to change this to a more robust check. */ if (strncasecmp (header, "HTTP/", 5) == 0) - { - double_cgi = TRUE; + { + double_cgi = TRUE; - safefree (header); - continue; - } + safefree (header); + continue; + } if (!double_cgi - && add_header_to_connection (hashofheaders, header, len) < 0) - { - safefree (header); - return -1; - } + && add_header_to_connection (hashofheaders, header, len) < 0) + { + safefree (header); + return -1; + } safefree (header); } @@ -959,7 +958,7 @@ remove_connection_headers (hashmap_t hashofheaders) /* Look for the connection header. If it's not found, return. */ len = hashmap_entry_by_key (hashofheaders, headers[i], (void **) &data); if (len <= 0) - return 0; + return 0; /* * Go through the data line and replace any special characters @@ -967,7 +966,7 @@ remove_connection_headers (hashmap_t hashofheaders) */ ptr = data; while ((ptr = strpbrk (ptr, "()<>@,;:\\\"/[]?={} \t"))) - *ptr++ = '\0'; + *ptr++ = '\0'; /* * All the tokens are separated by NULLs. Now go through the @@ -975,14 +974,14 @@ remove_connection_headers (hashmap_t hashofheaders) */ ptr = data; while (ptr < data + len) - { - hashmap_remove (hashofheaders, ptr); + { + hashmap_remove (hashofheaders, ptr); - /* Advance ptr to the next token */ - ptr += strlen (ptr) + 1; - while (ptr < data + len && *ptr == '\0') - ptr++; - } + /* Advance ptr to the next token */ + ptr += strlen (ptr) + 1; + while (ptr < data + len && *ptr == '\0') + ptr++; + } /* Now remove the connection header it self. */ hashmap_remove (hashofheaders, headers[i]); @@ -1019,7 +1018,7 @@ get_content_length (hashmap_t hashofheaders) */ static int write_via_header (int fd, hashmap_t hashofheaders, - unsigned int major, unsigned int minor) + unsigned int major, unsigned int minor) { ssize_t len; char hostname[512]; @@ -1043,16 +1042,16 @@ write_via_header (int fd, hashmap_t hashofheaders, if (len > 0) { ret = write_message (fd, - "Via: %s, %hu.%hu %s (%s/%s)\r\n", - data, major, minor, hostname, PACKAGE, VERSION); + "Via: %s, %hu.%hu %s (%s/%s)\r\n", + data, major, minor, hostname, PACKAGE, VERSION); hashmap_remove (hashofheaders, "via"); } else { ret = write_message (fd, - "Via: %hu.%hu %s (%s/%s)\r\n", - major, minor, hostname, PACKAGE, VERSION); + "Via: %hu.%hu %s (%s/%s)\r\n", + major, minor, hostname, PACKAGE, VERSION); } return ret; @@ -1121,15 +1120,15 @@ process_client_headers (struct conn_s *connptr, hashmap_t hashofheaders) /* Send, or add the Via header */ ret = write_via_header (connptr->server_fd, hashofheaders, - connptr->protocol.major, connptr->protocol.minor); + connptr->protocol.major, connptr->protocol.minor); if (ret < 0) { indicate_http_error (connptr, 503, - "Could not send data to remote server", - "detail", - "A network error occurred while " - "trying to write data to the remote web server.", - NULL); + "Could not send data to remote server", + "detail", + "A network error occurred while " + "trying to write data to the remote web server.", + NULL); goto PULL_CLIENT_DATA; } @@ -1140,28 +1139,27 @@ process_client_headers (struct conn_s *connptr, hashmap_t hashofheaders) if (iter >= 0) { for (; !hashmap_is_end (hashofheaders, iter); ++iter) - { - hashmap_return_entry (hashofheaders, - iter, &data, (void **) &header); - - if (!is_anonymous_enabled () || anonymous_search (data) > 0) - { - ret = - write_message (connptr->server_fd, - "%s: %s\r\n", data, header); - if (ret < 0) - { - indicate_http_error (connptr, 503, - "Could not send data to remote server", - "detail", - "A network error occurred while " - "trying to write data to the " - "remote web server.", - NULL); - goto PULL_CLIENT_DATA; - } - } - } + { + hashmap_return_entry (hashofheaders, + iter, &data, (void **) &header); + + if (!is_anonymous_enabled () || anonymous_search (data) > 0) + { + ret = + write_message (connptr->server_fd, + "%s: %s\r\n", data, header); + if (ret < 0) + { + indicate_http_error (connptr, 503, + "Could not send data to remote server", + "detail", + "A network error occurred while " + "trying to write data to the " + "remote web server.", NULL); + goto PULL_CLIENT_DATA; + } + } + } } #if defined(XTINYPROXY_ENABLE) if (config.my_domain) @@ -1243,17 +1241,16 @@ retry: if (get_all_headers (connptr->server_fd, hashofheaders) < 0) { log_message (LOG_WARNING, - "Could not retrieve all the headers from the remote server."); + "Could not retrieve all the headers from the remote server."); hashmap_delete (hashofheaders); safefree (response_line); indicate_http_error (connptr, 503, - "Could not retrieve all the headers", - "detail", - PACKAGE " " - "was unable to retrieve and process headers from " - "the remote web server.", - NULL); + "Could not retrieve all the headers", + "detail", + PACKAGE " " + "was unable to retrieve and process headers from " + "the remote web server.", NULL); return -1; } @@ -1298,7 +1295,7 @@ retry: /* Send, or add the Via header */ ret = write_via_header (connptr->client_fd, hashofheaders, - connptr->protocol.major, connptr->protocol.minor); + connptr->protocol.major, connptr->protocol.minor); if (ret < 0) goto ERROR_EXIT; @@ -1307,10 +1304,10 @@ retry: if (config.reversemagic && connptr->reversepath) { ret = write_message (connptr->client_fd, - "Set-Cookie: " REVERSE_COOKIE - "=%s; path=/\r\n", connptr->reversepath); + "Set-Cookie: " REVERSE_COOKIE + "=%s; path=/\r\n", connptr->reversepath); if (ret < 0) - goto ERROR_EXIT; + goto ERROR_EXIT; } /* Rewrite the HTTP redirect if needed */ @@ -1320,29 +1317,29 @@ retry: /* Look for a matching entry in the reversepath list */ while (reverse) - { - if (strncasecmp (header, - reverse->url, (len = strlen (reverse->url))) == 0) - break; - reverse = reverse->next; - } + { + if (strncasecmp (header, + reverse->url, (len = strlen (reverse->url))) == 0) + break; + reverse = reverse->next; + } if (reverse) - { - ret = - write_message (connptr->client_fd, - "Location: %s%s%s\r\n", - config.reversebaseurl, - (reverse->path + 1), (header + len)); - if (ret < 0) - goto ERROR_EXIT; - - log_message (LOG_INFO, - "Rewriting HTTP redirect: %s -> %s%s%s", - header, config.reversebaseurl, - (reverse->path + 1), (header + len)); - hashmap_remove (hashofheaders, "location"); - } + { + ret = + write_message (connptr->client_fd, + "Location: %s%s%s\r\n", + config.reversebaseurl, + (reverse->path + 1), (header + len)); + if (ret < 0) + goto ERROR_EXIT; + + log_message (LOG_INFO, + "Rewriting HTTP redirect: %s -> %s%s%s", + header, config.reversebaseurl, + (reverse->path + 1), (header + len)); + hashmap_remove (hashofheaders, "location"); + } } #endif @@ -1353,15 +1350,15 @@ retry: if (iter >= 0) { for (; !hashmap_is_end (hashofheaders, iter); ++iter) - { - hashmap_return_entry (hashofheaders, - iter, &data, (void **) &header); - - ret = write_message (connptr->client_fd, - "%s: %s\r\n", data, header); - if (ret < 0) - goto ERROR_EXIT; - } + { + hashmap_return_entry (hashofheaders, + iter, &data, (void **) &header); + + ret = write_message (connptr->client_fd, + "%s: %s\r\n", data, header); + if (ret < 0) + goto ERROR_EXIT; + } } hashmap_delete (hashofheaders); @@ -1409,73 +1406,73 @@ relay_connection (struct conn_s *connptr) tv.tv_usec = 0; if (buffer_size (connptr->sbuffer) > 0) - FD_SET (connptr->client_fd, &wset); + FD_SET (connptr->client_fd, &wset); if (buffer_size (connptr->cbuffer) > 0) - FD_SET (connptr->server_fd, &wset); + FD_SET (connptr->server_fd, &wset); if (buffer_size (connptr->sbuffer) < MAXBUFFSIZE) - FD_SET (connptr->server_fd, &rset); + FD_SET (connptr->server_fd, &rset); if (buffer_size (connptr->cbuffer) < MAXBUFFSIZE) - FD_SET (connptr->client_fd, &rset); + FD_SET (connptr->client_fd, &rset); ret = select (maxfd, &rset, &wset, NULL, &tv); if (ret == 0) - { - tdiff = difftime (time (NULL), last_access); - if (tdiff > config.idletimeout) - { - log_message (LOG_INFO, - "Idle Timeout (after select) as %g > %u.", - tdiff, config.idletimeout); - return; - } - else - { - continue; - } - } + { + tdiff = difftime (time (NULL), last_access); + if (tdiff > config.idletimeout) + { + log_message (LOG_INFO, + "Idle Timeout (after select) as %g > %u.", + tdiff, config.idletimeout); + return; + } + else + { + continue; + } + } else if (ret < 0) - { - log_message (LOG_ERR, - "relay_connection: select() error \"%s\". " - "Closing connection (client_fd:%d, server_fd:%d)", - strerror (errno), connptr->client_fd, - connptr->server_fd); - return; - } + { + log_message (LOG_ERR, + "relay_connection: select() error \"%s\". " + "Closing connection (client_fd:%d, server_fd:%d)", + strerror (errno), connptr->client_fd, + connptr->server_fd); + return; + } else - { - /* - * All right, something was actually selected so mark it. - */ - last_access = time (NULL); - } + { + /* + * All right, something was actually selected so mark it. + */ + last_access = time (NULL); + } if (FD_ISSET (connptr->server_fd, &rset)) - { - bytes_received = read_buffer (connptr->server_fd, connptr->sbuffer); - if (bytes_received < 0) - break; - - connptr->content_length.server -= bytes_received; - if (connptr->content_length.server == 0) - break; - } + { + bytes_received = read_buffer (connptr->server_fd, connptr->sbuffer); + if (bytes_received < 0) + break; + + connptr->content_length.server -= bytes_received; + if (connptr->content_length.server == 0) + break; + } if (FD_ISSET (connptr->client_fd, &rset) - && read_buffer (connptr->client_fd, connptr->cbuffer) < 0) - { - break; - } + && read_buffer (connptr->client_fd, connptr->cbuffer) < 0) + { + break; + } if (FD_ISSET (connptr->server_fd, &wset) - && write_buffer (connptr->server_fd, connptr->cbuffer) < 0) - { - break; - } + && write_buffer (connptr->server_fd, connptr->cbuffer) < 0) + { + break; + } if (FD_ISSET (connptr->client_fd, &wset) - && write_buffer (connptr->client_fd, connptr->sbuffer) < 0) - { - break; - } + && write_buffer (connptr->client_fd, connptr->sbuffer) < 0) + { + break; + } } /* @@ -1486,7 +1483,7 @@ relay_connection (struct conn_s *connptr) while (buffer_size (connptr->sbuffer) > 0) { if (write_buffer (connptr->client_fd, connptr->sbuffer) < 0) - break; + break; } shutdown (connptr->client_fd, SHUT_WR); @@ -1497,7 +1494,7 @@ relay_connection (struct conn_s *connptr) while (buffer_size (connptr->cbuffer) > 0) { if (write_buffer (connptr->server_fd, connptr->cbuffer) < 0) - break; + break; } return; @@ -1524,32 +1521,31 @@ connect_to_upstream (struct conn_s *connptr, struct request_s *request) if (!cur_upstream) { log_message (LOG_WARNING, - "No upstream proxy defined for %s.", request->host); + "No upstream proxy defined for %s.", request->host); indicate_http_error (connptr, 404, - "Unable to connect to upstream proxy."); + "Unable to connect to upstream proxy."); return -1; } connptr->server_fd = opensock (cur_upstream->host, cur_upstream->port, - connptr->server_ip_addr); + connptr->server_ip_addr); if (connptr->server_fd < 0) { log_message (LOG_WARNING, "Could not connect to upstream proxy."); indicate_http_error (connptr, 404, - "Unable to connect to upstream proxy", - "detail", - "A network error occurred while trying to " - "connect to the upstream web proxy.", - NULL); + "Unable to connect to upstream proxy", + "detail", + "A network error occurred while trying to " + "connect to the upstream web proxy.", NULL); return -1; } log_message (LOG_CONN, - "Established connection to upstream proxy \"%s\" " - "using file descriptor %d.", - cur_upstream->host, connptr->server_fd); + "Established connection to upstream proxy \"%s\" " + "using file descriptor %d.", + cur_upstream->host, connptr->server_fd); /* * We need to re-write the "path" part of the request so that we @@ -1562,9 +1558,9 @@ connect_to_upstream (struct conn_s *connptr, struct request_s *request) combined_string = safemalloc (len); if (!combined_string) - { - return -1; - } + { + return -1; + } snprintf (combined_string, len, "%s:%d", request->host, request->port); } @@ -1573,12 +1569,12 @@ connect_to_upstream (struct conn_s *connptr, struct request_s *request) len = strlen (request->host) + strlen (request->path) + 14; combined_string = safemalloc (len); if (!combined_string) - { - return -1; - } + { + return -1; + } snprintf (combined_string, len, "http://%s:%d%s", request->host, - request->port, request->path); + request->port, request->path); } if (request->path) @@ -1615,12 +1611,12 @@ handle_connection (int fd) getsock_ip (fd, sock_ipaddr); log_message (LOG_CONN, config.bindsame ? - "Connect (file descriptor %d): %s [%s] at [%s]" : - "Connect (file descriptor %d): %s [%s]", - fd, peer_string, peer_ipaddr, sock_ipaddr); + "Connect (file descriptor %d): %s [%s] at [%s]" : + "Connect (file descriptor %d): %s [%s]", + fd, peer_string, peer_ipaddr, sock_ipaddr); connptr = initialize_conn (fd, peer_ipaddr, peer_string, - config.bindsame ? sock_ipaddr : 0); + config.bindsame ? sock_ipaddr : 0); if (!connptr) { close (fd); @@ -1631,10 +1627,9 @@ handle_connection (int fd) { update_stats (STAT_DENIED); indicate_http_error (connptr, 403, "Access denied", - "detail", - "The administrator of this proxy has not configured " - "it to service requests from your host.", - NULL); + "detail", + "The administrator of this proxy has not configured " + "it to service requests from your host.", NULL); send_http_error_message (connptr); destroy_conn (connptr); return; @@ -1644,10 +1639,9 @@ handle_connection (int fd) { update_stats (STAT_BADCONN); indicate_http_error (connptr, 408, "Timeout", - "detail", - "Server timeout waiting for the HTTP request " - "from the client.", - NULL); + "detail", + "Server timeout waiting for the HTTP request " + "from the client.", NULL); send_http_error_message (connptr); destroy_conn (connptr); return; @@ -1660,10 +1654,10 @@ handle_connection (int fd) { update_stats (STAT_BADCONN); indicate_http_error (connptr, 503, "Internal error", - "detail", - "An internal server error occurred while processing " - "your request. Please contact the administrator.", - NULL); + "detail", + "An internal server error occurred while processing " + "your request. Please contact the administrator.", + NULL); send_http_error_message (connptr); destroy_conn (connptr); return; @@ -1675,7 +1669,7 @@ handle_connection (int fd) if (get_all_headers (connptr->client_fd, hashofheaders) < 0) { log_message (LOG_WARNING, - "Could not retrieve all the headers from the client"); + "Could not retrieve all the headers from the client"); hashmap_delete (hashofheaders); update_stats (STAT_BADCONN); destroy_conn (connptr); @@ -1686,12 +1680,12 @@ handle_connection (int fd) if (!request) { if (!connptr->error_variables && !connptr->show_stats) - { - update_stats (STAT_BADCONN); - destroy_conn (connptr); - hashmap_delete (hashofheaders); - return; - } + { + update_stats (STAT_BADCONN); + destroy_conn (connptr); + hashmap_delete (hashofheaders); + return; + } goto send_error; } @@ -1699,31 +1693,30 @@ handle_connection (int fd) if (connptr->upstream_proxy != NULL) { if (connect_to_upstream (connptr, request) < 0) - { - goto send_error; - } + { + goto send_error; + } } else { connptr->server_fd = opensock (request->host, request->port, - connptr->server_ip_addr); + connptr->server_ip_addr); if (connptr->server_fd < 0) - { - indicate_http_error (connptr, 500, "Unable to connect", - "detail", - PACKAGE " " - "was unable to connect to the remote web server.", - "error", strerror (errno), NULL); - goto send_error; - } + { + indicate_http_error (connptr, 500, "Unable to connect", + "detail", + PACKAGE " " + "was unable to connect to the remote web server.", + "error", strerror (errno), NULL); + goto send_error; + } log_message (LOG_CONN, - "Established connection to host \"%s\" using " - "file descriptor %d.", - request->host, connptr->server_fd); + "Established connection to host \"%s\" using " + "file descriptor %d.", request->host, connptr->server_fd); if (!connptr->connect_method) - establish_http_connection (connptr, request); + establish_http_connection (connptr, request); } send_error: @@ -1733,11 +1726,11 @@ send_error: { update_stats (STAT_BADCONN); if (!connptr->error_variables) - { - hashmap_delete (hashofheaders); - destroy_conn (connptr); - return; - } + { + hashmap_delete (hashofheaders); + destroy_conn (connptr); + return; + } } hashmap_delete (hashofheaders); @@ -1757,34 +1750,34 @@ send_error: if (!connptr->connect_method || (connptr->upstream_proxy != NULL)) { if (process_server_headers (connptr) < 0) - { - if (connptr->error_variables) - send_http_error_message (connptr); - - update_stats (STAT_BADCONN); - destroy_conn (connptr); - return; - } + { + if (connptr->error_variables) + send_http_error_message (connptr); + + update_stats (STAT_BADCONN); + destroy_conn (connptr); + return; + } } else { if (send_ssl_response (connptr) < 0) - { - log_message (LOG_ERR, - "handle_connection: Could not send SSL greeting " - "to client."); - update_stats (STAT_BADCONN); - destroy_conn (connptr); - return; - } + { + log_message (LOG_ERR, + "handle_connection: Could not send SSL greeting " + "to client."); + update_stats (STAT_BADCONN); + destroy_conn (connptr); + return; + } } relay_connection (connptr); log_message (LOG_INFO, - "Closed connection between local client (fd:%d) " - "and remote client (fd:%d)", - connptr->client_fd, connptr->server_fd); + "Closed connection between local client (fd:%d) " + "and remote client (fd:%d)", + connptr->client_fd, connptr->server_fd); /* * All done... close everything and go home... :) diff --git a/src/reverse-proxy.c b/src/reverse-proxy.c index db07bae..d4ca7eb 100644 --- a/src/reverse-proxy.c +++ b/src/reverse-proxy.c @@ -43,17 +43,16 @@ reversepath_add (const char *path, const char *url) if (!strstr (url, "://")) { log_message (LOG_WARNING, - "Skipping reverse proxy rule: '%s' is not a valid url", - url); + "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); + "Skipping reverse proxy rule: path '%s' " + "doesn't start with a /", path); return; } @@ -74,8 +73,8 @@ reversepath_add (const char *path, const char *url) config.reversepath_list = reverse; log_message (LOG_INFO, - "Added reverse proxy rule: %s -> %s", reverse->path, - reverse->url); + "Added reverse proxy rule: %s -> %s", reverse->path, + reverse->url); } /* @@ -89,7 +88,7 @@ reversepath_get (char *url) while (reverse) { if (strstr (url, reverse->path) == url) - return reverse; + return reverse; reverse = reverse->next; } @@ -102,7 +101,7 @@ reversepath_get (char *url) */ char * reverse_rewrite_url (struct conn_s *connptr, hashmap_t hashofheaders, - char *url) + char *url) { char *rewrite_url = NULL; char *cookie = NULL; @@ -115,31 +114,31 @@ reverse_rewrite_url (struct conn_s *connptr, hashmap_t hashofheaders, /* 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)); - } + { + 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); - } - } + && 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 */ @@ -147,8 +146,8 @@ reverse_rewrite_url (struct conn_s *connptr, hashmap_t hashofheaders, { log_message (LOG_ERR, "Bad request"); indicate_http_error (connptr, 400, "Bad Request", - "detail", - "Request has an invalid URL", "url", url, NULL); + "detail", + "Request has an invalid URL", "url", url, NULL); return NULL; } diff --git a/src/reverse-proxy.h b/src/reverse-proxy.h index 2bb8a16..a96dc9a 100644 --- a/src/reverse-proxy.h +++ b/src/reverse-proxy.h @@ -35,6 +35,6 @@ struct reversepath 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); + hashmap_t hashofheaders, char *url); #endif @@ -60,12 +60,12 @@ bind_socket (int sockfd, const char *addr) do { if (bind (sockfd, res->ai_addr, res->ai_addrlen) == 0) - break; /* success */ + break; /* success */ } while ((res = res->ai_next) != NULL); freeaddrinfo (ressave); - if (res == NULL) /* was not able to bind to any address */ + if (res == NULL) /* was not able to bind to any address */ return -1; return sockfd; @@ -104,28 +104,28 @@ opensock (const char *host, int port, const char *bind_to) { sockfd = socket (res->ai_family, res->ai_socktype, res->ai_protocol); if (sockfd < 0) - continue; /* ignore this one */ + continue; /* ignore this one */ /* Bind to the specified address */ if (bind_to) - { - if (bind_socket (sockfd, bind_to) < 0) - { - close (sockfd); - continue; /* can't bind, so try again */ - } - } + { + if (bind_socket (sockfd, bind_to) < 0) + { + close (sockfd); + continue; /* can't bind, so try again */ + } + } else if (config.bind_address) - { - if (bind_socket (sockfd, config.bind_address) < 0) - { - close (sockfd); - continue; /* can't bind, so try again */ - } - } + { + if (bind_socket (sockfd, config.bind_address) < 0) + { + close (sockfd); + continue; /* can't bind, so try again */ + } + } if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0) - break; /* success */ + break; /* success */ close (sockfd); } @@ -135,7 +135,7 @@ opensock (const char *host, int port, const char *bind_to) if (res == NULL) { log_message (LOG_ERR, - "opensock: Could not establish a connection to %s", host); + "opensock: Could not establish a connection to %s", host); return -1; } @@ -205,16 +205,16 @@ listen_sock (uint16_t port, socklen_t * addrlen) if (bind (listenfd, (struct sockaddr *) &addr, sizeof (addr)) < 0) { log_message (LOG_ERR, - "Unable to bind listening socket because of %s", - strerror (errno)); + "Unable to bind listening socket because of %s", + strerror (errno)); return -1; } if (listen (listenfd, MAXLISTEN) < 0) { log_message (LOG_ERR, - "Unable to start listening socket because of %s", - strerror (errno)); + "Unable to start listening socket because of %s", + strerror (errno)); return -1; } @@ -237,7 +237,7 @@ getsock_ip (int fd, char *ipaddr) if (getsockname (fd, (struct sockaddr *) &name, &namelen) != 0) { log_message (LOG_ERR, "getsock_ip: getsockname() error: %s", - strerror (errno)); + strerror (errno)); return -1; } @@ -273,5 +273,5 @@ getpeer_information (int fd, char *ipaddr, char *string_addr) /* Get the full host name */ return getnameinfo ((struct sockaddr *) &sa, salen, - string_addr, HOSTNAME_LENGTH, NULL, 0, 0); + string_addr, HOSTNAME_LENGTH, NULL, 0, 0); } diff --git a/src/stats.c b/src/stats.c index 4ec1bc9..3c3b0d9 100644 --- a/src/stats.c +++ b/src/stats.c @@ -95,20 +95,20 @@ showstats (struct conn_s *connptr) { message_buffer = safemalloc (MAXBUFFSIZE); if (!message_buffer) - return -1; + return -1; snprintf (message_buffer, MAXBUFFSIZE, msg, - PACKAGE, VERSION, PACKAGE, VERSION, - stats->num_open, - stats->num_reqs, - stats->num_badcons, stats->num_denied, - stats->num_refused, PACKAGE, VERSION); + PACKAGE, VERSION, PACKAGE, VERSION, + stats->num_open, + stats->num_reqs, + stats->num_badcons, stats->num_denied, + stats->num_refused, PACKAGE, VERSION); if (send_http_message (connptr, 200, "OK", message_buffer) < 0) - { - safefree (message_buffer); - return -1; - } + { + safefree (message_buffer); + return -1; + } safefree (message_buffer); return 0; diff --git a/src/stats.h b/src/stats.h index a75d92e..23f44e4 100644 --- a/src/stats.h +++ b/src/stats.h @@ -28,11 +28,11 @@ */ typedef enum { - STAT_BADCONN, /* bad connection, for unknown reason */ - STAT_OPEN, /* connection opened */ - STAT_CLOSE, /* connection closed */ - STAT_REFUSE, /* connection refused (to outside world) */ - STAT_DENIED /* connection denied to tinyproxy itself */ + STAT_BADCONN, /* bad connection, for unknown reason */ + STAT_OPEN, /* connection opened */ + STAT_CLOSE, /* connection closed */ + STAT_REFUSE, /* connection refused (to outside world) */ + STAT_DENIED /* connection denied to tinyproxy itself */ } status_t; /* @@ -107,7 +107,7 @@ chomp (char *buffer, size_t length) /* Stop once we get to zero to prevent wrap-around */ if (length-- == 0) - break; + break; } return chars; diff --git a/src/tinyproxy.c b/src/tinyproxy.c index 0a9c499..6d2987d 100644 --- a/src/tinyproxy.c +++ b/src/tinyproxy.c @@ -48,8 +48,8 @@ RETSIGTYPE takesig (int sig); */ struct config_s config; float load = 0.00; -unsigned int received_sighup = FALSE; /* boolean */ -unsigned int processed_config_file = FALSE; /* boolean */ +unsigned int received_sighup = FALSE; /* boolean */ +unsigned int processed_config_file = FALSE; /* boolean */ /* * Handle a signal @@ -162,7 +162,7 @@ get_id (char *str) while (*tstr != 0) { if (!isdigit (*tstr)) - return -1; + return -1; tstr++; } @@ -173,7 +173,7 @@ int main (int argc, char **argv) { int optch; - unsigned int godaemon = TRUE; /* boolean */ + unsigned int godaemon = TRUE; /* boolean */ struct passwd *thisuser = NULL; struct group *thisgroup = NULL; FILE *config_file; @@ -192,29 +192,29 @@ main (int argc, char **argv) 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); - } + { + 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 " ..."); @@ -232,14 +232,14 @@ main (int argc, char **argv) if (!config_file) { fprintf (stderr, - "%s: Could not open configuration file \"%s\".\n", - argv[0], config.config_file); + "%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"); + "Unable to parse configuration file. Not starting.\n"); exit (EX_SOFTWARE); } fclose (config_file); @@ -251,24 +251,24 @@ main (int argc, char **argv) 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 */ + { + 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); + openlog ("tinyproxy", LOG_PID, LOG_DAEMON); else - openlog ("tinyproxy", LOG_PID, LOG_USER); + openlog ("tinyproxy", LOG_PID, LOG_USER); } else { fprintf (stderr, - "%s: Either define a logfile or enable syslog logging\n", - argv[0]); + "%s: Either define a logfile or enable syslog logging\n", + argv[0]); exit (EX_SOFTWARE); } @@ -281,8 +281,8 @@ main (int argc, char **argv) if (config.port == 0) { fprintf (stderr, - "%s: You MUST set a Port in the configuration file.\n", - argv[0]); + "%s: You MUST set a Port in the configuration file.\n", + argv[0]); exit (EX_SOFTWARE); } if (!config.stathost) @@ -293,15 +293,15 @@ main (int argc, char **argv) if (!config.user) { log_message (LOG_WARNING, - "You SHOULD set a UserName in the configuration file. " - "Using current user instead."); + "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); + "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; } @@ -326,16 +326,16 @@ main (int argc, char **argv) if (config.pidpath) { if (pidfile_create (config.pidpath) < 0) - { - fprintf (stderr, "%s: Could not create PID file.\n", argv[0]); - exit (EX_OSERR); - } + { + 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]); + argv[0]); exit (EX_OSERR); } #ifdef FILTER_ENABLE @@ -358,59 +358,59 @@ main (int argc, char **argv) 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); - } + { + 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\".", argv[0], config.user); - exit (EX_NOUSER); - } - uid = thisuser->pw_uid; - } - if (setuid (uid) < 0) - { - fprintf (stderr, - "%s: Unable to change to user \"%s\".", - argv[0], config.user); - exit (EX_CANTCREAT); - } - log_message (LOG_INFO, "Now running as user \"%s\".", config.user); - } + { + int uid = get_id (config.user); + if (uid < 0) + { + thisuser = getpwnam (config.user); + if (!thisuser) + { + fprintf (stderr, + "%s: Unable to find " + "user \"%s\".", argv[0], config.user); + exit (EX_NOUSER); + } + uid = thisuser->pw_uid; + } + if (setuid (uid) < 0) + { + fprintf (stderr, + "%s: Unable to change to user \"%s\".", + 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."); + "Not running as root, so not changing UID/GID."); } if (child_pool_create () < 0) @@ -426,13 +426,13 @@ main (int argc, char **argv) if (set_signal_handler (SIGCHLD, takesig) == SIG_ERR) { fprintf (stderr, "%s: Could not set the \"SIGCHLD\" signal.\n", - argv[0]); + 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]); + argv[0]); exit (EX_OSERR); } if (set_signal_handler (SIGHUP, takesig) == SIG_ERR) @@ -459,8 +459,8 @@ main (int argc, char **argv) if (unlink (config.pidpath) < 0) { log_message (LOG_WARNING, - "Could not remove PID file \"%s\": %s.", - config.pidpath, strerror (errno)); + "Could not remove PID file \"%s\": %s.", + config.pidpath, strerror (errno)); } #ifdef FILTER_ENABLE if (config.filter) diff --git a/src/tinyproxy.h b/src/tinyproxy.h index fe73da7..2d4a187 100644 --- a/src/tinyproxy.h +++ b/src/tinyproxy.h @@ -26,8 +26,8 @@ #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 */ +#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 @@ -36,7 +36,7 @@ struct upstream { struct upstream *next; - char *domain; /* optional */ + char *domain; /* optional */ char *host; int port; in_addr_t ip, mask; @@ -49,31 +49,31 @@ struct config_s { char *logf_name; char *config_file; - unsigned int syslog; /* boolean */ + unsigned int syslog; /* boolean */ int port; char *stathost; - unsigned int quit; /* boolean */ + 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 */ + 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 */ + unsigned int reverseonly; /* boolean */ + unsigned int reversemagic; /* boolean */ char *reversebaseurl; #endif #ifdef UPSTREAM_SUPPORT struct upstream *upstream_list; -#endif /* UPSTREAM_SUPPORT */ +#endif /* UPSTREAM_SUPPORT */ char *pidpath; unsigned int idletimeout; char *bind_address; @@ -103,7 +103,7 @@ struct config_s /* Global Structures used in the program */ extern struct config_s config; -extern unsigned int received_sighup; /* boolean */ -extern unsigned int processed_config_file; /* boolean */ +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 a1ecb7a..a3c4ee8 100644 --- a/src/transparent-proxy.c +++ b/src/transparent-proxy.c @@ -56,8 +56,8 @@ build_url (char **url, const char *host, int port, const char *path) int do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders, - struct request_s *request, struct config_s *conf, - char *url) + struct request_s *request, struct config_s *conf, + char *url) { socklen_t length; char *data; @@ -68,16 +68,16 @@ do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders, struct sockaddr_in dest_addr; if (getsockname - (connptr->client_fd, (struct sockaddr *) &dest_addr, &length) < 0) - { - log_message (LOG_ERR, - "process_request: cannot get destination IP for %d", - connptr->client_fd); - indicate_http_error (connptr, 400, "Bad Request", - "detail", - "Unknown destination", "url", url, NULL); - return 0; - } + (connptr->client_fd, (struct sockaddr *) &dest_addr, &length) < 0) + { + log_message (LOG_ERR, + "process_request: cannot get destination IP for %d", + connptr->client_fd); + indicate_http_error (connptr, 400, "Bad Request", + "detail", + "Unknown destination", "url", url, NULL); + return 0; + } request->host = safemalloc (17); strcpy (request->host, inet_ntoa (dest_addr.sin_addr)); request->port = ntohs (dest_addr.sin_port); @@ -86,35 +86,34 @@ do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders, safefree (url); build_url (&url, request->host, request->port, request->path); log_message (LOG_INFO, - "process_request: trans IP %s %s for %d", - request->method, url, connptr->client_fd); + "process_request: trans IP %s %s for %d", + request->method, url, connptr->client_fd); } else { request->host = safemalloc (length + 1); if (sscanf (data, "%[^:]:%hu", request->host, &request->port) != 2) - { - strcpy (request->host, data); - request->port = HTTP_PORT; - } + { + strcpy (request->host, data); + request->port = HTTP_PORT; + } request->path = safemalloc (strlen (url) + 1); strcpy (request->path, url); safefree (url); build_url (&url, request->host, request->port, request->path); log_message (LOG_INFO, - "process_request: trans Host %s %s for %d", - request->method, url, connptr->client_fd); + "process_request: trans Host %s %s for %d", + request->method, url, connptr->client_fd); } if (conf->ipAddr && strcmp (request->host, conf->ipAddr) == 0) { log_message (LOG_ERR, - "process_request: destination IP is localhost %d", - connptr->client_fd); + "process_request: destination IP is localhost %d", + connptr->client_fd); indicate_http_error (connptr, 400, "Bad Request", - "detail", - "You tried to connect to the machine " - "the proxy is running on", - "url", url, NULL); + "detail", + "You tried to connect to the machine " + "the proxy is running on", "url", url, NULL); return 0; } diff --git a/src/transparent-proxy.h b/src/transparent-proxy.h index 09a7777..f18285f 100644 --- a/src/transparent-proxy.h +++ b/src/transparent-proxy.h @@ -30,9 +30,9 @@ #include "reqs.h" extern int do_transparent_proxy (struct conn_s *connptr, - hashmap_t hashofheaders, - struct request_s *request, - struct config_s *config, char *url); + hashmap_t hashofheaders, + struct request_s *request, + struct config_s *config, char *url); #endif diff --git a/src/utils.c b/src/utils.c index a457857..4f1df87 100644 --- a/src/utils.c +++ b/src/utils.c @@ -35,7 +35,7 @@ */ int send_http_message (struct conn_s *connptr, int http_code, - const char *error_title, const char *message) + const char *error_title, const char *message) { static char *headers[] = { "Server: " PACKAGE "/" VERSION, @@ -78,12 +78,12 @@ create_file_safely (const char *filename, unsigned int truncate_file) * existing", exit. */ if (errno != ENOENT) - { - fprintf (stderr, - "%s: Error checking file %s: %s\n", - PACKAGE, filename, strerror (errno)); - return -EACCES; - } + { + fprintf (stderr, + "%s: Error checking file %s: %s\n", + PACKAGE, filename, strerror (errno)); + return -EACCES; + } /* * The file doesn't exist, so create it with O_EXCL to make @@ -91,12 +91,12 @@ create_file_safely (const char *filename, unsigned int truncate_file) * and open() */ if ((fildes = open (filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) - { - fprintf (stderr, - "%s: Could not create file %s: %s\n", - PACKAGE, filename, strerror (errno)); - return fildes; - } + { + fprintf (stderr, + "%s: Could not create file %s: %s\n", + PACKAGE, filename, strerror (errno)); + return fildes; + } } else { @@ -105,34 +105,34 @@ create_file_safely (const char *filename, unsigned int truncate_file) flags = O_RDWR; if (!truncate_file) - flags |= O_APPEND; + flags |= O_APPEND; /* * Open an existing file. */ if ((fildes = open (filename, flags)) < 0) - { - fprintf (stderr, - "%s: Could not open file %s: %s\n", - PACKAGE, filename, strerror (errno)); - return fildes; - } + { + fprintf (stderr, + "%s: Could not open file %s: %s\n", + PACKAGE, filename, strerror (errno)); + return fildes; + } /* * fstat() the opened file and check that the file mode bits, * inode, and device match. */ if (fstat (fildes, &fstatinfo) < 0 - || lstatinfo.st_mode != fstatinfo.st_mode - || lstatinfo.st_ino != fstatinfo.st_ino - || lstatinfo.st_dev != fstatinfo.st_dev) - { - fprintf (stderr, - "%s: The file %s has been changed before it could be opened\n", - PACKAGE, filename); - close (fildes); - return -EIO; - } + || lstatinfo.st_mode != fstatinfo.st_mode + || lstatinfo.st_ino != fstatinfo.st_ino + || lstatinfo.st_dev != fstatinfo.st_dev) + { + fprintf (stderr, + "%s: The file %s has been changed before it could be opened\n", + PACKAGE, filename); + close (fildes); + return -EIO; + } /* * If the above check was passed, we know that the lstat() @@ -142,21 +142,21 @@ create_file_safely (const char *filename, unsigned int truncate_file) * st_mode check would also find this) */ if (fstatinfo.st_nlink > 1 || !S_ISREG (lstatinfo.st_mode)) - { - fprintf (stderr, - "%s: The file %s has too many links, " - "or is not a regular file: %s\n", - PACKAGE, filename, strerror (errno)); - close (fildes); - return -EMLINK; - } + { + fprintf (stderr, + "%s: The file %s has too many links, " + "or is not a regular file: %s\n", + PACKAGE, filename, strerror (errno)); + close (fildes); + return -EMLINK; + } /* * Just return the file descriptor if we _don't_ want the file * truncated. */ if (!truncate_file) - return fildes; + return fildes; /* * On systems which don't support ftruncate() the best we can @@ -171,12 +171,12 @@ create_file_safely (const char *filename, unsigned int truncate_file) #else close (fildes); if ((fildes = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) - { - fprintf (stderr, - "%s: Could not open file %s: %s.", - PACKAGE, filename, strerror (errno)); - return fildes; - } + { + fprintf (stderr, + "%s: Could not open file %s: %s.", + PACKAGE, filename, strerror (errno)); + return fildes; + } #endif /* HAVE_FTRUNCATE */ } @@ -204,8 +204,8 @@ pidfile_create (const char *filename) if ((fd = fdopen (fildes, "w")) == NULL) { fprintf (stderr, - "%s: Could not write PID file %s: %s.", - PACKAGE, filename, strerror (errno)); + "%s: Could not write PID file %s: %s.", + PACKAGE, filename, strerror (errno)); close (fildes); unlink (filename); return -EIO; diff --git a/src/utils.h b/src/utils.h index 784a2ac..e6b0fc4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -28,10 +28,10 @@ struct conn_s; extern int send_http_message (struct conn_s *connptr, int http_code, - const char *error_title, const char *message); + const char *error_title, const char *message); extern int pidfile_create (const char *path); extern int create_file_safely (const char *filename, - unsigned int truncate_file); + unsigned int truncate_file); #endif diff --git a/src/vector.h b/src/vector.h index f319cad..dde4add 100644 --- a/src/vector.h +++ b/src/vector.h @@ -80,5 +80,5 @@ extern "C" #if defined(__cplusplus) } -#endif /* C++ */ -#endif /* _VECTOR_H */ +#endif /* C++ */ +#endif /* _VECTOR_H */ |