2016-01-04 16:33:20 +01:00
|
|
|
#!/usr/bin/env python3
|
2012-02-02 15:27:49 +01:00
|
|
|
#
|
|
|
|
# publish.py - part of the FDroid server tools
|
2013-10-31 16:37:39 +01:00
|
|
|
# Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
|
2014-01-28 14:07:19 +01:00
|
|
|
# Copyright (C) 2013-2014 Daniel Martí <mvdan@mvdan.cc>
|
2012-02-02 15:27:49 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2017-07-15 22:15:30 +02:00
|
|
|
import re
|
2012-02-02 15:27:49 +01:00
|
|
|
import shutil
|
|
|
|
import glob
|
2016-01-04 18:36:47 +01:00
|
|
|
import hashlib
|
2015-09-04 11:37:05 +02:00
|
|
|
from argparse import ArgumentParser
|
2014-01-27 17:08:54 +01:00
|
|
|
import logging
|
2017-09-13 17:33:57 +02:00
|
|
|
from gettext import ngettext
|
2012-02-02 15:27:49 +01:00
|
|
|
|
2017-09-13 18:03:57 +02:00
|
|
|
from . import _
|
2016-01-04 17:37:35 +01:00
|
|
|
from . import common
|
|
|
|
from . import metadata
|
2017-05-22 21:33:52 +02:00
|
|
|
from .common import FDroidPopen, SdkToolsPopen
|
|
|
|
from .exception import BuildException
|
2012-02-02 15:27:49 +01:00
|
|
|
|
2013-11-01 12:10:57 +01:00
|
|
|
config = None
|
|
|
|
options = None
|
2013-10-31 16:37:39 +01:00
|
|
|
|
2014-05-02 05:39:33 +02:00
|
|
|
|
2012-02-26 15:18:58 +01:00
|
|
|
def main():
|
|
|
|
|
2013-11-01 12:10:57 +01:00
|
|
|
global config, options
|
2012-02-26 15:18:58 +01:00
|
|
|
|
|
|
|
# Parse command line...
|
2015-09-04 11:37:05 +02:00
|
|
|
parser = ArgumentParser(usage="%(prog)s [options] "
|
|
|
|
"[APPID[:VERCODE] [APPID[:VERCODE] ...]]")
|
2015-09-12 08:42:50 +02:00
|
|
|
common.setup_global_opts(parser)
|
2017-09-15 11:41:39 +02:00
|
|
|
parser.add_argument("appid", nargs='*', help=_("applicationId with optional versionCode in the form APPID[:VERCODE]"))
|
2016-09-12 12:55:48 +02:00
|
|
|
metadata.add_metadata_arguments(parser)
|
2015-09-04 11:37:05 +02:00
|
|
|
options = parser.parse_args()
|
2016-09-12 12:55:48 +02:00
|
|
|
metadata.warnings_action = options.W
|
2012-02-26 15:18:58 +01:00
|
|
|
|
2013-11-01 12:10:57 +01:00
|
|
|
config = common.read_config(options)
|
|
|
|
|
2016-02-11 20:43:55 +01:00
|
|
|
if not ('jarsigner' in config and 'keytool' in config):
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.critical(_('Java JDK not found! Install in standard location or set java_paths!'))
|
2016-02-11 20:43:55 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2012-02-26 15:18:58 +01:00
|
|
|
log_dir = 'logs'
|
|
|
|
if not os.path.isdir(log_dir):
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.info(_("Creating log directory"))
|
2012-02-26 15:18:58 +01:00
|
|
|
os.makedirs(log_dir)
|
|
|
|
|
|
|
|
tmp_dir = 'tmp'
|
|
|
|
if not os.path.isdir(tmp_dir):
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.info(_("Creating temporary directory"))
|
2012-02-26 15:18:58 +01:00
|
|
|
os.makedirs(tmp_dir)
|
|
|
|
|
|
|
|
output_dir = 'repo'
|
|
|
|
if not os.path.isdir(output_dir):
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.info(_("Creating output directory"))
|
2012-02-26 15:18:58 +01:00
|
|
|
os.makedirs(output_dir)
|
|
|
|
|
|
|
|
unsigned_dir = 'unsigned'
|
|
|
|
if not os.path.isdir(unsigned_dir):
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.warning(_("No unsigned directory - nothing to do"))
|
2014-04-04 16:37:18 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-04-11 21:34:49 +02:00
|
|
|
if not os.path.exists(config['keystore']):
|
|
|
|
logging.error("Config error - missing '{0}'".format(config['keystore']))
|
|
|
|
sys.exit(1)
|
2012-02-26 15:18:58 +01:00
|
|
|
|
2014-05-13 00:01:53 +02:00
|
|
|
# It was suggested at
|
|
|
|
# https://dev.guardianproject.info/projects/bazaar/wiki/FDroid_Audit
|
2013-11-07 09:11:05 +01:00
|
|
|
# that a package could be crafted, such that it would use the same signing
|
|
|
|
# key as an existing app. While it may be theoretically possible for such a
|
|
|
|
# colliding package ID to be generated, it seems virtually impossible that
|
|
|
|
# the colliding ID would be something that would be a) a valid package ID,
|
|
|
|
# and b) a sane-looking ID that would make its way into the repo.
|
|
|
|
# Nonetheless, to be sure, before publishing we check that there are no
|
|
|
|
# collisions, and refuse to do any publishing if that's the case...
|
2013-12-19 22:55:17 +01:00
|
|
|
allapps = metadata.read_metadata()
|
2015-09-04 11:37:05 +02:00
|
|
|
vercodes = common.read_pkg_args(options.appid, True)
|
2013-11-07 09:11:05 +01:00
|
|
|
allaliases = []
|
2014-08-16 12:46:02 +02:00
|
|
|
for appid in allapps:
|
2016-01-04 18:36:47 +01:00
|
|
|
m = hashlib.md5()
|
2016-01-04 18:46:51 +01:00
|
|
|
m.update(appid.encode('utf-8'))
|
2013-11-07 09:11:05 +01:00
|
|
|
keyalias = m.hexdigest()[:8]
|
|
|
|
if keyalias in allaliases:
|
2017-09-13 18:03:57 +02:00
|
|
|
logging.error(_("There is a keyalias collision - publishing halted"))
|
2013-11-07 09:11:05 +01:00
|
|
|
sys.exit(1)
|
|
|
|
allaliases.append(keyalias)
|
2017-09-13 17:33:57 +02:00
|
|
|
logging.info(ngettext('{0} app, {1} key aliases',
|
|
|
|
'{0} apps, {1} key aliases', len(allapps)).format(len(allapps), len(allaliases)))
|
2013-11-07 09:11:05 +01:00
|
|
|
|
2016-12-07 11:48:05 +01:00
|
|
|
# Process any APKs or ZIPs that are waiting to be signed...
|
|
|
|
for apkfile in sorted(glob.glob(os.path.join(unsigned_dir, '*.apk'))
|
|
|
|
+ glob.glob(os.path.join(unsigned_dir, '*.zip'))):
|
2012-02-26 15:18:58 +01:00
|
|
|
|
2016-12-07 11:48:05 +01:00
|
|
|
appid, vercode = common.publishednameinfo(apkfile)
|
2012-02-26 15:18:58 +01:00
|
|
|
apkfilename = os.path.basename(apkfile)
|
2013-12-19 22:55:17 +01:00
|
|
|
if vercodes and appid not in vercodes:
|
|
|
|
continue
|
|
|
|
if appid in vercodes and vercodes[appid]:
|
|
|
|
if vercode not in vercodes[appid]:
|
|
|
|
continue
|
2017-09-15 23:20:29 +02:00
|
|
|
logging.info(_("Processing {apkfilename}").format(apkfilename=apkfile))
|
2013-12-19 22:55:17 +01:00
|
|
|
|
2014-10-24 22:04:15 +02:00
|
|
|
# There ought to be valid metadata for this app, otherwise why are we
|
|
|
|
# trying to publish it?
|
2014-10-24 22:23:58 +02:00
|
|
|
if appid not in allapps:
|
2014-10-24 22:04:15 +02:00
|
|
|
logging.error("Unexpected {0} found in unsigned directory"
|
2014-10-24 22:23:58 +02:00
|
|
|
.format(apkfilename))
|
2014-10-24 22:04:15 +02:00
|
|
|
sys.exit(1)
|
|
|
|
app = allapps[appid]
|
|
|
|
|
2016-12-07 11:48:05 +01:00
|
|
|
if app.Binaries:
|
2014-10-24 22:04:15 +02:00
|
|
|
|
|
|
|
# It's an app where we build from source, and verify the apk
|
|
|
|
# contents against a developer's binary, and then publish their
|
|
|
|
# version if everything checks out.
|
2015-01-31 16:36:57 +01:00
|
|
|
# The binary should already have been retrieved during the build
|
|
|
|
# process.
|
2017-07-15 22:15:30 +02:00
|
|
|
srcapk = re.sub(r'.apk$', '.binary.apk', apkfile)
|
2014-10-24 22:04:15 +02:00
|
|
|
|
|
|
|
# Compare our unsigned one with the downloaded one...
|
2015-01-31 16:59:00 +01:00
|
|
|
compare_result = common.verify_apks(srcapk, apkfile, tmp_dir)
|
2014-10-24 22:04:15 +02:00
|
|
|
if compare_result:
|
2015-01-07 19:57:15 +01:00
|
|
|
logging.error("...verification failed - publish skipped : "
|
2014-10-24 22:23:58 +02:00
|
|
|
+ compare_result)
|
2014-10-24 22:04:15 +02:00
|
|
|
continue
|
|
|
|
|
2015-01-31 16:36:57 +01:00
|
|
|
# Success! So move the downloaded file to the repo, and remove
|
|
|
|
# our built version.
|
2014-10-24 22:04:15 +02:00
|
|
|
shutil.move(srcapk, os.path.join(output_dir, apkfilename))
|
2015-01-31 16:36:57 +01:00
|
|
|
os.remove(apkfile)
|
2014-10-24 22:04:15 +02:00
|
|
|
|
2016-12-07 11:48:05 +01:00
|
|
|
elif apkfile.endswith('.zip'):
|
|
|
|
|
|
|
|
# OTA ZIPs built by fdroid do not need to be signed by jarsigner,
|
|
|
|
# just to be moved into place in the repo
|
|
|
|
shutil.move(apkfile, os.path.join(output_dir, apkfilename))
|
|
|
|
|
2014-10-24 22:04:15 +02:00
|
|
|
else:
|
|
|
|
|
|
|
|
# It's a 'normal' app, i.e. we sign and publish it...
|
|
|
|
|
|
|
|
# Figure out the key alias name we'll use. Only the first 8
|
|
|
|
# characters are significant, so we'll use the first 8 from
|
|
|
|
# the MD5 of the app's ID and hope there are no collisions.
|
|
|
|
# If a collision does occur later, we're going to have to
|
|
|
|
# come up with a new alogrithm, AND rename all existing keys
|
|
|
|
# in the keystore!
|
|
|
|
if appid in config['keyaliases']:
|
|
|
|
# For this particular app, the key alias is overridden...
|
|
|
|
keyalias = config['keyaliases'][appid]
|
|
|
|
if keyalias.startswith('@'):
|
2016-01-04 18:36:47 +01:00
|
|
|
m = hashlib.md5()
|
2016-01-04 18:46:51 +01:00
|
|
|
m.update(keyalias[1:].encode('utf-8'))
|
2014-10-24 22:04:15 +02:00
|
|
|
keyalias = m.hexdigest()[:8]
|
|
|
|
else:
|
2016-01-04 18:36:47 +01:00
|
|
|
m = hashlib.md5()
|
2016-01-04 18:46:51 +01:00
|
|
|
m.update(appid.encode('utf-8'))
|
2012-02-26 15:18:58 +01:00
|
|
|
keyalias = m.hexdigest()[:8]
|
2014-10-24 22:04:15 +02:00
|
|
|
logging.info("Key alias: " + keyalias)
|
|
|
|
|
|
|
|
# See if we already have a key for this application, and
|
|
|
|
# if not generate one...
|
2017-04-11 21:34:49 +02:00
|
|
|
env_vars = {
|
|
|
|
'FDROID_KEY_STORE_PASS': config['keystorepass'],
|
|
|
|
'FDROID_KEY_PASS': config['keypass'],
|
|
|
|
}
|
2016-02-11 20:43:55 +01:00
|
|
|
p = FDroidPopen([config['keytool'], '-list',
|
2014-10-24 22:04:15 +02:00
|
|
|
'-alias', keyalias, '-keystore', config['keystore'],
|
2017-04-11 21:34:49 +02:00
|
|
|
'-storepass:env', 'FDROID_KEY_STORE_PASS'], envs=env_vars)
|
2014-10-24 22:04:15 +02:00
|
|
|
if p.returncode != 0:
|
|
|
|
logging.info("Key does not exist - generating...")
|
2016-02-11 20:43:55 +01:00
|
|
|
p = FDroidPopen([config['keytool'], '-genkey',
|
2014-10-24 22:04:15 +02:00
|
|
|
'-keystore', config['keystore'],
|
|
|
|
'-alias', keyalias,
|
|
|
|
'-keyalg', 'RSA', '-keysize', '2048',
|
|
|
|
'-validity', '10000',
|
2017-04-11 21:34:49 +02:00
|
|
|
'-storepass:env', 'FDROID_KEY_STORE_PASS',
|
|
|
|
'-keypass:env', 'FDROID_KEY_PASS',
|
|
|
|
'-dname', config['keydname']], envs=env_vars)
|
2014-10-24 22:04:15 +02:00
|
|
|
if p.returncode != 0:
|
|
|
|
raise BuildException("Failed to generate key")
|
|
|
|
|
2017-05-30 14:53:54 +02:00
|
|
|
signed_apk_path = os.path.join(output_dir, apkfilename)
|
|
|
|
if os.path.exists(signed_apk_path):
|
|
|
|
raise BuildException("Refusing to sign '{0}' file exists in both "
|
|
|
|
"{1} and {2} folder.".format(apkfilename,
|
|
|
|
unsigned_dir,
|
|
|
|
output_dir))
|
|
|
|
|
2014-10-24 22:04:15 +02:00
|
|
|
# Sign the application...
|
2016-02-11 20:43:55 +01:00
|
|
|
p = FDroidPopen([config['jarsigner'], '-keystore', config['keystore'],
|
2017-04-11 21:34:49 +02:00
|
|
|
'-storepass:env', 'FDROID_KEY_STORE_PASS',
|
|
|
|
'-keypass:env', 'FDROID_KEY_PASS', '-sigalg',
|
2015-09-25 03:50:51 +02:00
|
|
|
'SHA1withRSA', '-digestalg', 'SHA1',
|
2017-04-11 21:34:49 +02:00
|
|
|
apkfile, keyalias], envs=env_vars)
|
2012-02-02 15:27:49 +01:00
|
|
|
if p.returncode != 0:
|
2017-09-13 18:03:57 +02:00
|
|
|
raise BuildException(_("Failed to sign application"))
|
2014-10-24 22:04:15 +02:00
|
|
|
|
|
|
|
# Zipalign it...
|
2014-12-09 15:15:36 +01:00
|
|
|
p = SdkToolsPopen(['zipalign', '-v', '4', apkfile,
|
|
|
|
os.path.join(output_dir, apkfilename)])
|
2014-10-24 22:04:15 +02:00
|
|
|
if p.returncode != 0:
|
2017-09-13 18:03:57 +02:00
|
|
|
raise BuildException(_("Failed to align application"))
|
2014-10-24 22:04:15 +02:00
|
|
|
os.remove(apkfile)
|
2013-12-19 22:55:17 +01:00
|
|
|
|
|
|
|
# Move the source tarball into the output directory...
|
|
|
|
tarfilename = apkfilename[:-4] + '_src.tar.gz'
|
2014-02-17 16:51:42 +01:00
|
|
|
tarfile = os.path.join(unsigned_dir, tarfilename)
|
|
|
|
if os.path.exists(tarfile):
|
|
|
|
shutil.move(tarfile, os.path.join(output_dir, tarfilename))
|
2013-12-19 22:55:17 +01:00
|
|
|
|
2014-01-27 17:08:54 +01:00
|
|
|
logging.info('Published ' + apkfilename)
|
2012-02-26 15:18:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|