Convert tabs to spaces
This commit is contained in:
parent
6e6f992f8c
commit
024b317de0
140
src/acl.c
140
src/acl.c
@ -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;
|
||||
{
|
||||
/*
|
||||
* 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);
|
||||
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;
|
||||
}
|
||||
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_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_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
|
||||
|
@ -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)
|
||||
|
101
src/buffer.c
101
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
206
src/child.c
206
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);
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 ();
|
||||
{
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
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);
|
||||
{
|
||||
log_message (LOG_NOTICE,
|
||||
"Waiting servers (%d) is less than MinSpareServers (%d). "
|
||||
"Creating new child.",
|
||||
*servers_waiting, child_config.minspareservers);
|
||||
|
||||
SERVER_COUNT_UNLOCK ();
|
||||
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");
|
||||
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;
|
||||
}
|
||||
child_ptr[i].status = T_EMPTY;
|
||||
break;
|
||||
}
|
||||
|
||||
SERVER_INC ();
|
||||
SERVER_INC ();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
10
src/common.h
10
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.
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
162
src/filter.c
162
src/filter.c
@ -60,75 +60,75 @@ filter_init (void)
|
||||
{
|
||||
fd = fopen (config.filter, "r");
|
||||
if (fd)
|
||||
{
|
||||
p = NULL;
|
||||
{
|
||||
p = NULL;
|
||||
|
||||
cflags = REG_NEWLINE | REG_NOSUB;
|
||||
if (config.filter_extended)
|
||||
cflags |= REG_EXTENDED;
|
||||
if (!config.filter_casesensitive)
|
||||
cflags |= REG_ICASE;
|
||||
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';
|
||||
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 leading whitespace */
|
||||
s = buf;
|
||||
while (*s && isspace ((unsigned char) *s))
|
||||
s++;
|
||||
|
||||
/* skip blank lines and comments */
|
||||
if (*s == '\0')
|
||||
continue;
|
||||
/* 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;
|
||||
}
|
||||
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);
|
||||
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;
|
||||
}
|
||||
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:
|
||||
|
102
src/hashmap.c
102
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;
|
||||
}
|
||||
{
|
||||
if (strcasecmp (ptr->key, key) == 0)
|
||||
{
|
||||
/* Found it, so return the current count */
|
||||
return iter;
|
||||
}
|
||||
|
||||
iter++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
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;
|
||||
}
|
||||
{
|
||||
if (count == iter)
|
||||
{
|
||||
/* This is the data so return it */
|
||||
*key = ptr->key;
|
||||
*data = ptr->data;
|
||||
return ptr->len;
|
||||
}
|
||||
|
||||
ptr = ptr->next;
|
||||
count++;
|
||||
}
|
||||
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;
|
||||
{
|
||||
/*
|
||||
* 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 (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;
|
||||
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);
|
||||
safefree (ptr->key);
|
||||
safefree (ptr->data);
|
||||
safefree (ptr);
|
||||
|
||||
++deleted;
|
||||
--map->end_iterator;
|
||||
++deleted;
|
||||
--map->end_iterator;
|
||||
|
||||
ptr = next;
|
||||
continue;
|
||||
}
|
||||
ptr = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* This entry didn't contain the key; move to the next one */
|
||||
ptr = ptr->next;
|
||||
|
@ -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__)
|
||||
|
118
src/html-error.c
118
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;
|
||||
|
@ -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 */
|
||||
|
@ -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 */
|
||||
|
@ -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_ */
|
||||
|
34
src/log.c
34
src/log.c
@ -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:
|
||||
|
@ -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);
|
||||
}
|
||||
|
815
src/reqs.c
815
src/reqs.c
File diff suppressed because it is too large
Load Diff
@ -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)
|
||||
{
|
||||
&& 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)))
|
||||
{
|
||||
/* 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
50
src/sock.c
50
src/sock.c
@ -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);
|
||||
}
|
||||
|
20
src/stats.c
20
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;
|
||||
|
10
src/stats.h
10
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;
|
||||
|
206
src/tinyproxy.c
206
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)
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
94
src/utils.c
94
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;
|
||||
|
@ -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
|
||||
|
@ -80,5 +80,5 @@ extern "C"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif /* C++ */
|
||||
#endif /* _VECTOR_H */
|
||||
#endif /* C++ */
|
||||
#endif /* _VECTOR_H */
|
||||
|
Loading…
Reference in New Issue
Block a user