2017-02-22 11:20:08 +00:00
|
|
|
#!/usr/bin/python3
|
2013-03-16 17:54:00 +00:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import shutil
|
|
|
|
import grp
|
2017-05-24 19:53:08 +00:00
|
|
|
import qubesadmin
|
2013-03-16 17:54:00 +00:00
|
|
|
|
|
|
|
updates_dir = "/var/lib/qubes/updates"
|
|
|
|
updates_rpm_dir = updates_dir + "/rpm"
|
|
|
|
updates_repodata_dir = updates_dir + "/repodata"
|
|
|
|
updates_error_file = updates_dir + "/errors"
|
|
|
|
|
|
|
|
comps_file = None
|
|
|
|
if os.path.exists('/usr/share/qubes/Qubes-comps.xml'):
|
|
|
|
comps_file = '/usr/share/qubes/Qubes-comps.xml'
|
|
|
|
|
2019-10-09 02:49:01 +00:00
|
|
|
package_regex = re.compile(r"^[A-Za-z0-9._+-]{1,128}\.rpm$")
|
2015-05-13 00:36:38 +00:00
|
|
|
# example valid outputs:
|
|
|
|
# .....rpm: rsa sha1 (md5) pgp md5 OK
|
|
|
|
# .....rpm: (sha1) dsa sha1 md5 gpg OK
|
2019-04-02 15:58:30 +00:00
|
|
|
# .....rpm: digests signatures OK
|
2015-05-13 00:36:38 +00:00
|
|
|
# example INVALID outputs:
|
|
|
|
# .....rpm: sha1 md5 OK
|
|
|
|
# .....rpm: RSA sha1 ((MD5) PGP) md5 NOT OK (MISSING KEYS: (MD5) PGP#246110c1)
|
2019-04-02 15:58:30 +00:00
|
|
|
# .....rpm: digests OK
|
|
|
|
gpg_ok_regex = re.compile(r": [a-z0-9() ]* (pgp|gpg|signatures) [a-z0-9 ]*OK$")
|
2013-03-16 17:54:00 +00:00
|
|
|
|
2017-02-22 11:20:34 +00:00
|
|
|
|
2019-10-09 02:49:01 +00:00
|
|
|
def dom0updates_fatal(msg):
|
2017-02-22 11:20:08 +00:00
|
|
|
print(msg, file=sys.stderr)
|
2019-10-09 02:49:01 +00:00
|
|
|
with open(updates_error_file, "a") as updates_error_file_handle:
|
|
|
|
updates_error_file_handle.write(msg + "\n")
|
|
|
|
shutil.rmtree(updates_rpm_dir)
|
|
|
|
exit(1)
|
2013-03-16 17:54:00 +00:00
|
|
|
|
2017-02-22 11:20:34 +00:00
|
|
|
|
2013-03-16 17:54:00 +00:00
|
|
|
def handle_dom0updates(updatevm):
|
2017-02-22 11:20:34 +00:00
|
|
|
source = os.getenv("QREXEC_REMOTE_DOMAIN")
|
2013-03-16 17:54:00 +00:00
|
|
|
if source != updatevm.name:
|
2017-02-22 11:20:08 +00:00
|
|
|
print('Domain ' + str(source) + ' not allowed to send dom0 updates',
|
|
|
|
file=sys.stderr)
|
2013-03-16 17:54:00 +00:00
|
|
|
exit(1)
|
|
|
|
# Clean old packages
|
|
|
|
if os.path.exists(updates_rpm_dir):
|
|
|
|
shutil.rmtree(updates_rpm_dir)
|
|
|
|
if os.path.exists(updates_repodata_dir):
|
|
|
|
shutil.rmtree(updates_repodata_dir)
|
|
|
|
if os.path.exists(updates_error_file):
|
|
|
|
os.remove(updates_error_file)
|
2014-05-05 02:00:52 +00:00
|
|
|
os.environ['LC_ALL'] = 'C'
|
2013-03-16 17:54:00 +00:00
|
|
|
qubes_gid = grp.getgrnam('qubes').gr_gid
|
2017-02-22 11:20:08 +00:00
|
|
|
old_umask = os.umask(0o002)
|
2013-03-16 17:54:00 +00:00
|
|
|
os.mkdir(updates_rpm_dir)
|
|
|
|
os.chown(updates_rpm_dir, -1, qubes_gid)
|
2017-02-22 11:20:08 +00:00
|
|
|
os.chmod(updates_rpm_dir, 0o0775)
|
2019-10-09 02:49:01 +00:00
|
|
|
try:
|
|
|
|
subprocess.check_call(["/usr/libexec/qubes/qfile-dom0-unpacker",
|
|
|
|
str(os.getuid()), updates_rpm_dir])
|
|
|
|
# Verify received files
|
|
|
|
for untrusted_f in os.listdir(updates_rpm_dir):
|
|
|
|
if not package_regex.match(untrusted_f):
|
|
|
|
raise Exception(
|
|
|
|
'Domain ' + source + ' sent unexpected file')
|
2013-03-16 17:54:00 +00:00
|
|
|
f = untrusted_f
|
2014-05-19 11:36:02 +00:00
|
|
|
assert '/' not in f
|
|
|
|
assert '\0' not in f
|
|
|
|
assert '\x1b' not in f
|
|
|
|
|
2013-03-16 17:54:00 +00:00
|
|
|
full_path = updates_rpm_dir + "/" + f
|
|
|
|
if os.path.islink(full_path) or not os.path.isfile(full_path):
|
2019-10-09 02:49:01 +00:00
|
|
|
raise Exception(
|
|
|
|
'Domain ' + source + ' sent not regular file')
|
2017-02-22 11:20:34 +00:00
|
|
|
p = subprocess.Popen(["/bin/rpm", "-K", full_path],
|
2013-03-16 17:54:00 +00:00
|
|
|
stdout=subprocess.PIPE)
|
2017-02-22 11:20:08 +00:00
|
|
|
output = p.communicate()[0].decode('ascii')
|
2013-03-16 17:54:00 +00:00
|
|
|
if p.returncode != 0:
|
2019-10-09 02:49:01 +00:00
|
|
|
raise Exception(
|
2017-02-22 11:20:34 +00:00
|
|
|
'Error while verifing %s signature: %s' % (f, output))
|
2013-03-16 17:54:00 +00:00
|
|
|
if not gpg_ok_regex.search(output.strip()):
|
2019-10-09 02:49:01 +00:00
|
|
|
raise Exception(
|
2017-02-22 11:20:34 +00:00
|
|
|
'Domain ' + source + ' sent not signed rpm: ' + f)
|
2019-10-09 02:49:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
dom0updates_fatal(str(e))
|
2013-03-16 17:54:00 +00:00
|
|
|
# After updates received - create repo metadata
|
2017-08-01 03:31:25 +00:00
|
|
|
createrepo_cmd = ["/usr/bin/createrepo_c"]
|
2013-03-16 17:54:00 +00:00
|
|
|
if comps_file:
|
|
|
|
createrepo_cmd += ["-g", comps_file]
|
|
|
|
createrepo_cmd += ["-q", updates_dir]
|
|
|
|
subprocess.check_call(createrepo_cmd)
|
|
|
|
os.chown(updates_repodata_dir, -1, qubes_gid)
|
2017-02-22 11:20:08 +00:00
|
|
|
os.chmod(updates_repodata_dir, 0o0775)
|
2013-03-16 17:54:00 +00:00
|
|
|
# Clean old cache
|
2017-02-22 11:20:34 +00:00
|
|
|
subprocess.call(["sudo", "/usr/bin/yum", "-q", "clean", "all"],
|
|
|
|
stdout=sys.stderr)
|
2014-06-30 14:16:59 +00:00
|
|
|
os.umask(old_umask)
|
2013-03-16 17:54:00 +00:00
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
2017-02-22 11:20:34 +00:00
|
|
|
def main():
|
2017-05-24 19:53:08 +00:00
|
|
|
app = qubesadmin.Qubes()
|
2013-03-16 17:54:00 +00:00
|
|
|
|
2016-02-11 01:38:59 +00:00
|
|
|
updatevm = app.updatevm
|
|
|
|
if updatevm is None:
|
|
|
|
exit(1)
|
2013-03-16 17:54:00 +00:00
|
|
|
handle_dom0updates(updatevm)
|
|
|
|
|
2019-10-09 02:49:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|