5 use Fcntl qw':flock :seek :mode';
9 use Digest::MD5 qw'md5_hex';
12 use Symbol qw'gensym';
14 # set and untaint ENV if not in CLI (fexsrv provides clean ENV)
16 foreach my $v (keys %ENV) {
17 ($ENV{$v}) = ($ENV{$v} =~ /(.*)/s) if defined $ENV{$v};
19 $ENV{PATH} = '/usr/local/bin:/bin:/usr/bin';
24 unless ($FEXLIB = $ENV{FEXLIB} and -d $FEXLIB) {
25 die "$0: found no FEXLIB - fexsrv needs full path\n"
31 # $FEXHOME is top-level directory of F*EX installation or vhost
32 # $ENV{HOME} is login-directory of user fex
33 # in default-installation both are equal, but they may differ
34 $FEXHOME = $ENV{FEXHOME} or $ENV{FEXHOME} = $FEXHOME = dirname($FEXLIB);
39 $hostname = gethostname();
40 $tmpdir = $ENV{TMPDIR} || '/var/tmp';
41 $spooldir = $FEXHOME.'/spool';
42 $docdir = $FEXHOME.'/htdocs';
46 $limited_download = 'YES'; # multiple downloads only from same client
47 $fex_yourself = 'YES'; # allow SENDER = RECIPIENT
49 $recipient_quota = 0; # MB
50 $sender_quota = 0; # MB
51 $timeout = 30; # seconds
52 $bs = 2**16; # I/O blocksize
53 $DS = 60*60*24; # seconds in a day
54 $MB = 1024*1024; # binary Mega
56 $sendmail = '/usr/lib/sendmail';
57 $sendmail = '/usr/sbin/sendmail' unless -x $sendmail;
66 $FHS = -f '/etc/fex/fex.ph' and -d '/usr/share/fex/lib';
69 $ENV{FEXHOME} = $FEXHOME = '/usr/share/fex';
70 $spooldir = '/var/spool/fex';
71 $logdir = '/var/log/fex';
72 $docdir = '/var/lib/fex/htdocs';
73 $notify_newrelease = '';
76 # allowed download managers (HTTP User-Agent)
77 $adlm = '^(Axel|fex)';
80 require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
82 $fop_auth = 0 if $fop_auth =~ /no/i;
83 $mail_authid = 0 if $mail_authid =~ /no/i;
84 $force_https = 0 if $force_https =~ /no/i;
85 $debug = 0 if $debug =~ /no/i;
87 @logdir = ($logdir) unless @logdir;
90 # allowed multi download recipients: from any ip, any times
92 $amdl = '^('.join('|',map { quotewild($_) } @mailing_lists).')$';
97 # check for name based virtual host
98 $vhost = vhost($ENV{'HTTP_HOST'});
100 $RB = 0; # read POST bytes
102 push @doc_dirs,$docdir;
103 foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
107 $nomail = ($mailmode =~ /^MANUAL|nomail$/i);
109 if (not $nomail and not -x $sendmail) {
110 http_die("found no sendmail");
112 http_die("cannot determine the server hostname") unless $hostname;
114 $ENV{PROTO} = 'http' unless $ENV{PROTO};
115 $keep = $keep_default ||= $keep || 5;
116 $fra = $ENV{REMOTE_ADDR} || '';
117 $sid = $ENV{SID} || '';
119 $dkeydir = "$spooldir/.dkeys"; # download keys
120 $ukeydir = "$spooldir/.ukeys"; # upload keys
121 $akeydir = "$spooldir/.akeys"; # authentification keys
122 $skeydir = "$spooldir/.skeys"; # subuser authentification keys
123 $gkeydir = "$spooldir/.gkeys"; # group authentification keys
124 $xkeydir = "$spooldir/.xkeys"; # extra download keys
125 $lockdir = "$spooldir/.locks"; # download lock files
127 if (my $ra = $ENV{REMOTE_ADDR} and $max_fail) {
128 mkdirp("$spooldir/.fail");
129 $faillog = "$spooldir/.fail/$ra";
133 $admin = $ENV{SERVER_ADMIN} ? $ENV{SERVER_ADMIN} : 'fex@'.$hostname;
136 # $ENV{SERVER_ADMIN} may be set empty in fex.ph!
137 $ENV{SERVER_ADMIN} = $admin unless defined $ENV{SERVER_ADMIN};
142 if (my $cookie = $ENV{HTTP_COOKIE}) {
143 if ($cookie =~ /\bakey=(\w+)/) { $akey = $1 }
144 # elsif ($cookie =~ /\bskey=(\w+)/) { $skey = $1 }
149 if ($default_locale and not grep /^$default_locale$/,@locales) {
150 push @locales,$default_locale;
153 $default_locale = $locales[0];
157 $default_locale ||= 'english';
159 # $durl is first default fop download URL
160 # @durl is optional mandatory fop download URL list (from fex.ph)
164 my $xinetd = '/etc/xinetd.d/fex';
168 } elsif ($ENV{HTTP_HOST} and $ENV{PROTO}) {
170 ($host,$port) = split(':',$ENV{HTTP_HOST}||'');
175 if (open $xinetd,$xinetd) {
177 if (/^\s*port\s*=\s*(\d+)/) {
186 # use same protocal as uploader for download
187 if ($ENV{PROTO} eq 'https' and $port == 443 or $port == 80) {
188 $durl = "$ENV{PROTO}://$host/fop";
190 $durl = "$ENV{PROTO}://$host:$port/fop";
193 if (open $xinetd,$xinetd) {
195 if (/^\s*port\s*=\s*(\d+)/) {
203 $durl = "http://$hostname/fop";
205 $durl = "http://$hostname:$port/fop";
209 @durl = ($durl) unless @durl;
213 exec($FEXHOME.'/bin/fexsrv') if $ENV{KEEP_ALIVE};
220 $cont = shift || 'request accepted: continue';
222 http_header('200 ok');
223 print html_header($head||$ENV{SERVER_NAME});
225 '<script type="text/javascript">'
226 ' window.location.replace("$url");'
229 ' <h3><a href="$url">$cont</a></h3>'
238 print header(),"<pre>\n";
239 print "file = $file\n";
240 foreach $v (keys %ENV) {
241 print $v,' = "',$ENV{$v},"\"\n";
248 foreach (@_) { syswrite STDOUT,"$_\r\n" }
269 return if $HTTP_HEADER;
270 $HTTP_HEADER = $status;
274 nvt_print("HTTP/1.1 $status");
275 nvt_print("X-Message: $msg");
276 # nvt_print("X-SID: $ENV{SID}") if $ENV{SID};
277 nvt_print("Server: fexsrv");
278 nvt_print("Expires: 0");
279 nvt_print("Cache-Control: no-cache");
280 # http://en.wikipedia.org/wiki/Clickjacking
281 nvt_print("X-Frame-Options: SAMEORIGIN");
283 # https://www.owasp.org/index.php/HTTP_Strict_Transport_Security
284 nvt_print("Strict-Transport-Security: max-age=2851200; preload");
287 $akey = md5_hex("$from:$id") if $id and $from;
289 nvt_print("Set-Cookie: akey=$akey; path=/; Max-Age=9999; Discard");
292 # nvt_print("Set-Cookie: skey=$skey; Max-Age=9999; Discard");
295 nvt_print("Set-Cookie: locale=$locale");
298 unless (grep /^Content-Type:/i,@_) {
299 # nvt_print("Content-Type: text/html; charset=ISO-8859-1");
300 nvt_print("Content-Type: text/html; charset=UTF-8");
309 my $header = 'header.html';
312 # http://www.w3.org/TR/html401/struct/global.html
313 # http://www.w3.org/International/O-charset
317 ' <meta http-equiv="expires" content="0">'
318 ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8">'
319 ' <title>$title</title>'
322 # '<!-- <style type="text/css">\@import "/fex.css";</style> -->'
324 if ($0 =~ /fexdev/) { $head .= "<body bgcolor=\"pink\">\n" }
325 else { $head .= "<body>\n" }
327 $title =~ s:F\*EX:<a href="/index.html">F*EX</a>:;
329 if (open $header,'<',"$docdir/$header") {
330 $head .= $_ while <$header>;
334 $head .= &$prolog($title) if defined($prolog);
338 '<h1><a href="%s"><img align=center src="%s" border=0></a>%s</h1>',
339 $H1_extra[0],$H1_extra[1]||'',$title
342 $head .= "<h1>$title</h1>";
354 my $isodate = isodate(time);
356 $msg =~ s/[\s\n]+/ /g;
357 $msg =~ s/<.+?>//g; # remove HTML
358 map { s/<script.*?>//gi } @msg;
362 # cannot send standard HTTP Status-Code 400, because stupid
363 # Internet Explorer then refuses to display HTML body!
364 http_header("666 Bad Request - $msg");
365 print html_header($error);
366 print 'ERROR: ',join("<p>\n",@msg),"\n";
372 ' <a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>'
383 unless ($ENV{GATEWAY_INTERFACE}) {
384 warn "$0: @_\n"; # must not die, because of fex_cleanup!
390 # create special error file on upload
392 my $ukey = "$spooldir/.ukeys/$uid";
393 $ukey .= "/error" if -d $ukey;
395 if (open $ukey,'>',$ukey) {
396 print {$ukey} join("\n",@_),"\n";
401 html_error($error||'',@_);
406 if (my $status = readlink '@MAINTENANCE') {
407 my $isodate = isodate(time);
408 http_header('666 MAINTENANCE');
409 print html_header($head||'');
412 "<h1>Server is in maintenance mode</h1>"
416 "<address>$ENV{HTTP_HOST} $isodate</address>"
428 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
430 if (-e "$user/\@DISABLED") {
431 my $isodate = isodate(time);
432 http_header('666 DISABLED');
433 print html_header($head);
435 "<h3>$user is disabled</h3>"
436 "Contact $ENV{SERVER_ADMIN} for details"
438 "<address>$ENV{HTTP_HOST} $isodate</address>"
447 my @d = localtime shift;
448 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
449 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
455 $s =~ s{([\=\x00-\x20\x7F-\xA0])}{sprintf("=%02X",ord($1))}eog;
460 # from MIME::Base64::Perl
469 return '' unless length;
471 for ($i = 0; $i <= $l; $i += 60) {
472 $uu .= "M" . substr($_,$i,60);
475 $uu .= chr(32+(length)*3/4) . $_ if $_;
476 return unpack ("u",$uu);
480 # short base64 encoding
486 $_ = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
487 tr|` -_|AA-Za-z0-9+/|;
488 $x = (3 - length($_[0]) % 3) % 3;
495 # simulate a "rm -rf", but never removes '..'
496 # return number of removed files
505 next if /(^|\/)\.\.$/;
507 if (-d $file and not -l $file) {
509 opendir D,$dir or next;
510 while ($file = readdir D) {
511 next if $file eq '.' or $file eq '..';
512 $dels += rmrf("$dir/$file");
515 rmdir $dir and $dels++;
517 unlink $file and $dels++;
525 my $hostname = hostname;
530 $_ = `hostname 2>/dev/null`;
531 $hostname = /(.+)/ ? $1 : '';
533 if ($hostname !~ /\./ and open my $rc,'/etc/resolv.conf') {
535 if (/^\s*domain\s+([\w.-]+)/) {
539 if (/^\s*search\s+([\w.-]+)/) {
544 $hostname .= ".$domain" if $domain;
546 if ($hostname !~ /\./ and $admin and $admin =~ /\@([\w.-]+)/) {
554 # strip off path names (Windows or UNIX)
558 s/.*\\// if /^([A-Z]:)?\\/;
565 # substitute all critcal chars
569 return '' unless defined $_;
571 # we need perl native utf8 (see perldoc utf8)
572 $_ = decode_utf8($_) unless utf8::is_utf8($_);
575 s/[\x00-\x1F\x80-\x9F]/_/g;
579 return encode_utf8($_);
583 # substitute all critcal chars
587 return '' unless defined $_;
597 # substitute all critcal chars with underscore
598 sub normalize_filename {
603 # we need native utf8
604 $_ = decode_utf8($_) unless utf8::is_utf8($_);
608 # substitute all critcal chars with underscore
609 s/[^a-zA-Z0-9_=.+-]/_/g;
612 return encode_utf8($_);
616 sub normalize_email {
619 s/[^\w_.+=!~#^\@\-]//g;
629 $user = lc(urldecode(despace($user)));
630 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
631 checkaddress($user) or http_die("$user is not a valid e-mail address");
632 return untaint($user);
638 s/%([a-f0-9]{2})/chr(hex($1))/gie;
655 http_die("\"$1\" is not allowed at beginning of $input");
657 if (/([\/\"\'\\<>;])/) {
658 http_die(sprintf("\"&#%s;\" is not allowed in %s",ord($1),$input));
661 http_die("\"$1\" is not allowed at end of $input");
664 http_die("control characters are not allowed in $input");
675 local ($domain,$dns);
677 $a =~ s/:\w+=.*//; # remove options from address
679 return $a if $a eq 'anonymous';
681 $a .= '@'.$mdomain if $mdomain and $a !~ /@/;
683 $re = '^[.@-]|@.*@|local(host|domain)$|["\'\`\|\s()<>/;,]';
685 debuglog("$a has illegal syntax ($re)");
688 $re = '^[!^=~#_:.+*{}\w\-\[\]]+\@(\w[.\w\-]*\.[a-z]+)$';
692 local $SIG{__DIE__} = sub { die "\n" };
695 $dns = Net::DNS::Resolver->new->query($domain)||mx($domain);
696 unless ($dns or mx('uni-stuttgart.de')) {
697 http_die("Internal error: bad resolver");
704 debuglog("no A or MX DNS record found for $domain");
708 debuglog("$a does not match e-mail regexp ($re)");
714 # check forbidden addresses
720 $a .= '@'.$mdomain if $mdomain and $a !~ /@/;
721 return $a if -d "$spooldir/$a"; # ok, if user already exists
722 if (@forbidden_recipients) {
723 foreach (@forbidden_recipients) {
725 # skip public recipients
726 if (@public_recipients) {
727 foreach $pr (@public_recipients) {
728 return $a if $a eq lc $pr;
731 return '' if $a =~ /^$fr$/i;
740 my @rc = ('A'..'Z','a'..'z',0..9 );
744 for (1..$n) { $rs .= $rc[int(rand($rn))] };
756 http_die("cannot mkdir /") unless $dir;
758 if ($pdir =~ s:/[^/]+$::) {
759 mkdirp($pdir) unless -d $pdir;
762 mkdir $dir,0770 or http_die("mkdir $dir - $!");
771 if ($rid and $ENV{SID} and $id =~ /^MD5H:/) {
772 $rid = 'MD5H:'.md5_hex($rid.$ENV{SID});
778 # test if ip is in iplist (ipv4/ipv6)
779 # iplist is an array with ips and ip-ranges
788 if ($ip =~ /\./ and $i =~ /\./ or $ip =~ /:/ and $i =~ /:/) {
789 if ($i =~ /(.+)-(.+)/) {
793 return $ip if $ipe ge $ia and $ipe le $ib;
795 return $ip if $ipe eq ipe($i);
802 # ip expand (ipv4/ipv6)
806 if (/^\d+\.\d+\.\d+\.\d+$/) {
807 s/\b(\d\d?)\b/sprintf "%03d",$1/ge;
808 } elsif (/^[:\w]+:\w+$/) {
809 s/\b(\w+)\b/sprintf "%04s",$1/ge;
811 while (s/::/::0000:/) { last if length > 39 }
824 if (open $file,'<',"$file/filename") {
825 $filename = <$file>||'';
832 $filename =~ s:.*/::;
841 s/(^[.~]|[^\w.,=:~^+-])/sprintf "%%%X",ord($1)/ge;
846 # file and document log
848 my ($log,$file,$s,$size) = @_;
849 my $ra = $ENV{REMOTE_ADDR}||'-';
852 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
855 $msg = sprintf "%s [%s_%s] %s %s %s/%s\n",
856 isodate(time),$$,$ENV{REQUESTCOUNT},$ra,encode_Q($file),$s,$size;
867 return unless $debug and @_;
868 unless ($debuglog and fileno $debuglog) {
869 my $ddir = "$spooldir/.debug";
870 mkdir $ddir,0770 unless -d $ddir;
872 $prg = untaint($prg);
873 $debuglog = sprintf("%s/%s_%s_%s.%s",
874 $ddir,time,$$,$ENV{REQUESTCOUNT}||0,$prg);
875 $debuglog =~ s/\s/_/g;
876 # http://perldoc.perl.org/perlunifaq.html#What-is-a-%22wide-character%22%3f
877 # open $debuglog,'>>:encoding(UTF-8)',$debuglog or return;
878 open $debuglog,'>>',$debuglog or return;
879 # binmode($debuglog,":utf8");
880 autoflush $debuglog 1;
881 # printf {$debuglog} "\n### %s ###\n",isodate(time);
883 while ($_ = shift @_) {
884 $_ = encode_utf8($_) if utf8::is_utf8($_);
886 s/<.+?>//g; # remove HTML
887 print {$debuglog} $_;
888 print "DEBUG: $_" if -t;
897 my $ra = $ENV{REMOTE_ADDR}||'-';
899 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
902 $msg =~ s/[\r\n]+$//;
903 $msg =~ s/[\r\n]+/ /;
904 $msg =~ s/\s*<p>.*//;
905 $msg = sprintf "%s %s %s %s\n",isodate(time),$prg,$ra,$msg;
907 writelog('error.log',$msg);
915 foreach my $logdir (@logdir) {
916 if (open $log,'>>',"$logdir/$log") {
918 seek $log,0,SEEK_END;
926 # failed authentification log
931 if ($faillog and $max_fail_handler and open $faillog,"+>>$faillog") {
932 flock($faillog,LOCK_EX);
933 seek $faillog,0,SEEK_SET;
934 $n++ while <$faillog>;
935 printf {$faillog} "%s %s\n",isodate(time),$request;
937 &$max_fail_handler($ENV{REMOTE_ADDR}) if $n > $max_fail;
941 # remove all white space
953 my $q = "[\'\"]"; # quote delimiter chars " and '
955 # remove first newline and look for default indention
959 # remove trailing spaces at end
964 # first line have a quote delimiter char?
966 # remove heading spaces and delimiter chars
972 # find the line with the fewest heading spaces (and count them)
976 if (/^( *)\S/ and length($1) < $s) { $s = length($1) };
984 return join("\n",@s)."\n";
992 if (@_ > 1 and defined fileno $_[0]) { $H = shift }
999 sub check_sender_quota {
1001 my $squota = $sender_quota||0;
1003 my ($file,$size,%file,$data,$upload);
1006 if (open $qf,'<',"$sender/\@QUOTA") {
1009 $squota = $1 if /sender.*?(\d+)/i;
1014 foreach $file (glob "*/$sender/*") {
1015 $data = "$file/data";
1016 $upload = "$file/upload";
1017 if (not -l $data and $size = -s $data) {
1018 # count hard links only once (= same inode)
1019 my $i = (stat($data))[1]||0;
1020 unless ($file{$i}) {
1024 } elsif (-f $upload) {
1025 # count hard links only once (= same inode)
1026 my $i = (stat($upload))[1]||0;
1027 unless ($file{$i}) {
1028 $size = readlink "$file/size" and $du += $size;
1034 return($squota,int($du/1024/1024));
1038 # check recipient quota
1039 sub check_recipient_quota {
1040 my $recipient = shift;
1041 my $rquota = $recipient_quota||0;
1046 if (open my $qf,'<',"$recipient/\@QUOTA") {
1049 $rquota = $1 if /recipient.*?(\d+)/i;
1054 foreach $file (glob "$recipient/*/*") {
1055 if (-f "$file/upload" and $size = readlink "$file/size") {
1057 } elsif (not -l "$file/data" and $size = -s "$file/data") {
1062 return($rquota,int($du/1024/1024));
1069 chomp($_ = <$file>||'');
1074 # (shell) wildcard matching
1077 my $p = quotemeta shift;
1090 if ($skey) { $logout = "/fup?logout=skey:$skey" }
1091 elsif ($gkey) { $logout = "/fup?logout=gkey:$gkey" }
1092 elsif ($akey) { $logout = "/fup?logout=akey:$akey" }
1093 else { $logout = "/fup?logout" }
1096 '<form name="logout" action="$logout">'
1097 ' <input type="submit" name="logout" value="logout">'
1104 # print data dump of global or local variables in HTML
1105 # input musst be a string, eg: '%ENV'
1111 $_ = eval(qq(use Data::Dumper;Data::Dumper->Dump([\\$v])));
1115 print "<pre>\n$_\n</pre>\n";
1120 my ($file,$link) = @_;
1122 return symlink untaint($link),$file;
1126 # copy file (and modify) or symlink
1127 # returns chomped file contents or link name
1128 # preserves permissions and time stamps
1130 my ($from,$to,$mod) = @_;
1135 $to .= '/'.basename($from) if -d $to;
1137 if (defined($link = readlink $from)) {
1138 mksymlink($to,$link);
1141 open $from,'<',$from or return;
1142 open $to,'>',$to or return;
1147 close $to or http_die("internal error: $to - $!");
1148 if (my @s = stat($from)) {
1150 utime @s[8,9],$to unless $mod;
1163 if (open $file,$file) {
1172 # read one line from STDIN (net socket) and assign it to $_
1173 # return number of read bytes
1174 # also set global variable $RB (read bytes)
1178 if (defined ($_ = <STDIN>)) {
1188 # read forward to given pattern
1190 my $pattern = shift;
1192 while (&nvt_read) { return if /$pattern/ }
1196 # HTTP GET and POST parameters
1198 # fills global variable %PARAM :
1199 # normal parameter is $PARAM{$parameter}
1200 # file parameter is $PARAM{$parameter}{filename} $PARAM{$parameter}{data}
1201 sub parse_parameters {
1202 my $cl = $ENV{X_CONTENT_LENGTH} || $ENV{CONTENT_LENGTH} || 0;
1207 if ($cl > 128*$MB) {
1208 http_die("request too large");
1211 binmode(STDIN,':raw');
1213 foreach (split('&',$ENV{QUERY_STRING})) {
1214 if (/(.+?)=(.*)/) { $PARAM{$1} = $2 }
1215 else { $PARAM{$_} = $_ }
1217 $_ = $ENV{CONTENT_TYPE}||'';
1218 if ($ENV{REQUEST_METHOD} eq 'POST' and /boundary=\"?([\w\-\+\/_]+)/) {
1220 while ($RB<$cl and &nvt_read) { last if /^--\Q$boundary/ }
1221 # continuation lines are not checked!
1222 while ($RB<$cl and &nvt_read) {
1224 if (/^Content-Disposition:.*\s*filename="(.+?)"/i) {
1227 if (/^Content-Disposition:\s*form-data;\s*name="(.+?)"/i) {
1229 # skip rest of mime part header
1230 while ($RB<$cl and &nvt_read) { last if /^\s*$/ }
1233 if ($p =~ /password/i) {
1234 debuglog('*' x length)
1239 last if /^--\Q$boundary/;
1242 unless (defined $_) { die "premature end of HTTP POST\n" }
1243 $data =~ s/\r?\n$//;
1245 $PARAM{$p}{filename} = $filename;
1246 $PARAM{$p}{data} = $data;
1250 last if /^--\Q$boundary--/;
1257 # name based virtual host?
1259 my $hh = shift; # HTTP_HOST
1261 my $locale = $ENV{LOCALE};
1263 # memorized vhost? (default is in fex.ph)
1264 %vhost = split(':',$ENV{VHOST}) if $ENV{VHOST};
1266 if (%vhost and $hh and $hh =~ s/^([\w\.-]+).*/$1/) {
1267 if ($vhost = $vhost{$hh} and -f "$vhost/lib/fex.ph") {
1268 $ENV{VHOST} = "$hh:$vhost"; # memorize vhost for next run
1269 $ENV{FEXLIB} = $FEXLIB = "$vhost/lib";
1270 $logdir = $spooldir = "$vhost/spool";
1271 $docdir = "$vhost/htdocs";
1272 @logdir = ($logdir);
1273 if ($locale and -e "$vhost/locale/$locale/lib/fex.ph") {
1274 $ENV{FEXLIB} = $FEXLIB = "$vhost/locale/$locale/lib";
1276 require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
1277 $ENV{SERVER_NAME} = $hostname;
1278 @doc_dirs = ($docdir);
1279 foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
1289 my ($plain,$to,$keyring,$from) = @_;
1290 my ($pid,$pi,$po,$pe,$enc,$err);
1295 $pid = open3($po,$pi,$pe,
1296 "gpg --batch --trust-model always --keyring $keyring".
1297 " -a -e -r $bcc -r $to"
1300 print {$po} "\n",$plain,"\n";
1303 $enc .= $_ while <$pi>;
1304 $err .= $_ while <$pe>;
1305 errorlog("($from --> $to) $err") if $err;
1316 my @s = stat(shift) or return;
1321 # wildcard * to perl regexp
1323 local $_ = quotemeta shift;
1324 s/\\\*/.*/g; # allow wildcard *
1329 # extract locale functions into hash of subroutine references
1330 # e.g. \&german ==> $notify{german}
1331 sub locale_functions {
1336 if ($locale and open my $fexpp,"$FEXHOME/locale/$locale/lib/fex.pp") {
1338 s/.*\n(\#\#\# locale functions)/$1/s;
1339 # sub xx {} ==> xx{$locale} = sub {}
1340 s/\nsub (\w+)/\n\$$1\{$locale\} = sub/gs;
1349 my $status = shift || 'new';
1350 my ($to,$keep,$locale,$file,$filename,$comment,$autodelete,$replyto,$mtime);
1353 if ($dkey =~ m:/.+/.+/:) {
1355 $dkey = readlink("$file/dkey");
1357 $file = readlink("$dkeydir/$dkey")
1358 or http_die("internal error: no DKEY $DKEY");
1361 $filename = filename($file);
1364 $mtime = mtime("$file/data") or http_die("internal error: no $file/data");
1365 $comment = slurp("$file/comment") || '';
1366 $replyto = readlink "$file/replyto" || '';
1367 $autodelete = readlink "$file/autodelete"
1368 || readlink "$to/\@AUTODELETE"
1370 $keep = readlink "$file/keep"
1371 || readlink "$to/\@KEEP"
1374 $locale = readlink "$to/\@LOCALE" || readlink "$file/locale" || 'english';
1375 $_ = untaint("$FEXHOME/locale/$locale/lib/lf.pl");
1377 unless ($notify{$locale}) {
1378 $locale = 'english';
1379 $notify{$locale} ||= \¬ify;
1381 return &{$notify{$locale}}(
1384 filename => $filename,
1385 keep => $keep-int((time-$mtime)/$DS),
1386 comment => $comment,
1387 autodelete => $autodelete,
1388 replyto => $replyto,
1392 ########################### locale functions ###########################
1393 # Will be extracted by install process and saved in $FEXHOME/lib/lf.pl #
1394 # You cannot modify them here without re-installing! #
1395 ########################################################################
1399 # my ($status,$dkey,$filename,$keep,$warn,$comment,$autodelete) = @_;
1401 my ($to,$from,$file,$mimefilename,$receiver,$warn,$comment,$autodelete);
1402 my ($size,$bytes,$days,$header,$data,$replyto,$uurl);
1403 my ($mfrom,$mto,$dfrom,$dto);
1408 my $fua = $ENV{HTTP_USER_AGENT}||'';
1410 my $disclaimer = '';
1413 my $boundary = randstring(16);
1414 my ($body,$enc_body);
1418 $warn = $P{warn}||2;
1419 $comment = $P{comment}||'';
1420 $comment = encode_utf8($P{comment}||'') if utf8::is_utf8($comment);
1421 $comment =~ s/^!\*!//; # multi download allow flag
1422 $autodelete = $P{autodelete}||$::autodelete;
1424 $file = untaint(readlink("$dkeydir/$P{dkey}"));
1425 $file =~ s/^\.\.\///;
1426 # make download protocal same as upload protocol
1427 if ($uurl = readlink("$file/uurl") and $uurl =~ /^(\w+):/) {
1429 $durl =~ s/^\w+::/$proto::/;
1431 $index = "$proto://$hostname/index.html";
1432 ($to,$from,$file) = split('/',$file);
1433 $filename = strip_path($P{filename});
1436 $mfrom .= '@'.$mdomain if $mdomain and $mfrom !~ /@/;
1437 $mto .= '@'.$mdomain if $mdomain and $mto !~ /@/;
1438 $keyring = $to.'/@GPG';
1439 # $to = '' if $to eq $from; # ???
1440 $replyto = $P{replyto}||$mfrom;
1441 $header = "From: <$mfrom> ($mfrom via F*EX service $hostname)\n";
1442 $header .= "Reply-To: <$replyto>\n" if $replyto ne $mfrom;
1443 $header .= "To: <$mto>\n";
1444 $data = "$dkeydir/$P{dkey}/data";
1445 $size = $bytes = -s $data;
1446 return unless $size;
1448 "We recommend fexget or fexit for download,\n".
1449 "because these clients can resume the download after an interruption.\n".
1450 "See $proto://$hostname/tools.html";
1455 # "Please avoid download with Internet Explorer, ".
1456 # "because it has too many bugs.\n\n";
1458 if ($filename =~ /\.(tar|zip|7z|arj|rar)$/) {
1460 "$filename is a container file.\n".
1461 "You can unpack it for example with 7zip ".
1462 "(http://www.7-zip.org/download.html)";
1464 if ($limited_download =~ /^y/i) {
1466 'This download link only works for you, you cannot distribute it.';
1469 $size = "$size Bytes";
1470 } elsif ($size/1024 < 2048) {
1471 $size = int($size/1024)." kB";
1473 $size = int($size/1024/1024)." MB";
1475 if ($autodelete eq 'YES') {
1476 $autodelete = "WARNING: After download (or view with a web browser!), "
1477 . "the file will be deleted!";
1478 } elsif ($autodelete eq 'DELAY') {
1479 $autodelete = "WARNING: When you download the file it will be deleted "
1480 . "soon afterwards!";
1488 $mimefilename = $filename;
1489 if ($mimefilename =~ s/([_\?\=\x00-\x1F\x7F-\xFF])/sprintf("=%02X",ord($1))/eog) {
1490 $mimefilename =~ s/ /_/g;
1491 $mimefilename = '=?UTF-8?Q?'.$mimefilename.'?=';
1495 unless ($fileid = readlink("$dkeydir/$P{dkey}/id")) {
1496 my @s = stat($data);
1497 $fileid = @s ? $s[1].$s[9] : 0;
1500 if ($P{status} eq 'new') {
1502 $header .= "Subject: F*EX-upload: $mimefilename\n";
1505 $header .= "Subject: reminder F*EX-upload: $mimefilename\n";
1507 $header .= "X-FEX-Client-Address: $fra\n" if $fra;
1508 $header .= "X-FEX-Client-Agent: $fua\n" if $fua;
1509 foreach my $u (@durl?@durl:($durl)) {
1510 my $durl = sprintf("%s/%s/%s",$u,$P{dkey},normalize_filename($filename));
1511 $header .= "X-FEX-URL: $durl\n" unless -s $keyring;
1512 $download .= "$durl\n";
1515 "X-FEX-Filesize: $bytes\n".
1516 "X-FEX-File-ID: $fileid\n".
1517 "X-FEX-Fexmaster: $ENV{SERVER_ADMIN}\n".
1519 "MIME-Version: 1.0\n";
1520 if ($comment =~ s/^\[(\@(.*?))\]\s*//) {
1521 $receiver = "group $1";
1522 if ($_ = readlink "$from/\@GROUP/$2" and m:^../../(.+?)/:) {
1523 $receiver .= " (maintainer: $1)";
1528 if ($days == 1) { $days .= " day" }
1529 else { $days .= " days" }
1531 # explicite sender set in fex.ph?
1533 map { s/^From: <$mfrom/From: <$sender_from/ } $header;
1534 open $sendmail,'|-',$sendmail,$mto,$bcc
1535 or http_die("cannot start sendmail - $!");
1537 # for special remote domains do not use same domain in From,
1538 # because remote MTA will probably reject this e-mail
1539 $dfrom = $1 if $mfrom =~ /@(.+)/;
1540 $dto = $1 if $mto =~ /@(.+)/;
1541 if ($dfrom and $dto and @remote_domains and
1543 $dfrom =~ /(^|\.)$_$/ and $dto =~ /(^|\.)$_$/
1546 $header =~ s/(From: <)\Q$mfrom\E(.*?)\n/$1$admin$2\nReply-To: $mfrom\n/;
1547 open $sendmail,'|-',$sendmail,$mto,$bcc
1548 or http_die("cannot start sendmail - $!");
1550 open $sendmail,'|-',$sendmail,'-f',$mfrom,$mto,$bcc
1551 or http_die("cannot start sendmail - $!");
1554 $comment .= "\n" if $comment;
1555 if ($comment =~ s/^!(shortmail|\.)!\s*//i
1556 or (readlink("$to/\@NOTIFICATION")||'') =~ /short/i
1564 $disclaimer = slurp("$from/\@DISCLAIMER") || qqq(qq(
1567 'F*EX is not an archive, it is a transfer system for personal files.'
1568 'For more information see $index'
1570 'Questions? ==> F*EX admin: $admin'
1572 $disclaimer .= "\n" . $::disclaimer if $::disclaimer;
1575 '$from has uploaded the file'
1577 '($size) for $receiver. Use'
1580 'to download this file within $days.'
1587 $body =~ s/\n\n+/\n\n/g;
1589 $enc_body = gpg_encrypt($body,$to,$keyring,$from);
1594 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";'
1595 '\tboundary="$boundary"'
1596 'Content-Disposition: inline'
1600 'Content-Type: application/pgp-encrypted'
1601 'Content-Disposition: attachment'
1606 'Content-Type: application/octet-stream'
1607 'Content-Disposition: inline; filename="fex.pgp"'
1614 "Content-Type: text/plain; charset=UTF-8\n".
1615 "Content-Transfer-Encoding: 8bit\n";
1617 print {$sendmail} $header,"\n",$body;
1618 close $sendmail and return $to;
1619 http_die("cannot send notification e-mail (sendmail error $!)");
1625 my ($expire,$user) = @_;
1626 my $fexsend = "$FEXHOME/bin/fexsend";
1627 my $reactivation = "$FEXLIB/reactivation.txt";
1633 my $lr = "$FEXHOME/locale/$locale/lib/reactivation.txt";
1634 $reactivation = $lr if -f $lr and -s $lr;
1636 $fexsend .= " -M -D -k 30 -C"
1637 ." 'Your F*EX account has been inactive for $expire days,"
1638 ." you must download this file to reactivate it."
1639 ." Otherwise your account will be deleted.'"
1640 ." $reactivation $user";
1641 # on error show STDOUT and STDERR
1642 my $fo = `$fexsend 2>&1`;
1643 warn $fexsend.'\n'.$fo if $?;
1645 warn "$0: cannot execute $fexsend for reactivation()\n";