Debian Codegraph @

cgi-bin/fup

raw
1 #!/usr/bin/perl -wT
2
3 # F*EX CGI for upload
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7 # Contribs:
8 # Sebastian Zaiser <[email protected]> (upload status)
9 #
10
11 BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 }
12
13 # use utf8;
14 use Encode;
15 use IO::Handle;
16 use File::Basename;
17 use Cwd qw(abs_path getcwd);
18 use Fcntl qw(:flock :seek :mode);
19 use Digest::MD5 qw(md5_hex);
20
21 # add fex lib
22 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/;
23
24 $| = 1;
25
26 our $debug;
27 our $ndata = 0;
28 our $error = 'F*EX upload ERROR';
29 our $head = "$ENV{SERVER_NAME} F*EX upload";
30 our $autodelete = 'YES';
31 our $locale;
32
33 # import from fex.ph
34 our ($bcc,$keep,$keep_max,$nomail,$nostore,$overwrite,$max_fail);
35 our ($archive_sharing,$document_exchange,$encryption);
36 our (@locales,@throttle);
37 our (@local_domains,@local_rdomains,@local_hosts,@local_rhosts);
38 our (@registration_hosts,@demo,@file_link_dirs);
39
40 # import from fex.pp
41 our ($FEXHOME);
42 our ($spooldir,$durl,$tmpdir,@logdir,$logdir,$docdir,$hostname,$admin,$fra);
43 our ($keep_default,$fex_yourself);
44 our ($sendmail,$mdomain,$fop_auth,$mail_auth,$faillog,$amdl,$vp);
45 our ($dkeydir,$ukeydir,$akeydir,$skeydir,$gkeydir,$xkeydir);
46 our ($MB,$DS);
47 our $RB = 0; # read POST bytes (total)
48 our $akey = '';
49 our $dkey = '';
50 our $skey = '';
51 our $ukey = '';
52 our $gkey = '';
53
54 local $filesize = 0; # total file size
55
56 my $seek = 0; # already sent bytes (from previous upload)
57 my $fpsize = 0; # file part size (MIME-part)
58 my $boundary;
59 my $rid = ''; # real ID
60 my @header; # HTTP entity header
61 my $fileid; # file ID
62 my $captive;
63 my $muser; # main user fur sub or group user
64 my %specific; # upload specific KEEP and AUTODELETE parameters
65 my @to = ();
66 my $data = '';
67 my $file = '';
68 my $comment = '';
69 my $command = '';
70 my $bwlimit = '';
71 my $filename = '';
72 my $fkey = '';
73 my $okey = '';
74 my $pkey = '';
75 my $share = '';
76 my $shared = '';
77 my $archive = '';
78 my $avt = '';
79 my $azip = '';
80 my $addto = '';
81 my $replyto = '';
82 my $submit = '';
83 my $owner;
84 my $prefetch;
85 my $mup = 0;
86 my $back = "javascript:history.back()";
87 my $formpara =
88 'method="post" accept-charset="UTF-8" enctype="multipart/form-data"';
89 my $js_scroll =
90 "<script>".
91 "function scroll() { window.scrollTo(0,document.body.scrollHeight) }".
92 "</script>\n";
93 my $scroll = '<script>scroll()</script>';
94
95 our $from = '';
96 our $to = '';
97 our $id = '';
98
99 # load common code, local config: $FEXLIB/fex.ph
100 require "$FEXLIB/fex.pp";
101
102 # load fup local config
103 our ($info_1,$info_2,$info_login);
104
105 $locale = $ENV{LOCALE} || 'english';
106 foreach (
107 "/var/lib/fex/locale/$locale/lib/fup.pl",
108 "$FEXLIB/fup.pl",
109 ) {
110 if (-f) {
111 require;
112 last;
113 }
114 }
115
116 &check_camel unless $sid;
117
118 my $log = 'fup.log';
119
120 my $http_client = $ENV{HTTP_USER_AGENT} || '';
121 my $cl = $ENV{X_CONTENT_LENGTH} || $ENV{CONTENT_LENGTH} || 0;
122
123 $fra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
124 $locale = untaint($ENV{LOCALE}||'');
125
126 my $RA = $ENV{REMOTE_ADDR}||0;
127 if (@upload_hosts and not ipin($RA,@upload_hosts)) {
128 http_die(
129 "Uploads from your host ($RA) are not allowed.",
130 "Contact $ENV{SERVER_ADMIN} for details."
131 );
132 }
133 if ($max_fail and $faillog and not $ENV{HTTP_X_FORWARDED_FOR} and
134 open $faillog,$faillog) {
135 my $n = 0;
136 $n++ while <$faillog>;
137 close $faillog;
138 if ($n > $max_fail) {
139 http_die(
140 "Too many login failures from $RA : IP BLOCKED",
141 "Contact $ENV{SERVER_ADMIN} for details."
142 );
143 }
144 }
145
146 &check_maint;
147
148 &parse_request; # showstatus will not come back!
149
150 if ($addto) {
151 my %to;
152 foreach $to (@to) { $to{$to} = 1 }
153 push @to,$addto unless $to{$addto};
154 # user has submitted with [select from your address book] ?
155 # if ($submit and @to == 1) { $addto = '' }
156 }
157
158 $to = join(',',@to);
159
160 if ($from eq $to and $fex_yourself =~ /^no|0$/i) {
161 http_die("fexing to yourself is not allowed");
162 }
163
164 $uid = randstring(8) unless $uid; # upload ID
165
166 # user requests for forgotten ID
167 if ($id_forgotten and $mail_authid and not ($fop_auth or $nomail)) {
168 &check_status($from);
169 &id_forgotten;
170 exit;
171 }
172
173 # public recipients? (needs no auth-ID for sender)
174 if ($to and $id and $id eq 'PUBLIC' and @public_recipients) {
175
176 unless ($from) {
177 http_die("missing sender email address");
178 }
179 # must use $param{FROM} for checking because $from is expanded with $mdomain
180 unless (checkaddress(despace($param{FROM}))) {
181 http_die("$param{FROM} is not a valid email address");
182 }
183 foreach my $to (@to) {
184 unless (grep /^\Q$to\E$/i,@public_recipients) {
185 http_die("$to is not a valid recipient");
186 }
187 }
188 $restricted = $public = $rid = $id;
189 $akey = '';
190 }
191
192 # anonymous upload?
193 if (not $share and $from =~ /^anonymous@/) {
194 unless (@anonymous_upload) {
195 http_die("anonymous usage is not allowed");
196 }
197 unless (ipin($RA,@anonymous_upload)) {
198 http_die("anonymous upload from your ip $RA is not allowed");
199 }
200 $id = $rid = $anonymous = 'anonymous';
201 if ($to =~ /^anonymous/) {
202 @to = ($to);
203 $autodelete{$to} = $autodelete = $specific{'autodelete'}||'NO';
204 }
205 $nomail = $anonymous;
206 $akey = '';
207 }
208
209 $comment = 'NOMAIL' if $nomail and not $comment;
210
211 # one time token
212 if ($okey) {
213 $to = "@to" or http_die("no recipient specified");
214 $from = readlink("$to/\@OKEY/$okey")
215 or http_die("no upload key \"<code>$okey</code>\" - ".
216 "request another one from $to");
217 $from = untaint($from);
218 $akey = '';
219 }
220
221 $nomail = $http_client if $http_client =~ /fexsync/;
222
223 check_status($from) if $from;
224
225 # check regular ID
226 if ($from and $id and
227 not ($public||$anonymous||$gkey||$skey||$ukey||$okey||$pkey))
228 {
229 $rid = readline1("$from/@")||'';
230 if ($rid and sidhash($rid,$id) eq $id) {
231 $fulluser = $from;
232 # set time mark for successfull access
233 my $time = untaint(time);
234 utime $time,$time,$from;
235 $akey = genakey($from); # needed for fexsend commands, too!
236 $captive = -e "$from/\@CAPTIVE";
237 } else {
238 fuplog("ERROR: wrong auth-ID for $from");
239 debuglog("id sent by user $from=$id, real id=$rid");
240 faillog("user $from, id $id");
241 if ($from and $mail_authid and -f "$from/@" and not ($fop_auth or $nomail)) {
242 http_header('200 OK','Content-Type: text/html');
243 print html_header($head);
244 pq(qq(
245 "<h1>ERROR: wrong auth-ID</h1>"
246 'I have lost my auth-ID!'
247 '<a href="/fup?from=$from&id=?">Mail it</a> to $from!\n'
248 ));
249 exit;
250 } else {
251 http_die("wrong user or auth-ID");
252 }
253 }
254 }
255
256 if ($ukey and $ukey =~ /^_/ and $ENV{REQUEST_METHOD} eq 'GET') {
257 http_header('200 OK','Content-Type: text/plain');
258 print "FUP=$ENV{PROTO}://$ENV{HTTP_HOST}/fup?ukey=$ukey;\n";
259 if ($ukey =~ /_$/) {
260 # for xxx
261 pq(q(
262 'xup() {'
263 ' local c t;'
264 ' [ -t 0 -a ! -r "$1" ] && { echo "usage: xup FILE..."; echo "usage: ... | xup"; return; };'
265 ' c="[$(id -nu)@$(hostname)] ${@:--}";'
266 ' t="${TMPDIR:-/tmp}/xxx_$(date +%Y%m%d_%H%M%S)";'
267 ' [ -t 0 ] && { t="$t.tar"; echo $t:; tar cvf "$t" "$@"; } || { t="$t.gz"; gzip > "$t"; };'
268 ' [ $? = 0 ] && curl -F "keep=1" -F "comment=$c" -F "file=@$t" $FUP|cat;'
269 ' rm "$t";'
270 '}; xup'
271 ));
272 } else {
273 pq(q(
274 'fup() {'
275 ' local o c a t;'
276 ' [ "$1" = -h ] && { echo "usage: fup [\"CURL-OPTIONS\"] FILE..."; return; };'
277 ' while [[ "$1" =~ ^- ]]; do o="$o $1"; shift; done;'
278 ' [ -r "$1" ] || { echo "usage: fup FILE..."; return; };'
279 ' c="[$(id -nu)@$(hostname)]";'
280 ' [ -z "$2" ] && [[ "$1" =~ \.(tar|tgz|tar\.gz|zip)$ ]] &&'
281 ' { curl $o -F "keep=1" -F "comment=$c" -F "file=@$1" $FUP|cat; return; };'
282 ' a=$(basename "$1");'
283 ' [ -n "$2" ] && { echo -n "archive name: "; read a; };'
284 ' t="${TMPDIR:-/tmp}/fup_$a.tar";'
285 ' echo $t:;'
286 ' tar cvf "$t" "$@" &&'
287 ' curl $o -F "keep=1" -F "comment=$c" -F "file=@$t" $FUP|cat;'
288 ' rm "$t";'
289 '}; fup'
290 ));
291 }
292 exit;
293 }
294
295 if ($share) {
296 $share =~ s/\s//g;
297 if ($share !~ /^[\w-]+$/) {
298 http_die("illegal share name");
299 }
300 unless ($from) {
301 http_die("sender needed for share");
302 }
303 unless (@to) {
304 http_die("owner needed for share");
305 }
306 $share = untaint($share);
307 $to = "@to";
308 if ($to =~ /\@.*\@/) {
309 http_die("sharing not allowed with multiple owners");
310 }
311 unless (checkaddress($to)) {
312 http_die("$to is not a valid email address");
313 }
314 $autodelete{$to} = 'no';
315
316 my $sharing = readlink_("$to/\@ARCHIVE_SHARING");
317 if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) {
318 http_die("archive sharing is not allowed");
319 }
320
321 $owner = $to;
322 $shared = "$to/SHARE/$share";
323
324 if ($pkey) {
325 &check_pkey;
326 my $access = readlink("$shared/users/$from/access")||'read';
327 $access = 'owner' if $to eq $from;
328 if ($file) {
329 if ($access eq 'read') {
330 http_die("no write access for $to/$share/$from");
331 }
332 # archive can be given as URI parameter
333 unless ($archive) {
334 $file =~ /^(.+)_($vp\.(tar|tgz|zip|7z|gz))$/;
335 $archive = $1 // '';
336 $avt = $2 // '';
337 }
338 if ($access eq 'write' and not
339 (length($archive) and -d "$shared/archives/$archive"))
340 { http_die("no write access for $to/$share/$from/$file") }
341 }
342 } elsif ($to eq $from and $fulluser) {
343 if ($file and not $archive) {
344 $file =~ /^(.+)_($vp\.(tar|tgz|zip|7z|gz))$/;
345 $archive = $1 // '';
346 $avt = $2 // '';
347 }
348 } else {
349 http_die("PKEY missing (from=$from to=$to)");
350 }
351
352 if ($file) {
353 # no archive version and container type given?
354 unless ($avt) {
355 $file =~ s/\.tar\.gz$/.tgz/;
356 $archive ||= $file;
357 $archive =~ s/[^\w.+-]/_/g;
358 $archive =~ s:^\.:_:;
359 $archive = untaint($archive);
360 my $container = 'zip';
361 if ($file =~ s/\.(tar|tgz|zip|7z)$//) {
362 $container = $1;
363 $archive =~ s/\.(tar|tgz|zip|7z)$//;
364 } else {
365 # filename for zip archiving after upload (not zip filename!)
366 $azip = $file;
367 $azip =~ s:/:_:g;
368 $azip = untaint($azip);
369 }
370 my @d = gmtime(time());
371 $avt = sprintf('%d%02d%02d_%02d%02d%02d.%s',
372 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],$container);
373 }
374 if ($archive !~ /^[\w.+-]+$/) {
375 http_die("illegal filename for archive sharing");
376 }
377 }
378 }
379
380 # optional $auth_hook() in fup.pl
381 if ($auth_hook and ($akey||$skey||$gkey||$ukey) and $from and -d $from) {
382 &$auth_hook;
383 }
384
385 if ($from and $fulluser and not $restricted) {
386
387 if ($command =~ /^DOX/) {
388 my $doxua = readlink_("$from/\@DOCUMENT_EXCHANGE");
389 if ($doxua eq 'no' or not $document_exchange and $doxua ne 'yes') {
390 http_die('DOX not allowed');
391 }
392 }
393
394 if ($command =~ /^DOXSYNC:([^\s\/]+):([^\s\/]+)/) {
395 my $owner = $1;
396 my $folder = $2;
397 my $sync = '';
398 my $fexsync = $ENV{FEXHOME}.'/bin/fexsync';
399
400 if ($mdomain and -d "$owner\@$mdomain/DOX") {
401 $owner .= '@'.$mdomain;
402 }
403
404 if ($from ne $owner) {
405 http_die("DOX users are not yet supported");
406 }
407
408 if ("/$folder" =~ m:/\.: or $folder =~ /[<>|]/) {
409 http_die("illegal DOX directory");
410 }
411
412 if ($from eq $owner and $folder =~ s/:DELETE$//) {
413 my $dd = "$owner/DOX/$folder";
414 rmrf($dd);
415 if (-d $dd) {
416 http_die("cannot delete $folder");
417 } else {
418 http_header('222 OK');
419 }
420 exit;
421 }
422
423 $folder =~ s/:COMPRESS$// and $fexsync .= ' -z';
424
425 # my $tkey = sprintf "%06d",int(rand(1000000));
426
427 my $df = "$owner/DOX/$folder";
428 chdir $df or http_die("no such DOX directory");
429 debuglog("cd $df");
430 debuglog("$fexsync .");
431 local $ENV{FEXCOMMENT} = $df;
432 local $ENV{FEXSERVER} = '';
433
434 my $log = sprintf "../%s_fexsync_%s.log",$folder,time;
435 # tee is needed for process timing!
436 open $fexsync,"$fexsync . 2>&1|tee $log|"
437 or http_die("cannot run fexsync - $!");
438
439 while (<$fexsync>) {
440 debuglog($_);
441 if (/^fexsync:/) {
442 http_die($_);
443 }
444 if (/fexsync \Q$folder/) {
445 s/:\d+/:$ENV{PORT}/;
446 $sync = $_;
447 last;
448 }
449 }
450
451 unless ($sync) {
452 http_die("fexsync failed");
453 }
454
455 http_header(
456 '222 OK',
457 'Content-Type: text/plain',
458 );
459
460 print $sync;
461 shutdown(STDOUT,2);
462 close STDOUT;
463
464 while (<$fexsync>) { }
465 exit;
466 }
467
468 if ($command =~ /^DOXDEL:([^\s\/]+)+$/) {
469 my $owner = $from;
470 my $folder = $1;
471 my $dd = "$owner/DOX/$folder";
472
473 if (-d $dd) {
474 rmrf($dd);
475 } else {
476 http_die("no DOX directory $folder");
477 }
478
479 if (my @v = grep /\/\Q$folder\E_$vp$/,glob "${dd}_*") {
480 foreach my $v (@v) {
481 rmrf(untaint($v));
482 }
483 }
484
485 if (-d $dd) {
486 http_die("deleting of $dd failed - $!");
487 } else {
488 http_header(
489 '222 OK',
490 'Content-Type: text/plain',
491 );
492 print "DOX directory $folder removed\n";
493 }
494 close STDOUT;
495 doxdu($owner);
496 exit;
497 }
498
499 if ($command eq 'DOXLIST') {
500 my $owner = $from;
501
502 chdir "$owner/DOX" or http_die("no DOX directory");
503
504 http_header(
505 '222 OK',
506 'Content-Type: text/plain',
507 );
508
509 my $dox = sprintf "%s://%s/dox/%s",
510 $ENV{PROTO},($ENV{HTTP_HOST}||$ENV{SERVER_NAME}),$owner;
511 $dox =~ s/:443// if $ENV{PROTO} eq 'https';
512 print "$dox/\n";
513
514 alarm(0);
515 foreach (glob '.*.du') {
516 if (-l and /^\.(\S+)\.du$/ and -d $1) {
517 my $folder = $1;
518 my $du = readlink;
519 printf "%s %s %s\n",mtime($folder),int($du/1024),$folder;
520 }
521 }
522
523 exit;
524 }
525
526 if ($command =~ /^DOXLIST:([^\s\/]+)+$/) {
527 my $owner = $from;
528 my $folder = $1;
529 my $dd = "$owner/DOX/$folder";
530
531 chdir $dd or http_die("no DOX directory $folder");
532
533 http_header(
534 '222 OK',
535 'Content-Type: text/plain',
536 );
537
538 my $dox = sprintf "%s://%s/dox/%s",
539 $ENV{PROTO},($ENV{HTTP_HOST}||$ENV{SERVER_NAME}),$owner;
540 $dox =~ s/:443// if $ENV{PROTO} eq 'https';
541 print "$dox/$folder\n";
542
543 foreach (asort(find('.'))) {
544 if ((-l or -f) and my @s = lstat) {
545 s:./::;
546 printf "%d %s\n",$s[9],hexencode($_);
547 }
548 }
549
550 exit;
551 }
552
553 # create one day UKEY
554 if ($command eq 'GENUKEY') {
555 my $ukey = randstring(8);
556 $ukey = '_'.$ukey if $http_client =~ /^fexpack/;
557 $ukey = '_'.$ukey.'_' if $http_client =~ /^xxx/;
558 symlink $from,"$ukeydir/$ukey"
559 or http_die("cannot create UKEY $ukey - $!\n");
560 if ($http_client =~ /fex/) {
561 http_header(
562 '200 OK',
563 "X-UKEY: $ukey",
564 'Content-Type: text/plain',
565 );
566 print "$ukey\n";
567 exit;
568 } else {
569 nvt_print(
570 "HTTP/1.1 302 Found",
571 "Location: /fup?ukey=$ukey",
572 'Content-Length: 0',
573 ""
574 );
575 # control back to fexsrv for further HTTP handling
576 &reexec;
577 }
578 }
579
580 # generate onetime upload key and show URL
581 if ($command =~ /^GENOKEY:(\S+)/) {
582 my $user = $1;
583 my $okey = randstring(8);
584 my $okeyd = "$spooldir/$from/\@OKEY";
585
586 unless ($user =~ /@/ and checkaddress($user)) {
587 http_die("$user is not a valid email address");
588 }
589
590 mkdir $okeyd;
591 symlink $user,"$okeyd/$okey"
592 or http_die("cannot create OKEY $okeyd/$okey - $!");
593
594 nvt_print(
595 'HTTP/1.0 200 OK',
596 "Server: fexsrv",
597 "X-okey: $okey",
598 "Content-Type: text/plain",
599 ''
600 );
601
602 print "$ENV{PROTO}://$ENV{HTTP_HOST}/fup?to=$from?okey=$okey\n";
603 exit;
604 }
605 }
606
607 # forward a copy of a file to another recipient
608 if (($akey or $fulluser) and $dkey and $command eq 'FORWARD') {
609 my $file = untaint(readlink_("$dkeydir/$dkey"));
610 http_die("unknown dkey <code>$dkey></code>") unless $file;
611 $file =~ s:^\.\./::;
612 forward($file);
613 exit;
614 }
615
616 # modify file parameter
617 if ($akey and $dkey and $command eq 'MODIFY') {
618 my $file = untaint(readlink_("$dkeydir/$dkey")) or
619 http_die("unknown dkey <code>$dkey</code>");
620 $file =~ s:^\.\./::;
621 modify($file);
622 exit;
623 }
624
625 # copy file from incoming to outgoing spool
626 if ($akey and $dkey and $command eq 'COPY') {
627 unless ($file = readlink("$dkeydir/$dkey")) {
628 http_die("no such file with DKEY=$dkey");
629 }
630 if ($file =~ m:../(.+)/(.+)/(.+):) {
631 ($to,$from,$file) = ($1,$2,$3);
632 } else {
633 http_die("bad DKEY $dkey -> $file");
634 }
635 unless (-f "$to/$from/$file/data") {
636 http_die("file not found");
637 }
638 if (-e "$to/$to/$file/data") {
639 http_die("file $file already exists in your outgoing spool")
640 if (readlink("$to/$to/$file/id")||$to) ne
641 (readlink("$to/$from/$file/id")||$from);
642 } else {
643 mkdirp("$to/$to/$file");
644 link "$to/$from/$file/data","$to/$to/$file/data"
645 or http_die("cannot link to $to/$to/$file/data - $!\n");
646 copy("$to/$from/$file/filename","$to/$to/$file/filename");
647 copy("$to/$from/$file/id","$to/$to/$file/id");
648 open $file,'>',"$to/$to/$file/notify";
649 close $file;
650 open $file,'>',"$to/$to/$file/download";
651 print {$file} "$to\n";
652 close $file;
653 $dkey = randstring(8);
654 unlink "$to/$to/$file/dkey","$to/$to/$file/keep","$dkeydir/$dkey";
655 symlink "../$to/$to/$file","$dkeydir/$dkey";
656 symlink $dkey,"$to/$to/$file/dkey";
657 }
658 nvt_print(
659 "HTTP/1.1 302 Found",
660 "Location: $ENV{PROTO}://$ENV{HTTP_HOST}/rup?oto=$to&file=$file",
661 'Content-Length: 0',
662 ''
663 );
664 # control back to fexsrv for further HTTP handling
665 &reexec;
666 }
667
668 # delete file without download
669 if ($akey and $dkey and $command eq 'DELETE') {
670 $del = untaint(readlink_("$dkeydir/$dkey"));
671 http_die("unknown dkey <code>$dkey</code>") unless $del;
672 $del =~ s:^\.\./::;
673 $filename = filename($del);
674 if (unlink("$del/data") or unlink("$del/upload")) {
675 if (open F,'>',"$del/error") {
676 printf F "%s has been deleted by %s at %s\n",
677 $filename,$ENV{REMOTE_ADDR},isodate(time);
678 close F;
679 }
680 my $referer = $ENV{HTTP_REFERER}||'';
681 if ($referer =~ m:/fas:) {
682 nvt_print(
683 "HTTP/1.1 302 Found",
684 "Location: $referer",
685 'Content-Length: 0',
686 ""
687 );
688 } else {
689 nvt_print(
690 "HTTP/1.1 302 Found",
691 "Location: $ENV{PROTO}://$ENV{HTTP_HOST}/fup?command=LISTRECEIVED",
692 'Content-Length: 0',
693 ""
694 );
695 }
696 # control back to fexsrv for further HTTP handling
697 &reexec;
698 } else {
699 my $s = $!;
700 http_header('404 Not Found');
701 print html_header($head);
702 print "<h3>$filename not deleted ($s)</h3>\n";
703 print "<a href=\"/fup?command=LISTRECEIVED\">continue</a>\n" if $akey;
704 print "</body></html>\n";
705 }
706 exit;
707 }
708
709 # ip restrictions
710 if ($from and open my $ipr,"$from/\@UPLOAD_HOSTS") {
711 my @hosts;
712 while (<$ipr>) {
713 chomp;
714 s/#.*//;
715 push @hosts,$_ if /\w/;
716 }
717 close $ipr;
718 unless (@hosts and ipin($RA,@hosts)) {
719 http_die("$from is not allowed from IP $RA");
720 }
721 }
722
723 # special commands
724 if (($fulluser or $gkey or $skey) and $command) {
725
726 if ($command eq 'CHECKQUOTA') {
727 http_die("illegal command \"$command\"") if $public or $anonymous;
728 nvt_print('HTTP/1.1 222 OK');
729 # nvt_print("X-SID: $ENV{SID}") if $ENV{SID};
730 my ($quota,$du) = check_sender_quota($muser||$from);
731 nvt_print("X-Sender-Quota: $quota $du") if $quota;
732 ($quota,$du) = check_recipient_quota($muser||$from);
733 nvt_print("X-Recipient-Quota: $quota $du") if $quota;
734 nvt_print('');
735 exit;
736 }
737
738 if ($command eq 'LISTSETTINGS') {
739 http_die("illegal command \"$command\"") if $public or $anonymous;
740 nvt_print('HTTP/1.1 222 OK');
741 # nvt_print("X-SID: $ENV{SID}") if $ENV{SID};
742 my ($quota,$du) = check_sender_quota($muser||$from);
743 nvt_print("X-Sender-Quota: $quota $du") if $quota;
744 ($quota,$du) = check_recipient_quota($muser||$from);
745 nvt_print("X-Recipient-Quota: $quota $du") if $quota;
746 $autodelete = lc(readlink("$from/\@AUTODELETE") || $autodelete);
747 nvt_print("X-Autodelete: $autodelete");
748 $keep = readlink("$from/\@KEEP") || $keep;
749 nvt_print("X-Default-Keep: $keep");
750 $locale = readlink("$from/\@LOCALE") || $default_locale || 'english';
751 nvt_print("X-Default-Locale: $locale");
752 $mime = -e "$from/\@MIME" ? 'yes' : 'no';
753 nvt_print("X-MIME: $mime");
754 nvt_print('');
755 exit;
756 }
757
758 if ($command eq 'RENOTIFY') {
759 http_die("illegal command \"$command\"") if $public or $anonymous;
760 my $nfile = '';
761 if ($dkey) {
762 # resend notification email
763 $file = readlink("$dkeydir/$dkey")
764 or html_error($error,"illegal DKEY $dkey");
765 $file =~ s:^../::;
766 $file = untaint($file);
767 unlink "$file/download"; # re-allow download from any ip address
768 notify_locale($dkey,'new');
769 http_header(
770 '200 OK',
771 "X-Notify: $file",
772 );
773 $nfile = $file;
774 } else {
775 http_header('200 OK');
776 }
777 print html_header($head);
778 # list sent files
779 print "<h3>Files from $from, ",
780 "click on the file name to resend a notification email:</h3>\n",
781 "<pre>\n";
782 foreach $file (glob "*/$from/*") {
783 next if $file =~ m:/STDFEX$:;
784 next if $file =~ m:(.+?)/: and -l $1;
785 $size = -s "$file/data";
786 next unless $size;
787 $size = int($size/$MB+0.5);
788 $filename = $comment = '';
789 my $rto = $file;
790 $rto =~ s:/.*::;
791 if ($dkey = readlink("$file/dkey")) {
792 if ($rto ne $to) {
793 $to = $rto;
794 print "\nto $to :\n";
795 }
796 if (open $file,'<',"$file/filename") {
797 $filename = <$file>;
798 close $file;
799 }
800 if ($filename and length $filename) {
801 $filename = htmlquote($filename);
802 } else {
803 $filename = '???';
804 }
805 if (open $file,'<',"$file/comment") {
806 $comment = untaint(htmlquote(getline($file)));
807 close $file;
808 }
809 my $rkeep = untaint(readlink("$file/keep")||$keep_default)
810 - int((time-mtime("$file/filename"))/$DS);
811 if ($comment =~ /NOMAIL/ or
812 readlink_("$to/\@NOTIFICATION") =~ /^no/i) {
813 printf "%8s MB (%2s d) %s/%s/%s\n",
814 $size,
815 $rkeep,
816 $durl,
817 $dkey,
818 urlencode(basename($file));
819 } else {
820 printf "%8s MB (%2s d) <a href=\"%s\">%s</a>%s %s\n",
821 $size,
822 $rkeep,
823 untaint("/fup?dkey=$dkey&command=RENOTIFY"),
824 $filename,
825 $comment ? qq' "$comment"' : '',
826 $file eq $nfile ?
827 " &rarr; notification email has been resent" :
828 "";
829 }
830 }
831 }
832 pq(qq(
833 '</pre>'
834 '<p><a href="/foc">back to F*EX operation control</a>'
835 '</body></html>'
836 ));
837 exit;
838 }
839
840 if ($command =~ /^LIST(RECEIVED)?$/) {
841 http_die("illegal command \"$command\"") if $public or $anonymous;
842 # list sent files
843 if ($to and $param{'TO'} eq '*') {
844 http_header('200 OK');
845 print html_header($head);
846 # "(Format: [size] [rest keep time] [filename] [comment])<p>\n",
847 print "<h3>Files from $from:</h3>\n",
848 "<pre>\n";
849 foreach $file (glob "*/$from/*") {
850 next if $file =~ m:/STDFEX$:;
851 next if $file =~ m:(.+?)/: and -l $1;
852 next if readlink_("$file/data") eq 'upload';
853 $size = -s "$file/data";
854 next unless $size;
855 $size = int($size/$MB+0.5);
856 $filename = $comment = '';
857 my $rto = $file;
858 $rto =~ s:/.*::;
859 if ($dkey = readlink("$file/dkey")) {
860 # die $file if -s "$file/data" and $file =~ /^$from/;
861 if ($rto ne $to) {
862 $to = $rto;
863 print "\nto $to :\n";
864 }
865 if (open $file,'<',"$file/filename") {
866 $filename = <$file>;
867 close $file;
868 }
869 if ($filename and length $filename) {
870 $filename = htmlquote($filename);
871 } else {
872 $filename = '???';
873 }
874 if (open $file,'<',"$file/comment") {
875 $comment = untaint(htmlquote(getline($file)));
876 close $file;
877 }
878 my $rkeep = untaint(readlink("$file/keep")||$keep_default)
879 - int((time-lmtime("$file/keep"))/$DS);
880 printf "%8s MB (%3s d) %s <a href=\"%s\">%s</a>%s\n",
881 $size,
882 $rkeep,
883 stat("$file/download")?'+':'-',
884 untaint("/fup?dkey=$dkey&command=FORWARD"),
885 $filename,
886 $comment?qq( "$comment"):'';
887 }
888 }
889 pq(qq(
890 '</pre>'
891 '<p><a href="$back">back to F*EX operation control</a>'
892 '</body></html>'
893 ));
894 }
895 # list received files
896 else {
897 $to = $from;
898 http_header('200 OK');
899 print html_header($head);
900 # "(Format: [size] [rest keep time] [URL] [comment])<p>\n",
901 print "<h3>Files for $to (*):</h3>\n",
902 "<pre>\n";
903 foreach $from (glob "$to/*") {
904 next if $from =~ /[A-Z]/;
905 $from =~ s:.*/::;
906 $url = '';
907 foreach $file (glob "$to/$from/*") {
908 next if $file =~ /\/STDFEX$/;
909 next if readlink_("$file/data") eq 'upload';
910 $filename = $comment = '';
911 $size = -s "$file/data";
912 next unless $size;
913 $size = int($size/$MB+0.5);
914 if ($dkey = readlink("$file/dkey")) {
915 print "\nfrom $from :\n" unless $url;
916 $file =~ m:.*/(.+):;
917 $url = "$durl/$dkey/$1";
918 unless (-l "$dkeydir/$dkey") {
919 symlink untaint("../$file"),untaint("$dkeydir/$dkey");
920 }
921 if (open $file,'<',"$file/filename") {
922 $filename = <$file>;
923 close $file;
924 }
925 if ($filename and length $filename) {
926 $filename = htmlquote($filename);
927 } else {
928 $filename = '???';
929 }
930 if (open $file,'<',"$file/comment") {
931 $comment = untaint(htmlquote(getline($file)));
932 $comment = ' "'.$comment.'"';
933 close $file;
934 }
935 my $rkeep = untaint(readlink("$file/keep")||$keep_default)
936 - int((time-mtime("$file/filename"))/$DS);
937 printf "[<a href=\"/fup?akey=%s&dkey=%s&command=DELETE\">delete</a>] ",
938 $akey,$dkey;
939 printf "[<a href=\"/fup?akey=%s&dkey=%s&command=COPY\">forward</a>] ",
940 $akey,$dkey;
941 printf "%8s MB (%3s d) <a href=\"%s\">%s</a>%s\n",
942 $size,$rkeep,$url,$filename,$comment;
943 }
944 }
945 }
946 pq(qq(
947 '</pre>'
948 '(*) Files for other email addresses you own will not be listed here!<p>'
949 '<a href="/foc">back to F*EX operation control</a>'
950 '</body></html>'
951 ));
952 }
953 exit;
954 }
955
956 if ($command eq 'LISTSENT') {
957 http_die("illegal command \"$command\"") if $public or $anonymous;
958 # show download URLs
959 http_header('200 OK');
960 print html_header($head);
961 print "<h2>Download URLs of files you have sent\n";
962 foreach $to (glob "*/$from") {
963 if (@files = glob "$to/*/data") {
964 $to =~ s:/.*::;
965 print "<h3>to $to :</h3>\n";
966 print "<pre>\n";
967 foreach $file (@files) {
968 $file =~ s:/data::;
969 next if $file =~ /\/STDFEX$/;
970 $dkey = readlink("$file/dkey") or next;
971 $file =~ s:.*/::;
972 print "$ENV{PROTO}://$ENV{HTTP_HOST}/fop/$dkey/$file\n";
973 }
974 print "</pre>\n";
975 }
976 }
977 pq(qq(
978 '</pre>'
979 '<p><a href="$back">back to F*EX operation control</a>'
980 '</body></html>'
981 ));
982 exit;
983 }
984
985 if ($command eq 'FUPWATCH') {
986 http_die("illegal command \"$command\"") if $public or $anonymous;
987 http_header(
988 '222 OK',
989 'Content-Type: text/plain',
990 );
991 alarm(0);
992 $| = 1;
993 exec "$FEXHOME/bin/fupwatch",$from or
994 http_die("cannot run fupwatch : $!");
995 }
996
997 if ($command eq 'FOPLOG') {
998 http_die("illegal command \"$command\"") if $public or $anonymous;
999 if (open my $log,"$logdir/fop.log") {
1000 http_header('200 OK');
1001 while (<$log>) {
1002 next if /\/STDFEX\s/;
1003 if (s:^([^/]+)/$from/:$1 :) {
1004 if (s:(\d+)/(\d+)$:$1: and $1 and $1 == $2) {
1005 s/ \[[\d_]+\]//;
1006 print;
1007 }
1008 }
1009 }
1010 }
1011 exit;
1012 }
1013
1014 if ($command eq 'RECEIVEDLOG') {
1015 http_die("illegal command \"$command\"") if $public or $anonymous;
1016 if (open my $log,"$logdir/fup.log") {
1017 http_header('200 OK');
1018 while (<$log>) {
1019 next if /\sSTDFEX\s/;
1020 if (/\d+$/) {
1021 my @F = split;
1022 if ($F[5] eq $to) {
1023 s/ \[[\d_]+\]//;
1024 print;
1025 }
1026 }
1027 }
1028 }
1029 exit;
1030 }
1031
1032 if ($command eq 'SENDLOG') {
1033 http_die("illegal command \"$command\"") if $public or $anonymous;
1034 if (open my $log,"$logdir/fup.log") {
1035 http_header('200 OK');
1036 while (<$log>) {
1037 next if /\sSTDFEX\s/;
1038 if (/(\S+\@\S+)/ and $1 eq $from) {
1039 s/ \[[\d_]+\]//;
1040 print;
1041 }
1042 }
1043 }
1044 exit;
1045 }
1046
1047 if (@to and $command eq 'CHECKRECIPIENT') {
1048 http_die("illegal command \"$command\"") if $public or $anonymous;
1049 check_rr($from,@to);
1050 nvt_print('HTTP/1.1 204 OK');
1051 nvt_print("X-SID: $sid") if $sid;
1052 foreach my $to (@group?@group:@to) {
1053 my $options = sprintf "(autodelete=%s,keep=%s,locale=%s)",
1054 $autodelete{$to}||$autodelete,
1055 $keep{$to}||$keep_default,
1056 readlink("$to/\@LOCALE")||$locale{$to}||$default_locale;
1057 nvt_print("X-Recipient: $to $options");
1058 }
1059 nvt_print('');
1060 # control back to fexsrv for further HTTP handling
1061 &reexec;
1062 }
1063
1064 if ($file and @to and $command eq 'DELETE') {
1065 http_die("illegal command \"$command\"") if $public or $anonymous;
1066 http_die("subuser cannot delete") if $from eq 'SUBUSER';
1067 foreach (@group?@group:@to) {
1068 my $to = $_;
1069 $to =~ s/:\w+=.*//; # remove options from address
1070 $del = "$to/$from/$fkey";
1071 # swap to and from for special senders, see fup storage swap!
1072 $del = "$from/$to/$fkey" if $from =~ /^(fexmail|anonymous)/;
1073
1074 $del =~ s:^/+::;
1075 if ($del =~ /\/\./) {
1076 http_die("illegal parameter <code>$del</code>");
1077 }
1078 $del = untaint($del);
1079
1080 if (unlink("$del/data") or unlink("$del/upload")) {
1081 if (open F,'>',"$del/error") {
1082 print F "$file has been deleted by $from\n";
1083 close F;
1084 }
1085 http_header('200 OK',"X-File: $del");
1086 print html_header($head);
1087 print "<h3>$file for $to deleted</h3>\n";
1088 } else {
1089 http_header("404 Not Found");
1090 print html_header($head);
1091 print "<h3>$file not deleted</h3>\n";
1092 }
1093 if ($akey) {
1094 printf "<a href=\"/fup?akey=%s&to=%s&command=LISTRECEIVED\">continue</a>\n",
1095 $akey,$to;
1096 }
1097 print "</body></html>\n";
1098 }
1099 exit;
1100 }
1101
1102 }
1103
1104 # list shares
1105 if ($fulluser and $command eq 'LISTSHARES') {
1106 my $sharing = readlink_("$from/\@ARCHIVE_SHARING");
1107 if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) {
1108 http_die("archive sharing is not allowed");
1109 }
1110
1111 http_header(
1112 '200 OK',
1113 'Content-Type: text/plain',
1114 );
1115 print "_\n";
1116 foreach (
1117 sort { lc $a cmp lc $b }
1118 grep { s:.*/(.+)/.*:$1: }
1119 glob "$from/SHARE/*/archives"
1120 ) {
1121 print "$_\n" unless /^_$/;
1122 }
1123 exit;
1124 }
1125
1126 # copy share archive
1127 if ($fulluser and
1128 $command =~ /^COPYARCHIVE:([\w-]+):([\w.+-]+):($vp\.\w+):(.+):(.+)/)
1129 {
1130 my $share = $1;
1131 my $archive = $2;
1132 my $avt = $3;
1133 my $nshare = $4;
1134 my $narchive = $5;
1135 my $shared = "$from/SHARE";
1136 my $sharing = readlink_("$from/\@ARCHIVE_SHARING");
1137 if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) {
1138 http_die("archive sharing is not allowed");
1139 }
1140 $nshare =~ s/[^\w-]/_/g;
1141 $narchive =~ s/[^\w.+-]/_/g;
1142 unless (-d "$shared/$share") {
1143 http_die("unknown share $share");
1144 }
1145 unless (-d "$shared/$nshare" or mkdir "$shared/$nshare") {
1146 http_die("cannot mkdir share $nshare - $!\n");
1147 }
1148 my $ad = "$shared/$share/archives/$archive/$avt";
1149 my $nad = "$shared/$nshare/archives/$narchive/$avt";
1150 if (-d $nad) {
1151 http_die("archive $nshare:$narchive:$avt does already exist\n");
1152 }
1153 mkdirp($nad);
1154 unless (-d $nad) {
1155 http_die("cannot create $nshare:$narchive:$avt - $!\n");
1156 }
1157 my $rr = `rsync -a --exclude data $ad $nad/../ 2>&1`;
1158 if ($rr =~ /\w/) {
1159 http_die($rr);
1160 }
1161 unlink "$nad/download","$nad/dkey";
1162 unless (link "$ad/data","$nad/data") {
1163 http_die("cannot link $nad/$ndata - $!\n");
1164 }
1165 if ($nshare eq '_') {
1166 my $dkey = randstring(8);
1167 symlink "../$nad","$dkeydir/$dkey" and
1168 symlink $dkey,"$nad/dkey";
1169 }
1170 faslog("$shared/$nshare","$from copied $share:$archive:$avt to $narchive");
1171 http_header(
1172 '200 OK',
1173 'Content-Type: text/plain',
1174 );
1175 print "$share:$archive:$avt copied to $nshare:$narchive\n";
1176 exit;
1177 }
1178
1179 if ($share and $command) {
1180 my $sx = '';
1181 my $access = '';
1182 my @list;
1183
1184 if ($from eq $to and $fulluser) {
1185 $access = 'owner';
1186 $sx = md5_hex("$1:$rid") if $id =~ /^MD5H:(\w+)/;
1187 } elsif ($pkey and $access = readlink("$shared/users/$from/access")) {
1188 if ($access eq 'manage' and $pkey =~ /^MD5H:(\w+)/) {
1189 $sx = md5_hex("$1:".readline1("$shared/users/$from/pkey"))
1190 }
1191 } else {
1192 http_die("no access");
1193 }
1194
1195 # list share with users and archives
1196 if ($command eq 'LISTSHARE') {
1197 http_header(
1198 '200 OK',
1199 'Content-Type: text/plain',
1200 "X-Access: $access",
1201 );
1202 print "access=$access\n";
1203 # list users
1204 if ($access eq 'owner' or $access eq 'manage') {
1205 print "$to:owner:-\n";
1206 foreach my $u (glob("$shared/users/*")) {
1207 if (-d $u) {
1208 my $up = readline1("$u/pkey") or next;
1209 my $ua = readlink_("$u/access");
1210 $up = unpack("H*",$up ^ $sx) if $sx;
1211 printf "%s:%s:%s\n",basename($u),$ua,$up;
1212 }
1213 }
1214 }
1215
1216 @list = ();
1217 # list writable archive sets
1218 if ($access ne 'read') {
1219 foreach my $a (glob("$shared/archives/*")) {
1220 if (-d $a) {
1221 push @list,sprintf("%s\n",basename($a));
1222 }
1223 }
1224 }
1225 print _sort(@list);
1226
1227 @list = ();
1228 $sx = substr($sx,0,8);
1229 # list available archives
1230 foreach my $a (glob("$shared/archives/*/*/data")) {
1231 my $utime = mtime($a);
1232 $a =~ s:/data$::;
1233 $a =~ m:.*/(.+)/(.+):;
1234 my $archive = $1;
1235 my $avt = $2;
1236 my $size = -s "$a/data";
1237 my $uploader = readlink("$a/uploader")||$to;
1238 my $comment = readline1("$a/comment")||'';
1239 my $dkey = readlink_("$a/dkey");
1240 my $xdkey = '';
1241 if ($id and $dkey and -l "$dkeydir/$dkey") {
1242 if ($sx) {
1243 $xdkey = unpack("H*",$dkey ^ $sx);
1244 } else {
1245 $xdkey = $dkey;
1246 }
1247 }
1248 push @list,sprintf(qq'%s %s %d %d %s "%s" %s\n',
1249 $archive,$avt,$utime,$size,$uploader,$comment,$xdkey);
1250 }
1251 print _sort(@list);
1252 exit;
1253 }
1254
1255 # add or modify share user
1256 if ($command =~ '^SHAREUSER:([^\s\/]+):([a-z]+)$') {
1257 my $suser = lc $1;
1258 my $access = $2;
1259 my $shared = "$to/SHARE/$share";
1260 if ($suser !~ /^\w[\w~!_.=+-]*\@[\w.-]+\.[a-z]+$/) {
1261 http_die("illegal user $suser");
1262 }
1263 unless ($from eq $to and $fulluser) {
1264 if (readlink_("$shared/users/$from/access") ne 'manage') {
1265 http_die("no manage access for $to/$share/$from");
1266 }
1267 if ($access eq 'manage') {
1268 http_die("you are not the owner and cannot set manage access right");
1269 }
1270 }
1271 my $suserd = "$shared/users/$suser";
1272 if ($access eq 'none') {
1273 rmrf($suserd);
1274 http_header(
1275 '200 OK',
1276 'Content-Type: text/plain',
1277 );
1278 print "$to/$share/$suser removed\n";
1279 faslog($shared,"$from removed user $suser");
1280 } else {
1281 mkdirp($suserd);
1282 mksymlink("$suserd/access",$access);
1283 my $pkey = '';
1284 my $pkf = "$suserd/pkey";
1285 if ($pkey = readline1($pkf)) {
1286 faslog($shared,"$from set access=$access for $suser");
1287 } else {
1288 $pkey = randstring(32);
1289 open $pkf,'>',$pkf or http_die("cannot create $pkf - $!\n");
1290 print {$pkf} $pkey,"\n";
1291 close $pkf;
1292 faslog($shared,"$from created $suser");
1293 }
1294 http_header(
1295 '200 OK',
1296 'Content-Type: text/plain',
1297 );
1298 $pkey = 'MD5H:'.unpack("H*",$pkey ^ $sx) if $sx;
1299 print "pkey=$pkey\n";
1300 }
1301 exit;
1302 }
1303
1304 http_die("unknown command $command");
1305 }
1306
1307 # quotas
1308 if ($fulluser and @to and not $flink and not $seek) {
1309
1310 # check sender quota
1311 my ($quota,$du) = check_sender_quota($muser||$from);
1312 if ($quota and $du+$cl/$MB > $quota) {
1313 if ($from and not $muser) {
1314 # remove aborted dox stream (stage 1 from fexdox)
1315 foreach my $ds (glob "$from/DOX/*!stream") {
1316 rmrf(untaint($ds));
1317 }
1318 }
1319 http_die("you are overquota");
1320 }
1321
1322 # check recipient quota
1323 foreach my $to (@to) {
1324 ($quota,$du) = check_recipient_quota($to);
1325 if ($quota and $du+$cl/$MB > $quota) {
1326 http_die("$to cannot receive files: is overquota");
1327 }
1328 }
1329
1330 }
1331
1332 # check recipients restriction
1333 if ($fulluser and @to and not $public) {
1334 check_rr($from,@to);
1335 }
1336
1337 # on secure mode "fop authorization" also check if recipient(s) exists
1338 # (= has a F*EX ID)
1339 if (not $addto and $fop_auth and $fulluser and @to) {
1340 my ($to_reg,$idf,$subuser);
1341 foreach my $to (my @loop = @to) {
1342 $to =~ s/:\w+=.*//; # remove options from address
1343 $to_reg = 0;
1344 # full user?
1345 if (open $idf,'<',"$to/@") {
1346 $to_reg = getline($idf);
1347 close $idf;
1348 }
1349 # sub user?
1350 elsif (open $idf,'<',"$from/\@SUBUSER") {
1351 while (<$idf>) {
1352 s/#.*//;
1353 next unless /:/;
1354 chomp;
1355 ($subuser) = split ':';
1356 if ($subuser eq $to or $subuser eq '*@*'
1357 or $subuser =~ /^\*\@(.+)/ and $to =~ /\@\Q$1\E$/i
1358 or $subuser =~ /(.+)\@\*$/ and $to =~ /^\Q$1\E\@/i) {
1359 $to_reg = $_;
1360 last;
1361 }
1362 }
1363 close $idf;
1364 }
1365 unless ($to_reg) {
1366 http_die("recipient $to is not a registered F*EX full or sub user");
1367 }
1368 }
1369 }
1370
1371 $to = join(',',@to);
1372
1373 if ($to =~ /^@(.+)/) {
1374 if ($nomail) {
1375 http_die("server runs in NOMAIL mode - groups ($to) are not allowed");
1376 }
1377 my $gf = "$from/\@GROUP/$1";
1378 if (open $gf,'<',$gf) {
1379 while (<$gf>) {
1380 s/#.*//;
1381 $keep = $1 if /^keep=(\d+)/i;
1382 $autodelete = $1 if /^autodelete=(yes|no)/i;
1383 push @group,$1 if /(.+@.+):/;
1384 }
1385 }
1386 close $gf;
1387 $group = $to;
1388 }
1389
1390 if ($redirect) {
1391 nvt_print(
1392 "HTTP/1.1 302 Found",
1393 "Location: $ENV{PROTO}://$ENV{HTTP_HOST}/$redirect",
1394 'Content-Length: 0',
1395 ""
1396 );
1397 # control back to fexsrv for further HTTP handling
1398 &reexec;
1399 }
1400
1401 # on successful login, delete faillog
1402 if ($fulluser and $faillog) {
1403 unlink $faillog;
1404 }
1405
1406 # display HTML form and request user data
1407 unless ($file) {
1408
1409 # if ($fulluser and $RA and $http_client !~ /^fex/) {
1410 # mkdir '.login' unless -d '.login';
1411 # unlink ".login/$RA";
1412 # symlink $fulluser,".login/$RA";
1413 # }
1414
1415 if ($test) { $cgi = $test }
1416 else { $cgi = $ENV{SCRIPT_NAME} }
1417 $cgi = 'fup';
1418
1419 # delete old cookies on logout referer
1420 my @cookies;
1421 if ($logout and my $cookie = $ENV{HTTP_COOKIE}) {
1422 while ($cookie =~ s/(\w+key)=\w+//) {
1423 push @cookies,"Set-Cookie: $1=x; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
1424 }
1425 }
1426
1427 if (($akey or $skey or $gkey) and $from and -d $from) {
1428 # save default locale for this user
1429 if (not $locale and ($ENV{HTTP_COOKIE}||'') =~ /\blocale=(\w+)/) {
1430 $locale = $1;
1431 }
1432 mksymlink("$from/\@LOCALE",$locale) if $locale;
1433 }
1434
1435 if (my $forward = $param{FORWARD} and $akey) {
1436 $forward =~ s:^/::;
1437 my $login = basename(readlink("$akeydir/$akey")||'');
1438 nvt_print(
1439 "HTTP/1.1 302 Found",
1440 "Location: /$forward",
1441 "Set-Cookie: locale=$locale",
1442 "Set-Cookie: akey=$akey; path=/; Max-Age=44444; Discard",
1443 "Set-Cookie: login=$login; path=/",
1444 'Expires: 0',
1445 'Content-Length: 0',
1446 ''
1447 );
1448 # control back to fexsrv for further HTTP handling
1449 &reexec;
1450 }
1451
1452 http_header('200 OK',@cookies);
1453 # print html_header($head,'<img src="/fex_small.gif">');
1454 print html_header($head);
1455
1456 if ($http_client =~ /(Konqueror|w3m)/) {
1457 pq(qq(
1458 '<p><hr><p>'
1459 '<center>'
1460 '<h3>Your client seems to be "$1" which is incompatible with F*EX and will probably not work!</h3>'
1461 'We recommend firefox or google chrome.'
1462 '</center>'
1463 '<p><hr><p>'
1464 ));
1465 }
1466
1467 # default "fex yourself" setting?
1468 if ($fulluser and not $addto
1469 and not ($gkey or $skey or $okey or $public or $anonymous)
1470 and (not @to or "@to" eq $from)
1471 and -f "$from/\@FEXYOURSELF")
1472 {
1473 @to = ($from);
1474 $nomail = 'fexyourself';
1475 }
1476
1477 # ask for recipient address(es)
1478 elsif ($fulluser and ($addto or not $submit or not @to)
1479 and not ($gkey||$skey||$okey||$ukey||$public||$anonymous))
1480 {
1481 present_locales('/fup');
1482
1483 # print "[$addto] [$submit] [@to]<p>\n";
1484
1485 @ab = ("<option></option>");
1486
1487 # select menu from server address book
1488 if (open my $AB,'<',"$from/\@ADDRESS_BOOK") {
1489 while (<$AB>) {
1490 s/#.*//g;
1491 if (/(\S+)[=\s]+(\S+@[\w.-]+\S*)/) {
1492 $_ = "$1 &lt;$2>";
1493 s/,.*/,.../g;
1494 s/:.*/>/;
1495 push @ab,"<option>$_</option>";
1496 }
1497 }
1498 close $AB;
1499 }
1500
1501 unless (@to) {
1502 unless ($nomail) {
1503 foreach (glob "$from/\@GROUP/*") {
1504 if (-f and not -l) {
1505 s:.*/::;
1506 push @ab,"<option>\@$_</option>" unless /~$/;
1507 }
1508 }
1509 }
1510 }
1511
1512 my $ab64 = b64("from=$from&id=$id");
1513 # '<form class="uploadform" name="upload"'
1514 pq(qq(
1515 '<form name="upload" action="/fup" $formpara>'
1516 ' <input type="hidden" name="from" value="$from">'
1517 ' <input type="hidden" name="id" value="$id">'
1518 ' <table border="1">'
1519 ' <tr><td>sender: <td><a href="/foc">$from</a></tr>'
1520 ' <tr title="email address or alias"><td>recipient(s):'
1521 ' <td><input type="text" name="to" size="96" value="$to"><br>'
1522 ));
1523 if (grep /@/,@ab) {
1524 pq(qq(
1525 ' or select from your address book:'
1526 ' <select name="addto" size="1">@ab</select>'
1527 ' and'
1528 ' <input type="submit" name="addsubmit" value="add to recipients list">'
1529 ));
1530 }
1531 pq(qq(
1532 ' </tr>'
1533 ' </table>'
1534 ' <p>'
1535 ));
1536 my $rr = "$from/\@ALLOWED_RECIPIENTS";
1537 if (-s $rr and open $rr,'<',$rr) {
1538 pq(qq(
1539 'You are a restricted user and may only fex to these recipients:<p>'
1540 '<pre>'
1541 ));
1542 while (<$rr>) {
1543 chomp;
1544 s/#.*//;
1545 s/\s//g;
1546 next unless $_;
1547 if (/^\@LOCAL_RDOMAINS/) {
1548 foreach my $rd (@local_rdomains) {
1549 print "*\@$rd\n";
1550 }
1551 } elsif (/^\@LOCAL_USERS/) {
1552 foreach (glob "*/@") {
1553 s:/.::;
1554 print "$_\n";
1555 }
1556 } else {
1557 print "$_\n";
1558 }
1559 }
1560 print "</pre><p>\n";
1561 close $rr;
1562 }
1563 print qq' <input type="submit" name="submit" value="check recipient(s) and continue">';
1564 if ($fex_yourself =~ /^yes|1/i) {
1565 print qq' or <input type="submit" name="fexyourself" value="fex yourself">'
1566 }
1567 print "\n</form>\n";
1568 if ($akey and -f "$from/@" and not ($captive or $ukey)) {
1569 pq(qq(
1570 '<p>'
1571 '<a href="/foc">user config & operation control</a>'
1572 ));
1573 }
1574
1575 if ($from eq $admin ) {
1576 pq(qq(
1577 '<p>'
1578 '<a href="/fac">server config & admin control</a>'
1579 ));
1580 }
1581
1582 print &logout;
1583 pq(qq(
1584 '<p><hr><p>'
1585 '<b>'
1586 'Warning: the recipient must not be a mailing list,'
1587 'because after download the file will be no more available!'
1588 '</b><br>'
1589 'Contact <a href="mailto:$ENV{SERVER_ADMIN}">fexmaster</a> if you want to fex to a mailing list,'
1590 'he can allow multiple downloads for specific addresses.'
1591 '<p>'
1592 'Use a <a href="/tools.html">F*EX client</a> if you want to send more than one file or resume an interrupted upload.'
1593 '</body></html>'
1594 '<p>'
1595 ));
1596 print $info_1;
1597 exit;
1598 }
1599
1600 # ask for filename
1601 if ($from and ($id or $okey or $pkey)) {
1602 $to = $group if $group;
1603 present_locales($ENV{REQUEST_URI}) if $skey or $gkey;
1604 pq(qq(
1605 '<script>'
1606 ' function showstatus() {'
1607 ' var file = document.forms["upload"].elements["file"].value;'
1608 ' if (file == "") return false;'
1609 ' window.open('
1610 " '/$cgi?showstatus=$uid',"
1611 " 'fup_status',"
1612 " 'width=700,height=500'"
1613 ' );'
1614 ' return true;'
1615 ' }'
1616 ''
1617 ' function checkupload() {'
1618 ' var file = document.forms["upload"].elements["file"].value;'
1619 ' if (file == "") { alert("No file selected") }'
1620 ' }'
1621 ''
1622 ' function reportsize() {'
1623 ' var form = document.forms["upload"];'
1624 ' var filesize = form.file.files[0].size;'
1625 ' // alert(filesize + " bytes");'
1626 ' form.elements["filesize"].value = filesize;'
1627 ' filesize = filesize.toString();'
1628 ' filesize = filesize.replace(/(\\d)(?=(\\d\\d\\d)+(?!\\d))/g,"\$1,");'
1629 ' document.getElementById("filesize").innerHTML = filesize + " bytes";'
1630 ' }'
1631 '</script>'
1632 ));
1633 pq(qq(
1634 '<form name="upload"'
1635 ' action="/fup"'
1636 ' onsubmit="return showstatus()"'
1637 ' $formpara>'
1638 ' <input type="hidden" name="uid" value="$uid">'
1639 ' <input type="hidden" name="filesize" value="">'
1640 ));
1641
1642 if ($ukey) {
1643 pq(qq(
1644 ' <input type="hidden" name="user" value="$from">'
1645 ));
1646 } else {
1647 pq(qq(
1648 ' <input type="hidden" name="from" value="$from">'
1649 ));
1650 }
1651
1652 if ($public) {
1653 my $toh = join('<br>',@to);
1654 pq(qq(
1655 ' <input type="hidden" name="sender" value="$from">'
1656 ' <input type="hidden" name="id" value="$public">'
1657 ' <input type="hidden" name="to" value="$to">'
1658 ' <table border="1">'
1659 ' <tr><td>sender: <td>$from</tr>'
1660 ' <tr><td>recipient:<td>$toh</tr>'
1661 ));
1662 } elsif ($okey) {
1663 pq(qq(
1664 ' <input type="hidden" name="okey" value="$okey">'
1665 ' <input type="hidden" name="to" value="$to">'
1666 ' <table border="1">'
1667 ' <tr><td>sender: <td>$from</tr>'
1668 ' <tr><td>recipient:<td>$to</tr>'
1669 ));
1670 } elsif ($skey) {
1671 pq(qq(
1672 ' <input type="hidden" name="skey" value="$skey">'
1673 ' <table border="1">'
1674 ' <tr><td>sender: <td>$from</tr>'
1675 ' <tr><td>recipient:<td>$to</tr>'
1676 ));
1677 } elsif ($ukey) {
1678 $from = '' if $from eq $to;
1679 pq(qq(
1680 ' <input type="hidden" name="ukey" value="$ukey">'
1681 ' <table border="1">'
1682 ' <tr><td>sender: <td><input type="text" name="from" value="$from" size="80"></tr>'
1683 ' <tr><td>recipient:<td>$to</tr>'
1684 ));
1685 } elsif ($pkey) {
1686 pq(qq(
1687 ' <input type="hidden" name="pkey" value="$pkey">'
1688 ' <input type="hidden" name="to" value="$to">'
1689 ' <table border="1">'
1690 ' <tr><td>sender: <td>$from</tr>'
1691 ' <tr><td>recipient:<td>$to</tr>'
1692 ));
1693 } elsif (@group) {
1694 if ($gkey) {
1695 pq(qq(
1696 ' <input type="hidden" name="gkey" value="$gkey">'
1697 ' <input type="hidden" name="to" value="_">'
1698 ));
1699 }
1700 my $toh = "group $group:<ul>";
1701 foreach my $gm (@group) { $toh .= "<li>$gm" }
1702 $toh .= "</ul>";
1703 pq(qq(
1704 ' <input type="hidden" name="id" value="$id">'
1705 ' <table border="1">'
1706 ' <tr><td>sender:<td>$from</tr>'
1707 ' <tr><td>recipient(s):<td>$toh</tr>'
1708 ));
1709 } else {
1710 my $toc = join(',',@to);
1711 my $toh = join('<br>',@to);
1712 pq(qq(
1713 ' <input type="hidden" name="akey" value="$akey">'
1714 ' <table border="1">'
1715 ' <tr><td>sender:<td><a href="/foc">$from</a></tr>'
1716 ));
1717 if ($anonymous) {
1718 pq(qq(
1719 ' <tr><td>recipient:'
1720 ' <td><input type="hidden" name="to" value="$toc">$toh</tr>'
1721 ));
1722 } else {
1723 pq(qq(
1724 ' <tr><td><a href="/fup?to=$toc">recipient(s)</a>:'
1725 ' <td><input type="hidden" name="to" value="$toc">$toh</tr>'
1726 ));
1727 }
1728 }
1729
1730 $autodelete = lc $autodelete;
1731 $keep = $keep_default unless $keep;
1732 my ($quota,$du) = check_sender_quota($muser||$from);
1733 $quota = $quota
1734 ? "<tr><td>sender quota (used):<td>$quota ($du) MB</tr>"
1735 : '';
1736
1737 $bwl = qq'<input type="text" name="bwlimit" size="8" value="$bwlimit"> kB/s';
1738 if (@throttle) {
1739 foreach (@throttle) {
1740 if (/\[?(.+?)\]?:(\d+)$/) {
1741 my $throttle = $1;
1742 my $limit = $2;
1743 # throttle ip address?
1744 if ($throttle =~ /^[\w:.-]+$/) {
1745 if (ipin($RA,$throttle)) {
1746 $bwl = qq'<input type="hidden" name="bwlimit" value="$limit"> $limit kB/s';
1747 last;
1748 }
1749 }
1750 # throttle email address?
1751 else {
1752 # allow wildcard *, but not regexps
1753 $throttle =~ quotemeta $throttle;
1754 $throttle =~ s/\*/.*/g;
1755 if ($from =~ /^$throttle$/i) {
1756 $bwl = qq'<input type="hidden" name="bwlimit" value="$limit"> $limit kB/s';
1757 last;
1758 }
1759 }
1760 }
1761 }
1762 }
1763
1764 $autodelete = $autodelete{$to} if $autodelete{$to};
1765
1766 my $adt = '';
1767 for ($autodelete) {
1768 if (/yes/i) { $adt = 'delete file after download' }
1769 elsif (/no/i) { $adt = 'do not delete file after download' }
1770 elsif (/delay/i) { $adt = 'delete file after download with delay' }
1771 elsif (/^\d+$/) { $adt = "delete file $autodelete days after download" }
1772 }
1773 $adt .= qq'<input type="hidden" name="autodelete" value="$autodelete">';
1774
1775 my $ctr;
1776 if ($nomail) {
1777 $ctr = qq'<em>no notification email will be send</em>';
1778 } else {
1779 $ctr = qq'<input type="text" name="comment" size="80" value="$comment">';
1780 }
1781 my $kos;
1782 if ($share) {
1783 $kos = qq'<tr><td>share:<td>$share'.
1784 qq'<input type="hidden" name="share" value="$share"></tr>';
1785 if ($archive) {
1786 $kos .= qq'\n <tr><td>archive set:<td>$archive'.
1787 qq'<input type="hidden" name="archive" value="$archive"></tr>';
1788 }
1789 } else {
1790 $kos = qq'<tr title="keep file max $keep days, then delete it">'
1791 . qq'<td>keep:<td>$keep days'
1792 . qq'<input type="hidden" name="keep" value="$keep"></tr>';
1793 }
1794 pq(qq(
1795 ' <tr><td>autodelete:'
1796 ' <td>$adt'
1797 ' </tr>'
1798 ' $kos'
1799 ' $quota'
1800 ' <tr title="optional, full speed if empty"><td>bandwith limit:'
1801 ' <td>$bwl'
1802 ' </tr>'
1803 ' <tr title="optional, will be included in notification email"><td>comment:'
1804 ' <td>$ctr'
1805 ' </tr>'
1806 ' <tr title="If you want to send more than one file, then put them in a zip or tar archive"><td>file:'
1807 ' <td><input type="file" name="file" size="80" value="$file" onchange="reportsize()">'
1808 ' </tr>'
1809 ' <tr><td>file size:<td id="filesize"></td></tr>'
1810 ' </table>'
1811 ' <p>'
1812 ' <input type="submit" value="upload" onclick="checkupload()">'
1813 '<p>'
1814 '</form>'
1815 ));
1816 if ($share) {
1817 my $fas = '/fas';
1818 if ($pkey) {
1819 $fas .=
1820 "?owner=$to".
1821 "?share=$share".
1822 "?user=$from".
1823 "?pkey=$pkey".
1824 "?show=archive:$archive";
1825 } else {
1826 $fas .= "?show=archive:$share:$archive";
1827 }
1828 pq(qq(
1829 '<p>'
1830 '<a href="$back">back to file sharing</a>'
1831 ));
1832 }
1833 if ($akey and -f "$from/@" and not ($captive or $ukey)) {
1834 pq(qq(
1835 '<p>'
1836 '<a href=\"/foc\">user config & operation control</a>'
1837 # '<form method="post" action="/foc">'
1838 # '<input type="submit" value="user config & operation control">'
1839 # '</form>'
1840 ));
1841 }
1842 if ($from eq $admin) {
1843 pq(qq(
1844 '<p>'
1845 '<a href="/fac">server config & admin control</a>'
1846 ));
1847 }
1848 print &logout unless $ukey;
1849 print $info_2;
1850 # printf "<hr><pre>%s</pre>\n",$ENV{HTTP_HEADER};
1851 print "</body></html>\n";
1852 exit;
1853 }
1854
1855 present_locales('/fup');
1856
1857 my $lost = '';
1858 if ($ENV{REQUEST_METHOD} eq 'POST') {
1859 if ($from and -f "$from/@") {
1860 $lost = '(I have lost my auth-ID! ';
1861 if ($encryption or -f "$from/@@") {
1862 $lost .= qq'<a href="/fup?id_forgotten=$from">Send a reset email</a> to $from!\n';
1863 } else {
1864 $lost .= qq'<a href="/fup?from=$from&id=?">Mail it</a> to $from!';
1865 }
1866 $lost .= ")";
1867 } else {
1868 pq(qq(
1869 '<font color="red">'
1870 '<h3>You have to fill out this form completely to continue.</h3>'
1871 '</font>'
1872 ));
1873 }
1874 }
1875
1876 unless (length($from)) {
1877 if (($ENV{HTTP_COOKIE}||'') =~ /\blogin=([^\s\/]+@[a-z\d.-]+)/i and -f "$1/@") {
1878 $from = $1;
1879 } else {
1880 # $from = readlink(".login/$RA")||'';
1881 }
1882 }
1883
1884 pq(qq(
1885 '<form action="/fup" $formpara>'
1886 ' <table>'
1887 ' <tr><td>sender:'
1888 ' <td><input type="text" name="from" size="40" value="$from"></tr>'
1889 ' <tr><td>auth-ID:'
1890 ' <td><input type="password" name="id" size="16" autocomplete="off"> $lost</tr>'
1891 ' </table>'
1892 ));
1893 if ($ENV{QUERY_STRING} =~ /forward=(\S+)/) {
1894 print qq' <input type="hidden" name="forward" value="$1">\n';
1895 }
1896 pq(qq(
1897 ' <p><input type="submit" value="check ID and continue"><p>'
1898 ));
1899 if ($mail_authid and not ($fop_auth or $nomail)) {
1900 unless ($from) {
1901 pq(qq(
1902 'If you have lost your auth-ID,'
1903 'enter your email address in the sender field above and'
1904 '<input type="submit"><p>'
1905 ));
1906 }
1907 }
1908 if (not $nomail and (
1909 @local_domains and @local_hosts or
1910 @local_rdomains and @local_rhosts or
1911 @demo
1912 )) {
1913 pq(qq(
1914 'You can <a href="/fur">register yourself</a> '
1915 'if you do not have a F*EX account yet.<p>'
1916 ));
1917 }
1918 if (@anonymous_upload and ipin($RA,@anonymous_upload)) {
1919 my $a = 'anonymous_'.int(rand(999999));
1920 pq(qq(
1921 'You may also use <a href="/fup?from=anonymous&to=$a">anonymous upload</a>'
1922 ));
1923 }
1924 if (-f "$docdir/sup.html") {
1925 pq(qq(
1926 '<br>'
1927 'You may also use <a href="/sup.html">simple upload</a>'
1928 ));
1929 }
1930 print "</form><p>\n";
1931
1932 print $info_login||$info_1;
1933
1934 if ($debug and $debug>1) {
1935 print "<hr>\n<pre>\n";
1936 foreach $v (sort keys %ENV) {
1937 print "$v = $ENV{$v}\n";
1938 }
1939 print "</pre>\n";
1940 }
1941
1942 print "</body></html>\n";
1943 exit;
1944 }
1945
1946 # from sup.html
1947 if ($from and $file and not @to) {
1948 check_rr($from,$from);
1949 @to = ($from);
1950 $sup = 'fexyourself';
1951 $keep{$from} = readlink("$from/\@KEEP")||$keep_default;
1952 }
1953
1954 # all these variables should be defined here, but just to be sure...
1955 http_die("no file specified") unless $file;
1956 http_die("no sender specified") unless $from;
1957 http_die("no recipient specified") unless @to;
1958 unless ($okey and -l "$to/\@OKEY/$okey" or $pkey) {
1959 http_die("no auth-ID specified") unless $id;
1960 unless ($id eq sidhash($rid,$id) or $gkey or $skey or $ukey) {
1961 faillog("user $from, id $id");
1962 http_die("wrong auth-ID specified");
1963 }
1964 }
1965
1966 &check_status($from);
1967
1968 if (@throttle) {
1969 foreach (@throttle) {
1970 if (/(.+):(\d+)$/) {
1971 my $throttle = $1;
1972 my $limit = $2;
1973 if (not $bwlimit or $limit < $bwlimit) {
1974 # throttle ip address?
1975 if ($throttle =~ /^[\d.-]+$/) {
1976 if (ipin($RA,$throttle)) {
1977 $bwlimit = $limit;
1978 last;
1979 }
1980 }
1981 # throttle email address?
1982 else {
1983 # allow wildcard *, but not regexps
1984 $throttle =~ quotemeta $throttle;
1985 $throttle =~ s/\*/.*/g;
1986 if ($from =~ /^$throttle$/i) {
1987 $bwlimit = $limit;
1988 last;
1989 }
1990 }
1991 }
1992 }
1993 }
1994 }
1995
1996 # address rewriting for storage (swap sender and recipient), see also fop!
1997 if (not ($skey or $gkey) and $from =~ /^(anonymous|fexmail)/) {
1998 ($from,@to) = ("@to",$from);
1999 }
2000
2001 if (not $anonymous and $overwrite =~ /^n/i) {
2002 foreach $to (@to) {
2003 if (-f "$to/$from/$fkey/data") {
2004 http_die("<code>$file</code> already exists for $to");
2005 }
2006 }
2007 }
2008
2009 # additional last check
2010 unless (@group or $gkey or $skey or $public or $okey) {
2011 foreach $to (@to) {
2012 checkaddress($to) or
2013 http_die("$to is not a valid email address");
2014 }
2015 }
2016
2017
2018 $to = join(',',@to);
2019
2020 # file overwriting for anonymous is only possible if his client has the
2021 # download cookie - else request purging
2022 if ($anonymous and not $seek and my $dkey = readlink("$to/$from/$fkey/dkey")) {
2023 if ($overwrite =~ /^n/i) {
2024 http_die("<code>$file</code> already exists for $to");
2025 }
2026 if ($ENV{HTTP_COOKIE} !~ /$dkey/) {
2027 my $purge = "/fop/$dkey/$dkey?purge";
2028 # http_die("$file already exists $dkey:$ENV{HTTP_COOKIE}:");
2029 http_die("<code>$file</code> already exists - <a href=\"$purge\">purge it?!</a>");
2030 }
2031 }
2032
2033 if (@group) {
2034 @to = @group;
2035 $comment = "[$group] $comment";
2036 } elsif ($public) {
2037 $comment .= ' (public upload)';
2038 } elsif ($ukey and $ukey !~ /_$/) {
2039 $comment = "[ukey=$ukey] $comment";
2040 }
2041
2042 # file data still waits on STDIN ... get it now!
2043 &get_file;
2044
2045 if ($to eq $from and $file eq 'ADDRESS_BOOK') {
2046 unlink "$from/\@ADDRESS_BOOK";
2047 rename "$from/$from/ADDRESS_BOOK/upload","$from/\@ADDRESS_BOOK"
2048 or http_die("cannot save $from/\@ADDRESS_BOOK - $!\n");
2049 http_header('200 OK');
2050 print html_header($head);
2051 print "address book updated\n";
2052 print "</body></html>\n";
2053 exit;
2054 }
2055
2056 my $doxua = readlink_("$from/\@DOCUMENT_EXCHANGE");
2057 if ($doxua eq 'yes' or $doxua eq '' and $document_exchange) {
2058 $doxua = 1;
2059 } else {
2060 $doxua = 0;
2061 }
2062
2063 # finalize upload
2064 unless ($nostore) {
2065
2066 if ($shared) {
2067 my $archived = "$shared/archives/$archive";
2068 $filed = "$archived/$avt";
2069 $save = "$filed/data";
2070 $upload = "$filed/upload";
2071 $download = "$filed/download";
2072 unlink $save,$download;
2073 # need archiving?
2074 if ($azip) {
2075 my $tmp = "$filed/tmp";
2076 mkdir $tmp;
2077 unless (rename $upload,"$tmp/$azip") {
2078 http_die("cannot create $tmp/$azip - $!\n");
2079 }
2080 system qw'zip -q -0 -j',"$save.zip","$tmp/$azip";
2081 if (not rename("$save.zip",$save) or -s $save < -s "$tmp/$azip") {
2082 http_die("cannot zip upload - $!\n");
2083 }
2084 if (open my $filename,'>',"$filed/filename") {
2085 print {$filename} sprintf("%s_%s",$archive,$avt);
2086 close $filename;
2087 }
2088 rmrf($tmp);
2089 } else {
2090 rename($upload,$save) or http_die("cannot move upload to data - $!\n");
2091 }
2092
2093 faslog($shared,"$from uploaded file $archive:$avt");
2094
2095 if ($share eq '_' and $dkey = readlink("$filed/dkey")) {
2096 # log dkey
2097 my $msg = sprintf "%s %s %s %s _/%s/%s\n",
2098 isodate(time),$dkey,$from,$from,$archive,$avt;
2099 writelog('dkey.log',$msg);
2100 }
2101
2102 # delete old versions from this uploader
2103 if (my $mv = readlink("$archived/versions")||readlink("$shared/versions")) {
2104 my @versions;
2105 foreach my $v (glob("$archived/*_*")) {
2106 if (readlink_("$v/uploader") eq $from and -f "$v/data") {
2107 push @versions,untaint($v);
2108 }
2109 }
2110 if (@versions) {
2111 while (scalar(@versions) > $mv) {
2112 rmrf(shift @versions);
2113 }
2114 }
2115 }
2116
2117 # send notification emails
2118 my $version = $1 if $avt =~ /([\d_]+)/;
2119 my $file = sprintf "%s_%s",$archive,$avt;
2120 my ($key,$to,$durl);
2121
2122 foreach my $user (glob "$shared/users/*") {
2123 if (readlink_("$user/notification") eq 'yes') {
2124 $to = basename($user);
2125 if ($to eq $owner) {
2126 $key = md5_hex(sprintf("%s:%s",$file,readline1("$owner/@")));
2127 } else {
2128 next unless -f "$user/pkey";
2129 $key = md5_hex(sprintf("%s:%s",$file,readline1("$user/pkey")));
2130 }
2131 $durl = sprintf "%s/%s/%s/%s/%s/%s",
2132 $::durl,$owner,$share,$to,$key,$file;
2133 notify(
2134 share => $share,
2135 to => $to,
2136 from => $from,
2137 filename => "$archive:$version",
2138 filed => $filed,
2139 durl => $durl,
2140 comment => $comment,
2141 );
2142 debuglog("notify $to $filed");
2143 }
2144 }
2145
2146 } else {
2147
2148 if ($mup) {
2149 # directory MIME multipart upload postprocessing
2150 local $_;
2151 local $/ = "\n";
2152 my ($file,$size,$tmp,$zip,$x);
2153 my $to = $group[0]||$to[0];
2154 $to =~ s/:\w+=.*//; # remove options from address
2155 $filed = "$spooldir/$to/$from/$fkey"; # must be abspath!
2156 $upload = "$filed/upload";
2157 $tmp = "$filed/tmp";
2158 $zip = "$upload.zip";
2159 open $upload,$upload or http_die("cannot open $upload - $!\n");
2160 http_header('200 OK','Connection: close');
2161 print html_header($head);
2162 print $js_scroll;
2163 $_ = slurp("$filed/header") or http_die("no header file found");
2164 $file = mupfile($filed,$_); # files go to $tmp/
2165 print "<h3>extracting:</h3>\n";
2166 print "<pre>\n";
2167 print htmlencode($1),"\n" if $file =~ m:/tmp/(.+):;
2168 while (<$upload>) {
2169 if (/^--\Q$boundary/) {
2170 close $file;
2171 truncate $file,(-s $file)-2;
2172 $header = '';
2173 while (<$upload>) {
2174 last if /^\r?\n/;
2175 s/\r//;
2176 $header .= $_;
2177 }
2178 $file = mupfile($filed,$header);
2179 if ($file =~ m:/tmp/(.+):) {
2180 print $scroll,htmlencode($1),"\n";
2181 }
2182 } else {
2183 print {$file} $_;
2184 }
2185 }
2186 print "</pre>\n";
2187 close $file;
2188 close $upload;
2189 unless (-d $tmp) { die("no files in upload") }
2190
2191 # DOX directory upload
2192 if ($doxua and $comment =~ /^DOX:SAVE=./) {
2193
2194 if ($from eq "@to") {
2195 if ($comment eq 'DOX:SAVE=/') {
2196 # toplevel dox
2197 my $doxd = "$spooldir/$from/DOX";
2198 mkdirp($doxd);
2199 chdir $tmp or die("no $tmp ?!");
2200 foreach (glob "*") {
2201 my $a = my $b = untaint($_);
2202 $b =~ s/[^a-z\d\@~^,.+-]/_/gi;
2203 rename $a,$b;
2204 }
2205 foreach (glob "*") {
2206 next if -l;
2207 next unless -d;
2208 my $dir = untaint($_);
2209 my $doxdd = "$doxd/$dir";
2210 unless (-d $doxdd) {
2211 my @d = localtime time;
2212 my $dt = sprintf(
2213 '_%d%02d%02d_%02d%02d%02d',
2214 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
2215 );
2216 mkdirp("$doxdd$dt");
2217 symlink basename("$doxdd$dt"),$doxdd
2218 or die("cannot symlink $doxdd - $!");
2219 }
2220 $_ = `rsync -a $dir/. $doxdd/ 2>&1`;
2221 if ($? != 0) { die($_) }
2222 doxunweed($doxdd);
2223 }
2224 my $dox = 'dox';
2225 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2226 print qq'<a href="/$dox">back to DOX</a>\n';
2227 } elsif ($comment =~ /^DOX:SAVE=(.+)/) {
2228 # decode_utf8() does not work because of internal UTF-8 flag!
2229 my $dir = '/'.decode('utf-8',urldecode($1));
2230 $dir =~ s://+:/:g;
2231 while ($dir =~ s:/\.\./:/__/:) {}
2232 $dir =~ s:^/::;
2233 my $doxd = "$spooldir/$from/DOX/$dir";
2234 unless (-d $doxd) { die("no such DOX directory /$dir") }
2235 chdir $tmp or die("no $tmp ?!");
2236 my @files = ();
2237 foreach (glob '*') {
2238 foreach (find($_)) {
2239 push @files,"/$_" if -f and not -l;
2240 }
2241 }
2242 doxlog($from,$doxd,@files);
2243 @files = map { /(.+)/;$_=$1 } glob '*';
2244 if (my $errors = move(@files,$doxd)) {
2245 print "<h3>move error</h3>\n";
2246 print htmlquote($errors);
2247 }
2248 if ($dir =~ m:([^/]+):) {
2249 my $folder = $1;
2250 update_dox_folder($folder) ;
2251 doxunweed($folder);
2252 }
2253 my $dox = 'dox';
2254 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2255 my $href = urlencode(encode_utf8("/$dox/$from/$dir"));
2256 print qq'<a href="$href">back to DOX</a>\n';
2257 }
2258 print $scroll,"\n";
2259 print "</body></html>\n";
2260 chdir $spooldir;
2261 rmrf($tmp);
2262 sleep 3; # wait for showupload to finish
2263 unlink $upload;
2264 doxdu($from);
2265 exit;
2266 }
2267
2268 # user dox upload (mup)
2269 if ($comment =~ m:SAVE=([^/]+)/\Q.upload/$from\E(.*):) {
2270 my $folder = $1;
2271 my $dir = '/'.urldecode($2);
2272 $dir =~ s://+:/:g;
2273 while ($dir =~ s:/\.\./:/__/:) {}
2274 $dir =~ s:^/+::;
2275 my $doxd = "$spooldir/$to/DOX/$folder/.upload/$from/$dir";
2276 if (-d $doxd) {
2277 chdir $tmp or die("no $tmp ?!");
2278 my @files = ();
2279 foreach (glob '*') {
2280 foreach (find($_)) {
2281 push @files,".upload/$from/$dir/$_" if -f and not -l;
2282 }
2283 }
2284 doxlog($from,$doxd,@files);
2285 # rrrx("[\n\r]",$tmp);
2286 rmrx("^[.#]",$tmp);
2287 @files = map { /(.+)/;$_=$1 } glob '*';
2288 if (my $errors = move(@files,$doxd)) {
2289 print "<h3>move error</h3>\n";
2290 print htmlquote($errors);
2291 }
2292 doxunweed($doxd);
2293 my $dox = 'dox';
2294 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2295 my $href = urlencode("/$dox/$to/$folder/UPLOAD/$dir");
2296 $href =~ s:/+:/:g;
2297 print qq'<a href="$href">back to DOX</a>\n';
2298 print $scroll,"\n";
2299 print "</body></html>\n";
2300 chdir $spooldir;
2301 rmrf($tmp);
2302 sleep 3; # wait for showupload to finish
2303 unlink $upload;
2304 doxdu($to);
2305 } else {
2306 print "<h2>ERROR</h2>\n";
2307 print "No $doxd\n";
2308 print "</body></html>\n";
2309 }
2310 exit;
2311 }
2312 }
2313
2314 # regular fex (not dox)
2315 unlink $zip;
2316 open my $z,"cd '$tmp' && zip -0 -vr '$zip' * 2>&1|"
2317 or http_die("cannot run zip - $!");
2318 print "<h3 id=\"zipping\">zipping</h3>\n";
2319 print "$scroll\n";
2320 my $zipping = '';
2321 my $zipping_ = '';
2322 while (<$z>) {
2323 if (/ \(out=(\d+)\) /) {
2324 $size += $1;
2325 $zipping = sprintf 'zipping %d MB',int($size/$MB);
2326 next if $zipping eq $zipping_;
2327 print qq'<script>';
2328 print qq'document.getElementById("zipping").innerHTML = "$zipping"';
2329 print qq'</script>\n';
2330 $zipping_ = $zipping;
2331 }
2332 }
2333 close $z;
2334 open $upload,'>>',$upload or die "cannot rewrite $upload - $!\n";
2335 seek $upload,0,0;
2336 truncate $upload,0;
2337 open $zip,$zip or die "cannot open $zip - $!\n";
2338 print {$upload} $x while read($zip,$x,$bs);
2339 close $upload;
2340 close $zip;
2341 unlink $zip;
2342 rmrf($tmp);
2343 }
2344
2345 # DOX toplevel zip upload
2346 if ($doxua and $comment eq 'DOX:SAVE=/' and $from eq "@to") {
2347 if ($fkey =~ /(.+)\.zip$/) {
2348 my $dir = $1;
2349 $dir =~ s:[^\w\@%~^.,+-]:_:g;
2350 my $upload = "$spooldir/$from/$from/$fkey/upload";
2351 my $doxd = "$spooldir/$from/DOX";
2352 mkdirp($doxd);
2353 chdir $doxd or http_die("$doxd - $!");
2354 unlink $dir; # ==> force new version
2355 mkdirp($dir);
2356 chdir $dir or http_die("$dir - $!");
2357 $_ = `unzip -l $upload 2>&1`;
2358 if (m:/\.\./|\d\d\s+/:) { http_die("bad zip") }
2359 open my $z,"unzip -o $upload 2>&1|" or http_die("cannot run unzip - $!");
2360 http_header('200 OK','Connection: close');
2361 print html_header($head);
2362 print $js_scroll;
2363 pq(qq(
2364 '<h3>extracting $fkey:</h3>'
2365 '<pre>'
2366 ));
2367 while (<$z>) {
2368 next if /^Archive:/;
2369 chomp;
2370 s/^\s*\w+://;
2371 print $scroll,htmlencode($_),"\n";
2372 }
2373 close $z;
2374 print "</pre>\n";
2375 # rrrx("[\n\r]","$doxd/$dir");
2376 my @x = glob "*";
2377 my $x = untaint("@x");
2378 # does it contain a single directory with same name as zip file?
2379 if ($x eq $dir and -d $x) {
2380 # then use this directory as folder
2381 rename $x,"../.$dir" or die "cannot rename $dir - $!\n";
2382 chdir $doxd;
2383 rmrf($dir);
2384 rename ".$dir",$dir or die "cannot rename .$dir - $!\n";
2385 }
2386 my @d = localtime time;
2387 my $dt = sprintf(
2388 '_%d%02d%02d_%02d%02d%02d',
2389 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
2390 );
2391 chdir $doxd;
2392 rename $dir,"$dir$dt" or die("cannot rename $dir to $dir$dt - $!");
2393 symlink "$dir$dt",$dir or die("cannot symlink $dir - $!");
2394 doxunweed($dir);
2395 my $dox = 'dox';
2396 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2397 pq(qq(
2398 '<a href="/$dox">back to DOX</a>'
2399 '$scroll'
2400 '</body></html>'
2401 ));
2402 system qw'chmod -R u+rwX .';
2403 system qw'chmod -R go+rX-w .';
2404 sleep 3; # wait for showupload to finish
2405 chdir $spooldir;
2406 unlink $upload;
2407 } else {
2408 http_die("$fkey is not a zip file");
2409 }
2410 doxdu($from);
2411 exit;
2412 }
2413
2414 # single file DOX upload
2415 if ($doxua and $comment =~ /^DOX:SAVE=(.+)/) {
2416 # decode_utf8() does not work because of internal UTF-8 flag!
2417 my $dir = '/'.decode('utf-8',urldecode($1));
2418 $dir =~ s://+:/:g;
2419 while ($dir =~ s:/\.\./:/__/:) {}
2420 $dir =~ s:^/+::;
2421 my $filed = "$spooldir/$to/$from/$fkey";
2422 my $upload = "$filed/upload";
2423 my $folder = $dir;
2424 $folder =~ s:/.*::;
2425
2426 if ($from eq "@to") {
2427 my $doxd = "$spooldir/$to/DOX/$dir";
2428 my $udir = urlencode($dir);
2429 unless (-d $doxd) { http_die("no such DOX directory /$udir") }
2430 if ($fkey eq 'dox.zip') {
2431 chdir $doxd or http_die("$doxd - $!");
2432 $_ = `unzip -l $upload 2>&1`;
2433 if (m:/\.\./|\d\d\s+/:) { http_die("bad zip") }
2434 open my $z,"unzip -o $upload 2>&1|" or
2435 http_die("cannot run unzip - $!");
2436 http_header('200 OK','Connection: close');
2437 print html_header($head);
2438 print $js_scroll;
2439 pq(qq(
2440 '<h3>extracting $fkey in /$dir:</h3>'
2441 '<pre>'
2442 ));
2443 my @files = ();
2444 my $sdir = $dir;
2445 $sdir =~ s:^.+?/::;
2446 while (<$z>) {
2447 next if /^Archive:/;
2448 chomp;
2449 s/^\s*\w+:\s*//;
2450 push @files,"$sdir/$_";
2451 print $scroll,htmlencode($_),"\n";
2452 }
2453 close $z;
2454 # rrrx("[\n\r]",'.');
2455 my $dox = 'dox';
2456 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2457 my $href = urlencode(encode_utf8("/$dox/$to/$dir"));
2458 pq(qq(
2459 '</pre>'
2460 '<a href="$href">back to DOX</a>'
2461 '$scroll'
2462 '</body></html>'
2463 ));
2464 doxlog($from,$doxd,@files);
2465 if ($dir =~ m:([^/]+):) {
2466 my $folder = $1;
2467 update_dox_folder($folder) ;
2468 doxunweed($folder);
2469 }
2470 system qw'chmod -R u+rwX .';
2471 system qw'chmod -R go+rX-w .';
2472 sleep 3; # wait for showupload to finish
2473 unlink $upload;
2474 doxdu($from);
2475 exit;
2476 } else {
2477 chdir $filed or http_die("$filed - $!");
2478 my $filename = untaint(slurp('filename'));
2479 $filename = decode('utf-8',$filename);
2480 sleep 3; # wait for showupload to finish
2481 unlink("$doxd/$filename");
2482 rename('upload',"$doxd/$filename")
2483 or http_die("cannot save $filename - $!");
2484 if ("$doxd/$filename" =~ m:.+?/DOX/.+?/(.+):) {
2485 doxlog($from,$doxd,$1);
2486 }
2487 update_dox_folder($1) if $dir =~ m:([^/]+):;
2488 my $dox = 'dox';
2489 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2490 $dir = urlencode(encode('utf-8',$dir));
2491 nvt_print(
2492 "HTTP/1.1 302 Found",
2493 "Location: /$dox/$to/$dir",
2494 'Content-Length: 0',
2495 ""
2496 );
2497 doxdu($from);
2498 exit;
2499 }
2500 }
2501
2502 # user dox upload
2503 if (-d "$to/DOX/$folder/.upload/$from") {
2504 $dir =~ s:\Q$folder/.upload/$from\E/?::;
2505 chdir "$to/DOX/$folder/.upload/$from/$dir" or
2506 http_die("no DOX directory $to/$folder/$dir/UPLOAD");
2507 my $cwd = untaint(abs_path('.'));
2508 if ($fkey eq 'dox.zip') {
2509 $_ = `unzip -l $upload 2>&1`;
2510 if (m:/\.\./|\d\d\s+/:) { http_die("bad zip") }
2511 open my $z,"unzip -o $upload 2>&1|" or
2512 http_die("cannot run unzip - $!");
2513 http_header('200 OK','Connection: close');
2514 print html_header($head);
2515 print $js_scroll;
2516 my $upload = 'UPLOAD';
2517 $upload = "$dir/$upload" if length $dir;
2518 pq(qq(
2519 '<h3>extracting $fkey in $folder/$upload of $to:</h3>'
2520 '<pre>'
2521 ));
2522 my @files = ();
2523 while (<$z>) {
2524 next if /^Archive:/;
2525 chomp;
2526 s/^\s*\w+:\s*//;
2527 push @files,".upload/$from/$dir/$_";
2528 print $scroll,htmlencode($_),"\n";
2529 }
2530 close $z;
2531 # rrrx("[\n\r]",$cwd);
2532 rmrx("^[.#]",$cwd);
2533 doxunweed($cwd);
2534 my $dox = 'dox';
2535 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2536 my $href = urlencode("/$dox/$to/$folder/$upload");
2537 pq(qq(
2538 '</pre>'
2539 '<a href="$href">back to DOX</a>'
2540 '$scroll'
2541 '</body></html>'
2542 ));
2543 doxlog($from,$cwd,@files);
2544 doxdu($to);
2545 system qw'chmod -R u+rwX .';
2546 system qw'chmod -R go+rX-w .';
2547 sleep 3; # wait for showupload to finish
2548 unlink $upload;
2549 exit;
2550 } else {
2551 my $filename = untaint(slurp("$filed/filename"));
2552 $filename =~ s/^[.#]/_/;
2553 $filename =~ s/[\r\n]/ /g;
2554 sleep 3; # wait for showupload to finish
2555 rmrf($filename);
2556 rename($upload,$filename) or http_die("cannot save $filename - $!");
2557 $ _ = ".upload/$from/$dir/$filename";
2558 s:/+:/:g;
2559 doxlog($from,$cwd,$_);
2560 my $dox = 'dox';
2561 $dox = $1 if (($ENV{HTTP_REFERER}||'') =~ m:/(dox\w*)/:);
2562 $dir = urlencode(encode('utf-8',$dir));
2563 nvt_print(
2564 "HTTP/1.1 302 Found",
2565 "Location: /$dox/$to/$folder/UPLOAD/$dir",
2566 'Content-Length: 0',
2567 ""
2568 );
2569 doxdu($to);
2570 exit;
2571 }
2572 }
2573 }
2574
2575 foreach (@group?@group:@to) {
2576 my $to = $_; local $_;
2577 $to =~ s/:\w+=.*//; # remove options from address
2578 $filed = "$to/$from/$fkey";
2579 $save = "$filed/data";
2580 $dkeylink = "$filed/dkey";
2581 $upload = "$filed/upload";
2582 $download = "$filed/download";
2583 $dkey{$to} = readlink("$filed/dkey");
2584 my $time = isodate(time);
2585 $overwrite{$to}++ if -f $save and not -f $download;
2586 unlink $save;
2587 rename $upload,$save
2588 or http_die("cannot rename $upload to $save - $!\n");
2589
2590 # generate new dkey if file has been already downloaded to prevent file
2591 # corruption on download-resume
2592 if (-s $download and unlink $download and
2593 my $dkey = untaint(readlink_($dkeylink))
2594 ) {
2595 unlink $dkeylink,"$dkeydir/$dkey";
2596 symlink '/dev/null',"$dkeydir/$dkey";
2597 $dkey = randstring(8);
2598 symlink $dkey,$dkeylink;
2599 symlink "../$filed","$dkeydir/$dkey";
2600 $dkey{$to} = $dkey;
2601 }
2602
2603 # dkey log
2604 my $msg = sprintf "%s %s %s %s %s\n",
2605 $time,$dkey{$to},$from,$to,$fkey;
2606 writelog('dkey.log',$msg);
2607
2608 # user log (recipient) for fexget -w / fexsend -~ FUPWATCH
2609 {
2610 mkdir "$to/.log";
2611 my $log = "$to/.log/fup";
2612 my $size = -s $save;
2613 my $comment = slurp("$filed/comment")||'';
2614 chomp $comment;
2615 if (open $log,'>>',$log) {
2616 printf {$log} "%s %s %s %s %s %d \"%s\"\n",
2617 $time,$from,$fra,$fkey,$dkey{$to},$size,$comment;
2618 close $log;
2619 }
2620 }
2621
2622 # send notification email
2623 if (not $nomail and readlink_("$to/\@NOTIFICATION") !~ /^no/i
2624 and ($comment or not $overwrite{$to}))
2625 {
2626 notify_locale($dkey{$to},'new');
2627 debuglog("notify $filed [$filename] '$comment'");
2628 }
2629 }
2630
2631 }
2632
2633 $size = -s $save||0;
2634 $size = sprintf "%s MB",int($size/$MB+0.5);
2635 }
2636
2637 # send HTTP reply
2638 unless ($mup) {
2639 nvt_print("HTTP/1.1 200 OK");
2640 }
2641
2642 if ($nostore or $mup) {
2643 # nothing
2644 } elsif ($from eq $to and $file eq 'STDFEX') {
2645 # xx needs no more response
2646 exit;
2647 } elsif ($share) {
2648 if ($dkey) {
2649 foreach my $durl (@durl) {
2650 nvt_print("X-Location: $durl/$dkey/${archive}_$avt");
2651 }
2652 }
2653 } else {
2654 if ($xkey and not $restricted) {
2655 my $x = "$durl//$xkey";
2656 $x =~ s:/fop::;
2657 nvt_print("X-Location: $x");
2658 }
2659 if ($anonymous) {
2660 my $dkey = $dkey{$to};
2661 my $cookie = $dkey;
2662 $cookie = $1 if $ENV{HTTP_COOKIE} =~ /anonymous=([\w:]+)/;
2663 $cookie .= ':'.$dkey if $cookie !~ /$dkey/;
2664 nvt_print("Set-Cookie: anonymous=$cookie");
2665 $keep{$to} = readlink("$to/\@KEEP")||$keep_default;
2666 }
2667 foreach (@group?@group:@to) {
2668 my $to = $_;
2669 $to =~ s/:\w+=.*//; # remove options from address
2670 my $file = "$to/$from/$fkey";
2671 my $options = sprintf "(autodelete=%s,keep=%s,locale=%s)",
2672 readlink("$file/autodelete")||$autodelete,
2673 readlink("$file/keep")||readlink("$to/\@KEEP")||$keep_default,
2674 readlink("$to/\@LOCALE")||readlink("$file/locale")||$default_locale;
2675 nvt_print("X-Recipient: $to $options");
2676 unless ($restricted) {
2677 foreach my $durl (@durl) {
2678 if ($durl =~ /^$ENV{PROTO}/i) {
2679 nvt_print("X-Location: $durl/$dkey{$to}/$fkey");
2680 }
2681 }
2682 }
2683 }
2684 }
2685
2686 # from fexdox client (owner)
2687 if ($doxua and $comment =~ /^DOX/ and $from eq "@to" and
2688 $fkey =~ /^fexsync_(.+)_(\w+)\.(tar|tgz)$/)
2689 {
2690 my $transfer = $1; # DOX folder name
2691 my $tkey = $2;
2692 my $tar = $3 eq 'tar' ? 'tar -xf' : 'tar -xzf';
2693 my $to = $from;
2694 my $doxd = "$spooldir/$to/DOX";
2695 my $data = "$spooldir/$to/$from/$fkey/data";
2696 my @d = localtime time;
2697 my $tdt = sprintf(
2698 '%s_%d%02d%02d_%02d%02d%02d',
2699 $transfer,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
2700 );
2701 my $backups = 1;
2702 my $new = '';
2703 my $stream = '';
2704 my $old = '';
2705
2706 mkdirp($doxd);
2707 debuglog("cd $doxd");
2708 chdir $doxd or http_die("no $doxd - $!\n");
2709 if (-d $transfer and not -l $transfer) {
2710 my @d = localtime mtime($transfer);
2711 $old = sprintf(
2712 '%s_%d%02d%02d_%02d%02d%02d',
2713 $transfer,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
2714 );
2715 rename $transfer,$old or http_die("cannot rename $transfer to $old - $!\n");
2716 symlink $old,$transfer or http_die("cannot symlink $old - $!\n");
2717 }
2718 $stream = "$transfer!stream"; # from fexdox first stage
2719 $new = "$transfer!new";
2720 rmrf($new);
2721 $old = untaint(readlink_($transfer));
2722 if (-d $stream and -d $old and mtime($old) > mtime($stream)) {
2723 rmrf($stream);
2724 }
2725 if (-d $transfer) {
2726 if (-d $stream) {
2727 rename $stream,$new or http_die("cannot rename $stream to $new - $!\n");
2728 } else {
2729 $_ = `cp -al $old $new 2>&1`;
2730 http_die($_) if $?;
2731 }
2732 } else {
2733 if (-d $stream) {
2734 rename $stream,$new or http_die("cannot rename $stream to $new - $!\n");
2735 } else {
2736 mkdirp($new);
2737 }
2738 }
2739
2740 chdir $new or http_die("no $new - $!\n");
2741 debuglog("$tar $data");
2742 system("$tar $data 2>/dev/null");
2743 unlink($data);
2744 # rrrx("[\n\r]",'.');
2745 utime time,time,'.';
2746 doxunweed('.');
2747 system qw'chmod -R u+rwX .';
2748 system qw'chmod -R go+rX-w .';
2749 # system(ssplit('find . -type d -exec chmod u=rwx,go=rx {} +'));
2750 # system(ssplit('find . -type f -exec chmod ug+r,o+r-w {} +'));
2751
2752 if ($comment =~ /:DELETE$/ and
2753 open my $sfiles,"$spooldir/$to/CCC/$transfer#$tkey/sfiles")
2754 {
2755 my %sfiles = ();
2756 while (<$sfiles>) {
2757 $sfiles{hexdecode($2)} = $1 if /^(\d+) (.+)/;
2758 }
2759 close $sfiles;
2760 # delete extraneous files
2761 foreach (find('.')) {
2762 m:^\./(.+): or next;
2763 $_ = $1;
2764 next if m:^\.(fexdox|upload)/:; # never delete .fexdox and .upload
2765 if ((-l or -f) and not $sfiles{$_}) {
2766 debuglog("unlink $_");
2767 unlink $_;
2768 }
2769 }
2770 # delete empty directories but keep .fexdox and .upload
2771 if (opendir my $dir,'.') {
2772 while (defined($_ = readdir($dir))) {
2773 if (-d and not -l and not /^(\.|\.\.|\.fexdox|\.upload)$/) {
2774 rmdirr($_);
2775 }
2776 }
2777 closedir $dir;
2778 }
2779 }
2780
2781 chdir $doxd or die;
2782 if ($comment =~ /:ADD/) {
2783 if (-d $old) {
2784 rmrf($old);
2785 rename $new,$old;
2786 } else {
2787 rename $new,$tdt;
2788 unlink $transfer;
2789 symlink $tdt,$transfer or http_die("cannot symlink $tdt");
2790 }
2791 } else {
2792 rename $new,$tdt;
2793 unlink $transfer;
2794 symlink $tdt,$transfer or http_die("cannot symlink $tdt");
2795 rmrf("$old!backup") if -d $old;
2796
2797 if (open my $fdc,"$transfer/.fexdox/config") {
2798 while (<$fdc>) {
2799 $backups = $1 if /^backups\s*=\s*(\d+)/;
2800 }
2801 close $fdc;
2802 }
2803
2804 if (my @v = grep /^\Q$transfer\E_$vp$/,glob "${transfer}_*") {
2805 while (scalar(@v)-1 > $backups) {
2806 my $v = untaint(shift @v);
2807 rmrf($v,"$v!backup");
2808 }
2809 }
2810 }
2811
2812 my $location =
2813 sprintf "%s://%s/dox/%s/%s",
2814 $ENV{PROTO},($ENV{HTTP_HOST}||$ENV{SERVER_NAME}),$to,$transfer;
2815 $location =~ s^:(80|443)/^/^;
2816
2817 nvt_print("Location: $location",'');
2818 doxdu($from);
2819 shutdown(STDOUT,2);
2820 exit;
2821 }
2822
2823 # from fexdox client (user)
2824 if ($okey and $comment =~ /^DOX:/ and $fkey =~ /^fexdox_(.+)_\d+\.(tar|tgz)$/) {
2825 my $folder = $1;
2826 my $tar = $2 eq 'tar' ? 'tar -xvf' : 'tar -xvzf';
2827 my $doxd = "$spooldir/$to/DOX/$folder";
2828 my $doxu = "$doxd/.upload/$from";
2829
2830 unlink "$to/\@OKEY/$okey";
2831
2832 if (chdir $doxu) {
2833
2834 my $data = "$spooldir/$to/$from/$fkey/data";
2835 my $alist = "$spooldir/$to/$from/$fkey/alist";
2836 debuglog("$tar $data");
2837 if (open $tar,"$tar $data 2>&1|") {
2838 my @files = ();
2839 while (<$tar>) {
2840 chomp;
2841 push @files,".upload/$from/".hexencode($_);
2842 }
2843 close $tar;
2844 doxlog($from,$doxd,@files);
2845 unlink $data;
2846 }
2847
2848 my %exclude = ();
2849 my $x = "$doxd/.fexdox/private";
2850 if (open $x,$x) {
2851 while (<$x>) {
2852 next if /^#/;
2853 s/[\r\n]//g;
2854 s://+:/:;
2855 s:^/\Q$folder:/:;
2856 s:/$::;
2857 $exclude{$_} = shp2prx($_) if length;
2858 }
2859 close $x;
2860 foreach (find('.')) {
2861 if (m:^\./(.+): and excluded($1)) {
2862 rmrf($1);
2863 }
2864 }
2865 }
2866 doxunweed('.');
2867 system qw'chmod -R u+rwX .';
2868 system qw'chmod -R go+rX-w .';
2869 # system(ssplit('find . -type d -exec chmod u=rwx,go=rx {} +'));
2870 # system(ssplit('find . -type f -exec chmod ug+r,o+r-w {} +'));
2871 }
2872
2873 my $location =
2874 sprintf "%s://%s/dox/%s/%s/UPLOAD/",
2875 $ENV{PROTO},($ENV{HTTP_HOST}||$ENV{SERVER_NAME}),$to,$folder;
2876 $location =~ s^:(80|443)/^/^;
2877
2878 nvt_print("Location: $location",'');
2879 doxdu($to);
2880 shutdown(STDOUT,2);
2881 exit;
2882 }
2883
2884 if ($http_client =~ /^(fex|schwuppdiwupp)/) {
2885 unlink "$to/\@OKEY/$okey" if $okey;
2886 nvt_print('Content-Type: text/html','Connection: close','');
2887 if (@warning) {
2888 foreach (@warning) {
2889 s/<\/?code>/\"/g;
2890 s/<.*?>//g;
2891 print "<!-- %W: $_ -->\n";
2892 }
2893 }
2894 sleep(1);
2895 shutdown(STDOUT,2);
2896 exit;
2897 }
2898
2899 if ($ukey and $http_client =~ /^curl/) {
2900 $_ = "$file ($size) received and saved for $to\n";
2901 # ukey from fexpack ==> show durl
2902 $_ .= "$durl/$dkey{$to}/$fkey\n" if $ukey =~ /^_/;
2903 my $length = length;
2904 nvt_print(
2905 'Content-Type: text/plain',
2906 "Content-Length: $length",
2907 'Connection: close',
2908 ''
2909 );
2910 print;
2911 shutdown(STDOUT,2);
2912 exit;
2913 }
2914
2915 # send HTML report
2916 unless ($mup) {
2917 nvt_print('Content-Type: text/html','Connection: close','');
2918 print html_header($head);
2919 }
2920
2921 if ($nostore) {
2922 printf "%s (%s MB) received\n",$file,int($ndata/$MB);
2923 } elsif ($share) {
2924 pq(qq(
2925 '<code>$file</code> ($size) received and saved'
2926 '<p>'
2927 ));
2928 if ($pkey) {
2929 pq(qq'<a href="/fas/$to/$share/$from/$pkey">share index</a>');
2930 } else {
2931 pq(qq'<a href="/fas?show=share:$share:archives">share index</a>');
2932 }
2933 } else {
2934 if (@warning) {
2935 foreach (@warning) {
2936 print "WARNING: $_<p>\n";
2937 s/<\/?code>/\"/g;
2938 s/<.*?>//g;
2939 print "<!-- %W: $_ -->\n";
2940 }
2941 }
2942 if (not $restricted and ($anonymous or $from eq $to)) {
2943 my $size = $ndata<2*1024 ? sprintf "%s B",$ndata:
2944 $ndata<2*$MB ? sprintf "%s kB",int($ndata/1024):
2945 sprintf "%s MB",int($ndata/$MB);
2946 pq(qq(
2947 '<code>$file</code> ($size) received and saved<p>'
2948 'Download URL for copy & paste:'
2949 '<h2>$durl/$dkey{$to}/$fkey</h2>'
2950 'Link is valid for $keep{$to} days!<p>'
2951 ));
2952 } else {
2953 if ($ndata<2*1024) {
2954 print "<code>$file</code> ($ndata B) received and saved<p>\n";
2955 if (not $boring and not $seek) {
2956 print "Ehh... $ndata <b>BYTES</b>?! You are kidding?<p>\n";
2957 }
2958 } elsif ($ndata<2*$MB) {
2959 $ndata = int($ndata/1024);
2960 print "<code>$file</code> ($ndata kB) received and saved<p>\n";
2961 if ($ndata<1024 and not ($boring or $seek)) {
2962 print "Using F*EX for less than 1 MB: ",
2963 "ever heard of MIME email? &#9786;<p>\n";
2964 }
2965 } else {
2966 $ndata = int($ndata/$MB);
2967 print "<code>$file</code> ($ndata MB) received and saved<p>\n";
2968 }
2969 if ($ukey and $ukey =~ /^_/) {
2970 # ukey from fexpack ==> show durl
2971 pq(qq(
2972 '$durl/$dkey{$to}/$fkey'
2973 '<p>'
2974 ));
2975 }
2976 print "<ul>\n";
2977 foreach $to (@to) {
2978 print "<li>";
2979 if ($nomail or $nomail{$to}) {
2980 if ($restricted) {
2981 rmrf("$to/$from/$fkey");
2982 print "<code>$file</code> removed because you are a restricted user ".
2983 "and recipient $to cannot receive email<p>\n";
2984 } else {
2985 pq(qq(
2986 '$to cannot receive email &rarr;'
2987 '<font color="red">'
2988 '<h3>No notification email has been sent to $to!</h3>'
2989 '</font>'
2990 'Download URL for copy & paste:'
2991 ));
2992 if ($xkey) {
2993 my $x = "$durl{$to}//$xkey";
2994 $x =~ s:/fop::;
2995 print "<h2><code>$x</code></h2>\n";
2996 } else {
2997 print "<h2>$durl/$dkey{$to}/$fkey</h2>\n";
2998 print "Link is valid for $keep{$to} days!<p>\n";
2999 }
3000 }
3001 } elsif ($overwrite{$to} and not $comment) {
3002 print "(old <code>$file</code> for $to overwritten)<p>\n"
3003 } else {
3004 print "$to notified<p>\n"
3005 }
3006 }
3007 print "</ul>\n";
3008 }
3009 }
3010
3011 if ($okey) {
3012 unlink "$to/\@OKEY/$okey";
3013 } elsif ($share) {
3014 } elsif (not $anonymous and not $sup) {
3015 print "<a href=\"/fup?submit=again";
3016 if ($public) { print "&from=$from&to=$to&id=$id" }
3017 elsif ($skey) { print "&skey=$skey" }
3018 elsif ($ukey) { print "&ukey=$ukey&from=$from" }
3019 elsif ($gkey) { print "&gkey=$gkey" }
3020 elsif ($akey) { print "&to=$to" }
3021 print "&bwlimit=$bwlimit&autodelete=$autodelete&keep=$keep\">";
3022 print "send another file</a>\n";
3023 if ($http_client !~ /fexsend/ and $http_client =~ /Linux/i) {
3024 print '<p>Hi Linux-user, try ',
3025 '<a href="/FAQ/user.html#Why_should_I_use_a_special_F_EX_client">',
3026 "fexsend</a>! &#9786;<p>\n";
3027 }
3028 if ($http_client !~ /fexit/ and $http_client =~ /Windows/i) {
3029 print '<p>Hi Windows-user, try <a href="/fexit.html">fexit</a>! ',
3030 "&#9786;<p>\n";
3031 }
3032 print &logout;
3033 }
3034 print "$scroll\n" if $mup;
3035 print "</body></html>\n";
3036 shutdown(STDOUT,2);
3037 exit;
3038
3039
3040 # parse GET and POST requests
3041 sub parse_request {
3042 my %to;
3043 my ($to,$dkey);
3044 my ($x,$k,$v);
3045 my $qs = $ENV{QUERY_STRING};
3046 local $_;
3047
3048 # forward request (from fas) is handled later
3049 $qs =~ s/(^|\?)forward=.*//;
3050
3051 # get JUP parameters from environment (HTTP headers)
3052 while (($k,$v) = each %ENV) {
3053 if ($k =~ s/^FEX_//) {
3054 setparam($k,$v);
3055 }
3056 }
3057
3058 # decode base64 PATH_INFO to QUERY_STRING
3059 if ($ENV{PATH_INFO} =~ m:^/(\w+=*)$:) {
3060 if ($qs) {
3061 $qs = sprintf("%s&%s",decode_b64($1),$qs);
3062 } else {
3063 $qs = decode_b64($1);
3064 }
3065 }
3066
3067 # parse HTTP QUERY_STRING (parameter=value pairs)
3068 if ($qs) {
3069 $qs =~ s/\?/&/g if $qs !~ /&/ and $qs =~ /\?\w+=/;
3070 foreach (split '&',$qs) {
3071 if (s/^(\w+)=(.*)//) {
3072 my $p = uc($1);
3073 my $v = $2;
3074 # decode URL-encoding
3075 $v =~ s/%([a-f0-9]{2})/chr(hex($1))/gie;
3076 setparam($p,$v);
3077 if ($p eq 'AUTODELETE') {
3078 $specific{'autodelete'} = $autodelete = $v;
3079 }
3080 if ($p eq 'KEEP' and $v =~ /^(\d+)$/) {
3081 $keep = $1;
3082 $keep = $keep_max if $keep>$keep_max;
3083 $specific{'keep'} = $keep;
3084 }
3085 # if ($p eq 'LOCALE') {
3086 # $specific{'locale'} = $locale = $v;
3087 # }
3088 }
3089 }
3090 }
3091
3092 # HTTP redirect does not work correctly with opera!
3093 # ==> locale handling is now done by fexsrv
3094 if (0 and $locale) {
3095 nvt_print(
3096 "HTTP/1.1 302 Found",
3097 "Location: $ENV{PROTO}://$ENV{HTTP_HOST}/fup",
3098 "Set-Cookie: locale=$locale",
3099 'Expires: 0',
3100 'Content-Length: 0',
3101 ''
3102 );
3103 # control back to fexsrv for further HTTP handling
3104 &reexec;
3105 }
3106
3107 if ($showstatus) {
3108 &showstatus;
3109 exit;
3110 }
3111
3112 # check for akey, gkey, skey and pkey (from HTTP GET)
3113 &check_keys;
3114
3115 if ($ENV{REQUEST_METHOD} eq 'POST' and $cl) {
3116 foreach $sig (keys %SIG) {
3117 if ($sig !~ /^(CHLD|CLD)$/) {
3118 $SIG{$sig} = \&sigexit;
3119 }
3120 }
3121 $SIG{PIPE} = 'IGNORE' if $ENV{PROTO} eq 'https'; # stunnel workaround
3122 $SIG{__DIE__} = \&sigdie;
3123 http_die("invalid Content-Length header \"$cl\"") if $cl !~ /^-?\d+$/;
3124 debuglog($0);
3125 debuglog(sprintf("awaiting %d bytes from %s %s",
3126 $cl,$ENV{REMOTE_ADDR}||'',$ENV{REMOTE_HOST}||''),"\n");
3127
3128 &check_space($cl) if $cl > 0;
3129
3130 $SIG{ALRM} = sub {
3131 $SIG{__DIE__} = 'DEFAULT';
3132 fuplog($to||'',$fkey||'-',$RB,'(TIMEOUT)');
3133 die "TIMEOUT\n";
3134 };
3135 alarm($timeout);
3136 binmode(STDIN,':raw');
3137
3138 if (defined($ENV{FEX_FILENAME})) {
3139 # JUP via HTTP header
3140 $file = $param{'FILE'} = $ENV{FEX_FILENAME};
3141 $fileid = $ENV{FEX_FILEID} || 0;
3142 $fpsize = $ENV{X_CONTENT_LENGTH} || 0;
3143 $boundary = '';
3144 } elsif ($contentlength) {
3145 # JUP via URL parameter
3146 $fpsize = $contentlength;
3147 $boundary = '';
3148 } else {
3149 # FUP
3150 if (($ENV{CONTENT_TYPE}||'') =~ /boundary=\"?([^\s\";,]+)/) {
3151 $boundary = $1;
3152 } else {
3153 http_die("malformed HTTP POST (no boundary found)");
3154 }
3155
3156 READPOST: while (&nvt_read) {
3157 # the file itself - *must* be last part of POST!
3158 if (/^Content-Disposition:\s*form-data;\s*name="file";\s*filename="(.+)"/i) {
3159 push @header,$_;
3160 $file = $param{'FILE'} = $1;
3161 while (&nvt_read) {
3162 last if /^\s*$/;
3163 $fileid = $1 if /^X-File-ID:\s*(.+)/;
3164 $fpsize = $1 if /^Content-Length:\s*(\d+)/;
3165 $flink = $1 if /^Content-Location:\s*(\/.+)/;
3166 push @header,$_;
3167 }
3168 # STDIN is now at begin of file, will be read later with get_file()
3169 last;
3170 }
3171 if (/^Content-Disposition:\s*form-data;\s*name="directory";\s*filename="(.+)"/i) {
3172 push @header,$_;
3173 $file = $param{'FILE'} = $1;
3174 $file =~ s:/.*::;
3175 $file .= '.zip';
3176 $mup = 1;
3177 while (&nvt_read) {
3178 last if /^\s*$/;
3179 push @header,$_;
3180 }
3181 # STDIN is now at begin of directory, will be read later with get_file()
3182 last;
3183 }
3184 # all other parameters
3185 if (/^Content-Disposition:\s*form-data;\s*name="([a-z]\w*)"/i) {
3186 my $x = $1;
3187 nvt_skip_to('^\s*$');
3188 &nvt_read;
3189 setparam($x,$_);
3190 NEXTPART: while (&nvt_read) {
3191 last READPOST if /^--\Q$boundary--/;
3192 last NEXTPART if /^--\Q$boundary/;
3193 }
3194 }
3195 }
3196 }
3197
3198 if (length($file)) {
3199 $file =~ s/%0D$//; # chomp CR
3200 $file =~ s/%(\d+)/chr($1)/ge;
3201 $file = strip_path($file);
3202 my $ofile = $file;
3203 # filter out dangerous or invalid chars
3204 $file = normalize($file);
3205 $file = '_' unless length($file);
3206 $file =~ s/[\\\/\|<>:]/_/g;
3207 $file =~ s/^\./_./;
3208 # censorship sanctions
3209 if (%censor) {
3210 foreach my $rx (keys %censor) {
3211 $file =~ s/$rx/$censor{$rx}/g;
3212 }
3213 }
3214 if ($file ne $ofile) {
3215 $ofile =~ s/</&lt;/g;
3216 $ofile =~ s/>/&gt;/g;
3217 push @warning,
3218 "<code>$ofile</code> has been renamed to <code>$file</code>";
3219 }
3220 $filename = $file = untaint($file);
3221 $fkey = urlencode($file);
3222 }
3223
3224 # check for akey, gkey, skey and pkey (from HTTP POST)
3225 &check_keys;
3226
3227 }
3228
3229 if ($from) {
3230 unless ($skey or $gkey or $okey) {
3231 $from .= '@'.$mdomain if $mdomain and $from !~ /@/;
3232 if ($from ne 'anonymous' and not checkaddress($from)) {
3233 http_die("$from is not a valid email address");
3234 }
3235 }
3236 $from = untaint($from);
3237 }
3238
3239 # collect multiple addresses and check for aliases (not group)
3240 if (@to and "@to" !~ /^@[\w-]+$/
3241 and not ($gkey or $addto or $command =~ /^LIST(RECEIVED)?$/)) {
3242 # read address book
3243 if ($from and open my $AB,'<',"$from/\@ADDRESS_BOOK") {
3244 my ($alias,$addresses,$autodelete,$locale,$keep);
3245 while (<$AB>) {
3246 s/#.*//;
3247 $_ = lc $_;
3248 if (s/^\s*(\S+)[=\s]+(\S+)//) {
3249 ($alias,$addresses) = ($1,$2);
3250 # alias specific options?
3251 $autodelete = $locale = $keep = '';
3252 $autodelete = $1 if /autodelete=(\w+)/;
3253 $locale = $1 if /locale=(\w+)/;
3254 $keep = $1 if /keep=(\d+)/;
3255 foreach my $address (split(",",$addresses)) {
3256 # alias address specific :options?
3257 if ($address =~ s/(.+?):(.+)/$1/) {
3258 my @options = split(':',$2);
3259 $address = expand($address);
3260 foreach (@options) {
3261 if (/^keep=(\d+)$/i) {
3262 $alias_keep{$alias}{$address} = $1
3263 }
3264 if (/^autodelete=(yes|no|delay)$/i) {
3265 $alias_autodelete{$alias}{$address} = $1
3266 }
3267 if (/^locale=(\w+)$/i) {
3268 $alias_locale{$alias}{$address} = $1
3269 }
3270 }
3271 } else {
3272 $address = expand($address);
3273 }
3274 push @{$ab{$alias}},$address;
3275 $autodelete{$alias} = $autodelete if $autodelete;
3276 $keep{$alias} = $keep if $keep;
3277 $locale{$alias} = $locale if $locale;
3278 }
3279 }
3280 }
3281 close $AB;
3282 }
3283
3284 # look for recipient's options and eliminate dupes
3285 %to = ();
3286 foreach my $to (my @loop = @to) {
3287 # address book alias?
3288 if ($to !~ /@/ and ($ab{$to} or $to =~ /(.+?):(.+)/ and $ab{$1})) {
3289 my $alias = $to;
3290 my @options = ();
3291 $alias =~ s/:(.*)// and @options = split(':',$1);
3292 if (@options) {
3293 # alias with :options
3294 $alias =~ s/:.*//;
3295 foreach my $address (my @loop = @{$ab{$alias}}) {
3296 $to{$address} = $address; # ignore dupes
3297 foreach (@options) {
3298 $keep{$address} = $1 if /^keep=(\d+)$/i;
3299 $autodelete{$address} = $1 if /^autodelete=(yes|no|delay)$/i;
3300 $locale{$address} = $1 if /^locale=(\w+)$/i;
3301 }
3302 }
3303 }
3304 foreach my $address (my @loop = @{$ab{$alias}}) {
3305 $to{$address} = $address; # ignore dupes
3306 unless ($keep{$address}) {
3307 $keep{$address} = $keep{$alias} if $keep{$alias};
3308 if ($specific{'keep'}) {
3309 $keep{$address} = $specific{'keep'}
3310 } elsif (my $keep = $alias_keep{$alias}{$address}) {
3311 $keep{$address} = $keep;
3312 } elsif ($keep{$alias}) {
3313 $keep{$address} = $keep{$alias}
3314 }
3315 }
3316 unless ($autodelete{$address}) {
3317 if ($specific{'autodelete'}) {
3318 $autodelete{$address} = $specific{'autodelete'};
3319 } elsif (my $autodelete = $alias_autodelete{$alias}{$address}) {
3320 $autodelete{$address} = $autodelete;
3321 } elsif ($autodelete{$alias}) {
3322 $autodelete{$address} = $autodelete{$alias};
3323 } else {
3324 $autodelete{$address} = readlink("$address/\@AUTODELETE")
3325 || $autodelete;
3326 }
3327 }
3328 unless ($locale{$address}) {
3329 if (my $locale = readlink("$address/\@LOCALE")) {
3330 $locale{$address} = $locale;
3331 } elsif ($locale{$alias}) {
3332 $locale{$address} = $locale{$alias};
3333 } elsif ($locale = $alias_locale{$alias}{$address}) {
3334 $locale{$address} = $locale;
3335 } else {
3336 $locale{$address} = $::locale ;
3337 }
3338 $locale{$address} ||= $default_locale || 'english';
3339 }
3340 }
3341 } else {
3342 # regular address, not an alias
3343 if ($to =~ s/(.+?):(.+)/$1/) {
3344 my @options = split(':',$2);
3345 $to = expand($to);
3346 foreach (@options) {
3347 $keep{$to} = $1 if /^keep=(\d+)$/i;
3348 $autodelete{$to} = $1 if /^autodelete=(yes|no|delay)$/i;
3349 $locale{$to} = $1 if /^locale=(\w+)$/i;
3350 }
3351 }
3352 $to = expand($to);
3353 $to{$to} = $to; # ignore dupes
3354 unless ($autodelete{$to}) {
3355 $autodelete{$to} = untaint(readlink("$to/\@AUTODELETE")
3356 ||$autodelete);
3357 if ($specific{'autodelete'}) {
3358 $autodelete{$to} = $specific{'autodelete'};
3359 }
3360 }
3361 unless ($keep{$to}) {
3362 $keep{$to} = $keep_default;
3363 $keep{$to} = $keep if $keep;
3364 $keep{$to} = untaint(readlink("$to/\@KEEP")) if -l "$to/\@KEEP";
3365 $keep{$to} = $specific{'keep'} if $specific{'keep'};
3366 }
3367 if (my $user_keep_max = readlink("$to/\@KEEP_MAX")) {
3368 $keep{$to} = $user_keep_max if $keep{$to} > $user_keep_max;
3369 } else {
3370 $keep{$to} = $keep_max if $keep_max and $keep{$to} > $keep_max;
3371 }
3372 }
3373 $autodelete{$to} = 'NO' if $to =~ /$amdl/; # mailing lists, etc
3374 if (-e "$to/\@CAPTIVE") {
3375 my $v;
3376 $v = readlink("$to/\@AUTODELETE") and $autodelete{$to} = $v;
3377 $v = readlink("$to/\@KEEP") and $keep{$to} = $v;
3378 }
3379 }
3380 @to = keys %to;
3381
3382 if (scalar(@to) == 1) {
3383 $to = "@to";
3384 $keep = $keep{$to} if $keep{$to};
3385 $autodelete = $autodelete{$to} if $autodelete{$to};
3386 if ($from eq $to and not $specific{'autodelete'}) {
3387 $autodelete = $autodelete{$to} = 'NO';
3388 }
3389 }
3390
3391 # check recipients and eliminate dupes
3392 %to = ();
3393 foreach $to (@to) {
3394 if ($to eq 'anonymous') {
3395 $to{$to} = $to;
3396 } else {
3397 if ($to =~ /^@(.+)/) {
3398 http_die("you cannot send to more than one group") if @to > 1;
3399 http_die("group $to does not exist") unless -f "$from/\@GROUP/$1";
3400 } else {
3401 if ($skey or $gkey or $okey or checkaddress($to)) {
3402 $to .= '@'.$mdomain if $mdomain and $to !~ /@/;
3403 $to{$to} = untaint($to);
3404 } else {
3405 http_die("$to is not a valid email address");
3406 }
3407 }
3408 }
3409 }
3410 @to = values %to;
3411 }
3412
3413 foreach $to (@to) {
3414 unless (checkforbidden($to)) {
3415 http_die("$to is not allowed");
3416 }
3417 }
3418 }
3419
3420
3421 # show the status progress bar
3422 sub showstatus {
3423 my $wclose;
3424 my ($upload,$data,$sfile,$ukey,$file);
3425 my ($nsize,$tsize);
3426 my ($t0,$t1,$t2,$tt,$ts,$tm);
3427 my ($osize,$percent,$npercent);
3428 local $_;
3429
3430 $wclose = '<p><a href="#" onclick="window.close()">close</a>'."\n".
3431 '</body></html>'."\n";
3432 $ukey = "$ukeydir/$uid";
3433 $upload = "$ukey/upload";
3434 $data = "$ukey/data";
3435 $sfile = "$ukey/size";
3436 for (1..$timeout) {
3437 sleep 1;
3438 $tsize = readlink($sfile) and last;
3439 # upload error?
3440 # remark: stupid Internet Explorer *needs* the error represented in this
3441 # asynchronous popup window, because it cannot display the error in the
3442 # main window on HTTP POST!
3443 if (-f $ukey and open $ukey,'<',$ukey or
3444 -f "$ukey/error" and open $ukey,'<',"$ukey/error") {
3445 undef $/;
3446 unlink $ukey;
3447 html_error($error,<$ukey> || 'unknown');
3448 }
3449 }
3450 # unlink $sfile;
3451
3452 if (defined $tsize and $tsize == 0) {
3453 print "<script>window.close()</script>\n";
3454 exit;
3455 }
3456 unless ($tsize) {
3457 html_error($error,
3458 "no file data received - does your file exist or is it >2GB?")
3459 }
3460 html_error($error,"file size unknown") unless $tsize =~ /^\d+$/;
3461
3462 http_header('200 OK');
3463 if (open $ukey,'<',"$ukey/filename") {
3464 local $/;
3465 $file = <$ukey>;
3466 close $ukey;
3467 }
3468 http_die("no filename?!") unless $file;
3469
3470 if ((slurp("$ukey/header")||'') =~ /;\s*name="directory";/) {
3471 $file =~ s:\.zip$:/:;
3472 }
3473
3474 my $ssize = $tsize;
3475 if ($ssize<2097152) {
3476 $ssize = sprintf "%d kB",int($ssize/1024);
3477 } else {
3478 $ssize = sprintf "%d MB",int($ssize/1048576);
3479 }
3480
3481 pq(qq(
3482 "<html><body>"
3483 "<center>"
3484 "<h1>Upload Status for<br><code>$file</code> ($ssize)</h1>"
3485 '<img src="/action-fex-camel.gif" id="afc">'
3486 "</center>"
3487 "<input type='text' id='percent' style='margin-left:1ex;color:black;background:transparent;border:none;width:32ex;' disabled='true' value='0%'>"
3488 "<div style='border:1px solid black;width:100%;height:20px;'>"
3489 "<div style='float:left;width:0%;background:black;height:20px;' id='bar'>"
3490 "</div></div>"
3491 ));
3492
3493 # wait for upload file
3494 for (1..9) {
3495 last if -f $upload or -f $data;
3496 sleep 1;
3497 }
3498 unless (-f $upload or -f $data) {
3499 print "<p><H3>ERROR: no upload received</H3>\n";
3500 print $wclose;
3501 exit;
3502 }
3503
3504 $SIG{ALRM} = sub {
3505 $SIG{__DIE__} = 'DEFAULT';
3506 pq(qq(
3507 "<script>"
3508 " document.getElementById('afc').src='/logo.jpg'"
3509 "</script>"
3510 ));
3511 die "TIMEOUT in showstatus: no (more) data received\n";
3512 };
3513 alarm($timeout*2);
3514
3515 $t0 = $t1 = time;
3516 $osize = $percent = 0;
3517
3518 for ($npercent = 0; $npercent < 100; sleep 1) {
3519 $t2 = time;
3520 $nsize = -s $upload;
3521 if (defined $nsize) {
3522 if ($nsize<$osize) {
3523 print "<p><h3>ABORTED</h3>\n";
3524 print $wclose;
3525 exit;
3526 }
3527 if ($nsize>$osize) {
3528 alarm($timeout*2);
3529 $osize = $nsize;
3530 }
3531 $npercent = int($nsize*100/$tsize);
3532 # wait until upload is processed
3533 # $npercent = 99 if $npercent == 100 and -f $upload;
3534 $showsize = calcsize($tsize,$nsize);
3535 } else {
3536 $npercent = -f $data ? 100 : 99;
3537 $showsize = calcsize($tsize,$tsize);
3538 }
3539 # hint: for ISDN (or even slower) links, 5 s tcp delay is minimum
3540 # so, updating more often is contra-productive
3541 if ($t2>$t1+5 or $npercent>$percent) {
3542 $percent = $npercent;
3543 $t1 = $t2;
3544 $tm = int(($t2-$t0)/60);
3545 $ts = $t2-$t0-$tm*60;
3546 $tt = sprintf("%d:%02d",$tm,$ts);
3547 pq(qq(
3548 "<script>"
3549 " document.getElementById('bar').style.width = '$percent%';"
3550 " document.getElementById('percent').value = '$showsize, $tt, $percent %';"
3551 "</script>"
3552 )) or last;
3553 }
3554 }
3555
3556 alarm(0);
3557 if ($npercent == 100) {
3558 print "<h3>file successfully transferred</h3>\n";
3559 } else {
3560 print "<h3>file transfer aborted</h3>\n";
3561 }
3562 pq(qq(
3563 "<script>"
3564 " document.getElementById('afc').src='/logo.jpg'"
3565 "</script>"
3566 ));
3567 print $wclose;
3568 unlink $ukey;
3569 exit;
3570 }
3571
3572
3573 # get file from post request
3574 sub get_file {
3575 my ($to,$filed,$upload,$nupload,$speed,$download);
3576 my ($b,$n,$uss);
3577 my ($dkey,$aprg,$alist);
3578 my ($fh,$filesize);
3579 my ($t0,$pt,$tt);
3580 my @alister;
3581 my $fb = 0; # file bytes
3582 my $ebl = 0; # end boundary length
3583
3584 # FUP, not JUP
3585 if ($boundary) {
3586 $ebl = length($boundary)+8; # 8: 2 * CRLF + 2 * "--"
3587 }
3588
3589 unless ($nostore) {
3590
3591 unless ($share) {
3592 # download already in progress?
3593 foreach $to (@to) {
3594 $to =~ s/:\w+=.*//; # remove options from address
3595 $filed = "$to/$from/$fkey";
3596 $download = "$filed/download";
3597 if (-f $download and open $download,'>>',$download) {
3598 flock($download,LOCK_EX|LOCK_NB) or
3599 http_die("<code>$filed</code> locked: a download is currently in progress");
3600 }
3601 }
3602 }
3603
3604 # prepare upload
3605 foreach $to (@to) {
3606 if ($shared) {
3607 $filed = "$shared/archives/$archive/$avt";
3608 unlink "$filed/data" and unlink "$filed/size";
3609 } else {
3610 $to =~ s/:\w+=.*//; # remove options from address
3611 $filed = "$to/$from/$fkey";
3612 }
3613 $nupload = "$filed/upload"; # upload for next recipient
3614 mkdirp($filed);
3615
3616 # upload already prepared (for first recipient)?
3617 if ($upload) {
3618 # link upload for next recipient
3619 unless ($upload eq $nupload or
3620 -r $upload and -r $nupload and
3621 (stat $upload)[1] == (stat $nupload)[1])
3622 {
3623 unlink $nupload;
3624 link $upload,$nupload;
3625 }
3626 if ($alist) {
3627 unlink "$filed/alist";
3628 link $alist,"$filed/alist";
3629 }
3630 }
3631
3632 # first recipient => create upload
3633 else {
3634 $upload = $nupload;
3635 unlink "$ukeydir/$uid";
3636 if ($flink) {
3637 if ($seek) {
3638 http_die("cannot resume on link upload");
3639 }
3640 &nvt_read and $flink = $_;
3641 if ($flink !~ /^\//) {
3642 http_die("no file link name ($flink)");
3643 }
3644 $flink = abs_path($flink);
3645 my $fok;
3646 foreach (@file_link_dirs) {
3647 my $dir = abs_path($_);
3648 $fok = $flink if $flink =~ /^\Q$dir\//;
3649 }
3650 unless ($fok) {
3651 http_die("<code>$flink</code> not allowed for linking");
3652 }
3653 my @s = stat($flink);
3654 unless (@s and ($s[2] & S_IROTH) and -r $flink) {
3655 http_die("cannot read <code>$flink</code>");
3656 }
3657 unless (-f $flink and not -l $flink) {
3658 http_die("<code>$flink</code> is not a regular file");
3659 }
3660 # http_die("DEBUG: flink = $flink");
3661 &nvt_read;
3662 &nvt_read if /^$/;
3663 unless (/^--\Q$boundary--/) {
3664 http_die("found no MIME end boundary in upload ($_)");
3665 }
3666 unlink $upload;
3667 symlink untaint($flink),$upload;
3668 } else {
3669 unlink $upload if -l $upload;
3670 open $upload,'>>',$upload or http_die("cannot write $upload - $!");
3671 flock($upload,LOCK_EX|LOCK_NB) or
3672 http_die("<code>$file</code> locked: a transfer is already in progress");
3673 unless ($seek) {
3674 seek $upload,0,0;
3675 truncate $upload,0;
3676 }
3677 # already uploaded file data size
3678 $uss = -s $upload;
3679 # provide upload ID symlink for showstatus
3680 symlink "../$filed","$ukeydir/$uid";
3681 if ($prefetch) {
3682 unlink "$filed/data";
3683 symlink 'upload',"$filed/data";
3684 }
3685
3686 if ($filed =~ /\.(tar|tgz|tar\.gz|zip|7z)$/
3687 and $http_client !~ /fexsync/)
3688 {
3689 $alist = "$filed/alist";
3690 if (open $alist,'>',$alist) {
3691 print {$alist} "archive index not yet created\n";
3692 close $alist;
3693 }
3694 }
3695 }
3696 }
3697
3698 unlink "$filed/autodelete",
3699 "$filed/error",
3700 "$filed/restrictions",
3701 "$filed/locale",
3702 "$filed/keep",
3703 "$filed/header",
3704 "$filed/id",
3705 "$filed/ip",
3706 "$filed/speed",
3707 "$filed/replyto",
3708 "$filed/uploader",
3709 "$filed/useragent",
3710 "$filed/uurl",
3711 "$filed/comment",
3712 "$filed/notify";
3713 unlink "$filed/size" unless $seek;
3714
3715 # showstatus needs file name and size
3716 # fexsend needs full file size (+$seek)
3717 $fh = "$filed/filename";
3718 open $fh,'>',$fh or die "cannot write $fh - $!\n";
3719 print {$fh} $filename;
3720 close $fh;
3721 if ($::filesize > 0 or $cl > 0) {
3722 if ($::filesize > 0) { $filesize = $fpsize || $::filesize }
3723 else { $filesize = $cl-$RB-$ebl+$seek }
3724 # new file
3725 unless ($seek) {
3726 if ($::filesize > 0) {
3727 # total file size as reported by POST
3728 mksymlink("$filed/size",$::filesize)
3729 or die "cannot write $filed/size - $!\n";
3730 } else {
3731 # file size as counted
3732 mksymlink("$filed/size",$filesize)
3733 or die "cannot write $filed/size - $!\n";
3734 }
3735 }
3736 }
3737
3738 if ($share) {
3739 if ($share eq '_') {
3740 mksymlink("$filed/autodelete",$specific{'autodelete'}||'NO');
3741 } else {
3742 mksymlink("$filed/autodelete",'NO');
3743 }
3744 } else {
3745 if ($from eq "@to") {
3746 # special "fex yourself"
3747 mksymlink("$filed/autodelete",$specific{'autodelete'}||'NO');
3748 } else {
3749 $autodelete{$to} ||= $autodelete;
3750 if ($autodelete{$to} =~ /^(DELAY|NO|\d+)$/i) {
3751 mksymlink("$filed/autodelete",$autodelete{$to});
3752 }
3753 }
3754
3755 if (my $keep = $keep{$to} || $::keep) {
3756 my $keep_max = readlink("$to/\@KEEP")||$::keep_max;
3757 $keep = $keep_max if $keep>$keep_max;
3758 mksymlink("$filed/keep",$keep);
3759 }
3760 }
3761 mksymlink("$filed/id",$fileid) if $fileid;
3762 mksymlink("$filed/ip",$RA) if $RA;
3763 if (my $uurl = $ENV{REQUEST_URL}) {
3764 $uurl =~ s!:443/!/!;
3765 $uurl =~ s/\?.*//;
3766 mksymlink("$filed/uurl",$uurl);
3767 }
3768 if ($http_client and open $http_client,'>',"$filed/useragent") {
3769 print {$http_client} $http_client,"\n";
3770 close $http_client;
3771 }
3772
3773 unless ($share) {
3774 if ($_ = readlink("$to/\@LOCALE")) {
3775 # mksymlink("$filed/locale",$_);
3776 } elsif ($locale{$to}) {
3777 mksymlink("$filed/locale",$locale{$to});
3778 } elsif ($locale and $locale ne $default_locale) {
3779 mksymlink("$filed/locale",$locale);
3780 }
3781 if ($replyto and $replyto =~ /.@./) {
3782 mksymlink("$filed/replyto",$replyto);
3783 }
3784
3785 my $arh = "$from/\@ALLOWED_RHOSTS";
3786 if (-s $arh) {
3787 copy($arh,"$filed/restrictions");
3788 }
3789
3790 if (readlink_("$to/\@NOTIFICATION") =~ /^no/i) {
3791 $nomail{$to} = 'NOTIFICATION';
3792 }
3793
3794 if ($nomail) {
3795 open $fh,'>',"$filed/notify" and close $fh;
3796 }
3797 }
3798
3799 if (@header and open $fh,'>',"$filed/header") {
3800 print {$fh} join("\n",@header),"\n";
3801 close $fh;
3802 }
3803
3804 if ($comment) {
3805 if (open $fh,'>',"$filed/comment") {
3806 print {$fh} encode_utf8($comment),"\n";
3807 close $fh;
3808 }
3809 }
3810
3811 # provide download ID key
3812 if ($share and $share eq '_' and $from eq $to or not $share and not
3813 ($dkey = readlink("$filed/dkey") and -l "$dkeydir/$dkey"))
3814 {
3815 $dkey = randstring(8);
3816 unlink "$dkeydir/$dkey";
3817 symlink "../$filed","$dkeydir/$dkey"
3818 or http_die("cannot symlink $dkeydir/$dkey ($!)");
3819 unlink "$filed/dkey";
3820 symlink $dkey,"$filed/dkey";
3821 }
3822
3823 }
3824
3825 # extra download (XKEY)?
3826 if ($anonymous and $fkey =~ /^afex_\d/ or
3827 $from eq "@to" and $comment =~ s:^//(.*)$:NOMAIL:)
3828 {
3829 $xkey = $1||$fkey;
3830 $nomail = $comment;
3831 my $x = "$xkeydir/$xkey";
3832 unless (-l $x and readlink($x) eq "../$from/$from/$fkey") {
3833 if (-e $x) {
3834 http_die("extra download key $xkey already exists");
3835 }
3836 symlink "../$from/$from/$fkey",$x
3837 or http_die("cannot symlink $x - $!\n");
3838 unlink "$x/xkey";
3839 symlink $xkey,"$x/xkey";
3840 }
3841 }
3842
3843 # save download URL for fexsync
3844 if ($prefetch and $fkey =~ /^fexsync_(.+)_(\d+)\.(tar|tgz)$/) {
3845 my $transfer = "$1#$2";
3846 my $filed = "$from/$from/$fkey";
3847 if (my $dkey = readlink "$filed/dkey") {
3848 symlink untaint("$durl/$dkey/$fkey"),"$from/CCC/$transfer/durl";
3849 }
3850 }
3851
3852 }
3853
3854 # file link?
3855 if ($flink) {
3856 # upload link has been already created, no data to read any more
3857 $to = join(',',@to);
3858 fuplog($to,$fkey,0);
3859 debuglog("upload link successfull, dkey=$dkey");
3860 }
3861
3862 # regular file
3863 else {
3864
3865 # at last, read (real) file data
3866 $t0 = time();
3867
3868 # streaming data?
3869 if ($cl == -1) {
3870 alarm($timeout*2);
3871 # read until EOF, including MIME end boundary
3872 # note: cannot use sysread because of previous buffered read!
3873 while ($n = read(STDIN,$_,$bs)) {
3874 $RB += $n;
3875 $fb += $n;
3876 syswrite $upload,$_ unless $nostore;
3877 alarm($timeout*2);
3878 }
3879 # size of transferred file, without end boundary
3880 $ndata = untaint($fb-$ebl);
3881 }
3882
3883 # normal file with known file size
3884 else {
3885
3886 if ($fpsize) {
3887 debuglog(sprintf("still awaiting %d+%d = %d bytes",
3888 $fpsize,$ebl,$fpsize+$ebl));
3889 $cl = $RB+$fpsize+$ebl; # recalculate CONTENT_LENGTH
3890 } else {
3891 if ($::filesize) {
3892 $cl = $RB+$::filesize+$ebl; # recalculate CONTENT_LENGTH
3893 }
3894 debuglog(sprintf("still awaiting %d-%d = %d bytes",
3895 $cl,$RB,$cl-$RB));
3896 }
3897
3898 # prefetch means:
3899 # a client can download while upload is still in progress
3900 # BUT on NFS a sysread can block when the write-rate is too high!
3901 # The workaround hack is: close and reopen the upload file
3902 # See prefetch comment below
3903 if ($prefetch) {
3904 close $upload;
3905 # need an other lock filehandle
3906 open $prefetch,'>>',$upload;
3907 flock($prefetch,LOCK_EX|LOCK_NB);
3908 open $upload,'>>',$upload or http_die("cannot reopen $upload - $!");
3909 $pt = time;
3910 }
3911
3912 # read until end boundary, not EOF
3913 while ($RB < $cl-$ebl) {
3914 $b = $cl-$ebl-$RB;
3915 $b = $bs if $b > $bs;
3916 # max wait for 1 kB/s, but at least 10 s
3917 # $timeout = $b/1024;
3918 # $timeout = 10 if $timeout < 10;
3919 alarm($timeout);
3920 # note: cannot use sysread because of previous buffered read!
3921 if ($n = read(STDIN,$_,$b)) {
3922 $RB += $n;
3923 $fb += $n;
3924 # syswrite is much faster than print
3925 # print {$upload} $_ unless $nostore;
3926 syswrite $upload,$_ unless $nostore;
3927 if ($bwlimit) {
3928 alarm(0);
3929 $tt = (time-$t0) || 1;
3930 while ($RB/$tt/1024 > $bwlimit) {
3931 sleep 1;
3932 $tt = time-$t0;
3933 }
3934 } elsif ($prefetch and time>$pt) {
3935 # NFS unblocking hack: close and reopen the upload file
3936 # see prefetch comment above
3937 close $upload;
3938 open $upload,'>>',$upload or http_die("cannot append $upload - $!");
3939 flock($upload,LOCK_EX|LOCK_NB);
3940 $pt = time;
3941 }
3942 # debuglog($_);
3943 } else {
3944 last;
3945 }
3946 }
3947 $RB += $ebl;
3948 $ndata = untaint($fb);
3949 }
3950
3951 alarm(0);
3952
3953 unless ($nostore) {
3954 close $upload; # or die "cannot close $upload - $!\n";;
3955
3956 # throuput in kB/s
3957 $tt = (time-$t0) || 1;
3958 mksymlink("$filed/speed",int($fb/1024/$tt));
3959
3960 unless ($ndata) {
3961 http_die(
3962 "No file data received!".
3963 " File name correct?".
3964 " File too big (browser-limit: 2 GB!)?"
3965 );
3966 }
3967
3968 $to = join(',',@to);
3969
3970 # streaming upload?
3971 if ($cl == -1) {
3972
3973 open $upload,'<',$upload or http_die("internal error - cannot read upload");
3974 seek $upload,$ndata+2,0;
3975 $_ = <$upload>||'';
3976 unless (/^--\Q$boundary--/) {
3977 http_die("found no MIME end boundary in upload ($_)");
3978 }
3979 close $upload;
3980 truncate $upload,$ndata;
3981
3982 } else {
3983
3984 # truncate boundary string
3985 # truncate $upload,$ndata+$uss if -s $upload > $ndata+$uss;
3986
3987 # incomplete?
3988 if ($cl != $RB) {
3989 fuplog($to,$fkey,$ndata,'(aborted)');
3990 if ($fpsize) {
3991 http_die("read $RB bytes, but Content-Length announces $fpsize bytes");
3992 } else {
3993 http_die("read $RB bytes, but CONTENT_LENGTH announces $cl bytes");
3994 }
3995 }
3996
3997 # multipost, not complete
3998 if ($::filesize > -s $upload) {
3999 http_header('206 Partial OK');
4000 exit;
4001 }
4002
4003 # save error?
4004 if (-s $upload > ($::filesize||$filesize)) {
4005 fuplog($to,$fkey,$ndata,'(write error: upload > filesize)');
4006 http_die("internal server error while writing file data");
4007 }
4008
4009 }
4010 fuplog(sprintf("%s %s %s/%s",
4011 $to,$fkey,$ndata,-s "$filed/upload"||-s "$filed/data"||0));
4012 $dkey ||= '';
4013 debuglog("upload successfull, dkey=$dkey");
4014
4015 if ($share and $share ne '_') {
4016 symlink $from,"$filed/uploader";
4017 }
4018
4019 if ($alist) {
4020 my $tarlist = "$FEXHOME/bin/tarlist";
4021 if ($filed =~ /\.tar$/) {
4022 if (-x $tarlist) {
4023 @alister = ($tarlist);
4024 } else {
4025 @alister = qw'tar tvf';
4026 }
4027 } elsif ($filed =~ /\.(tgz|tar\.gz)$/) {
4028 if (-x $tarlist) {
4029 @alister = ($tarlist);
4030 } else {
4031 @alister = qw'tar tvzf';
4032 }
4033 } elsif ($filed =~ /\.zip$/) {
4034 if (-x "$FEXHOME/bin/unzipl") {
4035 @alister = ("$FEXHOME/bin/unzipl");
4036 } else {
4037 @alister = qw'unzip -l';
4038 }
4039 } elsif ($filed =~ /\.7z$/) {
4040 if (-x "$FEXHOME/bin/7zl") {
4041 @alister = ("$FEXHOME/bin/7zl");
4042 } else {
4043 @alister = qw'7z l';
4044 }
4045 }
4046
4047 if (@alister) {
4048 if (searchpath($alister[0])) {
4049 my $pid = fork();
4050 if (defined $pid and $pid == 0) {
4051 for (1..5) {
4052 sleep 1; # wait for rename upload -> data
4053 if (-f "$filed/data") {
4054 system "@alister '$filed/data' >$alist 2>&1";
4055 unlink $alist unless -s $alist;
4056 exit;
4057 }
4058 }
4059 exit;
4060 }
4061 } else {
4062 if (open $alist,'>',$alist) {
4063 print {$alist} "cannot create archive index: ",
4064 "$alister[0] not installed\n";
4065 close $alist;
4066 }
4067 }
4068 }
4069 }
4070
4071 }
4072 }
4073 }
4074
4075
4076 sub mupfile {
4077 my $filed = shift;
4078 local $_ = shift;
4079
4080 if (/^Content-Disposition:.*?filename=(.+)/m) {
4081 my $filename = $1;
4082 $filename =~ s/\r$//;
4083 $filename =~ s/^"(.+)"$/$1/;
4084 $filename =~ s:^/+::;
4085 $filename =~ s:^[.-]:_:;
4086 my $dir = "$filed/tmp/".dirname($filename);
4087 while ($dir =~ s:/\.\./:/__/:) {}
4088 mkdirp($dir);
4089 my $file = $dir.'/'.filename($filename);
4090 open $file,'>',$file or die "cannot write $file - $!\n";
4091 return $file;
4092 } else {
4093 die "no filename in upload MIME part\n";
4094 }
4095 }
4096
4097
4098 # check recipients restriction
4099 sub check_rr {
4100 my $from = shift;
4101 my @to = @_;
4102 my $rr = "$from/\@ALLOWED_RECIPIENTS";
4103 my ($allowed,$to,$ar,$rd);
4104
4105 if (-s $rr and open $rr,'<',$rr) {
4106
4107 $restricted = $rr;
4108
4109 foreach (@to) {
4110 my $to = $_;
4111 $allowed = 0;
4112 seek $rr,0,0;
4113 while (<$rr>) {
4114 chomp;
4115 s/#.*//;
4116 s/\s//g;
4117
4118 if (/^\@LOCAL_RDOMAINS/) {
4119 $ar = '(@';
4120 foreach (@local_rdomains) {
4121 my $rd = $_;
4122 # allow wildcard *, but not regexps
4123 $rd =~ s/\./\\./g;
4124 $rd =~ s/\*/[\\w.-]+/g;
4125 $ar .= '|[^\@]+\@' . $rd;
4126 }
4127 $ar .= ')';
4128 } elsif (/^\@LOCAL_USERS/ and -s "$to/@") {
4129 $allowed = 1;
4130 last;
4131 } else {
4132 # allow wildcard *, but not regexps
4133 $ar = quotemeta $_;
4134 $ar =~ s/\\\*/[^@]*/g;
4135 }
4136
4137 if ($to =~ /^$ar$/i) {
4138 $allowed = 1;
4139 last;
4140 }
4141
4142 }
4143
4144 unless ($allowed) {
4145 fuplog("ERROR: $from not allowed to fex to $to");
4146 debuglog("$to not in $spooldir/$from/\@ALLOWED_RECIPIENTS");
4147 http_die("you ($from) are not allowed to fex to $to");
4148 }
4149 }
4150
4151 close $rr;
4152 }
4153 }
4154
4155
4156 # add domain to user if necessary
4157 sub expand {
4158 my @users = @_;
4159 my @ua;
4160
4161 foreach my $u (my @loop = @users) {
4162 if ($u =~ /^anonymous(_\d+)?$/) {
4163 $u = "$u\@$hostname";
4164 }
4165 if ($u eq 'nettest') {
4166 if ($mdomain and -d "$u\@$mdomain") {
4167 $u .= "\@$mdomain"
4168 } elsif (-d "$u\@$hostname") {
4169 $u .= "\@$hostname"
4170 }
4171 }
4172 if ($u =~ /@/) { push @ua,$u }
4173 elsif ($mdomain) { push @ua,"$u\@$mdomain" }
4174 elsif (-d "$u\@$hostname") { push @ua,"$u\@$hostname" }
4175 else { push @ua,$u }
4176 }
4177
4178 return wantarray ? @ua : join(',',@ua);
4179 }
4180
4181
4182 # forward-copy (bounce) an already uploaded file
4183 sub forward {
4184 my $file = shift;
4185 my ($nfile,$to,$AB);
4186 my ($filename,$keep);
4187 my (%to);
4188
4189 http_die("no file data for <code>$file</code>") unless -f "$file/data";
4190
4191 $keep = $::keep||$keep_default;
4192 if (my $mt = mtime("$file/data")) { $keep += int((time-$mt)/$DS) }
4193
4194 if (@to) {
4195
4196 # check recipients restriction
4197 check_rr($from,@to);
4198
4199 # read aliases from address book
4200 if (open $AB,'<',"$from/\@ADDRESS_BOOK") {
4201 while (<$AB>) {
4202 s/#.*//;
4203 $_ = lc $_;
4204 if (s/^\s*(\S+)[=\s]+(\S+)//) {
4205 my ($alias,$address) = ($1,$2);
4206 foreach my $address (split(",",$address)) {
4207 $address .= '@'.$mdomain if $mdomain and $address !~ /@/;
4208 push @{$ab{$alias}},$address;
4209 }
4210 }
4211 }
4212 close $AB;
4213 }
4214
4215 # collect addresses
4216 foreach my $to (my @loop = @to) {
4217 if ($ab{$to}) {
4218 foreach my $address (@{$ab{$to}}) {
4219 $to{$address} = $address;
4220 }
4221 } else {
4222 $to .= '@'.$mdomain if $mdomain and $to !~ /@/;
4223 $to{$to} = $to;
4224 }
4225 }
4226
4227 @to = keys %to;
4228
4229 http_header('200 OK');
4230 print html_header($head);
4231
4232 foreach my $to (my @loop = @to) {
4233 $to =~ s/:\w+=.*//; # remove options from address
4234 $nfile = $file;
4235 $nfile =~ s:.*?/:$to/:;
4236 next if $nfile eq $file;
4237 mkdirp($nfile);
4238 http_die("cannot create directory $nfile") unless -d $nfile;
4239 unlink "$nfile/data",
4240 "$nfile/upload",
4241 "$nfile/download",
4242 "$nfile/alist",
4243 "$nfile/autodelete",
4244 "$nfile/error",
4245 "$nfile/restrictions",
4246 "$nfile/keep",
4247 "$nfile/header",
4248 "$nfile/id",
4249 "$nfile/speed",
4250 "$nfile/comment",
4251 "$nfile/replyto",
4252 "$nfile/notify";
4253 if ($comment and open $comment,'>',"$nfile/comment") {
4254 chomp $comment;
4255 print {$comment} encode_utf8($comment),"\n";
4256 close $comment;
4257 }
4258 my $autodelete = readlink("$to/\@AUTODELETE") || $::autodelete;
4259 $autodelete = uc(untaint($autodelete));
4260 if ($autodelete =~ /^(DELAY|NO|\d+)$/i) {
4261 symlink $autodelete,"$nfile/autodelete";
4262 }
4263 symlink $keep, "$nfile/keep";
4264 copy("$file/id", "$nfile/id");
4265 copy("$file/ip", "$nfile/ip");
4266 copy("$file/speed", "$nfile/speed");
4267 copy("$file/replyto", "$nfile/replyto");
4268 copy("$file/alist", "$nfile/alist");
4269 $filename = copy("$file/filename","$nfile/filename");
4270 link "$file/data", "$nfile/data"
4271 or die http_die("cannot create $nfile/data - $!");
4272 unless ($dkey = readlink("$nfile/dkey") and -l "$dkeydir/$dkey") {
4273 $dkey = randstring(8);
4274 unlink "$dkeydir/$dkey";
4275 symlink "../$nfile","$dkeydir/$dkey"
4276 or http_die("cannot symlink $dkeydir/$dkey");
4277 unlink "$nfile/dkey";
4278 symlink $dkey,"$nfile/dkey"
4279 or http_die("cannot create $nfile/dkey - $!");
4280 }
4281
4282 if ($nomail or $nomail{$to}) {
4283 if ($filename) {
4284 my $url = "$durl/$dkey/".normalize_filename($filename);
4285 pq(qq(
4286 'Download-URL for $to:<br>'
4287 '<code>$url</code>'
4288 '<p>'
4289 ));
4290 }
4291 } else {
4292 notify_locale($dkey,'new');
4293 fuplog($to,urlencode($filename),"(forwarded)");
4294 if ($filename) {
4295 pq(qq(
4296 'File "$filename" copy-forwarded to $to and notified.'
4297 '<p>'
4298 ));
4299 }
4300 }
4301 }
4302 pq(qq(
4303 '<a href="/foc">back to F*EX operation control</a>'
4304 '</body></html>'
4305 ));
4306 } else {
4307 $filename = filename($file);
4308 http_header('200 OK');
4309 print html_header($head);
4310 pq(qq(
4311 '<form name="upload" action="/fup" $formpara>'
4312 ' <input type="hidden" name="akey" value="$akey">'
4313 ' <input type="hidden" name="dkey" value="$dkey">'
4314 ' <input type="hidden" name="command" value="FORWARD">'
4315 ' forward a copy of "<code>$filename</code>" to:<br>'
4316 ' <input type="text" name="to" size="80">'
4317 '</form>'
4318 '</body></html>'
4319 ));
4320 }
4321 }
4322
4323
4324 # modify file parameter
4325 sub modify {
4326 my $file = shift;
4327 my $filename = filename($file);
4328 my $dkey = readlink("$file/dkey");
4329 my $to;
4330 my @parameter;
4331
4332 http_die("no file data for <code>$file</code>") unless -f "$file/data";
4333
4334 $to = $file;
4335 $to =~ s:/.*::;
4336 if (my $keep = $specific{'keep'}) {
4337 my $keep_max = readlink("$to/\@KEEP")||$::keep_max;
4338 $keep = $keep_max if $keep>$keep_max;
4339 mksymlink("$file/keep",$keep);
4340 utime time,time,"$file/filename";
4341 push @parameter,'KEEP';
4342 }
4343 if ($specific{'autodelete'}) {
4344 mksymlink("$file/autodelete",$autodelete);
4345 push @parameter,'AUTODELETE';
4346 }
4347 if ($comment) {
4348 if (open $comment,'>',"$file/comment") {
4349 print {$comment} encode_utf8($comment),"\n";
4350 close $comment;
4351 }
4352 notify_locale($dkey,'new') if $dkey;
4353 push @parameter,'COMMENT';
4354 }
4355 http_header('200 OK');
4356 print "Parameter ".join(',',@parameter)." modified for $filename for $to\n";
4357 }
4358
4359
4360 sub calcsize {
4361 my ($tsize,$nsize) = @_;
4362 if ($tsize<2097152) {
4363 return sprintf "%d kB",int($nsize/1024);
4364 } else {
4365 return sprintf "%d MB",int($nsize/1048576);
4366 }
4367 }
4368
4369
4370 # set parameter variables
4371 sub setparam {
4372 my ($v,$vv) = @_;
4373 my ($idf,$to);
4374
4375 $v = uc(despace($v));
4376
4377 # if ($vv =~ /([<>])/) {
4378 # http_die(sprintf("\"&#%s;\" is not allowed in parameter $v",ord($1)));
4379 # }
4380
4381 $param{$v} = $vv;
4382 if ($v eq 'LOGOUT') {
4383 my $location = "$ENV{PROTO}://$ENV{HTTP_HOST}/";
4384 my $login = $FEXHOME.'/cgi-bin/login';
4385 # skey and gkey are persistant!
4386 $akey = $1 if $ENV{QUERY_STRING} =~ /AKEY:(\w+)/i;
4387 unlink "$akeydir/$akey" if $akey;
4388 if (-x $login) {
4389 $login = readlink($login) || 'login';
4390 $location .= basename($login);
4391 } else {
4392 $location .= 'fup';
4393 }
4394 nvt_print(
4395 'HTTP/1.1 302 Found',
4396 "Location: $location",
4397 'Set-Cookie: akey=; path=/', # ; expires=Thu, 01 Jan 1970 00:00:00 GMT
4398 'Content-Length: 0',
4399 ''
4400 );
4401 &reexec;
4402 } elsif ($v eq 'LOCALE' and $vv =~ /^(\w+)$/) {
4403 $locale = $1;
4404 } elsif ($v eq 'REDIRECT' and $vv =~ /^([\w?=]+)$/) {
4405 $redirect = $1;
4406 } elsif ($v eq 'SKEY' and $vv =~ /^([\w:]+)/) {
4407 $skey = $1;
4408 $restricted = $v;
4409 } elsif ($v eq 'GKEY' and $vv =~ /^([\w:]+)/) {
4410 $gkey = $1 unless $nomail;
4411 $restricted = $v;
4412 } elsif ($v eq 'UKEY' and $vv =~ /^(\w+)$/) {
4413 $ukey = $1;
4414 $restricted = $v;
4415 } elsif ($v eq 'PKEY' and $vv =~ /^((MD5H:)?(\w+))/) {
4416 $pkey = $1;
4417 $restricted = $v;
4418 } elsif ($v eq 'DKEY' and $vv =~ /^(\w+)/) {
4419 $dkey = $1;
4420 } elsif ($v eq 'AKEY' and $vv =~ /^(\w+)/) {
4421 $akey = $1;
4422 } elsif (($v eq 'FROM' or $v eq 'USER') and $vv =~ /\w/) {
4423 $from = normalize_email($vv);
4424 $from = expand($from);
4425 $from = checkchars('sender address',$from);
4426 # maybe FROM=SUBUSER !
4427 # checkaddress($from) or http_die("FROM $from is no legal email address");
4428 } elsif ($v eq 'REPLYTO') {
4429 $replyto = normalize_email($vv);
4430 checkchars('replyto address',$replyto);
4431 checkaddress($replyto) or
4432 http_die("REPLYTO $replyto is no legal email address");
4433 } elsif ($v eq 'ADDTO') {
4434 $vv =~ s/\s.*//;
4435 $addto = normalize_email($vv);
4436 } elsif ($v eq 'SUBMIT') {
4437 $submit = decode_utf8(normalize($vv));
4438 } elsif ($v eq 'FEXYOURSELF') {
4439 $submit = $vv;
4440 @to = ($from);
4441 $specific{'autodelete'} = $autodelete = 'no';
4442 } elsif ($v eq 'TO') {
4443 # extract AUTODELETE and KEEP options
4444 if ($vv =~ s/[\s,]+AUTODELETE=(\w+)//i) {
4445 $specific{'autodelete'} = $autodelete = uc($1);
4446 }
4447 if ($vv =~ s/[\s,]+KEEP=(\d+)//i) {
4448 $keep = $1;
4449 my $keep_max = readlink("$to/\@KEEP")||$::keep_max;
4450 $keep = $keep_max if $keep>$keep_max;
4451 $specific{'keep'} = $keep;
4452 }
4453 # extract SHARE
4454 $share = $1 if $vv =~ s/:SHARE=(\S+)$//i;
4455 $to = normalize(lc($vv));
4456 $to =~ s/[\n\s;,]+/,/g;
4457 if ($from) {
4458 if ($to eq '.') {
4459 # fake header to force MIME fop
4460 push @header,'Content-Type: application/x-mime';
4461 $nomail = $to = $from;
4462 unless ($specific{'autodelete'}) {
4463 $specific{'autodelete'} = $autodelete = 'no';
4464 }
4465 }
4466 if ($to eq '+') {
4467 $prefetch = $nomail = $to = $from;
4468 }
4469 if ($to eq '//') {
4470 $to = $from;
4471 unless ($specific{'autodelete'}) {
4472 $specific{'autodelete'} = $autodelete = 'no';
4473 }
4474 $comment = '//';
4475 }
4476 }
4477 checkchars('recipient address',$to);
4478 push @to,split(',',$to);
4479 } elsif ($v eq 'ID') {
4480 $id = $vv;
4481 $id =~ s/^\s+//;
4482 $id =~ s/\s+$//;
4483 # checkchars('auth-ID',$id);
4484 $id_forgotten = $id if $id eq '?';
4485 } elsif ($v eq 'SHARE') {
4486 $share = $vv;
4487 } elsif ($v eq 'ARCHIVE') {
4488 $archive = $vv;
4489 $archive =~ s/^\./_/;
4490 $archive =~ s/[^\w.+-]/_/g;
4491 } elsif ($v eq 'TCE') {
4492 $test = despace($vv);
4493 } elsif ($v eq 'OKEY' and $vv =~ /^(\w+)$/) {
4494 $okey = $1;
4495 $restricted = $v;
4496 } elsif ($v eq 'FILEID' and $vv =~ /^(\w+)$/) {
4497 $fileid = $1;
4498 } elsif ($v eq 'CONTENTLENGTH' and $vv =~ /^(\d+)$/) {
4499 $contentlength = $1;
4500 } elsif ($v eq 'FILE' or $v eq 'FILENAME') {
4501 $file = strip_path(normalize($vv));
4502 } elsif ($v eq 'UID' and $vv =~ /^(\w+)$/) {
4503 $uid = $1;
4504 } elsif ($v eq 'ID_FORGOTTEN' and $vv =~ /@/) {
4505 $id_forgotten = $from = normalize_email($vv);
4506 } elsif ($v eq 'SHOWSTATUS' and $vv =~ /^(\w+)$/) {
4507 $showstatus = $uid = $1;
4508 } elsif ($v eq 'COMMENT') {
4509 $comment = normalize($vv);
4510 if ($comment =~ /%[A-F\d]{2}/) {
4511 $comment = urldecode($comment);
4512 }
4513 $comment = decode_utf8($comment);
4514 $comment =~ s/^\s*!\.!/!SHORTMAIL!/;
4515 $comment =~ s/^!#!/!NOMAIL!/;
4516 $comment =~ s/^!-!/!NOSTORE!/;
4517 $nomail = $comment if $comment =~ /!NOMAIL!/ or $comment eq 'NOMAIL';
4518 $nostore = $nomail = $1 if $comment =~ /!(NOSTORE)!/;
4519 $bcc .= " $from" if $comment =~ s/\s*!BCC!?\s*//i;
4520 # backward compatibility
4521 foreach my $cmd (qw(
4522 DELETE LIST CHECKQUOTA CHECKRECIPIENT RECEIVEDLOG SENDLOG FOPLOG FORWARD
4523 )) { $command = $comment if $comment eq $cmd }
4524 } elsif ($v eq 'COMMAND') {
4525 $command = normalize($vv);
4526 } elsif ($v eq 'BWLIMIT' and $vv =~ /^(\d+)$/) {
4527 $bwlimit = $1;
4528 } elsif ($v eq 'SEEK' and $vv =~ /^(\d+)$/) {
4529 $seek = $1;
4530 } elsif ($v eq 'FILESIZE' and $vv =~ /^(\d+)$/) {
4531 $filesize = $1; # complete filesize!
4532 &check_space($filesize-$seek);
4533 } elsif ($v eq 'AUTODELETE' and $vv =~ /^(\w+)$/) {
4534 $specific{'autodelete'} = $autodelete = uc($1);
4535 } elsif ($v eq 'KEEP' and $vv =~ /^(\d+)$/) {
4536 $keep = $1;
4537 $specific{'keep'} = $keep;
4538 } elsif ($v eq 'TIMEOUT' and $vv =~ /^(\d+)$/) {
4539 $specific{'timeout'} = $timeout = $1;
4540 }
4541 }
4542
4543
4544 sub id_forgotten {
4545 my ($id,$to,$subuser,$gm,$skey,$gkey,$url,$fup,$reply);
4546
4547 return if $nomail;
4548
4549 $fup = $durl;
4550 $fup =~ s:/fop:/fup:;
4551
4552 # full user
4553 if ($id = readline1("$from/@")) {
4554 if ($encryption or -f "$from/@@") {
4555 http_die("auth-ID reset");
4556 } else {
4557 $url = "$fup/".b64("from=$from&id=$id");
4558 $reply = "Your requested F*EX auth-ID for $fup?from=$from is:\n$id\n";
4559 $reply .= "\nOr use:\n$url\n" if $http_client !~ /Mac OS/;
4560 mail_forgotten($from,$reply);
4561 }
4562 exit;
4563 }
4564
4565 # sub user
4566 foreach my $skey (glob("$skeydir/*")) {
4567 if (-f $skey and open $skey,'<',$skey) {
4568 while (<$skey>) {
4569 $_ = lc;
4570 if (/^(\w+)=(.+)/) {
4571 $subuser = $2 if $1 eq 'from';
4572 $to = $2 if $1 eq 'to';
4573 }
4574 }
4575 close $skey;
4576 }
4577 if ($from and $to and $from eq $subuser) {
4578 $skey =~ s:.*/::;
4579 mail_forgotten($subuser,qqq(qq(
4580 'Your requested F*EX login is:'
4581 ''
4582 '$fup?skey=$skey?$to'
4583 )));
4584 exit;
4585 }
4586 }
4587
4588 # group user
4589 foreach my $gkey (glob("$gkeydir/*")) {
4590 if (-f $gkey and open $gkey,'<',$gkey) {
4591 while (<$gkey>) {
4592 $_ = lc;
4593 if (/^(\w+)=(.+)/) {
4594 $gm = $2 if $1 eq 'from';
4595 $to = $2 if $1 eq 'to';
4596 }
4597 }
4598 close $gkey;
4599 }
4600 if ($gm and $to and $from eq $gm) {
4601 $gkey =~ s:.*/::;
4602 mail_forgotten($gm,qqq(qq(
4603 'Your requested F*EX login is:'
4604 ''
4605 '$fup?gkey=$gkey'
4606 )));
4607 exit;
4608 }
4609 }
4610
4611 http_die("$from is not a F*EX user on this server");
4612 }
4613
4614
4615 sub mail_forgotten {
4616 my $user = shift;
4617 my @msg = @_;
4618 local *P;
4619
4620 return if $nomail;
4621
4622 open P,'|-',$sendmail,$user,$bcc or http_die("cannot start sendmail - $!\n");
4623 pq(P,qq(
4624 'From: $admin'
4625 'To: $user'
4626 'Subject: F*EX service $hostname'
4627 'X-Mailer: F*EX'
4628 ''
4629 ));
4630 print P @msg;
4631 close P or http_die("cannot send mail - $!\n");
4632 http_header('200 OK');
4633 print html_header($head);
4634 pq(qq(
4635 '<h3>Mail has been sent to you ($from)</h3>'
4636 '<a href="/fup">Back to F*EX login</a>'
4637 '</body></html>'
4638 ));
4639 }
4640
4641
4642 # lookup akey, skey, gkey and ukey
4643 sub check_keys {
4644
4645 if (@to and "@to" ne '_') {
4646 http_die("you cannot mix TO and SKEY URL parameters") if $skey;
4647 http_die("you cannot mix TO and GKEY URL parameters") if $gkey;
4648 }
4649
4650 if ($pkey and $share and $from and $to) {
4651 $shared = "$to/SHARE/$share";
4652 &check_pkey;
4653 return;
4654 }
4655
4656 if ($ukey) {
4657 if (my $user = readlink("$ukeydir/$ukey")) {
4658 if ($id = readline1("$user/@")) {
4659 $from ||= $user;
4660 $to = $user;
4661 @to = ($to);
4662 $rid = $id;
4663 $nomail = 'xup' if $ukey =~ /_$/;
4664 return;
4665 } else {
4666 http_die("$user unknown");
4667 }
4668 } else {
4669 if ($http_client =~ /^curl/) {
4670 nvt_print(
4671 "HTTP/1.1 404 UKEY $ukey not found",
4672 'Content-Type: text/plain',
4673 'Connection: close',
4674 ''
4675 );
4676 print "wrong UKEY $ukey\n";
4677 exit;
4678 } else {
4679 http_die("wrong UKEY $ukey");
4680 }
4681 }
4682 }
4683
4684 # only one key can be valid
4685 $akey = $gkey = '' if $skey;
4686 $akey = $skey = '' if $gkey;
4687
4688 if ($skey) {
4689 # encrypted SKEY?
4690 if ($skey =~ s/^MD5H:(.+)/$1/) {
4691 # search real SKEY
4692 foreach my $s (glob "$skeydir/*") {
4693 $s =~ s:.*/::;
4694 if ($skey eq md5_hex($s.$sid)) {
4695 $skey = $s;
4696 last;
4697 }
4698 }
4699 }
4700 # own (special) skey?
4701 if (my $user = readlink("$skeydir/$skey")) {
4702 $id = readline1("$user/@")||'';
4703 if (md5_hex($id) eq $skey) {
4704 $from = $to = $user;
4705 @to = ($to);
4706 $rid = $id;
4707 } else {
4708 http_die("expired SKEY <code>$skey</code>");
4709 }
4710 } elsif (open $skey,'<',"$skeydir/$skey") {
4711 $akey = $gkey = '';
4712 while (<$skey>) {
4713 if (/^(\w+)=(.+)/) {
4714 $from = $2 if lc($1) eq 'from';
4715 @to = ($muser = $2) if lc($1) eq 'to';
4716 $rid = $id = $2 if lc($1) eq 'id';
4717 }
4718 }
4719 close $skey;
4720 } else {
4721 # $skey = '';
4722 http_die("invalid SKEY <code>$skey</code>");
4723 }
4724 }
4725
4726 if ($gkey) {
4727 # encrypted GKEY?
4728 if ($gkey =~ s/^MD5H:(.+)/$1/) {
4729 # search real GKEY
4730 foreach my $g (glob "$gkeydir/*") {
4731 $g =~ s:.*/::;
4732 if ($gkey eq md5_hex($g.$sid)) {
4733 $gkey = $g;
4734 last;
4735 }
4736 }
4737 }
4738 if (open $gkey,'<',"$gkeydir/$gkey") {
4739 $akey = $skey = '';
4740 while (<$gkey>) {
4741 if (/^(\w+)=(.+)/) {
4742 $from = $2 if lc($1) eq 'from';
4743 $to = $muser = $2 if lc($1) eq 'to';
4744 $rid = $id = $2 if lc($1) eq 'id';
4745 # $user = $2 if lc($1) eq 'user';
4746 }
4747 }
4748 close $gkey;
4749 @to = ($to);
4750 } else {
4751 # $gkey = '';
4752 http_die("invalid GKEY <code>$gkey</code>");
4753 }
4754 }
4755
4756 if ($akey and not $id and not $pkey) {
4757
4758 # sid is not set with web browser
4759 # akey with sid is set with schwuppdiwupp & co
4760 if ($id = readline1("$akeydir/$akey/@")) {
4761 if (readlink_("$akeydir/$akey") =~ m:.*/(.+):) {
4762 $from = $1;
4763 } else {
4764 http_die("internal server error: unknown akey $akey");
4765 }
4766 } else {
4767 $akey = $id = '';
4768 }
4769 }
4770
4771 }
4772
4773
4774 # check if pkey is valid
4775 sub check_pkey {
4776 unless (-d $shared) {
4777 http_die("share $to/$share does not exist");
4778 }
4779 if ($to eq $from) {
4780 $rid = $id = readline1("$from/@") or http_die("unknown user $from");
4781 if ($pkey =~ /^MD5H:/) {
4782 unless ($sid) { http_die("no SID") }
4783 if ($pkey ne 'MD5H:'.md5_hex("$id$sid")) {
4784 http_die("wrong PKEY");
4785 }
4786 } else {
4787 if ($pkey ne md5_hex($id)) {
4788 http_die("wrong PKEY");
4789 }
4790 }
4791 # we are owner!
4792 $fulluser = $from;
4793 $pkey = '';
4794 } else {
4795 my $rpkey = readline1("$shared/users/$from/pkey") or
4796 http_die("share user $to/$share/$from does not exist");
4797 if ($pkey =~ /^MD5H:/) {
4798 unless ($sid) { http_die("no SID") }
4799 if ($pkey ne 'MD5H:'.md5_hex("$rpkey$sid")) {
4800 http_die("wrong PKEY");
4801 }
4802 } elsif ($pkey ne $rpkey) {
4803 http_die("wrong PKEY");
4804 }
4805 }
4806 }
4807
4808
4809 # check if there is enough space on spool
4810 sub check_space {
4811 my $req = shift;
4812 my ($df,$free,$uprq);
4813
4814 return if $flink;
4815
4816 if (open $df,"df -k $spooldir|") {
4817 while (<$df>) {
4818 if (/^.+?\s+\d+\s+\d+\s+(\d+)/ and $req/1024 > $1) {
4819 $free = int($1/1024);
4820 $uprq = int($req/$MB);
4821 if (not $nomail and open $sendmail,"|$sendmail -t") {
4822 print {$sendmail} qqq(qq(
4823 'From: $admin'
4824 'To: $admin'
4825 'Subject: F*EX spool out of space'
4826 ''
4827 'F*EX spool $spooldir on $ENV{SERVER_NAME} is out of space.'
4828 ''
4829 'Current free space: $free MB'
4830 'Upload request: $uprq MB'
4831 ));
4832 close $sendmail;
4833 }
4834 debuglog("aborting because not enough free space in spool ($free MB)");
4835 http_die("not enough free space for this upload");
4836 }
4837 }
4838 close $df;
4839 }
4840 }
4841
4842
4843 sub update_dox_folder {
4844 my $folder = shift;
4845 my @d = localtime time;
4846 my ($lv,$nv);
4847
4848 chdir "$spooldir/$from/DOX" or return;
4849 if ($lv = readlink($folder)) {
4850 $lv = untaint($lv);
4851 $nv = sprintf(
4852 '%s_%d%02d%02d_%02d%02d%02d',
4853 $folder,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]
4854 );
4855
4856 if (rename $lv,$nv) {
4857 unlink $folder;
4858 symlink $nv,$folder;
4859 }
4860 }
4861 chdir $spooldir;
4862 }
4863
4864
4865 # global substitution as a function like in gawk
4866 sub gsub {
4867 local $_ = shift;
4868 my ($p,$r) = @_;
4869 s/$p/$r/g;
4870 return $_;
4871 }
4872
4873
4874 # alphabetic sort, but with _ first
4875 sub _sort {
4876 my @a = asort(@_);
4877 my @b = ();
4878 foreach (@a) {
4879 push @b,$_ if /^_/;
4880 }
4881 foreach (@a) {
4882 push @b,$_ unless /^_/;
4883 }
4884 return @b;
4885 }
4886
4887
4888 # remove empty directories recursive
4889 sub rmdirr {
4890 my $dir = shift;
4891 local $_;
4892
4893 if (opendir $dir,$dir) {
4894 while (defined($_ = readdir($dir))) {
4895 next if /^\.\.?$/;
4896 $_ = "$dir/$_";
4897 rmdirr($_) if -d and not -l;
4898 }
4899 closedir $dir;
4900 rmdir untaint($dir) and debuglog("rmdir $dir");
4901 }
4902 }
4903
4904
4905 # standard log
4906 sub fuplog {
4907 my $msg = "@_";
4908 my $time = isodate(time);
4909
4910 $msg =~ s/\n/ /g;
4911 $msg =~ s/\s+$//;
4912 $msg = sprintf "%s [%s_%s] %s (%s) %s\n",
4913 $time,$$,$ENV{REQUESTCOUNT},$from,$fra,$msg;
4914 writelog($log,$msg);
4915 }
4916
4917
4918 sub doxlog {
4919 my $from = shift;
4920 my $dir = shift;
4921 my @files = @_;
4922 my @d = localtime time;
4923 my $log;
4924 local $_;
4925
4926 $dir =~ s:(.+?/DOX/[^/]+).*:$1:;
4927 mkdir "$dir/.upload";
4928 $log = "$dir/.upload/log";
4929 open $log,'>>',$log or return;
4930 flock($log,LOCK_EX);
4931 printf {$log}
4932 "\nUPLOAD %d-%02d-%02d %02d:%02d:%02d %s %s %s\n",
4933 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],
4934 $from,$ENV{REMOTE_HOST}||'-',$ENV{REMOTE_ADDR}||'.';
4935 foreach (@files) {
4936 s:/+:/:g;
4937 s:^/::;
4938 # s:^\Q.upload/::;
4939 $_ = hexencode($_) if /[^ -~]/;
4940 print {$log} "$1\n" if /(.+)/;
4941 }
4942 close $log;
4943 }
4944
4945
4946 sub sigdie {
4947 local $_ = shift;
4948 chomp;
4949 sigexit('DIE',$_);
4950 }
4951
4952
4953 sub sigexit {
4954 my ($sig) = @_;
4955 my $msg;
4956 my $to = join(',',@to);
4957
4958 $SIG{__DIE__} = 'DEFAULT';
4959 foreach (keys %SIG) { $SIG{$_} = 'DEFAULT' }
4960
4961 $msg = @_ ? "@_" : '???';
4962 $msg =~ s/\n/ /g;
4963 $msg =~ s/\s+$//;
4964 $msg = sprintf "%s %s (%s) %s %s caught SIGNAL %s %s\n",
4965 isodate(time),
4966 $from||'-',
4967 $fra||'-',
4968 $to||'-',
4969 encode_Q($file||'-'),
4970 $msg,
4971 $RB?"(after $RB bytes)":"";
4972
4973 writelog($log,$msg);
4974
4975 if ($sig eq 'DIE') {
4976 shift;
4977 die "$msg\n";
4978 } else {
4979 die "SIGNAL $msg\n";
4980 }
4981 }
4982
4983
4984 sub present_locales {
4985 my $url = shift;
4986 my @locales = @::locales; # from fex.ph
4987 my ($locale,$lang);
4988
4989 if ($url =~ /\?/) {
4990 $url .= "&";
4991 $url =~ s/locale=\w+&//g;
4992 } else {
4993 $url .= "?";
4994 }
4995
4996 if (@locales) {
4997 map { $_ = "$FEXHOME/locale/$_" } @locales;
4998 } else {
4999 @locales = glob "$FEXHOME/locale/*";
5000 }
5001
5002 if (@locales > 1) {
5003 print "<h3>";
5004 foreach my $locale (my @loop = @locales) {
5005 if (-x "$locale/cgi-bin/fup") {
5006 $lang = "$locale/lang.html";
5007 $locale =~ s:.*/::;
5008 if (open $lang,'<',$lang and $lang = getline($lang)) {
5009 close $lang;
5010 } else {
5011 $lang = $locale;
5012 }
5013 print "<a href=\"${url}locale=$locale\">$lang</a> ";
5014 }
5015 }
5016 print "</h3>\n";
5017 }
5018 }
5019
5020
5021 sub check_camel {
5022 my ($logo,$camel);
5023 local $/;
5024
5025 if (open $logo,"$docdir/logo.jpg") {
5026 $camel = md5_hex(<$logo>) eq 'ad8a95bba8dd1a61d70bd38611bc2059';
5027 }
5028 if ($camel and open $logo,"$docdir/action-fex-camel.gif") {
5029 $camel = md5_hex(<$logo>) eq '1f3d7acc70377496f95c5adddaf4ca7b';
5030 }
5031 http_die("missing camel") unless $camel;
5032 }