| 1 | # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify | ||
| 4 | # it under the terms of the GNU General Public License as published by | ||
| 5 | # the Free Software Foundation, either version 3 of the License, or | ||
| 6 | # (at your option) any later version. | ||
| 7 | # | ||
| 8 | # This program is distributed in the hope that it will be useful, | ||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | # GNU General Public License for more details. | ||
| 12 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | |||
| 16 | package SMB::v2::Command::Negotiate; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use parent 'SMB::v2::Command'; | ||
| 22 | |||
| 23 | sub new_from_v1 ($$) { | ||
| 24 | my $class = shift; | ||
| 25 | my $command1 = shift; | ||
| 26 | |||
| 27 | my $header1 = $command1->header; | ||
| 28 | |||
| 29 | my $flags = $header1->{flags} & SMB::v1::Header::FLAGS_RESPONSE ? SMB::v2::Header::FLAGS_RESPONSE : 0; | ||
| 30 | my $security_mode = | ||
| 31 | ($header1->{flags2} & SMB::v1::Header::FLAGS2_SECURITY_SIGNATURE ? 1 : 0) | | ||
| 32 | ($header1->{flags2} & SMB::v1::Header::FLAGS2_SECURITY_SIGNATURE_REQUIRED ? 2 : 0); | ||
| 33 | |||
| 34 | my $header = SMB::v2::Header->new( | ||
| 35 | code => 0, | ||
| 36 | mid => 0, | ||
| 37 | flags => $flags, | ||
| 38 | status => $command1->status, | ||
| 39 | ); | ||
| 40 | |||
| 41 | my $self = $class->SUPER::new($header); | ||
| 42 | |||
| 43 | $self->set( | ||
| 44 | security_mode => $security_mode, | ||
| 45 | dialects => [ 0x0202, 0x02ff ], | ||
| 46 | ); | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub init ($) { | ||
| 52 | $_[0]->set( | ||
| 53 | dialects => [ 0x0202 ], | ||
| 54 | dialect => 0x0202, | ||
| 55 | security_mode => 0, | ||
| 56 | capabilities => 0x7, | ||
| 57 | guid => "\5" x 16, | ||
| 58 | max_transact_size => 1 << 20, | ||
| 59 | max_read_size => 1 << 16, | ||
| 60 | max_write_size => 1 << 16, | ||
| 61 | current_time => 0, | ||
| 62 | boot_time => 0, | ||
| 63 | security_buffer => undef, | ||
| 64 | ) | ||
| 65 | } | ||
| 66 | |||
| 67 | sub parse ($$%) { | ||
| 68 | my $self = shift; | ||
| 69 | my $parser = shift; | ||
| 70 | |||
| 71 | if ($self->is_response) { | ||
| 72 | $self->security_mode($parser->uint16); | ||
| 73 | $self->dialect($parser->uint16); | ||
| 74 | $parser->uint16; # reserved | ||
| 75 | $self->guid($parser->bytes(16)); | ||
| 76 | $self->capabilities($parser->uint32); | ||
| 77 | $self->max_transact_size($parser->uint32); | ||
| 78 | $self->max_read_size($parser->uint32); | ||
| 79 | $self->max_write_size($parser->uint32); | ||
| 80 | $self->current_time($parser->uint64); | ||
| 81 | $self->boot_time($parser->uint64); | ||
| 82 | my $offset = $parser->uint16; | ||
| 83 | my $length = $parser->uint16; | ||
| 84 | $parser->uint32; # reserved2 | ||
| 85 | $self->security_buffer($parser->bytes($length)); | ||
| 86 | } else { | ||
| 87 | my $num_dialects = $parser->uint16; | ||
| 88 | $self->security_mode($parser->uint16); | ||
| 89 | $parser->uint16; # reserved | ||
| 90 | $self->capabilities($parser->uint32); | ||
| 91 | $self->guid($parser->bytes(16)); | ||
| 92 | $self->boot_time($parser->uint64); | ||
| 93 | $self->dialects([ map { $parser->uint16 } 1 .. $num_dialects ]); | ||
| 94 | } | ||
| 95 | |||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub pack ($$) { | ||
| 100 | my $self = shift; | ||
| 101 | my $packer = shift; | ||
| 102 | |||
| 103 | if ($self->is_response) { | ||
| 104 | my $security_buffer = $self->security_buffer // die 'No security_buffer'; | ||
| 105 | |||
| 106 | $packer | ||
| 107 | ->uint16($self->security_mode) | ||
| 108 | ->uint16($self->dialects->[1] || $self->dialect) | ||
| 109 | ->uint16(0) # reserved | ||
| 110 | ->bytes ($self->guid) | ||
| 111 | ->uint32($self->capabilities) | ||
| 112 | ->uint32($self->max_transact_size) | ||
| 113 | ->uint32($self->max_read_size) | ||
| 114 | ->uint32($self->max_write_size) | ||
| 115 | ->uint64($self->current_time) | ||
| 116 | ->uint64($self->boot_time) | ||
| 117 | ->uint16($packer->diff('smb-header') + 8) | ||
| 118 | ->uint16(length $security_buffer) | ||
| 119 | ->uint32(0) # reserved2 | ||
| 120 | ->bytes ($security_buffer) | ||
| 121 | ; | ||
| 122 | } else { | ||
| 123 | my $dialects = $self->dialects; | ||
| 124 | $packer | ||
| 125 | ->uint16(scalar @$dialects) | ||
| 126 | ->uint16($self->security_mode) | ||
| 127 | ->uint16(0) # reserved | ||
| 128 | ->uint32($self->capabilities) | ||
| 129 | ->bytes ($self->guid) | ||
| 130 | ->uint64($self->boot_time) | ||
| 131 | ; | ||
| 132 | $packer->uint16($_) for @$dialects; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub supports_protocol ($$) { | ||
| 137 | my $self = shift; | ||
| 138 | |||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; |