| 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::SessionSetup; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use parent 'SMB::v2::Command'; | ||
| 22 | |||
| 23 | sub init ($) { | ||
| 24 | $_[0]->set( | ||
| 25 | flags => 0, | ||
| 26 | security_mode => 0, | ||
| 27 | capabilities => 0, | ||
| 28 | prev_session_id => 0, | ||
| 29 | security_buffer => undef, | ||
| 30 | ) | ||
| 31 | } | ||
| 32 | |||
| 33 | sub parse ($$%) { | ||
| 34 | my $self = shift; | ||
| 35 | my $parser = shift; | ||
| 36 | |||
| 37 | if ($self->is_response) { | ||
| 38 | $parser->uint16; # session flags | ||
| 39 | my $offset = $parser->uint16; | ||
| 40 | my $length = $parser->uint16; | ||
| 41 | $self->security_buffer($parser->bytes($length)); | ||
| 42 | } else { | ||
| 43 | $self->flags($parser->uint8); | ||
| 44 | $self->security_mode($parser->uint8); | ||
| 45 | $self->capabilities($parser->uint32); | ||
| 46 | $parser->uint32; # channel | ||
| 47 | my $offset = $parser->uint16; | ||
| 48 | my $length = $parser->uint16; | ||
| 49 | $self->prev_session_id($parser->uint64); | ||
| 50 | $self->security_buffer($parser->bytes($length)); | ||
| 51 | } | ||
| 52 | |||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub prepare_response ($) { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | $self->SUPER::prepare_response; | ||
| 60 | |||
| 61 | my $more_processing = $self->security_buffer && length($self->security_buffer) > 70; | ||
| 62 | |||
| 63 | $self->set_status(SMB::STATUS_MORE_PROCESSING_REQUIRED) if $more_processing; | ||
| 64 | |||
| 65 | $self->header->uid(int(rand(999999)) + 1) unless $self->header->uid; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub pack ($$) { | ||
| 69 | my $self = shift; | ||
| 70 | my $packer = shift; | ||
| 71 | |||
| 72 | my $security_buffer = $self->security_buffer; | ||
| 73 | |||
| 74 | if ($self->is_response) { | ||
| 75 | $security_buffer | ||
| 76 | or return $self->abort_pack($packer, SMB::STATUS_LOGON_FAILURE); | ||
| 77 | |||
| 78 | $packer | ||
| 79 | ->uint16(0) # session flags | ||
| 80 | ->uint16($packer->diff('smb-header') + 4) | ||
| 81 | ->uint16(length $security_buffer) | ||
| 82 | ->bytes ($security_buffer) | ||
| 83 | ; | ||
| 84 | } else { | ||
| 85 | $security_buffer | ||
| 86 | or die "No security_buffer"; | ||
| 87 | |||
| 88 | $packer | ||
| 89 | ->uint8 ($self->flags) | ||
| 90 | ->uint8 ($self->security_mode) | ||
| 91 | ->uint32($self->capabilities) | ||
| 92 | ->uint32(0) # channel | ||
| 93 | ->uint16($packer->diff('smb-header') + 12) | ||
| 94 | ->uint16(length $security_buffer) | ||
| 95 | ->uint64($self->prev_session_id) | ||
| 96 | ->bytes ($security_buffer) | ||
| 97 | ; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | 1; |