1
# Copyright (c) 2007-2008 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
This module contains interfaces defined for the L{twisted.conch} package.
8
from zope.interface import Interface, Attribute
10
class IConchUser(Interface):
12
A user who has been authenticated to Cred through Conch. This is
13
the interface between the SSH connection and the user.
16
conn = Attribute('The SSHConnection object for this user.')
18
def lookupChannel(channelType, windowSize, maxPacket, data):
20
The other side requested a channel of some sort.
21
channelType is the type of channel being requested,
22
windowSize is the initial size of the remote window,
23
maxPacket is the largest packet we should send,
24
data is any other packet data (often nothing).
26
We return a subclass of L{SSHChannel<ssh.channel.SSHChannel>}. If
27
an appropriate channel can not be found, an exception will be
28
raised. If a L{ConchError<error.ConchError>} is raised, the .value
29
will be the message, and the .data will be the error code.
31
@type channelType: C{str}
32
@type windowSize: C{int}
33
@type maxPacket: C{int}
35
@rtype: subclass of L{SSHChannel}/C{tuple}
38
def lookupSubsystem(subsystem, data):
40
The other side requested a subsystem.
41
subsystem is the name of the subsystem being requested.
42
data is any other packet data (often nothing).
44
We return a L{Protocol}.
47
def gotGlobalRequest(requestType, data):
49
A global request was sent from the other side.
51
By default, this dispatches to a method 'channel_channelType' with any
52
non-alphanumerics in the channelType replace with _'s. If it cannot
53
find a suitable method, it returns an OPEN_UNKNOWN_CHANNEL_TYPE error.
54
The method is called with arguments of windowSize, maxPacket, data.
57
class ISession(Interface):
59
def getPty(term, windowSize, modes):
61
Get a psuedo-terminal for use by a shell or command.
63
If a psuedo-terminal is not available, or the request otherwise
64
fails, raise an exception.
69
Open a shell and connect it to proto.
71
@param proto: a L{ProcessProtocol} instance.
74
def execCommand(proto, command):
78
@param proto: a L{ProcessProtocol} instance.
81
def windowChanged(newWindowSize):
83
Called when the size of the remote screen has changed.
88
Called when the other side has indicated no more data will be sent.
93
Called when the session is closed.
97
class ISFTPServer(Interface):
99
The only attribute of this class is "avatar". It is the avatar
100
returned by the Realm that we are authenticated with, and
101
represents the logged-in user. Each method should check to verify
102
that the user has permission for their actions.
105
def gotVersion(otherVersion, extData):
107
Called when the client sends their version info.
109
otherVersion is an integer representing the version of the SFTP
110
protocol they are claiming.
111
extData is a dictionary of extended_name : extended_data items.
112
These items are sent by the client to indicate additional features.
114
This method should return a dictionary of extended_name : extended_data
115
items. These items are the additional features (if any) supported
120
def openFile(filename, flags, attrs):
122
Called when the clients asks to open a file.
124
@param filename: a string representing the file to open.
126
@param flags: an integer of the flags to open the file with, ORed together.
127
The flags and their values are listed at the bottom of this file.
129
@param attrs: a list of attributes to open the file with. It is a
130
dictionary, consisting of 0 or more keys. The possible keys are::
132
size: the size of the file in bytes
133
uid: the user ID of the file as an integer
134
gid: the group ID of the file as an integer
135
permissions: the permissions of the file with as an integer.
136
the bit representation of this field is defined by POSIX.
137
atime: the access time of the file as seconds since the epoch.
138
mtime: the modification time of the file as seconds since the epoch.
139
ext_*: extended attributes. The server is not required to
140
understand this, but it may.
142
NOTE: there is no way to indicate text or binary files. it is up
143
to the SFTP client to deal with this.
145
This method returns an object that meets the ISFTPFile interface.
146
Alternatively, it can return a L{Deferred} that will be called back
150
def removeFile(filename):
152
Remove the given file.
154
This method returns when the remove succeeds, or a Deferred that is
155
called back when it succeeds.
157
@param filename: the name of the file as a string.
160
def renameFile(oldpath, newpath):
162
Rename the given file.
164
This method returns when the rename succeeds, or a L{Deferred} that is
165
called back when it succeeds. If the rename fails, C{renameFile} will
166
raise an implementation-dependent exception.
168
@param oldpath: the current location of the file.
169
@param newpath: the new file name.
172
def makeDirectory(path, attrs):
176
This method returns when the directory is created, or a Deferred that
177
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.
184
def removeDirectory(path):
186
Remove a directory (non-recursively)
188
It is an error to remove a directory that has files or directories in
191
This method returns when the directory is removed, or a Deferred that
192
is called back when it is removed.
194
@param path: the directory to remove.
197
def openDirectory(path):
199
Open a directory for scanning.
201
This method returns an iterable object that has a close() method,
202
or a Deferred that is called back with same.
204
The close() method is called when the client is finished reading
205
from the directory. At this point, the iterable will no longer
208
The iterable should return triples of the form (filename,
209
longname, attrs) or Deferreds that return the same. The
210
sequence must support __getitem__, but otherwise may be any
211
'sequence-like' object.
213
filename is the name of the file relative to the directory.
214
logname is an expanded format of the filename. The recommended format
216
-rwxr-xr-x 1 mjos staff 348911 Mar 25 14:29 t-filexfer
217
1234567890 123 12345678 12345678 12345678 123456789012
219
The first line is sample output, the second is the length of the field.
220
The fields are: permissions, link count, user owner, group owner,
221
size in bytes, modification time.
223
attrs is a dictionary in the format of the attrs argument to openFile.
225
@param path: the directory to open.
228
def getAttrs(path, followLinks):
230
Return the attributes for the given path.
232
This method returns a dictionary in the same format as the attrs
233
argument to openFile or a Deferred that is called back with same.
235
@param path: the path to return attributes for as a string.
236
@param followLinks: a boolean. If it is True, follow symbolic links
237
and return attributes for the real path at the base. If it is False,
238
return attributes for the specified path.
241
def setAttrs(path, attrs):
243
Set the attributes for the path.
245
This method returns when the attributes are set or a Deferred that is
246
called back when they are.
248
@param path: the path to set attributes for as a string.
249
@param attrs: a dictionary in the same format as the attrs argument to
255
Find the root of a set of symbolic links.
257
This method returns the target of the link, or a Deferred that
260
@param path: the path of the symlink to read.
263
def makeLink(linkPath, targetPath):
265
Create a symbolic link.
267
This method returns when the link is made, or a Deferred that
270
@param linkPath: the pathname of the symlink as a string.
271
@param targetPath: the path of the target of the link as a string.
276
Convert any path to an absolute path.
278
This method returns the absolute path as a string, or a Deferred
279
that returns the same.
281
@param path: the path to convert as a string.
284
def extendedRequest(extendedName, extendedData):
286
This is the extension mechanism for SFTP. The other side can send us
289
If we don't implement the request given by extendedName, raise
292
The return value is a string, or a Deferred that will be called
295
@param extendedName: the name of the request as a string.
296
@param extendedData: the data the other side sent with the request,
302
class IKnownHostEntry(Interface):
304
A L{IKnownHostEntry} is an entry in an OpenSSH-formatted C{known_hosts}
312
Return True if this entry matches the given Key object, False
315
@param key: The key object to match against.
316
@type key: L{twisted.conch.ssh.Key}
320
def matchesHost(hostname):
322
Return True if this entry matches the given hostname, False otherwise.
324
Note that this does no name resolution; if you want to match an IP
325
address, you have to resolve it yourself, and pass it in as a dotted
328
@param key: The hostname to match against.
335
@return: a serialized string representation of this entry, suitable for
336
inclusion in a known_hosts file. (Newline not included.)
343
class ISFTPFile(Interface):
345
This represents an open file on the server. An object adhering to this
346
interface should be returned from L{openFile}().
353
This method returns nothing if the close succeeds immediately, or a
354
Deferred that is called back when the close succeeds.
357
def readChunk(offset, length):
361
If EOF is reached before any data is read, raise EOFError.
363
This method returns the data as a string, or a Deferred that is
364
called back with same.
366
@param offset: an integer that is the index to start from in the file.
367
@param length: the maximum length of data to return. The actual amount
368
returned may less than this. For normal disk files, however,
369
this should read the requested number (up to the end of the file).
372
def writeChunk(offset, data):
376
This method returns when the write completes, or a Deferred that is
377
called when it completes.
379
@param offset: an integer that is the index to start from in the file.
380
@param data: a string that is the data to write.
385
Return the attributes for the file.
387
This method returns a dictionary in the same format as the attrs
388
argument to L{openFile} or a L{Deferred} that is called back with same.
393
Set the attributes for the file.
395
This method returns when the attributes are set or a Deferred that is
396
called back when they are.
398
@param attrs: a dictionary in the same format as the attrs argument to