Class: RubySMB::SMB1::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_smb/smb1/tree.rb

Overview

An SMB1 connected remote Tree, as returned by a [RubySMB::SMB1::Packet::TreeConnectRequest]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, share:, response:) ⇒ Tree

Returns a new instance of Tree.



31
32
33
34
35
36
37
# File 'lib/ruby_smb/smb1/tree.rb', line 31

def initialize(client:, share:, response:)
  @client             = client
  @share              = share
  @id                 = response.smb_header.tid
  @guest_permissions  = response.parameter_block.guest_access_rights
  @permissions        = response.parameter_block.access_rights
end

Instance Attribute Details

#clientRubySMB::Client

Returns:



9
10
11
# File 'lib/ruby_smb/smb1/tree.rb', line 9

def client
  @client
end

#guest_permissionsRubySMB::SMB1::BitField::DirectoryAccessMask



14
15
16
# File 'lib/ruby_smb/smb1/tree.rb', line 14

def guest_permissions
  @guest_permissions
end

#idInteger

Returns:

  • (Integer)


29
30
31
# File 'lib/ruby_smb/smb1/tree.rb', line 29

def id
  @id
end

#permissionsRubySMB::SMB1::BitField::DirectoryAccessMask



19
20
21
# File 'lib/ruby_smb/smb1/tree.rb', line 19

def permissions
  @permissions
end

#shareString

Returns:

  • (String)


24
25
26
# File 'lib/ruby_smb/smb1/tree.rb', line 24

def share
  @share
end

Instance Method Details

#disconnect!WindowsError::ErrorCode

Disconnects this Tree from the current session

Returns:

  • (WindowsError::ErrorCode)

    the NTStatus sent back by the server.

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_smb/smb1/tree.rb', line 43

def disconnect!
  request = RubySMB::SMB1::Packet::TreeDisconnectRequest.new
  request = set_header_fields(request)
  raw_response = client.send_recv(request)
  response = RubySMB::SMB1::Packet::TreeDisconnectResponse.read(raw_response)
  unless response.valid?
    raise RubySMB::Error::InvalidPacket.new(
      expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
      expected_cmd:   RubySMB::SMB1::Packet::TreeDisconnectResponse::COMMAND,
      packet:         response
    )
  end
  response.status_code
end

#list(directory: '\\', pattern: '*', unicode: true, type: RubySMB::SMB1::Packet::Trans2::FindInformationLevel::FindFileFullDirectoryInfo) ⇒ Array

List directory on the remote share.

Examples:

tree = client.tree_connect("\\\\192.168.99.134\\Share")
tree.list(directory: "path\\to\\directory")

Parameters:

  • directory (String) (defaults to: '\\')

    path to the directory to be listed

  • pattern (String) (defaults to: '*')

    search pattern

  • type (Class) (defaults to: RubySMB::SMB1::Packet::Trans2::FindInformationLevel::FindFileFullDirectoryInfo)

    file information class

Returns:

  • (Array)

    array of directory structures

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ruby_smb/smb1/tree.rb', line 103

def list(directory: '\\', pattern: '*', unicode: true,
         type: RubySMB::SMB1::Packet::Trans2::FindInformationLevel::FindFileFullDirectoryInfo)
  find_first_request = RubySMB::SMB1::Packet::Trans2::FindFirst2Request.new
  find_first_request = set_header_fields(find_first_request)
  find_first_request.smb_header.flags2.unicode  = 1 if unicode

  search_path = directory.dup
  search_path << '\\' unless search_path.end_with?('\\')
  search_path << pattern
  search_path = '\\' + search_path unless search_path.start_with?('\\')

  # Set the search parameters
  t2_params = find_first_request.data_block.trans2_parameters
  t2_params.search_attributes.hidden    = 1
  t2_params.search_attributes.system    = 1
  t2_params.search_attributes.directory = 1
  t2_params.flags.close_eos             = 1
  t2_params.flags.resume_keys           = 0
  t2_params.information_level           = type::CLASS_LEVEL
  t2_params.filename                    = search_path
  t2_params.search_count                = 10

  find_first_request = set_find_params(find_first_request)

  raw_response  = client.send_recv(find_first_request)
  response      = RubySMB::SMB1::Packet::Trans2::FindFirst2Response.read(raw_response)
  unless response.valid?
    raise RubySMB::Error::InvalidPacket.new(
      expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
      expected_cmd:   RubySMB::SMB1::Packet::Trans2::FindFirst2Response::COMMAND,
      packet:         response
    )
  end
  unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
    raise RubySMB::Error::UnexpectedStatusCode, response.status_code
  end

  results = response.results(type, unicode: unicode)

  eos   = response.data_block.trans2_parameters.eos
  sid   = response.data_block.trans2_parameters.sid
  last  = results.last.file_name

  while eos.zero?
    find_next_request = RubySMB::SMB1::Packet::Trans2::FindNext2Request.new
    find_next_request = set_header_fields(find_next_request)
    find_next_request.smb_header.flags2.unicode   = 1 if unicode

    t2_params                             = find_next_request.data_block.trans2_parameters
    t2_params.sid                         = sid
    t2_params.flags.close_eos             = 1
    t2_params.flags.resume_keys           = 0
    t2_params.information_level           = type::CLASS_LEVEL
    t2_params.filename                    = last
    t2_params.search_count                = 10

    find_next_request = set_find_params(find_next_request)

    raw_response  = client.send_recv(find_next_request)
    response      = RubySMB::SMB1::Packet::Trans2::FindNext2Response.read(raw_response)
    unless response.valid?
      raise RubySMB::Error::InvalidPacket.new(
        expected_proto: RubySMB::SMB1::SMB_PROTOCOL_ID,
        expected_cmd:   RubySMB::SMB1::Packet::Trans2::FindNext2Response::COMMAND,
        packet:         response
      )
    end
    unless response.status_code == WindowsError::NTStatus::STATUS_SUCCESS
      raise RubySMB::Error::UnexpectedStatusCode, response.status_code
    end

    results += response.results(type, unicode: unicode)

    eos   = response.data_block.trans2_parameters.eos
    last  = results.last.file_name
  end

  results
end

#open_file(opts) ⇒ RubySMB::SMB1::File

Open a file on the remote share.

Examples:

tree = client.tree_connect("\\\\192.168.99.134\\Share")
tree.open_file(filename: "myfile")

Parameters:

  • filename (String)

    name of the file to be opened

  • flags (BinData::Struct, Hash)

    flags to setup the request (see Packet::NtCreateAndxRequest)

  • options (RubySMB::SMB1::BitField::CreateOptions, Hash)

    flags that defines how the file should be created

  • disposition (Integer)

    32-bit field that defines how an already-existing file or a new file needs to be handled (constants are defined in Dispositions)

  • impersonation (Integer)

    32-bit field that defines the impersonation level (constants are defined in ImpersonationLevels)

  • read (TrueClass, FalseClass)

    request a read access

  • write (TrueClass, FalseClass)

    request a write access

  • delete (TrueClass, FalseClass)

    request a delete access

Returns:

Raises:



83
84
85
86
87
88
89
# File 'lib/ruby_smb/smb1/tree.rb', line 83

def open_file(opts)
  # Make sure we don't modify the caller's hash options
  opts = opts.dup
  opts[:filename] = opts[:filename].dup
  opts[:filename] = opts[:filename][1..-1] if opts[:filename].start_with?('\\'.encode(opts[:filename].encoding))
  _open(**opts)
end

#open_pipe(opts) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/ruby_smb/smb1/tree.rb', line 58

def open_pipe(opts)
  # Make sure we don't modify the caller's hash options
  opts = opts.dup
  opts[:filename] = opts[:filename].dup
  opts[:filename].prepend('\\') unless opts[:filename].start_with?('\\'.encode(opts[:filename].encoding))
  _open(**opts)
end

#set_header_fields(request) ⇒ RubySMB::SMB::Packet

Sets a few preset header fields that will always be set the same way for Tree operations. This is, the TreeID and Extended Attributes.

Parameters:

  • the (RubySMB::SMB::Packet)

    request packet to modify

Returns:

  • (RubySMB::SMB::Packet)

    the modified packet.



188
189
190
191
192
# File 'lib/ruby_smb/smb1/tree.rb', line 188

def set_header_fields(request)
  request.smb_header.tid        = @id
  request.smb_header.flags2.eas = 1
  request
end