Debian Codegraph @

cgi-bin/sex

raw
1 #!/usr/bin/perl -wT
2
3 # CGI for stream exchange
4 #
5 # Author: Ulli Horlacher <[email protected]>
6
7 use Fcntl qw':flock :seek :mode';
8 use POSIX qw'mkfifo';
9 use Digest::MD5 qw'md5_hex';
10
11 # add fex lib
12 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/;
13 die "$0: no $FEXLIB\n" unless -d $FEXLIB;
14
15 $| = 1;
16
17 # import from fex.pp
18 our ($tmpdir,@logdir,$timeout,$fra,$bs,$MB);
19 our ($document_exchange);
20
21 # load common code, local config: $HOME/lib/fex.ph
22 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
23
24 chdir $spooldir or error(500,"$spooldir - $!");
25
26 # my $debuglog = "$tmpdir/sex.log";
27 my $ra = $ENV{REMOTE_ADDR}||0;
28 $fra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
29 $timeout *= 10;
30
31 # normal / public / DOX :
32 # in normal mode the recipient needs authentification, not the sender
33 # in public or DOX mode the sender needs authentification, not the recipient
34
35 $user = $id = $pmode = $type = '';
36 $stream = 'STDSTR';
37 $mode = $ENV{REQUEST_METHOD} eq 'POST' ? 'PUSH' : 'POP';
38
39 # parse HTTP QUERY_STRING
40 if (my $qs = $ENV{QUERY_STRING}) {
41 $qs = decode_b64($qs) if $qs =~ /^\w+=*$/;
42 foreach (split '&',$qs) { setparam(split '=',"$_=") };
43 }
44
45 unless ($user) { error(400,"Missing user") }
46 if ($mdomain and $user !~ /@/) { $user .= '@'.$mdomain }
47 if ($user =~ /^anonymous/) {
48 if (@anonymous_upload and ipin($ra,@anonymous_upload)) {
49 mkdirp($user);
50 } else {
51 error(403,"Forbidden");
52 }
53 } else {
54 unless (-f "$user/@") { error(404,"Unknown user $user") }
55 }
56 chdir $user or error(500,"$user - $!");
57
58 $stream = "STREAM/$stream";
59
60 if ($mode eq 'PUSH') {
61 # sexlog($mode);
62
63 if ($pmode eq 'DOX') {
64 # die "DOX mode not yet implemented";
65 &authentificate;
66 $stream =~ s:.*/::;
67 doxstream($stream);
68 exit;
69 } elsif ($pmode eq 'PUBLIC') {
70 &authentificate;
71 $stream =~ s:/STDSTR:/PUBLIC:;
72 }
73
74 mkdirp($stream);
75 my $fifo = "$stream/fifo";
76 unless (-p $fifo) {
77 mkfifo($fifo,0600) or error(503,"Cannot create $fifo : $!");
78 }
79
80 my $lock = "$stream/lock";
81 open $lock,'>>',$lock or error(503,"Cannot open $lock : $!");
82 flock $lock,LOCK_EX|LOCK_NB or error(409,"$stream already in use");
83
84 chmod 0600,$fifo;
85 unlink "$stream/mode";
86 unlink "$stream/type";
87 symlink $pmode,"$stream/mode" if $pmode;
88 symlink $type, "$stream/type" if $type;
89
90 $SIG{PIPE} = sub {
91 sleep 1;
92 rmrf($stream);
93 exit;
94 };
95 $SIG{ALRM} = sub {
96 syswrite STDOUT,".";
97 exit if $!;
98 $ALARM = 1;
99 };
100 syswrite STDOUT,"HTTP/1.9 199 Hold on";
101 for (my $i=0;$i<$timeout;$i++) {
102 alarm(1);
103 $ALARM = 0;
104 # will hang until $stream is opend for reading by another process
105 open $fifo,'>',$fifo and last;
106 unless ($ALARM) { error(503,"Cannot open $fifo : $!") }
107 }
108 alarm(0);
109 syswrite STDOUT,"\r\n";
110
111 unless (fileno $fifo) {
112 rmrf($stream);
113 error(504,"Timeout");
114 }
115
116 header('200 OK');
117
118 $B = 0;
119 # $shutdown = sub { sexlog("$mode $B"); close $fifo; rmrf($stream); exit; };
120 $shutdown = sub { sexlog("$mode $B"); exit; };
121 $SIG{PIPE} = sub { sleep 1; &$shutdown; };
122 # syswrite $fifo,$data if $data;
123 while ($b = sysread(STDIN,$_,$bs)) {
124 $B += $b;
125 syswrite $fifo,$_ or die $!;
126 }
127
128 &$shutdown;
129 }
130 elsif ($mode eq 'POP') {
131 $stream =~ s:/STDSTR:/PUBLIC: if $id eq 'public';
132 my $fifo = "$stream/fifo";
133 unless (-r $fifo) {
134 $stream =~ s:.*/::;
135 error(503,"No stream $stream for $user");
136 }
137 unless ($id eq 'public' and (readlink "$stream/mode"||'') eq 'PUBLIC'
138 or $user =~ /^anonymous/) {
139 &authentificate;
140 }
141 $type = readlink "$stream/type" || '';
142 $SIG{ALRM} = sub { error(504,"Timeout") };
143 alarm($timeout);
144 if (-e $fifo and not -r $fifo) { error(503,"$stream already in use") }
145 open $fifo,'<',$fifo or error(503,"Cannot open $fifo : $!");
146 chmod 0,$fifo;
147 alarm(0);
148 header('200 OK',$type);
149 # sexlog($mode);
150
151 $shutdown = sub { sexlog("$mode $B"); exit; };
152 $SIG{PIPE} = sub { &$shutdown; };
153 while ($b = sysread($fifo,$_,$bs)) {
154 $B += $b;
155 syswrite STDOUT,$_ or last;
156 }
157
158 &$shutdown;
159 }
160 else {
161 error(405,"Unknown Request");
162 }
163
164 exit;
165
166
167 sub setparam {
168 my ($v,$vv) = @_;
169
170 $v = uc(despace($v));
171 $vv = untaint(normalize($vv));
172 # $param{$v} = $vv;
173 if ($v eq 'USER') { $user = lc(despace($vv)) }
174 elsif ($v eq 'ID') { $id = despace($vv) }
175 elsif ($v eq 'MODE') { $pmode = uc(despace($vv)) }
176 elsif ($v eq 'TYPE') { $type = uc(despace($vv)) }
177 elsif ($v eq 'STREAM') { $stream = normalize_filename($vv) }
178 elsif ($v eq 'BS' and $vv =~ /(\d+)/) { $bs = $1 }
179 elsif ($v eq 'TIMEOUT' and $vv =~ /(\d+)/) { $timeout = $1 }
180 elsif ($v eq 'ANONYMOUS') { $id = $user ='anonymous'; $stream = $vv; }
181 }
182
183 sub sexlog {
184 my $msg = untaint("@_");
185
186 $msg =~ s/\n/ /g;
187 $msg =~ s/\s+$//;
188 $msg = sprintf "%s [%s_%s] %s (%s) %s\n",
189 isodate(time),$$,$ENV{REQUESTCOUNT},$user,$fra,$msg;
190
191 foreach my $log (@logdir) {
192 if (open $log,'>>',"$log/sex.log") {
193 flock $log,LOCK_EX;
194 seek $log,0,SEEK_END;
195 printf {$log} $msg;
196 close $log;
197 }
198 }
199 }
200
201 sub sigdie {
202 local $_ = shift;
203 chomp;
204 sigexit('DIE',$_);
205 }
206
207 sub sigexit {
208 my ($sig) = @_;
209 my $msg = "@_";
210
211 $msg =~ s/\n/ /g;
212 $msg =~ s/\s+$//;
213 $msg = sprintf "%s %s (%s) caught SIGNAL %s\n",
214 isodate(time),$user||'-',$fra||'-',$msg;
215
216 foreach my $log (@logdir) {
217 if (open $log,'>>',"$log/sex.log") {
218 flock $log,LOCK_EX;
219 seek $log,0,SEEK_END;
220 printf {$log} $msg;
221 close $log;
222 }
223 }
224 if ($sig eq 'DIE') {
225 shift;
226 die "@_\n";
227 } else {
228 die "SIGNAL @_\n";
229 }
230 }
231
232 sub error {
233 nvt_print("HTTP/1.1 @_");
234 exit;
235 }
236
237 sub header {
238 my ($status,$type) = @_;
239
240 return if $HTTP_HEADER;
241 $HTTP_HEADER = $status;
242
243 nvt_print("HTTP/1.1 $status");
244 if ($mode eq 'POP') {
245 nvt_print("Server: sexsrv");
246 if ($type eq 'GZIP') {
247 nvt_print("Content-Type: application/gzip");
248 } else {
249 nvt_print("Content-Type: application/binary");
250 }
251 nvt_print("Expires: 0");
252 nvt_print("Cache-Control: no-cache");
253 nvt_print("Connection: close");
254 }
255 nvt_print("");
256 }
257
258 sub authentificate {
259 my $rid;
260
261 error(400,"Missing auth-ID") unless $id;
262 open $id,'<','@' or error(401,"$user/@ - $!");
263 chomp($rid = <$id>||'');
264 close $id;
265 if ($rid and $sid and $id =~ /^(MD5H:)/) {
266 $rid = $1 . md5_hex($rid.$sid);
267 }
268 error(401,"Wrong auth-ID") if $rid ne $id;
269 }
270
271
272 sub doxstream {
273 my $folder = shift;
274 my $sdir = "$folder!stream";
275 my $log = ".fexdox/tarstream.log";
276 my $error = ".fexdox/tarstream.error";
277 my $lfolder = '';
278 my $b;
279 my $B = 0;
280 my $untar;
281 my $overquota;
282 my ($quota,$du) = check_sender_quota($user);
283 my $shutdown = sub {
284 my $file = '';
285 close $untar;
286 sexlog("DOX $folder $B");
287 if ((-s $error or $overquota) and -s $log and open $log,$log) {
288 while (<$log>) {
289 chomp;
290 $file = $_;
291 }
292 unlink untaint($file);
293 close $log;
294 }
295 unlink $log,$error;
296 doxunweed('.');
297 exit;
298 };
299 local $_;
300
301 my $limit = ($quota-$du)*$MB;
302 my $doxua = readlink_('@DOCUMENT_EXCHANGE');
303 if ($doxua eq 'yes' or not $doxua and $document_exchange) {
304 mkdir('DOX');
305 } else {
306 http_die("DOX not allowed for $user");
307 }
308 chdir 'DOX' or http_die("INTERNAL ERROR: no $user/DOX");
309 $lfolder = untaint(readlink($folder)) if -d $folder and -l $folder;
310 if (-d $folder and my $s = mtime($sdir)) {
311 if (mtime($folder) > $s or -d $lfolder and mtime($lfolder) > $s) {
312 rmrf($sdir);
313 }
314 }
315 mkdirp("$sdir/.fexdox");
316
317 if (-d $lfolder) {
318 $_ = `cp -alf '$lfolder/.' '$sdir/' 2>&1`;
319 http_die($_) if $?;
320 }
321
322 chdir $sdir or http_die("INTERNAL ERROR: no $user/DOX/$sdir");
323 header('200 OK');
324 $B = sysread(STDIN,$_,$bs);
325 # https://www.forensicswiki.org/wiki/Gzip
326 if (/^\x1F\x8B\x08/) {
327 $untar = 'tar -xvzf -';
328 } else {
329 $untar = 'tar -xvf -';
330 }
331 open $untar,"|$untar >'$log' 2>'$error'";
332 local $SIG{PIPE} = sub { sleep 1; &$shutdown; };
333 syswrite $untar,$_;
334 while ($b = sysread(STDIN,$_,$bs)) {
335 $B += $b;
336 if ($overquota = $B > $limit) {
337 last;
338 }
339 syswrite $untar,$_;
340 }
341
342 &$shutdown();
343 }
344
345
346 sub shellquote {
347 local $_ = shift;
348 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
349 return $_;
350 }