PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/remctl-3.2/perl/Remctl.pm.in

#
Autoconf | 376 lines | 269 code | 94 blank | 13 comment | 41 complexity | a816b443ac03953c48cb993d1af482c3 MD5 | raw file
Possible License(s): ISC
  1. # Net::Remctl -- Perl bindings for the remctl client library.
  2. #
  3. # This is the Perl boostrap file for the Net::Remctl module, nearly all of
  4. # which is implemented in XS. For the actual source, see Remctl.xs. This
  5. # file contains the bootstrap and export code and the documentation.
  6. #
  7. # Written by Russ Allbery <rra@stanford.edu>
  8. # Copyright 2007, 2008, 2011, 2012
  9. # The Board of Trustees of the Leland Stanford Junior University
  10. #
  11. # See LICENSE for licensing terms.
  12. package Net::Remctl;
  13. use 5.006;
  14. use strict;
  15. use warnings;
  16. our $VERSION = '@PACKAGE_VERSION@';
  17. require Exporter;
  18. require DynaLoader;
  19. our @ISA = qw(Exporter DynaLoader);
  20. our @EXPORT = qw(remctl);
  21. bootstrap Net::Remctl;
  22. 1;
  23. =for stopwords
  24. remctl libremctl GSS-API HOSTNAME remctld IPv4 IPv6 NUL CNAME DNS Allbery
  25. MERCHANTABILITY sublicense NONINFRINGEMENT libdefaults CCACHE IP DNS-based
  26. unencoded lookups canonicalization
  27. =head1 NAME
  28. Net::Remctl - Perl bindings for remctl (Kerberos remote command execution)
  29. =head1 SYNOPSIS
  30. # Simplified form.
  31. use Net::Remctl;
  32. my $result = remctl("hostname", undef, undef, "test", "echo", "Hi");
  33. if ($result->error) {
  34. die "test echo failed with error ", $result->error, "\n";
  35. } else {
  36. warn $result->stderr;
  37. print $result->stdout;
  38. exit $result->status;
  39. }
  40. # Full interface.
  41. use Net::Remctl ();
  42. my $remctl = Net::Remctl->new;
  43. $remctl->open("hostname")
  44. or die "Cannot connect to hostname: ", $remctl->error, "\n";
  45. $remctl->command("test", "echo", "Hi there")
  46. or die "Cannot send command: ", $remctl->error, "\n";
  47. my $output;
  48. do {
  49. $output = $remctl->output;
  50. if ($output->type eq 'output') {
  51. if ($output->stream == 1) {
  52. print $output->data;
  53. } elsif ($output->stream == 2) {
  54. warn $output->data;
  55. }
  56. } elsif ($output->type eq 'error') {
  57. warn $output->error, "\n";
  58. } elsif ($output->type eq 'status') {
  59. exit $output->status;
  60. } else {
  61. die "Unknown output token from library: ", $output->type, "\n";
  62. }
  63. } while ($output->type eq 'output');
  64. $remctl->noop or die "Cannot send NOOP: ", $remctl->error, "\n";
  65. =head1 DESCRIPTION
  66. Net::Remctl provides Perl bindings to the libremctl client library. remctl
  67. is a protocol for remote command execution using GSS-API authentication.
  68. The specific allowable commands must be listed in a configuration file on
  69. the remote system and the remote system can map the remctl command names to
  70. any local command without exposing that mapping to the client. This module
  71. implements a remctl client.
  72. =head2 Simplified Interface
  73. If you want to run a single command on a remote system and get back the
  74. output and exit status, you can use the exported remctl() function:
  75. =over 4
  76. =item remctl(HOSTNAME, PORT, PRINCIPAL, COMMAND, [ARGS, ...])
  77. Runs a command on the remote system and returns a Net::Remctl::Result
  78. object (see below). HOSTNAME is the remote host to contact. PORT is the
  79. port of the remote B<remctld> server and may be 0 to tell the library to
  80. use the default (first try 4373, the registered remctl port, and fall back
  81. to the legacy 4444 port if that fails). PRINCIPAL is the principal of the
  82. server to use for authentication; pass in the empty string to use the
  83. default of host/HOSTNAME, with the realm determined by domain-realm
  84. mapping. The remaining arguments are the remctl command and arguments
  85. passed to the remote server.
  86. As far as the module is concerned, undef may be passed as PORT and
  87. PRINCIPAL and is the same as 0 and the empty string respectively.
  88. However, Perl will warn about passing undef explicitly as a function
  89. argument.
  90. The return value is a Net::Remctl::Result object which supports the
  91. following methods:
  92. =over 4
  93. =item error()
  94. Returns the error message from either the remote host or from the local
  95. client library (if, for instance, contacting the remote host failed).
  96. Returns undef if there was no error. Checking whether error() returns undef
  97. is the supported way of determining whether the remctl() call succeeded.
  98. =item stdout()
  99. Returns the command's standard output or undef if there was none.
  100. =item stderr()
  101. Returns the command's standard error or undef if there was none.
  102. =item status()
  103. Returns the command's exit status.
  104. =back
  105. Each call to remctl() will open a new connection to the remote host and
  106. close it after retrieving the results of the command. To maintain a
  107. persistent connection, use the full interface described below.
  108. =back
  109. =head2 Full Interface
  110. The full remctl library interface requires that the user do more
  111. bookkeeping, but it provides more flexibility and allows one to issue
  112. multiple commands on the same persistent connection (provided that the
  113. remote server supports protocol version two; if not, the library will
  114. transparently fall back to opening a new connection for each command).
  115. To use the full interface, first create a Net::Remctl object with new() and
  116. then connect() to a remote server. Then, issue a command() and call
  117. output() to retrieve output tokens (as Net::Remctl::Output objects) until a
  118. status token is received. Destroying the Net::Remctl object will close the
  119. connection.
  120. The supported object methods are:
  121. =over 4
  122. =item new()
  123. Create a new Net::Remctl object. This doesn't attempt to connect to a host
  124. and hence will only fail (by throwing an exception) if the library cannot
  125. allocate memory.
  126. =item error()
  127. Retrieves the error message from the last failing operation and returns it
  128. as a string.
  129. =item set_ccache(CCACHE)
  130. Sets the GSS-API credential cache for outgoing connections to CCACHE,
  131. which is normally the path to a Kerberos ticket cache but may have other
  132. valid forms depending on the underlying Kerberos implementation in use by
  133. GSS-API. This method must be called prior to calling open() and will
  134. affect all subsequent open() calls on at least the same object. Returns
  135. true on success and false on failure.
  136. For current GSS-API implementations, this will affect not only all
  137. subsequent open() calls for the same object, but all subsequent remctl
  138. connections of any kind from the same process, and even other GSS-API
  139. connections from the same process unrelated to remctl. This is due to a
  140. limitation in the GSS-API that makes this setting a global setting for the
  141. process or thread.
  142. Not all GSS-API implementations support setting the credential cache. If
  143. this is not supported, false will be returned.
  144. =item set_source_ip(SOURCE)
  145. Sets the source IP for outgoing connections to SOURCE, which can be either
  146. an IPv4 or an IPv6 address (if IPv6 is supported). It must be an IP
  147. address, not a host name. This method must be called prior to calling
  148. open() and will affect all subsequent open() calls on the same object.
  149. Returns true on success and false on failure.
  150. =item set_timeout(TIMEOUT)
  151. Sets the timeout for connections and commands to TIMEOUT, which should be
  152. an integer number of seconds. TIMEOUT may be 0 to clear a timeout that
  153. was previously set. All subsequent operations on this Net::Remctl object
  154. will be subject to this timeout, including open() if called prior to
  155. calling open(). Returns true on success and false on failure. Failure is
  156. only possible if TIMEOUT is malformed.
  157. The timeout is a timeout on network activity from the server, not on a
  158. complete operation. So, for example, a timeout of ten seconds just
  159. requires that the server send some data every ten seconds. If the server
  160. sends only tiny amounts of data at a time, the complete operation could
  161. take much longer than ten seconds without triggering the timeout.
  162. =item open(HOSTNAME[, PORT[, PRINCIPAL]])
  163. Connect to HOSTNAME on port PORT using PRINCIPAL as the remote server's
  164. principal for authentication. If PORT is omitted or 0, use the default
  165. (first try 4373, the registered remctl port, and fall back to the legacy
  166. 4444 port if that fails). If PRINCIPAL is omitted or the empty string,
  167. use the default of host/HOSTNAME, with the realm determined by
  168. domain-realm mapping. Returns true on success, false on failure. On
  169. failure, call error() to get the failure message.
  170. As far as the module is concerned, undef may be passed as PORT and
  171. PRINCIPAL and is the same as 0 and the empty string respectively.
  172. However, Perl will warn about passing undef explicitly as a function
  173. argument.
  174. =item command(COMMAND[, ARGS, ...])
  175. Send the command and arguments to the remote host. The command and the
  176. arguments may, under the remctl protocol, contain any character, but be
  177. aware that most remctl servers will reject commands or arguments containing
  178. ASCII 0 (NUL), so currently this cannot be used for upload of arbitrary
  179. unencoded binary data. Returns true on success (meaning success in sending
  180. the command, and implying nothing about the result of the command), false on
  181. failure. On failure, call error() to get the failure message.
  182. =item output()
  183. Returns the next output token from the remote host. The token is returned
  184. as a Net::Remctl::Output object, which supports the following methods:
  185. =over 4
  186. =item type()
  187. Returns the type of the output token, which will be one of C<output>,
  188. C<error>, C<status>, or C<done>. A command will result in either one
  189. C<error> token or zero or more C<output> tokens followed by a C<status>
  190. token. After either a C<error> or C<status> token is seen, another command
  191. can be issued. If the caller tries to retrieve another output token when it
  192. has already consumed all of them for that command, the library will return a
  193. C<done> token.
  194. =item data()
  195. Returns the contents of the token. This method only makes sense for
  196. C<output> and C<error> tokens; otherwise, it will return undef. Note that
  197. the returned value may contain any character, including ASCII 0 (NUL).
  198. =item length()
  199. Returns the length of the data in the token. As with data(), this method
  200. only makes sense for the C<output> and C<error> tokens. It will return 0 if
  201. there is no data or if the data is zero-length.
  202. =item stream()
  203. For an C<output> token, returns the stream with which the data is
  204. associated. Currently, only two stream values will be used: 1, meaning
  205. standard output; and 2, meaning standard error. The value is undefined for
  206. all other output token types.
  207. =item status()
  208. For a C<status> token, returns the exit status of the remote command. The
  209. value is undefined for all other token types.
  210. =item error()
  211. For an C<error> token, returns the remctl error code for the protocol
  212. error. The text message will be returned by data(). The value is undefined
  213. for all other token types.
  214. =back
  215. =item noop()
  216. Send a NOOP message to the server and read the reply. This is primarily
  217. used to keep a connection to a remctl server alive, such as through a
  218. firewall with a session timeout, while waiting to issue further commands.
  219. Returns true on success, false on failure. On failure, call error() to
  220. get the failure message.
  221. The NOOP message requires protocol version 3 support in the server, so the
  222. caller should be prepared for this function to fail, indicating that the
  223. connection could not be kept alive and possibly that it was closed by the
  224. server. In this case, the client will need to explicitly reopen the
  225. connection with open().
  226. =back
  227. Note that, due to internal implementation details in the library, the
  228. Net::Remctl::Output object returned by output() will be invalidated by the
  229. next call to command() or output() or by destroying the producing
  230. Net::Remctl object. Therefore, any data in the output token should be
  231. processed and stored if needed before making any further Net::Remctl method
  232. calls on the same object.
  233. =head1 CAVEATS
  234. If the I<principal> argument to remctl() or remctl_open() is NULL, most
  235. GSS-API libraries will canonicalize the I<host> using DNS before deriving
  236. the principal name from it. This means that when connecting to a remctl
  237. server via a CNAME, remctl() and remctl_open() will normally authenticate
  238. using a principal based on the canonical name of the host instead of the
  239. specified I<host> parameter. This behavior may cause problems if two
  240. consecutive DNS lookups of I<host> may return two different results, such
  241. as with some DNS-based load-balancing systems.
  242. The canonicalization behavior is controlled by the GSS-API library; with
  243. the MIT Kerberos GSS-API library, canonicalization can be disabled by
  244. setting C<rdns> to false in the [libdefaults] section of F<krb5.conf>. It
  245. can also be disabled by passing an explicit Kerberos principal name via
  246. the I<principal> argument, which will then be used without changes. If
  247. canonicalization is desired, the caller may wish to canonicalize I<host>
  248. before calling remctl() or remctl_open() to avoid problems with multiple
  249. DNS calls returning different results.
  250. The default behavior, when the port is not specified, of trying 4373 and
  251. falling back to 4444 will be removed in a future version of this module in
  252. favor of using the C<remctl> service in F</etc/services> if set and then
  253. falling back on only 4373. 4444 was the poorly-chosen original remctl
  254. port and should be phased out.
  255. =head1 NOTES
  256. The remctl port number, 4373, was derived by tracing the diagonals of a
  257. QWERTY keyboard up from the letters C<remc> to the number row.
  258. =head1 SEE ALSO
  259. remctl(1), remctld(8)
  260. The current version of this module is available from its web page at
  261. L<http://www.eyrie.org/~eagle/software/remctl/>.
  262. =head1 AUTHOR
  263. Russ Allbery <rra@stanford.edu>
  264. =head1 COPYRIGHT AND LICENSE
  265. Copyright 2007, 2008, 2011 The Board of Trustees of the Leland Stanford
  266. Junior University. All rights reserved.
  267. Permission is hereby granted, free of charge, to any person obtaining a
  268. copy of this software and associated documentation files (the "Software"),
  269. to deal in the Software without restriction, including without limitation
  270. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  271. and/or sell copies of the Software, and to permit persons to whom the
  272. Software is furnished to do so, subject to the following conditions:
  273. The above copyright notice and this permission notice shall be included in
  274. all copies or substantial portions of the Software.
  275. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  276. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  277. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  278. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  279. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  280. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  281. DEALINGS IN THE SOFTWARE.
  282. =cut