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 # https://securityheaders.io/
67 # https://scotthelme.co.uk/hardening-your-http-response-headers/
68 # http://content-security-policy.com/
70 # "Content-Security-Policy: sandbox allow-forms allow-scripts",
71 "Content-Security-Policy: script-src 'self' 'unsafe-inline'",
72 "X-Frame-Options: SAMEORIGIN",
73 "X-XSS-Protection: 1; mode=block",
74 "X-Content-Type-Options: nosniff",
77 $FHS = -f '/etc/fex/fex.ph' and -d '/usr/share/fex/lib';
80 $ENV{FEXHOME} = $FEXHOME = '/usr/share/fex';
81 $spooldir = '/var/spool/fex';
82 $logdir = '/var/log/fex';
83 $docdir = '/var/lib/fex/htdocs';
84 $notify_newrelease = '';
87 # allowed download managers (HTTP User-Agent)
88 $adlm = '^(Axel|fex)';
91 require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
93 $fop_auth = 0 if $fop_auth =~ /no/i;
94 $mail_authid = 0 if $mail_authid =~ /no/i;
95 $force_https = 0 if $force_https =~ /no/i;
96 $debug = 0 if $debug =~ /no/i;
98 @logdir = ($logdir) unless @logdir;
101 # allowed multi download recipients: from any ip, any times
102 if (@mailing_lists) {
103 $amdl = '^('.join('|',map { quotewild($_) } @mailing_lists).')$';
108 # check for name based virtual host
109 $vhost = vhost($ENV{'HTTP_HOST'});
111 $RB = 0; # read POST bytes
113 push @doc_dirs,$docdir;
114 foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
118 $nomail = ($mailmode =~ /^MANUAL|nomail$/i);
120 if (not $nomail and not -x $sendmail) {
121 http_die("found no sendmail");
123 http_die("cannot determine the server hostname") unless $hostname;
125 $ENV{PROTO} = 'http' unless $ENV{PROTO};
126 $keep = $keep_default ||= $keep || 5;
127 $fra = $ENV{REMOTE_ADDR} || '';
128 $sid = $ENV{SID} || '';
130 $dkeydir = "$spooldir/.dkeys"; # download keys
131 $ukeydir = "$spooldir/.ukeys"; # upload keys
132 $akeydir = "$spooldir/.akeys"; # authentification keys
133 $skeydir = "$spooldir/.skeys"; # subuser authentification keys
134 $gkeydir = "$spooldir/.gkeys"; # group authentification keys
135 $xkeydir = "$spooldir/.xkeys"; # extra download keys
136 $lockdir = "$spooldir/.locks"; # download lock files
138 if (my $ra = $ENV{REMOTE_ADDR} and $max_fail) {
139 mkdirp("$spooldir/.fail");
140 $faillog = "$spooldir/.fail/$ra";
144 $admin = $ENV{SERVER_ADMIN} ? $ENV{SERVER_ADMIN} : 'fex@'.$hostname;
147 # $ENV{SERVER_ADMIN} may be set empty in fex.ph!
148 $ENV{SERVER_ADMIN} = $admin unless defined $ENV{SERVER_ADMIN};
153 if (my $cookie = $ENV{HTTP_COOKIE}) {
154 if ($cookie =~ /\bakey=(\w+)/) { $akey = $1 }
155 # elsif ($cookie =~ /\bskey=(\w+)/) { $skey = $1 }
160 if ($default_locale and not grep /^$default_locale$/,@locales) {
161 push @locales,$default_locale;
164 $default_locale = $locales[0];
168 $default_locale ||= 'english';
170 # $durl is first default fop download URL
171 # @durl is optional mandatory fop download URL list (from fex.ph)
175 my $xinetd = '/etc/xinetd.d/fex';
179 } elsif ($ENV{HTTP_HOST} and $ENV{PROTO}) {
181 ($host,$port) = split(':',$ENV{HTTP_HOST}||'');
186 if (open $xinetd,$xinetd) {
188 if (/^\s*port\s*=\s*(\d+)/) {
197 # use same protocal as uploader for download
198 if ($ENV{PROTO} eq 'https' and $port == 443 or $port == 80) {
199 $durl = "$ENV{PROTO}://$host/fop";
201 $durl = "$ENV{PROTO}://$host:$port/fop";
204 if (open $xinetd,$xinetd) {
206 if (/^\s*port\s*=\s*(\d+)/) {
214 $durl = "http://$hostname/fop";
216 $durl = "http://$hostname:$port/fop";
220 @durl = ($durl) unless @durl;
224 exec($FEXHOME.'/bin/fexsrv') if $ENV{KEEP_ALIVE};
231 $cont = shift || 'request accepted: continue';
233 http_header('200 ok');
234 print html_header($head||$ENV{SERVER_NAME});
236 '<script type="text/javascript">'
237 ' window.location.replace("$url");'
240 ' <h3><a href="$url">$cont</a></h3>'
249 print header(),"<pre>\n";
250 print "file = $file\n";
251 foreach $v (keys %ENV) {
252 print $v,' = "',$ENV{$v},"\"\n";
259 foreach (@_) { syswrite STDOUT,"$_\r\n" }
280 return if $HTTP_HEADER;
281 $HTTP_HEADER = $status;
285 nvt_print("HTTP/1.1 $status");
286 nvt_print("X-Message: $msg");
287 # nvt_print("X-SID: $ENV{SID}") if $ENV{SID};
288 nvt_print("Server: fexsrv");
289 nvt_print("Expires: 0");
290 nvt_print("Cache-Control: no-cache");
292 # https://www.owasp.org/index.php/HTTP_Strict_Transport_Security
293 # https://scotthelme.co.uk/hsts-the-missing-link-in-tls/
294 nvt_print("Strict-Transport-Security: max-age=2851200; preload");
296 nvt_print($_) foreach(@extra_header);
298 $akey = md5_hex("$from:$id") if $id and $from;
300 nvt_print("Set-Cookie: akey=$akey; path=/; Max-Age=9999; Discard");
303 # nvt_print("Set-Cookie: skey=$skey; Max-Age=9999; Discard");
306 nvt_print("Set-Cookie: locale=$locale");
309 unless (grep /^Content-Type:/i,@_) {
310 # nvt_print("Content-Type: text/html; charset=ISO-8859-1");
311 nvt_print("Content-Type: text/html; charset=UTF-8");
320 my $header = 'header.html';
323 # http://www.w3.org/TR/html401/struct/global.html
324 # http://www.w3.org/International/O-charset
328 ' <meta http-equiv="expires" content="0">'
329 ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8">'
330 ' <title>$title</title>'
333 # '<!-- <style type="text/css">\@import "/fex.css";</style> -->'
335 if ($0 =~ /fexdev/) { $head .= "<body bgcolor=\"pink\">\n" }
336 else { $head .= "<body>\n" }
338 $title =~ s:F\*EX:<a href="/index.html">F*EX</a>:;
340 if (open $header,'<',"$docdir/$header") {
341 $head .= $_ while <$header>;
345 $head .= &$prolog($title) if defined($prolog);
349 '<h1><a href="%s"><img align=center src="%s" border=0></a>%s</h1>',
350 $H1_extra[0],$H1_extra[1]||'',$title
353 $head .= "<h1>$title</h1>";
365 my $isodate = isodate(time);
367 $msg =~ s/[\s\n]+/ /g;
368 $msg =~ s/<.+?>//g; # remove HTML
369 map { s/<script.*?>//gi } @msg;
373 # cannot send standard HTTP Status-Code 400, because stupid
374 # Internet Explorer then refuses to display HTML body!
375 http_header("666 Bad Request - $msg");
376 print html_header($error);
377 print 'ERROR: ',join("<p>\n",@msg),"\n";
383 ' <a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>'
394 unless ($ENV{GATEWAY_INTERFACE}) {
395 warn "$0: @_\n"; # must not die, because of fex_cleanup!
401 # create special error file on upload
403 my $ukey = "$spooldir/.ukeys/$uid";
404 $ukey .= "/error" if -d $ukey;
406 if (open $ukey,'>',$ukey) {
407 print {$ukey} join("\n",@_),"\n";
412 html_error($error||'',@_);
417 if (my $status = readlink '@MAINTENANCE') {
418 my $isodate = isodate(time);
419 http_header('666 MAINTENANCE');
420 print html_header($head||'');
423 "<h1>Server is in maintenance mode</h1>"
427 "<address>$ENV{HTTP_HOST} $isodate</address>"
439 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
441 if (-e "$user/\@DISABLED") {
442 my $isodate = isodate(time);
443 http_header('666 DISABLED');
444 print html_header($head);
446 "<h3>$user is disabled</h3>"
447 "Contact $ENV{SERVER_ADMIN} for details"
449 "<address>$ENV{HTTP_HOST} $isodate</address>"
458 my @d = localtime shift;
459 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
460 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
466 $s =~ s{([\=\x00-\x20\x7F-\xA0])}{sprintf("=%02X",ord($1))}eog;
471 # from MIME::Base64::Perl
480 return '' unless length;
482 for ($i = 0; $i <= $l; $i += 60) {
483 $uu .= "M" . substr($_,$i,60);
486 $uu .= chr(32+(length)*3/4) . $_ if $_;
487 return unpack ("u",$uu);
491 # short base64 encoding
497 $_ = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
498 tr|` -_|AA-Za-z0-9+/|;
499 $x = (3 - length($_[0]) % 3) % 3;
506 # simulate a "rm -rf", but never removes '..'
507 # return number of removed files
516 next if /(^|\/)\.\.$/;
518 if (-d $file and not -l $file) {
520 opendir D,$dir or next;
521 while ($file = readdir D) {
522 next if $file eq '.' or $file eq '..';
523 $dels += rmrf("$dir/$file");
526 rmdir $dir and $dels++;
528 unlink $file and $dels++;
536 my $hostname = hostname;
541 $_ = `hostname 2>/dev/null`;
542 $hostname = /(.+)/ ? $1 : '';
544 if ($hostname !~ /\./ and open my $rc,'/etc/resolv.conf') {
546 if (/^\s*domain\s+([\w.-]+)/) {
550 if (/^\s*search\s+([\w.-]+)/) {
555 $hostname .= ".$domain" if $domain;
557 if ($hostname !~ /\./ and $admin and $admin =~ /\@([\w.-]+)/) {
565 # strip off path names (Windows or UNIX)
569 s/.*\\// if /^([A-Z]:)?\\/;
576 # substitute all critcal chars
580 return '' unless defined $_;
582 # we need perl native utf8 (see perldoc utf8)
583 $_ = decode_utf8($_) unless utf8::is_utf8($_);
586 s/[\x00-\x1F\x80-\x9F]/_/g;
590 return encode_utf8($_);
594 # substitute all critcal chars
598 return '' unless defined $_;
608 # substitute all critcal chars with underscore
609 sub normalize_filename {
614 # we need native utf8
615 $_ = decode_utf8($_) unless utf8::is_utf8($_);
619 # substitute all critcal chars with underscore
620 s/[^a-zA-Z0-9_=.+-]/_/g;
623 return encode_utf8($_);
627 sub normalize_email {
630 s/[^\w_.+=!~#^\@\-]//g;
640 $user = lc(urldecode(despace($user)));
641 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
642 checkaddress($user) or http_die("$user is not a valid e-mail address");
643 return untaint($user);
649 s/%([a-f0-9]{2})/chr(hex($1))/gie;
666 http_die("\"$1\" is not allowed at beginning of $input");
668 if (/([\/\"\'\\<>;])/) {
669 http_die(sprintf("\"&#%s;\" is not allowed in %s",ord($1),$input));
672 http_die("\"$1\" is not allowed at end of $input");
675 http_die("control characters are not allowed in $input");
686 local ($domain,$dns);
688 $a =~ s/:\w+=.*//; # remove options from address
690 return $a if $a eq 'anonymous';
692 $a .= '@'.$mdomain if $mdomain and $a !~ /@/;
694 $re = '^[.@-]|@.*@|local(host|domain)$|["\'\`\|\s()<>/;,]';
696 debuglog("$a has illegal syntax ($re)");
699 $re = '^[!^=~#_:.+*{}\w\-\[\]]+\@(\w[.\w\-]*\.[a-z]+)$';
703 local $SIG{__DIE__} = sub { die "\n" };
706 $dns = Net::DNS::Resolver->new->query($domain)||mx($domain);
707 unless ($dns or mx('uni-stuttgart.de')) {
708 http_die("Internal error: bad resolver");
715 debuglog("no A or MX DNS record found for $domain");
719 debuglog("$a does not match e-mail regexp ($re)");
725 # check forbidden addresses
731 $a .= '@'.$mdomain if $mdomain and $a !~ /@/;
732 return $a if -d "$spooldir/$a"; # ok, if user already exists
733 if (@forbidden_recipients) {
734 foreach (@forbidden_recipients) {
736 # skip public recipients
737 if (@public_recipients) {
738 foreach $pr (@public_recipients) {
739 return $a if $a eq lc $pr;
742 return '' if $a =~ /^$fr$/i;
751 my @rc = ('A'..'Z','a'..'z',0..9 );
755 for (1..$n) { $rs .= $rc[int(rand($rn))] };
767 http_die("cannot mkdir /") unless $dir;
769 if ($pdir =~ s:/[^/]+$::) {
770 mkdirp($pdir) unless -d $pdir;
773 mkdir $dir,0770 or http_die("mkdir $dir - $!");
782 if ($rid and $ENV{SID} and $id =~ /^MD5H:/) {
783 $rid = 'MD5H:'.md5_hex($rid.$ENV{SID});
789 # test if ip is in iplist (ipv4/ipv6)
790 # iplist is an array with ips and ip-ranges
799 if ($ip =~ /\./ and $i =~ /\./ or $ip =~ /:/ and $i =~ /:/) {
800 if ($i =~ /(.+)-(.+)/) {
804 return $ip if $ipe ge $ia and $ipe le $ib;
806 return $ip if $ipe eq ipe($i);
813 # ip expand (ipv4/ipv6)
817 if (/^\d+\.\d+\.\d+\.\d+$/) {
818 s/\b(\d\d?)\b/sprintf "%03d",$1/ge;
819 } elsif (/^[:\w]+:\w+$/) {
820 s/\b(\w+)\b/sprintf "%04s",$1/ge;
822 while (s/::/::0000:/) { last if length > 39 }
835 if (open $file,'<',"$file/filename") {
836 $filename = <$file>||'';
843 $filename =~ s:.*/::;
852 s/(^[.~]|[^\w.,=:~^+-])/sprintf "%%%X",ord($1)/ge;
857 # file and document log
859 my ($log,$file,$s,$size) = @_;
860 my $ra = $ENV{REMOTE_ADDR}||'-';
863 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
866 $msg = sprintf "%s [%s_%s] %s %s %s/%s\n",
867 isodate(time),$$,$ENV{REQUESTCOUNT},$ra,encode_Q($file),$s,$size;
878 return unless $debug and @_;
879 unless ($debuglog and fileno $debuglog) {
880 my $ddir = "$spooldir/.debug";
881 mkdir $ddir,0770 unless -d $ddir;
883 $prg = untaint($prg);
884 $debuglog = sprintf("%s/%s_%s_%s.%s",
885 $ddir,time,$$,$ENV{REQUESTCOUNT}||0,$prg);
886 $debuglog =~ s/\s/_/g;
887 # http://perldoc.perl.org/perlunifaq.html#What-is-a-%22wide-character%22%3f
888 # open $debuglog,'>>:encoding(UTF-8)',$debuglog or return;
889 open $debuglog,'>>',$debuglog or return;
890 # binmode($debuglog,":utf8");
891 autoflush $debuglog 1;
892 # printf {$debuglog} "\n### %s ###\n",isodate(time);
894 while ($_ = shift @_) {
895 $_ = encode_utf8($_) if utf8::is_utf8($_);
897 s/<.+?>//g; # remove HTML
898 print {$debuglog} $_;
899 print "DEBUG: $_" if -t;
908 my $ra = $ENV{REMOTE_ADDR}||'-';
910 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
913 $msg =~ s/[\r\n]+$//;
914 $msg =~ s/[\r\n]+/ /;
915 $msg =~ s/\s*<p>.*//;
916 $msg = sprintf "%s %s %s %s\n",isodate(time),$prg,$ra,$msg;
918 writelog('error.log',$msg);
926 foreach my $logdir (@logdir) {
927 if (open $log,'>>',"$logdir/$log") {
929 seek $log,0,SEEK_END;
937 # failed authentification log
942 if ($faillog and $max_fail_handler and open $faillog,"+>>$faillog") {
943 flock($faillog,LOCK_EX);
944 seek $faillog,0,SEEK_SET;
945 $n++ while <$faillog>;
946 printf {$faillog} "%s %s\n",isodate(time),$request;
948 &$max_fail_handler($ENV{REMOTE_ADDR}) if $n > $max_fail;
952 # remove all white space
964 my $q = "[\'\"]"; # quote delimiter chars " and '
966 # remove first newline and look for default indention
970 # remove trailing spaces at end
975 # first line have a quote delimiter char?
977 # remove heading spaces and delimiter chars
983 # find the line with the fewest heading spaces (and count them)
987 if (/^( *)\S/ and length($1) < $s) { $s = length($1) };
995 return join("\n",@s)."\n";
1003 if (@_ > 1 and defined fileno $_[0]) { $H = shift }
1004 binmode($H,':utf8');
1009 # check sender quota
1010 sub check_sender_quota {
1012 my $squota = $sender_quota||0;
1014 my ($file,$size,%file,$data,$upload);
1017 if (open $qf,'<',"$sender/\@QUOTA") {
1020 $squota = $1 if /sender.*?(\d+)/i;
1025 foreach $file (glob "*/$sender/*") {
1026 $data = "$file/data";
1027 $upload = "$file/upload";
1028 if (not -l $data and $size = -s $data) {
1029 # count hard links only once (= same inode)
1030 my $i = (stat($data))[1]||0;
1031 unless ($file{$i}) {
1035 } elsif (-f $upload) {
1036 # count hard links only once (= same inode)
1037 my $i = (stat($upload))[1]||0;
1038 unless ($file{$i}) {
1039 $size = readlink "$file/size" and $du += $size;
1045 return($squota,int($du/1024/1024));
1049 # check recipient quota
1050 sub check_recipient_quota {
1051 my $recipient = shift;
1052 my $rquota = $recipient_quota||0;
1057 if (open my $qf,'<',"$recipient/\@QUOTA") {
1060 $rquota = $1 if /recipient.*?(\d+)/i;
1065 foreach $file (glob "$recipient/*/*") {
1066 if (-f "$file/upload" and $size = readlink "$file/size") {
1068 } elsif (not -l "$file/data" and $size = -s "$file/data") {
1073 return($rquota,int($du/1024/1024));
1080 chomp($_ = <$file>||'');
1085 # (shell) wildcard matching
1088 my $p = quotemeta shift;
1101 if ($skey) { $logout = "/fup?logout=skey:$skey" }
1102 elsif ($gkey) { $logout = "/fup?logout=gkey:$gkey" }
1103 elsif ($akey) { $logout = "/fup?logout=akey:$akey" }
1104 else { $logout = "/fup?logout" }
1107 '<form name="logout" action="$logout">'
1108 ' <input type="submit" name="logout" value="logout">'
1115 # print data dump of global or local variables in HTML
1116 # input musst be a string, eg: '%ENV'
1122 $_ = eval(qq(use Data::Dumper;Data::Dumper->Dump([\\$v])));
1126 print "<pre>\n$_\n</pre>\n";
1131 my ($file,$link) = @_;
1133 return symlink untaint($link),$file;
1137 # copy file (and modify) or symlink
1138 # returns chomped file contents or link name
1139 # preserves permissions and time stamps
1141 my ($from,$to,$mod) = @_;
1146 $to .= '/'.basename($from) if -d $to;
1148 if (defined($link = readlink $from)) {
1149 mksymlink($to,$link);
1152 open $from,'<',$from or return;
1153 open $to,'>',$to or return;
1158 close $to or http_die("internal error: $to - $!");
1159 if (my @s = stat($from)) {
1161 utime @s[8,9],$to unless $mod;
1174 if (open $file,$file) {
1183 # read one line from STDIN (net socket) and assign it to $_
1184 # return number of read bytes
1185 # also set global variable $RB (read bytes)
1189 if (defined ($_ = <STDIN>)) {
1199 # read forward to given pattern
1201 my $pattern = shift;
1203 while (&nvt_read) { return if /$pattern/ }
1207 # HTTP GET and POST parameters
1209 # fills global variable %PARAM :
1210 # normal parameter is $PARAM{$parameter}
1211 # file parameter is $PARAM{$parameter}{filename} $PARAM{$parameter}{data}
1212 sub parse_parameters {
1213 my $cl = $ENV{X_CONTENT_LENGTH} || $ENV{CONTENT_LENGTH} || 0;
1218 if ($cl > 128*$MB) {
1219 http_die("request too large");
1222 binmode(STDIN,':raw');
1224 foreach (split('&',$ENV{QUERY_STRING})) {
1225 if (/(.+?)=(.*)/) { $PARAM{$1} = $2 }
1226 else { $PARAM{$_} = $_ }
1228 $_ = $ENV{CONTENT_TYPE}||'';
1229 if ($ENV{REQUEST_METHOD} eq 'POST' and /boundary=\"?([\w\-\+\/_]+)/) {
1231 while ($RB<$cl and &nvt_read) { last if /^--\Q$boundary/ }
1232 # continuation lines are not checked!
1233 while ($RB<$cl and &nvt_read) {
1235 if (/^Content-Disposition:.*\s*filename="(.+?)"/i) {
1238 if (/^Content-Disposition:\s*form-data;\s*name="(.+?)"/i) {
1240 # skip rest of mime part header
1241 while ($RB<$cl and &nvt_read) { last if /^\s*$/ }
1244 if ($p =~ /password/i) {
1245 debuglog('*' x length)
1250 last if /^--\Q$boundary/;
1253 unless (defined $_) { die "premature end of HTTP POST\n" }
1254 $data =~ s/\r?\n$//;
1256 $PARAM{$p}{filename} = $filename;
1257 $PARAM{$p}{data} = $data;
1261 last if /^--\Q$boundary--/;
1268 # name based virtual host?
1270 my $hh = shift; # HTTP_HOST
1272 my $locale = $ENV{LOCALE};
1274 # memorized vhost? (default is in fex.ph)
1275 %vhost = split(':',$ENV{VHOST}) if $ENV{VHOST};
1277 if (%vhost and $hh and $hh =~ s/^([\w\.-]+).*/$1/) {
1278 if ($vhost = $vhost{$hh} and -f "$vhost/lib/fex.ph") {
1279 $ENV{VHOST} = "$hh:$vhost"; # memorize vhost for next run
1280 $ENV{FEXLIB} = $FEXLIB = "$vhost/lib";
1281 $logdir = $spooldir = "$vhost/spool";
1282 $docdir = "$vhost/htdocs";
1283 @logdir = ($logdir);
1284 if ($locale and -e "$vhost/locale/$locale/lib/fex.ph") {
1285 $ENV{FEXLIB} = $FEXLIB = "$vhost/locale/$locale/lib";
1287 require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
1288 $ENV{SERVER_NAME} = $hostname;
1289 @doc_dirs = ($docdir);
1290 foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
1300 my ($plain,$to,$keyring,$from) = @_;
1301 my ($pid,$pi,$po,$pe,$enc,$err);
1306 $pid = open3($po,$pi,$pe,
1307 "gpg --batch --trust-model always --keyring $keyring".
1308 " -a -e -r $bcc -r $to"
1311 print {$po} "\n",$plain,"\n";
1314 $enc .= $_ while <$pi>;
1315 $err .= $_ while <$pe>;
1316 errorlog("($from --> $to) $err") if $err;
1327 my @s = stat(shift) or return;
1332 # wildcard * to perl regexp
1334 local $_ = quotemeta shift;
1335 s/\\\*/.*/g; # allow wildcard *
1340 # extract locale functions into hash of subroutine references
1341 # e.g. \&german ==> $notify{german}
1342 sub locale_functions {
1347 if ($locale and open my $fexpp,"$FEXHOME/locale/$locale/lib/fex.pp") {
1349 s/.*\n(\#\#\# locale functions)/$1/s;
1350 # sub xx {} ==> xx{$locale} = sub {}
1351 s/\nsub (\w+)/\n\$$1\{$locale\} = sub/gs;
1360 my $status = shift || 'new';
1361 my ($to,$keep,$locale,$file,$filename,$comment,$autodelete,$replyto,$mtime);
1364 if ($dkey =~ m:/.+/.+/:) {
1366 $dkey = readlink("$file/dkey");
1368 $file = readlink("$dkeydir/$dkey")
1369 or http_die("internal error: no DKEY $DKEY");
1372 $filename = filename($file);
1375 $mtime = mtime("$file/data") or http_die("internal error: no $file/data");
1376 $comment = slurp("$file/comment") || '';
1377 $replyto = readlink "$file/replyto" || '';
1378 $autodelete = readlink "$file/autodelete"
1379 || readlink "$to/\@AUTODELETE"
1381 $keep = readlink "$file/keep"
1382 || readlink "$to/\@KEEP"
1385 $locale = readlink "$to/\@LOCALE" || readlink "$file/locale" || 'english';
1386 $_ = untaint("$FEXHOME/locale/$locale/lib/lf.pl");
1388 unless ($notify{$locale}) {
1389 $locale = 'english';
1390 $notify{$locale} ||= \¬ify;
1392 return &{$notify{$locale}}(
1395 filename => $filename,
1396 keep => $keep-int((time-$mtime)/$DS),
1397 comment => $comment,
1398 autodelete => $autodelete,
1399 replyto => $replyto,
1403 ########################### locale functions ###########################
1404 # Will be extracted by install process and saved in $FEXHOME/lib/lf.pl #
1405 # You cannot modify them here without re-installing! #
1406 ########################################################################
1410 # my ($status,$dkey,$filename,$keep,$warn,$comment,$autodelete) = @_;
1412 my ($to,$from,$file,$mimefilename,$receiver,$warn,$comment,$autodelete);
1413 my ($size,$bytes,$days,$header,$data,$replyto,$uurl);
1414 my ($mfrom,$mto,$dfrom,$dto);
1419 my $fua = $ENV{HTTP_USER_AGENT}||'';
1421 my $disclaimer = '';
1424 my $boundary = randstring(16);
1425 my ($body,$enc_body);
1429 $warn = $P{warn}||2;
1430 $comment = $P{comment}||'';
1431 $comment = encode_utf8($P{comment}||'') if utf8::is_utf8($comment);
1432 $comment =~ s/^!\*!//; # multi download allow flag
1433 $autodelete = $P{autodelete}||$::autodelete;
1435 $file = untaint(readlink("$dkeydir/$P{dkey}"));
1436 $file =~ s/^\.\.\///;
1437 # make download protocal same as upload protocol
1438 if ($uurl = readlink("$file/uurl") and $uurl =~ /^(\w+):/) {
1440 $durl =~ s/^\w+::/$proto::/;
1442 $index = "$proto://$hostname/index.html";
1443 ($to,$from,$file) = split('/',$file);
1444 $filename = strip_path($P{filename});
1447 $mfrom .= '@'.$mdomain if $mdomain and $mfrom !~ /@/;
1448 $mto .= '@'.$mdomain if $mdomain and $mto !~ /@/;
1449 $keyring = $to.'/@GPG';
1450 # $to = '' if $to eq $from; # ???
1451 $replyto = $P{replyto}||$mfrom;
1452 $header = "From: <$mfrom> ($mfrom via F*EX service $hostname)\n";
1453 $header .= "Reply-To: <$replyto>\n" if $replyto ne $mfrom;
1454 $header .= "To: <$mto>\n";
1455 $data = "$dkeydir/$P{dkey}/data";
1456 $size = $bytes = -s $data;
1457 return unless $size;
1459 "We recommend fexget or fexit for download,\n".
1460 "because these clients can resume the download after an interruption.\n".
1461 "See $proto://$hostname/tools.html";
1466 # "Please avoid download with Internet Explorer, ".
1467 # "because it has too many bugs.\n\n";
1469 if ($filename =~ /\.(tar|zip|7z|arj|rar)$/) {
1471 "$filename is a container file.\n".
1472 "You can unpack it for example with 7zip ".
1473 "(http://www.7-zip.org/download.html)";
1475 if ($limited_download =~ /^y/i) {
1477 'This download link only works for you, you cannot distribute it.';
1480 $size = "$size Bytes";
1481 } elsif ($size/1024 < 2048) {
1482 $size = int($size/1024)." kB";
1484 $size = int($size/1024/1024)." MB";
1486 if ($autodelete eq 'YES') {
1487 $autodelete = "WARNING: After download (or view with a web browser!), "
1488 . "the file will be deleted!";
1489 } elsif ($autodelete eq 'DELAY') {
1490 $autodelete = "WARNING: When you download the file it will be deleted "
1491 . "soon afterwards!";
1499 $mimefilename = $filename;
1500 if ($mimefilename =~ s/([_\?\=\x00-\x1F\x7F-\xFF])/sprintf("=%02X",ord($1))/eog) {
1501 $mimefilename =~ s/ /_/g;
1502 $mimefilename = '=?UTF-8?Q?'.$mimefilename.'?=';
1506 unless ($fileid = readlink("$dkeydir/$P{dkey}/id")) {
1507 my @s = stat($data);
1508 $fileid = @s ? $s[1].$s[9] : 0;
1511 if ($P{status} eq 'new') {
1513 $header .= "Subject: F*EX-upload: $mimefilename\n";
1516 $header .= "Subject: reminder F*EX-upload: $mimefilename\n";
1518 $header .= "X-FEX-Client-Address: $fra\n" if $fra;
1519 $header .= "X-FEX-Client-Agent: $fua\n" if $fua;
1520 foreach my $u (@durl?@durl:($durl)) {
1521 my $durl = sprintf("%s/%s/%s",$u,$P{dkey},normalize_filename($filename));
1522 $header .= "X-FEX-URL: $durl\n" unless -s $keyring;
1523 $download .= "$durl\n";
1526 "X-FEX-Filesize: $bytes\n".
1527 "X-FEX-File-ID: $fileid\n".
1528 "X-FEX-Fexmaster: $ENV{SERVER_ADMIN}\n".
1530 "MIME-Version: 1.0\n";
1531 if ($comment =~ s/^\[(\@(.*?))\]\s*//) {
1532 $receiver = "group $1";
1533 if ($_ = readlink "$from/\@GROUP/$2" and m:^../../(.+?)/:) {
1534 $receiver .= " (maintainer: $1)";
1539 if ($days == 1) { $days .= " day" }
1540 else { $days .= " days" }
1542 # explicite sender set in fex.ph?
1544 map { s/^From: <$mfrom/From: <$sender_from/ } $header;
1545 open $sendmail,'|-',$sendmail,$mto,$bcc
1546 or http_die("cannot start sendmail - $!");
1548 # for special remote domains do not use same domain in From,
1549 # because remote MTA will probably reject this e-mail
1550 $dfrom = $1 if $mfrom =~ /@(.+)/;
1551 $dto = $1 if $mto =~ /@(.+)/;
1552 if ($dfrom and $dto and @remote_domains and
1554 $dfrom =~ /(^|\.)$_$/ and $dto =~ /(^|\.)$_$/
1557 $header =~ s/(From: <)\Q$mfrom\E(.*?)\n/$1$admin$2\nReply-To: $mfrom\n/;
1558 open $sendmail,'|-',$sendmail,$mto,$bcc
1559 or http_die("cannot start sendmail - $!");
1561 open $sendmail,'|-',$sendmail,'-f',$mfrom,$mto,$bcc
1562 or http_die("cannot start sendmail - $!");
1565 $comment = "\n$comment\n" if $comment;
1566 if ($comment =~ s/\n!(shortmail|\.)!\s*//i
1567 or (readlink("$to/\@NOTIFICATION")||'') =~ /short/i
1575 $disclaimer = slurp("$from/\@DISCLAIMER") || qqq(qq(
1578 'F*EX is not an archive, it is a transfer system for personal files.'
1579 'For more information see $index'
1581 'Questions? ==> F*EX admin: $admin'
1583 $disclaimer .= "\n$::disclaimer\n" if $::disclaimer;
1586 '$from has uploaded the file'
1588 '($size) for $receiver. Use'
1591 'to download this file within $days.'
1598 $body =~ s/\n\n+/\n\n/g;
1600 $enc_body = gpg_encrypt($body,$to,$keyring,$from);
1605 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";'
1606 '\tboundary="$boundary"'
1607 'Content-Disposition: inline'
1611 'Content-Type: application/pgp-encrypted'
1612 'Content-Disposition: attachment'
1617 'Content-Type: application/octet-stream'
1618 'Content-Disposition: inline; filename="fex.pgp"'
1625 "Content-Type: text/plain; charset=UTF-8\n".
1626 "Content-Transfer-Encoding: 8bit\n";
1628 print {$sendmail} $header,"\n",$body;
1629 close $sendmail and return $to;
1630 http_die("cannot send notification e-mail (sendmail error $!)");
1636 my ($expire,$user) = @_;
1637 my $fexsend = "$FEXHOME/bin/fexsend";
1638 my $reactivation = "$FEXLIB/reactivation.txt";
1644 my $lr = "$FEXHOME/locale/$locale/lib/reactivation.txt";
1645 $reactivation = $lr if -f $lr and -s $lr;
1647 $fexsend .= " -M -D -k 30 -C"
1648 ." 'Your F*EX account has been inactive for $expire days,"
1649 ." you must download this file to reactivate it."
1650 ." Otherwise your account will be deleted.'"
1651 ." $reactivation $user";
1652 # on error show STDOUT and STDERR
1653 my $fo = `$fexsend 2>&1`;
1654 warn $fexsend.'\n'.$fo if $?;
1656 warn "$0: cannot execute $fexsend for reactivation()\n";