4 use Fcntl qw':flock :seek :mode';
8 use Digest::MD5 qw'md5_hex';
11 use Symbol qw'gensym';
13 # set and untaint ENV if not in CLI (fexsrv provides clean ENV)
15 foreach my $v (keys %ENV) {
16 ($ENV{$v}) = ($ENV{$v} =~ /(.*)/s) if defined $ENV{$v};
18 $ENV{PATH} = '/usr/local/bin:/bin:/usr/bin';
23 unless ($FEXLIB = $ENV{FEXLIB} and -d $FEXLIB) {
24 die "$0: found no FEXLIB - fexsrv needs full path\n"
30 # $FEXHOME is top-level directory of F*EX installation or vhost
31 # $ENV{HOME} is login-directory of user fex
32 # in default-installation both are equal, but they may differ
33 $FEXHOME = $ENV{FEXHOME} or $ENV{FEXHOME} = $FEXHOME = dirname($FEXLIB);
38 $hostname = gethostname();
39 $tmpdir = $ENV{TMPDIR} || '/var/tmp';
40 $spooldir = $FEXHOME.'/spool';
41 $docdir = $FEXHOME.'/htdocs';
45 $limited_download = 'YES'; # multiple downloads only from same client
46 $fex_yourself = 'YES'; # allow SENDER = RECIPIENT
48 $recipient_quota = 0; # MB
49 $sender_quota = 0; # MB
50 $timeout = 30; # seconds
51 $bs = 2**16; # I/O blocksize
52 $DS = 60*60*24; # seconds in a day
53 $MB = 1024*1024; # binary Mega
55 $sendmail = '/usr/lib/sendmail';
56 $sendmail = '/usr/sbin/sendmail' unless -x $sendmail;
65 $FHS = -f '/etc/fex/fex.ph' and -d '/usr/share/fex/lib';
68 $ENV{FEXHOME} = $FEXHOME = '/usr/share/fex';
69 $spooldir = '/var/spool/fex';
70 $logdir = '/var/log/fex';
71 $docdir = '/var/lib/fex/htdocs';
72 $notify_newrelease = '';
75 # allowed download managers (HTTP User-Agent)
76 $adlm = '^(Axel|fex)';
78 # allowed multi download recipients
79 $amdl = '^(anonymous|_fexmail_)';
82 require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
84 $fop_auth = 0 if $fop_auth =~ /no/i;
85 $mail_authid = 0 if $mail_authid =~ /no/i;
86 $force_https = 0 if $force_https =~ /no/i;
87 $debug = 0 if $debug =~ /no/i;
89 @logdir = ($logdir) unless @logdir;
92 # check for name based virtual host
93 $vhost = vhost($ENV{'HTTP_HOST'});
95 $RB = 0; # read POST bytes
97 push @doc_dirs,$docdir;
98 foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
102 $nomail = ($mailmode =~ /^MANUAL|nomail$/i);
104 if (not $nomail and not -x $sendmail) {
105 http_die("found no sendmail");
107 http_die("cannot determine the server hostname") unless $hostname;
109 $ENV{PROTO} = 'http' unless $ENV{PROTO};
110 $keep = $keep_default ||= $keep || 5;
111 $fra = $ENV{REMOTE_ADDR} || '';
112 $sid = $ENV{SID} || '';
114 mkdirp($dkeydir = "$spooldir/.dkeys"); # download keys
115 mkdirp($ukeydir = "$spooldir/.ukeys"); # upload keys
116 mkdirp($akeydir = "$spooldir/.akeys"); # authentification keys
117 mkdirp($skeydir = "$spooldir/.skeys"); # subuser authentification keys
118 mkdirp($gkeydir = "$spooldir/.gkeys"); # group authentification keys
119 mkdirp($xkeydir = "$spooldir/.xkeys"); # extra download keys
120 mkdirp($lockdir = "$spooldir/.locks"); # download lock files
122 if (my $ra = $ENV{REMOTE_ADDR} and $max_fail) {
123 mkdirp("$spooldir/.fail");
124 $faillog = "$spooldir/.fail/$ra";
128 $admin = $ENV{SERVER_ADMIN} ? $ENV{SERVER_ADMIN} : 'fex@'.$hostname;
131 # $ENV{SERVER_ADMIN} may be set empty in fex.ph!
132 $ENV{SERVER_ADMIN} = $admin unless defined $ENV{SERVER_ADMIN};
137 if (my $cookie = $ENV{HTTP_COOKIE}) {
138 if ($cookie =~ /\bakey=(\w+)/) { $akey = $1 }
139 # elsif ($cookie =~ /\bskey=(\w+)/) { $skey = $1 }
144 if ($default_locale and not grep /^$default_locale$/,@locales) {
145 push @locales,$default_locale;
148 $default_locale = $locales[0];
152 $default_locale ||= 'english';
154 # $durl is first default fop download URL
155 # @durl is optional mandatory fop download URL list (from fex.ph)
159 } elsif ($ENV{HTTP_HOST} and $ENV{PROTO}) {
163 ($host,$port) = split(':',$ENV{HTTP_HOST}||'');
168 if (open my $xinetd,'<',"/etc/xinetd.d/fex") {
170 if (/^\s*port\s*=\s*(\d+)/) {
179 # use same protocal as uploader for download
180 if ($ENV{PROTO} eq 'https' and $port == 443 or $port == 80) {
181 $durl = "$ENV{PROTO}://$host/fop";
183 $durl = "$ENV{PROTO}://$host:$port/fop";
186 $durl = "http://$hostname/fop";
192 exec($FEXHOME.'/bin/fexsrv') if $ENV{KEEP_ALIVE};
199 $cont = shift || 'request accepted: continue';
201 http_header('200 ok');
202 print html_header($head||$ENV{SERVER_NAME});
204 '<script type="text/javascript">'
205 ' window.location.replace("$url");'
208 ' <h3><a href="$url">$cont</a></h3>'
217 print header(),"<pre>\n";
218 print "file = $file\n";
219 foreach $v (keys %ENV) {
220 print $v,' = "',$ENV{$v},"\"\n";
227 foreach (@_) { syswrite STDOUT,"$_\r\n" }
248 return if $HTTP_HEADER;
249 $HTTP_HEADER = $status;
253 nvt_print("HTTP/1.1 $status");
254 nvt_print("X-Message: $msg");
255 # nvt_print("X-SID: $ENV{SID}") if $ENV{SID};
256 nvt_print("Server: fexsrv");
257 nvt_print("Expires: 0");
258 nvt_print("Cache-Control: no-cache");
259 # http://en.wikipedia.org/wiki/Clickjacking
260 nvt_print("X-Frame-Options: SAMEORIGIN");
262 # https://www.owasp.org/index.php/HTTP_Strict_Transport_Security
263 nvt_print("Strict-Transport-Security: max-age=2851200");
267 nvt_print("Set-Cookie: akey=$akey; Max-Age=9999; Discard");
270 # nvt_print("Set-Cookie: skey=$skey; Max-Age=9999; Discard");
273 nvt_print("Set-Cookie: locale=$locale");
276 unless (grep /^Content-Type:/i,@_) {
277 # nvt_print("Content-Type: text/html; charset=ISO-8859-1");
278 nvt_print("Content-Type: text/html; charset=UTF-8");
287 my $header = 'header.html';
290 # http://www.w3.org/TR/html401/struct/global.html
291 # http://www.w3.org/International/O-charset
295 ' <meta http-equiv="expires" content="0">'
296 ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8">'
297 ' <title>$title</title>'
300 # '<!-- <style type="text/css">\@import "/fex.css";</style> -->'
302 if ($0 =~ /fexdev/) { $head .= "<body bgcolor=\"pink\">\n" }
303 else { $head .= "<body>\n" }
305 $title =~ s:F\*EX:<a href="/index.html">F*EX</a>:;
307 if (open $header,'<',"$docdir/$header") {
308 $head .= $_ while <$header>;
312 $head .= &$prolog($title) if defined($prolog);
316 '<h1><a href="%s"><img align=center src="%s" border=0></a>%s</h1>',
317 $H1_extra[0],$H1_extra[1]||'',$title
320 $head .= "<h1>$title</h1>";
332 my $isodate = isodate(time);
334 $msg =~ s/[\s\n]+/ /g;
335 $msg =~ s/<.+?>//g; # remove HTML
336 map { s/<script.*?>//gi } @msg;
340 # cannot send standard HTTP Status-Code 400, because stupid
341 # Internet Explorer then refuses to display HTML body!
342 http_header("666 Bad Request - $msg");
343 print html_header($error);
344 print 'ERROR: ',join("<p>\n",@msg),"\n";
350 ' <a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>'
361 unless ($ENV{GATEWAY_INTERFACE}) {
362 warn "$0: @_\n"; # must not die, because of fex_cleanup!
368 # create special error file on upload
370 my $ukey = "$spooldir/.ukeys/$uid";
371 $ukey .= "/error" if -d $ukey;
373 if (open $ukey,'>',$ukey) {
374 print {$ukey} join("\n",@_),"\n";
379 html_error($error||'',@_);
384 if (my $status = readlink '@MAINTENANCE') {
385 my $isodate = isodate(time);
386 http_header('666 MAINTENANCE');
387 print html_header($head||'');
390 "<h1>Server is in maintenance mode</h1>"
394 "<address>$ENV{HTTP_HOST} $isodate</address>"
406 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
408 if (-e "$user/\@DISABLED") {
409 my $isodate = isodate(time);
410 http_header('666 DISABLED');
411 print html_header($head);
413 "<h3>$user is disabled</h3>"
414 "Contact $ENV{SERVER_ADMIN} for details"
416 "<address>$ENV{HTTP_HOST} $isodate</address>"
425 my @d = localtime shift;
426 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
427 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
433 $s =~ s{([\=\x00-\x20\x7F-\xA0])}{sprintf("=%02X",ord($1))}eog;
438 # from MIME::Base64::Perl
447 return '' unless length;
449 for ($i = 0; $i <= $l; $i += 60) {
450 $uu .= "M" . substr($_,$i,60);
453 $uu .= chr(32+(length)*3/4) . $_ if $_;
454 return unpack ("u",$uu);
458 # short base64 encoding
464 $_ = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
465 tr|` -_|AA-Za-z0-9+/|;
466 $x = (3 - length($_[0]) % 3) % 3;
473 # simulate a "rm -rf", but never removes '..'
474 # return number of removed files
483 next if /(^|\/)\.\.$/;
485 if (-d $file and not -l $file) {
487 opendir D,$dir or next;
488 while ($file = readdir D) {
489 next if $file eq '.' or $file eq '..';
490 $dels += rmrf("$dir/$file");
493 rmdir $dir and $dels++;
495 unlink $file and $dels++;
503 my $hostname = hostname;
508 $_ = `hostname 2>/dev/null`;
509 $hostname = /(.+)/ ? $1 : '';
511 if ($hostname !~ /\./ and open my $rc,'/etc/resolv.conf') {
513 if (/^\s*domain\s+([\w.-]+)/) {
517 if (/^\s*search\s+([\w.-]+)/) {
522 $hostname .= ".$domain" if $domain;
524 if ($hostname !~ /\./ and $admin and $admin =~ /\@([\w.-]+)/) {
532 # strip off path names (Windows or UNIX)
536 s/.*\\// if /^([A-Z]:)?\\/;
543 # substitute all critcal chars
547 return '' unless defined $_;
549 # we need perl native utf8 (see perldoc utf8)
550 $_ = decode_utf8($_) unless utf8::is_utf8($_);
553 s/[\x00-\x1F\x80-\x9F]/_/g;
557 return encode_utf8($_);
561 # substitute all critcal chars
565 return '' unless defined $_;
575 # substitute all critcal chars with underscore
576 sub normalize_filename {
581 # we need native utf8
582 $_ = decode_utf8($_) unless utf8::is_utf8($_);
586 # substitute all critcal chars with underscore
587 s/[^a-zA-Z0-9_=.+-]/_/g;
590 return encode_utf8($_);
594 sub normalize_email {
597 s/[^\w_.+=!~#^\@\-]//g;
607 $user = lc(urldecode(despace($user)));
608 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
609 checkaddress($user) or http_die("$user is not a valid e-mail address");
610 return untaint($user);
616 s/%([a-f0-9]{2})/chr(hex($1))/gie;
633 http_die("\"$1\" is not allowed at beginning of $input");
635 if (/([\/\"\'\\<>;])/) {
636 http_die(sprintf("\"&#%s;\" is not allowed in %s",ord($1),$input));
639 http_die("\"$1\" is not allowed at end of $input");
642 http_die("control characters are not allowed in $input");
653 local ($domain,$dns);
655 $a =~ s/:\w+=.*//; # remove options from address
657 return $a if $a eq 'anonymous';
659 $a .= '@'.$mdomain if $mdomain and $a !~ /@/;
661 $re = '^[.@-]|@.*@|local(host|domain)$|["\'\`\|\s()<>/;,]';
663 debuglog("$a has illegal syntax ($re)");
666 $re = '^[!^=~#_:.+*{}\w\-\[\]]+\@(\w[.\w\-]*\.[a-z]+)$';
670 local $SIG{__DIE__} = sub { die "\n" };
673 $dns = Net::DNS::Resolver->new->query($domain)||mx($domain);
674 unless ($dns or mx('uni-stuttgart.de')) {
675 http_die("Internal error: bad resolver");
682 debuglog("no A or MX DNS record found for $domain");
686 debuglog("$a does not match e-mail regexp ($re)");
692 # check forbidden addresses
698 $a .= '@'.$mdomain if $mdomain and $a !~ /@/;
699 return $a if -d "$spooldir/$a"; # ok, if user already exists
700 if (@forbidden_recipients) {
701 foreach (@forbidden_recipients) {
703 $fr =~ s/\\\*/.*/g; # allow wildcard *
704 # skip public recipients
705 if (@public_recipients) {
706 foreach $pr (@public_recipients) {
707 return $a if $a eq lc $pr;
710 return '' if $a =~ /^$fr$/i;
719 my @rc = ('A'..'Z','a'..'z',0..9 );
723 for (1..$n) { $rs .= $rc[int(rand($rn))] };
735 http_die("cannot mkdir /") unless $dir;
737 if ($pdir =~ s:/[^/]+$::) {
738 mkdirp($pdir) unless -d $pdir;
741 mkdir $dir,0770 or http_die("mkdir $dir - $!");
750 if ($rid and $ENV{SID} and $id =~ /^MD5H:/) {
751 $rid = 'MD5H:'.md5_hex($rid.$ENV{SID});
757 # test if ip is in iplist (ipv4/ipv6)
758 # iplist is an array with ips and ip-ranges
767 if ($ip =~ /\./ and $i =~ /\./ or $ip =~ /:/ and $i =~ /:/) {
768 if ($i =~ /(.+)-(.+)/) {
772 return $ip if $ipe ge $ia and $ipe le $ib;
774 return $ip if $ipe eq ipe($i);
781 # ip expand (ipv4/ipv6)
785 if (/^\d+\.\d+\.\d+\.\d+$/) {
786 s/\b(\d\d?)\b/sprintf "%03d",$1/ge;
787 } elsif (/^[:\w]+:\w+$/) {
788 s/\b(\w+)\b/sprintf "%04s",$1/ge;
790 while (s/::/::0000:/) { last if length > 39 }
803 if (open $file,'<',"$file/filename") {
804 $filename = <$file>||'';
811 $filename =~ s:.*/::;
820 s/(^[.~]|[^\w.,=:~^+-])/sprintf "%%%X",ord($1)/ge;
825 # file and document log
827 my ($log,$file,$s,$size) = @_;
828 my $ra = $ENV{REMOTE_ADDR}||'-';
831 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
834 $msg = sprintf "%s [%s_%s] %s %s %s/%s\n",
835 isodate(time),$$,$ENV{REQUESTCOUNT},$ra,encode_Q($file),$s,$size;
846 return unless $debug and @_;
847 unless ($debuglog and fileno $debuglog) {
848 my $ddir = "$spooldir/.debug";
849 mkdir $ddir,0770 unless -d $ddir;
851 $prg = untaint($prg);
852 $debuglog = sprintf("%s/%s_%s_%s.%s",
853 $ddir,time,$$,$ENV{REQUESTCOUNT}||0,$prg);
854 $debuglog =~ s/\s/_/g;
855 # open $debuglog,'>>:encoding(UTF-8)',$debuglog or return;
856 open $debuglog,'>>',$debuglog or return;
857 autoflush $debuglog 1;
858 # printf {$debuglog} "\n### %s ###\n",isodate(time);
860 while ($_ = shift @_) {
862 s/<.+?>//g; # remove HTML
863 print {$debuglog} $_;
864 print "DEBUG: $_" if -t;
873 my $ra = $ENV{REMOTE_ADDR}||'-';
875 $ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
878 $msg =~ s/[\r\n]+$//;
879 $msg =~ s/[\r\n]+/ /;
880 $msg =~ s/\s*<p>.*//;
881 $msg = sprintf "%s %s %s %s\n",isodate(time),$prg,$ra,$msg;
883 writelog('error.log',$msg);
891 foreach my $logdir (@logdir) {
892 if (open $log,'>>',"$logdir/$log") {
894 seek $log,0,SEEK_END;
902 # failed authentification log
907 if ($faillog and $max_fail_handler and open $faillog,"+>>$faillog") {
908 flock($faillog,LOCK_EX);
909 seek $faillog,0,SEEK_SET;
910 $n++ while <$faillog>;
911 printf {$faillog} "%s %s\n",isodate(time),$request;
913 &$max_fail_handler($ENV{REMOTE_ADDR}) if $n > $max_fail;
917 # remove all white space
929 my $q = "[\'\"]"; # quote delimiter chars " and '
931 # remove first newline and look for default indention
935 # remove trailing spaces at end
940 # first line have a quote delimiter char?
942 # remove heading spaces and delimiter chars
948 # find the line with the fewest heading spaces (and count them)
952 if (/^( *)\S/ and length($1) < $s) { $s = length($1) };
960 return join("\n",@s)."\n";
967 if (@_ > 1 and defined fileno $_[0]) { $H = shift }
973 sub check_sender_quota {
975 my $squota = $sender_quota||0;
977 my ($file,$size,%file,$data,$upload);
980 if (open $qf,'<',"$sender/\@QUOTA") {
983 $squota = $1 if /sender.*?(\d+)/i;
988 foreach $file (glob "*/$sender/*") {
989 $data = "$file/data";
990 $upload = "$file/upload";
991 if (not -l $data and $size = -s $data) {
992 # count hard links only once (= same inode)
993 my $i = (stat($data))[1]||0;
998 } elsif (-f $upload) {
999 # count hard links only once (= same inode)
1000 my $i = (stat($upload))[1]||0;
1001 unless ($file{$i}) {
1002 $size = readlink "$file/size" and $du += $size;
1008 return($squota,int($du/1024/1024));
1012 # check recipient quota
1013 sub check_recipient_quota {
1014 my $recipient = shift;
1015 my $rquota = $recipient_quota||0;
1020 if (open my $qf,'<',"$recipient/\@QUOTA") {
1023 $rquota = $1 if /recipient.*?(\d+)/i;
1028 foreach $file (glob "$recipient/*/*") {
1029 if (-f "$file/upload" and $size = readlink "$file/size") {
1031 } elsif (not -l "$file/data" and $size = -s "$file/data") {
1036 return($rquota,int($du/1024/1024));
1043 chomp($_ = <$file>||'');
1048 # (shell) wildcard matching
1051 my $p = quotemeta shift;
1064 if ($skey) { $logout = "/fup?logout=skey:$skey" }
1065 elsif ($gkey) { $logout = "/fup?logout=gkey:$gkey" }
1066 elsif ($akey) { $logout = "/fup?logout=akey:$akey" }
1067 else { $logout = "/fup?logout" }
1070 '<form name="logout" action="$logout">'
1071 ' <input type="submit" name="logout" value="logout">'
1078 # print data dump of global or local variables in HTML
1079 # input musst be a string, eg: '%ENV'
1085 $_ = eval(qq(use Data::Dumper;Data::Dumper->Dump([\\$v])));
1089 print "<pre>\n$_\n</pre>\n";
1094 my ($file,$link) = @_;
1096 return symlink untaint($link),$file;
1100 # copy file (and modify) or symlink
1101 # returns chomped file contents or link name
1102 # preserves permissions and time stamps
1104 my ($from,$to,$mod) = @_;
1109 $to .= '/'.basename($from) if -d $to;
1111 if (defined($link = readlink $from)) {
1112 mksymlink($to,$link);
1115 open $from,'<',$from or return;
1116 open $to,'>',$to or return;
1121 close $to or http_die("internal error: $to - $!");
1122 if (my @s = stat($from)) {
1124 utime @s[8,9],$to unless $mod;
1137 if (open $file,$file) {
1146 # read one line from STDIN (net socket) and assign it to $_
1147 # return number of read bytes
1148 # also set global variable $RB (read bytes)
1152 if (defined ($_ = <STDIN>)) {
1162 # read forward to given pattern
1164 my $pattern = shift;
1166 while (&nvt_read) { return if /$pattern/ }
1170 # HTTP GET and POST parameters
1172 # fills global variable %PARAM :
1173 # normal parameter is $PARAM{$parameter}
1174 # file parameter is $PARAM{$parameter}{filename} $PARAM{$parameter}{data}
1175 sub parse_parameters {
1176 my $cl = $ENV{X_CONTENT_LENGTH} || $ENV{CONTENT_LENGTH} || 0;
1181 if ($cl > 128*$MB) {
1182 http_die("request too large");
1185 foreach (split('&',$ENV{QUERY_STRING})) {
1186 if (/(.+?)=(.*)/) { $PARAM{$1} = $2 }
1187 else { $PARAM{$_} = $_ }
1189 $_ = $ENV{CONTENT_TYPE}||'';
1190 if ($ENV{REQUEST_METHOD} eq 'POST' and /boundary=\"?([\w\-\+\/_]+)/) {
1192 while ($RB<$cl and &nvt_read) { last if /^--\Q$boundary/ }
1193 # continuation lines are not checked!
1194 while ($RB<$cl and &nvt_read) {
1196 if (/^Content-Disposition:.*\s*filename="(.+?)"/i) {
1199 if (/^Content-Disposition:\s*form-data;\s*name="(.+?)"/i) {
1201 # skip rest of mime part header
1202 while ($RB<$cl and &nvt_read) { last if /^\s*$/ }
1205 if ($p =~ /password/i) {
1206 debuglog('*' x length)
1211 last if /^--\Q$boundary/;
1214 unless (defined $_) { die "premature end of HTTP POST\n" }
1215 $data =~ s/\r?\n$//;
1217 $PARAM{$p}{filename} = $filename;
1218 $PARAM{$p}{data} = $data;
1222 last if /^--\Q$boundary--/;
1229 # name based virtual host?
1231 my $hh = shift; # HTTP_HOST
1233 my $locale = $ENV{LOCALE};
1235 # memorized vhost? (default is in fex.ph)
1236 %vhost = split(':',$ENV{VHOST}) if $ENV{VHOST};
1238 if (%vhost and $hh and $hh =~ s/^([\w\.-]+).*/$1/) {
1239 if ($vhost = $vhost{$hh} and -f "$vhost/lib/fex.ph") {
1240 $ENV{VHOST} = "$hh:$vhost"; # memorize vhost for next run
1241 $ENV{FEXLIB} = $FEXLIB = "$vhost/lib";
1242 $logdir = $spooldir = "$vhost/spool";
1243 $docdir = "$vhost/htdocs";
1244 @logdir = ($logdir);
1245 if ($locale and -e "$vhost/locale/$locale/lib/fex.ph") {
1246 $ENV{FEXLIB} = $FEXLIB = "$vhost/locale/$locale/lib";
1248 require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
1249 $ENV{SERVER_NAME} = $hostname;
1250 @doc_dirs = ($docdir);
1251 foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
1261 my ($plain,$to,$keyring,$from) = @_;
1262 my ($pid,$pi,$po,$pe,$enc,$err);
1267 $pid = open3($po,$pi,$pe,
1268 "gpg --batch --trust-model always --keyring $keyring".
1269 " -a -e -r $bcc -r $to"
1275 $enc .= $_ while <$pi>;
1276 $err .= $_ while <$pe>;
1277 errorlog("($from --> $to) $err") if $err;
1288 my @s = stat(shift) or return;
1293 # extract locale functions into hash of subroutine references
1294 # e.g. \&german ==> $notify{german}
1295 sub locale_functions {
1300 if ($locale and open my $fexpp,"$FEXHOME/locale/$locale/lib/fex.pp") {
1302 s/.*\n(\#\#\# locale functions)/$1/s;
1303 # sub xx {} ==> xx{$locale} = sub {}
1304 s/\nsub (\w+)/\n\$$1\{$locale\} = sub/gs;
1313 my $status = shift || 'new';
1314 my ($to,$keep,$locale,$file,$filename,$comment,$autodelete,$replyto,$mtime);
1317 if ($dkey =~ m:/.+/.+/:) {
1319 $dkey = readlink("$file/dkey");
1321 $file = readlink("$dkeydir/$dkey")
1322 or http_die("internal error: no DKEY $DKEY");
1325 $filename = filename($file);
1328 $mtime = mtime("$file/data") or http_die("internal error: no $file/data");
1329 $comment = slurp("$file/comment") || '';
1330 $replyto = readlink "$file/replyto" || '';
1331 $autodelete = readlink "$file/autodelete"
1332 || readlink "$to/\@AUTODELETE"
1334 $keep = readlink "$file/keep"
1335 || readlink "$to/\@KEEP"
1338 $locale = readlink "$to/\@LOCALE" || readlink "$file/locale" || 'english';
1339 $_ = untaint("$FEXHOME/locale/$locale/lib/lf.pl");
1341 unless ($notify{$locale}) {
1342 $locale = 'english';
1343 $notify{$locale} ||= \¬ify;
1345 return &{$notify{$locale}}(
1348 filename => $filename,
1349 keep => $keep-int((time-$mtime)/$DS),
1350 comment => $comment,
1351 autodelete => $autodelete,
1352 replyto => $replyto,
1356 ### locale functions ###
1357 # will be extracted by install process and saved in $FEXHOME/lib/lf.pl
1358 # you cannot modify them here without re-installing!
1361 # my ($status,$dkey,$filename,$keep,$warn,$comment,$autodelete) = @_;
1363 my ($to,$from,$file,$mimefilename,$receiver,$warn,$comment,$autodelete);
1364 my ($size,$bytes,$days,$header,$data,$replyto,$uurl);
1365 my ($mfrom,$mto,$dfrom,$dto);
1370 my $fua = $ENV{HTTP_USER_AGENT}||'';
1372 my $disclaimer = '';
1375 my $boundary = randstring(16);
1376 my ($body,$enc_body);
1380 $warn = $P{warn}||2;
1381 $comment = encode_utf8($P{comment}||'');
1382 $comment =~ s/^!\*!//; # multi download allow flag
1383 $autodelete = $P{autodelete}||$::autodelete;
1385 $file = untaint(readlink("$dkeydir/$P{dkey}"));
1386 $file =~ s/^\.\.\///;
1387 # make download protocal same as upload protocol
1388 if ($uurl = readlink("$file/uurl") and $uurl =~ /^(\w+):/) {
1390 $durl =~ s/^\w+::/$proto::/;
1392 $index = "$proto://$hostname/index.html";
1393 ($to,$from,$file) = split('/',$file);
1394 $filename = strip_path($P{filename});
1397 $mfrom .= '@'.$mdomain if $mdomain and $mfrom !~ /@/;
1398 $mto .= '@'.$mdomain if $mdomain and $mto !~ /@/;
1399 $keyring = $to.'/@GPG';
1400 # $to = '' if $to eq $from; # ???
1401 $replyto = $P{replyto}||$mfrom;
1402 $header = "From: <$mfrom> ($mfrom via F*EX service $hostname)\n";
1403 $header .= "Reply-To: <$replyto>\n" if $replyto ne $mfrom;
1404 $header .= "To: <$mto>\n";
1405 $data = "$dkeydir/$P{dkey}/data";
1406 $size = $bytes = -s $data;
1407 return unless $size;
1412 "Please avoid download with Internet Explorer, ".
1413 "because it has too many bugs.\n".
1414 "We recommend Firefox or wget.";
1416 if ($filename =~ /\.(tar|zip|7z|arj|rar)$/) {
1418 "$filename is a container file.\n".
1419 "You can unpack it for example with 7zip ".
1420 "(http://www.7-zip.org/download.html)";
1422 if ($limited_download =~ /^y/i) {
1424 'This download link only works for you, you cannot distribute it.';
1427 $size = "$size Bytes";
1428 } elsif ($size/1024 < 2048) {
1429 $size = int($size/1024)." kB";
1431 $size = int($size/1024/1024)." MB";
1433 if ($autodelete eq 'YES') {
1434 $autodelete = "WARNING: After download (or view with a web browser!), "
1435 . "the file will be deleted!";
1436 } elsif ($autodelete eq 'DELAY') {
1437 $autodelete = "WARNING: When you download the file it will be deleted "
1438 . "soon afterwards!";
1446 $mimefilename = $filename;
1447 if ($mimefilename =~ s/([_\?\=\x00-\x1F\x7F-\xFF])/sprintf("=%02X",ord($1))/eog) {
1448 $mimefilename =~ s/ /_/g;
1449 $mimefilename = '=?UTF-8?Q?'.$mimefilename.'?=';
1453 unless ($fileid = readlink("$dkeydir/$P{dkey}/id")) {
1454 my @s = stat($data);
1455 $fileid = @s ? $s[1].$s[9] : 0;
1458 if ($P{status} eq 'new') {
1460 $header .= "Subject: F*EX-upload: $mimefilename\n";
1463 $header .= "Subject: reminder F*EX-upload: $mimefilename\n";
1465 $header .= "X-FEX-Client-Address: $fra\n" if $fra;
1466 $header .= "X-FEX-Client-Agent: $fua\n" if $fua;
1467 foreach my $u (@durl?@durl:($durl)) {
1468 my $durl = sprintf("%s/%s/%s",$u,$P{dkey},normalize_filename($filename));
1469 $header .= "X-FEX-URL: $durl\n" unless -s $keyring;
1470 $download .= "$durl\n";
1473 "X-FEX-Filesize: $bytes\n".
1474 "X-FEX-File-ID: $fileid\n".
1475 "X-FEX-Fexmaster: $ENV{SERVER_ADMIN}\n".
1477 "MIME-Version: 1.0\n";
1478 if ($comment =~ s/^\[(\@(.*?))\]\s*//) {
1479 $receiver = "group $1";
1480 if ($_ = readlink "$from/\@GROUP/$2" and m:^../../(.+?)/:) {
1481 $receiver .= " (maintainer: $1)";
1486 if ($days == 1) { $days .= " day" }
1487 else { $days .= " days" }
1489 # explicite sender set in fex.ph?
1491 map { s/^From: <$mfrom/From: <$sender_from/ } $header;
1492 open $sendmail,'|-',$sendmail,$mto,$bcc
1493 or http_die("cannot start sendmail - $!");
1495 # for special remote domains do not use same domain in From,
1496 # because remote MTA will probably reject this e-mail
1497 $dfrom = $1 if $mfrom =~ /@(.+)/;
1498 $dto = $1 if $mto =~ /@(.+)/;
1499 if ($dfrom and $dto and @remote_domains and
1501 $dfrom =~ /(^|\.)$_$/ and $dto =~ /(^|\.)$_$/
1504 $header =~ s/(From: <)\Q$mfrom\E(.*?)\n/$1$admin$2\nReply-To: $mfrom\n/;
1505 open $sendmail,'|-',$sendmail,$mto,$bcc
1506 or http_die("cannot start sendmail - $!");
1508 open $sendmail,'|-',$sendmail,'-f',$mfrom,$mto,$bcc
1509 or http_die("cannot start sendmail - $!");
1512 if ($comment =~ s/^!(shortmail|\.)!\s*//i
1513 or (readlink "$to/\@NOTIFICATION"||'') =~ /short/i
1522 $comment = "Comment: $comment\n" if $comment;
1523 $disclaimer = slurp("$from/\@DISCLAIMER") || qqq(qq(
1526 'F*EX is not an archive, it is a transfer system for personal files.'
1527 'For more information see $index'
1529 'Questions? ==> F*EX admin: $admin'
1531 $disclaimer .= "\n" . $::disclaimer if $::disclaimer;
1533 '$from has uploaded the file'
1535 '($size) for $receiver. Use'
1538 'to download this file within $days.'
1546 $body =~ s/\n\n+/\n\n/g;
1548 $enc_body = gpg_encrypt($body,$to,$keyring,$from);
1553 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";'
1554 '\tboundary="$boundary"'
1555 'Content-Disposition: inline'
1559 'Content-Type: application/pgp-encrypted'
1560 'Content-Disposition: attachment'
1565 'Content-Type: application/octet-stream'
1566 'Content-Disposition: inline; filename="fex.pgp"'
1573 "Content-Type: text/plain; charset=UTF-8\n".
1574 "Content-Transfer-Encoding: 8bit\n";
1576 print {$sendmail} $header,"\n",$body;
1577 close $sendmail and return $to;
1578 http_die("cannot send notification e-mail (sendmail error $!)");
1583 my ($expire,$user) = @_;
1584 my $fexsend = "$FEXHOME/bin/fexsend";
1589 $fexsend .= " -M -D -k 30 -C"
1590 ." 'Your F*EX account has been inactive for $expire days,"
1591 ." you must download this file to reactivate it."
1592 ." Otherwise your account will be deleted.'"
1593 ." $FEXLIB/reactivation.txt $user";
1594 # on error show STDOUT and STDERR
1595 system "$fexsend >/dev/null 2>&1";
1601 warn "$0: cannot execute $fexsend for reactivation()\n";