~apachelogger/software-properties/python-apt-is-like

« back to all changes in this revision

Viewing changes to softwareproperties/AptAuth.py

  • Committer: Dimitri John Ledkov
  • Date: 2016-09-21 11:45:53 UTC
  • Revision ID: xnox@ubuntu.com-20160921114553-v6xo202os2tmkz7b
Port AptAuth.py from gpg command parsing to apt-key command
parsing. Simplifies invocations, and allows for proper handling of
simplified apt-secure(8) implementation with key fragments in
/etc/apt/trusted.gpg.d. Make the code gnupg1 and gnupg2
compatible. Display long keyids in the UI (i.e. anti-evil32 keys
feature). Revert dependency on gnupg1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from __future__ import print_function
23
23
 
24
24
import atexit
 
25
import datetime
25
26
import gettext
26
27
import os
27
28
import shutil
44
45
 
45
46
class AptAuth:
46
47
    def __init__(self, rootdir="/"):
47
 
        self.gpg = ["/usr/bin/gpg1"]
48
 
        self.base_opt = self.gpg + [
49
 
            "--no-options", 
50
 
            "--no-default-keyring",
51
 
            "--no-auto-check-trustdb",
52
 
            "--trust-model", "always",
53
 
            "--keyring", os.path.join(rootdir, "etc/apt/trusted.gpg"),
54
 
            ]
 
48
        self.rootdir = rootdir
55
49
        self.tmpdir = tempfile.mkdtemp()
56
 
        self.base_opt += ["--secret-keyring", 
57
 
                          os.path.join(self.tmpdir, "secring.gpg")]
58
 
        self.list_opt = self.base_opt + ["--with-colons",
59
 
                                         "--batch",
60
 
                                         "--list-keys"]
61
 
        self.rm_opt = self.base_opt + ["--quiet",
62
 
                                       "--batch",
63
 
                                       "--delete-key",
64
 
                                       "--yes"]
65
 
        self.add_opt = self.base_opt + ["--quiet", 
66
 
                                        "--batch",
67
 
                                        "--import"]
 
50
        self.aptconf = os.path.join(self.tmpdir, 'apt.conf')
 
51
        with open(self.aptconf, 'w') as f:
 
52
            f.write('DIR "%s";\n' % self.rootdir)
 
53
        os.environ['APT_CONFIG'] = self.aptconf
68
54
        atexit.register(self._cleanup_tmpdir)
69
55
 
70
56
    def _cleanup_tmpdir(self):
71
57
        shutil.rmtree(self.tmpdir)
72
 
       
 
58
 
73
59
    def list(self):
 
60
        cmd = ["/usr/bin/apt-key", "--quiet", "adv", "--with-colons", "--batch", "--fixed-list-mode", "--list-keys"]
74
61
        res = []
75
 
        #print(self.list_opt)
76
 
        p = subprocess.Popen(
77
 
            self.list_opt, stdout=PIPE, universal_newlines=True).stdout
 
62
        p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout
 
63
        name = ''
78
64
        for line in p:
79
65
            fields = line.split(":")
 
66
            if fields[0] in ["pub", "uid"]:
 
67
                name = fields[9]
80
68
            if fields[0] == "pub":
81
 
                name = fields[9]
82
 
                res.append("%s %s\n%s" %((fields[4])[-8:],fields[5], _(name)))
 
69
                key = fields[4]
 
70
                expiry = datetime.date.fromtimestamp(int(fields[5])).isoformat()
 
71
            if not name:
 
72
                continue
 
73
            res.append("%s %s\n%s" % (key, expiry, _(name)))
 
74
            name = ''
83
75
        p.close()
84
76
        return res
85
77
 
86
78
    def add(self, filename):
87
 
        #print("request to add " + filename)
88
 
        cmd = self.add_opt[:]
89
 
        cmd.append(filename)
90
 
        #print("cmd is: %s" % ' '.join(cmd))
 
79
        cmd = ["/usr/bin/apt-key", "--quiet", "--fakeroot", "add", filename]
91
80
        p = subprocess.Popen(cmd)
92
81
        return (p.wait() == 0)
93
82
        
94
83
    def update(self):
95
 
        cmd = ["/usr/bin/apt-key", "update"]
 
84
        cmd = ["/usr/bin/apt-key", "--quiet", "--fakeroot", "update"]
96
85
        p = subprocess.Popen(cmd)
97
86
        return (p.wait() == 0)
98
87
 
99
88
    def rm(self, key):
100
 
        #print("request to remove " + key)
101
 
        cmd = self.rm_opt[:]
102
 
        cmd.append(key)
 
89
        cmd = ["/usr/bin/apt-key", "--quiet", "--fakeroot", "rm", key]
103
90
        p = subprocess.Popen(cmd)
104
91
        return (p.wait() == 0)