aboutsummaryrefslogtreecommitdiff
path: root/paludis/report_task.cc
blob: 89b47f7979ca2a13b55aa1edcda8a7a30cc2e68d (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Fernando J. Pereda
 *
 * 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 "report_task.hh"
#include <paludis/util/log.hh>
#include <paludis/uninstall_list.hh>
#include <paludis/environment.hh>
#include <paludis/metadata_key.hh>
#include <paludis/dep_tag.hh>
#include <paludis/package_id.hh>
#include <paludis/selection.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/visitor_cast.hh>
#include <paludis/util/set.hh>
#include <paludis/package_database.hh>
#include <paludis/version_requirements.hh>
#include <set>
#include <map>

using namespace paludis;

namespace
{
    class VulnerableChecker :
        public ConstVisitor<SetSpecTree>,
        public ConstVisitor<SetSpecTree>::VisitConstSequence<VulnerableChecker, AllDepSpec>
    {
        private:
            std::multimap<std::tr1::shared_ptr<const PackageID>, std::tr1::shared_ptr<const DepTag>, PackageIDSetComparator> _found;
            const Environment & _env;
            std::set<SetName> _recursing_sets;

        public:
            typedef std::multimap<std::tr1::shared_ptr<const PackageID>, std::tr1::shared_ptr<const DepTag>,
                    PackageIDSetComparator>::const_iterator ConstIterator;

            using ConstVisitor<SetSpecTree>::VisitConstSequence<VulnerableChecker, AllDepSpec>::visit;

            VulnerableChecker(const Environment & e) :
                _env(e)
            {
            }

            void visit_leaf(const PackageDepSpec &);

            void visit_leaf(const NamedSetDepSpec & s)
            {
                Context context("When expanding named set '" + stringify(s) + "':");

                std::tr1::shared_ptr<const SetSpecTree::ConstItem> set(_env.set(s.name()));
                if (! set)
                {
                    Log::get_instance()->message("report_task.unknown_set", ll_warning, lc_context) << "Unknown set '" << s.name() << "'";
                    return;
                }

                if (! _recursing_sets.insert(s.name()).second)
                {
                    Log::get_instance()->message("report_task.recursive_set", ll_warning, lc_context)
                        << "Recursively defined set '" << s.name() << "'";
                    return;
                }

                set->accept(*this);

                _recursing_sets.erase(s.name());
            }

            std::pair<ConstIterator, ConstIterator> insecure_tags(const std::tr1::shared_ptr<const PackageID> & id) const
            {
                return _found.equal_range(id);
            }
    };

    void
    VulnerableChecker::visit_leaf(const PackageDepSpec & a)
    {
        std::tr1::shared_ptr<const PackageIDSequence> insecure(_env[selection::AllVersionsSorted(generator::Matches(a))]);
        for (PackageIDSequence::ConstIterator i(insecure->begin()),
                i_end(insecure->end()) ; i != i_end ; ++i)
            if (a.tag() && visitor_cast<const GLSADepTag>(*a.tag()))
                _found.insert(std::make_pair(*i, a.tag()));
            else
                throw InternalError(PALUDIS_HERE, "didn't get a tag");
    }
}

namespace paludis
{
    template<>
    struct Implementation<ReportTask>
    {
        Environment * const env;

        Implementation(Environment * const e) :
            env(e)
        {
        }
    };
}

ReportTask::ReportTask(Environment * const env) :
    PrivateImplementationPattern<ReportTask>(new Implementation<ReportTask>(env))
{
}

ReportTask::~ReportTask()
{
}

void
ReportTask::execute()
{
    Context context("When executing report task:");

    on_report_all_pre();

    paludis::Environment * const e(_imp->env);
    bool once(true);

    VulnerableChecker vuln(*e);
    for (PackageDatabase::RepositoryConstIterator r(e->package_database()->begin_repositories()),
            r_end(e->package_database()->end_repositories()) ; r != r_end ; ++r)
    {
        std::tr1::shared_ptr<const Repository> rr(e->package_database()->fetch_repository((*r)->name()));
        if (! (*rr)[k::sets_interface()])
            continue;

        try
        {
            std::tr1::shared_ptr<const SetSpecTree::ConstItem> insecure((*rr)[k::sets_interface()]->package_set(SetName("insecurity")));
            if (! insecure)
                continue;
            insecure->accept(vuln);
        }
        catch (const NotAvailableError &)
        {
            if (once)
            {
                Log::get_instance()->message("report_task.skipping_glsas", ll_warning, lc_no_context)
                        << "Skipping GLSA checks because Paludis was built without XML support";
                once = false;
            }
        }
    }

    UninstallList unused_list(e, UninstallListOptions());
    unused_list.add_unused();
    std::set<std::tr1::shared_ptr<const PackageID>, PackageIDSetComparator> unused;
    for (UninstallList::ConstIterator i(unused_list.begin()), i_end(unused_list.end());
            i != i_end ; ++i)
        if (i->kind != ulk_virtual)
            unused.insert(i->package_id);

    for (PackageDatabase::RepositoryConstIterator r(e->package_database()->begin_repositories()),
            r_end(e->package_database()->end_repositories()) ; r != r_end ; ++r)
    {
        std::tr1::shared_ptr<const Repository> rr(e->package_database()->fetch_repository((*r)->name()));
        if (! rr->installed_root_key())
            continue;

        std::tr1::shared_ptr<const CategoryNamePartSet> cat_names(rr->category_names());
        for (CategoryNamePartSet::ConstIterator c(cat_names->begin()), c_end(cat_names->end()) ;
                    c != c_end ; ++c)
        {
            std::tr1::shared_ptr<const QualifiedPackageNameSet> packages(rr->package_names(*c));
            for (QualifiedPackageNameSet::ConstIterator p(packages->begin()), p_end(packages->end()) ;
                    p != p_end ; ++p)
            {
                on_report_check_package_pre(*p);

                std::tr1::shared_ptr<const PackageIDSequence> ids(rr->package_ids(*p));
                for (PackageIDSequence::ConstIterator v(ids->begin()), v_end(ids->end()) ;
                        v != v_end ; ++v)
                {
                    bool is_missing(false);
                    std::tr1::shared_ptr<const PackageID> origin;
                    std::tr1::shared_ptr<RepositoryName> repo_name;

                    if ((*v)->source_origin_key())
                    {
                        repo_name.reset(new RepositoryName((*v)->source_origin_key()->value()));

                        std::tr1::shared_ptr<const PackageIDSequence> installable(
                            (*e)[selection::BestVersionOnly(
                                generator::Matches(make_package_dep_spec()
                                    .package((*v)->name())
                                    .version_requirement(VersionRequirement(vo_equal, (*v)->version()))
                                    .repository(*repo_name)) |
                                filter::SupportsAction<InstallAction>())]);

                        if (installable->empty())
                            is_missing = ! ((*v)->transient_key() && (*v)->transient_key()->value());
                        else
                            origin = *installable->last();
                    }

                    bool is_masked(origin && origin->masked());
                    bool is_vulnerable(false);
                    bool is_unused(false);

                    std::pair<VulnerableChecker::ConstIterator, VulnerableChecker::ConstIterator> pi(vuln.insecure_tags(*v));
                    if (pi.first != pi.second)
                        is_vulnerable = true;

                    if (unused.end() != unused.find(*v))
                        is_unused = true;

                    if (is_masked || is_vulnerable || is_missing || is_unused)
                    {
                        on_report_package_failure_pre(*v);
                        if (is_masked)
                            on_report_package_is_masked(*v, origin);
                        if (is_vulnerable)
                        {
                            on_report_package_is_vulnerable_pre(*v);
                            for (VulnerableChecker::ConstIterator itag(pi.first) ; itag != pi.second ; ++itag)
                                on_report_package_is_vulnerable(*v, *static_cast<const GLSADepTag *>(itag->second.get()) );
                            on_report_package_is_vulnerable_post(*v);
                        }
                        if (is_missing)
                            on_report_package_is_missing(*v, *repo_name);
                        if (is_unused)
                            on_report_package_is_unused(*v);
                        on_report_package_failure_post(*v);
                    }
                    else
                        on_report_package_success(*v);
                }

                on_report_check_package_post(*p);
            }
        }
    }

    on_report_all_post();
}