3 # client for stream exchange of the FEX service
5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
7 # Perl Artistic Licence
9 # sexsend / sexget / sexxx
15 use Digest::MD5 qw(md5_hex); # encypted ID / SID
17 use constant k => 2**10;
18 use constant M => 2**20;
20 eval 'use Net::INET6Glue::INET_is_INET6';
22 our $version = 20150120;
24 my %SSL = (SSL_version => 'TLSv1');
27 if (-f ($_ = '/etc/fex/config.pl')) {
28 eval { require } or warn $@;
36 "usage: ... | $0 [options] [SEX-URL/]recipient [stream]\n".
37 "options: -v verbose mode\n".
38 " -g show transfer rate\n".
40 " -t timeout timeout in s (waiting for recipient)\n".
41 "special: recipient may be \"public\" or \"anonymous\" or \".\"\n".
42 "see also: sexget, sexxx\n".
43 "example: tail -f /var/log/syslog | $0 fex.flupp.org/admin log\n";
45 if ($0 eq 'sexget' or $0 eq 'fuckme') {
47 "usage: $0 [options] [[SEX-URL/]user:ID] [stream]\n".
48 "options: -v verbose mode\n".
49 " -g show transfer rate\n".
51 "arguments: user:ID use this user & ID\n".
52 " (ID may be \"public\" or user:ID may be \"anonymous\")\n".
53 " stream name of the stream\n".
54 "see also: sexsend, sexxx\n".
55 "example: $0 log | grep kernel\n";
60 "usage: $0 [-v] [-g] [-c] [-u [SEX-URL/]user] [-s stream] [files...]\n".
61 "usage: $0 [-v] [-g] [-u [SEX-URL/]user] [-s stream] | ...\n".
62 "options: -v verbose mode\n".
63 " -g show transfer rate\n".
65 " -c compress files\n".
66 " -u SEX-URL/user SEX-URL and user (default: use FEXID/FEXXX)\n".
67 " -s stream stream name (default: xx)\n".
68 "see also: sexsend, sexget\n".
69 "examples: $0 -s config /etc /usr/local/etc\n".
73 $fexhome = $ENV{FEXHOME} || $ENV{HOME}.'/.fex';
75 $type = $timeout = $stream = $mode = '';
77 $bs = $ENV{BS} || 2**16; # I/O blocksize
79 # server URL, user and auth-ID
80 if ($FEXID = $ENV{FEXID}) {
81 $FEXID = decode_b64($FEXID) if $FEXID !~ /\s/;
82 ($fexcgi,$user,$id) = split(/\s+/,$FEXID);
85 chomp($fexcgi = <$idf>) or die "$0: no FEX-URL in $idf\n";
86 chomp($user = <$idf>) or die "$0: no FROM in $idf\n";
87 chomp($id = <$idf>) or die "$0: no ID in $idf\n";
89 despace($fexcgi,$user,$id);
90 unless ($fexcgi =~ /^[_:=\w\-\.\/\@\%]+$/) {
91 die "$0: illegal FEX-URL \"$fexcgi\" in $idf\n";
93 unless ($user =~ /^[_:=\w\-\.\/\@\%\+]+$/) {
94 die "$0: illegal FROM \"$user\" in $idf\n";
99 $opt_h = $opt_v = $opt_V = $opt_q = 0;
100 $opt_u = $opt_s = $opt_c = $opt_t = '';
102 $_ = "$fexhome/config.pl"; require if -f;
106 # xx server URL, user and auth-ID
107 if ($FEXXX = $ENV{FEXXX}) {
108 $FEXXX = decode_b64($FEXXX) if $FEXXX !~ /\s/;
109 ($fexcgi,$user,$id) = split(/\s+/,$FEXXX);
110 } elsif (open $idf,$idf) {
113 chomp($fexcgi = <$idf>) or die "$0: no xx FEX-URL in $idf\n";
114 chomp($user = <$idf>) or die "$0: no xx FROM in $idf\n";
115 chomp($id = <$idf>) or die "$0: no xx ID in $idf\n";
122 getopts('hgvcu:s:') or die $usage;
123 die $usage if $opt_h;
124 die $usage unless -t;
128 $type = '&type=GZIP';
132 $fexcgi = $1 if $opt_u =~ s:(.+)/::;
137 die "$0: no xx user found, use \"$0 -u SEX-URL/user\"\n";
141 die "$0: no xx user found, use \"$0 -u user\"\n";
144 } elsif ($0 eq 'sexget' or $0 eq 'fuckme') {
145 getopts('hgvVdu:') or die $usage;
146 die $usage if $opt_h;
150 print "Version: $version\n";
154 if (not $opt_u and @ARGV and $ARGV[0] =~ m{^anonymous|/|:}) {
155 $opt_u = shift @ARGV;
159 $fexcgi = $1 if $opt_u =~ s:(.+)/::;
160 ($user,$id) = split(':',$opt_u);
161 if ($user =~ /^anonymous/) {
169 die "$0: no SEX URL found, use \"$0 -u SEX-URL/recipient\" or \"fexsend -I\"\n";
173 die "$0: no recipient found, use \"$0 -u SEX-URL/recipient\" or \"fexsend -I\"\n";
179 getopts('hguvqVTt:') or die $usage;
180 die $usage if $opt_h;
183 print "Version: $version\n";
187 if ($opt_t and $opt_t =~ /^\d+$/) {
188 $timeout = "&timeout=$opt_t";
191 my $save_user = $user;
192 $user = shift or die $usage;
193 $fexcgi = $1 if $user =~ s:(.+)/::;
195 if ($user =~ /^anonymous/) {
196 die "$0: need SEX-URL with anonymous SEX\n" unless $fexcgi;
198 } elsif ($user eq 'public') {
200 die "$0: public SEX not possible without FEXID, set it with \"fexsend -I\"\n";
204 } elsif ($user eq '.') {
205 open $idf,$idf or die "$0: no $idf\n";
211 die "$0: no SEX URL found, use \"$0 SEX-URL/recipient\" or \"fexsend -I\"\n";
219 $fexcgi =~ s(^http://)()i;
220 $fexcgi =~ s(/fup.*)();
223 if ($server =~ s(^https://)()i) { $port = 443 }
224 elsif ($server =~ /:(\d+)/) { $port = $1 }
227 $server =~ s([:/].*)();
229 ## set up tcp/ip connection
230 # $iaddr = gethostbyname($server)
231 # or die "$0: cannot find ip-address for $server $!\n";
232 # socket(SH,PF_INET,SOCK_STREAM,getprotobyname('tcp')) or die "$0: socket $!\n";
233 # connect(SH,sockaddr_in($port,$iaddr)) or die "$0: connect $!\n";
234 # warn "connecting $server:$port user=$user\n";
236 if ($opt_v and %SSL) {
237 foreach my $v (keys %SSL) {
238 printf "%s => %s\n",$v,$SSL{$v};
241 eval "use IO::Socket::SSL";
242 die "$0: cannot load IO::Socket::SSL\n" if $@;
243 $SH = IO::Socket::SSL->new(
250 $SH = IO::Socket::INET->new(
257 die "cannot connect $server:$port - $!\n" unless $SH;
258 warn "TCPCONNECT to $server:$port\n" if $opt_v;
263 $SIG{PIPE} = \&sigpipehandler;
265 if ($0 eq 'sexget' or $0 eq 'fuckme') {
266 $stream = "&stream=" . shift if @ARGV;
269 } elsif ($id eq 'public') {
272 $cid = query_sid($server,$port,$id);
274 request("GET /sex?BS=$bs&user=$user&ID=$cid$stream HTTP/1.0");
275 transfer($SH,STDOUT);
276 # print while sysread $SH,$_,$bs;
281 $stream = "&stream=" . ($opt_s || 'xx');
284 open my $tar,'-|','tar',"cv${opt_c}f",'-',@ARGV or die "$0: cannot run tar - $!\n";
285 request("POST /sex?BS=$bs&user=$user$type$stream HTTP/1.0");
287 # while (read $tar,$_,$bs) { syswrite $SH,$_ }
289 $cid = query_sid($server,$port,$id);
290 request("GET /sex?BS=$bs&user=$user&ID=$cid$stream HTTP/1.0");
291 $opt_c = 'z' if $H{'CONTENT-TYPE'} =~ /gzip/i;
293 print "extracting from stream:\n";
294 open $out,"|tar xv${opt_c}f -" or die "$0: cannot run tar - $!\n";
297 open $out,"|gzip -d" or die "$0: cannot run gunzip - $!\n";
302 print {$out} $_ while sysread $SH,$_,$bs;
308 $stream = "&stream=" . shift if @ARGV;
310 if ($mode eq 'anonymous') {
312 print "http://$server:$port/sex?user=$user&ID=anonymous$stream\n";
313 printf "http://$server:$port/sex?%s\n",
314 encode_b64("user=$user&ID=anonymous$stream");
316 $mode = "&mode=anonymous";
317 } elsif ($mode eq 'public') {
318 die "$0: need user/ID when sending to public, set it with fexsend -I\n" unless $user and $id;
320 print "http://$server:$port/sex?user=$user&ID=public$stream\n";
321 printf "http://$server:$port/sex?%s\n",
322 encode_b64("user=$user&ID=public$stream");
324 $cid = query_sid($server,$port,$id);
325 $mode = "&ID=$cid&mode=public";
327 # $user = checkalias($user) unless $opt_d;
330 request("POST /sex?BS=$bs&user=$user$mode$type$timeout$stream HTTP/1.0");
331 print STDERR "==> (streaming ...)\n" if $opt_v;
340 my $destination = shift;
348 while ($b = sysread $source,$_,$bs) {
349 print {$destination} $_ or die "$0: link failure - $!\n";
356 printf STDERR "%d MB %d kB/s \r",
357 int($B/M),int($bt/k/($t2-$tt));
359 printf STDERR "%d kB %d kB/s \r",
360 int($B/k),int($bt/k/($t2-$tt));
365 sleep 1; # be nice to bandwith
372 die "$0: no stream data\n" unless $B;
376 if ($opt_v or $opt_g) {
378 printf STDERR "transfered: %d MB in %d s with %d kB/s\n",
379 int($B/1048576),$tt,int($B/1024/$tt);
381 printf STDERR "transfered: %d kB in %d s with %d kB/s\n",
382 int($B/1024),$tt,int($B/1024/$tt);
384 printf STDERR "transfered: %d B in %d s with %d kB/s\n",
385 $B,$tt,int($B/1024/$tt);
395 print STDERR "==> $req\n" if $opt_v;
396 syswrite $SH,"$req\r\n\r\n";
398 unless (defined($_ = &getline)) {
399 die "$0: server has closed the connection\n";
401 if (/^HTTP\/[\d\.]+ 200/) {
402 print STDERR "<== $_" if $opt_v;
404 } elsif (/^HTTP\/[\d\.]+ 199/) {
405 print STDERR "<== $_" if $opt_v;
408 print STDERR "<== $_";
413 die "$0: server response: $_";
417 while (defined($_ = &getline)) {
419 $H{uc($1)} = $2 if /(.+):\s*(.+)/;
420 print STDERR "<== $_" if $opt_v;
424 # check for (mutt) alias
427 if ($to !~ /@/ and open F,$ENV{HOME}.'/.mutt/aliases') {
430 if (/^alias $to\s/i) {
438 warn "$0: found alias, using address $to\n";
456 my ($server,$port,$id) = @_;
460 $req = "GET SID HTTP/1.1";
461 print STDERR "==> $req\n" if $opt_v;
462 syswrite $SH,"$req\r\n\r\n";
464 unless (defined $_ and /\w/) {
465 print STDERR "\n" if $opt_v;
466 die "$0: no response from server\n";
469 if (/^HTTP.* 201 (.+)/) {
470 print STDERR "<== $_" if $opt_v;
471 $id = 'MD5H:'.md5_hex($id.$1);
472 while (defined($_ = &getline)) {
475 print STDERR "<== $_" if $opt_v;
478 die "$0: $server does not support session ID\n";
485 $SIG{ALRM} = sub { };
488 if (/^HTTP.* \d+ (.*)/) {
490 die "\n$0: server error: @_\n";
492 die "\n$0: server error: $1\n";
495 die "\n$0: got SIGPIPE (server closed connection)\n";
499 # read one text line from $SH;
504 local $SIG{ALRM} = sub { die "$0: timeout while waiting for server reply\n" };
507 # must use sysread to avoid perl line buffering
508 while (sysread $SH,$c,1) {
518 # from MIME::Base64::Perl
527 return "" unless length;
530 for ($i = 0; $i <= $l; $i += 60) {
531 $uu .= "M" . substr($_,$i,60);
535 $uu .= chr(32 + (length)*3/4) . $_;
537 return unpack ("u",$uu);
541 ### common functions ###
545 my @d = localtime((stat shift)[9]);
546 return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
552 s/\%([a-f\d]{2})/chr(hex($1))/ige;
558 # set SSL/TLS options
559 $SSL{SSL_verify_mode} = $ENV{SSLVERIFY} if defined($ENV{SSLVERIFY});
569 $SSL{$opt} = $ENV{$env} if defined($ENV{$env});
572 if ($SSL{SSL_verify_mode}) {
574 unless ($SSL{SSL_ca_path} or $SSL{SSL_ca_file}) {
575 die "$0: \$SSLVERIFYMODE, but not valid \$SSLCAPATH or \$SSLCAFILE\n";
577 } elsif (defined($SSL{SSL_verify_mode})) {
578 # user has set SSLVERIFY=0 !
581 $SSL{SSL_verify_mode} = 1 if $SSL{SSL_ca_path} or $SSL{SSL_ca_file};
587 return if $SSL{SSL_ca_file} or $SSL{SSL_ca_path};
588 foreach (qw(/etc/ssl/certs/ca-certificates.crt)) {
590 $SSL{SSL_ca_file} = $_;
594 foreach (qw(/etc/ssl/certs /etc/pki/tls/certs)) {
596 $SSL{SSL_ca_path} = $_;
604 my ($server,$port) = @_;
605 my $connect = "CONNECT $server:$port HTTP/1.1";
608 if ($opt_v and $port == 443 and %SSL) {
609 foreach my $v (keys %SSL) {
610 printf "%s => %s\n",$v,$SSL{$v};
615 tcpconnect(split(':',$proxy));
617 printf "--> %s\n",$connect if $opt_v;
618 nvtsend($connect,"");
621 printf "<-- $_"if $opt_v;
622 unless (/^HTTP.1.. 200/) {
623 die "$0: proxy error : $_";
625 eval "use IO::Socket::SSL";
626 die "$0: cannot load IO::Socket::SSL\n" if $@;
627 $SH = IO::Socket::SSL->start_SSL($SH,%SSL);
630 tcpconnect($server,$port);
632 # if ($port == 443 and $opt_v) {
633 # printf "%s\n",$SH->get_cipher();
638 # set up tcp/ip connection
640 my ($server,$port) = @_;
648 # eval "use IO::Socket::SSL qw(debug3)";
649 eval "use IO::Socket::SSL";
650 die "$0: cannot load IO::Socket::SSL\n" if $@;
651 $SH = IO::Socket::SSL->new(
658 $SH = IO::Socket::INET->new(
668 die "$0: cannot connect $server:$port - $@\n";
671 print "TCPCONNECT to $server:$port\n" if $opt_v;
680 push @head,"Host: $sp";
682 foreach $head (@head) {
683 print "--> $head\n" if $opt_v;
684 print {$SH} $head,"\r\n";
686 print "-->\n" if $opt_v;
692 local $SIG{PIPE} = sub { $sigpipe = "@_" };
696 die "$0: internal error: no active network handle\n" unless $SH;
697 die "$0: remote host has closed the link\n" unless $SH->connected;
699 foreach my $line (@_) {
700 print {$SH} $line,"\r\n";
711 # from MIME::Base64::Perl
718 $res = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
719 $res =~ tr|` -_|AA-Za-z0-9+/|;
720 $padding = (3-length($_[0])%3)%3;
721 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;