Entirely rewritten fiche

This commit is contained in:
solusipse
2017-09-02 17:51:43 +02:00
parent c054f8dc20
commit 8ff08a0409
5 changed files with 910 additions and 634 deletions

6
.gitignore vendored
View File

@@ -1,2 +1,8 @@
# ignore binaries
/fiche
# ignore default outpit dir
code/
# ignore log files
*.log

View File

@@ -1,13 +1,9 @@
# -----------------------------------
# Fiche MAKEFILE
# https://github.com/solusipse/fiche
# solusipse.net
# -----------------------------------
CFLAGS+=-pthread -O2
# for debug add -g -O0 to line below
CFLAGS+=-pthread -O2 -Wall -Wextra -Wpedantic -Wstrict-overflow -fno-strict-aliasing -std=gnu11 -g -O0
prefix=/usr/local
all: fiche
all:
${CC} main.c fiche.c $(CFLAGS) -o fiche
install: fiche
install -m 0755 fiche $(prefix)/bin

1196
fiche.c

File diff suppressed because it is too large Load Diff

159
fiche.h
View File

@@ -5,7 +5,7 @@ Fiche - Command line pastebin for sharing terminal output.
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Repository: https://github.com/solusipse/fiche/
Live example: http://code.solusipse.net/
Live example: http://termbin.com
-------------------------------------------------------------------------------
@@ -14,15 +14,7 @@ usage: fiche [-DepbsdolBuw].
[-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist]
-D option is for daemonizing fiche
-e option is for using an extended character set for the URL
Compile with Makefile or manually with -O2 and -pthread flags.
To install use `make install` command.
Use netcat to push text - example:
$ cat fiche.c | nc localhost 9999
-------------------------------------------------------------------------------
@@ -31,91 +23,86 @@ $ cat fiche.c | nc localhost 9999
#ifndef FICHE_H
#define FICHE_H
#ifndef HAVE_INET6
#define HAVE_INET6 1
#endif
#include <stdint.h>
#include <pwd.h>
#include <time.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int UID = -1;
int GID = -1;
char *LOG;
char *BASEDIR;
char *BANLIST;
char *BANFILE;
char *WHITEFILE;
char *WHITELIST;
int DAEMON = 0;
int HTTPS = 0;
int PORT = 9999;
int IPv6 = 0;
int SLUG_SIZE = 4;
int BUFSIZE = 32768;
int QUEUE_SIZE = 500;
char DOMAIN[128] = "localhost/";
char symbols[67] = "abcdefghijklmnopqrstuvwxyz0123456789";
/**
* @brief Used as a container for fiche settings. Create before
* the initialization
*
*/
typedef struct Fiche_Settings {
/**
* @brief Domain used in output links
*/
char *domain;
unsigned int time_seed;
/**
* @brief Path to directory used for storing uploaded pastes
*/
char *output_dir_path;
struct thread_arguments
{
int connection_socket;
struct sockaddr_in client_address;
#if (HAVE_INET6)
struct sockaddr_in6 client_address6;
#endif
};
/**
* @brief Port on which fiche is waiting for connections
*/
uint16_t port;
struct client_data
{
char *ip_address;
char *hostname;
};
/**
* @brief Length of a paste's name
*/
uint8_t slug_len;
int create_socket();
int create_directory(char *slug);
int check_protocol(char *buffer);
/**
* @brief Connection buffer length
*
* @remarks Length of this buffer limits max size of uploaded files
*/
uint32_t buffer_len;
void bind_to_port(int listen_socket, struct sockaddr_in serveraddr);
#if (HAVE_INET6)
void bind_to_port6(int listen_socket, struct sockaddr_in6 serveraddr6);
#endif
void error(char *buffer);
void perform_connection(int listen_socket);
void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data);
void save_to_file(char *buffer, char *slug, struct client_data data);
void display_info(struct client_data data, char *slug, char *message);
void startup_message();
void set_basedir();
void set_domain_name();
void load_list(char *file_path, int type);
void parse_parameters(int argc, char **argv);
void save_log(char *slug, char *hostaddrp, char *h_name);
void set_uid_gid();
/**
* @brief Name of the user that runs fiche process
*/
char *user_name;
char *check_banlist(char *ip_address);
char *check_whitelist(char *ip_address);
char *get_date();
/**
* @brief Path to the log file
*/
char *log_file_path;
/**
* @brief Path to the file with banned IPs
*/
char *banlist_path;
/**
* @brief Path to the file with whitelisted IPs
*/
char *whitelist_path;
} Fiche_Settings;
/**
* @brief Initializes Fiche_Settings instance
*/
void fiche_init(Fiche_Settings *settings);
/**
* @brief Runs fiche server
*
* @return 0 if it was able to start, any other value otherwise
*/
int fiche_run(Fiche_Settings settings);
/**
* @brief array of symbols used in slug generation
* @remarks defined in fiche.c
*/
extern const char *Fiche_Symbols;
struct sockaddr_in set_address(struct sockaddr_in serveraddr);
#if (HAVE_INET6)
struct sockaddr_in6 set_address6(struct sockaddr_in6 serveraddr6);
#endif
struct client_data get_client_address(struct sockaddr_in client_address);
#if (HAVE_INET6)
struct client_data get_client_address6(struct sockaddr_in6 client_address6);
#endif
#endif

133
main.c Normal file
View File

@@ -0,0 +1,133 @@
/*
Fiche - Command line pastebin for sharing terminal output.
-------------------------------------------------------------------------------
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Repository: https://github.com/solusipse/fiche/
Live example: http://termbin.com
-------------------------------------------------------------------------------
usage: fiche [-DepbsdolBuw].
[-D] [-e] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist]
Use netcat to push text - example:
$ cat fiche.c | nc localhost 9999
-------------------------------------------------------------------------------
*/
#include "fiche.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
int main(int argc, char **argv) {
// Fiche settings instance
Fiche_Settings fs;
// Initialize settings instance to default values
fiche_init(&fs);
// Note: fiche_run is responsible for checking if these values
// were set correctly
// Note: according to getopt documentation, we don't need to
// copy strings, so we decided to go with pointer approach for these
// Parse input arguments
int c;
while ((c = getopt(argc, argv, "D6eSp:b:s:d:o:l:B:u:w:")) != -1) {
switch (c) {
// domain
case 'd':
{
fs.domain = optarg;
}
break;
// port
case 'p':
{
fs.port = atoi(optarg);
}
break;
// slug size
case 's':
{
fs.slug_len = atoi(optarg);
}
break;
// output directory path
case 'o':
{
fs.output_dir_path = optarg;
}
break;
// buffer size
case 'B':
{
fs.buffer_len = atoi(optarg);
}
break;
// user name
case 'u':
{
fs.user_name = optarg;
}
break;
// log file path
case 'l':
{
fs.log_file_path = optarg;
}
break;
// banlist file path
case 'b':
{
fs.banlist_path = optarg;
}
break;
// whitelist file path
case 'w':
{
fs.whitelist_path = optarg;
}
break;
// Display help in case of any unsupported argument
default:
{
printf("usage: fiche [-dpsoBulbw].\n");
printf(" [-d domain] [-p port] [-s slug size]\n");
printf(" [-o output directory] [-B buffer size] [-u user name]\n");
printf(" [-l log file] [-b banlist] [-w whitelist]\n");
return 0;
}
break;
}
}
fiche_run(fs);
return 0;
}