Module: Ftpd::DataConnectionHelper

Included in:
CommandHandler
Defined in:
lib/ftpd/data_connection_helper.rb

Instance Method Summary collapse

Instance Method Details

#close_data_server_socket_when_doneObject



112
113
114
115
116
# File 'lib/ftpd/data_connection_helper.rb', line 112

def close_data_server_socket_when_done
  yield
ensure
  close_data_server_socket
end

#data_connection_descriptionObject



94
95
96
97
98
99
100
# File 'lib/ftpd/data_connection_helper.rb', line 94

def data_connection_description
  [
    Session::DATA_TYPES[data_type][0],
    "mode data connection",
    ("(TLS)" if encrypt_data?)
  ].compact.join(' ')
end

#encrypt_data?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ftpd/data_connection_helper.rb', line 118

def encrypt_data?
  data_channel_protection_level != :clear
end

#handle_data_disconnectObject



80
81
82
83
84
# File 'lib/ftpd/data_connection_helper.rb', line 80

def handle_data_disconnect
  return yield
rescue Errno::ECONNRESET, Errno::EPIPE
  reply "426 Connection closed; transfer aborted."
end

#make_tls_connection(data_socket) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/ftpd/data_connection_helper.rb', line 102

def make_tls_connection(data_socket)
  ssl_socket = OpenSSL::SSL::SSLSocket.new(data_socket, socket.ssl_context)
  ssl_socket.accept
  begin
    yield(ssl_socket)
  ensure
    ssl_socket.close
  end
end

#open_active_data_connectionObject



62
63
64
65
66
67
68
69
# File 'lib/ftpd/data_connection_helper.rb', line 62

def open_active_data_connection
  data_socket = TCPSocket.new(data_hostname, data_port)
  begin
    yield(data_socket)
  ensure
    data_socket.close
  end
end

#open_active_tls_data_connectionObject



54
55
56
57
58
59
60
# File 'lib/ftpd/data_connection_helper.rb', line 54

def open_active_tls_data_connection
  open_active_data_connection do |socket|
    make_tls_connection(socket) do |ssl_socket|
      yield(ssl_socket)
    end
  end
end

#open_data_connection(path_to_advertise = nil, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ftpd/data_connection_helper.rb', line 29

def open_data_connection(path_to_advertise = nil, &block)
  send_start_of_data_connection_reply(path_to_advertise)
  if data_server
    if encrypt_data?
      open_passive_tls_data_connection(&block)
    else
      open_passive_data_connection(&block)
    end
  else
    if encrypt_data?
      open_active_tls_data_connection(&block)
    else
      open_active_data_connection(&block)
    end
  end
end

#open_passive_data_connectionObject



71
72
73
74
75
76
77
78
# File 'lib/ftpd/data_connection_helper.rb', line 71

def open_passive_data_connection
  data_socket = data_server.accept
  begin
    yield(data_socket)
  ensure
    data_socket.close
  end
end

#open_passive_tls_data_connectionObject



46
47
48
49
50
51
52
# File 'lib/ftpd/data_connection_helper.rb', line 46

def open_passive_tls_data_connection
  open_passive_data_connection do |socket|
    make_tls_connection(socket) do |ssl_socket|
      yield(ssl_socket)
    end
  end
end

#receive_file(path_to_advertise = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/ftpd/data_connection_helper.rb', line 18

def receive_file(path_to_advertise = nil)
  open_data_connection(path_to_advertise) do |data_socket|
    contents = handle_data_disconnect do
      data_socket.read
    end
    contents = nvt_ascii_to_unix(contents) if data_type == 'A'
    config.log.debug "Received #{contents.size} bytes"
    contents
  end
end

#send_start_of_data_connection_reply(path) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ftpd/data_connection_helper.rb', line 86

def send_start_of_data_connection_reply(path)
  if path
    reply "150 FILE: #{path}"
  else
    reply "150 Opening #{data_connection_description}"
  end
end

#transmit_file(contents, data_type = session.data_type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/ftpd/data_connection_helper.rb', line 7

def transmit_file(contents, data_type = session.data_type)
  open_data_connection do |data_socket|
    contents = unix_to_nvt_ascii(contents) if data_type == 'A'
    handle_data_disconnect do
      data_socket.write(contents)
    end
    config.log.debug "Sent #{contents.size} bytes"
    reply "226 Transfer complete"
  end
end