Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. # -*- test-case-name: twisted.conch.test.test_cftp -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Implementation module for the I{cftp} command.
  6. """
  7. import fcntl
  8. import fnmatch
  9. import getpass
  10. import glob
  11. import os
  12. import pwd
  13. import stat
  14. import struct
  15. import sys
  16. import tty
  17. from typing import List, Optional, TextIO, Union
  18. from twisted.conch.client import connect, default, options
  19. from twisted.conch.ssh import channel, common, connection, filetransfer
  20. from twisted.internet import defer, reactor, stdio, utils
  21. from twisted.protocols import basic
  22. from twisted.python import failure, log, usage
  23. from twisted.python.filepath import FilePath
  24. class ClientOptions(options.ConchOptions):
  25. synopsis = """Usage: cftp [options] [user@]host
  26. cftp [options] [user@]host[:dir[/]]
  27. cftp [options] [user@]host[:file [localfile]]
  28. """
  29. longdesc = (
  30. "cftp is a client for logging into a remote machine and "
  31. "executing commands to send and receive file information"
  32. )
  33. optParameters: List[List[Optional[Union[str, int]]]] = [
  34. ["buffersize", "B", 32768, "Size of the buffer to use for sending/receiving."],
  35. ["batchfile", "b", None, "File to read commands from, or '-' for stdin."],
  36. ["requests", "R", 5, "Number of requests to make before waiting for a reply."],
  37. ["subsystem", "s", "sftp", "Subsystem/server program to connect to."],
  38. ]
  39. compData = usage.Completions(
  40. descriptions={"buffersize": "Size of send/receive buffer (default: 32768)"},
  41. extraActions=[
  42. usage.CompleteUserAtHost(),
  43. usage.CompleteFiles(descr="local file"),
  44. ],
  45. )
  46. def parseArgs(self, host, localPath=None):
  47. self["remotePath"] = ""
  48. if ":" in host:
  49. host, self["remotePath"] = host.split(":", 1)
  50. self["remotePath"].rstrip("/")
  51. self["host"] = host
  52. self["localPath"] = localPath
  53. def run():
  54. args = sys.argv[1:]
  55. if "-l" in args: # cvs is an idiot
  56. i = args.index("-l")
  57. args = args[i : i + 2] + args
  58. del args[i + 2 : i + 4]
  59. options = ClientOptions()
  60. try:
  61. options.parseOptions(args)
  62. except usage.UsageError as u:
  63. print("ERROR: %s" % u)
  64. sys.exit(1)
  65. if options["log"]:
  66. realout = sys.stdout
  67. log.startLogging(sys.stderr)
  68. sys.stdout = realout
  69. else:
  70. log.discardLogs()
  71. doConnect(options)
  72. reactor.run()
  73. def handleError():
  74. global exitStatus
  75. exitStatus = 2
  76. try:
  77. reactor.stop()
  78. except BaseException:
  79. pass
  80. log.err(failure.Failure())
  81. raise
  82. def doConnect(options):
  83. if "@" in options["host"]:
  84. options["user"], options["host"] = options["host"].split("@", 1)
  85. host = options["host"]
  86. if not options["user"]:
  87. options["user"] = getpass.getuser()
  88. if not options["port"]:
  89. options["port"] = 22
  90. else:
  91. options["port"] = int(options["port"])
  92. host = options["host"]
  93. port = options["port"]
  94. conn = SSHConnection()
  95. conn.options = options
  96. vhk = default.verifyHostKey
  97. uao = default.SSHUserAuthClient(options["user"], options, conn)
  98. connect.connect(host, port, options, vhk, uao).addErrback(_ebExit)
  99. def _ebExit(f):
  100. if hasattr(f.value, "value"):
  101. s = f.value.value
  102. else:
  103. s = str(f)
  104. print(s)
  105. try:
  106. reactor.stop()
  107. except BaseException:
  108. pass
  109. def _ignore(*args):
  110. pass
  111. class FileWrapper:
  112. def __init__(self, f):
  113. self.f = f
  114. self.total = 0.0
  115. f.seek(0, 2) # seek to the end
  116. self.size = f.tell()
  117. def __getattr__(self, attr):
  118. return getattr(self.f, attr)
  119. class StdioClient(basic.LineReceiver):
  120. _pwd = pwd
  121. ps = "cftp> "
  122. delimiter = b"\n"
  123. reactor = reactor
  124. def __init__(self, client, f=None):
  125. self.client = client
  126. self.currentDirectory = ""
  127. self.file = f
  128. self.useProgressBar = (not f and 1) or 0
  129. def connectionMade(self):
  130. self.client.realPath("").addCallback(self._cbSetCurDir)
  131. def _cbSetCurDir(self, path):
  132. self.currentDirectory = path
  133. self._newLine()
  134. def _writeToTransport(self, msg):
  135. if isinstance(msg, str):
  136. msg = msg.encode("utf-8")
  137. return self.transport.write(msg)
  138. def lineReceived(self, line):
  139. if self.client.transport.localClosed:
  140. return
  141. if isinstance(line, bytes):
  142. line = line.decode("utf-8")
  143. log.msg("got line %s" % line)
  144. line = line.lstrip()
  145. if not line:
  146. self._newLine()
  147. return
  148. if self.file and line.startswith("-"):
  149. self.ignoreErrors = 1
  150. line = line[1:]
  151. else:
  152. self.ignoreErrors = 0
  153. d = self._dispatchCommand(line)
  154. if d is not None:
  155. d.addCallback(self._cbCommand)
  156. d.addErrback(self._ebCommand)
  157. def _dispatchCommand(self, line):
  158. if " " in line:
  159. command, rest = line.split(" ", 1)
  160. rest = rest.lstrip()
  161. else:
  162. command, rest = line, ""
  163. if command.startswith("!"): # command
  164. f = self.cmd_EXEC
  165. rest = (command[1:] + " " + rest).strip()
  166. else:
  167. command = command.upper()
  168. log.msg("looking up cmd %s" % command)
  169. f = getattr(self, "cmd_%s" % command, None)
  170. if f is not None:
  171. return defer.maybeDeferred(f, rest)
  172. else:
  173. errMsg = "No command called `%s'" % (command)
  174. self._ebCommand(failure.Failure(NotImplementedError(errMsg)))
  175. self._newLine()
  176. def _printFailure(self, f):
  177. log.msg(f)
  178. e = f.trap(NotImplementedError, filetransfer.SFTPError, OSError, IOError)
  179. if e == NotImplementedError:
  180. self._writeToTransport(self.cmd_HELP(""))
  181. elif e == filetransfer.SFTPError:
  182. errMsg = "remote error %i: %s\n" % (f.value.code, f.value.message)
  183. self._writeToTransport(errMsg)
  184. elif e in (OSError, IOError):
  185. errMsg = "local error %i: %s\n" % (f.value.errno, f.value.strerror)
  186. self._writeToTransport(errMsg)
  187. def _newLine(self):
  188. if self.client.transport.localClosed:
  189. return
  190. self._writeToTransport(self.ps)
  191. self.ignoreErrors = 0
  192. if self.file:
  193. l = self.file.readline()
  194. if not l:
  195. self.client.transport.loseConnection()
  196. else:
  197. self._writeToTransport(l)
  198. self.lineReceived(l.strip())
  199. def _cbCommand(self, result):
  200. if result is not None:
  201. if isinstance(result, str):
  202. result = result.encode("utf-8")
  203. self._writeToTransport(result)
  204. if not result.endswith(b"\n"):
  205. self._writeToTransport(b"\n")
  206. self._newLine()
  207. def _ebCommand(self, f):
  208. self._printFailure(f)
  209. if self.file and not self.ignoreErrors:
  210. self.client.transport.loseConnection()
  211. self._newLine()
  212. def cmd_CD(self, path):
  213. path, rest = self._getFilename(path)
  214. if not path.endswith("/"):
  215. path += "/"
  216. newPath = path and os.path.join(self.currentDirectory, path) or ""
  217. d = self.client.openDirectory(newPath)
  218. d.addCallback(self._cbCd)
  219. d.addErrback(self._ebCommand)
  220. return d
  221. def _cbCd(self, directory):
  222. directory.close()
  223. d = self.client.realPath(directory.name)
  224. d.addCallback(self._cbCurDir)
  225. return d
  226. def _cbCurDir(self, path):
  227. self.currentDirectory = path
  228. def cmd_CHGRP(self, rest):
  229. grp, rest = rest.split(None, 1)
  230. path, rest = self._getFilename(rest)
  231. grp = int(grp)
  232. d = self.client.getAttrs(path)
  233. d.addCallback(self._cbSetUsrGrp, path, grp=grp)
  234. return d
  235. def cmd_CHMOD(self, rest):
  236. mod, rest = rest.split(None, 1)
  237. path, rest = self._getFilename(rest)
  238. mod = int(mod, 8)
  239. d = self.client.setAttrs(path, {"permissions": mod})
  240. d.addCallback(_ignore)
  241. return d
  242. def cmd_CHOWN(self, rest):
  243. usr, rest = rest.split(None, 1)
  244. path, rest = self._getFilename(rest)
  245. usr = int(usr)
  246. d = self.client.getAttrs(path)
  247. d.addCallback(self._cbSetUsrGrp, path, usr=usr)
  248. return d
  249. def _cbSetUsrGrp(self, attrs, path, usr=None, grp=None):
  250. new = {}
  251. new["uid"] = (usr is not None) and usr or attrs["uid"]
  252. new["gid"] = (grp is not None) and grp or attrs["gid"]
  253. d = self.client.setAttrs(path, new)
  254. d.addCallback(_ignore)
  255. return d
  256. def cmd_GET(self, rest):
  257. remote, rest = self._getFilename(rest)
  258. if "*" in remote or "?" in remote: # wildcard
  259. if rest:
  260. local, rest = self._getFilename(rest)
  261. if not os.path.isdir(local):
  262. return "Wildcard get with non-directory target."
  263. else:
  264. local = b""
  265. d = self._remoteGlob(remote)
  266. d.addCallback(self._cbGetMultiple, local)
  267. return d
  268. if rest:
  269. local, rest = self._getFilename(rest)
  270. else:
  271. local = os.path.split(remote)[1]
  272. log.msg((remote, local))
  273. lf = open(local, "wb", 0)
  274. path = FilePath(self.currentDirectory).child(remote)
  275. d = self.client.openFile(path.path, filetransfer.FXF_READ, {})
  276. d.addCallback(self._cbGetOpenFile, lf)
  277. d.addErrback(self._ebCloseLf, lf)
  278. return d
  279. def _cbGetMultiple(self, files, local):
  280. # XXX this can be optimized for times w/o progress bar
  281. return self._cbGetMultipleNext(None, files, local)
  282. def _cbGetMultipleNext(self, res, files, local):
  283. if isinstance(res, failure.Failure):
  284. self._printFailure(res)
  285. elif res:
  286. self._writeToTransport(res)
  287. if not res.endswith("\n"):
  288. self._writeToTransport("\n")
  289. if not files:
  290. return
  291. f = files.pop(0)[0]
  292. lf = open(os.path.join(local, os.path.split(f)[1]), "wb", 0)
  293. path = FilePath(self.currentDirectory).child(f)
  294. d = self.client.openFile(path.path, filetransfer.FXF_READ, {})
  295. d.addCallback(self._cbGetOpenFile, lf)
  296. d.addErrback(self._ebCloseLf, lf)
  297. d.addBoth(self._cbGetMultipleNext, files, local)
  298. return d
  299. def _ebCloseLf(self, f, lf):
  300. lf.close()
  301. return f
  302. def _cbGetOpenFile(self, rf, lf):
  303. return rf.getAttrs().addCallback(self._cbGetFileSize, rf, lf)
  304. def _cbGetFileSize(self, attrs, rf, lf):
  305. if not stat.S_ISREG(attrs["permissions"]):
  306. rf.close()
  307. lf.close()
  308. return "Can't get non-regular file: %s" % rf.name
  309. rf.size = attrs["size"]
  310. bufferSize = self.client.transport.conn.options["buffersize"]
  311. numRequests = self.client.transport.conn.options["requests"]
  312. rf.total = 0.0
  313. dList = []
  314. chunks = []
  315. startTime = self.reactor.seconds()
  316. for i in range(numRequests):
  317. d = self._cbGetRead("", rf, lf, chunks, 0, bufferSize, startTime)
  318. dList.append(d)
  319. dl = defer.DeferredList(dList, fireOnOneErrback=1)
  320. dl.addCallback(self._cbGetDone, rf, lf)
  321. return dl
  322. def _getNextChunk(self, chunks):
  323. end = 0
  324. for chunk in chunks:
  325. if end == "eof":
  326. return # nothing more to get
  327. if end != chunk[0]:
  328. i = chunks.index(chunk)
  329. chunks.insert(i, (end, chunk[0]))
  330. return (end, chunk[0] - end)
  331. end = chunk[1]
  332. bufSize = int(self.client.transport.conn.options["buffersize"])
  333. chunks.append((end, end + bufSize))
  334. return (end, bufSize)
  335. def _cbGetRead(self, data, rf, lf, chunks, start, size, startTime):
  336. if data and isinstance(data, failure.Failure):
  337. log.msg("get read err: %s" % data)
  338. reason = data
  339. reason.trap(EOFError)
  340. i = chunks.index((start, start + size))
  341. del chunks[i]
  342. chunks.insert(i, (start, "eof"))
  343. elif data:
  344. log.msg("get read data: %i" % len(data))
  345. lf.seek(start)
  346. lf.write(data)
  347. if len(data) != size:
  348. log.msg("got less than we asked for: %i < %i" % (len(data), size))
  349. i = chunks.index((start, start + size))
  350. del chunks[i]
  351. chunks.insert(i, (start, start + len(data)))
  352. rf.total += len(data)
  353. if self.useProgressBar:
  354. self._printProgressBar(rf, startTime)
  355. chunk = self._getNextChunk(chunks)
  356. if not chunk:
  357. return
  358. else:
  359. start, length = chunk
  360. log.msg("asking for %i -> %i" % (start, start + length))
  361. d = rf.readChunk(start, length)
  362. d.addBoth(self._cbGetRead, rf, lf, chunks, start, length, startTime)
  363. return d
  364. def _cbGetDone(self, ignored, rf, lf):
  365. log.msg("get done")
  366. rf.close()
  367. lf.close()
  368. if self.useProgressBar:
  369. self._writeToTransport("\n")
  370. return f"Transferred {rf.name} to {lf.name}"
  371. def cmd_PUT(self, rest):
  372. """
  373. Do an upload request for a single local file or a globing expression.
  374. @param rest: Requested command line for the PUT command.
  375. @type rest: L{str}
  376. @return: A deferred which fires with L{None} when transfer is done.
  377. @rtype: L{defer.Deferred}
  378. """
  379. local, rest = self._getFilename(rest)
  380. # FIXME: https://twistedmatrix.com/trac/ticket/7241
  381. # Use a better check for globbing expression.
  382. if "*" in local or "?" in local:
  383. if rest:
  384. remote, rest = self._getFilename(rest)
  385. remote = os.path.join(self.currentDirectory, remote)
  386. else:
  387. remote = ""
  388. files = glob.glob(local)
  389. return self._putMultipleFiles(files, remote)
  390. else:
  391. if rest:
  392. remote, rest = self._getFilename(rest)
  393. else:
  394. remote = os.path.split(local)[1]
  395. return self._putSingleFile(local, remote)
  396. def _putSingleFile(self, local, remote):
  397. """
  398. Perform an upload for a single file.
  399. @param local: Path to local file.
  400. @type local: L{str}.
  401. @param remote: Remote path for the request relative to current working
  402. directory.
  403. @type remote: L{str}
  404. @return: A deferred which fires when transfer is done.
  405. """
  406. return self._cbPutMultipleNext(None, [local], remote, single=True)
  407. def _putMultipleFiles(self, files, remote):
  408. """
  409. Perform an upload for a list of local files.
  410. @param files: List of local files.
  411. @type files: C{list} of L{str}.
  412. @param remote: Remote path for the request relative to current working
  413. directory.
  414. @type remote: L{str}
  415. @return: A deferred which fires when transfer is done.
  416. """
  417. return self._cbPutMultipleNext(None, files, remote)
  418. def _cbPutMultipleNext(self, previousResult, files, remotePath, single=False):
  419. """
  420. Perform an upload for the next file in the list of local files.
  421. @param previousResult: Result form previous file form the list.
  422. @type previousResult: L{str}
  423. @param files: List of local files.
  424. @type files: C{list} of L{str}
  425. @param remotePath: Remote path for the request relative to current
  426. working directory.
  427. @type remotePath: L{str}
  428. @param single: A flag which signals if this is a transfer for a single
  429. file in which case we use the exact remote path
  430. @type single: L{bool}
  431. @return: A deferred which fires when transfer is done.
  432. """
  433. if isinstance(previousResult, failure.Failure):
  434. self._printFailure(previousResult)
  435. elif previousResult:
  436. if isinstance(previousResult, str):
  437. previousResult = previousResult.encode("utf-8")
  438. self._writeToTransport(previousResult)
  439. if not previousResult.endswith(b"\n"):
  440. self._writeToTransport(b"\n")
  441. currentFile = None
  442. while files and not currentFile:
  443. try:
  444. currentFile = files.pop(0)
  445. localStream = open(currentFile, "rb")
  446. except BaseException:
  447. self._printFailure(failure.Failure())
  448. currentFile = None
  449. # No more files to transfer.
  450. if not currentFile:
  451. return None
  452. if single:
  453. remote = remotePath
  454. else:
  455. name = os.path.split(currentFile)[1]
  456. remote = os.path.join(remotePath, name)
  457. log.msg((name, remote, remotePath))
  458. d = self._putRemoteFile(localStream, remote)
  459. d.addBoth(self._cbPutMultipleNext, files, remotePath)
  460. return d
  461. def _putRemoteFile(self, localStream, remotePath):
  462. """
  463. Do an upload request.
  464. @param localStream: Local stream from where data is read.
  465. @type localStream: File like object.
  466. @param remotePath: Remote path for the request relative to current working directory.
  467. @type remotePath: L{str}
  468. @return: A deferred which fires when transfer is done.
  469. """
  470. remote = os.path.join(self.currentDirectory, remotePath)
  471. flags = filetransfer.FXF_WRITE | filetransfer.FXF_CREAT | filetransfer.FXF_TRUNC
  472. d = self.client.openFile(remote, flags, {})
  473. d.addCallback(self._cbPutOpenFile, localStream)
  474. d.addErrback(self._ebCloseLf, localStream)
  475. return d
  476. def _cbPutOpenFile(self, rf, lf):
  477. numRequests = self.client.transport.conn.options["requests"]
  478. if self.useProgressBar:
  479. lf = FileWrapper(lf)
  480. dList = []
  481. chunks = []
  482. startTime = self.reactor.seconds()
  483. for i in range(numRequests):
  484. d = self._cbPutWrite(None, rf, lf, chunks, startTime)
  485. if d:
  486. dList.append(d)
  487. dl = defer.DeferredList(dList, fireOnOneErrback=1)
  488. dl.addCallback(self._cbPutDone, rf, lf)
  489. return dl
  490. def _cbPutWrite(self, ignored, rf, lf, chunks, startTime):
  491. chunk = self._getNextChunk(chunks)
  492. start, size = chunk
  493. lf.seek(start)
  494. data = lf.read(size)
  495. if self.useProgressBar:
  496. lf.total += len(data)
  497. self._printProgressBar(lf, startTime)
  498. if data:
  499. d = rf.writeChunk(start, data)
  500. d.addCallback(self._cbPutWrite, rf, lf, chunks, startTime)
  501. return d
  502. else:
  503. return
  504. def _cbPutDone(self, ignored, rf, lf):
  505. lf.close()
  506. rf.close()
  507. if self.useProgressBar:
  508. self._writeToTransport("\n")
  509. return f"Transferred {lf.name} to {rf.name}"
  510. def cmd_LCD(self, path):
  511. os.chdir(path)
  512. def cmd_LN(self, rest):
  513. linkpath, rest = self._getFilename(rest)
  514. targetpath, rest = self._getFilename(rest)
  515. linkpath, targetpath = map(
  516. lambda x: os.path.join(self.currentDirectory, x), (linkpath, targetpath)
  517. )
  518. return self.client.makeLink(linkpath, targetpath).addCallback(_ignore)
  519. def cmd_LS(self, rest):
  520. # possible lines:
  521. # ls current directory
  522. # ls name_of_file that file
  523. # ls name_of_directory that directory
  524. # ls some_glob_string current directory, globbed for that string
  525. options = []
  526. rest = rest.split()
  527. while rest and rest[0] and rest[0][0] == "-":
  528. opts = rest.pop(0)[1:]
  529. for o in opts:
  530. if o == "l":
  531. options.append("verbose")
  532. elif o == "a":
  533. options.append("all")
  534. rest = " ".join(rest)
  535. path, rest = self._getFilename(rest)
  536. if not path:
  537. fullPath = self.currentDirectory + "/"
  538. else:
  539. fullPath = os.path.join(self.currentDirectory, path)
  540. d = self._remoteGlob(fullPath)
  541. d.addCallback(self._cbDisplayFiles, options)
  542. return d
  543. def _cbDisplayFiles(self, files, options):
  544. files.sort()
  545. if "all" not in options:
  546. files = [f for f in files if not f[0].startswith(b".")]
  547. if "verbose" in options:
  548. lines = [f[1] for f in files]
  549. else:
  550. lines = [f[0] for f in files]
  551. if not lines:
  552. return None
  553. else:
  554. return b"\n".join(lines)
  555. def cmd_MKDIR(self, path):
  556. path, rest = self._getFilename(path)
  557. path = os.path.join(self.currentDirectory, path)
  558. return self.client.makeDirectory(path, {}).addCallback(_ignore)
  559. def cmd_RMDIR(self, path):
  560. path, rest = self._getFilename(path)
  561. path = os.path.join(self.currentDirectory, path)
  562. return self.client.removeDirectory(path).addCallback(_ignore)
  563. def cmd_LMKDIR(self, path):
  564. os.system("mkdir %s" % path)
  565. def cmd_RM(self, path):
  566. path, rest = self._getFilename(path)
  567. path = os.path.join(self.currentDirectory, path)
  568. return self.client.removeFile(path).addCallback(_ignore)
  569. def cmd_LLS(self, rest):
  570. os.system("ls %s" % rest)
  571. def cmd_RENAME(self, rest):
  572. oldpath, rest = self._getFilename(rest)
  573. newpath, rest = self._getFilename(rest)
  574. oldpath, newpath = map(
  575. lambda x: os.path.join(self.currentDirectory, x), (oldpath, newpath)
  576. )
  577. return self.client.renameFile(oldpath, newpath).addCallback(_ignore)
  578. def cmd_EXIT(self, ignored):
  579. self.client.transport.loseConnection()
  580. cmd_QUIT = cmd_EXIT
  581. def cmd_VERSION(self, ignored):
  582. version = "SFTP version %i" % self.client.version
  583. if isinstance(version, str):
  584. version = version.encode("utf-8")
  585. return version
  586. def cmd_HELP(self, ignored):
  587. return """Available commands:
  588. cd path Change remote directory to 'path'.
  589. chgrp gid path Change gid of 'path' to 'gid'.
  590. chmod mode path Change mode of 'path' to 'mode'.
  591. chown uid path Change uid of 'path' to 'uid'.
  592. exit Disconnect from the server.
  593. get remote-path [local-path] Get remote file.
  594. help Get a list of available commands.
  595. lcd path Change local directory to 'path'.
  596. lls [ls-options] [path] Display local directory listing.
  597. lmkdir path Create local directory.
  598. ln linkpath targetpath Symlink remote file.
  599. lpwd Print the local working directory.
  600. ls [-l] [path] Display remote directory listing.
  601. mkdir path Create remote directory.
  602. progress Toggle progress bar.
  603. put local-path [remote-path] Put local file.
  604. pwd Print the remote working directory.
  605. quit Disconnect from the server.
  606. rename oldpath newpath Rename remote file.
  607. rmdir path Remove remote directory.
  608. rm path Remove remote file.
  609. version Print the SFTP version.
  610. ? Synonym for 'help'.
  611. """
  612. def cmd_PWD(self, ignored):
  613. return self.currentDirectory
  614. def cmd_LPWD(self, ignored):
  615. return os.getcwd()
  616. def cmd_PROGRESS(self, ignored):
  617. self.useProgressBar = not self.useProgressBar
  618. return "%ssing progess bar." % (self.useProgressBar and "U" or "Not u")
  619. def cmd_EXEC(self, rest):
  620. """
  621. Run C{rest} using the user's shell (or /bin/sh if they do not have
  622. one).
  623. """
  624. shell = self._pwd.getpwnam(getpass.getuser())[6]
  625. if not shell:
  626. shell = "/bin/sh"
  627. if rest:
  628. cmds = ["-c", rest]
  629. return utils.getProcessOutput(shell, cmds, errortoo=1)
  630. else:
  631. os.system(shell)
  632. # accessory functions
  633. def _remoteGlob(self, fullPath):
  634. log.msg("looking up %s" % fullPath)
  635. head, tail = os.path.split(fullPath)
  636. if "*" in tail or "?" in tail:
  637. glob = 1
  638. else:
  639. glob = 0
  640. if tail and not glob: # could be file or directory
  641. # try directory first
  642. d = self.client.openDirectory(fullPath)
  643. d.addCallback(self._cbOpenList, "")
  644. d.addErrback(self._ebNotADirectory, head, tail)
  645. else:
  646. d = self.client.openDirectory(head)
  647. d.addCallback(self._cbOpenList, tail)
  648. return d
  649. def _cbOpenList(self, directory, glob):
  650. files = []
  651. d = directory.read()
  652. d.addBoth(self._cbReadFile, files, directory, glob)
  653. return d
  654. def _ebNotADirectory(self, reason, path, glob):
  655. d = self.client.openDirectory(path)
  656. d.addCallback(self._cbOpenList, glob)
  657. return d
  658. def _cbReadFile(self, files, matchedFiles, directory, glob):
  659. if not isinstance(files, failure.Failure):
  660. if glob:
  661. glob = glob.encode("utf-8")
  662. matchedFiles.extend([f for f in files if fnmatch.fnmatch(f[0], glob)])
  663. else:
  664. matchedFiles.extend(files)
  665. d = directory.read()
  666. d.addBoth(self._cbReadFile, matchedFiles, directory, glob)
  667. return d
  668. else:
  669. reason = files
  670. reason.trap(EOFError)
  671. directory.close()
  672. return matchedFiles
  673. def _abbrevSize(self, size):
  674. # from http://mail.python.org/pipermail/python-list/1999-December/018395.html
  675. _abbrevs = [
  676. (1 << 50, "PB"),
  677. (1 << 40, "TB"),
  678. (1 << 30, "GB"),
  679. (1 << 20, "MB"),
  680. (1 << 10, "kB"),
  681. (1, "B"),
  682. ]
  683. for factor, suffix in _abbrevs:
  684. if size > factor:
  685. break
  686. return "%.1f" % (size / factor) + suffix
  687. def _abbrevTime(self, t):
  688. if t > 3600: # 1 hour
  689. hours = int(t / 3600)
  690. t -= 3600 * hours
  691. mins = int(t / 60)
  692. t -= 60 * mins
  693. return "%i:%02i:%02i" % (hours, mins, t)
  694. else:
  695. mins = int(t / 60)
  696. t -= 60 * mins
  697. return "%02i:%02i" % (mins, t)
  698. def _printProgressBar(self, f, startTime):
  699. """
  700. Update a console progress bar on this L{StdioClient}'s transport, based
  701. on the difference between the start time of the operation and the
  702. current time according to the reactor, and appropriate to the size of
  703. the console window.
  704. @param f: a wrapper around the file which is being written or read
  705. @type f: L{FileWrapper}
  706. @param startTime: The time at which the operation being tracked began.
  707. @type startTime: L{float}
  708. """
  709. diff = self.reactor.seconds() - startTime
  710. total = f.total
  711. try:
  712. winSize = struct.unpack("4H", fcntl.ioctl(0, tty.TIOCGWINSZ, "12345679"))
  713. except OSError:
  714. winSize = [None, 80]
  715. if diff == 0.0:
  716. speed = 0.0
  717. else:
  718. speed = total / diff
  719. if speed:
  720. timeLeft = (f.size - total) / speed
  721. else:
  722. timeLeft = 0
  723. front = f.name
  724. if f.size:
  725. percentage = (total / f.size) * 100
  726. else:
  727. percentage = 100
  728. back = "%3i%% %s %sps %s " % (
  729. percentage,
  730. self._abbrevSize(total),
  731. self._abbrevSize(speed),
  732. self._abbrevTime(timeLeft),
  733. )
  734. spaces = (winSize[1] - (len(front) + len(back) + 1)) * " "
  735. command = f"\r{front}{spaces}{back}"
  736. self._writeToTransport(command)
  737. def _getFilename(self, line):
  738. """
  739. Parse line received as command line input and return first filename
  740. together with the remaining line.
  741. @param line: Arguments received from command line input.
  742. @type line: L{str}
  743. @return: Tupple with filename and rest. Return empty values when no path was not found.
  744. @rtype: C{tupple}
  745. """
  746. line = line.strip()
  747. if not line:
  748. return "", ""
  749. if line[0] in "'\"":
  750. ret = []
  751. line = list(line)
  752. try:
  753. for i in range(1, len(line)):
  754. c = line[i]
  755. if c == line[0]:
  756. return "".join(ret), "".join(line[i + 1 :]).lstrip()
  757. elif c == "\\": # quoted character
  758. del line[i]
  759. if line[i] not in "'\"\\":
  760. raise IndexError(f"bad quote: \\{line[i]}")
  761. ret.append(line[i])
  762. else:
  763. ret.append(line[i])
  764. except IndexError:
  765. raise IndexError("unterminated quote")
  766. ret = line.split(None, 1)
  767. if len(ret) == 1:
  768. return ret[0], ""
  769. else:
  770. return ret[0], ret[1]
  771. setattr(StdioClient, "cmd_?", StdioClient.cmd_HELP)
  772. class SSHConnection(connection.SSHConnection):
  773. def serviceStarted(self):
  774. self.openChannel(SSHSession())
  775. class SSHSession(channel.SSHChannel):
  776. name: bytes = b"session"
  777. stderr: TextIO = sys.stderr
  778. def channelOpen(self, foo):
  779. log.msg("session %s open" % self.id)
  780. if self.conn.options["subsystem"].startswith("/"):
  781. request = "exec"
  782. else:
  783. request = "subsystem"
  784. d = self.conn.sendRequest(
  785. self, request, common.NS(self.conn.options["subsystem"]), wantReply=1
  786. )
  787. d.addCallback(self._cbSubsystem)
  788. d.addErrback(_ebExit)
  789. def _cbSubsystem(self, result):
  790. self.client = filetransfer.FileTransferClient()
  791. self.client.makeConnection(self)
  792. self.dataReceived = self.client.dataReceived
  793. f = None
  794. if self.conn.options["batchfile"]:
  795. fn = self.conn.options["batchfile"]
  796. if fn != "-":
  797. f = open(fn)
  798. self.stdio = stdio.StandardIO(StdioClient(self.client, f))
  799. def extReceived(self, t: int, data: bytes) -> None:
  800. if t == connection.EXTENDED_DATA_STDERR:
  801. log.msg("got %s stderr data" % len(data))
  802. # RFC 4251
  803. # ========
  804. # Strings are also used to store text. In that case, US-ASCII is
  805. # used for internal names, and ISO-10646 UTF-8 for text that might
  806. # be displayed to the user. The terminating null character SHOULD
  807. # NOT normally be stored in the string. For example: the US-ASCII
  808. # string "testing" is represented as 00 00 00 07 t e s t i n g.
  809. # The UTF-8 mapping does not alter the encoding of US-ASCII
  810. # characters.
  811. #
  812. # RFC 4254
  813. # ========
  814. # Additionally, some channels can transfer several types of data. An
  815. # example of this is stderr data from interactive sessions. Such data
  816. # can be passed with SSH_MSG_CHANNEL_EXTENDED_DATA messages, where a
  817. # separate integer specifies the type of data. The available types and
  818. # their interpretation depend on the type of channel.
  819. #
  820. # byte SSH_MSG_CHANNEL_EXTENDED_DATA
  821. # uint32 recipient channel
  822. # uint32 data_type_code
  823. # string data
  824. #
  825. # Data sent with these messages consumes the same window as ordinary
  826. # data.
  827. #
  828. # Currently, only the following type is defined. Note that the value
  829. # for the 'data_type_code' is given in decimal format for readability,
  830. # but the values are actually uint32 values.
  831. #
  832. # Symbolic name data_type_code
  833. # ------------- --------------
  834. # SSH_EXTENDED_DATA_STDERR 1
  835. #
  836. # (end of RFC quotations)
  837. #
  838. # Here we decode the stderr bytes as UTF-8 and handle errors by
  839. # representing undecodeable bytes with a certain escape scheme.
  840. # There is no guarantee that the peer is sending UTF-8 encoded
  841. # bytes but if they are not it is complex to determine what
  842. # encoding they _are_ sending. The standard says nothing about
  843. # how these bytes should be decoded because the standard probably
  844. # doesn't think they should be decoded at all - just handle them
  845. # as bytes! However, our stderr is a text-mode file so we *must*
  846. # decode them to be able to write them out at all. And even if we
  847. # had a binary-mode file we would still /probably/ want to write
  848. # bytes in a *known* encoding to it.
  849. #
  850. # Perhaps in the future we can somehow inspect LANG or LC_* in the
  851. # remote execution environment (but I'm not sure how) and use that
  852. # as a hint about which encoding to use for decoding here.
  853. # Meanwhile, UTF-8 is the de facto universal interoperable
  854. # encoding so: use it.
  855. self.stderr.write(data.decode("utf-8", "backslashreplace"))
  856. self.stderr.flush()
  857. def eofReceived(self):
  858. log.msg("got eof")
  859. self.stdio.loseWriteConnection()
  860. def closeReceived(self):
  861. log.msg("remote side closed %s" % self)
  862. self.conn.sendClose(self)
  863. def closed(self):
  864. try:
  865. reactor.stop()
  866. except BaseException:
  867. pass
  868. def stopWriting(self):
  869. self.stdio.pauseProducing()
  870. def startWriting(self):
  871. self.stdio.resumeProducing()
  872. if __name__ == "__main__":
  873. run()