Convert tabs to spaces

This commit is contained in:
Mukund Sivaraman 2008-12-08 13:39:44 +00:00
parent 6e6f992f8c
commit 024b317de0
37 changed files with 1221 additions and 1233 deletions

140
src/acl.c
View File

@ -72,13 +72,13 @@ static vector_t access_list = NULL;
*/ */
inline static int inline static int
fill_netmask_array (char *bitmask_string, unsigned char array[], fill_netmask_array (char *bitmask_string, unsigned char array[],
unsigned int len) unsigned int len)
{ {
unsigned int i; unsigned int i;
long int mask; long int mask;
char *endptr; char *endptr;
errno = 0; /* to distinguish success/failure after call */ errno = 0; /* to distinguish success/failure after call */
mask = strtol (bitmask_string, &endptr, 10); mask = strtol (bitmask_string, &endptr, 10);
/* check for various conversion errors */ /* check for various conversion errors */
@ -94,19 +94,19 @@ fill_netmask_array (char *bitmask_string, unsigned char array[],
for (i = 0; i != len; ++i) for (i = 0; i != len; ++i)
{ {
if (mask >= 8) if (mask >= 8)
{ {
array[i] = 0xff; array[i] = 0xff;
mask -= 8; mask -= 8;
} }
else if (mask > 0) else if (mask > 0)
{ {
array[i] = (unsigned char) (0xff << (8 - mask)); array[i] = (unsigned char) (0xff << (8 - mask));
mask = 0; mask = 0;
} }
else else
{ {
array[i] = 0; array[i] = 0;
} }
} }
return 0; return 0;
@ -138,10 +138,10 @@ insert_acl (char *location, acl_access_t access_type)
{ {
access_list = vector_create (); access_list = vector_create ();
if (!access_list) if (!access_list)
{ {
log_message (LOG_ERR, "Unable to allocate memory for access list"); log_message (LOG_ERR, "Unable to allocate memory for access list");
return -1; return -1;
} }
} }
/* /*
@ -167,30 +167,30 @@ insert_acl (char *location, acl_access_t access_type)
*/ */
p = strchr (location, '/'); p = strchr (location, '/');
if (p != NULL) if (p != NULL)
{ {
/* /*
* We have a slash, so it's intended to be an * We have a slash, so it's intended to be an
* IP address with mask * IP address with mask
*/ */
*p = '\0'; *p = '\0';
if (full_inet_pton (location, ip_dst) <= 0) if (full_inet_pton (location, ip_dst) <= 0)
return -1; return -1;
acl.type = ACL_NUMERIC; acl.type = ACL_NUMERIC;
memcpy (acl.address.ip.octet, ip_dst, IPV6_LEN); memcpy (acl.address.ip.octet, ip_dst, IPV6_LEN);
if (fill_netmask_array (p + 1, &(acl.address.ip.mask[0]), IPV6_LEN) if (fill_netmask_array (p + 1, &(acl.address.ip.mask[0]), IPV6_LEN)
< 0) < 0)
return -1; return -1;
} }
else else
{ {
/* In all likelihood a string */ /* In all likelihood a string */
acl.type = ACL_STRING; acl.type = ACL_STRING;
acl.address.string = safestrdup (location); acl.address.string = safestrdup (location);
if (!acl.address.string) if (!acl.address.string)
return -1; return -1;
} }
} }
/* /*
@ -212,7 +212,7 @@ insert_acl (char *location, acl_access_t access_type)
*/ */
static int static int
acl_string_processing (struct acl_s *acl, 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; int match;
struct addrinfo hints, *res, *ressave; struct addrinfo hints, *res, *ressave;
@ -234,31 +234,31 @@ acl_string_processing (struct acl_s *acl,
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo (acl->address.string, NULL, &hints, &res) != 0) if (getaddrinfo (acl->address.string, NULL, &hints, &res) != 0)
goto STRING_TEST; goto STRING_TEST;
ressave = res; ressave = res;
match = FALSE; match = FALSE;
do do
{ {
get_ip_string (res->ai_addr, ipbuf, sizeof (ipbuf)); get_ip_string (res->ai_addr, ipbuf, sizeof (ipbuf));
if (strcmp (ip_address, ipbuf) == 0) if (strcmp (ip_address, ipbuf) == 0)
{ {
match = TRUE; match = TRUE;
break; break;
} }
} }
while ((res = res->ai_next) != NULL); while ((res = res->ai_next) != NULL);
freeaddrinfo (ressave); freeaddrinfo (ressave);
if (match) if (match)
{ {
if (acl->access == ACL_DENY) if (acl->access == ACL_DENY)
return 0; return 0;
else else
return 1; return 1;
} }
} }
STRING_TEST: STRING_TEST:
@ -277,9 +277,9 @@ STRING_TEST:
acl->address.string) == 0) acl->address.string) == 0)
{ {
if (acl->access == ACL_DENY) if (acl->access == ACL_DENY)
return 0; return 0;
else else
return 1; return 1;
} }
/* Indicate that no tests succeeded, so skip to next control. */ /* 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 and y don't match, the IP addresses don't match */
if (x != y) if (x != y)
return 0; return 0;
} }
/* The addresses match, return the permission */ /* 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); acl = vector_getentry (access_list, i, NULL);
switch (acl->type) switch (acl->type)
{ {
case ACL_STRING: case ACL_STRING:
perm = acl_string_processing (acl, ip, host); perm = acl_string_processing (acl, ip, host);
break; break;
case ACL_NUMERIC: case ACL_NUMERIC:
if (ip[0] == '\0') if (ip[0] == '\0')
continue; continue;
perm = check_numeric_acl (acl, ip); perm = check_numeric_acl (acl, ip);
break; break;
} }
/* /*
* Check the return value too see if the IP address is * Check the return value too see if the IP address is
* allowed or denied. * allowed or denied.
*/ */
if (perm == 0) if (perm == 0)
break; break;
else if (perm == 1) else if (perm == 1)
return perm; return perm;
} }
/* /*
* Deny all connections by default. * Deny all connections by default.
*/ */
log_message (LOG_NOTICE, "Unauthorized connection from \"%s\" [%s].", log_message (LOG_NOTICE, "Unauthorized connection from \"%s\" [%s].",
host, ip); host, ip);
return 0; return 0;
} }

View File

@ -26,6 +26,6 @@ typedef enum
extern int insert_acl (char *location, acl_access_t access_type); extern int insert_acl (char *location, acl_access_t access_type);
extern int check_acl (int fd, const char *ip_address, extern int check_acl (int fd, const char *ip_address,
const char *string_address); const char *string_address);
#endif #endif

View File

@ -65,7 +65,7 @@ anonymous_insert (char *s)
{ {
anonymous_map = hashmap_create (32); anonymous_map = hashmap_create (32);
if (!anonymous_map) if (!anonymous_map)
return -1; return -1;
} }
if (hashmap_search (anonymous_map, s) > 0) if (hashmap_search (anonymous_map, s) > 0)

View File

@ -36,10 +36,10 @@
struct bufline_s struct bufline_s
{ {
unsigned char *string; /* the actual string of data */ unsigned char *string; /* the actual string of data */
struct bufline_s *next; /* pointer to next in linked list */ struct bufline_s *next; /* pointer to next in linked list */
size_t length; /* length of the string of data */ size_t length; /* length of the string of data */
size_t pos; /* start sending from this offset */ size_t pos; /* start sending from this offset */
}; };
/* /*
@ -48,9 +48,9 @@ struct bufline_s
*/ */
struct buffer_s struct buffer_s
{ {
struct bufline_s *head; /* top of the buffer */ struct bufline_s *head; /* top of the buffer */
struct bufline_s *tail; /* bottom of the buffer */ struct bufline_s *tail; /* bottom of the buffer */
size_t size; /* total size 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 (bytesin > 0)
{ {
if (add_to_buffer (buffptr, buffer, bytesin) < 0) if (add_to_buffer (buffptr, buffer, bytesin) < 0)
{ {
log_message (LOG_ERR, "readbuff: add_to_buffer() error."); log_message (LOG_ERR, "readbuff: add_to_buffer() error.");
bytesin = -1; bytesin = -1;
} }
} }
else else
{ {
if (bytesin == 0) if (bytesin == 0)
{ {
/* connection was closed by client */ /* connection was closed by client */
bytesin = -1; bytesin = -1;
} }
else else
{ {
switch (errno) switch (errno)
{ {
#ifdef EWOULDBLOCK #ifdef EWOULDBLOCK
case EWOULDBLOCK: case EWOULDBLOCK:
#else #else
# ifdef EAGAIN # ifdef EAGAIN
case EAGAIN: case EAGAIN:
# endif # endif
#endif #endif
case EINTR: case EINTR:
bytesin = 0; bytesin = 0;
break; break;
default: default:
log_message (LOG_ERR, log_message (LOG_ERR,
"readbuff: recv() error \"%s\" on file descriptor %d", "readbuff: recv() error \"%s\" on file descriptor %d",
strerror (errno), fd); strerror (errno), fd);
bytesin = -1; bytesin = -1;
break; break;
} }
} }
} }
safefree (buffer); safefree (buffer);
@ -306,41 +306,40 @@ write_buffer (int fd, struct buffer_s * buffptr)
bytessent = bytessent =
send (fd, line->string + line->pos, line->length - line->pos, send (fd, line->string + line->pos, line->length - line->pos,
MSG_NOSIGNAL); MSG_NOSIGNAL);
if (bytessent >= 0) if (bytessent >= 0)
{ {
/* bytes sent, adjust buffer */ /* bytes sent, adjust buffer */
line->pos += bytessent; line->pos += bytessent;
if (line->pos == line->length) if (line->pos == line->length)
free_line (remove_from_buffer (buffptr)); free_line (remove_from_buffer (buffptr));
return bytessent; return bytessent;
} }
else else
{ {
switch (errno) switch (errno)
{ {
#ifdef EWOULDBLOCK #ifdef EWOULDBLOCK
case EWOULDBLOCK: case EWOULDBLOCK:
#else #else
# ifdef EAGAIN # ifdef EAGAIN
case EAGAIN: case EAGAIN:
# endif # endif
#endif #endif
case EINTR: case EINTR:
return 0; return 0;
case ENOBUFS: case ENOBUFS:
case ENOMEM: case ENOMEM:
log_message (LOG_ERR, log_message (LOG_ERR,
"writebuff: write() error [NOBUFS/NOMEM] \"%s\" on " "writebuff: write() error [NOBUFS/NOMEM] \"%s\" on "
"file descriptor %d", "file descriptor %d", strerror (errno), fd);
strerror (errno), fd); return 0;
return 0; default:
default: log_message (LOG_ERR,
log_message (LOG_ERR, "writebuff: write() error \"%s\" on file descriptor %d",
"writebuff: write() error \"%s\" on file descriptor %d", strerror (errno), fd);
strerror (errno), fd); return -1;
return -1; }
}
} }
} }

View File

@ -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. * 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, 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 read_buffer (int fd, struct buffer_s *buffptr);
extern ssize_t write_buffer (int fd, struct buffer_s *buffptr); extern ssize_t write_buffer (int fd, struct buffer_s *buffptr);

View File

@ -58,7 +58,7 @@ static struct child_config_s
int maxspareservers, minspareservers, startservers; int maxspareservers, minspareservers, startservers;
} child_config; } 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 * 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) while ((rc = fcntl (lock_fd, F_SETLKW, &lock_it)) < 0)
{ {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
else else
return; return;
} }
} }
@ -202,26 +202,26 @@ child_main (struct child_s *ptr)
* want to use the GDB debugger. * want to use the GDB debugger.
*/ */
if (getenv ("TINYPROXY_DEBUG")) if (getenv ("TINYPROXY_DEBUG"))
{ {
/* Pause for 10 seconds to allow us to connect debugger */ /* Pause for 10 seconds to allow us to connect debugger */
fprintf (stderr, fprintf (stderr,
"Process has accepted connection: %ld\n", "Process has accepted connection: %ld\n",
(long int) ptr->tid); (long int) ptr->tid);
sleep (10); sleep (10);
fprintf (stderr, "Continuing process: %ld\n", (long int) ptr->tid); fprintf (stderr, "Continuing process: %ld\n", (long int) ptr->tid);
} }
#endif #endif
/* /*
* Make sure no error occurred... * Make sure no error occurred...
*/ */
if (connfd < 0) if (connfd < 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"Accept returned an error (%s) ... retrying.", "Accept returned an error (%s) ... retrying.",
strerror (errno)); strerror (errno));
continue; continue;
} }
ptr->status = T_CONNECTED; ptr->status = T_CONNECTED;
@ -231,38 +231,37 @@ child_main (struct child_s *ptr)
ptr->connects++; ptr->connects++;
if (child_config.maxrequestsperchild != 0) 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) if (ptr->connects == child_config.maxrequestsperchild)
{ {
log_message (LOG_NOTICE, log_message (LOG_NOTICE,
"Child has reached MaxRequestsPerChild (%u). " "Child has reached MaxRequestsPerChild (%u). "
"Killing child.", "Killing child.", ptr->connects);
ptr->connects); break;
break; }
} }
}
SERVER_COUNT_LOCK (); SERVER_COUNT_LOCK ();
if (*servers_waiting > child_config.maxspareservers) if (*servers_waiting > child_config.maxspareservers)
{ {
/* /*
* There are too many spare children, kill ourself * There are too many spare children, kill ourself
* off. * off.
*/ */
log_message (LOG_NOTICE, log_message (LOG_NOTICE,
"Waiting servers (%d) exceeds MaxSpareServers (%d). " "Waiting servers (%d) exceeds MaxSpareServers (%d). "
"Killing child.", "Killing child.",
*servers_waiting, child_config.maxspareservers); *servers_waiting, child_config.maxspareservers);
SERVER_COUNT_UNLOCK (); SERVER_COUNT_UNLOCK ();
break; break;
} }
else else
{ {
SERVER_COUNT_UNLOCK (); SERVER_COUNT_UNLOCK ();
} }
SERVER_INC (); SERVER_INC ();
} }
@ -283,7 +282,7 @@ child_make (struct child_s *ptr)
pid_t pid; pid_t pid;
if ((pid = fork ()) > 0) if ((pid = fork ()) > 0)
return pid; /* parent */ return pid; /* parent */
/* /*
* Reset the SIGNALS so that the child can be reaped. * 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 (SIGTERM, SIG_DFL);
set_signal_handler (SIGHUP, SIG_DFL); set_signal_handler (SIGHUP, SIG_DFL);
child_main (ptr); /* never returns */ child_main (ptr); /* never returns */
return -1; return -1;
} }
@ -312,20 +311,20 @@ child_pool_create (void)
if (child_config.maxclients == 0) if (child_config.maxclients == 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"child_pool_create: \"MaxClients\" must be " "child_pool_create: \"MaxClients\" must be "
"greater than zero."); "greater than zero.");
return -1; return -1;
} }
if (child_config.startservers == 0) if (child_config.startservers == 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"child_pool_create: \"StartServers\" must be " "child_pool_create: \"StartServers\" must be "
"greater than zero."); "greater than zero.");
return -1; return -1;
} }
child_ptr = calloc_shared_memory (child_config.maxclients, child_ptr = calloc_shared_memory (child_config.maxclients,
sizeof (struct child_s)); sizeof (struct child_s));
if (!child_ptr) if (!child_ptr)
{ {
log_message (LOG_ERR, "Could not allocate memory for children."); 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) if (child_config.startservers > child_config.maxclients)
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Can not start more than \"MaxClients\" servers. " "Can not start more than \"MaxClients\" servers. "
"Starting %u servers instead.", "Starting %u servers instead.", child_config.maxclients);
child_config.maxclients);
child_config.startservers = 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++) for (i = 0; i != child_config.startservers; i++)
{ {
DEBUG2 ("Trying to create child %d of %d", i + 1, 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].status = T_WAITING;
child_ptr[i].tid = child_make (&child_ptr[i]); child_ptr[i].tid = child_make (&child_ptr[i]);
if (child_ptr[i].tid < 0) if (child_ptr[i].tid < 0)
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Could not create child number %d of %d", "Could not create child number %d of %d",
i, child_config.startservers); i, child_config.startservers);
return -1; return -1;
} }
else else
{ {
log_message (LOG_INFO, log_message (LOG_INFO,
"Creating child number %d of %d ...", "Creating child number %d of %d ...",
i + 1, child_config.startservers); i + 1, child_config.startservers);
SERVER_INC (); SERVER_INC ();
} }
} }
log_message (LOG_INFO, "Finished creating all children."); log_message (LOG_INFO, "Finished creating all children.");
@ -402,62 +400,62 @@ child_main_loop (void)
while (1) while (1)
{ {
if (config.quit) if (config.quit)
return; return;
/* If there are not enough spare servers, create more */ /* If there are not enough spare servers, create more */
SERVER_COUNT_LOCK (); SERVER_COUNT_LOCK ();
if (*servers_waiting < child_config.minspareservers) if (*servers_waiting < child_config.minspareservers)
{ {
log_message (LOG_NOTICE, log_message (LOG_NOTICE,
"Waiting servers (%d) is less than MinSpareServers (%d). " "Waiting servers (%d) is less than MinSpareServers (%d). "
"Creating new child.", "Creating new child.",
*servers_waiting, child_config.minspareservers); *servers_waiting, child_config.minspareservers);
SERVER_COUNT_UNLOCK (); SERVER_COUNT_UNLOCK ();
for (i = 0; i != child_config.maxclients; i++) for (i = 0; i != child_config.maxclients; i++)
{ {
if (child_ptr[i].status == T_EMPTY) if (child_ptr[i].status == T_EMPTY)
{ {
child_ptr[i].status = T_WAITING; child_ptr[i].status = T_WAITING;
child_ptr[i].tid = child_make (&child_ptr[i]); child_ptr[i].tid = child_make (&child_ptr[i]);
if (child_ptr[i].tid < 0) if (child_ptr[i].tid < 0)
{ {
log_message (LOG_NOTICE, "Could not create child"); log_message (LOG_NOTICE, "Could not create child");
child_ptr[i].status = T_EMPTY; child_ptr[i].status = T_EMPTY;
break; break;
} }
SERVER_INC (); SERVER_INC ();
break; break;
} }
} }
} }
else else
{ {
SERVER_COUNT_UNLOCK (); SERVER_COUNT_UNLOCK ();
} }
sleep (5); sleep (5);
/* Handle log rotation if it was requested */ /* Handle log rotation if it was requested */
if (received_sighup) if (received_sighup)
{ {
truncate_log_file (); truncate_log_file ();
#ifdef FILTER_ENABLE #ifdef FILTER_ENABLE
if (config.filter) if (config.filter)
{ {
filter_destroy (); filter_destroy ();
filter_init (); filter_init ();
} }
log_message (LOG_NOTICE, "Re-reading filter file."); log_message (LOG_NOTICE, "Re-reading filter file.");
#endif /* FILTER_ENABLE */ #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++) for (i = 0; i != child_config.maxclients; i++)
{ {
if (child_ptr[i].status != T_EMPTY) if (child_ptr[i].status != T_EMPTY)
kill (child_ptr[i].tid, SIGTERM); kill (child_ptr[i].tid, SIGTERM);
} }
} }

View File

@ -173,13 +173,13 @@
# define MSG_NOSIGNAL (0) # define MSG_NOSIGNAL (0)
#endif #endif
#ifndef SHUT_RD /* these three Posix.1g names are quite new */ #ifndef SHUT_RD /* these three Posix.1g names are quite new */
# define SHUT_RD 0 /* shutdown for reading */ # define SHUT_RD 0 /* shutdown for reading */
# define SHUT_WR 1 /* shutdown for writing */ # define SHUT_WR 1 /* shutdown for writing */
# define SHUT_RDWR 2 /* shutdown for reading and writing */ # define SHUT_RDWR 2 /* shutdown for reading and writing */
#endif #endif
#define MAXLISTEN 1024 /* Max number of connections */ #define MAXLISTEN 1024 /* Max number of connections */
/* /*
* SunOS doesn't have INADDR_NONE defined. * SunOS doesn't have INADDR_NONE defined.

View File

@ -67,7 +67,7 @@
* with the same function template as below. * with the same function template as below.
*/ */
typedef int (*CONFFILE_HANDLER) (struct config_s *, const char *, typedef int (*CONFFILE_HANDLER) (struct config_s *, const char *,
regmatch_t[]); regmatch_t[]);
/* /*
* Define the pattern used by any directive handling function. The * Define the pattern used by any directive handling function. The
@ -92,7 +92,7 @@ static
HANDLE_FUNC (handle_nop) HANDLE_FUNC (handle_nop)
{ {
return 0; return 0;
} /* do nothing function */ } /* do nothing function */
static HANDLE_FUNC (handle_allow); static HANDLE_FUNC (handle_allow);
static HANDLE_FUNC (handle_anonymous); static HANDLE_FUNC (handle_anonymous);
@ -228,7 +228,7 @@ struct
#endif #endif
/* loglevel */ /* loglevel */
STDCONF ("loglevel", "(critical|error|warning|notice|connect|info)", STDCONF ("loglevel", "(critical|error|warning|notice|connect|info)",
handle_loglevel)}; handle_loglevel)};
const unsigned int ndirectives = sizeof (directives) / sizeof (directives[0]); const unsigned int ndirectives = sizeof (directives) / sizeof (directives[0]);
/* /*
@ -249,12 +249,12 @@ config_compile (void)
directives[i].cre = safemalloc (sizeof (regex_t)); directives[i].cre = safemalloc (sizeof (regex_t));
if (!directives[i].cre) if (!directives[i].cre)
return -1; return -1;
r = regcomp (directives[i].cre, 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) if (r)
return r; return r;
} }
return 0; return 0;
} }
@ -279,7 +279,7 @@ check_match (struct config_s *conf, const char *line)
{ {
assert (directives[i].cre); assert (directives[i].cre);
if (!regexec (directives[i].cre, line, RE_MAX_MATCHES, match, 0)) 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; return -1;
@ -291,16 +291,16 @@ check_match (struct config_s *conf, const char *line)
int int
config_parse (struct config_s *conf, FILE * f) 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; unsigned long lineno = 1;
while (fgets (buffer, sizeof (buffer), f)) while (fgets (buffer, sizeof (buffer), f))
{ {
if (check_match (conf, buffer)) if (check_match (conf, buffer))
{ {
printf ("Syntax error on line %ld\n", lineno); printf ("Syntax error on line %ld\n", lineno);
return 1; return 1;
} }
++lineno; ++lineno;
} }
return 0; return 0;
@ -440,7 +440,7 @@ HANDLE_FUNC (handle_viaproxyname)
if (r) if (r)
return r; return r;
log_message (LOG_INFO, 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; return 0;
} }
@ -474,7 +474,7 @@ HANDLE_FUNC (handle_xtinyproxy)
return set_string_arg (&conf->my_domain, line, &match[2]); return set_string_arg (&conf->my_domain, line, &match[2]);
#else #else
fprintf (stderr, fprintf (stderr,
"XTinyproxy NOT Enabled! Recompile with --enable-xtinyproxy\n"); "XTinyproxy NOT Enabled! Recompile with --enable-xtinyproxy\n");
return 1; return 1;
#endif #endif
} }
@ -596,11 +596,11 @@ HANDLE_FUNC (handle_bind)
if (r) if (r)
return r; return r;
log_message (LOG_INFO, log_message (LOG_INFO,
"Outgoing connections bound to IP %s", conf->bind_address); "Outgoing connections bound to IP %s", conf->bind_address);
return 0; return 0;
#else #else
fprintf (stderr, fprintf (stderr,
"\"Bind\" cannot be used with transparent support enabled.\n"); "\"Bind\" cannot be used with transparent support enabled.\n");
return 1; return 1;
#endif #endif
} }
@ -663,11 +663,11 @@ HANDLE_FUNC (handle_loglevel)
for (i = 0; i != nlevels; ++i) for (i = 0; i != nlevels; ++i)
{ {
if (!strcasecmp (arg, log_levels[i].string)) if (!strcasecmp (arg, log_levels[i].string))
{ {
set_log_level (log_levels[i].level); set_log_level (log_levels[i].level);
safefree (arg); safefree (arg);
return 0; return 0;
} }
} }
safefree (arg); safefree (arg);
@ -745,10 +745,10 @@ HANDLE_FUNC (handle_reversepath)
{ {
arg2 = get_string_arg (line, &match[3]); arg2 = get_string_arg (line, &match[3]);
if (!arg2) if (!arg2)
{ {
safefree (arg1); safefree (arg1);
return -1; return -1;
} }
reversepath_add (arg1, arg2); reversepath_add (arg1, arg2);
safefree (arg1); safefree (arg1);
safefree (arg2); safefree (arg2);
@ -779,10 +779,10 @@ HANDLE_FUNC (handle_upstream)
{ {
domain = get_string_arg (line, &match[9]); domain = get_string_arg (line, &match[9]);
if (domain) if (domain)
{ {
upstream_add (ip, port, domain); upstream_add (ip, port, domain);
safefree (domain); safefree (domain);
} }
} }
else else
{ {

View File

@ -32,7 +32,7 @@
struct conn_s * struct conn_s *
initialize_conn (int client_fd, const char *ipaddr, const char *string_addr, 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 conn_s *connptr;
struct buffer_s *cbuffer, *sbuffer; struct buffer_s *cbuffer, *sbuffer;
@ -110,11 +110,11 @@ destroy_conn (struct conn_s *connptr)
if (connptr->client_fd != -1) if (connptr->client_fd != -1)
if (close (connptr->client_fd) < 0) if (close (connptr->client_fd) < 0)
log_message (LOG_INFO, "Client (%d) close message: %s", 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 (connptr->server_fd != -1)
if (close (connptr->server_fd) < 0) if (close (connptr->server_fd) < 0)
log_message (LOG_INFO, "Server (%d) close message: %s", log_message (LOG_INFO, "Server (%d) close message: %s",
connptr->server_fd, strerror (errno)); connptr->server_fd, strerror (errno));
if (connptr->cbuffer) if (connptr->cbuffer)
delete_buffer (connptr->cbuffer); delete_buffer (connptr->cbuffer);

View File

@ -95,8 +95,8 @@ struct conn_s
* Functions for the creation and destruction of a connection structure. * Functions for the creation and destruction of a connection structure.
*/ */
extern struct conn_s *initialize_conn (int client_fd, const char *ipaddr, extern struct conn_s *initialize_conn (int client_fd, const char *ipaddr,
const char *string_addr, const char *string_addr,
const char *sock_ipaddr); const char *sock_ipaddr);
extern void destroy_conn (struct conn_s *connptr); extern void destroy_conn (struct conn_s *connptr);
#endif #endif

View File

@ -70,13 +70,13 @@ set_signal_handler (int signo, signal_func * func)
if (signo == SIGALRM) if (signo == SIGALRM)
{ {
#ifdef SA_INTERRUPT #ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */ act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
#endif #endif
} }
else else
{ {
#ifdef SA_RESTART #ifdef SA_RESTART
act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */ act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */
#endif #endif
} }

View File

@ -60,75 +60,75 @@ filter_init (void)
{ {
fd = fopen (config.filter, "r"); fd = fopen (config.filter, "r");
if (fd) if (fd)
{ {
p = NULL; p = NULL;
cflags = REG_NEWLINE | REG_NOSUB; cflags = REG_NEWLINE | REG_NOSUB;
if (config.filter_extended) if (config.filter_extended)
cflags |= REG_EXTENDED; cflags |= REG_EXTENDED;
if (!config.filter_casesensitive) if (!config.filter_casesensitive)
cflags |= REG_ICASE; cflags |= REG_ICASE;
while (fgets (buf, FILTER_BUFFER_LEN, fd)) while (fgets (buf, FILTER_BUFFER_LEN, fd))
{ {
/* /*
* Remove any trailing white space and * Remove any trailing white space and
* comments. * comments.
*/ */
s = buf; s = buf;
while (*s) while (*s)
{ {
if (isspace ((unsigned char) *s)) if (isspace ((unsigned char) *s))
break; break;
if (*s == '#') if (*s == '#')
{ {
/* /*
* If the '#' char is preceeded by * If the '#' char is preceeded by
* an escape, it's not a comment * an escape, it's not a comment
* string. * string.
*/ */
if (s == buf || *(s - 1) != '\\') if (s == buf || *(s - 1) != '\\')
break; break;
} }
++s; ++s;
} }
*s = '\0'; *s = '\0';
/* skip leading whitespace */ /* skip leading whitespace */
s = buf; s = buf;
while (*s && isspace ((unsigned char) *s)) while (*s && isspace ((unsigned char) *s))
s++; s++;
/* skip blank lines and comments */ /* skip blank lines and comments */
if (*s == '\0') if (*s == '\0')
continue; continue;
if (!p) /* head of list */ if (!p) /* head of list */
fl = p = safecalloc (1, sizeof (struct filter_list)); fl = p = safecalloc (1, sizeof (struct filter_list));
else else
{ /* next entry */ { /* next entry */
p->next = safecalloc (1, sizeof (struct filter_list)); p->next = safecalloc (1, sizeof (struct filter_list));
p = p->next; p = p->next;
} }
p->pat = safestrdup (s); p->pat = safestrdup (s);
p->cpat = safemalloc (sizeof (regex_t)); p->cpat = safemalloc (sizeof (regex_t));
if ((err = regcomp (p->cpat, p->pat, cflags)) != 0) if ((err = regcomp (p->cpat, p->pat, cflags)) != 0)
{ {
fprintf (stderr, "Bad regex in %s: %s\n", fprintf (stderr, "Bad regex in %s: %s\n",
config.filter, p->pat); config.filter, p->pat);
exit (EX_DATAERR); exit (EX_DATAERR);
} }
} }
if (ferror (fd)) if (ferror (fd))
{ {
perror ("fgets"); perror ("fgets");
exit (EX_DATAERR); exit (EX_DATAERR);
} }
fclose (fd); fclose (fd);
already_init = 1; already_init = 1;
} }
} }
} }
@ -141,13 +141,13 @@ filter_destroy (void)
if (already_init) if (already_init)
{ {
for (p = q = fl; p; p = q) for (p = q = fl; p; p = q)
{ {
regfree (p->cpat); regfree (p->cpat);
safefree (p->cpat); safefree (p->cpat);
safefree (p->pat); safefree (p->pat);
q = p->next; q = p->next;
safefree (p); safefree (p);
} }
fl = NULL; fl = NULL;
already_init = 0; already_init = 0;
} }
@ -168,12 +168,12 @@ filter_domain (const char *host)
result = regexec (p->cpat, host, (size_t) 0, (regmatch_t *) 0, 0); result = regexec (p->cpat, host, (size_t) 0, (regmatch_t *) 0, 0);
if (result == 0) if (result == 0)
{ {
if (default_policy == FILTER_DEFAULT_ALLOW) if (default_policy == FILTER_DEFAULT_ALLOW)
return 1; return 1;
else else
return 0; return 0;
} }
} }
COMMON_EXIT: COMMON_EXIT:
@ -198,12 +198,12 @@ filter_url (const char *url)
result = regexec (p->cpat, url, (size_t) 0, (regmatch_t *) 0, 0); result = regexec (p->cpat, url, (size_t) 0, (regmatch_t *) 0, 0);
if (result == 0) if (result == 0)
{ {
if (default_policy == FILTER_DEFAULT_ALLOW) if (default_policy == FILTER_DEFAULT_ALLOW)
return 1; return 1;
else else
return 0; return 0;
} }
} }
COMMON_EXIT: COMMON_EXIT:

View File

@ -172,9 +172,9 @@ hashmap_delete (hashmap_t map)
for (i = 0; i != map->size; i++) for (i = 0; i != map->size; i++)
{ {
if (map->buckets[i].head != NULL) if (map->buckets[i].head != NULL)
{ {
delete_hashbucket (&map->buckets[i]); delete_hashbucket (&map->buckets[i]);
} }
} }
safefree (map->buckets); safefree (map->buckets);
@ -329,16 +329,16 @@ hashmap_find (hashmap_t map, const char *key)
ptr = map->buckets[i].head; ptr = map->buckets[i].head;
while (ptr) while (ptr)
{ {
if (strcasecmp (ptr->key, key) == 0) if (strcasecmp (ptr->key, key) == 0)
{ {
/* Found it, so return the current count */ /* Found it, so return the current count */
return iter; return iter;
} }
iter++; iter++;
ptr = ptr->next; ptr = ptr->next;
} }
} }
return iter; return iter;
@ -352,7 +352,7 @@ hashmap_find (hashmap_t map, const char *key)
*/ */
ssize_t ssize_t
hashmap_return_entry (hashmap_t map, hashmap_iter iter, char **key, hashmap_return_entry (hashmap_t map, hashmap_iter iter, char **key,
void **data) void **data)
{ {
unsigned int i; unsigned int i;
struct hashentry_s *ptr; struct hashentry_s *ptr;
@ -371,18 +371,18 @@ hashmap_return_entry (hashmap_t map, hashmap_iter iter, char **key,
{ {
ptr = map->buckets[i].head; ptr = map->buckets[i].head;
while (ptr) while (ptr)
{ {
if (count == iter) if (count == iter)
{ {
/* This is the data so return it */ /* This is the data so return it */
*key = ptr->key; *key = ptr->key;
*data = ptr->data; *data = ptr->data;
return ptr->len; return ptr->len;
} }
ptr = ptr->next; ptr = ptr->next;
count++; count++;
} }
} }
return -EFAULT; return -EFAULT;
@ -415,7 +415,7 @@ hashmap_search (hashmap_t map, const char *key)
while (ptr) while (ptr)
{ {
if (strcasecmp (ptr->key, key) == 0) if (strcasecmp (ptr->key, key) == 0)
++count; ++count;
/* This entry didn't contain the key; move to the next one */ /* This entry didn't contain the key; move to the next one */
ptr = ptr->next; ptr = ptr->next;
@ -450,10 +450,10 @@ hashmap_entry_by_key (hashmap_t map, const char *key, void **data)
while (ptr) while (ptr)
{ {
if (strcasecmp (ptr->key, key) == 0) if (strcasecmp (ptr->key, key) == 0)
{ {
*data = ptr->data; *data = ptr->data;
return ptr->len; return ptr->len;
} }
ptr = ptr->next; ptr = ptr->next;
} }
@ -487,33 +487,33 @@ hashmap_remove (hashmap_t map, const char *key)
while (ptr) while (ptr)
{ {
if (strcasecmp (ptr->key, key) == 0) if (strcasecmp (ptr->key, key) == 0)
{ {
/* /*
* Found the data, now need to remove everything * Found the data, now need to remove everything
* and update the hashmap. * and update the hashmap.
*/ */
next = ptr->next; next = ptr->next;
if (ptr->prev) if (ptr->prev)
ptr->prev->next = ptr->next; ptr->prev->next = ptr->next;
if (ptr->next) if (ptr->next)
ptr->next->prev = ptr->prev; ptr->next->prev = ptr->prev;
if (map->buckets[hash].head == ptr) if (map->buckets[hash].head == ptr)
map->buckets[hash].head = ptr->next; map->buckets[hash].head = ptr->next;
if (map->buckets[hash].tail == ptr) if (map->buckets[hash].tail == ptr)
map->buckets[hash].tail = ptr->prev; map->buckets[hash].tail = ptr->prev;
safefree (ptr->key); safefree (ptr->key);
safefree (ptr->data); safefree (ptr->data);
safefree (ptr); safefree (ptr);
++deleted; ++deleted;
--map->end_iterator; --map->end_iterator;
ptr = next; ptr = next;
continue; continue;
} }
/* This entry didn't contain the key; move to the next one */ /* This entry didn't contain the key; move to the next one */
ptr = ptr->next; ptr = ptr->next;

View File

@ -52,7 +52,7 @@ extern "C"
* 0 upon successful insert * 0 upon successful insert
*/ */
extern int hashmap_insert (hashmap_t map, const char *key, 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. * Get an iterator to the first entry.
@ -88,7 +88,7 @@ extern "C"
* negative upon error * negative upon error
*/ */
extern ssize_t hashmap_return_entry (hashmap_t map, hashmap_iter iter, 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 * 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 * length of data for the entry
*/ */
extern ssize_t hashmap_entry_by_key (hashmap_t map, const char *key, 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 * Searches for _any_ occurrances of "key" within the hashmap and returns the
@ -123,5 +123,5 @@ extern "C"
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif /* C++ */ #endif /* C++ */
#endif /* _HASHMAP_H */ #endif /* _HASHMAP_H */

View File

@ -29,7 +29,7 @@
void * void *
debugging_calloc (size_t nmemb, size_t size, const char *file, debugging_calloc (size_t nmemb, size_t size, const char *file,
unsigned long line) unsigned long line)
{ {
void *ptr; void *ptr;
@ -38,7 +38,7 @@ debugging_calloc (size_t nmemb, size_t size, const char *file,
ptr = calloc (nmemb, size); ptr = calloc (nmemb, size);
fprintf (stderr, "{calloc: %p:%zu x %zu} %s:%lu\n", ptr, nmemb, size, file, fprintf (stderr, "{calloc: %p:%zu x %zu} %s:%lu\n", ptr, nmemb, size, file,
line); line);
return ptr; return ptr;
} }
@ -56,7 +56,7 @@ debugging_malloc (size_t size, const char *file, unsigned long line)
void * void *
debugging_realloc (void *ptr, size_t size, const char *file, debugging_realloc (void *ptr, size_t size, const char *file,
unsigned long line) unsigned long line)
{ {
void *newptr; void *newptr;
@ -64,7 +64,7 @@ debugging_realloc (void *ptr, size_t size, const char *file,
newptr = realloc (ptr, size); newptr = realloc (ptr, size);
fprintf (stderr, "{realloc: %p -> %p:%zu} %s:%lu\n", ptr, newptr, size, fprintf (stderr, "{realloc: %p -> %p:%zu} %s:%lu\n", ptr, newptr, size,
file, line); file, line);
return newptr; return newptr;
} }

View File

@ -27,14 +27,14 @@
#ifndef NDEBUG #ifndef NDEBUG
extern void *debugging_calloc (size_t nmemb, size_t size, const char *file, 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, 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_free (void *ptr, const char *file, unsigned long line);
extern void *debugging_realloc (void *ptr, size_t size, const char *file, 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, 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 safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__)
# define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__) # define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__)

View File

@ -33,7 +33,7 @@
/* /*
* Add an error number -> filename mapping to the errorpages list. * 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 #define ERRPAGES_BUCKETCOUNT 16
int int
@ -48,7 +48,7 @@ add_new_errorpage (char *filepath, unsigned int errornum)
snprintf (errornbuf, ERRORNUM_BUFSIZE, "%u", errornum); snprintf (errornbuf, ERRORNUM_BUFSIZE, "%u", errornum);
if (hashmap_insert (config.errorpages, errornbuf, if (hashmap_insert (config.errorpages, errornbuf,
filepath, strlen (filepath) + 1) < 0) filepath, strlen (filepath) + 1) < 0)
return (-1); return (-1);
return (0); return (0);
@ -78,7 +78,7 @@ get_html_file (unsigned int errornum)
return (config.errorpage_undef); return (config.errorpage_undef);
if (hashmap_return_entry (config.errorpages, result_iter, if (hashmap_return_entry (config.errorpages, result_iter,
&key, (void **) &val) < 0) &key, (void **) &val) < 0)
return (config.errorpage_undef); return (config.errorpage_undef);
return (val); return (val);
@ -100,7 +100,7 @@ lookup_variable (struct conn_s *connptr, char *varname)
return (NULL); return (NULL);
if (hashmap_return_entry (connptr->error_variables, result_iter, if (hashmap_return_entry (connptr->error_variables, result_iter,
&key, (void **) &data) < 0) &key, (void **) &data) < 0)
return (NULL); return (NULL);
return (data); return (data);
@ -121,51 +121,51 @@ send_html_file (FILE * infile, struct conn_s *connptr)
while (fgets (inbuf, HTML_BUFSIZE, infile) != NULL) while (fgets (inbuf, HTML_BUFSIZE, infile) != NULL)
{ {
for (p = inbuf; *p; p++) for (p = inbuf; *p; p++)
{ {
switch (*p) switch (*p)
{ {
case '}': case '}':
if (in_variable) if (in_variable)
{ {
*p = '\0'; *p = '\0';
if (!(varval = lookup_variable (connptr, varstart))) if (!(varval = lookup_variable (connptr, varstart)))
varval = "(unknown)"; varval = "(unknown)";
writeret = write_message (connptr->client_fd, "%s", varval); writeret = write_message (connptr->client_fd, "%s", varval);
if (writeret) if (writeret)
return (writeret); return (writeret);
in_variable = 0; in_variable = 0;
} }
else else
{ {
writeret = write_message (connptr->client_fd, "%c", *p); writeret = write_message (connptr->client_fd, "%c", *p);
if (writeret) if (writeret)
return (writeret); return (writeret);
} }
break; break;
case '{': case '{':
/* a {{ will print a single {. If we are NOT /* a {{ will print a single {. If we are NOT
* already in a { variable, then proceed with * already in a { variable, then proceed with
* setup. If we ARE already in a { variable, * setup. If we ARE already in a { variable,
* this code will fallthrough to the code that * this code will fallthrough to the code that
* just dumps a character to the client fd. * just dumps a character to the client fd.
*/ */
if (!in_variable) if (!in_variable)
{ {
varstart = p + 1; varstart = p + 1;
in_variable++; in_variable++;
} }
else else
in_variable = 0; in_variable = 0;
default: default:
if (!in_variable) if (!in_variable)
{ {
writeret = write_message (connptr->client_fd, "%c", *p); writeret = write_message (connptr->client_fd, "%c", *p);
if (writeret) if (writeret)
return (writeret); return (writeret);
} }
} }
} }
in_variable = 0; in_variable = 0;
} }
return (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"; "Content-Type: text/html\r\n" "Connection: close\r\n" "\r\n";
return (write_message (connptr->client_fd, headers, 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"); char *detail = lookup_variable (connptr, "detail");
return (write_message (connptr->client_fd, fallback_error, return (write_message (connptr->client_fd, fallback_error,
connptr->error_number, connptr->error_number,
connptr->error_string, connptr->error_string,
connptr->error_string, connptr->error_string,
detail, PACKAGE, VERSION)); detail, PACKAGE, VERSION));
} }
ret = send_html_file (infile, connptr); ret = send_html_file (infile, connptr);
@ -236,7 +236,7 @@ add_error_variable (struct conn_s *connptr, char *key, char *val)
return (-1); return (-1);
return hashmap_insert (connptr->error_variables, key, val, return hashmap_insert (connptr->error_variables, key, val,
strlen (val) + 1); strlen (val) + 1);
} }
#define ADD_VAR_RET(x, y) \ #define ADD_VAR_RET(x, y) \
@ -272,7 +272,7 @@ add_standard_vars (struct conn_s *connptr)
global_time = time (NULL); global_time = time (NULL);
strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT", strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT",
gmtime (&global_time)); gmtime (&global_time));
add_error_variable (connptr, "date", timebuf); add_error_variable (connptr, "date", timebuf);
add_error_variable (connptr, "website", "http://www.banu.com/tinyproxy/"); 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 *); val = va_arg (ap, char *);
if (add_error_variable (connptr, key, val) == -1) if (add_error_variable (connptr, key, val) == -1)
{ {
va_end (ap); va_end (ap);
return (-1); return (-1);
} }
} }
connptr->error_number = number; connptr->error_number = number;

View File

@ -27,11 +27,11 @@ struct conn_s;
extern int add_new_errorpage (char *filepath, unsigned int errornum); extern int add_new_errorpage (char *filepath, unsigned int errornum);
extern int send_http_error_message (struct conn_s *connptr); extern int send_http_error_message (struct conn_s *connptr);
extern int indicate_http_error (struct conn_s *connptr, int number, 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 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_html_file (FILE * infile, struct conn_s *connptr);
extern int send_http_headers (struct conn_s *connptr, int code, extern int send_http_headers (struct conn_s *connptr, int code,
char *message); char *message);
extern int add_standard_vars (struct conn_s *connptr); extern int add_standard_vars (struct conn_s *connptr);
#endif /* !TINYPROXY_HTML_ERROR_H */ #endif /* !TINYPROXY_HTML_ERROR_H */

View File

@ -144,7 +144,7 @@ http_message_destroy (http_message_t msg)
*/ */
int int
http_message_set_response (http_message_t msg, 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 */ /* Check for valid arguments */
if (msg == NULL) 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 *)); new_headers = safecalloc (msg->headers.total * 2, sizeof (char *));
if (new_headers == NULL) if (new_headers == NULL)
return -ENOMEM; return -ENOMEM;
/* Copy the array */ /* Copy the array */
for (i = 0; i != msg->headers.used; ++i) 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 */ /* Remove the old array and replace it with the new array */
safefree (msg->headers.strings); safefree (msg->headers.strings);
@ -251,7 +251,7 @@ http_message_send (http_message_t msg, int fd)
/* Write the response line */ /* Write the response line */
write_message (fd, "HTTP/1.0 %d %s\r\n", 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 */ /* Go through all the headers */
for (i = 0; i != msg->headers.used; ++i) for (i = 0; i != msg->headers.used; ++i)
@ -260,7 +260,7 @@ http_message_send (http_message_t msg, int fd)
/* Output the date */ /* Output the date */
global_time = time (NULL); global_time = time (NULL);
strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT", strftime (timebuf, sizeof (timebuf), "%a, %d %b %Y %H:%M:%S GMT",
gmtime (&global_time)); gmtime (&global_time));
write_message (fd, "Date: %s\r\n", timebuf); write_message (fd, "Date: %s\r\n", timebuf);
/* Output the content-length */ /* Output the content-length */

View File

@ -59,7 +59,7 @@ typedef struct http_message_s *http_message_t;
/* Initialize the internal structure of the HTTP message */ /* Initialize the internal structure of the HTTP message */
extern http_message_t http_message_create (int response_code, extern http_message_t http_message_create (int response_code,
const char *response_string); const char *response_string);
/* Free up an _internal_ resources */ /* Free up an _internal_ resources */
extern int http_message_destroy (http_message_t msg); 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. * add a new set of headers.
*/ */
extern int http_message_set_body (http_message_t msg, 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, extern int http_message_set_response (http_message_t msg,
int response_code, int response_code,
const char *response_string); const char *response_string);
/* /*
* Set the headers for this HTTP message. Each string must be NUL ('\0') * 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. * sent.
*/ */
extern int http_message_add_headers (http_message_t msg, 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_ */ #endif /* _TINYPROXY_HTTP_MESSAGE_H_ */

View File

@ -118,12 +118,12 @@ log_message (int level, char *fmt, ...)
if (log_level == LOG_CONN) if (log_level == LOG_CONN)
{ {
if (level == LOG_INFO) if (level == LOG_INFO)
return; return;
} }
else if (log_level == LOG_INFO) else if (log_level == LOG_INFO)
{ {
if (level > LOG_INFO && level != LOG_CONN) if (level > LOG_INFO && level != LOG_CONN)
return; return;
} }
else if (level > log_level) else if (level > log_level)
return; return;
@ -145,21 +145,21 @@ log_message (int level, char *fmt, ...)
char *entry_buffer; char *entry_buffer;
if (!log_message_storage) if (!log_message_storage)
{ {
log_message_storage = vector_create (); log_message_storage = vector_create ();
if (!log_message_storage) if (!log_message_storage)
goto out; goto out;
} }
vsnprintf (str, STRING_LENGTH, fmt, args); vsnprintf (str, STRING_LENGTH, fmt, args);
entry_buffer = safemalloc (strlen (str) + 6); entry_buffer = safemalloc (strlen (str) + 6);
if (!entry_buffer) if (!entry_buffer)
goto out; goto out;
sprintf (entry_buffer, "%d %s", level, str); sprintf (entry_buffer, "%d %s", level, str);
vector_append (log_message_storage, entry_buffer, vector_append (log_message_storage, entry_buffer,
strlen (entry_buffer) + 1); strlen (entry_buffer) + 1);
safefree (entry_buffer); safefree (entry_buffer);
goto out; goto out;
@ -180,10 +180,10 @@ log_message (int level, char *fmt, ...)
nowtime = time (NULL); nowtime = time (NULL);
/* Format is month day hour:minute:second (24 time) */ /* Format is month day hour:minute:second (24 time) */
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S", strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
localtime (&nowtime)); localtime (&nowtime));
snprintf (str, STRING_LENGTH, "%-9s %s [%ld]: ", 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); assert (log_file_fd >= 0);
@ -223,14 +223,14 @@ send_stored_logs (void)
#ifdef NDEBUG #ifdef NDEBUG
if (log_level == LOG_CONN && level == LOG_INFO) if (log_level == LOG_CONN && level == LOG_INFO)
continue; continue;
else if (log_level == LOG_INFO) else if (log_level == LOG_INFO)
{ {
if (level > LOG_INFO && level != LOG_CONN) if (level > LOG_INFO && level != LOG_CONN)
continue; continue;
} }
else if (level > log_level) else if (level > log_level)
continue; continue;
#endif #endif
log_message (level, ptr); log_message (level, ptr);

View File

@ -87,7 +87,7 @@
# define LOG_DEBUG 7 # define LOG_DEBUG 7
#endif #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: * Use this for debugging. The format is specific:

View File

@ -49,15 +49,15 @@ safe_write (int fd, const char *buffer, size_t count)
len = send (fd, buffer, bytestosend, MSG_NOSIGNAL); len = send (fd, buffer, bytestosend, MSG_NOSIGNAL);
if (len < 0) if (len < 0)
{ {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
else else
return -errno; return -errno;
} }
if (len == bytestosend) if (len == bytestosend)
break; break;
buffer += len; buffer += len;
bytestosend -= len; bytestosend -= len;
@ -94,7 +94,7 @@ int
write_message (int fd, const char *fmt, ...) write_message (int fd, const char *fmt, ...)
{ {
ssize_t n; 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; char *buf, *tmpbuf;
va_list ap; 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 that worked, break out so we can send the buffer */
if (n > -1 && n < size) if (n > -1 && n < size)
break; break;
/* Else, try again with more space */ /* Else, try again with more space */
if (n > -1) if (n > -1)
/* precisely what is needed (glibc2.1) */ /* precisely what is needed (glibc2.1) */
size = n + 1; size = n + 1;
else else
/* twice the old size (glibc2.0) */ /* twice the old size (glibc2.0) */
size *= 2; size *= 2;
if ((tmpbuf = saferealloc (buf, size)) == NULL) if ((tmpbuf = saferealloc (buf, size)) == NULL)
{ {
safefree (buf); safefree (buf);
return -1; return -1;
} }
else else
buf = tmpbuf; buf = tmpbuf;
} }
if (safe_write (fd, buf, n) < 0) 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); ret = recv (fd, buffer, SEGMENT_LEN, MSG_PEEK);
if (ret <= 0) if (ret <= 0)
goto CLEANUP; goto CLEANUP;
ptr = memchr (buffer, '\n', ret); ptr = memchr (buffer, '\n', ret);
if (ptr) if (ptr)
diff = ptr - buffer + 1; diff = ptr - buffer + 1;
else else
diff = ret; diff = ret;
whole_buffer_len += diff; whole_buffer_len += diff;
@ -193,33 +193,33 @@ readline (int fd, char **whole_buffer)
* get to more than MAXIMUM_BUFFER_LENGTH close. * get to more than MAXIMUM_BUFFER_LENGTH close.
*/ */
if (whole_buffer_len > MAXIMUM_BUFFER_LENGTH) if (whole_buffer_len > MAXIMUM_BUFFER_LENGTH)
{ {
ret = -ERANGE; ret = -ERANGE;
goto CLEANUP; goto CLEANUP;
} }
line_ptr->data = safemalloc (diff); line_ptr->data = safemalloc (diff);
if (!line_ptr->data) if (!line_ptr->data)
{ {
ret = -ENOMEM; ret = -ENOMEM;
goto CLEANUP; goto CLEANUP;
} }
recv (fd, line_ptr->data, diff, 0); recv (fd, line_ptr->data, diff, 0);
line_ptr->len = diff; line_ptr->len = diff;
if (ptr) if (ptr)
{ {
line_ptr->next = NULL; line_ptr->next = NULL;
break; break;
} }
line_ptr->next = safecalloc (sizeof (struct read_lines_s), 1); line_ptr->next = safecalloc (sizeof (struct read_lines_s), 1);
if (!line_ptr->next) if (!line_ptr->next)
{ {
ret = -ENOMEM; ret = -ENOMEM;
goto CLEANUP; goto CLEANUP;
} }
line_ptr = line_ptr->next; line_ptr = line_ptr->next;
} }
@ -237,7 +237,7 @@ readline (int fd, char **whole_buffer)
while (line_ptr) while (line_ptr)
{ {
memcpy (*whole_buffer + whole_buffer_len, line_ptr->data, memcpy (*whole_buffer + whole_buffer_len, line_ptr->data,
line_ptr->len); line_ptr->len);
whole_buffer_len += line_ptr->len; whole_buffer_len += line_ptr->len;
line_ptr = line_ptr->next; line_ptr = line_ptr->next;
@ -250,7 +250,7 @@ CLEANUP:
{ {
line_ptr = first_line->next; line_ptr = first_line->next;
if (first_line->data) if (first_line->data)
safefree (first_line->data); safefree (first_line->data);
safefree (first_line); safefree (first_line);
first_line = line_ptr; first_line = line_ptr;
} }
@ -269,23 +269,23 @@ get_ip_string (struct sockaddr *sa, char *buf, size_t buflen)
assert (sa != NULL); assert (sa != NULL);
assert (buf != NULL); assert (buf != NULL);
assert (buflen != 0); assert (buflen != 0);
buf[0] = '\0'; /* start with an empty string */ buf[0] = '\0'; /* start with an empty string */
switch (sa->sa_family) switch (sa->sa_family)
{ {
case AF_INET: 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); inet_ntop (AF_INET, &sa_in->sin_addr, buf, buflen);
break; break;
} }
case AF_INET6: 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); inet_ntop (AF_INET6, &sa_in6->sin6_addr, buf, buflen);
break; break;
} }
default: default:
/* no valid family */ /* no valid family */
@ -305,7 +305,7 @@ get_ip_string (struct sockaddr *sa, char *buf, size_t buflen)
int int
full_inet_pton (const char *ip, void *dst) 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; int n;
assert (ip != NULL && strlen (ip) != 0); assert (ip != NULL && strlen (ip) != 0);
@ -339,6 +339,6 @@ full_inet_pton (const char *ip, void *dst)
* full string. * full string.
*/ */
snprintf (buf, sizeof (buf), "::ffff:%s", 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); return inet_pton (AF_INET6, buf, dst);
} }

File diff suppressed because it is too large Load Diff

View File

@ -43,17 +43,16 @@ reversepath_add (const char *path, const char *url)
if (!strstr (url, "://")) if (!strstr (url, "://"))
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Skipping reverse proxy rule: '%s' is not a valid url", "Skipping reverse proxy rule: '%s' is not a valid url",
url); url);
return; return;
} }
if (path && *path != '/') if (path && *path != '/')
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Skipping reverse proxy rule: path '%s' " "Skipping reverse proxy rule: path '%s' "
"doesn't start with a /", "doesn't start with a /", path);
path);
return; return;
} }
@ -74,8 +73,8 @@ reversepath_add (const char *path, const char *url)
config.reversepath_list = reverse; config.reversepath_list = reverse;
log_message (LOG_INFO, log_message (LOG_INFO,
"Added reverse proxy rule: %s -> %s", reverse->path, "Added reverse proxy rule: %s -> %s", reverse->path,
reverse->url); reverse->url);
} }
/* /*
@ -89,7 +88,7 @@ reversepath_get (char *url)
while (reverse) while (reverse)
{ {
if (strstr (url, reverse->path) == url) if (strstr (url, reverse->path) == url)
return reverse; return reverse;
reverse = reverse->next; reverse = reverse->next;
} }
@ -102,7 +101,7 @@ reversepath_get (char *url)
*/ */
char * char *
reverse_rewrite_url (struct conn_s *connptr, hashmap_t hashofheaders, reverse_rewrite_url (struct conn_s *connptr, hashmap_t hashofheaders,
char *url) char *url)
{ {
char *rewrite_url = NULL; char *rewrite_url = NULL;
char *cookie = 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 */ /* First try locating the reverse mapping by request url */
reverse = reversepath_get (url); reverse = reversepath_get (url);
if (reverse) if (reverse)
{ {
rewrite_url = safemalloc (strlen (url) + strlen (reverse->url) + 1); rewrite_url = safemalloc (strlen (url) + strlen (reverse->url) + 1);
strcpy (rewrite_url, reverse->url); strcpy (rewrite_url, reverse->url);
strcat (rewrite_url, url + strlen (reverse->path)); strcat (rewrite_url, url + strlen (reverse->path));
} }
else if (config.reversemagic else if (config.reversemagic
&& hashmap_entry_by_key (hashofheaders, && hashmap_entry_by_key (hashofheaders,
"cookie", (void **) &cookie) > 0) "cookie", (void **) &cookie) > 0)
{ {
/* No match - try the magical tracking cookie next */ /* No match - try the magical tracking cookie next */
if ((cookieval = strstr (cookie, REVERSE_COOKIE "=")) if ((cookieval = strstr (cookie, REVERSE_COOKIE "="))
&& (reverse = && (reverse =
reversepath_get (cookieval + strlen (REVERSE_COOKIE) + 1))) reversepath_get (cookieval + strlen (REVERSE_COOKIE) + 1)))
{ {
rewrite_url = safemalloc (strlen (url) + rewrite_url = safemalloc (strlen (url) +
strlen (reverse->url) + 1); strlen (reverse->url) + 1);
strcpy (rewrite_url, reverse->url); strcpy (rewrite_url, reverse->url);
strcat (rewrite_url, url + 1); strcat (rewrite_url, url + 1);
log_message (LOG_INFO, log_message (LOG_INFO,
"Magical tracking cookie says: %s", reverse->path); "Magical tracking cookie says: %s", reverse->path);
} }
} }
} }
/* Forward proxy support off and no reverse path match found */ /* 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"); log_message (LOG_ERR, "Bad request");
indicate_http_error (connptr, 400, "Bad Request", indicate_http_error (connptr, 400, "Bad Request",
"detail", "detail",
"Request has an invalid URL", "url", url, NULL); "Request has an invalid URL", "url", url, NULL);
return NULL; return NULL;
} }

View File

@ -35,6 +35,6 @@ struct reversepath
extern void reversepath_add (const char *path, const char *url); extern void reversepath_add (const char *path, const char *url);
extern struct reversepath *reversepath_get (char *url); extern struct reversepath *reversepath_get (char *url);
extern char *reverse_rewrite_url (struct conn_s *connptr, extern char *reverse_rewrite_url (struct conn_s *connptr,
hashmap_t hashofheaders, char *url); hashmap_t hashofheaders, char *url);
#endif #endif

View File

@ -60,12 +60,12 @@ bind_socket (int sockfd, const char *addr)
do do
{ {
if (bind (sockfd, res->ai_addr, res->ai_addrlen) == 0) if (bind (sockfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */ break; /* success */
} }
while ((res = res->ai_next) != NULL); while ((res = res->ai_next) != NULL);
freeaddrinfo (ressave); 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 -1;
return sockfd; 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); sockfd = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0) if (sockfd < 0)
continue; /* ignore this one */ continue; /* ignore this one */
/* Bind to the specified address */ /* Bind to the specified address */
if (bind_to) if (bind_to)
{ {
if (bind_socket (sockfd, bind_to) < 0) if (bind_socket (sockfd, bind_to) < 0)
{ {
close (sockfd); close (sockfd);
continue; /* can't bind, so try again */ continue; /* can't bind, so try again */
} }
} }
else if (config.bind_address) else if (config.bind_address)
{ {
if (bind_socket (sockfd, config.bind_address) < 0) if (bind_socket (sockfd, config.bind_address) < 0)
{ {
close (sockfd); close (sockfd);
continue; /* can't bind, so try again */ continue; /* can't bind, so try again */
} }
} }
if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0) if (connect (sockfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */ break; /* success */
close (sockfd); close (sockfd);
} }
@ -135,7 +135,7 @@ opensock (const char *host, int port, const char *bind_to)
if (res == NULL) if (res == NULL)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"opensock: Could not establish a connection to %s", host); "opensock: Could not establish a connection to %s", host);
return -1; return -1;
} }
@ -205,16 +205,16 @@ listen_sock (uint16_t port, socklen_t * addrlen)
if (bind (listenfd, (struct sockaddr *) &addr, sizeof (addr)) < 0) if (bind (listenfd, (struct sockaddr *) &addr, sizeof (addr)) < 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"Unable to bind listening socket because of %s", "Unable to bind listening socket because of %s",
strerror (errno)); strerror (errno));
return -1; return -1;
} }
if (listen (listenfd, MAXLISTEN) < 0) if (listen (listenfd, MAXLISTEN) < 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"Unable to start listening socket because of %s", "Unable to start listening socket because of %s",
strerror (errno)); strerror (errno));
return -1; return -1;
} }
@ -237,7 +237,7 @@ getsock_ip (int fd, char *ipaddr)
if (getsockname (fd, (struct sockaddr *) &name, &namelen) != 0) if (getsockname (fd, (struct sockaddr *) &name, &namelen) != 0)
{ {
log_message (LOG_ERR, "getsock_ip: getsockname() error: %s", log_message (LOG_ERR, "getsock_ip: getsockname() error: %s",
strerror (errno)); strerror (errno));
return -1; return -1;
} }
@ -273,5 +273,5 @@ getpeer_information (int fd, char *ipaddr, char *string_addr)
/* Get the full host name */ /* Get the full host name */
return getnameinfo ((struct sockaddr *) &sa, salen, return getnameinfo ((struct sockaddr *) &sa, salen,
string_addr, HOSTNAME_LENGTH, NULL, 0, 0); string_addr, HOSTNAME_LENGTH, NULL, 0, 0);
} }

View File

@ -95,20 +95,20 @@ showstats (struct conn_s *connptr)
{ {
message_buffer = safemalloc (MAXBUFFSIZE); message_buffer = safemalloc (MAXBUFFSIZE);
if (!message_buffer) if (!message_buffer)
return -1; return -1;
snprintf (message_buffer, MAXBUFFSIZE, msg, snprintf (message_buffer, MAXBUFFSIZE, msg,
PACKAGE, VERSION, PACKAGE, VERSION, PACKAGE, VERSION, PACKAGE, VERSION,
stats->num_open, stats->num_open,
stats->num_reqs, stats->num_reqs,
stats->num_badcons, stats->num_denied, stats->num_badcons, stats->num_denied,
stats->num_refused, PACKAGE, VERSION); stats->num_refused, PACKAGE, VERSION);
if (send_http_message (connptr, 200, "OK", message_buffer) < 0) if (send_http_message (connptr, 200, "OK", message_buffer) < 0)
{ {
safefree (message_buffer); safefree (message_buffer);
return -1; return -1;
} }
safefree (message_buffer); safefree (message_buffer);
return 0; return 0;

View File

@ -28,11 +28,11 @@
*/ */
typedef enum typedef enum
{ {
STAT_BADCONN, /* bad connection, for unknown reason */ STAT_BADCONN, /* bad connection, for unknown reason */
STAT_OPEN, /* connection opened */ STAT_OPEN, /* connection opened */
STAT_CLOSE, /* connection closed */ STAT_CLOSE, /* connection closed */
STAT_REFUSE, /* connection refused (to outside world) */ STAT_REFUSE, /* connection refused (to outside world) */
STAT_DENIED /* connection denied to tinyproxy itself */ STAT_DENIED /* connection denied to tinyproxy itself */
} status_t; } status_t;
/* /*

View File

@ -107,7 +107,7 @@ chomp (char *buffer, size_t length)
/* Stop once we get to zero to prevent wrap-around */ /* Stop once we get to zero to prevent wrap-around */
if (length-- == 0) if (length-- == 0)
break; break;
} }
return chars; return chars;

View File

@ -48,8 +48,8 @@ RETSIGTYPE takesig (int sig);
*/ */
struct config_s config; struct config_s config;
float load = 0.00; float load = 0.00;
unsigned int received_sighup = FALSE; /* boolean */ unsigned int received_sighup = FALSE; /* boolean */
unsigned int processed_config_file = FALSE; /* boolean */ unsigned int processed_config_file = FALSE; /* boolean */
/* /*
* Handle a signal * Handle a signal
@ -162,7 +162,7 @@ get_id (char *str)
while (*tstr != 0) while (*tstr != 0)
{ {
if (!isdigit (*tstr)) if (!isdigit (*tstr))
return -1; return -1;
tstr++; tstr++;
} }
@ -173,7 +173,7 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
int optch; int optch;
unsigned int godaemon = TRUE; /* boolean */ unsigned int godaemon = TRUE; /* boolean */
struct passwd *thisuser = NULL; struct passwd *thisuser = NULL;
struct group *thisgroup = NULL; struct group *thisgroup = NULL;
FILE *config_file; FILE *config_file;
@ -192,29 +192,29 @@ main (int argc, char **argv)
while ((optch = getopt (argc, argv, "c:vldh")) != EOF) while ((optch = getopt (argc, argv, "c:vldh")) != EOF)
{ {
switch (optch) switch (optch)
{ {
case 'v': case 'v':
display_version (); display_version ();
exit (EX_OK); exit (EX_OK);
case 'l': case 'l':
display_license (); display_license ();
exit (EX_OK); exit (EX_OK);
case 'd': case 'd':
godaemon = FALSE; godaemon = FALSE;
break; break;
case 'c': case 'c':
config.config_file = safestrdup (optarg); config.config_file = safestrdup (optarg);
if (!config.config_file) if (!config.config_file)
{ {
fprintf (stderr, "%s: Could not allocate memory.\n", argv[0]); fprintf (stderr, "%s: Could not allocate memory.\n", argv[0]);
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
break; break;
case 'h': case 'h':
default: default:
display_usage (); display_usage ();
exit (EX_OK); exit (EX_OK);
} }
} }
log_message (LOG_INFO, "Initializing " PACKAGE " ..."); log_message (LOG_INFO, "Initializing " PACKAGE " ...");
@ -232,14 +232,14 @@ main (int argc, char **argv)
if (!config_file) if (!config_file)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Could not open configuration file \"%s\".\n", "%s: Could not open configuration file \"%s\".\n",
argv[0], config.config_file); argv[0], config.config_file);
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
if (config_compile () || config_parse (&config, config_file)) if (config_compile () || config_parse (&config, config_file))
{ {
fprintf (stderr, fprintf (stderr,
"Unable to parse configuration file. Not starting.\n"); "Unable to parse configuration file. Not starting.\n");
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
fclose (config_file); fclose (config_file);
@ -251,24 +251,24 @@ main (int argc, char **argv)
if (config.logf_name) if (config.logf_name)
{ {
if (open_log_file (config.logf_name) < 0) if (open_log_file (config.logf_name) < 0)
{ {
fprintf (stderr, "%s: Could not create log file.\n", argv[0]); fprintf (stderr, "%s: Could not create log file.\n", argv[0]);
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
config.syslog = FALSE; /* disable syslog */ config.syslog = FALSE; /* disable syslog */
} }
else if (config.syslog) else if (config.syslog)
{ {
if (godaemon == TRUE) if (godaemon == TRUE)
openlog ("tinyproxy", LOG_PID, LOG_DAEMON); openlog ("tinyproxy", LOG_PID, LOG_DAEMON);
else else
openlog ("tinyproxy", LOG_PID, LOG_USER); openlog ("tinyproxy", LOG_PID, LOG_USER);
} }
else else
{ {
fprintf (stderr, fprintf (stderr,
"%s: Either define a logfile or enable syslog logging\n", "%s: Either define a logfile or enable syslog logging\n",
argv[0]); argv[0]);
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
@ -281,8 +281,8 @@ main (int argc, char **argv)
if (config.port == 0) if (config.port == 0)
{ {
fprintf (stderr, fprintf (stderr,
"%s: You MUST set a Port in the configuration file.\n", "%s: You MUST set a Port in the configuration file.\n",
argv[0]); argv[0]);
exit (EX_SOFTWARE); exit (EX_SOFTWARE);
} }
if (!config.stathost) if (!config.stathost)
@ -293,15 +293,15 @@ main (int argc, char **argv)
if (!config.user) if (!config.user)
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"You SHOULD set a UserName in the configuration file. " "You SHOULD set a UserName in the configuration file. "
"Using current user instead."); "Using current user instead.");
} }
if (config.idletimeout == 0) if (config.idletimeout == 0)
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Invalid idle time setting. Only values greater than zero " "Invalid idle time setting. Only values greater than zero "
"allowed; therefore setting idle timeout to %u seconds.", "allowed; therefore setting idle timeout to %u seconds.",
MAX_IDLE_TIME); MAX_IDLE_TIME);
config.idletimeout = MAX_IDLE_TIME; config.idletimeout = MAX_IDLE_TIME;
} }
@ -326,16 +326,16 @@ main (int argc, char **argv)
if (config.pidpath) if (config.pidpath)
{ {
if (pidfile_create (config.pidpath) < 0) if (pidfile_create (config.pidpath) < 0)
{ {
fprintf (stderr, "%s: Could not create PID file.\n", argv[0]); fprintf (stderr, "%s: Could not create PID file.\n", argv[0]);
exit (EX_OSERR); exit (EX_OSERR);
} }
} }
if (set_signal_handler (SIGPIPE, SIG_IGN) == SIG_ERR) if (set_signal_handler (SIGPIPE, SIG_IGN) == SIG_ERR)
{ {
fprintf (stderr, "%s: Could not set the \"SIGPIPE\" signal.\n", fprintf (stderr, "%s: Could not set the \"SIGPIPE\" signal.\n",
argv[0]); argv[0]);
exit (EX_OSERR); exit (EX_OSERR);
} }
#ifdef FILTER_ENABLE #ifdef FILTER_ENABLE
@ -358,59 +358,59 @@ main (int argc, char **argv)
if (geteuid () == 0) if (geteuid () == 0)
{ {
if (config.group && strlen (config.group) > 0) if (config.group && strlen (config.group) > 0)
{ {
int gid = get_id (config.group); int gid = get_id (config.group);
if (gid < 0) if (gid < 0)
{ {
thisgroup = getgrnam (config.group); thisgroup = getgrnam (config.group);
if (!thisgroup) if (!thisgroup)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Unable to find " "%s: Unable to find "
"group \"%s\".\n", argv[0], config.group); "group \"%s\".\n", argv[0], config.group);
exit (EX_NOUSER); exit (EX_NOUSER);
} }
gid = thisgroup->gr_gid; gid = thisgroup->gr_gid;
} }
if (setgid (gid) < 0) if (setgid (gid) < 0)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Unable to change to " "%s: Unable to change to "
"group \"%s\".\n", argv[0], config.group); "group \"%s\".\n", argv[0], config.group);
exit (EX_CANTCREAT); exit (EX_CANTCREAT);
} }
log_message (LOG_INFO, "Now running as group \"%s\".", log_message (LOG_INFO, "Now running as group \"%s\".",
config.group); config.group);
} }
if (config.user && strlen (config.user) > 0) if (config.user && strlen (config.user) > 0)
{ {
int uid = get_id (config.user); int uid = get_id (config.user);
if (uid < 0) if (uid < 0)
{ {
thisuser = getpwnam (config.user); thisuser = getpwnam (config.user);
if (!thisuser) if (!thisuser)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Unable to find " "%s: Unable to find "
"user \"%s\".", argv[0], config.user); "user \"%s\".", argv[0], config.user);
exit (EX_NOUSER); exit (EX_NOUSER);
} }
uid = thisuser->pw_uid; uid = thisuser->pw_uid;
} }
if (setuid (uid) < 0) if (setuid (uid) < 0)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Unable to change to user \"%s\".", "%s: Unable to change to user \"%s\".",
argv[0], config.user); argv[0], config.user);
exit (EX_CANTCREAT); exit (EX_CANTCREAT);
} }
log_message (LOG_INFO, "Now running as user \"%s\".", config.user); log_message (LOG_INFO, "Now running as user \"%s\".", config.user);
} }
} }
else else
{ {
log_message (LOG_WARNING, 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) if (child_pool_create () < 0)
@ -426,13 +426,13 @@ main (int argc, char **argv)
if (set_signal_handler (SIGCHLD, takesig) == SIG_ERR) if (set_signal_handler (SIGCHLD, takesig) == SIG_ERR)
{ {
fprintf (stderr, "%s: Could not set the \"SIGCHLD\" signal.\n", fprintf (stderr, "%s: Could not set the \"SIGCHLD\" signal.\n",
argv[0]); argv[0]);
exit (EX_OSERR); exit (EX_OSERR);
} }
if (set_signal_handler (SIGTERM, takesig) == SIG_ERR) if (set_signal_handler (SIGTERM, takesig) == SIG_ERR)
{ {
fprintf (stderr, "%s: Could not set the \"SIGTERM\" signal.\n", fprintf (stderr, "%s: Could not set the \"SIGTERM\" signal.\n",
argv[0]); argv[0]);
exit (EX_OSERR); exit (EX_OSERR);
} }
if (set_signal_handler (SIGHUP, takesig) == SIG_ERR) if (set_signal_handler (SIGHUP, takesig) == SIG_ERR)
@ -459,8 +459,8 @@ main (int argc, char **argv)
if (unlink (config.pidpath) < 0) if (unlink (config.pidpath) < 0)
{ {
log_message (LOG_WARNING, log_message (LOG_WARNING,
"Could not remove PID file \"%s\": %s.", "Could not remove PID file \"%s\": %s.",
config.pidpath, strerror (errno)); config.pidpath, strerror (errno));
} }
#ifdef FILTER_ENABLE #ifdef FILTER_ENABLE
if (config.filter) if (config.filter)

View File

@ -26,8 +26,8 @@
#include "hashmap.h" #include "hashmap.h"
/* Global variables for the main controls of the program */ /* Global variables for the main controls of the program */
#define MAXBUFFSIZE ((size_t)(1024 * 96)) /* Max size of buffer */ #define MAXBUFFSIZE ((size_t)(1024 * 96)) /* Max size of buffer */
#define MAX_IDLE_TIME (60 * 10) /* 10 minutes of no activity */ #define MAX_IDLE_TIME (60 * 10) /* 10 minutes of no activity */
/* /*
* Even if upstream support is not compiled into tinyproxy, this * Even if upstream support is not compiled into tinyproxy, this
@ -36,7 +36,7 @@
struct upstream struct upstream
{ {
struct upstream *next; struct upstream *next;
char *domain; /* optional */ char *domain; /* optional */
char *host; char *host;
int port; int port;
in_addr_t ip, mask; in_addr_t ip, mask;
@ -49,31 +49,31 @@ struct config_s
{ {
char *logf_name; char *logf_name;
char *config_file; char *config_file;
unsigned int syslog; /* boolean */ unsigned int syslog; /* boolean */
int port; int port;
char *stathost; char *stathost;
unsigned int quit; /* boolean */ unsigned int quit; /* boolean */
char *user; char *user;
char *group; char *group;
char *ipAddr; char *ipAddr;
#ifdef FILTER_ENABLE #ifdef FILTER_ENABLE
char *filter; char *filter;
unsigned int filter_url; /* boolean */ unsigned int filter_url; /* boolean */
unsigned int filter_extended; /* boolean */ unsigned int filter_extended; /* boolean */
unsigned int filter_casesensitive; /* boolean */ unsigned int filter_casesensitive; /* boolean */
#endif /* FILTER_ENABLE */ #endif /* FILTER_ENABLE */
#ifdef XTINYPROXY_ENABLE #ifdef XTINYPROXY_ENABLE
char *my_domain; char *my_domain;
#endif #endif
#ifdef REVERSE_SUPPORT #ifdef REVERSE_SUPPORT
struct reversepath *reversepath_list; struct reversepath *reversepath_list;
unsigned int reverseonly; /* boolean */ unsigned int reverseonly; /* boolean */
unsigned int reversemagic; /* boolean */ unsigned int reversemagic; /* boolean */
char *reversebaseurl; char *reversebaseurl;
#endif #endif
#ifdef UPSTREAM_SUPPORT #ifdef UPSTREAM_SUPPORT
struct upstream *upstream_list; struct upstream *upstream_list;
#endif /* UPSTREAM_SUPPORT */ #endif /* UPSTREAM_SUPPORT */
char *pidpath; char *pidpath;
unsigned int idletimeout; unsigned int idletimeout;
char *bind_address; char *bind_address;
@ -103,7 +103,7 @@ struct config_s
/* Global Structures used in the program */ /* Global Structures used in the program */
extern struct config_s config; extern struct config_s config;
extern unsigned int received_sighup; /* boolean */ extern unsigned int received_sighup; /* boolean */
extern unsigned int processed_config_file; /* boolean */ extern unsigned int processed_config_file; /* boolean */
#endif #endif

View File

@ -56,8 +56,8 @@ build_url (char **url, const char *host, int port, const char *path)
int int
do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders, do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders,
struct request_s *request, struct config_s *conf, struct request_s *request, struct config_s *conf,
char *url) char *url)
{ {
socklen_t length; socklen_t length;
char *data; char *data;
@ -68,16 +68,16 @@ do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders,
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
if (getsockname if (getsockname
(connptr->client_fd, (struct sockaddr *) &dest_addr, &length) < 0) (connptr->client_fd, (struct sockaddr *) &dest_addr, &length) < 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"process_request: cannot get destination IP for %d", "process_request: cannot get destination IP for %d",
connptr->client_fd); connptr->client_fd);
indicate_http_error (connptr, 400, "Bad Request", indicate_http_error (connptr, 400, "Bad Request",
"detail", "detail",
"Unknown destination", "url", url, NULL); "Unknown destination", "url", url, NULL);
return 0; return 0;
} }
request->host = safemalloc (17); request->host = safemalloc (17);
strcpy (request->host, inet_ntoa (dest_addr.sin_addr)); strcpy (request->host, inet_ntoa (dest_addr.sin_addr));
request->port = ntohs (dest_addr.sin_port); request->port = ntohs (dest_addr.sin_port);
@ -86,35 +86,34 @@ do_transparent_proxy (struct conn_s *connptr, hashmap_t hashofheaders,
safefree (url); safefree (url);
build_url (&url, request->host, request->port, request->path); build_url (&url, request->host, request->port, request->path);
log_message (LOG_INFO, log_message (LOG_INFO,
"process_request: trans IP %s %s for %d", "process_request: trans IP %s %s for %d",
request->method, url, connptr->client_fd); request->method, url, connptr->client_fd);
} }
else else
{ {
request->host = safemalloc (length + 1); request->host = safemalloc (length + 1);
if (sscanf (data, "%[^:]:%hu", request->host, &request->port) != 2) if (sscanf (data, "%[^:]:%hu", request->host, &request->port) != 2)
{ {
strcpy (request->host, data); strcpy (request->host, data);
request->port = HTTP_PORT; request->port = HTTP_PORT;
} }
request->path = safemalloc (strlen (url) + 1); request->path = safemalloc (strlen (url) + 1);
strcpy (request->path, url); strcpy (request->path, url);
safefree (url); safefree (url);
build_url (&url, request->host, request->port, request->path); build_url (&url, request->host, request->port, request->path);
log_message (LOG_INFO, log_message (LOG_INFO,
"process_request: trans Host %s %s for %d", "process_request: trans Host %s %s for %d",
request->method, url, connptr->client_fd); request->method, url, connptr->client_fd);
} }
if (conf->ipAddr && strcmp (request->host, conf->ipAddr) == 0) if (conf->ipAddr && strcmp (request->host, conf->ipAddr) == 0)
{ {
log_message (LOG_ERR, log_message (LOG_ERR,
"process_request: destination IP is localhost %d", "process_request: destination IP is localhost %d",
connptr->client_fd); connptr->client_fd);
indicate_http_error (connptr, 400, "Bad Request", indicate_http_error (connptr, 400, "Bad Request",
"detail", "detail",
"You tried to connect to the machine " "You tried to connect to the machine "
"the proxy is running on", "the proxy is running on", "url", url, NULL);
"url", url, NULL);
return 0; return 0;
} }

View File

@ -30,9 +30,9 @@
#include "reqs.h" #include "reqs.h"
extern int do_transparent_proxy (struct conn_s *connptr, extern int do_transparent_proxy (struct conn_s *connptr,
hashmap_t hashofheaders, hashmap_t hashofheaders,
struct request_s *request, struct request_s *request,
struct config_s *config, char *url); struct config_s *config, char *url);
#endif #endif

View File

@ -35,7 +35,7 @@
*/ */
int int
send_http_message (struct conn_s *connptr, int http_code, 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[] = { static char *headers[] = {
"Server: " PACKAGE "/" VERSION, "Server: " PACKAGE "/" VERSION,
@ -78,12 +78,12 @@ create_file_safely (const char *filename, unsigned int truncate_file)
* existing", exit. * existing", exit.
*/ */
if (errno != ENOENT) if (errno != ENOENT)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Error checking file %s: %s\n", "%s: Error checking file %s: %s\n",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
return -EACCES; return -EACCES;
} }
/* /*
* The file doesn't exist, so create it with O_EXCL to make * 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() * and open()
*/ */
if ((fildes = open (filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) if ((fildes = open (filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Could not create file %s: %s\n", "%s: Could not create file %s: %s\n",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
return fildes; return fildes;
} }
} }
else else
{ {
@ -105,34 +105,34 @@ create_file_safely (const char *filename, unsigned int truncate_file)
flags = O_RDWR; flags = O_RDWR;
if (!truncate_file) if (!truncate_file)
flags |= O_APPEND; flags |= O_APPEND;
/* /*
* Open an existing file. * Open an existing file.
*/ */
if ((fildes = open (filename, flags)) < 0) if ((fildes = open (filename, flags)) < 0)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Could not open file %s: %s\n", "%s: Could not open file %s: %s\n",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
return fildes; return fildes;
} }
/* /*
* fstat() the opened file and check that the file mode bits, * fstat() the opened file and check that the file mode bits,
* inode, and device match. * inode, and device match.
*/ */
if (fstat (fildes, &fstatinfo) < 0 if (fstat (fildes, &fstatinfo) < 0
|| lstatinfo.st_mode != fstatinfo.st_mode || lstatinfo.st_mode != fstatinfo.st_mode
|| lstatinfo.st_ino != fstatinfo.st_ino || lstatinfo.st_ino != fstatinfo.st_ino
|| lstatinfo.st_dev != fstatinfo.st_dev) || lstatinfo.st_dev != fstatinfo.st_dev)
{ {
fprintf (stderr, fprintf (stderr,
"%s: The file %s has been changed before it could be opened\n", "%s: The file %s has been changed before it could be opened\n",
PACKAGE, filename); PACKAGE, filename);
close (fildes); close (fildes);
return -EIO; return -EIO;
} }
/* /*
* If the above check was passed, we know that the lstat() * 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) * st_mode check would also find this)
*/ */
if (fstatinfo.st_nlink > 1 || !S_ISREG (lstatinfo.st_mode)) if (fstatinfo.st_nlink > 1 || !S_ISREG (lstatinfo.st_mode))
{ {
fprintf (stderr, fprintf (stderr,
"%s: The file %s has too many links, " "%s: The file %s has too many links, "
"or is not a regular file: %s\n", "or is not a regular file: %s\n",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
close (fildes); close (fildes);
return -EMLINK; return -EMLINK;
} }
/* /*
* Just return the file descriptor if we _don't_ want the file * Just return the file descriptor if we _don't_ want the file
* truncated. * truncated.
*/ */
if (!truncate_file) if (!truncate_file)
return fildes; return fildes;
/* /*
* On systems which don't support ftruncate() the best we can * 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 #else
close (fildes); close (fildes);
if ((fildes = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) if ((fildes = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Could not open file %s: %s.", "%s: Could not open file %s: %s.",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
return fildes; return fildes;
} }
#endif /* HAVE_FTRUNCATE */ #endif /* HAVE_FTRUNCATE */
} }
@ -204,8 +204,8 @@ pidfile_create (const char *filename)
if ((fd = fdopen (fildes, "w")) == NULL) if ((fd = fdopen (fildes, "w")) == NULL)
{ {
fprintf (stderr, fprintf (stderr,
"%s: Could not write PID file %s: %s.", "%s: Could not write PID file %s: %s.",
PACKAGE, filename, strerror (errno)); PACKAGE, filename, strerror (errno));
close (fildes); close (fildes);
unlink (filename); unlink (filename);
return -EIO; return -EIO;

View File

@ -28,10 +28,10 @@
struct conn_s; struct conn_s;
extern int send_http_message (struct conn_s *connptr, int http_code, 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 pidfile_create (const char *path);
extern int create_file_safely (const char *filename, extern int create_file_safely (const char *filename,
unsigned int truncate_file); unsigned int truncate_file);
#endif #endif

View File

@ -80,5 +80,5 @@ extern "C"
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif /* C++ */ #endif /* C++ */
#endif /* _VECTOR_H */ #endif /* _VECTOR_H */