|
|
|
@ -44,11 +44,11 @@ Settings settings = {
|
|
|
|
|
.user = "www",
|
|
|
|
|
|
|
|
|
|
.id_length = 4,
|
|
|
|
|
.worker_count = 1,
|
|
|
|
|
.worker_count = 4,
|
|
|
|
|
.port = 9999,
|
|
|
|
|
.timeout = 4,
|
|
|
|
|
.max_size = 2097152, /* = 2MiB = 1024 * 1024 * 2 */
|
|
|
|
|
.buffer_size = 131072, /* = 128KiB = 1024 * 128 */
|
|
|
|
|
.timeout = 2,
|
|
|
|
|
.max_size = 1048576, /* = 1MiB = 1024 * 1024 */
|
|
|
|
|
.buffer_size = 131072, /* = 128KiB = 1024 * 128 */
|
|
|
|
|
|
|
|
|
|
.verbose = 0,
|
|
|
|
|
.foreground = 0
|
|
|
|
@ -57,6 +57,7 @@ Settings settings = {
|
|
|
|
|
/* functions declarations */
|
|
|
|
|
static void usage(int exit_code);
|
|
|
|
|
static void version(void);
|
|
|
|
|
static void accept_loop(int);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display feuille's basic usage.
|
|
|
|
@ -76,6 +77,89 @@ void version(void)
|
|
|
|
|
die(0, "%s %s by Tom MTT. <tom@heimdall.pm>\n", argv0, VERSION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Feuille's accept loop.
|
|
|
|
|
* server: the server socket.
|
|
|
|
|
*/
|
|
|
|
|
void accept_loop(int server)
|
|
|
|
|
{
|
|
|
|
|
/* get current process' pid */
|
|
|
|
|
int pid = getpid();
|
|
|
|
|
|
|
|
|
|
/* feed the random number god */
|
|
|
|
|
srand(time(0) + pid);
|
|
|
|
|
|
|
|
|
|
/* accept loop */
|
|
|
|
|
int connection;
|
|
|
|
|
while ((connection = accept_connection(server))) {
|
|
|
|
|
verbose(1, "--- new incoming connection. connection ID: %d:%d ---", pid, time(0));
|
|
|
|
|
|
|
|
|
|
char *paste = NULL;
|
|
|
|
|
char *id = NULL;
|
|
|
|
|
char *url = NULL;
|
|
|
|
|
|
|
|
|
|
/* read paste from connection */
|
|
|
|
|
verbose(1, "reading paste from incoming connection...");
|
|
|
|
|
|
|
|
|
|
if ((paste = read_paste(connection)) != NULL) {
|
|
|
|
|
/* generate random ID */
|
|
|
|
|
verbose(1, "done.");
|
|
|
|
|
verbose(2, "generating a random ID...");
|
|
|
|
|
|
|
|
|
|
if ((id = generate_id(settings.id_length)) != NULL) {
|
|
|
|
|
/* write paste to disk */
|
|
|
|
|
verbose(2, "done.");
|
|
|
|
|
verbose(1, "writing paste `%s' to disk...", id);
|
|
|
|
|
|
|
|
|
|
if (write_paste(paste, id) == 0) {
|
|
|
|
|
/* create URL */
|
|
|
|
|
verbose(1, "done.");
|
|
|
|
|
verbose(2, "making the right URL...");
|
|
|
|
|
|
|
|
|
|
if ((url = create_url(id)) != NULL) {
|
|
|
|
|
/* send URL */
|
|
|
|
|
verbose(2, "done.", url);
|
|
|
|
|
verbose(1, "sending the link to the client...");
|
|
|
|
|
|
|
|
|
|
send_response(connection, url);
|
|
|
|
|
|
|
|
|
|
verbose(1, "All done.");
|
|
|
|
|
|
|
|
|
|
free(url);
|
|
|
|
|
} else {
|
|
|
|
|
error("error while making a valid URL.");
|
|
|
|
|
send_response(connection, "Could not create your paste URL.\nPlease try again later.\n");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error("error while writing paste to disk.");
|
|
|
|
|
send_response(connection, "Could not write your paste to disk.\nPlease try again later.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(id);
|
|
|
|
|
} else {
|
|
|
|
|
error("error while generating a random ID.");
|
|
|
|
|
send_response(connection, "Could not generate your paste ID.\nPlease try again later.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(paste);
|
|
|
|
|
} else {
|
|
|
|
|
if (errno == EFBIG)
|
|
|
|
|
send_response(connection, "Paste too big.\n");
|
|
|
|
|
|
|
|
|
|
if (errno == ENOENT)
|
|
|
|
|
send_response(connection, "Empty paste.\n");
|
|
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
send_response(connection, "Timeout'd.\n");
|
|
|
|
|
|
|
|
|
|
error("error %d while reading paste from incoming connection.", errno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* close connection */
|
|
|
|
|
close_connection(connection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Feuille's main function.
|
|
|
|
|
* argc: the argument count.
|
|
|
|
@ -87,14 +171,18 @@ int main(int argc, char *argv[])
|
|
|
|
|
/* locale */
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
|
|
/* syslog setup */
|
|
|
|
|
openlog("feuille", LOG_NDELAY | LOG_PERROR, LOG_USER);
|
|
|
|
|
/* syslog */
|
|
|
|
|
openlog("feuille", LOG_NDELAY | LOG_PERROR, LOG_USER);
|
|
|
|
|
|
|
|
|
|
/* ignore signals that could kill feuille */
|
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* when send(2) or write(2) fails */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* settings */
|
|
|
|
|
long long tmp;
|
|
|
|
|
|
|
|
|
|
/* set number of workers */
|
|
|
|
|
if ((tmp = sysconf(_SC_NPROCESSORS_ONLN)) > 0 && tmp <= USHRT_MAX)
|
|
|
|
|
if ((tmp = sysconf(_SC_NPROCESSORS_ONLN)) > settings.worker_count && tmp <= USHRT_MAX)
|
|
|
|
|
settings.worker_count = tmp;
|
|
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
@ -263,6 +351,7 @@ int main(int argc, char *argv[])
|
|
|
|
|
if ((server = initialize_server()) == -1)
|
|
|
|
|
die(errno, "Failed to initialize server socket: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* make feuille run in the background */
|
|
|
|
|
if (!settings.foreground) {
|
|
|
|
|
verbose(1, "making feuille run in the background...");
|
|
|
|
@ -271,11 +360,6 @@ int main(int argc, char *argv[])
|
|
|
|
|
daemon(1, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ignore most signals that could kill feuille */
|
|
|
|
|
verbose(3, "ignoring signals that could kill feuille...");
|
|
|
|
|
|
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* when send(2) or write(2) fails */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* chroot and drop root permissions */
|
|
|
|
|
if (getuid() == 0) {
|
|
|
|
@ -293,12 +377,13 @@ int main(int argc, char *argv[])
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
|
/* OpenBSD-only security measures */
|
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
|
pledge("proc stdio rpath wpath cpath inet", "stdio rpath wpath cpath inet");
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
/* create a thread pool for incoming connections */
|
|
|
|
|
verbose(1, "initializing worker pool...");
|
|
|
|
|
|
|
|
|
@ -306,95 +391,41 @@ int main(int argc, char *argv[])
|
|
|
|
|
for (int i = 1; i <= settings.worker_count; i++) {
|
|
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
|
verbose(2, " worker n. %d...", i);
|
|
|
|
|
accept_loop(server);
|
|
|
|
|
|
|
|
|
|
pid = getpid();
|
|
|
|
|
|
|
|
|
|
/* feed the random number god */
|
|
|
|
|
srand(time(0) + pid);
|
|
|
|
|
|
|
|
|
|
/* accept loop */
|
|
|
|
|
int connection;
|
|
|
|
|
while ((connection = accept_connection(server))) {
|
|
|
|
|
verbose(1, "--- new incoming connection. connection ID: %d:%d ---", pid, time(0));
|
|
|
|
|
|
|
|
|
|
char *paste = NULL;
|
|
|
|
|
char *id = NULL;
|
|
|
|
|
char *url = NULL;
|
|
|
|
|
|
|
|
|
|
/* read paste from connection */
|
|
|
|
|
verbose(1, "reading paste from incoming connection...");
|
|
|
|
|
|
|
|
|
|
if ((paste = read_paste(connection)) != NULL) {
|
|
|
|
|
/* generate random ID */
|
|
|
|
|
verbose(1, "done.");
|
|
|
|
|
verbose(2, "generating a random ID...");
|
|
|
|
|
|
|
|
|
|
if ((id = generate_id(settings.id_length)) != NULL) {
|
|
|
|
|
/* write paste to disk */
|
|
|
|
|
verbose(2, "done.");
|
|
|
|
|
verbose(1, "writing paste `%s' to disk...", id);
|
|
|
|
|
|
|
|
|
|
if (write_paste(paste, id) == 0) {
|
|
|
|
|
/* create URL */
|
|
|
|
|
verbose(1, "done.");
|
|
|
|
|
verbose(2, "making the right URL...");
|
|
|
|
|
|
|
|
|
|
if ((url = create_url(id)) != NULL) {
|
|
|
|
|
verbose(2, "done.", url);
|
|
|
|
|
verbose(1, "sending the link to the client...");
|
|
|
|
|
|
|
|
|
|
send_response(connection, url);
|
|
|
|
|
|
|
|
|
|
verbose(1, "All done.");
|
|
|
|
|
} else if (pid < 0)
|
|
|
|
|
die(errno, "Could not initialize worker n. %d: %s\n", i, strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* do not create a thread pool if in DEBUG mode */
|
|
|
|
|
verbose(1, "running in DEBUG mode, won't create a worker pool.");
|
|
|
|
|
accept_loop(server);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
free(url);
|
|
|
|
|
} else {
|
|
|
|
|
error("error while making a valid URL.");
|
|
|
|
|
send_response(connection, "Could not create your paste URL.\nPlease try again later.\n");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error("error while writing paste to disk.");
|
|
|
|
|
send_response(connection, "Could not write your paste to disk.\nPlease try again later.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(id);
|
|
|
|
|
} else {
|
|
|
|
|
error("error while generating a random ID.");
|
|
|
|
|
send_response(connection, "Could not generate your paste ID.\nPlease try again later.\n");
|
|
|
|
|
}
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
|
|
free(paste);
|
|
|
|
|
} else {
|
|
|
|
|
if (errno == EFBIG)
|
|
|
|
|
send_response(connection, "Paste too big.\n");
|
|
|
|
|
verbose(1, "all workers have been initialized.");
|
|
|
|
|
verbose(1, "beginning to accept incoming connections.");
|
|
|
|
|
|
|
|
|
|
if (errno == ENOENT)
|
|
|
|
|
send_response(connection, "Empty paste.\n");
|
|
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
send_response(connection, "Timeout'd.\n");
|
|
|
|
|
/* fork again if a child dies */
|
|
|
|
|
int status;
|
|
|
|
|
int child_pid;
|
|
|
|
|
while ((child_pid = wait(&status)) > 0) {
|
|
|
|
|
error("child %d unexpectedly died with exit code %d.", child_pid, WEXITSTATUS(status));
|
|
|
|
|
|
|
|
|
|
error("error %d while reading paste from incoming connection.", errno);
|
|
|
|
|
}
|
|
|
|
|
/* do not fork if child was KILL'ed */
|
|
|
|
|
if (WTERMSIG(status) == 9)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* close connection */
|
|
|
|
|
close_connection(connection);
|
|
|
|
|
}
|
|
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
|
accept_loop(server);
|
|
|
|
|
|
|
|
|
|
} else if (pid < 0)
|
|
|
|
|
die(errno, "Could not initialize worker n. %d: %s\n", i, strerror(errno));
|
|
|
|
|
error("could not fork killed child again: ", strerror(errno));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
|
|
verbose(1, "all workers have been initialized.");
|
|
|
|
|
verbose(1, "beginning to accept incoming connections.");
|
|
|
|
|
|
|
|
|
|
/* wait for children to finish */
|
|
|
|
|
// TODO: handle children exit codes properly
|
|
|
|
|
while (wait(0) > 0);
|
|
|
|
|
close(server);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|