| 1 | package IPC::Transit; | ||
| 2 | $IPC::Transit::VERSION = '1.161450'; | ||
| 3 | 16 | ▶ | use strict;use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 16 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 16 | ▶ | use IPC::Transit::Internal; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 16 | ▶ | use Storable; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 16 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 16 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 16 | ▶ | use Sereal::Encoder; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 16 | ▶ | use Sereal::Decoder qw(looks_like_sereal decode_sereal); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 16 | ▶ | use Sys::Hostname; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 16 | ▶ | use HTTP::Lite; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 16 | ▶ | use File::Temp qw/tempfile/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 16 | ▶ | use Tie::DNS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use Crypt::Sodium; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use MIME::Base64; | ||
| 17 | |||
| 18 | use vars qw( | ||
| 19 | $config_file $config_dir $large_transit_message_dir | ||
| 20 | $local_queues | ||
| 21 | ); | ||
| 22 | |||
| 23 | $IPC::Transit::my_keys = { | ||
| 24 | default => 'ftlMCefNymrF66r2VlFBgHYbWRZqSJPzVg4Vz/I86UQ=' | ||
| 25 | }; | ||
| 26 | $IPC::Transit::public_keys = { | ||
| 27 | default => 'vbqcxUUGIOvIKzpFWyBbYrSTsmSGj+/zlkF9H3tJ0DI=' | ||
| 28 | }; | ||
| 29 | |||
| 30 | our $large_transit_message_dir = '/tmp/transit_large_messages' | ||
| 31 | unless $IPC::Transit::large_transit_message_dir; | ||
| 32 | |||
| 33 | ##sorry, gotta have this temp dir in a known location | ||
| 34 | mkdir $large_transit_message_dir unless -d $large_transit_message_dir; | ||
| 35 | chmod 0777, $large_transit_message_dir; ##sorry, it has to be 0777 :( | ||
| 36 | |||
| 37 | our $wire_header_arg_translate = { | ||
| 38 | destination => 'd', | ||
| 39 | destination_qname => 'q', | ||
| 40 | compression => 'c', | ||
| 41 | serializer => 's', | ||
| 42 | message_length => 'l', | ||
| 43 | local_filename => 'f', | ||
| 44 | ttl => 't', | ||
| 45 | nonce => 'n', | ||
| 46 | source => 'S', | ||
| 47 | }; | ||
| 48 | our $max_message_size = 4096 unless $IPC::Transit::max_message_size; | ||
| 49 | |||
| 50 | { | ||
| 51 | my %dns; | ||
| 52 | my $cache; | ||
| 53 | my $ts; | ||
| 54 | sub cached_dns { | ||
| 55 | my $thing = shift; | ||
| 56 | return $thing unless $thing; | ||
| 57 | if(not $ts) { | ||
| 58 | $ts = time; | ||
| 59 | $cache = {}; | ||
| 60 | tie %dns, 'Tie::DNS' unless %dns; | ||
| 61 | } | ||
| 62 | if(time > $ts + 10) { | ||
| 63 | $ts = time; | ||
| 64 | $cache = {}; | ||
| 65 | } | ||
| 66 | my $ret = eval { | ||
| 67 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 68 | alarm 2; | ||
| 69 | return $dns{$thing}; | ||
| 70 | }; | ||
| 71 | alarm 0; | ||
| 72 | $ret = $thing unless $ret; | ||
| 73 | return $ret; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | #This validates some allowed values of wire header arguments | ||
| 78 | our $wire_header_args = { | ||
| 79 | s => { #serializer | ||
| 80 | json => 1, | ||
| 81 | sereal => 1, | ||
| 82 | yaml => 1, | ||
| 83 | storable => 1, | ||
| 84 | dumper => 1, | ||
| 85 | }, | ||
| 86 | c => { #compression | ||
| 87 | zlib => 1, | ||
| 88 | snappy => 1, | ||
| 89 | none => 1 | ||
| 90 | }, | ||
| 91 | d => 1, #destination address | ||
| 92 | t => 1, #hop TTL | ||
| 93 | q => 1, #destination qname | ||
| 94 | l => 1, #length of the message itself | ||
| 95 | f => 1, #local_filename, optionally a path on the filesystem where the message can be found | ||
| 96 | t => 1, #Time To Live | ||
| 97 | #for crypto | ||
| 98 | n => 1, #nonce | ||
| 99 | S => 1, #source | ||
| 100 | }; | ||
| 101 | our $std_args = { | ||
| 102 | message => 1, | ||
| 103 | qname => 1, | ||
| 104 | nowait => 1, | ||
| 105 | encrypt => 1, | ||
| 106 | }; | ||
| 107 | |||
| 108 | sub send { | ||
| 109 | my %args; | ||
| 110 | { my @args = @_; | ||
| 111 | die 'IPC::Transit::send: even number of arguments required' | ||
| 112 | if scalar @args % 2; | ||
| 113 | %args = @args; | ||
| 114 | } | ||
| 115 | my $qname = $args{qname}; | ||
| 116 | die "IPC::Transit::send: parameter 'qname' required" | ||
| 117 | unless $qname; | ||
| 118 | die "IPC::Transit::send: parameter 'qname' must be a scalar" | ||
| 119 | if ref $qname; | ||
| 120 | my $message = $args{message}; | ||
| 121 | die "IPC::Transit::send: parameter 'message' required" | ||
| 122 | unless $message; | ||
| 123 | die "IPC::Transit::send: parameter 'message' must be a HASH reference" | ||
| 124 | if ref $message ne 'HASH'; | ||
| 125 | $message->{'.ipc_transit_meta'} = {} unless $message->{'.ipc_transit_meta'}; | ||
| 126 | $message->{'.ipc_transit_meta'}->{send_ts} = time; | ||
| 127 | if($args{encrypt} and not $args{destination}) { | ||
| 128 | die "IPC::Transit::send: parameter 'destination' must exist if encryption is selected"; | ||
| 129 | } | ||
| 130 | if($args{destination}) { | ||
| 131 | #let's take a stab at efficiently getting destination to either an | ||
| 132 | #IP address or a FQDN | ||
| 133 | #If destination has less then tree .'s in it, then we will do a DNS | ||
| 134 | #lookup on it, and if that returns anything, replace it | ||
| 135 | if(not $args{no_dns_normalize}) { | ||
| 136 | if($args{destination} =~ tr/\./\./ < 3) { | ||
| 137 | my $new = cached_dns($args{destination}); | ||
| 138 | $args{destination} = $new if $new; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | $args{destination_qname} = $args{qname}; | ||
| 142 | $args{qname} = 'transitd'; | ||
| 143 | $args{ttl} = '9' unless $args{ttl}; | ||
| 144 | |||
| 145 | return _deliver_non_local($qname, \%args); | ||
| 146 | } | ||
| 147 | |||
| 148 | #begin the hard work of figuring out if this message should be sent as | ||
| 149 | #local delivery or not. | ||
| 150 | #overall default is to non-local delivery | ||
| 151 | |||
| 152 | #the overrides in .ipc_transit_meta in the message takes precidence | ||
| 153 | #over previous calls to ::local_queue and/or ::no_local_queue | ||
| 154 | |||
| 155 | #insides of overrides, the force_local and force_non_local | ||
| 156 | #take precidence over the default_to. | ||
| 157 | |||
| 158 | #algo | ||
| 159 | #1. absolute override goes to the invocation: override_local/ | ||
| 160 | # override_non_local | ||
| 161 | #2. next, look at force_* in the message. If they conflict, then we go | ||
| 162 | # with force_non_local. | ||
| 163 | #3. lacking any instructions there, we go with the default_to directive, | ||
| 164 | # if any, in the message | ||
| 165 | #4. lacking that, we go with what's been set with ::local_queue and/or | ||
| 166 | # ::no_local_queue | ||
| 167 | #5. And finally, non-local delivery | ||
| 168 | |||
| 169 | |||
| 170 | #1a: | ||
| 171 | return _deliver_non_local($qname, \%args) if $args{override_local}; | ||
| 172 | |||
| 173 | #1b: | ||
| 174 | return _deliver_local($qname, \%args) if $args{override_non_local}; | ||
| 175 | |||
| 176 | #2a: | ||
| 177 | if( $message->{'.ipc_transit_meta'}->{overrides} and | ||
| 178 | $message->{'.ipc_transit_meta'}->{overrides}->{force_non_local} and | ||
| 179 | $message->{'.ipc_transit_meta'}->{overrides}->{force_non_local}->{$args{qname}}) { | ||
| 180 | return _deliver_non_local($qname, \%args); | ||
| 181 | } | ||
| 182 | |||
| 183 | #2b: | ||
| 184 | if( $message->{'.ipc_transit_meta'}->{overrides} and | ||
| 185 | $message->{'.ipc_transit_meta'}->{overrides}->{force_local} and | ||
| 186 | $message->{'.ipc_transit_meta'}->{overrides}->{force_local}->{$args{qname}}) { | ||
| 187 | return _deliver_local($qname, \%args); | ||
| 188 | } | ||
| 189 | |||
| 190 | #3a: | ||
| 191 | if( $message->{'.ipc_transit_meta'}->{overrides} and | ||
| 192 | $message->{'.ipc_transit_meta'}->{overrides}->{default_to} and | ||
| 193 | $message->{'.ipc_transit_meta'}->{overrides}->{default_to} eq 'local' | ||
| 194 | ) { | ||
| 195 | return _deliver_local($qname, \%args); | ||
| 196 | } | ||
| 197 | |||
| 198 | #3b: | ||
| 199 | if( $message->{'.ipc_transit_meta'}->{overrides} and | ||
| 200 | $message->{'.ipc_transit_meta'}->{overrides}->{default_to} and | ||
| 201 | $message->{'.ipc_transit_meta'}->{overrides}->{default_to} eq 'non-local' | ||
| 202 | ) { | ||
| 203 | return _deliver_non_local($qname, \%args); | ||
| 204 | } | ||
| 205 | |||
| 206 | #4: | ||
| 207 | if( $local_queues and | ||
| 208 | $local_queues->{$qname}) { | ||
| 209 | return _deliver_local($qname, \%args); | ||
| 210 | } | ||
| 211 | |||
| 212 | #5: | ||
| 213 | return _deliver_non_local($qname, \%args); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub _deliver_local { | ||
| 217 | my ($qname, $args) = @_; | ||
| 218 | push @{$local_queues->{$qname}}, $args; | ||
| 219 | return $args; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _get_tmp_file { | ||
| 223 | my ($fh, $filename) = tempfile(SUFFIX => '.transit', DIR => $large_transit_message_dir); | ||
| 224 | die 'failed to create tmpfile' unless -e $filename; | ||
| 225 | return ($fh, $filename); | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | sub _deliver_non_local { | ||
| 230 | my ($qname, $args) = @_; | ||
| 231 | my $to_queue = IPC::Transit::Internal::_initialize_queue(%$args); | ||
| 232 | eval { | ||
| 233 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 234 | alarm 2; | ||
| 235 | pack_message($args); | ||
| 236 | }; | ||
| 237 | alarm 0; | ||
| 238 | if($@) { | ||
| 239 | print STDERR "IPC::Transit::_deliver_non_local: pack_message failed: $@\n"; | ||
| 240 | unlink $args->{local_filename} | ||
| 241 | if $args->{local_filename} and -e $args->{local_filename}; | ||
| 242 | return undef; | ||
| 243 | } | ||
| 244 | my $ret = $to_queue->snd(1,$args->{serialized_wire_data}, IPC::Transit::Internal::_get_flags('nonblock')); | ||
| 245 | unlink $args->{local_filename} | ||
| 246 | if not $ret and $args->{local_filename}; | ||
| 247 | return $ret; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub stats { | ||
| 251 | my $info = IPC::Transit::Internal::_stats(); | ||
| 252 | return $info; | ||
| 253 | } | ||
| 254 | sub stat { | ||
| 255 | my %args; | ||
| 256 | { my @args = @_; | ||
| 257 | die 'IPC::Transit::stat: even number of arguments required' | ||
| 258 | if scalar @args % 2; | ||
| 259 | %args = @args; | ||
| 260 | } | ||
| 261 | my $qname = $args{qname}; | ||
| 262 | if(not $args{override_local} and $local_queues and $local_queues->{$qname}) { | ||
| 263 | return { | ||
| 264 | qnum => scalar @{$local_queues->{$qname}} | ||
| 265 | }; | ||
| 266 | } | ||
| 267 | die "IPC::Transit::stat: parameter 'qname' required" | ||
| 268 | unless $qname; | ||
| 269 | die "IPC::Transit::stat: parameter 'qname' must be a scalar" | ||
| 270 | if ref $qname; | ||
| 271 | my $info = IPC::Transit::Internal::_stat(%args); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub receive { | ||
| 275 | my %args; | ||
| 276 | { my @args = @_; | ||
| 277 | die 'IPC::Transit::receive: even number of arguments required' | ||
| 278 | if scalar @args % 2; | ||
| 279 | %args = @args; | ||
| 280 | } | ||
| 281 | my $qname = $args{qname}; | ||
| 282 | |||
| 283 | die "IPC::Transit::receive: parameter 'qname' required" | ||
| 284 | unless $qname; | ||
| 285 | die "IPC::Transit::receive: parameter 'qname' must be a scalar" | ||
| 286 | if ref $qname; | ||
| 287 | if( not $args{override_local} and | ||
| 288 | $local_queues and | ||
| 289 | $local_queues->{$qname}) { | ||
| 290 | my $m = shift @{$local_queues->{$qname}}; | ||
| 291 | return $m->{message}; | ||
| 292 | } | ||
| 293 | my $ret = eval { | ||
| 294 | my $flags = IPC::Transit::Internal::_get_flags('nowait') if $args{nonblock}; | ||
| 295 | my $from_queue = IPC::Transit::Internal::_initialize_queue(%args); | ||
| 296 | my $ref = { #just doing this so we can pass the possibly big serialized | ||
| 297 | #data around as a reference | ||
| 298 | serialized_wire_data => '', | ||
| 299 | }; | ||
| 300 | if(not $from_queue->rcv($ref->{serialized_wire_data}, 102400000, 0, $flags)) { | ||
| 301 | return undef; | ||
| 302 | } | ||
| 303 | if(not defined $ref->{serialized_wire_data}) { | ||
| 304 | print STDERR "IPC::Transit::receive: received message had no data"; | ||
| 305 | return undef; | ||
| 306 | } | ||
| 307 | |||
| 308 | my ($header_length, $wire_headers) = _parse_wire_header($ref); | ||
| 309 | if(not defined $wire_headers) { | ||
| 310 | print STDERR 'IPC::Transit::receive: received message had no wire headers: ' . substr($ref->{serialized_wire_data}, 0, 30) . "\n"; | ||
| 311 | return undef; | ||
| 312 | } | ||
| 313 | if(not defined $header_length) { | ||
| 314 | print STDERR 'IPC::Transit::receive: received message had no header length: ' . substr($ref->{serialized_wire_data}, 0, 30) . "\n"; | ||
| 315 | return undef; | ||
| 316 | } | ||
| 317 | sync_serialized_wire_data($wire_headers, $ref); | ||
| 318 | |||
| 319 | my $message = { | ||
| 320 | wire_headers => $wire_headers, | ||
| 321 | serialized_message => substr( | ||
| 322 | $ref->{serialized_wire_data}, | ||
| 323 | $header_length + length($header_length) + 1, | ||
| 324 | 9999999, # :( | ||
| 325 | ), | ||
| 326 | }; | ||
| 327 | my $used_default_public = 1; | ||
| 328 | if($message->{wire_headers}->{n}) { | ||
| 329 | #we be encrypted | ||
| 330 | #validate $IPC::Transit::my_keys->{private} | ||
| 331 | # | ||
| 332 | my $source = $message->{wire_headers}->{S}; | ||
| 333 | my $public_key; | ||
| 334 | if($IPC::Transit::public_keys->{$source}) { | ||
| 335 | $public_key = $IPC::Transit::public_keys->{$source}; | ||
| 336 | $used_default_public = 0; | ||
| 337 | } else { | ||
| 338 | $public_key = $IPC::Transit::public_keys->{default}; | ||
| 339 | } | ||
| 340 | my @private_keys = ($IPC::Transit::my_keys->{default}); | ||
| 341 | push @private_keys, $IPC::Transit::my_keys->{private} | ||
| 342 | if $IPC::Transit::my_keys->{private}; | ||
| 343 | my $nonce = decode_base64($message->{wire_headers}->{n}); | ||
| 344 | my $public_keys; | ||
| 345 | if(not ref $public_key) { | ||
| 346 | $public_keys = [$public_key]; | ||
| 347 | } else { | ||
| 348 | $public_keys = $public_key; | ||
| 349 | } | ||
| 350 | push @$public_keys, $IPC::Transit::public_keys->{default}; | ||
| 351 | my $cleartext; | ||
| 352 | PUBLIC: | ||
| 353 | foreach my $public (@$public_keys) { | ||
| 354 | foreach my $private_key (@private_keys) { | ||
| 355 | $cleartext = crypto_box_open( | ||
| 356 | $message->{serialized_message}, | ||
| 357 | $nonce, | ||
| 358 | decode_base64($public), | ||
| 359 | decode_base64($private_key), | ||
| 360 | ); | ||
| 361 | last PUBLIC if $cleartext; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | $message->{serialized_message} = $cleartext; | ||
| 365 | } | ||
| 366 | return undef unless _thaw($message); | ||
| 367 | $message->{message}->{'.ipc_transit_meta'}->{encrypt_source} = | ||
| 368 | $message->{wire_headers}->{S} if $message->{wire_headers}->{S}; | ||
| 369 | $message->{message}->{'.ipc_transit_meta'}->{encrypt_source} = 'default' | ||
| 370 | if $used_default_public; | ||
| 371 | return $message if $args{raw}; | ||
| 372 | return $message->{message}; | ||
| 373 | }; | ||
| 374 | die $@ if $@; | ||
| 375 | return $ret; | ||
| 376 | } | ||
| 377 | |||
| 378 | sub sync_serialized_wire_data { | ||
| 379 | my ($wire_headers, $ref) = @_; | ||
| 380 | if($wire_headers->{f} and -r $wire_headers->{f}) { | ||
| 381 | eval { | ||
| 382 | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||
| 383 | alarm 5; | ||
| 384 | open my $fh, '<', $wire_headers->{f} | ||
| 385 | or die "failed to open $wire_headers->{f} for reading: $!"; | ||
| 386 | read $fh, $ref->{serialized_wire_data}, 1024000000 | ||
| 387 | or die "failed to read from $wire_headers->{f}: $!"; | ||
| 388 | close $fh or die "failed to close $wire_headers->{f}: $!"; | ||
| 389 | }; | ||
| 390 | alarm 0; | ||
| 391 | unlink $wire_headers->{f}; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | sub post_remote { | ||
| 396 | #This is very simple, first-generation logic. It assumes that every | ||
| 397 | #message that is received that has a qname set is destined for off box. | ||
| 398 | |||
| 399 | #so here, we want to post this message to the destination over http | ||
| 400 | my $message = shift; | ||
| 401 | my $http = HTTP::Lite->new; | ||
| 402 | my $vars = { | ||
| 403 | message => $message->{serialized_wire_data}, | ||
| 404 | }; | ||
| 405 | $http->prepare_post($vars); | ||
| 406 | my $url = 'http://' . $message->{message}->{'.ipc_transit_meta'}->{destination} . ':9816/message'; | ||
| 407 | my $req; | ||
| 408 | eval { | ||
| 409 | $req = $http->request($url) | ||
| 410 | or die "Unable to get document: $!"; | ||
| 411 | }; | ||
| 412 | print STDERR "IPC::Transit::post_remote: (\$url=$url) failed: $@\n" if $@; | ||
| 413 | return $req; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub no_local_queue { | ||
| 417 | my %args; | ||
| 418 | { my @args = @_; | ||
| 419 | die 'IPC::Transit::no_local_queue: even number of arguments required' | ||
| 420 | if scalar @args % 2; | ||
| 421 | %args = @args; | ||
| 422 | } | ||
| 423 | my $qname = $args{qname}; | ||
| 424 | delete $local_queues->{$qname}; | ||
| 425 | return 1; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub queue_exists { | ||
| 429 | my $qname = shift; | ||
| 430 | return IPC::Transit::Internal::_queue_exists($qname); | ||
| 431 | } | ||
| 432 | |||
| 433 | sub _parse_wire_header { | ||
| 434 | my $ref = shift; | ||
| 435 | if($ref->{serialized_wire_data} !~ /^(\d+)/sm) { | ||
| 436 | print STDERR 'IPC::Transit::_parse_wire_header: malformed message received: ' . substr($ref->{serialized_wire_data}, 0, 60) . "\n"; | ||
| 437 | return (undef, undef); | ||
| 438 | } | ||
| 439 | my $header_length = $1; | ||
| 440 | return ( | ||
| 441 | $header_length, | ||
| 442 | deserialize_wire_meta( | ||
| 443 | substr( $ref->{serialized_wire_data}, | ||
| 444 | length($header_length) + 1, | ||
| 445 | $header_length | ||
| 446 | ) | ||
| 447 | ), | ||
| 448 | ); | ||
| 449 | } | ||
| 450 | sub local_queue { | ||
| 451 | my %args; | ||
| 452 | { my @args = @_; | ||
| 453 | die 'IPC::Transit::local_queue: even number of arguments required' | ||
| 454 | if scalar @args % 2; | ||
| 455 | %args = @args; | ||
| 456 | } | ||
| 457 | my $qname = $args{qname}; | ||
| 458 | $local_queues = {} unless $local_queues; | ||
| 459 | $local_queues->{$qname} = [] unless $local_queues->{$qname}; | ||
| 460 | return 1; | ||
| 461 | } | ||
| 462 | |||
| 463 | sub pack_message { | ||
| 464 | my $args = shift; | ||
| 465 | $args->{message}->{'.ipc_transit_meta'} = {} | ||
| 466 | unless $args->{message}->{'.ipc_transit_meta'}; | ||
| 467 | foreach my $key (keys %$wire_header_arg_translate) { | ||
| 468 | next unless $args->{$key}; | ||
| 469 | $args->{$wire_header_arg_translate->{$key}} = $args->{$key}; | ||
| 470 | } | ||
| 471 | foreach my $key (keys %$args) { | ||
| 472 | next if $wire_header_args->{$key}; | ||
| 473 | next if $std_args->{$key}; | ||
| 474 | $args->{message}->{'.ipc_transit_meta'}->{$key} = $args->{$key}; | ||
| 475 | } | ||
| 476 | if($args->{encrypt}) { | ||
| 477 | $args->{message}->{'.ipc_transit_meta'}->{destination} = $args->{destination}; | ||
| 478 | } | ||
| 479 | $args->{message}->{'.ipc_transit_meta'}->{source_hostname} = _get_my_hostname(); | ||
| 480 | if($args->{encrypt}) { | ||
| 481 | my $sender = _get_my_hostname(); | ||
| 482 | if(not $sender) { | ||
| 483 | die 'encrypt selected but unable to determine hostname. Set $IPC::Transit::my_hostname to override'; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | if($args->{encrypt}) { | ||
| 487 | my $nonce = crypto_box_nonce(); | ||
| 488 | $args->{nonce} = encode_base64($nonce); | ||
| 489 | |||
| 490 | my $my_private_key; | ||
| 491 | if($IPC::Transit::my_keys->{private}) { | ||
| 492 | $my_private_key = $IPC::Transit::my_keys->{private}; | ||
| 493 | $args->{message}->{'.ipc_transit_meta'}->{signed_destination} = 'my_private'; | ||
| 494 | } else { | ||
| 495 | $my_private_key = $IPC::Transit::my_keys->{default}; | ||
| 496 | $args->{message}->{'.ipc_transit_meta'}->{signed_destination} = 'default'; | ||
| 497 | } | ||
| 498 | my $their_public_key; | ||
| 499 | if($IPC::Transit::public_keys->{$args->{destination}}) { | ||
| 500 | $their_public_key = $IPC::Transit::public_keys->{$args->{destination}}; | ||
| 501 | } else { | ||
| 502 | $their_public_key = $IPC::Transit::public_keys->{default}; | ||
| 503 | } | ||
| 504 | $args->{serialized_message} = _freeze($args); | ||
| 505 | my $cipher_text = crypto_box( | ||
| 506 | $args->{serialized_message}, | ||
| 507 | $nonce, | ||
| 508 | decode_base64($their_public_key), | ||
| 509 | decode_base64($my_private_key) | ||
| 510 | ); | ||
| 511 | $args->{serialized_message} = $cipher_text; | ||
| 512 | $args->{source} = _get_my_hostname(); | ||
| 513 | } else { | ||
| 514 | $args->{serialized_message} = _freeze($args); | ||
| 515 | } | ||
| 516 | $args->{message_length} = length $args->{serialized_message}; | ||
| 517 | if($args->{message_length} > $IPC::Transit::max_message_size) { | ||
| 518 | my $s; | ||
| 519 | eval { | ||
| 520 | my $fh; | ||
| 521 | ($fh, $args->{local_filename}) = _get_tmp_file(); | ||
| 522 | $s = serialize_wire_meta($args); | ||
| 523 | print $fh "$s$args->{serialized_message}" | ||
| 524 | or die "failed to write to file $args->{local_filename}: $!"; | ||
| 525 | close $fh or die "failed to close $args->{local_filename}: $!"; | ||
| 526 | chmod 0666, $args->{local_filename}; | ||
| 527 | }; | ||
| 528 | if($@) { | ||
| 529 | unlink $args->{local_filename}; | ||
| 530 | die "IPC::Transit::pack_message: failed: $@"; | ||
| 531 | } | ||
| 532 | $args->{serialized_wire_data} = $s; | ||
| 533 | return; | ||
| 534 | } | ||
| 535 | my $s = serialize_wire_meta($args); | ||
| 536 | $args->{serialized_wire_data} = "$s$args->{serialized_message}"; | ||
| 537 | return; | ||
| 538 | } | ||
| 539 | |||
| 540 | sub serialize_wire_meta { | ||
| 541 | my $args = shift; | ||
| 542 | my $s = ''; | ||
| 543 | foreach my $key (keys %$args) { | ||
| 544 | my $translated_key = $wire_header_arg_translate->{$key}; | ||
| 545 | if($translated_key and $wire_header_args->{$translated_key}) { | ||
| 546 | if($wire_header_args->{$translated_key} == 1) { | ||
| 547 | $s = "$s$translated_key=$args->{$key},"; | ||
| 548 | } elsif($wire_header_args->{$translated_key}->{$args->{$key}}) { | ||
| 549 | $s = "$s$translated_key=$args->{$key},"; | ||
| 550 | } else { | ||
| 551 | die "passed wire argument $translated_key had value of $args->{$translated_key} not of allowed type"; | ||
| 552 | } | ||
| 553 | } | ||
| 554 | } | ||
| 555 | chop $s; #no trailing , | ||
| 556 | my $l = length $s; | ||
| 557 | return "$l:$s"; | ||
| 558 | } | ||
| 559 | |||
| 560 | sub deserialize_wire_meta { | ||
| 561 | my $header = shift; | ||
| 562 | my $ret = {}; | ||
| 563 | foreach my $part (split ',', $header) { | ||
| 564 | my ($key, $val) = split '=', $part; | ||
| 565 | $ret->{$key} = $val; | ||
| 566 | } | ||
| 567 | return $ret; | ||
| 568 | } | ||
| 569 | |||
| 570 | { | ||
| 571 | my $encoder; | ||
| 572 | sub _freeze { | ||
| 573 | my $args = shift; | ||
| 574 | $encoder = Sereal::Encoder->new() unless $encoder; | ||
| 575 | if(not defined $args->{serializer} and $ENV{IPC_TRANSIT_DEFAULT_SERIALIZER}) { | ||
| 576 | $args->{serializer} = $ENV{IPC_TRANSIT_DEFAULT_SERIALIZER}; | ||
| 577 | } | ||
| 578 | if(not defined $args->{serializer} or $args->{serializer} eq 'json') { | ||
| 579 | return encode_json $args->{message}; | ||
| 580 | } elsif($args->{serializer} eq 'sereal') { | ||
| 581 | return $encoder->encode($args->{message}); | ||
| 582 | } elsif($args->{serializer} eq 'dumper') { | ||
| 583 | return Data::Dumper::Dumper $args->{message}; | ||
| 584 | } elsif($args->{serializer} eq 'storable') { | ||
| 585 | return Storable::freeze $args->{message}; | ||
| 586 | } else { | ||
| 587 | die "_freeze: undefined serializer: $args->{serializer}"; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | sub _thaw { | ||
| 593 | my $args = shift; | ||
| 594 | my $ret = eval { | ||
| 595 | die 'passed serialized_message is falsy' | ||
| 596 | unless $args->{serialized_message}; | ||
| 597 | if(not defined $args->{wire_headers}->{s} or $args->{wire_headers}->{s} eq 'sereal') { | ||
| 598 | if(looks_like_sereal($args->{serialized_message})) { | ||
| 599 | return $args->{message} = decode_sereal($args->{serialized_message}); | ||
| 600 | } else { | ||
| 601 | return $args->{message} = decode_json($args->{serialized_message}); | ||
| 602 | } | ||
| 603 | } elsif($args->{wire_headers}->{s} eq 'json') { | ||
| 604 | return $args->{message} = decode_json($args->{serialized_message}); | ||
| 605 | } elsif($args->{wire_headers}->{s} eq 'dumper') { | ||
| 606 | our $VAR1; | ||
| 607 | eval $args->{serialized_message}; | ||
| 608 | return $args->{message} = $VAR1; | ||
| 609 | } elsif($args->{wire_headers}->{s} eq 'storable') { | ||
| 610 | return $args->{message} = Storable::thaw($args->{serialized_message}); | ||
| 611 | } else { | ||
| 612 | die "undefined serializer: $args->{wire_headers}->{s}"; | ||
| 613 | } | ||
| 614 | }; | ||
| 615 | my $err = $@; | ||
| 616 | if($err) { | ||
| 617 | if($args->{serialized_message}) { | ||
| 618 | print STDERR "_thaw: failed: $err: $args->{serialized_message}\n"; | ||
| 619 | } else { | ||
| 620 | print STDERR "_thaw: failed: $err: <undef>\n"; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | return $ret; | ||
| 624 | } | ||
| 625 | |||
| 626 | sub gen_key_pair { | ||
| 627 | my ($pk, $sk) = box_keypair(); | ||
| 628 | return (encode_base64($pk),encode_base64($sk)); | ||
| 629 | } | ||
| 630 | |||
| 631 | { | ||
| 632 | my $hostname; | ||
| 633 | sub _get_my_hostname { | ||
| 634 | return $IPC::Transit::my_hostname if $IPC::Transit::my_hostname; | ||
| 635 | return $hostname if $hostname; | ||
| 636 | { my $ret = `hostname -f 2> /dev/null`; | ||
| 637 | chomp $ret; | ||
| 638 | if(length($ret) > 5) { | ||
| 639 | $hostname = $ret; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | $hostname = hostname unless $hostname; | ||
| 643 | return $hostname; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | 1; | ||
| 647 | |||
| 648 | __END__ | ||