Class: NetConfGen::Handler::RWSimple
- Defined in:
- lib/netconfgen/netconfgen.rb
Overview
Basic read-write session over a ‘physical’ directory.
Instance Method Summary collapse
-
#initialize(path, opts = {}) ⇒ RWSimple
constructor
Initialize the handler.
-
#run!(tag, req, sock, src) ⇒ Object
Handle a session.
Methods inherited from Base
Constructor Details
#initialize(path, opts = {}) ⇒ RWSimple
Initialize the handler.
Options:
- :no_read => deny read access if true
- :no_write => deny write access if true
376 377 378 379 |
# File 'lib/netconfgen/netconfgen.rb', line 376 def initialize(path, opts = {}) @path = path super(opts) end |
Instance Method Details
#run!(tag, req, sock, src) ⇒ Object
Handle a session.
Has to close the socket (and any other resources). Note that the current version ‘guards’ against path traversal by a simple substitution of ‘..’ with ‘__’.
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 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/netconfgen/netconfgen.rb', line 391 def run!(tag, req, sock, src) name = req.filename.gsub('..', '__') path = File.join(@path, name) case req when Packet::RRQ if @opts[:no_read] log :info, "#{tag} Denied read request for #{req.filename}" sock.send(Packet::ERROR.new(2, 'Access denied.').encode, 0) sock.close return end log :info, "#{tag} Read request for #{req.filename} (#{req.mode})" unless File.exist? path log :warn, "#{tag} File not found" sock.send(Packet::ERROR.new(1, 'File not found.').encode, 0) sock.close return end mode = 'r' mode += 'b' if req.mode == :octet io = File.open(path, mode) send(tag, sock, io) sock.close io.close when Packet::WRQ if @opts[:no_write] log :info, "#{tag} Denied write request for #{req.filename}" sock.send(Packet::ERROR.new(2, 'Access denied.').encode, 0) sock.close return end log :info, "#{tag} Write request for #{req.filename} (#{req.mode})" mode = 'w' mode += 'b' if req.mode == :octet io = File.open(path, mode) ok = recv(tag, sock, io) sock.close io.close unless ok log :warn, "#{tag} Removing partial file #{req.filename}" File.delete(path) end end end |