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



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

def close_data_server_socket_when_done
  yield
ensure
  close_data_server_socket
end

#data_connection_descriptionObject



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

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)


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

def encrypt_data?
  data_channel_protection_level != :clear
end

#handle_data_disconnectObject



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

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

#make_tls_connection(data_socket) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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, &block) ⇒ Object



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

def receive_file(path_to_advertise = nil, &block)
  open_data_connection(path_to_advertise) do |data_socket|
    socket = Ftpd::Stream.new(data_socket, data_type)
    handle_data_disconnect do
      yield socket
    end
    config.log.debug "Received #{socket.byte_count} bytes"
  end
end

#send_start_of_data_connection_reply(path) ⇒ Object



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

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(io, 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(io, data_type = session.data_type)
  open_data_connection do |data_socket|
    socket = Ftpd::Stream.new(data_socket, data_type)
    handle_data_disconnect do
      socket.write(io)
    end
    config.log.debug "Sent #{socket.byte_count} bytes"
    reply "226 Transfer complete"
  end
end