Merge pull request #15 from renaudallard/master

Multiple fixes
This commit is contained in:
solusipse
2015-08-29 16:12:12 +02:00
2 changed files with 31 additions and 19 deletions

42
fiche.c
View File

@@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
usage: fiche [-pbsdolBuw]. usage: fiche [-epbsdolBuw].
[-d domain] [-p port] [-s slug size] [-e] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name] [-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist] [-l log file] [-b banlist] [-w whitelist]
-e option is for using an extended character set for the URL
Compile with Makefile or manually with -O2 and -pthread flags. Compile with Makefile or manually with -O2 and -pthread flags.
To install use `make install` command. To install use `make install` command.
@@ -24,6 +26,7 @@ $ cat fiche.c | nc localhost 9999
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
*/ */
#include <sys/param.h>
#include "fiche.h" #include "fiche.h"
int main(int argc, char **argv) int main(int argc, char **argv)
@@ -57,7 +60,7 @@ void *thread_connection(void *args)
char buffer[BUFSIZE]; char buffer[BUFSIZE];
bzero(buffer, BUFSIZE); bzero(buffer, BUFSIZE);
int status = recv(connection_socket, buffer, BUFSIZE, MSG_WAITALL); int status = recv(connection_socket, buffer, BUFSIZE, MSG_DONTWAIT);
if (WHITELIST != NULL) if (WHITELIST != NULL)
if (check_whitelist(data.ip_address) == NULL) if (check_whitelist(data.ip_address) == NULL)
@@ -107,8 +110,8 @@ void perform_connection(int listen_socket)
pthread_t thread_id; pthread_t thread_id;
struct sockaddr_in client_address; struct sockaddr_in client_address;
int address_lenght = sizeof(client_address); int address_length = sizeof(client_address);
int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_lenght); int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_length);
struct timeval timeout; struct timeval timeout;
timeout.tv_sec = 5; timeout.tv_sec = 5;
@@ -271,13 +274,21 @@ void generate_url(char *buffer, char *slug, size_t slug_length, struct client_da
for (i = 0; i <= SLUG_SIZE - 1; i++) for (i = 0; i <= SLUG_SIZE - 1; i++)
{ {
#if defined(BSD)
int symbol_id = arc4random() % strlen(symbols);
#else
int symbol_id = rand_r(&time_seed) % strlen(symbols); int symbol_id = rand_r(&time_seed) % strlen(symbols);
#endif
slug[i] = symbols[symbol_id]; slug[i] = symbols[symbol_id];
} }
while (create_directory(slug) == -1) while (create_directory(slug) == -1)
{ {
#if defined(BSD)
int symbol_id = arc4random() % strlen(symbols);
#else
int symbol_id = rand_r(&time_seed) % strlen(symbols); int symbol_id = rand_r(&time_seed) % strlen(symbols);
#endif
slug[strlen(slug)] = symbols[symbol_id]; slug[strlen(slug)] = symbols[symbol_id];
} }
@@ -286,10 +297,9 @@ void generate_url(char *buffer, char *slug, size_t slug_length, struct client_da
int create_directory(char *slug) int create_directory(char *slug)
{ {
char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 1); char *directory = malloc(strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1);
strcpy(directory, BASEDIR); snprintf(directory, strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1, "%s%s%s", BASEDIR, "/", slug);
strcat(directory, slug);
mkdir(BASEDIR, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP); mkdir(BASEDIR, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP);
int result = mkdir(directory, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP); int result = mkdir(directory, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP);
@@ -303,10 +313,9 @@ int create_directory(char *slug)
void save_to_file(char *slug, char *buffer, struct client_data data) void save_to_file(char *slug, char *buffer, struct client_data data)
{ {
char *directory = malloc(strlen(BASEDIR) + strlen(slug) + strlen("/index.txt") + 1); char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1 );
strcpy(directory, BASEDIR);
strcat(directory, slug); snprintf(directory, strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1, "%s%s%s%s", BASEDIR , "/", slug, "/index.txt");
strcat(directory, "/index.txt");
FILE *fp; FILE *fp;
fp = fopen(directory, "w"); fp = fopen(directory, "w");
@@ -348,7 +357,7 @@ int check_protocol(char *buffer)
void set_basedir() void set_basedir()
{ {
BASEDIR = getenv("HOME"); BASEDIR = getenv("HOME");
strcat(BASEDIR, "/code/"); strncat(BASEDIR, "/code", 5 * sizeof(char));
} }
void startup_message() void startup_message()
@@ -364,9 +373,12 @@ void parse_parameters(int argc, char **argv)
{ {
int c; int c;
while ((c = getopt (argc, argv, "p:b:s:d:o:l:B:u:w:")) != -1) while ((c = getopt (argc, argv, "ep:b:s:d:o:l:B:u:w:")) != -1)
switch (c) switch (c)
{ {
case 'e':
snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ");
break;
case 'd': case 'd':
snprintf(DOMAIN, sizeof DOMAIN, "%s%s%s", "http://", optarg, "/"); snprintf(DOMAIN, sizeof DOMAIN, "%s%s%s", "http://", optarg, "/");
break; break;
@@ -387,8 +399,6 @@ void parse_parameters(int argc, char **argv)
break; break;
case 'o': case 'o':
BASEDIR = optarg; BASEDIR = optarg;
if((BASEDIR[strlen(BASEDIR) - 1]) != '/')
strcat(BASEDIR, "/");
break; break;
case 'l': case 'l':
LOG = optarg; LOG = optarg;

View File

@@ -9,11 +9,13 @@ Live example: http://code.solusipse.net/
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
usage: fiche [-pbsdolBuw]. usage: fiche [-epbsdolBuw].
[-d domain] [-p port] [-s slug size] [-e] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name] [-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist] [-l log file] [-b banlist] [-w whitelist]
-e option is for using an extended character set for the URL
Compile with Makefile or manually with -O2 and -pthread flags. Compile with Makefile or manually with -O2 and -pthread flags.
To install use `make install` command. To install use `make install` command.
@@ -54,9 +56,9 @@ int SLUG_SIZE = 4;
int BUFSIZE = 32768; int BUFSIZE = 32768;
int QUEUE_SIZE = 500; int QUEUE_SIZE = 500;
char DOMAIN[128] = "http://localhost/"; char DOMAIN[128] = "http://localhost/";
char symbols[67] = "abcdefghijklmnopqrstuvwxyz0123456789";
unsigned int time_seed; unsigned int time_seed;
const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
struct thread_arguments struct thread_arguments
{ {