tpkg 2.3.3 → 2.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +20 -0
  5. data/Portfile +39 -0
  6. data/Portfile.template +39 -0
  7. data/README.md +43 -0
  8. data/Rakefile +468 -18
  9. data/bin/gem2tpkg +2 -2
  10. data/bin/tpkg +18 -13
  11. data/bin/tpkg_uploader +132 -0
  12. data/ca.pem +1 -0
  13. data/control +7 -0
  14. data/depend +3 -0
  15. data/externals-etch/authorized_keys +40 -0
  16. data/externals-etch/group +9 -0
  17. data/externals-etch/iptables +38 -0
  18. data/externals-etch/limits +30 -0
  19. data/externals-etch/nfs +30 -0
  20. data/externals-etch/sudo +30 -0
  21. data/externals-etch/supplemental_groups +8 -0
  22. data/externals-etch/sysctl +30 -0
  23. data/externals-etch/user +41 -0
  24. data/externals/group +39 -0
  25. data/externals/supplemental_groups +48 -0
  26. data/externals/user +39 -0
  27. data/lib/tpkg.rb +260 -991
  28. data/lib/tpkg/os.rb +164 -0
  29. data/lib/tpkg/os/debian.rb +159 -0
  30. data/lib/tpkg/os/freebsd.rb +113 -0
  31. data/lib/tpkg/os/macosx.rb +113 -0
  32. data/lib/tpkg/os/redhat.rb +173 -0
  33. data/lib/tpkg/os/solaris.rb +101 -0
  34. data/lib/tpkg/os/windows.rb +26 -0
  35. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify.rb +67 -0
  36. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/errors.rb +127 -0
  37. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/kwalify.schema.yaml +58 -0
  38. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/main.rb +442 -0
  39. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/messages.rb +173 -0
  40. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/meta-validator.rb +275 -0
  41. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/parser/base.rb +127 -0
  42. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/parser/yaml.rb +841 -0
  43. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/rule.rb +559 -0
  44. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/templates/genclass-java.eruby +222 -0
  45. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/templates/genclass-php.eruby +104 -0
  46. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/templates/genclass-ruby.eruby +113 -0
  47. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/types.rb +156 -0
  48. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util.rb +158 -0
  49. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/assert-text-equal.rb +46 -0
  50. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/hash-interface.rb +18 -0
  51. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/hashlike.rb +51 -0
  52. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/option-parser.rb +220 -0
  53. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/ordered-hash.rb +57 -0
  54. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/util/testcase-helper.rb +112 -0
  55. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/validator.rb +282 -0
  56. data/lib/tpkg/thirdparty/kwalify-0.7.2/lib/kwalify/yaml-parser.rb +870 -0
  57. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh.rb +219 -0
  58. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/agent.rb +179 -0
  59. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/constants.rb +18 -0
  60. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/key_manager.rb +219 -0
  61. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/abstract.rb +60 -0
  62. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/hostbased.rb +71 -0
  63. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/keyboard_interactive.rb +66 -0
  64. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/password.rb +39 -0
  65. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/methods/publickey.rb +92 -0
  66. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/pageant.rb +183 -0
  67. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/authentication/session.rb +134 -0
  68. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/buffer.rb +340 -0
  69. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/buffered_io.rb +198 -0
  70. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/config.rb +205 -0
  71. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/channel.rb +630 -0
  72. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/constants.rb +33 -0
  73. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/session.rb +597 -0
  74. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/connection/term.rb +178 -0
  75. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/errors.rb +85 -0
  76. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/key_factory.rb +102 -0
  77. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/known_hosts.rb +129 -0
  78. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/loggable.rb +61 -0
  79. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/packet.rb +102 -0
  80. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/prompt.rb +93 -0
  81. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/command.rb +75 -0
  82. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/errors.rb +14 -0
  83. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/http.rb +94 -0
  84. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/socks4.rb +70 -0
  85. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/proxy/socks5.rb +142 -0
  86. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/ruby_compat.rb +43 -0
  87. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/service/forward.rb +288 -0
  88. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test.rb +89 -0
  89. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/channel.rb +129 -0
  90. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/extensions.rb +152 -0
  91. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/kex.rb +44 -0
  92. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/local_packet.rb +51 -0
  93. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/packet.rb +81 -0
  94. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/remote_packet.rb +38 -0
  95. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/script.rb +157 -0
  96. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/test/socket.rb +64 -0
  97. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/algorithms.rb +384 -0
  98. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/cipher_factory.rb +97 -0
  99. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/constants.rb +30 -0
  100. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac.rb +31 -0
  101. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/abstract.rb +79 -0
  102. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/md5.rb +12 -0
  103. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
  104. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/none.rb +15 -0
  105. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/sha1.rb +13 -0
  106. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
  107. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/identity_cipher.rb +55 -0
  108. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/kex.rb +13 -0
  109. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +208 -0
  110. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +77 -0
  111. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/openssl.rb +127 -0
  112. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/packet_stream.rb +235 -0
  113. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/server_version.rb +71 -0
  114. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/session.rb +276 -0
  115. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/transport/state.rb +206 -0
  116. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/verifiers/lenient.rb +30 -0
  117. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/verifiers/null.rb +12 -0
  118. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/verifiers/strict.rb +53 -0
  119. data/lib/tpkg/thirdparty/net-ssh-2.1.0/lib/net/ssh/version.rb +62 -0
  120. data/lib/tpkg/version.rb +3 -0
  121. data/man/man1/cpan2tpkg.1 +82 -0
  122. data/man/man1/gem2tpkg.1 +120 -0
  123. data/man/man1/tpkg.1 +411 -0
  124. data/pkginfo +8 -0
  125. data/postinstall.solaris +11 -0
  126. data/postremove.solaris +16 -0
  127. data/schema/schema-1.0.5.yml +0 -0
  128. data/schema/schema-1.0.6.yml +0 -0
  129. data/schema/schema-1.0.7.yml +0 -0
  130. data/schema/schema-1.0.8.yml +0 -0
  131. data/schema/schema-1.0.9.yml +0 -0
  132. data/schema/schema.yml +0 -0
  133. data/schema/tpkg-1.0.0.dtd +0 -0
  134. data/schema/tpkg-1.0.1.dtd +0 -0
  135. data/schema/tpkg-1.0.2.dtd +0 -0
  136. data/schema/tpkg-1.0.3.dtd +0 -0
  137. data/schema/tpkg-1.0.4.dtd +0 -0
  138. data/schema/tpkg-1.0.5.dtd +0 -0
  139. data/schema/tpkg-1.0.6.dtd +0 -0
  140. data/schema/tpkg-1.0.7.dtd +0 -0
  141. data/schema/tpkg-1.0.8.dtd +0 -0
  142. data/schema/tpkg-1.0.9.dtd +0 -0
  143. data/schema/tpkg.dtd +0 -0
  144. data/test/TODO +30 -0
  145. data/test/premadetestpkg/pkg_without_file_metadata-1.0-1.tpkg +0 -0
  146. data/test/test_checksum.rb +53 -0
  147. data/test/test_compress.rb +55 -0
  148. data/test/test_conflict.rb +41 -0
  149. data/test/test_crontabs.rb +398 -0
  150. data/test/test_dependency.rb +1113 -0
  151. data/test/test_downgrade.rb +80 -0
  152. data/test/test_download.rb +95 -0
  153. data/test/test_encrypt.rb +136 -0
  154. data/test/test_filemetadata.rb +131 -0
  155. data/test/test_initscript.rb +93 -0
  156. data/test/test_install.rb +186 -0
  157. data/test/test_lock.rb +82 -0
  158. data/test/test_make.rb +410 -0
  159. data/test/test_metadata.rb +805 -0
  160. data/test/test_misc.rb +379 -0
  161. data/test/test_options.rb +1711 -0
  162. data/test/test_os.rb +193 -0
  163. data/test/test_os_debian.rb +99 -0
  164. data/test/test_os_freebsd.rb +89 -0
  165. data/test/test_os_macosx.rb +79 -0
  166. data/test/test_os_redhat.rb +124 -0
  167. data/test/test_os_solaris.rb +85 -0
  168. data/test/test_os_windows.rb +26 -0
  169. data/test/test_query.rb +134 -0
  170. data/test/test_remove.rb +539 -0
  171. data/test/test_tar.rb +99 -0
  172. data/test/test_unpack.rb +977 -0
  173. data/test/test_upgrade.rb +398 -0
  174. data/test/test_version.rb +54 -0
  175. data/test/testcmds/crontab +14 -0
  176. data/test/testcmds/debian/apt-cache +145 -0
  177. data/test/testcmds/debian/dpkg-query +16 -0
  178. data/test/testcmds/freebsd/pkg_info +13 -0
  179. data/test/testcmds/macosx/port +35 -0
  180. data/test/testcmds/redhat/rpmbuild +6 -0
  181. data/test/testcmds/redhat/yum +90 -0
  182. data/test/testcmds/solaris/pkginfo +25 -0
  183. data/test/testcmds/solaris/pkgutil +36 -0
  184. data/test/testpkg/reloc/encfile +2 -0
  185. data/test/testpkg/reloc/file +2 -0
  186. data/test/testpkg/reloc/precryptfile +1 -0
  187. data/test/testpkg/reloc/precryptfile.plaintext +3 -0
  188. data/test/testpkg/tpkg-bad-ownergroup.xml +25 -0
  189. data/test/testpkg/tpkg-bad-ownergroup.yml +18 -0
  190. data/test/testpkg/tpkg-default-perms.xml +28 -0
  191. data/test/testpkg/tpkg-default-perms.yml +20 -0
  192. data/test/testpkg/tpkg-good-ownergroup.xml +25 -0
  193. data/test/testpkg/tpkg-good-ownergroup.yml +18 -0
  194. data/test/testpkg/tpkg-nativedeps.yml +13 -0
  195. data/test/testpkg/tpkg-nofiles.xml +14 -0
  196. data/test/testpkg/tpkg-nofiles.yml +9 -0
  197. data/test/testpkg/tpkg.xml +35 -0
  198. data/test/testpkg/tpkg.yml +25 -0
  199. data/test/tpkgtest.rb +300 -0
  200. data/tpkg.conf +16 -0
  201. data/tpkg.gemspec +24 -0
  202. data/tpkg.spec +28 -0
  203. data/tpkg.xml +17 -0
  204. data/tpkg_profile.sh +32 -0
  205. metadata +306 -31
@@ -0,0 +1,142 @@
1
+ require 'socket'
2
+ require 'net/ssh/ruby_compat'
3
+ require 'net/ssh/proxy/errors'
4
+
5
+ module Net
6
+ module SSH
7
+ module Proxy
8
+
9
+ # An implementation of a SOCKS5 proxy. To use it, instantiate it, then
10
+ # pass the instantiated object via the :proxy key to Net::SSH.start:
11
+ #
12
+ # require 'net/ssh/proxy/socks5'
13
+ #
14
+ # proxy = Net::SSH::Proxy::SOCKS5.new('proxy.host', proxy_port,
15
+ # :user => 'user', :password => "password")
16
+ # Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
17
+ # ...
18
+ # end
19
+ class SOCKS5
20
+ # The SOCKS protocol version used by this class
21
+ VERSION = 5
22
+
23
+ # The SOCKS authentication type for requests without authentication
24
+ METHOD_NO_AUTH = 0
25
+
26
+ # The SOCKS authentication type for requests via username/password
27
+ METHOD_PASSWD = 2
28
+
29
+ # The SOCKS authentication type for when there are no supported
30
+ # authentication methods.
31
+ METHOD_NONE = 0xFF
32
+
33
+ # The SOCKS packet type for requesting a proxy connection.
34
+ CMD_CONNECT = 1
35
+
36
+ # The SOCKS address type for connections via IP address.
37
+ ATYP_IPV4 = 1
38
+
39
+ # The SOCKS address type for connections via domain name.
40
+ ATYP_DOMAIN = 3
41
+
42
+ # The SOCKS response code for a successful operation.
43
+ SUCCESS = 0
44
+
45
+ # The proxy's host name or IP address
46
+ attr_reader :proxy_host
47
+
48
+ # The proxy's port number
49
+ attr_reader :proxy_port
50
+
51
+ # The map of options given at initialization
52
+ attr_reader :options
53
+
54
+ # Create a new proxy connection to the given proxy host and port.
55
+ # Optionally, :user and :password options may be given to
56
+ # identify the username and password with which to authenticate.
57
+ def initialize(proxy_host, proxy_port=1080, options={})
58
+ @proxy_host = proxy_host
59
+ @proxy_port = proxy_port
60
+ @options = options
61
+ end
62
+
63
+ # Return a new socket connected to the given host and port via the
64
+ # proxy that was requested when the socket factory was instantiated.
65
+ def open(host, port)
66
+ socket = TCPSocket.new(proxy_host, proxy_port)
67
+
68
+ methods = [METHOD_NO_AUTH]
69
+ methods << METHOD_PASSWD if options[:user]
70
+
71
+ packet = [VERSION, methods.size, *methods].pack("C*")
72
+ socket.send packet, 0
73
+
74
+ version, method = socket.recv(2).unpack("CC")
75
+ if version != VERSION
76
+ socket.close
77
+ raise Net::SSH::Proxy::Error, "invalid SOCKS version (#{version})"
78
+ end
79
+
80
+ if method == METHOD_NONE
81
+ socket.close
82
+ raise Net::SSH::Proxy::Error, "no supported authorization methods"
83
+ end
84
+
85
+ negotiate_password(socket) if method == METHOD_PASSWD
86
+
87
+ packet = [VERSION, CMD_CONNECT, 0].pack("C*")
88
+
89
+ if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
90
+ packet << [ATYP_IPV4, $1.to_i, $2.to_i, $3.to_i, $4.to_i].pack("C*")
91
+ else
92
+ packet << [ATYP_DOMAIN, host.length, host].pack("CCA*")
93
+ end
94
+
95
+ packet << [port].pack("n")
96
+ socket.send packet, 0
97
+
98
+ version, reply, = socket.recv(2).unpack("C*")
99
+ socket.recv(1)
100
+ address_type = socket.recv(1).getbyte(0)
101
+ case address_type
102
+ when 1
103
+ socket.recv(4) # get four bytes for IPv4 address
104
+ when 3
105
+ len = socket.recv(1).getbyte(0)
106
+ hostname = socket.recv(len)
107
+ when 4
108
+ ipv6addr hostname = socket.recv(16)
109
+ else
110
+ socket.close
111
+ raise ConnectionError, "Illegal response type"
112
+ end
113
+ portnum = socket.recv(2)
114
+
115
+ unless reply == SUCCESS
116
+ socket.close
117
+ raise ConnectError, "#{reply}"
118
+ end
119
+
120
+ return socket
121
+ end
122
+
123
+ private
124
+
125
+ # Simple username/password negotiation with the SOCKS5 server.
126
+ def negotiate_password(socket)
127
+ packet = [0x01, options[:user].length, options[:user],
128
+ options[:password].length, options[:password]].pack("CCA*CA*")
129
+ socket.send packet, 0
130
+
131
+ version, status = socket.recv(2).unpack("CC")
132
+
133
+ if status != SUCCESS
134
+ socket.close
135
+ raise UnauthorizedError, "could not authorize user"
136
+ end
137
+ end
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,43 @@
1
+ require 'thread'
2
+
3
+ class String
4
+ if RUBY_VERSION < "1.9"
5
+ def getbyte(index)
6
+ self[index]
7
+ end
8
+ end
9
+ end
10
+
11
+ module Net; module SSH
12
+
13
+ # This class contains miscellaneous patches and workarounds
14
+ # for different ruby implementations.
15
+ class Compat
16
+
17
+ # A workaround for an IO#select threading bug in certain versions of MRI 1.8.
18
+ # See: http://net-ssh.lighthouseapp.com/projects/36253/tickets/1-ioselect-threading-bug-in-ruby-18
19
+ # The root issue is documented here: http://redmine.ruby-lang.org/issues/show/1993
20
+ if RUBY_VERSION >= '1.9' || RUBY_PLATFORM == 'java'
21
+ def self.io_select(*params)
22
+ IO.select(*params)
23
+ end
24
+ else
25
+ SELECT_MUTEX = Mutex.new
26
+ def self.io_select(*params)
27
+ # It should be safe to wrap calls in a mutex when the timeout is 0
28
+ # (that is, the call is not supposed to block).
29
+ # We leave blocking calls unprotected to avoid causing deadlocks.
30
+ # This should still catch the main case for Capistrano users.
31
+ if params[3] == 0
32
+ SELECT_MUTEX.synchronize do
33
+ IO.select(*params)
34
+ end
35
+ else
36
+ IO.select(*params)
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end; end
@@ -0,0 +1,288 @@
1
+ require 'net/ssh/loggable'
2
+
3
+ module Net; module SSH; module Service
4
+
5
+ # This class implements various port forwarding services for use by
6
+ # Net::SSH clients. The Forward class should never need to be instantiated
7
+ # directly; instead, it should be accessed via the singleton instance
8
+ # returned by Connection::Session#forward:
9
+ #
10
+ # ssh.forward.local(1234, "www.capify.org", 80)
11
+ class Forward
12
+ include Loggable
13
+
14
+ # The underlying connection service instance that the port-forwarding
15
+ # services employ.
16
+ attr_reader :session
17
+
18
+ # A simple class for representing a requested remote forwarded port.
19
+ Remote = Struct.new(:host, :port) #:nodoc:
20
+
21
+ # Instantiates a new Forward service instance atop the given connection
22
+ # service session. This will register new channel open handlers to handle
23
+ # the specialized channels that the SSH port forwarding protocols employ.
24
+ def initialize(session)
25
+ @session = session
26
+ self.logger = session.logger
27
+ @remote_forwarded_ports = {}
28
+ @local_forwarded_ports = {}
29
+ @agent_forwarded = false
30
+
31
+ session.on_open_channel('forwarded-tcpip', &method(:forwarded_tcpip))
32
+ session.on_open_channel('auth-agent', &method(:auth_agent_channel))
33
+ session.on_open_channel('auth-agent@openssh.com', &method(:auth_agent_channel))
34
+ end
35
+
36
+ # Starts listening for connections on the local host, and forwards them
37
+ # to the specified remote host/port via the SSH connection. This method
38
+ # accepts either three or four arguments. When four arguments are given,
39
+ # they are:
40
+ #
41
+ # * the local address to bind to
42
+ # * the local port to listen on
43
+ # * the remote host to forward connections to
44
+ # * the port on the remote host to connect to
45
+ #
46
+ # If three arguments are given, it is as if the local bind address is
47
+ # "127.0.0.1", and the rest are applied as above.
48
+ #
49
+ # ssh.forward.local(1234, "www.capify.org", 80)
50
+ # ssh.forward.local("0.0.0.0", 1234, "www.capify.org", 80)
51
+ def local(*args)
52
+ if args.length < 3 || args.length > 4
53
+ raise ArgumentError, "expected 3 or 4 parameters, got #{args.length}"
54
+ end
55
+
56
+ bind_address = "127.0.0.1"
57
+ bind_address = args.shift if args.first.is_a?(String) && args.first =~ /\D/
58
+
59
+ local_port = args.shift.to_i
60
+ remote_host = args.shift
61
+ remote_port = args.shift.to_i
62
+
63
+ socket = TCPServer.new(bind_address, local_port)
64
+
65
+ @local_forwarded_ports[[local_port, bind_address]] = socket
66
+
67
+ session.listen_to(socket) do |server|
68
+ client = server.accept
69
+ debug { "received connection on #{bind_address}:#{local_port}" }
70
+
71
+ channel = session.open_channel("direct-tcpip", :string, remote_host, :long, remote_port, :string, bind_address, :long, local_port) do |achannel|
72
+ achannel.info { "direct channel established" }
73
+ end
74
+
75
+ prepare_client(client, channel, :local)
76
+
77
+ channel.on_open_failed do |ch, code, description|
78
+ channel.error { "could not establish direct channel: #{description} (#{code})" }
79
+ channel[:socket].close
80
+ end
81
+ end
82
+ end
83
+
84
+ # Terminates an active local forwarded port. If no such forwarded port
85
+ # exists, this will raise an exception. Otherwise, the forwarded connection
86
+ # is terminated.
87
+ #
88
+ # ssh.forward.cancel_local(1234)
89
+ # ssh.forward.cancel_local(1234, "0.0.0.0")
90
+ def cancel_local(port, bind_address="127.0.0.1")
91
+ socket = @local_forwarded_ports.delete([port, bind_address])
92
+ socket.shutdown rescue nil
93
+ socket.close rescue nil
94
+ session.stop_listening_to(socket)
95
+ end
96
+
97
+ # Returns a list of all active locally forwarded ports. The returned value
98
+ # is an array of arrays, where each element is a two-element tuple
99
+ # consisting of the local port and bind address corresponding to the
100
+ # forwarding port.
101
+ def active_locals
102
+ @local_forwarded_ports.keys
103
+ end
104
+
105
+ # Requests that all connections on the given remote-port be forwarded via
106
+ # the local host to the given port/host. The last argument describes the
107
+ # bind address on the remote host, and defaults to 127.0.0.1.
108
+ #
109
+ # This method will return immediately, but the port will not actually be
110
+ # forwarded immediately. If the remote server is not able to begin the
111
+ # listener for this request, an exception will be raised asynchronously.
112
+ #
113
+ # If you want to know when the connection is active, it will show up in the
114
+ # #active_remotes list. If you want to block until the port is active, you
115
+ # could do something like this:
116
+ #
117
+ # ssh.forward.remote(80, "www.google.com", 1234, "0.0.0.0")
118
+ # ssh.loop { !ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
119
+ def remote(port, host, remote_port, remote_host="127.0.0.1")
120
+ session.send_global_request("tcpip-forward", :string, remote_host, :long, remote_port) do |success, response|
121
+ if success
122
+ debug { "remote forward from remote #{remote_host}:#{remote_port} to #{host}:#{port} established" }
123
+ @remote_forwarded_ports[[remote_port, remote_host]] = Remote.new(host, port)
124
+ else
125
+ error { "remote forwarding request failed" }
126
+ raise Net::SSH::Exception, "remote forwarding request failed"
127
+ end
128
+ end
129
+ end
130
+
131
+ # an alias, for token backwards compatibility with the 1.x API
132
+ alias :remote_to :remote
133
+
134
+ # Requests that a remote forwarded port be cancelled. The remote forwarded
135
+ # port on the remote host, bound to the given address on the remote host,
136
+ # will be terminated, but not immediately. This method returns immediately
137
+ # after queueing the request to be sent to the server. If for some reason
138
+ # the port cannot be cancelled, an exception will be raised (asynchronously).
139
+ #
140
+ # If you want to know when the connection has been cancelled, it will no
141
+ # longer be present in the #active_remotes list. If you want to block until
142
+ # the port is no longer active, you could do something like this:
143
+ #
144
+ # ssh.forward.cancel_remote(1234, "0.0.0.0")
145
+ # ssh.loop { ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
146
+ def cancel_remote(port, host="127.0.0.1")
147
+ session.send_global_request("cancel-tcpip-forward", :string, host, :long, port) do |success, response|
148
+ if success
149
+ @remote_forwarded_ports.delete([port, host])
150
+ else
151
+ raise Net::SSH::Exception, "could not cancel remote forward request on #{host}:#{port}"
152
+ end
153
+ end
154
+ end
155
+
156
+ # Returns all active forwarded remote ports. The returned value is an
157
+ # array of two-element tuples, where the first element is the port on the
158
+ # remote host and the second is the bind address.
159
+ def active_remotes
160
+ @remote_forwarded_ports.keys
161
+ end
162
+
163
+ # Enables SSH agent forwarding on the given channel. The forwarded agent
164
+ # will remain active even after the channel closes--the channel is only
165
+ # used as the transport for enabling the forwarded connection. You should
166
+ # never need to call this directly--it is called automatically the first
167
+ # time a session channel is opened, when the connection was created with
168
+ # :forward_agent set to true:
169
+ #
170
+ # Net::SSH.start("remote.host", "me", :forwrd_agent => true) do |ssh|
171
+ # ssh.open_channel do |ch|
172
+ # # agent will be automatically forwarded by this point
173
+ # end
174
+ # ssh.loop
175
+ # end
176
+ def agent(channel)
177
+ return if @agent_forwarded
178
+ @agent_forwarded = true
179
+
180
+ channel.send_channel_request("auth-agent-req@openssh.com") do |achannel, success|
181
+ if success
182
+ debug { "authentication agent forwarding is active" }
183
+ else
184
+ achannel.send_channel_request("auth-agent-req") do |a2channel, success2|
185
+ if success2
186
+ debug { "authentication agent forwarding is active" }
187
+ else
188
+ error { "could not establish forwarding of authentication agent" }
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ private
196
+
197
+ # Perform setup operations that are common to all forwarded channels.
198
+ # +client+ is a socket, +channel+ is the channel that was just created,
199
+ # and +type+ is an arbitrary string describing the type of the channel.
200
+ def prepare_client(client, channel, type)
201
+ client.extend(Net::SSH::BufferedIo)
202
+ client.extend(Net::SSH::ForwardedBufferedIo)
203
+ client.logger = logger
204
+
205
+ session.listen_to(client)
206
+ channel[:socket] = client
207
+
208
+ channel.on_data do |ch, data|
209
+ debug { "data:#{data.length} on #{type} forwarded channel" }
210
+ ch[:socket].enqueue(data)
211
+ end
212
+
213
+ # Handles server close on the sending side by Miklós Fazekas
214
+ channel.on_eof do |ch|
215
+ debug { "eof #{type} on #{type} forwarded channel" }
216
+ begin
217
+ ch[:socket].send_pending
218
+ ch[:socket].shutdown Socket::SHUT_WR
219
+ rescue IOError => e
220
+ if e.message =~ /closed/ then
221
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
222
+ else
223
+ raise
224
+ end
225
+ rescue Errno::EPIPE => e
226
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
227
+ rescue Errno::ENOTCONN => e
228
+ debug { "enotconn in on_eof => shallowing exception:#{e}" }
229
+ end
230
+ end
231
+
232
+ channel.on_close do |ch|
233
+ debug { "closing #{type} forwarded channel" }
234
+ ch[:socket].close if !client.closed?
235
+ session.stop_listening_to(ch[:socket])
236
+ end
237
+
238
+ channel.on_process do |ch|
239
+ if ch[:socket].closed?
240
+ ch.info { "#{type} forwarded connection closed" }
241
+ ch.close
242
+ elsif ch[:socket].available > 0
243
+ data = ch[:socket].read_available(8192)
244
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
245
+ ch.send_data(data)
246
+ end
247
+ end
248
+ end
249
+
250
+ # The callback used when a new "forwarded-tcpip" channel is requested
251
+ # by the server. This will open a new socket to the host/port specified
252
+ # when the forwarded connection was first requested.
253
+ def forwarded_tcpip(session, channel, packet)
254
+ connected_address = packet.read_string
255
+ connected_port = packet.read_long
256
+ originator_address = packet.read_string
257
+ originator_port = packet.read_long
258
+
259
+ remote = @remote_forwarded_ports[[connected_port, connected_address]]
260
+
261
+ if remote.nil?
262
+ raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
263
+ end
264
+
265
+ client = TCPSocket.new(remote.host, remote.port)
266
+ info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
267
+
268
+ prepare_client(client, channel, :remote)
269
+ rescue SocketError => err
270
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to remote host (#{remote.host}:#{remote.port}): #{err.message}")
271
+ end
272
+
273
+ # The callback used when an auth-agent channel is requested by the server.
274
+ def auth_agent_channel(session, channel, packet)
275
+ info { "opening auth-agent channel" }
276
+ channel[:invisible] = true
277
+
278
+ begin
279
+ agent = Authentication::Agent.connect(logger)
280
+ prepare_client(agent.socket, channel, :agent)
281
+ rescue Exception => e
282
+ error { "attempted to connect to agent but failed: #{e.class.name} (#{e.message})" }
283
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to authentication agent")
284
+ end
285
+ end
286
+ end
287
+
288
+ end; end; end