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.

interfaces.py 15KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 Attribute, Interface
  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 EnvironmentVariableNotPermitted(ValueError):
  87. """Setting this environment variable in this session is not permitted."""
  88. class ISessionSetEnv(Interface):
  89. """A session that can set environment variables."""
  90. def setEnv(name, value):
  91. """
  92. Set an environment variable for the shell or command to be started.
  93. From U{RFC 4254, section 6.4
  94. <https://tools.ietf.org/html/rfc4254#section-6.4>}: "Uncontrolled
  95. setting of environment variables in a privileged process can be a
  96. security hazard. It is recommended that implementations either
  97. maintain a list of allowable variable names or only set environment
  98. variables after the server process has dropped sufficient
  99. privileges."
  100. (OpenSSH refuses all environment variables by default, but has an
  101. C{AcceptEnv} configuration option to select specific variables to
  102. accept.)
  103. @param name: The name of the environment variable to set.
  104. @type name: L{bytes}
  105. @param value: The value of the environment variable to set.
  106. @type value: L{bytes}
  107. @raise EnvironmentVariableNotPermitted: if setting this environment
  108. variable is not permitted.
  109. """
  110. class ISFTPServer(Interface):
  111. """
  112. SFTP subsystem for server-side communication.
  113. Each method should check to verify that the user has permission for
  114. their actions.
  115. """
  116. avatar = Attribute(
  117. """
  118. The avatar returned by the Realm that we are authenticated with,
  119. and represents the logged-in user.
  120. """
  121. )
  122. def gotVersion(otherVersion, extData):
  123. """
  124. Called when the client sends their version info.
  125. otherVersion is an integer representing the version of the SFTP
  126. protocol they are claiming.
  127. extData is a dictionary of extended_name : extended_data items.
  128. These items are sent by the client to indicate additional features.
  129. This method should return a dictionary of extended_name : extended_data
  130. items. These items are the additional features (if any) supported
  131. by the server.
  132. """
  133. return {}
  134. def openFile(filename, flags, attrs):
  135. """
  136. Called when the clients asks to open a file.
  137. @param filename: a string representing the file to open.
  138. @param flags: an integer of the flags to open the file with, ORed
  139. together. The flags and their values are listed at the bottom of
  140. L{twisted.conch.ssh.filetransfer} as FXF_*.
  141. @param attrs: a list of attributes to open the file with. It is a
  142. dictionary, consisting of 0 or more keys. The possible keys are::
  143. size: the size of the file in bytes
  144. uid: the user ID of the file as an integer
  145. gid: the group ID of the file as an integer
  146. permissions: the permissions of the file with as an integer.
  147. the bit representation of this field is defined by POSIX.
  148. atime: the access time of the file as seconds since the epoch.
  149. mtime: the modification time of the file as seconds since the epoch.
  150. ext_*: extended attributes. The server is not required to
  151. understand this, but it may.
  152. NOTE: there is no way to indicate text or binary files. it is up
  153. to the SFTP client to deal with this.
  154. This method returns an object that meets the ISFTPFile interface.
  155. Alternatively, it can return a L{Deferred} that will be called back
  156. with the object.
  157. """
  158. def removeFile(filename):
  159. """
  160. Remove the given file.
  161. This method returns when the remove succeeds, or a Deferred that is
  162. called back when it succeeds.
  163. @param filename: the name of the file as a string.
  164. """
  165. def renameFile(oldpath, newpath):
  166. """
  167. Rename the given file.
  168. This method returns when the rename succeeds, or a L{Deferred} that is
  169. called back when it succeeds. If the rename fails, C{renameFile} will
  170. raise an implementation-dependent exception.
  171. @param oldpath: the current location of the file.
  172. @param newpath: the new file name.
  173. """
  174. def makeDirectory(path, attrs):
  175. """
  176. Make a directory.
  177. This method returns when the directory is created, or a Deferred that
  178. is called back when it is created.
  179. @param path: the name of the directory to create as a string.
  180. @param attrs: a dictionary of attributes to create the directory with.
  181. Its meaning is the same as the attrs in the L{openFile} method.
  182. """
  183. def removeDirectory(path):
  184. """
  185. Remove a directory (non-recursively)
  186. It is an error to remove a directory that has files or directories in
  187. it.
  188. This method returns when the directory is removed, or a Deferred that
  189. is called back when it is removed.
  190. @param path: the directory to remove.
  191. """
  192. def openDirectory(path):
  193. """
  194. Open a directory for scanning.
  195. This method returns an iterable object that has a close() method,
  196. or a Deferred that is called back with same.
  197. The close() method is called when the client is finished reading
  198. from the directory. At this point, the iterable will no longer
  199. be used.
  200. The iterable should return triples of the form (filename,
  201. longname, attrs) or Deferreds that return the same. The
  202. sequence must support __getitem__, but otherwise may be any
  203. 'sequence-like' object.
  204. filename is the name of the file relative to the directory.
  205. logname is an expanded format of the filename. The recommended format
  206. is:
  207. -rwxr-xr-x 1 mjos staff 348911 Mar 25 14:29 t-filexfer
  208. 1234567890 123 12345678 12345678 12345678 123456789012
  209. The first line is sample output, the second is the length of the field.
  210. The fields are: permissions, link count, user owner, group owner,
  211. size in bytes, modification time.
  212. attrs is a dictionary in the format of the attrs argument to openFile.
  213. @param path: the directory to open.
  214. """
  215. def getAttrs(path, followLinks):
  216. """
  217. Return the attributes for the given path.
  218. This method returns a dictionary in the same format as the attrs
  219. argument to openFile or a Deferred that is called back with same.
  220. @param path: the path to return attributes for as a string.
  221. @param followLinks: a boolean. If it is True, follow symbolic links
  222. and return attributes for the real path at the base. If it is False,
  223. return attributes for the specified path.
  224. """
  225. def setAttrs(path, attrs):
  226. """
  227. Set the attributes for the path.
  228. This method returns when the attributes are set or a Deferred that is
  229. called back when they are.
  230. @param path: the path to set attributes for as a string.
  231. @param attrs: a dictionary in the same format as the attrs argument to
  232. L{openFile}.
  233. """
  234. def readLink(path):
  235. """
  236. Find the root of a set of symbolic links.
  237. This method returns the target of the link, or a Deferred that
  238. returns the same.
  239. @param path: the path of the symlink to read.
  240. """
  241. def makeLink(linkPath, targetPath):
  242. """
  243. Create a symbolic link.
  244. This method returns when the link is made, or a Deferred that
  245. returns the same.
  246. @param linkPath: the pathname of the symlink as a string.
  247. @param targetPath: the path of the target of the link as a string.
  248. """
  249. def realPath(path):
  250. """
  251. Convert any path to an absolute path.
  252. This method returns the absolute path as a string, or a Deferred
  253. that returns the same.
  254. @param path: the path to convert as a string.
  255. """
  256. def extendedRequest(extendedName, extendedData):
  257. """
  258. This is the extension mechanism for SFTP. The other side can send us
  259. arbitrary requests.
  260. If we don't implement the request given by extendedName, raise
  261. NotImplementedError.
  262. The return value is a string, or a Deferred that will be called
  263. back with a string.
  264. @param extendedName: the name of the request as a string.
  265. @param extendedData: the data the other side sent with the request,
  266. as a string.
  267. """
  268. class IKnownHostEntry(Interface):
  269. """
  270. A L{IKnownHostEntry} is an entry in an OpenSSH-formatted C{known_hosts}
  271. file.
  272. @since: 8.2
  273. """
  274. def matchesKey(key):
  275. """
  276. Return True if this entry matches the given Key object, False
  277. otherwise.
  278. @param key: The key object to match against.
  279. @type key: L{twisted.conch.ssh.keys.Key}
  280. """
  281. def matchesHost(hostname):
  282. """
  283. Return True if this entry matches the given hostname, False otherwise.
  284. Note that this does no name resolution; if you want to match an IP
  285. address, you have to resolve it yourself, and pass it in as a dotted
  286. quad string.
  287. @param hostname: The hostname to match against.
  288. @type hostname: L{str}
  289. """
  290. def toString():
  291. """
  292. @return: a serialized string representation of this entry, suitable for
  293. inclusion in a known_hosts file. (Newline not included.)
  294. @rtype: L{str}
  295. """
  296. class ISFTPFile(Interface):
  297. """
  298. This represents an open file on the server. An object adhering to this
  299. interface should be returned from L{openFile}().
  300. """
  301. def close():
  302. """
  303. Close the file.
  304. This method returns nothing if the close succeeds immediately, or a
  305. Deferred that is called back when the close succeeds.
  306. """
  307. def readChunk(offset, length):
  308. """
  309. Read from the file.
  310. If EOF is reached before any data is read, raise EOFError.
  311. This method returns the data as a string, or a Deferred that is
  312. called back with same.
  313. @param offset: an integer that is the index to start from in the file.
  314. @param length: the maximum length of data to return. The actual amount
  315. returned may less than this. For normal disk files, however,
  316. this should read the requested number (up to the end of the file).
  317. """
  318. def writeChunk(offset, data):
  319. """
  320. Write to the file.
  321. This method returns when the write completes, or a Deferred that is
  322. called when it completes.
  323. @param offset: an integer that is the index to start from in the file.
  324. @param data: a string that is the data to write.
  325. """
  326. def getAttrs():
  327. """
  328. Return the attributes for the file.
  329. This method returns a dictionary in the same format as the attrs
  330. argument to L{openFile} or a L{Deferred} that is called back with same.
  331. """
  332. def setAttrs(attrs):
  333. """
  334. Set the attributes for the file.
  335. This method returns when the attributes are set or a Deferred that is
  336. called back when they are.
  337. @param attrs: a dictionary in the same format as the attrs argument to
  338. L{openFile}.
  339. """