| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2009 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2009-Mar-30 13:22 (EDT) | ||
| 6 | # Function: read protocol data | ||
| 7 | # | ||
| 8 | # $Id: Protocol.pm,v 1.13 2010/12/07 19:11:40 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::Yenta::Protocol; | ||
| 11 | use AC::Yenta::Debug 'protocol'; | ||
| 12 | use AC::Yenta::Config; | ||
| 13 | use AC::DC::Protocol; | ||
| 14 | use AC::Yenta::MySelf; | ||
| 15 | use AC::Yenta::Crypto; | ||
| 16 | use AC::Misc; | ||
| 17 | use AC::Import; | ||
| 18 | use strict; | ||
| 19 | |||
| 20 | require 'AC/protobuf/heartbeat.pl'; | ||
| 21 | require 'AC/protobuf/auth.pl'; | ||
| 22 | require 'AC/protobuf/std_reply.pl'; | ||
| 23 | require 'AC/protobuf/yenta_status.pl'; | ||
| 24 | require 'AC/protobuf/yenta_check.pl'; | ||
| 25 | require 'AC/protobuf/yenta_getset.pl'; | ||
| 26 | |||
| 27 | |||
| 28 | our @ISA = 'AC::DC::Protocol'; | ||
| 29 | our @EXPORT = qw(read_protocol use_encryption); | ||
| 30 | my $HDRSIZE = __PACKAGE__->header_size(); | ||
| 31 | |||
| 32 | my %MSGTYPE = | ||
| 33 | ( | ||
| 34 | heartbeat_request => { num => 2, reqc => '', resc => 'ACPHeartBeat' }, | ||
| 35 | |||
| 36 | yenta_status => { num => 6, reqc => 'ACPYentaStatusRequest', resc => 'ACPYentaStatusReply' }, | ||
| 37 | yenta_get => { num => 7, reqc => 'ACPYentaGetSet', resc => 'ACPYentaGetSet' }, | ||
| 38 | yenta_distrib => { num => 8, reqc => 'ACPYentaDistRequest', resc => 'ACPYentaDistReply' }, | ||
| 39 | yenta_check => { num => 9, reqc => 'ACPYentaCheckRequest', resc => 'ACPYentaCheckReply' }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | for my $name (keys %MSGTYPE){ | ||
| 44 | my $r = $MSGTYPE{$name}; | ||
| 45 | __PACKAGE__->add_msg( $name, $r->{num}, $r->{reqc}, $r->{resc}); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub read_protocol { | ||
| 50 | my $me = shift; | ||
| 51 | my $io = shift; | ||
| 52 | my $evt = shift; | ||
| 53 | |||
| 54 | $io->{rbuffer} .= $evt->{data}; | ||
| 55 | |||
| 56 | return _read_http($io, $evt) if $io->{rbuffer} =~ /^GET/; | ||
| 57 | |||
| 58 | if( length($io->{rbuffer}) >= $HDRSIZE && !$io->{proto_header} ){ | ||
| 59 | # decode header | ||
| 60 | eval { | ||
| 61 | $io->{proto_header} = $me->decode_header( $io->{rbuffer} ); | ||
| 62 | }; | ||
| 63 | if(my $e=$@){ | ||
| 64 | verbose("cannot decode protocol header: $e"); | ||
| 65 | $io->run_callback('error', { | ||
| 66 | cause => 'read', | ||
| 67 | error => "cannot decode protocol: $e", | ||
| 68 | }); | ||
| 69 | $io->shut(); | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | my $p = $io->{proto_header}; | ||
| 75 | return unless $p; # read more | ||
| 76 | |||
| 77 | # do we have everything? | ||
| 78 | return unless length($io->{rbuffer}) >= ($p->{auth_length} + $p->{data_length} + $p->{content_length} + $HDRSIZE); | ||
| 79 | |||
| 80 | my $auth = substr($io->{rbuffer}, $HDRSIZE, $p->{auth_length}); | ||
| 81 | my $data = substr($io->{rbuffer}, $HDRSIZE + $p->{auth_length}, $p->{data_length}); | ||
| 82 | my $content = substr($io->{rbuffer}, $HDRSIZE + $p->{auth_length} + $p->{data_length}, $p->{content_length}); | ||
| 83 | |||
| 84 | # RSN - validate auth | ||
| 85 | |||
| 86 | if( $p->{data_encrypted} && $data ){ | ||
| 87 | $data = $me->_decrypt_data( $io, $auth, $data ); | ||
| 88 | return unless $data; | ||
| 89 | } | ||
| 90 | |||
| 91 | if( $p->{content_encrypted} && $content ){ | ||
| 92 | $content = $me->_decrypt_data( $io, $auth, $content ); | ||
| 93 | return unless $content; | ||
| 94 | } | ||
| 95 | |||
| 96 | # content is passed as reference | ||
| 97 | return ($p, $data, ($content ? \$content : undef)); | ||
| 98 | } | ||
| 99 | |||
| 100 | # for simple status queries, argus, debugging | ||
| 101 | # this is not an RFC compliant http server | ||
| 102 | sub _read_http { | ||
| 103 | my $io = shift; | ||
| 104 | my $evt = shift; | ||
| 105 | |||
| 106 | return unless $io->{rbuffer} =~ /\r?\n\r?\n/s; | ||
| 107 | my($get, $url, $http) = split /\s+/, $io->{rbuffer}; | ||
| 108 | |||
| 109 | return ( { type => 'http', method => $get }, $url ); | ||
| 110 | } | ||
| 111 | |||
| 112 | ################################################################ | ||
| 113 | |||
| 114 | sub _decrypt_data { | ||
| 115 | my $me = shift; | ||
| 116 | my $io = shift; | ||
| 117 | my $auth = shift; | ||
| 118 | my $data = shift; | ||
| 119 | |||
| 120 | eval { | ||
| 121 | $data = $me->decrypt( $auth, $data ); | ||
| 122 | }; | ||
| 123 | if(my $e=$@){ | ||
| 124 | verbose("cannot decrypt protocol data: $e"); | ||
| 125 | $io->run_callback('error', { | ||
| 126 | cause => 'read', | ||
| 127 | error => "cannot decrypt protocol: $e", | ||
| 128 | }); | ||
| 129 | $io->shut(); | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | return $data; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub use_encryption { | ||
| 137 | my $peer = shift; | ||
| 138 | |||
| 139 | return unless conf_value('secret'); | ||
| 140 | # only encrypt far-away traffic, not local | ||
| 141 | return $peer->{datacenter} ne my_datacenter(); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub encrypt { | ||
| 145 | my $me = shift; | ||
| 146 | my $auth = shift; # not currently used | ||
| 147 | my $buf = shift; | ||
| 148 | |||
| 149 | my $secret = $me->{secret}; | ||
| 150 | return $buf unless $secret; | ||
| 151 | return unless $buf; | ||
| 152 | my $crypto = AC::Yenta::Crypto->new( $secret ); | ||
| 153 | return $crypto->encrypt( $buf ); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub decrypt { | ||
| 157 | my $me = shift; | ||
| 158 | my $abuf = shift; # not currently used | ||
| 159 | my $buf = shift; | ||
| 160 | |||
| 161 | my $secret = $me->{secret}; | ||
| 162 | return $buf unless $secret; | ||
| 163 | return unless $buf; | ||
| 164 | my $crypto = AC::Yenta::Crypto->new( $secret ); | ||
| 165 | return $crypto->decrypt( $buf ); | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; |