aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/qa/manifest.cc
blob: b65e6071afd1be6c9e21503d8e6e4ac78be07735 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Fernando J. Pereda
 * Copyright (c) 2008 David Leverton
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis 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
 */

#include <iostream>
#include "manifest.hh"
#include <paludis/qa.hh>
#include <paludis/dep_tree.hh>
#include <paludis/metadata_key.hh>
#include <paludis/util/log.hh>
#include <paludis/util/system.hh>
#include <paludis/util/map.hh>
#include <paludis/util/member_iterator-impl.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/sha1.hh>
#include <paludis/util/sha256.hh>
#include <paludis/util/rmd160.hh>
#include <paludis/util/md5.hh>
#include <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/e/layout.hh>
#include <paludis/repositories/e/manifest2_reader.hh>
#include <fstream>
#include <set>
#include <map>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace paludis;
using namespace paludis::erepository;

namespace
{
    struct DistfilesCollector :
        ConstVisitor<FetchableURISpecTree>,
        ConstVisitor<FetchableURISpecTree>::VisitConstSequence<DistfilesCollector, AllDepSpec>,
        ConstVisitor<FetchableURISpecTree>::VisitConstSequence<DistfilesCollector, UseDepSpec>
    {
        tr1::shared_ptr<const PackageID> id;
        std::multimap<std::string, tr1::shared_ptr<const PackageID> > & distfiles;

        using ConstVisitor<FetchableURISpecTree>::VisitConstSequence<DistfilesCollector, AllDepSpec>::visit_sequence;
        using ConstVisitor<FetchableURISpecTree>::VisitConstSequence<DistfilesCollector, UseDepSpec>::visit_sequence;

        DistfilesCollector(const tr1::shared_ptr<const PackageID> & i,
                           std::multimap<std::string, tr1::shared_ptr<const PackageID> > & d) :
            id(i),
            distfiles(d)
        {
        }

        void visit_leaf(const URILabelsDepSpec &)
        {
        }

        void visit_leaf(const FetchableURIDepSpec & u)
        {
            distfiles.insert(std::make_pair(u.filename(), id));
        }
    };

    struct Manifest2Checker
    {
        QAReporter & reporter;
        FSEntry dir;
        std::string name;

        FSEntry manifest;
        tr1::shared_ptr<Map<FSEntry, std::string> > files;
        tr1::shared_ptr<const PackageIDSequence> packages;

        std::set<FSEntry> accounted_files;
        std::multimap<std::string, tr1::shared_ptr<const PackageID> > distfiles;
        std::set<std::string> accounted_distfiles;

        Manifest2Checker(QAReporter & r, const FSEntry & d, const std::string & n,
                         const FSEntry & m, const tr1::shared_ptr<Map<FSEntry, std::string> > & f,
                         const tr1::shared_ptr<const PackageIDSequence> & p) :
            reporter(r),
            dir(d),
            name(n),
            manifest(m),
            files(f),
            packages(p)
        {
        }

        void check_file(const Manifest2Entry & entry)
        {
            if ("DIST" == entry.type)
            {
                if (distfiles.end() == distfiles.find(entry.name))
                {
                    QAMessage m(manifest, qaml_minor, name, "DIST file '" + entry.name + "' is not used by any package");
                    for (PackageIDSequence::ConstIterator it(packages->begin()),
                             it_end(packages->end()); it_end != it; ++it)
                    {
                        m = m.with_associated_id(*it);
                        if ((*it)->fetches_key())
                            m = m.with_associated_key(*it, (*it)->fetches_key());
                    }
                    reporter.message(m);
                }

                accounted_distfiles.insert(entry.name);
                return;
            }

            FSEntry file("AUX" == entry.type ? dir / "files" / entry.name : dir / entry.name);
            Map<FSEntry, std::string>::ConstIterator it(files->find(file));
            if (files->end() == it)
            {
                reporter.message(QAMessage(file, qaml_normal, name, "File is not present but listed in the Manifest"));
                return;
            }

            if (entry.type != it->second)
                reporter.message(QAMessage(file, qaml_normal, name, "File is of type '" + it->second + "', but Manifest lists '" + entry.type + "'"));
            if (entry.size != file.file_size())
                reporter.message(QAMessage(file, qaml_normal, name, "File size is '" + stringify(file.file_size()) + "', but Manifest lists '" + stringify(entry.size) + "'"));

            if (! entry.sha1.empty())
            {
                std::ifstream s(stringify(file).c_str());
                SHA1 sha1(s);
                if (entry.sha1 != sha1.hexsum())
                    reporter.message(QAMessage(file, qaml_normal, name, "File SHA1 is '" + sha1.hexsum() + "', but Manifest lists '" + entry.sha1 + "'"));

            }

            if (! entry.sha256.empty())
            {
                std::ifstream s(stringify(file).c_str());
                SHA256 sha256(s);
                if (entry.sha256 != sha256.hexsum())
                    reporter.message(QAMessage(file, qaml_normal, name, "File SHA256 is '" + sha256.hexsum() + "', but Manifest lists '" + entry.sha256 + "'"));

            }

            if (! entry.rmd160.empty())
            {
                std::ifstream s(stringify(file).c_str());
                RMD160 rmd160(s);
                if (entry.rmd160 != rmd160.hexsum())
                    reporter.message(QAMessage(file, qaml_normal, name, "File RMD160 is '" + rmd160.hexsum() + "', but Manifest lists '" + entry.rmd160 + "'"));

            }

            if (! entry.md5.empty())
            {
                std::ifstream s(stringify(file).c_str());
                MD5 md5(s);
                if (entry.md5 != md5.hexsum())
                    reporter.message(QAMessage(file, qaml_normal, name, "File MD5 is '" + md5.hexsum() + "', but Manifest lists '" + entry.md5 + "'"));

            }

            accounted_files.insert(it->first);
        }

        void check_unmanifested()
        {
            std::set<FSEntry> stray_files;
            std::set_difference(first_iterator(files->begin()), first_iterator(files->end()),
                                accounted_files.begin(), accounted_files.end(),
                                std::inserter(stray_files, stray_files.end()));

            for (std::set<FSEntry>::const_iterator it(stray_files.begin()),
                     it_end(stray_files.end()); it_end != it; ++it)
                reporter.message(QAMessage(*it, qaml_normal, name, "File is not listed in the Manifest"));

            std::set<std::string> unique_distfiles;
            std::unique_copy(first_iterator(distfiles.begin()), first_iterator(distfiles.end()),
                             std::inserter(unique_distfiles, unique_distfiles.end()));
            std::set<std::string> stray_distfiles;
            std::set_difference(unique_distfiles.begin(), unique_distfiles.end(),
                                accounted_distfiles.begin(), accounted_distfiles.end(),
                                std::inserter(stray_distfiles, stray_distfiles.end()));

            for (std::set<std::string>::const_iterator it(stray_distfiles.begin()),
                     it_end(stray_distfiles.end()); it_end != it; ++it)
            {
                QAMessage m(manifest, qaml_normal, name, "DIST file '" + *it + "' is not listed in the Manifest");
                for (std::multimap<std::string, tr1::shared_ptr<const PackageID> >::const_iterator it2(distfiles.lower_bound(*it)),
                         it2_end(distfiles.upper_bound(*it)); it2_end != it2; ++it2)
                    m = m.with_associated_id(it2->second)
                         .with_associated_key(it2->second, it2->second->fetches_key());
                reporter.message(m);
            }
        }
    };
}

bool
paludis::erepository::manifest_check(
        QAReporter & reporter,
        const FSEntry & dir,
        const tr1::shared_ptr<const ERepository> & repo,
        const QualifiedPackageName & qpn,
        const std::string & name
        )
{
    using namespace tr1::placeholders;

    Context context("When performing check '" + name + "' using manifest_check on directory '" + stringify(dir) + "':");
    Log::get_instance()->message(ll_debug, lc_context) << "manifest_check '"
        << dir << "', " << name << "'";

    try
    {
        FSEntry manifest(dir / "Manifest");

        if (! manifest.is_regular_file())
        {
            reporter.message(QAMessage(manifest, qaml_normal, name, "Manifest is missing or not a regular file"));
            return true;
        }

        tr1::shared_ptr<const PackageIDSequence> ids(repo->package_ids(qpn));
        Manifest2Checker checker(reporter, dir, name, manifest, repo->layout()->manifest_files(qpn), ids);
        for (PackageIDSequence::ConstIterator it(ids->begin()),
                 it_end(ids->end()); it_end != it; ++it)
        {
            if ((*it)->fetches_key())
            {
                DistfilesCollector c(*it, checker.distfiles);
                (*it)->fetches_key()->value()->accept(c);
            }
        }

        Manifest2Reader reader(manifest);
        std::for_each(reader.begin(), reader.end(), tr1::bind(&Manifest2Checker::check_file, tr1::ref(checker), _1));
        checker.check_unmanifested();

        bool is_signed(false);
        {
            std::ifstream ff(stringify(manifest).c_str());
            if (! ff)
            {
                reporter.message(QAMessage(manifest, qaml_normal, name, "Can't read Manifest file"));
                return true;
            }

            std::string s;
            if (std::getline(ff, s))
                is_signed = (0 == s.compare("-----BEGIN PGP SIGNED MESSAGE-----"));
        }

        if (is_signed)
        {
            int status(run_command("gpg --verify " + stringify(manifest) + " >/dev/null 2>/dev/null"));

            if (1 == status)
                reporter.message(QAMessage(manifest, qaml_normal, name, "Broken Manifest signature"));
            else if (2 == status)
                reporter.message(QAMessage(manifest, qaml_maybe, name, "Manifest signature cannot be verified"));
        }
        else
            reporter.message(QAMessage(manifest, qaml_minor, name, "Manifest not signed"));
    }
    catch (const Exception & e)
    {
        reporter.message(QAMessage(dir, qaml_severe, name,
                    "Caught exception '" + stringify(e.message()) + "' ("
                    + stringify(e.what()) + ") when checking Manifest for '" + stringify(qpn) + "'"));
    }

    return true;
}