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,13 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ class CmdSmnt < CommandHandler
6
+
7
+ def cmd_smnt(argument)
8
+ unimplemented_error
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ class CmdStat < CommandHandler
6
+
7
+ def cmd_stat(argument)
8
+ ensure_logged_in
9
+ syntax_error if argument
10
+ reply "211 #{server_name_and_version}"
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ class CmdStor < CommandHandler
6
+
7
+ def cmd_stor(argument)
8
+ close_data_server_socket_when_done do
9
+ ensure_logged_in
10
+ ensure_file_system_supports :write
11
+ path = argument
12
+ syntax_error unless path
13
+ path = File.expand_path(path, name_prefix)
14
+ ensure_accessible path
15
+ ensure_exists File.dirname(path)
16
+ receive_file do |data_socket|
17
+ file_system.write path, data_socket
18
+ end
19
+ reply "226 Transfer complete"
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ class CmdStou < CommandHandler
6
+
7
+ def cmd_stou(argument)
8
+ close_data_server_socket_when_done do
9
+ ensure_logged_in
10
+ ensure_file_system_supports :write
11
+ path = argument || 'ftpd'
12
+ path = File.expand_path(path, name_prefix)
13
+ path = unique_path(path)
14
+ ensure_accessible path
15
+ ensure_exists File.dirname(path)
16
+ receive_file(File.basename(path)) do |data_socket|
17
+ file_system.write path, data_socket
18
+ end
19
+ reply "226 Transfer complete"
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ class CmdStru < CommandHandler
6
+
7
+ def cmd_stru(argument)
8
+ syntax_error unless argument
9
+ ensure_logged_in
10
+ name, implemented = FILE_STRUCTURES[argument]
11
+ error "Invalid structure code", 504 unless name
12
+ error "Structure not implemented", 504 unless implemented
13
+ self.structure = argument
14
+ reply "200 File structure set to #{name}"
15
+ end
16
+
17
+ private
18
+
19
+ FILE_STRUCTURES = {
20
+ 'R'=>['Record', false],
21
+ 'F'=>['File', true],
22
+ 'P'=>['Page', false],
23
+ }
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ # The System (SYST) command.
6
+
7
+ class CmdSyst < CommandHandler
8
+
9
+ def cmd_syst(argument)
10
+ syntax_error if argument
11
+ reply "215 UNIX Type: L8"
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ class CmdType < CommandHandler
6
+
7
+ def cmd_type(argument)
8
+ ensure_logged_in
9
+ syntax_error unless argument =~ /^(\S)(?: (\S+))?$/
10
+ type_code = $1
11
+ format_code = $2
12
+ unless argument =~ /^([AEI]( [NTC])?|L .*)$/
13
+ error 'Invalid type code', 504
14
+ end
15
+ case argument
16
+ when /^A( [NT])?$/
17
+ self.data_type = 'A'
18
+ when /^(I|L 8)$/
19
+ self.data_type = 'I'
20
+ else
21
+ error 'Type not implemented', 504
22
+ end
23
+ reply "200 Type set to #{data_type}"
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,90 @@
1
+ require_relative 'data_connection_helper'
2
+ require_relative 'error'
3
+ require_relative 'file_system_helper'
4
+
5
+ module Ftpd
6
+
7
+ # Command handler base class
8
+
9
+ class CommandHandler
10
+
11
+ extend Forwardable
12
+
13
+ include DataConnectionHelper
14
+ include Error
15
+ include FileSystemHelper
16
+
17
+ COMMAND_FILENAME_PREFIX = 'cmd_'
18
+ COMMAND_KLASS_PREFIX = 'Cmd'
19
+ COMMAND_METHOD_PREFIX = 'cmd_'
20
+
21
+ # param session [Session] The session
22
+
23
+ def initialize(session)
24
+ @session = session
25
+ end
26
+
27
+ # Return the commands implemented by this handler. For example,
28
+ # if the handler has the method "cmd_allo", this returns ['allo'].
29
+
30
+ class << self
31
+ include Memoizer
32
+ def commands
33
+ public_instance_methods.map(&:to_s).grep(/#{COMMAND_METHOD_PREFIX}/).map do |method|
34
+ method.gsub(/^#{COMMAND_METHOD_PREFIX}/, '')
35
+ end
36
+ end
37
+ memoize :commands
38
+ end
39
+
40
+ def_delegator 'self.class', :commands
41
+
42
+ private
43
+
44
+ attr_reader :session
45
+
46
+ # Forward methods to the session
47
+
48
+ def_delegators :@session,
49
+ :close_data_server_socket,
50
+ :command_not_needed,
51
+ :config,
52
+ :data_channel_protection_level,
53
+ :data_channel_protection_level=,
54
+ :data_hostname,
55
+ :data_port,
56
+ :data_server,
57
+ :data_server=,
58
+ :data_type,
59
+ :data_type=,
60
+ :ensure_logged_in,
61
+ :ensure_not_epsv_all,
62
+ :ensure_protocol_supported,
63
+ :ensure_tls_supported,
64
+ :epsv_all=,
65
+ :execute_command,
66
+ :expect,
67
+ :file_system,
68
+ :list,
69
+ :list_path,
70
+ :logged_in,
71
+ :logged_in=,
72
+ :login,
73
+ :mode=,
74
+ :name_list,
75
+ :name_prefix,
76
+ :name_prefix=,
77
+ :protection_buffer_size_set,
78
+ :protection_buffer_size_set=,
79
+ :pwd,
80
+ :reply,
81
+ :server_name_and_version,
82
+ :set_active_mode_address,
83
+ :socket,
84
+ :structure=,
85
+ :supported_commands,
86
+ :tls_enabled?
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,51 @@
1
+ module Ftpd
2
+
3
+ class CommandHandlerFactory
4
+
5
+ def self.standard_command_handlers
6
+ [
7
+ CmdAbor,
8
+ CmdAllo,
9
+ CmdAppe,
10
+ CmdAuth,
11
+ CmdCdup,
12
+ CmdCwd,
13
+ CmdDele,
14
+ CmdEprt,
15
+ CmdEpsv,
16
+ CmdFeat,
17
+ CmdHelp,
18
+ CmdList,
19
+ CmdLogin,
20
+ CmdMdtm,
21
+ CmdMkd,
22
+ CmdMode,
23
+ CmdNlst,
24
+ CmdNoop,
25
+ CmdOpts,
26
+ CmdPasv,
27
+ CmdPbsz,
28
+ CmdPort,
29
+ CmdProt,
30
+ CmdPwd,
31
+ CmdQuit,
32
+ CmdRein,
33
+ CmdRename,
34
+ CmdRest,
35
+ CmdRetr,
36
+ CmdRmd,
37
+ CmdSite,
38
+ CmdSize,
39
+ CmdSmnt,
40
+ CmdStat,
41
+ CmdStor,
42
+ CmdStou,
43
+ CmdStru,
44
+ CmdSyst,
45
+ CmdType,
46
+ ]
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,60 @@
1
+ module Ftpd
2
+
3
+ # All FTP commands which the server supports are dispatched by this
4
+ # class.
5
+
6
+ class CommandHandlers
7
+
8
+ def initialize
9
+ @commands = {}
10
+ end
11
+
12
+ # Add a command handler
13
+ #
14
+ # @param command_handler [Command]
15
+
16
+ def <<(command_handler)
17
+ command_handler.commands.each do |command|
18
+ @commands[command] = command_handler
19
+ end
20
+ end
21
+
22
+ # @param command [String] the command (e.g. "STOR"). Case
23
+ # insensitive.
24
+ # @return truthy if the server supports the command.
25
+
26
+ def has?(command)
27
+ command = canonical_command(command)
28
+ @commands.has_key?(command)
29
+ end
30
+
31
+ # Dispatch a command to the appropriate command handler.
32
+ #
33
+ # @param command [String] the command (e.g. "STOR"). Case
34
+ # insensitive.
35
+ # @param argument [String] The argument, or nil if there isn't
36
+ # one.
37
+
38
+ def execute(command, argument)
39
+ command = canonical_command(command)
40
+ method = "cmd_#{command}"
41
+ @commands[command.downcase].send(method, argument)
42
+ end
43
+
44
+ # Return the sorted list of commands supported by this handler
45
+ #
46
+ # @return [Array<String>] Lowercase command
47
+
48
+ def commands
49
+ @commands.keys.sort
50
+ end
51
+
52
+ private
53
+
54
+ def canonical_command(command)
55
+ command.downcase
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,80 @@
1
+ module Ftpd
2
+
3
+ class CommandLoop
4
+
5
+ extend Forwardable
6
+
7
+ include Error
8
+
9
+ def initialize(session)
10
+ @session = session
11
+ end
12
+
13
+ def read_and_execute_commands
14
+ catch :done do
15
+ begin
16
+ reply "220 #{server_name_and_version}"
17
+ loop do
18
+ begin
19
+ s = get_command
20
+ s = process_telnet_sequences(s)
21
+ syntax_error unless s =~ /^(\w+)(?: (.*))?$/
22
+ command, argument = $1.downcase, $2
23
+ unless valid_command?(command)
24
+ error "Syntax error, command unrecognized: #{s.chomp}", 500
25
+ end
26
+ command_sequence_checker.check command
27
+ execute_command command, argument
28
+ rescue FtpServerError => e
29
+ reply e.message_with_code
30
+ rescue => e
31
+ reply "451 Requested action aborted. Local error in processing."
32
+ config.exception_handler.call(e) unless config.exception_handler.nil?
33
+ end
34
+ end
35
+ rescue Errno::ECONNRESET, Errno::EPIPE
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def_delegators :@session,
43
+ :command_sequence_checker,
44
+ :config,
45
+ :execute_command,
46
+ :reply,
47
+ :server_name_and_version,
48
+ :socket,
49
+ :valid_command?
50
+
51
+ def get_command
52
+ s = gets_with_timeout(socket)
53
+ throw :done if s.nil?
54
+ s = s.chomp
55
+ config.log.debug s
56
+ s
57
+ end
58
+
59
+ def gets_with_timeout(socket)
60
+ ready = IO.select([socket], nil, nil, config.session_timeout)
61
+ timeout if ready.nil?
62
+ ready[0].first.gets
63
+ end
64
+
65
+ def timeout
66
+ reply '421 Control connection timed out.'
67
+ throw :done
68
+ end
69
+
70
+ def process_telnet_sequences(s)
71
+ telnet = Telnet.new(s)
72
+ unless telnet.reply.empty?
73
+ socket.write telnet.reply
74
+ end
75
+ telnet.plain
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,58 @@
1
+ # Some commands are supposed to occur in sequence. For example, USER
2
+ # must be immediately followed by PASS. This class keeps track of
3
+ # when a specific command either must arrive or must not arrive, and
4
+ # raises a "bad sequence" error when commands arrive in the wrong
5
+ # sequence.
6
+
7
+ module Ftpd
8
+ class CommandSequenceChecker
9
+
10
+ include Error
11
+
12
+ def initialize
13
+ @must_expect = []
14
+ @expected_command = nil
15
+ end
16
+
17
+ # Set the command to expect next. If not set, then any command
18
+ # will be accepted, so long as it hasn't been registered using
19
+ # {#must_expect}. Otherwise, the set command must be next or a
20
+ # sequence error will result.
21
+ #
22
+ # @param command [String] The command. Must be lowercase.
23
+
24
+ def expect(command)
25
+ @expected_command = command
26
+ end
27
+
28
+ # Register a command that must be expected. When that command is
29
+ # received without {#expect} having been called for it, a sequence
30
+ # error will result.
31
+
32
+ def must_expect(command)
33
+ @must_expect << command
34
+ end
35
+
36
+ # Check a command. If expecting a specific command and this
37
+ # command isn't it, then raise an error that will cause a "503 Bad
38
+ # sequence" error to be sent. After checking, the expected
39
+ # command is cleared and any command will be accepted until
40
+ # {#expect} is called again.
41
+ #
42
+ # @param command [String] The command. Must be lowercase.
43
+ # @raise [FtpServerError] A "503 Bad sequence" error
44
+
45
+ def check(command)
46
+ if @expected_command
47
+ begin
48
+ sequence_error unless command == @expected_command
49
+ ensure
50
+ @expected_command = nil
51
+ end
52
+ else
53
+ sequence_error if @must_expect.include?(command)
54
+ end
55
+ end
56
+
57
+ end
58
+ end