示例#1
0
    def _init_fs(self):
        # Create/register all necessary users and groups in the file system
        _ = {conpot_core.get_vfs().register_user(uid=k, name=v['uname']) for k, v in self.user_db.items()}
        _ = {conpot_core.get_vfs().create_group(gid=k, name=v['group']) for k, v in self.grp_db.items()}
        _ = {conpot_core.get_vfs().add_users_to_group(gid=k, uids=list(v['users'])) for k, v in self.grp_db.items()}
        # Initialize file system
        self.vfs, self.data_fs = conpot_core.add_protocol(protocol_name='ftp',
                                                          data_fs_subdir=self.data_fs_subdir,
                                                          vfs_dst_path=self.root_path,
                                                          src_path=self.add_src,
                                                          owner_uid=self.default_owner,
                                                          group_gid=self.default_group,
                                                          perms=self.default_perms)
        if self.add_src:
            logger.info('FTP Serving File System from {} at {} in vfs. FTP data_fs sub directory: {}'.format(
                self.add_src, self.root_path, self.data_fs._sub_dir
            ))
        else:
            logger.info('FTP Serving File System at {} in vfs. FTP data_fs sub directory: {}'.format(
                self.root_path, self.data_fs._sub_dir
            ))
        logger.debug('FTP serving list of files : {}'.format(', '.join(self.vfs.listdir('.'))))
        self.root = '/'  # Setup root dir.
        # check for permissions etc.
        logger.debug("FTP root {} is a directory".format(self.vfs.getcwd() + self.root))
        if self.vfs.access(self.root, 0, R_OK):
            logger.debug("FTP root {} is readable".format(self.vfs.getcwd() + self.root))
        else:
            raise FTPException("FTP root must be readable")
        if self.vfs.access(self.root, 0, W_OK):
            logger.debug("FTP root {} is writable".format(self.vfs.getcwd() + self.root))
        else:
            logger.warning("FTP root {} is not writable".format(self.vfs.getcwd() + self.root))
        # Finally apply permissions to specific files.
        for _file in self._custom_files:
            _path = _file.attrib['path']
            _path = _path.replace(self.root_path, self.root)
            _owner = int(_file.xpath('./owner_uid/text()')[0])
            _perms = oct(int(_file.xpath('./perms/text()')[0], 8))
            _accessed = datetime.fromtimestamp(float(_file.xpath('./last_accessed/text()')[0]))
            _modified = datetime.fromtimestamp(float(_file.xpath('./last_modified/text()')[0]))
            self.vfs.chown(_path, _owner, self.default_group)
            self.vfs.chmod(_path, _perms)
            _fs = self.vfs.delegate_fs().delegate_fs()
            _fs.settimes(self.vfs.delegate_path(_path)[1], _accessed, _modified)

        for _dir in self._custom_dirs:
            _path = _dir.attrib['path']
            _recursive = bool(_dir.attrib['recursive'])
            _path = _path.replace(self.root_path, self.root)
            _owner = int(_dir.xpath('./owner_uid/text()')[0])
            _perms = oct(int(_dir.xpath('./perms/text()')[0], 8))
            _accessed = datetime.fromtimestamp(float(_dir.xpath('./last_accessed/text()')[0]))
            _modified = datetime.fromtimestamp(float(_dir.xpath('./last_modified/text()')[0]))
            self.vfs.chown(_path, _owner, self.default_group, _recursive)
            self.vfs.chmod(_path, _perms)
            _fs = self.vfs.delegate_fs().delegate_fs()
            _fs.settimes(self.vfs.delegate_path(_path)[1], _accessed, _modified)
示例#2
0
 def test_list(self):
     # TODO: check for a user who does not have permissions to do list!
     self.client.connect(host="127.0.0.1",
                         port=self.ftp_server.server.server_port)
     self.client.login(user="******", passwd="nobody")
     _vfs, _ = conpot_core.get_vfs("ftp")
     _vfs.settimes("ftp_data.txt",
                   accessed=datetime.now(),
                   modified=datetime.now())
     # Do a list of directory for passive mode
     _pasv_list = list()
     self.client.retrlines("LIST", _pasv_list.append)
     # note that this time is set in ftp_server settimes method. Picked up from the default template.
     self.assertEqual(
         [
             "-rwxrwxrwx   1 nobody   ftp            49 Jul 15 17:51 ftp_data.txt"
         ],
         _pasv_list,
     )
     # check list for active mode
     _actv_list = list()
     self.client.set_pasv(False)
     self.client.retrlines("LIST", _actv_list.append)
     # note that this time is set in ftp_server settimes method. Picked up from the default template.
     self.assertEqual(
         [
             "-rwxrwxrwx   1 nobody   ftp            49 Jul 15 17:51 ftp_data.txt"
         ],
         _actv_list,
     )
示例#3
0
 def test_cwd(self):
     #  TODO: test for a user who does not has permissions to change directory
     _vfs, _ = conpot_core.get_vfs("ftp")
     self.client.connect(host="127.0.0.1",
                         port=self.ftp_server.server.server_port)
     self.client.login(user="******", passwd="nobody")
     # create a directory to cwd to.
     _vfs.makedir("testing")
     self.assertEqual(
         self.client.sendcmd("cwd testing"),
         '250 "/testing" is the current directory.',
     )
     # check consistency with pwd
     self.assertEqual(self.client.sendcmd("pwd"),
                      '257 "/testing" is the current directory.')
     # test for cdup.
     self.assertEqual(self.client.sendcmd("cdup"),
                      '250 "/" is the current directory.')
     # make sure that user does not go - out of the root path.
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 'cwd ../' points to a path which is outside the user's "
         "root directory.",
         self.client.sendcmd,
         "cwd ../",
     )
     _vfs.removedir("testing")
 def test_file_rename(self):
     # TODO: check for a user who does not have permissions to rename a file!
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.client.connect(host='127.0.0.1',
                         port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # First we would do everything for a valid file and all valid params
     # check with invalid rnfr params
     self.assertRaisesRegex(ftplib.error_perm,
                            "550 Can't rename home directory.",
                            self.client.sendcmd, 'rnfr /')
     self.assertRaisesRegex(ftplib.error_perm,
                            '550 No such file or directory.',
                            self.client.sendcmd, 'rnfr file_DNE')
     self.assertRaisesRegex(
         ftplib.error_perm, "503 Bad sequence of commands: use RNFR first.",
         self.client.sendcmd, 'rnto /random_path')
     # create a custom file to play with.
     try:
         # do a rnfr to rename file ftp_data.txt
         with _vfs.open('/test_rename_file.txt', mode='w') as _test:
             _test.write(
                 'This is just a test file for rename testing of FTP server'
             )
         self.assertEqual(self.client.sendcmd('rnfr test_rename_file.txt'),
                          '350 Ready for destination name.')
         self.assertEqual(self.client.sendcmd('rnto new_data.txt'),
                          '250 Renaming ok.')
         # try for a case that would fail --
         # fixme: tests fail after trying to rename files once they have been renamed.
         # self.assertEqual(self.client.sendcmd('rnfr new_data.txt'), '350 Ready for destination name.')
         # self.assertRaisesRegex(ftplib.error_perm, '501 can\'t decode command.', self.client.sendcmd,
         #                        'rnto Very / Unsafe / file\nname hähä \n\r .txt')
     finally:
         _vfs.remove('new_data.txt')
示例#5
0
 def test_rmd(self):
     _vfs, _ = conpot_core.get_vfs("ftp")
     self.client.connect(host="127.0.0.1",
                         port=self.ftp_server.server.server_port)
     self.client.login(user="******", passwd="nobody")
     # let us create a temp dir for deleting
     _vfs.makedir("tmp")
     self.assertEqual(self.client.sendcmd("rmd tmp"),
                      "250 Directory removed.")
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 Remove directory operation failed.",
         self.client.sendcmd,
         "rmd tmp",
     )
     # TODO: Test with user that has no or little permissions.
     # test for a user trying to delete '/'
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 Can't remove root directory.",
         self.client.sendcmd,
         "rmd /",
     )
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 'rmd ../../' points to a path which is outside the user's root directory.",
         self.client.sendcmd,
         "rmd ../../",
     )
示例#6
0
 def test_file_rename(self):
     # TODO: check for a user who does not have permissions to rename a file!
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # First we would do everything for a valid file and all valid params
     # check with invalid rnfr params
     self.assertRaisesRegex(ftplib.error_perm, "550 Can't rename home directory.", self.client.sendcmd, 'rnfr /')
     self.assertRaisesRegex(ftplib.error_perm, '550 No such file or directory.', self.client.sendcmd,
                            'rnfr file_DNE')
     self.assertRaisesRegex(ftplib.error_perm, "503 Bad sequence of commands: use RNFR first.", self.client.sendcmd,
                            'rnto /random_path')
     # create a custom file to play with.
     try:
         # do a rnfr to rename file ftp_data.txt
         with _vfs.open('/test_rename_file.txt', mode='w') as _test:
             _test.write('This is just a test file for rename testing of FTP server')
         self.assertEqual(self.client.sendcmd('rnfr test_rename_file.txt'), '350 Ready for destination name.')
         self.assertEqual(self.client.sendcmd('rnto new_data.txt'), '250 Renaming ok.')
         # try for a case that would fail --
         # fixme: tests fail after trying to rename files once they have been renamed.
         # self.assertEqual(self.client.sendcmd('rnfr new_data.txt'), '350 Ready for destination name.')
         # self.assertRaisesRegex(ftplib.error_perm, '501 can\'t decode command.', self.client.sendcmd,
         #                        'rnto Very / Unsafe / file\nname hähä \n\r .txt')
     finally:
         _vfs.remove('new_data.txt')
示例#7
0
 def test_site_chmod(self):
     # TODO: check for a user who does not have permissions to do chmod!
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # change permissions
     self.client.sendcmd('site chmod 644 ftp_data.txt')
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.assertEqual(_vfs.get_permissions('ftp_data.txt'), 'rw-r--r--')
示例#8
0
    def setUp(self):
        conpot_core.initialize_vfs()

        self.ftp_server, self.greenlet = spawn_test_server(
            FTPServer, "default", "ftp")
        self.client = ftplib.FTP()

        self.vfs, self.data_fs = conpot_core.get_vfs("ftp")
示例#9
0
 def test_site_chmod(self):
     # TODO: check for a user who does not have permissions to do chmod!
     self.client.connect(host="127.0.0.1",
                         port=self.ftp_server.server.server_port)
     self.client.login(user="******", passwd="nobody")
     # change permissions
     self.client.sendcmd("site chmod 644 ftp_data.txt")
     _vfs, _ = conpot_core.get_vfs("ftp")
     self.assertEqual(_vfs.get_permissions("ftp_data.txt"), "rw-r--r--")
示例#10
0
 def test_mkdir_upload(self):
     """Testing TFTP upload files - while recursively making directories as per the TFTP path."""
     client = tftpy.TftpClient('127.0.0.1', self.tftp_server.server.server_port)
     client.upload('/dir/dir/test.txt', self._test_file)
     gevent.sleep(3)
     _, _data_fs = conpot_core.get_vfs('tftp')
     [_file] = [i for i in _data_fs.listdir('./') if '2018-07-15 17:51:17-test-txt' in i]
     self.assertEqual(_data_fs.gettext(_file), 'This is just a test file for Conpot\'s TFTP server\n')
     _data_fs.remove(_file)
示例#11
0
 def test_tftp_download(self):
     _dst_path = "/".join(conpot.__path__ +
                          ["tests/data/data_temp_fs/tftp/download"])
     try:
         self.client.download("tftp_data.txt", _dst_path)
         self.assertTrue(filecmp.cmp(_dst_path, self._test_file))
     finally:
         _, _data_fs = conpot_core.get_vfs("tftp")
         _data_fs.remove("download")
示例#12
0
 def test_site_chmod(self):
     # TODO: check for a user who does not have permissions to do chmod!
     self.client.connect(host='127.0.0.1',
                         port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # change permissions
     self.client.sendcmd('site chmod 644 ftp_data.txt')
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.assertEqual(_vfs.get_permissions('ftp_data.txt'), 'rw-r--r--')
示例#13
0
 def test_mdtm(self):
     # TODO : test for user that does not have permissions for mdtm
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     _vfs, _ = conpot_core.get_vfs('ftp')
     _vfs.settimes('ftp_data.txt', accessed=datetime.now(), modified=datetime.now())
     # test for a file that already exists
     self.assertEqual(self.client.sendcmd('mdtm ftp_data.txt'), '213 20180715175117')
     self.assertRaisesRegex(ftplib.error_perm, "550 /this_file_does_not_exist.txt is not retrievable",
                            self.client.sendcmd, 'mdtm this_file_does_not_exist.txt')
示例#14
0
 def test_tftp_download(self):
     _dst_path = '/'.join(conpot.__path__ + ['tests/data/data_temp_fs/tftp/download'])
     client = tftpy.TftpClient('127.0.0.1', self.tftp_server.server.server_port)
     try:
         client.download('tftp_data.txt', _dst_path)
         gevent.sleep(3)
         self.assertTrue(filecmp.cmp(_dst_path, self._test_file))
     finally:
         _, _data_fs = conpot_core.get_vfs('tftp')
         _data_fs.remove('download')
示例#15
0
 def setUp(self):
     conpot_core.initialize_vfs()
     self._vfs = conpot_core.get_vfs()
     self._vfs.register_user('test_user', 13)
     self._vfs.create_group('test_grp', 13)
     self.test_vfs = self._vfs.mount_fs(
         fs_url='/'.join(conpot.__path__ + ['tests/data/test_data_fs/vfs']),
         dst_path='/data',
         owner_uid=13,
         group_gid=13,
         perms=0o750)
示例#16
0
 def setUp(self):
     conpot_core.initialize_vfs()
     self._vfs = conpot_core.get_vfs()
     self._vfs.register_user('test_user', 13)
     self._vfs.create_group('test_grp', 13)
     self.test_vfs = self._vfs.mount_fs(
         fs_url='/'.join(conpot.__path__ + ['tests/data/test_data_fs/vfs']),
         dst_path='/data',
         owner_uid=13,
         group_gid=13,
         perms=0o750
     )
示例#17
0
 def setUp(self):
     conpot_core.initialize_vfs()
     self._vfs = conpot_core.get_vfs()
     self._vfs.register_user("test_user", 13)
     self._vfs.create_group("test_grp", 13)
     self.test_vfs = self._vfs.mount_fs(
         fs_url="/".join(conpot.__path__ + ["tests/data/test_data_fs/vfs"]),
         dst_path="/data",
         owner_uid=13,
         group_gid=13,
         perms=0o750,
     )
示例#18
0
 def test_dele(self):
     # TODO: check for a user who does not have permissions to delete a file!
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # let us create a temp file just for deleting.
     with _vfs.open('/temp_file', mode='w') as _tmp:
         _tmp.write('This is just a temp file for testing rm')
     # delete that file
     self.assertEqual(self.client.sendcmd('dele temp_file'), '250 File removed.')
     # check for errors
     self.assertRaisesRegex(ftplib.error_perm, '550 Failed to delete file.', self.client.sendcmd, 'dele temp_file')
示例#19
0
 def test_tftp_download(self):
     _dst_path = "/".join(conpot.__path__ +
                          ["tests/data/data_temp_fs/tftp/download"])
     client = tftpy.TftpClient("127.0.0.1",
                               self.tftp_server.server.server_port)
     try:
         client.download("tftp_data.txt", _dst_path)
         gevent.sleep(3)
         self.assertTrue(filecmp.cmp(_dst_path, self._test_file))
     finally:
         _, _data_fs = conpot_core.get_vfs("tftp")
         _data_fs.remove("download")
示例#20
0
 def test_stor(self):
     # let us test by uploading a file called ftp_testing.txt
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     _path = os.path.join(''.join(conpot.__path__), 'tests', 'data', 'test_data_fs', 'ftp')
     with open(_path + '/ftp_testing.txt', mode='rb') as _file:
         self.client.storbinary("stor ftp_testing_stor.txt", _file)
     self.assertIn('ftp_testing_stor.txt', self.ftp_server.handler.config.vfs.listdir('/'))
     _vfs, _data_fs = conpot_core.get_vfs('ftp')
     _vfs.remove('ftp_testing_stor.txt')
     _data_fs_file = sanitize_file_name('ftp_testing_stor.txt', self.client.sock.getsockname()[0],
                                        self.client.sock.getsockname()[1])
     _data_fs.remove(_data_fs_file)
示例#21
0
 def test_mkdir_upload(self):
     """Testing TFTP upload files - while recursively making directories as per the TFTP path."""
     self.client.upload("/dir/dir/test.txt", self._test_file)
     _, _data_fs = conpot_core.get_vfs("tftp")
     [_file] = [
         i for i in _data_fs.listdir("./")
         if "2018-07-15 17:51:17-test-txt" in i
     ]
     self.assertEqual(
         _data_fs.gettext(_file),
         "This is just a test file for Conpot's TFTP server\n",
     )
     _data_fs.remove(_file)
示例#22
0
 def test_tftp_upload(self):
     """Testing TFTP upload files. """
     self.client.upload("test.txt", self._test_file)
     _, _data_fs = conpot_core.get_vfs("tftp")
     [_file] = [
         i for i in _data_fs.listdir("./")
         if "2018-07-15 17:51:17-test-txt" in i
     ]
     self.assertEqual(
         _data_fs.gettext(_file),
         "This is just a test file for Conpot's TFTP server\n",
     )
     _data_fs.remove(_file)
示例#23
0
 def test_mkd(self):
     # TODO: test for a user who does not has permissions to make directory
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     self.assertEqual(self.client.sendcmd('mkd testing'), '257 "/testing" directory created.')
     self.assertRaisesRegex(ftplib.error_perm,
                            "550 'mkd /../../testing/testing' points to a path which is "
                            "outside the user's root directory.", self.client.sendcmd, 'mkd /../../testing/testing')
     _ = self.client.sendcmd('mkd testing/testing')
     self.assertEqual(self.client.sendcmd('mkd testing/testing/../demo'),
                      '257 "/testing/demo" directory created.')
     _vfs, _ = conpot_core.get_vfs('ftp')
     _vfs.removedir('testing/testing')
     _vfs.removedir('testing/demo')
     _vfs.removedir('testing')
示例#24
0
 def test_dele(self):
     # TODO: check for a user who does not have permissions to delete a file!
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.client.connect(host='127.0.0.1',
                         port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # let us create a temp file just for deleting.
     with _vfs.open('/temp_file', mode='w') as _tmp:
         _tmp.write('This is just a temp file for testing rm')
     # delete that file
     self.assertEqual(self.client.sendcmd('dele temp_file'),
                      '250 File removed.')
     # check for errors
     self.assertRaisesRegex(ftplib.error_perm, '550 Failed to delete file.',
                            self.client.sendcmd, 'dele temp_file')
示例#25
0
 def test_rmd(self):
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # let us create a temp dir for deleting
     _vfs.makedir('tmp')
     self.assertEqual(self.client.sendcmd('rmd tmp'), '250 Directory removed.')
     self.assertRaisesRegex(ftplib.error_perm, '550 Remove directory operation failed.', self.client.sendcmd,
                            'rmd tmp')
     # TODO: Test with user that has no or little permissions.
     # test for a user trying to delete '/'
     self.assertRaisesRegex(ftplib.error_perm, '550 Can\'t remove root directory.', self.client.sendcmd,
                            'rmd /')
     self.assertRaisesRegex(ftplib.error_perm,
                            "550 'rmd ../../' points to a path which is outside the user's root directory.",
                            self.client.sendcmd, 'rmd ../../')
示例#26
0
 def test_cwd(self):
     #  TODO: test for a user who does not has permissions to change directory
     _vfs, _ = conpot_core.get_vfs('ftp')
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     # create a directory to cwd to.
     _vfs.makedir('testing')
     self.assertEqual(self.client.sendcmd('cwd testing'), '250 "/testing" is the current directory.')
     # check consistency with pwd
     self.assertEqual(self.client.sendcmd('pwd'), '257 "/testing" is the current directory.')
     # test for cdup.
     self.assertEqual(self.client.sendcmd('cdup'), '250 "/" is the current directory.')
     # make sure that user does not go - out of the root path.
     self.assertRaisesRegex(ftplib.error_perm, "550 'cwd ../' points to a path which is outside the user's "
                                               "root directory.", self.client.sendcmd, 'cwd ../')
     _vfs.removedir('testing')
示例#27
0
 def test_mdtm(self):
     # TODO : test for user that does not have permissions for mdtm
     self.client.connect(host='127.0.0.1',
                         port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     _vfs, _ = conpot_core.get_vfs('ftp')
     _vfs.settimes('ftp_data.txt',
                   accessed=datetime.now(),
                   modified=datetime.now())
     # test for a file that already exists
     self.assertEqual(self.client.sendcmd('mdtm ftp_data.txt'),
                      '213 20180715175117')
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 /this_file_does_not_exist.txt is not retrievable",
         self.client.sendcmd, 'mdtm this_file_does_not_exist.txt')
示例#28
0
 def test_stor(self):
     # let us test by uploading a file called ftp_testing.txt
     self.client.connect(host='127.0.0.1',
                         port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     _path = os.path.join(''.join(conpot.__path__), 'tests', 'data',
                          'test_data_fs', 'ftp')
     with open(_path + '/ftp_testing.txt', mode='rb') as _file:
         self.client.storbinary("stor ftp_testing_stor.txt", _file)
     self.assertIn('ftp_testing_stor.txt',
                   self.ftp_server.handler.config.vfs.listdir('/'))
     _vfs, _data_fs = conpot_core.get_vfs('ftp')
     _vfs.remove('ftp_testing_stor.txt')
     _data_fs_file = sanitize_file_name('ftp_testing_stor.txt',
                                        self.client.sock.getsockname()[0],
                                        self.client.sock.getsockname()[1])
     _data_fs.remove(_data_fs_file)
示例#29
0
 def test_list(self):
     # TODO: check for a user who does not have permissions to do list!
     self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     _vfs, _ = conpot_core.get_vfs('ftp')
     _vfs.settimes('ftp_data.txt', accessed=datetime.now(), modified=datetime.now())
     # Do a list of directory for passive mode
     _pasv_list = list()
     self.client.retrlines('LIST', _pasv_list.append)
     # note that this time is set in ftp_server settimes method. Picked up from the default template.
     self.assertEqual(['-rwxrwxrwx   1 nobody   ftp            49 Jul 15 17:51 ftp_data.txt'], _pasv_list)
     # check list for active mode
     _actv_list = list()
     self.client.set_pasv(False)
     self.client.retrlines('LIST', _actv_list.append)
     # note that this time is set in ftp_server settimes method. Picked up from the default template.
     self.assertEqual(['-rwxrwxrwx   1 nobody   ftp            49 Jul 15 17:51 ftp_data.txt'], _actv_list)
示例#30
0
 def test_stor(self):
     # let us test by uploading a file called ftp_testing.txt
     self.client.connect(host="127.0.0.1",
                         port=self.ftp_server.server.server_port)
     self.client.login(user="******", passwd="nobody")
     _path = os.path.join("".join(conpot.__path__), "tests", "data",
                          "test_data_fs", "ftp")
     with open(_path + "/ftp_testing.txt", mode="rb") as _file:
         self.client.storbinary("stor ftp_testing_stor.txt", _file)
     self.assertIn("ftp_testing_stor.txt",
                   self.ftp_server.handler.config.vfs.listdir("/"))
     _vfs, _data_fs = conpot_core.get_vfs("ftp")
     _vfs.remove("ftp_testing_stor.txt")
     _data_fs_file = sanitize_file_name(
         "ftp_testing_stor.txt",
         self.client.sock.getsockname()[0],
         self.client.sock.getsockname()[1],
     )
     _data_fs.remove(_data_fs_file)
示例#31
0
 def test_dele(self):
     # TODO: check for a user who does not have permissions to delete a file!
     _vfs, _ = conpot_core.get_vfs("ftp")
     self.client.connect(host="127.0.0.1",
                         port=self.ftp_server.server.server_port)
     self.client.login(user="******", passwd="nobody")
     # let us create a temp file just for deleting.
     with _vfs.open("/temp_file", mode="w") as _tmp:
         _tmp.write("This is just a temp file for testing rm")
     # delete that file
     self.assertEqual(self.client.sendcmd("dele temp_file"),
                      "250 File removed.")
     # check for errors
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 Failed to delete file.",
         self.client.sendcmd,
         "dele temp_file",
     )
示例#32
0
 def test_mkd(self):
     # TODO: test for a user who does not has permissions to make directory
     self.client.connect(host='127.0.0.1',
                         port=self.ftp_server.server.server_port)
     self.client.login(user='******', passwd='nobody')
     self.assertEqual(self.client.sendcmd('mkd testing'),
                      '257 "/testing" directory created.')
     self.assertRaisesRegex(
         ftplib.error_perm,
         "550 'mkd /../../testing/testing' points to a path which is "
         "outside the user's root directory.", self.client.sendcmd,
         'mkd /../../testing/testing')
     _ = self.client.sendcmd('mkd testing/testing')
     self.assertEqual(self.client.sendcmd('mkd testing/testing/../demo'),
                      '257 "/testing/demo" directory created.')
     _vfs, _ = conpot_core.get_vfs('ftp')
     _vfs.removedir('testing/testing')
     _vfs.removedir('testing/demo')
     _vfs.removedir('testing')
示例#33
0
 def test_appe(self):
     _data_1 = 'This is just a test!\n'
     _data_2 = 'This is another test\n'
     _vfs, _ = conpot_core.get_vfs('ftp')
     with _vfs.open('ftp_appe_test.txt', mode='w') as _file:
         _file.write(_data_1)
     try:
         self.client.connect(host='127.0.0.1', port=self.ftp_server.server.server_port)
         self.client.login(user='******', passwd='nobody')
         _path = os.path.join(''.join(conpot.__path__), 'tests', 'data', 'data_temp_fs', 'ftp')
         with open(_path + '/ftp_appe.txt', mode='w+') as _file:
             _file.write(_data_2)
         with open(_path + '/ftp_appe.txt', mode='rb+') as _file:
             self.client.storbinary("appe ftp_appe_test.txt", _file)
         _buffer = ''
         with _vfs.open('ftp_appe_test.txt', mode='r') as _file:
             _buffer += _file.read()
         self.assertEqual(_buffer, _data_1 + _data_2)
     finally:
         _vfs.remove('ftp_appe_test.txt')
示例#34
0
 def test_appe(self):
     _data_1 = 'This is just a test!\n'
     _data_2 = 'This is another test\n'
     _vfs, _ = conpot_core.get_vfs('ftp')
     with _vfs.open('ftp_appe_test.txt', mode='w') as _file:
         _file.write(_data_1)
     try:
         self.client.connect(host='127.0.0.1',
                             port=self.ftp_server.server.server_port)
         self.client.login(user='******', passwd='nobody')
         _path = os.path.join(''.join(conpot.__path__), 'tests', 'data',
                              'data_temp_fs', 'ftp')
         with open(_path + '/ftp_appe.txt', mode='w+') as _file:
             _file.write(_data_2)
         with open(_path + '/ftp_appe.txt', mode='rb+') as _file:
             self.client.storbinary("appe ftp_appe_test.txt", _file)
         _buffer = ''
         with _vfs.open('ftp_appe_test.txt', mode='r') as _file:
             _buffer += _file.read()
         self.assertEqual(_buffer, _data_1 + _data_2)
     finally:
         _vfs.remove('ftp_appe_test.txt')
示例#35
0
 def test_appe(self):
     _data_1 = "This is just a test!\n"
     _data_2 = "This is another test\n"
     _vfs, _ = conpot_core.get_vfs("ftp")
     with _vfs.open("ftp_appe_test.txt", mode="w") as _file:
         _file.write(_data_1)
     try:
         self.client.connect(host="127.0.0.1",
                             port=self.ftp_server.server.server_port)
         self.client.login(user="******", passwd="nobody")
         _path = os.path.join("".join(conpot.__path__), "tests", "data",
                              "data_temp_fs", "ftp")
         with open(_path + "/ftp_appe.txt", mode="w+") as _file:
             _file.write(_data_2)
         with open(_path + "/ftp_appe.txt", mode="rb+") as _file:
             self.client.storbinary("appe ftp_appe_test.txt", _file)
         _buffer = ""
         with _vfs.open("ftp_appe_test.txt", mode="r") as _file:
             _buffer += _file.read()
         self.assertEqual(_buffer, _data_1 + _data_2)
     finally:
         _vfs.remove("ftp_appe_test.txt")
示例#36
0
 def setUp(self):
     conpot_core.initialize_vfs()
     self.test_vfs = conpot_core.get_vfs()
示例#37
0
    def _init_fs(self):
        # Create/register all necessary users and groups in the file system
        _ = {
            conpot_core.get_vfs().register_user(uid=k, name=v["uname"])
            for k, v in self.user_db.items()
        }
        _ = {
            conpot_core.get_vfs().create_group(gid=k, name=v["group"])
            for k, v in self.grp_db.items()
        }
        _ = {
            conpot_core.get_vfs().add_users_to_group(gid=k, uids=list(v["users"]))
            for k, v in self.grp_db.items()
        }
        # Initialize file system
        self.vfs, self.data_fs = conpot_core.add_protocol(
            protocol_name="ftp",
            data_fs_subdir=self.data_fs_subdir,
            vfs_dst_path=self.root_path,
            src_path=self.add_src,
            owner_uid=self.default_owner,
            group_gid=self.default_group,
            perms=self.default_perms,
        )
        if self.add_src:
            logger.info(
                "FTP Serving File System from {} at {} in vfs. FTP data_fs sub directory: {}".format(
                    self.add_src, self.root_path, self.data_fs._sub_dir
                )
            )
        else:
            logger.info(
                "FTP Serving File System at {} in vfs. FTP data_fs sub directory: {}".format(
                    self.root_path, self.data_fs._sub_dir
                )
            )
        logger.debug(
            "FTP serving list of files : {}".format(", ".join(self.vfs.listdir(".")))
        )
        self.root = "/"  # Setup root dir.
        # check for permissions etc.
        logger.debug("FTP root {} is a directory".format(self.vfs.getcwd() + self.root))
        if self.vfs.access(self.root, 0, R_OK):
            logger.debug(
                "FTP root {} is readable".format(self.vfs.getcwd() + self.root)
            )
        else:
            raise FTPException("FTP root must be readable")
        if self.vfs.access(self.root, 0, W_OK):
            logger.debug(
                "FTP root {} is writable".format(self.vfs.getcwd() + self.root)
            )
        else:
            logger.warning(
                "FTP root {} is not writable".format(self.vfs.getcwd() + self.root)
            )
        # Finally apply permissions to specific files.
        for _file in self._custom_files:
            _path = _file.attrib["path"]
            _path = _path.replace(self.root_path, self.root)
            _owner = int(_file.xpath("./owner_uid/text()")[0])
            _perms = oct(int(_file.xpath("./perms/text()")[0], 8))
            _accessed = datetime.fromtimestamp(
                float(_file.xpath("./last_accessed/text()")[0])
            )
            _modified = datetime.fromtimestamp(
                float(_file.xpath("./last_modified/text()")[0])
            )
            self.vfs.chown(_path, _owner, self.default_group)
            self.vfs.chmod(_path, _perms)
            _fs = self.vfs.delegate_fs().delegate_fs()
            _fs.settimes(self.vfs.delegate_path(_path)[1], _accessed, _modified)

        for _dir in self._custom_dirs:
            _path = _dir.attrib["path"]
            _recursive = bool(_dir.attrib["recursive"])
            _path = _path.replace(self.root_path, self.root)
            _owner = int(_dir.xpath("./owner_uid/text()")[0])
            _perms = oct(int(_dir.xpath("./perms/text()")[0], 8))
            _accessed = datetime.fromtimestamp(
                float(_dir.xpath("./last_accessed/text()")[0])
            )
            _modified = datetime.fromtimestamp(
                float(_dir.xpath("./last_modified/text()")[0])
            )
            self.vfs.chown(_path, _owner, self.default_group, _recursive)
            self.vfs.chmod(_path, _perms)
            _fs = self.vfs.delegate_fs().delegate_fs()
            _fs.settimes(self.vfs.delegate_path(_path)[1], _accessed, _modified)
示例#38
0
 def setUp(self):
     conpot_core.initialize_vfs()
     self.test_vfs = conpot_core.get_vfs()