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
+ module Ftpd
2
+ class Config
3
+
4
+ # The number of seconds to delay before replying. This is for
5
+ # testing client timeouts.
6
+ # Defaults to 0 (no delay).
7
+ #
8
+ # Change to this attribute only take effect for new sessions.
9
+
10
+ attr_accessor :response_delay
11
+
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ module Ftpd
2
+
3
+ # This class limits the number of connections
4
+
5
+ class ConnectionThrottle
6
+
7
+ DEFAULT_MAX_CONNECTIONS = nil
8
+ DEFAULT_MAX_CONNECTIONS_PER_IP = nil
9
+
10
+ # The maximum number of connections, or nil if there is no limit.
11
+ # @return [Integer]
12
+
13
+ attr_accessor :max_connections
14
+
15
+ # The maximum number of connections for an IP, or nil if there is
16
+ # no limit.
17
+ # @return [Integer]
18
+
19
+ attr_accessor :max_connections_per_ip
20
+
21
+ # @param connection_tracker [ConnectionTracker]
22
+
23
+ def initialize(connection_tracker)
24
+ @max_connections = DEFAULT_MAX_CONNECTIONS
25
+ @max_connections_per_ip = DEFAULT_MAX_CONNECTIONS_PER_IP
26
+ @connection_tracker = connection_tracker
27
+ end
28
+
29
+ # @return [Boolean] true if the connection should be allowed
30
+
31
+ def allow?(socket)
32
+ allow_by_total_count &&
33
+ allow_by_ip_count(socket)
34
+ end
35
+
36
+ # Reject a connection
37
+
38
+ def deny(socket)
39
+ socket.write "421 Too many connections\r\n"
40
+ end
41
+
42
+ private
43
+
44
+ def allow_by_total_count
45
+ return true unless @max_connections
46
+ @connection_tracker.connections < @max_connections
47
+ end
48
+
49
+ def allow_by_ip_count(socket)
50
+ return true unless @max_connections_per_ip
51
+ @connection_tracker.connections_for(socket) < @max_connections_per_ip
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,82 @@
1
+ require_relative "gets_peer_address"
2
+
3
+ module Ftpd
4
+
5
+ # This class keeps track of connections
6
+
7
+ class ConnectionTracker
8
+
9
+ include GetsPeerAddress
10
+
11
+ def initialize
12
+ @mutex = Mutex.new
13
+ @connections = {}
14
+ @socket_ips ={}
15
+ end
16
+
17
+ # Return the total number of connections
18
+
19
+ def connections
20
+ @mutex.synchronize do
21
+ @connections.values.inject(0, &:+)
22
+ end
23
+ end
24
+
25
+ # Return the number of connections for a socket's peer IP
26
+
27
+ def connections_for(socket)
28
+ @mutex.synchronize do
29
+ ip = peer_ip(socket)
30
+ @connections[ip] || 0
31
+ end
32
+ end
33
+
34
+ # Track a connection. Yields to a block; the connection is
35
+ # tracked until the block returns.
36
+
37
+ def track(socket)
38
+ start_track socket
39
+ begin
40
+ yield
41
+ ensure
42
+ stop_track socket
43
+ end
44
+ end
45
+
46
+ # Start tracking a connection
47
+
48
+ def start_track(socket)
49
+ @mutex.synchronize do
50
+ ip = peer_ip(socket)
51
+ @connections[ip] ||= 0
52
+ @connections[ip] += 1
53
+ @socket_ips[socket.object_id] = ip
54
+ end
55
+ end
56
+
57
+ # Stop tracking a connection
58
+
59
+ def stop_track(socket)
60
+ @mutex.synchronize do
61
+ ip = @socket_ips.delete(socket.object_id)
62
+ if (@connections[ip] -= 1) == 0
63
+ @connections.delete(ip)
64
+ end
65
+ end
66
+ end
67
+
68
+ # Return the number of known IPs. This exists for the benefit of
69
+ # the test, so that it can know the tracker has properly forgotten
70
+ # about an IP with no connections.
71
+
72
+ def known_ip_count
73
+ @mutex.synchronize do
74
+ @connections.size
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,123 @@
1
+ require_relative 'command_handler'
2
+
3
+ module Ftpd
4
+
5
+ module DataConnectionHelper
6
+
7
+ def transmit_file(io, data_type = session.data_type)
8
+ open_data_connection do |data_socket|
9
+ socket = Ftpd::Stream.new(data_socket, data_type)
10
+ handle_data_disconnect do
11
+ socket.write(io)
12
+ end
13
+ config.log.debug "Sent #{socket.byte_count} bytes"
14
+ reply "226 Transfer complete"
15
+ end
16
+ end
17
+
18
+ def receive_file(path_to_advertise = nil, &block)
19
+ open_data_connection(path_to_advertise) do |data_socket|
20
+ socket = Ftpd::Stream.new(data_socket, data_type)
21
+ handle_data_disconnect do
22
+ yield socket
23
+ end
24
+ config.log.debug "Received #{socket.byte_count} bytes"
25
+ end
26
+ end
27
+
28
+ def open_data_connection(path_to_advertise = nil, &block)
29
+ send_start_of_data_connection_reply(path_to_advertise)
30
+ if data_server
31
+ if encrypt_data?
32
+ open_passive_tls_data_connection(&block)
33
+ else
34
+ open_passive_data_connection(&block)
35
+ end
36
+ else
37
+ if encrypt_data?
38
+ open_active_tls_data_connection(&block)
39
+ else
40
+ open_active_data_connection(&block)
41
+ end
42
+ end
43
+ end
44
+
45
+ def open_passive_tls_data_connection
46
+ open_passive_data_connection do |socket|
47
+ make_tls_connection(socket) do |ssl_socket|
48
+ yield(ssl_socket)
49
+ end
50
+ end
51
+ end
52
+
53
+ def open_active_tls_data_connection
54
+ open_active_data_connection do |socket|
55
+ make_tls_connection(socket) do |ssl_socket|
56
+ yield(ssl_socket)
57
+ end
58
+ end
59
+ end
60
+
61
+ def open_active_data_connection
62
+ data_socket = TCPSocket.new(data_hostname, data_port)
63
+ begin
64
+ yield(data_socket)
65
+ ensure
66
+ data_socket.close
67
+ end
68
+ end
69
+
70
+ def open_passive_data_connection
71
+ data_socket = data_server.accept
72
+ begin
73
+ yield(data_socket)
74
+ ensure
75
+ data_socket.close
76
+ end
77
+ end
78
+
79
+ def handle_data_disconnect
80
+ return yield
81
+ rescue Errno::ECONNRESET, Errno::EPIPE
82
+ reply "426 Connection closed; transfer aborted."
83
+ end
84
+
85
+ def send_start_of_data_connection_reply(path)
86
+ if path
87
+ reply "150 FILE: #{path}"
88
+ else
89
+ reply "150 Opening #{data_connection_description}"
90
+ end
91
+ end
92
+
93
+ def data_connection_description
94
+ [
95
+ Session::DATA_TYPES[data_type][0],
96
+ "mode data connection",
97
+ ("(TLS)" if encrypt_data?)
98
+ ].compact.join(' ')
99
+ end
100
+
101
+ def make_tls_connection(data_socket)
102
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(data_socket, socket.ssl_context)
103
+ ssl_socket.accept
104
+ begin
105
+ yield(ssl_socket)
106
+ ensure
107
+ ssl_socket.close
108
+ end
109
+ end
110
+
111
+ def close_data_server_socket_when_done
112
+ yield
113
+ ensure
114
+ close_data_server_socket
115
+ end
116
+
117
+ def encrypt_data?
118
+ data_channel_protection_level != :clear
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,434 @@
1
+ require_relative 'translate_exceptions'
2
+
3
+ module Ftpd
4
+
5
+ class DiskFileSystem
6
+
7
+ # DiskFileSystem mixin for path expansion. Used by every command
8
+ # that accesses the disk file system.
9
+
10
+ module PathExpansion
11
+
12
+ # Set the data directory, the root of the disk file system.
13
+ # data_dir should be an absolute path.
14
+
15
+ def set_data_dir(data_dir)
16
+ @data_dir = data_dir
17
+ end
18
+
19
+ # Expand an ftp_path to an absolute file system path.
20
+ #
21
+ # ftp_path is an absolute path relative to the FTP file system.
22
+ # The return value is an absolute path relative to the disk file
23
+ # system.
24
+
25
+ def expand_ftp_path(ftp_path)
26
+ File.expand_path(File.join(@data_dir, ftp_path))
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ class DiskFileSystem
33
+
34
+ # DiskFileSystem mixin providing file attributes. These are used,
35
+ # alone or in combination, by nearly every command that accesses the
36
+ # disk file system.
37
+
38
+ module Accessors
39
+
40
+ # Return true if the path is accessible to the user. This will be
41
+ # called for put, get and directory lists, so the file or
42
+ # directory named by the path may not exist.
43
+ # @param ftp_path [String] The virtual path
44
+ # @return [Boolean]
45
+
46
+ def accessible?(ftp_path)
47
+ # The server should never try to access a path outside of the
48
+ # directory (such as '../foo'), but if it does, we'll catch it
49
+ # here.
50
+ expand_ftp_path(ftp_path).start_with?(@data_dir)
51
+ end
52
+
53
+ # Return true if the file or directory path exists.
54
+ # @param ftp_path [String] The virtual path
55
+ # @return [Boolean]
56
+
57
+ def exists?(ftp_path)
58
+ File.exists?(expand_ftp_path(ftp_path))
59
+ end
60
+
61
+ # Return true if the path exists and is a directory.
62
+ # @param ftp_path [String] The virtual path
63
+ # @return [Boolean]
64
+
65
+ def directory?(ftp_path)
66
+ File.directory?(expand_ftp_path(ftp_path))
67
+ end
68
+
69
+ end
70
+ end
71
+
72
+ class DiskFileSystem
73
+
74
+ # DiskFileSystem mixin for writing files. Used by Append and Write.
75
+
76
+ module FileWriting
77
+
78
+ def write_file(ftp_path, stream, mode)
79
+ File.open(expand_ftp_path(ftp_path), mode) do |file|
80
+ while line = stream.read
81
+ file.write line
82
+ end
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
90
+ class DiskFileSystem
91
+
92
+ # DiskFileSystem mixin providing file deletion
93
+
94
+ module Delete
95
+
96
+ include TranslateExceptions
97
+
98
+ # Remove a file.
99
+ # @param ftp_path [String] The virtual path
100
+ #
101
+ # Called for:
102
+ # * DELE
103
+ #
104
+ # If missing, then these commands are not supported.
105
+
106
+ def delete(ftp_path)
107
+ FileUtils.rm expand_ftp_path(ftp_path)
108
+ end
109
+ translate_exceptions :delete
110
+
111
+ end
112
+ end
113
+
114
+ class DiskFileSystem
115
+
116
+ # DiskFileSystem mixin providing file reading
117
+
118
+ module Read
119
+
120
+ include TranslateExceptions
121
+
122
+ # Read a file from disk.
123
+ # @param ftp_path [String] The virtual path
124
+ # @yield [io] Passes an IO object to the block
125
+ #
126
+ # Called for:
127
+ # * RETR
128
+ #
129
+ # If missing, then these commands are not supported.
130
+
131
+ def read(ftp_path, &block)
132
+ io = File.open(expand_ftp_path(ftp_path), 'rb')
133
+ begin
134
+ yield(io)
135
+ ensure
136
+ io.close
137
+ end
138
+ end
139
+ translate_exceptions :read
140
+
141
+ end
142
+ end
143
+
144
+ class DiskFileSystem
145
+
146
+ # DiskFileSystem mixin providing file writing
147
+
148
+ module Write
149
+
150
+ include FileWriting
151
+ include TranslateExceptions
152
+
153
+ # Write a file to disk.
154
+ # @param ftp_path [String] The virtual path
155
+ # @param stream [Ftpd::Stream] Stream that contains the data to write
156
+ #
157
+ # Called for:
158
+ # * STOR
159
+ # * STOU
160
+ #
161
+ # If missing, then these commands are not supported.
162
+
163
+ def write(ftp_path, stream)
164
+ write_file ftp_path, stream, 'wb'
165
+ end
166
+ translate_exceptions :write
167
+
168
+ end
169
+ end
170
+
171
+ class DiskFileSystem
172
+
173
+ # DiskFileSystem mixin providing file appending
174
+
175
+ module Append
176
+
177
+ include FileWriting
178
+ include TranslateExceptions
179
+
180
+ # Append to a file. If the file does not exist, create it.
181
+ # @param ftp_path [String] The virtual path
182
+ # @param stream [Ftpd::Stream] Stream that contains the data to write
183
+ #
184
+ # Called for:
185
+ # * APPE
186
+ #
187
+ # If missing, then these commands are not supported.
188
+
189
+ def append(ftp_path, stream)
190
+ write_file ftp_path, stream, 'ab'
191
+ end
192
+ translate_exceptions :append
193
+
194
+ end
195
+ end
196
+
197
+ class DiskFileSystem
198
+
199
+ # DiskFileSystem mixing providing mkdir
200
+
201
+ module Mkdir
202
+
203
+ include TranslateExceptions
204
+
205
+ # Create a directory.
206
+ # @param ftp_path [String] The virtual path
207
+ #
208
+ # Called for:
209
+ # * MKD
210
+ #
211
+ # If missing, then these commands are not supported.
212
+
213
+ def mkdir(ftp_path)
214
+ Dir.mkdir expand_ftp_path(ftp_path)
215
+ end
216
+ translate_exceptions :mkdir
217
+
218
+ end
219
+
220
+ end
221
+
222
+ class DiskFileSystem
223
+
224
+ # DiskFileSystem mixing providing mkdir
225
+
226
+ module Rmdir
227
+
228
+ include TranslateExceptions
229
+
230
+ # Remove a directory.
231
+ # @param ftp_path [String] The virtual path
232
+ #
233
+ # Called for:
234
+ # * RMD
235
+ #
236
+ # If missing, then these commands are not supported.
237
+
238
+ def rmdir(ftp_path)
239
+ Dir.rmdir expand_ftp_path(ftp_path)
240
+ end
241
+ translate_exceptions :rmdir
242
+
243
+ end
244
+
245
+ end
246
+
247
+ class DiskFileSystem
248
+
249
+ # DiskFileSystem mixin providing directory listing
250
+
251
+ module List
252
+
253
+ include TranslateExceptions
254
+
255
+ # Get information about a single file or directory.
256
+ # @param ftp_path [String] The virtual path
257
+ # @return [FileInfo]
258
+ #
259
+ # Should follow symlinks (per
260
+ # {http://cr.yp.to/ftp/list/eplf.html}, "lstat() is not a good
261
+ # idea for FTP directory listings").
262
+ #
263
+ # Called for:
264
+ # * LIST
265
+ #
266
+ # If missing, then these commands are not supported.
267
+
268
+ def file_info(ftp_path)
269
+ stat = File.stat(expand_ftp_path(ftp_path))
270
+ FileInfo.new(:ftype => stat.ftype,
271
+ :group => gid_name(stat.gid),
272
+ :identifier => identifier(stat),
273
+ :mode => stat.mode,
274
+ :mtime => stat.mtime,
275
+ :nlink => stat.nlink,
276
+ :owner => uid_name(stat.uid),
277
+ :path => ftp_path,
278
+ :size => stat.size)
279
+ end
280
+ translate_exceptions :file_info
281
+
282
+ # Expand a path that may contain globs into a list of paths of
283
+ # matching files and directories.
284
+ # @param ftp_path [String] The virtual path
285
+ # @return [Array<String>]
286
+ #
287
+ # The paths returned are fully qualified, relative to the root
288
+ # of the virtual file system.
289
+ #
290
+ # For example, suppose these files exist on the physical file
291
+ # system:
292
+ #
293
+ # /var/lib/ftp/foo/foo
294
+ # /var/lib/ftp/foo/subdir/bar
295
+ # /var/lib/ftp/foo/subdir/baz
296
+ #
297
+ # and that the directory /var/lib/ftp is the root of the virtual
298
+ # file system. Then:
299
+ #
300
+ # dir('foo') # => ['/foo']
301
+ # dir('subdir') # => ['/subdir']
302
+ # dir('subdir/*') # => ['/subdir/bar', '/subdir/baz']
303
+ # dir('*') # => ['/foo', '/subdir']
304
+ #
305
+ # Called for:
306
+ # * LIST
307
+ # * NLST
308
+ #
309
+ # If missing, then these commands are not supported.
310
+
311
+ def dir(ftp_path)
312
+ Dir[expand_ftp_path(ftp_path)].map do |path|
313
+ path.sub(/^#{@data_dir}/, '')
314
+ end
315
+ end
316
+ translate_exceptions :dir
317
+
318
+ private
319
+
320
+ def uid_name(uid)
321
+ Etc.getpwuid(uid).name
322
+ end
323
+
324
+ def gid_name(gid)
325
+ Etc.getgrgid(gid).name
326
+ end
327
+
328
+ def identifier(stat)
329
+ [stat.dev, stat.ino].join('.')
330
+ end
331
+
332
+ end
333
+ end
334
+
335
+ class DiskFileSystem
336
+
337
+ # DiskFileSystem mixin providing file/directory rename/move
338
+
339
+ module Rename
340
+
341
+ include TranslateExceptions
342
+
343
+ # Rename or move a file or directory
344
+ #
345
+ # Called for:
346
+ # * RNTO
347
+ #
348
+ # If missing, then these commands are not supported.
349
+
350
+ def rename(from_ftp_path, to_ftp_path)
351
+ FileUtils.mv(expand_ftp_path(from_ftp_path),
352
+ expand_ftp_path(to_ftp_path))
353
+ end
354
+ translate_exceptions :rename
355
+
356
+ end
357
+ end
358
+
359
+ class DiskFileSystem
360
+
361
+ # DiskFileSystem "omnibus" mixin, which pulls in mixins which are
362
+ # likely to be needed by any DiskFileSystem.
363
+
364
+ module Base
365
+ include TranslateExceptions
366
+ include DiskFileSystem::Accessors
367
+ include DiskFileSystem::PathExpansion
368
+ end
369
+
370
+ end
371
+
372
+ # An FTP file system mapped to a disk directory. This can serve as
373
+ # a template for creating your own specialized driver.
374
+ #
375
+ # Any method may raise a PermanentFileSystemError (e.g. "file not
376
+ # found") or TransientFileSystemError (e.g. "file busy"). A
377
+ # PermanentFileSystemError will cause a "550" error response to be
378
+ # sent; a TransientFileSystemError will cause a "450" error response
379
+ # to be sent. Methods may also raise an FtpServerError with any
380
+ # desired error code.
381
+ #
382
+ # The class is divided into modules that may be included piecemeal.
383
+ # By including some mixins and not others, you can compose a disk
384
+ # file system driver "a la carte." This is useful if you want an
385
+ # FTP server that, for example, allows reading but not writing
386
+ # files.
387
+
388
+ class DiskFileSystem
389
+
390
+ include DiskFileSystem::Base
391
+
392
+ # Mixins that make available commands or groups of commands. Each
393
+ # can be safely left out with the only effect being to make One or
394
+ # more commands be unimplemented.
395
+
396
+ include DiskFileSystem::Append
397
+ include DiskFileSystem::Delete
398
+ include DiskFileSystem::List
399
+ include DiskFileSystem::Mkdir
400
+ include DiskFileSystem::Read
401
+ include DiskFileSystem::Rename
402
+ include DiskFileSystem::Rmdir
403
+ include DiskFileSystem::Write
404
+
405
+ # Make a new instance to serve a directory. data_dir should be an
406
+ # absolute path.
407
+
408
+ def initialize(data_dir)
409
+ set_data_dir data_dir
410
+ translate_exception SystemCallError
411
+ end
412
+
413
+ end
414
+
415
+ # A disk file system that does not allow any modification (writes,
416
+ # deletes, etc.)
417
+
418
+ class ReadOnlyDiskFileSystem
419
+
420
+ include DiskFileSystem::Base
421
+ include DiskFileSystem::List
422
+ include DiskFileSystem::Read
423
+
424
+ # Make a new instance to serve a directory. data_dir should be an
425
+ # absolute path.
426
+
427
+ def initialize(data_dir)
428
+ set_data_dir data_dir
429
+ translate_exception SystemCallError
430
+ end
431
+
432
+ end
433
+
434
+ end