2 # -*- coding: utf-8 -*-
4 # scanner.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from argparse import ArgumentParser
28 from common import BuildException, VCSException
34 # Scan the source code in the given directory (and all subdirectories)
35 # and return the number of fatal problems encountered
36 def scan_source(build_dir, root_dir, thisbuild):
40 # Common known non-free blobs (always lower case):
42 exp: re.compile(r'.*' + exp, re.IGNORECASE) for exp in [
46 r'admob.*sdk.*android',
49 r'google.*play.*services',
53 r'youtube.*android.*player.*api',
61 def suspects_found(s):
62 for n, r in usual_suspects.iteritems():
66 scanignore = common.getpaths(build_dir, thisbuild, 'scanignore')
67 scandelete = common.getpaths(build_dir, thisbuild, 'scandelete')
69 scanignore_worked = set()
70 scandelete_worked = set()
75 scanignore_worked.add(p)
82 scandelete_worked.add(p)
86 def ignoreproblem(what, fd, fp):
87 logging.info('Ignoring %s at %s' % (what, fd))
90 def removeproblem(what, fd, fp):
91 logging.info('Removing %s at %s' % (what, fd))
95 def warnproblem(what, fd):
96 logging.warn('Found %s at %s' % (what, fd))
98 def handleproblem(what, fd, fp):
100 return ignoreproblem(what, fd, fp)
102 return removeproblem(what, fd, fp)
103 logging.error('Found %s at %s' % (what, fd))
106 def is_executable(path):
107 return os.path.exists(path) and os.access(path, os.X_OK)
109 textchars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100)) - {0x7f})
113 with open(path, 'rb') as f:
115 return bool(d.translate(None, textchars))
117 # Iterate through all files in the source code
118 for r, d, f in os.walk(build_dir, topdown=True):
120 # It's topdown, so checking the basename is enough
121 for ignoredir in ('.hg', '.git', '.svn', '.bzr'):
127 # Path (relative) to the file
128 fp = os.path.join(r, curfile)
129 fd = fp[len(build_dir) + 1:]
131 ext = common.get_extension(fd)
134 count += handleproblem('shared library', fd, fp)
136 count += handleproblem('static library', fd, fp)
138 count += handleproblem('Java compiled class', fd, fp)
140 removeproblem('APK file', fd, fp)
143 for name in suspects_found(curfile):
144 count += handleproblem('usual supect \'%s\'' % name, fd, fp)
145 warnproblem('JAR file', fd)
148 if not os.path.isfile(fp):
150 for line in file(fp):
151 if 'DexClassLoader' in line:
152 count += handleproblem('DexClassLoader', fd, fp)
155 elif ext == 'gradle':
156 if not os.path.isfile(fp):
158 for i, line in enumerate(file(fp)):
160 for name in suspects_found(line):
161 count += handleproblem('usual supect \'%s\' at line %d' % (name, i), fd, fp)
163 # These files are often found - avoid checking if they are binary
164 # to speed up the scanner
165 elif ext in ['xml', 'md', 'txt', 'html', 'sh', 'png']:
169 if is_executable(fp):
170 count += handleproblem('executable binary', fd, fp)
172 count += handleproblem('unknown binary', fd, fp)
175 if p not in scanignore_worked:
176 logging.error('Unused scanignore path: %s' % p)
180 if p not in scandelete_worked:
181 logging.error('Unused scandelete path: %s' % p)
184 # Presence of a jni directory without buildjni=yes might
185 # indicate a problem (if it's not a problem, explicitly use
186 # buildjni=no to bypass this check)
187 if (os.path.exists(os.path.join(root_dir, 'jni')) and
188 not thisbuild['buildjni']):
189 logging.error('Found jni directory, but buildjni is not enabled. Set it to \'no\' to ignore.')
197 global config, options
199 # Parse command line...
200 parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
201 common.setup_global_opts(parser)
202 parser.add_argument("appid", nargs='*', help="app-id with optional versioncode in the form APPID[:VERCODE]")
203 options = parser.parse_args()
205 config = common.read_config(options)
207 # Read all app and srclib metadata
208 allapps = metadata.read_metadata()
209 apps = common.read_app_args(options.appid, allapps, True)
214 if not os.path.isdir(build_dir):
215 logging.info("Creating build directory")
216 os.makedirs(build_dir)
217 srclib_dir = os.path.join(build_dir, 'srclib')
218 extlib_dir = os.path.join(build_dir, 'extlib')
220 for appid, app in apps.iteritems():
223 logging.info("Skipping %s: disabled" % appid)
225 if not app['builds']:
226 logging.info("Skipping %s: no builds specified" % appid)
229 logging.info("Processing " + appid)
233 if app['Repo Type'] == 'srclib':
234 build_dir = os.path.join('build', 'srclib', app['Repo'])
236 build_dir = os.path.join('build', appid)
238 # Set up vcs interface and make sure we have the latest code...
239 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
241 for thisbuild in app['builds']:
243 if thisbuild['disable']:
244 logging.info("...skipping version %s - %s" % (
245 thisbuild['version'], thisbuild.get('disable', thisbuild['commit'][1:])))
247 logging.info("...scanning version " + thisbuild['version'])
249 # Prepare the source code...
250 root_dir, _ = common.prepare_source(vcs, app, thisbuild,
251 build_dir, srclib_dir,
255 count = scan_source(build_dir, root_dir, thisbuild)
257 logging.warn('Scanner found %d problems in %s (%s)' % (
258 count, appid, thisbuild['vercode']))
261 except BuildException as be:
262 logging.warn("Could not scan app %s due to BuildException: %s" % (
265 except VCSException as vcse:
266 logging.warn("VCS error while scanning app %s: %s" % (appid, vcse))
269 logging.warn("Could not scan app %s due to unknown error: %s" % (
270 appid, traceback.format_exc()))
273 logging.info("Finished:")
274 print "%d problems found" % probcount
276 if __name__ == "__main__":