| 1 |
#!/usr/bin/perl -wT |
| 2 |
|
| 3 |
# FEX CGI for redirect uploaded files |
| 4 |
# |
| 5 |
# Author: Ulli Horlacher <[email protected]> |
| 6 |
# |
| 7 |
|
| 8 |
BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 } |
| 9 |
|
| 10 |
# use utf8; |
| 11 |
use Fcntl qw(:flock :seek :mode); |
| 12 |
use Digest::MD5 qw(md5_hex); |
| 13 |
|
| 14 |
# add fex lib |
| 15 |
(our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/; |
| 16 |
die "$0: no $FEXLIB\n" unless -d $FEXLIB; |
| 17 |
|
| 18 |
our ($keep_default,$dkeydir,$akeydir,$mdomain,@logdir,$fra); |
| 19 |
our $akey = ''; |
| 20 |
|
| 21 |
# load common code, local config : $HOME/lib/fex.ph |
| 22 |
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n"; |
| 23 |
|
| 24 |
our $error = 'F*EX redirect ERROR'; |
| 25 |
|
| 26 |
chdir $spooldir or die "$spooldir - $!\n"; |
| 27 |
|
| 28 |
$from = $id = $oto = $nto = $file = ''; |
| 29 |
|
| 30 |
# look for CGI parameters |
| 31 |
our %PARAM; |
| 32 |
&parse_parameters; |
| 33 |
foreach my $v (keys %PARAM) { |
| 34 |
my $vv = $PARAM{$v}; |
| 35 |
$vv =~ s/[<>\'\`\"\000-\037]//g; |
| 36 |
if ($v =~ /^akey$/i and $vv =~ /^(\w+)$/) { |
| 37 |
$akey = $1; |
| 38 |
} elsif ($v =~ /^(from|user)$/i) { |
| 39 |
$from = normalize_address($vv); |
| 40 |
$from .= '@'.$mdomain if $mdomain and $from !~ /@/; |
| 41 |
} elsif ($v =~ /^id$/i) { |
| 42 |
$id = despace($vv); |
| 43 |
} elsif ($v =~ /^file$/i) { |
| 44 |
$vv =~ s:/:_:g; |
| 45 |
$file = untaint(normalize($vv)); |
| 46 |
} elsif ($v =~ /^oto$/i) { |
| 47 |
$oto = normalize_address($vv); |
| 48 |
$oto .= '@'.$mdomain if $mdomain and $oto !~ /@/; |
| 49 |
} elsif ($v =~ /^nto$/i) { |
| 50 |
$nto = normalize_address($vv); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if ($akey and not $from) { |
| 55 |
if ($id = readline1("$akeydir/$akey/@")) { |
| 56 |
if (readlink_("$akeydir/$akey") =~ m:.*/(.+):) { |
| 57 |
$from = $1; |
| 58 |
} else { |
| 59 |
http_die("internal server error: unknown akey $akey"); |
| 60 |
} |
| 61 |
} else { |
| 62 |
$akey = ''; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if ($from and -s "$from/\@ALLOWED_RECIPIENTS") { |
| 67 |
http_die("You are a restricted user"); |
| 68 |
} |
| 69 |
|
| 70 |
if ($from and $id and $rid = readline1("$from/@") and $id eq $rid) { |
| 71 |
unless ($akey) { |
| 72 |
$akey = genakey($from); |
| 73 |
} |
| 74 |
} else { |
| 75 |
http_die("wrong user or auth-ID"); |
| 76 |
} |
| 77 |
|
| 78 |
if ($oto and not glob("$oto/$from/*")) { |
| 79 |
http_die("$oto has no files in spool from you ($from)"); |
| 80 |
} |
| 81 |
|
| 82 |
# display HTML form and request user data |
| 83 |
unless ($from and $id and $file and $oto and $nto) { |
| 84 |
$head = "$ENV{SERVER_NAME} F*EX redirect"; |
| 85 |
http_header("200 OK"); |
| 86 |
print html_header($head); |
| 87 |
pq(qq( |
| 88 |
'<form action="$ENV{SCRIPT_NAME}" method="post"' |
| 89 |
' accept-charset="UTF-8" enctype="multipart/form-data">' |
| 90 |
' <table>' |
| 91 |
)); |
| 92 |
if ($akey) { |
| 93 |
print "<input type=\"hidden\" name=\"akey\" value=\"$akey\">\n"; |
| 94 |
} else { |
| 95 |
pq(qq( |
| 96 |
' <tr><td>sender:' |
| 97 |
' <td><input type="text" name="from" size="80" value="$from"></tr>' |
| 98 |
' <tr><td>auth-ID:' |
| 99 |
' <td><input type="password" name="id" size="16" value="$id"></tr>' |
| 100 |
)); |
| 101 |
} |
| 102 |
if ($oto) { |
| 103 |
pq(qq( |
| 104 |
' <tr><td>old (wrong) recipient:<td>$oto</tr>' |
| 105 |
' <input type="hidden" name="oto" value="$oto">' |
| 106 |
)); |
| 107 |
} else { |
| 108 |
pq(qq( |
| 109 |
' <tr><td>old (wrong) recipient:' |
| 110 |
' <td><input type="text" name="oto" size="80" value="$oto"></tr>' |
| 111 |
)); |
| 112 |
} |
| 113 |
if ($from and $oto) { |
| 114 |
pq(qq( |
| 115 |
' <tr><td>new recipient:' |
| 116 |
' <td><input type="text" name="nto" size="80" value="$nto"></tr>' |
| 117 |
' <tr><td>filename: <td><select name="file" size="1">' |
| 118 |
)); |
| 119 |
if ($file) { |
| 120 |
print "\t<option>$file</option>\n"; |
| 121 |
} else { |
| 122 |
foreach my $file (glob "$oto/$from/*/data") { |
| 123 |
next if $file =~ m:/STDFEX/:; |
| 124 |
$file =~ s:/data$::; |
| 125 |
if ($filename = filename($file)) { |
| 126 |
print "\t<option>$filename</option>\n"; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
print " </tr>\n"; |
| 131 |
} |
| 132 |
pq(qq( |
| 133 |
' </table>' |
| 134 |
' <p>' |
| 135 |
' <input type="submit" value="submit">' |
| 136 |
'</form>' |
| 137 |
'<p>' |
| 138 |
'<a href="/foc?akey=$akey">back to F*EX operation control</a>' |
| 139 |
'</body></html>' |
| 140 |
)); |
| 141 |
exit; |
| 142 |
} |
| 143 |
|
| 144 |
if ($nto) { |
| 145 |
|
| 146 |
# read aliases from address book |
| 147 |
if (open my $AB,'<',"$from/\@ADDRESS_BOOK") { |
| 148 |
while (<$AB>) { |
| 149 |
s/#.*//; |
| 150 |
$_ = lc $_; |
| 151 |
if (s/^\s*(\S+)[=\s]+(\S+)//) { |
| 152 |
my ($alias,$address) = ($1,$2); |
| 153 |
if ($nto eq $alias) { |
| 154 |
$nto = $address; |
| 155 |
last; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
close $AB; |
| 160 |
} |
| 161 |
|
| 162 |
$nto .= '@'.$mdomain if $mdomain and $nto !~ /@/ and $nto =~ /\w/; |
| 163 |
checkaddress($nto) or http_die("$nto is not a valid email address"); |
| 164 |
} else { |
| 165 |
http_die("no new recipient given"); |
| 166 |
} |
| 167 |
|
| 168 |
if ($oto and $nto and $oto eq $nto) { |
| 169 |
http_die("new recipient must be other than old recipient"); |
| 170 |
} |
| 171 |
|
| 172 |
$fkey = urlencode($file); |
| 173 |
unless (-s "$oto/$from/$fkey/data") { |
| 174 |
http_die("no upload data found for $file for $oto"); |
| 175 |
} |
| 176 |
|
| 177 |
if (not -e "$oto/$from/$fkey/data") { |
| 178 |
if (my $download = slurp("$oto/$from/$fkey/download")) { |
| 179 |
chomp $download; |
| 180 |
http_die("$file already downloaded by $download"); |
| 181 |
} |
| 182 |
http_die("$file has gone"); |
| 183 |
} |
| 184 |
|
| 185 |
mkdirp("$nto/$from"); |
| 186 |
rmrf("$nto/$from/$fkey"); |
| 187 |
|
| 188 |
if (rename "$oto/$from/$fkey","$nto/$from/$fkey") { |
| 189 |
mkdirp("$oto/$from/$fkey"); |
| 190 |
if (open $fkey,'>',"$oto/$from/$fkey/error") { |
| 191 |
print {$fkey} "$from has removed $file\n"; |
| 192 |
close $fkey; |
| 193 |
} |
| 194 |
unlink "$nto/$from/$fkey/dkey"; |
| 195 |
unlink "$nto/$from/$fkey/notify"; |
| 196 |
unlink "$nto/$from/$fkey/error"; |
| 197 |
unlink "$nto/$from/$fkey/download"; |
| 198 |
if (slurp("$oto/$from/$fkey/comment")||'' =~ /NOMAIL/) { |
| 199 |
unlink "$nto/$from/$fkey/comment"; |
| 200 |
} |
| 201 |
$dkey = randstring(8); |
| 202 |
symlink $dkey,"$nto/$from/$fkey/dkey"; |
| 203 |
symlink "../$nto/$from/$fkey","$dkeydir/$dkey"; |
| 204 |
$filename = filename("$nto/$from/$fkey") || $fkey; |
| 205 |
notify_locale($dkey,'new'); |
| 206 |
ruplog("$oto/$from/$fkey ==> $nto"); |
| 207 |
http_header("200 OK"); |
| 208 |
print html_header('F*EX redirect'); |
| 209 |
pq(qq( |
| 210 |
'<h3>F*EX redirect</h3>' |
| 211 |
'notification of file upload \"$filename\" sent to $nto' |
| 212 |
'<p>' |
| 213 |
'<a href="/foc">back to F*EX operation control</a>' |
| 214 |
'</body></html>' |
| 215 |
)); |
| 216 |
} else { |
| 217 |
http_die("redirect $nto/$from/$fkey failed : $!") |
| 218 |
} |
| 219 |
|
| 220 |
exit; |
| 221 |
|
| 222 |
|
| 223 |
sub normalize_address { |
| 224 |
local $_ = shift; |
| 225 |
s/[<>;,\s\|\/\'\`\"\000-\037]//g; |
| 226 |
$_ = untaint($_); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
# standard log |
| 231 |
sub ruplog { |
| 232 |
my $msg = "@_"; |
| 233 |
|
| 234 |
$msg =~ s/\n/ /g; |
| 235 |
$msg =~ s/\s+$//; |
| 236 |
$msg = sprintf "%s [%s_%s] (%s) %s\n", |
| 237 |
isodate(time),$$,$ENV{REQUESTCOUNT},$fra,$msg; |
| 238 |
|
| 239 |
foreach my $log (@logdir) { |
| 240 |
if (open $log,'>>',"$log/rup.log") { |
| 241 |
flock $log,LOCK_EX; |
| 242 |
seek $log,0,SEEK_END; |
| 243 |
printf {$log} $msg; |
| 244 |
close $log; |
| 245 |
} |
| 246 |
} |
| 247 |
} |