示例#1
0
 def drop(self):
     """Clear system info and delete all info of cowry."""
     # check if system been droped
     # Check that the system has been deleted
     if self.settings.default.inited == '0':
         self.log.info('the system has been deleted'.title())
         utils.deleteFile(utils.getenv('COWRY_CONFIG'))
         exit()
     self.log.info('Start drop cowry system !!! ')
     # delete uploaded files
     if utils.checkFolderExists(self.settings.storage.datapath):
         utils.delfolder(self.settings.storage.datapath)
     # delete server certificates
     if utils.checkFileExists(self.settings.certificates.certificate
                              ) or utils.checkFileExists(
                                  self.settings.certificates.privatekey):
         utils.deleteFile(self.settings.certificates.certificate)
         utils.deleteFile(self.settings.certificates.privatekey)
     utils.delfolder(
         utils.getDirNameByPath(self.settings.certificates.privatekey))
     # delete all database tables
     self.init_db()
     self.db.drop()
     # delete all database file if use sqlite
     if self.settings.database.df:
         utils.deleteFile(self.settings.database.df)
         utils.delfolder(utils.getDirNameByPath(self.settings.database.df))
     # delete configure file
     utils.deleteFile(utils.getenv('COWRY_CONFIG'))
     self.log.info('Finished drop cowry system')
     exit()
示例#2
0
 def init_ssl(self):
     self.ssl = SSLCertSetting()
     # check if the certificate exists, if not, sysytem should be auto generate a new certificate
     if not utils.checkFileExists(
             self.settings.certificates.privatekey
     ) and not utils.checkFileExists(
             self.settings.certificates.certificate):
         # generate new certificate
         # check if settings have bind doamin
         if utils.verifyDomain(self.settings.server.bind_domain):
             # a server domain is valid in configure file
             # the server will use this domain to generate a new certificate
             self.ssl.create_self_signed_cert(
                 self.settings.server.bind_domain)
         elif self.settings.server.bind_address:
             self.ssl.create_self_signed_cert(self.settings.certificates.cn)
         else:
             self.log.error(
                 "can't read valid domain or IP adress from configure file, create a new certificate need it"
             )
         # set key own
         os.chmod(path=self.settings.certificates.privatekey,
                  mode=stat.S_IRUSR)
     # start load certificate
     # verify certificates
     if self.ssl.validate_ssl_cert():
         self.sslContext = ssl.create_default_context(
             ssl.Purpose.CLIENT_AUTH)
         self.log.info('start load certificates: {}'.format(
             self.settings.certificates.certificate))
         self.sslContext.load_cert_chain(
             certfile=self.settings.certificates.certificate,
             keyfile=self.settings.certificates.privatekey)
     else:
         exit()
示例#3
0
    def init_configure(self):
        """Pass."""
        # self.log.info('start init configure file')
        if not utils.checkFileExists(utils.getenv('COWRY_CONFIG')):
            src = utils.joinFilePath(utils.getenv('COWRY_ROOT'), 'cowry.conf.default')
            dst = utils.joinFilePath(utils.getenv('COWRY_ROOT'), 'cowry.conf')
            utils.copyfile(src, dst)
            print('Not find default configure file, copy default configure to use')

        self.settings = Settings()

        # set default certificates folders values
        if not self.settings.certificates.certdirs:
            setDefaultCertDirs = utils.joinFilePath(utils.getenv('COWRY_ROOT'), 'certs')
            self.settings._set(('certificates', 'certdirs', setDefaultCertDirs))
示例#4
0
def main():
    cmd = 'start'

    if args.config:
        print('read config from :', args.config)
        defaultConfigPath = args.config
        if utils.checkFileExists(defaultConfigPath):
            utils.setenv('COWRY_CONFIG', defaultConfigPath)
    else:
        currentPath = os.path.dirname(os.path.realpath(__file__))
        utils.setenv('COWRY_ROOT', currentPath)
        defaultConfigPath = utils.joinFilePath(currentPath, 'cowry.conf')
        utils.setenv('COWRY_CONFIG', defaultConfigPath)

    app = QApplication(os.sys.argv)
    prog = Action_MainWindow()
    getattr(prog, cmd)()
    os.sys.exit(app.exec_())
示例#5
0
 def createSslConttext(self):
     self.sslContext = ssl.create_default_context()
     certFileName = "{}.crt".format(self.host)
     certFilePath = utils.joinFilePath(self.settings.certificates.certdirs,
                                       certFileName)
     print('fuck1')
     if not utils.checkFileExists(certFilePath):
         # get remote certificates
         self.acquire_remote_cert()
     try:
         # verify_cert
         utils.verify_cert(certFilePath, self.host)
     except Exception as e:
         self.log.error(str(e))
         return (1, str(e))
     print('fuck2')
     try:
         self.sslContext.load_verify_locations(certFilePath)
     except Exception as e:
         self.log.error(str(e))
         return (1, str(e))
示例#6
0
    def init_configure(self):
        """Pass."""
        # self.log.info('start init configure file')
        if not utils.checkFileExists(utils.getenv('COWRY_CONFIG')):
            src = utils.joinFilePath(utils.getenv('COWRY_ROOT'),
                                     'cowry.conf.default')
            dst = utils.joinFilePath(utils.getenv('COWRY_ROOT'), 'cowry.conf')
            utils.copyfile(src, dst)
            print(
                'Not find default configure file, copy default configure to use'
            )

        self.settings = Settings()

        # set default uploaded files path
        if not utils.checkAbsPath(
                self.settings.storage.datapath) or not utils.checkFolderExists(
                    self.settings.storage.datapath):
            setDefaultDataPath = utils.joinFilePath(utils.getenv('COWRY_ROOT'),
                                                    'data')
            self.settings._set(('storage', 'datapath', setDefaultDataPath))
        # set default certificates values
        if not utils.checkAbsPath(self.settings.certificates.privatekey
                                  ) and not utils.checkAbsPath(
                                      self.settings.certificates.certificate):
            setDefaultPrivateKey = utils.joinFilePath(
                utils.getenv('COWRY_ROOT'), 'certs', 'server.key')
            setDefaultCert = utils.joinFilePath(utils.getenv('COWRY_ROOT'),
                                                'certs', 'server.crt')
            self.settings._set(
                ('certificates', 'privatekey', setDefaultPrivateKey))
            self.settings._set(('certificates', 'certificate', setDefaultCert))
        # set db default path if sqlite be used
        if self.settings.database.type == 'sqlite' and not self.settings.database.df:
            setDefaultSqliteDbPath = utils.joinFilePath(
                utils.getenv('COWRY_ROOT'), 'db', 'data', 'default.sqlite')
            self.settings._set(('database', 'df', setDefaultSqliteDbPath))
示例#7
0
def main():
    # currentPath = os.path.dirname(os.path.realpath(__file__))
    # utils.setenv('COWRY_ROOT', currentPath)
    # utils.addAppPath(currentPath)
    cmd = 'start'

    if args.config:
        defaultConfigPath = args.config
        if utils.checkFileExists(defaultConfigPath):
            utils.setenv('COWRY_CONFIG', defaultConfigPath)
            currentPath = os.path.dirname(os.path.realpath(defaultConfigPath))
            utils.setenv('COWRY_ROOT', currentPath)
    else:
        currentPath = os.path.dirname(os.path.realpath(__file__))
        utils.setenv('COWRY_ROOT', currentPath)
        defaultConfigPath = utils.joinFilePath(currentPath, 'cowry.conf')
        utils.setenv('COWRY_CONFIG', defaultConfigPath)

    if args.new:
        cmd = 'new'

    if args.drop:
        cmd = 'drop'

    if args.quiet:
        utils.setenv('COWRY_STATUS', 'NO')
    else:
        utils.setenv('COWRY_STATUS', 'YES')

    if args.webconsole:
        utils.setenv('COWRY_WEB_CONSOLE', 'YES')
    else:
        utils.setenv('COWRY_WEB_CONSOLE', 'NO')

    server = Server()
    getattr(server, cmd)()