| 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::Create; | ||
| 17 | |||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use parent 'SMB::v2::Command'; | ||
| 22 | |||
| 23 | use SMB::OpenFile; | ||
| 24 | |||
| 25 | use constant { | ||
| 26 | OPTIONS_DIRECTORY_FILE => 0x00000001, | ||
| 27 | OPTIONS_WRITE_THROUGH => 0x00000002, | ||
| 28 | OPTIONS_SEQUENTIAL_ONLY => 0x00000004, | ||
| 29 | OPTIONS_NO_INTERMEDIATE_BUFFERING => 0x00000008, | ||
| 30 | OPTIONS_SYNCHRONOUS_IO_ALERT => 0x00000010, | ||
| 31 | OPTIONS_SYNCRHONOUS_IO_NONALERT => 0x00000020, | ||
| 32 | OPTIONS_NON_DIRECTORY_FILE => 0x00000040, | ||
| 33 | OPTIONS_COMPLETE_IF_OPLOCKED => 0x00000100, | ||
| 34 | OPTIONS_NO_EA_KNOWLEDGE => 0x00000200, | ||
| 35 | OPTIONS_RANDOM_ACCESS => 0x00000800, | ||
| 36 | OPTIONS_DELETE_ON_CLOSE => 0x00001000, | ||
| 37 | OPTIONS_OPEN_BY_FILE_ID => 0x00002000, | ||
| 38 | OPTIONS_OPEN_FOR_BACKUP_INTENT => 0x00004000, | ||
| 39 | OPTIONS_NO_COMPRESSION => 0x00008000, | ||
| 40 | OPTIONS_RESERVE_OPFILTER => 0x00100000, | ||
| 41 | OPTIONS_OPEN_REPARSE_POINT => 0x00200000, | ||
| 42 | OPTIONS_OPEN_NO_RECALL => 0x00400000, | ||
| 43 | OPTIONS_OPEN_FOR_FREE_SPACE_QUERY => 0x00800000, | ||
| 44 | }; | ||
| 45 | |||
| 46 | sub init ($) { | ||
| 47 | $_[0]->set( | ||
| 48 | security_flags => 0, | ||
| 49 | oplock => 0, | ||
| 50 | impersonation => 2, | ||
| 51 | create_flags => 0, | ||
| 52 | access_mask => 0x81, | ||
| 53 | file_attributes => 0, | ||
| 54 | share_access => 3, | ||
| 55 | disposition => 1, | ||
| 56 | options => 0, | ||
| 57 | file_name => '', | ||
| 58 | |||
| 59 | flags => 0, | ||
| 60 | fid => undef, | ||
| 61 | openfile => undef, | ||
| 62 | ) | ||
| 63 | } | ||
| 64 | |||
| 65 | sub parse ($$) { | ||
| 66 | my $self = shift; | ||
| 67 | my $parser = shift; | ||
| 68 | |||
| 69 | if ($self->is_response) { | ||
| 70 | my $file = SMB::File->new(name => $self->file_name); | ||
| 71 | |||
| 72 | $self->oplock($parser->uint8); | ||
| 73 | $self->flags($parser->uint8); | ||
| 74 | my $action = $parser->uint32; | ||
| 75 | |||
| 76 | my @file_params = ( | ||
| 77 | $parser->uint64, # creation_time | ||
| 78 | $parser->uint64, # last_access_time | ||
| 79 | $parser->uint64, # last_write_time | ||
| 80 | $parser->uint64, # change_time | ||
| 81 | $parser->uint64, # allocation_size | ||
| 82 | $parser->uint64, # end_of_file | ||
| 83 | $parser->uint32, # attributes | ||
| 84 | ); | ||
| 85 | $file->update(@file_params); | ||
| 86 | |||
| 87 | $parser->uint32; # reserved | ||
| 88 | $self->fid($parser->fid2); | ||
| 89 | $self->openfile(SMB::OpenFile->new($file, 0, $action)); | ||
| 90 | } else { | ||
| 91 | $self->security_flags($parser->uint8); | ||
| 92 | $self->oplock($parser->uint8); | ||
| 93 | $self->impersonation($parser->uint32); | ||
| 94 | $self->create_flags($parser->uint64); | ||
| 95 | $parser->uint64; # reserved | ||
| 96 | $self->access_mask($parser->uint32); | ||
| 97 | $self->file_attributes($parser->uint32); | ||
| 98 | $self->share_access($parser->uint32); | ||
| 99 | $self->disposition($parser->uint32); | ||
| 100 | $self->options($parser->uint32); | ||
| 101 | my $name_offset = $parser->uint16; | ||
| 102 | my $name_len = $parser->uint16; | ||
| 103 | $parser->uint32(0); # contexts | ||
| 104 | $parser->uint32(0); | ||
| 105 | $self->file_name($parser->utf16($name_len)); | ||
| 106 | } | ||
| 107 | |||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub pack ($$) { | ||
| 112 | my $self = shift; | ||
| 113 | my $packer = shift; | ||
| 114 | |||
| 115 | if ($self->is_response) { | ||
| 116 | my $openfile = $self->openfile; | ||
| 117 | my $file = $openfile && $openfile->file; | ||
| 118 | |||
| 119 | return $self->abort_pack($packer, SMB::STATUS_NO_SUCH_FILE) | ||
| 120 | unless $file && $file->exists && $self->fid; | ||
| 121 | |||
| 122 | $packer | ||
| 123 | ->uint8($self->oplock) | ||
| 124 | ->uint8($self->flags) | ||
| 125 | ->uint32($openfile->action) | ||
| 126 | ->uint64($file->creation_time) | ||
| 127 | ->uint64($file->last_access_time) | ||
| 128 | ->uint64($file->last_write_time) | ||
| 129 | ->uint64($file->change_time) | ||
| 130 | ->uint64($file->allocation_size) | ||
| 131 | ->uint64($file->end_of_file) | ||
| 132 | ->uint32($file->attributes) | ||
| 133 | ->uint32(0) # reserved | ||
| 134 | ->fid2($self->fid) | ||
| 135 | ->uint32(0) # contexts | ||
| 136 | ->uint32(0) | ||
| 137 | ; | ||
| 138 | } else { | ||
| 139 | $packer | ||
| 140 | ->uint8($self->security_flags) | ||
| 141 | ->uint8($self->oplock) | ||
| 142 | ->uint32($self->impersonation) | ||
| 143 | ->uint64($self->create_flags) | ||
| 144 | ->uint64(0) # reserved | ||
| 145 | ->uint32($self->access_mask) | ||
| 146 | ->uint32($self->file_attributes) | ||
| 147 | ->uint32($self->share_access) | ||
| 148 | ->uint32($self->disposition) | ||
| 149 | ->uint32($self->options) | ||
| 150 | ->uint16($packer->diff('smb-header') + 12) | ||
| 151 | ->uint16(length($self->file_name) * 2) | ||
| 152 | ->uint32(0) # contexts | ||
| 153 | ->uint32(0) | ||
| 154 | ->utf16($self->file_name) | ||
| 155 | ->uint8(0) | ||
| 156 | ; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub requested_directory ($) { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | return $self->options & OPTIONS_DIRECTORY_FILE ? 1 : 0; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub requested_non_directory ($) { | ||
| 167 | my $self = shift; | ||
| 168 | |||
| 169 | return $self->options & OPTIONS_NON_DIRECTORY_FILE ? 1 : 0; | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; |