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,41 @@
1
+ require_relative "gets_peer_address"
2
+
3
+ module Ftpd
4
+
5
+ module GetsPeerAddress
6
+
7
+ # Obtain the IP that the client connected _from_.
8
+ #
9
+ # How this is done depends upon which type of socket (SSL or not)
10
+ # and what version of Ruby.
11
+ #
12
+ # * SSL socket
13
+ # * #peeraddr. Uses BasicSocket.do_not_reverse_lookup.
14
+ # * Ruby 1.8.7
15
+ # * #peeraddr, which does not take the "reverse lookup"
16
+ # argument, relying instead using
17
+ # BasicSocket.do_not_reverse_lookup.
18
+ # * #getpeername, which does not do a reverse lookup. It is a
19
+ # little uglier than #peeraddr.
20
+ # * Ruby >=1.9.3
21
+ # * #peeraddr, which takes the "reverse lookup" argument.
22
+ # * #getpeername - same as 1.8.7
23
+ #
24
+ # @return [String] IP address
25
+
26
+ def peer_ip(socket)
27
+ if socket.respond_to?(:getpeername)
28
+ # Non SSL
29
+ sockaddr = socket.getpeername
30
+ port, host = Socket.unpack_sockaddr_in(sockaddr)
31
+ host
32
+ else
33
+ # SSL
34
+ BasicSocket.do_not_reverse_lookup = true
35
+ socket.peeraddr.last
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,16 @@
1
+ module Ftpd
2
+
3
+ # This mixin provides an insecure SSL certificate. This certificate
4
+ # should only be used for testing.
5
+
6
+ module InsecureCertificate
7
+
8
+ # The path of an insecure SSL certificate.
9
+
10
+ def insecure_certfile_path
11
+ File.expand_path('../../insecure-test-cert.pem',
12
+ File.dirname(__FILE__))
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,74 @@
1
+ module Ftpd
2
+ module ListFormat
3
+
4
+ # Easily Parsed LIST Format (EPLF) Directory formatter
5
+ # See: {http://cr.yp.to/ftp/list/eplf.html}
6
+
7
+ class Eplf
8
+
9
+ extend Forwardable
10
+
11
+ # Create a new formatter for a file object
12
+ # @param file_info [FileInfo]
13
+
14
+ def initialize(file_info)
15
+ @file_info = file_info
16
+ end
17
+
18
+ # Return the formatted directory entry.
19
+ # For example:
20
+ # +i8388621.48598,m824253270,r,s612, 514.html
21
+ # Note: The calling code adds the \r\n
22
+
23
+ def to_s
24
+ "+%s\t%s" % [facts, filename]
25
+ end
26
+
27
+ private
28
+
29
+ def facts
30
+ [
31
+ retrievable_fact,
32
+ cwd_target_fact,
33
+ size_fact,
34
+ mtime_fact,
35
+ identifier_fact,
36
+ ].compact.join(',')
37
+ end
38
+
39
+ def retrievable_fact
40
+ 'r' if retrievable?
41
+ end
42
+
43
+ def cwd_target_fact
44
+ '/' if cwd_target?
45
+ end
46
+
47
+ def size_fact
48
+ "s#{@file_info.size}" if retrievable?
49
+ end
50
+
51
+ def mtime_fact
52
+ "m#{@file_info.mtime.to_i}"
53
+ end
54
+
55
+ def identifier_fact
56
+ "i#{@file_info.identifier}" if @file_info.identifier
57
+ end
58
+
59
+ def filename
60
+ File.basename(@file_info.path)
61
+ end
62
+
63
+ def retrievable?
64
+ @file_info.file?
65
+ end
66
+
67
+ def cwd_target?
68
+ @file_info.directory?
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,154 @@
1
+ module Ftpd
2
+ module ListFormat
3
+
4
+ # Directory formatter that approximates the output of "ls -l"
5
+
6
+ class Ls
7
+
8
+ extend Forwardable
9
+
10
+ # Create a new formatter for a file object
11
+ # @param file_info [FileInfo]
12
+
13
+ def initialize(file_info)
14
+ @file_info = file_info
15
+ end
16
+
17
+ # Return the formatted directory entry, for example:
18
+ # -rw-r--r-- 1 user group Mar 3 08:38 foo
19
+
20
+ def to_s
21
+ '%s%s %d %-8s %-8s %8d %s %s' % [
22
+ file_type,
23
+ file_mode_letters,
24
+ @file_info.nlink,
25
+ @file_info.owner,
26
+ @file_info.group,
27
+ @file_info.size,
28
+ format_time(@file_info.mtime),
29
+ filename,
30
+ ]
31
+ end
32
+
33
+ private
34
+
35
+ SIX_MONTHS = 180 * 24 * 60 * 60
36
+
37
+ def filename
38
+ File.basename(@file_info.path)
39
+ end
40
+
41
+ def file_type
42
+ FileType.letter(@file_info.ftype)
43
+ end
44
+
45
+ def file_mode_letters
46
+ FileMode.new(@file_info.mode).letters
47
+ end
48
+
49
+ def self.format_time(mtime)
50
+ age = Time.now - mtime
51
+ format = '%b %e ' + if age < 0 || age > SIX_MONTHS
52
+ ' %Y'
53
+ else
54
+ '%H:%M'
55
+ end
56
+ mtime.strftime(format)
57
+ end
58
+ def_delegator self, :format_time
59
+
60
+ # Map file type strings to ls file type letters
61
+
62
+ class FileType
63
+
64
+ # Map a file type string to a file type letter.
65
+ # @param ftype [String] file type as returned by File::Stat#ftype
66
+ # @return [String] File type letter
67
+
68
+ def self.letter(ftype)
69
+ case ftype
70
+ when 'file'
71
+ '-'
72
+ when 'directory'
73
+ 'd'
74
+ when 'characterSpecial'
75
+ 'c'
76
+ when 'blockSpecial'
77
+ 'b'
78
+ when 'fifo'
79
+ 'p'
80
+ when 'link'
81
+ 'l'
82
+ when 'socket'
83
+ 's'
84
+ else # 'unknown', etc.
85
+ '?'
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ # Map file mode bits into ls style file mode letters
92
+
93
+ class FileMode
94
+
95
+ # @param mode [Integer] File mode bits, as returned by
96
+ # File::Stat#mode
97
+
98
+ def initialize(mode)
99
+ @mode = mode
100
+ end
101
+
102
+ # Return the mode bits as ls style letters.
103
+ # For example, "-rw-r--r--"
104
+
105
+ def letters
106
+ [
107
+ triad(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, SET_UID, 'Ss'),
108
+ triad(GROUP_READ, GROUP_WRITE, GROUP_EXECUTE, SET_GID, 'Ss'),
109
+ triad(OTHER_READ, OTHER_WRITE, OTHER_EXECUTE, STICKY, 'Tt'),
110
+ ].join
111
+ end
112
+
113
+ private
114
+
115
+ def bit(bit_number)
116
+ @mode >> bit_number & 1
117
+ end
118
+
119
+ def triad(read_bit, write_bit, execute_bit, special_bit, special_letters)
120
+ execute_chars = if bit(special_bit) != 0
121
+ special_letters
122
+ else
123
+ '-x'
124
+ end
125
+ [
126
+ pick_char('-r', read_bit),
127
+ pick_char('-w', write_bit),
128
+ pick_char(execute_chars, execute_bit),
129
+ ]
130
+ end
131
+
132
+ def pick_char(s, bit_number)
133
+ s[bit(bit_number), 1]
134
+ end
135
+
136
+ OTHER_EXECUTE = 0
137
+ OTHER_WRITE = 1
138
+ OTHER_READ = 2
139
+ GROUP_EXECUTE = 3
140
+ GROUP_WRITE = 4
141
+ GROUP_READ = 5
142
+ OWNER_EXECUTE = 6
143
+ OWNER_WRITE = 7
144
+ OWNER_READ = 8
145
+ STICKY = 9
146
+ SET_GID = 10
147
+ SET_UID = 11
148
+
149
+ end
150
+
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,28 @@
1
+ module Ftpd
2
+
3
+ # Functions for manipulating LIST and NLST arguments
4
+
5
+ module ListPath
6
+
7
+ # Turn the argument to LIST/NLST into a path
8
+ #
9
+ # @param argument [String] The argument, or nil if not present
10
+ # @return [String] The path
11
+ #
12
+ # Although compliant with the spec, this function does not do
13
+ # these things that traditional Unix FTP servers do:
14
+ #
15
+ # * Allow multiple paths
16
+ # * Handle switches such as "-a"
17
+ #
18
+ # See: http://cr.yp.to/ftp/list.html sections "LIST parameters"
19
+ # and "LIST wildcards"
20
+
21
+ def list_path(argument)
22
+ argument ||= '.'
23
+ argument = '' if argument =~ /^-/
24
+ argument
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module Ftpd
2
+
3
+ # A logger that does not log.
4
+ # Quacks enough like a Logger to fool Ftpd.
5
+
6
+ class NullLogger
7
+
8
+ def self.stub(method_name)
9
+ define_method method_name do |*args|
10
+ end
11
+ end
12
+
13
+ stub :unknown
14
+ stub :fatal
15
+ stub :error
16
+ stub :warn
17
+ stub :info
18
+ stub :debug
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,60 @@
1
+ module Ftpd
2
+
3
+ # With the commands EPORT and EPSV, the client sends a protocol code
4
+ # to indicate whether it wants an IPV4 or an IPV6 connection. This
5
+ # class contains functions related to that protocol code.
6
+
7
+ class Protocols
8
+
9
+ module Codes
10
+ IPV4 = 1
11
+ IPV6 = 2
12
+ end
13
+ include Codes
14
+
15
+ # @param socket [TCPSocket, OpenSSL::SSL::SSLSocket] The socket.
16
+ # It doesn't matter whether it's the server socket (the one on
17
+ # which #accept is called), or the socket returned by #accept.
18
+
19
+ def initialize(socket)
20
+ @socket = socket
21
+ end
22
+
23
+ # Can the socket support a connection in the indicated protocol?
24
+ #
25
+ # @param protocol_code [Integer] protocol code
26
+
27
+ def supports_protocol?(protocol_code)
28
+ protocol_codes.include?(protocol_code)
29
+ end
30
+
31
+ # What protocol codes does the socket support?
32
+ #
33
+ # @return [Array<Integer>] List of protocol codes
34
+
35
+ def protocol_codes
36
+ [
37
+ (IPV4 if supports_ipv4?),
38
+ (IPV6 if supports_ipv6?),
39
+ ].compact
40
+ end
41
+
42
+ private
43
+
44
+ def supports_ipv4?
45
+ @socket.local_address.ipv4? || ipv6_dual_stack?
46
+ end
47
+
48
+ def supports_ipv6?
49
+ @socket.local_address.ipv6?
50
+ end
51
+
52
+ def ipv6_dual_stack?
53
+ v6only = @socket.getsockopt(Socket::IPPROTO_IPV6,
54
+ Socket::IPV6_V6ONLY).unpack('i')
55
+ v6only == [0]
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,22 @@
1
+ module Ftpd
2
+
3
+ # A disk file system that does not allow any modification (writes,
4
+ # deletes, etc.)
5
+
6
+ class ReadOnlyDiskFileSystem
7
+
8
+ include DiskFileSystem::Base
9
+ include DiskFileSystem::List
10
+ include DiskFileSystem::Read
11
+
12
+ # Make a new instance to serve a directory. data_dir should be an
13
+ # absolute path.
14
+
15
+ def initialize(data_dir)
16
+ set_data_dir data_dir
17
+ translate_exception SystemCallError
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,139 @@
1
+ module Ftpd
2
+ class Server
3
+
4
+ include Memoizer
5
+
6
+ # The interface to bind to (e.g. "127.0.0.1", "0.0.0.0",
7
+ # "10.0.0.12", "::1", "::", etc.). Defaults to "127.0.0.1"
8
+ #
9
+ # Set this before calling #start.
10
+ #
11
+ # @return [String]
12
+
13
+ attr_accessor :interface
14
+
15
+ # The port to bind to. Defaults to 0, which causes an ephemeral
16
+ # port to be used. When bound to an ephemeral port, use
17
+ # #bound_port to find out which port was actually bound to.
18
+ #
19
+ # Set this before calling #start.
20
+ #
21
+ # @return [String]
22
+
23
+ attr_accessor :port
24
+
25
+ def initialize
26
+ @interface = '127.0.0.1'
27
+ @port = 0
28
+ @stopping = false
29
+ end
30
+
31
+ # The port the server is bound to. Must not be called until after
32
+ # #start is called.
33
+ #
34
+ # @return [Integer]
35
+
36
+ def bound_port
37
+ @server_socket.addr[1]
38
+ end
39
+
40
+ # The calling thread will suspend execution until the server is
41
+ # stopped.
42
+
43
+ def join
44
+ raise 'Server is not started!' if @server_thread.nil?
45
+ @server_thread.join
46
+ end
47
+
48
+ # Start the server. This creates the server socket, and the
49
+ # thread to service it.
50
+
51
+ def start
52
+ @server_socket = make_server_socket
53
+ @server_thread = make_server_thread
54
+ end
55
+
56
+ # Stop the server. This closes the server socket, which in turn
57
+ # stops the thread.
58
+
59
+ def stop
60
+ @stopping = true
61
+ begin
62
+ @server_socket.shutdown
63
+ rescue Errno::ENOTCONN
64
+ end
65
+ @server_socket.close
66
+ end
67
+
68
+ private
69
+
70
+ def make_server_socket
71
+ return TCPServer.new(@interface, @port)
72
+ end
73
+
74
+ def make_server_thread
75
+ Thread.new do
76
+ Thread.abort_on_exception = true
77
+ loop do
78
+ begin
79
+ begin
80
+ socket = accept
81
+ rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINVAL
82
+ IO.select([@server_socket])
83
+ sleep(0.2)
84
+ retry
85
+ rescue Errno::EBADF, Errno::ENOTSOCK
86
+ raise unless @stopping
87
+ @stopping = false
88
+ break
89
+ end
90
+ start_session socket
91
+ rescue IOError
92
+ break
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ def start_session(socket)
99
+ if allow_session?(socket)
100
+ start_session_thread socket
101
+ else
102
+ deny_session socket
103
+ close_socket socket
104
+ end
105
+ end
106
+
107
+ def allow_session?(socket)
108
+ true
109
+ end
110
+
111
+ def deny_session socket
112
+ end
113
+
114
+ def start_session_thread(socket)
115
+ Thread.new do
116
+ begin
117
+ session socket
118
+ rescue OpenSSL::SSL::SSLError => e
119
+ ensure
120
+ close_socket socket
121
+ end
122
+ end
123
+ end
124
+
125
+ def accept
126
+ @server_socket.accept
127
+ end
128
+
129
+ def close_socket(socket)
130
+ if socket.respond_to?(:shutdown)
131
+ socket.shutdown rescue nil
132
+ socket.read rescue nil
133
+ end
134
+ ensure
135
+ socket.close
136
+ end
137
+
138
+ end
139
+ end