Debian Codegraph @

cgi-bin/fac

raw
1 #!/usr/bin/perl -Tw
2
3 # F*EX CGI for administration
4 #
5 # Original author: Andre Hafner <[email protected]>
6 #
7
8 BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 }
9
10 $| = 1;
11
12 $fac = $0;
13 $fac =~ s:.*/::;
14
15 # add fex lib
16 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/ or die "no \$FEXLIB\n";
17
18 # import from fex.pp and fex.ph
19 our ($FEXHOME,$spooldir,$logdir,$docdir,$akeydir,$durl,$mdomain,$bs,$hostname);
20 our ($keep_default,$keep_max,$recipient_quota,$sender_quota,$autodelete);
21 our ($admin,$admin_pw,$admin_hosts);
22 our ($sendmail,$bcc);
23 our $error = 'FAC error';
24
25 # load common code, local config : $HOME/lib/fex.ph
26 require "$FEXLIB/fex.pp";
27
28 my @http_auth = ();
29 my $ra = $ENV{REMOTE_ADDR}||0;
30
31 if (not @admin_hosts or not ipin($ra,@admin_hosts)) {
32 html_error($error,"Administration from your host ($ra) is not allowed.");
33 }
34
35 html_error($error,"\$admin not configured in $FEXLIB/fex.ph") unless $admin;
36
37 # auth-ID encryption is missing
38 html_error($error,"F*EX web administration is no longer supported. ".
39 "Use the CLI fac with UNIX user fex.");
40
41 chdir $spooldir or http_die("$spooldir - $!");
42 chomp($admin_pw = slurp("$admin/@")||'');
43 html_error($error,"no F*EX account for admin $admin\n") unless $admin_pw;
44
45 # redirect to https if configured
46 (undef,$port) = split(':',$ENV{HTTP_HOST}||'');
47 $port ||= $ENV{PROTO} eq 'https' ? 443 : 80;
48 if ($port == 80 and open my $x,'/etc/xinetd.d/fexs') {
49 while (<$x>) {
50 if (/^\s*disable\s*=\s*no/) {
51 nvt_print(
52 "HTTP/1.1 301 Moved Permanently",
53 "Location: https://$hostname$ENV{REQUEST_URI}",
54 'Content-Length: 0',
55 ''
56 );
57 exit;
58 }
59 }
60 close $x;
61 }
62
63 our %PARAM;
64 &parse_parameters;
65
66 $action = $PARAM{"action"}||'';
67
68 # authentication
69 &require_akey;
70
71 my $fup = $durl;
72 $fup =~ s:/fop:/fup:;
73
74 my $http_client = $ENV{HTTP_USER_AGENT} || '';
75
76 # files to save with backup function
77 my @backup_files = qw(
78 htdocs/index.html
79 lib/fex.ph
80 lib/fup.pl
81 spool/*@*/@*
82 spool/*@*/.auto
83 );
84
85 # backup goes first
86 if ($action eq "backup") {
87 &backup;
88 exit;
89 }
90
91 http_header('200 OK');
92
93 $_ = html_header("F*EX Admin Control for $hostname");
94 s:</h1>: (<a href="?action=logout">logout</a>)</h1>:;
95 print;
96
97 my $nav_user =
98 "<li><a href=\"?action=create\">Create new user</a>\n".
99 "<li><a href=\"?action=change-auth\">Change user auth-ID</a>\n".
100 "<li><a href=\"?action=edit\">Edit user restrictions file</a>\n".
101 "<li><a href=\"?action=delete\">Delete existing user</a>\n".
102 "<li><a href=\"?action=quota\">Manage disk quota</a>\n";
103
104 my $nav_log =
105 "<li><a href=\"?action=fup.log\">Get fup.log</a>\n".
106 "<li><a href=\"?action=fop.log\">Get fop.log</a>\n".
107 "<li><a href=\"?action=error.log\">Get error.log</a>\n";
108
109 if (-f "$logdir/fexsrv.log") {
110 $nav_log =
111 "<li><a href=\"?action=watch\">Watch logfile</a>\n".
112 "<li><a href=\"?action=fexsrv.log\">Get fexsrv.log</a>\n".
113 $nav_log;
114 }
115
116 my $nav_backup =
117 "<li><a href=\"?action=backup\">Download backup<br>(config only)</a>\n".
118 "<li><a href=\"?action=restore\">Restore backup</a>\n";
119
120 my $nav_show =
121 "<li><a href=\"?action=list\">List spooled files</a>\n".
122 "<li><a href=\"?action=showquota\">Show quotas (sender/recipient)</a>\n".
123 "<li><a href=\"?action=showconfig\">Show server config</a>\n".
124 "<li><a href=\"?action=userconfig\">Show user config</a>\n";
125
126 my $nav_edit =
127 "<li><a href=\"?action=editconfig\">Edit config</a>\n".
128 "<li><a href=\"?action=editindex\">Edit index.html</a>\n";
129
130 pq(qq(
131 '<table border="0">'
132 ' <th>manage user</th>'
133 ' <th>show</th>'
134 ' <th>log files</th>'
135 ' <th>edit</th>'
136 ' <th>backup</th>'
137 ' <tr valign="top">'
138 ' <td><ul>$nav_user</ul>'
139 ' <td><ul>$nav_show</ul>'
140 ' <td><ul>$nav_log</ul>'
141 ' <td><ul>$nav_edit</ul>'
142 ' <td><ul>$nav_backup</ul>'
143 ' </tr>'
144 '</table>'
145 '<hr>'
146 ));
147
148 my @user_items = &userList;
149
150 if ($action eq "create") { &createUserForm }
151 elsif ($action eq "change-auth") { &changeAuthForm }
152 elsif ($action eq "edit") { &editRestrictionsForm }
153 elsif ($action eq "delete") { &deleteUserForm }
154 elsif ($action eq "quota") { &changeQuotaForm }
155 elsif ($action eq "list") { &listFiles }
156 elsif ($action eq "showquota") { &showQuota }
157 elsif ($action eq "showconfig") { &showConfig }
158 elsif ($action eq "userconfig") { &userConfigForm }
159 elsif ($action eq "watch") { &watchLog }
160 elsif ($action eq "fexsrv.log") { &getlog("fexsrv.log") }
161 elsif ($action eq "fup.log") { &getlog("fup.log") }
162 elsif ($action eq "fop.log") { &getlog("fop.log") }
163 elsif ($action eq "error.log") { &getlog("error.log") }
164 elsif ($action eq "editconfig") { &editFile("$FEXLIB/fex.ph") }
165 elsif ($action eq "editindex") { &editFile("$docdir/index.html") }
166 elsif ($action eq "backup") { &backup }
167 elsif ($action eq "restore") { &restoreForm }
168
169 if (defined $PARAM{"createUser"}) {
170 createUser($PARAM{"createUser"}, $PARAM{"authID"});
171 } elsif (defined $PARAM{"changeAuthUser"}) {
172 if ($PARAM{"changeAuthUser"} =~ /^#.*/) {
173 &changeAuthForm;
174 } else {
175 changeUser($PARAM{"changeAuthUser"}, $PARAM{"authID"});
176 }
177 } elsif (defined $PARAM{"showUserConfig"}) {
178 if ($PARAM{"showUserConfig"} =~ /^#.*/) {
179 &userConfigForm;
180 } else {
181 showUserConfig($PARAM{"showUserConfig"});
182 }
183 } elsif (defined $PARAM{"deleteUser"}) {
184 if ($PARAM{"deleteUser"} =~ /^#.*/) {
185 &deleteUserForm;
186 } else {
187 deleteUser($PARAM{"deleteUser"});
188 }
189 } elsif (defined $PARAM{"userQuota"}) {
190 if ($PARAM{"userQuota"} =~ /^#.*/) {
191 &changeQuotaForm;
192 } else {
193 if (defined $PARAM{"default quota"}) {
194 $user = normalize_user($PARAM{"userQuota"});
195 unlink "$user/\@QUOTA";
196 print "$user has now default quota:<p>\n";
197 print "recipient quota: $recipient_quota MB<br>\n";
198 print "sender quota: $sender_quota MB<br>\n";
199 &end_html;
200 } else {
201 alterQuota(
202 $PARAM{"userQuota"},
203 $PARAM{"recipientQuota"},
204 $PARAM{"senderQuota"}
205 );
206 }
207 }
208 } elsif (defined $PARAM{"editUser"}) {
209 if ($PARAM{"editUser"} =~ /^#.*/) {
210 &editRestrictionsForm;
211 } else {
212 $user = normalize_user($PARAM{"editUser"});
213 if (defined $PARAM{"delete file"}) {
214 unlink "$user/\@ALLOWED_RECIPIENTS";
215 print "upload restrictions for $user have been deleted\n";
216 &end_html;
217 } else {
218 editUser($user);
219 }
220 }
221 } elsif ($PARAM{"contentBox"} and $PARAM{"ar"}) {
222 saveFile($PARAM{"contentBox"},$PARAM{"ar"});
223 } elsif ($PARAM{"upload_archive"}) {
224 restore($PARAM{"upload_archive"}{data});
225 }
226
227 &end_html;
228
229 #######
230 # declaration of formular functions
231 #######
232
233 # formular for creating new users
234 # required arguments: -
235 sub createUserForm {
236 print h3("Create new user");
237 pq(qq(
238 '<form action="/$fac" method="post" enctype="multipart/form-data">'
239 '<table>'
240 '<tr>'
241 '<td>user</td><td><input type="text" name="createUser" size="80"></td>'
242 '</tr>'
243 '<tr>'
244 '<td>auth-ID:</td><td><input type="text" name="authID" size="16"></td>'
245 '</tr>'
246 '</table>'
247 '<input type="submit" name="create user" value="create user">'
248 '</form>'
249 ));
250 &end_html;
251 }
252
253 # formular for changing auth-id of an user
254 # required arguments: -
255 sub changeAuthForm {
256 my @option = map { "<option value=\"$_\">$_</option>\n" } @user_items;
257
258 print h3("change auth-ID");
259 pq(qq(
260 '<form action="/$fac" method="post" enctype="multipart/form-data">'
261 '<table>'
262 '<tr>'
263 '<td>user:</td><td><select name="changeAuthUser">@option</select></td>'
264 '</tr>'
265 '<tr>'
266 '<td>new auth-ID:</td><td><input type="text" name="authID" size="16"></td>'
267 '</tr>'
268 '</table>'
269 '<input type="submit" name="change" value="change">'
270 '</form>'
271 ));
272 &end_html;
273 }
274
275 # formular choosing user, whose config files shall be shown
276 # required arguments: -
277 sub userConfigForm {
278 my @option = map { "<option value=\"$_\">$_</option>\n" } @user_items;
279
280 print h3("Show user config files");
281 pq(qq(
282 '<form action="/$fac" method="post enctype="multipart/form-data">'
283 '<table>'
284 '<tr>'
285 '<td>user:</td><td><select name="showUserConfig">@option</select></td>'
286 '</tr>'
287 '</table>'
288 '<input type="submit" name="show config files" value="show config files">'
289 '</form>'
290 ));
291 &end_html;
292 }
293
294 # formular for choosing user, whose restriction file shall be edited
295 # required arguments: -
296 sub editRestrictionsForm {
297 my @option = map { "<option value=\"$_\">$_</option>\n" } @user_items;
298
299 print h3("Edit user restriction file");
300 pq(qq(
301 '<form action="/$fac" method="post enctype="multipart/form-data">'
302 '<table>'
303 '<tr>'
304 '<td>user:</td><td><select name="editUser">@option</select></td>'
305 '</tr>'
306 '</table>'
307 '<input type="submit" name="edit file" value="edit file">'
308 '<input type="submit" name="delete file" value="delete file">'
309 '</form>'
310 ));
311 &end_html;
312 }
313
314 # formular for choosing user, who shall be removed
315 # required arguments: -
316 sub deleteUserForm {
317 my @option = map { "<option value=\"$_\">$_</option>\n" } @user_items;
318
319 print h3("Delete existing user");
320 pq(qq(
321 '<form action="/$fac" method="post enctype="multipart/form-data">'
322 '<table>'
323 '<tr>'
324 '<td>user:</td><td><select name="deleteUser">@option</select></td>'
325 '</tr>'
326 '</table>'
327 '<input type="submit" name="delete user" value="delete user">'
328 '</form>'
329 ));
330 &end_html;
331 }
332
333 # formular for changing an user's quota file
334 # required arguments: -
335 sub changeQuotaForm {
336 my $user;
337 my @option;
338 my $rquota = '';
339 my $squota = '';
340
341 if ($user = $PARAM{"user"}) {
342
343 $user = normalize_user($user);
344 $rquota = $1 if ($PARAM{"rquota"}||'') =~ /^(\d+)$/;
345 $squota = $1 if ($PARAM{"squota"}||'') =~ /^(\d+)$/;
346 }
347
348 foreach (@user_items) {
349 if ($user and $user eq $_) {
350 push @option,"<option value=\"$_\" selected>$_</option>\n";
351 } else {
352 push @option,"<option value=\"$_\">$_</option>\n";
353 }
354 }
355
356 print h3("Manage disk quota");
357 pq(qq(
358 '<form action="/$fac" method="post" enctype="multipart/form-data">'
359 '<table>'
360 '<tr>'
361 '<td>user:</td><td><select name="userQuota">@option</select></td>'
362 '</tr>'
363 '<tr>'
364 '<td>new quota for recipient:</td>'
365 '<td><input type="text" name="recipientQuota" size="12" value=\"$rquota\">'
366 ' MB (optional)</td>'
367 '</tr>'
368 '<tr>'
369 '<td>new quota for sender:</td>'
370 '<td><input type="text" name="senderQuota" size="12" value=\"$squota\">'
371 ' MB (optional)</td>'
372 '</tr>'
373 '</table>'
374 '<input type="submit" name="change quota" value="change quota">'
375 '<input type="submit" name="default quota" value="default quota">'
376 '</form>'
377 ));
378 &end_html;
379 }
380
381 # formular for choosing backup file to restore
382 # required arguments: -
383 sub restoreForm {
384 print h2("restore config");
385 pq(qq(
386 'Specify the backup-archive you want to restore:<br>'
387 '<form action="/$fac" method="post" enctype="multipart/form-data">'
388 '<input type="file" name="upload_archive" size="80"><br>'
389 '<input type="submit" name="restore" value="restore">'
390 '</form>'
391 ));
392 &end_html;
393 }
394
395
396 #######
397 # declaration user functions
398 #######
399
400 # function for creating new users
401 # required arguments: username, auth-id
402 sub createUser {
403 my ($user,$id) = @_;
404 my $idf;
405
406 http_die("not enough arguments in createUser") unless $id;
407
408 $user = normalize_user($user);
409
410 unless (-d "$user") {
411 mkdir "$user",0755 or http_die("cannot mkdir $user - $!");
412 }
413
414 $idf = "$user/@";
415
416 if (-f $idf) {
417 html_error($error,"There is already an user $user!");
418 }
419
420 open $idf,'>',$idf or http_die("cannot write $idf - $!");
421 print {$idf} $id,"\n";
422 close $idf or http_die("cannot write $idf - $!");
423 print "<code>\n";
424 printf "%s?from=%s&ID=%s<br>\n",$fup,$user,$id;
425 printf "%s/%s<p>\n",$fup,b64("from=$user&id=$id");
426 print "</code>\n";
427 notifyUser($user,$id);
428 print "An information email to $user has been sent.\n";
429 &end_html;
430 }
431
432 # function for changing an user's auth-ID
433 # required arguments: username, auth-id
434 sub changeUser {
435 my ($user,$id) = @_;
436
437 http_die("not enough arguments in changeUser") unless $id;
438
439 $id = despace($id);
440 $user = normalize_user($user);
441 my $idf = "$user/@";
442 print "<code>\n";
443 print "$idf<p>";
444
445 open $idf,'>',$idf or http_die("cannot write $idf - $!");
446 print {$idf} $id,"\n";
447 close $idf or http_die("cannot write $idf - $!");
448 printf "%s?from=%s&ID=%s<br>\n",$fup,$user,$id;
449 printf "%s/%s\n",$fup,b64("from=$user&id=$id");
450 print "</code><p>\n";
451 notifyUser($user,$id,"change-auth");
452 print "An information email to $user has been sent.\n";
453 &end_html;
454 }
455
456 # function for showing an user's config files
457 # required arguments: username
458 sub showUserConfig {
459 my $user = shift;
460
461 http_die("not enough arguments in showUserConfig!") unless $user;
462 $user = normalize_user($user);
463
464 chdir "$user" or http_die("could not change directory $user - $!");
465 print h2("Config files of $user");
466
467 foreach my $file (glob('.auto @* @GROUP/*')) {
468 if (-f $file and not -l $file and $file !~ /.*~$/) {
469 print h3($file), "\n";
470 open $file,'<',$file or http_die("cannot open $file - $!");
471 # print "<table border=1><tr><td>\n";
472 dumpfile($file);
473 # print "</tr></table>\n";
474 close $file;
475 }
476 }
477 &end_html;
478 }
479
480 # function for editing an user's recipient/sender restrictions
481 # required arguments: username
482 sub editUser {
483 my $user = shift;
484 my $content;
485
486 http_die("not enough arguments in editUser") unless $user;
487 $user = normalize_user($user);
488 http_die("no user $user") unless -d $user;
489 my $ar = "$user/\@ALLOWED_RECIPIENTS";
490 unless (-f $ar) {
491 open $ar,'>',$ar or http_die("cannot open $ar - $!");
492 print {$ar}<<'EOD';
493 # Restrict allowed recipients. Only those listed here are allowed.
494 # Make this file COMPLETLY empty if you want to disable the restriction.
495 # An allowed recipient is an email address, you can use * as wildcard.
496 # Example: *@flupp.org
497 EOD
498 close $ar;
499 }
500 $content = dehtml(slurp($ar));
501 pq(qq(
502 'Edit restrictions file for user $user :<br>'
503 '<form action="/$fac" method="post" enctype="multipart/form-data">'
504 '<textarea name="contentBox" rows="10" cols="80">'
505 '$content'
506 '</textarea><br>'
507 '<input type="hidden" name="ar" value="$ar">'
508 '<input type="submit" name="save changes" value="save changes">'
509 '</form>'
510 ));
511 &end_html;
512 }
513
514 # function for deleting files
515 # required arguments: list of Files
516 sub deleteFiles {
517 http_die("not enough arguments in deleteFiles") unless (my @files = @_);
518
519 foreach (@files) {
520 if (-e) {
521 if (unlink $_) {
522 print "file has been deleted: $_<br>\n";
523 } else {
524 print "file could not be deleted: $_ - $!<br>\n";
525 }
526 } else {
527 print "file does not exists: $_<br>\n";
528 }
529 }
530 &end_html;
531 }
532
533 # function for saving a single file
534 # required arguments: content, location
535 sub saveFile {
536 my ($rf,$ar) = @_;
537 my $new;
538
539 http_die("not enough arguments in saveFile") unless $ar;
540
541 if ($ar eq 'index.html') {
542 $ar = "$docdir/index.html"
543 } elsif ($ar eq 'fex.ph') {
544 $ar = "$FEXLIB/fex.ph"
545 } elsif ($ar =~ m'^([^/]+/\@ALLOWED_RECIPIENTS)$') {
546 $ar = $1;
547 } else {
548 http_die("unknown file $ar")
549 }
550
551 $new = $ar.'_new';
552 if ($ar =~ /fex.ph$/) {
553 open $new,'>',$new or http_die("cannot open ${ar}_new - $!");
554 print {$new} $rf;
555 close $new or http_die("cannot write $new - $!");;
556 my $status = dehtml(`perl -c $FEXLIB/fex.ph_new 2>&1`);
557 if ($status =~ /syntax OK/ ) {
558 rename $ar,"$ar~";
559 rename $new,$ar;
560 http_die("cannot write $ar~ - $!") if $?;
561 } else {
562 rename "$ar~",$ar;
563 pq(qq(
564 'No valid syntax in configuration file:'
565 '<p><pre>$status</pre><p>'
566 '<a href="javascript:history.back()">back</a>'
567 ));
568 &end_html;
569 }
570 } else {
571 system 'mv',$ar,"$ar~";
572 }
573 $rf =~ s/^\s+$//;
574 open $ar,'>',$ar or http_die("cannot write $ar - $!");
575 print {$ar} $rf;
576 close $ar or http_die("cannot write $ar - $!");;
577 print "<code>$ar</code> has been saved\n";
578 &end_html;
579 }
580
581 # function for deleting existing user
582 # required arguments: username
583 sub deleteUser {
584 my $user = shift;
585
586 http_die("not enough arguments in deleteUser") unless $user;
587
588 $user = normalize_user($user);
589
590 $idf = "$user/\@";
591 http_die("no such user $user") unless -f $idf;
592 unlink $idf or http_die("cannot remove $idf - $!");
593 unlink "$user/\@ALLOWED_RECIPIENTS";
594 unlink "$user/\@SUBUSER";
595 rmrf("$user/\@GROUP");
596 print "$user deleted\n";
597 &end_html;
598 }
599
600 # function for saving quota information for one single user
601 # required arguments: username, recipient-quota, sender-quota
602 sub alterQuota {
603 my ($user,$rq,$sq) = @_;
604 my ($rquota,$squota);
605 my $qf;
606
607 $user = normalize_user($user);
608 http_die("$user is not a F*EX user") unless -d $user;
609
610 $rquota = $squota = '';
611 $qf = "$user/\@QUOTA";
612 if (open $qf,$qf) {
613 while (<$qf>) {
614 s/#.*//;
615 $rquota = $1 if /recipient.*?(\d+)/i;
616 $squota = $1 if /sender.*?(\d+)/i;
617 }
618 close $qf;
619 }
620
621 $rquota = $1 if $rq and $rq =~ /(\d+)/;
622 $squota = $1 if $sq and $sq =~ /(\d+)/;
623 open $qf,'>',$qf or http_die("cannot write $qf - $!");
624 print {$qf} "recipient:$rquota\n" if $rquota;
625 print {$qf} "sender:$squota\n" if $squota;
626 close $qf or http_die("cannot write $qf - $!");
627
628 $rquota = $recipient_quota unless $rquota;
629 $squota = $sender_quota unless $squota;
630 print h3("New quotas for $user");
631 print "recipient quota: $rquota MB<br>\n";
632 print "sender quota: $squota MB<br>\n";
633 &end_html;
634 }
635
636 # function for listing f*exed files
637 # required arguments: -
638 sub listFiles {
639 print h3("List current files");
640 my ($file,$dkey);
641 print "<pre>\n";
642 foreach $recipient (glob "*@*") {
643 next if -l $recipient;
644 foreach $file (glob "$recipient/*/*") {
645 if (-s "$file/data" and $dkey = readlink("$file/dkey") and -l ".dkeys/$dkey") {
646 ($to,$from,$file) = split "/",$file;
647 $file = htmlquote($file);
648 print "$from &rarr; $to : $durl/$dkey/$file\n";
649 }
650 }
651 }
652 print "</pre>\n";
653 &end_html;
654 }
655
656 # function for watching the fex-logfile
657 # required arguments: -
658 sub watchLog {
659 if (-f "$logdir/fexsrv.log") {
660 print h2("polling fexsrv.log"),"\n";
661 open my $log,"$FEXHOME/bin/logwatch|"
662 or http_die("cannot run $FEXHOME/bin/logwatch - $!");
663 dumpfile($log);
664 } else {
665 print h2("no fexsrv.log");
666 }
667 &end_html;
668 }
669
670 # function for showing logfiles
671 # required arguments: logfile-name
672 sub getlog {
673 my $log = shift or http_die("not enough arguments in getLog");
674
675 print h2("show $log");
676 if (open $log,"$logdir/$log") {
677 dumpfile($log);
678 close $log;
679 } else {
680 http_die("cannot open $logdir/$log - $!");
681 }
682 &end_html;
683 }
684
685 # function for creating a new backup file
686 # required arguments: -
687 sub backup {
688 my @d = localtime time;
689 my $date = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
690 my $backup = "backup/config-$date.tar";
691 my $http_client = $ENV{HTTP_USER_AGENT} || '';
692 my $size;
693
694 my $home = $FEXHOME;
695 $home = $1 if $ENV{VHOST} and $ENV{VHOST} =~ /:(.+)/;
696
697 chdir $home or http_die("$home - $!");
698
699 unless (-d "backup") {
700 mkdir "backup",0700 or http_die("cannot mkdir backup - $!");
701 }
702
703 system "tar -cf $backup @backup_files 2>/dev/null";
704
705 $size = -s $backup or http_die("backup file empty");
706
707 open $backup,'<',$backup or http_die("cannot open $backup - $!");
708
709 nvt_print(
710 'HTTP/1.1 200 OK',
711 "Content-Length: $size",
712 "Content-Type: application/octet-stream; filename=fex-backup-$date.tar",
713 "Content-Disposition: attachment; filename=\"fex-backup-$date.tar\"",
714 "",
715 );
716
717 while (read($backup,my $b,$bs)) {
718 print $b or last;
719 }
720
721 exit;
722 }
723
724 # function for restoring an old configuration file
725 # required arguments: uploaded archive
726 sub restore {
727 my $archive_file = shift or http_die("not enough arguments in restore!");
728 my $restore = "backup.tar";
729 my $home = $FEXHOME;
730
731 $home = $1 if $ENV{VHOST} and $ENV{VHOST} =~ /:(.+)/;
732
733 chdir $home or http_die("$home - $!");
734 mkdir 'backup';
735
736 open $restore,'>',$restore or http_die("cannot open $restore - $!");
737 print {$restore} $archive_file;
738 close $restore or http_die("cannot write $restore - $!");
739 if (-s $restore) {
740 print "file upload successful<br>\n";
741 print "saving actual config in $home/backup/config.tar<br>\n";
742 print "<pre>\n";
743 system "tar -cf backup/config.tar @backup_files";
744 print "</pre>\n";
745 print "starting restore:\n<p>\n";
746 print "<pre>\n";
747 system "tar -xvf $restore";
748 unlink $restore;
749 &end_html;
750 } else {
751 http_die("upload error - no file data received");
752 }
753 }
754
755 # function for editing a text-file
756 # required arguments: filepath, filename
757 sub editFile {
758 my $ar = shift;
759 my $file;
760
761 $file = dehtml(slurp($ar));
762
763 $ar =~ s:.*/::;
764
765 print h2("edit <code>$ar<code>");
766
767 pq(qq(
768 '<form action="/$fac" enctype="multipart/form-data" method="post">'
769 '<textarea name="contentBox" rows="26" cols="80">'
770 '$file'
771 '</textarea><br>'
772 '<input type="hidden" name="ar" value="$ar">'
773 '<input type="submit" name="save changes" value="save changes">'
774 '</form>'
775 ));
776 &end_html;
777 }
778
779 # function for showing all users' quotas
780 # required arguments: -
781 sub showQuota {
782
783 print h2("Show quotas (domain sorted, values in MB)");
784 print "<table border=\"1\"><tr>";
785 foreach (@user_items) {
786 if (/\#\#\#\s(\S+)/) {
787 print "<tr>";
788 print "<th>\@$1</th>";
789 print "<th>sender</th>";
790 print "<th>sender (used)</th>";
791 print "<th>recipient</th>";
792 print "<th>recipient (used)</th>";
793 print "</tr>\n";
794 # $table = $_;
795 } else {
796 my $rquota = $recipient_quota;
797 my $squota = $sender_quota;
798 my $rquota_used = 0;
799 my $squota_used = 0;
800 my $user = $_;
801 ($squota,$squota_used) = check_sender_quota($user);
802 ($rquota,$rquota_used) = check_recipient_quota($user);
803 my $action = "quota&user=$user&rquota=$rquota&squota=$squota";
804 s/\@.*//;
805 print "<tr>";
806 print "<td><a href=\"?action=$action\">$_</a></td>";
807 print "<td align=\"right\">$squota</td>";
808 print "<td align=\"right\">$squota_used</td>";
809 print "<td align=\"right\">$rquota</td>";
810 print "<td align=\"right\">$rquota_used</td>";
811 print "</tr>\n";
812 }
813 }
814 print "</table>\n";
815 &end_html;
816
817 }
818
819 # function for showing fex-server configuration
820 # required arguments: -
821 sub showConfig {
822 print h3("Show config");
823 print "<table border=\"0\">\n";
824 printf "<tr><td>spooldir:</td><td>%s</td>\n",$spooldir;
825 printf "<tr><td>logdir:</td><td>%s</td>\n",$logdir;
826 printf "<tr><td>docdir:</td><td>%s</td>\n",$docdir;
827 printf "<tr><td>durl:</td><td>%s</td>\n",$durl;
828 printf "<tr><td>mdomain:</td><td>%s</td>\n",$mdomain||'';
829 printf "<tr><td>autodelete:</td><td>%s</td>\n",$autodelete;
830 printf "<tr><td>keep:</td><td>%s</td>\n",$keep_default;
831 printf "<tr><td>keep_max:</td><td>%s</td>\n",$keep_max;
832 printf "<tr><td>recipient_quota:</td><td>%s</td>\n",$recipient_quota;
833 printf "<tr><td>sender_quota:</td><td>%s</td>\n",$sender_quota;
834 printf "<tr><td>admin:</td><td>%s</td>\n",$admin;
835 print "</table>\n";
836 &end_html;
837 }
838
839 # require authentification
840 sub require_akey {
841 my $id;
842 my $rid;
843
844 if ($action eq 'logout') {
845 if (($ENV{HTTP_COOKIE}||'') =~ /akey=(\w+)/) {
846 unlink "$akeydir/$1";
847 }
848 nvt_print(
849 "HTTP/1.1 301 Moved Permanently",
850 "Location: /$fac",
851 'Content-Length: 0',
852 "Set-Cookie: akey=; Max-Age=0; Discard",
853 ''
854 );
855 &reexec;
856 }
857
858 $rid = slurp("$admin/@") or html_error($error,"no F*EX account for $admin");
859 chomp $rid;
860
861 $id = $PARAM{"id"};
862
863 if ($id) {
864 # correct auth-ID?
865 if ($id eq $rid) {
866 $akey = genakey($admin);
867 return;
868 }
869 } elsif ($akey) {
870 # correct akey?
871 return if $akey and readlink_("$akeydir/$akey") eq "../$admin";
872 }
873
874 http_header('200 OK');
875 print html_header("F*EX Admin Control for $hostname");
876
877 if ($akey) {
878 pq(qq(
879 '<font color="red"><h3>'
880 ' wrong akey for $admin'
881 '</h3></font>'
882 ));
883 }
884
885 if ($id and $id ne $rid) {
886 pq(qq(
887 '<font color="red"><h3>'
888 ' wrong auth-ID for $admin'
889 '</h3></font>'
890 ));
891 }
892
893 pq(qq(
894 '<form action="/$fac" method="post" enctype="multipart/form-data">'
895 ' auth-ID for $admin:'
896 ' <input type="password" name="id" size="16" autocomplete="off">'
897 '</form>'
898 ));
899 &end_html;
900 }
901
902
903 # function for checking simple HTTP authentication
904 # (not used any more, replaced with require_akey)
905 sub require_auth {
906 if ($ENV{HTTP_AUTHORIZATION} and $ENV{HTTP_AUTHORIZATION} =~ /Basic\s+(.+)/)
907 { @http_auth = split(':',decode_b64($1)) }
908 if (
909 @http_auth != 2
910 or $http_auth[0] !~ /^(fexmaster|admin|\Q$admin\E)$/
911 or $http_auth[1] ne $admin_pw
912 ) {
913 http_header(
914 '401 Authorization Required',
915 "WWW-Authenticate: Basic realm=$admin F*EX admin authentification",
916 'Content-Length: 0',
917 );
918 # control back to fexsrv for further HTTP handling
919 &reexec;
920 }
921 }
922
923
924 # function for sending notification mails to an user
925 # required arguments: username, auth-id, message-type
926 sub notifyUser {
927 my ($user,$id,$type) = @_;
928 my $url = $durl;
929 my $message = 'A F*EX account has been created for you. Use';
930
931 http_die("not enough arguments in createUser") unless $id;
932 if ($type and $type eq "change-auth") {
933 $message = 'New auth-ID for your F*EX account has been set. Use'
934 }
935
936 $user = normalize_user($user);
937 open my $mail,'|-',$sendmail,'-f',$admin,$user,$bcc
938 or http_die("cannot start sendmail - $!");
939 $url =~ s:/fop::;
940 pq($mail,qq(
941 'From: $admin'
942 'To: $user'
943 'Subject: your F*EX account on $hostname'
944 'X-Mailer: F*EX'
945 ''
946 '$message'
947 ''
948 '$url/fup?from=$user'
949 'auth-ID: $id'
950 ''
951 'See $url/index.html for more information about F*EX.'
952 ''
953 'Questions? ==> F*EX admin: $admin'
954 ));
955 close $mail
956 or http_die("cannot send notification email (sendmail error $!)");
957 }
958
959
960 # sort key is the (inverse) domain
961 # required arguments: list of usernames (email addresses)
962 sub domainsort {
963 # http_die("not enough arguments in domainsort") unless (my @d = @_);
964 my @d = @_;
965 local $_;
966
967 foreach (@d) {
968 s/\s//g;
969 s/\./,/ while /\..*@/;
970 s/@/@./;
971 $_ = join('.',reverse(split /\./));
972 }
973
974 @d = sort { lc $a cmp lc $b } @d;
975
976 foreach (@d) {
977 $_ = join('.',reverse(split /\./));
978 s/,/./g;
979 s/@\./@/;
980 }
981
982 return @d;
983 }
984
985 # function for creating a sorted list of all users
986 # required arguments: -
987 sub userList {
988 my (@u,@list);
989 my $domain = '';
990 my $u;
991
992 foreach $u (glob('*@*')) {
993 next if -l $u;
994 push @u,$u if -f "$u/@";
995 }
996
997 foreach (domainsort(@u)) {
998 if (/@(.+)/) {
999 if ($1 ne $domain) {
1000 push @list,"### $1 ###";
1001 }
1002 push @list,$_;
1003 $domain = $1;
1004 }
1005 }
1006
1007 return @list;
1008 }
1009
1010
1011 sub dumpfile {
1012 my $file = shift;
1013
1014 print "<pre>\n";
1015 while (<$file>) { print dehtml($_) }
1016 print "\n</pre>\n";
1017 }
1018
1019
1020 sub h2 {
1021 local $_ = shift;
1022 chomp;
1023 return "<h2>$_</h2>\n";
1024 }
1025
1026
1027 sub h3 {
1028 local $_ = shift;
1029 chomp;
1030 return "<h3>$_</h3>\n";
1031 }
1032
1033
1034 sub end_html {
1035 print "</body></html>\n";
1036 exit;
1037 }
1038
1039
1040 sub dehtml {
1041 local $_ = shift;
1042 s/&/&amp;/g;
1043 s/</&lt;/g;
1044 return $_;
1045 }