hrr_rb_sftp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +25 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +135 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/demo/hrr_rb_sftp_server.rb +23 -0
  13. data/demo/instantiate_hrr_rb_sftp_server.rb +67 -0
  14. data/demo/spawn_hrr_rb_sftp_server.rb +66 -0
  15. data/hrr_rb_sftp.gemspec +26 -0
  16. data/lib/hrr_rb_sftp.rb +13 -0
  17. data/lib/hrr_rb_sftp/loggable.rb +41 -0
  18. data/lib/hrr_rb_sftp/protocol.rb +62 -0
  19. data/lib/hrr_rb_sftp/protocol/common.rb +10 -0
  20. data/lib/hrr_rb_sftp/protocol/common/data_type.rb +15 -0
  21. data/lib/hrr_rb_sftp/protocol/common/data_type/byte.rb +22 -0
  22. data/lib/hrr_rb_sftp/protocol/common/data_type/extension_pair.rb +23 -0
  23. data/lib/hrr_rb_sftp/protocol/common/data_type/extension_pairs.rb +24 -0
  24. data/lib/hrr_rb_sftp/protocol/common/data_type/string.rb +24 -0
  25. data/lib/hrr_rb_sftp/protocol/common/data_type/uint32.rb +22 -0
  26. data/lib/hrr_rb_sftp/protocol/common/data_type/uint64.rb +22 -0
  27. data/lib/hrr_rb_sftp/protocol/common/packet.rb +11 -0
  28. data/lib/hrr_rb_sftp/protocol/common/packet/001_ssh_fxp_init.rb +18 -0
  29. data/lib/hrr_rb_sftp/protocol/common/packet/002_ssh_fxp_version.rb +19 -0
  30. data/lib/hrr_rb_sftp/protocol/common/packetable.rb +72 -0
  31. data/lib/hrr_rb_sftp/protocol/version1.rb +10 -0
  32. data/lib/hrr_rb_sftp/protocol/version1/data_type.rb +11 -0
  33. data/lib/hrr_rb_sftp/protocol/version1/data_type/attrs.rb +54 -0
  34. data/lib/hrr_rb_sftp/protocol/version1/packet.rb +29 -0
  35. data/lib/hrr_rb_sftp/protocol/version1/packet/003_ssh_fxp_open.rb +109 -0
  36. data/lib/hrr_rb_sftp/protocol/version1/packet/004_ssh_fxp_close.rb +44 -0
  37. data/lib/hrr_rb_sftp/protocol/version1/packet/005_ssh_fxp_read.rb +53 -0
  38. data/lib/hrr_rb_sftp/protocol/version1/packet/006_ssh_fxp_write.rb +46 -0
  39. data/lib/hrr_rb_sftp/protocol/version1/packet/007_ssh_fxp_lstat.rb +62 -0
  40. data/lib/hrr_rb_sftp/protocol/version1/packet/008_ssh_fxp_fstat.rb +48 -0
  41. data/lib/hrr_rb_sftp/protocol/version1/packet/009_ssh_fxp_setstat.rb +63 -0
  42. data/lib/hrr_rb_sftp/protocol/version1/packet/010_ssh_fxp_fsetstat.rb +48 -0
  43. data/lib/hrr_rb_sftp/protocol/version1/packet/011_ssh_fxp_opendir.rb +65 -0
  44. data/lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb +134 -0
  45. data/lib/hrr_rb_sftp/protocol/version1/packet/013_ssh_fxp_remove.rb +57 -0
  46. data/lib/hrr_rb_sftp/protocol/version1/packet/014_ssh_fxp_mkdir.rb +57 -0
  47. data/lib/hrr_rb_sftp/protocol/version1/packet/015_ssh_fxp_rmdir.rb +73 -0
  48. data/lib/hrr_rb_sftp/protocol/version1/packet/016_ssh_fxp_realpath.rb +30 -0
  49. data/lib/hrr_rb_sftp/protocol/version1/packet/017_ssh_fxp_stat.rb +62 -0
  50. data/lib/hrr_rb_sftp/protocol/version1/packet/101_ssh_fxp_status.rb +29 -0
  51. data/lib/hrr_rb_sftp/protocol/version1/packet/102_ssh_fxp_handle.rb +19 -0
  52. data/lib/hrr_rb_sftp/protocol/version1/packet/103_ssh_fxp_data.rb +19 -0
  53. data/lib/hrr_rb_sftp/protocol/version1/packet/104_ssh_fxp_name.rb +33 -0
  54. data/lib/hrr_rb_sftp/protocol/version1/packet/105_ssh_fxp_attrs.rb +19 -0
  55. data/lib/hrr_rb_sftp/protocol/version2.rb +10 -0
  56. data/lib/hrr_rb_sftp/protocol/version2/data_type.rb +9 -0
  57. data/lib/hrr_rb_sftp/protocol/version2/packet.rb +11 -0
  58. data/lib/hrr_rb_sftp/protocol/version2/packet/018_ssh_fxp_rename.rb +70 -0
  59. data/lib/hrr_rb_sftp/protocol/version3.rb +10 -0
  60. data/lib/hrr_rb_sftp/protocol/version3/data_type.rb +9 -0
  61. data/lib/hrr_rb_sftp/protocol/version3/packet.rb +16 -0
  62. data/lib/hrr_rb_sftp/protocol/version3/packet/014_ssh_fxp_mkdir.rb +58 -0
  63. data/lib/hrr_rb_sftp/protocol/version3/packet/019_ssh_fxp_readlink.rb +57 -0
  64. data/lib/hrr_rb_sftp/protocol/version3/packet/020_ssh_fxp_symlink.rb +58 -0
  65. data/lib/hrr_rb_sftp/protocol/version3/packet/101_ssh_fxp_status.rb +31 -0
  66. data/lib/hrr_rb_sftp/protocol/version3/packet/200_ssh_fxp_extended.rb +34 -0
  67. data/lib/hrr_rb_sftp/protocol/version3/packet/201_ssh_fxp_extended_reply.rb +23 -0
  68. data/lib/hrr_rb_sftp/receiver.rb +22 -0
  69. data/lib/hrr_rb_sftp/sender.rb +13 -0
  70. data/lib/hrr_rb_sftp/server.rb +96 -0
  71. data/lib/hrr_rb_sftp/version.rb +3 -0
  72. metadata +114 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 836dd45879b224d1b99e0584fa6dfb374d51ddb77cbd90dbb08c2697aed1e8b4
4
+ data.tar.gz: 68dccfef0acb49b3576ef35fe500c8669e213d5b732ebdac34d3ab54ebba3953
5
+ SHA512:
6
+ metadata.gz: cf36e84da8b26837ad259906b8d6340ed1cfc5e7e0ed3c88c1bd93555d8ab9964321504d40971b26e8cfae48808492389964bf5ffc706bbd4012c1adee1749ff
7
+ data.tar.gz: 87ef0bfbefdfc259747eecfce36cf7bcef3a07729380b1c827907f62cc64fe5b1065092f5ab65f41622b64afba0dac7744c73d6a69f4410001e65af64159b7c0
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Bundler related files
14
+ /Gemfile.lock
15
+ /vendor
16
+
17
+ # rbenv related files
18
+ /.ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,25 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ - 2.3
9
+ - 2.4
10
+ - 2.5
11
+ - 2.6
12
+ - 2.7
13
+ - ruby-head
14
+ jobs:
15
+ allow_failures:
16
+ - rvm: ruby-head
17
+ before_install:
18
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
19
+ - gem install bundler -v '< 2'
20
+ - yes | gem update --system --force
21
+ - gem update bundler
22
+ install:
23
+ - bundle install
24
+ script:
25
+ - bundle exec rake spec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hirura@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hrr_rb_sftp.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 hirura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,135 @@
1
+ # HrrRbSftp
2
+
3
+ [![Build Status](https://travis-ci.com/hirura/hrr_rb_sftp.svg?branch=master)](https://travis-ci.com/hirura/hrr_rb_sftp)
4
+ [![Gem Version](https://badge.fury.io/rb/hrr_rb_sftp.svg)](https://badge.fury.io/rb/hrr_rb_sftp)
5
+
6
+ hrr_rb_sftp is a pure Ruby SFTP server implementation. hrr_rb_sftp now supports SFTP protocol version 1, 2, and 3.
7
+
8
+ hrr_rb_sftp can be run on SSH 2.0 server like OpenSSH or [hrr_rb_ssh](https://github.com/hirura/hrr_rb_ssh).
9
+
10
+ ## Table of Contents
11
+
12
+ - [Installation](#installation)
13
+ - [Usage](#usage)
14
+ - [hrr\_rb\_ssh's SFTP subsystem](#hrr_rb_sshs-sftp-subsystem)
15
+ - [OpenSSH's SFTP subsystem](#opensshs-sftp-subsystem)
16
+ - [Development](#development)
17
+ - [Contributing](#contributing)
18
+ - [License](#license)
19
+ - [Code of Conduct](#code-of-conduct)
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'hrr_rb_sftp'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle install
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install hrr_rb_sftp
36
+
37
+ ## Usage
38
+
39
+ Here, some typical usage is described, but is not limited to.
40
+
41
+ ### hrr_rb_ssh's SFTP subsystem
42
+
43
+ hrr_rb_sftp is written in Ruby, so it is straightforward to implement SFTP server on SSH 2.0 server library written in Ruby like hrr_rb_ssh.
44
+
45
+ There are two ways to work with hrr_rb_ssh, on same process or spawning child process. On both cases, hrr_rb_ssh's request handler mechanism is used.
46
+
47
+ To run hrr_rb_sftp server on the same process as hrr_rb_ssh is running, the hrr_rb_ssh's request handler is as follows.
48
+
49
+ ```ruby
50
+ subsys = HrrRbSsh::Connection::RequestHandler.new { |ctx|
51
+ ctx.chain_proc { |chain|
52
+ case ctx.subsystem_name
53
+ when 'sftp'
54
+ begin
55
+ sftp_server = HrrRbSftp::Server.new(logger: nil)
56
+ sftp_server.start(ctx.io[0], ctx.io[1], ctx.io[2])
57
+ exitstatus = 0
58
+ rescue
59
+ exitstatus = 1
60
+ end
61
+ else
62
+ # Do something for other subsystem, or just return exitstatus
63
+ exitstatus = 0
64
+ end
65
+ exitstatus
66
+ }
67
+ }
68
+
69
+ options['connection_channel_request_subsystem'] = subsys
70
+ ```
71
+
72
+ On the other hand, because the arguments for the hrr_rb_sftp server can be standard input, output, and error, so hrr_rb_sftp can be a independent program and be spawned as a child process.
73
+
74
+ ```ruby
75
+ subsys = HrrRbSsh::Connection::RequestHandler.new { |ctx|
76
+ ctx.chain_proc { |chain|
77
+ case ctx.subsystem_name
78
+ when 'sftp'
79
+ pid = spawn("/path/to/hrr_rb_sftp_server.rb", {in: ctx.io[0], out: ctx.io[1], err: ctx.io[2]})
80
+ exitstatus = Process.waitpid(pid).to_i
81
+ else
82
+ # Do something for other subsystem, or just return exitstatus
83
+ exitstatus = 0
84
+ end
85
+ exitstatus
86
+ }
87
+ }
88
+
89
+ options['connection_channel_request_subsystem'] = subsys
90
+ ```
91
+
92
+ Where, the /path/to/hrr_rb_sftp_server.rb is as follows.
93
+
94
+ ```ruby
95
+ #!/usr/bin/env ruby
96
+
97
+ require "hrr_rb_sftp"
98
+
99
+ server = HrrRbSftp::Server.new(logger: nil)
100
+ server.start($stdin, $stdout, $stderr)
101
+ ```
102
+
103
+ ### OpenSSH's SFTP subsystem
104
+
105
+ OpenSSH has capability to run user-defined subsystems. Subsystems that the OpenSSH server recognizes are listed in /etc/ssh/sshd_config file. Usually SFTP subsystem is defined by default to use OpenSSH's SFTP server implementation.
106
+
107
+ $ cat /etc/ssh/sshd_config | grep Subsystem
108
+ Subsystem sftp /usr/lib/openssh/sftp-server
109
+
110
+ hrr_rb_sftp can be an alternative with replacing the line in the config file. (After editing the config, reloading or restarting sshd is required.)
111
+
112
+ $ cat /etc/ssh/sshd_config | grep Subsystem
113
+ #Subsystem sftp /usr/lib/openssh/sftp-server # Comment out the original line
114
+ Subsystem sftp /path/to/hrr_rb_sftp_server.rb
115
+
116
+ Where, the /path/to/hrr_rb_sftp_server.rb code is the same as shown above.
117
+
118
+ ## Development
119
+
120
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ To install this gem onto your local machine, run `bundle exec rake install`.
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hirura/hrr_rb_sftp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/hirura/hrr_rb_sftp/blob/master/CODE_OF_CONDUCT.md).
127
+
128
+
129
+ ## License
130
+
131
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
132
+
133
+ ## Code of Conduct
134
+
135
+ Everyone interacting in the HrrRbSftp project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hirura/hrr_rb_sftp/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hrr_rb_sftp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "logger"
4
+
5
+ begin
6
+ require 'hrr_rb_sftp'
7
+ rescue LoadError
8
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'hrr_rb_sftp'
10
+ end
11
+
12
+ class MyLoggerFormatter < ::Logger::Formatter
13
+ def call severity, time, progname, msg
14
+ "%s, [%s#%d.%x] %5s -- %s: %s\n" % [severity[0..0], format_datetime(time), Process.pid, Thread.current.object_id, severity, progname, msg2str(msg)]
15
+ end
16
+ end
17
+
18
+ logger = Logger.new(File.join(File.dirname(__FILE__), "hrr_rb_sftp_server.log"))
19
+ logger.level = Logger::INFO
20
+ logger.formatter = MyLoggerFormatter.new
21
+
22
+ server = HrrRbSftp::Server.new(logger: logger)
23
+ server.start $stdin, $stdout, $stderr
@@ -0,0 +1,67 @@
1
+ require 'logger'
2
+ require 'socket'
3
+ require 'hrr_rb_ssh'
4
+
5
+ begin
6
+ require 'hrr_rb_sftp'
7
+ rescue LoadError
8
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'hrr_rb_sftp'
10
+ end
11
+
12
+ class MyLoggerFormatter < ::Logger::Formatter
13
+ def call severity, time, progname, msg
14
+ "%s, [%s#%d.%x] %5s -- %s: %s\n" % [severity[0..0], format_datetime(time), Process.pid, Thread.current.object_id, severity, progname, msg2str(msg)]
15
+ end
16
+ end
17
+
18
+ logger = Logger.new(File.join(File.dirname(__FILE__), "instantiate_hrr_rb_sftp_server.log"))
19
+ logger.level = Logger::INFO
20
+ logger.formatter = MyLoggerFormatter.new
21
+
22
+ auth_publickey = HrrRbSsh::Authentication::Authenticator.new { |ctx|
23
+ true # accept any user
24
+ }
25
+
26
+ conn_subsys = HrrRbSsh::Connection::RequestHandler.new { |ctx|
27
+ ctx.chain_proc { |chain|
28
+ case ctx.subsystem_name
29
+ when 'sftp'
30
+ sftp_server = HrrRbSftp::Server.new(logger: logger)
31
+ sftp_server.start(ctx.io[0], ctx.io[1], ctx.io[2])
32
+ exitstatus = 0
33
+ else
34
+ exitstatus = 1
35
+ end
36
+ exitstatus
37
+ }
38
+ }
39
+
40
+ options = {}
41
+ options['authentication_publickey_authenticator'] = auth_publickey
42
+ options['connection_channel_request_subsystem'] = conn_subsys
43
+
44
+ server = TCPServer.new 10022
45
+ while true
46
+ Thread.new(server.accept) do |io|
47
+ begin
48
+ pid = fork do
49
+ begin
50
+ ssh_server = HrrRbSsh::Server.new(options, logger: logger)
51
+ ssh_server.start io
52
+ rescue => e
53
+ logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
54
+ exit false
55
+ end
56
+ end
57
+ logger.info { "process #{pid} started" }
58
+ io.close rescue nil
59
+ pid, status = Process.waitpid2(pid)
60
+ rescue => e
61
+ logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
62
+ ensure
63
+ status ||= nil
64
+ logger.info { "process #{pid} finished with status #{status.inspect}" }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,66 @@
1
+ require 'logger'
2
+ require 'socket'
3
+ require 'hrr_rb_ssh'
4
+
5
+ begin
6
+ require 'hrr_rb_sftp'
7
+ rescue LoadError
8
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'hrr_rb_sftp'
10
+ end
11
+
12
+ class MyLoggerFormatter < ::Logger::Formatter
13
+ def call severity, time, progname, msg
14
+ "%s, [%s#%d.%x] %5s -- %s: %s\n" % [severity[0..0], format_datetime(time), Process.pid, Thread.current.object_id, severity, progname, msg2str(msg)]
15
+ end
16
+ end
17
+
18
+ logger = Logger.new(File.join(File.dirname(__FILE__), "spawn_hrr_rb_sftp_server.log"))
19
+ logger.level = Logger::INFO
20
+ logger.formatter = MyLoggerFormatter.new
21
+
22
+ auth_publickey = HrrRbSsh::Authentication::Authenticator.new { |ctx|
23
+ true # accept any user
24
+ }
25
+
26
+ conn_subsys = HrrRbSsh::Connection::RequestHandler.new { |ctx|
27
+ ctx.chain_proc { |chain|
28
+ case ctx.subsystem_name
29
+ when 'sftp'
30
+ pid = spawn(File.join(File.dirname(__FILE__), "hrr_rb_sftp_server.rb"), {in: ctx.io[0], out: ctx.io[1], err: ctx.io[2]})
31
+ exitstatus = Process.waitpid(pid).to_i
32
+ else
33
+ exitstatus = 1
34
+ end
35
+ exitstatus
36
+ }
37
+ }
38
+
39
+ options = {}
40
+ options['authentication_publickey_authenticator'] = auth_publickey
41
+ options['connection_channel_request_subsystem'] = conn_subsys
42
+
43
+ server = TCPServer.new 10022
44
+ while true
45
+ Thread.new(server.accept) do |io|
46
+ begin
47
+ pid = fork do
48
+ begin
49
+ ssh_server = HrrRbSsh::Server.new(options, logger: logger)
50
+ ssh_server.start io
51
+ rescue => e
52
+ logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
53
+ exit false
54
+ end
55
+ end
56
+ logger.info { "process #{pid} started" }
57
+ io.close rescue nil
58
+ pid, status = Process.waitpid2(pid)
59
+ rescue => e
60
+ logger.error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
61
+ ensure
62
+ status ||= nil
63
+ logger.info { "process #{pid} finished with status #{status.inspect}" }
64
+ end
65
+ end
66
+ end