def _build_mbedtls(self, file_name):
        cc.n('prepare mbedtls source code... ', end='')
        if not os.path.exists(self.MBEDTLS_PATH_SRC):
            cc.v('')
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(
                os.path.join(PATH_EXTERNAL,
                             'mbedtls-mbedtls-{}'.format(env.ver_mbedtls)),
                self.MBEDTLS_PATH_SRC)
        else:
            cc.w('already exists, skip.')
            return
        cc.v('')

        # fix source file
        utils.ensure_file_exists(
            os.path.join(PATH_EXTERNAL, 'fix-external', 'mbedtls', 'include',
                         'mbedtls', 'config.h'))
        utils.copy_file(
            os.path.join(PATH_EXTERNAL, 'fix-external', 'mbedtls', 'include',
                         'mbedtls'),
            os.path.join(self.MBEDTLS_PATH_SRC, 'include', 'mbedtls'),
            'config.h')
        utils.ensure_file_exists(
            os.path.join(PATH_EXTERNAL, 'fix-external', 'mbedtls', 'library',
                         'rsa.c'))
        utils.copy_file(
            os.path.join(PATH_EXTERNAL, 'fix-external', 'mbedtls', 'library'),
            os.path.join(self.MBEDTLS_PATH_SRC, 'library'), 'rsa.c')
Beispiel #2
0
def upload():
    """Upload Zipped Source"""
    if 'file' in request.files:
        filen = request.files['file']
        _, extension = os.path.splitext(filen.filename.lower())
        # Check for Valid ZIP
        if (filen and filen.filename
                and extension in settings.UPLD_ALLOWED_EXTENSIONS
                and filen.mimetype in settings.UPLD_MIME):
            filename = secure_filename(filen.filename)
            # Make upload dir
            if not os.path.exists(settings.UPLOAD_FOLDER):
                os.makedirs(settings.UPLOAD_FOLDER)
            # Save file
            zip_file = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            filen.save(zip_file)
            # Get zip hash
            get_zip_hash = utils.gen_sha256_file(zip_file)
            # check if already scanned
            res = Results.query.filter(Results.scan_hash == get_zip_hash)
            if not res.count():
                # App analysis dir
                app_dir = os.path.join(app.config['UPLOAD_FOLDER'],
                                       get_zip_hash + "/")
                # Make app analysis dir
                if not os.path.exists(app_dir):
                    os.makedirs(app_dir)
                # Unzip
                utils.unzip(zip_file, app_dir)
                # Do scan
                scan_results = scan_dirs([app_dir])
                print("[INFO] Static Analysis Completed!")
                _, sha2_hashes, hash_of_sha2 = utils.gen_hashes([app_dir])
                tms = datetime.datetime.fromtimestamp(
                    time.time()).strftime('%Y-%m-%d %H:%M:%S')
                # Save Result
                print("[INFO] Saving Scan Results!")
                res_db = Results(
                    filename,
                    get_zip_hash,
                    [app_dir],
                    sha2_hashes,
                    hash_of_sha2,
                    scan_results['sec_issues'],
                    scan_results['good_finding'],
                    scan_results['missing_sec_header'],
                    scan_results['files'],
                    scan_results['total_count'],
                    scan_results['vuln_count'],
                    [],
                    [],
                    tms,
                )
                db_session.add(res_db)
                db_session.commit()
            return jsonify({
                "status": "success",
                "url": "result/" + get_zip_hash
            })
    return jsonify({"status": "error", "desc": "Upload Failed!"})
Beispiel #3
0
 def _build_mongoose(self, file_name):
     cc.n('prepare mongoose source code... ', end='')
     if not os.path.exists(self.MONGOOSE_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         os.rename(os.path.join(PATH_EXTERNAL, 'mongoose-{}'.format(env.ver_mongoose)), self.MONGOOSE_PATH_SRC)
     else:
         cc.w('already exists, skip.')
Beispiel #4
0
 def _build_jsoncpp(self, file_name):
     cc.n('prepare jsoncpp source code... ', end='')
     if not os.path.exists(self.JSONCPP_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         os.rename(os.path.join(PATH_EXTERNAL, 'jsoncpp-{}'.format(env.ver_jsoncpp)), self.JSONCPP_PATH_SRC)
     else:
         cc.w('already exists, skip.')
Beispiel #5
0
 def _build_libuv(self, file_name):
     cc.n('prepare libuv source code... ', end='')
     if not os.path.exists(self.LIBUV_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         time.sleep(1)   # wait for a while, otherwise rename may fail.
         os.rename(os.path.join(PATH_EXTERNAL, 'libuv-{}'.format(env.ver_libuv)), self.LIBUV_PATH_SRC)
     else:
         cc.w('already exists, skip.')
 def _build_libuv(self, file_name):
     cc.n('prepare libuv source code... ', end='')
     if not os.path.exists(self.LIBUV_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         os.rename(
             os.path.join(PATH_EXTERNAL, 'libuv-{}'.format(env.ver_libuv)),
             self.LIBUV_PATH_SRC)
     else:
         cc.w('already exists, skip.')
 def _build_mongoose(self, file_name):
     cc.n('prepare mongoose source code... ', end='')
     if not os.path.exists(self.MONGOOSE_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         os.rename(
             os.path.join(PATH_EXTERNAL,
                          'mongoose-{}'.format(env.ver_mongoose)),
             self.MONGOOSE_PATH_SRC)
     else:
         cc.w('already exists, skip.')
 def _build_jsoncpp(self, file_name):
     cc.n('prepare jsoncpp source code... ', end='')
     if not os.path.exists(self.JSONCPP_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         os.rename(
             os.path.join(PATH_EXTERNAL,
                          'jsoncpp-{}'.format(env.ver_jsoncpp)),
             self.JSONCPP_PATH_SRC)
     else:
         cc.w('already exists, skip.')
Beispiel #9
0
    def _build_openssl(self, file_name):
        cc.n('build openssl static library from source code... ')

        if not super()._build_openssl(file_name):
            return

        _chk_output = [
            os.path.join(self.OPENSSL_PATH_SRC, 'out32', 'libeay32.lib'),
            os.path.join(self.OPENSSL_PATH_SRC, 'out32', 'ssleay32.lib'),
            os.path.join(self.OPENSSL_PATH_SRC, 'inc32', 'openssl',
                         'opensslconf.h'),
        ]

        need_build = False
        for f in _chk_output:
            if not os.path.exists(f):
                need_build = True
                break

        if not need_build:
            cc.n('build openssl static library from source code... ', end='')
            cc.w('already exists, skip.')
            return
        cc.v('')

        cc.n('prepare openssl source code...')
        _alt_ver = '_'.join(env.ver_ossl.split('.'))
        if not os.path.exists(self.OPENSSL_PATH_SRC):
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(
                os.path.join(PATH_EXTERNAL,
                             'openssl-OpenSSL_{}'.format(_alt_ver)),
                self.OPENSSL_PATH_SRC)
            if not os.path.exists(self.OPENSSL_PATH_SRC):
                raise RuntimeError('can not prepare openssl source code.')
        else:
            cc.w('already exists, skip.')

        os.chdir(self.OPENSSL_PATH_SRC)
        os.system('""{}" Configure VC-WIN32"'.format(env.perl))
        os.system(r'ms\do_nasm')
        # for vs2015
        # utils.sys_exec(r'"{}\VC\bin\vcvars32.bat" && nmake -f ms\nt.mak'.format(env.visual_studio_path), direct_output=True)
        # for vs2017 community
        utils.sys_exec(
            r'"{}VC\Auxiliary\Build\vcvars32.bat" && nmake -f ms\nt.mak'.
            format(env.visual_studio_path),
            direct_output=True)

        for f in _chk_output:
            if not os.path.exists(f):
                raise RuntimeError(
                    'build openssl static library from source code failed.')
Beispiel #10
0
    def _build_zlib(self, file_name):
        if not self._download_zlib(file_name):
            return
        cc.n('build zlib library from source code... ', end='')

        if not os.path.exists(self.ZLIB_PATH_SRC):
            cc.v('')
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(
                os.path.join(PATH_EXTERNAL, 'zlib-{}'.format(env.ver_zlib)),
                self.ZLIB_PATH_SRC)

        if ctx.target_path == 'debug':
            olib = 'zlibd.lib'
            odll = 'zlibd.dll'
        else:
            olib = 'zlib.lib'
            odll = 'zlib.dll'
        out_file_lib = os.path.join(self.ZLIB_PATH_SRC, 'build',
                                    ctx.target_path, olib)
        out_file_dll = os.path.join(self.ZLIB_PATH_SRC, 'build',
                                    ctx.target_path, odll)

        if os.path.exists(out_file_lib) and os.path.exists(out_file_dll):
            cc.w('already exists, skip.')
            return
        cc.v('')

        cc.w(
            'On Windows, when build zlib, need you use cmake-gui.exe to generate solution file'
        )
        cc.w(
            'for Visual Studio 2017. Visit https://docs.tp4a.com for more details.'
        )
        cc.w(
            '\nOnce the zlib.sln generated, press Enter to continue or Q to quit...',
            end='')
        try:
            x = input()
        except EOFError:
            x = 'q'
        if x == 'q':
            return

        cc.i('build zlib...')
        sln_file = os.path.join(self.ZLIB_PATH_SRC, 'build', 'zlib.sln')
        utils.msvc_build(sln_file, 'zlib', ctx.target_path, 'win32', False)
        # utils.ensure_file_exists(os.path.join(self.ZLIB_PATH_SRC, 'build', ctx.target_path, 'zlib.lib'))
        # utils.ensure_file_exists(os.path.join(self.ZLIB_PATH_SRC, 'build', ctx.target_path, 'zlib.dll'))
        # utils.copy_file(os.path.join(self.ZLIB_PATH_SRC, 'build', ctx.target_path), os.path.join(self.ZLIB_PATH_SRC, 'lib', ctx.target_path), 'zlib.lib')
        # utils.copy_file(os.path.join(self.ZLIB_PATH_SRC, 'build', ctx.target_path), os.path.join(self.ZLIB_PATH_SRC, 'lib', ctx.target_path), 'zlib.dll')
        utils.ensure_file_exists(out_file_lib)
        utils.ensure_file_exists(out_file_dll)
Beispiel #11
0
 def _build_libuv(self, file_name):
     if not self._download_libuv(file_name):
         return
     cc.n('prepare libuv source code... ', end='')
     if not os.path.exists(self.LIBUV_PATH_SRC):
         cc.v('')
         utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
         time.sleep(1)  # wait for a while, otherwise rename may fail.
         os.rename(
             os.path.join(PATH_EXTERNAL, 'libuv-{}'.format(env.ver_libuv)),
             self.LIBUV_PATH_SRC)
     else:
         cc.w('already exists, skip.')
Beispiel #12
0
    def _build_mbedtls(self, file_name):
        cc.n('prepare mbedtls source code... ', end='')
        if not os.path.exists(self.MBEDTLS_PATH_SRC):
            cc.v('')
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(os.path.join(PATH_EXTERNAL, 'mbedtls-mbedtls-{}'.format(env.ver_mbedtls)), self.MBEDTLS_PATH_SRC)
        else:
            cc.w('already exists, skip.')
            return
        cc.v('')

        # fix source file
        utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'mbedtls', 'include', 'mbedtls', 'config.h'))
        utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'mbedtls', 'include', 'mbedtls'), os.path.join(self.MBEDTLS_PATH_SRC, 'include', 'mbedtls'), 'config.h')
Beispiel #13
0
    def _build_libssh(self, file_name):
        cc.n('build libssh static library from source code... ', end='')

        if not os.path.exists(self.LIBSSH_PATH_SRC):
            cc.v('')
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(os.path.join(PATH_EXTERNAL, 'libssh-{}'.format(env.ver_libssh)), self.LIBSSH_PATH_SRC)

            # cc.n('fix libssh source code... ', end='')
            # utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', 'src', 'sftp.c'))
            # utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'sftp.c')
            cc.n('fix libssh source code... ', end='')
            s_name = 'libssh-{}'.format(env.ver_libssh)
            utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src', 'session.c'))
            utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src', 'libcrypto.c'))
            utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src', 'libcrypto-compat.c'))
            utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'session.c')
            utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'libcrypto.c')
            utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'libcrypto-compat.c')

        out_file_lib = os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path, 'ssh.lib')
        out_file_dll = os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path, 'ssh.dll')

        if os.path.exists(out_file_lib) and os.path.exists(out_file_dll):
            cc.w('already exists, skip.')
            return
        cc.v('')

        cc.w('On Windows, when build libssh, need you use cmake-gui.exe to generate solution file')
        cc.w('for Visual Studio 2017. Visit https://docs.tp4a.com for more details.')
        cc.w('\nOnce the libssh.sln generated, press Enter to continue or Q to quit...', end='')
        try:
            x = env.input()
        except EOFError:
            x = 'q'
        if x == 'q':
            return

        cc.i('build libssh...')
        sln_file = os.path.join(self.LIBSSH_PATH_SRC, 'build', 'libssh.sln')
        utils.msvc_build(sln_file, 'ssh_shared', ctx.target_path, 'win32', False)
        utils.ensure_file_exists(os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path, 'ssh.lib'))
        utils.ensure_file_exists(os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path, 'ssh.dll'))
        utils.copy_file(os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path), os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path), 'ssh.lib')
        utils.copy_file(os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path), os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path), 'ssh.dll')
        utils.ensure_file_exists(out_file_lib)
        utils.ensure_file_exists(out_file_dll)
Beispiel #14
0
    def _build_openssl(self, file_name):
        cc.n('build openssl static library from source code... ')

        if not super()._build_openssl(file_name):
            return

        _chk_output = [
            os.path.join(self.OPENSSL_PATH_SRC, 'out32', 'libeay32.lib'),
            os.path.join(self.OPENSSL_PATH_SRC, 'out32', 'ssleay32.lib'),
            os.path.join(self.OPENSSL_PATH_SRC, 'inc32', 'openssl', 'opensslconf.h'),
            ]

        need_build = False
        for f in _chk_output:
            if not os.path.exists(f):
                need_build = True
                break

        if not need_build:
            cc.n('build openssl static library from source code... ', end='')
            cc.w('already exists, skip.')
            return
        cc.v('')

        cc.n('prepare openssl source code...')
        _alt_ver = '_'.join(env.ver_ossl.split('.'))
        if not os.path.exists(self.OPENSSL_PATH_SRC):
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(os.path.join(PATH_EXTERNAL, 'openssl-OpenSSL_{}'.format(_alt_ver)), self.OPENSSL_PATH_SRC)
            if not os.path.exists(self.OPENSSL_PATH_SRC):
                raise RuntimeError('can not prepare openssl source code.')
        else:
            cc.w('already exists, skip.')

        os.chdir(self.OPENSSL_PATH_SRC)
        os.system('""{}" Configure VC-WIN32"'.format(env.perl))
        os.system(r'ms\do_nasm')
        # for vs2015
        # utils.sys_exec(r'"{}\VC\bin\vcvars32.bat" && nmake -f ms\nt.mak'.format(env.visual_studio_path), direct_output=True)
        # for vs2017 community
        utils.sys_exec(r'"{}VC\Auxiliary\Build\vcvars32.bat" && nmake -f ms\nt.mak'.format(env.visual_studio_path), direct_output=True)

        for f in _chk_output:
            if not os.path.exists(f):
                raise RuntimeError('build openssl static library from source code failed.')
    def _build_libssh(self, file_name):
        cc.n('build libssh static library from source code... ', end='')
        out_file = os.path.join(self.LIBSSH_PATH_SRC, 'lib', 'libsshMT.lib')

        need_build = False
        if not os.path.exists(out_file):
            need_build = True

        if not need_build:
            cc.w('already exists, skip.')
            return
        cc.v('')

        cc.n('prepare libssh source code... ', end='')
        _include = os.path.join(self.LIBSSH_PATH_SRC, 'include', 'libssh')
        _src = os.path.join(self.LIBSSH_PATH_SRC, 'src')

        if not os.path.exists(_include) or not os.path.exists(_src):
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            # os.rename(os.path.join(PATH_EXTERNAL, 'openssl-OpenSSL_{}'.format(_alt_ver)), self.OPENSSL_PATH_SRC)

            _unzipped_path = os.path.join(PATH_EXTERNAL,
                                          'libssh-{}'.format(env.ver_libssh))

            utils.copy_ex(os.path.join(_unzipped_path, 'include', 'libssh'),
                          _include)
            utils.copy_ex(os.path.join(_unzipped_path, 'src'), _src)

            utils.remove(_unzipped_path)

            if not os.path.exists(_include) or not os.path.exists(_src):
                raise RuntimeError('\ncan not prepare libssh source code.')
        else:
            cc.w('already exists, skip.')

        cc.i('build libssh...')
        sln_file = os.path.join(self.LIBSSH_PATH_SRC, 'libssh.vs2015.sln')
        utils.msvc_build(sln_file, 'libssh', ctx.target_path, ctx.bits_path,
                         False)
        utils.ensure_file_exists(out_file)
Beispiel #16
0
def analyze():
    """Upload and analyze source code. Output JSON"""
    if 'file' in request.files:
        filen = request.files['file']
        _, extension = os.path.splitext(filen.filename.lower())
        # Check for Valid ZIP
        if (filen and filen.filename
                and extension in settings.UPLD_ALLOWED_EXTENSIONS
                and filen.mimetype in settings.UPLD_MIME):
            filename = secure_filename(filen.filename)
            # Make upload dir
            if not os.path.exists(settings.UPLOAD_FOLDER):
                os.makedirs(settings.UPLOAD_FOLDER)
            # Save file
            zip_file = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            filen.save(zip_file)
            # Get zip hash
            get_zip_hash = utils.gen_sha256_file(zip_file)
            res = Results.query.filter(Results.scan_hash == get_zip_hash)
            time_execute = 0
            #Check if file is scanned before
            if not res.count():
                path_dir = os.path.join(app.config['UPLOAD_FOLDER'],
                                        get_zip_hash + "/")
                # Make app analysis dir
                if not os.path.exists(path_dir):
                    os.makedirs(path_dir)
                # Unzip
                utils.unzip(zip_file, path_dir)
                start_time = time.time()
                scan_results = scan_dirs([path_dir])
                time_execute = round(time.time() - start_time, 3)
                _, sha2_hashes, hash_of_sha2 = utils.gen_hashes([path_dir])
                tms = datetime.datetime.fromtimestamp(
                    time.time()).strftime('%Y-%m-%d %H:%M:%S')
                print("[INFO] Saving Scan Results!")
                res_db = Results(
                    filename,
                    get_zip_hash,
                    [path_dir],
                    sha2_hashes,
                    hash_of_sha2,
                    scan_results['sec_issues'],
                    scan_results['good_finding'],
                    scan_results['missing_sec_header'],
                    scan_results['files'],
                    scan_results['total_count'],
                    scan_results['vuln_count'],
                    [],
                    [],
                    tms,
                    time_execute,
                )
                db_session.add(res_db)
                db_session.commit()
                # Get scan results from database
                res = Results.query.filter(Results.scan_hash == get_zip_hash)
            output = []
            iss_count = 0
            _type = "sast"
            time_execute = res.first().time_execute
            for group, issues in (res.first().sec_issues).items():
                for issue in issues:
                    iss_count += 1
                    _location = {}
                    _ruleId = issue['title']
                    _location['path'] = issue["path"]
                    _location['positions'] = {}
                    _location['positions']['begin'] = {}
                    _location['positions']['begin']['line'] = issue['line']
                    _metadata = {}
                    _metadata["description"] = issue['description']
                    output.append({
                        'type': _type,
                        "ruleId": _ruleId,
                        "location": _location,
                        "metadata": _metadata
                    })
            response = {
                "engine": {
                    "name": "guardrails/engine-javascript-nodejsscan",
                    "version": "1.0.0"
                },
                "process": {
                    "name": "nodejsscan",
                    "version": "3.4"
                },
                "language": "javascript",
                "status": "success",
                "executionTime": time_execute * 1000,
                "issues": iss_count,
                "output": output
            }
            return (json.dumps(OrderedDict(response)))
        else:
            return "Only accept zip file\n"
    else:
        return "Missing param file\n"
    return "Internal errors\n"
Beispiel #17
0
    def _build_libssh(self, file_name):
        if not self._download_libssh(file_name):
            return
        cc.n('build libssh library from source code... ', end='')

        if not os.path.exists(self.LIBSSH_PATH_SRC):
            cc.v('')
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(
                os.path.join(PATH_EXTERNAL,
                             'libssh-{}'.format(env.ver_libssh)),
                self.LIBSSH_PATH_SRC)

            cc.n('fix libssh source code... ', end='')
            s_name = 'libssh-{}'.format(env.ver_libssh)
            utils.ensure_file_exists(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name,
                             'src', 'session.c'))
            # # utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src', 'libcrypto.c'))
            # utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src', 'libcrypto-compat.c'))
            utils.copy_file(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh',
                             s_name, 'src'),
                os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'session.c')
            # ## utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'libcrypto.c')
            # # utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name, 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'libcrypto-compat.c')

        out_file_lib = os.path.join(self.LIBSSH_PATH_SRC, 'lib',
                                    ctx.target_path, 'ssh.lib')
        out_file_dll = os.path.join(self.LIBSSH_PATH_SRC, 'lib',
                                    ctx.target_path, 'ssh.dll')

        if os.path.exists(out_file_lib) and os.path.exists(out_file_dll):
            cc.w('already exists, skip.')
            return
        cc.v('')

        build_path = os.path.join(self.LIBSSH_PATH_SRC, 'build')
        if not os.path.exists(build_path):
            utils.makedirs(build_path)

        openssl_path = os.path.join(PATH_EXTERNAL, 'OpenSSL')

        cmake_define = ' -DOPENSSL_INCLUDE_DIR={path_release}\include' \
                       ' -DOPENSSL_LIBRARIES={path_release}\lib\VC\static' \
                       ' -DWITH_SFTP=ON' \
                       ' -DWITH_SERVER=ON' \
                       ' -DWITH_GSSAPI=OFF' \
                       ' -DWITH_ZLIB=OFF' \
                       ' -DWITH_PCAP=OFF' \
                       ' -DWITH_STATIC_LIB=ON' \
                       ' -DUNIT_TESTING=OFF' \
                       ' -DWITH_EXAMPLES=OFF' \
                       ' -DWITH_BENCHMARKS=OFF' \
                       ' -DWITH_NACL=OFF' \
                       ''.format(path_release=openssl_path)

        # ' -DCMAKE_INSTALL_PREFIX={path_release}'
        # ' -DWITH_STATIC_LIB=ON'
        # ' -DBUILD_SHARED_LIBS=OFF'

        old_p = os.getcwd()
        try:
            os.chdir(build_path)
            utils.cmake(build_path,
                        'Release',
                        False,
                        cmake_define=cmake_define)
            os.chdir(build_path)
            # utils.sys_exec('make install')
        except:
            cc.e('can not make')
            raise
        os.chdir(old_p)

        # cc.w('On Windows, when build libssh, need you use cmake-gui.exe to generate solution file')
        # cc.w('for Visual Studio 2017. Visit https://docs.tp4a.com for more details.')
        # cc.w('\nOnce the libssh.sln generated, press Enter to continue or Q to quit...', end='')
        # try:
        #     x = env.input()
        # except EOFError:
        #     x = 'q'
        # if x == 'q':
        #     return

        cc.i('build libssh...')
        sln_file = os.path.join(self.LIBSSH_PATH_SRC, 'build', 'libssh.sln')
        utils.msvc_build(sln_file, 'ssh_shared', ctx.target_path, 'win32',
                         False)
        utils.ensure_file_exists(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path,
                         'ssh.lib'))
        utils.ensure_file_exists(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path,
                         'ssh.dll'))
        utils.copy_file(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src',
                         ctx.target_path),
            os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path),
            'ssh.lib')
        utils.copy_file(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src',
                         ctx.target_path),
            os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path),
            'ssh.dll')
        utils.ensure_file_exists(out_file_lib)
        utils.ensure_file_exists(out_file_dll)
Beispiel #18
0
    def _build_libssh(self, file_name):
        cc.n('build libssh static library from source code... ', end='')

        if not os.path.exists(self.LIBSSH_PATH_SRC):
            cc.v('')
            utils.unzip(os.path.join(PATH_DOWNLOAD, file_name), PATH_EXTERNAL)
            os.rename(
                os.path.join(PATH_EXTERNAL,
                             'libssh-{}'.format(env.ver_libssh)),
                self.LIBSSH_PATH_SRC)

            # cc.n('fix libssh source code... ', end='')
            # utils.ensure_file_exists(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', 'src', 'sftp.c'))
            # utils.copy_file(os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', 'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'sftp.c')
            cc.n('fix libssh source code... ', end='')
            s_name = 'libssh-{}'.format(env.ver_libssh)
            utils.ensure_file_exists(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name,
                             'src', 'session.c'))
            utils.ensure_file_exists(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name,
                             'src', 'libcrypto.c'))
            utils.ensure_file_exists(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name,
                             'src', 'libcrypto-compat.c'))
            utils.copy_file(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh',
                             s_name, 'src'),
                os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'session.c')
            utils.copy_file(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh',
                             s_name, 'src'),
                os.path.join(self.LIBSSH_PATH_SRC, 'src'), 'libcrypto.c')
            utils.copy_file(
                os.path.join(PATH_EXTERNAL, 'fix-external', 'libssh', s_name,
                             'src'), os.path.join(self.LIBSSH_PATH_SRC, 'src'),
                'libcrypto-compat.c')

        out_file_lib = os.path.join(self.LIBSSH_PATH_SRC, 'lib',
                                    ctx.target_path, 'ssh.lib')
        out_file_dll = os.path.join(self.LIBSSH_PATH_SRC, 'lib',
                                    ctx.target_path, 'ssh.dll')

        if os.path.exists(out_file_lib) and os.path.exists(out_file_dll):
            cc.w('already exists, skip.')
            return
        cc.v('')

        cc.w(
            'On Windows, when build libssh, need you use cmake-gui.exe to generate solution file'
        )
        cc.w(
            'for Visual Studio 2017. Visit https://docs.tp4a.com for more details.'
        )
        cc.w(
            '\nOnce the libssh.sln generated, press Enter to continue or Q to quit...',
            end='')
        try:
            x = env.input()
        except EOFError:
            x = 'q'
        if x == 'q':
            return

        cc.i('build libssh...')
        sln_file = os.path.join(self.LIBSSH_PATH_SRC, 'build', 'libssh.sln')
        utils.msvc_build(sln_file, 'ssh_shared', ctx.target_path, 'win32',
                         False)
        utils.ensure_file_exists(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path,
                         'ssh.lib'))
        utils.ensure_file_exists(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src', ctx.target_path,
                         'ssh.dll'))
        utils.copy_file(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src',
                         ctx.target_path),
            os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path),
            'ssh.lib')
        utils.copy_file(
            os.path.join(self.LIBSSH_PATH_SRC, 'build', 'src',
                         ctx.target_path),
            os.path.join(self.LIBSSH_PATH_SRC, 'lib', ctx.target_path),
            'ssh.dll')
        utils.ensure_file_exists(out_file_lib)
        utils.ensure_file_exists(out_file_dll)