Debian Codegraph @

htdocs/download/fexsync

raw
1 #!/usr/bin/perl -w
2
3 # client for directory syncing of the FEX service
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7 # Perl Artistic Licence
8
9 # fexsync sends or receives new files of a specified directory via fexserver.
10 #
11 # Sending and receiving works synchronously.
12 # For sending you need a FEXID, receiving is "free".
13 #
14 # ---------------------------------------------------------------------------
15 #
16 # Send usage:
17 #
18 # fexsync [-v] [-i ID] [-m MAX] [-X 'EXCLUDE'] [-z] [-c] [-C TKEY] [-w SECONDS] DIRECTORY
19 #
20 # Option -v is for verbose mode.
21 #
22 # Option -i selects account ID from $HOME/.fex/id
23 #
24 # Option -m limits the throuput to MAX kB/s.
25 #
26 # Option -X excludes files.
27 #
28 # Option -z forces compression.
29 #
30 # Option -c is for continuous syncing (needs sexsend).
31 #
32 # Option -C is like -c but you can name the transfer key.
33 #
34 # Option -w waits SECONDS before next syncing.
35 #
36 # After initiating the transfer fexsync shows you
37 # TNAME TKEY FEXSERVER FEXUSER
38 # which you need as arguments for receiving.
39 #
40 # TNAME is the transfer name of the directory you want to sync.
41 # TKEY is a unique secret number (or a string when using option -c).
42 # FEXSERVER and FEXUSER are optional, if the receiving account has your FEXID.
43 # EXCLUDE is a Perl regexp matching full file name (no path allowed).
44 # TKEY is the secret transfer key for continuous syncing.
45 #
46 # Limitations:
47 # fexsync always excludes directories named .del .snapshot .snapshots
48 # fexsync breaks hard links when updating.
49 #
50 #
51 # ---------------------------------------------------------------------------
52 #
53 # Receive usage:
54 #
55 # fexsync [-v] [-m MAX] [-d] TNAME TKEY [FEXSERVER FEXUSER]
56 #
57 # Option -v is for verbose mode.
58 #
59 # Option -m limits the throuput to MAX kB/s.
60 #
61 # Option -d deletes extraneous files.
62 #
63 # TNAME is the transfer name of the directory you want to sync.
64 # TKEY is the secret transfer key.
65 #
66 # ---------------------------------------------------------------------------
67 #
68 # Easy send&receive usage:
69 #
70 # fexsync . # send directory
71 # fexsync : # receive directory
72 #
73 # On both sides you need the same FEXID and you have to be in a directory with
74 # the same name (pwd).
75 #
76 # ---------------------------------------------------------------------------
77 #
78 # You can send&install fexsync to the recieving account with:
79 #
80 # fexsend $(which fexsync) .
81 # wget <Location>
82 # chmod 755 fexsync
83 # alias fexsync="$(pwd)/fexsync"
84 #
85 # To synchronize a directory bidirectionally (A <-> B), simply call
86 # "fexsync DIRECTORY" first on host A, then on host B.
87 # Hint: you may use "." for DIRECTORY.
88 #
89 # If you have no FEXID on host B, then call "fexsend -I" on host A and copy
90 # the export line to host B.
91 #
92 # If you have the del utility installed (comes with the fextools), then
93 # existing files will be moved to a .del/ subdiretory before they will be
94 # overwritten or removed.
95 #
96 # See also:
97 # xxx -h
98 # http://fex.rus.uni-stuttgart.de/usecases/xx.html#xxx
99 # http://fex.rus.uni-stuttgart.de/usecases/fexsync.html
100
101 # ---------------------------------------------------------------------------
102
103 # fexdox sends or receives new or modified files of a specified directory
104 # to or from an associated folder on the fexserver.
105 # This means, files will be synchronized.
106 #
107 # For sending you need a FEXID from the fexmaster.
108 # For receiving you need a FEXID or user/password from the folder owner.
109 # If a folder is open for anonymous read access, then you can use "anonymous"
110 # as user name.
111 #
112 # A folder on the fexserver has the same name as the local directory, but
113 # problematic characters will be substituted with "_".
114 # File names are transfered "as is" (binary), no character set translation
115 # will be made.
116 #
117 # You can exclude files from sending when you write their filename pattern into
118 # a local file named .fexdox/exclude
119 # The filename patterns are the same as in a POSIX shell with an additional
120 # pattern /**/ which stands for any (sub)directory.
121 # If you want to exclude a certain directory/file, prefix it with a /
122 # otherwise all matching files in any directory will be excluded.
123 # Examples:
124 #
125 # blubb.tmp # will exclude all files with that name in any directory
126 # *.tmp # will exclude all tmp-files in any directory
127 # /test/blubb.tmp # will exclude only this single file
128 #
129 # This exclude list is also respected by the -d delete option when you
130 # download (receive) a folder. This means: matching files will not be deleted.
131 #
132 # When you download a folder from the fexserver it may contain the DOX user
133 # upload directory FOLDER/.upload
134 # This directory is excluded by default for (re)uploading.
135 #
136 # Automatic compression transfer mode is default, see fexsend -H
137 #
138 # Send usage:
139 # fexdox -s [-v] [-n] [-m MAX] [-z] [-i ID] [-d] [-f FOLDER] DIRECTORY
140 # fexdox -s [-v] [-n] [-m MAX] [-z] [-i ID] [-f FOLDER] . FILES_OR_DIRECTORIES
141 # fexdox -s [-v] [-n] [-m MAX] [-z] DOXURL DIRECTORY <<<USER:PASSWORD
142 #
143 # Receive usage:
144 # fexdox -r [-v] [-m MAX] [-z] [-i ID] [-d] FOLDER
145 # fexdox -r [-v] [-m MAX] [-z] [-d] DOXURL [<<<USER:PASSWORD]
146 #
147 # Other usage:
148 # fexdox [-v] [-i ID] [-l [FOLDER]] [-D FOLDER] [-a FOLDER]
149 #
150 # options: -v verbose mode
151 # -n no file transfer (dry-run)
152 # -i use account ID from $HOME/.fex/id
153 # -s send DIRECTORY (upload)
154 # -f save to FOLDER
155 # -r receive FOLDER (download)
156 # -m limit throuput to MAX kB/s
157 # -z force compression
158 # -d delete extraneous files
159 # -l list dox folders or FOLDER content
160 # -D delete FOLDER
161 # -a assign FOLDER name to current directory
162 #
163 # see also: http://fex.belwue.de/DOX/
164 #
165 # For limitations see: fexsync -H
166
167 use Socket;
168 use Config;
169 use Getopt::Std;
170 use IO::Handle;
171 use IO::Socket::INET;
172 use File::Basename;
173 use Sys::Hostname;
174 use Cwd qw(abs_path getcwd);
175 use Digest::MD5 qw(md5_hex);
176
177 eval 'use Net::INET6Glue::INET_is_INET6';
178
179 our $version = 20200429;
180 our $DEBUG = $ENV{DEBUG};
181
182 $ENV{LC_ALL} = 'C';
183
184 our %SSL = (SSL_version => 'TLSv1_2');
185
186 my $sigpipe;
187 my $transfer = '';
188
189 unless ($version) {
190 my @d = localtime((stat $0)[9]);
191 $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
192 }
193
194 $useragent = $ENV{FUA} = "fexsync-$version";
195
196 if (-f ($_ = '/etc/fex/config.pl')) {
197 eval { require } or warn $@;
198 }
199
200 $| = 1;
201 $prg = $0;
202 $prg =~ s:(.*)/::;
203 $fexsync = "$1/fexsync";
204 $del = "$1/del";
205 $sexget = "$1/sexget";
206 $sexsend = "$1/sexsend";
207 @fexsend = ("$1/fexsend");
208 @fexget = ("$1/fexget");
209 @fexsync = ($fexsync);
210
211 $exclude = '\.del|\.snapshots?';
212 $afnc = 'a-zA-Z_\d\@~^.,+-';
213 $vp = '\d{8}_\d{6}';
214 $fexhome = $ENV{HOME}.'/.fex';
215
216 $wget = 'wget';
217 if ($Config{osname} =~ /^(mswin|cygwin)/i) {
218 $wget .= ' --no-check-certificate';
219 }
220
221 if ($prg =~ /dox/) {
222
223 $dox = $prg;
224 @CMD = ($0,@ARGV);
225 $useragent = $ENV{FUA} = "$prg-$version";
226
227 $usage = ljoin(
228 "$prg: send or receive new documents to or from fexserver folder",
229 "send usage: $prg -s [-d] [-f FOLDER] DIRECTORY",
230 "send usage: $prg -s [-f FOLDER] . FILES_OR_DIRECTORIES",
231 "send usage: $prg -s DOXURL DIRECTORY <<<USER:PASSWORD",
232 "receive usage: $prg -r [-d] FOLDER",
233 "receive usage: $prg -r [-d] DOXURL [<<<USER:PASSWORD]",
234 "other usage: $prg [-H] [-l [FOLDER]] [-D FOLDER] [-a FOLDER]",
235 "options: -s send DIRECTORY (upload)",
236 " -r receive FOLDER (download)",
237 " -d delete extraneous files",
238 " -f upload to FOLDER",
239 " -l list folders (uploaded directories) or FOLDER content",
240 " -D delete FOLDER",
241 " -a assign FOLDER name to current directory",
242 " -H more options and help",
243 );
244
245 $opt_h = $opt_H = $opt_d = $opt_D = $opt_z = $opt_s = $opt_r = $opt_l = 0;
246 $opt_n = $opt_q = 0;
247 $opt_m = $opt_i = $opt_X = $opt_a = $opt_f = '';
248
249 $_ = "$fexhome/config.pl"; require if -f;
250
251 getopts('hHvnqdDlzrsi:m:X:a:f:') or die $usage;
252 if ($opt_a) {
253 die $usage if $opt_s+$opt_r+$opt_l+$opt_D;
254 } else {
255 die $usage if $opt_s+$opt_r+$opt_l+$opt_D+$opt_h+$opt_H != 1;
256 }
257
258 } else {
259
260 $usage = ljoin(
261 "$prg: send or receive new files",
262 "send usage: $prg [-c] DIRECTORY",
263 "receive usage: $prg [-d] TNAME TKEY [FEXSERVER FEXUSER]",
264 "options: -c continuous syncing",
265 " -d delete extraneous files",
266 "send example: $prg videos",
267 "receive example: $prg videos 419027 fex.belwue.de:443 framstag",
268 "help: $prg -H",
269 );
270
271 $opt_h = $opt_H = $opt_v = $opt_d = $opt_z = $opt_c = $opt_q = 0;
272 $opt_w = 66;
273 $opt_m = $opt_i = $opt_C = $opt_X = '';
274
275 $_ = "$fexhome/config.pl"; require if -f;
276
277 getopts('hHvzcqdi:C:w:m:X:') or die $usage;
278
279 $opt_c ||= $opt_C;
280 }
281
282 if ($opt_m =~ /^\d+$/) {
283 push @fexsend,'-m',$opt_m;
284 push @fexget, '-m',$opt_m;
285 push @fexsync,'-m',$opt_m;
286 } elsif ($opt_m) {
287 die $usage;
288 }
289
290 push @fexsend,'-q' if $opt_q;
291
292 if ($opt_X) {
293 $exclude .= '|'.$opt_X;
294 }
295
296 if ($opt_h) {
297 print $usage;
298 exit;
299 }
300
301 if ($opt_H) {
302 open $fexsync,$fexsync or die "$fexsync: $!\n";
303 while (<$fexsync>) {
304 last if /$prg/;
305 }
306 s/^# ?//;
307 $help = $_;
308 while (<$fexsync>) {
309 if (s/^# ?//) {
310 $help .= $_;
311 } else {
312 last;
313 }
314 }
315 print $help;
316 exit;
317 }
318
319 if ($dox) {
320 if ($prg =~ /doxx/) {
321 $ENV{FEXID} = $ENV{FEXXX}||'' or $opt_i = 'xx';
322 }
323 &getfid;
324 unless ($id) {
325 die "$prg: no FEXID\n";
326 }
327 $ENV{FEXID} = "$fexcgi $user $id"; # for fexsend
328 }
329
330 if ($dox and $opt_l) {
331 if (@ARGV) {
332 my $folder = shift;
333 push @fexsend,'-~',"DOXLIST:$folder";
334 warn "\$ @fexsend|\n" if $opt_v;
335 open my $fexsend,'-|',@fexsend or exit $?;
336 while (<$fexsend>) {
337 if (/^(\d+) (.+)/) {
338 my @d = localtime($1);
339 printf "%d-%02d-%02d %02d:%02d:%02d %s\n",
340 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0],hexdecode($2);
341 } else {
342 print;
343 }
344 }
345 exit;
346 } else {
347 vexec(@fexsend,qw'-~ DOXLIST');
348 }
349 }
350
351 if ($dox and $opt_D) {
352 if (scalar(@ARGV) != 1) {
353 die "usage: $prg -d DIRECTORY\n";
354 }
355 $_ = "@ARGV";
356 s:/+$::;
357 s:.*/+::;
358 vexec(@fexsend,'-~',"DOXDEL:$_");
359 }
360
361 if ($dox and length($opt_a)) {
362 unless (-d '.fexdox') {
363 mkdir '.fexdox' or die "$prg: cannot mkdir .fexdox - $!\n";
364 }
365 my $ff = '.fexdox/folder';
366 unlink $ff;
367 $opt_a =~ s/[^$afnc]/_/g;
368 $opt_a =~ s/^\./_/;
369 warn "\$ ln -s $opt_a $ff\n" if $opt_v;
370 symlink $opt_a,$ff
371 or die "$prg: cannot symlink $ff - $!\n";
372 system qw"ls -l",$ff;
373 exit;
374 }
375
376 if (@ARGV) {
377 $_ = "@ARGV";
378 } else {
379 die $usage;
380 }
381
382 # fexdox upload
383 if ($dox and $opt_s) {
384
385 $mode = 'send';
386
387 unless (scalar(@ARGV) == 1 or scalar(@ARGV) > 1 and $ARGV[0] eq '.') {
388 die $usage;
389 }
390
391 # doxurl argument?
392 if (m!^(https?)://!) {
393
394 # as DOX user
395 my $doxurl = shift;
396
397 if (scalar(@ARGV) != 1) {
398 die "usage: $prg -s DOXURL DIRECTORY <<<USER:PASSWORD\n";
399 }
400
401 my $ldir = shift;
402 chdir $ldir or die "$prg: $ldir - $!\n";
403
404 &get_xp;
405
406 my $tdir;
407 if ($ldir eq '.') {
408 $tdir = $ldir;
409 } else {
410 $tdir = basename(getcwd('.'));
411 chdir '..';
412 }
413
414
415 if ($doxurl =~
416 m!^(https?)://([\w.-]+)(:(\d+))?(/dox\w*/([^\s/]+\@[^\s/]+)/(\S+))!)
417 {
418 $proto = $1;
419 $server = $2;
420 $port = $4||80;
421 my $uri = $5;
422 my $owner = $6;
423 my $dir = $7;
424 $port = 443 if $proto eq 'https';
425 # $doxurl = $_;
426
427 $uri =~ s:/*$:?ACTION=userlist:;
428
429 my $folder = $dir;
430 $folder =~ s:/.*::;
431
432 my $user = my $password = '';
433 my $okey;
434
435 if (-t STDIN) {
436 print "user: ";
437 $user = <STDIN>||'';
438 chomp $user;
439 exit unless length($user);
440 if ($user =~ /(\S+):(\S+)/) {
441 $user = $1;
442 $password = $2;
443 } else {
444 print "password: ";
445 $password = <STDIN>||'';
446 chomp $password;
447 exit unless length($password);
448 }
449 } else {
450 $auth = <STDIN>||'';
451 chomp $auth;
452 if ($auth =~ /(\S+):(\S+)/) {
453 $user = $1;
454 $password = $2;
455 } else {
456 die "usage: $prg -s DOXURL DIRECTORY <<<USER:PASSWORD\n";
457 }
458 }
459
460 $auth = encode_b64("$user:$password");
461
462 &serverconnect;
463 sendrequest("GET $uri","Authorization: Basic $auth");
464 &checkreply;
465
466 while (<$SH>) {
467 s/\r?\n//;
468 warn "<-- $_\n" if $opt_v;
469 last if /^$/;
470 }
471
472 my %sfiles = ();
473
474 while (<$SH>) {
475 s/\r?\n//;
476 warn "<-- $_\n" if $opt_v;
477 if (/^(\d+) (\S+)/) {
478 $sfiles{hexdecode($2)} = $1;
479 }
480 }
481
482 close $SH;
483
484 $uri =~ s/=userlist$/=okey/;
485
486 &serverconnect;
487 sendrequest("GET $uri","Authorization: Basic $auth");
488 &checkreply;
489
490 while (<$SH>) {
491 s/\r?\n//;
492 warn "<-- $_\n" if $opt_v;
493 $okey = $1 if /^okey=(\S+)/;
494 }
495 unless ($okey) {
496 die "$prg: no okey reply from $server\n";
497 }
498
499 my $transfer = sprintf 'fexdox_%s_%d',$folder,time;
500 my @files = ();
501 $t0 = time;
502 %files = ();
503 collect($tdir);
504 &unweed;
505 &exclude;
506 foreach (asort(keys %files)) {
507 if (-l or -f) {
508 next if m:(^|/)[.#]|~$:;
509 next if $files{$_} >= $t0;
510 next if $sfiles{$_} and $sfiles{$_} >= $files{$_};
511 push @files,$_;
512 }
513 }
514
515 my $flist = sprintf '%s/.fex/tmp/%s',$ENV{HOME},$transfer;
516 mkdirp(dirname($flist));
517 open $flist,'>',$flist or die "$prg: cannot write $flist - $!\n";
518 foreach (@files) {
519 print {$flist} $_,"\n";
520 }
521 close $flist;
522
523 my $fup = $doxurl;
524 $fup =~ s:/dox.*:/fup?to=$owner?okey=$okey:;
525 push @fexsend,'-k','1';
526 push @fexsend,'-C',"DOX:UPLOAD=$user";
527 push @fexsend,'-A',"$transfer.taz";
528 push @fexsend,"[$flist]",$fup;
529 my $status = 0;
530 if ($opt_n) {
531 warn "\$ @fexsend\n" if $opt_v;
532 if (open $flist,$flist) {
533 print while <$flist>;
534 close $flist;
535 }
536 } else {
537 vsystem(@fexsend);
538 $status = $?;
539 }
540 unlink $flist;
541 exit $status;
542
543 } else {
544 die "$prg: $_ is not a DOX URL\n";
545 }
546
547 } else {
548
549 # as DOX owner
550
551 $dir = shift;
552 $dir =~ s:/+$::;
553
554 if (scalar(@ARGV) > 1 ) {
555 if ($dir eq '.') {
556 if ($opt_d) {
557 die "usage: $prg -s [-m MAX] [-z] [-f FOLDER] . FILES...\n";
558 }
559 } else {
560 die "usage: $prg -s [-m MAX] [-z] [-f FOLDER] [-d] DIRECTORY\n".
561 "usage: $prg -s [-m MAX] [-z] [-f FOLDER] . FILES...\n";
562 }
563 }
564 unless (-d $dir) {
565 if ($dir eq basename(getcwd)) {
566 $dir = '.';
567 } else {
568 die "$prg: no such directory $dir\n";
569 }
570 }
571 chdir $dir or die "$prg: cannot cd $dir - $!\n";
572
573 &get_xp;
574
575 # do not upload DOX user upload directory back again
576 $_ = '/.upload';
577 $xp{$_} = shp2prx($_);
578
579 &getfid;
580 unless ($id) {
581 die "$prg: no FEXID found, use \"$fexsend -I\"\n";
582 }
583
584 if (length($opt_f)) {
585 $transfer = $opt_f;
586 } else {
587 $transfer = readlink('.fexdox/folder')||'';
588 }
589
590 unless (length($transfer)) {
591 $transfer = basename(getcwd);
592 $transfer =~ s/_$vp$//;
593 }
594 $transfer =~ s/[^$afnc]/_/g;
595 $transfer =~ s/^\./_/;
596
597 }
598
599 }
600
601 # fexdox download
602 elsif ($dox and $opt_r) {
603
604 if (m!^(https?)://!) {
605
606 if (m!^(https?)://([\w.-]+)(:(\d+))?(/dox\w*/[^\s/]+\@[^\s/]+/(\S+))!) {
607 $proto = $1;
608 $server = $2;
609 $port = $4||80;
610 $uri = $5;
611 $dir = $6;
612 $port = 443 if $proto eq 'https';
613 # $doxurl = $_;
614 $uri =~ s:/*$:?ACTION=syncit:;
615
616 if (-t STDIN) {
617 my $user = my $password = '';
618 print "user: ";
619 $user = <STDIN>;
620 exit unless defined $user;
621 chomp $user;
622 $user = 'anonymous' if $user eq '';
623 if ($user =~ s/:(\S+)//) {
624 $password = $1;
625 } elsif ($user ne 'anonymous') {
626 print "password: ";
627 $password = <STDIN>||'';
628 chomp $password;
629 }
630 } else {
631 $auth = <STDIN>||'';
632 chomp $auth;
633 if ($auth =~ /^anonymous(:.*)?$/) {
634 $user = 'anonymous';
635 } elsif ($auth =~ /(\S+):(\S+)/) {
636 $user = $1;
637 $password = $2;
638 } else {
639 die "usage: $prg -r DOXURL<<<USER:PASSWORD\n";
640 }
641 }
642
643 $auth = encode_b64("$user:$password") if length($password);
644 push @fexsync,'-v' if $opt_v;
645 push @fexsync,'-z' if $opt_z;
646 push @fexsync,'-d' if $opt_d;
647 push @fexsync,'-m',$opt_m if $opt_m;
648
649 &serverconnect;
650 if ($auth) {
651 sendrequest("GET $uri","Authorization: Basic $auth");
652 } else {
653 sendrequest("GET $uri");
654 }
655 &checkreply;
656
657 while (<$SH>) {
658 s/\r?\n//;
659 warn "<-- $_\n" if $opt_v;
660 if (/^\s*fexsync (\S+) (\d+) (\S+) (\S+)/) {
661 if ($opt_n) {
662 warn "\$ @fexsync $1 $2 $3 $4\n" if $opt_v;
663 exit;
664 } else {
665 vexec(@fexsync,$1,$2,$3,$4);
666 exit $?;
667 }
668 }
669 }
670 die "$prg: no fexsync reply from $server\n";
671 } else {
672 die "$prg: $_ is not a DOX URL\n";
673 }
674
675 } else {
676
677 $_ = basename(getcwd) if $_ eq '.';
678 s/[^$afnc]/_/g;
679 s/^\./_/;
680 $folder = $_;
681 $folder = $_;
682 $user = $id = '';
683 &getfid;
684 unless ($id) {
685 die "$prg: no FEXID found, use \"$fexsend -I\"\n";
686 }
687
688 push @fexsync,'-v' if $opt_v;
689 push @fexsync,'-d' if $opt_d;
690
691 push @fexsend,'-~',"DOXSYNC:$user:$folder".($opt_z ? ':COMPRESS' : '');
692 warn "\$ @fexsend\n" if $opt_v;
693 exit if $opt_n;
694
695 open my $fexsend,'-|',@fexsend or exit $?;
696 while (<$fexsend>) {
697 if (/^fexsync (\S+) (\d+) (\S+) (\S+)/) {
698 vexec(@fexsync,$1,$2,$3,$4);
699 }
700 }
701 die "$prg: failed: @fexsend\n";
702 }
703
704 exit;
705
706 }
707
708 # fexsync : # receive directory
709 elsif (/^:$/) {
710
711 $mode = 'receive:';
712
713 $sdir = $dir = abs_path('.');
714 die "$prg: $dir is not writable\n" unless -w $dir;
715
716 &getfid;
717 unless ($id) {
718 die "$prg: no FEXID found, use \"$fexsend -I\"\n";
719 }
720
721 $transfer = basename($dir);
722 $tkey = '000000';
723
724 } elsif (/ \d+\b/) {
725
726 # single receiving
727
728 $mode = 'receive';
729
730 ($transfer,$tkey,$fexcgi,$user) = split;
731
732 if ($transfer eq '.') {
733 $transfer = basename(getcwd);
734 }
735
736 &getfid unless $user;
737
738 $sdir = abs_path('.');
739
740 $_ = basename(getcwd);
741 s/\./\000/g;
742 s/[^\000$afnc]/./g;
743 s/\000/\\./g;
744 s/\+/\\+/g;
745 if ($transfer !~ /^$_$/) {
746 unless (-e $transfer) {
747 mkdir $transfer or die "$prg: cannot mkdir $transfer - $!\n";
748 }
749 chdir $transfer or die "$prg: cannot chdir $transfer - $!\n";
750 }
751
752 $dir = abs_path('.');
753 die "$prg: $dir is not writable\n" unless -w $dir;
754
755 } elsif (/ (\w+)/ and $1 =~ /[a-z]/i) {
756
757 # continuous receiving
758
759 $dir = abs_path('.');
760 die "$prg: $dir is not writable\n" unless -w $dir;
761
762 ($transfer,$stream,$fexcgi,$user) = split;
763 &getfid unless $user;
764 die "$prg: unknown fexuser\n" unless $user;
765 $fexcgi =~ s:/fup.*::;
766 my $sex = "$sexget -q $fexcgi/$user:public $stream|";
767 push @fexsync,'-d' if $opt_d;
768
769 for (my $n = 1; $n<99; $n++) {
770 warn "\$ $sex\n" if $opt_v;
771 $spid = open $sex,$sex or die "$prg: cannot start $sexget - $!\n";
772 my $t = <$sex>;
773 if (defined $t) {
774 chomp $t;
775 if ($t ne $transfer) {
776 die "$prg: wrong transfer $t offered in stream $stream\n";
777 }
778 $n = 0;
779
780 my $dir = basename(getcwd);
781 $dir =~ s/[^$afnc]/_/g;
782 if ($dir ne $transfer) {
783 unless (chdir $transfer) {
784 mkdir $transfer or die "$prg: cannot mkdir $transfer - $!\n";
785 chdir $transfer or die "$prg: cannot chdir $transfer - $!\n";
786 }
787 }
788 $dir = getcwd;
789 unless (-w $dir) {
790 die "$: no write permission in $dir\n";
791 }
792 print "syncing to $dir/\n";
793
794 while (<$sex>) {
795 chomp;
796 if (/^-$/) {
797 printf "\n[%s]\n",isodate(time);
798 print "end of stream\n";
799 exit;
800 }
801 printf "\n[%s]\n",isodate(time);
802 vsystem(@fexsync,$transfer,$_,$fexcgi,$user);
803 }
804 }
805 close $sex;
806 my $wait = int($opt_w*1.1);
807 warn "$prg: $fexcgi has closed the connection\n";
808 warn "$prg: retrying after $wait seconds\n";
809 sleepwait($wait);
810 }
811 die "$prg: giving up\n";
812
813 } else {
814
815 $mode = 'send';
816
817 # hidden feature for testing: DIRECTORY#TKEY
818 s/#(\d+)$// and $tkey = $1;
819
820 $dir = $_;
821
822 unless (-d $dir) {
823 if ($dir eq basename(getcwd)) {
824 $dir = '.';
825 } else {
826 die "$prg: no such directory $dir\n";
827 }
828 }
829
830 chdir $dir or die "$prg: $dir - $!\n";
831
832 $user = $id = '';
833 &getfid;
834 unless ($id) {
835 die "$prg: no FEXID found, use \"$fexsend -I\"\n";
836 }
837
838 $transfer = basename(getcwd);
839 $transfer =~ s/_$vp$//;
840 $transfer =~ s/[^$afnc]/_/g;
841
842 if ($opt_c) {
843 $stream = $opt_C||randstring(8);
844 &preparevars;
845 $dir = basename(getcwd) if $dir eq '.';
846
847 my $sex = "|$sexsend -q public $stream";
848
849 {
850 my @fexsync = @fexsync;
851 $fexsync[0] =~ s:.*/::;
852 push @fexsync,'-d';
853 push @fexsync,'-v' if $opt_v;
854 print "to start sync receiving use:\n";
855 print " @fexsync $transfer $stream $server:$port $user\n\n";
856 }
857
858 for (my $n = 1; $n<99; $n++) {
859 warn "$sex\n" if $opt_v;
860
861 local $SIG{INT} = 'IGNORE';
862 $spid = open $sex,$sex or die "$prg: cannot start $sexsend - $!\n";
863 autoflush $sex 1;
864 warn $transfer,"\n" if $opt_v;
865 print {$sex} $transfer,"\n";
866
867 local $SIG{INT} = sub {
868 $^W = 0;
869 print {$sex} "-\n";
870 close $sex;
871 print "\n";
872 exit;
873 };
874
875 push @fexsync,'-z' if $opt_z;
876
877 my %ofiles = ();
878 while ($sex) {
879 my $change = 0;
880 $t0 = time;
881 %files = ();
882 print "searching new files... " unless $opt_q;
883 collect('.');
884 print "\r \r" unless $opt_q;
885 # new or modified files?
886 foreach my $file (keys %files) {
887 next if $files{$file} >= $t0;
888 if ($files{$file} != ($ofiles{$file}||0)) {
889 $change = 1;
890 last;
891 }
892 }
893 # deleted files?
894 foreach my $file (keys %ofiles) {
895 unless ($files{$file}) {
896 $change = 1;
897 last;
898 }
899 }
900 if ($change) {
901 %ofiles = %files;
902 printf "[%s]\n",isodate(time);
903 warn "\$ @fexsync $dir\n" if $opt_v;
904 if (open $fexsync,"@fexsync $dir|") {
905 local $SIG{ALRM} = sub {
906 close $sex;
907 undef $sex;
908 warn "$prg: SEX timeout\n";
909 };
910 while (<$fexsync>) {
911 warn $_ if $opt_v;
912 if (/fexsync $transfer (\d+)/) {
913 alarm(3);
914 if (print {$sex} $1,"\n") {
915 $n = 0;
916 } else {
917 close $sex;
918 undef $sex;
919 }
920 alarm(0);
921 }
922 }
923 close $fexsync;
924 print "\n";
925 } else {
926 exit $?;
927 }
928 }
929 if ($sex) {
930 sleepwait($opt_w,'waiting for new files... ');
931 }
932 }
933 warn "$prg: $fexcgi has closed the connection\n";
934 warn "$prg: retrying after $opt_w seconds\n";
935 sleepwait($opt_w);
936 }
937
938 die "$prg: giving up\n";
939 }
940 }
941
942 $t0 = time;
943 %files = ();
944 print "scanning files...\n" unless $opt_q;
945
946 if ($dox and $opt_s and @ARGV) {
947 collect(@ARGV);
948 } else {
949 collect('.');
950 }
951
952 if ($dox and $opt_s) {
953 &unweed;
954 &exclude;
955 }
956
957 if ($DEBUG) {
958 print "local files:\n";
959 foreach $file (asort(keys %files)) {
960 printf "%s %s\n",isodate($files{$file}),$file;
961 }
962 }
963
964 &preparevars;
965
966
967 # fexsync : # receive directory
968 if ($mode =~ /^receive(:?)/) {
969 my $qr = $1;
970
971 &get_xp;
972 &exclude;
973
974 print "connecting server...\n" unless $opt_q;
975 &serverconnect;
976
977 if ($qr) {
978 $sid = &query_sid;
979 sendrequest("POST /ccc?user=$user&tkey=$tkey&transfer=$transfer&ID=$sid");
980 } else {
981 sendrequest("POST /ccc?user=$user&tkey=$tkey&transfer=$transfer");
982 }
983
984 # warn "--> (local files list)\n" if $opt_v and %files;
985 foreach $file (asort(keys %files)) {
986 printf {$SH} "%s %s\n",$files{$file},$file;
987 warn sprintf("--> %s %s\n",$files{$file},$file) if $opt_v;
988 }
989 printf {$SH} "\n";
990
991 print "waiting for download...\n";
992 &checkreply;
993 while (<$SH>) {
994 s/\r?\n//;
995 warn "<-- $_\n" if $opt_v;
996 last unless length;
997 $durl = $1 if /^Location: (http.+)/;
998 }
999 %sfiles = ();
1000 while (<$SH>) {
1001 if (/^(\d+) (.+)/) {
1002 warn "<-- $_" if $opt_v;
1003 # warn "<-- (remote files list)\n" if $opt_v and not %sfiles;
1004 $sfiles{$2} = $1;
1005 } else {
1006 last;
1007 }
1008 }
1009 if ($durl) {
1010 if (-x $fexget[0]) {
1011 vsystem("@fexget -oX $durl");
1012 } else {
1013 if ($opt_m) {
1014 vsystem("$wget -c --limit-rate=${opt_m}k $durl");
1015 } else {
1016 vsystem("$wget -c $durl");
1017 }
1018 }
1019 my $a = basename($durl);
1020 if (-s $a) {
1021 my $tar = 'tar';
1022 $tar .= ' -z' if $a =~ /\.tgz$/;
1023 if (-x $del) {
1024 open $tar,"($tar -Oxvf $a 3>&2 2>&1 1>&3) 2>/dev/null |";
1025 while (<$tar>) {
1026 chomp;
1027 vsystem($del,$_) if lstat;
1028 }
1029 close $tar;
1030 }
1031 if ($dir eq $sdir) {
1032 print "extracting:\n";
1033 } else {
1034 print "extracting in $dir :\n";
1035 }
1036 vsystem("$tar -xvf $a && rm $a");
1037 }
1038 if ($opt_d and %sfiles) {
1039 my $xd = abs_path('.');
1040 foreach $file (asort(keys %files)) {
1041 unless ($sfiles{$file}) {
1042 unless ($deleted) {
1043 if ($dir eq $sdir) {
1044 print "removing extraneous files:\n"
1045 } else {
1046 print "removing extraneous files in $xd :\n"
1047 }
1048 }
1049 $deleted++;
1050 $file = hexdecode($file);
1051 if (-x $del) {
1052 print "$file\n" unless $opt_v;
1053 vsystem($del,$file);
1054 } else {
1055 print "$file\n";
1056 unlink $file;
1057 }
1058 }
1059 }
1060 rmdirr('.');
1061 }
1062 } else {
1063 die "$prg: no download URL in server reply\n";
1064 }
1065 exit;
1066 }
1067
1068 if ($mode eq 'send') {
1069
1070 # $SIG{PIPE} = \&sigpipehandler;
1071
1072 die "$prg: no files in $dir\n" unless %files;
1073
1074 if ($dox) {
1075
1076 # fexdox first stage with SEX
1077 print "server/user: $fexcgi/$user\n" unless $opt_q;
1078
1079 # my @x = asort(keys(%files)); die "@x";
1080
1081 my $fexsend = "@fexsend -~ DOXLIST:$transfer";
1082 warn "\$ $fexsend|\n" if $opt_v;
1083 exit if $opt_n;
1084 if (open $fexsend,"$fexsend 2>/dev/null|") {
1085 while (<$fexsend>) {
1086 if (/^(\d+) (.+)/) {
1087 warn "<-- $_" if $opt_v;
1088 if ($files{$2} and $1 >= $files{$2}) {
1089 delete $files{$2};
1090 }
1091 }
1092 }
1093 }
1094
1095 if (my @files = keys %files) {
1096 my $tar;
1097 my $uf = 1;
1098 my $cf = 1;
1099 my $cfx = 'gif|jpg|png|avi|mp\d|flv|m4v|ogg|tar|t?gz|zip|7z|bz2|rar|iso';
1100 foreach (@files) {
1101 $_ = hexdecode($_);
1102 if (my $s = -s) {
1103 if (/\.($cfx)$/i) {
1104 $cf += $s;
1105 } else {
1106 $uf += $s;
1107 }
1108 }
1109 }
1110 if ($uf+$cf < 2**31 and $uf/$cf > 0.2) {
1111 $tar = 'tar -cvzf -';
1112 } else {
1113 $tar = 'tar -cvf -';
1114 }
1115 my $flist = sprintf '%s/.fex/tmp/%s',$ENV{HOME},$transfer;
1116 mkdirp(dirname($flist));
1117 open $flist,'>',$flist or die "$prg: cannot write $flist - $!\n";
1118 foreach (asort(@files)) {
1119 print {$flist} "$_\n";
1120 }
1121 close $flist;
1122 print "streaming files:\n" unless $opt_q;
1123 my $opts = '';
1124 $opts = '-g' if not $opt_q and -t STDOUT;
1125 $opts .= " -m $opt_m" if $opt_m;
1126 my $sync = "$tar -T '$flist' | $sexsend $opts DOX $transfer";
1127 if (open my $sync,"($sync)2>&1|") {
1128 while (<$sync>) {
1129 if (/^\S+: /) {
1130 warn $_;
1131 } else {
1132 if ($opt_q) {
1133 } else {
1134 if (/^\d.* kB\/s\s/) {
1135 s/\n/\r/;
1136 } else {
1137 s/$/ / if length($_) <20;
1138 }
1139 print;
1140 }
1141 }
1142 }
1143 close $sync;
1144 }
1145 }
1146
1147 $t0 = time;
1148 %files = ();
1149 if (@ARGV) {
1150 collect(@ARGV);
1151 } else {
1152 collect('.');
1153 }
1154 &unweed;
1155 &exclude;
1156
1157 }
1158
1159 print "connecting server...\n" unless $opt_q;
1160 &serverconnect;
1161
1162 $sid = &query_sid;
1163 $request = "POST /ccc?user=$user&ID=$sid";
1164 if ($dox) {
1165 $request .= "&dox=$transfer";
1166 } else {
1167 $request .= "&transfer=$transfer";
1168 $request .= "&tkey=$tkey" if $tkey; # hidden feature for testing
1169 }
1170 sendrequest($request);
1171 # warn "--> (local files list)\n" if $opt_v;
1172 foreach $file (asort(keys %files)) {
1173 printf {$SH} "%s %s\n",$files{$file},$file;
1174 warn sprintf("--> %s %s\n",$files{$file},$file) if $opt_v;
1175 }
1176 printf {$SH} "\n";
1177
1178 &checkreply;
1179
1180 while (<$SH>) {
1181 s/\r?\n//;
1182 warn "<-- $_\n" if $opt_v;
1183 last unless length;
1184 if (/^Location: (.+)#(\d+)$/) {
1185 $transfer = $1;
1186 $tkey = $2;
1187 }
1188 }
1189
1190 if ($tkey) {
1191 unless ($opt_q or $dox) {
1192 my $dir = basename(getcwd);
1193 print "to start sync receiving with FEXID use:\n";
1194 print " cd $dir\n";
1195 print " $prg :\n";
1196 print "to start sync receiving without FEXID use:\n";
1197 print " $prg $transfer $tkey $server:$port $user\n";
1198 }
1199 } else {
1200 die "$prg: no tkey in server response\n";
1201 }
1202
1203 &serverconnect;
1204 $sid = &query_sid;
1205 $request = "GET /ccc?user=$user&ID=$sid&transfer=$transfer&tkey=$tkey";
1206 $request =~ s/&transfer=/&dox=/ if $dox;
1207 sendrequest($request);
1208 &checkreply;
1209
1210 while (<$SH>) {
1211 s/\r?\n//;
1212 warn "<-- $_\n" if $opt_v;
1213 last unless length;
1214 }
1215
1216 # warn "<-- (remote files list)\n" if $opt_v;
1217 while (<$SH>) {
1218 if (/^(\d+) (.+)/) {
1219 warn "<-- $_" if $opt_v;
1220 $time = $1;
1221 $file = $2;
1222 if ($files{$file} and $time >= $files{$file}) {
1223 delete $files{$file};
1224 }
1225 } else {
1226 last;
1227 }
1228 }
1229 close $SH;
1230
1231 $tmpdir = $fexhome.'/tmp';
1232 mkdir $fexhome;
1233 mkdir $tmpdir;
1234 if ($dox) {
1235 $flist = sprintf '%s/fexdox_%s.list',$tmpdir,$transfer;
1236 my $cmd = sprintf '%s/fexdox_%s.sh', $tmpdir,$transfer;
1237 if (-f $flist and -x $cmd) {
1238 warn "$prg: found ancient $cmd\n";
1239 exit 1 if $opt_n;
1240 if (-t STDIN) {
1241 print "resume? ";
1242 $_ = <STDIN>||'';
1243 exit if /^n/i;
1244 }
1245 vsystem($cmd);
1246 exit $? if -f $cmd;
1247 warn "$prg: resyncing...\n";
1248 vexec(@CMD);
1249 }
1250 } else {
1251 my @s = stat('.') or die "$prg: $dir - $!\n";
1252 $flist = sprintf '%s/fexsync_%s_%d%d.list',$tmpdir,$transfer,$s[0],$s[1];
1253 }
1254
1255 open $flist,'>',$flist or die "$prg: cannot create $flist - $!\n";
1256 # print "new/modified files:\n" if %files;
1257 foreach $file (asort(keys %files)) {
1258 next if $files{$file} >= $t0;
1259 print {$flist} hexdecode($file),"\n";
1260 }
1261 close $flist;
1262
1263 # must send empty flist archive, too,
1264 # because fexsync receive client waits for it!
1265 my $x = 'locale=english&autodelete=yes&keep=1';
1266 my $a = sprintf "fexsync_%s_%s.%s",$transfer,$tkey,$opt_z ? 'tgz' : 'taz';
1267 if ($dox and $flist =~ /(.+)\.list$/) {
1268 my $job = $1;
1269 my $cmd = "$1.sh";
1270 my $c = 'DOX';
1271 $c .= ':ADD' if $dox and $opt_s and @ARGV;
1272 $c .= ':DELETE' if $opt_d;
1273 $fexsend = qq'@fexsend -X "$x" -A $a -C $c "[\$job.list]" .';
1274 $dir = getcwd;
1275 open $cmd,'>',$cmd or die "$prg: cannot write $cmd - $!\n";
1276 print {$cmd} "#!/bin/bash -e\n";
1277 print {$cmd} "job=$job\n";
1278 print {$cmd} "cd $dir\n";
1279 print {$cmd} "$fexsend\n";
1280 print {$cmd} "rm \$job.list \$job.sh\n";
1281 close $cmd;
1282 chmod 0700,$cmd;
1283 $fexsend =~ s/\$job/$job/;
1284 if ($opt_n) {
1285 if (open $flist,$flist) {
1286 warn "transfer files:\n";
1287 print while <$flist>;
1288 close $flist;
1289 }
1290 open $flist,'>',$flist;
1291 close $flist;
1292 }
1293 vsystem($fexsend);
1294 if ($? == 0) {
1295 unlink $flist,$cmd;
1296 } else {
1297 die "\n".
1298 "$prg: fexsend failed, you can restart it with:\n".
1299 "$cmd\n";
1300 }
1301 } else {
1302 vsystem(@fexsend,'-X',$x,'-A',$a,"[$flist]",'+');
1303 }
1304 }
1305
1306 exit;
1307
1308
1309 sub getfid {
1310 my $idf = "$fexhome/id";
1311 $fexcgi = $user = $id = '';
1312
1313 # if (not $opt_i and $FEXID = $ENV{FEXID}||$ENV{FEXXX}) {
1314 if (not $opt_i and $FEXID = $ENV{FEXID}) {
1315 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
1316 $ENV{FEXID} = $FEXID; # for fexsend
1317 ($fexcgi,$user,$id) = split(/\s+/,$FEXID);
1318 } else {
1319 if (open $idf,$idf) {
1320 if ($opt_i) {
1321 while (<$idf>) {
1322 if (/^\[$opt_i\]/) {
1323 chomp($fexcgi = <$idf>) or die "$prg: no FEX-URL in [$opt_i] $idf\n";
1324 chomp($user = <$idf>) or die "$prg: no FROM in [$opt_i] $idf\n";
1325 chomp($id = <$idf>) or die "$prg: no ID in [$opt_i] $idf\n";
1326 push @fexsend,'-i',$opt_i;
1327 }
1328 }
1329 unless ($fexcgi) {
1330 die "$prg: id [$opt_i] not found in $idf\n";
1331 }
1332 } else {
1333 chomp($fexcgi = <$idf>) or die "$prg: no FEX-URL in $idf\n";
1334 chomp($user = <$idf>) or die "$prg: no FROM in $idf\n";
1335 chomp($id = <$idf>) or die "$prg: no ID in $idf\n";
1336 }
1337 close $idf;
1338 despace($fexcgi,$user,$id);
1339 if ($fexcgi !~ /^[\w\/.:-]+$/) {
1340 die "$prg: illegal FEX-URL \"$fexcgi\" in $idf\n";
1341 }
1342 if ($user !~ /^[\w\/\@.:%=+-]+$/) {
1343 die "$prg: illegal FROM \"$user\" in $idf\n";
1344 }
1345 }
1346 $fexcgi =~ s:/fup.*::;
1347 }
1348 if ($ENV{FEXSERVER} and not $ENV{GATEWAY_INTERFACE} and not $opt_i) {
1349 $fexcgi = $ENV{FEXSERVER};
1350 }
1351 }
1352
1353
1354 sub serverconnect {
1355 close $SH if $SH;
1356
1357 if ($port == 443) {
1358 if ($DEBUG and %SSL) {
1359 foreach my $v (keys %SSL) {
1360 printf "%s => %s\n",$v,$SSL{$v};
1361 }
1362 }
1363
1364 &enable_ssl;
1365 $SH = IO::Socket::SSL->new(
1366 PeerAddr => $server,
1367 PeerPort => $port,
1368 Proto => 'tcp',
1369 %SSL
1370 );
1371 } else {
1372 $SH = IO::Socket::INET->new(
1373 PeerAddr => $server,
1374 PeerPort => $port,
1375 Proto => 'tcp',
1376 );
1377 }
1378
1379 die "cannot connect $server:$port - $!\n" unless $SH;
1380 warn "TCPCONNECT to $server:$port\n" if $opt_v;
1381 }
1382
1383
1384 sub despace {
1385 foreach (@_) {
1386 s/^\s+//;
1387 s/\s+$//;
1388 }
1389 }
1390
1391
1392 sub query_sid {
1393 my $req;
1394 local $_;
1395 my $sid = $id;
1396
1397 if ($port ne 443) {
1398 sendrequest("GET /SID");
1399 &checkreply;
1400 if (/^HTTP.* 201 (.+)/) {
1401 $sid = 'MD5H:'.md5_hex($id.$1);
1402 while (<$SH>) {
1403 s/\r//;
1404 warn "<-- $_" if $opt_v;
1405 last if /^\n/;
1406 }
1407 } else {
1408 die "$prg: $server does not support session ID\n";
1409 }
1410 }
1411
1412 return $sid;
1413 }
1414
1415
1416 sub sigpipehandler {
1417 local $_ = '';
1418 $SIG{ALRM} = sub { };
1419 alarm(1);
1420 $_ = <$SH>||'';
1421 if (/^HTTP.* \d+ (.*)/) {
1422 if ($opt_v) {
1423 die "\n$prg: server error: @_\n";
1424 } else {
1425 die "\n$prg: server error: $1\n";
1426 }
1427 } else {
1428 die "\n$prg: got SIGPIPE (server closed connection)\n";
1429 }
1430 }
1431
1432
1433 # collect all regular files and symlinks with mtime
1434 sub collect {
1435 my $DIR;
1436 my ($dir,$file);
1437 local $_;
1438
1439 # warn "collect @_";
1440 foreach $dir (@_) {
1441 if (-l $dir or -f $dir) {
1442 $file = $dir;
1443 if (-l $file or -r $file) {
1444 if (my @s = lstat $file) {
1445 # using ctime is a stupid idea because it is also inode creation time
1446 # $files{$file} = $s[10] > $s[9] ? $s[10] : $s[9];
1447 $files{hexencode($file)} = $s[9];
1448 }
1449 } else {
1450 warn "$prg: cannot read $::dir/$file\n";
1451 $error++;
1452 }
1453 } elsif (opendir $DIR,$dir) {
1454 my @files = ();
1455 while (defined(my $file = readdir $DIR)) {
1456 next if $file =~ /^\.\.?$/;
1457 next if $file =~ /^($exclude)$/;
1458 $file = "$dir/$file";
1459 $file =~ s:^\./::;
1460 next if $file =~ /^($exclude)$/;
1461 if ($file =~ s/\n/?/g) {
1462 warn "$prg: ignoring file with newline character in filename: $file\n";
1463 next;
1464 }
1465 push @files,$file;
1466 }
1467 closedir $DIR;
1468 collect(@files);
1469 } else {
1470 warn "$prg: cannot open $::dir/$dir/ - $!\n";
1471 $error++;
1472 }
1473 }
1474 }
1475
1476
1477 sub isodate {
1478 my @d = localtime shift;
1479 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
1480 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
1481 }
1482
1483
1484 sub sendrequest {
1485 my $request = shift;
1486 my @head;
1487
1488 push @head,"$request HTTP/1.1";
1489 push @head,"Host: $server:$port";
1490 push @head,"User-Agent: $useragent";
1491 push @head,@_ if @_;
1492
1493 foreach my $head (@head) {
1494 warn "--> $head\n" if $opt_v;
1495 print {$SH} $head,"\r\n";
1496 }
1497 warn "-->\n" if $opt_v;
1498 print {$SH} "\r\n";
1499 }
1500
1501
1502 sub checkreply {
1503 $_ = <$SH>;
1504 die "$prg: no server reply\n" unless $_;
1505 s/\r?\n//;
1506 warn "<-- $_\n" if $opt_v;
1507 unless (/^HTTP.... 2\d\d /) {
1508 s/^HTTP.... \d\d\d //;
1509 die "$prg: server error: $_\n"
1510 }
1511 s/\r//;
1512 return $_;
1513 }
1514
1515
1516 sub nvtsend {
1517 local $SIG{PIPE} = sub { $sigpipe = "@_" };
1518
1519 $sigpipe = '';
1520
1521 die "$prg: internal error: no active network handle\n" unless $SH;
1522 die "$prg: remote host has closed the link\n" unless $SH->connected;
1523
1524 foreach my $line (@_) {
1525 warn "--> $line\n" if $opt_v;
1526 print {$SH} $line,"\r\n";
1527 if ($sigpipe) {
1528 undef $SH;
1529 return 0;
1530 }
1531 }
1532
1533 return 1;
1534 }
1535
1536
1537 sub enable_ssl {
1538 local $_;
1539 eval "use IO::Socket::SSL";
1540 die "$prg: cannot load IO::Socket::SSL\n" if $@;
1541 # needed for CentOS (Redhat, too?)
1542 foreach (qw'/etc/ssl/cert.pem /etc/ssl/certs/ca-bundle.crt') {
1543 $SSL{SSL_ca_file} = $_ if -f;
1544 }
1545 &get_ssl_env;
1546 eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143';
1547 if ($opt_v) {
1548 foreach my $v (keys %SSL) {
1549 printf STDERR "%s => %s\n",$v,$SSL{$v};
1550 }
1551 }
1552 }
1553
1554
1555 sub get_ssl_env {
1556 # set SSL/TLS options
1557 if (defined($ENV{SSLVERIFY})) {
1558 $SSL{SSL_verify_mode} = $ENV{SSLVERIFY};
1559 }
1560 foreach my $opt (qw(
1561 SSL_version
1562 SSL_cipher_list
1563 SSL_verify_mode
1564 SSL_ca_path
1565 SSL_ca_file)
1566 ) {
1567 my $env = uc($opt);
1568 $env =~ s/_//g;
1569 $SSL{$opt} = $ENV{$env} if defined($ENV{$env});
1570 }
1571
1572 if ($SSL{SSL_verify_mode} and $SSL{SSL_verify_mode} ne 'SSL_VERIFY_NONE') {
1573 &search_ca;
1574 unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
1575 die "$prg: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
1576 }
1577 } elsif (defined($SSL{SSL_verify_mode})) {
1578 # user has set SSLVERIFY=0 !
1579 } else {
1580 &search_ca;
1581 $SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
1582 }
1583 }
1584
1585
1586 sub search_ca {
1587 local $_;
1588
1589 return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
1590
1591 foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
1592 if (-f) {
1593 $SSL{SSL_ca_file} = $_;
1594 return;
1595 }
1596 }
1597
1598 foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
1599 if (-d) {
1600 $SSL{SSL_ca_path} = $_;
1601 return;
1602 }
1603 }
1604 }
1605
1606
1607 # from MIME::Base64::Perl
1608 sub decode_b64 {
1609 local $_ = shift;
1610 my $uu = '';
1611 my ($i,$l);
1612
1613 tr|A-Za-z0-9+=/||cd;
1614 s/=+$//;
1615 tr|A-Za-z0-9+/| -_|;
1616 return "" unless length;
1617
1618 $l = (length)-60;
1619 for ($i = 0; $i <= $l; $i += 60) {
1620 $uu .= "M" . substr($_,$i,60);
1621 }
1622 $_ = substr($_,$i);
1623 if (length) {
1624 $uu .= chr(32+(length)*3/4) . $_;
1625 }
1626 return unpack("u",$uu);
1627 }
1628
1629
1630 sub encode_b64 {
1631 local $_ = '';
1632
1633 pos($_[0]) = 0;
1634 $_ = join('',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs)));
1635 tr|\` -_|AA-Za-z0-9+/|;
1636 my $padding = (3 - length($_[0]) % 3) % 3;
1637 # warn $padding;
1638 s/.{$padding}$/'=' x $padding/e if $padding;
1639
1640 return $_;
1641 }
1642
1643
1644 sub hexencode {
1645 local $_ = shift;
1646 s/%/%25/g;
1647 s/([^!-~])/sprintf "%%%X",ord($1)/ige;
1648 return $_;
1649 }
1650
1651
1652 sub hexdecode {
1653 local $_ = shift;
1654 s/%([a-f0-9]{2})/chr(hex($1))/ige;
1655 return $_;
1656 }
1657
1658
1659 sub inputline {
1660 my $prompt = shift;
1661 my $default = shift||'';
1662 my $term = new Term::ReadLine $prg;
1663
1664 $term->ornaments(0) unless $ENV{PERL_RL};
1665 return $term->readline($prompt,$default)||'';
1666 }
1667
1668
1669 sub vexec {
1670 vsystem(@_);
1671 exit $?;
1672 }
1673
1674
1675 sub vsystem {
1676 my @cmd = @_;
1677 my $cmd;
1678 my $shellmeta = '[\\\'"`*?&|<>(){};]';
1679 local $_;
1680
1681 if ($opt_v) {
1682 if (-t STDIN) { print STDERR '$ ' }
1683 else { print STDERR '| ' }
1684 if (scalar(@cmd) == 1) {
1685 warn "@cmd\n";
1686 } else {
1687 my @w;
1688 foreach (@cmd) {
1689 if (/\'/) {
1690 push @w,shellquote($_);
1691 } elsif (/[^\w\/:=~^%@,.+-]/) {
1692 push @w,"'$_'";
1693 } else {
1694 push @w,$_;
1695 }
1696 }
1697 warn "@w\n";
1698 }
1699 }
1700
1701 $_ = "@cmd";
1702 if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) {
1703 @cmd = split;
1704 }
1705
1706 if (scalar(@cmd) == 1) {
1707 $cmd = "@cmd";
1708 $cmd =~ s/\|&$/ 2>&1|/;
1709 if ($cmd =~ s/\|$//) {
1710 my @a = `$cmd`;
1711 if (wantarray) {
1712 map { chomp } @a;
1713 return @a;
1714 } else {
1715 chomp $a[0] if scalar(@a) == 1;
1716 return join('',@a);
1717 }
1718 } else {
1719 system $cmd;
1720 }
1721 } else {
1722 system @cmd;
1723 }
1724 }
1725
1726
1727 sub preparevars {
1728 $transfer =~ s/[^$afnc]/_/g;
1729 $transfer =~ s/^\./_/;
1730
1731 $fexcgi =~ s(^http://)()i;
1732 $server = $fexcgi;
1733
1734 if ($server =~ s(^https://)()i) { $port = 443 }
1735 elsif ($server =~ /:(\d+)/) { $port = $1 }
1736 else { $port = 80 }
1737
1738 $server =~ s([:/].*)();
1739 }
1740
1741
1742 sub randstring {
1743 my $n = shift;
1744 my @rc = ('A'..'Z','a'..'z',0..9 );
1745 my $rn = @rc;
1746 my $rs;
1747
1748 for (1..$n) { $rs .= $rc[int(rand($rn))] };
1749 return $rs;
1750 }
1751
1752
1753 sub shellquote {
1754 local $_ = shift;
1755 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
1756 return $_;
1757 }
1758
1759
1760 sub get_xp {
1761 my $ux = '.fexdox/exclude';
1762 local $_;
1763
1764 $xp{$ux} = shp2prx($ux);
1765
1766 if (open $ux,$ux) {
1767 while (<$ux>) {
1768 chomp;
1769 $xp{$_} = shp2prx($_);
1770 }
1771 close $ux;
1772 $_ = '.fexdox/.versions/exclude~*~';
1773 $xp{$_} = shp2prx($_);
1774 }
1775
1776 if (0) {
1777 while (my($p,$x) = each %xp) {
1778 printf "%s : %s\n",$p,$x;
1779 }
1780 die;
1781 }
1782 }
1783
1784
1785 # remove non-regular files (for dox)
1786 sub unweed {
1787 local $_;
1788 foreach my $file (keys %files) {
1789 $_ = hexdecode($file);
1790 if (-l or not -f) {
1791 warn "excluded: $_\n" if $opt_v;
1792 delete $files{$file};
1793 }
1794 }
1795 }
1796
1797
1798 # dox exclude
1799 sub exclude {
1800 foreach my $file (asort(keys %files)) {
1801 foreach my $p (asort(keys %xp)) {
1802 my $pp = $xp{$p};
1803 $pp = '.*/'.$pp if $pp !~ m:^/:;
1804 my $dfile = hexdecode($file);
1805 if ("/$dfile" =~ m:^$pp(/.*)?$:) {
1806 warn "excluded: $dfile ($p)\n" if $opt_v;
1807 delete $files{$file};
1808 last;
1809 }
1810 }
1811 }
1812 }
1813
1814
1815 # shell pattern to Perl regular expression
1816 sub shp2prx {
1817 local $_ = shift;
1818 s:([^\w\[\]^/*?-]):\\$1:g;
1819 s:([^*])\*([^*]):$1\[^/\]*$2:g;
1820 s:([^*])\*$:$1\[^/\]*:;
1821 s:^\*([^*]):[^/]*$1:;
1822 s:\*\*:.*:g;
1823 s:\?:.:g;
1824 return $_;
1825 }
1826
1827
1828 # remove empty directories recursive
1829 sub rmdirr {
1830 my $dir = shift;
1831 local $_;
1832
1833 if (opendir $dir,$dir) {
1834 while (defined($_ = readdir($dir))) {
1835 next if /^\.\.?$/;
1836 $_ = "$dir/$_";
1837 rmdirr($_) if -d and not -l;
1838 }
1839 closedir $dir;
1840 rmdir $dir;
1841 }
1842 }
1843
1844
1845 sub sleepwait {
1846 my $wait = shift;
1847 my $comment = shift || '';
1848
1849 for (my $i = $wait; $i; $i--) {
1850 printf "%s%3d\r",$comment,$i;
1851 sleep 1;
1852 }
1853 print "$comment \r";
1854 }
1855
1856
1857 sub ljoin {
1858 return join("\n",@_)."\n";
1859 }
1860
1861
1862 sub mtime {
1863 my @s = stat shift;
1864 return @s ? $s[9] : 0;
1865 }
1866
1867
1868 sub mkdirp {
1869 my $dir = shift;
1870 my $pdir;
1871
1872 return if -d $dir;
1873 $dir =~ s://+:/:g;
1874 $dir =~ s:/$::;
1875 die "$prg: cannot mkdir /\n" unless length($dir);
1876 $pdir = $dir;
1877 if ($pdir =~ s:(.+)/.+:$1:) {
1878 mkdirp($pdir) unless -d $pdir;
1879 }
1880 mkdir $dir or die "$prg: mkdir $dir - $!\n";
1881 }
1882
1883
1884 sub asort {
1885 return sort { lc $a cmp lc $b } @_;
1886 }
1887
1888
1889 END { kill 9,$spid if $spid }