investtools-ftpd 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +5 -0
  3. data/.yardopts +7 -0
  4. data/Changelog.md +310 -0
  5. data/Gemfile +15 -0
  6. data/Gemfile.lock +93 -0
  7. data/LICENSE.md +9 -0
  8. data/README.md +371 -0
  9. data/Rakefile +14 -0
  10. data/VERSION +1 -0
  11. data/doc/benchmarks.md +82 -0
  12. data/doc/references.md +66 -0
  13. data/doc/rfc-compliance.md +292 -0
  14. data/examples/example.rb +275 -0
  15. data/examples/example_spec.rb +93 -0
  16. data/examples/hello_world.rb +32 -0
  17. data/features/example/eplf.feature +14 -0
  18. data/features/example/example.feature +18 -0
  19. data/features/example/read_only.feature +63 -0
  20. data/features/example/step_definitions/example_server.rb +11 -0
  21. data/features/ftp_server/abort.feature +13 -0
  22. data/features/ftp_server/allo.feature +33 -0
  23. data/features/ftp_server/append.feature +94 -0
  24. data/features/ftp_server/cdup.feature +36 -0
  25. data/features/ftp_server/command_errors.feature +13 -0
  26. data/features/ftp_server/concurrent_sessions.feature +14 -0
  27. data/features/ftp_server/delay_after_failed_login.feature +23 -0
  28. data/features/ftp_server/delete.feature +60 -0
  29. data/features/ftp_server/directory_navigation.feature +59 -0
  30. data/features/ftp_server/disconnect_after_failed_logins.feature +25 -0
  31. data/features/ftp_server/eprt.feature +55 -0
  32. data/features/ftp_server/epsv.feature +36 -0
  33. data/features/ftp_server/features.feature +38 -0
  34. data/features/ftp_server/file_structure.feature +43 -0
  35. data/features/ftp_server/get.feature +80 -0
  36. data/features/ftp_server/get_ipv6.feature +43 -0
  37. data/features/ftp_server/get_tls.feature +23 -0
  38. data/features/ftp_server/help.feature +21 -0
  39. data/features/ftp_server/implicit_tls.feature +23 -0
  40. data/features/ftp_server/invertability.feature +15 -0
  41. data/features/ftp_server/list.feature +94 -0
  42. data/features/ftp_server/list_tls.feature +29 -0
  43. data/features/ftp_server/logging.feature +11 -0
  44. data/features/ftp_server/login_auth_level_account.feature +51 -0
  45. data/features/ftp_server/login_auth_level_password.feature +59 -0
  46. data/features/ftp_server/login_auth_level_user.feature +31 -0
  47. data/features/ftp_server/max_connections.feature +39 -0
  48. data/features/ftp_server/mdtm.feature +53 -0
  49. data/features/ftp_server/mkdir.feature +70 -0
  50. data/features/ftp_server/mode.feature +43 -0
  51. data/features/ftp_server/name_list.feature +77 -0
  52. data/features/ftp_server/name_list_tls.feature +30 -0
  53. data/features/ftp_server/noop.feature +17 -0
  54. data/features/ftp_server/options.feature +17 -0
  55. data/features/ftp_server/pasv.feature +23 -0
  56. data/features/ftp_server/port.feature +49 -0
  57. data/features/ftp_server/put.feature +79 -0
  58. data/features/ftp_server/put_tls.feature +23 -0
  59. data/features/ftp_server/put_unique.feature +56 -0
  60. data/features/ftp_server/quit.feature +23 -0
  61. data/features/ftp_server/reinitialize.feature +13 -0
  62. data/features/ftp_server/rename.feature +97 -0
  63. data/features/ftp_server/rmdir.feature +71 -0
  64. data/features/ftp_server/site.feature +13 -0
  65. data/features/ftp_server/size.feature +69 -0
  66. data/features/ftp_server/status.feature +18 -0
  67. data/features/ftp_server/step_definitions/logging.rb +8 -0
  68. data/features/ftp_server/step_definitions/test_server.rb +65 -0
  69. data/features/ftp_server/structure_mount.feature +13 -0
  70. data/features/ftp_server/syntax_errors.feature +18 -0
  71. data/features/ftp_server/syst.feature +18 -0
  72. data/features/ftp_server/timeout.feature +26 -0
  73. data/features/ftp_server/type.feature +59 -0
  74. data/features/step_definitions/append.rb +15 -0
  75. data/features/step_definitions/client.rb +24 -0
  76. data/features/step_definitions/client_and_server_files.rb +24 -0
  77. data/features/step_definitions/client_files.rb +14 -0
  78. data/features/step_definitions/command.rb +5 -0
  79. data/features/step_definitions/connect.rb +37 -0
  80. data/features/step_definitions/delete.rb +15 -0
  81. data/features/step_definitions/directory_navigation.rb +26 -0
  82. data/features/step_definitions/error_replies.rb +115 -0
  83. data/features/step_definitions/features.rb +21 -0
  84. data/features/step_definitions/file_structure.rb +16 -0
  85. data/features/step_definitions/generic_send.rb +9 -0
  86. data/features/step_definitions/get.rb +16 -0
  87. data/features/step_definitions/help.rb +18 -0
  88. data/features/step_definitions/invalid_commands.rb +11 -0
  89. data/features/step_definitions/line_endings.rb +7 -0
  90. data/features/step_definitions/list.rb +73 -0
  91. data/features/step_definitions/login.rb +82 -0
  92. data/features/step_definitions/mkdir.rb +9 -0
  93. data/features/step_definitions/mode.rb +15 -0
  94. data/features/step_definitions/mtime.rb +23 -0
  95. data/features/step_definitions/noop.rb +15 -0
  96. data/features/step_definitions/options.rb +9 -0
  97. data/features/step_definitions/passive.rb +3 -0
  98. data/features/step_definitions/pending.rb +3 -0
  99. data/features/step_definitions/port.rb +5 -0
  100. data/features/step_definitions/put.rb +29 -0
  101. data/features/step_definitions/quit.rb +15 -0
  102. data/features/step_definitions/rename.rb +11 -0
  103. data/features/step_definitions/rmdir.rb +9 -0
  104. data/features/step_definitions/server_files.rb +61 -0
  105. data/features/step_definitions/server_title.rb +12 -0
  106. data/features/step_definitions/size.rb +20 -0
  107. data/features/step_definitions/status.rb +9 -0
  108. data/features/step_definitions/success_replies.rb +7 -0
  109. data/features/step_definitions/system.rb +7 -0
  110. data/features/step_definitions/timing.rb +19 -0
  111. data/features/step_definitions/type.rb +15 -0
  112. data/features/support/env.rb +4 -0
  113. data/features/support/example_server.rb +67 -0
  114. data/features/support/file_templates/ascii_unix +4 -0
  115. data/features/support/file_templates/ascii_windows +4 -0
  116. data/features/support/file_templates/binary +0 -0
  117. data/features/support/test_client.rb +250 -0
  118. data/features/support/test_file_templates.rb +33 -0
  119. data/features/support/test_server.rb +293 -0
  120. data/features/support/test_server_files.rb +57 -0
  121. data/ftpd.gemspec +283 -0
  122. data/insecure-test-cert.pem +29 -0
  123. data/investtools-ftpd.gemspec +284 -0
  124. data/lib/ftpd.rb +86 -0
  125. data/lib/ftpd/auth_levels.rb +9 -0
  126. data/lib/ftpd/cmd_abor.rb +13 -0
  127. data/lib/ftpd/cmd_allo.rb +20 -0
  128. data/lib/ftpd/cmd_appe.rb +24 -0
  129. data/lib/ftpd/cmd_auth.rb +21 -0
  130. data/lib/ftpd/cmd_cdup.rb +16 -0
  131. data/lib/ftpd/cmd_cwd.rb +20 -0
  132. data/lib/ftpd/cmd_dele.rb +21 -0
  133. data/lib/ftpd/cmd_eprt.rb +23 -0
  134. data/lib/ftpd/cmd_epsv.rb +30 -0
  135. data/lib/ftpd/cmd_feat.rb +44 -0
  136. data/lib/ftpd/cmd_help.rb +29 -0
  137. data/lib/ftpd/cmd_list.rb +33 -0
  138. data/lib/ftpd/cmd_login.rb +60 -0
  139. data/lib/ftpd/cmd_mdtm.rb +27 -0
  140. data/lib/ftpd/cmd_mkd.rb +23 -0
  141. data/lib/ftpd/cmd_mode.rb +27 -0
  142. data/lib/ftpd/cmd_nlst.rb +27 -0
  143. data/lib/ftpd/cmd_noop.rb +14 -0
  144. data/lib/ftpd/cmd_opts.rb +14 -0
  145. data/lib/ftpd/cmd_pasv.rb +28 -0
  146. data/lib/ftpd/cmd_pbsz.rb +23 -0
  147. data/lib/ftpd/cmd_port.rb +28 -0
  148. data/lib/ftpd/cmd_prot.rb +34 -0
  149. data/lib/ftpd/cmd_pwd.rb +15 -0
  150. data/lib/ftpd/cmd_quit.rb +18 -0
  151. data/lib/ftpd/cmd_rein.rb +13 -0
  152. data/lib/ftpd/cmd_rename.rb +32 -0
  153. data/lib/ftpd/cmd_rest.rb +13 -0
  154. data/lib/ftpd/cmd_retr.rb +24 -0
  155. data/lib/ftpd/cmd_rmd.rb +22 -0
  156. data/lib/ftpd/cmd_site.rb +13 -0
  157. data/lib/ftpd/cmd_size.rb +29 -0
  158. data/lib/ftpd/cmd_smnt.rb +13 -0
  159. data/lib/ftpd/cmd_stat.rb +15 -0
  160. data/lib/ftpd/cmd_stor.rb +25 -0
  161. data/lib/ftpd/cmd_stou.rb +25 -0
  162. data/lib/ftpd/cmd_stru.rb +27 -0
  163. data/lib/ftpd/cmd_syst.rb +16 -0
  164. data/lib/ftpd/cmd_type.rb +28 -0
  165. data/lib/ftpd/command_handler.rb +90 -0
  166. data/lib/ftpd/command_handler_factory.rb +51 -0
  167. data/lib/ftpd/command_handlers.rb +60 -0
  168. data/lib/ftpd/command_loop.rb +80 -0
  169. data/lib/ftpd/command_sequence_checker.rb +58 -0
  170. data/lib/ftpd/config.rb +13 -0
  171. data/lib/ftpd/connection_throttle.rb +56 -0
  172. data/lib/ftpd/connection_tracker.rb +82 -0
  173. data/lib/ftpd/data_connection_helper.rb +123 -0
  174. data/lib/ftpd/disk_file_system.rb +434 -0
  175. data/lib/ftpd/error.rb +21 -0
  176. data/lib/ftpd/exception_translator.rb +32 -0
  177. data/lib/ftpd/exceptions.rb +62 -0
  178. data/lib/ftpd/file_info.rb +115 -0
  179. data/lib/ftpd/file_system_helper.rb +67 -0
  180. data/lib/ftpd/ftp_server.rb +214 -0
  181. data/lib/ftpd/gets_peer_address.rb +41 -0
  182. data/lib/ftpd/insecure_certificate.rb +16 -0
  183. data/lib/ftpd/list_format/eplf.rb +74 -0
  184. data/lib/ftpd/list_format/ls.rb +154 -0
  185. data/lib/ftpd/list_path.rb +28 -0
  186. data/lib/ftpd/null_logger.rb +22 -0
  187. data/lib/ftpd/protocols.rb +60 -0
  188. data/lib/ftpd/read_only_disk_file_system.rb +22 -0
  189. data/lib/ftpd/server.rb +139 -0
  190. data/lib/ftpd/session.rb +220 -0
  191. data/lib/ftpd/session_config.rb +111 -0
  192. data/lib/ftpd/stream.rb +80 -0
  193. data/lib/ftpd/telnet.rb +114 -0
  194. data/lib/ftpd/temp_dir.rb +22 -0
  195. data/lib/ftpd/tls_server.rb +111 -0
  196. data/lib/ftpd/translate_exceptions.rb +68 -0
  197. data/rake_tasks/cucumber.rake +9 -0
  198. data/rake_tasks/default.rake +1 -0
  199. data/rake_tasks/jeweler.rake +52 -0
  200. data/rake_tasks/spec.rake +3 -0
  201. data/rake_tasks/test.rake +2 -0
  202. data/rake_tasks/yard.rake +3 -0
  203. data/spec/command_sequence_checker_spec.rb +83 -0
  204. data/spec/connection_throttle_spec.rb +99 -0
  205. data/spec/connection_tracker_spec.rb +97 -0
  206. data/spec/disk_file_system_spec.rb +320 -0
  207. data/spec/exception_translator_spec.rb +36 -0
  208. data/spec/file_info_spec.rb +59 -0
  209. data/spec/ftp_server_error_spec.rb +13 -0
  210. data/spec/list_format/eplf_spec.rb +61 -0
  211. data/spec/list_format/ls_spec.rb +270 -0
  212. data/spec/list_path_spec.rb +21 -0
  213. data/spec/null_logger_spec.rb +24 -0
  214. data/spec/protocols_spec.rb +139 -0
  215. data/spec/server_spec.rb +81 -0
  216. data/spec/spec_helper.rb +15 -0
  217. data/spec/telnet_spec.rb +75 -0
  218. data/spec/translate_exceptions_spec.rb +40 -0
  219. metadata +404 -0
@@ -0,0 +1,21 @@
1
+ module Ftpd
2
+ module Error
3
+
4
+ def error(message, code)
5
+ raise FtpServerError.new(message, code)
6
+ end
7
+
8
+ def unimplemented_error
9
+ error "Command not implemented", 502
10
+ end
11
+
12
+ def sequence_error
13
+ error "Bad sequence of commands", 503
14
+ end
15
+
16
+ def syntax_error
17
+ error "Syntax error", 501
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module Ftpd
2
+
3
+ # Translate specific exceptions to FileSystemError.
4
+ #
5
+ # This is not intended to be used directly, but via the
6
+ # TranslateExceptions module.
7
+
8
+ class ExceptionTranslator
9
+
10
+ def initialize
11
+ @exceptions = []
12
+ end
13
+
14
+ # Register an exception class.
15
+
16
+ def register_exception(e)
17
+ @exceptions << e
18
+ end
19
+
20
+ # Run a block, translating specific exceptions to FileSystemError.
21
+
22
+ def translate_exceptions
23
+ begin
24
+ return yield
25
+ rescue *@exceptions => e
26
+ raise PermanentFileSystemError, e.message
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,62 @@
1
+ module Ftpd
2
+
3
+ # All errors (purposefully) generated by this library driver from
4
+ # this class.
5
+
6
+ # Any error that send a reply to the client raises a FtpServerError.
7
+ # The message is the text to send (e.g. "Syntax error") and the code
8
+ # is the FTP response code to send (e.g. "502"). This is typically not
9
+ # raised directly, but using the Error mixin.
10
+
11
+ class FtpServerError < StandardError
12
+ attr_reader :code
13
+
14
+ def initialize(message, code)
15
+ @code = code
16
+ raise ArgumentError, "Invalid response code" unless valid_response_code?
17
+
18
+ super(message)
19
+ end
20
+
21
+ def message_with_code
22
+ "#{code} #{message}"
23
+ end
24
+
25
+ private
26
+ def valid_response_code?
27
+ (400..599).cover?(code)
28
+ end
29
+ end
30
+
31
+ # A permanent file system error. The file isn't there, etc.
32
+
33
+ class PermanentFileSystemError < FtpServerError
34
+ def initialize(message, code = 550)
35
+ super
36
+ end
37
+
38
+ private
39
+ def valid_response_code?
40
+ (550..559).cover?(code)
41
+ end
42
+ end
43
+
44
+ # A transient file system error. The file is busy, etc.
45
+
46
+ class TransientFileSystemError < FtpServerError
47
+ def initialize(message, code = 450)
48
+ super
49
+ end
50
+
51
+ private
52
+ def valid_response_code?
53
+ (450..459).cover?(code)
54
+ end
55
+ end
56
+
57
+ # A permanent file system error. Deprecated; use
58
+ # PermanentFileSystemError instead.
59
+
60
+ class FileSystemError < PermanentFileSystemError ; end
61
+
62
+ end
@@ -0,0 +1,115 @@
1
+ module Ftpd
2
+
3
+ # Information about a file object (file, directory, symlink, etc.)
4
+
5
+ class FileInfo
6
+
7
+ # @return [String] The file's type, as returned by File.lstat
8
+ # One of:
9
+ # * 'file'
10
+ # * 'directory'
11
+ # * 'characterSpecial'
12
+ # * 'blockSpecial'
13
+ # * 'fifo'
14
+ # * 'link'
15
+ # * 'socket'
16
+ # * 'unknown'
17
+
18
+ attr_reader :ftype
19
+
20
+ # @return [String] The group name
21
+
22
+ attr_reader :group
23
+
24
+ # @return [Integer] The mode bits, as returned by File::Stat#mode
25
+ # The bits are:
26
+ # * 0 - others have execute permission
27
+ # * 1 - others have write permission
28
+ # * 2 - others have read permission
29
+ # * 3 - group has execute permission
30
+ # * 4 - group has write permission
31
+ # * 5 - group has read permission
32
+ # * 6 - owner has execute permission
33
+ # * 7 - owner has write permission
34
+ # * 8 - owner has read permission
35
+ # * 9 - sticky bit
36
+ # * 10 - set-group-ID bit
37
+ # * 11 - set UID bit
38
+ # Other bits may be present; they are ignored
39
+
40
+ attr_reader :mode
41
+
42
+ # @return [Time] The modification time
43
+
44
+ attr_reader :mtime
45
+
46
+ # @return [Integer] The number of hard links
47
+
48
+ attr_reader :nlink
49
+
50
+ # @return [String] The owner name
51
+
52
+ attr_reader :owner
53
+
54
+ # @return [Integer] The size, in bytes
55
+
56
+ attr_reader :size
57
+
58
+ # @return [String] The object's path
59
+
60
+ attr_reader :path
61
+
62
+ # @return [String] The object's identifier
63
+ #
64
+ # This uniquely identifies the file: Two objects with the same
65
+ # identifier are expected to refer to the same file or directory.
66
+ #
67
+ # On a disk file system, might be _dev_._inode_,
68
+ # e.g. "8388621.48598"
69
+ #
70
+ # This is optional and does not have to be set. If set, it is
71
+ # used in EPLF output.
72
+
73
+ attr_reader :identifier
74
+
75
+ # Create a new instance. See the various attributes for argument
76
+ # details.
77
+ #
78
+ # @param opts [Hash] The file attributes
79
+ # @option opts [String] :ftype The file type
80
+ # @option opts [String] :group The group name
81
+ # @option opts [String] :identifier The object's identifier
82
+ # @option opts [Integer] :mode The mode bits
83
+ # @option opts [Time] :mtime The modification time
84
+ # @option opts [Integer] :nlink The number of hard links
85
+ # @option opts [String] :owner The owner name
86
+ # @option opts [Integer] :size The size
87
+ # @option opts [String] :path The object's path
88
+
89
+ def initialize(opts)
90
+ @ftype = opts[:ftype]
91
+ @group = opts[:group]
92
+ @identifier = opts[:identifier]
93
+ @mode = opts[:mode]
94
+ @mtime = opts[:mtime]
95
+ @nlink = opts[:nlink]
96
+ @owner = opts[:owner]
97
+ @path = opts[:path]
98
+ @size = opts[:size]
99
+ end
100
+
101
+ # @return true if the object is a file
102
+
103
+ def file?
104
+ @ftype == 'file'
105
+ end
106
+
107
+ # @return true if the object is a directory
108
+
109
+ def directory?
110
+ @ftype == 'directory'
111
+ end
112
+
113
+ end
114
+
115
+ end
@@ -0,0 +1,67 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ module FileSystemHelper
6
+
7
+ def path_list(path)
8
+ if file_system.directory?(path)
9
+ path = File.join(path, '*')
10
+ end
11
+ file_system.dir(path).sort
12
+ end
13
+
14
+ def ensure_file_system_supports(method)
15
+ unless file_system.respond_to?(method)
16
+ unimplemented_error
17
+ end
18
+ end
19
+
20
+ def ensure_accessible(path)
21
+ unless file_system.accessible?(path)
22
+ error 'Access denied', 550
23
+ end
24
+ end
25
+
26
+ def ensure_exists(path)
27
+ unless file_system.exists?(path)
28
+ error 'No such file or directory', 550
29
+ end
30
+ end
31
+
32
+ def ensure_does_not_exist(path)
33
+ if file_system.exists?(path)
34
+ error 'Already exists', 550
35
+ end
36
+ end
37
+
38
+ def ensure_directory(path)
39
+ unless file_system.directory?(path)
40
+ error 'Not a directory', 550
41
+ end
42
+ end
43
+
44
+ def unique_path(path)
45
+ suffix = nil
46
+ 100.times do
47
+ path_with_suffix = [path, suffix].compact.join('.')
48
+ unless file_system.exists?(path_with_suffix)
49
+ return path_with_suffix
50
+ end
51
+ suffix = generate_suffix
52
+ end
53
+ raise "Unable to find unique path"
54
+ end
55
+
56
+ private
57
+
58
+ def generate_suffix
59
+ set = ('a'..'z').to_a
60
+ 8.times.map do
61
+ set[rand(set.size)]
62
+ end.join
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,214 @@
1
+ require_relative 'tls_server'
2
+
3
+ module Ftpd
4
+ class FtpServer < TlsServer
5
+
6
+ extend Forwardable
7
+
8
+ DEFAULT_SERVER_NAME = 'wconrad/ftpd'
9
+ DEFAULT_SESSION_TIMEOUT = 300 # seconds
10
+
11
+ # If true, allow the PORT command to specify privileged data ports
12
+ # (those below 1024). Defaults to false. Setting this to true
13
+ # makes it easier for an attacker to use the server to attack
14
+ # another server. See RFC 2577 section 3.
15
+ #
16
+ # Set this before calling #start.
17
+ #
18
+ # @return [Boolean]
19
+
20
+ attr_accessor :allow_low_data_ports
21
+
22
+ # The authentication level. One of:
23
+ #
24
+ # * Ftpd::AUTH_USER
25
+ # * Ftpd::AUTH_PASSWORD (default)
26
+ # * Ftpd::AUTH_ACCOUNT
27
+ #
28
+ # @return [Integer] The authentication level
29
+
30
+ attr_accessor :auth_level
31
+
32
+ # The delay (in seconds) after a failed login. Defaults to 0.
33
+ # Setting this makes brute force password guessing less efficient
34
+ # for the attacker. RFC-2477 suggests a delay of 5 seconds.
35
+
36
+ attr_accessor :failed_login_delay
37
+
38
+ # The class for formatting for LIST output. Defaults to
39
+ # {Ftpd::ListFormat::Ls} (unix "ls -l" style).
40
+ #
41
+ # Set this before calling #start.
42
+ # @return [class that quacks like Ftpd::ListFormat::Ls]
43
+
44
+ attr_accessor :list_formatter
45
+
46
+ # The logger. Defaults to nil (no logging).
47
+ #
48
+ # Set this before calling #start.
49
+ #
50
+ # @return [Logger]
51
+
52
+ attr_reader :log
53
+
54
+ def log=(logger)
55
+ @log = logger || NullLogger.new
56
+ end
57
+
58
+ # The maximum number of connections the server will allow.
59
+ # Defaults to {ConnectionThrottle::DEFAULT_MAX_CONNECTIONS}.
60
+ #
61
+ # Set this before calling #start.
62
+ #
63
+ # @!attribute max_connections
64
+ # @return [Integer]
65
+
66
+ def_delegator :@connection_throttle, :'max_connections'
67
+ def_delegator :@connection_throttle, :'max_connections='
68
+
69
+ # The maximum number of failed login attempts before disconnecting
70
+ # the user. Defaults to nil (no maximum). When set, this may
71
+ # makes brute-force password guessing attack less efficient.
72
+ #
73
+ # Set this before calling #start.
74
+ #
75
+ # @return [Integer]
76
+
77
+ attr_accessor :max_failed_logins
78
+
79
+ # The maximum number of connections the server will allow from a
80
+ # given IP. Defaults to
81
+ # {ConnectionThrottle::DEFAULT_MAX_CONNECTIONS_PER_IP}.
82
+ #
83
+ # Set this before calling #start.
84
+ #
85
+ # @!attribute max_connections_per_ip
86
+ # @return [Integer]
87
+
88
+ def_delegator :@connection_throttle, :'max_connections_per_ip'
89
+ def_delegator :@connection_throttle, :'max_connections_per_ip='
90
+
91
+ # The number of seconds to delay before replying. This is for
92
+ # testing, when you need to test, for example, client timeouts.
93
+ # Defaults to 0 (no delay).
94
+ #
95
+ # Set this before calling #start.
96
+ #
97
+ # @return [Numeric]
98
+
99
+ attr_accessor :response_delay
100
+
101
+ # The server's name, sent in a STAT reply. Defaults to
102
+ # {DEFAULT_SERVER_NAME}.
103
+ #
104
+ # Set this before calling #start.
105
+ #
106
+ # @return [String]
107
+
108
+ attr_accessor :server_name
109
+
110
+ # The server's version, sent in a STAT reply. Defaults to the
111
+ # contents of the VERSION file.
112
+ #
113
+ # Set this before calling #start.
114
+ #
115
+ # @return [String]
116
+
117
+ attr_accessor :server_version
118
+
119
+ # The session timeout. When a session is awaiting a command, if
120
+ # one is not received in this many seconds, the session is
121
+ # disconnected. Defaults to {DEFAULT_SESSION_TIMEOUT}. If nil,
122
+ # then timeout is disabled.
123
+ #
124
+ # Set this before calling #start.
125
+ #
126
+ # @return [Numeric]
127
+
128
+ attr_accessor :session_timeout
129
+
130
+ # The exception handler. When there is an unknown exception,
131
+ # server replies 451 and calls exception_handler. If nil,
132
+ # then it's ignored.
133
+ #
134
+ # @return [Proc]
135
+
136
+ attr_accessor :exception_handler
137
+
138
+ # Defines the exception_handler.
139
+
140
+ def on_exception(&block)
141
+ self.exception_handler = block
142
+ end
143
+
144
+ # Create a new FTP server. The server won't start until the
145
+ # #start method is called.
146
+ #
147
+ # @param driver A driver for the server's dynamic behavior such as
148
+ # authentication and file system access.
149
+ #
150
+ # The driver should expose these public methods:
151
+ # * {Example::Driver#authenticate authenticate}
152
+ # * {Example::Driver#file_system file_system}
153
+
154
+ def initialize(driver)
155
+ super()
156
+ @driver = driver
157
+ @response_delay = 0
158
+ @list_formatter = ListFormat::Ls
159
+ @auth_level = AUTH_PASSWORD
160
+ @session_timeout = 300
161
+ @server_name = DEFAULT_SERVER_NAME
162
+ @server_version = read_version_file
163
+ @allow_low_data_ports = false
164
+ @failed_login_delay = 0
165
+ self.log = nil
166
+ @connection_tracker = ConnectionTracker.new
167
+ @connection_throttle = ConnectionThrottle.new(@connection_tracker)
168
+ end
169
+
170
+ private
171
+
172
+ def allow_session?(socket)
173
+ @connection_throttle.allow?(socket)
174
+ end
175
+
176
+ def deny_session socket
177
+ @connection_throttle.deny socket
178
+ end
179
+
180
+ def session(socket)
181
+ @connection_tracker.track(socket) do
182
+ run_session socket
183
+ end
184
+ end
185
+
186
+ def run_session(socket)
187
+ config = SessionConfig.new
188
+ config.allow_low_data_ports = @allow_low_data_ports
189
+ config.auth_level = @auth_level
190
+ config.driver = @driver
191
+ config.failed_login_delay = @failed_login_delay
192
+ config.list_formatter = @list_formatter
193
+ config.log = @log
194
+ config.max_failed_logins = @max_failed_logins
195
+ config.response_delay = response_delay
196
+ config.server_name = @server_name
197
+ config.server_version = @server_version
198
+ config.session_timeout = @session_timeout
199
+ config.tls = @tls
200
+ config.exception_handler = exception_handler
201
+ session = Session.new(config, socket)
202
+ session.run
203
+ end
204
+
205
+ def read_version_file
206
+ File.open(version_file_path, 'r', &:read).strip
207
+ end
208
+
209
+ def version_file_path
210
+ File.expand_path('../../VERSION', File.dirname(__FILE__))
211
+ end
212
+
213
+ end
214
+ end