forked from wconrad/ftpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpdrb
executable file
·289 lines (252 loc) · 8.18 KB
/
ftpdrb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env ruby
#
# Ftpd is a pure Ruby FTP server library. CLI version
#
unless $:.include?(File.dirname(__FILE__) + '/../lib')
$:.unshift(File.dirname(__FILE__) + '/../lib')
end
require 'ftpd'
require 'ipaddr'
require 'optparse'
module FTPDrb
# Command-line option parser
class Arguments
attr_reader :account
attr_reader :auth_level
attr_reader :debug
attr_reader :eplf
attr_reader :interface
attr_reader :nat_ip
attr_reader :passive_ports
attr_reader :password
attr_reader :port
attr_reader :path
attr_reader :read_only
attr_reader :session_timeout
attr_reader :servername
attr_reader :tls
attr_reader :user
def initialize(argv)
@interface = '127.0.0.1'
@tls = :explicit
@port = 0
@auth_level = 'password'
@user = ENV['LOGNAME']
@password = ''
@account = ''
@path = nil
@session_timeout = default_session_timeout
@servername = 'FTPd - Pure Ruby FTP server'
@log = nil
@nat_ip = nil
@passive_ports = nil
opts = option_parser
opts.parse!(argv)
rescue OptionParser::ParseError => e
$stderr.puts e
exit(1)
end
private
def option_parser
opts = OptionParser.new do |op|
op.banner = "ftpdrb - Pure Ruby FTP Server. (ftpd ruby gem)\n\nUsage: #{__FILE__} [OPTIONS]"
op.separator "Help menu:"
op.on('-p', '--port N', Integer, 'Bind to a specific port') do |t|
@port = t
end
op.on('-i', '--interface IP', 'Bind to a specific interface. (Use "0.0.0.0" for all interfaces)') do |t|
@interface = t
end
op.on('-s', '--servername SERVERNAME', 'Set servername for FTP server)',
"\tdefault = FTPd - Pure Ruby FTP server") do |t|
@servername = t
end
op.on('--tls [TYPE]', [:off, :explicit, :implicit],
'Select TLS support (off, explicit, implicit)',
"\tdefault = off") do |t|
@tls = t
end
op.on('--eplf', 'LIST uses EPLF format') do |t|
@eplf = t
end
op.on('--read-only', 'Prohibit put, delete, rmdir, etc.') do |t|
@read_only = t
end
op.on('--auth [LEVEL]', [:user, :password, :account],
'Set authorization level (user, password, account)',
"\tdefault = password") do |t|
@auth_level = t
end
op.on('-U', '--user NAME', 'User for authentication',
"\tdefaults to current user") do |t|
@user = t
end
op.on('-P', '--password PASS', 'Password for authentication',
"\tdefaults to empty string") do |t|
@password = t
end
op.on('-A', '--account P', 'Account for authentication',
"\tdefaults to empty string") do |t|
@account = t
end
op.on('--path PATH', 'Directory path for share',
"\tdefaults to random temp directory") do |t|
@path = t
end
op.on('--timeout SEC', Integer, 'Session idle timeout',
"\tdefaults to #{default_session_timeout}") do |t|
@session_timeout = t
end
op.on('-d', '--debug', 'Write server debug log to stdout') do |t|
@debug = t
end
op.on('--nat-ip IP', 'Set advertised passive mode IP') do |t|
@nat_ip = t
end
op.on('--ports MIN..MAX', 'Port numbers for passive mode sockets') do |v|
@passive_ports = Range.new(*v.split(/\.\./).map(&:to_i))
end
op.on("-h","--help","Show the full help screen.") do
puts opts
puts "\nExample:"
puts " #{__FILE__} -i 0.0.0.0 -p 21 -U ftp -P ftp --path /tmp/\n\n"
exit 0
end
end
end
def default_session_timeout
Ftpd::FtpServer::DEFAULT_SESSION_TIMEOUT
end
end
end
module FTPDrb
# The FTP server requires and instance of a _driver_ which can
# authenticate users and create a file system drivers for a given
# user.
class Driver
# Your driver's initialize method can be anything you need. Ftpd
# does not create an instance of your driver.
def initialize(user, password, account, data_dir, read_only)
@user = user
@password = password
@account = account
@data_dir = data_dir
@read_only = read_only
end
# Return true if the user should be allowed to log in.
# @param user [String]
# @param password [String]
# @param account [String]
# @return [Boolean]
#
# Depending upon the server's auth_level, some of these parameters
# may be nil. A parameter with a nil value is not required for
# authentication. Here are the parameters that are non-nil for
# each auth_level:
# * :user (user)
# * :password (user, password)
# * :account (user, password, account)
def authenticate(user, password, account)
user == @user &&
(password.nil? || password == @password) &&
(account.nil? || account == @account)
end
# Return the file system to use for a user.
# @param user [String]
# @return A file system driver that quacks like {Ftpd::DiskFileSystem}
def file_system(user)
if @read_only
Ftpd::ReadOnlyDiskFileSystem
else
Ftpd::DiskFileSystem.new(@data_dir)
end
end
end
end
module FTPDrb
class Server
include Ftpd::InsecureCertificate
def initialize(argv)
@args = Arguments.new(argv)
# Create a temp directory if path argument is not assigned
@args.path.nil? ? @data_dir = Ftpd::TempDir.make : @data_dir = @args.path
create_files if @args.path.nil?
@driver = Driver.new(user, password, account,
@data_dir, @args.read_only)
@server = Ftpd::FtpServer.new(@driver)
configure_server
@server.start
end
def run
puts "FTP server started. (Press CRL+C to stop it)"
connection_info.each do |key, value|
puts "#{key}: #{value}"
end
$stdout.flush
begin
loop{}
rescue Interrupt
puts "\nClosing FTP server..."
end
end
private
def configure_server
@server.server_name = @args.servername
@server.interface = @args.interface
@server.port = @args.port
@server.tls = @args.tls
@server.passive_ports = @args.passive_ports
@server.certfile_path = insecure_certfile_path
if @args.eplf
@server.list_formatter = Ftpd::ListFormat::Eplf
end
@server.auth_level = auth_level
@server.session_timeout = @args.session_timeout
@server.log = make_log
@server.nat_ip = @args.nat_ip
end
def auth_level
Ftpd.const_get("AUTH_#{@args.auth_level.upcase}")
end
def create_files
create_file 'README',
"Automatically created by FTPdrb.\n" +
"This file, and the directory it is in, will be deleted\n" +
"When ftpd server exits. Make sure your data is backed-up.\n"
end
def create_file(path, contents)
full_path = File.expand_path(path, @data_dir)
FileUtils.mkdir_p File.dirname(full_path)
File.open(full_path, 'w') do |file|
file.write contents
end
end
def connection_info
{
"Servername"=> @server.server_name,
"Interface" => @server.interface,
"Port" => @server.bound_port,
"User" => user,
"Pass" => "#{password if auth_level >= Ftpd::AUTH_PASSWORD}",
"Account" => "#{account if auth_level >= Ftpd::AUTH_ACCOUNT}",
"TLS" => @args.tls,
"Directory" => @data_dir,
"URI" => "ftp://#{user}:#{password}@#{IPAddr.new(@server.interface)}:#{@server.bound_port}",
"PID" => $$
}
end
def user
@args.user
end
def password
@args.password
end
def account
@args.account
end
def make_log
@args.debug && Logger.new($stdout)
end
end
end
FTPDrb::Server.new(ARGV).run if $0 == __FILE__