summaryrefslogtreecommitdiff
path: root/lib/thandy/repository.py
blob: 1385dd6b0a3a09615b28a3959c25a01c838960e3 (plain)
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
# Copyright 2008 The Tor Project, Inc.  See LICENSE for licensing information.

import thandy.formats
import thandy.util

try:
    import json
except ImportError:
    import simplejson as json

import logging
import os
import threading
import time

MAX_TIMESTAMP_AGE = 24*60*60

class RepositoryFile:
    def __init__(self, repository, relativePath, schema,
                 needRole=None, signedFormat=True, needSigs=1):
        self._repository = repository
        self._relativePath = relativePath
        self._schema = schema
        self._needRole = needRole
        self._signedFormat = signedFormat
        self._needSigs = needSigs

        self._signed_obj = self._main_obj = None
        self._sigStatus = None
        self._mtime = None

    def getRelativePath(self):
        return self._relativePath

    def getPath(self):
        return self._repository.getFilename(self._relativePath)

    def _load(self):
        fname = self.getPath()

        # Propagate OSError
        f = None
        fd = os.open(fname, os.O_RDONLY)
        try:
            f = os.fdopen(fd, 'r')
        except:
            os.close(fd)
            raise
        try:
            mtime = os.fstat(fd).st_mtime
            content = f.read()
        finally:
            f.close()

        signed_obj,main_obj = self._checkContent(content)

        self._signed_obj = signed_obj
        self._main_obj = main_obj
        self._mtime = mtime

    def _save(self, content=None):
        if content == None:
            content = sexpr.encode

        signed_obj,main_obj = self._checkContent(content)

        fname = self.getPath()
        thandy.util.replaceFile(fname, contents)

        self._signed_obj = signed_obj
        self._main_obj = main_obj
        self._mtime = mtime

    def _checkContent(self, content):

        try:
            obj = json.loads(content)
        except ValueError, e:
            raise thandy.FormatException("Couldn't decode content: %s"%e)

        if self._signedFormat:
            # This is supposed to be signed.
            thandy.formats.SIGNED_SCHEMA.checkMatch(obj)

            main_obj = obj['signed']
            signed_obj = obj
        else:
            signed_obj = None
            main_obj = obj

        if self._schema != None:
            self._schema.checkMatch(main_obj)

        return signed_obj, main_obj

    def load(self):
        if self._main_obj == None:
            self._load()

    def get(self):
        return self._main_obj

    def isLoaded(self):
        return self._main_obj != None

    def getContent(self):
        self.load()
        return self._main_obj

    def _checkSignatures(self):
        self.load()
        sigStatus = thandy.formats.checkSignatures(self._signed_obj,
                                     self._repository._keyDB,
                                     self._needRole, self._relativePath)
        self._sigStatus = sigStatus

    def checkSignatures(self):
        if self._sigStatus is None:
            self._checkSignatures()
        return self._sigStatus

class LocalRepository:
    def __init__(self, root):
        self._root = root
        self._keyDB = thandy.util.getKeylist(None)

        self._keylistFile = RepositoryFile(
            self, "/meta/keys.txt", thandy.formats.KEYLIST_SCHEMA,
            needRole="master")
        self._timestampFile = RepositoryFile(
            self, "/meta/timestamp.txt", thandy.formats.TIMESTAMP_SCHEMA,
            needRole="timestamp")
        self._mirrorlistFile = RepositoryFile(
            self, "/meta/mirrors.txt", thandy.formats.MIRRORLIST_SCHEMA,
            needRole="mirrors")
        self._metaFiles = [ self._keylistFile,
                            self._timestampFile,
                            self._mirrorlistFile ]

        self._packageFiles = {}
        self._bundleFiles = {}

    def getFilename(self, relativePath):
        if relativePath.startswith("/"):
            relativePath = relativePath[1:]
        return os.path.join(self._root, relativePath)

    def getKeylistFile(self):
        return self._keylistFile

    def getTimestampFile(self):
        return self._timestampFile

    def getMirrorlistFile(self):
        return self._mirrorlistFile

    def getPackageFile(self, relPath):
        try:
            return self._packageFiles[relPath]
        except KeyError:
            self._packageFiles[relPath] = pkg = RepositoryFile(
                self, relPath, thandy.formats.PACKAGE_SCHEMA,
                needRole='package')
            return pkg

    def getBundleFile(self, relPath):
        try:
            return self._bundleFiles[relPath]
        except KeyError:
            self._bundleFiles[relPath] = pkg = RepositoryFile(
                self, relPath, thandy.formats.BUNDLE_SCHEMA,
                needRole='bundle')
            return pkg

    def getFilesToUpdate(self, now=None, trackingBundles=()):
        if now == None:
            now = time.time()

        need = set()

        # Fetch missing metafiles.
        for f in self._metaFiles:
            try:
                f.load()
            except OSError, e:
                print "need", f.getPath()
                logging.info("Couldn't load %s: %s.  Must fetch it.",
                             f.getPath(), e)
                need.add(f.getRelativePath())

        # If the timestamp file is out of date, we need to fetch it no
        # matter what.  (Even if it is isn't signed, it can't possibly
        # be good.)
        ts = self._timestampFile.get()
        if ts:
            age = now - thandy.formats.parseTime(ts['at'])
            ts = thandy.formats.TimestampFile.fromJSon(ts)
            if age > MAX_TIMESTAMP_AGE:
                need.add(self._timestampFile.getRelativePath())

        # If the keylist isn't signed right, we can't check the
        # signatures on anything else.
        if self._keylistFile.get():
            s = self._keylistFile.checkSignatures()
            if not s.isValid(): # For now only require one master key.
                need.add(self._keylistFile.getRelativePath())

        if need:
            return need

        # Import the keys from the keylist.
        self._keyDB.addFromKeylist(self._keylistFile.get())

        # If the timestamp isn't signed right, get a new timestamp and a
        # new keylist.
        s = self._timestampFile.checkSignatures()
        if not s.isValid():
            need.add(self._keylistFile.getRelativePath())
            need.add(self._timestampFile.getRelativePath())
            return need

        # FINALLY, we know we have an up-to-date, signed timestamp
        # file.  Check whether the keys and mirrors file are as
        # authenticated.
        h_kf = thandy.formats.getDigest(self._keylistFile.get())
        h_expected = ts.getKeylistInfo().getHash()
        if h_kf != h_expected:
            need.add(self._keylistFile.getRelativePath())

        if need:
            return need

        s = self._mirrorlistFile.checkSignatures()
        if not s.isValid():
            need.add(self._mirrorlistFile.getRelativePath())

        h_mf = thandy.formats.getDigest(self._mirrorlistFile.get())
        h_expected = ts.getMirrorlistInfo().getHash()
        if h_mf != h_expected:
            need.add(self._mirrorlistFile.getRelativePath())

        if need:
            return need

        # Okay; that's it for the metadata.  Do we have the right
        # bundles?
        bundles = {}
        for b in trackingBundles:
            try:
                binfo = ts.getBundleInfo(b)
            except KeyError:
                logging.warn("Unrecognized bundle %s"%b)
                continue

            rp = binfo.getRelativePath()
            bfile = self.getBundleFile(rp)
            try:
                bfile.load()
            except OSError:
                need.add(rp)
                continue

            h_b = thandy.formats.getDigest(bfile.get())
            h_expected = binfo.getHash()
            if h_b != h_expected:
                need.add(rp)
                continue

            s = bfile.checkSignatures()
            if not s.isValid():
                # Can't actually use it.
                continue

            bundles[rp] = bfile

        # Okay.  So we have some bundles.  See if we have their packages.
        packages = {}
        for bfile in bundles.values():
            bundle = bfile.get()
            for pkginfo in bundle['packages']:
                rp = pkginfo['path']
                pfile = self.getPackageFile(rp)
                try:
                    pfile.load()
                except OSError:
                    need.add(rp)
                    continue

                h_p = thandy.formats.getDigest(pfile.get())
                h_expected = thandy.formats.parseHash(pkginfo['hash'])
                if h_p != h_expected:
                    need.add(rp)
                    continue

                s = pfile.checkSignatures()
                if not s.isValid():
                    # Can't use it.
                    continue
                packages[rp] = pfile

        # Finally, we have some packages.  Do we have their underlying
        # files?
        for pfile in packages.values():
            package = pfile.get()
            for f in package['files']:
                rp, h = f[:2]
                h_expected = thandy.formats.parseHash(h)
                fn = self.getFilename(rp)
                try:
                    h_got = thandy.formats.getFileDigest(fn)
                except OSError:
                    need.add(rp)
                    continue
                if h_got != h_expected:
                    need.add(rp)

        # Okay; these are the files we need.
        return need