interfaces.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. This module contains interfaces defined for the L{twisted.conch} package.
  5. """
  6. from zope.interface import Interface, Attribute
  7. class IConchUser(Interface):
  8. """
  9. A user who has been authenticated to Cred through Conch. This is
  10. the interface between the SSH connection and the user.
  11. """
  12. conn = Attribute('The SSHConnection object for this user.')
  13. def lookupChannel(channelType, windowSize, maxPacket, data):
  14. """
  15. The other side requested a channel of some sort.
  16. C{channelType} is the type of channel being requested,
  17. as an ssh connection protocol channel type.
  18. C{data} is any other packet data (often nothing).
  19. We return a subclass of L{SSHChannel<ssh.channel.SSHChannel>}. If
  20. the channel type is unknown, we return C{None}.
  21. For other failures, we raise an exception. If a
  22. L{ConchError<error.ConchError>} is raised, the C{.value} will
  23. be the message, and the C{.data} will be the error code.
  24. @param channelType: The requested channel type
  25. @type channelType: L{bytes}
  26. @param windowSize: The initial size of the remote window
  27. @type windowSize: L{int}
  28. @param maxPacket: The largest packet we should send
  29. @type maxPacket: L{int}
  30. @param data: Additional request data
  31. @type data: L{bytes}
  32. @rtype: a subclass of L{SSHChannel} or L{None}
  33. """
  34. def lookupSubsystem(subsystem, data):
  35. """
  36. The other side requested a subsystem.
  37. We return a L{Protocol} implementing the requested subsystem.
  38. If the subsystem is not available, we return C{None}.
  39. @param subsystem: The name of the subsystem being requested
  40. @type subsystem: L{bytes}
  41. @param data: Additional request data (often nothing)
  42. @type data: L{bytes}
  43. @rtype: L{Protocol} or L{None}
  44. """
  45. def gotGlobalRequest(requestType, data):
  46. """
  47. A global request was sent from the other side.
  48. We return a true value on success or a false value on failure.
  49. If we indicate success by returning a tuple, its second item
  50. will be sent to the other side as additional response data.
  51. @param requestType: The type of the request
  52. @type requestType: L{bytes}
  53. @param data: Additional request data
  54. @type data: L{bytes}
  55. @rtype: boolean or L{tuple}
  56. """
  57. class ISession(Interface):
  58. def getPty(term, windowSize, modes):
  59. """
  60. Get a pseudo-terminal for use by a shell or command.
  61. If a pseudo-terminal is not available, or the request otherwise
  62. fails, raise an exception.
  63. """
  64. def openShell(proto):
  65. """
  66. Open a shell and connect it to proto.
  67. @param proto: a L{ProcessProtocol} instance.
  68. """
  69. def execCommand(proto, command):
  70. """
  71. Execute a command.
  72. @param proto: a L{ProcessProtocol} instance.
  73. """
  74. def windowChanged(newWindowSize):
  75. """
  76. Called when the size of the remote screen has changed.
  77. """
  78. def eofReceived():
  79. """
  80. Called when the other side has indicated no more data will be sent.
  81. """
  82. def closed():
  83. """
  84. Called when the session is closed.
  85. """
  86. class ISFTPServer(Interface):
  87. """
  88. SFTP subsystem for server-side communication.
  89. Each method should check to verify that the user has permission for
  90. their actions.
  91. """
  92. avatar = Attribute(
  93. """
  94. The avatar returned by the Realm that we are authenticated with,
  95. and represents the logged-in user.
  96. """)
  97. def gotVersion(otherVersion, extData):
  98. """
  99. Called when the client sends their version info.
  100. otherVersion is an integer representing the version of the SFTP
  101. protocol they are claiming.
  102. extData is a dictionary of extended_name : extended_data items.
  103. These items are sent by the client to indicate additional features.
  104. This method should return a dictionary of extended_name : extended_data
  105. items. These items are the additional features (if any) supported
  106. by the server.
  107. """
  108. return {}
  109. def openFile(filename, flags, attrs):
  110. """
  111. Called when the clients asks to open a file.
  112. @param filename: a string representing the file to open.
  113. @param flags: an integer of the flags to open the file with, ORed
  114. together. The flags and their values are listed at the bottom of
  115. L{twisted.conch.ssh.filetransfer} as FXF_*.
  116. @param attrs: a list of attributes to open the file with. It is a
  117. dictionary, consisting of 0 or more keys. The possible keys are::
  118. size: the size of the file in bytes
  119. uid: the user ID of the file as an integer
  120. gid: the group ID of the file as an integer
  121. permissions: the permissions of the file with as an integer.
  122. the bit representation of this field is defined by POSIX.
  123. atime: the access time of the file as seconds since the epoch.
  124. mtime: the modification time of the file as seconds since the epoch.
  125. ext_*: extended attributes. The server is not required to
  126. understand this, but it may.
  127. NOTE: there is no way to indicate text or binary files. it is up
  128. to the SFTP client to deal with this.
  129. This method returns an object that meets the ISFTPFile interface.
  130. Alternatively, it can return a L{Deferred} that will be called back
  131. with the object.
  132. """
  133. def removeFile(filename):
  134. """
  135. Remove the given file.
  136. This method returns when the remove succeeds, or a Deferred that is
  137. called back when it succeeds.
  138. @param filename: the name of the file as a string.
  139. """
  140. def renameFile(oldpath, newpath):
  141. """
  142. Rename the given file.
  143. This method returns when the rename succeeds, or a L{Deferred} that is
  144. called back when it succeeds. If the rename fails, C{renameFile} will
  145. raise an implementation-dependent exception.
  146. @param oldpath: the current location of the file.
  147. @param newpath: the new file name.
  148. """
  149. def makeDirectory(path, attrs):
  150. """
  151. Make a directory.
  152. This method returns when the directory is created, or a Deferred that
  153. is called back when it is created.
  154. @param path: the name of the directory to create as a string.
  155. @param attrs: a dictionary of attributes to create the directory with.
  156. Its meaning is the same as the attrs in the L{openFile} method.
  157. """
  158. def removeDirectory(path):
  159. """
  160. Remove a directory (non-recursively)
  161. It is an error to remove a directory that has files or directories in
  162. it.
  163. This method returns when the directory is removed, or a Deferred that
  164. is called back when it is removed.
  165. @param path: the directory to remove.
  166. """
  167. def openDirectory(path):
  168. """
  169. Open a directory for scanning.
  170. This method returns an iterable object that has a close() method,
  171. or a Deferred that is called back with same.
  172. The close() method is called when the client is finished reading
  173. from the directory. At this point, the iterable will no longer
  174. be used.
  175. The iterable should return triples of the form (filename,
  176. longname, attrs) or Deferreds that return the same. The
  177. sequence must support __getitem__, but otherwise may be any
  178. 'sequence-like' object.
  179. filename is the name of the file relative to the directory.
  180. logname is an expanded format of the filename. The recommended format
  181. is:
  182. -rwxr-xr-x 1 mjos staff 348911 Mar 25 14:29 t-filexfer
  183. 1234567890 123 12345678 12345678 12345678 123456789012
  184. The first line is sample output, the second is the length of the field.
  185. The fields are: permissions, link count, user owner, group owner,
  186. size in bytes, modification time.
  187. attrs is a dictionary in the format of the attrs argument to openFile.
  188. @param path: the directory to open.
  189. """
  190. def getAttrs(path, followLinks):
  191. """
  192. Return the attributes for the given path.
  193. This method returns a dictionary in the same format as the attrs
  194. argument to openFile or a Deferred that is called back with same.
  195. @param path: the path to return attributes for as a string.
  196. @param followLinks: a boolean. If it is True, follow symbolic links
  197. and return attributes for the real path at the base. If it is False,
  198. return attributes for the specified path.
  199. """
  200. def setAttrs(path, attrs):
  201. """
  202. Set the attributes for the path.
  203. This method returns when the attributes are set or a Deferred that is
  204. called back when they are.
  205. @param path: the path to set attributes for as a string.
  206. @param attrs: a dictionary in the same format as the attrs argument to
  207. L{openFile}.
  208. """
  209. def readLink(path):
  210. """
  211. Find the root of a set of symbolic links.
  212. This method returns the target of the link, or a Deferred that
  213. returns the same.
  214. @param path: the path of the symlink to read.
  215. """
  216. def makeLink(linkPath, targetPath):
  217. """
  218. Create a symbolic link.
  219. This method returns when the link is made, or a Deferred that
  220. returns the same.
  221. @param linkPath: the pathname of the symlink as a string.
  222. @param targetPath: the path of the target of the link as a string.
  223. """
  224. def realPath(path):
  225. """
  226. Convert any path to an absolute path.
  227. This method returns the absolute path as a string, or a Deferred
  228. that returns the same.
  229. @param path: the path to convert as a string.
  230. """
  231. def extendedRequest(extendedName, extendedData):
  232. """
  233. This is the extension mechanism for SFTP. The other side can send us
  234. arbitrary requests.
  235. If we don't implement the request given by extendedName, raise
  236. NotImplementedError.
  237. The return value is a string, or a Deferred that will be called
  238. back with a string.
  239. @param extendedName: the name of the request as a string.
  240. @param extendedData: the data the other side sent with the request,
  241. as a string.
  242. """
  243. class IKnownHostEntry(Interface):
  244. """
  245. A L{IKnownHostEntry} is an entry in an OpenSSH-formatted C{known_hosts}
  246. file.
  247. @since: 8.2
  248. """
  249. def matchesKey(key):
  250. """
  251. Return True if this entry matches the given Key object, False
  252. otherwise.
  253. @param key: The key object to match against.
  254. @type key: L{twisted.conch.ssh.keys.Key}
  255. """
  256. def matchesHost(hostname):
  257. """
  258. Return True if this entry matches the given hostname, False otherwise.
  259. Note that this does no name resolution; if you want to match an IP
  260. address, you have to resolve it yourself, and pass it in as a dotted
  261. quad string.
  262. @param hostname: The hostname to match against.
  263. @type hostname: L{str}
  264. """
  265. def toString():
  266. """
  267. @return: a serialized string representation of this entry, suitable for
  268. inclusion in a known_hosts file. (Newline not included.)
  269. @rtype: L{str}
  270. """
  271. class ISFTPFile(Interface):
  272. """
  273. This represents an open file on the server. An object adhering to this
  274. interface should be returned from L{openFile}().
  275. """
  276. def close():
  277. """
  278. Close the file.
  279. This method returns nothing if the close succeeds immediately, or a
  280. Deferred that is called back when the close succeeds.
  281. """
  282. def readChunk(offset, length):
  283. """
  284. Read from the file.
  285. If EOF is reached before any data is read, raise EOFError.
  286. This method returns the data as a string, or a Deferred that is
  287. called back with same.
  288. @param offset: an integer that is the index to start from in the file.
  289. @param length: the maximum length of data to return. The actual amount
  290. returned may less than this. For normal disk files, however,
  291. this should read the requested number (up to the end of the file).
  292. """
  293. def writeChunk(offset, data):
  294. """
  295. Write to the file.
  296. This method returns when the write completes, or a Deferred that is
  297. called when it completes.
  298. @param offset: an integer that is the index to start from in the file.
  299. @param data: a string that is the data to write.
  300. """
  301. def getAttrs():
  302. """
  303. Return the attributes for the file.
  304. This method returns a dictionary in the same format as the attrs
  305. argument to L{openFile} or a L{Deferred} that is called back with same.
  306. """
  307. def setAttrs(attrs):
  308. """
  309. Set the attributes for the file.
  310. This method returns when the attributes are set or a Deferred that is
  311. called back when they are.
  312. @param attrs: a dictionary in the same format as the attrs argument to
  313. L{openFile}.
  314. """