Debian Codegraph @

bin/fac

raw
1 #!/usr/bin/perl -w
2
3 # CLI admin client for the FEX service
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7
8 use 5.010;
9 use Getopt::Std;
10 use File::Basename;
11 use Cwd 'abs_path';
12 use Digest::MD5 'md5_hex';
13
14 use constant M => 1024*1024;
15 use constant DS => 60*60*24;
16
17 # do not run as CGI!
18 exit if $ENV{SCRIPT_NAME};
19
20 unless ($FEXLIB = $ENV{FEXLIB}) {
21 if ($ENV{FEXHOME}) {
22 $FEXLIB = $ENV{FEXHOME}.'/lib';
23 } elsif (-f '/usr/share/fex/lib/fex.ph') {
24 $FEXLIB = '/usr/share/fex/lib';
25 } else {
26 if ($0 =~ m:^/:) {
27 $FEXLIB = dirname(dirname($0)).'/lib';
28 } else {
29 $FEXLIB = dirname(dirname(abs_path($0))).'/lib';
30 }
31 }
32 $ENV{FEXLIB} = $FEXLIB;
33 }
34 die "$0: no FEXLIB\n" unless -f "$FEXLIB/fex.pp";
35
36 # become effective user fex
37 unless ($<) {
38 if (my @pw = getpwnam('fex')) {
39 $) = $pw[3];
40 $> = $pw[2];
41 $ENV{HOME} = $pw[7];
42 } else {
43 die "$0: no such user 'fex'\n";
44 }
45 }
46
47 umask 077;
48
49 # import from fex.pp
50 our ($FEXHOME,$FHS,$hostname,$spooldir,@logdir,$logdir,$akeydir,$docdir);
51 our ($durl,@durl,$mdomain,$admin,$mailmode);
52 our ($autodelete,$keep_default,$keep_max);
53 our ($document_exchange,$archive_sharing);
54 our ($recipient_quota,$sender_quota);
55 our (@local_rdomains);
56 local $notification = 'full';
57
58 # load common code, local config : $HOME/lib/fex.ph
59 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
60
61 die "$0: \$admin not configured in $FEXLIB/fex.ph\n" unless $admin;
62
63 $EDITOR = $ENV{EDITOR} || $ENV{VISUAL} ||
64 (-x '/usr/bin/editor' ? '/usr/bin/editor' : 'vi');
65
66 $opt_c = $opt_v = $opt_l = $opt_L = $opt_h = $opt_w = $opt_u = 0;
67 $opt_M = $opt_E = $opt_b = 0;
68 $opt_r = $opt_d = $opt_q = $opt_a = $opt_n = $opt_k = $opt_K = $opt_m = '';
69 $opt_y = $opt_S = $opt_C = $opt_D = $opt_A = $opt_V = $opt_P = $opt_R = '';
70 $opt_s = $opt_x = '';
71 ${'opt_/'} = '';
72
73 @__ = @ARGV;
74 while (my $a = shift @__) {
75 if ($a eq '-V') {
76 shift @__;
77 } else {
78 push @_ARGV,$a;
79 }
80 }
81
82 chdir $spooldir or die "$0: no $spooldir\n";
83
84 @stat = stat $spooldir or die "$0: cannot access $spooldir - $!\n";
85 warn "WARNING: $spooldir with owner=root !?\n" unless $stat[4];
86 if (abs_path($spooldir) ne abs_path("$FEXHOME/spool")) {
87 warn "WARNING: \$spooldir differs from $FEXHOME/spool !\n";
88 }
89
90 getopts('hcvlLwubME/q:r:d:a:n:k:K:s:m:x:y:S:C:D:P:R:') or usage(2);
91 usage(0) if $opt_h;
92 examples() if $opt_E;
93
94 if (${'opt_/'}) {
95 my $admin = shift;
96 my $id = shift or die "usage: $0 -/ admin-email-address auth-ID\n";
97 if ($admin !~ /.\@[\w.-]+\.[a-z]+$/) {
98 die "$0: $admin is not an email address\n";
99 }
100 mkdir $admin;
101 my $aa = "$spooldir/$admin/@";
102 open $aa,'>',$aa or die "$0: cannot write $aa - $!\n";
103 print {$aa} $id,"\n";
104 close $aa or die "$0: cannot write $aa - $!\n";
105 my $fph = "$FEXLIB/fex.ph";
106 $_ = slurp($fph) or die "$0: cannot read $fph\n";
107 s/^\s*\$admin\s*=.*/\$admin = '$admin';/m or
108 $_ = "\$admin = '$admin';\n".$_;
109 open $fph,">$fph.new" or die "$0: cannot write $fph.new\n";
110 print {$fph} $_;
111 close $fph;
112 rename "$fph.new",$fph or die "$0: cannot rename $fph.new to $fph\n";
113 my $fid = "$ENV{HOME}/.fex/id";
114 mkdir dirname($fid);
115 rename $fid,$fid.'_save';
116 open $fid,'>',$fid or die "$0: cannot create $fid - $!\n";
117 if ($durl =~ m{(https?://.+?)/}) {
118 print {$fid} "$1\n";
119 } else {
120 print {$fid} "$hostname\n";
121 }
122 print {$fid} "$admin\n";
123 print {$fid} "$id\n";
124 close $fid;
125 print "new admin account: $admin\n";
126 exit;
127 }
128
129 &check_admin;
130
131 if ($opt_V) {
132 while (my ($hh,$vh) = each (%vhost)) {
133 if ($opt_V eq basename($vh) or $opt_V eq $hh) {
134 $ENV{HTTP_HOST} = $hh;
135 $ENV{VHOST} = "$hh:$vh";
136 $ENV{FEXLIB} = "$vh/lib";
137 die "$0: no $ENV{FEXLIB}/fex.ph\n" unless -f "$ENV{FEXLIB}/fex.ph";
138 exec $0,@_ARGV;
139 die "$0: cannot re-exec\n";
140 }
141 }
142 die "$0: no virtual host $opt_V defined\n";
143 }
144
145 $fup = $durl;
146 $fup =~ s:/[^/]+$:/fup:;
147
148 # maintenance mode
149 if ($opt_m) {
150 if ($opt_m eq 'exit') {
151 if (unlink '@MAINTENANCE') {
152 warn "$0: leaving maintenance mode\n";
153 } else {
154 warn "$0: no maintenance mode\n";
155 }
156 } else {
157 unlink '@MAINTENANCE';
158 symlink $opt_m,'@MAINTENANCE'
159 or die "$0: cannot write $spooldir/\@MAINTENANCE - $!";
160 warn "$0: entering maintenance mode\n";
161 }
162 exit;
163 }
164
165 # list files or resend notification e-mails
166 if ($opt_M) {
167 my ($mtime,$comment,$file,$keep);
168 local $_;
169
170 if (@ARGV) {
171 foreach $file (glob("@ARGV")) {
172 $mtime = mtime("$file/data") or next;
173 $comment = slurp("$file/comment")||'';
174 next if $comment =~ /NOMAIL/;
175 $keep = readlink("$file/keep")
176 || readlink("$file/../../\@KEEP")
177 || $keep_default;
178 $keep = $keep - int((time-mtime("$file/data"))/60/60/24);
179
180 notify(
181 status => 'new',
182 dkey => readlink("$file/dkey"),
183 filename => filename($file),
184 keep => $keep,
185 comment => $comment,
186 warn => int(($mtime-time)/DS)+$keep,
187 autodelete => readlink("$file/autodelete") || $autodelete,
188 );
189 print "send notification e-mail for $file\n";
190 }
191 } else {
192 # just list files
193 foreach $file (glob "*/*/*/data") {
194 next if $file =~ /^_?(anonymous|fexmail)/;
195 $file =~ s:/data$::;
196 $comment = "$file/comment";
197 if (open $comment,$comment and <$comment> =~ /NOMAIL/) {
198 next;
199 }
200 print "$file\n";
201 }
202 }
203 exit;
204 }
205
206 # show logfile
207 if ($opt_w) {
208 $log = "$logdir/fexsrv.log";
209 warn "$0: polling $log\n\n";
210 exec "$FEXHOME/bin/logwatch",$log;
211 die "$0: logwatch not found\n";
212 }
213
214 # list blocked IPs
215 if ($opt_b) {
216 my $ip;
217 die "$0: \$max_fail not defined in $FEXLIB/fex.ph\n" unless $max_fail;
218 chdir '.fail' or exit;
219 if ($ip = shift @ARGV) {
220 die "$ip is not an IP\n" if $ip !~ /^[\da-f.:]+$/i;
221 if ("@ARGV" eq '-') {
222 unlink $ip;
223 } else {
224 system 'cat',$ip if -f $ip;
225 }
226 } elsif (@fail = glob('*')) {
227 printf "%5s %s\n","fails","IP";
228 foreach $ip (ipsort(@fail)) {
229 if (open $ip,$ip) {
230 my $n = 0;
231 $n++ while <$ip>;
232 close $ip;
233 if ($n > $max_fail) {
234 printf "%4d %s\n",$n,$ip;
235 }
236 }
237 }
238 }
239 exit;
240 }
241
242 # list files and download URLs
243 if ($opt_l) {
244 my ($file,$dkey,@L);
245 chdir $spooldir or die "$0: $spooldir - $!\n";
246 foreach $file (glob "*/*/*") {
247 if (-s "$file/data" and
248 $dkey = readlink("$file/dkey") and
249 -l ".dkeys/$dkey"
250 ) {
251 push @L,sprintf "%2\$s --> %1\$s : $durl/$dkey/%3\$s\n",split "/",$file;
252 }
253 }
254 print sort @L if @L;
255 exit;
256 }
257
258 # list files detailed
259 if ($opt_L) {
260 my $filter = shift;
261 my ($comment,$file,$keep,$old,$size,$download);
262 local $_;
263
264 foreach $file (glob "*/*/*/data") {
265 next if $file =~ m:(.+?)/: and -l $1;
266 $size = -s $file or next;
267 $file =~ s:/data$::;
268 next if $filter and $file !~ /$filter/;
269 $comment = slurp("$file/comment")||'';
270 $dkey = readlink("$file/dkey")||'';
271 $keep = readlink("$file/keep")||$keep_default;
272 $old = int((time-mtime("$file/data"))/60/60/24);
273 $download = join(' & ',split("\n",(slurp("$file/download")||'')));
274 print "\n$file\n";
275 printf " comment: %s\n",decode_utf8($comment);
276 printf " size: %s\n",d3($size);
277 printf " sender ip: %s\n",readlink("$file/ip")||'';
278 printf " expire in: %s days\n",$keep-$old;
279 printf " upload speed: %s kB/s\n",readlink("$file/speed")||0;
280 printf " URL: $durl/$dkey/%3\$s\n",split "/",$file;
281 printf " download: %s\n",$download;
282 }
283 exit;
284 }
285
286 # delete user
287 if ($opt_d) {
288 $idf = "$spooldir/$opt_d/\@";
289 die "$0: no such user $opt_d\n" unless -f $idf;
290 unlink $idf or die "$0: cannot remove $idf - $!\n";
291 foreach $rf (glob "$spooldir/$opt_d/\@*") { unlink $rf }
292 print "$opt_d deleted\n";
293 exit;
294 }
295
296 # set user restriction file
297 if ($opt_R) {
298 if ($opt_R eq 'i') {
299 $user = shift or die "usage: $0 -Ri user\n";
300 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
301 die "$0: no user $user\n" unless -d "$spooldir/$user";
302 unless (@local_rdomains) {
303 die "$0: no \@local_rdomains in server config\n";
304 }
305 my $rf = "$spooldir/$user/\@ALLOWED_RECIPIENTS";
306 open $rf,'>',$rf or die "$0: cannot open $rf - $!";
307 print {$rf} "\@LOCAL_RDOMAINS\n";
308 close $rf;
309 print "$user restricted to internal recipients\n";
310 exit;
311 } elsif ($opt_R eq 'l') {
312 $user = shift or die "usage: $0 -Rl user\n";
313 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
314 die "$0: no user $user\n" unless -d "$spooldir/$user";
315 my $rf = "$spooldir/$user/\@ALLOWED_RECIPIENTS";
316 open $rf,'>',$rf or die "$0: cannot open $rf - $!";
317 print {$rf} "\@LOCAL_USERS\n";
318 close $rf;
319 print "$user restricted to local recipients\n";
320 exit;
321 } else {
322 usage(2);
323 }
324 exit;
325 }
326
327 # edit user restriction file
328 if ($opt_r) {
329 if ($opt_r =~ /^r/i) { $opt_r = 'ALLOWED_RECIPIENTS' }
330 elsif ($opt_r =~ /^u/i) { $opt_r = 'UPLOAD_HOSTS' }
331 elsif ($opt_r =~ /^d/i) { $opt_r = 'DOWNLOAD_HOSTS' }
332 else { usage(2) }
333 $user = shift or usage(2);
334 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
335 die "$0: no user $user\n" unless -d "$spooldir/$user";
336 my $rf = "$spooldir/$user/\@$opt_r";
337 unless (-s $rf) {
338 open $rf,'>',$rf or die "$0: cannot open $rf - $!";
339 if ($opt_r eq 'ALLOWED_RECIPIENTS') {
340 print {$rf}<<EOD;
341 # Restrict allowed recipients. Only listed addresses are allowed as recipients.
342 # Make this file COMPLETLY empty if you want to disable the restriction.
343 # An allowed recipient is an e-mail address. You can use * as wildcard.
344 # Examples:
345 # framstag\@rus.uni-stuttgart.de
346 # *\@flupp.org
347 EOD
348 } elsif ($opt_r eq 'UPLOAD_HOSTS') {
349 print {$rf}<<EOD;
350 # Restrict allowed upload hosts.
351 # Only listed addresses are allowed as upload hosts.
352 # Make this file COMPLETLY empty if you want to disable the restriction.
353 # You can add single ip adresses or ip ranges.
354 # Examples:
355 # 129.69.1.11
356 # 10.0.10.0-10.0.10.255
357 EOD
358 } elsif ($opt_r eq 'DOWNLOAD_HOSTS') {
359 print {$rf}<<EOD;
360 # Restrict allowed download hosts.
361 # Only listed addresses are allowed as download hosts.
362 # Make this file COMPLETLY empty if you want to disable the restriction.
363 # You can add single ip adresses or ip ranges.
364 # Examples:
365 # 129.69.1.11
366 # 10.0.10.0-10.0.10.255
367 EOD
368 } else {
369 die "$0: unknown option -r $opt_r\n";
370 }
371 close $rf;
372 }
373 system $EDITOR,$rf;
374 unlink $rf if -s $rf<5;
375 exit;
376 }
377
378 # edit configuration
379 if ($opt_c) {
380 exec $EDITOR,"$FEXLIB/fex.ph";
381 }
382
383 # add virtual server
384 if ($opt_A) {
385 if ($opt_A =~ /(.+):(.+)/) {
386 $vhost = $1;
387 $hhost = $2;
388 } else {
389 die "usage: $0 -A alias:hostname\n".
390 "example: $0 -A flupp:fex.flupp.org\n";
391 }
392 if ($FHS) {
393 $vhd = "/var/lib/fex/vhosts/$vhost";
394 mkdir $vhd or die "$0: cannot mkdir $vhd - $!\n";
395 mkdir "/etc/fex/vhosts/$vhost";
396 symlink "/etc/fex/vhosts/$vhost", "$vhd/lib";
397 mkdir "$spooldir/vhosts/$vhost";
398 symlink "$spooldir/vhosts/$vhost","$vhd/spool";
399 } else {
400 $vhd = "$FEXHOME/$vhost";
401 mkdir $vhd or die "$0: cannot mkdir $vhd - $!\n";
402 mkdir "$vhd/lib";
403 mkdir "$vhd/spool";
404 }
405
406 mkdir "$vhd/htdocs";
407 mkdir "$vhd/htdocs/locale";
408 $_ = slurp("$FEXLIB/fex.ph");
409 s/\$hostname\s*=.*/\$hostname = '$hhost';/ or s/^/\$hostname = '$hhost';\n/;
410 $fph = "$vhd/lib/fex.ph";
411 open $fph,">$fph" or die "$0: cannot write to $fph - $!\n";
412 print {$fph} $_;
413 close $fph;
414 cpa("$FEXLIB/fup.pl","$vhd/lib");
415 foreach $i (qw'dop fex.pp fup.pl lf.pl reactivation.txt') {
416 # symlink "$FEXLIB/$i","$vhd/lib/$i";
417 symlink "../../lib/$i","$vhd/lib/$i";
418 }
419 foreach $i (qw(
420 index.html tools.html SEX.html robots.txt
421 logo.jpg small_logo.jpg action-fex-camel.gif favicon.ico
422 FAQ
423 )) {
424 cpa("$docdir/$i","$vhd/htdocs");
425 }
426 symlink "$docdir/version","../../htdocs/version";
427 symlink "$docdir/download","../../htdocs/download";
428 cpa("$FEXHOME/locale",$vhd);
429 foreach $ld (glob "$vhd/locale/*") {
430 if (not -l $ld and -d "$ld/cgi-bin") {
431 $locale = basename($ld);
432 rmrf("$ld/cgi-bin");
433 # symlink "../../../locale/$locale/cgi-bin","$ld/cgi-bin";
434 symlink "../../../locale/$locale/htdocs","$vhd/htdocs/locale/$locale";
435 unlink "$ld/lib/fex.ph";
436 symlink "../../../lib/fex.ph","$ld/lib/fex.ph";
437 symlink "../../../../locale/$locale/lib","$ld/lib/master";
438 foreach $f (qw'dop fex.pp lf.pl reactivation.txt') {
439 unlink "$ld/lib/$f";
440 symlink "master/$f","$ld/lib/$f";
441 }
442 }
443 }
444 $fph = "$FEXLIB/fex.ph";
445 open $fph,">>$fph" or die "$0: cannot write to $fph = $!\n";
446 print {$fph} "\n\$vhost{'$hhost'} = '$vhd';\n";
447 close $fph;
448 print "You must now edit and configure $vhd/lib/fex.ph\n";
449 print "or execute: $0 -V $vhost -c\n";
450 exit;
451 }
452
453 # show config
454 if ($opt_v and not @ARGV) {
455 $version = slurp("$docdir/version");
456 $version =~ s/\n.*//s;
457 print "config from $FEXLIB/fex.ph :\n";
458 print " version = $version\n";
459 print " spooldir = $spooldir\n";
460 print " logdir = @logdir\n";
461 print " docdir = $docdir\n";
462 print " durl = @durl\n";
463 print " admin = $admin\n";
464 print " mdomain = $mdomain\n";
465 print " mailmode = $mailmode\n";
466 print " autodelete = $autodelete\n";
467 print " keep_default = $keep_default\n";
468 printf " keep_max = %s\n",$keep_max||'unlimited';
469 printf " document_exchange = %s\n",$document_exchange||'no';
470 printf " archive_sharing = %s\n",$archive_sharing||'no';
471 printf " recipient_quota = %d GB\n",int($recipient_quota/1024);
472 printf " sender_quota = %d GB\n",int($sender_quota/1024);
473 # while (($hh,$vh) = each %vhost) {
474 # printf " virtual server %s : %s\n",basename($vh),$hh;
475 # }
476 # unless (@ARGV) {
477 # foreach $ph (glob "$ENV{HOME}/*/lib/fex.ph") {
478 # $ENV{FEXLIB} = dirname($ph);
479 # print "\n";
480 # system $0,'-v',$ph;
481 # }
482 # }
483 if ($m = readlink('@MAINTENANCE')) {
484 print "server is in maintenance mode ($m)!\n" ;
485 }
486 exit;
487 }
488
489 # add user or show user config
490 if ($opt_u) {
491 chdir $spooldir or die "$0: cannot chdir $spooldir = $!\n";
492 if ($opt_u = shift @ARGV) {
493 $user = lc $opt_u;
494 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
495 $id = shift @ARGV;
496 $idf = "$user/@";
497 if (open $idf,$idf) {
498 chomp($ido = <$idf>||'');
499 close $idf;
500 }
501 unless ($id) {
502 die "$0: $user is not a regular FEX user\n" unless -f "$user/@";
503 showuser($user,$ido);
504 exit;
505 }
506 unless ($user =~ /\w@[\w.-]+\.[a-z]+$/) {
507 die "$0: $user is not a valid email-address!\n";
508 }
509 unless (-d $user) {
510 mkdir $user,0755
511 or die "$0: cannot mkdir $user - $!\n";
512 }
513 open F,">$idf" or die "$0: cannot write $idf - $!\n";
514 print F $id,"\n";
515 close F or die "$0: cannot write $idf - $!\n";
516 showuser($user,$id);
517 } else {
518 print "Users in $spooldir:\n";
519 foreach $user (glob "*/@") {
520 $user =~ s:.*/(.+)/@:$1:;
521 print "$user\n";
522 }
523 }
524 exit;
525 }
526
527 # set user autodelete default
528 if ($opt_a) {
529 $user = lc $opt_a;
530 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
531 $_ = shift @ARGV || '';
532 if (/^n/i) { $autodelete = 'no' }
533 elsif (/^y/i) { $autodelete = 'yes' }
534 elsif (/^d/i) { $autodelete = 'delay' }
535 else {
536 die "usage: $0 -a user yes\n".
537 "usage: $0 -a user no\n".
538 "usage: $0 -a user delay\n".
539 "example: $0 -a framstag\@rus.uni-stuttgart.de no\n";
540 }
541 mkdir "$spooldir/$user",0755;
542 my $adf = "$spooldir/$user/\@AUTODELETE";
543 unlink $adf;
544 symlink $autodelete,$adf or die "$0: cannot create symlink $adf - $!\n";
545 exit;
546 }
547
548 # set user notification default
549 if ($opt_n) {
550 $user = lc $opt_n;
551 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
552 $_ = shift @ARGV || '';
553 if (/^n/i) { $notification = 'no' }
554 elsif (/^[sb]/i) { $notification = 'short' }
555 elsif (/^[fd]/i) { $notification = '' }
556 else {
557 die "usage: $0 -n user no\n".
558 "usage: $0 -n user brief\n".
559 "usage: $0 -n user detailed\n".
560 "example: $0 -n framstag\@rus.uni-stuttgart.de brief\n";
561 }
562 mkdir "$spooldir/$user",0755;
563 my $ndf = "$spooldir/$user/\@NOTIFICATION";
564 unlink $ndf;
565 if ($notification) {
566 symlink $notification,$ndf or die "$0: cannot create symlink $ndf - $!\n";
567 }
568 exit;
569 }
570
571 # set user keep default
572 if ($opt_k) {
573 $user = lc $opt_k;
574 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
575 my $kf = "$spooldir/$user/\@KEEP_ARCHIVES";
576 my $keep = shift @ARGV || '';
577 unless ($keep) {
578 unlink $kf;
579 exit;
580 }
581 if ($keep !~ /^\d+$/) {
582 die "usage: $0 -s user keep_days\n".
583 "example: $0 -s framstag\@rus.uni-stuttgart.de 33\n";
584 }
585 mkdir "$spooldir/$user",0755;
586 unlink $kf;
587 symlink $keep,$kf or die "$0: cannot create symlink $kf - $!\n";
588 exit;
589 }
590
591 # set user keep maximum
592 if ($opt_K) {
593 $user = lc $opt_K;
594 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
595 my $kf = "$spooldir/$user/\@KEEP_MAX";
596 my $keep_max = shift @ARGV || '';
597 unless ($keep_max) {
598 unlink $kf;
599 exit;
600 }
601 if ($keep_max !~ /^\d+$/) {
602 die "usage: $0 -K user keep_max_days\n".
603 "example: $0 -K framstag\@rus.uni-stuttgart.de 99\n";
604 }
605 mkdir "$spooldir/$user",0755;
606 unlink $kf;
607 symlink $keep_max,$kf or die "$0: cannot create symlink $kf - $!\n";
608 exit;
609 }
610
611 # (dis)allow user archive sharing
612 if ($opt_s) {
613 $user = lc $opt_s;
614 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
615 unless (-d "$spooldir/$user") {
616 die "$0: $user does not exist\n";
617 }
618 my $as = "$spooldir/$user/\@ARCHIVE_SHARING";
619 my $allow = shift @ARGV // '-';
620 if ($allow eq '-') {
621 unlink $as;
622 } elsif ($allow eq 'y') {
623 unlink $as;
624 symlink 'yes',$as;
625 } elsif ($allow eq 'n') {
626 unlink $as;
627 symlink 'no',$as;
628 } else {
629 die "usage: $0 -s user [yn]\n".
630 "example: $0 -s framstag\@rus.uni-stuttgart.de y\n";
631 }
632 exit;
633 }
634
635 # (dis)allow user document exchange (dox)
636 if ($opt_x) {
637 $user = lc $opt_x;
638 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
639 unless (-d "$spooldir/$user") {
640 die "$0: $user does not exist\n";
641 }
642 my $dp = "$spooldir/$user/\@DOCUMENT_EXCHANGE";
643 my $allow = shift @ARGV // '-';
644 if ($allow eq '-') {
645 unlink $dp;
646 } elsif ($allow eq 'y') {
647 unlink $dp;
648 symlink 'yes',$dp;
649 } elsif ($allow eq 'n') {
650 unlink $dp;
651 symlink 'no',$dp;
652 } else {
653 die "usage: $0 -x user [yn]\n".
654 "example: $0 -x framstag\@rus.uni-stuttgart.de y\n";
655 }
656 exit;
657 }
658
659 # quota
660 if ($opt_q) {
661 $user = lc $opt_q;
662 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
663 unless (-d "$spooldir/$user") {
664 die "$0: $user is not a FEX user\n";
665 }
666 quota($user,@ARGV);
667 exit;
668 }
669
670 if ($opt_C) {
671 $user = lc $opt_C;
672 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
673 unless (-f "$spooldir/$user/@") {
674 die "$0: $user is not a regular FEX user\n";
675 }
676 $_ = shift @ARGV || '';
677 if (/^y/i) {
678 open $user,">>$spooldir/$user/\@CAPTIVE";
679 close $user;
680 print "$user is now captive\n";
681 } elsif (/^n/i) {
682 unlink "$spooldir/$user/\@CAPTIVE";
683 print "$user is no more captive\n";
684 } else {
685 die "usage: $0 -C user yes\n".
686 "usage: $0 -C user no\n".
687 "example: $0 -C framstag\@rus.uni-stuttgart.de no\n";
688 }
689 exit;
690 }
691
692 # FEXYOURSELF = user can only fex to himself via web interface
693 if ($opt_y) {
694 $user = lc $opt_y;
695 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
696 unless (-f "$spooldir/$user/@") {
697 die "$0: $user is not a regular FEX user\n";
698 }
699 $_ = shift @ARGV || '';
700 if (/^y/i) {
701 open $user,">>$spooldir/$user/\@FEXYOURSELF";
702 close $user;
703 print "$user has now \"fex yourself\" web default\n";
704 } elsif (/^n/i) {
705 unlink "$spooldir/$user/\@FEXYOURSELF";
706 print "$user has no \"fex yourself\" web default\n";
707 } else {
708 die "usage: $0 -y user yes\n".
709 "usage: $0 -y user no\n".
710 "example: $0 -y framstag\@rus.uni-stuttgart.de no\n";
711 }
712 exit;
713 }
714
715 if ($opt_D) {
716 $user = lc $opt_D;
717 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
718 $_ = $ARGV[0] || '';
719 if (/^no?$/i) {
720 unlink "$spooldir/$user/\@DISABLED";
721 print "$user is now enabled\n";
722 } else {
723 open $user,">>$spooldir/$user/\@DISABLED";
724 print {$user} "@ARGV\n";
725 close $user;
726 print "$user is now disabled\n";
727 }
728 exit;
729 }
730
731 if ($opt_P) {
732 $user = lc $opt_P;
733 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
734 $_ = shift @ARGV || '';
735 if (/^y/i) {
736 open $user,">>$spooldir/$user/\@PERSISTENT";
737 close $user;
738 print "$user is now persistent\n";
739 } elsif (/^n/i) {
740 unlink "$spooldir/$user/\@PERSISTENT";
741 print "$user is no more persistent\n";
742 } else {
743 die "usage: $0 -P user yes\n".
744 "usage: $0 -P user no\n".
745 "example: $0 -P framstag\@rus.uni-stuttgart.de yes\n";
746 }
747 exit;
748 }
749
750 if ($opt_S eq 'fup') {
751 &fupstat;
752 exit;
753 }
754
755 if ($opt_S eq 'fop') {
756 &fopstat;
757 exit;
758 }
759
760 if ($opt_S eq 'sex') {
761 &sexstat;
762 exit;
763 }
764
765 usage(3);
766
767 sub showuser {
768 my $user = shift;
769 my $id = shift;
770 my ($keep,$keep_max,$keep_archives,$autodelete,$notification,$login);
771
772 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
773
774 print "[using config $FEXLIB/fex.ph]\n";
775 print "$fup?from=$user&id=$id\n";
776 printf "%s/%s\n",$fup,b64("from=$user&id=$id");
777 # printf "%s/%s\n",$fup,b64("from=$user&to=$user&id=$id&submit=.");
778 print "spool: $spooldir/$user/\n";
779 if ($login_check and $login = readlink("$user/.login")) {
780 my $lc = &$login_check($login);
781 if ($lc) {
782 print "login: $login\n";
783 } else {
784 print "login: DELETED\n";
785 }
786 }
787 my $disabled = 'no';
788 if (-e "$spooldir/$user/\@DISABLED") {
789 $disabled = slurp("$spooldir/$user/\@DISABLED");
790 chomp $disabled;
791 $disabled ||= 'yes';
792 }
793 printf "fex yourself web default: %s\n",
794 -e "$spooldir/$user/\@FEXYOURSELF" ? 'yes' : 'no';
795 printf "persistent: %s\n",
796 -e "$spooldir/$user/\@PERSISTENT" ? 'yes' : 'no';
797 printf "captive: %s\n",
798 -e "$spooldir/$user/\@CAPTIVE" ? 'yes' : 'no';
799 printf "disabled: %s\n",$disabled;
800 printf "recipients restrictions: %s\n",
801 -e "$spooldir/$user/\@ALLOWED_RECIPIENTS" ? 'yes' : 'no';
802 printf "upload restrictions: %s\n",
803 -e "$spooldir/$user/\@UPLOAD_HOSTS" ? 'yes' : 'no';
804 printf "download restrictions: %s\n",
805 -e "$spooldir/$user/\@DOWNLOAD_HOSTS" ? 'yes' : 'no';
806 $autodelete = lc(readlink("$spooldir/$user/\@AUTODELETE") || $::autodelete);
807 print "autodelete default: $autodelete\n";
808 $notification = lc(readlink("$spooldir/$user/\@NOTIFICATION") || $::notification);
809 print "notification default: $notification\n";
810 $keep = readlink("$spooldir/$user/\@KEEP") || $keep_default;
811 print "keep default: $keep\n";
812 $keep_max = readlink("$spooldir/$user/\@KEEP_MAX") || $::keep_max;
813 print "keep maximum: $keep_max\n";
814 $dox = readlink("$spooldir/$user/\@DOCUMENT_EXCHANGE")
815 || ($document_exchange =~ /y/i ? 'yes' : 'no') . ' (system default)';
816 print "document exchange: $dox\n";
817 $fas = readlink("$spooldir/$user/\@ARCHIVE_SHARING")
818 || ($archive_sharing =~ /y/i ? 'yes' : 'no') . ' (system default)';
819 print "archive sharing: $fas\n";
820 quota($user);
821 if (-f "$spooldir/$user/@") {
822 if (my $ac = slurp("$spooldir/$user/.auto")) {
823 chomp $ac;
824 my @d = localtime(mtime("$spooldir/$user/.auto"));
825 printf "account creation: %s (%d-%02d-%02d %02d:%02d)\n",
826 $ac,$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1];
827 } else {
828 print "account creation: manual\n";
829 }
830 }
831 }
832
833 # set or show disk quota
834 sub quota {
835 my $user = shift;
836 my $rquota = '';
837 my $squota = '';
838 my $qf = "$spooldir/$user/\@QUOTA";
839 local $_;
840
841 if (open $qf,$qf) {
842 while (<$qf>) {
843 s/#.*//;
844 $rquota = $1 if /recipient.*?(\d+)/i;
845 $squota = $1 if /sender.*?(\d+)/i;
846 }
847 close $qf;
848 }
849
850 if (@_) {
851 for (@_) {
852 $rquota = $1 if /^r.*:(\d*)/i;
853 $squota = $1 if /^s.*:(\d*)/i;
854 }
855 open $qf,'>',$qf or die "$0: cannot write $qf - $!\n";
856 print {$qf} "recipient:$rquota\n" if $rquota;
857 print {$qf} "sender:$squota\n" if $squota;
858 close $qf;
859 }
860
861 printf "recpient quota (used): %d (%d) MB\n",check_recipient_quota($user);
862 printf "sender quota (used): %d (%d) MB\n",check_sender_quota($user);
863 }
864
865
866 sub fupstat {
867 my (%user,%domain,%du,%fup);
868 my ($log,$u,$d,$z);
869 my $fups = 0;
870 my $Z = 0;
871
872 if (-t) { $log = "$logdir/fup.log" }
873 else { $log = '>&=STDIN' }
874 open $log,$log or die "$0: cannot open $log - $!\n";
875
876 while (<$log>) {
877 if (/^([\d: -]+) (\[[\d_]+\] )?(\w\S+) .* (\d+)($| |\/)/) {
878 $z = $4;
879 $u = $3;
880 $u .= '@'.$mdomain if $mdomain and $u !~ /@/;
881 next if $u eq $admin;
882 $user{$u} += $z;
883 $d = 'unknown';
884 if (
885 $u =~ /([\w-]+\.(ac|gov|mil|com|org|net)\.[a-z]+)$/ or
886 $u =~ /([\w-]+\.[a-z]+)$/
887 ) { $d = $1 }
888 $domain{$d} += $z;
889 $fup{$d}++;
890 $du{$d}{$u}++;
891 $Z += $z;
892 $fups++;
893 }
894 }
895
896 foreach $u (sort {$user{$a} <=> $user{$b}} keys %user) {
897 printf "%s : %d\n",$u,$user{$u}/M;
898 }
899 print "========================================================\n";
900 foreach $d (sort {$domain{$a} <=> $domain{$b}} keys %domain) {
901 my $users = scalar(keys %{$du{$d}});
902 printf "%-36s %12d MB %4d user%s %6d upload%s\n",
903 $d,$domain{$d}/M,$users,$users==1?' ':'s',$fup{$d},$fup{$d}==1?'':'s';
904 }
905 printf "Total: %d GB, %d users, %d uploads\n",$Z/M/1024,scalar(keys %user),$fups;
906
907 exit;
908 }
909
910
911 sub fopstat {
912 my $Z = 0;
913 my $fops = 0;
914 my ($log,$u,$d,$z);
915 my (%user,%domain,%du,%fop);
916
917 if (-t) { $log = "$logdir/fop.log" }
918 else { $log = '>&=STDIN' }
919 open $log,$log or die "$0: cannot open $log - $!\n";
920
921 while (<$log>) {
922 if (/^([\d: -]+) (\[[\d_]+\] )?\S+ (.+?)\/.* (\d+)\/\d+/) {
923 $z = $4;
924 $u = $3;
925 $u .= '@'.$mdomain if $mdomain and $u !~ /@/;
926 $user{$u} += $z;
927 $d = 'unknown';
928 if (
929 $u =~ /([\w-]+\.(ac|gov|mil|com|org|net)\.[a-z]+)$/ or
930 $u =~ /([\w-]+\.[a-z]+)$/
931 ) { $d = $1 }
932 $domain{$d} += $z;
933 $fop{$d}++;
934 $du{$d}{$u}++;
935 $Z += $z;
936 $fops++;
937 }
938 }
939
940 foreach $u (sort {$user{$a} <=> $user{$b}} keys %user) {
941 printf "%s : %d\n",$u,$user{$u}/M;
942 }
943 print "========================================================\n";
944 foreach $d (sort {$domain{$a} <=> $domain{$b}} keys %domain) {
945 my $users = scalar(keys %{$du{$d}});
946 printf "%-36s %12d MB %4d user%s %6d download%s\n",
947 $d,$domain{$d}/M,$users,$users==1?' ':'s',$fop{$d},$fop{$d}==1?'':'s';
948 }
949 printf "Total: %d GB, %d users, %d downloads\n",$Z/M/1024,scalar(keys %user),$fops;
950
951 exit;
952 }
953
954
955 sub sexstat {
956 my $Z = 0;
957 my ($log,$u,$d,$z);
958 my (%user,%domain,%du);
959
960 if (-t) { $log = "$logdir/sex.log" }
961 else { $log = '>&=STDIN' }
962 open $log,$log or die "$0: cannot open $log - $!\n";
963
964 while (<$log>) {
965 # 2018-12-24 20:12:09 [30870_1] [email protected] (129.69.1.129) DOX tmp 788736000
966 if (/^([\d: -]+) \[[\d_]+\] (\S+\@\S+) .* (\d+)$/) {
967 $z = $3;
968 $u = $2;
969 $user{$u} += $z;
970 $d = $u;
971 $d =~ s/.*@//;
972 $d =~ s/.*\.(.+\.\w+)/$1/;
973 $domain{$d} += $z;
974 $du{$d}{$u}++;
975 $Z += $z;
976 }
977 }
978
979 foreach $u (sort {$user{$a} <=> $user{$b}} keys %user) {
980 printf "%s : %d\n",$u,$user{$u}/M;
981 }
982 print "========================================================\n";
983 foreach $d (sort {$domain{$a} <=> $domain{$b}} keys %domain) {
984 my $users = scalar(keys %{$du{$d}});
985 printf "%s : %d MB, %d user%s\n",$d,$domain{$d}/M,$users,$users==1?'':'s';
986 }
987 printf "Total: %d GB, %d users\n",$Z/M/1024,scalar(keys %user);
988
989 exit;
990 }
991
992
993 sub cpa {
994 my $dd = pop @_;
995
996 die "(cpa): $dd is not a directory" unless -d $dd;
997 system "rsync -a @_ $dd/" ;
998 }
999
1000
1001 sub check_admin {
1002
1003 my $admin_id = slurp("$spooldir/$admin/@") or
1004 die "$0: no admin account - you have to create it with:\n".
1005 "$0 -/ $admin ".randstring(8)."\n";
1006
1007 chomp $admin_id;
1008
1009 my $fid = "$ENV{HOME}/.fex/id";
1010 if (open $fid,$fid) {
1011 $_ = <$fid>;
1012 chomp($_ = <$fid>||'');
1013 if ($_ ne $admin) {
1014 warn "WARNING: user $admin not in $fid\n";
1015 $mismatch++;
1016 }
1017 chomp($_ = <$fid>||'');
1018 if ($_ ne $admin_id) {
1019 warn "WARNING: $admin auth-ID mismatch in $fid\n";
1020 $mismatch++;
1021 }
1022 close $fid;
1023 if ($mismatch) {
1024 warn "$0: moving $fid to ${fid}_save\n";
1025 rename $fid,$fid.'_save';
1026 }
1027 }
1028 unless (-f $fid) {
1029 mkdir dirname($fid);
1030 open $fid,'>',$fid or die "$0: cannot create $fid - $!\n";
1031 if ($durl =~ m{(https?://.+?)/}) {
1032 print {$fid} "$1\n";
1033 } else {
1034 print {$fid} "$hostname\n";
1035 }
1036 print {$fid} "$admin\n";
1037 print {$fid} "$admin_id\n";
1038 close $fid;
1039 warn "$0: new $fid created\n";
1040 }
1041 }
1042
1043
1044 sub ipsort {
1045 my @sorted = ();
1046 my @ips;
1047 local $_;
1048
1049 if (@ips = grep /:/,@_) {
1050 my ($c,$ip,@L);
1051 foreach (@ips) {
1052 if (/([a-f\d]+:[a-f:\d]+)/i) {
1053 $ip = $1;
1054 $c = ($ip =~ tr/://);
1055 if ($c < 7 and $ip =~ /::/) {
1056 $ip =~ s/::/':'.'0:'x(8-$c)/e;
1057 }
1058 if (($ip =~ tr/://) == 7) {
1059 $ip =~ s/(\w+)/sprintf("%4s",$1)/eg;
1060 push @L, "$ip $_";
1061 } else {
1062 push @L, ' '.$_;
1063 }
1064 }
1065 }
1066 push @sorted,grep { s/^[ :\w]* // } sort @L;
1067 }
1068 if (@ips = grep /\./,@_) {
1069 my ($n,@L);
1070 foreach (@ips) {
1071 if (/\b(\d+)(\.(\d+))(\.(\d+))?(\.(\d+))?/) {
1072 $n = $1*256**3+($3||0)*256**2+($5||0)*256+($7||0);
1073 }
1074 push @L, sprintf("%010u $_",$n);
1075 }
1076 push @sorted,grep { s/^[ :\w]* // } sort @L;
1077 }
1078 return @sorted;
1079 }
1080
1081
1082 sub d3 {
1083 local $_ = shift;
1084 while (s/(\d)(\d\d\d\b)/$1,$2/) {};
1085 return $_;
1086 }
1087
1088
1089 sub usage {
1090 my $port = '';
1091 my $proto = 'http';
1092
1093 if ($durl =~ /:(\d+)/) { $port = ":$1" }
1094 if ($durl =~ /^(https?)/) { $proto = $1 }
1095
1096 $0 =~ s:.*/::;
1097 print <<EOD;
1098 Usages:
1099 $0 -u # list full users
1100 $0 -u user # show user config
1101 $0 -u user auth-ID # create new user or set new auth-ID
1102 $0 -/ admin auth-ID # set new admin and auth-ID
1103 $0 -q user s:quota # set new disk quota (MB) for sender user
1104 $0 -q user r:quota # set new disk quota (MB) for recipient user
1105 $0 -Ri user # restrict user: only internal domain recipients allowed
1106 $0 -Rl user # restrict user: only local users as recipients allowed
1107 $0 -rr user # edit user recipients restriction
1108 $0 -ru user # edit user upload restriction
1109 $0 -rd user # edit user download restriction
1110 $0 -d user # delete user
1111 $0 -D user "reason" # disable user (use "!reason" to show user the reason)
1112 $0 -D user "no" # re-enable user
1113 $0 -P user [yn] # make user persistent = no account expiration (yes,no)
1114 $0 -a user [ynd] # set user autodelete default (yes,no,delay)
1115 $0 -n user [dbn] # set user notification default (detailed,brief,no)
1116 $0 -k user days # set user keep default in days (none = remove)
1117 $0 -K user days # set user keep maximum in days (none = remove)
1118 $0 -s user [yn] # (dis)allow user archive sharing
1119 $0 -x user [yn] # (dis)allow user document exchange
1120 $0 -C user [yn] # set user captive (yes,no)
1121 $0 -y user [yn] # set user "fex yourself" web default (yes,no)
1122 $0 -S fup # file upload statistics
1123 $0 -S fop # file download statistics
1124 $0 -S sex # stream statistics
1125 $0 -v # show server config
1126 $0 -c # edit server config
1127 $0 -w # watch fexsrv.log (continously)
1128 $0 -l # list pending files with download URLs
1129 $0 -L [filter] # list pending files in detail
1130 $0 -M # list pending files with TO/FROM/FILE
1131 $0 -M TO/FROM/FILE # resend notification email
1132 $0 -b # list login blocked IPs
1133 $0 -b IP # show fail logins for IP
1134 $0 -b IP - # unblock IP
1135 $0 -m "reason" # enter maintenance mode (reason "exit" to leave)
1136 $0 -E # show usage examples
1137 EOD
1138 # $0 -A alias:hostname # add new virtual server
1139 # $0 -V virtualhost ... # operations on virtualhost (alias or hostname)
1140 if (-x "$FEXHOME/cgi-bin/fac") {
1141 print "See also web admin interface $proto://$hostname$port/fac\n";
1142 }
1143 exit shift;
1144 }
1145
1146 sub examples {
1147 $0 =~ s:.*/::;
1148 print <<EOD;
1149 # create new user:
1150 $0 -u framstag\@rus.uni-stuttgart.de schwubbeldidu
1151
1152 # set 10 GB sender quota for this user:
1153 $0 -q framstag\@rus.uni-stuttgart.de s:10240
1154
1155 # set default file expiration to 30 days for this user:
1156 $0 -k framstag\@rus.uni-stuttgart.de 30
1157
1158 # disable account expiration for this user:
1159 $0 -P framstag\@rus.uni-stuttgart.de y
1160
1161 # list spooled files and resend notification email for this file:
1162 $0 -M | grep frams
1163 $0 -M framstag\@rus.uni-stuttgart.de/hoppel\@flupp.org/jump.avi
1164 EOD
1165 exit;
1166 }