Skip to content

Commit

Permalink
Merge branch 'rename_project_files'
Browse files Browse the repository at this point in the history
  • Loading branch information
cyli committed Jul 10, 2012
2 parents 39cf046 + 2f0bc83 commit 8a18ff9
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 349 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.pyc
*_trial_temp
*coverage_html_report
*.coverage
*dropin.cache
File renamed without changes.
2 changes: 2 additions & 0 deletions csftp/_openSSHConfig.py → ess/_openSSHConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
HostKeyAlias %(hostname)s
HostName localhost
CheckHostIP no
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
IdentityFile %(clientPrivkeyFile)s
Port %(port)d
Expand All @@ -111,6 +112,7 @@
GSSAPIDelegateCredentials no
"""


def setupConfig(directoryPath, port):
f = FilePath(directoryPath)
hostkey = f.child('hostkey')
Expand Down
69 changes: 19 additions & 50 deletions csftp/server.py → ess/essftp.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os

from csftp import shelless
from csftp.filepath import FilePath

from zope.interface import implements
from twisted.cred import portal
from twisted.conch.interfaces import ISFTPServer, ISFTPFile
from twisted.python import components
from twisted.conch.ssh import filetransfer
from twisted.conch.ls import lsLine
from twisted.conch.ssh import filetransfer
from twisted.cred import portal
from twisted.python import components

from zope.interface import implements

from ess import shelless
from ess.filepath import FilePath


def _simplifyAttributes(filePath):
Expand All @@ -24,11 +25,10 @@ def _simplifyAttributes(filePath):
"mtime": filePath.getModificationTime()}



class ChrootedSFTPServer:
class EssFTPServer:
implements(ISFTPServer)
"""
A Chrooted SFTP server based on twisted.python.filepath.FilePath.
An SFTP server based on twisted.python.filepath.FilePath.
This prevents users from connecting to a path above the set root
path. It ignores permissions, since everything is executed as
whatever user the SFTP server is executed as (it does not need to
Expand All @@ -39,7 +39,6 @@ def __init__(self, avatar):
self.avatar = avatar
self.root = FilePath(self.avatar.root)


def _getFilePath(self, path):
"""
Takes a string path and returns a FilePath object corresponding
Expand All @@ -61,7 +60,6 @@ def _getFilePath(self, path):
assert fp.path.startswith(self.root.path)
return fp


def _getRelativePath(self, filePath):
"""
Takes a FilePath and returns a path string relative to the root
Expand All @@ -74,21 +72,17 @@ def _getRelativePath(self, filePath):
return "/"
return "/" + "/".join(filePath.segmentsFrom(self.root))


def _islink(self, fp):
if fp.islink() and fp.realpath().path.startswith(self.root.path):
return True
return False


def gotVersion(self, otherVersion, extData):
return {}


def openFile(self, filename, flags, attrs):
fp = self._getFilePath(filename)
return ChrootedSFTPFile(fp, flags, attrs)

return ChrootedFile(fp, flags, attrs)

def removeFile(self, filename):
"""
Expand All @@ -104,7 +98,6 @@ def removeFile(self, filename):
raise IOError("%s is a directory" % filename)
fp.remove()


def renameFile(self, oldname, newname):
"""
Rename the given file/directory/link.
Expand All @@ -120,7 +113,6 @@ def renameFile(self, oldname, newname):
raise IOError("%s does not exist" % oldname)
oldFP.moveTo(newFP)


def makeDirectory(self, path, attrs=None):
"""
Make a directory. Ignores the attributes.
Expand All @@ -130,7 +122,6 @@ def makeDirectory(self, path, attrs=None):
raise IOError("%s already exists." % path)
fp.createDirectory()


def removeDirectory(self, path):
"""
Remove a directory non-recursively.
Expand All @@ -152,14 +143,12 @@ def removeDirectory(self, path):
raise IOError("%s is not empty.")
fp.remove()


def openDirectory(self, path):
fp = self._getFilePath(path)
if not fp.isdir():
raise IOError("%s is not a directory." % path)
return ChrootedDirectory(self, fp)


def getAttrs(self, path, followLinks=True):
"""
Get attributes of the path.
Expand All @@ -172,11 +161,9 @@ def getAttrs(self, path, followLinks=True):
fp.restat(followLink=followLinks)
return _simplifyAttributes(fp)


def setAttrs(self, path, attrs):
raise NotImplementedError


def readLink(self, path):
"""
Returns the target of a symbolic link (relative to the root), so
Expand All @@ -195,7 +182,6 @@ def readLink(self, path):
return self._getRelativePath(rp)
raise IOError("%s is not a link." % path)


def makeLink(self, linkPath, targetPath):
"""
Create a symbolic link from linkPath to targetPath.
Expand All @@ -211,7 +197,6 @@ def makeLink(self, linkPath, targetPath):
raise IOError("%s does not exist." % targetPath)
tp.linkTo(lp)


def realPath(self, path):
"""
Despite what the interface says, this function will only return
Expand All @@ -226,16 +211,14 @@ def realPath(self, path):
fp = fp.realpath()
return self._getRelativePath(fp)


def extendedRequest(self, extendedName, extendedData):
raise NotImplementedError



#Figure out a way to test this
class ChrootedDirectory:
"""
A Chrooted SFTP directory based on twisted.python.filepath.FilePath. It
A "chrooted" directory based on twisted.python.filepath.FilePath. It
does not expose uid and gid, and hides the fact that "fake directories"
and "fake files" are links.
"""
Expand All @@ -248,15 +231,12 @@ def __init__(self, server, filePath):
self.server = server
self.files = filePath.children()


def __iter__(self):
return self


def has_next(self):
return len(self.files) > 0


def next(self):
# TODO: problem - what if the user that logs in is not a user in the
# system?
Expand All @@ -272,15 +252,13 @@ def next(self):
longname = longname[:15] + longname[32:] # remove uid and gid
return (f.basename(), longname, _simplifyAttributes(f))


def close(self):
self.files = None



class ChrootedSFTPFile:
class ChrootedFile:
"""
A Chrooted SFTP file based on twisted.python.filepath.FilePath.
A "chrooted" file based on twisted.python.filepath.FilePath.
"""
implements(ISFTPFile)

Expand All @@ -292,7 +270,6 @@ def __init__(self, filePath, flags, attrs=None):
self.filePath = filePath
self.fd = self.filePath.open(flags=self.flagTranslator(flags))


def flagTranslator(self, flags):
"""
Translate filetransfer flags to Python file opening modes
Expand Down Expand Up @@ -330,11 +307,9 @@ def isInFlags(lookingFor):

return newflags


def close(self):
self.fd.close()


def readChunk(self, offset, length):
"""
Read a chunk of data from the file
Expand All @@ -345,7 +320,6 @@ def readChunk(self, offset, length):
self.fd.seek(offset)
return self.fd.read(length)


def writeChunk(self, offset, data):
"""
Write data to the file at the given offset
Expand All @@ -356,11 +330,9 @@ def writeChunk(self, offset, data):
self.fd.seek(offset)
self.fd.write(data)


def getAttrs(self):
return _simplifyAttributes(self.filePath)


def setAttrs(self, attrs=None):
"""
This must return something, in order for certain write to be able to
Expand All @@ -370,24 +342,21 @@ def setAttrs(self, attrs=None):
#raise NotImplementedError



class ChrootedSSHRealm(object):
class EssFTPRealm(object):
"""
A realm that returns a ChrootedUser as an avatar
A realm that returns a EssSFTPUser as an avatar
"""
implements(portal.IRealm)

def __init__(self, root):
self.root = root


def requestAvatar(self, avatarID, mind, *interfaces):
user = ChrootedUser(self.root)
user = EssFTPUser(self.root)
return interfaces[0], user, user.logout



class ChrootedUser(shelless.ShelllessUser):
class EssFTPUser(shelless.ShelllessUser):
"""
A shell-less user that does not answer any global requests.
"""
Expand All @@ -397,5 +366,5 @@ def __init__(self, root):
self.root = root


components.registerAdapter(ChrootedSFTPServer, ChrootedUser,
components.registerAdapter(EssFTPServer, EssFTPUser,
filetransfer.ISFTPServer)
File renamed without changes.
15 changes: 4 additions & 11 deletions csftp/shelless.py → ess/shelless.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from zope import interface
from twisted.cred import portal
from twisted.python import log
from twisted.conch.avatar import ConchUser
from twisted.conch.ssh import session
from twisted.cred import portal
from twisted.python import log

from zope import interface


class ShelllessSSHRealm:
Expand All @@ -13,7 +14,6 @@ def requestAvatar(self, avatarID, mind, *interfaces):
return interfaces[0], user, user.logout



class ShelllessUser(ConchUser):
"""
A shell-less user that does not answer any global requests.
Expand All @@ -22,43 +22,36 @@ def __init__(self, root=None):
ConchUser.__init__(self)
self.channelLookup["session"] = ShelllessSession


def logout(self):
pass # nothing to do



class ShelllessSession(session.SSHSession):

name = 'shellessSession'

def __init__(self, *args, **kw):
session.SSHSession.__init__(self, *args, **kw)


def _noshell(self):
if not self.closing:
self.write("This server does not provide shells "
"or allow command execution.\n")
self.loseConnection()
return 0


def request_shell(self, data):
log.msg("shell request rejected")
return self._noshell()


def request_exec(self, data):
log.msg("execution request rejected")
return self._noshell()


def request_pty_req(self, data):
log.msg("pty request rejected")
return self._noshell()


def request_window_change(self, data):
log.msg("window change request rejected")
return 0
Empty file added ess/test/__init__.py
Empty file.

0 comments on commit 8a18ff9

Please sign in to comment.