def test_validpath(self): # Tests for validpath method. fs = AbstractedFS(u('/'), None) fs._root = HOME self.assertTrue(fs.validpath(HOME)) self.assertTrue(fs.validpath(HOME + '/')) self.assertFalse(fs.validpath(HOME + 'bar'))
def main(): # Instantiate a dummy authorizer for managing 'virtual' users authorizer = DummyAuthorizer() # Define a new user having full r/w permissions authorizer.add_user('user_name', 'pass_word', './', perm='elradfmwM', msg_login='******', msg_quit='bye') # Define a read-only anonymous user authorizer.add_anonymous('./') # Instantiate FTP handler class handler = FTPHandler handler.authorizer = authorizer handler.max_login_attempts = 3 handler.permit_foreign_addresses = True handler.tcp_no_delay = True # Define a customized banner (string returned when client connects) handler.banner = "Welcome to my FTP." # Instantiate FTP server class and listen on 127.0.0.1:21 address = ('0.0.0.0', 2121) server = FTPServer(address, handler) # set a limit for connections server.max_cons = 128 server.max_cons_per_ip = 2 absfs = AbstractedFS(u"./", handler) absfs.cwd = u"/bbb/ss/" # start ftp server server.serve_forever()
def test_ftpnorm(self): # Tests for ftpnorm method. ae = self.assertEqual fs = AbstractedFS(u('/'), None) fs._cwd = u('/') ae(fs.ftpnorm(u('')), u('/')) ae(fs.ftpnorm(u('/')), u('/')) ae(fs.ftpnorm(u('.')), u('/')) ae(fs.ftpnorm(u('..')), u('/')) ae(fs.ftpnorm(u('a')), u('/a')) ae(fs.ftpnorm(u('/a')), u('/a')) ae(fs.ftpnorm(u('/a/')), u('/a')) ae(fs.ftpnorm(u('a/..')), u('/')) ae(fs.ftpnorm(u('a/b')), '/a/b') ae(fs.ftpnorm(u('a/b/..')), u('/a')) ae(fs.ftpnorm(u('a/b/../..')), u('/')) fs._cwd = u('/sub') ae(fs.ftpnorm(u('')), u('/sub')) ae(fs.ftpnorm(u('/')), u('/')) ae(fs.ftpnorm(u('.')), u('/sub')) ae(fs.ftpnorm(u('..')), u('/')) ae(fs.ftpnorm(u('a')), u('/sub/a')) ae(fs.ftpnorm(u('a/')), u('/sub/a')) ae(fs.ftpnorm(u('a/..')), u('/sub')) ae(fs.ftpnorm(u('a/b')), u('/sub/a/b')) ae(fs.ftpnorm(u('a/b/')), u('/sub/a/b')) ae(fs.ftpnorm(u('a/b/..')), u('/sub/a')) ae(fs.ftpnorm(u('a/b/../..')), u('/sub')) ae(fs.ftpnorm(u('a/b/../../..')), u('/')) ae(fs.ftpnorm(u('//')), u('/')) # UNC paths must be collapsed
def test_validpath_validlink(self): # Test validpath by issuing a symlink pointing to a path # inside the root directory. testfn = self.get_testfn() testfn2 = self.get_testfn() fs = AbstractedFS(u('/'), None) fs._root = HOME touch(testfn) os.symlink(testfn, testfn2) self.assertTrue(fs.validpath(u(testfn)))
def test_validpath_validlink(self): # Test validpath by issuing a symlink pointing to a path # inside the root directory. fs = AbstractedFS(u('/'), None) fs._root = HOME TESTFN2 = TESTFN + '1' try: touch(TESTFN) os.symlink(TESTFN, TESTFN2) self.assertTrue(fs.validpath(u(TESTFN))) finally: safe_remove(TESTFN, TESTFN2)
def test_ftp2fs(self): # Tests for ftp2fs method. def join(x, y): return os.path.join(x, y.replace('/', os.sep)) ae = self.assertEqual fs = AbstractedFS(u('/'), None) def goforit(root): fs._root = root fs._cwd = u('/') ae(fs.ftp2fs(u('')), root) ae(fs.ftp2fs(u('/')), root) ae(fs.ftp2fs(u('.')), root) ae(fs.ftp2fs(u('..')), root) ae(fs.ftp2fs(u('a')), join(root, u('a'))) ae(fs.ftp2fs(u('/a')), join(root, u('a'))) ae(fs.ftp2fs(u('/a/')), join(root, u('a'))) ae(fs.ftp2fs(u('a/..')), root) ae(fs.ftp2fs(u('a/b')), join(root, u(r'a/b'))) ae(fs.ftp2fs(u('/a/b')), join(root, u(r'a/b'))) ae(fs.ftp2fs(u('/a/b/..')), join(root, u('a'))) ae(fs.ftp2fs(u('/a/b/../..')), root) fs._cwd = u('/sub') ae(fs.ftp2fs(u('')), join(root, u('sub'))) ae(fs.ftp2fs(u('/')), root) ae(fs.ftp2fs(u('.')), join(root, u('sub'))) ae(fs.ftp2fs(u('..')), root) ae(fs.ftp2fs(u('a')), join(root, u('sub/a'))) ae(fs.ftp2fs(u('a/')), join(root, u('sub/a'))) ae(fs.ftp2fs(u('a/..')), join(root, u('sub'))) ae(fs.ftp2fs(u('a/b')), join(root, 'sub/a/b')) ae(fs.ftp2fs(u('a/b/..')), join(root, u('sub/a'))) ae(fs.ftp2fs(u('a/b/../..')), join(root, u('sub'))) ae(fs.ftp2fs(u('a/b/../../..')), root) # UNC paths must be collapsed ae(fs.ftp2fs(u('//a')), join(root, u('a'))) if os.sep == '\\': goforit(u(r'C:\dir')) goforit(u('C:\\')) # on DOS-derived filesystems (e.g. Windows) this is the same # as specifying the current drive directory (e.g. 'C:\\') goforit(u('\\')) elif os.sep == '/': goforit(u('/home/user')) goforit(u('/')) else: # os.sep == ':'? Don't know... let's try it anyway goforit(getcwdu())
def test_validpath_external_symlink(self): # Test validpath by issuing a symlink pointing to a path # outside the root directory. fs = AbstractedFS(u('/'), None) fs._root = HOME # tempfile should create our file in /tmp directory # which should be outside the user root. If it is # not we just skip the test. with tempfile.NamedTemporaryFile() as file: try: if HOME == os.path.dirname(file.name): return os.symlink(file.name, TESTFN) self.assertFalse(fs.validpath(u(TESTFN))) finally: safe_remove(TESTFN)
def test_fs2ftp(self): # Tests for fs2ftp method. def join(x, y): return os.path.join(x, y.replace('/', os.sep)) ae = self.assertEqual fs = AbstractedFS(u('/'), None) def goforit(root): fs._root = root ae(fs.fs2ftp(root), u('/')) ae(fs.fs2ftp(join(root, u('/'))), u('/')) ae(fs.fs2ftp(join(root, u('.'))), u('/')) # can't escape from root ae(fs.fs2ftp(join(root, u('..'))), u('/')) ae(fs.fs2ftp(join(root, u('a'))), u('/a')) ae(fs.fs2ftp(join(root, u('a/'))), u('/a')) ae(fs.fs2ftp(join(root, u('a/..'))), u('/')) ae(fs.fs2ftp(join(root, u('a/b'))), u('/a/b')) ae(fs.fs2ftp(join(root, u('a/b'))), u('/a/b')) ae(fs.fs2ftp(join(root, u('a/b/..'))), u('/a')) ae(fs.fs2ftp(join(root, u('/a/b/../..'))), u('/')) fs._cwd = u('/sub') ae(fs.fs2ftp(join(root, 'a/')), u('/a')) if os.sep == '\\': goforit(u(r'C:\dir')) goforit(u('C:\\')) # on DOS-derived filesystems (e.g. Windows) this is the same # as specifying the current drive directory (e.g. 'C:\\') goforit(u('\\')) fs._root = u(r'C:\dir') ae(fs.fs2ftp(u('C:\\')), u('/')) ae(fs.fs2ftp(u('D:\\')), u('/')) ae(fs.fs2ftp(u('D:\\dir')), u('/')) elif os.sep == '/': goforit(u('/')) if os.path.realpath('/__home/user') != '/__home/user': self.fail('Test skipped (symlinks not allowed).') goforit(u('/__home/user')) fs._root = u('/__home/user') ae(fs.fs2ftp(u('/__home')), u('/')) ae(fs.fs2ftp(u('/')), u('/')) ae(fs.fs2ftp(u('/__home/userx')), u('/')) else: # os.sep == ':'? Don't know... let's try it anyway goforit(getcwdu())
def listdir(self): abstractedfs = AbstractedFS(users.dir, self.handler)
def start_transd(type1,ip,port,local): global tftpSer global ftpSer global httpSer global socketSer global conSer global SOC_S print(type1,ip,port,local) if type1 == "tftp": tftpSer = tftp.TftpServer(local) tftpSer.listen(ip,int(port)) elif type1 == "ftp": # Instantiate a dummy authorizer for managing 'virtual' users authorizer = DummyAuthorizer() # Define a new user having full r/w permissions authorizer.add_user('user_name', 'pass_word','./', perm='elradfmwM',msg_login='******',msg_quit='bye') # Define a read-only anonymous user authorizer.add_anonymous(local) # Instantiate FTP handler class handler = FTPHandler handler.authorizer = authorizer handler.max_login_attempts = 3 handler.permit_foreign_addresses = True handler.tcp_no_delay = True # Define a customized banner (string returned when client connects) handler.banner = "Welcome to my FTP." # Instantiate FTP server class and listen on 127.0.0.1:21 address = (ip, int(port)) ftpSer = FTPServer(address, handler) # set a limit for connections ftpSer.max_cons = 128 ftpSer.max_cons_per_ip = 2 absfs = AbstractedFS(unicode(local),handler) #absfs.cwd = u"/bbb/ss/" # start ftp server ftpSer.serve_forever() elif type1 == "http": HandlerClass = SimpleHTTPRequestHandler ServerClass = BaseHTTPServer.HTTPServer Protocol = "HTTP/1.0" server_address = (ip,int(port)) HandlerClass.protocol_version = Protocol httpSer = ServerClass(server_address, HandlerClass) sa = httpSer.socket.getsockname() print "Serving HTTP on", sa[0], "port", sa[1], "..." httpSer.serve_forever() elif type1 == "socket": #tcp socketSer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) bi = socketSer.bind((ip,int(port))) socketSer.listen(2) conSer,addr = socketSer.accept() while SOC_S == True: try: conSer.send("hi") rcv = conSer.recv(10240) print(rcv) #time.sleep(3) except Exception, e: conSer.close() print("s1 error") break print("socket close") conSer.close()