Debian Codegraph @

cgi-bin/fas

raw
1 #!/usr/bin/perl -wT
2
3 # FEX CGI for file archive sharing
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7
8 BEGIN { ($ENV{PERLINIT}||'') =~ /(.+)/s and eval $1 }
9
10 # use utf8;
11 use strict qw(vars);
12 use Fcntl qw(:flock :seek :mode);
13 use Digest::MD5 qw(md5_hex);
14
15 # add fex lib
16 (our $FEXLIB) = $ENV{FEXLIB} =~ /(.+)/;
17 die "$0: no $FEXLIB\n" unless -d $FEXLIB;
18
19 our ($spooldir,$dkeydir,$akeydir);
20 our ($archive_sharing,$mdomain,$MB,$DS,$hostname,$admin,$locale);
21 our ($vp,$RA);
22 our ($sender_from,$sendmail,$bcc);
23 our (@remote_domains);
24 our (%PARAM);
25 our $akey = '';
26
27 # load common code, local config : $HOME/lib/fex.ph
28 require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";
29
30 our $error = 'F*EX file archive sharing ERROR';
31
32 my $share = '';
33 my $archive = '';
34 my $owner = '';
35 my $user = '';
36 my $master = '';
37 my $pkey = '';
38 my $id = '';
39 my $aid = '';
40 my $access = '';
41 my $remove = '';
42 my $rename = '';
43 my $clone = '';
44 my $copy = '';
45 my $modify = '';
46 my $comment = '';
47 my $maxversions;
48 my $show = '';
49 my $notify = '';
50 my $notification = '';
51 my $nshare = '';
52 my $nuser = '';
53 my $description = '';
54 my $narchive = '';
55 my $help;
56 my $accesshelp;
57 my $logout = '';
58 my $style;
59 my $atype = 'tar|tgz|zip|7z|gz';
60 my $alignc = 'align="center"';
61 my $alignl = 'align="left"';
62 my $alignr = 'align="right"';
63 my $back = "javascript:history.back()";
64 my $CGI = $ENV{SCRIPT_NAME};
65 my $URI = $ENV{REQUEST_URI};
66 my $formpara =
67 'method="post" accept-charset="UTF-8" enctype="multipart/form-data"';
68 my $head =
69 qq'$ENV{SERVER_NAME} F*EX <a href="/sharing/">archive sharing</a>';
70
71 $ENV{LOCALE} = $locale = 'english';
72
73 # $URI =~ m:^/fas\?owner=(.+)\?share=(.+)\?user=([^\?]+)$:)
74
75 if ($URI =~ m:^/fas/(.+\@[\w.-]+)/([\w-]+)/(.+\@[\w.-]+)/-$:) {
76 query_pkey(norm($1),$2,norm($3));
77 exit;
78 }
79
80 # user share URL
81 if ($URI =~ m:^/fas/(.+\@[\w.-]+)/([\w-]+)/(.+\@[\w.-]+)/(\w+)$:) {
82 $owner = norm($1);
83 $share = $2;
84 $user = norm($3);
85 $pkey = $4;
86 $show = "share:$share:archives";
87 }
88
89 if ($URI =~ m:^/fas\?owner=([^/?]+)$:) {
90 redirect("/fup?from=$1?forward=$ENV{SCRIPT_NAME}");
91 }
92
93 $akey = $ENV{AKEY}||'';
94
95 # look for CGI parameters
96 &parse_parameters;
97 foreach my $v (keys %PARAM) {
98 my $vv = $PARAM{$v};
99 if ($v =~ /^pkey$/i and $vv =~ /^(\w+)$/) { $pkey = $1 }
100 elsif ($v =~ /^akey$/i and $vv =~ /^(\w+)$/) { $akey = $1 }
101 elsif ($v =~ /^share$/i) { $share = norm($vv) }
102 elsif ($v =~ /^archive$/i) { $archive = anorm($vv) }
103 elsif ($v =~ /^user$/i) { $user = norm($vv) }
104 elsif ($v =~ /^aid$/i) { $aid = $vv }
105 elsif ($v =~ /^owner$/i) { $owner = norm($vv) }
106 elsif ($v =~ /^remove$/i) { $remove = norm($vv) }
107 elsif ($v =~ /^rename$/i) { $rename = norm($vv) }
108 elsif ($v =~ /^clone$/i) { $clone = norm($vv) }
109 elsif ($v =~ /^copy$/i) { $copy = norm($vv) }
110 elsif ($v =~ /^modify$/i) { $modify = norm($vv) }
111 elsif ($v =~ /^comment$/i) { $comment = $vv }
112 elsif ($v =~ /^maxversions$/i) { $maxversions = norm($vv) }
113 elsif ($v =~ /^show$/i) { $show = norm($vv) }
114 elsif ($v =~ /^access$/i) { $access = norm($vv) }
115 elsif ($v =~ /^notify$/i) { $notify = norm($vv) }
116 elsif ($v =~ /^notification$/i) { $notification = norm($vv) }
117 elsif ($v =~ /^description$/i) { $description = $vv }
118 elsif ($v =~ /^nshare$/i) { $nshare = norm($vv) }
119 elsif ($v =~ /^narchive$/i) { $narchive = anorm($vv) }
120 elsif ($v =~ /^nuser$/i) { $nuser = lc(norm($vv)) }
121 }
122
123 $akey = '' unless -f "$akeydir/$akey/@";
124
125 redirect("/fup?forward=$ENV{REQUEST_URI}") unless $akey or $pkey;
126
127 # if ($owner and $share and $user and not $akey and not $pkey) {
128 # query_pkey($owner,$share,$user);
129 # exit;
130 # }
131
132 if ($akey and not $pkey) {
133 $owner = untaint(basename(readlink("$akeydir/$akey")||'/'));
134 if ($id = readline1("$owner/@")) {
135 $id = untaint($id);
136 $user = $owner;
137 } else {
138 redirect("/fup?forward=$ENV{REQUEST_URI}");
139 # redirect("/fup?forward=fas");
140 }
141 }
142
143 http_die("owner missing") unless $owner;
144 http_die("user missing") unless $user;
145
146 $owner .= '@'.$mdomain if $mdomain and $owner !~ /@/;
147 $user .= '@'.$mdomain if $mdomain and $user !~ /@/;
148
149 unless (checkaddress($owner)) {
150 http_die("$owner is not a valid email address")
151 }
152 unless (checkaddress($user) or $user =~ /\.test$/) {
153 http_die("$user is not a valid email address")
154 }
155 unless ($share =~ /^[\w-]*$/) {
156 http_die("$share is not a valid share name")
157 }
158
159 my $sharing = readlink_("$owner/\@ARCHIVE_SHARING");
160 if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) {
161 http_die("archive sharing is not allowed");
162 }
163
164 my $shared = "$owner/SHARE";
165
166 if ($pkey) {
167 http_die("share missing") unless $share;
168 if ($owner eq $user) {
169 $id = readline1("$owner/@")||'';
170 if ($pkey eq md5_hex($id)) {
171 $pkey = '';
172 $akey = untaint(md5_hex("$owner:$id"));
173 mksymlink("$akeydir/$akey","../$owner");
174 } else {
175 http_die("wrong share URL");
176 }
177 } else {
178 my $rpkey = readline1("$shared/$share/users/$user/pkey")||'';
179 # passkey = short alias of pkey
180 if (length($pkey) == 8) {
181 $pkey = $rpkey if $pkey eq substr(md5_hex($rpkey),0,8);
182 }
183 http_die("wrong pkey") if $pkey ne $rpkey;
184 my $access = readlink("$shared/$share/users/$user/access")
185 or http_die("no access for $owner/$share/$user");
186 $master = $user if $access eq 'manage';
187 $CGI .= "?owner=$owner?share=$share?user=$user?pkey=$pkey";
188 }
189 }
190
191 $help = qqq(qq(
192 '<p><hr><p>'
193 'You can share archive files with other users.<br>'
194 'Archives and users are stored in a so called <em>share</em> on the F*EX server.<br>'
195 'A <em>share archive</em> is a tar or zip container file with a unique name and a version date.<br>'
196 'An <em>archive set</em> comprises all versions of this archive.<br>'
197 ));
198 if ($id) {
199 $help .= qqq(qq(
200 'To create new archives or users click on a share name.<br>'
201 ));
202 } elsif ($master) {
203 $help .= qqq(qq(
204 'There can be any number of shares. Currently you see only (this) one.<br>'
205 ));
206 } else {
207 $help .= qqq(qq(
208 'There can be any number of shares. Currently you see only (this) one.<br>'
209 'Only the share master(s) can see, add or modify users.<br>'
210 ));
211 }
212 $help .= qqq(qq(
213 'To upload or download archive files use'
214 '<a href="http://fex.belwue.de/usecases/fexpush.html">fexpush and fexpull</a>'
215 '<p>'
216 '<a href="/sharing/">More information</a>'
217 ));
218 $accesshelp = qqq(qq(
219 '<p>'
220 '<b>user access rights:</b>'
221 '<table border=1>'
222 '<tr><td $alignc>read<td><ul>'
223 ' <li>read archives'
224 '</ul></tr>'
225 '<tr><td $alignc>write<td><ul>'
226 ' <li>read archives<br>'
227 ' <li>write new archive version'
228 '</ul></tr>'
229 '<tr><td $alignc>add<td><ul>'
230 ' <li>read archives<br>'
231 ' <li>write new archive version<br>'
232 ' <li>add new archive set</tr>'
233 '</ul></tr>'
234 '<tr><td $alignc>manage<td><ul>'
235 ' <li>read archives<br>'
236 ' <li>write new archive version<br>'
237 ' <li>add new archive set<br>'
238 ' <li>add new user<br>'
239 ' <li>remove archive (version)<br>'
240 ' <li>remove user<br>'
241 ' <li>(un)set user access rights read|write|add'
242 '</ul></tr>'
243 '</table>'
244 '<p>'
245 '[<a href="/sharing/">More information</a>]'
246 ));
247
248 if ($modify =~ /^comment:(.+):(.+):(($vp)\.($atype))$/) {
249 my $share = $1;
250 my $archive = $2;
251 my $vt = $3;
252 my $version = $4;
253 my $container = $5;
254 my $avtd = "$shared/$share/archives/$archive/$vt";
255 unless (-f "$avtd/data") {
256 http_die("$share:$archive:$vt does not exist");
257 }
258 my $comment = unhtml(decode_utf8(slurp("$avtd/comment")||''));
259 $comment =~ s/\n/ /g;
260 http_header('200 OK');
261 print html_header($head);
262 pq(qq(
263 '<form action="/$CGI" $formpara>'
264 'Comment for "$share:$archive:$version"<br>'
265 '<input type="text" name="comment" value="$comment" size="64"><br>'
266 '<input type="hidden" name="share" value="$share">'
267 '<input type="hidden" name="archive" value="$archive:$vt">'
268 '<input type="submit" value="save">'
269 '</form>'
270 ));
271 exit;
272 }
273
274 if ($share and defined $PARAM{comment} and
275 $archive =~ /(.+)_($vp\.($atype))$/)
276 {
277 my $archive = $1;
278 my $vt = $2;
279 my $sds = "$shared/$share";
280 unless ($id or $master) {
281 if (readlink_("$sds/archives/$archive/$vt/uploader") ne $user) {
282 http_die("you are not the uploader of $share:$archive:$vt");
283 }
284 }
285 $comment =~ s/[\r\n]/ /g;
286 $comment =~ s/^\s+//;
287 $comment =~ s/\s+$//;
288 my $cf = "$sds/archives/$archive/$vt/comment";
289 if (open $cf,'>',$cf) {
290 print {$cf} $comment,"\n";
291 close $cf;
292 faslog($sds,"$user changed comment for $archive:$vt");
293 }
294 if ($id or $master) {
295 redirect("/$CGI?show=archive:$share:$archive");
296 } else {
297 redirect("/$CGI?show=archive:$archive");
298 }
299 }
300
301 if ($id) {
302
303 $logout = qq'[<a href="/fup?logout=logout">logout</a>]';
304
305 if ($share and $share ne '_') {
306 my $uo = "$shared/$share/users/$owner";
307 unless (-d $uo) {
308 mkdirp($uo);
309 mksymlink("$uo/notification",'yes');
310 }
311 }
312
313 if ($remove =~ /^share:([^\s\/]+)$/) {
314 my $share = $1;
315 my $sds = "$shared/$share";
316 if ($sds =~ s/!$//) {
317 rmrf($sds);
318 } else {
319 my @n;
320 my $na = scalar(@n=glob("$sds/archives/*"));
321 my $nu = scalar(@n=glob("$sds/users/*/pkey"));
322 if ($nu or $na) {
323 http_header('200 OK');
324 print html_header($head);
325 $nu = "$nu user" . ($nu == 1 ? '' : 's');
326 $na = "$na archive" . ($na == 1 ? '' : 's');
327 pq(qq(
328 '<h3>Share "$share" contains $na and $nu.</h3>'
329 '<p>'
330 '[<a href="/$CGI?remove=share:$share!">really remove</a>]'
331 '[<a href="$back">keep</a>]'
332 '</body></html>'
333 ));
334 exit;
335 } else {
336 rmrf($sds);
337 }
338 }
339 redirect("/$CGI");
340 }
341
342 if ($clone =~ /^share:(.+)$/) {
343 my $share = $1;
344 if ($nshare) {
345 if (-e "$shared/$nshare") {
346 http_die("share $nshare does already exist");
347 }
348 unless (-d "$shared/$share") {
349 http_die("unknown share $share");
350 }
351 unless (mkdir "$shared/$nshare") {
352 http_die("cannot mkdir $nshare - $!\n");
353 }
354 esystem("rsync -a --exclude data $shared/$share/* $shared/$nshare/");
355 unlink "$shared/$nshare/log";
356 foreach my $a (glob "$shared/$nshare/archives/*/*") {
357 $a = untaint($a);
358 unlink "$a/download","$a/dkey";
359 }
360 foreach my $p (glob "$shared/$nshare/users/*/pkey") {
361 $p = untaint($p);
362 my $pkey = randstring(32);
363 if (open $p,'>',$p) {
364 print {$p} "$pkey\n";
365 close $p;
366 }
367 }
368 foreach my $data (glob "$shared/$share/archives/*/*/data") {
369 my $ndata = $data;
370 $ndata =~ s:\Q/$share/:/$nshare/:;
371 link untaint($data),untaint($ndata) or
372 http_die("cannot link $ndata - $!\n");
373 }
374 redirect("/$CGI");
375 } else {
376 http_header('200 OK');
377 print html_header($head);
378 pq(qq(
379 '<form action="/$CGI" $formpara>'
380 'Clone share "$share" to'
381 '<input type="text" name="nshare" size="16">'
382 '<input type="hidden" name="clone" value="share:$share">'
383 '<input type="submit" value="ok">'
384 '</form>'
385 '<p>'
386 '<a href="$back">back</a>'
387 '</body></html>'
388 ));
389 exit;
390 }
391 }
392
393 if ($copy =~ /^archive:(.+):(.+):($vp\.($atype))$/) {
394 my $share = $1;
395 my $archive = $2;
396 my $avt = $3;
397 my $sda = "$shared/$share/archives";
398 if ($narchive) {
399 if (-e "$sda/$narchive/$avt") {
400 http_die("archive $narchive:avt does already exist in share $share");
401 }
402 unless (-d "$sda/$archive/$avt") {
403 http_die("unknown archive $archive:$avt in share $share");
404 }
405 unless (-d "$sda/$narchive") {
406 http_die("unknown archive $narchive in share $share");
407 }
408 esystem("rsync -a --exclude data $sda/$archive/$avt $sda/$narchive/");
409 faslog("$shared/$share",
410 "$user copied archive $archive:$avt to archive $narchive");
411 unlink "$sda/$narchive/$avt/download";
412 unlink "$sda/$narchive/$avt/dkey";
413 my $data = "$sda/$archive/$avt/data";
414 my $ndata = "$sda/$narchive/$avt/data";
415 link $data,$ndata or http_die("cannot link $data to $ndata - $!\n");
416 if ($nshare eq '_') {
417 my $dkey = randstring(8);
418 symlink "../$sda/$narchive/$avt","$dkeydir/$dkey" and
419 symlink $dkey,"$sda/$narchive/$avt/dkey";
420 }
421 redirect("/$CGI?show=archive:$share:$narchive");
422 } else {
423 my $archiveselect = '';
424 foreach my $a (glob("$sda/*")) {
425 if (-d $a and not -e "$a/$avt") {
426 $archiveselect .= sprintf '<option>%s</option>',basename($a);
427 }
428 }
429 http_header('200 OK');
430 print html_header($head);
431 if ($archiveselect) {
432 pq(qq(
433 '<form action="/$CGI" $formpara>'
434 'Copy archive "$share:$archive:$avt" to archive set'
435 '<select name="narchive" size="1">$archiveselect</select>'
436 '<input type="hidden" name="copy" value="archive:$share:$archive:$avt">'
437 '<input type="submit" value="ok">'
438 '</form>'
439 ));
440 } else {
441 pq(qq(
442 '<h3>No other archive sets available</h3>'
443 '[<a href="/$CGI?show=share:$share:archives">manage archive sets</a>]'
444 ));
445 }
446 pq(qq(
447 '<p>'
448 '[<a href="$back">back</a>]'
449 '</body></html>'
450 ));
451 exit;
452 }
453 }
454
455 if ($clone =~ /^archive:(.+):(.+)$/) {
456 my $share = $1;
457 my $archive = $2;
458 my $sds = "$shared/$share";
459 if ($nshare) {
460 my $sdn = "$shared/$nshare";
461 if (-e "$sdn/archives/$archive") {
462 http_die("archive $archive does already exist in share $nshare");
463 }
464 unless (-d "$sds/archives/$archive") {
465 http_die("unknown archive $archive in share $share");
466 }
467 esystem("rsync -a --exclude data $sds/archives/$archive $sdn/archives");
468 faslog($sdn,"$user copied archive $archive from share $share");
469 foreach my $a (glob "$sdn/archives/$archive/*") {
470 $a = untaint($a);
471 unlink "$a/download","$a/dkey";
472 }
473 foreach my $data (glob "$sds/archives/$archive/*/data") {
474 my $ndata = $data;
475 $ndata =~ s:\Q/$share/:/$nshare/:;
476 unless (link untaint($data),untaint($ndata)) {
477 http_die("cannot link $ndata - $!\n");
478 }
479 }
480 if ($nshare eq '_') {
481 foreach my $a (glob "$sdn/archives/$archive/*") {
482 $a = untaint($a);
483 my $dkey = randstring(8);
484 symlink "../$a","$dkeydir/$dkey" and
485 symlink $dkey,"$a/dkey";
486 }
487 }
488 redirect("/$CGI?show=share:$nshare:archives");
489 } else {
490 my $shareselect = '';
491 foreach my $s (glob("$shared/*")) {
492 if (-d $s and not -e "$s/archives/$archive") {
493 $shareselect .= sprintf '<option>%s</option>',basename($s);
494 }
495 }
496 http_header('200 OK');
497 print html_header($head);
498 if ($shareselect) {
499 pq(qq(
500 '<form action="/$CGI" $formpara>'
501 'Copy archive set "$archive" to share'
502 '<select name="nshare" size="1">$shareselect</select>'
503 '<input type="hidden" name="clone" value="archive:$share:$archive">'
504 '<input type="submit" value="ok">'
505 '</form>'
506 ));
507 } else {
508 pq(qq(
509 '<h3>No other shares available</h3>'
510 '[<a href="/$CGI?show=shares">manage shares</a>]'
511 ));
512 }
513 pq(qq(
514 '<p>'
515 '[<a href="$back">back</a>]'
516 '</body></html>'
517 ));
518 exit;
519 }
520 }
521
522 if ($nshare) {
523 $nshare =~ s/[^\w-]/_/g;
524 my $sdn = "$shared/$nshare";
525 unless (-d $sdn) {
526 mkdirp("$sdn/users");
527 symlink 'yes',"$sdn/notification";
528 faslog($sdn,"$user created share");
529 }
530 redirect("/$CGI");
531 }
532
533 if (defined($maxversions) and $share) {
534 my $mv = 0;
535 my $sds = "$shared/$share";
536 if (length($archive)) {
537 my $versions = "$sds/archives/$archive/versions";
538 unlink $versions;
539 if ($maxversions =~ /^(\d+)$/ and $mv = int($1)) {
540 symlink $mv,$versions;
541 }
542 faslog($sds,"$user set maxversions=$mv for archive $archive");
543 redirect("/$CGI?show=archive:$share:$archive");
544 } else {
545 my $versions = "$sds/versions";
546 unlink $versions;
547 if ($maxversions =~ /^(\d+)$/ and $mv = int($1)) {
548 symlink $mv,$versions;
549 }
550 faslog($sds,"$user set maxversions=$mv as default");
551 redirect("/$CGI?show=share:$share:archives");
552 }
553 }
554
555 if ($show =~ /^log:(.+)/) {
556 my $share = $1;
557 my $log = "$shared/$share/log";
558 my @log = ();
559 open $log,$log or http_die("cannot open $share/log - $!\n");
560 http_header('200 OK');
561 print html_header($head);
562 my $sv = '';
563 $sv .= qq' [<a href="/$CGI?show=share:$share:archives">show archive sets</a>]';
564 if ($share ne '_') {
565 $sv .= qq' [<a href="/$CGI?show=share:$share:users">show users</a>]';
566 }
567 $sv .= qq' [<a href="/$CGI">show shares</a>]';
568 pq(qq(
569 '<table>'
570 '<tr><td><b>owner: <td><b>$owner</tr>'
571 '<tr><td><b>share: <td><b>$share</tr>'
572 '</table>'
573 '<p>'
574 '$sv'
575 '<p>'
576 '<pre>'
577 ));
578 while (<$log>) {
579 s/</&lt;/g;
580 push @log,$_;
581 }
582 print reverse @log;
583 pq(qq(
584 '</pre>'
585 '$logout'
586 '</body></html>'
587 ));
588 exit;
589 }
590
591 } else { # not owner
592
593 if ($show eq 'shares') {
594 # share index
595 $CGI =~ s/\?.*//;
596 http_header('200 OK');
597 print html_header($head);
598 my ($oowner,$share);
599 my @shares = ();
600 foreach my $share (_sort(glob("$user/SHARE/*"))) {
601 push @shares,untaint(basename($share)) if -d $share;
602 }
603 if (@shares) {
604 pq(qq(
605 '<h3>shares of owner $user:</h3>'
606 '<table border="1">'
607 '<tr><th>share<th>archive sets<th>users<th>size<th>action</tr>'
608 ));
609 foreach my $share (@shares) {
610 my $sds = "$user/SHARE/$share";
611 my @n;
612 my $na = scalar(@n=glob("$sds/archives/*"));
613 my $nu = scalar(@n=glob("$sds/users/*/pkey"));
614 my $size = 0;
615 map { $size += -s } glob("$sds/archives/*/*/data");
616 $size = int($size/$MB)||'<1' if $size;
617 pq(qq(
618 '<tr>'
619 '<td $alignc><a href="/$CGI?show=share:$share:archives">$share</a>'
620 '<td $alignr>$na'
621 '<td $alignr>$nu'
622 '<td $alignr>$size&nbsp;MB'
623 ));
624 if ($share eq '_') {
625 pq(qq'<td>');
626 } else {
627 pq(qq(
628 '<td>[<a href="/$CGI?remove=share:$share">remove</a>]'
629 ' [<a href="/$CGI?clone=share:$share">clone</a>]'
630 ));
631 }
632 pq(qq'</tr>');
633 }
634 pq(qq(
635 '</table>'
636 '<p>'
637 ));
638 }
639 othershares($user);
640 exit;
641 }
642
643 if ($master and $clone =~ /^share:(.+)$/) {
644 my $share = $1;
645 my $sds = "$shared/$share";
646 if (readlink_("$sds/users/$master/access") ne 'manage') {
647 http_die("no access for $master to $owner/$share");
648 }
649 unless (-d $sds) {
650 http_die("unknown share $share");
651 }
652 my $ms = "$master/SHARE/$share";
653 if (-d $ms) {
654 http_die(qq'you already have a share "$share"');
655 }
656 mkdirp("$master/SHARE");
657 esystem("rsync -a --exclude data $sds $master/SHARE");
658 unlink "$ms/log";
659 foreach my $u (glob "$ms/users/*") {
660 my $p = untaint("$u/pkey");
661 my $pkey = randstring(32);
662 if (open $p,'>',$p) {
663 print {$p} "$pkey\n";
664 close $p;
665 }
666 }
667 unlink "$ms/users/$master/pkey";
668 unlink "$ms/users/$master/access";
669 foreach my $a (glob "$ms/archives/*/*") {
670 $a = untaint($a);
671 unlink "$a/download","$a/dkey";
672 }
673 foreach my $data (glob "$sds/archives/*/*/data") {
674 my $ndata = $data;
675 $ndata =~ s:\Q$owner:$master:;
676 unless (link untaint($data),untaint($ndata)) {
677 http_die("cannot link $ndata - $!\n");
678 }
679 }
680 http_header('200 OK');
681 print html_header($head);
682 pq(qq(
683 '<h3>Share "$share" cloned from $owner to your account $master</h3>'
684 '<p>'
685 '<a href="/$CGI">continue</a>'
686 '</body></html>'
687 ));
688 exit;
689 }
690
691 if ($clone =~ /^archive:(.+):(.+)$/) {
692 my $share = $1;
693 my $archive = $2;
694 unless (-f "$user/@") {
695 http_die("$user is not a registered full user");
696 }
697 my $sharing = readlink_("$user/\@ARCHIVE_SHARING");
698 if ($sharing =~ /no/i or not $archive_sharing and $sharing !~ /yes/i) {
699 http_die("archive sharing is not allowed for user $user");
700 }
701 my $sds = "$shared/$share";
702 unless (-d "$sds/users/$user") {
703 http_die("no access for $user to $owner/$share");
704 }
705 my $ushared = "$user/SHARE";
706 if ($nshare and $aid) {
707 if ($aid ne readline1("$user/@")||'') {
708 http_die("wrong auth-ID for user $user");
709 }
710 my $usn = "$ushared/$nshare";
711 if (-e "$usn/archives/$archive") {
712 http_die("archive $archive does already exist in your share $nshare");
713 }
714 unless (-d "$sds/archives/$archive") {
715 http_die("unknown archive $archive in $owner/$share");
716 }
717 esystem("rsync -a --exclude data $sds/archives/$archive $usn/archives");
718 faslog($usn,"$user copied archive $archive from share $owner:$share");
719 foreach my $a (glob "$usn/archives/$archive/*") {
720 $a = untaint($a);
721 unlink "$a/download","$a/dkey";
722 }
723 foreach my $data (glob "$sds/archives/$archive/*/data") {
724 my $ndata = $data;
725 $ndata =~ s:\Q/$share/:/$nshare/:;
726 $ndata =~ s:\Q$owner:$user:;
727 unless (link untaint($data),untaint($ndata)) {
728 http_die("cannot link $ndata - $!\n");
729 }
730 }
731 if ($nshare eq '_') {
732 foreach my $a (glob "$usn/archives/$archive/*") {
733 $a = untaint($a);
734 my $dkey = randstring(8);
735 symlink "../$a","$dkeydir/$dkey" and
736 symlink $dkey,"$a/dkey";
737 }
738 }
739 redirect("/$CGI");
740 } else {
741 mkdirp("$user/SHARE/_/archives");
742 my $shareselect = '';
743 foreach my $s (glob("$user/SHARE/*")) {
744 if (-d $s and not -e "$s/archives/$archive") {
745 $shareselect .= sprintf '<option>%s</option>',basename($s);
746 }
747 }
748 http_header('200 OK');
749 print html_header($head);
750 if ($shareselect) {
751 pq(qq(
752 '<form action="/$CGI" $formpara>'
753 'Your account: $user<br>'
754 'Your auth-ID: <input type="password" name="aid" size="12">'
755 '<p>'
756 'Copy archive set "$archive" from $owner share "$share" to your share'
757 '<select name="nshare" size="1">$shareselect</select>'
758 '<input type="hidden" name="clone" value="archive:$share:$archive">'
759 '<input type="submit" value="ok">'
760 '</form>'
761 ));
762 } else {
763 pq(qq(
764 '<h3>You already have a archive set "$archive" in your account $user</h3>'
765 ));
766 }
767 pq(qq(
768 '<p>'
769 '<a href="$back">back</a>'
770 '</body></html>'
771 ));
772 exit;
773 }
774 }
775 }
776
777 if ($id or $master) {
778
779 if ($show =~ /^share:(.+):users/) {
780 my $share = $1;
781 my $sds = "$shared/$share";
782 my $ssa = "show=share:$share:archives";
783 my $fas = "$ENV{PROTO}://$ENV{HTTP_HOST}/$ENV{SCRIPT_NAME}";
784 unless (-d $sds) {
785 http_die("no such share $owner/$share");
786 }
787 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
788 http_die("no access for $master to $owner/$share");
789 }
790 http_header('200 OK');
791 print html_header($head);
792 pq(qq(
793 '<script>'
794 'function popup(title,text) {'
795 " var w = window.open('','','width=1200,height=160');"
796 " w.document.write('<title>'+title+'</title>');"
797 " w.document.write(text);"
798 " w.document.write('<p><a href=\"#\" onclick=\"window.close()\">close</a>');"
799 ' w.document.close();'
800 '}'
801 '</script>'
802 ));
803 if ($master) {
804 pq(qq(
805 '<table>'
806 '<tr><td><b>share: <td><b>$share</tr>'
807 '<tr><td><b>owner: <td><b>$owner</tr>'
808 '<tr><td><b>user: <td><b>$user</b> (logged in)</tr>'
809 '<tr><td><b>access:<td><b>manage</tr>'
810 '</table>'
811 '<p>'
812 '[<a href="/$CGI?$ssa">show share</a>]'
813 '<p>'
814 ));
815 } else { # owner
816 my $sv = '';
817 $sv .= qq' [<a href="/$CGI?$ssa">show archive sets</a>]';
818 $sv .= qq' [<a href="/$CGI">show all shares</a>]';
819 $sv .= qq' [<a href="/$CGI?show=log:$share">view log</a>]' if -f "$sds/log";
820 my $sowner =
821 sprintf qq'<a href="" onclick="alert(\'%s/%s/%s/%s/%s\')">%s</a>',
822 $fas,$owner,$share,$owner,md5_hex($id),$owner;
823 pq(qq(
824 '<table>'
825 '<tr><td><b>owner:<td><b>$sowner</b> (logged in)</tr>'
826 '<tr><td><b>share:<td><b>$share</tr>'
827 '</table>'
828 '<p>'
829 '$sv'
830 '<p>'
831 ));
832 }
833 pq(qq(
834 '<p>'
835 '<table><tr valign="top"><td>'
836 '<table border="1">'
837 '<tr><th>user'
838 ' <th>access'
839 ' <th title="send notification email for new uploads">upload<br>notification'
840 # ' <th>quota'
841 ' <th>action'
842 '</tr>'
843 ));
844 my @users = glob("$sds/users/*");
845 foreach my $user (@users) {
846 my $upkey = readline1("$user/pkey") or next;
847 my $access = readlink("$user/access")||'none';
848 my $notification = readlink("$user/notification")||'no';
849 $user = basename($user);
850 my $su = "$share:$user";
851 my $nny = '';
852 if ($notification eq 'no') {
853 $nny =
854 qq'<a href="/$CGI?notification=$su:never">never</a>|'.
855 qq'no|'.
856 qq'<a href="/$CGI?notification=$su:yes">yes</a>';
857 } elsif ($notification eq 'never') {
858 $nny =
859 qq'never|'.
860 qq'<a href="/$CGI?notification=$su:no">no</a>|'.
861 qq'<a href="/$CGI?notification=$su:yes">yes</a>';
862 } elsif ($notification eq 'yes') {
863 $nny =
864 qq'<a href="/$CGI?notification=$su:never">never</a>|'.
865 qq'<a href="/$CGI?notification=$su:no">no</a>|'.
866 qq'yes';
867 }
868 my $quota = 0;
869 my $read = $access eq 'read' ? 'read' :
870 qq'<a href="/$CGI?access=$su:read">read</a>';
871 my $write = $access eq 'write' ? 'write' :
872 qq'<a href="/$CGI?access=$su:write">write</a>';
873 my $add = $access eq 'add' ? 'add' :
874 qq'<a href="/$CGI?access=$su:add">add</a>';
875 my $manage = $access eq 'manage' ? 'manage' :
876 qq'<a href="/$CGI?access=$su:manage">manage</a>';
877 pq(qq'<tr>');
878 # passkey = short alias of pkey
879 my $supkey = substr(md5_hex($upkey),0,8);
880 my $title = 'share user URL';
881 my $popup =
882 "share user direct URL:<br>\\n".
883 "<nobr>$fas/$owner/$share/$user/$upkey</nobr><p>\\n".
884 "share user login URL:<br>\\n".
885 "<nobr>$fas/$owner/$share/$user/-</nobr><br>\\n".
886 "passkey: $supkey\\n";
887 pq(qq(
888 '<td><a href="" onclick="popup(\'$title\',\'$popup\')">$user</a>'
889 '<td $alignc>$access'
890 '<td $alignc>$notification'
891 # '<td $alignc>$quota'
892 '<td><ul>'
893 ));
894 if ($notification ne 'never') {
895 pq(qq' <li><a href="/$CGI?notify=$su">send user info mail</a><br>');
896 }
897 if ($user =~ /^anonymous/) {
898 pq(qq(
899 ' <li><a href="/$CGI?remove=user:$su">remove user</a><br>'
900 ));
901 } elsif ($id) {
902 pq(qq(
903 ' <li>access=[$read|$write|$add|$manage]<br>'
904 ' <li>notification=[$nny]<br>'
905 ' <li><a href="/$CGI?remove=user:$su">remove user</a><br>'
906 ));
907 } elsif ($master eq $user) {
908 pq(qq(
909 ' <li>notification=[$nny]<br>'
910 ));
911 } elsif ($access ne 'manage') {
912 pq(qq(
913 ' <li>access=[$read|$write|$add]<br>'
914 ' <li>notification=[$nny]<br>'
915 ' <li><a href="/$CGI?remove=user:$su">remove user</a><br>'
916 ));
917 }
918 pq(qq'</ul></tr>');
919 }
920 my $nuser = "anonymous\@$ENV{SERVER_NAME}";
921 $nuser = '' if -d "$sds/users/$nuser";
922 pq(qq(
923 '</table>'
924 '<form action="/$CGI" $formpara>'
925 'new user: <input type="text" name="nuser" value="$nuser" size="40">'
926 '<input type="submit" value="create">'
927 '<input type="hidden" name="share" value="$share">'
928 '</form>'
929 '<td>$accesshelp'
930 '</tr></table>'
931 '$logout'
932 '</body></html>'
933 ));
934 exit;
935 }
936
937 if ($show =~ /^archive:([^:]+):([^:]+)(:edit)?/) {
938 my $share = $1;
939 my $archive = $2;
940 my $edit = $3||'';
941 my $show = "show=share:$share:archives";
942 my $sds = "$shared/$share";
943 unless (-d "$sds/archives/$archive") {
944 http_die("no such archive $owner/$share/$archive");
945 }
946 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
947 http_die("no access for $master to $owner/$share");
948 }
949 my $sd = "$sds/archives/$archive/description";
950 if (defined $PARAM{description}) {
951 open my $sd,'>',$sd or http_die("cannot write $sd - $!\n");
952 $description =~ s/\n*$/\n/;
953 print {$sd} $description;
954 close $sd;
955 faslog($sds,"$user changed description for archive $archive");
956 } else {
957 chomp($description = slurp($sd)||'');
958 }
959 $description = unhtml(decode_utf8($description));
960 my $ed;
961 if ($edit) {
962 $ed =
963 qq'<br>'.
964 qq'<form action="/$CGI?show=archive:$share:$archive" $formpara>'.
965 qq'<input type="text" name="description" value="$description" size="64"><br>'.
966 qq'<input type="submit" value="save">'.
967 qq'</form>';
968 } else {
969 $description =~ s!(https?://\S+)!<a href="$1">$1</a>!g;
970 $ed =
971 qq'[<a href="/$CGI?show=archive:$share:$archive:edit">edit description</a>]<br>'.
972 qq'<em>$description</em>';
973 }
974 http_header('200 OK');
975 print html_header($head);
976 pq(qq'<table>');
977 my $td = 'td valign="top"';
978 if ($master) {
979 my $access = readlink("$sds/users/$master/access")||'none';
980 my $key = md5_hex("$archive:$pkey");
981 my $afop = qq'<a href="/fop/$owner/$share/$user/$key/$archive">'.
982 qq'$archive</a>';
983 pq(qq(
984 '<tr><$td><b>share: <td><b>$share</tr>'
985 '<tr><$td><b>owner: <td><b>$owner</tr>'
986 '<tr><$td><b>user: <td><b>$user</b> (logged in)</tr>'
987 '<tr><$td><b>archive:<td><b>$afop</b> $ed</tr>'
988 '<tr><$td><b>access: <td><b>$access</tr>'
989 '</table>'
990 '<p>'
991 '[<a href="/$CGI?$show">show share</a>]'
992 ));
993 } else { # owner
994 my $mv = readlink("$sds/archives/$archive/versions")||
995 readlink("$sds/versions")||'unlimited'||
996 'unlimited';
997 my $key = md5_hex("$archive:$id");
998 my $afop = qq'<a href="/fop/$owner/$share/$owner/$key/$archive">'.
999 qq'$archive</a>';
1000 pq(qq(
1001 '<tr><$td><b>owner: <td><b>$owner</b> (logged in)</tr>'
1002 '<tr><$td><b>share: <td><b>$share</tr>'
1003 '<tr><$td><b>archive:<td><b>$afop</b> $ed</tr>'
1004 '<tr><$td><b>max versions:</b><br>(per uploader)<td>'
1005 ' <form action="/$CGI" $formpara>'
1006 ' $mv [new value:'
1007 ' <input type="text" name="maxversions" size="3">'
1008 ' <input type="hidden" name="share" value="$share">'
1009 ' <input type="hidden" name="archive" value="$archive">'
1010 ' <input type="submit" value="set">'
1011 ' will be active after next upload]'
1012 ' </form></tr>'
1013 '</table>'
1014 '<p>'
1015 '[<a href="/$CGI?$show">show share</a>]'
1016 '[<a href="/$CGI?show=shares">show all shares</a>]'
1017 ));
1018 }
1019 pq(qq(
1020 '<p>'
1021 '<table border="1">'
1022 ));
1023 if ($share eq '_') {
1024 pq(qq'<tr><th>version<th>container<th>size<th><th>content<th>comment<th>action</tr>');
1025 } else {
1026 pq(qq'<tr><th>version<th>container<th>size<th>uploader<th>content<th>comment<th>action</tr>');
1027 }
1028 my @archives = glob("$sds/archives/$archive/*");
1029 foreach my $archive (reverse @archives) {
1030 my $size = -s "$archive/data" or next;
1031 my $content = content($archive);
1032 $size = int($size/$MB)||'<1' if $size;
1033 $archive =~ m:.*/archives/(.+)/($vp)\.($atype): or next;
1034 my $avt = $1.'_'.$2.'.'.$3;
1035 my $cp = "archive:$share:$1:$2.$3";
1036 my $mod = "comment:$share:$1:$2.$3";
1037 my $version = $2;
1038 my $container = $3;
1039 my $uploader = readlink("$archive/uploader")||$owner;
1040 my $comment = unhtml(readline1("$archive/comment")||'');
1041 my $url = "/fop/$owner/$share/$user/";
1042 if ($id) { $url .= md5_hex("$avt:$id") }
1043 else { $url .= md5_hex("$avt:$pkey") }
1044 $url .= "/$avt";
1045 if ($share eq '_' and my $dkey = readlink("$archive/dkey")) {
1046 $url = "$ENV{PROTO}://$ENV{HTTP_HOST}/fop/$dkey/$avt";
1047 $uploader = ''; # "<code>$url</code>";
1048 }
1049 pq(qq(
1050 '<tr>'
1051 '<td><a href="$url">$version</a>'
1052 '<td $alignc>$container'
1053 '<td $alignr>$size&nbsp;MB'
1054 '<td><nobr>$uploader</nobr>'
1055 '<td>$content'
1056 '<td>$comment'
1057 '<td>[<a href="/$CGI?remove=$cp">remove</a>]'
1058 ' [<a href="/$CGI?copy=$cp">copy</a>]'
1059 ' [<a href="/$CGI?modify=$mod">edit&nbsp;comment</a>]'
1060 '</tr>'
1061 ));
1062 }
1063 pq(qq(
1064 '</table>'
1065 '<p>'
1066 '[<a href="/fup?from=$user?to=$owner:share=$share?archive=$archive?submit=share?pkey=$pkey">upload file</a>]'
1067 # '[<a href="/$CGI?$show">back</a>]'
1068 '<p>'
1069 '$logout'
1070 '</body></html>'
1071 ));
1072 exit;
1073 }
1074
1075 if ($rename =~ /^archive:(.+):(.+)$/) {
1076 my $share = $1;
1077 my $archive = $2;
1078 my $sad = "$shared/$share/archives";
1079 if ($narchive) {
1080 if (-e "$sad/$narchive") {
1081 http_die("archive $narchive does already exist in share $share");
1082 }
1083 unless (-d "$sad/$archive") {
1084 http_die("unknown archive $archive in share $share");
1085 }
1086 rename "$sad/$archive","$sad/$narchive"
1087 or http_die("cannot rename $sad/$archive to $sad/$narchive - $!");
1088 faslog("$shared/$share","$user renamed archive $archive to $narchive");
1089 redirect("/$CGI?show=share:$share:archives");
1090 } else {
1091 http_header('200 OK');
1092 print html_header($head);
1093 pq(qq(
1094 '<form action="/$CGI" $formpara>'
1095 'Rename archive set "$archive" in share "$share" to'
1096 '<input type="text" name="narchive" size="32">'
1097 '<input type="hidden" name="rename" value="archive:$share:$archive">'
1098 '<input type="submit" value="ok">'
1099 '</form>'
1100 '<p>'
1101 '<a href="$back">back</a>'
1102 '</body></html>'
1103 ));
1104 exit;
1105 }
1106 }
1107
1108 if ($access =~ /([\w-]+):(.+\@.+):(read|write|add|manage)/) {
1109 my $share = $1;
1110 my $suser = $2;
1111 my $access = $3;
1112 my $sds = "$shared/$share";
1113 my $suserd = "$shared/$share/users/$suser";
1114 unless (-d $suserd) {
1115 http_die("no such user $owner/$share/$suser");
1116 }
1117 if ($suser eq $owner) {
1118 http_die("cannot modify owner access right");
1119 }
1120 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
1121 http_die("no access for $master to $owner/$share");
1122 }
1123 if ($master and readlink_("$suserd/access") eq 'manage') {
1124 http_die("cannot modify master user $suser")
1125 }
1126 if ($access eq 'manage' and
1127 (not -f "$suser/@" or grep /ALLOWED|HOSTS/,glob("$suser/\@*"))) {
1128 http_die("cannot make external user $suser to master user")
1129 }
1130 if ($id or readlink_("$suserd/access") ne 'manage') {
1131 mksymlink("$suserd/access",$access);
1132 faslog($sds,"$user set access=$access for $suser");
1133 }
1134 redirect("/$CGI?show=share:$share:users");
1135 }
1136
1137 if ($notification =~ /([\w-]+):(.+\@.+):(yes|no|never)/) {
1138 my $share = $1;
1139 my $suser = $2;
1140 my $notification = $3;
1141 my $sds = "$shared/$share";
1142 my $suserd = "$sds/users/$suser";
1143 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
1144 http_die("no access for $master to $owner/$share");
1145 }
1146 if ($id or $master eq $suser or
1147 $suser ne $owner and readlink_("$suserd/access") ne 'manage')
1148 {
1149 unless (-d $suserd) {
1150 http_die("no such user $owner/$share/$suser");
1151 }
1152 mksymlink("$suserd/notification",$notification);
1153 faslog($sds,"$user set notification=$notification for $suser");
1154 if ($ENV{HTTP_REFERER} =~ /show=.*:users/) {
1155 redirect("/$CGI?show=share:$share:users");
1156 } else {
1157 redirect("/$CGI?show=share:$share:archives");
1158 }
1159 }
1160 }
1161
1162 if ($notify =~ /(.+):(.+)/) {
1163 my $share = $1;
1164 my $suser = $2;
1165 my $sds = "$shared/$share";
1166 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
1167 http_die("no access for $master to $owner/$share");
1168 }
1169 http_die("illegal share user $suser") unless checkaddress($suser);
1170 $pkey = readline1("$sds/users/$suser/pkey") or
1171 http_die("no pkey for user $suser");
1172 my $url = "$ENV{PROTO}://$ENV{HTTP_HOST}/$ENV{SCRIPT_NAME}"
1173 . "/$owner/$share/$suser/$pkey";
1174 my $description = decode_utf8(slurp("$sds/description")||'');
1175 fas_notify($suser,$user,$url,$description);
1176 faslog($sds,"$user sent notification to $suser");
1177 http_header('200 OK');
1178 print html_header($head);
1179 pq(qq(
1180 'notfication email has been sent to $suser'
1181 '<p>'
1182 '<a href="$back">back</a>'
1183 '</body></html>'
1184 ));
1185 exit;
1186 }
1187
1188 if ($remove =~ /^user:([^\s\/]+):([^\s\/]+)/) {
1189 my $share = $1;
1190 my $suser = $2;
1191 my $sds = "$shared/$share";
1192 my $suserd = "$sds/users/$suser";
1193 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
1194 http_die("no access for $master to $owner/$share");
1195 }
1196 if ($id or readlink_("$suserd/access") ne 'manage') {
1197 rmrf($suserd);
1198 faslog($sds,"$user removed user $suser");
1199 }
1200 redirect("/$CGI?show=share:$share:users");
1201 }
1202
1203 if ($remove =~ /^archive:([^\s\/]+?):([^\s\/]+)/) {
1204 my $share = $1;
1205 my $archive = $2;
1206 my $sds = "$shared/$share";
1207 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
1208 http_die("no access for $master to $owner/$share");
1209 }
1210 if ($archive =~ s/(.+):($vp\.($atype))$/$1\/$2/ or $archive =~ s/!$//) {
1211 rmrf("$sds/archives/$archive");
1212 } else {
1213 my @n;
1214 my $nv = scalar(@n=glob("$sds/archives/$archive/*/data"));
1215 if ($nv) {
1216 http_header('200 OK');
1217 print html_header($head);
1218 $nv = "$nv archive" . ($nv == 1 ? '' : 's');
1219 pq(qq(
1220 '<h3>Archive set "$share:$archive" contains $nv.</h3>'
1221 '<p>'
1222 '[<a href="/$CGI?remove=archive:$share:$archive!">really remove</a>]'
1223 '[<a href="$back">keep</a>]'
1224 '</body></html>'
1225 ));
1226 exit;
1227 } else {
1228 rmrf("$sds/archives/$archive");
1229 }
1230 }
1231 if ($archive =~ m:(.+)/(.+):) {
1232 faslog($sds,"$user removed archive $1:$2");
1233 redirect("/$CGI?show=archive:$share:$1");
1234 } else {
1235 faslog($sds,"$user removed archive $archive");
1236 redirect("/$CGI?show=share:$share:archives");
1237 }
1238 }
1239
1240 if ($share and $narchive) {
1241 my $sds = "$shared/$share";
1242 $narchive =~ s/_?$vp//;
1243 $narchive =~ s/\.($atype)$//;
1244 mkdirp("$sds/archives/$narchive");
1245 faslog($sds,"$user created archive $narchive");
1246 redirect("/$CGI?show=share:$share:archives");
1247 }
1248
1249 if ($share and $nuser) {
1250 my $sds = "$shared/$share";
1251 $nuser .= '@'.$mdomain if $nuser !~ /@/ and $mdomain;
1252 unless (checkaddress($nuser) or $nuser =~ /\.test$/) {
1253 http_die("illegal user $nuser");
1254 }
1255 my $dir = "$sds/users/$nuser";
1256 if (-d $dir) {
1257 http_die("user $nuser already exists in share $share");
1258 }
1259 mkdirp($dir);
1260 my $pkey = randstring(32);
1261 open $pkey,'>',"$dir/pkey" or http_die("cannot create $dir/pkey - $!\n");
1262 print {$pkey} "$pkey\n";
1263 close $pkey;
1264 symlink 'read',"$dir/access";
1265 if ($nuser =~ /^anonymous/) {
1266 symlink 'never',"$dir/notification";
1267 } else {
1268 symlink 'no',"$dir/notification";
1269 }
1270 faslog($sds,"$user created user $nuser");
1271 redirect("/$CGI?show=share:$share:users");
1272 }
1273
1274 # default action for master user is show share archives
1275
1276 if ($master and $share =~ /(.+)/ or $show =~ /^share:(.+):archives/) {
1277 my $share = $1;
1278 my $sds = "$shared/$share";
1279 my $ssu = "show=share:$share:users";
1280 unless (-d $sds) {
1281 http_die("no such share $owner/$share");
1282 }
1283 if ($master and readlink_("$sds/users/$master/access") ne 'manage') {
1284 http_die("no access for $master to $owner/$share");
1285 }
1286 if ($share ne '_') {
1287 my $uo = "$sds/users/$owner";
1288 unless (-d $uo) {
1289 mkdirp($uo);
1290 mksymlink("$uo/notification",'yes');
1291 }
1292 }
1293 my $nyn = 'no';
1294 my $notification = 'yes';
1295 if (readlink_("$sds/users/$user/notification") eq 'yes') {
1296 $nyn = 'yes';
1297 $notification = 'no';
1298 }
1299 $notification = "notification=$share:$user:$notification";
1300 $notification = qq'<a href="/$CGI?$notification">notification</a>';
1301 my $sd = "$sds/description";
1302 if (defined $PARAM{description}) {
1303 open my $sd,'>',$sd or http_die("cannot write $sd - $!\n");
1304 $description =~ s/[\s\n]*$//;
1305 print {$sd} $description,"\n";
1306 close $sd;
1307 } else {
1308 chomp($description = slurp($sd)||'');
1309 }
1310 $description = decode_utf8($description);
1311 my $hdescription = '';
1312 if ($description =~ /\S/) {
1313 $hdescription = unhtml($description);
1314 $hdescription =~ s/\n/<br>/g;
1315 $hdescription =~ s/<br><br>/<p>/g;
1316 $hdescription =~ s!(https?://\S+)!<a href="$1">$1</a>!g;
1317 $hdescription = "<em>$hdescription</em>";
1318 }
1319 http_header('200 OK');
1320 print html_header($head);
1321 my $td = 'td valign="top"';
1322 if ($master) {
1323 my $oli = '';
1324 if ($_ = readlink("$akeydir/$akey") and s/...// and $_ eq $owner) {
1325 $oli = "(logged in)";
1326 }
1327 pq(qq(
1328 '<table>'
1329 '<tr><$td><b>share:<td><b>$share</b><br>$hdescription</tr>'
1330 '<tr><$td><b>owner: <td><b>$owner</b> $oli</tr>'
1331 '<tr><$td><b>user: <td><b>$user</b> (logged in)</tr>'
1332 '<tr><$td><b>access: <td><b>manage</tr>'
1333 '<tr><$td><b>$notification:<td><b>$nyn</tr>'
1334 '</table>'
1335 '<p>'
1336 '[<a href="/$CGI?$ssu">show users</a>]'
1337 # '[<a href="/$ENV{SCRIPT_NAME}?owner=$user">show own shares</a>]'
1338 '[<a href="/$CGI?show=shares">show shares</a>]'
1339 '[<a href="/$CGI?clone=share:$share">clone share</a>]'
1340 '<p>'
1341 ));
1342 } else {
1343 my $mv = readlink("$sds/versions")||'unlimited';
1344 my $sv = '';
1345 $sv .= qq' [<a href="/$CGI?$ssu">show users</a>]' if $share ne '_';
1346 $sv .= qq' [<a href="/$CGI?show=shares">show shares</a>]';
1347 $sv .= qq' [<a href="/$CGI?show=log:$share">view log</a>]' if -f "$sds/log";
1348 pq(qq(
1349 '<table>'
1350 '<tr><td><b>owner:<td><b>$owner</b> (logged in)</tr>'
1351 ));
1352 if ($share eq '_') {
1353 $description = '<em>Your personal share</em>';
1354 pq(qq'<tr><td><b>share:<td><b>_</b><br>$description</tr>');
1355 } elsif ($show =~ s/:edit$//) {
1356 pq(qq(
1357 '<tr><$td><b>share:<td><b>$share</b><br>'
1358 '<form action="/$CGI" $formpara>'
1359 '<input type="hidden" name="show" value="$show">'
1360 '<textarea name="description" cols="80" rows="5">'
1361 '$description'
1362 '</textarea><br>'
1363 '<input type="submit" value="save">'
1364 '</form></tr>'
1365 ));
1366 } else {
1367 pq(qq(
1368 '<tr><$td><b>share:'
1369 '<td><b>$share</b>'
1370 '[<a href="/$CGI?show=$show:edit">edit description</a>]<br>'
1371 '$hdescription'
1372 '</tr>'
1373 ));
1374 }
1375 pq(qq'<tr><td><b>$notification:<td><b>$nyn</tr>') if $share ne '_';
1376 my $mvt = 'title="max versions per user and per archive"';
1377 pq(qq(
1378 '<tr><td><b>max versions:</b><br>(per uploader)<td $mvt>'
1379 '<form action="/$CGI" $formpara>'
1380 '$mv [new value: <input type="text" name="maxversions" size="3" $mvt>'
1381 '<input type="submit" value="set">'
1382 '<input type="hidden" name="share" value="$share">'
1383 '(will be active after next upload)]'
1384 '</form>'
1385 '</tr>'
1386 '</table>'
1387 '<p>'
1388 '$sv<br>'
1389 ));
1390 }
1391 pq(qq(
1392 '<p>'
1393 '<table border="1">'
1394 '<tr><th>archive set<th>versions<th>size<th>description<th>action</tr>'
1395 ));
1396 my @archives = _sort(grep { -d } glob("$sds/archives/*"));
1397 foreach my $archive (@archives) {
1398 my @n;
1399 my $description = unhtml(decode_utf8(slurp("$archive/description")||''));
1400 my $mv = readlink("$archive/versions")||
1401 readlink("$sds/versions")||
1402 'unlimited';
1403 my $nv = 0;
1404 my $size = 0;
1405 foreach my $a (glob("$archive/*/data")) {
1406 $nv++;
1407 $size += -s $a;
1408 }
1409 $size = int($size/$MB)||'<1' if $size;
1410 $archive = basename($archive);
1411 $description =~ s!(https?://\S+)!<a href="$1">$1</a>!g;
1412 pq(qq(
1413 '<tr>'
1414 '<td $alignl><a href="/$CGI?show=archive:$share:$archive">$archive</a>'
1415 '<td $alignr>$nv'
1416 '<td $alignr>$size&nbsp;MB'
1417 '<td>$description'
1418 '<td>[<a href="/$CGI?remove=archive:$share:$archive">remove</a>]'
1419 ' [<a href="/$CGI?clone=archive:$share:$archive">copy</a>]'
1420 ' [<a href="/$CGI?rename=archive:$share:$archive">rename</a>]'
1421 # ' [<a href="/$CGI?show=archive:$share:$archive:edit">edit description</a>]'
1422 '</tr>'
1423 ));
1424 }
1425 pq(qq(
1426 '</table>'
1427 '<form action="/$CGI" $formpara>'
1428 'new archive set: <input type="text" name="narchive" size="32">'
1429 '<input type="submit" value="create">'
1430 '<input type="hidden" name="share" value="$share">'
1431 '</form>'
1432 ));
1433 my $upload = "from=$user?to=$owner:share=$share?submit=share";
1434 if ($id) {
1435 pq(qq'[<a href="/fup?$upload">upload file</a>]');
1436 } else {
1437 pq(qq'[<a href="/fup?$upload?pkey=$pkey">upload file</a>]');
1438 }
1439 pq(qq'<p>');
1440 print $logout if $id;
1441 print $help if $master;
1442 pq(qq'</body></html>');
1443 exit;
1444 }
1445
1446 }
1447
1448 if ($id) {
1449
1450 # share index
1451 http_header('200 OK');
1452 print html_header($head);
1453 pq(qq(
1454 '<h3>shares of owner $user:</h3>'
1455 '<table border="1">'
1456 '<tr><th>share<th>archive sets<th>users<th>size<th>action</tr>'
1457 ));
1458 my @shares = ();
1459 foreach my $share (_sort(glob("$shared/*"))) {
1460 push @shares,untaint(basename($share)) if -d $share;
1461 }
1462 foreach my $share (@shares) {
1463 my $sds = "$shared/$share";
1464 my @n;
1465 my $na = scalar(@n=glob("$sds/archives/*"));
1466 my $nu = scalar(@n=glob("$sds/users/*/pkey"));
1467 my $size = 0;
1468 map { $size += -s } glob("$sds/archives/*/*/data");
1469 $size = int($size/$MB)||'<1' if $size;
1470 pq(qq(
1471 '<tr>'
1472 '<td $alignc><a href="/$CGI?show=share:$share:archives">$share</a>'
1473 '<td $alignr>$na'
1474 '<td $alignr>$nu'
1475 '<td $alignr>$size&nbsp;MB'
1476 ));
1477 if ($share eq '_') {
1478 pq(qq'<td>');
1479 } else {
1480 pq(qq(
1481 '<td>[<a href="/$CGI?remove=share:$share">remove</a>]'
1482 ' [<a href="/$CGI?clone=share:$share">clone</a>]'
1483 ));
1484 }
1485 pq(qq'</tr>');
1486 }
1487 pq(qq(
1488 '</table>'
1489 '<p>'
1490 '<form action="/$CGI" $formpara>'
1491 ' new share:'
1492 ' <input type="text" name="nshare" size="16">'
1493 ' <input type="submit" value="create">'
1494 '</form>'
1495 '<p>'
1496 ));
1497 othershares($user);
1498 exit;
1499 }
1500
1501 # share user (but not master!)
1502 my $userd = "$shared/$share/users/$user";
1503 http_die("internal error: master=$master") if $master;
1504 http_die("no such share $owner/$share/$user") unless -d $userd;
1505
1506 # show archive versions
1507 if ($show =~ /^archive:(.+)/) {
1508 my $archive = $1;
1509 my $archived = "$shared/$share/archives/$archive";
1510 my $access = readlink("$userd/access")||'read';
1511 my $description = unhtml(decode_utf8(slurp("$archived/description")||''));
1512 $description =~ s!(https?://\S+)!<a href="$1">$1</a>!g;
1513 unless (-d $archived) {
1514 http_die("no such archive $owner/$share/$archive");
1515 }
1516 my $key = md5_hex("$archive:$pkey");
1517 my $afop = qq'<a href="/fop/$owner/$share/$user/$key/$archive">'.
1518 qq'$archive</a>';
1519 http_header('200 OK');
1520 print html_header($head);
1521 my $td = 'td valign="top"';
1522 pq(qq(
1523 '<table>'
1524 '<tr><$td><b>share: <td><b>$share</tr>'
1525 '<tr><$td><b>owner: <td><b>$owner</tr>'
1526 '<tr><$td><b>user: <td><b>$user</b> (logged in)</tr>'
1527 '<tr><$td><b>archive:<td><b>$afop</b><br><em>$description</em></tr>'
1528 '<tr><$td><b>access: <td><b>$access</tr>'
1529 '</table>'
1530 '<p>'
1531 '[<a href="$CGI?show=share:$share:archives">show share</a>]'
1532 '<p>'
1533 '<table border="1">'
1534 '<tr><th>version<th>container<th>size<th>uploader<th>content<th>comment<th>action</tr>'
1535 ));
1536 my @archives = glob("$archived/*");
1537 foreach my $archive (reverse @archives) {
1538 my $size = -s "$archive/data" or next;
1539 my $content = content($archive);
1540 $size = int($size/$MB)||'<1' if $size;
1541 $archive =~ m:.*/archives/(.+)/($vp)\.($atype): or next;
1542 my $avt = $1.'_'.$2.'.'.$3;
1543 my $version = $2;
1544 my $container = $3;
1545 my $modify = "modify=comment:$share:$1:$2.$3";
1546 my $uploader = readlink("$archive/uploader")||$owner;
1547 my $comment = unhtml(readline1("$archive/comment")||'');
1548 my $url = "/fop/$owner/$share/$user/".md5_hex("$avt:$pkey")."/$avt";
1549 my $action = '';
1550 if ($user eq $uploader) {
1551 $action = qq'[<a href="/$CGI?$modify">edit&nbsp;comment</a>]';
1552 }
1553 pq(qq(
1554 '<tr>'
1555 '<td><a href="$url">$version</a>'
1556 '<td $alignc>$container'
1557 '<td $alignr>$size&nbsp;MB'
1558 '<td><nobr>$uploader</nobr>'
1559 '<td>$content'
1560 '<td>$comment'
1561 '<td>$action'
1562 '</tr>'
1563 ));
1564 }
1565 pq(qq(
1566 '</table>'
1567 '<p>'
1568 ));
1569 # $access = '';
1570 if ($access eq 'write' or $access eq 'add') {
1571 my $fup = '/fup'.
1572 "?from=$user".
1573 "?to=$owner:share=$share".
1574 "?archive=$archive".
1575 "?submit=share".
1576 "?pkey=$pkey";
1577 pq(qq(
1578 '[<a href="$fup">upload file</a>]'
1579 '<p>'
1580 ));
1581 }
1582 pq(qq(
1583 '</body></html>'
1584 ));
1585 exit;
1586 }
1587
1588 if ($notification =~ /^(yes|no)$/) {
1589 my $notification = $1;
1590 mksymlink("$userd/notification",$notification);
1591 faslog("$shared/$share","$user set notification=$notification for $user");
1592 redirect("/$CGI");
1593 }
1594
1595 # share archive index for user
1596 http_header('200 OK');
1597 print html_header($head);
1598 $access = readlink("$userd/access")||'read';
1599 $description = unhtml(decode_utf8(slurp("$shared/$share/description")||''));
1600 $description =~ s/\n*$//;
1601 $description =~ s/\n/<br>/g;
1602 $description =~ s/<br><br>/<p>/g;
1603 $description =~ s!(https?://\S+)!<a href="$1">$1</a>!g;
1604 $description = "<em>$description</em>";
1605 $notification = readlink("$userd/notification")||'no';
1606 if ($notification eq 'never') {
1607 $notification =
1608 qq'notification:<td><b>no';
1609 } elsif ($notification eq 'no') {
1610 $notification =
1611 qq'<a href="/$CGI?notification=yes">notification:</a><td><b>no';
1612 } elsif ($notification eq 'yes') {
1613 $notification =
1614 qq'<a href="/$CGI?notification=no">notification:</a><td><b>yes';
1615 }
1616
1617 my $td = 'td valign="top"';
1618 my $showshares = qq'[<a href="/$CGI?show=shares">show shares</a>]';
1619 $showshares = '' if $user =~ /^anonymous/;
1620 pq(qq(
1621 '<table>'
1622 '<tr><$td><b>share: <td><b>$share</b><br>$description</tr>'
1623 '<tr><$td><b>owner: <td><b>$owner</tr>'
1624 '<tr><$td><b>user: <td><b>$user</b> (logged in)</tr>'
1625 '<tr><$td><b>access:<td><b>$access</tr>'
1626 '<tr><$td><b>$notification</tr>'
1627 '</table>'
1628 '<p>'
1629 '$showshares'
1630 '<p>'
1631 '<table border="1">'
1632 '<tr><th>archive set<th>versions<th>size<th>description<th>action</tr>'
1633 ));
1634 my @archives = _sort(grep { -d } glob("$shared/$share/archives/*"));
1635 foreach my $archive (@archives) {
1636 my $nv = 0;
1637 my $size = 0;
1638 my $fop = '';
1639 my $description = unhtml(decode_utf8(slurp("$archive/description")||''));
1640 foreach my $a (glob("$archive/*/data")) {
1641 $nv++;
1642 $size += -s $a;
1643 if ($a =~ m:.*/archives/(.+)/($vp)\.($atype):) {
1644 my $avt = $1.'_'.$2.'.'.$3;
1645 $fop = "/fop/$owner/$share/$user/".md5_hex("$avt:$pkey")."/$avt";
1646 }
1647 }
1648 $size = int($size/$MB)||'<1' if $size;
1649 $archive = basename($archive);
1650 pq(qq'<tr>');
1651 if (0 and $access eq 'read') {
1652 pq(qq'<td $alignl><a href="$fop">$archive</a>');
1653 } else {
1654 pq(qq'<td $alignl><a href="/$CGI?show=archive:$archive">$archive</a>');
1655 }
1656 pq(qq(
1657 '<td $alignr>$nv'
1658 '<td $alignr>$size&nbsp;MB'
1659 '<td>$description'
1660 ));
1661 if (-f "$user/@") {
1662 pq(qq'<td><a href="/$CGI?clone=archive:$share:$archive">copy</a>');
1663 } else {
1664 pq(qq'<td>');
1665 }
1666 pq(qq'</tr>');
1667 }
1668 pq(qq'</table>');
1669 if ($access eq 'add') {
1670 my $fup = '/fup'.
1671 "?from=$user".
1672 "?to=$owner:share=$share".
1673 "?submit=share".
1674 "?pkey=$pkey";
1675 pq(qq(
1676 '<p>'
1677 '<a href="$fup">upload file</a>'
1678 ));
1679 }
1680 pq(qq'$help') if $access ne 'read';
1681 pq(qq'</body></html>');
1682 exit;
1683
1684
1685 sub norm {
1686 local $_ = shift;
1687 s/^\s+//;
1688 s/\s+$//;
1689 s/[;,<>(){}\[\]\s\|\/\'\`\"\000-\037\\]/_/g;
1690 s/^\./_/;
1691 s/:\./:_/g;
1692 return untaint($_);
1693 }
1694
1695
1696 sub anorm {
1697 local $_ = shift;
1698 s/^\s+//;
1699 s/\s+$//;
1700 s/^\./_/;
1701 s/[^\w.+-]/_/g;
1702 return untaint($_);
1703 }
1704
1705
1706 sub fas_notify {
1707 my ($to,$user,$url,$description) = @_;
1708 my ($duser,$dto,$body);
1709 my ($mfrom,$hfrom) = mhfrom($user);
1710 my $header =
1711 'From: <$mfrom> ($hfrom via F*EX service $hostname)\n'.
1712 "To: <$to>\n".
1713 "Cc: <$user>\n".
1714 "Subject: F*EX archive sharing\n".
1715 "X-FEX-Client-Address: $RA\n".
1716 "X-FEX-Fexmaster: $ENV{SERVER_ADMIN}\n".
1717 "X-Mailer: F*EX\n";
1718
1719 # explicite sender set in fex.ph?
1720 if ($sender_from) {
1721 # obsoleted feature!
1722 map { s/^From: <\Q$user/From: <$sender_from/ } $header;
1723 open $sendmail,'|-',$sendmail,$to,$user,$bcc
1724 or http_die("cannot start sendmail - $!");
1725 } else {
1726 # for special remote domains do not use same domain in From,
1727 # because remote MTA will probably reject this email
1728 $duser = $1 if $user =~ /@(.+)/;
1729 $dto = $1 if $to =~ /@(.+)/;
1730 if ($duser and $dto and @remote_domains and
1731 grep {
1732 $duser =~ /(^|\.)$_$/ and $dto =~ /(^|\.)$_$/
1733 } @remote_domains)
1734 {
1735 $header =~ s/(From: <)\Q$user\E(.*?)\n/$1$admin$2\nReply-To: $user\n/;
1736 open $sendmail,'|-',$sendmail,$to,$user,$bcc
1737 or http_die("cannot start sendmail - $!");
1738 } else {
1739 open $sendmail,'|-',$sendmail,'-f',$mfrom,$to,$user,$bcc
1740 or http_die("cannot start sendmail - $!");
1741 }
1742 }
1743 my $disclaimer = "Questions? ==> F*EX admin: $admin\n";
1744 $disclaimer .= "\n".$::disclaimer if $::disclaimer;
1745 my $info = $url;
1746 $info =~ s:/fas/.*:/sharing/:;
1747 $description =~ s/\n*$//;
1748 $body = qqq(qq(
1749 '$user has set up F*EX archive sharing for you:'
1750 ''
1751 '$url'
1752 ''
1753 '$description'
1754 ''
1755 'See $info'
1756 'for more information about F*EX archive sharing.'
1757 ''
1758 '$disclaimer'
1759 ));
1760 # For a Windows F*EX client see http://fex.belwue.de/fexit.html
1761 print {$sendmail} $header,"\n",$body;
1762 close $sendmail and return $to;
1763 http_die("cannot send notification email (sendmail error $!)");
1764 }
1765
1766
1767 sub redirect {
1768 my $uri = shift;
1769
1770 nvt_print(
1771 "HTTP/1.1 302 Found",
1772 "Location: $uri",
1773 'Content-Length: 0',
1774 ''
1775 );
1776 # control back to fexsrv for further HTTP handling
1777 &reexec;
1778 }
1779
1780
1781 sub othershares {
1782 my $user = shift;
1783 my ($oowner,$share);
1784 my @shares = ();
1785
1786 foreach (asort(glob "*/SHARE/*/users/$user/pkey")) {
1787 ($oowner,undef,$share) = split '/';
1788 push @shares,"$oowner $share";
1789 }
1790 if (@shares) {
1791 pq(qq(
1792 '<h3>shares from other owners:</h3>'
1793 '<table border="1">'
1794 '<tr><th>owner<th>share<th>archive sets<th>size</tr>'
1795 ));
1796 foreach (@shares) {
1797 ($oowner,$share) = split;
1798 my $oos = "$oowner/SHARE/$share";
1799 my $pkey = readline1("$oos/users/$user/pkey");
1800 my @n;
1801 my $na = scalar(@n=glob("$oos/archives/*"));
1802 my $size = 0;
1803 map { $size += -s } glob("$oos/archives/*/*/data");
1804 $size = int($size/$MB)||'<1' if $size;
1805 pq(qq(
1806 '<tr>'
1807 '<td>$oowner'
1808 '<td $alignc><a href="/$CGI/$oowner/$share/$user/$pkey">$share</a>'
1809 '<td $alignr>$na'
1810 '<td $alignr>$size&nbsp;MB'
1811 '</tr>'
1812 ));
1813 }
1814 pq(qq'</table>');
1815 }
1816 if ($akey) {
1817 pq(qq(
1818 '<p>'
1819 '[<a href="/foc">F*EX operation control</a>]'
1820 '<p>'
1821 '$logout'
1822 ));
1823 }
1824 pq(qq(
1825 '$help'
1826 '</body></html>'
1827 ));
1828 }
1829
1830
1831 sub query_pkey {
1832 my ($owner,$share,$user) = @_;
1833
1834 http_header('200 OK');
1835 print html_header($head);
1836 pq(qq(
1837 '<form action="/$CGI?owner=$owner?share=$share?user=$user" $formpara>'
1838 '<table>'
1839 '<tr><td><b>share: <td>$share</tr>'
1840 '<tr><td><b>owner: <td>$owner</tr>'
1841 '<tr><td><b>user: <td>$user</tr>'
1842 '<tr><td><b>passkey:<td><input type="password" name="pkey" size="8"></tr>'
1843 '</table>'
1844 '<input type="submit" value="login">'
1845 '</form>'
1846 '</body></html>'
1847 ));
1848 exit;
1849 }
1850
1851
1852 sub content {
1853 my $archive = shift;
1854 my $alist = "$archive/alist";
1855 my $content = '';
1856 if (open $alist,$alist) {
1857 while (<$alist>) {
1858 if ($archive =~ /7z$/ and / \d\d:\d\d:\d\d\s+\d+\s+(.+)/
1859 or / \d\d:\d\d\s+(.+)/)
1860 {
1861 if (length($content)) {
1862 $content .= ' ...';
1863 last;
1864 } else {
1865 $content = unhtml($1);
1866 }
1867 }
1868 }
1869 close $alist;
1870 }
1871 return $content;
1872 }
1873
1874
1875 sub _sort {
1876 my @a = asort(@_);
1877 my @b = ();
1878 foreach (@a) {
1879 push @b,$_ if m:.*/(.): and $1 eq '_';
1880 }
1881 foreach (@a) {
1882 push @b,$_ if m:.*/(.): and $1 ne '_';
1883 }
1884 return @b;
1885 }
1886
1887
1888 sub esystem {
1889 my $cmd = shift;
1890 local $_ = `$cmd 2>&1`;
1891 if (/\w/) {
1892 $cmd =~ s/</&lt;/g;
1893 s/</&lt;/g;
1894 http_header('200 OK');
1895 print html_header($head);
1896 pq(qq(
1897 '<h3>ERROR</h3>'
1898 '<pre>'
1899 '<b>$cmd</b>'
1900 '$_'
1901 '</pre>'
1902 '</body></html>'
1903 ));
1904 exit;
1905 }
1906 }
1907
1908 sub unhtml {
1909 local $_ = shift;
1910 s/^\s+//;
1911 s/[\s\n]+$//;
1912 s/ +/ /g;
1913 return htmlquote($_);
1914 }