Class: Ftpd::FtpServer

Inherits:
TlsServer show all
Extended by:
Forwardable
Defined in:
lib/ftpd/ftp_server.rb

Constant Summary collapse

DEFAULT_SERVER_NAME =
'wconrad/ftpd'
DEFAULT_SESSION_TIMEOUT =

seconds

300

Instance Attribute Summary collapse

Attributes inherited from TlsServer

#certfile_path, #tls

Attributes inherited from Server

#interface, #port

Instance Method Summary collapse

Methods inherited from Server

#bound_port, #join, #start, #stop

Constructor Details

#initialize(driver) ⇒ FtpServer

Create a new FTP server. The server won’t start until the #start method is called.

The driver should expose these public methods:

Parameters:

  • driver

    A driver for the server’s dynamic behavior such as authentication and file system access.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ftpd/ftp_server.rb', line 179

def initialize(driver)
  super()
  @driver = driver
  @response_delay = 0
  @list_formatter = ListFormat::Ls
  @auth_level = AUTH_PASSWORD
  @session_timeout = 300
  @server_name = DEFAULT_SERVER_NAME
  @server_version = Release::VERSION
  @allow_low_data_ports = false
  @failed_login_delay = 0
  @nat_ip = nil
  @passive_ports = nil
  self.log = nil
  @connection_tracker = ConnectionTracker.new
  @connection_throttle = ConnectionThrottle.new(@connection_tracker)
end

Instance Attribute Details

#allow_low_data_portsBoolean

If true, allow the PORT command to specify privileged data ports (those below 1024). Defaults to false. Setting this to true makes it easier for an attacker to use the server to attack another server. See RFC 2577 section 3.

Set this before calling #start.

Returns:

  • (Boolean)


22
23
24
# File 'lib/ftpd/ftp_server.rb', line 22

def allow_low_data_ports
  @allow_low_data_ports
end

#auth_levelInteger

The authentication level. One of:

  • Ftpd::AUTH_USER

  • Ftpd::AUTH_PASSWORD (default)

  • Ftpd::AUTH_ACCOUNT

Returns:

  • (Integer)

    The authentication level



32
33
34
# File 'lib/ftpd/ftp_server.rb', line 32

def auth_level
  @auth_level
end

#exception_handlerProc

The exception handler. When there is an unknown exception, server replies 451 and calls exception_handler. If nil, then it’s ignored.

Set this before calling #start.

Returns:

  • (Proc)


161
162
163
# File 'lib/ftpd/ftp_server.rb', line 161

def exception_handler
  @exception_handler
end

#failed_login_delayObject

The delay (in seconds) after a failed login. Defaults to 0. Setting this makes brute force password guessing less efficient for the attacker. RFC-2477 suggests a delay of 5 seconds.



38
39
40
# File 'lib/ftpd/ftp_server.rb', line 38

def 
  @failed_login_delay
end

#list_formatterclass that quacks like Ftpd::ListFormat::Ls

The class for formatting for LIST output. Defaults to ListFormat::Ls (unix “ls -l” style).

Set this before calling #start.

Returns:



46
47
48
# File 'lib/ftpd/ftp_server.rb', line 46

def list_formatter
  @list_formatter
end

#logLogger

The logger. Defaults to nil (no logging).

Set this before calling #start.

Returns:

  • (Logger)


54
55
56
# File 'lib/ftpd/ftp_server.rb', line 54

def log
  @log
end

#max_connectionsInteger

The maximum number of connections the server will allow. Defaults to ConnectionThrottle::DEFAULT_MAX_CONNECTIONS.

Set this before calling #start.

Returns:

  • (Integer)


68
# File 'lib/ftpd/ftp_server.rb', line 68

def_delegator :@connection_throttle, :'max_connections'

#max_connections_per_ipInteger

The maximum number of connections the server will allow from a given IP. Defaults to ConnectionThrottle::DEFAULT_MAX_CONNECTIONS_PER_IP.

Set this before calling #start.

Returns:

  • (Integer)


90
# File 'lib/ftpd/ftp_server.rb', line 90

def_delegator :@connection_throttle, :'max_connections_per_ip'

#max_failed_loginsInteger

The maximum number of failed login attempts before disconnecting the user. Defaults to nil (no maximum). When set, this may makes brute-force password guessing attack less efficient.

Set this before calling #start.

Returns:

  • (Integer)


79
80
81
# File 'lib/ftpd/ftp_server.rb', line 79

def max_failed_logins
  @max_failed_logins
end

#nat_ipnil, String

The advertised public IP for passive mode connections. This is the IP that the client must use to make a connection back to the server. If nil, the IP of the bound interface is used. When the FTP server is behind a firewall, set this to firewall’s public IP and add the appropriate rule to the firewall to forward that IP to the machine that ftpd is running on.

Set this before calling #start.

Returns:

  • (nil, String)


103
104
105
# File 'lib/ftpd/ftp_server.rb', line 103

def nat_ip
  @nat_ip
end

#passive_portsnil, Range

The range of ports for passive mode connections. If nil, then a random etherial port is used. Otherwise, a random port from this range is used.

Set this before calling #start.

Returns:

  • (nil, Range)


112
113
114
# File 'lib/ftpd/ftp_server.rb', line 112

def passive_ports
  @passive_ports
end

#response_delayNumeric

The number of seconds to delay before replying. This is for testing, when you need to test, for example, client timeouts. Defaults to 0 (no delay).

Set this before calling #start.

Returns:

  • (Numeric)


122
123
124
# File 'lib/ftpd/ftp_server.rb', line 122

def response_delay
  @response_delay
end

#server_nameString

The server’s name, sent in a STAT reply. Defaults to DEFAULT_SERVER_NAME.

Set this before calling #start.

Returns:

  • (String)


131
132
133
# File 'lib/ftpd/ftp_server.rb', line 131

def server_name
  @server_name
end

#server_versionString

The server’s version, sent in a STAT reply. Defaults to Release::VERSION.

Set this before calling #start.

Returns:

  • (String)


140
141
142
# File 'lib/ftpd/ftp_server.rb', line 140

def server_version
  @server_version
end

#session_timeoutNumeric

The session timeout. When a session is awaiting a command, if one is not received in this many seconds, the session is disconnected. Defaults to DEFAULT_SESSION_TIMEOUT. If nil, then timeout is disabled.

Set this before calling #start.

Returns:

  • (Numeric)


151
152
153
# File 'lib/ftpd/ftp_server.rb', line 151

def session_timeout
  @session_timeout
end

Instance Method Details

#on_exception(&block) ⇒ Object

Defines the exception_handler.



165
166
167
# File 'lib/ftpd/ftp_server.rb', line 165

def on_exception(&block)
  self.exception_handler = block
end