em-ftpd-memory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 935bb1e47a5d580f87e0b150f866074fdf5ac242
4
+ data.tar.gz: 976fcbab17763aeddf50197cd3bd36279fa7b7e8
5
+ SHA512:
6
+ metadata.gz: 8d00f379e4d9b66db5f43aeabc4caa9588cd82ff6ecc93d67eeb06c84c506896b12f736a813652a1da5c741db55978c13aa5b21e158caa60b4b88f4a081ac9be
7
+ data.tar.gz: 5aac095867e9ed4ae16224ac3e852763d091d6471ed75dc0e73698d635b7c271c3ec2777055a5f21357033de79453e7004fdeee109f9e40aec0a3f2102679afd
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in em-ftpd-memory.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 chrislee35
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # EM::FTPD::Memory
2
+
3
+ This is an in-memory fake filesystem for em-ftpd.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'em-ftpd-memory'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install em-ftpd-memory
18
+
19
+ ## Usage
20
+
21
+ require 'eventmachine'
22
+ require 'em/ftpd/memory'
23
+ require 'em/ftpd'
24
+
25
+ options = {
26
+ "filesystem_name" => "boss",
27
+ "authentication_realm" => "georgia",
28
+ "pwalgo" => "otp",
29
+
30
+ }
31
+ auth = EM::FTPD::Memory::Authenticator.getAuthenticatorByRealm(options["authentication_realm"], options)
32
+ auth << EM::FTPD::Memory::User.new("test", "test1\ntest2\ntest3\ntest4\ntest5")
33
+ fs = EM::FTPD::Memory::FileSystem.getFileSystemByName(options["filesystem_name"])
34
+ fs.create_dir("/pub")
35
+ fs.create_file("/pub/helper.rb", "test/helper.rb")
36
+
37
+ EM.run {
38
+ EventMachine::start_server("0.0.0.0", 2021, EM::FTPD::Server, EM::FTPD::Memory::Driver, options)
39
+ }
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/em-ftpd-memory/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib'
8
+ t.test_files = FileList['test/test_*.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "em-ftpd-memory"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["chrislee35"]
9
+ spec.email = ["rubygems@chrislee.dhs.org"]
10
+ spec.summary = %q{Memory-based backing store for em-ftpd}
11
+ spec.description = %q{This implements a simple in-memory storage of files for use with em-ftpd}
12
+ spec.homepage = "https://github.com/chrislee35/em-ftpd-memory"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "em-ftpd", "~> 0.0.1"
21
+ spec.add_runtime_dependency "eventmachine", "~> 1.0.7"
22
+ spec.add_development_dependency "minitest", "~> 5.5"
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,3 @@
1
+ require 'em/ftpd/memory/authenticator'
2
+ require 'em/ftpd/memory/filesystem'
3
+ require 'em/ftpd/memory/driver'
@@ -0,0 +1,82 @@
1
+ require 'em-ftpd'
2
+ require 'digest/md5'
3
+
4
+ module EM::FTPD::Memory
5
+ class User < Struct.new(:name, :credential); end
6
+ class InvalidPasswordAlgorithm < StandardError; end
7
+ class NoSuchUserError < StandardError; end
8
+ class InvalidCredentialdError < StandardError; end
9
+ class ExpiredCredentialError < StandardError; end
10
+
11
+ class Authenticator
12
+ @@realms = Hash.new
13
+ def self.getAuthenticatorByRealm(realm, options = {})
14
+ if @@realms[realm].nil?
15
+ @@realms[realm] = Authenticator.new(options)
16
+ end
17
+ #puts "returning Authenticator of realm #{realm}"
18
+ @@realms[realm]
19
+ end
20
+
21
+ def initialize(options = {})
22
+ @users = Hash.new
23
+ @pwalgo = options["pwalgo"] || "plain"
24
+ end
25
+
26
+ def <<(user)
27
+ @users[user.name] = user
28
+ end
29
+
30
+ def delete(user)
31
+ @users.delete(user.name)
32
+ end
33
+
34
+ def authenticate(username, credential)
35
+ if @users[username].nil?
36
+ raise NoSuchUserError.new
37
+ end
38
+ # this prevents someone from setting "delete" as the authentication algorithm
39
+ algo = "#{@pwalgo}_authentication".to_sym
40
+ if self.respond_to?(algo)
41
+ self.send(algo, username, credential)
42
+ else
43
+ raise InvalidPasswordAlgorithmError.new
44
+ end
45
+ end
46
+
47
+ def plain_authentication(username, credential)
48
+ if @users[username].credential != credential
49
+ raise InvalidCredentialdError.new
50
+ else
51
+ return true
52
+ end
53
+ end
54
+
55
+ def timed_md5_authentication(username, credential)
56
+ seed, time, hash = credential.split(/:/, 3)
57
+ if (time.to_i - Time.now.to_i).abs > 300
58
+ raise ExpiredCredentialError.new
59
+ else
60
+ if Digest::MD5.hexdigest("#{seed}:#{time}:#{@users[username].credential}") == hash
61
+ return true
62
+ else
63
+ raise InvalidCredentialError.new
64
+ end
65
+ end
66
+ end
67
+
68
+ def otp_authentication(username, credential)
69
+ passwords = @users[username].credential.split(/\n/)
70
+ if passwords.empty?
71
+ raise NoFurtherCredentialsAvailable.new
72
+ end
73
+ current_password = passwords.shift
74
+ if credential == current_password
75
+ @users[username].credential = passwords.join("\n")
76
+ return true
77
+ else
78
+ raise InvalidCredentialError.new
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,56 @@
1
+ module EM::FTPD::Memory
2
+ class Driver
3
+ def initialize(options = {})
4
+ filesystem_name = options["filesystem_name"] || "default"
5
+ realm = options["authentication_realm"] || "default"
6
+ @fs = FileSystem.getFileSystemByName(filesystem_name)
7
+ @authenticator = Authenticator.getAuthenticatorByRealm(realm, options)
8
+ end
9
+
10
+ def change_dir(path, &block)
11
+ yield @fs.is_dir(path)
12
+ end
13
+
14
+ def dir_contents(path, &block)
15
+ yield @fs.list_files(path)
16
+ end
17
+
18
+ def authenticate(user, pass, &block)
19
+ begin
20
+ @authenticator.authenticate(user, pass)
21
+ yield true
22
+ rescue Exception => e
23
+ #puts e.backtrace
24
+ yield false
25
+ end
26
+ end
27
+
28
+ def bytes(path, &block)
29
+ yield @fs.file_size(path)
30
+ end
31
+
32
+ def get_file(path, &block)
33
+ yield @fs.file_contents(path)
34
+ end
35
+
36
+ def put_file(path, data, &block)
37
+ yield @fs.create_file(path, data)
38
+ end
39
+
40
+ def delete_file(path, &block)
41
+ yield @fs.remove_file(path)
42
+ end
43
+
44
+ def delete_dir(path, &block)
45
+ yield @fs.delete_dir(path)
46
+ end
47
+
48
+ def rename(from, to, &block)
49
+ yield @fs.rename(from, to)
50
+ end
51
+
52
+ def make_dir(path, &block)
53
+ yield @fs.create_dir(path)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,138 @@
1
+ module EM::FTPD::Memory
2
+ class FileSystem
3
+ @@filesystems = Hash.new
4
+ def self.getFileSystemByName(name)
5
+ if @@filesystems[name].nil?
6
+ @@filesystems[name] = FileSystem.new
7
+ end
8
+ #puts "returning FileSystem named #{name}"
9
+ return @@filesystems[name]
10
+ end
11
+
12
+ def initialize()
13
+ @root = {
14
+ '/' => Array.new
15
+ }
16
+ @contents = Hash.new
17
+ end
18
+
19
+ def exist?(path)
20
+ is_dir(path) || is_file(path)
21
+ end
22
+
23
+ def list_files(path)
24
+ @root[path] || []
25
+ end
26
+
27
+ def file_size(path)
28
+ f = get_file(path)
29
+ return false unless f
30
+ f.size
31
+ end
32
+
33
+ def file_contents(path)
34
+ # nil || false => false
35
+ @contents[path] || false
36
+ end
37
+
38
+ def get_file(path)
39
+ dirname = File.dirname(path)
40
+ basename = File.basename(path)
41
+ if is_dir(dirname)
42
+ return @root[dirname].find {|file| file.directory == false && file.name == basename}
43
+ end
44
+ nil
45
+ end
46
+
47
+ def is_file(path)
48
+ @contents[path] || false
49
+ end
50
+
51
+ def is_dir(path)
52
+ @root[path] != nil
53
+ end
54
+
55
+ def create_file(path, data)
56
+ dirname = File.dirname(path)
57
+ basename = File.basename(path)
58
+ if is_dir(dirname)
59
+ contents = File.open(data,'r').read
60
+ f = get_file(path)
61
+ if f
62
+ f.size = contents.length
63
+ else
64
+ @root[dirname] << EM::FTPD::DirectoryItem.new(:name => basename, :directory => false, :size => contents.length)
65
+ end
66
+ @contents[path] = contents
67
+ return true
68
+ else
69
+ return false
70
+ end
71
+ end
72
+
73
+ def remove_file(path)
74
+ if is_file(path)
75
+ dirname = File.dirname(path)
76
+ basename = File.basename(path)
77
+ @root[dirname].reject! {|file| file.directory == false && file.name == basename}
78
+ @contents.delete(path)
79
+ return true
80
+ end
81
+ false
82
+ end
83
+
84
+ def delete_dir(path)
85
+ if is_dir(path) && @root[path].empty?
86
+ @root.delete(path)
87
+ dirname = File.dirname(path)
88
+ basename = File.basename(path)
89
+ @root[dirname].reject! {|file| file.directory == true && file.name == basename}
90
+ return true
91
+ end
92
+ false
93
+ end
94
+
95
+ def create_dir(path)
96
+ if not exist?(path)
97
+ dirname = File.dirname(path)
98
+ basename = File.basename(path)
99
+ if is_dir(dirname)
100
+ @root[path] = Array.new
101
+ @root[dirname] << EM::FTPD::DirectoryItem.new(:name => basename, :directory => true, :size => 0)
102
+ return true
103
+ end
104
+ end
105
+ return false
106
+ end
107
+
108
+ def rename(from, to)
109
+ return false if exist?(to)
110
+ return false unless exist?(from)
111
+
112
+ from_dirname = File.dirname(from)
113
+ from_basename = File.basename(from)
114
+ to_dirname = File.dirname(to)
115
+ to_basename = File.basename(to)
116
+
117
+ if from_dirname == to_dirname
118
+ @root[from_dirname].find {|file| file.name == from_basename}.name = to_basename
119
+ else
120
+ entry = @root[from_dirname].reject! {|file| file.name == from_basename}
121
+ entry.name = to_basename
122
+ @root[to_dirname] << entry
123
+ end
124
+
125
+ if is_dir(from)
126
+ @root[to] = @root[from]
127
+ @root.delete(from)
128
+ else
129
+ # @contents[to] points to the same reference/pointer as @contents[from]
130
+ # i.e., this is not a copy, they point to the same object
131
+ @contents[to] = @contents[from]
132
+ @contents.delete(from)
133
+ end
134
+ return true
135
+ end
136
+
137
+ end
138
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/test'
3
+ require 'minitest/unit'
4
+ include MiniTest::Assertions
5
+ require File.expand_path('../../lib/em/ftpd/memory.rb', __FILE__)
@@ -0,0 +1,68 @@
1
+ unless Kernel.respond_to?(:require_relative)
2
+ module Kernel
3
+ def require_relative(path)
4
+ require File.join(File.dirname(caller[0]), path.to_str)
5
+ end
6
+ end
7
+ end
8
+
9
+ require_relative 'helper'
10
+ require 'eventmachine'
11
+ require 'em-ftpd'
12
+
13
+ class TestFTPDMemory < Minitest::Test
14
+ def test_example
15
+ options = {
16
+ "filesystem_name" => "boss",
17
+ "authentication_realm" => "georgia",
18
+ "pwalgo" => "otp",
19
+
20
+ }
21
+ auth = EM::FTPD::Memory::Authenticator.getAuthenticatorByRealm(options["authentication_realm"], options)
22
+ auth << EM::FTPD::Memory::User.new("test", "test1\ntest2\ntest3\ntest4\ntest5")
23
+ fs = EM::FTPD::Memory::FileSystem.getFileSystemByName(options["filesystem_name"])
24
+ fs.create_dir("/pub")
25
+ fs.create_file("/pub/helper.rb", "test/helper.rb")
26
+
27
+ EM.run {
28
+ EventMachine::start_server("0.0.0.0", 2021, EM::FTPD::Server, EM::FTPD::Memory::Driver, options)
29
+ EM::Timer.new(0.1) do
30
+ EM.stop
31
+ end
32
+ }
33
+ end
34
+
35
+ def test_plain_login
36
+ end
37
+
38
+ def test_time_based_login
39
+ end
40
+
41
+ def test_otp_login
42
+ end
43
+
44
+ def test_file_creation
45
+ end
46
+
47
+ def test_file_deletion
48
+ end
49
+
50
+ def test_create_directory
51
+ end
52
+
53
+ def test_delete_directory
54
+ end
55
+
56
+ def test_file_rename
57
+ end
58
+
59
+ def test_file_move_between_directories
60
+ end
61
+
62
+ def test_chmod_file
63
+ end
64
+
65
+ def test_chmod_directory
66
+ end
67
+
68
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-ftpd-memory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - chrislee35
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: em-ftpd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This implements a simple in-memory storage of files for use with em-ftpd
84
+ email:
85
+ - rubygems@chrislee.dhs.org
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - em-ftpd-memory.gemspec
96
+ - lib/em/ftpd/memory.rb
97
+ - lib/em/ftpd/memory/authenticator.rb
98
+ - lib/em/ftpd/memory/driver.rb
99
+ - lib/em/ftpd/memory/filesystem.rb
100
+ - test/helper.rb
101
+ - test/test_ftpd-memory.rb
102
+ homepage: https://github.com/chrislee35/em-ftpd-memory
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Memory-based backing store for em-ftpd
126
+ test_files:
127
+ - test/helper.rb
128
+ - test/test_ftpd-memory.rb