Reformated text.

This commit is contained in:
Robert James Kaes 2001-11-22 00:31:10 +00:00
parent bcb7c68911
commit 787ece6c01
19 changed files with 541 additions and 332 deletions

View File

@ -1,4 +1,4 @@
/* $Id: acl.c,v 1.10 2001-11-03 06:08:37 rjkaes Exp $
/* $Id: acl.c,v 1.11 2001-11-22 00:31:10 rjkaes Exp $
*
* This system handles Access Control for use of this daemon. A list of
* domains, or IP addresses (including IP blocks) are stored in a list
@ -38,7 +38,8 @@ static struct acl_s *access_list = NULL;
* Take a netmask number (between 0 and 32) and returns a network ordered
* value for comparison. Somebody please clean this up. :)
*/
static in_addr_t make_netmask(int netmask_num)
static in_addr_t
make_netmask(int netmask_num)
{
static in_addr_t netmasks[] = {
0x00000000, 0x80000000, 0xc0000000, 0xe0000000,
@ -66,7 +67,8 @@ static in_addr_t make_netmask(int netmask_num)
* -1 on failure
* 0 otherwise.
*/
int insert_acl(char *location, acl_access_t access_type)
int
insert_acl(char *location, acl_access_t access_type)
{
size_t i;
struct acl_s **rev_acl_ptr, *acl_ptr, *new_acl_ptr;
@ -115,7 +117,8 @@ int insert_acl(char *location, acl_access_t access_type)
*nptr++ = '\0';
new_acl_ptr->netmask = strtol(nptr, NULL, 10);
if (new_acl_ptr->netmask < 0 || new_acl_ptr->netmask > 32) {
if (new_acl_ptr->netmask < 0
|| new_acl_ptr->netmask > 32) {
safefree(new_acl_ptr);
return -1;
}
@ -149,7 +152,8 @@ int insert_acl(char *location, acl_access_t access_type)
* 0 if denied
* -1 if error
*/
int check_acl(int fd)
int
check_acl(int fd)
{
struct acl_s *aclptr;
char ip_address[PEER_IP_LENGTH];
@ -180,9 +184,13 @@ int check_acl(int fd)
continue;
}
if (strcasecmp(string_address + (test_length - match_length), aclptr->location) == 0) {
if (strcasecmp
(string_address + (test_length - match_length),
aclptr->location) == 0) {
if (aclptr->acl_access == ACL_DENY) {
log_message(LOG_NOTICE, "Unauthorized access from \"%s\"", string_address);
log_message(LOG_NOTICE,
"Unauthorized access from \"%s\"",
string_address);
return 0;
} else {
return 1;
@ -202,9 +210,12 @@ int check_acl(int fd)
netmask_addr = make_netmask(aclptr->netmask);
if ((test_addr.s_addr & netmask_addr) == (match_addr.s_addr & netmask_addr)) {
if ((test_addr.s_addr & netmask_addr) ==
(match_addr.s_addr & netmask_addr)) {
if (aclptr->acl_access == ACL_DENY) {
log_message(LOG_NOTICE, "Unauthorized access from [%s].", ip_address);
log_message(LOG_NOTICE,
"Unauthorized access from [%s].",
ip_address);
return 0;
} else {
return 1;
@ -218,10 +229,10 @@ int check_acl(int fd)
aclptr = aclptr->next;
}
/*
* Deny all connections by default.
*/
log_message(LOG_NOTICE, "Unauthorized connection from \"%s\" [%s].", string_address, ip_address);
log_message(LOG_NOTICE, "Unauthorized connection from \"%s\" [%s].",
string_address, ip_address);
return 0;
}

View File

@ -1,4 +1,4 @@
/* $Id: anonymous.c,v 1.8 2001-11-05 15:24:42 rjkaes Exp $
/* $Id: anonymous.c,v 1.9 2001-11-22 00:31:10 rjkaes Exp $
*
* Handles insertion and searches for headers which should be let through when
* the anonymous feature is turned on. The headers are stored in a Ternary
@ -32,12 +32,14 @@ static TERNARY anonymous_tree = 0;
*/
static short int anonymous_is_enabled = 0;
inline short int is_anonymous_enabled(void)
inline short int
is_anonymous_enabled(void)
{
return anonymous_is_enabled;
}
int anonymous_search(char *s)
int
anonymous_search(char *s)
{
assert(s != NULL);
assert(anonymous_is_enabled == 1);
@ -46,7 +48,8 @@ int anonymous_search(char *s)
return ternary_search(anonymous_tree, s, NULL);
}
int anonymous_insert(char *s)
int
anonymous_insert(char *s)
{
assert(s != NULL);

View File

@ -1,4 +1,4 @@
/* $Id: buffer.c,v 1.15 2001-11-05 15:23:05 rjkaes Exp $
/* $Id: buffer.c,v 1.16 2001-11-22 00:31:10 rjkaes Exp $
*
* The buffer used in each connection is a linked list of lines. As the lines
* are read in and written out the buffer expands and contracts. Basically,
@ -43,7 +43,8 @@ struct bufline_s {
* pointer into the structure. In other words, when you insert data into the
* buffer, the buffer becomes responsible for freeing it.
*/
static struct bufline_s *makenewline(unsigned char *data, size_t length)
static struct bufline_s *
makenewline(unsigned char *data, size_t length)
{
struct bufline_s *newline;
@ -66,7 +67,8 @@ static struct bufline_s *makenewline(unsigned char *data, size_t length)
/*
* Free the allocated buffer line
*/
static void free_line(struct bufline_s *line)
static void
free_line(struct bufline_s *line)
{
assert(line != NULL);
@ -82,7 +84,8 @@ static void free_line(struct bufline_s *line)
/*
* Create a new buffer
*/
struct buffer_s *new_buffer(void)
struct buffer_s *
new_buffer(void)
{
struct buffer_s *buffptr;
@ -103,7 +106,8 @@ struct buffer_s *new_buffer(void)
/*
* Delete all the lines in the buffer and the buffer itself
*/
void delete_buffer(struct buffer_s *buffptr)
void
delete_buffer(struct buffer_s *buffptr)
{
struct bufline_s *next;
@ -121,8 +125,8 @@ void delete_buffer(struct buffer_s *buffptr)
/*
* Push a new line on to the end of the buffer
*/
static int add_to_buffer(struct buffer_s *buffptr, unsigned char *data,
size_t length)
static int
add_to_buffer(struct buffer_s *buffptr, unsigned char *data, size_t length)
{
struct bufline_s *newline;
@ -158,7 +162,8 @@ static int add_to_buffer(struct buffer_s *buffptr, unsigned char *data,
/*
* Remove the first line from the top of the buffer
*/
static struct bufline_s *remove_from_buffer(struct buffer_s *buffptr)
static struct bufline_s *
remove_from_buffer(struct buffer_s *buffptr)
{
struct bufline_s *line;
@ -178,7 +183,8 @@ static struct bufline_s *remove_from_buffer(struct buffer_s *buffptr)
* Takes a connection and returns the number of bytes read.
*/
#define READ_BUFFER_SIZE (1024 * 2)
ssize_t readbuff(int fd, struct buffer_s *buffptr)
ssize_t
readbuff(int fd, struct buffer_s * buffptr)
{
ssize_t bytesin;
unsigned char *buffer;
@ -204,7 +210,8 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr)
}
if (add_to_buffer(buffptr, newbuffer, bytesin) < 0) {
log_message(LOG_ERR, "readbuff: add_to_buffer() error.");
log_message(LOG_ERR,
"readbuff: add_to_buffer() error.");
return -1;
}
@ -226,7 +233,9 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr)
case EINTR:
return 0;
default:
log_message(LOG_ERR, "readbuff: recv() error \"%s\" on file descriptor %d", strerror(errno), fd);
log_message(LOG_ERR,
"readbuff: recv() error \"%s\" on file descriptor %d",
strerror(errno), fd);
return -1;
}
}
@ -237,7 +246,8 @@ ssize_t readbuff(int fd, struct buffer_s *buffptr)
* Write the bytes in the buffer to the socket.
* Takes a connection and returns the number of bytes written.
*/
ssize_t writebuff(int fd, struct buffer_s *buffptr)
ssize_t
writebuff(int fd, struct buffer_s * buffptr)
{
ssize_t bytessent;
struct bufline_s *line;
@ -252,7 +262,8 @@ ssize_t writebuff(int fd, struct buffer_s *buffptr)
assert(BUFFER_HEAD(buffptr) != NULL);
line = BUFFER_HEAD(buffptr);
bytessent = write(fd, line->string + line->pos, line->length - line->pos);
bytessent =
write(fd, line->string + line->pos, line->length - line->pos);
if (bytessent >= 0) {
/* bytes sent, adjust buffer */
@ -273,10 +284,14 @@ ssize_t writebuff(int fd, struct buffer_s *buffptr)
return 0;
case ENOBUFS:
case ENOMEM:
log_message(LOG_ERR, "writebuff: write() error [NOBUFS/NOMEM] \"%s\" on file descriptor %d", strerror(errno), fd);
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);
log_message(LOG_ERR,
"writebuff: write() error \"%s\" on file descriptor %d",
strerror(errno), fd);
return -1;
}
}

View File

@ -1,4 +1,4 @@
/* $Id: buffer.h,v 1.5 2001-11-05 15:23:05 rjkaes Exp $
/* $Id: buffer.h,v 1.6 2001-11-22 00:31:10 rjkaes Exp $
*
* See 'buffer.c' for a detailed description.
*

View File

@ -1,4 +1,4 @@
/* $Id: conns.c,v 1.4 2001-11-21 01:00:08 rjkaes Exp $
/* $Id: conns.c,v 1.5 2001-11-22 00:31:10 rjkaes Exp $
*
* Create and free the connection structure. One day there could be
* other connnection related tasks put here, but for now the header
@ -25,7 +25,8 @@
#include "stats.h"
#include "utils.h"
void initialize_conn(struct conn_s *connptr)
void
initialize_conn(struct conn_s *connptr)
{
connptr->client_fd = connptr->server_fd = -1;
connptr->cbuffer = new_buffer();
@ -42,7 +43,8 @@ void initialize_conn(struct conn_s *connptr)
update_stats(STAT_OPEN);
}
void destroy_conn(struct conn_s *connptr)
void
destroy_conn(struct conn_s *connptr)
{
if (connptr->client_fd != -1)
close(connptr->client_fd);

View File

@ -1,4 +1,4 @@
/* $Id: dnscache.c,v 1.17 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: dnscache.c,v 1.18 2001-11-22 00:31:10 rjkaes Exp $
*
* This is a caching DNS system. When a host name is needed we look it up here
* and see if there is already an answer for it. The domains are placed in a
@ -46,7 +46,8 @@ struct dnscache_s {
static TERNARY dns_tree = -1;
static unsigned int dns_insertions;
static int dns_lookup(struct in_addr *addr, char *domain)
static int
dns_lookup(struct in_addr *addr, char *domain)
{
int ret;
struct dnscache_s *ptr;
@ -66,7 +67,8 @@ static int dns_lookup(struct in_addr *addr, char *domain)
return 0;
}
static int dns_insert(struct in_addr *addr, char *domain)
static int
dns_insert(struct in_addr *addr, char *domain)
{
struct dnscache_s *newptr;
@ -92,7 +94,8 @@ static int dns_insert(struct in_addr *addr, char *domain)
return 0;
}
int dnscache(struct in_addr *addr, char *domain)
int
dnscache(struct in_addr *addr, char *domain)
{
struct hostent *resolv;
@ -130,7 +133,9 @@ int dnscache(struct in_addr *addr, char *domain)
dns_insertions++;
if (dns_insertions > DNS_INSERT_LIMIT) {
log_message(LOG_INFO, "DNS Insertion limit reached (%u). Rebuilding cache.", dns_insertions);
log_message(LOG_INFO,
"DNS Insertion limit reached (%u). Rebuilding cache.",
dns_insertions);
ternary_destroy(dns_tree, free);
dns_tree = ternary_new();
dns_insertions = 0;

View File

@ -1,4 +1,4 @@
/* $Id: filter.c,v 1.7 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: filter.c,v 1.8 2001-11-22 00:31:10 rjkaes Exp $
*
* Copyright (c) 1999 George Talusan (gstalusan@uwaterloo.ca)
*
@ -30,12 +30,12 @@ struct filter_list {
regex_t *cpat;
};
static struct filter_list *fl = NULL;
static int already_init = 0;
/* initializes a linked list of strings containing hosts to be filtered */
void filter_init(void)
void
filter_init(void)
{
FILE *fd;
struct filter_list *p;
@ -50,9 +50,15 @@ void filter_init(void)
while (fgets(buf, 255, fd)) {
s = buf;
if (!p) /* head of list */
fl = p = safecalloc(1, sizeof(struct filter_list));
fl = p =
safecalloc(1,
sizeof(struct
filter_list));
else { /* next entry */
p->next = safecalloc(1, sizeof(struct filter_list));
p->next =
safecalloc(1,
sizeof(struct
filter_list));
p = p->next;
}
@ -63,9 +69,10 @@ void filter_init(void)
p->pat = strdup(buf);
p->cpat = safemalloc(sizeof(regex_t));
if ((err = regcomp(p->cpat, p->pat, REG_NEWLINE | REG_NOSUB)) != 0) {
fprintf(stderr,
"Bad regex in %s: %s\n",
if ((err =
regcomp(p->cpat, p->pat,
REG_NEWLINE | REG_NOSUB)) != 0) {
fprintf(stderr, "Bad regex in %s: %s\n",
config.filter, p->pat);
exit(EX_DATAERR);
}
@ -77,7 +84,8 @@ void filter_init(void)
}
/* unlink the list */
void filter_destroy(void)
void
filter_destroy(void)
{
struct filter_list *p, *q;
@ -95,7 +103,8 @@ void filter_destroy(void)
}
/* returns 0 if host is not an element of filter list, non-zero otherwise */
int filter_url(char *host)
int
filter_url(char *host)
{
struct filter_list *p;
char *s, *port;

View File

@ -1,4 +1,4 @@
/* $Id: log.c,v 1.15 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: log.c,v 1.16 2001-11-22 00:31:10 rjkaes Exp $
*
* Logs the various messages which tinyproxy produces to either a log file or
* the syslog daemon. Not much to it...
@ -44,7 +44,8 @@ static short int log_level = LOG_ERR;
/*
* Set the log level for writing to the log file.
*/
void set_log_level(short int level)
void
set_log_level(short int level)
{
log_level = level;
}
@ -52,7 +53,8 @@ void set_log_level(short int level)
/*
* This routine logs messages to either the log file or the syslog function.
*/
void log_message(short int level, char *fmt, ...)
void
log_message(short int level, char *fmt, ...)
{
va_list args;
time_t nowtime;
@ -77,7 +79,6 @@ void log_message(short int level, char *fmt, ...)
return;
#endif
#ifdef HAVE_SYSLOG_H
if (config.syslog && level == LOG_CONN)
level = LOG_INFO;

View File

@ -1,4 +1,4 @@
/* $Id: log.h,v 1.7 2001-08-26 21:10:04 rjkaes Exp $
/* $Id: log.h,v 1.8 2001-11-22 00:31:10 rjkaes Exp $
*
* See 'log.c' for a detailed description.
*

View File

@ -1,4 +1,4 @@
/* $Id: reqs.c,v 1.40 2001-11-22 00:19:45 rjkaes Exp $
/* $Id: reqs.c,v 1.41 2001-11-22 00:31:10 rjkaes Exp $
*
* This is where all the work in tinyproxy is actually done. Incoming
* connections have a new thread created for them. The thread then
@ -46,7 +46,8 @@
/*
* Remove any new lines or carriage returns from the end of a string.
*/
static inline void trim(char *string, unsigned int len)
static inline void
trim(char *string, unsigned int len)
{
char *ptr;
@ -71,14 +72,17 @@ static inline void trim(char *string, unsigned int len)
* connections. The request line is allocated from the heap, but it must
* be freed in another function.
*/
static char *read_request_line(struct conn_s *connptr)
static char *
read_request_line(struct conn_s *connptr)
{
char *request_buffer;
size_t len;
len = readline(connptr->client_fd, &request_buffer);
if (len <= 0) {
log_message(LOG_ERR, "read_request_line: Client (file descriptor: %d) closed socket before read.", connptr->client_fd);
log_message(LOG_ERR,
"read_request_line: Client (file descriptor: %d) closed socket before read.",
connptr->client_fd);
safefree(request_buffer);
return NULL;
}
@ -106,7 +110,8 @@ struct request_s {
int port;
};
static void free_request_struct(struct request_s *request)
static void
free_request_struct(struct request_s *request)
{
if (!request)
return;
@ -123,7 +128,8 @@ static void free_request_struct(struct request_s *request)
/*
* Pull the information out of the URL line.
*/
static int extract_http_url(const char *url, struct request_s *request)
static int
extract_http_url(const char *url, struct request_s *request)
{
request->host = safemalloc(strlen(url) + 1);
request->path = safemalloc(strlen(url) + 1);
@ -135,11 +141,14 @@ static int extract_http_url(const char *url, struct request_s *request)
return -1;
}
if (sscanf(url, "http://%[^:/]:%d%s", request->host, &request->port, request->path) == 3)
;
else if (sscanf(url, "http://%[^/]%s", request->host, request->path) == 2)
if (sscanf
(url, "http://%[^:/]:%d%s", request->host, &request->port,
request->path) == 3) ;
else if (sscanf(url, "http://%[^/]%s", request->host, request->path) ==
2)
request->port = 80;
else if (sscanf(url, "http://%[^:/]:%d", request->host, &request->port) == 2)
else if (sscanf(url, "http://%[^:/]:%d", request->host, &request->port)
== 2)
strcpy(request->path, "/");
else if (sscanf(url, "http://%[^/]", request->host) == 1) {
request->port = 80;
@ -159,14 +168,14 @@ static int extract_http_url(const char *url, struct request_s *request)
/*
* Extract the URL from a SSL connection.
*/
static int extract_ssl_url(const char *url, struct request_s *request)
static int
extract_ssl_url(const char *url, struct request_s *request)
{
request->host = safemalloc(strlen(url) + 1);
if (!request->host)
return -1;
if (sscanf(url, "%[^:]:%d", request->host, &request->port) == 2)
;
if (sscanf(url, "%[^:]:%d", request->host, &request->port) == 2) ;
else if (sscanf(url, "%s", request->host) == 1)
request->port = 443;
else {
@ -182,17 +191,19 @@ static int extract_ssl_url(const char *url, struct request_s *request)
/*
* Create a connection for HTTP connections.
*/
static int establish_http_connection(struct conn_s *connptr,
struct request_s *request)
static int
establish_http_connection(struct conn_s *connptr, struct request_s *request)
{
/*
* Send the request line
*/
if (safe_write(connptr->server_fd, request->method, strlen(request->method)) < 0)
if (safe_write
(connptr->server_fd, request->method, strlen(request->method)) < 0)
return -1;
if (safe_write(connptr->server_fd, " ", 1) < 0)
return -1;
if (safe_write(connptr->server_fd, request->path, strlen(request->path)) < 0)
if (safe_write(connptr->server_fd, request->path, strlen(request->path))
< 0)
return -1;
if (safe_write(connptr->server_fd, " ", 1) < 0)
return -1;
@ -204,7 +215,8 @@ static int establish_http_connection(struct conn_s *connptr,
*/
if (safe_write(connptr->server_fd, "Host: ", 6) < 0)
return -1;
if (safe_write(connptr->server_fd, request->host, strlen(request->host)) < 0)
if (safe_write(connptr->server_fd, request->host, strlen(request->host))
< 0)
return -1;
if (safe_write(connptr->server_fd, "\r\n", 2) < 0)
@ -230,12 +242,16 @@ static int establish_http_connection(struct conn_s *connptr,
* Send the appropriate response to the client to establish a SSL
* connection.
*/
static inline int send_ssl_response(struct conn_s *connptr)
static inline int
send_ssl_response(struct conn_s *connptr)
{
if (safe_write(connptr->client_fd, SSL_CONNECTION_RESPONSE, strlen(SSL_CONNECTION_RESPONSE)) < 0)
if (safe_write
(connptr->client_fd, SSL_CONNECTION_RESPONSE,
strlen(SSL_CONNECTION_RESPONSE)) < 0)
return -1;
if (safe_write(connptr->client_fd, PROXY_AGENT, strlen(PROXY_AGENT)) < 0)
if (safe_write(connptr->client_fd, PROXY_AGENT, strlen(PROXY_AGENT)) <
0)
return -1;
if (safe_write(connptr->client_fd, "\r\n", 2) < 0)
@ -248,8 +264,8 @@ static inline int send_ssl_response(struct conn_s *connptr)
* Break the request line apart and figure out where to connect and
* build a new request line. Finally connect to the remote server.
*/
static struct request_s *process_request(struct conn_s *connptr,
char *request_line)
static struct request_s *
process_request(struct conn_s *connptr, char *request_line)
{
char *url;
struct request_s *request;
@ -276,9 +292,13 @@ static struct request_s *process_request(struct conn_s *connptr,
return NULL;
}
ret = sscanf(request_line, "%[^ ] %[^ ] %[^ ]", request->method, url, request->protocol);
ret =
sscanf(request_line, "%[^ ] %[^ ] %[^ ]", request->method, url,
request->protocol);
if (ret < 2) {
log_message(LOG_ERR, "process_request: Bad Request on file descriptor %d", connptr->client_fd);
log_message(LOG_ERR,
"process_request: Bad Request on file descriptor %d",
connptr->client_fd);
httperr(connptr, 400, "Bad Request. No request found.");
safefree(url);
@ -290,7 +310,9 @@ static struct request_s *process_request(struct conn_s *connptr,
}
if (!url) {
log_message(LOG_ERR, "process_request: Null URL on file descriptor %d", connptr->client_fd);
log_message(LOG_ERR,
"process_request: Null URL on file descriptor %d",
connptr->client_fd);
httperr(connptr, 400, "Bad Request. Null URL.");
safefree(url);
@ -304,7 +326,8 @@ static struct request_s *process_request(struct conn_s *connptr,
memcpy(url, "http", 4);
if (extract_http_url(url, request) < 0) {
httperr(connptr, 400, "Bad Request. Could not parse URL.");
httperr(connptr, 400,
"Bad Request. Could not parse URL.");
safefree(url);
free_request_struct(request);
@ -314,7 +337,8 @@ static struct request_s *process_request(struct conn_s *connptr,
connptr->ssl = FALSE;
} else if (strcmp(request->method, "CONNECT") == 0) {
if (extract_ssl_url(url, request) < 0) {
httperr(connptr, 400, "Bad Request. Could not parse URL.");
httperr(connptr, 400,
"Bad Request. Could not parse URL.");
safefree(url);
free_request_struct(request);
@ -323,7 +347,9 @@ static struct request_s *process_request(struct conn_s *connptr,
}
connptr->ssl = TRUE;
} else {
log_message(LOG_ERR, "process_request: Unknown URL type on file descriptor %d", connptr->client_fd);
log_message(LOG_ERR,
"process_request: Unknown URL type on file descriptor %d",
connptr->client_fd);
httperr(connptr, 400, "Bad Request. Unknown URL type.");
safefree(url);
@ -342,8 +368,11 @@ static struct request_s *process_request(struct conn_s *connptr,
if (filter_url(request->host)) {
update_stats(STAT_DENIED);
log_message(LOG_NOTICE, "Proxying refused on filtered domain \"%s\"", request->host);
httperr(connptr, 404, "Connection to filtered domain is now allowed.");
log_message(LOG_NOTICE,
"Proxying refused on filtered domain \"%s\"",
request->host);
httperr(connptr, 404,
"Connection to filtered domain is now allowed.");
free_request_struct(request);
@ -369,7 +398,8 @@ static struct request_s *process_request(struct conn_s *connptr,
*/
if (strncasecmp(request->protocol, "http", 4) == 0) {
memcpy(request->protocol, "HTTP", 4);
sscanf(request->protocol, "HTTP/%hu.%hu", &connptr->protocol.major, &connptr->protocol.minor);
sscanf(request->protocol, "HTTP/%hu.%hu",
&connptr->protocol.major, &connptr->protocol.minor);
}
return request;
@ -380,7 +410,8 @@ static struct request_s *process_request(struct conn_s *connptr,
* headers which are to be allowed. If the header is found in the
* anonymous list return 0, otherwise return -1.
*/
static int compare_header(char *line)
static int
compare_header(char *line)
{
char *buffer;
char *ptr;
@ -407,7 +438,8 @@ static int compare_header(char *line)
* server headers can be processed.
* - rjkaes
*/
static int pull_client_data(struct conn_s *connptr, unsigned long int length)
static int
pull_client_data(struct conn_s *connptr, unsigned long int length)
{
char *buffer;
ssize_t len;
@ -417,7 +449,9 @@ static int pull_client_data(struct conn_s *connptr, unsigned long int length)
return -1;
do {
len = safe_read(connptr->client_fd, buffer, min(MAXBUFFSIZE, length));
len =
safe_read(connptr->client_fd, buffer,
min(MAXBUFFSIZE, length));
if (len <= 0) {
safefree(buffer);
@ -444,7 +478,8 @@ static int pull_client_data(struct conn_s *connptr, unsigned long int length)
* the server.
* -rjkaes
*/
static int add_xtinyproxy_header(struct conn_s *connptr)
static int
add_xtinyproxy_header(struct conn_s *connptr)
{
char ipaddr[PEER_IP_LENGTH];
char xtinyproxy[32];
@ -472,7 +507,8 @@ static int add_xtinyproxy_header(struct conn_s *connptr)
* (plus a few which are required for various methods).
* - rjkaes
*/
static int process_client_headers(struct conn_s *connptr)
static int
process_client_headers(struct conn_s *connptr)
{
char *header;
long content_length = -1;
@ -492,7 +528,8 @@ static int process_client_headers(struct conn_s *connptr)
for (;;) {
if (readline(connptr->client_fd, &header) <= 0) {
DEBUG2("Client (file descriptor %d) closed connection.", connptr->client_fd);
DEBUG2("Client (file descriptor %d) closed connection.",
connptr->client_fd);
return -1;
}
@ -514,7 +551,6 @@ static int process_client_headers(struct conn_s *connptr)
safefree(header);
continue;
}
#if 0
/*
* If we find a Via header we need to append our information
@ -527,7 +563,11 @@ static int process_client_headers(struct conn_s *connptr)
sent_via_header = 1;
gethostname(hostname, sizeof(hostname));
snprintf(via_header_buffer, sizeof(via_header_buffer), ", %hu.%hu %s (%s/%s)\r\n", connptr->protocol.major, connptr->protocol.minor, hostname, PACKAGE, VERSION);
snprintf(via_header_buffer, sizeof(via_header_buffer),
", %hu.%hu %s (%s/%s)\r\n",
connptr->protocol.major,
connptr->protocol.minor, hostname, PACKAGE,
VERSION);
trim(header, strlen(header));
@ -539,7 +579,9 @@ static int process_client_headers(struct conn_s *connptr)
* Don't send certain headers.
*/
for (i = 0; i < (sizeof(skipheaders) / sizeof(char *)); i++) {
if (strncasecmp(header, skipheaders[i], strlen(skipheaders[i])) == 0) {
if (strncasecmp
(header, skipheaders[i],
strlen(skipheaders[i])) == 0) {
break;
}
}
@ -559,7 +601,9 @@ static int process_client_headers(struct conn_s *connptr)
content_length = atol(content_ptr);
}
if ((connptr->server_fd != -1) && safe_write(connptr->server_fd, header, strlen(header)) < 0) {
if ((connptr->server_fd != -1)
&& safe_write(connptr->server_fd, header,
strlen(header)) < 0) {
safefree(header);
return -1;
}
@ -576,22 +620,26 @@ static int process_client_headers(struct conn_s *connptr)
char hostname[128];
gethostname(hostname, sizeof(hostname));
snprintf(via_header_buffer, sizeof(via_header_buffer), "Via: %hu.%hu %s (%s/%s)\r\n", connptr->protocol.major, connptr->protocol.minor, hostname, PACKAGE, VERSION);
snprintf(via_header_buffer, sizeof(via_header_buffer),
"Via: %hu.%hu %s (%s/%s)\r\n", connptr->protocol.major,
connptr->protocol.minor, hostname, PACKAGE, VERSION);
safe_write(connptr->server_fd, via_header_buffer, strlen(via_header_buffer));
safe_write(connptr->server_fd, via_header_buffer,
strlen(via_header_buffer));
}
#endif
if (!connptr->send_message && (connptr->upstream || !connptr->ssl)) {
#ifdef XTINYPROXY_ENABLE
if (config.my_domain
&& add_xtinyproxy_header(connptr) < 0) {
if (config.my_domain && add_xtinyproxy_header(connptr) < 0) {
safefree(header);
return -1;
}
#endif /* XTINYPROXY */
if ((connptr->server_fd != -1) && safe_write(connptr->server_fd, header, strlen(header)) < 0) {
if ((connptr->server_fd != -1)
&& safe_write(connptr->server_fd, header,
strlen(header)) < 0) {
safefree(header);
return -1;
}
@ -603,7 +651,8 @@ static int process_client_headers(struct conn_s *connptr)
* Spin here pulling the data from the client.
*/
if (content_length >= 0)
return pull_client_data(connptr, (unsigned long int)content_length);
return pull_client_data(connptr,
(unsigned long int) content_length);
else
return 0;
}
@ -612,13 +661,15 @@ static int process_client_headers(struct conn_s *connptr)
* Loop through all the headers (including the response code) from the
* server.
*/
static int process_server_headers(struct conn_s *connptr)
static int
process_server_headers(struct conn_s *connptr)
{
char *header;
for (;;) {
if (readline(connptr->server_fd, &header) <= 0) {
DEBUG2("Server (file descriptor %d) closed connection.", connptr->server_fd);
DEBUG2("Server (file descriptor %d) closed connection.",
connptr->server_fd);
return -1;
}
@ -628,7 +679,8 @@ static int process_server_headers(struct conn_s *connptr)
}
if (!connptr->simple_req
&& safe_write(connptr->client_fd, header, strlen(header)) < 0) {
&& safe_write(connptr->client_fd, header,
strlen(header)) < 0) {
safefree(header);
return -1;
}
@ -652,7 +704,8 @@ static int process_server_headers(struct conn_s *connptr)
* tinyproxy oh so long ago...)
* - rjkaes
*/
static void relay_connection(struct conn_s *connptr)
static void
relay_connection(struct conn_s *connptr)
{
fd_set rset, wset;
struct timeval tv;
@ -670,7 +723,8 @@ static void relay_connection(struct conn_s *connptr)
FD_ZERO(&rset);
FD_ZERO(&wset);
tv.tv_sec = config.idletimeout - difftime(time(NULL), last_access);
tv.tv_sec =
config.idletimeout - difftime(time(NULL), last_access);
tv.tv_usec = 0;
if (BUFFER_SIZE(connptr->sbuffer) > 0)
@ -687,13 +741,18 @@ static void relay_connection(struct conn_s *connptr)
if (ret == 0) {
tdiff = difftime(time(NULL), last_access);
if (tdiff > config.idletimeout) {
log_message(LOG_INFO, "Idle Timeout (after select) as %g > %u.", tdiff, config.idletimeout);
log_message(LOG_INFO,
"Idle Timeout (after select) as %g > %u.",
tdiff, config.idletimeout);
return;
} else {
continue;
}
} else if (ret < 0) {
log_message(LOG_ERR, "relay_connection: select() error \"%s\". Closing connection (client_fd:%d, server_fd:%d)", strerror(errno), connptr->client_fd, connptr->server_fd);
log_message(LOG_ERR,
"relay_connection: select() error \"%s\". Closing connection (client_fd:%d, server_fd:%d)",
strerror(errno), connptr->client_fd,
connptr->server_fd);
return;
} else {
/*
@ -746,21 +805,25 @@ static void relay_connection(struct conn_s *connptr)
/*
* Establish a connection to the upstream proxy server.
*/
static int connect_to_upstream(struct conn_s *connptr,
struct request_s *request)
static int
connect_to_upstream(struct conn_s *connptr, struct request_s *request)
{
char *combined_string;
int len;
connptr->server_fd = opensock(config.upstream_name, config.upstream_port);
connptr->server_fd =
opensock(config.upstream_name, config.upstream_port);
if (connptr->server_fd < 0) {
log_message(LOG_WARNING, "Could not connect to upstream proxy.");
log_message(LOG_WARNING,
"Could not connect to upstream proxy.");
httperr(connptr, 404, "Unable to connect to upstream proxy.");
return -1;
}
log_message(LOG_CONN, "Established connection to upstream proxy \"%s\" using file descriptor %d.", config.upstream_name, connptr->server_fd);
log_message(LOG_CONN,
"Established connection to upstream proxy \"%s\" using file descriptor %d.",
config.upstream_name, connptr->server_fd);
/*
* We need to re-write the "path" part of the request so that we
@ -775,7 +838,8 @@ static int connect_to_upstream(struct conn_s *connptr,
return -1;
}
snprintf(combined_string, len, "%s:%d", request->host, request->port);
snprintf(combined_string, len, "%s:%d", request->host,
request->port);
} else {
len = strlen(request->host) + strlen(request->path) + 14;
combined_string = safemalloc(len + 1);
@ -783,7 +847,8 @@ static int connect_to_upstream(struct conn_s *connptr,
return -1;
}
snprintf(combined_string, len, "http://%s:%d%s", request->host, request->port, request->path);
snprintf(combined_string, len, "http://%s:%d%s", request->host,
request->port, request->path);
}
safefree(request->path);
@ -802,7 +867,8 @@ static int connect_to_upstream(struct conn_s *connptr,
* tinyproxy code, which was confusing, redundant. Hail progress.
* - rjkaes
*/
void handle_connection(int fd)
void
handle_connection(int fd)
{
struct conn_s *connptr;
struct request_s *request = NULL;
@ -826,10 +892,10 @@ void handle_connection(int fd)
if (check_acl(fd) <= 0) {
update_stats(STAT_DENIED);
httperr(connptr, 403, "You do not have authorization for using this service.");
httperr(connptr, 403,
"You do not have authorization for using this service.");
goto send_error;
}
#ifdef TUNNEL_SUPPORT
/*
* If tunnel has been configured then redirect any connections to
@ -841,16 +907,20 @@ void handle_connection(int fd)
log_message(LOG_INFO, "Redirecting to %s:%d",
config.tunnel_name, config.tunnel_port);
connptr->server_fd = opensock(config.tunnel_name, config.tunnel_port);
connptr->server_fd =
opensock(config.tunnel_name, config.tunnel_port);
if (connptr->server_fd < 0) {
log_message(LOG_WARNING, "Could not connect to tunnel.");
log_message(LOG_WARNING,
"Could not connect to tunnel.");
httperr(connptr, 404, "Unable to connect to tunnel.");
goto internal_proxy;
}
log_message(LOG_INFO, "Established a connection to the tunnel \"%s\" using file descriptor %d.", config.tunnel_name, connptr->server_fd);
log_message(LOG_INFO,
"Established a connection to the tunnel \"%s\" using file descriptor %d.",
config.tunnel_name, connptr->server_fd);
/*
* I know GOTOs are evil, but duplicating the code is even
@ -880,7 +950,6 @@ internal_proxy:
}
goto send_error;
}
#ifdef UPSTREAM_SUPPORT
if (config.upstream_name && config.upstream_port != -1) {
connptr->upstream = TRUE;
@ -895,7 +964,9 @@ internal_proxy:
goto send_error;
}
log_message(LOG_CONN, "Established connection to host \"%s\" using file descriptor %d.", request->host, connptr->server_fd);
log_message(LOG_CONN,
"Established connection to host \"%s\" using file descriptor %d.",
request->host, connptr->server_fd);
if (!connptr->ssl)
establish_http_connection(connptr, request);
@ -929,7 +1000,8 @@ send_error:
}
} else {
if (send_ssl_response(connptr) < 0) {
log_message(LOG_ERR, "handle_connection: Could not send SSL greeting to client.");
log_message(LOG_ERR,
"handle_connection: Could not send SSL greeting to client.");
update_stats(STAT_BADCONN);
destroy_conn(connptr);
return;

View File

@ -1,4 +1,4 @@
/* $Id: stats.c,v 1.6 2001-09-15 21:27:58 rjkaes Exp $
/* $Id: stats.c,v 1.7 2001-11-22 00:31:10 rjkaes Exp $
*
* This module handles the statistics for tinyproxy. There are only two
* public API functions. The reason for the functions, rather than just a
@ -48,7 +48,8 @@ pthread_mutex_t stats_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* Initialise the statistics information to zero.
*/
void init_stats(void)
void
init_stats(void)
{
LOCK();
memset(&stats, 0, sizeof(stats));
@ -58,18 +59,19 @@ void init_stats(void)
/*
* Display the statics of the tinyproxy server.
*/
int showstats(struct conn_s *connptr)
int
showstats(struct conn_s *connptr)
{
static char *msg = \
"<html><head><title>%s (%s) stats</title></head>\r\n" \
"<body>\r\n" \
"<center><h2>%s (%s) run-time statistics</h2></center><hr>\r\n" \
"<blockquote>\r\n" \
"Number of open connections: %lu<br>\r\n" \
"Number of requests: %lu<br>\r\n" \
"Number of bad connections: %lu<br>\r\n" \
"Number of denied connections: %lu<br>\r\n" \
"Number of refused connections due to high load: %lu\r\n" \
static char *msg =
"<html><head><title>%s (%s) stats</title></head>\r\n"
"<body>\r\n"
"<center><h2>%s (%s) run-time statistics</h2></center><hr>\r\n"
"<blockquote>\r\n"
"Number of open connections: %lu<br>\r\n"
"Number of requests: %lu<br>\r\n"
"Number of bad connections: %lu<br>\r\n"
"Number of denied connections: %lu<br>\r\n"
"Number of refused connections due to high load: %lu\r\n"
"</blockquote>\r\n</body></html>\r\n";
char *message_buffer;
@ -83,9 +85,7 @@ int showstats(struct conn_s *connptr)
PACKAGE, VERSION, PACKAGE, VERSION,
stats.num_open,
stats.num_reqs,
stats.num_badcons,
stats.num_denied,
stats.num_refused);
stats.num_badcons, stats.num_denied, stats.num_refused);
UNLOCK();
if (send_http_message(connptr, 200, "OK", message_buffer) < 0) {
@ -101,7 +101,8 @@ int showstats(struct conn_s *connptr)
* Update the value of the statistics. The update_level is defined in
* stats.h
*/
int update_stats(status_t update_level)
int
update_stats(status_t update_level)
{
LOCK();
switch (update_level) {

View File

@ -1,4 +1,4 @@
/* $Id: stats.h,v 1.3 2001-10-25 16:58:50 rjkaes Exp $
/* $Id: stats.h,v 1.4 2001-11-22 00:31:10 rjkaes Exp $
*
* See 'stats.h' for a detailed description.
*

View File

@ -1,4 +1,4 @@
/* $Id: ternary.c,v 1.11 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: ternary.c,v 1.12 2001-11-22 00:31:10 rjkaes Exp $
*
* This module creates a Ternary Search Tree which can store both string
* keys, and arbitrary data for each key. It works similar to a hash, and
@ -82,7 +82,8 @@ char te_errbuf[256];
* (te_errbuf has disambiguating string)
* Exceptions: none
*/
static TERNARY create_token_ref(unsigned int ind)
static TERNARY
create_token_ref(unsigned int ind)
{
unsigned int high; /* high 16 bits of token (index) */
unsigned int low; /* low 16 bits of token (nonce) */
@ -136,7 +137,8 @@ static TERNARY create_token_ref(unsigned int ind)
* (te_errbuf has disambiguating string)
* EXCEPTIONS: none
*/
static int read_token_ref(TERNARY tno)
static int
read_token_ref(TERNARY tno)
{
unsigned int ind; /* index of current tree */
@ -186,7 +188,8 @@ static int read_token_ref(TERNARY tno)
* (te_errbuf has descriptive string)
* Exceptions: none
*/
TERNARY ternary_new(void)
TERNARY
ternary_new(void)
{
int cur; /* index of current tree */
TERNARY token; /* new token for current tree */
@ -242,7 +245,8 @@ TERNARY ternary_new(void)
* read_token_ref()).
* Exceptions: none
*/
int ternary_destroy(TERNARY tno, void (*freeptr)(void *))
int
ternary_destroy(TERNARY tno, void (*freeptr) (void *))
{
int cur; /* index of current tree */
unsigned int i, j;
@ -287,7 +291,8 @@ int ternary_destroy(TERNARY tno, void (*freeptr)(void *))
* TE_TOOFULL tree is full, so no new elements can be added.
* Exceptions: none
*/
int ternary_insert_replace(TERNARY tno, const char *s, void *data,
int
ternary_insert_replace(TERNARY tno, const char *s, void *data,
short int replace)
{
int cur; /* index of current tree */
@ -327,14 +332,14 @@ int ternary_insert_replace(TERNARY tno, const char *s, void *data,
if (tree->bufn-- == 0) {
tree->buf = safecalloc(BUFSIZE, sizeof(Tnode));
if (!tree->buf) {
ERRBUF("ternary_insert: malloc: no more memory");
ERRBUF
("ternary_insert: malloc: no more memory");
return TE_NOROOM;
}
if (tree->freen == BUFARRAY - 1) {
ERRBUF3("ternary_insert: freen %u equals %u",
tree->freen,
BUFARRAY - 1);
tree->freen, BUFARRAY - 1);
return TE_TOOFULL;
}
@ -364,7 +369,8 @@ int ternary_insert_replace(TERNARY tno, const char *s, void *data,
* Errors:
* Exceptions:
*/
int ternary_search(TERNARY tno, const char *s, void **data)
int
ternary_search(TERNARY tno, const char *s, void **data)
{
int cur;
Tnode *p;

View File

@ -1,4 +1,4 @@
/* $Id: ternary.h,v 1.3 2001-08-30 16:52:09 rjkaes Exp $
/* $Id: ternary.h,v 1.4 2001-11-22 00:31:10 rjkaes Exp $
*
* See 'ternary.c' for a detailed description.
*

View File

@ -1,4 +1,4 @@
/* $Id: thread.c,v 1.17 2001-10-24 00:37:23 rjkaes Exp $
/* $Id: thread.c,v 1.18 2001-11-22 00:31:10 rjkaes Exp $
*
* Handles the creation/destruction of the various threads required for
* processing incoming connections.
@ -80,7 +80,8 @@ static pthread_mutex_t servers_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* Set the configuration values for the various thread related settings.
*/
short int thread_configure(thread_config_t type, unsigned int val)
short int
thread_configure(thread_config_t type, unsigned int val)
{
switch (type) {
case THREAD_MAXCLIENTS:
@ -109,7 +110,8 @@ short int thread_configure(thread_config_t type, unsigned int val)
/*
* This is the main (per thread) loop.
*/
static void *thread_main(void *arg)
static void *
thread_main(void *arg)
{
int connfd;
struct sockaddr *cliaddr;
@ -142,7 +144,10 @@ static void *thread_main(void *arg)
DEBUG2("%u connections so far...", ptr->connects);
if (ptr->connects >= thread_config.maxrequestsperchild) {
log_message(LOG_NOTICE, "Thread has reached MaxRequestsPerChild (%u > %u). Killing thread.", ptr->connects, thread_config.maxrequestsperchild);
log_message(LOG_NOTICE,
"Thread has reached MaxRequestsPerChild (%u > %u). Killing thread.",
ptr->connects,
thread_config.maxrequestsperchild);
ptr->status = T_EMPTY;
@ -160,7 +165,8 @@ static void *thread_main(void *arg)
*/
SERVER_UNLOCK();
log_message(LOG_NOTICE, "Waiting servers exceeds MaxSpareServers. Killing thread.");
log_message(LOG_NOTICE,
"Waiting servers exceeds MaxSpareServers. Killing thread.");
ptr->status = T_EMPTY;
@ -181,7 +187,8 @@ static void *thread_main(void *arg)
/*
* Create the initial pool of threads.
*/
short int thread_pool_create(void)
short int
thread_pool_create(void)
{
unsigned int i;
@ -196,26 +203,33 @@ short int thread_pool_create(void)
pthread_attr_setstacksize(&thread_attr, THREAD_STACK_SIZE);
if (thread_config.maxclients == 0) {
log_message(LOG_ERR, "thread_pool_create: \"MaxClients\" must be greater than zero.");
log_message(LOG_ERR,
"thread_pool_create: \"MaxClients\" must be greater than zero.");
return -1;
}
if (thread_config.startservers == 0) {
log_message(LOG_ERR, "thread_pool_create: \"StartServers\" must be greater than zero.");
log_message(LOG_ERR,
"thread_pool_create: \"StartServers\" must be greater than zero.");
return -1;
}
thread_ptr = safecalloc((size_t)thread_config.maxclients, sizeof(struct thread_s));
thread_ptr =
safecalloc((size_t) thread_config.maxclients,
sizeof(struct thread_s));
if (!thread_ptr)
return -1;
if (thread_config.startservers > thread_config.maxclients) {
log_message(LOG_WARNING, "Can not start more than \"MaxClients\" servers. Starting %u servers instead.", thread_config.maxclients);
log_message(LOG_WARNING,
"Can not start more than \"MaxClients\" servers. Starting %u servers instead.",
thread_config.maxclients);
thread_config.startservers = thread_config.maxclients;
}
for (i = 0; i < thread_config.startservers; i++) {
thread_ptr[i].status = T_WAITING;
pthread_create(&thread_ptr[i].tid, &thread_attr, &thread_main, &thread_ptr[i]);
pthread_create(&thread_ptr[i].tid, &thread_attr, &thread_main,
&thread_ptr[i]);
}
servers_waiting = thread_config.startservers;
@ -231,7 +245,8 @@ short int thread_pool_create(void)
* Keep the proper number of servers running. This is the birth of the
* servers. It monitors this at least once a second.
*/
void thread_main_loop(void)
void
thread_main_loop(void)
{
int i;
@ -242,13 +257,15 @@ void thread_main_loop(void)
for (i = 0; i < thread_config.maxclients; i++) {
if (thread_ptr[i].status == T_EMPTY) {
pthread_create(&thread_ptr[i].tid, &thread_attr, &thread_main, &thread_ptr[i]);
pthread_create(&thread_ptr[i].tid, &thread_attr,
&thread_main, &thread_ptr[i]);
thread_ptr[i].status = T_WAITING;
thread_ptr[i].connects = 0;
SERVER_INC();
log_message(LOG_NOTICE, "Waiting servers is less than MinSpareServers. Creating new thread.");
log_message(LOG_NOTICE,
"Waiting servers is less than MinSpareServers. Creating new thread.");
break;
}
@ -257,13 +274,15 @@ void thread_main_loop(void)
SERVER_UNLOCK();
}
int thread_listening_sock(uint16_t port)
int
thread_listening_sock(uint16_t port)
{
listenfd = listen_sock(port, &addrlen);
return listenfd;
}
void thread_close_sock(void)
void
thread_close_sock(void)
{
close(listenfd);
}

View File

@ -1,4 +1,4 @@
/* $Id: tinyproxy.c,v 1.18 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: tinyproxy.c,v 1.19 2001-11-22 00:31:10 rjkaes Exp $
*
* The initialise routine. Basically sets up all the initial stuff (logfile,
* listening socket, config options, etc.) and then sits there and loops
@ -49,7 +49,8 @@ float load = 0.00;
/*
* Handle a signal
*/
void takesig(int sig)
void
takesig(int sig)
{
switch (sig) {
case SIGHUP:
@ -62,7 +63,8 @@ void takesig(int sig)
rename_file = safemalloc(strlen(config.logf_name) + 5);
if (!rename_file) {
fprintf(stderr, "Could not allocate memory in signal handler!\n");
fprintf(stderr,
"Could not allocate memory in signal handler!\n");
exit(EX_OSERR);
}
@ -73,14 +75,16 @@ void takesig(int sig)
log_file_des = create_file_safely(config.logf_name);
if (log_file_des < 0) {
fprintf(stderr, "Could not safely create new log file.\n");
fprintf(stderr,
"Could not safely create new log file.\n");
exit(EX_OSERR);
}
old_fd = config.logf;
if (!(config.logf = fdopen(log_file_des, "w"))) {
fprintf(stderr, "Could not create new log file.\n");
fprintf(stderr,
"Could not create new log file.\n");
exit(EX_CANTCREAT);
}
@ -90,7 +94,6 @@ void takesig(int sig)
safefree(rename_file);
}
#ifdef FILTER_ENABLE
if (config.filter) {
filter_destroy();
@ -98,7 +101,8 @@ void takesig(int sig)
}
log_message(LOG_NOTICE, "Re-reading filter file.");
#endif /* FILTER_ENABLE */
log_message(LOG_NOTICE, "Finished cleaning memory/connections.");
log_message(LOG_NOTICE,
"Finished cleaning memory/connections.");
break;
case SIGTERM:
#ifdef FILTER_ENABLE
@ -118,7 +122,8 @@ void takesig(int sig)
/*
* Display the version information for the user.
*/
static void display_version(void)
static void
display_version(void)
{
printf("%s %s (%s)\n", PACKAGE, VERSION, TARGET_SYSTEM);
}
@ -126,7 +131,8 @@ static void display_version(void)
/*
* Display the copyright and license for this program.
*/
static void display_license(void)
static void
display_license(void)
{
display_version();
@ -154,7 +160,8 @@ static void display_license(void)
/*
* Display usage to the user.
*/
static void display_usage(void)
static void
display_usage(void)
{
printf("Usage: %s [options]\n", PACKAGE);
printf("\
@ -165,7 +172,6 @@ Options:\n\
-l Display the license.\n\
-v Display the version number.\n");
/* Display the modes compiled into tinyproxy */
printf("\nFeatures Compiled In:\n");
#ifdef XTINYPROXY_ENABLE
@ -183,7 +189,8 @@ Options:\n\
#endif /* TUNNEL_SUPPORT */
}
int main(int argc, char **argv)
int
main(int argc, char **argv)
{
int optch;
bool_t godaemon = TRUE;
@ -197,7 +204,8 @@ int main(int argc, char **argv)
#ifdef HAVE_SETRLIMIT
struct rlimit core_limit = { 0, 0 };
if (setrlimit(RLIMIT_CORE, &core_limit) < 0) {
fprintf(stderr, "%s: Could not set the core limit to zero.\n", argv[0]);
fprintf(stderr, "%s: Could not set the core limit to zero.\n",
argv[0]);
exit(EX_SOFTWARE);
}
#endif /* HAVE_SETRLIMIT */
@ -205,8 +213,7 @@ int main(int argc, char **argv)
/*
* Process the various options
*/
while ((optch = getopt(argc, argv, "c:vldh")) !=
EOF) {
while ((optch = getopt(argc, argv, "c:vldh")) != EOF) {
switch (optch) {
case 'v':
display_version();
@ -220,7 +227,9 @@ int main(int argc, char **argv)
case 'c':
conf_file = strdup(optarg);
if (!conf_file) {
fprintf(stderr, "%s: Could not allocate memory.\n", argv[0]);
fprintf(stderr,
"%s: Could not allocate memory.\n",
argv[0]);
exit(EX_SOFTWARE);
}
break;
@ -236,14 +245,18 @@ int main(int argc, char **argv)
*/
yyin = fopen(conf_file, "r");
if (!yyin) {
fprintf(stderr, "%s: Could not open configuration file \"%s\".\n", argv[0], conf_file);
fprintf(stderr,
"%s: Could not open configuration file \"%s\".\n",
argv[0], conf_file);
exit(EX_SOFTWARE);
}
yyparse();
#if defined(TUNNEL_SUPPORT) && defined(UPSTREAM_SUPPORT)
if (config.tunnel_name && config.upstream_name) {
fprintf(stderr, "%s: \"Tunnel\" and \"Upstream\" directives can not be both set.\n", argv[0]);
fprintf(stderr,
"%s: \"Tunnel\" and \"Upstream\" directives can not be both set.\n",
argv[0]);
exit(EX_SOFTWARE);
}
#endif
@ -253,19 +266,24 @@ int main(int argc, char **argv)
int log_file_fd;
if (!config.logf_name) {
fprintf(stderr, "%s: You MUST set a LogFile in the configuration file.\n", argv[0]);
fprintf(stderr,
"%s: You MUST set a LogFile in the configuration file.\n",
argv[0]);
exit(EX_SOFTWARE);
}
log_file_fd = create_file_safely(config.logf_name);
if (log_file_fd < 0) {
fprintf(stderr, "Could not safely create logfile \"%s\".\n", config.logf_name);
fprintf(stderr,
"Could not safely create logfile \"%s\".\n",
config.logf_name);
exit(EX_CANTCREAT);
}
config.logf = fdopen(log_file_fd, "w");
if (!config.logf) {
fprintf(stderr, "Could not write to log file \"%s\".\n", config.logf_name);
fprintf(stderr, "Could not write to log file \"%s\".\n",
config.logf_name);
exit(EX_CANTCREAT);
}
} else {
@ -281,18 +299,23 @@ int main(int argc, char **argv)
* Set the default values if they were not set in the config file.
*/
if (config.port == 0) {
fprintf(stderr, "%s: You MUST set a Port in the configuration file.\n", argv[0]);
fprintf(stderr,
"%s: You MUST set a Port in the configuration file.\n",
argv[0]);
exit(EX_SOFTWARE);
}
if (!config.stathost) {
log_message(LOG_INFO, "Setting stathost to \"%s\".", DEFAULT_STATHOST);
log_message(LOG_INFO, "Setting stathost to \"%s\".",
DEFAULT_STATHOST);
config.stathost = DEFAULT_STATHOST;
}
if (!config.username) {
log_message(LOG_WARNING, "You SHOULD set a UserName in the configuration file. Using current user instead.");
log_message(LOG_WARNING,
"You SHOULD set a UserName in the configuration file. Using current user instead.");
}
if (config.idletimeout == 0) {
log_message(LOG_INFO, "Setting idle timeout to %u seconds.", MAX_IDLE_TIME);
log_message(LOG_INFO, "Setting idle timeout to %u seconds.",
MAX_IDLE_TIME);
config.idletimeout = MAX_IDLE_TIME;
}
@ -318,10 +341,10 @@ int main(int argc, char **argv)
}
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
fprintf(stderr, "%s: Could not set the \"SIGPIPE\" signal.\n", argv[0]);
fprintf(stderr, "%s: Could not set the \"SIGPIPE\" signal.\n",
argv[0]);
exit(EX_OSERR);
}
#ifdef FILTER_ENABLE
if (config.filter)
filter_init();
@ -331,7 +354,8 @@ int main(int argc, char **argv)
* Start listening on the selected port.
*/
if (thread_listening_sock(config.port) < 0) {
fprintf(stderr, "%s: Could not create listening socket.\n", argv[0]);
fprintf(stderr, "%s: Could not create listening socket.\n",
argv[0]);
exit(EX_OSERR);
}
@ -342,33 +366,45 @@ int main(int argc, char **argv)
if (config.group && strlen(config.group) > 0) {
thisgroup = getgrnam(config.group);
if (!thisgroup) {
fprintf(stderr, "%s: Unable to find group \"%s\".\n", argv[0], config.group);
fprintf(stderr,
"%s: Unable to find group \"%s\".\n",
argv[0], config.group);
exit(EX_NOUSER);
}
if (setgid(thisgroup->gr_gid) < 0) {
fprintf(stderr, "%s: Unable to change to group \"%s\".\n", argv[0], config.group);
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);
log_message(LOG_INFO, "Now running as group \"%s\".",
config.group);
}
if (config.username && strlen(config.username) > 0) {
thisuser = getpwnam(config.username);
if (!thisuser) {
fprintf(stderr, "%s: Unable to find user \"%s\".", argv[0], config.username);
fprintf(stderr,
"%s: Unable to find user \"%s\".",
argv[0], config.username);
exit(EX_NOUSER);
}
if (setuid(thisuser->pw_uid) < 0) {
fprintf(stderr, "%s: Unable to change to user \"%s\".", argv[0], config.username);
fprintf(stderr,
"%s: Unable to change to user \"%s\".",
argv[0], config.username);
exit(EX_CANTCREAT);
}
log_message(LOG_INFO, "Now running as user \"%s\".", config.username);
log_message(LOG_INFO, "Now running as user \"%s\".",
config.username);
}
} else {
log_message(LOG_WARNING, "Not running as root, so not changing UID/GID.");
log_message(LOG_WARNING,
"Not running as root, so not changing UID/GID.");
}
if (thread_pool_create() < 0) {
fprintf(stderr, "%s: Could not create the pool of threads.", argv[0]);
fprintf(stderr, "%s: Could not create the pool of threads.",
argv[0]);
exit(EX_SOFTWARE);
}
@ -377,11 +413,13 @@ int main(int argc, char **argv)
*/
log_message(LOG_INFO, "Setting the various signals.");
if (signal(SIGTERM, takesig) == SIG_ERR) {
fprintf(stderr, "%s: Could not set the \"SIGTERM\" signal.\n", argv[0]);
fprintf(stderr, "%s: Could not set the \"SIGTERM\" signal.\n",
argv[0]);
exit(EX_OSERR);
}
if (signal(SIGHUP, takesig) == SIG_ERR) {
fprintf(stderr, "%s: Could not set the \"SIGHUP\" signal.\n", argv[0]);
fprintf(stderr, "%s: Could not set the \"SIGHUP\" signal.\n",
argv[0]);
exit(EX_OSERR);
}
@ -401,10 +439,10 @@ int main(int argc, char **argv)
* Remove the PID file.
*/
if (unlink(config.pidpath) < 0) {
log_message(LOG_WARNING, "Could not remove PID file \"%s\": %s.",
log_message(LOG_WARNING,
"Could not remove PID file \"%s\": %s.",
config.pidpath, strerror(errno));
}
#ifdef FILTER_ENABLE
if (config.filter)
filter_destroy();

View File

@ -1,4 +1,4 @@
/* $Id: tinyproxy.h,v 1.19 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: tinyproxy.h,v 1.20 2001-11-22 00:31:10 rjkaes Exp $
*
* See 'tinyproxy.c' for a detailed description.
*

View File

@ -1,4 +1,4 @@
/* $Id: utils.c,v 1.16 2001-10-25 17:27:39 rjkaes Exp $
/* $Id: utils.c,v 1.17 2001-11-22 00:31:10 rjkaes Exp $
*
* Misc. routines which are used by the various functions to handle strings
* and memory allocation and pretty much anything else we can think of. Also,
@ -32,28 +32,35 @@
*/
#ifndef NDEBUG
void *debugging_calloc(size_t nmemb, size_t size, const char *file, unsigned long line)
void *
debugging_calloc(size_t nmemb, size_t size, const char *file,
unsigned long line)
{
void *ptr = calloc(nmemb, size);
fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file, line);
fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file,
line);
return ptr;
}
void *debugging_malloc(size_t size, const char *file, unsigned long line)
void *
debugging_malloc(size_t size, const char *file, unsigned long line)
{
void *ptr = malloc(size);
fprintf(stderr, "{malloc: %p:%u} %s:%lu\n", ptr, size, file, line);
return ptr;
}
void *debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line)
void *
debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line)
{
void *newptr = realloc(ptr, size);
fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size, file, line);
fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size,
file, line);
return newptr;
}
void debugging_free(void *ptr, const char *file, unsigned long line)
void
debugging_free(void *ptr, const char *file, unsigned long line)
{
fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line);
free(ptr);
@ -66,17 +73,16 @@ void debugging_free(void *ptr, const char *file, unsigned long line)
/*
* Build the data for a complete HTTP & HTML message for the client.
*/
int send_http_message(struct conn_s* connptr, int http_code,
int
send_http_message(struct conn_s *connptr, int http_code,
const char *error_title, const char *message)
{
static char *headers = \
"HTTP/1.0 %d %s\r\n" \
"Server: %s/%s\r\n" \
"Date: %s\r\n" \
"Content-Type: text/html\r\n" \
"Content-Length: %d\r\n" \
"Connection: close\r\n" \
"\r\n";
static char *headers =
"HTTP/1.0 %d %s\r\n"
"Server: %s/%s\r\n"
"Date: %s\r\n"
"Content-Type: text/html\r\n"
"Content-Length: %d\r\n" "Connection: close\r\n" "\r\n";
char *header_buffer;
char timebuf[30];
@ -87,9 +93,11 @@ int send_http_message(struct conn_s* connptr, int http_code,
return -1;
global_time = time(NULL);
strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&global_time));
strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT",
gmtime(&global_time));
snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code, error_title, PACKAGE, VERSION, timebuf, strlen(message));
snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code,
error_title, PACKAGE, VERSION, timebuf, strlen(message));
safe_write(connptr->client_fd, header_buffer, strlen(header_buffer));
safe_write(connptr->client_fd, message, strlen(message));
@ -104,15 +112,16 @@ int send_http_message(struct conn_s* connptr, int http_code,
/*
* Display an error to the client.
*/
int httperr(struct conn_s *connptr, int err, const char *msg)
int
httperr(struct conn_s *connptr, int err, const char *msg)
{
static char *message = \
"<html><head><title>%s</title></head>\r\n" \
"<body>\r\n" \
"<font size=\"+2\">Cache Error!</font><br>\r\n" \
"An error of type %d occurred: %s\r\n" \
"<hr>\r\n" \
"<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n" \
static char *message =
"<html><head><title>%s</title></head>\r\n"
"<body>\r\n"
"<font size=\"+2\">Cache Error!</font><br>\r\n"
"An error of type %d occurred: %s\r\n"
"<hr>\r\n"
"<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n"
"</body></html>\r\n\r\n";
char *message_buffer;
@ -121,7 +130,8 @@ int httperr(struct conn_s *connptr, int err, const char *msg)
if (!message_buffer)
return -1;
snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg, PACKAGE, VERSION);
snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg,
PACKAGE, VERSION);
if (send_http_message(connptr, err, msg, message_buffer) < 0) {
safefree(message_buffer);
@ -132,7 +142,8 @@ int httperr(struct conn_s *connptr, int err, const char *msg)
return 0;
}
void makedaemon(void)
void
makedaemon(void)
{
if (fork() != 0)
exit(0);
@ -154,7 +165,8 @@ void makedaemon(void)
/*
* Safely creates filename and returns the low-level file descriptor.
*/
int create_file_safely(const char *filename)
int
create_file_safely(const char *filename)
{
struct stat lstatinfo;
int fildes;
@ -170,7 +182,8 @@ int create_file_safely(const char *filename)
* existing", exit.
*/
if (errno != ENOENT) {
log_message(LOG_ERR, "create_file_safely: Error checking PID file %s: %s.",
log_message(LOG_ERR,
"create_file_safely: Error checking PID file %s: %s.",
filename, strerror(errno));
return -1;
}
@ -180,8 +193,10 @@ int create_file_safely(const char *filename)
* sure an attacker can't slip in a file between the lstat()
* and open()
*/
if ((fildes = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) {
log_message(LOG_ERR, "create_file_safely: Could not create PID file %s: %s.",
if ((fildes =
open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) {
log_message(LOG_ERR,
"create_file_safely: Could not create PID file %s: %s.",
filename, strerror(errno));
return -1;
}
@ -192,7 +207,8 @@ int create_file_safely(const char *filename)
* Open an existing file.
*/
if ((fildes = open(filename, O_RDWR)) < 0) {
log_message(LOG_ERR, "create_file_safely: Could not open PID file %s: %s.",
log_message(LOG_ERR,
"create_file_safely: Could not open PID file %s: %s.",
filename, strerror(errno));
return -1;
}
@ -205,7 +221,8 @@ int create_file_safely(const char *filename)
|| lstatinfo.st_mode != fstatinfo.st_mode
|| lstatinfo.st_ino != fstatinfo.st_ino
|| lstatinfo.st_dev != fstatinfo.st_dev) {
log_message(LOG_ERR, "create_file_safely: The PID file %s has been changed before it could be opened.",
log_message(LOG_ERR,
"create_file_safely: The PID file %s has been changed before it could be opened.",
filename);
close(fildes);
return -1;
@ -219,7 +236,8 @@ int create_file_safely(const char *filename)
* st_mode check would also find this)
*/
if (fstatinfo.st_nlink > 1 || !S_ISREG(lstatinfo.st_mode)) {
log_message(LOG_ERR, "create_file_safely: The PID file %s has too many links, or is not a regular file: %s.",
log_message(LOG_ERR,
"create_file_safely: The PID file %s has too many links, or is not a regular file: %s.",
filename, strerror(errno));
close(fildes);
return -1;
@ -237,8 +255,10 @@ int create_file_safely(const char *filename)
ftruncate(fildes, 0);
#else
close(fildes);
if ((fildes = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
log_message(LOG_ERR, "create_file_safely: Could not open PID file %s: %s.",
if ((fildes =
open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
log_message(LOG_ERR,
"create_file_safely: Could not open PID file %s: %s.",
filename, strerror(errno));
return -1;
}
@ -251,7 +271,8 @@ int create_file_safely(const char *filename)
/*
* Write the PID of the program to the specified file.
*/
void pidfile_create(const char *filename)
void
pidfile_create(const char *filename)
{
int fildes;
FILE *fd;
@ -266,7 +287,8 @@ void pidfile_create(const char *filename)
* Open a stdio file over the low-level one.
*/
if ((fd = fdopen(fildes, "w")) == NULL) {
log_message(LOG_ERR, "pidfile_create: fdopen() error on PID file %s: %s.",
log_message(LOG_ERR,
"pidfile_create: fdopen() error on PID file %s: %s.",
filename, strerror(errno));
close(fildes);
unlink(filename);
@ -283,7 +305,8 @@ void pidfile_create(const char *filename)
* buffer, and always NULL terminates the buffer. size is the size of the
* destination buffer.
*/
size_t strlcpy(char *dst, const char *src, size_t size)
size_t
strlcpy(char *dst, const char *src, size_t size)
{
size_t len = strlen(src);
size_t ret = len;
@ -305,7 +328,8 @@ size_t strlcpy(char *dst, const char *src, size_t size)
* buffer, which should be one more than the maximum resulting string
* length.
*/
size_t strlcat(char *dst, const char *src, size_t size)
size_t
strlcat(char *dst, const char *src, size_t size)
{
size_t len1 = strlen(dst);
size_t len2 = strlen(src);

View File

@ -1,4 +1,4 @@
/* $Id: utils.h,v 1.10 2001-10-25 16:58:50 rjkaes Exp $
/* $Id: utils.h,v 1.11 2001-11-22 00:31:10 rjkaes Exp $
*
* See 'utils.h' for a detailed description.
*
@ -45,10 +45,13 @@ extern size_t strlcpy(char *dst, const char *src, size_t size);
*/
#ifndef NDEBUG
extern void *debugging_calloc(size_t nmemb, size_t size, const char *file, unsigned long line);
extern void *debugging_malloc(size_t size, const char *file, unsigned long line);
extern void *debugging_calloc(size_t nmemb, size_t size, const char *file,
unsigned long line);
extern void *debugging_malloc(size_t size, 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, unsigned long line);
extern void *debugging_realloc(void *ptr, size_t size, const char *file,
unsigned long line);
# define safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__)
# define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__)