/
pstd_server.pl
executable file
·771 lines (623 loc) · 20.1 KB
/
pstd_server.pl
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
771
#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;
use v5.10;
use IO::Select;
use IO::Socket::INET;
use Getopt::Std;
use POSIX 'strftime';
use Data::Dumper;
use Digest::MD5 qw(md5_hex);
use Crypt::Mode::CBC;
# Name and version
my $prgnam = $0 =~ s/^.*\///r;
my $version = '0.0.2';
# These are defaults that can be overridden with command-line switches
my $bindaddr = "127.0.0.1"; # -l
my $bindport = 8080; # -l, On Linux, authbind(1) can be used to listen on low ports without having to run as root
my $pastedir = 'pastes'; # -d
my $manpath = 'pstd.1'; # -m
my $cltscript = 'pstd.sh'; # -c, we need to know this because we distribute this as (synthetical) paste "0"
my $verbose = 0; # -v
my $myhost = ''; # If not overridden by -H, we figure it out by running hostname(1)
my $logfile = ''; # Don't log by default
my $maxbuflen = 256*1024; # -s, The largest single paste we'll accept, in bytes
my $compr = 'cat'; # -C, (default is no compression, hence cat)
my $decompr = 'cat'; # -D, (default is no decompression, hence cat)
# Paste-ID alphabet and the shortest length of IDs we'll hand out
my @idalpha = ("A".."Z", "a".."z", "0".."9");
my $minidlen = 2;
# Map client sockets to their respective read buffers (strings)
my %readbuf;
# Map client sockets to the amount of data we expect from them
my %datalen;
# Map md5 digests of recently pasted files to their respective URLs (prefixed
# with a timestamp followed by a blank. This is to avoid creating unnecessary
# files in the event that some script or person keeps pasting the same thing.
my %recent;
my $hk_interval = 30*60;
my $hk_next = time + $hk_interval;
my $recent_purge_age = 60*60*24;
my $rateinfo_purge_age; # set to 2*$ratetspan later
# Map client IP addresses to arrays containing timestamps of their latest
# $ratesmpl attempts to paste. Rate-limiting occurs once the difference
# between the first and the last element of such an array is smaller than
# $ratetspan. Not only successful pastes, but also running into the
# rate-limiter count towards it, so trying to paste when already rate-limited
# might keep the rate-limiting in place for arbitrary timespans. This is
# considered a feature.
my %rateinfo;
my $ratesmpl = 5;
my $ratetspan = 30;
# getopts
my %opts;
# map from syntax hilighting language names (e.g. "c") to command lines that
# produce HTML on stdout when presented a source file on stdin. Elements are
# added by the -y command line option
my %hilite;
my $year = '2015';
my $tmpfile = "/tmp/pstd.tmp.$$";
# Socket and Select objects
my $sck;
my $sel;
my $quit = 0;
my $inforeq;
my $cbc = Crypt::Mode::CBC->new('AES');
sub INFO_handler
{
$inforeq = 1;
}
sub HK_handler
{
$hk_next = time;
}
sub INT_handler
{
$quit = 1;
}
sub L;
sub now { return strftime('%Y-%m-%d %H:%M:%S %z', localtime); }
sub W { say STDERR "$prgnam: ".now.": ".($_[0] =~ s/[\r\n]/\$/grm); }
sub E { my $msg = $_[0]; L "ERROR: $msg"; W "ERROR: $msg"; exit 1; }
sub D { W "DBG: $_[0]" if $verbose; }
sub L
{
return if (!$logfile);
my $fhnd;
if (!open $fhnd, '>>', "$logfile") {
W "Failed to open $logfile for appending: $!";
return;
}
say $fhnd "$prgnam: ".now.": ".($_[0] =~ s/[\r\n]/\$/grm);
close $fhnd;
}
sub usage
{
my ($str, $ec) = @_;
print $str "Usage: $prgnam [-options (see below)]"
." [-d <path>] [-m <path>]\n"
." -h: Show this usage statement\n"
." -V: Print version on stdout\n"
." -v: Be more verbose\n"
." -l [addr:]port: Listen on port, optionally bind to addr\n"
." -m path: Path to manual (pstd.1)\n"
." -d path: Path to paste directory\n"
." -c path: Path to pstd.sh client-script (becomes a paste\n"
." referred to by the manpage)\n"
." -L path: Path to logfile\n"
." -H FQDN: Our hostname\n"
." -s size: Maximum paste size in KiB (this isn't exact \n"
." because it does not account for the HTTP Header\n"
." -r num: Rate limit on `num` pastes in `time` (see -R) secs\n"
." -R time: See -r. Giving 0 (to -r or -R) means no rate-limit\n"
." -C cmdl: Compression program to use. The argument should\n"
." be a program (incl. possible switches) that\n"
." compresses stdin to stdout (e.g. 'gzip -c')\n"
." -D cmdl: Decompression program to use. The argument should\n"
." be a program (incl. possible switches) that\n"
." decompresses stdin to stdout (e.g. 'gzcat')\n"
." The default is not to compress/decompress.\n"
." Once decided for a compression format, you cannot\n"
." turn around and use something else without clearing\n"
." the paste directory.\n"
."\nv$version, written by Timo Buhrmester, $year\n";
exit $ec;
}
sub test_compression
{
my $teststr = "foo bar\n baz";
my @out = `echo '$teststr' | $compr | $decompr`;
return join('', @out) eq "$teststr\n"; #echo appends a newline
}
# Generate an unused paste ID
sub gen_id
{
# We're trying 10 times to obtain an unused 2-letter ID,
# then 10 times to obtain an unused 3-letter ID, and so forth
# We also remember when we start needing to use something longer,
# and avoid searching the apparently full-ish shorter ID-space then.
foreach my $idlen ($minidlen..32) {
foreach my $attempt (1..10) {
my $id = '';
$id .= $idalpha[rand @idalpha] for 1..$idlen;
if (! -e "$pastedir/$id") {
D "Generated ID $id";
if ($idlen > $minidlen) {
$minidlen = $idlen;
}
return $id;
}
}
}
return '';
}
# The maps %recent and %rateinfo grow, but never shrink on their own.
# Therefore, we need to occasionally iterate them and purge old stuff
sub housekeep
{
my $cnt = 0;
my @drop;
my $now = time;
D "Housekeeping";
foreach my $key (keys %recent) {
my $time = $recent{$key} =~ s/ .*$//r;
if ($now - $time > $recent_purge_age) {
push @drop, $key;
$cnt++;
}
}
delete $recent{$_} foreach (@drop);
D "house-kept away $cnt elements from \%recent";
$cnt = 0;
@drop = ();
foreach my $key (keys %rateinfo) {
my $aref = $rateinfo{$key};
my $time = $aref->[@$aref - 1];
if ($now - $time > $rateinfo_purge_age) {
push @drop, $key;
$cnt++;
}
}
delete $rateinfo{$_} foreach (@drop);
D "house-kept away $cnt elements from \%rateinfo";
}
# Register event and return how many seconds the offender needs to wait
# until they are permitted to paste again
sub ratelimit_check
{
my ($who) = @_;
return 0 if (!$ratesmpl or !$ratetspan);
$rateinfo{$who} = [] if not exists $rateinfo{$who};
my $aref = $rateinfo{$who};
shift @$aref if @$aref >= $ratesmpl;
push @$aref, time;
if (@$aref >= $ratesmpl) {
my $tdiff = $aref->[@$aref - 1] - $aref->[0];
if ($tdiff < $ratetspan) { # going too fast...
return $ratetspan - (time - $aref->[@$aref - 2]);
}
}
return 0; #okay, not rate limited (yet)
}
# Read and return the man page
sub manpage
{
my $fhnd;
if (!open $fhnd, '<', $manpath) {
W "Failed to open $manpath: $!";
return "ERROR: Manpage not found\n";
}
my @lines = <$fhnd>;
my $man = join '', @lines;
close $fhnd;
return $man =~ s/MYHOST/$myhost/rg;
}
# Deal with a client (most likely a web browser) requesting a paste
# Takes socket and ID as parmeters, returns what we're going to
# pass back to the client (i.e. ideally, the actual paste)
sub process_GET
{
my ($clt, $id, $hilang, $key) = @_;
my $who = $clt->peerhost();
if (! -e "$pastedir/$id") {
W "$who: Requested nonexistant paste $id";
L "$who: Nonexistant $id";
return "ERROR: No such paste.\n";
}
my $add = '';
if ($hilang and exists $hilite{$hilang}) {
$add = "| $hilite{$hilang}";
}
my $paste;
my @data = `$decompr <'$pastedir/$id'`;
if ($key) {
my @out = `$decompr <'$pastedir/$id'`;
$paste = join '', @out;
$paste = $cbc->decrypt($paste, $key, 'deadbeefdeadfeed'); # XXX iv
if ($add ne '') {
#oh well..
my $fhnd;
if (!open $fhnd, '>', $tmpfile) {
W "$who: Failed open tempfile '$pastedir/$id'";
return "ERROR: Failed to write\n";
}
print $fhnd $paste;
close $fhnd;
@out = `cat $tmpfile $add`;
$paste = join '', @out;
}
} else {
my @out = `$decompr <'$pastedir/$id' $add`;
if (${^CHILD_ERROR_NATIVE} != 0) {
W "$who: Failed to obtain paste '$pastedir/$id'";
L "$who: Nonexistant $id";
return "ERROR: Failed to load paste\n";
}
$paste = join '', @out;
}
D "$who: Got $id";
L "$who: Got $id";
return $paste;
}
# Deal with something (most likely paste.sh or wget) submitting
# a paste. We don't support much HTTP, so this may not work
# with arbitrary clients. (We require a Content-Length header,
# and no transfer-encoding. wget --post-file http://.. is okay.
sub process_POST
{
my ($key, $clt) = @_;
my $who = $clt->peerhost();
my $whoipp = $who.":".$clt->peerport();
my $twait = ratelimit_check $who;
if ($twait) {
W "$who: Rate limited";
return "ERROR: Slow down, cowboy. Try again in $twait seconds (and don't be early)!\n";
}
my $paste = $readbuf{$whoipp} =~ s/^(.*?)\r\n\r\n//rs;
if ($paste eq '') {
W "$who: Empty paste";
return "ERROR: Empty paste\n";
}
if ($key ne '') {
$paste = $cbc->encrypt($paste, $key, 'deadbeefdeadfeed'); # XXX iv
}
my $digest = md5_hex($paste);
if (exists $recent{$digest}) {
my $url = $recent{$digest} =~ s/^[^ ]+ //r;
my $id = $url =~ s/^.*\///r;
D "$who: Re-pasted $id";
L "$who: Re-pasted $id";
return "$url\n";
}
my $id = gen_id;
if (!$id) {
W "Out of IDs (what?)";
return "ERROR: Out of IDs\n";
}
my $fhnd;
if (!open $fhnd, '>', $tmpfile) {
W "$who: Failed open tempfile '$pastedir/$id'";
return "ERROR: Failed to write\n";
}
print $fhnd $paste;
close $fhnd;
`$compr <"$tmpfile" >"$pastedir/$id"`;
if (${^CHILD_ERROR_NATIVE} != 0) {
W "$who: Failed to paste '$pastedir/$id'";
return "ERROR: Failed to write paste\n";
}
D "$who: Pasted $id";
L "$who: Pasted $id";
my $url = "http://$myhost/$id";
if ($key ne '') {
$url .= "/$key"
}
$recent{$digest} = time.' '.$url;
return "$url\n";
}
# This is called once we have a complete header (for a GET)
# or a complete request (for a POST), it dispatches to
# process_GET and process_POST
sub process_dispatch
{
my ($clt) = @_;
my $who = $clt->peerhost();
my $whoipp = $who.":".$clt->peerport();
my $resp;
D "$who: Processing '$readbuf{$whoipp}'";
if ($readbuf{$whoipp} =~ /^POST \//) {
# yes this is a hack...
if ($readbuf{$whoipp} =~ /^POST \/([a-zA-Z0-9]{16}) HTTP\//) {
$resp=process_POST($1, $clt);
} elsif ($readbuf{$whoipp} =~ /^POST \/ HTTP\//) {
$resp=process_POST('', $clt);
} else {
W "$who: Request not understood";
$resp = "ERROR: Request not understood\n";
}
} elsif ($readbuf{$whoipp} =~ /^GET \/([a-zA-Z0-9]+)(?:\/([a-zA-Z0-9]{16}))?\b(?:\?([a-z]+))?\b/) {
$resp=process_GET($clt, $1, $3, $2);
} elsif ($readbuf{$whoipp} =~ /^GET \/ /) {
$resp=manpage;
L "$who: Manpage";
} else {
W "$who: Request not understood";
L "$who: Request not understood: '$readbuf{$whoipp}'";
$resp = "ERROR: Request not understood\n";
}
return $resp;
}
# Read some more data for the given client and call process_dispatch
# on it once we have enough. Bail out if we get too much
# Return 0 to drop the client, 1 to keep going
sub handle_clt
{
my ($clt) = @_;
my $who = $clt->peerhost();
my $whoipp = $who.":".$clt->peerport();
D "$who: Handling";
my $data = '';
$clt->recv($data, 1024); # XXX can this fail?!
# I suppose empty data means EOF, but not quite sure. XXX
if ($data eq '') {
W "$who: Empty read";
respond($clt, "text/plain", "ERROR: You what?\n");
return 0;
}
# some early sanity check, this assumes the first couple bytes come in in one chunk, though.
if (!length $readbuf{$whoipp}) {
if ($data =~ /^POST \//) {
$datalen{$whoipp} = -1; #don't know yet
} elsif ($data =~ /^GET \/([a-zA-Z0-9]+(?:\/([0-9a-zA-Z]{16}))?(?:\?([a-z]+))?)? HTTP/) {
$datalen{$whoipp} = 0; #don't care
} else {
W "$who: Bad first data chunk '$data'";
respond($clt, "text/plain", "ERROR: Request not understood\n");
return 0;
}
}
$readbuf{$whoipp} .= $data;
my $buflen = length $readbuf{$whoipp};
if ($buflen > $maxbuflen) {
W "$who: Too much data ($buflen/$maxbuflen)";
respond($clt, "text/plain", "ERROR: Too much data\n");
return 0;
}
# $datalen{$who} contains how many bytes we're expecting to receive
# from $who. If it is zero, then we read until we have a complete
# HTTP-header (i.e. till the first \r\n\r\n). If it is -1, then
# this is a POST, and we haven't seen the Content-Length header yet.
if ($datalen{$whoipp} == -1) {
# POST, see if we have a header yet...
my $hdr = $readbuf{$whoipp} =~ s/\r\n\r\n.*$//rs;
if ($hdr) {
#... and extract the Content-Length; bail if none
my $match = $hdr =~ /Content-Length: ([0-9]+)/;
if ($match and !$1) {
W "$who: No Content-Length in header";
respond($clt, "text/plain", "ERROR: Need Content-Length Header\n");
return 0;
}
$datalen{$whoipp} = $1 + 4 + length $hdr;
D "$who: Expecting $datalen{$whoipp} bytes in total";
# Also complain if we happen to see an unsupported TE
$match = $hdr =~ /Transfer-Encoding: ([a-zA-Z0-9_-]+)/;
if ($match and $1) {
if ($1 ne 'Identity' and $1 ne 'None') {
W "$who: Bad TE '$1'";
respond($clt, "text/plain", "ERROR: Bad Transfer-Encoding (use Identity)\n");
return 0;
}
}
}
} elsif ($datalen{$whoipp} == 0) {
# a GET, process_dispatch once we have a complete request
if ($readbuf{$whoipp} =~ /\r\n\r\n/) {
my $resp = process_dispatch($clt);
# hack: deliver manpage as HTML (for links, mainly)
my $ctype = ($resp =~ /^<html>/ or $resp =~ /^<!DOCTYPE/) ? "text/html" : "text/plain";
respond($clt, $ctype, $resp);
return 0;
}
}
# datalen may have changed at this point (in the above conditional)
if ($datalen{$whoipp} > 0) {
if (length $readbuf{$whoipp} == $datalen{$whoipp}) {
# a POST, we got everything.
respond($clt, "text/plain", process_dispatch($clt));
return 0;
} elsif (length $readbuf{$whoipp} > $datalen{$whoipp}) {
# a POST, we got more than advertised.
W "$who: More data than advertised";
respond($clt, "text/plain", "ERROR: More data than advertised. Nice try?\n");
return 0;
}
}
return 1;
}
# respond to client with a fake 200 OK and the actual response
sub respond
{
my ($clt, $ctype, $data) = @_;
my $who = $clt->peerhost();
if (!$who) {
W "Don't know peer address, aborting";
return;
}
my $len = length $data;
my $resp = "HTTP/1.1 200 OK\r\n".
"Content-Type: $ctype; charset=UTF-8\r\n".
"Content-Length: $len\r\n".
"Connection: close\r\n\r\n$data";
if (!$clt->send($resp)) {
W "$who: send: $!";
}
}
sub dump_state
{
say STDERR "============ state dump =============";
say STDERR "\$^O = '$^O'";
say STDERR "\$0 = '$0'";
say STDERR "\$prgnam = '$prgnam'";
say STDERR "\$version = '$version'";
say STDERR "\$bindaddr = '$bindaddr'";
say STDERR "\$bindport = '$bindport'";
say STDERR "\$pastedir = '$pastedir'";
say STDERR "\$manpath = '$manpath'";
say STDERR "\$cltscript = '$cltscript'";
say STDERR "\$verbose = '$verbose'";
say STDERR "\$myhost = '$myhost'";
say STDERR "\$logfile = '$logfile'";
say STDERR "\$maxbuflen = '$maxbuflen'";
say STDERR "\$compr = '$compr'";
say STDERR "\$decompr = '$decompr'";
say STDERR "\$minidlen = '$minidlen'";
say STDERR "\$ratesmpl = '$ratesmpl'";
say STDERR "\$ratetspan = '$ratetspan'";
say STDERR "\$year = '$year'";
say STDERR "\$inforeq = '$inforeq'";
say STDERR "\$quit = '$quit'";
say STDERR "\$hk_interval = '$hk_interval'";
say STDERR "\$hk_next = '$hk_next'";
say STDERR "\$recent_purge_age = '$recent_purge_age'";
say STDERR "\$rateinfo_purge_age = '$rateinfo_purge_age;'";
say STDERR "\@idalpha: '".(join '', @idalpha)."'";
print STDERR Dumper(\%readbuf) =~ s/\$VAR1/%readbuf/r;
print STDERR Dumper(\%datalen) =~ s/\$VAR1/%datalen/r;
print STDERR Dumper(\%rateinfo) =~ s/\$VAR1/%rateinfo/r;
print STDERR Dumper(\%recent) =~ s/\$VAR1/%recent/r;
print STDERR Dumper(\%opts) =~ s/\$VAR1/%opts/r;
print STDERR Dumper($sck) =~ s/\$VAR1/\$sck/r;
print STDERR Dumper($sel) =~ s/\$VAR1/\$sel/r;
say STDERR "========= End of state dump =========";
}
# -----------------------------------------------------------------------------
# Parse command-line, overriding defaults
usage(\*STDERR, 1) if !getopts("hvVl:d:m:H:c:L:s:r:R:C:D:y:", \%opts);
if (defined $opts{V}) {
print "$version\n";
exit 0;
}
usage(\*STDOUT, 0) if defined $opts{h};
$verbose = 1 if defined $opts{v};
$manpath = $opts{m} if defined $opts{m};
$pastedir = $opts{d} if defined $opts{d};
$cltscript = $opts{c} if defined $opts{c};
$myhost = $opts{H} if defined $opts{H};
$logfile = $opts{L} if defined $opts{L};
$maxbuflen = $opts{s} if defined $opts{s};
$ratesmpl = $opts{r} if defined $opts{r};
$ratetspan = $opts{R} if defined $opts{R};
$compr = $opts{C} if defined $opts{C};
$decompr = $opts{D} if defined $opts{C};
if (defined $opts{l}) {
my $tmp = $opts{l};
if (!($tmp =~ /(?:(^.+):)?([0-9]+)$/)) {
E 'Bad argument to -l (should be "PORT" or "ADDR:PORT")';
}
$bindaddr = $1 ? $1 : "0.0.0.0";
$bindport = $2;
}
E "Could not read man page '$manpath' (Bad -m? Try -h)" if ! -r $manpath;
E "Could not read client script '$cltscript' (Bad -c? Try -h)" if ! -r $cltscript;
E "Could not access paste directory '$pastedir' (Bad -d? Try -h)" if ! -d $pastedir;
E "Invalid maximum paste size '$maxbuflen' (Bad -s? Try -h)" if !($maxbuflen =~ /^[1-9][0-9]*$/);
$maxbuflen *= 1024 if defined $opts{s}; # Is given in KiB on the command line
E "Invalid number of rate limiting samples '$ratesmpl' (Bad -r? Try -h)" if !($ratesmpl =~ /^[0-9]+$/);
E "Invalid timespan for rate-limiting '$ratetspan' (Bad -R? Try -h)" if !($ratetspan =~ /^[0-9]+$/);
$rateinfo_purge_age = 2*$ratetspan;
L "Logfile created" if $logfile and ! -e $logfile;
E "Cannot write to logfile '$logfile' (Bad -L? Try -h)" if $logfile and ! -w $logfile;
if (!$myhost) {
my @out = `hostname`;
$myhost = $out[0];
chop $myhost;
E "Failed to figure out hostname, use -H <FQDN>" if !$myhost;
W "Determined our hostname to be '$myhost' (override with -H)";
}
if (!test_compression) {
E "Chosen compression/decompression does not work! (see -h)";
}
if (defined $opts{y}) {
foreach my $hi (split ',', $opts{y}) {
my $lang = $hi =~ s/:.*$//r;
my $prog = $hi =~ s/^[^:]*://r;
my @out = `echo 'int main(void){}' | $prog`;
if (${^CHILD_ERROR_NATIVE} != 0) {
W "Ignoring $lang syntax hilighting because 'echo some-C-code | $prog' didn't work";
next;
}
$hilite{$lang} = $prog;
}
}
D "Will listen on $bindaddr:$bindport; accessible as http://$myhost/";
# Generate paste '0' because it contains the client script we advertise in the man page
`sed "s/^site=.*\$/site='$myhost'/" $cltscript | $compr >$pastedir/0`;
E "Failed to generate paste 0 (the client script)" if (${^CHILD_ERROR_NATIVE} != 0);
W "NOT doing rate-limiting! (Bad -r and/or -R?)" if (!$ratesmpl || !$ratetspan);
$| = 1;
$sck = new IO::Socket::INET (
Type => SOCK_STREAM,
Proto => 'tcp',
Listen => 64,
Reuse => 1,
Blocking => 1,
LocalAddr => $bindaddr,
LocalPort => $bindport
) or E "Could not create socket $!\n";
$SIG{'INFO'} = 'INFO_handler' if $^O =~ /^(.*bsd)|darwin$/;
$SIG{'USR1'} = 'INFO_handler';
$SIG{'USR2'} = 'HK_handler';
$SIG{'INT'} = 'INT_handler';
$sel = IO::Select->new();
$sel->add($sck);
while (!$quit)
{
D "Selecting...";
my @rdbl = $sel->can_read;
if ($inforeq) {
dump_state;
$inforeq = 0;
}
last if ($quit);
if (time >= $hk_next) {
housekeep
$hk_next = time + $hk_interval;
}
if (!@rdbl) {
D "Nothing selected";
next;
}
foreach my $s (@rdbl) {
if ($s == $sck) {
D "Listener is readable...";
my $clt = $sck->accept();
if (!$clt) {
W "Failed to accept: $!";
next;
}
my $who = $clt->peerhost();
my $whoipp = $who.":".$clt->peerport();
D "$who: Connected";
$sel->add($clt);
$readbuf{$whoipp} = '';
delete $datalen{$whoipp};
next;
}
my $who = $s->peerhost();
my $whoipp = $who.":".$s->peerport();
D "$who: Readable";
if (!handle_clt($s, $who)) {
D "$who: Dropping";
$sel->remove($s);
$s->close();
delete $readbuf{$whoipp};
delete $datalen{$whoipp};
}
}
}
$sck->close();
L "Terminating";
W "Terminating";
#2015, Timo Buhrmester