Debian Codegraph @

bin/sexxx

raw
1 #!/usr/bin/perl -w
2
3 # client for stream exchange of the FEX service
4 #
5 # Author: Ulli Horlacher <[email protected]>
6 #
7 # Perl Artistic Licence
8
9 # sexsend / sexget / sexxx
10
11 use Getopt::Std;
12 use Socket;
13 use IO::Handle;
14 use IO::Socket::INET;
15 use Digest::MD5 qw(md5_hex); # encypted ID / SID
16
17 use constant k => 2**10;
18 use constant M => 2**20;
19
20 eval 'use Net::INET6Glue::INET_is_INET6';
21
22 our $version = 20200429;
23 our $DEBUG = $ENV{DEBUG};
24 our $prg = $0;
25
26 our %SSL = (SSL_version => 'TLSv1');
27
28 my $sigpipe;
29
30 unless ($version) {
31 my @d = localtime((stat $0)[9]);
32 $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
33 }
34
35 our $useragent = "sexsend-$version";
36 if (my $fua = $ENV{FUA}) {
37 $useragent =~ s:^:$fua/:;
38 }
39
40 if (-f ($_ = '/etc/fex/config.pl')) {
41 eval { require } or warn $@;
42 }
43
44 $| = 1;
45 $prg =~ s:(.*)/::; # and $fexsend = "$1/fexsend";
46 $0 = "$prg @ARGV";
47
48 # sexsend is default
49 $usage =
50 "usage: ... | $prg [options] [SEX-URL/]recipient [stream]\n".
51 "options: -v verbose mode\n".
52 " -g show transfer rate\n".
53 " -V show version\n".
54 " -m KBS limit throughput (KBS kB/s)\n".
55 " -t timeout timeout in s (waiting for recipient)\n".
56 "special: recipient may be \"public\" or \"anonymous\" or \".\"\n".
57 "see also: sexget, sexxx\n".
58 "example: tail -f /var/log/syslog | $prg fex.flupp.org/admin log\n";
59
60 if ($prg eq 'sexget' or $prg eq 'fuckme') {
61 $usage =
62 "usage: $prg [options] [[SEX-URL/]user:ID] [stream]\n".
63 "options: -v verbose mode\n".
64 " -g show transfer rate\n".
65 " -m KBS limit throughput (KBS kB/s)\n".
66 "arguments: user:ID use this user & ID\n".
67 " (ID may be \"public\" or user:ID may be \"anonymous\")\n".
68 " stream name of the stream\n".
69 "see also: sexsend, sexxx\n".
70 "example: $prg log | grep kernel\n";
71 }
72
73 if ($prg eq 'sexxx') {
74 $usage =
75 "usage: $prg [-v] [-g] [-c] [-u [SEX-URL/]user] [-s stream] [files...]\n".
76 "usage: $prg [-v] [-g] [-u [SEX-URL/]user] [-s stream] | ...\n".
77 "options: -v verbose mode\n".
78 " -g show transfer rate\n".
79 " -q quiet mode\n".
80 " -c compress files\n".
81 " -m KBS limit throughput (KBS kB/s)\n".
82 " -u SEX-URL/user SEX-URL and user (default: use FEXID/FEXXX)\n".
83 " -s stream stream name (default: xx)\n".
84 "see also: sexsend, sexget\n".
85 "examples: $prg -s config /etc /usr/local/etc\n".
86 " $prg > backup.tar\n";
87 }
88
89 $fexhome = $ENV{FEXHOME} || $ENV{HOME}.'/.fex';
90 $user = $id = '';
91 $type = $timeout = $stream = $mode = '';
92 $idf = "$fexhome/id";
93 $bs = $ENV{BS} || 2**16; # I/O blocksize
94
95 # server URL, user and auth-ID
96 if ($FEXID = $ENV{FEXID}) {
97 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
98 if ($FEXID =~ /!/) {
99 die qq'$prg: \$FEXID contains "!" - proxy is not supported\n';
100 }
101 ($fexcgi,$user,$id) = split(/\s+/,$FEXID);
102 } else {
103 if (open $idf,$idf) {
104 chomp($fexcgi = <$idf>) or die "$prg: no FEX-URL in $idf\n";
105 chomp($user = <$idf>) or die "$prg: no FROM in $idf\n";
106 chomp($id = <$idf>) or die "$prg: no ID in $idf\n";
107 close $idf;
108 despace($fexcgi,$user,$id);
109 if ($fexcgi =~ /!/) {
110 die qq'$prg: FEX-URL "$fexcgi" in $idf contains "!"'.
111 qq' - proxy is not supported\n';
112 }
113 if ($fexcgi !~ /^[_:=\w\-\.\/\@\%]+$/) {
114 die qq'$prg: illegal FEX-URL "$fexcgi" in $idf\n';
115 }
116 unless ($user =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
117 die qq'$prg: illegal FROM "$user" in $idf\n';
118 }
119 }
120 }
121
122 $_ = "$fexhome/config.pl"; require if -f;
123
124 $opt_h = $opt_v = $opt_V = $opt_q = 0;
125 $opt_u = $opt_s = $opt_c = $opt_t = $opt_m = '';
126
127 if ($prg eq 'sexxx') {
128
129 # xx server URL, user and auth-ID
130 if ($FEXXX = $ENV{FEXXX}) {
131 $FEXXX = decode_b64($FEXXX) if $FEXXX !~ /\s/;
132 ($fexcgi,$user,$id) = split(/\s+/,$FEXXX);
133 } elsif (open $idf,$idf) {
134 while (<$idf>) {
135 if (/^\[xx\]/) {
136 chomp($fexcgi = <$idf>) or die "$prg: no xx FEX-URL in $idf\n";
137 chomp($user = <$idf>) or die "$prg: no xx FROM in $idf\n";
138 chomp($id = <$idf>) or die "$prg: no xx ID in $idf\n";
139 last;
140 }
141 }
142 close $idf;
143 }
144
145 getopts('hgvcu:s:m:') or die $usage;
146 die $usage if $opt_h;
147 die $usage unless -t;
148
149 if ($opt_c) {
150 $opt_c = 'z';
151 $type = '&type=GZIP';
152 }
153
154 if ($opt_u) {
155 $fexcgi = $1 if $opt_u =~ s:(.+)/::;
156 $user = $opt_u;
157 }
158
159 if ($opt_m !~ /^\d+$/) {
160 $opt_m = 0;
161 }
162
163 unless ($fexcgi) {
164 die "$prg: no xx user found, use \"$prg -u SEX-URL/user\"\n";
165 }
166
167 unless ($user) {
168 die "$prg: no xx user found, use \"$prg -u user\"\n";
169 }
170
171 } elsif ($prg eq 'sexget' or $prg eq 'fuckme') {
172
173 $opt_g = 0;
174 getopts('hgvVdqu:m:') or die $usage;
175 die $usage if $opt_h;
176
177
178 if ($opt_V) {
179 print "Version: $version\n";
180 exit unless @ARGV;
181 }
182
183 if (not $opt_u and @ARGV and $ARGV[0] =~ m{^anonymous|/|:}) {
184 $opt_u = shift @ARGV;
185 }
186
187 if ($opt_u) {
188 $fexcgi = $1 if $opt_u =~ s:(.+)/::;
189 ($user,$id) = split(':',$opt_u);
190 if ($user =~ /^anonymous/) {
191 $anonymous = $user;
192 } elsif (not $id) {
193 die $usage;
194 }
195 }
196
197 unless ($fexcgi) {
198 die "$prg: no SEX URL found, use \"$prg -u SEX-URL/recipient\" or \"fexsend -I\"\n";
199 }
200
201 unless ($user) {
202 die "$prg: no recipient found, use \"$prg -u SEX-URL/recipient\" or \"fexsend -I\"\n";
203 }
204
205 } else { # sexsend
206
207 $opt_g = 0;
208 getopts('hguvqVTt:m:') or die $usage;
209 die $usage if $opt_h;
210
211 if ($opt_V) {
212 print "Version: $version\n";
213 exit unless @ARGV;
214 }
215
216 if ($opt_t and $opt_t =~ /^\d+$/) {
217 $timeout = "&timeout=$opt_t";
218 }
219
220 my $save_user = $user;
221 $user = shift or die $usage;
222 $fexcgi = $1 if $user =~ s:(.+)/::;
223
224 if ($user =~ /^anonymous/) {
225 die "$prg: need SEX-URL with anonymous SEX\n" unless $fexcgi;
226 $mode = 'anonymous';
227 } elsif ($user eq 'public') {
228 unless ($id) {
229 die "$prg: public SEX not possible without FEXID, set it with \"fexsend -I\"\n";
230 }
231 $mode = $user;
232 $user = $save_user;
233 } elsif ($user eq 'DOX') {
234 unless ($id) {
235 die "$prg: DOX SEX not possible without FEXID\n";
236 }
237 $mode = $user;
238 $user = $save_user;
239 } elsif ($user eq '.') {
240 open $idf,$idf or die "$prg: no $idf\n";
241 $_ = <$idf>;
242 $user = <$idf>||'';
243 chomp $user;
244 } else {
245 unless ($fexcgi) {
246 die "$prg: no SEX URL found, use \"$prg SEX-URL/recipient\" or \"fexsend -I\"\n";
247 }
248 }
249
250 }
251
252 # &get_ssl_env;
253
254 $fexcgi =~ s(^http://)()i;
255 $fexcgi =~ s(/fup.*)();
256 $server = $fexcgi;
257
258 if ($server =~ s(^https://)()i) { $port = 443 }
259 elsif ($server =~ /:(\d+)/) { $port = $1 }
260 else { $port = 80 }
261
262 $server =~ s([:/].*)();
263
264 ## set up tcp/ip connection
265 # $iaddr = gethostbyname($server)
266 # or die "$prg: cannot find ip-address for $server $!\n";
267 # socket(SH,PF_INET,SOCK_STREAM,getprotobyname('tcp')) or die "$prg: socket $!\n";
268 # connect(SH,sockaddr_in($port,$iaddr)) or die "$prg: connect $!\n";
269 # warn "connecting $server:$port user=$user\n";
270 if ($port == 443) {
271 if ($opt_v and %SSL) {
272 foreach my $v (keys %SSL) {
273 printf "%s => %s\n",$v,$SSL{$v};
274 }
275 }
276
277 &enable_ssl;
278 $SH = IO::Socket::SSL->new(
279 PeerAddr => $server,
280 PeerPort => $port,
281 Proto => 'tcp',
282 %SSL
283 );
284 } else {
285 $SH = IO::Socket::INET->new(
286 PeerAddr => $server,
287 PeerPort => $port,
288 Proto => 'tcp',
289 );
290 }
291
292 die "cannot connect $server:$port - $!\n" unless $SH;
293 warn "TCPCONNECT to $server:$port\n" if $opt_v;
294
295 # autoflush $SH 1;
296 autoflush STDERR;
297
298 $SIG{PIPE} = \&sigpipehandler;
299
300 if ($prg eq 'sexget' or $prg eq 'fuckme') {
301 $stream = "&stream=" . shift if @ARGV;
302 if ($anonymous) {
303 $cid = 'anonymous';
304 } elsif ($id eq 'public') {
305 $cid = 'public';
306 } else {
307 $cid = query_sid($server,$port,$id);
308 }
309 request("GET /sex?BS=$bs&user=$user&ID=$cid$stream HTTP/1.0");
310 transfer($SH,STDOUT);
311 # print while sysread $SH,$_,$bs;
312 exit;
313 }
314
315 if ($prg eq 'sexxx') {
316 $stream = "&stream=" . ($opt_s || 'xx');
317 if (@ARGV) {
318 warn "streaming:\n";
319 open my $tar,'-|','tar',"-cv${opt_c}f",'-',@ARGV or die "$prg: cannot run tar - $!\n";
320 request("POST /sex?BS=$bs&user=$user$type$stream HTTP/1.0");
321 transfer($tar,$SH);
322 # while (read $tar,$_,$bs) { syswrite $SH,$_ }
323 } else {
324 $cid = query_sid($server,$port,$id);
325 request("GET /sex?BS=$bs&user=$user&ID=$cid$stream HTTP/1.0");
326 $opt_c = 'z' if $H{'CONTENT-TYPE'} =~ /gzip/i;
327 if (-t STDOUT) {
328 print "extracting from stream:\n";
329 open $out,"|tar xv${opt_c}f -" or die "$prg: cannot run tar - $!\n";
330 } else {
331 if ($opt_c) {
332 open $out,"|gzip -d" or die "$prg: cannot run gunzip - $!\n";
333 } else {
334 $out = *STDOUT;
335 }
336 }
337 $B = 0;
338 $t0 = time;
339 while ($b = sysread $SH,$_,$bs) {
340 $B += $b;
341 print {$out} $_;
342 if ($opt_m) {
343 sleep 1 while $B/((time-$t0)||1)/k > $opt_m;
344 }
345 }
346 }
347 exit;
348 } # sexxx
349
350 # sexsend
351 $stream = "&stream=" . shift if @ARGV;
352
353 my $psp = "http://$server:$port";
354 $psp =~ s/:443// and $psp =~ s/http/https/;
355
356 if ($mode eq 'anonymous') {
357 unless ($opt_q) {
358 print "$psp/sex?user=$user&ID=anonymous$stream\n";
359 printf "$psp/sex?%s\n",
360 encode_b64("user=$user&ID=anonymous$stream");
361 }
362 $mode = "&mode=anonymous";
363 } elsif ($mode eq 'public') {
364 unless ($user and $id) {
365 die "$prg: need user/ID when sending to public, set it with fexsend -I\n";
366 }
367 unless ($opt_q) {
368 unless ($ENV{GATEWAY_INTERFACE}) {
369 print "$psp/sex?user=$user&ID=public$stream\n";
370 printf "$psp/sex?%s\n",
371 encode_b64("user=$user&ID=public$stream");
372 }
373 $stream =~ /=(.+)/ and
374 print "sexget $psp/$user:public $1\n";
375 }
376 $cid = query_sid($server,$port,$id);
377 $mode = "&ID=$cid&mode=$mode";
378 } elsif ($mode eq 'DOX') {
379 unless ($user and $id) {
380 die "$prg: need user/ID\n";
381 }
382 $cid = query_sid($server,$port,$id);
383 $mode = "&ID=$cid&mode=$mode";
384 } else {
385 # $user = checkalias($user) unless $opt_d;
386 }
387
388 request("POST /sex?BS=$bs&user=$user$mode$timeout$stream HTTP/1.0");
389 print STDERR "--> (streaming ...)\n" if $opt_v;
390
391 transfer(STDIN,$SH);
392
393 exit;
394
395
396 sub transfer {
397 my $source = shift;
398 my $destination = shift;
399 my $B = 0;
400 my ($t0,$t1,$tt);
401 my ($b,$bt);
402
403 $t0 = $t2 = time;
404 $tt = $t0-1;
405 $t1 = 0;
406
407 while ($b = sysread $source,$_,$bs) {
408 print {$destination} $_ or die "$prg: link failure - $!\n";
409 $B += $b;
410 $bt += $b;
411 $t2 = time;
412 if ($opt_m) {
413 sleep 1 while $B/((time-$t0)||1)/k > $opt_m;
414 }
415 if ($t2>$t1) {
416 if ($opt_g) {
417 if ($B>2*M) {
418 printf "%d MB %d kB/s \n",
419 int($B/M),int($bt/k/($t2-$tt));
420 } else {
421 printf "%d kB %d kB/s \n",
422 int($B/k),int($bt/k/($t2-$tt));
423 }
424 # print "\n" if $dox;
425 }
426 $t1 = $t2;
427 if ($t2-$tt>10) {
428 $bt = 0;
429 $tt = $t2;
430 }
431 }
432 }
433
434 die "$prg: no stream data\n" unless $B;
435
436 $tt = (time-$t0)||1;
437
438 if ($opt_v or $opt_g) {
439 if ($B>2097152) {
440 printf "transfered: %d MB in %d s with %d kB/s\n",
441 int($B/1048576),$tt,int($B/1024/$tt);
442 } elsif($B>2048) {
443 printf "transfered: %d kB in %d s with %d kB/s\n",
444 int($B/1024),$tt,int($B/1024/$tt);
445 } else {
446 printf "transfered: %d B in %d s with %d kB/s\n",
447 $B,$tt,int($B/1024/$tt);
448 }
449 }
450
451 }
452
453
454 sub request {
455 my $req = shift;
456
457 print STDERR "--> $req\n" if $opt_v;
458 syswrite $SH,"$req\r\n";
459 syswrite $SH,"Host: $server\r\n";
460 syswrite $SH,"User-Agent: $useragent\r\n";
461 syswrite $SH,"\r\n";
462 for (;;) {
463 unless (defined($_ = &getline)) {
464 die "$prg: server has closed the connection\n";
465 }
466 if (/^HTTP\/[\d\.]+ 200/) {
467 print STDERR "<-- $_" if $opt_v;
468 last;
469 } elsif (/^HTTP\/[\d\.]+ 199/) {
470 print STDERR "<-- $_" if $opt_v;
471 } else {
472 if ($opt_v) {
473 print STDERR "<-- $_";
474 exit 3;
475 } else {
476 s:^HTTP/[ \d\.]+::;
477 s/\r//;
478 die "$prg: server response: $_";
479 }
480 }
481 }
482 while (defined($_ = &getline)) {
483 last if /^\s*$/;
484 $H{uc($1)} = $2 if /(.+):\s*(.+)/;
485 print STDERR "<-- $_" if $opt_v;
486 }
487 }
488
489 # check for (mutt) alias
490 sub checkalias {
491 my $to = shift;
492 if ($to !~ /@/ and open F,$ENV{HOME}.'/.mutt/aliases') {
493 while (<F>) {
494 next if /,/;
495 if (/^alias $to\s/i) {
496 chomp;
497 s/\s*#.*//;
498 s/\s+$//;
499 s/.*\s+//;
500 s/<//;
501 s/>//;
502 $to = $_;
503 warn "$prg: found alias, using address $to\n";
504 die unless $to;
505 last;
506 }
507 }
508 close F;
509 }
510 return $to;
511 }
512
513 sub despace {
514 foreach (@_) {
515 s/^\s+//;
516 s/\s+$//;
517 }
518 }
519
520 sub query_sid {
521 my ($server,$port,$id) = @_;
522 my $req;
523 local $_;
524
525 $req = "GET SID HTTP/1.1";
526 print STDERR "--> $req\n" if $opt_v;
527 syswrite $SH,"$req\r\n\r\n";
528 $_ = &getline;
529 unless (defined $_ and /\w/) {
530 print STDERR "\n" if $opt_v;
531 die "$prg: no response from server\n";
532 }
533 s/\r//;
534 if (/^HTTP.* 201 (.+)/) {
535 print STDERR "<-- $_" if $opt_v;
536 $id = 'MD5H:'.md5_hex($id.$1);
537 while (defined($_ = &getline)) {
538 s/\r//;
539 last if /^\n/;
540 print STDERR "<-- $_" if $opt_v;
541 }
542 } else {
543 die "$prg: $server does not support session ID\n";
544 }
545 return $id;
546 }
547
548 sub sigpipehandler {
549 local $_ = '';
550 $SIG{ALRM} = sub { };
551 alarm(1);
552 $_ = &getline||'';
553 if (/^HTTP.* \d+ (.*)/) {
554 if ($opt_v) {
555 die "\n$prg: server error: @_\n";
556 } else {
557 die "\n$prg: server error: $1\n";
558 }
559 } else {
560 die "\n$prg: got SIGPIPE (server closed connection)\n";
561 }
562 }
563
564 # read one text line from $SH;
565 sub getline {
566 my $line = '';
567 my $c;
568
569 local $SIG{ALRM} = sub { die "$prg: timeout while waiting for server reply\n" };
570 alarm($opt_t||300);
571
572 # must use sysread to avoid perl line buffering
573 while (sysread $SH,$c,1) {
574 $line .= $c;
575 last if $c eq "\n";
576 }
577
578 alarm(0);
579
580 return $line;
581 }
582
583 ### common functions ###
584
585 sub mtime {
586 my @d = localtime((stat shift)[9]);
587 return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
588 }
589
590
591 sub urldecode {
592 local $_ = shift;
593 s/\%([a-f\d]{2})/chr(hex($1))/ige;
594 return $_;
595 }
596
597
598 sub serverconnect {
599 my ($server,$port) = @_;
600 my $connect = "CONNECT $server:$port HTTP/1.1";
601 local $_;
602
603 if ($proxy) {
604 tcpconnect(split(':',$proxy));
605 if ($port == 443) {
606 nvtsend($connect,"");
607 $_ = <$SH>;
608 s/\r//;
609 warn "<-- $_"if $opt_v;
610 unless (/^HTTP.1.. 200/) {
611 die "$prg: proxy error : $_";
612 }
613 &enable_ssl;
614 $SH = IO::Socket::SSL->start_SSL($SH,%SSL);
615 }
616 } else {
617 tcpconnect($server,$port);
618 }
619 }
620
621
622 # set up tcp/ip connection
623 sub tcpconnect {
624 my ($server,$port) = @_;
625
626 if ($SH) {
627 close $SH;
628 undef $SH;
629 }
630
631 if ($port == 443) {
632 # eval "use IO::Socket::SSL qw(debug3)";
633 &enable_ssl;
634 $SH = IO::Socket::SSL->new(
635 PeerAddr => $server,
636 PeerPort => $port,
637 Proto => 'tcp',
638 %SSL
639 );
640 } else {
641 $SH = IO::Socket::INET->new(
642 PeerAddr => $server,
643 PeerPort => $port,
644 Proto => 'tcp',
645 );
646 }
647
648 if ($SH) {
649 autoflush $SH 1;
650 binmode $SH;
651 } else {
652 die "$prg: cannot connect $server:$port - $@\n";
653 }
654
655 warn "TCPCONNECT to $server:$port\n" if $opt_v;
656 }
657
658
659 sub sendheader {
660 my $sp = shift;
661 my @head = @_;
662 my $head;
663
664 push @head,"Host: $sp";
665 push @head,"User-Agent: $useragent";
666
667 foreach $head (@head) {
668 chomp $head;
669 warn "--> $head\n" if $opt_v;
670 print {$SH} $head,"\r\n";
671 }
672 warn "-->\n" if $opt_v;
673 print {$SH} "\r\n";
674 }
675
676
677 sub nvtsend {
678 local $SIG{PIPE} = sub { $sigpipe = "@_" };
679
680 $sigpipe = '';
681
682 die "$prg: internal error: no active network handle\n" unless $SH;
683 die "$prg: remote host has closed the link\n" unless $SH->connected;
684
685 foreach my $line (@_) {
686 warn "--> $line\n" if $opt_v;
687 print {$SH} $line,"\r\n";
688 if ($sigpipe) {
689 undef $SH;
690 return 0;
691 }
692 }
693
694 return 1;
695 }
696
697
698 sub shellquote {
699 local $_ = shift;
700 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
701 return $_;
702 }
703
704
705 sub debug {
706 print "## DEBUG: @_\n" if $DEBUG;
707 }
708
709
710 sub locale {
711 my $string = shift;
712
713 # my @x = Encode->encodings(':all'); die "$CTYPE - @x";
714 if ($CTYPE) {
715 if ($CTYPE =~ /UTF-?8/i) {
716 return $string;
717 } elsif (grep { $CTYPE =~ /^$_$/i } Encode->encodings(':all')) {
718 return encode($CTYPE,decode('UTF8',$string));
719 } else {
720 return encode('ISO-8859-1',decode('UTF8',$string));
721 }
722 }
723
724 return $string;
725 }
726
727
728 sub enable_ssl {
729 eval "use IO::Socket::SSL";
730 die "$prg: cannot load IO::Socket::SSL\n" if $@;
731 # needed for CentOS (Redhat, too?)
732 foreach (qw'/etc/ssl/cert.pem /etc/ssl/certs/ca-bundle.crt') {
733 $SSL{SSL_ca_file} = $_ if -f;
734 }
735 &get_ssl_env;
736 eval '$SSL{SSL_verify_mode} = 0 if Net::SSLeay::SSLeay() <= 9470143';
737 if ($opt_v) {
738 foreach my $v (keys %SSL) {
739 printf STDERR "%s => %s\n",$v,$SSL{$v};
740 }
741 }
742 }
743
744
745 sub get_ssl_env {
746 # set SSL/TLS options
747 $SSL{SSL_verify_mode} = $ENV{SSLVERIFY} if defined($ENV{SSLVERIFY});
748 foreach my $opt (qw(
749 SSL_version
750 SSL_cipher_list
751 SSL_verify_mode
752 SSL_ca_path
753 SSL_ca_file)
754 ) {
755 my $env = uc($opt);
756 $env =~ s/_//g;
757 $SSL{$opt} = $ENV{$env} if defined($ENV{$env});
758 }
759
760 if ($SSL{SSL_verify_mode} and $SSL{SSL_verify_mode} ne 'SSL_VERIFY_NONE') {
761 &search_ca;
762 unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
763 die "$prg: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
764 }
765 } elsif (defined($SSL{SSL_verify_mode})) {
766 # user has set SSLVERIFY=0 !
767 } else {
768 &search_ca;
769 $SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
770 }
771 }
772
773
774 sub search_ca {
775 local $_;
776
777 return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
778
779 foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
780 if (-f) {
781 $SSL{SSL_ca_file} = $_;
782 return;
783 }
784 }
785
786 foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
787 if (-d) {
788 $SSL{SSL_ca_path} = $_;
789 return;
790 }
791 }
792 }
793
794
795 # from MIME::Base64::Perl
796 sub encode_b64 {
797 my $res = "";
798 my $eol = "\n";
799 my $padding;
800
801 pos($_[0]) = 0;
802 $res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
803 $res =~ tr|\` -_|AA-Za-z0-9+/|;
804 $padding = (3-length($_[0])%3)%3;
805 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
806 return $res;
807 }
808
809
810 # from MIME::Base64::Perl
811 sub decode_b64 {
812 local $_ = shift;
813 my $uu = '';
814 my ($i,$l);
815
816 tr|A-Za-z0-9+=/||cd;
817 s/=+$//;
818 tr|A-Za-z0-9+/| -_|;
819 return "" unless length;
820
821 $l = (length)-60;
822 for ($i = 0; $i <= $l; $i += 60) {
823 $uu .= "M" . substr($_,$i,60);
824 }
825 $_ = substr($_,$i);
826 if (length) {
827 $uu .= chr(32+(length)*3/4) . $_;
828 }
829 return unpack("u",$uu);
830 }
831
832
833 sub inputline {
834 my $prompt = shift;
835 my $default = shift||'';
836 my $term = new Term::ReadLine $prg;
837
838 $term->ornaments(0) unless $ENV{PERL_RL};
839 return $term->readline($prompt,$default)||'';
840 }
841
842
843 sub vexec {
844 vsystem(@_);
845 exit $?;
846 }
847
848
849 sub vsystem {
850 my @cmd = @_;
851 my $cmd;
852 my $shellmeta = '[\\\'"`*?&|<>(){};]';
853 local $_;
854
855 if ($opt_v) {
856 if (-t STDIN) { print STDERR '$ ' }
857 else { print STDERR '| ' }
858 if (scalar(@cmd) == 1) {
859 warn "@cmd\n";
860 } else {
861 my @w;
862 foreach (@cmd) {
863 if (/\'/) {
864 push @w,shellquote($_);
865 } elsif (/[^\w\/:=~^%@,.+-]/) {
866 push @w,"'$_'";
867 } else {
868 push @w,$_;
869 }
870 }
871 warn "@w\n";
872 }
873 }
874
875 $_ = "@cmd";
876 if (scalar(@cmd) == 1 and /\s/ and not (/^\w+=/ or /$shellmeta/)) {
877 @cmd = split;
878 }
879
880 if (scalar(@cmd) == 1) {
881 $cmd = "@cmd";
882 $cmd =~ s/\|&$/ 2>&1|/;
883 if ($cmd =~ s/\|$//) {
884 my @a = `$cmd`;
885 if (wantarray) {
886 map { chomp } @a;
887 return @a;
888 } else {
889 chomp $a[0] if scalar(@a) == 1;
890 return join('',@a);
891 }
892 } else {
893 system $cmd;
894 }
895 } else {
896 system @cmd;
897 }
898 }