chiark / gitweb /
git: use /bin/true for 'askpass' to prevent all password prompts
[fdroidserver.git] / fdroidserver / common.py
index 456b6e997f596c36bdcac6cca3a03cc2da82504b..6b34969fa9b6b415ad6947c577271bac413023dc 100644 (file)
@@ -77,16 +77,14 @@ orig_path = None
 default_config = {
     'sdk_path': "$ANDROID_HOME",
     'ndk_paths': {
-        'r9b': None,
         'r10e': None,
         'r11c': None,
         'r12b': "$ANDROID_NDK",
         'r13b': None,
         'r14b': None,
         'r15c': None,
-        'r16': None,
+        'r16b': None,
     },
-    'qt_sdk_path': None,
     'build_tools': MINIMUM_AAPT_VERSION,
     'force_build_tools': False,
     'java_paths': None,
@@ -817,7 +815,8 @@ class vcs_git(vcs):
         #
         # supported in git >= 2.3
         git_config = [
-            '-c', 'core.sshCommand=false',
+            '-c', 'core.askpass=/bin/true',
+            '-c', 'core.sshCommand=/bin/false',
             '-c', 'url.https://.insteadOf=ssh://',
         ]
         for domain in ('bitbucket.org', 'github.com', 'gitlab.com'):
@@ -829,7 +828,9 @@ class vcs_git(vcs):
             git_config.append('url.https://u:p@' + domain + '.insteadOf=https://' + domain)
         envs.update({
             'GIT_TERMINAL_PROMPT': '0',
-            'GIT_SSH': 'false',  # for git < 2.3
+            'GIT_ASKPASS': '/bin/true',
+            'SSH_ASKPASS': '/bin/true',
+            'GIT_SSH': '/bin/false',  # for git < 2.3
         })
         return FDroidPopen(['git', ] + git_config + args,
                            envs=envs, cwd=cwd, output=output)
@@ -850,7 +851,7 @@ class vcs_git(vcs):
     def gotorevisionx(self, rev):
         if not os.path.exists(self.local):
             # Brand new checkout
-            p = self.git(['clone', self.remote, self.local])
+            p = self.git(['clone', '--', self.remote, self.local])
             if p.returncode != 0:
                 self.clone_failed = True
                 raise VCSException("Git clone failed", p.output)
@@ -883,7 +884,8 @@ class vcs_git(vcs):
                     if 'Multiple remote HEAD branches' not in lines[0]:
                         raise VCSException(_("Git remote set-head failed"), p.output)
                     branch = lines[1].split(' ')[-1]
-                    p2 = FDroidPopen(['git', 'remote', 'set-head', 'origin', branch], cwd=self.local, output=False)
+                    p2 = FDroidPopen(['git', 'remote', 'set-head', 'origin', '--', branch],
+                                     cwd=self.local, output=False)
                     if p2.returncode != 0:
                         raise VCSException(_("Git remote set-head failed"), p.output + '\n' + p2.output)
                 self.refreshed = True
@@ -961,21 +963,34 @@ class vcs_gitsvn(vcs):
 
     def git(self, args, envs=dict(), cwd=None, output=True):
         '''Prevent git fetch/clone/submodule from hanging at the username/password prompt
+
+        AskPass is set to /bin/true to let the process try to connect
+        without a username/password.
+
+        The SSH command is set to /bin/false to block all SSH URLs
+        (supported in git >= 2.3).  This protects against
+        CVE-2017-1000117.
+
         '''
-        # CVE-2017-1000117 block all SSH URLs (supported in git >= 2.3)
-        config = ['-c', 'core.sshCommand=false']
+        git_config = [
+            '-c', 'core.askpass=/bin/true',
+            '-c', 'core.sshCommand=/bin/false',
+        ]
         envs.update({
             'GIT_TERMINAL_PROMPT': '0',
-            'GIT_SSH': 'false',  # for git < 2.3
-            'SVN_SSH': 'false',
+            'GIT_ASKPASS': '/bin/true',
+            'SSH_ASKPASS': '/bin/true',
+            'GIT_SSH': '/bin/false',  # for git < 2.3
+            'SVN_SSH': '/bin/false',
         })
-        return FDroidPopen(['git', ] + config + args,
+        return FDroidPopen(['git', ] + git_config + args,
                            envs=envs, cwd=cwd, output=output)
 
     def gotorevisionx(self, rev):
         if not os.path.exists(self.local):
             # Brand new checkout
             gitsvn_args = ['svn', 'clone']
+            remote = None
             if ';' in self.remote:
                 remote_split = self.remote.split(';')
                 for i in remote_split[1:]:
@@ -985,17 +1000,15 @@ class vcs_gitsvn(vcs):
                         gitsvn_args.extend(['-t', i[5:]])
                     elif i.startswith('branches='):
                         gitsvn_args.extend(['-b', i[9:]])
-                gitsvn_args.extend([remote_split[0], self.local])
-                p = self.git(gitsvn_args, output=False)
-                if p.returncode != 0:
-                    self.clone_failed = True
-                    raise VCSException("Git svn clone failed", p.output)
+                remote = remote_split[0]
             else:
-                gitsvn_args.extend([self.remote, self.local])
-                p = self.git(gitsvn_args, output=False)
-                if p.returncode != 0:
-                    self.clone_failed = True
-                    raise VCSException("Git svn clone failed", p.output)
+                remote = self.remote
+
+            gitsvn_args.extend(['--', remote, self.local])
+            p = self.git(gitsvn_args)
+            if p.returncode != 0:
+                self.clone_failed = True
+                raise VCSException(_('git svn clone failed'), p.output)
             self.checkrepo()
         else:
             self.checkrepo()
@@ -1091,7 +1104,8 @@ class vcs_hg(vcs):
 
     def gotorevisionx(self, rev):
         if not os.path.exists(self.local):
-            p = FDroidPopen(['hg', 'clone', '--ssh', 'false', self.remote, self.local], output=False)
+            p = FDroidPopen(['hg', 'clone', '--ssh', 'false', '--', self.remote, self.local],
+                            output=False)
             if p.returncode != 0:
                 self.clone_failed = True
                 raise VCSException("Hg clone failed", p.output)
@@ -1102,7 +1116,7 @@ class vcs_hg(vcs):
             for line in p.output.splitlines():
                 if not line.startswith('? '):
                     raise VCSException("Unexpected output from hg status -uS: " + line)
-                FDroidPopen(['rm', '-rf', line[2:]], cwd=self.local, output=False)
+                FDroidPopen(['rm', '-rf', '--', line[2:]], cwd=self.local, output=False)
             if not self.refreshed:
                 p = FDroidPopen(['hg', 'pull', '--ssh', 'false'], cwd=self.local, output=False)
                 if p.returncode != 0:
@@ -1112,7 +1126,7 @@ class vcs_hg(vcs):
         rev = rev or 'default'
         if not rev:
             return
-        p = FDroidPopen(['hg', 'update', '-C', rev], cwd=self.local, output=False)
+        p = FDroidPopen(['hg', 'update', '-C', '--', rev], cwd=self.local, output=False)
         if p.returncode != 0:
             raise VCSException("Hg checkout of '%s' failed" % rev, p.output)
         p = FDroidPopen(['hg', 'purge', '--all'], cwd=self.local, output=False)
@@ -1512,7 +1526,7 @@ def getsrclib(spec, srclib_dir, subdir=None, basepath=False,
         if srclib["Prepare"]:
             cmd = replace_config_vars(srclib["Prepare"], build)
 
-            p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir)
+            p = FDroidPopen(['bash', '-x', '-c', '--', cmd], cwd=libdir)
             if p.returncode != 0:
                 raise BuildException("Error running prepare command for srclib %s"
                                      % name, p.output)
@@ -1567,7 +1581,7 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
         cmd = replace_config_vars(build.init, build)
         logging.info("Running 'init' commands in %s" % root_dir)
 
-        p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
+        p = FDroidPopen(['bash', '-x', '-c', '--', cmd], cwd=root_dir)
         if p.returncode != 0:
             raise BuildException("Error running init command for %s:%s" %
                                  (app.id, build.versionName), p.output)
@@ -1725,7 +1739,7 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
             libpath = os.path.relpath(libpath, root_dir)
             cmd = cmd.replace('$$' + name + '$$', libpath)
 
-        p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
+        p = FDroidPopen(['bash', '-x', '-c', '--', cmd], cwd=root_dir)
         if p.returncode != 0:
             raise BuildException("Error running prebuild command for %s:%s" %
                                  (app.id, build.versionName), p.output)
@@ -2023,7 +2037,8 @@ def FDroidPopenBytes(commands, cwd=None, envs=None, output=True, stderr_to_stdou
     p = None
     try:
         p = subprocess.Popen(commands, cwd=cwd, shell=False, env=process_env,
-                             stdout=subprocess.PIPE, stderr=stderr_param)
+                             stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
+                             stderr=stderr_param)
     except OSError as e:
         raise BuildException("OSError while trying to execute " +
                              ' '.join(commands) + ': ' + str(e))
@@ -2209,7 +2224,6 @@ def replace_config_vars(cmd, build):
     cmd = cmd.replace('$$SDK$$', config['sdk_path'])
     cmd = cmd.replace('$$NDK$$', build.ndk_path())
     cmd = cmd.replace('$$MVN3$$', config['mvn3'])
-    cmd = cmd.replace('$$QT$$', config['qt_sdk_path'] or '')
     if build is not None:
         cmd = replace_build_vars(cmd, build)
     return cmd
@@ -3018,3 +3032,39 @@ def get_wiki_timestamp(timestamp=None):
     if timestamp is None:
         timestamp = time.gmtime()
     return time.strftime("%Y-%m-%d %H:%M:%SZ", timestamp)
+
+
+def get_android_tools_versions(ndk_path=None):
+    '''get a list of the versions of all installed Android SDK/NDK components'''
+
+    global config
+    sdk_path = config['sdk_path']
+    if sdk_path[-1] != '/':
+        sdk_path += '/'
+    components = []
+    if ndk_path:
+        ndk_release_txt = os.path.join(ndk_path, 'RELEASE.TXT')
+        if os.path.isfile(ndk_release_txt):
+            with open(ndk_release_txt, 'r') as fp:
+                components.append((os.path.basename(ndk_path), fp.read()[:-1]))
+
+    pattern = re.compile('^Pkg.Revision=(.+)', re.MULTILINE)
+    for root, dirs, files in os.walk(sdk_path):
+        if 'source.properties' in files:
+            source_properties = os.path.join(root, 'source.properties')
+            with open(source_properties, 'r') as fp:
+                m = pattern.search(fp.read())
+                if m:
+                    components.append((root[len(sdk_path):], m.group(1)))
+
+    return components
+
+
+def get_android_tools_version_log(ndk_path=None):
+    '''get a list of the versions of all installed Android SDK/NDK components'''
+    log = '== Installed Android Tools ==\n\n'
+    components = get_android_tools_versions(ndk_path)
+    for name, version in sorted(components):
+        log += '* ' + name + ' (' + version + ')\n'
+
+    return log