-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiche.c
770 lines (571 loc) · 18.2 KB
/
fiche.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/*
Gofiche - Command line pastebin for sharing terminal output.
-------------------------------------------------------------------------------
License: MIT (http://www.opensource.org/licenses/mit-license.php)
Repository: https://github.com/slackhead/gofiche/
Live example: http://r0t.uk 9999
-------------------------------------------------------------------------------
usage: fiche [-DepsdolBug].
[-D] [-e] [-d domain] [-p port] [-s slug size]
[-o output directory] [-B buffer size] [-u user name]
[-l log file] [-g gopher domain]
-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
-------------------------------------------------------------------------------
*/
#include "fiche.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
/******************************************************************************
* Various declarations
*/
const char *Fiche_Symbols = "abcdefghijklmnopqrstuvwxyz0123456789";
/******************************************************************************
* Inner structs
*/
struct fiche_connection {
int socket;
struct sockaddr_in address;
Fiche_Settings *settings;
};
/******************************************************************************
* Static function declarations
*/
// Settings-related
/**
* @brief Sets domain name
* @warning settings.domain has to be freed after using this function!
*/
static int set_domain_name(Fiche_Settings *settings);
/**
* @brief Changes user running this program to requested one
* @warning Application has to be run as root to use this function
*/
static int perform_user_change(const Fiche_Settings *settings);
// Server-related
/**
* @brief Starts server with settings provided in Fiche_Settings struct
*/
static int start_server(Fiche_Settings *settings);
/**
* @brief Dispatches incoming connections by spawning threads
*/
static void dispatch_connection(int socket, Fiche_Settings *settings);
/**
* @brief Handles connections
* @remarks Is being run by dispatch_connection in separate threads
* @arg args Struct fiche_connection containing connection details
*/
static void *handle_connection(void *args);
// Server-related utils
/**
* @brief Generates a slug that will be used for paste creation
* @warning output has to be freed after using!
*
* @arg output pointer to output string containing full path to directory
* @arg length default or user-requested length of a slug
* @arg extra_length additional length that was added to speed-up the
* generation process
*
* This function is used in connection with create_directory function
* It generates strings that are used to create a directory for
* user-provided data. If directory already exists, we ask this function
* to generate another slug with increased size.
*/
static void generate_slug(char **output, uint8_t length, uint8_t extra_length);
/**
* @brief Creates a directory at requested path using requested slug
* @returns 0 if succeded, 1 if failed or dir already existed
*
* @arg output_dir root directory for all pastes
* @arg slug directory name for a particular paste
*/
static int create_directory(char *output_dir);
/**
* @brief Saves data to file at requested path
*
* @arg data Buffer with data received from the user
* @arg path Path at which file containing data from the buffer will be created
*/
static int save_to_file(const Fiche_Settings *s, uint8_t *data, char *slug);
// Logging-related
/**
* @brief Displays error messages
*/
static void print_error(const char *format, ...);
/**
* @brief Displays status messages
*/
static void print_status(const char *format, ...);
/**
* @brief Displays horizontal line
*/
static void print_separator();
/**
* @brief Saves connection entry to the logfile
*/
static void log_entry(const Fiche_Settings *s, const char *ip,
const char *hostname, const char *slug);
/**
* @brief Returns string containing current date
* @warning Output has to be freed!
*/
static void get_date(char *buf);
/**
* @brief Time seed
*/
unsigned int seed;
/******************************************************************************
* Public fiche functions
*/
void fiche_init(Fiche_Settings *settings) {
// Initialize everything to default values
// or to NULL in case of pointers
struct Fiche_Settings def = {
// domain
NULL,
// output dir
"code",
// port
9999,
// slug length
6,
// https
false,
// buffer length
32768,
// user name
NULL,
// path to log file
NULL,
// path to banlist
NULL,
// path to whitelist
NULL,
// gopher
NULL
};
// Copy default settings to provided instance
*settings = def;
}
int fiche_run(Fiche_Settings settings) {
seed = time(NULL);
// Display welcome message
{
char date[64];
get_date(date);
print_status("Starting fiche on %s...", date);
}
// Try to set requested user
if ( perform_user_change(&settings) != 0) {
print_error("Was not able to change the user!");
return -1;
}
// Check if output directory is writable
// - First we try to create it
{
mkdir(
settings.output_dir_path,
S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP
);
// - Then we check if we can write there
if ( access(settings.output_dir_path, W_OK) != 0 ) {
print_error("Output directory not writable!");
return -1;
}
}
// Check if log file is writable (if set)
if ( settings.log_file_path ) {
// Create log file if it doesn't exist
FILE *f = fopen(settings.log_file_path, "a+");
fclose(f);
// Then check if it's accessible
if ( access(settings.log_file_path, W_OK) != 0 ) {
print_error("Log file not writable!");
return -1;
}
}
// Try to set domain name
if ( set_domain_name(&settings) != 0 ) {
print_error("Was not able to set domain name!");
return -1;
}
// Main loop in this method
start_server(&settings);
// Perform final cleanup
// This is allways allocated on the heap
free(settings.domain);
return 0;
}
/******************************************************************************
* Static functions below
*/
static void print_error(const char *format, ...) {
va_list args;
va_start(args, format);
printf("[Gofiche][ERROR] ");
vprintf(format, args);
printf("\n");
va_end(args);
}
static void print_status(const char *format, ...) {
va_list args;
va_start(args, format);
printf("[Gofiche][STATUS] ");
vprintf(format, args);
printf("\n");
va_end(args);
}
static void print_separator() {
printf("============================================================\n");
}
static void log_entry(const Fiche_Settings *s, const char *ip,
const char *hostname, const char *slug)
{
// Logging to file not enabled, finish here
if (!s->log_file_path) {
return;
}
FILE *f = fopen(s->log_file_path, "a");
if (!f) {
print_status("Was not able to save entry to the log!");
return;
}
char date[64];
get_date(date);
// Write entry to file
fprintf(f, "%s -- %s -- %s (%s)\n", slug, date, ip, hostname);
fclose(f);
}
static void get_date(char *buf) {
struct tm curtime;
time_t ltime;
ltime=time(<ime);
localtime_r(<ime, &curtime);
// Save data to provided buffer
if (asctime_r(&curtime, buf) == 0) {
// Couldn't get date, setting first byte of the
// buffer to zero so it won't be displayed
buf[0] = 0;
return;
}
// Remove newline char
buf[strlen(buf)-1] = 0;
}
static int set_domain_name(Fiche_Settings *settings) {
char *prefix = "";
if (settings->https) {
prefix = "https://";
} else {
prefix = "http://";
}
if (settings->domain == NULL && settings->godomain != NULL)
{
prefix = "";
settings->domain = settings->godomain;
settings->godomain = NULL;
}
const int len = strlen(settings->domain) + strlen(prefix) + 1;
char *b = malloc(len);
if (!b) {
return -1;
}
strcpy(b, prefix);
strcat(b, settings->domain);
settings->domain = b;
print_status("Domain set to: %s.", settings->domain);
return 0;
}
static int perform_user_change(const Fiche_Settings *settings) {
// User change wasn't requested, finish here
if (settings->user_name == NULL) {
return 0;
}
// Check if root, if not - finish here
if (getuid() != 0) {
print_error("Run as root if you want to change the user!");
return -1;
}
// Get user details
const struct passwd *userdata = getpwnam(settings->user_name);
const int uid = userdata->pw_uid;
const int gid = userdata->pw_gid;
if (uid == -1 || gid == -1) {
print_error("Could find requested user: %s!", settings->user_name);
return -1;
}
if (setgid(gid) != 0) {
print_error("Couldn't switch to requested user: %s!", settings->user_name);
}
if (setuid(uid) != 0) {
print_error("Couldn't switch to requested user: %s!", settings->user_name);
}
print_status("User changed to: %s.", settings->user_name);
return 0;
}
static int start_server(Fiche_Settings *settings) {
// Perform socket creation
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
print_error("Couldn't create a socket!");
return -1;
}
// Set socket settings
if ( setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 } , sizeof(int)) != 0 ) {
print_error("Couldn't prepare the socket!");
return -1;
}
// Prepare address and port handler
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(settings->port);
// Bind to port
if ( bind(s, (struct sockaddr *) &address, sizeof(address)) != 0) {
print_error("Couldn't bind to the port: %d!", settings->port);
return -1;
}
// Start listening
if ( listen(s, 128) != 0 ) {
print_error("Couldn't start listening on the socket!");
return -1;
}
print_status("Server started listening on port: %d.", settings->port);
print_separator();
// Run dispatching loop
while (1) {
dispatch_connection(s, settings);
}
// Give some time for all threads to finish
// NOTE: this code is reached only in testing environment
// There is currently no way to kill the main thread from any thread
// Something like this can be done for testing purpouses:
// int i = 0;
// while (i < 3) {
// dispatch_connection(s, settings);
// i++;
// }
sleep(5);
return 0;
}
static void dispatch_connection(int socket, Fiche_Settings *settings) {
// Create address structs for this socket
struct sockaddr_in address;
socklen_t addlen = sizeof(address);
// Accept a connection and get a new socket id
const int s = accept(socket, (struct sockaddr *) &address, &addlen);
if (s < 0 ) {
print_error("Error on accepting connection!");
return;
}
// Set timeout for accepted socket
const struct timeval timeout = { 5, 0 };
if ( setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0 ) {
print_error("Couldn't set a timeout!");
}
if ( setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0 ) {
print_error("Couldn't set a timeout!");
}
// Create an argument for the thread function
struct fiche_connection *c = malloc(sizeof(*c));
if (!c) {
print_error("Couldn't allocate memory!");
return;
}
c->socket = s;
c->address = address;
c->settings = settings;
// Spawn a new thread to handle this connection
pthread_t id;
if ( pthread_create(&id, NULL, &handle_connection, c) != 0 ) {
print_error("Couldn't spawn a thread!");
return;
}
// Detach thread if created succesfully
// TODO: consider using pthread_tryjoin_np
pthread_detach(id);
}
static void *handle_connection(void *args) {
// Cast args to it's previous type
struct fiche_connection *c = (struct fiche_connection *) args;
// Get client's IP
const char *ip = inet_ntoa(c->address.sin_addr);
// Get client's hostname
char hostname[1024];
if (getnameinfo((struct sockaddr *)&c->address, sizeof(c->address),
hostname, sizeof(hostname), NULL, 0, 0) != 0 ) {
// Couldn't resolve a hostname
strcpy(hostname, "n/a");
}
// Print status on this connection
{
char date[64];
get_date(date);
print_status("%s", date);
print_status("Incoming connection from: %s (%s).", ip, hostname);
}
// Create a buffer
uint8_t buffer[c->settings->buffer_len];
memset(buffer, 0, c->settings->buffer_len);
const int r = recv(c->socket, buffer, sizeof(buffer), MSG_WAITALL);
if (r <= 0) {
print_error("No data received from the client!");
print_separator();
// Close the socket
close(c->socket);
// Cleanup
free(c);
pthread_exit(NULL);
return 0;
}
// - Check if request was performed with a known protocol
// TODO
// - Check if on whitelist
// TODO
// - Check if on banlist
// TODO
// Generate slug and use it to create an url
char *slug;
uint8_t extra = 0;
do {
// Generate slugs until it's possible to create a directory
// with generated slug on disk
generate_slug(&slug, c->settings->slug_len, extra);
// Something went wrong in slug generation, break here
if (!slug) {
break;
}
// Increment counter for additional letters needed
++extra;
// If i was incremented more than 128 times, something
// for sure went wrong. We are closing connection and
// killing this thread in such case
if (extra > 128) {
print_error("Couldn't generate a valid slug!");
print_separator();
// Cleanup
free(c);
free(slug);
close(c->socket);
pthread_exit(NULL);
return NULL;
}
}
while(create_directory(c->settings->output_dir_path));
// Slug generation failed, we have to finish here
if (!slug) {
print_error("Couldn't generate a slug!");
print_separator();
close(c->socket);
// Cleanup
free(c);
pthread_exit(NULL);
return NULL;
}
// Save to file failed, we have to finish here
if ( save_to_file(c->settings, buffer, slug) != 0 ) {
print_error("Couldn't save a file!");
print_separator();
close(c->socket);
// Cleanup
free(c);
free(slug);
pthread_exit(NULL);
return NULL;
}
// Write a response to the user
{
// Create an url (additional byte for slash and one for new line)
size_t len = strlen(c->settings->domain) + strlen(slug) + 3;
if (c->settings->godomain != NULL)
len = strlen(c->settings->godomain) + strlen(slug) + 1 + strlen(c->settings->domain) + strlen(slug) + 4;
char url[len];
if (c->settings->godomain != NULL)
snprintf(url, len, "%s%s%s%s%s%s%s%s", c->settings->godomain, "/", slug, "\n", c->settings->domain, "/", slug, "\n");
else
snprintf(url, len, "%s%s%s%s", c->settings->domain, "/", slug, "\n");
// Send the response
write(c->socket, url, len);
}
print_status("Received %d bytes, saved to: %s.", r, slug);
print_separator();
// Log connection
// TODO: log unsuccessful and rejected connections
log_entry(c->settings, ip, hostname, slug);
// Close the connection
close(c->socket);
// Perform cleanup of values used in this thread
free(slug);
free(c);
pthread_exit(NULL);
return NULL;
}
static void generate_slug(char **output, uint8_t length, uint8_t extra_length) {
// Realloc buffer for slug when we want it to be bigger
// This happens in case when directory with this name already
// exists. To save time, we don't generate new slugs until
// we spot an available one. We add another letter instead.
if (extra_length > 0) {
free(*output);
}
// Create a buffer for slug with extra_length if any
*output = calloc(length + 1 + extra_length, sizeof(char));
if (*output == NULL) {
return;
}
// Take n-th symbol from symbol table and use it for slug generation
for (int i = 0; i < length + extra_length; i++) {
int n = rand_r(&seed) % strlen(Fiche_Symbols);
*(output[0] + sizeof(char) * i) = Fiche_Symbols[n];
}
}
static int create_directory(char *output_dir) {
// Create output directory, just in case
mkdir(output_dir, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP);
return 0;
}
static int save_to_file(const Fiche_Settings *s, uint8_t *data, char *slug) {
// Additional 2 bytes are for 2 slashes
size_t len = strlen(s->output_dir_path) + strlen(slug) + 2;
// Generate a path
char *path = malloc(len);
if (!path) {
return -1;
}
snprintf(path, len, "%s%s%s", s->output_dir_path, "/", slug);
// Attempt file saving
FILE *f = fopen(path, "w");
if (!f) {
free(path);
return -1;
}
// Null-terminate buffer if not null terminated already
data[s->buffer_len - 1] = 0;
if ( fprintf(f, "%s", data) < 0 ) {
fclose(f);
free(path);
return -1;
}
fclose(f);
free(path);
return 0;
}