endpoints.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. # -*- test-case-name: twisted.conch.test.test_endpoints -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Endpoint implementations of various SSH interactions.
  6. """
  7. from __future__ import annotations
  8. __all__ = [
  9. "AuthenticationFailed",
  10. "SSHCommandAddress",
  11. "SSHCommandClientEndpoint",
  12. ]
  13. import signal
  14. from io import BytesIO
  15. from os.path import expanduser
  16. from struct import unpack
  17. from typing import IO, Any
  18. from zope.interface import Interface, implementer
  19. from twisted.conch.client.agent import SSHAgentClient
  20. from twisted.conch.client.default import _KNOWN_HOSTS
  21. from twisted.conch.client.knownhosts import ConsoleUI, KnownHostsFile
  22. from twisted.conch.ssh.channel import SSHChannel
  23. from twisted.conch.ssh.common import NS, getNS
  24. from twisted.conch.ssh.connection import SSHConnection
  25. from twisted.conch.ssh.keys import Key
  26. from twisted.conch.ssh.transport import SSHClientTransport
  27. from twisted.conch.ssh.userauth import SSHUserAuthClient
  28. from twisted.internet.defer import CancelledError, Deferred, succeed
  29. from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
  30. from twisted.internet.error import ConnectionDone, ProcessTerminated
  31. from twisted.internet.interfaces import IStreamClientEndpoint
  32. from twisted.internet.protocol import Factory
  33. from twisted.logger import Logger
  34. from twisted.python.compat import nativeString, networkString
  35. from twisted.python.failure import Failure
  36. from twisted.python.filepath import FilePath
  37. class AuthenticationFailed(Exception):
  38. """
  39. An SSH session could not be established because authentication was not
  40. successful.
  41. """
  42. # This should be public. See #6541.
  43. class _ISSHConnectionCreator(Interface):
  44. """
  45. An L{_ISSHConnectionCreator} knows how to create SSH connections somehow.
  46. """
  47. def secureConnection():
  48. """
  49. Return a new, connected, secured, but not yet authenticated instance of
  50. L{twisted.conch.ssh.transport.SSHServerTransport} or
  51. L{twisted.conch.ssh.transport.SSHClientTransport}.
  52. """
  53. def cleanupConnection(connection, immediate):
  54. """
  55. Perform cleanup necessary for a connection object previously returned
  56. from this creator's C{secureConnection} method.
  57. @param connection: An L{twisted.conch.ssh.transport.SSHServerTransport}
  58. or L{twisted.conch.ssh.transport.SSHClientTransport} returned by a
  59. previous call to C{secureConnection}. It is no longer needed by
  60. the caller of that method and may be closed or otherwise cleaned up
  61. as necessary.
  62. @param immediate: If C{True} don't wait for any network communication,
  63. just close the connection immediately and as aggressively as
  64. necessary.
  65. """
  66. class SSHCommandAddress:
  67. """
  68. An L{SSHCommandAddress} instance represents the address of an SSH server, a
  69. username which was used to authenticate with that server, and a command
  70. which was run there.
  71. @ivar server: See L{__init__}
  72. @ivar username: See L{__init__}
  73. @ivar command: See L{__init__}
  74. """
  75. def __init__(self, server, username, command):
  76. """
  77. @param server: The address of the SSH server on which the command is
  78. running.
  79. @type server: L{IAddress} provider
  80. @param username: An authentication username which was used to
  81. authenticate against the server at the given address.
  82. @type username: L{bytes}
  83. @param command: A command which was run in a session channel on the
  84. server at the given address.
  85. @type command: L{bytes}
  86. """
  87. self.server = server
  88. self.username = username
  89. self.command = command
  90. class _CommandChannel(SSHChannel):
  91. """
  92. A L{_CommandChannel} executes a command in a session channel and connects
  93. its input and output to an L{IProtocol} provider.
  94. @ivar _creator: See L{__init__}
  95. @ivar _command: See L{__init__}
  96. @ivar _protocolFactory: See L{__init__}
  97. @ivar _commandConnected: See L{__init__}
  98. @ivar _protocol: An L{IProtocol} provider created using C{_protocolFactory}
  99. which is hooked up to the running command's input and output streams.
  100. """
  101. name = b"session"
  102. _log = Logger()
  103. def __init__(self, creator, command, protocolFactory, commandConnected):
  104. """
  105. @param creator: The L{_ISSHConnectionCreator} provider which was used
  106. to get the connection which this channel exists on.
  107. @type creator: L{_ISSHConnectionCreator} provider
  108. @param command: The command to be executed.
  109. @type command: L{bytes}
  110. @param protocolFactory: A client factory to use to build a L{IProtocol}
  111. provider to use to associate with the running command.
  112. @param commandConnected: A L{Deferred} to use to signal that execution
  113. of the command has failed or that it has succeeded and the command
  114. is now running.
  115. @type commandConnected: L{Deferred}
  116. """
  117. SSHChannel.__init__(self)
  118. self._creator = creator
  119. self._command = command
  120. self._protocolFactory = protocolFactory
  121. self._commandConnected = commandConnected
  122. self._reason = None
  123. def openFailed(self, reason):
  124. """
  125. When the request to open a new channel to run this command in fails,
  126. fire the C{commandConnected} deferred with a failure indicating that.
  127. """
  128. self._commandConnected.errback(reason)
  129. def channelOpen(self, ignored):
  130. """
  131. When the request to open a new channel to run this command in succeeds,
  132. issue an C{"exec"} request to run the command.
  133. """
  134. command = self.conn.sendRequest(
  135. self, b"exec", NS(self._command), wantReply=True
  136. )
  137. command.addCallbacks(self._execSuccess, self._execFailure)
  138. def _execFailure(self, reason):
  139. """
  140. When the request to execute the command in this channel fails, fire the
  141. C{commandConnected} deferred with a failure indicating this.
  142. @param reason: The cause of the command execution failure.
  143. @type reason: L{Failure}
  144. """
  145. self._commandConnected.errback(reason)
  146. def _execSuccess(self, ignored):
  147. """
  148. When the request to execute the command in this channel succeeds, use
  149. C{protocolFactory} to build a protocol to handle the command's input
  150. and output and connect the protocol to a transport representing those
  151. streams.
  152. Also fire C{commandConnected} with the created protocol after it is
  153. connected to its transport.
  154. @param ignored: The (ignored) result of the execute request
  155. """
  156. self._protocol = self._protocolFactory.buildProtocol(
  157. SSHCommandAddress(
  158. self.conn.transport.transport.getPeer(),
  159. self.conn.transport.creator.username,
  160. self.conn.transport.creator.command,
  161. )
  162. )
  163. self._protocol.makeConnection(self)
  164. self._commandConnected.callback(self._protocol)
  165. def dataReceived(self, data):
  166. """
  167. When the command's stdout data arrives over the channel, deliver it to
  168. the protocol instance.
  169. @param data: The bytes from the command's stdout.
  170. @type data: L{bytes}
  171. """
  172. self._protocol.dataReceived(data)
  173. def request_exit_status(self, data):
  174. """
  175. When the server sends the command's exit status, record it for later
  176. delivery to the protocol.
  177. @param data: The network-order four byte representation of the exit
  178. status of the command.
  179. @type data: L{bytes}
  180. """
  181. (status,) = unpack(">L", data)
  182. if status != 0:
  183. self._reason = ProcessTerminated(status, None, None)
  184. def request_exit_signal(self, data):
  185. """
  186. When the server sends the command's exit status, record it for later
  187. delivery to the protocol.
  188. @param data: The network-order four byte representation of the exit
  189. signal of the command.
  190. @type data: L{bytes}
  191. """
  192. shortSignalName, data = getNS(data)
  193. coreDumped, data = bool(ord(data[0:1])), data[1:]
  194. errorMessage, data = getNS(data)
  195. languageTag, data = getNS(data)
  196. signalName = f"SIG{nativeString(shortSignalName)}"
  197. signalID = getattr(signal, signalName, -1)
  198. self._log.info(
  199. "Process exited with signal {shortSignalName!r};"
  200. " core dumped: {coreDumped};"
  201. " error message: {errorMessage};"
  202. " language: {languageTag!r}",
  203. shortSignalName=shortSignalName,
  204. coreDumped=coreDumped,
  205. errorMessage=errorMessage.decode("utf-8"),
  206. languageTag=languageTag,
  207. )
  208. self._reason = ProcessTerminated(None, signalID, None)
  209. def closed(self):
  210. """
  211. When the channel closes, deliver disconnection notification to the
  212. protocol.
  213. """
  214. self._creator.cleanupConnection(self.conn, False)
  215. if self._reason is None:
  216. reason = ConnectionDone("ssh channel closed")
  217. else:
  218. reason = self._reason
  219. self._protocol.connectionLost(Failure(reason))
  220. class _ConnectionReady(SSHConnection):
  221. """
  222. L{_ConnectionReady} is an L{SSHConnection} (an SSH service) which only
  223. propagates the I{serviceStarted} event to a L{Deferred} to be handled
  224. elsewhere.
  225. """
  226. def __init__(self, ready):
  227. """
  228. @param ready: A L{Deferred} which should be fired when
  229. I{serviceStarted} happens.
  230. """
  231. SSHConnection.__init__(self)
  232. self._ready = ready
  233. def serviceStarted(self):
  234. """
  235. When the SSH I{connection} I{service} this object represents is ready
  236. to be used, fire the C{connectionReady} L{Deferred} to publish that
  237. event to some other interested party.
  238. """
  239. self._ready.callback(self)
  240. del self._ready
  241. class _UserAuth(SSHUserAuthClient):
  242. """
  243. L{_UserAuth} implements the client part of SSH user authentication in the
  244. convenient way a user might expect if they are familiar with the
  245. interactive I{ssh} command line client.
  246. L{_UserAuth} supports key-based authentication, password-based
  247. authentication, and delegating authentication to an agent.
  248. """
  249. password = None
  250. keys = None
  251. agent = None
  252. def getPublicKey(self):
  253. """
  254. Retrieve the next public key object to offer to the server, possibly
  255. delegating to an authentication agent if there is one.
  256. @return: The public part of a key pair that could be used to
  257. authenticate with the server, or L{None} if there are no more
  258. public keys to try.
  259. @rtype: L{twisted.conch.ssh.keys.Key} or L{None}
  260. """
  261. if self.agent is not None:
  262. return self.agent.getPublicKey()
  263. if self.keys:
  264. self.key = self.keys.pop(0)
  265. else:
  266. self.key = None
  267. return self.key.public()
  268. def signData(self, publicKey, signData):
  269. """
  270. Extend the base signing behavior by using an SSH agent to sign the
  271. data, if one is available.
  272. @type publicKey: L{Key}
  273. @type signData: L{str}
  274. """
  275. if self.agent is not None:
  276. return self.agent.signData(publicKey.blob(), signData)
  277. else:
  278. return SSHUserAuthClient.signData(self, publicKey, signData)
  279. def getPrivateKey(self):
  280. """
  281. Get the private part of a key pair to use for authentication. The key
  282. corresponds to the public part most recently returned from
  283. C{getPublicKey}.
  284. @return: A L{Deferred} which fires with the private key.
  285. @rtype: L{Deferred}
  286. """
  287. return succeed(self.key)
  288. def getPassword(self):
  289. """
  290. Get the password to use for authentication.
  291. @return: A L{Deferred} which fires with the password, or L{None} if the
  292. password was not specified.
  293. """
  294. if self.password is None:
  295. return
  296. return succeed(self.password)
  297. def ssh_USERAUTH_SUCCESS(self, packet):
  298. """
  299. Handle user authentication success in the normal way, but also make a
  300. note of the state change on the L{_CommandTransport}.
  301. """
  302. self.transport._state = b"CHANNELLING"
  303. return SSHUserAuthClient.ssh_USERAUTH_SUCCESS(self, packet)
  304. def connectToAgent(self, endpoint):
  305. """
  306. Set up a connection to the authentication agent and trigger its
  307. initialization.
  308. @param endpoint: An endpoint which can be used to connect to the
  309. authentication agent.
  310. @type endpoint: L{IStreamClientEndpoint} provider
  311. @return: A L{Deferred} which fires when the agent connection is ready
  312. for use.
  313. """
  314. factory = Factory()
  315. factory.protocol = SSHAgentClient
  316. d = endpoint.connect(factory)
  317. def connected(agent):
  318. self.agent = agent
  319. return agent.getPublicKeys()
  320. d.addCallback(connected)
  321. return d
  322. def loseAgentConnection(self):
  323. """
  324. Disconnect the agent.
  325. """
  326. if self.agent is None:
  327. return
  328. self.agent.transport.loseConnection()
  329. class _CommandTransport(SSHClientTransport):
  330. """
  331. L{_CommandTransport} is an SSH client I{transport} which includes a host
  332. key verification step before it will proceed to secure the connection.
  333. L{_CommandTransport} also knows how to set up a connection to an
  334. authentication agent if it is told where it can connect to one.
  335. @ivar _userauth: The L{_UserAuth} instance which is in charge of the
  336. overall authentication process or L{None} if the SSH connection has not
  337. reach yet the C{user-auth} service.
  338. @type _userauth: L{_UserAuth}
  339. """
  340. # STARTING -> SECURING -> AUTHENTICATING -> CHANNELLING -> RUNNING
  341. _state = b"STARTING"
  342. _hostKeyFailure = None
  343. _userauth = None
  344. def __init__(self, creator):
  345. """
  346. @param creator: The L{_NewConnectionHelper} that created this
  347. connection.
  348. @type creator: L{_NewConnectionHelper}.
  349. """
  350. self.connectionReady = Deferred(lambda d: self.transport.abortConnection())
  351. # Clear the reference to that deferred to help the garbage collector
  352. # and to signal to other parts of this implementation (in particular
  353. # connectionLost) that it has already been fired and does not need to
  354. # be fired again.
  355. def readyFired(result):
  356. self.connectionReady = None
  357. return result
  358. self.connectionReady.addBoth(readyFired)
  359. self.creator = creator
  360. def verifyHostKey(self, hostKey, fingerprint):
  361. """
  362. Ask the L{KnownHostsFile} provider available on the factory which
  363. created this protocol this protocol to verify the given host key.
  364. @return: A L{Deferred} which fires with the result of
  365. L{KnownHostsFile.verifyHostKey}.
  366. """
  367. hostname = self.creator.hostname
  368. ip = networkString(self.transport.getPeer().host)
  369. self._state = b"SECURING"
  370. d = self.creator.knownHosts.verifyHostKey(
  371. self.creator.ui, hostname, ip, Key.fromString(hostKey)
  372. )
  373. d.addErrback(self._saveHostKeyFailure)
  374. return d
  375. def _saveHostKeyFailure(self, reason):
  376. """
  377. When host key verification fails, record the reason for the failure in
  378. order to fire a L{Deferred} with it later.
  379. @param reason: The cause of the host key verification failure.
  380. @type reason: L{Failure}
  381. @return: C{reason}
  382. @rtype: L{Failure}
  383. """
  384. self._hostKeyFailure = reason
  385. return reason
  386. def connectionSecure(self):
  387. """
  388. When the connection is secure, start the authentication process.
  389. """
  390. self._state = b"AUTHENTICATING"
  391. command = _ConnectionReady(self.connectionReady)
  392. self._userauth = _UserAuth(self.creator.username, command)
  393. self._userauth.password = self.creator.password
  394. if self.creator.keys:
  395. self._userauth.keys = list(self.creator.keys)
  396. if self.creator.agentEndpoint is not None:
  397. d = self._userauth.connectToAgent(self.creator.agentEndpoint)
  398. else:
  399. d = succeed(None)
  400. def maybeGotAgent(ignored):
  401. self.requestService(self._userauth)
  402. d.addBoth(maybeGotAgent)
  403. def connectionLost(self, reason):
  404. """
  405. When the underlying connection to the SSH server is lost, if there were
  406. any connection setup errors, propagate them. Also, clean up the
  407. connection to the ssh agent if one was created.
  408. """
  409. if self._userauth:
  410. self._userauth.loseAgentConnection()
  411. if self._state == b"RUNNING" or self.connectionReady is None:
  412. return
  413. if self._state == b"SECURING" and self._hostKeyFailure is not None:
  414. reason = self._hostKeyFailure
  415. elif self._state == b"AUTHENTICATING":
  416. reason = Failure(
  417. AuthenticationFailed("Connection lost while authenticating")
  418. )
  419. self.connectionReady.errback(reason)
  420. @implementer(IStreamClientEndpoint)
  421. class SSHCommandClientEndpoint:
  422. """
  423. L{SSHCommandClientEndpoint} exposes the command-executing functionality of
  424. SSH servers.
  425. L{SSHCommandClientEndpoint} can set up a new SSH connection, authenticate
  426. it in any one of a number of different ways (keys, passwords, agents),
  427. launch a command over that connection and then associate its input and
  428. output with a protocol.
  429. It can also re-use an existing, already-authenticated SSH connection
  430. (perhaps one which already has some SSH channels being used for other
  431. purposes). In this case it creates a new SSH channel to use to execute the
  432. command. Notably this means it supports multiplexing several different
  433. command invocations over a single SSH connection.
  434. """
  435. def __init__(self, creator, command):
  436. """
  437. @param creator: An L{_ISSHConnectionCreator} provider which will be
  438. used to set up the SSH connection which will be used to run a
  439. command.
  440. @type creator: L{_ISSHConnectionCreator} provider
  441. @param command: The command line to execute on the SSH server. This
  442. byte string is interpreted by a shell on the SSH server, so it may
  443. have a value like C{"ls /"}. Take care when trying to run a
  444. command like C{"/Volumes/My Stuff/a-program"} - spaces (and other
  445. special bytes) may require escaping.
  446. @type command: L{bytes}
  447. """
  448. self._creator = creator
  449. self._command = command
  450. @classmethod
  451. def newConnection(
  452. cls,
  453. reactor,
  454. command,
  455. username,
  456. hostname,
  457. port=None,
  458. keys=None,
  459. password=None,
  460. agentEndpoint=None,
  461. knownHosts=None,
  462. ui=None,
  463. ):
  464. """
  465. Create and return a new endpoint which will try to create a new
  466. connection to an SSH server and run a command over it. It will also
  467. close the connection if there are problems leading up to the command
  468. being executed, after the command finishes, or if the connection
  469. L{Deferred} is cancelled.
  470. @param reactor: The reactor to use to establish the connection.
  471. @type reactor: L{IReactorTCP} provider
  472. @param command: See L{__init__}'s C{command} argument.
  473. @param username: The username with which to authenticate to the SSH
  474. server.
  475. @type username: L{bytes}
  476. @param hostname: The hostname of the SSH server.
  477. @type hostname: L{bytes}
  478. @param port: The port number of the SSH server. By default, the
  479. standard SSH port number is used.
  480. @type port: L{int}
  481. @param keys: Private keys with which to authenticate to the SSH server,
  482. if key authentication is to be attempted (otherwise L{None}).
  483. @type keys: L{list} of L{Key}
  484. @param password: The password with which to authenticate to the SSH
  485. server, if password authentication is to be attempted (otherwise
  486. L{None}).
  487. @type password: L{bytes} or L{None}
  488. @param agentEndpoint: An L{IStreamClientEndpoint} provider which may be
  489. used to connect to an SSH agent, if one is to be used to help with
  490. authentication.
  491. @type agentEndpoint: L{IStreamClientEndpoint} provider
  492. @param knownHosts: The currently known host keys, used to check the
  493. host key presented by the server we actually connect to.
  494. @type knownHosts: L{KnownHostsFile}
  495. @param ui: An object for interacting with users to make decisions about
  496. whether to accept the server host keys. If L{None}, a L{ConsoleUI}
  497. connected to /dev/tty will be used; if /dev/tty is unavailable, an
  498. object which answers C{b"no"} to all prompts will be used.
  499. @type ui: L{None} or L{ConsoleUI}
  500. @return: A new instance of C{cls} (probably
  501. L{SSHCommandClientEndpoint}).
  502. """
  503. helper = _NewConnectionHelper(
  504. reactor,
  505. hostname,
  506. port,
  507. command,
  508. username,
  509. keys,
  510. password,
  511. agentEndpoint,
  512. knownHosts,
  513. ui,
  514. )
  515. return cls(helper, command)
  516. @classmethod
  517. def existingConnection(cls, connection, command):
  518. """
  519. Create and return a new endpoint which will try to open a new channel
  520. on an existing SSH connection and run a command over it. It will
  521. B{not} close the connection if there is a problem executing the command
  522. or after the command finishes.
  523. @param connection: An existing connection to an SSH server.
  524. @type connection: L{SSHConnection}
  525. @param command: See L{SSHCommandClientEndpoint.newConnection}'s
  526. C{command} parameter.
  527. @type command: L{bytes}
  528. @return: A new instance of C{cls} (probably
  529. L{SSHCommandClientEndpoint}).
  530. """
  531. helper = _ExistingConnectionHelper(connection)
  532. return cls(helper, command)
  533. def connect(self, protocolFactory):
  534. """
  535. Set up an SSH connection, use a channel from that connection to launch
  536. a command, and hook the stdin and stdout of that command up as a
  537. transport for a protocol created by the given factory.
  538. @param protocolFactory: A L{Factory} to use to create the protocol
  539. which will be connected to the stdin and stdout of the command on
  540. the SSH server.
  541. @return: A L{Deferred} which will fire with an error if the connection
  542. cannot be set up for any reason or with the protocol instance
  543. created by C{protocolFactory} once it has been connected to the
  544. command.
  545. """
  546. d = self._creator.secureConnection()
  547. d.addCallback(self._executeCommand, protocolFactory)
  548. return d
  549. def _executeCommand(self, connection, protocolFactory):
  550. """
  551. Given a secured SSH connection, try to execute a command in a new
  552. channel created on it and associate the result with a protocol from the
  553. given factory.
  554. @param connection: See L{SSHCommandClientEndpoint.existingConnection}'s
  555. C{connection} parameter.
  556. @param protocolFactory: See L{SSHCommandClientEndpoint.connect}'s
  557. C{protocolFactory} parameter.
  558. @return: See L{SSHCommandClientEndpoint.connect}'s return value.
  559. """
  560. commandConnected = Deferred()
  561. def disconnectOnFailure(passthrough):
  562. # Close the connection immediately in case of cancellation, since
  563. # that implies user wants it gone immediately (e.g. a timeout):
  564. immediate = passthrough.check(CancelledError)
  565. self._creator.cleanupConnection(connection, immediate)
  566. return passthrough
  567. commandConnected.addErrback(disconnectOnFailure)
  568. channel = _CommandChannel(
  569. self._creator, self._command, protocolFactory, commandConnected
  570. )
  571. connection.openChannel(channel)
  572. return commandConnected
  573. @implementer(_ISSHConnectionCreator)
  574. class _NewConnectionHelper:
  575. """
  576. L{_NewConnectionHelper} implements L{_ISSHConnectionCreator} by
  577. establishing a brand new SSH connection, securing it, and authenticating.
  578. """
  579. _KNOWN_HOSTS = _KNOWN_HOSTS
  580. port = 22
  581. def __init__(
  582. self,
  583. reactor: Any,
  584. hostname: str,
  585. port: int,
  586. command: str,
  587. username: str,
  588. keys: str,
  589. password: str,
  590. agentEndpoint: str,
  591. knownHosts: str | None,
  592. ui: ConsoleUI | None,
  593. tty: FilePath[bytes] | FilePath[str] = FilePath(b"/dev/tty"),
  594. ):
  595. """
  596. @param tty: The path of the tty device to use in case C{ui} is L{None}.
  597. @type tty: L{FilePath}
  598. @see: L{SSHCommandClientEndpoint.newConnection}
  599. """
  600. self.reactor = reactor
  601. self.hostname = hostname
  602. if port is not None:
  603. self.port = port
  604. self.command = command
  605. self.username = username
  606. self.keys = keys
  607. self.password = password
  608. self.agentEndpoint = agentEndpoint
  609. if knownHosts is None:
  610. knownHosts = self._knownHosts()
  611. self.knownHosts = knownHosts
  612. if ui is None:
  613. ui = ConsoleUI(self._opener)
  614. self.ui = ui
  615. self.tty: FilePath[bytes] | FilePath[str] = tty
  616. def _opener(self) -> IO[bytes]:
  617. """
  618. Open the tty if possible, otherwise give back a file-like object from
  619. which C{b"no"} can be read.
  620. For use as the opener argument to L{ConsoleUI}.
  621. """
  622. try:
  623. return self.tty.open("r+")
  624. except BaseException:
  625. # Give back a file-like object from which can be read a byte string
  626. # that KnownHostsFile recognizes as rejecting some option (b"no").
  627. return BytesIO(b"no")
  628. @classmethod
  629. def _knownHosts(cls):
  630. """
  631. @return: A L{KnownHostsFile} instance pointed at the user's personal
  632. I{known hosts} file.
  633. @rtype: L{KnownHostsFile}
  634. """
  635. return KnownHostsFile.fromPath(FilePath(expanduser(cls._KNOWN_HOSTS)))
  636. def secureConnection(self):
  637. """
  638. Create and return a new SSH connection which has been secured and on
  639. which authentication has already happened.
  640. @return: A L{Deferred} which fires with the ready-to-use connection or
  641. with a failure if something prevents the connection from being
  642. setup, secured, or authenticated.
  643. """
  644. protocol = _CommandTransport(self)
  645. ready = protocol.connectionReady
  646. sshClient = TCP4ClientEndpoint(
  647. self.reactor, nativeString(self.hostname), self.port
  648. )
  649. d = connectProtocol(sshClient, protocol)
  650. d.addCallback(lambda ignored: ready)
  651. return d
  652. def cleanupConnection(self, connection, immediate):
  653. """
  654. Clean up the connection by closing it. The command running on the
  655. endpoint has ended so the connection is no longer needed.
  656. @param connection: The L{SSHConnection} to close.
  657. @type connection: L{SSHConnection}
  658. @param immediate: Whether to close connection immediately.
  659. @type immediate: L{bool}.
  660. """
  661. if immediate:
  662. # We're assuming the underlying connection is an ITCPTransport,
  663. # which is what the current implementation is restricted to:
  664. connection.transport.transport.abortConnection()
  665. else:
  666. connection.transport.loseConnection()
  667. @implementer(_ISSHConnectionCreator)
  668. class _ExistingConnectionHelper:
  669. """
  670. L{_ExistingConnectionHelper} implements L{_ISSHConnectionCreator} by
  671. handing out an existing SSH connection which is supplied to its
  672. initializer.
  673. """
  674. def __init__(self, connection):
  675. """
  676. @param connection: See L{SSHCommandClientEndpoint.existingConnection}'s
  677. C{connection} parameter.
  678. """
  679. self.connection = connection
  680. def secureConnection(self):
  681. """
  682. @return: A L{Deferred} that fires synchronously with the
  683. already-established connection object.
  684. """
  685. return succeed(self.connection)
  686. def cleanupConnection(self, connection, immediate):
  687. """
  688. Do not do any cleanup on the connection. Leave that responsibility to
  689. whatever code created it in the first place.
  690. @param connection: The L{SSHConnection} which will not be modified in
  691. any way.
  692. @type connection: L{SSHConnection}
  693. @param immediate: An argument which will be ignored.
  694. @type immediate: L{bool}.
  695. """