-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaste.pl~
executable file
·187 lines (167 loc) · 5.49 KB
/
spaste.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
#!/usr/bin/perl
#
# __ _ _ __ ___ __ ____ ____
# / ( \/ )/ _\ / __)/ _\/ ___(_ _)
# ( O ) (/ ( (_ / \___ \ )(
# \__(_/\_\_/\_/\___\_/\_(____/(__)
#
# By oxagast, thanks to termbin.org creators for the idea.
# I would suggest creating an ssl user and pastebot user for this for security reasons.
# Set the permissions on your valid cert.pm and privkey.pem and on the directory on the
# webserver.
#
# Babe! Let me lick your butthole!
#
# useage: ./spaste.pl --conf spaste.conf
use strict;
use warnings;
use IO::Handle;
use Fcntl ("F_GETFL", "F_SETFL", "O_NONBLOCK");
use Socket;
use IO::Socket::SSL;
use threads;
use Config::Tiny;
use Getopt::Long qw (GetOptions);
$SIG{TERM} = $SIG{INT} = sub { die "Caught a sigterm $!" };
STDOUT->autoflush();
STDERR->autoflush();
if ($#ARGV + 1 ne 2) {
print "Incorrect number of arguments.\n Useage:\n $ARGV[0] --conf [file]\n";
exit $SIG{TERM};
}
our ($logfile, $pasteroot, $host, $srvname, $port, $certfile, $keyfile, $pidfile);
my $cfgf = undef;
GetOptions('conf=s' => \$cfgf);
my $config = Config::Tiny->read($cfgf);
$host = $config->{Server}{fqdn};
$srvname = $config->{Server}{baseuri};
$port = $config->{Server}{listenport};
$certfile = $config->{SSL}{certfile};
$keyfile = $config->{SSL}{keyfile};
$pidfile = $config->{Settings}{pidfile};
$pasteroot = $config->{Server}{pasteroot};
$logfile = $config->{Settings}{logfile}; # log
my $ver = "v1.0"; # hell yea, new revision!
# can we have a party
# with lots of hookers?
# bonus points for anal beads
if (-e $pidfile) {
die
"SPaste is already running or the lockfile didn't get wiped! If you are sure it is not running, remove $pidfile";
}
my $th;
open(PIDF, ">", $pidfile) or die $!;
print PIDF $$ . "\n";
close(PIDF);
open(STDERR, ">>", $logfile) or die $!;
open(LOG, '>>', $logfile) or die $!;
LOG->autoflush();
my $datet = purdydate();
print LOG "$datet Starting spaste $ver using $host:$port\n";
my $siteroot = $pasteroot;
$siteroot =~ s|/p/$||;
chdir "$siteroot" or die "$datet $!";
my $sock = IO::Socket::IP->new(
Listen => SOMAXCONN,
LocalPort => $port,
Blocking => 1,
ReuseAddr => 1
) or die "$datet $!";
umask(022);
my $WITH_THREADS = 1; # the switch!!
while (1) {
eval {
my $cl = $sock->accept(); # threaded accept
if ($cl) {
$datet = purdydate();
$th = threads->create(\&client, $cl) or die "$datet $!";
}
}; # eval
if ($@) {
print STDERR "ex: $@\n";
exit(1);
}
} # forever
close(LOG);
close(STDERR);
sub client # worker
{
my $cl = shift;
# upgrade INET socket to SSL
$cl = IO::Socket::SSL->start_SSL(
$cl,
SSL_server => 1,
SSL_cert_file => $certfile,
SSL_key_file => $keyfile,
SSL_verifycn_name => $host,
SSL_verifycn_scheme => 'default',
SSL_hostname => $host
) or die "$datet $@";
$th->detach() or print LOG "$datet Thread detach request failed. $!\n";
binmode($cl);
# unblock
my $flags = fcntl($cl, F_GETFL, 0) or die "$datet $cl->peerhost $!";
fcntl($cl, F_SETFL, $flags | O_NONBLOCK) or die "$datet $cl->peerhost $!";
while (!$cl->pending()) {
CT:
my $ret;
$ret = $cl->read(my $recv, 4096); # get the data
if (defined($ret) && length($recv) > 0) {
my $rndid = "";
# while (1) {
$rndid = genuniq();
if (!-e "$pasteroot$rndid") {
print $cl "$srvname/p/$rndid\n";
writef($rndid, $recv, $cl, $logfile);
$cl->close() or die "$datet $cl->peerhost $!"; # close last sock and move on
if ($cl->pending()) {
goto CT;
}
return 0; # return so we don't get stuck in the loop
# }
}
}
}
}
sub writef() {
my ($rndid, $recv, $cl, $logfile) = @_;
$datet = purdydate();
print LOG $datet . " " . $cl->peerhost . "/" . $cl->peerport;
print LOG " $rndid : storing at $pasteroot$rndid\n";
print "$rndid : storing at $pasteroot$rndid\n";
$datet = purdydate();
print LOG $datet . " " . $cl->peerhost . "/" . $cl->peerport;
print LOG " $rndid : serving at $srvname/p/$rndid\n";
print "$rndid : serving at $srvname/p/$rndid\n";
my $filename = $pasteroot . $rndid;
open(P, '>', $filename) or die "$datet $cl->peerhost $!";
print P $recv;
close(P);
return 1;
}
sub genuniq {
my $pasid; # for unique paste identifier
my @set = ('A' .. 'Z', 'a' .. 'z', 0 .. 9);
my $num = $#set;
$pasid .= $set[rand($num)] for 1 .. 8;
return $pasid; # push it back
}
sub purdydate {
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
my $datetime = sprintf("%04d%02d%02d %02d:%02d:%02d",
$year + 1900,
$mon + 1, $mday, $hour, $min, $sec);
return $datetime;
}
END {
if ($cfgf) {
if (-e $pidfile) {
unless ($SIG{TERM} || $SIG{INT}) {
print "Something unusual happened... check $logfile\n";
}
print LOG "Removing lockfile...\n";
unlink($pidfile);
}
print LOG "Stopping SPaste process cleanly...\n";
}
}