summaryrefslogtreecommitdiffstats
path: root/nselib/ndmp.lua
blob: 833903712e71ac41e068955422c440dcb2041581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
---
-- A minimalistic NDMP (Network Data Management Protocol) library
--
-- @author Patrik Karlsson <patrik@cqure.net>
--

local match = require "match"
local nmap = require "nmap"
local os = require "os"
local stdnse = require "stdnse"
local string = require "string"
local table = require "table"
_ENV = stdnse.module("ndmp", stdnse.seeall)

NDMP = {

  -- Message types
  MessageType = {
    CONFIG_GET_HOST_INFO = 0x00000100,
    CONFIG_GET_FS_INFO = 0x00000105,
    CONFIG_GET_AUTH_ATTR = 0x00000103,
    CONFIG_GET_SERVER_INFO = 0x00000108,
    CONNECT_CLIENT_AUTH = 0x00000901,
  },

  -- Error types
  ErrorType = {
    NOT_AUTHORIZED_ERROR = 0x00000004
  },

  -- The fragment header, 4 bytes where the highest bit is used to determine
  -- whether the fragment is the last or not.
  FragmentHeader = {
    size = 4,

    -- Creates a new instance of fragment header
    -- @return o instance of Class
    new = function(self)
      local o = {
        last = true,
        length = 24,
      }
      setmetatable(o, self)
      self.__index = self
      return o
    end,

    -- Parse data stream and create a new instance of this class
    -- @param data opaque string
    -- @return fh new instance of FragmentHeader class
    parse = function(data)
      local fh = NDMP.FragmentHeader:new()
      local tmp = string.unpack(">I4", data)
      fh.length = (tmp & 0x7fffffff)
      fh.last= (tmp >> 31)
      return fh
    end,

    -- Serializes the instance to an opaque string
    -- @return str string containing the serialized class
    __tostring = function(self)
      local tmp = 0
      if ( self.last ) then
        tmp = 0x80000000
      end
      tmp = tmp + self.length
      return string.pack(">I4", tmp)
    end,

  },

  -- The ndmp 24 byte header
  Header = {
    size = 24,

    -- creates a new instance of Header
    -- @return o instance of Header
    new = function(self)
      local o = {
        seq = 0,
        time = os.time(),
        type = 0,
        msg = 0x00000108,
        reply_seq = 0,
        error = 0,
      }
      setmetatable(o, self)
      self.__index = self
      return o
    end,

    -- Create a Header instance from opaque data string
    -- @param data opaque string
    -- @return hdr new instance of Header
    parse = function(data)
      local hdr = NDMP.Header:new()
      hdr.seq, hdr.time, hdr.type, hdr.msg, hdr.reply_seq, hdr.error = string.unpack(">I4I4I4I4I4I4", data)
      return hdr
    end,

    -- Serializes the instance to an opaque string
    -- @return str string containing the serialized class instance
    __tostring = function(self)
      return string.pack(">I4I4I4I4I4I4", self.seq, self.time, self.type, self.msg, self.reply_seq, self.error)
    end,

  },
}

NDMP.Message = {}

NDMP.Message.ConfigGetServerInfo = {

  -- Creates a Config Server Info instance
  -- @return o new instance of Class
  new = function(self)
    local o = {
      frag_header = NDMP.FragmentHeader:new(),
      header = NDMP.Header:new(),
      data = nil,
    }
    o.header.msg = NDMP.MessageType.CONFIG_GET_SERVER_INFO
    setmetatable(o, self)
    self.__index = self
    return o
  end,

  -- Create a ConfigGetServerInfo instance from opaque data string
  -- @param data opaque string
  -- @return msg new instance of ConfigGetServerInfo
  parse = function(data)
    local msg = NDMP.Message.ConfigGetServerInfo:new()
    msg.frag_header = NDMP.FragmentHeader.parse(data)
    data = data:sub(NDMP.FragmentHeader.size + 1)
    msg.header = NDMP.Header.parse(data)
    msg.data = data:sub(NDMP.Header.size + 1)

    msg.serverinfo = {}
    local err, pos = string.unpack(">I4", msg.data)
    pos, msg.serverinfo.vendor = Util.parseString(msg.data, pos)
    pos, msg.serverinfo.product = Util.parseString(msg.data, pos)
    pos, msg.serverinfo.version = Util.parseString(msg.data, pos)
    return msg
  end,

  -- Serializes the instance to an opaque string
  -- @return str string containing the serialized class instance
  __tostring = function(self)
    return tostring(self.frag_header) .. tostring(self.header) .. tostring(self.data or "")
  end,

}

NDMP.Message.ConfigGetHostInfo = {
  new = function(self)
    local o = {
      frag_header = NDMP.FragmentHeader:new(),
      header = NDMP.Header:new(),
      data = nil,
    }
    o.header.msg = NDMP.MessageType.CONFIG_GET_HOST_INFO
    setmetatable(o, self)
    self.__index = self
    return o
  end,

  parse = function(data)
    local msg = NDMP.Message.ConfigGetServerInfo:new()
    msg.frag_header = NDMP.FragmentHeader.parse(data)
    data = data:sub(NDMP.FragmentHeader.size + 1)
    msg.header = NDMP.Header.parse(data)
    if ( msg.header.error == NDMP.ErrorType.NOT_AUTHORIZED_ERROR ) then
      -- no data to parse
      return msg
    end
    msg.data = data:sub(NDMP.Header.size + 1)

    msg.hostinfo = {}
    local err, pos = string.unpack(">I4", msg.data)
    pos, msg.hostinfo.hostname = Util.parseString(msg.data, pos)
    pos, msg.hostinfo.ostype = Util.parseString(msg.data, pos)
    pos, msg.hostinfo.osver = Util.parseString(msg.data, pos)
    pos, msg.hostinfo.hostid = Util.parseString(msg.data, pos)
    return msg
  end,

  __tostring = function(self)
    return tostring(self.frag_header) .. tostring(self.header) .. tostring(self.data or "")
  end,
}

NDMP.Message.ConfigGetFsInfo = {

  new = function(self)
    local o = {
      frag_header = NDMP.FragmentHeader:new(),
      header = NDMP.Header:new(),
      data = nil,
      fsinfo = {},
    }
    o.header.msg = NDMP.MessageType.CONFIG_GET_FS_INFO
    setmetatable(o, self)
    self.__index = self
    return o
  end,

  parse = function(data)
    local msg = NDMP.Message.ConfigGetFsInfo:new()
    msg.frag_header = NDMP.FragmentHeader.parse(data)
    data = data:sub(NDMP.FragmentHeader.size + 1)
    msg.header = NDMP.Header.parse(data)
    if ( msg.header.error == NDMP.ErrorType.NOT_AUTHORIZED_ERROR ) then
      -- no data to parse
      return msg
    end
    msg.data = data:sub(NDMP.Header.size + 1)

    local err, count, pos = string.unpack(">I4I4", msg.data)
    for i=1, count do
      local item = {}
      item.invalid, pos = string.unpack(">I4", msg.data, pos)
      pos, item.fs_type = Util.parseString(msg.data, pos)
      pos, item.fs_logical_device = Util.parseString(msg.data, pos)
      pos, item.fs_physical_device = Util.parseString(msg.data, pos)
      item.total_size,
      item.used_size,
      item.avail_size,
      item.total_inodes,
      item.used_inodes, pos = string.unpack(">I8I8I8I8I8", msg.data, pos)
      pos, item.fs_env = Util.parseString(msg.data, pos)
      pos, item.fs_status = Util.parseString(msg.data, pos)
      table.insert(msg.fsinfo, item)
    end
    return msg
  end,

  __tostring = function(self)
    return tostring(self.frag_header) .. tostring(self.header) .. tostring(self.data or "")
  end,
}

NDMP.Message.UnhandledMessage = {

  new = function(self)
    local o = {
      frag_header = NDMP.FragmentHeader:new(),
      header = NDMP.Header:new(),
      data = nil,
    }
    setmetatable(o, self)
    self.__index = self
    return o
  end,

  parse = function(data)
    local msg = NDMP.Message.ConfigGetFsInfo:new()
    msg.frag_header = NDMP.FragmentHeader.parse(data)
    data = data:sub(NDMP.FragmentHeader.size + 1)
    msg.header = NDMP.Header.parse(data)
    msg.data = data:sub(NDMP.Header.size + 1)
    return msg
  end,

  __tostring = function(self)
    return tostring(self.frag_header) .. tostring(self.header) .. tostring(self.data or "")
  end

}

Util = {

  parseString = function(data, pos)
    local str, pos = string.unpack(">s4", data, pos)
    local pad = ( 4 - ( #str % 4 ) ~= 4 ) and 4 - ( #str % 4 ) or 0
    return pos + pad, str

  end,

}

NDMP.TypeToMessage = {
  [NDMP.MessageType.CONFIG_GET_SERVER_INFO] = NDMP.Message.ConfigGetServerInfo,
  [NDMP.MessageType.CONFIG_GET_HOST_INFO] = NDMP.Message.ConfigGetHostInfo,
  [NDMP.MessageType.CONFIG_GET_FS_INFO] = NDMP.Message.ConfigGetFsInfo,
}

-- Handles the communication with the NDMP service
Comm = {

  -- Creates new Comm instance
  -- @param host table as received by the action method
  -- @param port table as received by the action method
  -- @return o new instance of Comm
  new = function(self, host, port)
    local o = {
      host = host,
      port = port,
      socket = nmap.new_socket(),
      seq = 0,
      in_queue = {},
    }
    setmetatable(o, self)
    self.__index = self
    return o
  end,

  -- Connects to the NDMP server
  -- @return status true on success, false on failure
  connect = function(self)
    -- some servers seem to take their time, so leave this as 10s for now
    self.socket:set_timeout(10000)
    return self.socket:connect(self.host, self.port)
  end,

  -- Receives a message from the server
  -- @return status true on success, false on failure
  -- @return msg NDMP message when a parser exists, otherwise nil
  sock_recv = function(self)
    local status, frag_data = self.socket:receive_buf(match.numbytes(4), true)
    if ( not(status) ) then
      return false, "Failed to read NDMP 4-byte fragment header"
    end
    local frag_header = NDMP.FragmentHeader.parse(frag_data)

    local status, header_data = self.socket:receive_buf(match.numbytes(24), true)
    if ( not(status) ) then
      return false, "Failed to read NDMP 24-byte header"
    end
    local header = NDMP.Header.parse(header_data)

    local status, data = self.socket:receive_buf(match.numbytes(frag_header.length - 24), true)
    if ( not(status) ) then
      return false, "Failed to read NDMP data"
    end

    if ( NDMP.TypeToMessage[header.msg] ) then
      return true, NDMP.TypeToMessage[header.msg].parse(frag_data .. header_data .. data)
    end
    return true, NDMP.Message.UnhandledMessage.parse(frag_data .. header_data .. data)
  end,

  recv = function(self)
    if ( #self.in_queue > 0 ) then
      return true, table.remove(self.in_queue, 1)
    end
    return self:sock_recv()
  end,

  -- Sends a message to the server
  -- @param msg NDMP message
  -- @return status true on success, false on failure
  -- @return err string containing the error message when status is false
  send = function(self, msg)
    self.seq = self.seq + 1
    msg.header.seq = self.seq
    return self.socket:send(tostring(msg))
  end,


  exch = function(self, msg)
    local status, err = self:send(msg)
    if ( not(status) ) then
      return false, "Failed to send ndmp Message to server"
    end
    local s_seq = msg.header.seq

    for k, v in ipairs(self.in_queue) do
      if ( v.reply_seq == s_seq ) then
        return true, table.remove(self.in_queue, k)
      end
    end

    while(true) do
      local reply
      status, reply = self:sock_recv()
      if ( not(status) ) then
        return false, "Failed to receive msg from server"
      elseif ( reply and reply.header and reply.header.reply_seq == s_seq ) then
        return true, reply
      else
        table.insert(self.in_queue, reply)
      end
    end
  end,

  close = function(self) return self.socket:close() end,

}


Helper = {

  new = function(self, host, port)
    local o = { comm = Comm:new(host, port) }
    setmetatable(o, self)
    self.__index = self
    return o
  end,

  connect = function(self)
    return self.comm:connect()
  end,

  getFsInfo = function(self)
    return self.comm:exch(NDMP.Message.ConfigGetFsInfo:new())
  end,

  getHostInfo = function(self)
    return self.comm:exch(NDMP.Message.ConfigGetHostInfo:new())
  end,

  getServerInfo = function(self)
    return self.comm:exch(NDMP.Message.ConfigGetServerInfo:new())
  end,

  close = function(self)
    return self.comm:close()

  end

}

return _ENV;