This file is indexed.

/usr/lib/python3/dist-packages/DistUpgrade/DistUpgradeAptCdrom.py is in python3-distupgrade 1:16.04.12.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# DistUpgradeAptCdrom.py 
#  
#  Copyright (c) 2008 Canonical
#  
#  Author: Michael Vogt <michael.vogt@ubuntu.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import re
import os
import apt
import apt_pkg
import logging
import gzip
import shutil
import subprocess
import sys
from gettext import gettext as _


class AptCdromError(Exception):
    """ base exception for apt cdrom errors """
    pass


class AptCdrom(object):
    """ represents a apt cdrom object """

    def __init__(self, view, path):
        self.view = view
        self.cdrompath = path
        # the directories we found on disk with signatures, packages and i18n
        self.packages = set()
        self.signatures = set()
        self.i18n = set()

    def restore_backup(self, backup_ext):
        """ restore the backup copy of the cdroms.list file 
           (*not* sources.list)! 
        """
        cdromstate = os.path.join(apt_pkg.config.find_dir("Dir::State"),
                                  apt_pkg.config.find("Dir::State::cdroms"))
        if os.path.exists(cdromstate + backup_ext):
            shutil.copy(cdromstate + backup_ext, cdromstate)
        # mvo: we don't have to care about restoring the sources.list here
        #      because aptsources will do this for us anyway

    def comment_out_cdrom_entry(self):
        """ comment out the cdrom entry """
        diskname = self._readDiskName()
        pentry = self._generateSourcesListLine(diskname, self.packages)
        sourceslist = apt_pkg.config.find_file("Dir::Etc::sourcelist")
        with open(sourceslist) as f:
            content = f.read()
        content = content.replace(pentry, "# %s" % pentry)
        with open(sourceslist, "w") as f:
            f.write(content)

    def _scanCD(self):
        """ 
        scan the CD for interessting files and return them as:
        (packagesfiles, signaturefiles, i18nfiles)
        """
        packages = set()
        signatures = set()
        i18n = set()
        for root, dirs, files in os.walk(self.cdrompath, topdown=True):
            if (root.endswith("debian-installer") or 
                    root.endswith("dist-upgrader")):
                del dirs[:]
                continue
            elif ".aptignr" in files:
                continue
            elif "Packages" in files:
                packages.add(os.path.join(root, "Packages"))
            elif "Packages.gz" in files:
                packages.add(os.path.join(root, "Packages.gz"))
            elif "Sources" in files or "Sources.gz" in files:
                logging.error(
                    "Sources entry found in %s but not supported" % root)
            elif "Release.gpg" in files:
                signatures.add(os.path.join(root, "Release.gpg"))
            elif "i18n" in dirs:
                for f in os.listdir(os.path.join(root, "i18n")):
                    i18n.add(os.path.join(root, "i18n", f))
            # there is nothing under pool but deb packages (no
            # indexfiles, so we skip that here
            elif os.path.split(root)[1] == "pool":
                del dirs[:]
        return (packages, signatures, i18n)

    def _writeDatabase(self):
        """ update apts cdrom.list """
        dbfile = apt_pkg.config.find_file("Dir::State::cdroms")
        cdrom = apt_pkg.Cdrom()
        id = cdrom.ident(apt.progress.base.CdromProgress())
        label = self._readDiskName()
        with open(dbfile, "a") as out:
            out.write('CD::%s "%s";\n' % (id, label))
            out.write('CD::%s::Label "%s";\n' % (id, label))

    def _dropArch(self, packages):
        """ drop architectures that are not ours """
        # create a copy
        packages = set(packages)
        # now go over the packagesdirs and drop stuff that is not
        # our binary-$arch 
        arch = apt_pkg.config.find("APT::Architecture")
        for d in set(packages):
            if "/binary-" in d and arch not in d:
                packages.remove(d)
        return packages
    
    def _readDiskName(self):
        # default to cdrompath if there is no name
        diskname = self.cdrompath
        info = os.path.join(self.cdrompath, ".disk", "info")
        if os.path.exists(info):
            with open(info) as f:
                diskname = f.read()
            for special in ('"', ']', '[', '_'):
                diskname = diskname.replace(special, '_')
        return diskname

    def _generateSourcesListLine(self, diskname, packages):
        # see apts indexcopy.cc:364 for details
        path = ""                                    
        dist = ""
        comps = []
        for d in packages:
            # match(1) is the path, match(2) the dist
            # and match(3) the components
            m = re.match("(.*)/dists/([^/]*)/(.*)/binary-*", d)
            if not m:
                raise AptCdromError(
                    _("Could not calculate sources.list entry"))
            path = m.group(1)
            dist = m.group(2)
            comps.append(m.group(3))
        if not path or not comps:
            return None
        comps.sort()
        pentry = "deb cdrom:[%s]/ %s %s" % (diskname, dist, " ".join(comps))
        return pentry

    def _copyTranslations(self, translations, targetdir=None):
        if not targetdir:
            targetdir = apt_pkg.config.find_dir("Dir::State::lists")
        diskname = self._readDiskName()
        for f in translations:
            fname = apt_pkg.uri_to_filename(
                "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):]))
            outf = os.path.join(targetdir, os.path.splitext(fname)[0])
            if f.endswith(".gz"):
                with gzip.open(f) as g, open(outf, "wb") as out:
                    # uncompress in 64k chunks
                    while True:
                        s = g.read(64000)
                        out.write(s)
                        if s == b"":
                            break
            else:
                shutil.copy(f, outf)
        return True

    def _copyPackages(self, packages, targetdir=None):
        if not targetdir:
            targetdir = apt_pkg.config.find_dir("Dir::State::lists")
        # CopyPackages()
        diskname = self._readDiskName()
        for f in packages:
            fname = apt_pkg.uri_to_filename(
                "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):]))
            outf = os.path.join(targetdir, os.path.splitext(fname)[0])
            if f.endswith(".gz"):
                with gzip.open(f) as g, open(outf, "wb") as out:
                    # uncompress in 64k chunks
                    while True:
                        s = g.read(64000)
                        out.write(s)
                        if s == b"":
                            break
            else:
                shutil.copy(f, outf)
        return True

    def _verifyRelease(self, signatures):
        " verify the signatues and hashes "
        gpgv = apt_pkg.config.find("Dir::Bin::gpg", "/usr/bin/gpgv")
        keyring = apt_pkg.config.find("Apt::GPGV::TrustedKeyring",
                                      "/etc/apt/trusted.gpg")
        for sig in signatures:
            basepath = os.path.split(sig)[0]
            # do gpg checking
            releasef = os.path.splitext(sig)[0]
            cmd = [gpgv, "--keyring", keyring,
                   "--ignore-time-conflict",
                   sig, releasef]
            ret = subprocess.call(cmd)
            if not (ret == 0):
                return False
            # now do the hash sum checks
            with open(releasef) as f:
                t = apt_pkg.TagFile(f)
                t.step()
                sha256_section = t.section["SHA256"]
            for entry in sha256_section.split("\n"):
                (hash, size, name) = entry.split()
                f = os.path.join(basepath, name)
                if not os.path.exists(f):
                    logging.info("ignoring missing '%s'" % f)
                    continue
                with open(f) as fp:
                    sum = apt_pkg.sha256sum(fp)
                if not (sum == hash):
                    logging.error(
                        "hash sum mismatch expected %s but got %s" % (
                            hash, sum))
                    return False
        return True

    def _copyRelease(self, signatures, targetdir=None):
        " copy the release file "
        if not targetdir:
            targetdir = apt_pkg.config.find_dir("Dir::State::lists")
        diskname = self._readDiskName()
        for sig in signatures:
            releasef = os.path.splitext(sig)[0]
            # copy both Release and Release.gpg
            for f in (sig, releasef):
                fname = apt_pkg.uri_to_filename(
                    "cdrom:[%s]/%s" % (diskname, f[f.find("dists"):]))
                shutil.copy(f, os.path.join(targetdir, fname))
        return True

    def _doAdd(self):
        " reimplement pkgCdrom::Add() in python "
        # os.walk() will not follow symlinks so we don't need
        # pkgCdrom::Score() and not dropRepeats() that deal with
        # killing the links
        (self.packages, self.signatures, self.i18n) = self._scanCD()
        self.packages = self._dropArch(self.packages)
        if len(self.packages) == 0:
            logging.error("no useable indexes found on CD, wrong ARCH?")
            raise AptCdromError(
                _("Unable to locate any package files, "
                  "perhaps this is not a Ubuntu Disc or the wrong "
                  "architecture?"))

        # CopyAndVerify
        if self._verifyRelease(self.signatures):
            self._copyRelease(self.signatures)
        
        # copy the packages and translations
        self._copyPackages(self.packages)
        self._copyTranslations(self.i18n)

        # add CD to cdroms.list "database" and update sources.list
        diskname = self._readDiskName()
        if not diskname:
            logging.error("no .disk/ directory found")
            return False
        debline = self._generateSourcesListLine(diskname, self.packages)
        
        # prepend to the sources.list
        sourceslist = apt_pkg.config.find_file("Dir::Etc::sourcelist")
        with open(sourceslist) as f:
            content = f.read()
        with open(sourceslist, "w") as f:
            f.write(
                "# added by the release upgrader\n%s\n%s" %
                (debline, content))
        self._writeDatabase()

        return True

    def add(self, backup_ext=None):
        " add a cdrom to apt's database "
        logging.debug("AptCdrom.add() called with '%s'", self.cdrompath)
        # do backup (if needed) of the cdroms.list file
        if backup_ext:
            cdromstate = os.path.join(
                apt_pkg.config.find_dir("Dir::State"),
                apt_pkg.config.find("Dir::State::cdroms"))
            if os.path.exists(cdromstate):
                shutil.copy(cdromstate, cdromstate + backup_ext)
        # do the actual work
        apt_pkg.config.set("Acquire::cdrom::mount", self.cdrompath)
        apt_pkg.config.set("APT::CDROM::NoMount", "true")
        # FIXME: add cdrom progress here for the view
        #progress = self.view.getCdromProgress()
        try:
            res = self._doAdd()
        except (SystemError, AptCdromError) as e:
            logging.error("can't add cdrom: %s" % e)
            self.view.error(_("Failed to add the CD"),
                            _("There was a error adding the CD, the "
                              "upgrade will abort. Please report this as "
                              "a bug if this is a valid Ubuntu CD.\n\n"
                              "The error message was:\n'%s'") % e)
            return False
        logging.debug("AptCdrom.add() returned: %s" % res)
        return res

    def __bool__(self):
        """ helper to use this as 'if cdrom:' """
        return self.cdrompath is not None

    if sys.version < '3':
        __nonzero__ = __bool__