2002-06-01 02:26:30 +08:00
|
|
|
/* $Id: utils.c,v 1.32 2002-05-31 18:26:30 rjkaes Exp $
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
|
|
|
* 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,
|
2000-09-12 08:01:29 +08:00
|
|
|
* the load cutoff routine is in here. Could not think of a better place for
|
|
|
|
* it, so it's in here.
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
2001-12-20 12:48:52 +08:00
|
|
|
* Copyright (C) 1998 Steven Young
|
|
|
|
* Copyright (C) 1999,2001 Robert James Kaes (rjkaes@flarenet.com)
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2000-09-12 08:01:29 +08:00
|
|
|
#include "tinyproxy.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
#include "buffer.h"
|
2001-10-26 00:58:50 +08:00
|
|
|
#include "conns.h"
|
2002-04-19 00:57:06 +08:00
|
|
|
#include "filter.h"
|
2002-05-24 02:28:12 +08:00
|
|
|
#include "heap.h"
|
2000-09-12 08:01:29 +08:00
|
|
|
#include "log.h"
|
2002-05-24 02:28:12 +08:00
|
|
|
#include "network.h"
|
2000-09-12 08:01:29 +08:00
|
|
|
#include "sock.h"
|
|
|
|
#include "utils.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-09-09 02:55:58 +08:00
|
|
|
|
2001-09-16 05:29:59 +08:00
|
|
|
#define HEADER_SIZE (1024 * 8)
|
2000-02-17 01:32:49 +08:00
|
|
|
/*
|
2001-09-16 05:29:59 +08:00
|
|
|
* Build the data for a complete HTTP & HTML message for the client.
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
int
|
|
|
|
send_http_message(struct conn_s *connptr, int http_code,
|
|
|
|
const char *error_title, const char *message)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2001-12-24 08:02:32 +08:00
|
|
|
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";
|
|
|
|
|
2001-08-27 11:45:34 +08:00
|
|
|
char timebuf[30];
|
|
|
|
time_t global_time;
|
2002-04-16 11:22:16 +08:00
|
|
|
size_t message_len = strlen(message);
|
2001-08-27 11:45:34 +08:00
|
|
|
|
|
|
|
global_time = time(NULL);
|
2001-11-22 08:31:10 +08:00
|
|
|
strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT",
|
|
|
|
gmtime(&global_time));
|
2001-08-27 11:45:34 +08:00
|
|
|
|
2002-04-29 04:03:53 +08:00
|
|
|
if (write_message(connptr->client_fd,
|
|
|
|
headers,
|
|
|
|
http_code, error_title, PACKAGE, VERSION,
|
|
|
|
timebuf, message_len) < 0)
|
|
|
|
return -1;
|
2001-08-27 11:45:34 +08:00
|
|
|
|
2002-04-16 11:22:16 +08:00
|
|
|
return safe_write(connptr->client_fd, message, message_len);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2001-09-16 05:29:59 +08:00
|
|
|
/*
|
|
|
|
* Display an error to the client.
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
int
|
2002-04-15 10:07:27 +08:00
|
|
|
send_http_error_message(struct conn_s *connptr)
|
2001-09-16 05:29:59 +08:00
|
|
|
{
|
2001-12-24 08:02:32 +08:00
|
|
|
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";
|
2001-09-16 05:29:59 +08:00
|
|
|
|
|
|
|
char *message_buffer;
|
2001-12-24 08:02:32 +08:00
|
|
|
char *tmpbuf;
|
|
|
|
size_t size = (1024 * 8); /* start with 8 KB */
|
|
|
|
ssize_t n;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
message_buffer = safemalloc(size);
|
2001-09-16 05:29:59 +08:00
|
|
|
if (!message_buffer)
|
|
|
|
return -1;
|
|
|
|
|
2001-12-24 08:02:32 +08:00
|
|
|
/*
|
|
|
|
* Build a new line. Keep increasing the size until the line fits.
|
|
|
|
* See the write_message() function in sock.c for more information.
|
|
|
|
*/
|
|
|
|
while (1) {
|
2002-04-15 10:07:27 +08:00
|
|
|
n = snprintf(message_buffer, size, message,
|
|
|
|
connptr->error_string, connptr->error_number,
|
|
|
|
connptr->error_string, PACKAGE, VERSION);
|
2001-12-24 08:02:32 +08:00
|
|
|
|
|
|
|
if (n > -1 && n < size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (n > - 1)
|
|
|
|
size = n + 1;
|
|
|
|
else
|
|
|
|
size *= 2;
|
|
|
|
|
|
|
|
if ((tmpbuf = saferealloc(message_buffer, size)) == NULL) {
|
|
|
|
safefree(message_buffer);
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
message_buffer = tmpbuf;
|
2001-09-16 05:29:59 +08:00
|
|
|
}
|
|
|
|
|
2002-04-15 10:07:27 +08:00
|
|
|
ret = send_http_message(connptr, connptr->error_number,
|
|
|
|
connptr->error_string, message_buffer);
|
2001-09-16 05:29:59 +08:00
|
|
|
safefree(message_buffer);
|
2001-12-24 08:02:32 +08:00
|
|
|
return ret;
|
2001-09-16 05:29:59 +08:00
|
|
|
}
|
|
|
|
|
2002-04-15 10:07:27 +08:00
|
|
|
/*
|
|
|
|
* Add the error information to the conn structure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
indicate_http_error(struct conn_s* connptr, int number, const char* string)
|
|
|
|
{
|
2002-04-19 01:59:21 +08:00
|
|
|
connptr->error_string = safestrdup(string);
|
2002-04-15 10:07:27 +08:00
|
|
|
if (!connptr->error_string)
|
|
|
|
return -1;
|
|
|
|
connptr->error_number = number;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/*
|
2000-09-12 08:01:29 +08:00
|
|
|
* Safely creates filename and returns the low-level file descriptor.
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
int
|
2002-06-01 02:26:30 +08:00
|
|
|
create_file_safely(const char *filename, bool_t truncate_file)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 08:01:29 +08:00
|
|
|
struct stat lstatinfo;
|
|
|
|
int fildes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lstat() the file. If it doesn't exist, create it with O_EXCL.
|
|
|
|
* If it does exist, open it for writing and perform the fstat()
|
|
|
|
* check.
|
|
|
|
*/
|
|
|
|
if (lstat(filename, &lstatinfo) < 0) {
|
|
|
|
/*
|
|
|
|
* If lstat() failed for any reason other than "file not
|
|
|
|
* existing", exit.
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_ERR,
|
2002-06-01 02:26:30 +08:00
|
|
|
"create_file_safely: Error checking file %s: %s.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename, strerror(errno));
|
2000-09-12 08:01:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The file doesn't exist, so create it with O_EXCL to make
|
|
|
|
* sure an attacker can't slip in a file between the lstat()
|
|
|
|
* and open()
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
if ((fildes =
|
|
|
|
open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) {
|
|
|
|
log_message(LOG_ERR,
|
2002-06-01 02:26:30 +08:00
|
|
|
"create_file_safely: Could not create file %s: %s.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename, strerror(errno));
|
2000-09-12 08:01:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct stat fstatinfo;
|
2002-06-01 02:26:30 +08:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = O_RDWR;
|
|
|
|
if (!truncate_file)
|
|
|
|
flags |= O_APPEND;
|
2001-11-22 08:31:10 +08:00
|
|
|
|
2000-09-12 08:01:29 +08:00
|
|
|
/*
|
|
|
|
* Open an existing file.
|
|
|
|
*/
|
2002-06-01 02:26:30 +08:00
|
|
|
if ((fildes = open(filename, flags)) < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_ERR,
|
2002-06-01 02:26:30 +08:00
|
|
|
"create_file_safely: Could not open file %s: %s.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename, strerror(errno));
|
2000-09-12 08:01:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fstat() the opened file and check that the file mode bits,
|
|
|
|
* inode, and device match.
|
|
|
|
*/
|
|
|
|
if (fstat(fildes, &fstatinfo) < 0
|
|
|
|
|| lstatinfo.st_mode != fstatinfo.st_mode
|
|
|
|
|| lstatinfo.st_ino != fstatinfo.st_ino
|
|
|
|
|| lstatinfo.st_dev != fstatinfo.st_dev) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_ERR,
|
2002-06-01 02:26:30 +08:00
|
|
|
"create_file_safely: The file %s has been changed before it could be opened.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename);
|
2000-09-12 08:01:29 +08:00
|
|
|
close(fildes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the above check was passed, we know that the lstat()
|
|
|
|
* and fstat() were done on the same file. Now we check that
|
|
|
|
* there's only one link, and that it's a normal file (this
|
|
|
|
* isn't strictly necessary because the fstat() vs lstat()
|
|
|
|
* st_mode check would also find this)
|
|
|
|
*/
|
|
|
|
if (fstatinfo.st_nlink > 1 || !S_ISREG(lstatinfo.st_mode)) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_ERR,
|
2002-06-01 02:26:30 +08:00
|
|
|
"create_file_safely: The file %s has too many links, or is not a regular file: %s.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename, strerror(errno));
|
2000-09-12 08:01:29 +08:00
|
|
|
close(fildes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-06-01 02:26:30 +08:00
|
|
|
/*
|
|
|
|
* Just return the file descriptor if we _don't_ want the file
|
|
|
|
* truncated.
|
|
|
|
*/
|
|
|
|
if (!truncate_file)
|
|
|
|
return fildes;
|
|
|
|
|
2000-09-12 08:01:29 +08:00
|
|
|
/*
|
2002-05-24 12:45:32 +08:00
|
|
|
* On systems which don't support ftruncate() the best we can
|
2000-09-12 08:01:29 +08:00
|
|
|
* do is to close the file and reopen it in create mode, which
|
|
|
|
* unfortunately leads to a race condition, however "systems
|
|
|
|
* which don't support ftruncate()" is pretty much SCO only,
|
2001-05-27 10:38:46 +08:00
|
|
|
* and if you're using that you deserve what you get.
|
2000-09-12 08:01:29 +08:00
|
|
|
* ("Little sympathy has been extended")
|
|
|
|
*/
|
2001-05-27 10:38:46 +08:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
|
|
|
ftruncate(fildes, 0);
|
|
|
|
#else
|
2000-09-12 08:01:29 +08:00
|
|
|
close(fildes);
|
2001-11-22 08:31:10 +08:00
|
|
|
if ((fildes =
|
|
|
|
open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) {
|
|
|
|
log_message(LOG_ERR,
|
2002-06-01 02:26:30 +08:00
|
|
|
"create_file_safely: Could not open file %s: %s.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename, strerror(errno));
|
2000-09-12 08:01:29 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2001-11-22 08:31:10 +08:00
|
|
|
#endif /* HAVE_FTRUNCATE */
|
2000-09-12 08:01:29 +08:00
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 08:01:29 +08:00
|
|
|
return fildes;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-09-12 08:01:29 +08:00
|
|
|
* Write the PID of the program to the specified file.
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
void
|
|
|
|
pidfile_create(const char *filename)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 08:01:29 +08:00
|
|
|
int fildes;
|
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new file
|
|
|
|
*/
|
2002-06-01 02:26:30 +08:00
|
|
|
if ((fildes = create_file_safely(filename, TRUE)) < 0)
|
2000-09-12 08:01:29 +08:00
|
|
|
exit(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a stdio file over the low-level one.
|
|
|
|
*/
|
|
|
|
if ((fd = fdopen(fildes, "w")) == NULL) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_ERR,
|
|
|
|
"pidfile_create: fdopen() error on PID file %s: %s.",
|
2001-05-27 10:38:46 +08:00
|
|
|
filename, strerror(errno));
|
2000-09-12 08:01:29 +08:00
|
|
|
close(fildes);
|
|
|
|
unlink(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(fd, "%ld\n", (long) getpid());
|
2000-09-12 08:01:29 +08:00
|
|
|
fclose(fd);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2002-04-19 00:57:06 +08:00
|
|
|
/*
|
|
|
|
* Rotate the current log file. This is performed whenever a SIGHUP is
|
|
|
|
* received.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rotate_log_files(void)
|
|
|
|
{
|
|
|
|
char* rename_file;
|
|
|
|
int log_file_des;
|
|
|
|
FILE *old_fd;
|
|
|
|
FILE* new_fd;
|
|
|
|
|
|
|
|
log_message(LOG_NOTICE, "SIGHUP received, cleaning up.");
|
|
|
|
|
|
|
|
#ifdef FILTER_ENABLE
|
|
|
|
if (config.filter) {
|
|
|
|
filter_destroy();
|
|
|
|
filter_init();
|
|
|
|
}
|
|
|
|
log_message(LOG_NOTICE, "Re-reading filter file.");
|
|
|
|
#endif /* FILTER_ENABLE */
|
|
|
|
|
|
|
|
if (config.logf) {
|
|
|
|
rename_file = safemalloc(strlen(config.logf_name) + 5);
|
|
|
|
if (!rename_file) {
|
|
|
|
log_message(LOG_CRIT,
|
|
|
|
"Could not allocate memory when trying to rotate the log file; therefore, the log has not been rotated.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(rename_file, config.logf_name);
|
|
|
|
strcat(rename_file, ".rot");
|
|
|
|
|
|
|
|
rename(config.logf_name, rename_file);
|
|
|
|
|
2002-06-01 02:26:30 +08:00
|
|
|
log_file_des = create_file_safely(config.logf_name, TRUE);
|
2002-04-19 00:57:06 +08:00
|
|
|
if (log_file_des < 0) {
|
|
|
|
log_message(LOG_CRIT,
|
|
|
|
"Could not properly create the new log file; therefore the log has not been rotated.");
|
|
|
|
|
|
|
|
/* Switch the file name back */
|
|
|
|
rename(rename_file, config.logf_name);
|
|
|
|
safefree(rename_file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_fd = config.logf;
|
|
|
|
if (!(new_fd = fdopen(log_file_des, "w"))) {
|
|
|
|
log_message(LOG_CRIT,
|
|
|
|
"Could not create the new log file; therefore, the log has not been rotated.");
|
|
|
|
|
|
|
|
/* Switch the file name back */
|
|
|
|
rename(rename_file, config.logf_name);
|
|
|
|
safefree(rename_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.logf = new_fd;
|
|
|
|
fclose(old_fd);
|
|
|
|
|
|
|
|
log_message(LOG_NOTICE, "Log file rotated.");
|
|
|
|
|
|
|
|
safefree(rename_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
log_message(LOG_NOTICE,
|
|
|
|
"Finished cleaning memory/connections.");
|
|
|
|
}
|