48 Commits
0.9 ... legacy

Author SHA1 Message Date
solusipse
27639c2e6d Update README.md 2017-10-10 15:01:26 +02:00
solusipse
c054f8dc20 Fixed #50 2017-04-15 23:36:22 +02:00
solusipse
44b908ff2b Merge pull request #38 from nnnn20430/master
added IPv6 support #2
2016-07-09 17:04:05 +02:00
solusipse
55ef361999 Merge pull request #40 from part1zano/master
Fixed Makefile
2016-07-09 16:59:10 +02:00
part1zano
231954696e Fixed Makefile
If you don't like the default PREFIX on your system, you can always change it.
2016-04-11 21:20:41 +03:00
nnnn20430
b9ed17e937 added IPv6 support 2016-03-15 14:13:08 +01:00
solusipse
a5c137c06d enhancement #33 2016-01-23 12:36:53 +01:00
solusipse
abf8ae5117 fixing issue #31 2016-01-23 11:44:46 +01:00
solusipse
8f3e23d3de Merge pull request #25 from jungle-boogie/patch-1
better install path
2015-11-18 20:45:53 +01:00
jungle-boogie
21d8ac90e8 better install path 2015-11-17 20:51:14 -08:00
solusipse
f76eea8931 Merge pull request #23 from cgie/warnings
added checks on the return values of read/write
2015-10-31 12:47:19 +01:00
Christian Gießen
fdbfa98938 Mitigated the non-critical errors into warnings 2015-10-31 12:04:43 +01:00
Christian Gießen
5ceb31a97e added checks on the return values of read/write 2015-10-31 09:18:36 +01:00
solusipse
f6ecdab9c5 Merge pull request #22 from Hypsurus/master
Fix segfault, and error().
2015-10-13 19:19:07 +02:00
Hypsurus
bea2fc990d Fix segfault in fopen, no check for null 2015-10-13 19:40:23 +03:00
Hypsurus
a409d1cee6 error() - no need to use ERROR multiple times 2015-10-13 19:30:30 +03:00
solusipse
4c3f5908e3 Merge pull request #20 from bket/drop_priv
Drop privileges when running as root
2015-09-22 04:33:34 +02:00
Björn Ketelaars
79d3f1992e update README.md 2015-09-19 19:57:02 +02:00
Björn Ketelaars
c42b8526f0 only root can chown. However, when running as root privileges are dropped. As such, it doesn't make sense to have a function like change_owner() 2015-09-19 19:57:02 +02:00
Björn Ketelaars
053bd8d5d6 drop privileges when running as root 2015-09-19 19:57:01 +02:00
Björn Ketelaars
36f8df2011 instead of using perror() use printf(). Removes ugly and useless error codes. While here move function to *.c 2015-09-19 19:57:01 +02:00
Björn Ketelaars
880e5edf1b return after error() does not make sense 2015-09-19 19:57:00 +02:00
solusipse
a284706e78 Merge pull request #19 from bket/cleanup
Cleanup
2015-09-19 18:49:06 +02:00
Björn Ketelaars
b1de821740 combine 2 if-statements. While here; spacing, spacing, spacing 2015-09-19 18:00:06 +02:00
Björn Ketelaars
7e33e463bb remove info(), instead used printf's. Check for DAEMON at some stages 2015-09-19 17:35:16 +02:00
Björn Ketelaars
f7926b75fc display_date() is only used one time, doesn't make sense to make it a function 2015-09-19 17:35:15 +02:00
Björn Ketelaars
f9d7f3d5c8 WARNING != ERROR 2015-09-19 17:35:15 +02:00
Björn Ketelaars
9c2919f01d move all info() lines from parse_parameters() to startup_message(). Move DAEMON = 1 to getopt 2015-09-19 17:35:14 +02:00
Björn Ketelaars
19c2d8df76 main() is exptected to return a value 2015-09-19 17:35:14 +02:00
solusipse
888b578907 Merge pull request #18 from bket/daemon
daemonizing fiche
2015-09-19 16:29:54 +02:00
Björn Ketelaars
2972046f5f modify README.md accordingly. While here explain -e option 2015-09-04 14:26:59 +02:00
Björn Ketelaars
6f16b43387 add option to daemonize fiche 2015-09-04 13:54:56 +02:00
Björn Ketelaars
7a2b8b1888 wrap printf in info() and replace a couple of printf statements 2015-09-04 13:54:55 +02:00
Björn Ketelaars
89c5fc8680 stop using display_line(). Instead use a printf statement 2015-09-04 13:54:55 +02:00
Björn Ketelaars
bb59a374a1 remove unused function *return_line() 2015-09-04 13:54:54 +02:00
solusipse
a0bbd2fa28 Merge pull request #15 from renaudallard/master
Multiple fixes
2015-08-29 16:12:12 +02:00
Renaud Allard
047ecf672c Add -e option to use the extended charset I committed before.
"reduced" charset makes it easier to remember the URL by default
2015-08-29 11:34:32 +02:00
Renaud Allard
73bc067641 Use arc4random on BSD systems to avoid complaints from the compiler 2015-08-27 16:37:13 +02:00
Renaud Allard
8ccfcc8c06 Avoid using strcpy and strcat
That will also remove the warnings given by picky compilers
2015-08-27 15:55:09 +02:00
Renaud Allard
caa6f5ed6a Former patch had a small bug, this has been corrected 2015-08-27 14:59:08 +02:00
Renaud Allard
1bc2132e1d Do not append a / at the end of the output path
Solves issue #13
2015-08-27 14:48:21 +02:00
Renaud Allard
d771d7bdfd Decrease the likeliness of name collisions 2015-08-27 14:36:36 +02:00
Renaud Allard
49ea4c7ccd Use MSG_DONTWAIT to use non blocking operation. This solves issue #14 2015-08-27 11:51:25 +02:00
Renaud Allard
318987c100 Correct a typo: lenght -> length 2015-08-27 10:33:39 +02:00
solusipse
a85e04d816 Merge pull request #10 from part1zano/master
Simplified Makefile
2015-05-04 19:30:56 +02:00
Maxim Kirenenko
48c540d83b Simplified Makefile 2015-05-02 22:56:36 +03:00
solusipse
c75f4b29a1 Merge pull request #9 from part1zano/master
Makefile: more *BSD-friendly, got rid of mandatory gcc
2015-05-02 17:14:29 +02:00
Maxim V Filimonov
7ee9910cac Makefile: more *BSD-friendly, got rid of mandatory gcc 2015-05-01 23:49:11 +03:00
4 changed files with 289 additions and 336 deletions

View File

@@ -4,12 +4,15 @@
# solusipse.net # solusipse.net
# ----------------------------------- # -----------------------------------
CC=gcc CFLAGS+=-pthread -O2
CFLAGS=-pthread -O2
prefix=/usr/local prefix=/usr/local
all: fiche.c all: fiche
$(CC) -o fiche $(CFLAGS) fiche.c
install: fiche install: fiche
install -m 0755 fiche $(prefix)/bin install -m 0755 fiche $(prefix)/bin
clean:
rm -f fiche
.PHONY: clean

243
README.md
View File

@@ -1,242 +1,5 @@
fiche [![Build Status](https://travis-ci.org/solusipse/fiche.svg?branch=master)](https://travis-ci.org/solusipse/fiche) # fiche
=====
Command line pastebin for sharing terminal output. ## Warning
## Installation ## Do not use code from this branch. Please use code from [master](https://github.com/solusipse/fiche) instead.
1. Clone into repository:
```
https://github.com/solusipse/fiche.git
```
2. Build program:
```
make
```
3. Install:
```
sudo make install
```
## Client-side usage ##
Self explanatory live examples:
```
ls -la | nc localhost 9999
```
```
cat file.txt | nc solusipse.net 9999
```
```
echo just testing! | nc code.solusipse.net 9999
```
If you haven't already set up your server on localhost, try second or third command. My personal server is
providing fiche-based service all the time on this address `solusipse.net` and this port `9999`.
- To upload text you need to have netcat installed (to check if netcat is installed, simply type ```nc``` in terminal).
## Server-side usage ##
```
usage: fiche [-pbsdolBuw].
[-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name]
[-l log file] [-b banlist] [-w whitelist]
```
These are command line arguments. You don't have to provide any, but providing basic is recommended. Without them, program
will use these default settings:
```
domain = "http://localhost/";
basedir= "~/code/";
port = 9999;
slug_size = 4;
buffer_size = 8192;
```
### Arguments ###
Most important is providing **basedir** and **domain**.
-----------------
#### Basedir ####
Basedir should be **absolute** path to directory where you would like to store text files.
```
fiche -o /absolute/path/to/directory/
```
```
fiche -o /home/www/code/
```
-----------------
#### Domain ####
Domain should be provided in such format ```domain.com```.
```
fiche -d domain.com
```
```
fiche -d subdomain.domain.com
```
-----------------
#### Slug size ####
This will force fiche to create random slugs with given length, example:
```
fiche -s 6
```
```
http://domain.com/abcdef/
```
-----------------
#### User name ####
If you use fiche as service (see details below) you may want to save files as other user, to do that use `-u` option,
there's example:
```
fiche -u http
```
-----------------
#### Buffersize ####
This parameter defines max file size uploaded by user, by default it is set to `32768`.
Use `-B` parameter to change it:
```
fiche -B 2048
```
-----------------
#### Log file ###
Path to file where all logs will be stored:
```
fiche -l /home/www/fiche-log.txt
```
-----------------
#### Ban list ###
Path to file where you provided all banned IP adresses:
```
fiche -b /home/www/fiche-bans.txt
```
-----------------
#### White list ####
If whitelist mode is enabled, only addresses from list will be able to upload files. There's example:
```
fiche -w /home/www/fiche-whitelist.txt
```
-----------------
#### Whitelist and banlist syntax ####
There is no specific syntax, there files may contain not only addresses.
-----------------
#### Examples ####
Logging connections with banlist:
```
fiche -d domain.com -l /home/www/log.txt -b /home/www/bans.txt
```
-----------------
Only for personal use with whitelist
```
fiche -d domain.com -w /home/www/whitelist.txt
```
-----------------
Custom output directory, bigger slug size, reduced buffer, custom port:
```
fiche -d domain.com -o /media/disk/fiche/ -s 8 -B 2048 -p 6666
```
## Running as service ##
You can run fiche as service, there is simple systemd example:
```
[Unit]
Description=FICHE-SERVER
[Service]
ExecStart=/usr/local/bin/fiche -d code.solusipse.net -o /home/www/code/ -l /home/www/log.txt
[Install]
WantedBy=multi-user.target
```
In service mode you have to set output directory with `-o` parameter, there's example:
```
fiche -o /home/www/code/
```
## Webserver ##
To make files available for users, you need to host them somehow. Http server is easiest option. Just set root
directory to ```BASEDIR```.
There is sample configuration for nginx:
```
server {
listen 80;
server_name mysite.com www.mysite.com;
charset utf-8;
location / {
root /home/www/code/;
index index.txt index.html;
}
}
```
## License ##
Fiche is MIT licensed.

297
fiche.c Executable file → Normal file
View File

@@ -9,11 +9,15 @@ Live example: http://code.solusipse.net/
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
usage: fiche [-pbsdolBuw]. usage: fiche [-DepbsdolBuw].
[-d domain] [-p port] [-s slug size] [-D] [-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]
-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. Compile with Makefile or manually with -O2 and -pthread flags.
To install use `make install` command. To install use `make install` command.
@@ -24,6 +28,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)
@@ -31,6 +36,18 @@ int main(int argc, char **argv)
time_seed = time(0); time_seed = time(0);
parse_parameters(argc, argv); parse_parameters(argc, argv);
set_domain_name();
if (getuid() == 0)
{
if (UID == -1)
error("user not set");
if (setgid(GID) != 0)
error("Unable to drop group privileges");
if (setuid(UID) != 0)
error("Unable to drop user privileges");
}
if (BASEDIR == NULL) if (BASEDIR == NULL)
set_basedir(); set_basedir();
@@ -42,39 +59,79 @@ int main(int argc, char **argv)
listen_socket = create_socket(); listen_socket = create_socket();
setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int));
#if (HAVE_INET6)
struct sockaddr_in6 server_address6;
if (IPv6)
{
server_address6 = set_address6(server_address6);
bind_to_port6(listen_socket, server_address6);
}
else
{
#else
if (1) {
#endif
server_address = set_address(server_address); server_address = set_address(server_address);
bind_to_port(listen_socket, server_address); bind_to_port(listen_socket, server_address);
}
if (DAEMON)
{
pid_t pid;
pid = fork();
if (pid == -1)
error("Failed to fork");
if (pid == 0)
while (1) perform_connection(listen_socket); while (1) perform_connection(listen_socket);
} }
else
while (1) perform_connection(listen_socket);
return 0;
}
void *thread_connection(void *args) void *thread_connection(void *args)
{ {
int connection_socket = ((struct thread_arguments *) args ) -> connection_socket; int connection_socket = ((struct thread_arguments *) args ) -> connection_socket;
struct sockaddr_in client_address = ((struct thread_arguments *) args ) -> client_address; struct sockaddr_in client_address;
struct client_data data;
struct client_data data = get_client_address(client_address); #if (HAVE_INET6)
struct sockaddr_in6 client_address6;
if (IPv6)
{
client_address6 = ((struct thread_arguments *) args ) -> client_address6;
data = get_client_address6(client_address6);
}
else
{
#else
if (1) {
#endif
client_address = ((struct thread_arguments *) args ) -> client_address;
data = get_client_address(client_address);
}
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_WAITALL);
if (WHITELIST != NULL) if (WHITELIST != NULL && check_whitelist(data.ip_address) == NULL)
if (check_whitelist(data.ip_address) == NULL)
{ {
display_info(data, NULL, "Rejected connection from unknown user."); display_info(data, NULL, "Rejected connection from unknown user.");
save_log(NULL, data.ip_address, data.hostname); save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "You are not whitelisted!\n", 26); if (write(connection_socket, "You are not whitelisted!\n", 26) < 0)
printf("Error writing on stream socket\n");
close(connection_socket); close(connection_socket);
pthread_exit(NULL); pthread_exit(NULL);
} }
if (BANLIST != NULL) if (BANLIST != NULL && check_banlist(data.ip_address) != NULL)
if (check_banlist(data.ip_address) != NULL)
{ {
display_info(data, NULL, "Rejected connection from banned user."); display_info(data, NULL, "Rejected connection from banned user.");
save_log(NULL, data.ip_address, data.hostname); save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "You are banned!\n", 17); if (write(connection_socket, "You are banned!\n", 17) < 0)
printf("Error writing on stream socket\n");
close(connection_socket); close(connection_socket);
pthread_exit(NULL); pthread_exit(NULL);
} }
@@ -89,13 +146,15 @@ void *thread_connection(void *args)
save_log(slug, data.ip_address, data.hostname); save_log(slug, data.ip_address, data.hostname);
char response[strlen(slug) + strlen(DOMAIN) + 2]; char response[strlen(slug) + strlen(DOMAIN) + 2];
snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug); snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug);
write(connection_socket, response, strlen(response)); if (write(connection_socket, response, strlen(response)) < 0)
printf("Error writing on stream socket\n");
} }
else else
{ {
display_info(data, NULL, "Invalid connection."); display_info(data, NULL, "Invalid connection.");
save_log(NULL, data.ip_address, data.hostname); save_log(NULL, data.ip_address, data.hostname);
write(connection_socket, "Use netcat.\n", 12); if (write(connection_socket, "Use netcat.\n", 12) < 0)
printf("Error writing on stream socket\n");
} }
close(connection_socket); close(connection_socket);
@@ -107,33 +166,49 @@ 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;
int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_lenght); int connection_socket;
#if (HAVE_INET6)
struct sockaddr_in6 client_address6;
if (IPv6)
{
address_length = sizeof(client_address6);
connection_socket = accept(listen_socket, (struct sockaddr *) &client_address6, (void *) &address_length);
}
else
{
#else
if (1) {
#endif
address_length = sizeof(client_address);
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;
timeout.tv_usec = 0; timeout.tv_usec = 0;
if (setsockopt (connection_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) if (setsockopt (connection_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
error("ERROR while setting setsockopt timeout"); error("while setting setsockopt timeout");
if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
error("ERROR while setting setsockopt timeout"); error("while setting setsockopt timeout");
struct thread_arguments arguments; struct thread_arguments arguments;
arguments.connection_socket = connection_socket; arguments.connection_socket = connection_socket;
#if (HAVE_INET6)
if (IPv6)
arguments.client_address6 = client_address6;
else
#endif
arguments.client_address = client_address; arguments.client_address = client_address;
if (pthread_create(&thread_id, NULL, &thread_connection, &arguments) != 0) if (pthread_create(&thread_id, NULL, &thread_connection, &arguments) != 0)
error("ERROR on thread creation"); error("on thread creation");
else else
pthread_detach(thread_id); pthread_detach(thread_id);
} }
void display_date()
{
printf("%s\n", get_date());
}
char *get_date() char *get_date()
{ {
time_t rawtime; time_t rawtime;
@@ -157,7 +232,7 @@ struct client_data get_client_address(struct sockaddr_in client_address)
hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET); hostp = gethostbyaddr((const char *)&client_address.sin_addr.s_addr, sizeof(client_address.sin_addr.s_addr), AF_INET);
if (hostp == NULL) if (hostp == NULL)
{ {
printf("ERROR: Couldn't obtain client's hostname\n"); printf("WARNING: Couldn't obtain client's hostname\n");
data.hostname = "n/a"; data.hostname = "n/a";
} }
else else
@@ -166,7 +241,7 @@ struct client_data get_client_address(struct sockaddr_in client_address)
hostaddrp = inet_ntoa(client_address.sin_addr); hostaddrp = inet_ntoa(client_address.sin_addr);
if (hostaddrp == NULL) if (hostaddrp == NULL)
{ {
printf("ERROR: Couldn't obtain client's address\n"); printf("WARNING: Couldn't obtain client's address\n");
data.ip_address = "n/a"; data.ip_address = "n/a";
} }
else else
@@ -175,6 +250,36 @@ struct client_data get_client_address(struct sockaddr_in client_address)
return data; return data;
} }
#if (HAVE_INET6)
struct client_data get_client_address6(struct sockaddr_in6 client_address6)
{
struct hostent *hostp;
struct client_data data;
static char hostaddrp[INET6_ADDRSTRLEN];
hostp = gethostbyaddr((const char *)&client_address6.sin6_addr, sizeof(client_address6.sin6_addr), AF_INET6);
if (hostp == NULL)
{
printf("WARNING: Couldn't obtain client's hostname\n");
data.hostname = "n/a";
}
else
data.hostname = hostp->h_name;
inet_ntop(AF_INET6, &(client_address6.sin6_addr), hostaddrp,
INET6_ADDRSTRLEN);
if (hostaddrp == NULL)
{
printf("WARNING: Couldn't obtain client's address\n");
data.ip_address = "n/a";
}
else
data.ip_address = hostaddrp;
return data;
}
#endif
void save_log(char *slug, char *hostaddrp, char *h_name) void save_log(char *slug, char *hostaddrp, char *h_name)
{ {
if (LOG != NULL) if (LOG != NULL)
@@ -195,12 +300,17 @@ void save_log(char *slug, char *hostaddrp, char *h_name)
void display_info(struct client_data data, char *slug, char *message) void display_info(struct client_data data, char *slug, char *message)
{ {
if (DAEMON)
return;
if (slug == NULL) if (slug == NULL)
printf("%s\n", message); printf("%s\n", message);
else printf("Saved to: %s\n", slug); else
display_date(); printf("Saved to: %s\n", slug);
printf("%s\n", get_date());
printf("Client: %s (%s)\n", data.ip_address, data.hostname); printf("Client: %s (%s)\n", data.ip_address, data.hostname);
display_line(); printf("====================================\n");
} }
char *check_banlist(char *ip_address) char *check_banlist(char *ip_address)
@@ -217,13 +327,18 @@ char *check_whitelist(char *ip_address)
void load_list(char *file_path, int type) void load_list(char *file_path, int type)
{ {
FILE *fp = fopen(file_path, "r"); FILE *fp;
if (( fp = fopen(file_path, "r")) == NULL )
error("cannot load list");
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
long fsize = ftell(fp); long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET); fseek(fp, 0, SEEK_SET);
char *buffer = malloc(fsize + 1); char *buffer = malloc(fsize + 1);
fread(buffer, fsize, 1, fp); if (fread(buffer, fsize, 1, fp) != fsize)
error("reading list failed");
fclose(fp); fclose(fp);
buffer[fsize] = 0; buffer[fsize] = 0;
@@ -238,13 +353,18 @@ void load_list(char *file_path, int type)
int create_socket() int create_socket()
{ {
int lsocket = socket(AF_INET, SOCK_STREAM, 0); int lsocket;
#if (HAVE_INET6)
if (IPv6)
lsocket = socket(AF_INET6, SOCK_STREAM, 0);
else
#endif
lsocket = socket(AF_INET, SOCK_STREAM, 0);
if (lsocket < 0) if (lsocket < 0)
{ error("Couldn't open socket");
error("ERROR: Couldn't open socket");
return 0; return lsocket;
}
else return lsocket;
} }
struct sockaddr_in set_address(struct sockaddr_in server_address) struct sockaddr_in set_address(struct sockaddr_in server_address)
@@ -256,14 +376,35 @@ struct sockaddr_in set_address(struct sockaddr_in server_address)
return server_address; return server_address;
} }
#if (HAVE_INET6)
struct sockaddr_in6 set_address6(struct sockaddr_in6 server_address6)
{
bzero((char *) &server_address6, sizeof(server_address6));
server_address6.sin6_family = AF_INET6;
server_address6.sin6_addr = in6addr_any;
server_address6.sin6_port = htons((unsigned short)PORT);
return server_address6;
}
#endif
void bind_to_port(int listen_socket, struct sockaddr_in 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) if (bind(listen_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
error("ERROR while binding to port"); error("while binding to port");
if (listen(listen_socket, QUEUE_SIZE) < 0) if (listen(listen_socket, QUEUE_SIZE) < 0)
error("ERROR while starting listening"); error("while starting listening");
} }
#if (HAVE_INET6)
void bind_to_port6(int listen_socket, struct sockaddr_in6 server_address6)
{
if (bind(listen_socket, (struct sockaddr *) &server_address6, sizeof(server_address6)) < 0)
error("while binding to port");
if (listen(listen_socket, QUEUE_SIZE) < 0)
error("while starting listening");
}
#endif
void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data) void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data)
{ {
int i; int i;
@@ -271,13 +412,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,16 +435,13 @@ 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);
change_owner(directory);
free(directory); free(directory);
return result; return result;
@@ -303,28 +449,20 @@ 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");
fprintf(fp, "%s", buffer); fprintf(fp, "%s", buffer);
fclose(fp); fclose(fp);
change_owner(directory);
display_info(data, directory, ""); display_info(data, directory, "");
free(directory); free(directory);
} }
void change_owner(char *directory)
{
if ((UID != -1)&&(GID != -1))
chown(directory, UID, GID);
}
void set_uid_gid(char *username) void set_uid_gid(char *username)
{ {
struct passwd *userdata = getpwnam(username); struct passwd *userdata = getpwnam(username);
@@ -348,34 +486,67 @@ 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()
{ {
display_line(); if (DAEMON)
return;
printf("====================================\n");
printf("Domain name: %s\n", DOMAIN); printf("Domain name: %s\n", DOMAIN);
printf("Saving files to: %s\n", BASEDIR); printf("Saving files to: %s\n", BASEDIR);
printf("Fiche started listening on port %d.\n", PORT); printf("Fiche started listening on port %d.\n", PORT);
display_line(); printf("Buffer size set to: %d.\n", BUFSIZE);
printf("Slug size set to: %d.\n", SLUG_SIZE);
printf("Log file: %s\n", LOG);
printf("====================================\n");
}
void error(char *buffer)
{
printf("Error: %s\n", buffer);
exit(1);
}
void set_domain_name() {
char b[128];
memcpy(b, DOMAIN, sizeof DOMAIN);
if (HTTPS)
snprintf(DOMAIN, sizeof DOMAIN, "%s%s", "https://", b);
else
snprintf(DOMAIN, sizeof DOMAIN, "%s%s", "http://", b);
} }
void parse_parameters(int argc, char **argv) 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, "D6eSp:b:s:d:o:l:B:u:w:")) != -1)
switch (c) switch (c)
{ {
case 'D':
DAEMON = 1;
break;
case '6':
IPv6 = 1;
break;
case 'e':
snprintf(symbols, sizeof symbols, "%s", "abcdefghijklmnopqrstuvwxyz0123456789-+_=.ABCDEFGHIJKLMNOPQRSTUVWXYZ");
break;
case 'S':
HTTPS = 1;
break;
case 'd': case 'd':
snprintf(DOMAIN, sizeof DOMAIN, "%s%s%s", "http://", optarg, "/"); snprintf(DOMAIN, sizeof DOMAIN, "%s%s", optarg, "/");
break; break;
case 'p': case 'p':
PORT = atoi(optarg); PORT = atoi(optarg);
break; break;
case 'B': case 'B':
BUFSIZE = atoi(optarg); BUFSIZE = atoi(optarg);
printf("Buffer size set to: %d.\n", BUFSIZE);
break; break;
case 'b': case 'b':
BANFILE = optarg; BANFILE = optarg;
@@ -383,16 +554,12 @@ void parse_parameters(int argc, char **argv)
break; break;
case 's': case 's':
SLUG_SIZE = atoi(optarg); SLUG_SIZE = atoi(optarg);
printf("Slug size set to: %d.\n", SLUG_SIZE);
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;
printf("Log file: %s\n", LOG);
break; break;
case 'u': case 'u':
set_uid_gid(optarg); set_uid_gid(optarg);
@@ -402,7 +569,7 @@ void parse_parameters(int argc, char **argv)
load_list(WHITEFILE, 1); load_list(WHITEFILE, 1);
break; break;
default: default:
printf("usage: fiche [-pbsdolBuw].\n"); printf("usage: fiche [-D6epbsdSolBuw].\n");
printf(" [-d domain] [-p port] [-s slug_size]\n"); printf(" [-d domain] [-p port] [-s slug_size]\n");
printf(" [-o output directory] [-B buffer_size] [-u user name]\n"); printf(" [-o output directory] [-B buffer_size] [-u user name]\n");
printf(" [-l log file] [-b banlist] [-w whitelist]\n"); printf(" [-l log file] [-b banlist] [-w whitelist]\n");

38
fiche.h
View File

@@ -9,11 +9,15 @@ Live example: http://code.solusipse.net/
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
usage: fiche [-pbsdolBuw]. usage: fiche [-DepbsdolBuw].
[-d domain] [-p port] [-s slug size] [-D] [-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]
-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. Compile with Makefile or manually with -O2 and -pthread flags.
To install use `make install` command. To install use `make install` command.
@@ -27,6 +31,10 @@ $ cat fiche.c | nc localhost 9999
#ifndef FICHE_H #ifndef FICHE_H
#define FICHE_H #define FICHE_H
#ifndef HAVE_INET6
#define HAVE_INET6 1
#endif
#include <pwd.h> #include <pwd.h>
#include <time.h> #include <time.h>
#include <netdb.h> #include <netdb.h>
@@ -49,19 +57,25 @@ char *BANLIST;
char *BANFILE; char *BANFILE;
char *WHITEFILE; char *WHITEFILE;
char *WHITELIST; char *WHITELIST;
int DAEMON = 0;
int HTTPS = 0;
int PORT = 9999; int PORT = 9999;
int IPv6 = 0;
int SLUG_SIZE = 4; 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] = "localhost/";
char symbols[67] = "abcdefghijklmnopqrstuvwxyz0123456789";
unsigned int time_seed; unsigned int time_seed;
const char *symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
struct thread_arguments struct thread_arguments
{ {
int connection_socket; int connection_socket;
struct sockaddr_in client_address; struct sockaddr_in client_address;
#if (HAVE_INET6)
struct sockaddr_in6 client_address6;
#endif
}; };
struct client_data struct client_data
@@ -75,27 +89,33 @@ int create_directory(char *slug);
int check_protocol(char *buffer); int check_protocol(char *buffer);
void bind_to_port(int listen_socket, struct sockaddr_in serveraddr); void bind_to_port(int listen_socket, struct sockaddr_in serveraddr);
void display_line(){printf("====================================\n");} #if (HAVE_INET6)
void error(char *error_code){perror(error_code); exit(1);} void bind_to_port6(int listen_socket, struct sockaddr_in6 serveraddr6);
void display_date(); #endif
void error(char *buffer);
void perform_connection(int listen_socket); void perform_connection(int listen_socket);
void generate_url(char *buffer, char *slug, size_t slug_length, struct client_data data); 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 save_to_file(char *buffer, char *slug, struct client_data data);
void display_info(struct client_data data, char *slug, char *message); void display_info(struct client_data data, char *slug, char *message);
void startup_message(); void startup_message();
void set_basedir(); void set_basedir();
void set_domain_name();
void load_list(char *file_path, int type); void load_list(char *file_path, int type);
void parse_parameters(int argc, char **argv); void parse_parameters(int argc, char **argv);
void save_log(char *slug, char *hostaddrp, char *h_name); void save_log(char *slug, char *hostaddrp, char *h_name);
void change_owner(char *directory);
void set_uid_gid(); void set_uid_gid();
char *return_line(){return("\n====================================");}
char *check_banlist(char *ip_address); char *check_banlist(char *ip_address);
char *check_whitelist(char *ip_address); char *check_whitelist(char *ip_address);
char *get_date(); char *get_date();
struct sockaddr_in set_address(struct sockaddr_in serveraddr); 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); 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 #endif