Class: RubySMB::Server::Share::Provider::Disk::Processor

Inherits:
Processor::Base
  • Object
show all
Defined in:
lib/ruby_smb/server/share/provider/disk.rb

Defined Under Namespace

Classes: Handle

Instance Method Summary collapse

Constructor Details

#initialize(provider, server_client, session) ⇒ Processor

Returns a new instance of Processor.



12
13
14
15
16
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 12

def initialize(provider, server_client, session)
  super
  @handles = {}
  @query_directory_context = {}
end

Instance Method Details

#do_close_smb2(request) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 25

def do_close_smb2(request)
  local_path = get_local_path(request.file_id)
  if local_path.nil?
    response = RubySMB::SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_FILE_CLOSED
    return response
  end

  @handles.delete(request.file_id.to_binary_s)
  response = RubySMB::SMB2::Packet::CloseResponse.new
  set_common_info(response, local_path)
  response.flags = 1
  response
end

#do_create_smb2(request) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 40

def do_create_smb2(request)
  unless request.create_disposition == RubySMB::Dispositions::FILE_OPEN
    logger.warn("Can not handle CREATE request for disposition: #{request.create_disposition}")
    raise NotImplementedError
  end

  # process the delayed io fields
  request.name.read_now!
  unless request.contexts_offset == 0
    request.contexts.read_now!
    request.contexts.each do |context|
      context.name.read_now!
      context.data.read_now!
    end
  end

  path = request.name.snapshot
  path = path.encode.gsub('\\', File::SEPARATOR)
  local_path = get_local_path(path)
  unless local_path && (local_path.file? || local_path.directory?)
    logger.warn("Requested path does not exist: #{local_path}")
    response = RubySMB::SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_OBJECT_NAME_NOT_FOUND
    return response
  end

  durable = false
  response = RubySMB::SMB2::Packet::CreateResponse.new
  response.create_action = RubySMB::CreateActions::FILE_OPENED
  set_common_info(response, local_path)
  response.file_id.persistent = Zlib::crc32(path)
  response.file_id.volatile = rand(0xffffffff)

  request.contexts.each do |req_ctx|
    case req_ctx.name
    when SMB2::CreateContext::CREATE_DURABLE_HANDLE
      # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/9adbc354-5fad-40e7-9a62-4a4b6c1ff8a0
      next if request.contexts.any? { |ctx| ctx.name == SMB2::CreateContext::CREATE_DURABLE_HANDLE_RECONNECT }

      if request.contexts.any? { |ctx| [ SMB2::CreateContext::CREATE_DURABLE_HANDLE_V2, SMB2::CreateContext::CREATE_DURABLE_HANDLE_RECONNECT_v2 ].include?(ctx.name) }
        response = RubySMB::SMB2::Packet::ErrorPacket.new
        response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_INVALID_PARAMETER
        return response
      end

      durable = true
      res_ctx = SMB2::CreateContext::CreateDurableHandleResponse.new
    when SMB2::CreateContext::CREATE_DURABLE_HANDLE_V2
      # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/33e6800a-adf5-4221-af27-7e089b9e81d1
      if request.contexts.any? { |ctx| [ SMB2::CreateContext::CREATE_DURABLE_HANDLE, SMB2::CreateContext::CREATE_DURABLE_HANDLE_RECONNECT, SMB2::CreateContext::CREATE_DURABLE_HANDLE_RECONNECT_v2 ].include?(ctx.name) }
        response = RubySMB::SMB2::Packet::ErrorPacket.new
        response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_INVALID_PARAMETER
        return response
      end

      durable = true
      res_ctx = SMB2::CreateContext::CreateDurableHandleV2Response.new(
        timeout: 1000,
        flags: req_ctx.data.flags
      )
    when SMB2::CreateContext::CREATE_QUERY_MAXIMAL_ACCESS
      res_ctx = SMB2::CreateContext::CreateQueryMaximalAccessResponse.new(
        maximal_access: maximal_access(path)
      )
    when SMB2::CreateContext::CREATE_QUERY_ON_DISK_ID
      res_ctx = SMB2::CreateContext::CreateQueryOnDiskIdResponse.new(
        disk_file_id: local_path.stat.ino,
        volume_id: local_path.stat.dev
      )
    else
      logger.warn("Can not handle CREATE context: #{req_ctx.name}")
      next
    end

    response.contexts << SMB2::CreateContext::CreateContextResponse.new(name: res_ctx.class::NAME, data: res_ctx)
  end

  if response.contexts.length > 0
    # fixup the offsets
    response.contexts[0...-1].each do |ctx|
      ctx.next_offset = ctx.num_bytes
    end
    response.contexts[-1].next_offset = 0
    response.contexts_offset = response.buffer.abs_offset
    response.contexts_length = response.buffer.num_bytes
  else
    response.contexts_offset = 0
    response.contexts_length = 0
  end

  @handles[response.file_id.to_binary_s] = Handle.new(path, local_path, durable)
  response
end

#do_query_directory_smb2(request) ⇒ Object



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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 134

def do_query_directory_smb2(request)
  local_path = get_local_path(request.file_id)
  if local_path.nil?
    response = RubySMB::SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_FILE_CLOSED
    return response
  end

  # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/29dfcc9b-3aec-406b-abb5-0b4fe96712e2
  info_class = request.file_information_class.snapshot
  begin
    # probe #build_info to see if it supports the requested info class
    build_info(Pathname.new(__FILE__), info_class)
  rescue NotImplementedError
    logger.warn("Can not handle QUERY_DIRECTORY request for class: #{info_class}")
    raise
  end

  unless local_path.directory?
    response = SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_INVALID_PARAMETER
    return response
  end

  search_pattern = request.name.snapshot.dup.encode
  begin
    search_regex = wildcard_to_regex(search_pattern)
  rescue NotImplementedError
    logger.warn("Can not handle QUERY_DIRECTORY wildcard pattern: #{search_pattern}")
    raise
  end

  return_single = request.flags.return_single == 1

  align = 8
  infos = []
  total_size = 0

  if @query_directory_context[request.file_id.to_binary_s].nil? || request.flags.reopen == 1 || request.flags.restart_scans == 1
    dirents = local_path.children.sort.to_a
    dirents.unshift(local_path.parent) unless local_path.parent == local_path
    dirents.unshift(local_path)
    @query_directory_context[request.file_id.to_binary_s] = dirents
  else
    dirents = @query_directory_context[request.file_id.to_binary_s]
  end

  while dirents.length > 0
    dirent = dirents.shift
    next unless dirent.file? || dirent.directory? # filter out everything but files and directories

    case dirent
    when local_path
      dirent_name = '.'
    when local_path.parent
      dirent_name = '..'
    else
      dirent_name = dirent.basename.to_s
    end
    next unless search_regex.match?(dirent_name)

    info = build_info(dirent, info_class, rename: dirent_name)
    info_size = info.num_bytes + ((align - info.num_bytes % align) % align)
    if total_size + info_size > request.output_length
      dirents.unshift(dirent) # no space left for this one so put it back
      break
    end

    infos << info
    total_size += info_size
    break if return_single
  end

  if infos.length == 0
    response = SMB2::Packet::QueryDirectoryResponse.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_NO_MORE_FILES
    return response
  end

  response = SMB2::Packet::QueryDirectoryResponse.new
  infos.last.next_offset = 0 if infos.last
  buffer = ""
  infos.each do |info|
    info = info.to_binary_s
    buffer << info + "\x00".b * ((align - info.length % align) % align)
  end
  response.buffer = buffer
  response
end

#do_query_info_smb2(request) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 224

def do_query_info_smb2(request)
  local_path = get_local_path(request.file_id)
  if local_path.nil?
    response = RubySMB::SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_FILE_CLOSED
    return response
  end

  case request.info_type
  when SMB2::SMB2_INFO_FILE
    info = query_info_smb2_file(request, local_path)
  when SMB2::SMB2_INFO_FILESYSTEM
    info = query_info_smb2_filesystem(request, local_path)
  else
    logger.warn("Can not handle QUERY_INFO request for type: #{request.info_type}, class: #{request.file_information_class}")
    raise NotImplementedError
  end

  response = SMB2::Packet::QueryInfoResponse.new
  response.buffer = info.to_binary_s
  response
end

#do_read_smb2(request) ⇒ Object

Raises:

  • (NotImplementedError)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 247

def do_read_smb2(request)
  local_path = get_local_path(request.file_id)
  if local_path.nil?
    response = RubySMB::SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_FILE_CLOSED
    return response
  end

  raise NotImplementedError unless request.channel == SMB2::SMB2_CHANNEL_NONE

  buffer = nil
  local_path.open do |file|
    file.seek(request.offset.snapshot)
    buffer = file.read(request.read_length)
  end

  # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/21e8b343-34e9-4fca-8d93-03dd2d3e961e
  if buffer.nil? || buffer.length == 0 || buffer.length < request.min_bytes
    response = SMB2::Packet::ErrorPacket.new
    response.smb2_header.nt_status = WindowsError::NTStatus::STATUS_END_OF_FILE
    return response
  end

  response = SMB2::Packet::ReadResponse.new
  response.data_length = buffer.length
  response.buffer = buffer
  response
end

#maximal_access(path = nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/ruby_smb/server/share/provider/disk.rb', line 18

def maximal_access(path=nil)
  RubySMB::SMB2::BitField::FileAccessMask.new(
    read_attr: 1,
    read_data: 1
  )
end