Files
fiche/fiche.c

423 lines
12 KiB
C
Raw Normal View History

2013-09-06 04:01:55 +02:00
/*
2013-09-06 04:42:52 +02:00
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/
-------------------------------------------------------------------------------
usage: fiche [-epbsdolBuw].
[-e] [-d domain] [-p port] [-s slug size]
2013-09-13 23:51:58 +02:00
[-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist]
2013-09-06 04:42:52 +02:00
-e option is for using an extended character set for the URL
2013-09-06 04:42:52 +02:00
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
-------------------------------------------------------------------------------
2013-09-06 04:01:55 +02:00
*/
#include <sys/param.h>
#include "config.h"
2013-09-06 04:01:55 +02:00
#include "fiche.h"
int main(int argc, char **argv)
{
2013-09-06 05:03:11 +02:00
time_seed = time(0);
2013-09-06 04:01:55 +02:00
parse_parameters(argc, argv);
if (BASEDIR == NULL)
set_basedir();
2013-09-06 04:01:55 +02:00
startup_message();
2013-11-01 11:03:17 +01:00
int listen_socket, optval = 1;
2013-09-06 04:01:55 +02:00
struct sockaddr_in server_address;
listen_socket = create_socket();
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));
server_address = set_address(server_address);
bind_to_port(listen_socket, server_address);
while (1) perform_connection(listen_socket);
}
void *thread_connection(void *args)
{
2013-09-09 03:47:25 +02:00
int connection_socket = ((struct thread_arguments *) args ) -> connection_socket;
struct sockaddr_in client_address = ((struct thread_arguments *) args ) -> client_address;
2013-09-10 10:30:49 +02:00
struct client_data data = get_client_address(client_address);
2013-09-06 04:01:55 +02:00
char buffer[BUFSIZE];
bzero(buffer, BUFSIZE);
int status = recv(connection_socket, buffer, BUFSIZE, MSG_DONTWAIT);
2013-09-06 04:01:55 +02:00
2013-09-14 00:17:26 +02:00
if (WHITELIST != NULL)
if (check_whitelist(data.ip_address) == NULL)
{
2013-09-26 13:06:52 +02:00
display_info(data, NULL, "Rejected connection from unknown user.");
2013-09-14 00:17:26 +02:00
save_log(NULL, data.ip_address, data.hostname);
2013-09-26 12:36:29 +02:00
write(connection_socket, "You are not whitelisted!\n", 26);
2013-09-14 00:17:26 +02:00
close(connection_socket);
pthread_exit(NULL);
}
2013-09-26 12:36:29 +02:00
if (BANLIST != NULL)
2013-09-10 10:30:49 +02:00
if (check_banlist(data.ip_address) != NULL)
{
2013-09-26 13:06:52 +02:00
display_info(data, NULL, "Rejected connection from banned user.");
2013-09-10 10:30:49 +02:00
save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "You are banned!\n", 17);
close(connection_socket);
pthread_exit(NULL);
}
2013-09-26 12:36:29 +02:00
if (check_protocol(buffer) == 1)
2013-09-26 13:06:52 +02:00
status = -1;
2013-09-26 12:36:29 +02:00
2013-09-06 04:01:55 +02:00
if (status != -1)
{
2013-09-18 01:02:00 +02:00
char slug[SLUG_SIZE+8];
2013-09-26 13:06:52 +02:00
generate_url(buffer, slug, SLUG_SIZE+8, data);
2013-09-10 10:30:49 +02:00
save_log(slug, data.ip_address, data.hostname);
2013-09-06 04:01:55 +02:00
char response[strlen(slug) + strlen(DOMAIN) + 2];
2013-09-10 10:30:49 +02:00
snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug);
2013-09-09 03:47:25 +02:00
write(connection_socket, response, strlen(response));
2013-09-06 04:01:55 +02:00
}
else
{
2013-09-26 13:06:52 +02:00
display_info(data, NULL, "Invalid connection.");
2013-09-10 10:30:49 +02:00
save_log(NULL, data.ip_address, data.hostname);
2013-09-26 13:06:52 +02:00
write(connection_socket, "Use netcat.\n", 12);
2013-09-06 04:01:55 +02:00
}
2013-09-10 10:30:49 +02:00
2013-09-09 03:47:25 +02:00
close(connection_socket);
2013-09-06 04:01:55 +02:00
pthread_exit(NULL);
}
void perform_connection(int listen_socket)
{
pthread_t thread_id;
struct sockaddr_in client_address;
2015-08-27 10:33:39 +02:00
int address_length = sizeof(client_address);
int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_length);
2013-09-06 04:01:55 +02:00
2013-09-09 03:47:25 +02:00
struct timeval timeout;
2013-09-17 01:28:36 +02:00
timeout.tv_sec = 5;
2013-09-06 04:01:55 +02:00
timeout.tv_usec = 0;
if (setsockopt (connection_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
2013-09-16 10:53:32 +02:00
error("ERROR while setting setsockopt timeout");
2013-09-06 04:01:55 +02:00
if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
2013-09-16 10:53:32 +02:00
error("ERROR while setting setsockopt timeout");
2013-09-06 04:01:55 +02:00
2013-09-09 03:47:25 +02:00
struct thread_arguments arguments;
arguments.connection_socket = connection_socket;
arguments.client_address = client_address;
2013-09-06 04:01:55 +02:00
2013-09-09 03:47:25 +02:00
if (pthread_create(&thread_id, NULL, &thread_connection, &arguments) != 0)
2013-09-16 10:53:32 +02:00
error("ERROR on thread creation");
2013-09-06 04:01:55 +02:00
else
pthread_detach(thread_id);
}
void display_date()
2013-09-10 10:30:49 +02:00
{
2014-09-09 03:26:21 +02:00
printf("%s\n", get_date());
2013-09-10 10:30:49 +02:00
}
char *get_date()
2013-09-06 04:01:55 +02:00
{
time_t rawtime;
struct tm *timeinfo;
2014-09-09 03:26:21 +02:00
char *timechar;
2013-09-06 04:01:55 +02:00
time(&rawtime);
timeinfo = localtime(&rawtime);
2014-09-09 03:26:21 +02:00
timechar = asctime(timeinfo);
timechar[strlen(timechar)-1] = 0;
2013-09-10 10:30:49 +02:00
2014-09-09 03:26:21 +02:00
return timechar;
2013-09-06 04:01:55 +02:00
}
2013-09-10 10:30:49 +02:00
struct client_data get_client_address(struct sockaddr_in client_address)
2013-09-06 04:01:55 +02:00
{
struct hostent *hostp;
2013-09-10 10:30:49 +02:00
struct client_data data;
2013-09-06 04:01:55 +02:00
char *hostaddrp;
hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET);
2013-09-16 10:53:32 +02:00
if (hostp == NULL)
{
printf("ERROR: Couldn't obtain client's hostname\n");
2014-09-09 03:26:21 +02:00
data.hostname = "n/a";
}
else
data.hostname = hostp->h_name;
2013-09-06 04:01:55 +02:00
hostaddrp = inet_ntoa(client_address.sin_addr);
2013-09-16 10:53:32 +02:00
if (hostaddrp == NULL)
{
printf("ERROR: Couldn't obtain client's address\n");
2014-09-09 03:26:21 +02:00
data.ip_address = "n/a";
}
else
data.ip_address = hostaddrp;
2013-09-06 04:01:55 +02:00
2013-09-10 10:30:49 +02:00
return data;
2013-09-09 21:02:33 +02:00
}
void save_log(char *slug, char *hostaddrp, char *h_name)
{
2013-09-10 10:30:49 +02:00
if (LOG != NULL)
{
char contents[256];
2013-09-09 21:02:33 +02:00
2013-09-10 10:30:49 +02:00
if (slug != NULL)
2014-09-09 03:26:21 +02:00
snprintf(contents, sizeof contents, "%s -- %s -- %s (%s)\n", slug, get_date(), hostaddrp, h_name);
2013-09-10 10:30:49 +02:00
else
2014-09-09 03:26:21 +02:00
snprintf(contents, sizeof contents, "%s -- %s -- %s (%s)\n", "rej", get_date(), hostaddrp, h_name);
2013-09-09 21:02:33 +02:00
2013-09-10 10:30:49 +02:00
FILE *fp;
fp = fopen(LOG, "a");
fprintf(fp, "%s", contents);
fclose(fp);
}
}
2013-09-26 13:06:52 +02:00
void display_info(struct client_data data, char *slug, char *message)
{
if (slug == NULL)
printf("%s\n", message);
else printf("Saved to: %s\n", slug);
display_date();
printf("Client: %s (%s)\n", data.ip_address, data.hostname);
display_line();
}
2013-09-10 10:30:49 +02:00
char *check_banlist(char *ip_address)
{
2013-09-14 00:17:26 +02:00
load_list(BANFILE, 0);
2013-09-10 10:30:49 +02:00
return strstr(BANLIST, ip_address);
}
2013-09-14 00:17:26 +02:00
char *check_whitelist(char *ip_address)
{
load_list(WHITEFILE, 1);
return strstr(WHITELIST, ip_address);
}
void load_list(char *file_path, int type)
2013-09-10 10:30:49 +02:00
{
FILE *fp = fopen(file_path, "r");
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buffer = malloc(fsize + 1);
fread(buffer, fsize, 1, fp);
2013-09-09 21:02:33 +02:00
fclose(fp);
2013-09-10 10:30:49 +02:00
buffer[fsize] = 0;
2013-09-14 00:17:26 +02:00
if (type == 0)
BANLIST = buffer;
else
WHITELIST = buffer;
2013-09-10 10:30:49 +02:00
free(buffer);
2013-09-06 04:01:55 +02:00
}
int create_socket()
{
int lsocket = socket(AF_INET, SOCK_STREAM, 0);
if (lsocket < 0)
2013-10-30 00:39:51 +01:00
{
2013-09-16 10:53:32 +02:00
error("ERROR: Couldn't open socket");
2013-10-30 00:39:51 +01:00
return 0;
}
2013-09-06 04:01:55 +02:00
else return lsocket;
}
struct sockaddr_in set_address(struct sockaddr_in server_address)
{
bzero((char *) &server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons((unsigned short)PORT);
return server_address;
}
void bind_to_port(int listen_socket, struct sockaddr_in server_address)
{
if (bind(listen_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
2013-09-16 10:53:32 +02:00
error("ERROR while binding to port");
2013-09-06 04:01:55 +02:00
if (listen(listen_socket, QUEUE_SIZE) < 0)
2013-09-16 10:53:32 +02:00
error("ERROR while starting listening");
2013-09-06 04:01:55 +02:00
}
2013-09-26 13:06:52 +02:00
void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data)
2013-09-06 04:01:55 +02:00
{
2013-09-06 05:03:11 +02:00
int i;
memset(slug, '\0', slug_length);
2013-09-06 04:01:55 +02:00
for (i = 0; i <= SLUG_SIZE - 1; i++)
{
#if defined(HAVE_ARC4RANDOM)
int symbol_id = arc4random() % strlen(symbols);
#else
2013-09-06 04:01:55 +02:00
int symbol_id = rand_r(&time_seed) % strlen(symbols);
#endif
2013-09-06 04:01:55 +02:00
slug[i] = symbols[symbol_id];
}
while (create_directory(slug) == -1)
{
#if defined(HAVE_ARC4RANDOM)
int symbol_id = arc4random() % strlen(symbols);
#else
2013-09-06 04:42:52 +02:00
int symbol_id = rand_r(&time_seed) % strlen(symbols);
#endif
2013-09-06 04:01:55 +02:00
slug[strlen(slug)] = symbols[symbol_id];
}
2013-09-26 13:06:52 +02:00
save_to_file(slug, buffer, data);
2013-09-06 04:01:55 +02:00
}
int create_directory(char *slug)
{
char *directory = malloc(strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1);
2013-09-06 04:01:55 +02:00
snprintf(directory, strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1, "%s%s%s", BASEDIR, "/", slug);
2013-09-06 04:01:55 +02:00
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);
2013-09-06 04:01:55 +02:00
2013-09-13 23:51:58 +02:00
change_owner(directory);
2013-09-06 04:01:55 +02:00
free(directory);
return result;
}
2013-09-26 13:06:52 +02:00
void save_to_file(char *slug, char *buffer, struct client_data data)
2013-09-06 04:01:55 +02:00
{
char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1 );
snprintf(directory, strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1, "%s%s%s%s", BASEDIR , "/", slug, "/index.txt");
2013-09-06 04:01:55 +02:00
FILE *fp;
fp = fopen(directory, "w");
fprintf(fp, "%s", buffer);
fclose(fp);
2013-09-13 23:51:58 +02:00
change_owner(directory);
2013-09-26 13:06:52 +02:00
display_info(data, directory, "");
2013-09-13 23:51:58 +02:00
2013-09-06 04:01:55 +02:00
free(directory);
}
2013-09-13 23:51:58 +02:00
void change_owner(char *directory)
{
if ((UID != -1)&&(GID != -1))
chown(directory, UID, GID);
}
void set_uid_gid(char *username)
{
struct passwd *userdata = getpwnam(username);
if (userdata == NULL)
2013-09-16 10:53:32 +02:00
error("Provided user doesn't exist");
2013-09-13 23:51:58 +02:00
UID = userdata->pw_uid;
GID = userdata->pw_gid;
}
2013-09-26 12:36:29 +02:00
int check_protocol(char *buffer)
{
2013-09-26 13:06:52 +02:00
if (strlen(buffer) < 3)
2013-09-26 12:36:29 +02:00
return 1;
if ((strncmp(buffer, "GET", 3) == 0)||(strncmp(buffer, "POST", 4) == 0))
if (strstr(buffer, "HTTP/1."))
return 1;
return 0;
}
2013-09-06 04:01:55 +02:00
void set_basedir()
{
BASEDIR = getenv("HOME");
strncat(BASEDIR, "/code", 5 * sizeof(char));
2013-09-06 04:01:55 +02:00
}
void startup_message()
{
2013-09-10 10:30:49 +02:00
display_line();
2013-09-06 04:01:55 +02:00
printf("Domain name: %s\n", DOMAIN);
printf("Saving files to: %s\n", BASEDIR);
2013-09-09 21:02:33 +02:00
printf("Fiche started listening on port %d.\n", PORT);
2013-09-10 10:30:49 +02:00
display_line();
2013-09-06 04:01:55 +02:00
}
void parse_parameters(int argc, char **argv)
{
int c;
while ((c = getopt (argc, argv, "ep:b:s:d:o:l:B:u:w:")) != -1)
2013-09-06 04:01:55 +02:00
switch (c)
{
case 'e':
snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ");
break;
2013-09-06 04:01:55 +02:00
case 'd':
snprintf(DOMAIN, sizeof DOMAIN, "%s%s%s", "http://", optarg, "/");
break;
case 'p':
PORT = atoi(optarg);
break;
2013-09-10 10:30:49 +02:00
case 'B':
2013-09-06 04:01:55 +02:00
BUFSIZE = atoi(optarg);
printf("Buffer size set to: %d.\n", BUFSIZE);
break;
2013-09-10 10:30:49 +02:00
case 'b':
BANFILE = optarg;
2013-09-14 00:17:26 +02:00
load_list(BANFILE, 0);
2013-09-10 10:30:49 +02:00
break;
2013-09-06 04:01:55 +02:00
case 's':
SLUG_SIZE = atoi(optarg);
printf("Slug size set to: %d.\n", SLUG_SIZE);
break;
case 'o':
BASEDIR = optarg;
break;
2013-09-09 21:02:33 +02:00
case 'l':
LOG = optarg;
printf("Log file: %s\n", LOG);
break;
2013-09-13 23:51:58 +02:00
case 'u':
set_uid_gid(optarg);
break;
case 'w':
2013-09-14 00:17:26 +02:00
WHITEFILE = optarg;
load_list(WHITEFILE, 1);
2013-09-13 23:51:58 +02:00
break;
2013-09-06 04:01:55 +02:00
default:
2013-09-14 00:29:00 +02:00
printf("usage: fiche [-pbsdolBuw].\n");
2013-09-09 21:02:33 +02:00
printf(" [-d domain] [-p port] [-s slug_size]\n");
2013-09-13 23:51:58 +02:00
printf(" [-o output directory] [-B buffer_size] [-u user name]\n");
printf(" [-l log file] [-b banlist] [-w whitelist]\n");
2013-09-06 04:01:55 +02:00
exit(1);
}
2013-09-17 20:46:02 +04:00
}