aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/e_repository_sets.cc
blob: 186b28b6146db36080be3c0313b4d7ebc70767aa (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
 * Copyright (c) 2006 Danny van Dyk
 *
 * 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 <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/e/e_repository_sets.hh>
#include <paludis/repositories/e/glsa.hh>
#include <paludis/repositories/e/dep_parser.hh>
#include <paludis/repositories/e/eapi.hh>

#include <paludis/action-fwd.hh>
#include <paludis/dep_spec.hh>
#include <paludis/elike_slot_requirement.hh>
#include <paludis/environment.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/generator.hh>
#include <paludis/metadata_key.hh>
#include <paludis/package_id.hh>
#include <paludis/partially_made_package_dep_spec.hh>
#include <paludis/selection.hh>
#include <paludis/set_file.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/version_operator.hh>
#include <paludis/version_requirements.hh>

#include <paludis/util/config_file.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/log.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/make_null_shared_ptr.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/set.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/util/fs_iterator.hh>

#include <functional>
#include <algorithm>
#include <list>
#include <map>
#include <set>

#include "config.h"

using namespace paludis;
using namespace paludis::erepository;

namespace paludis
{
    /**
     * Imp data for ERepositorySets.
     *
     * \ingroup grperepository
     */
    template<>
    struct Imp<ERepositorySets>
    {
        const Environment * const environment;
        const ERepository * const e_repository;
        const erepository::ERepositoryParams params;

        Imp(const Environment * const e, const ERepository * const p,
                const erepository::ERepositoryParams & k) :
            environment(e),
            e_repository(p),
            params(k)
        {
        }
    };
}

ERepositorySets::ERepositorySets(const Environment * const e, const ERepository * const p,
        const erepository::ERepositoryParams & k) :
    _imp(e, p, k)
{
}

ERepositorySets::~ERepositorySets()
{
}

const std::shared_ptr<const SetSpecTree>
ERepositorySets::package_set(const SetName & ss) const
{
    using namespace std::placeholders;

    if ("system" == ss.value())
        throw InternalError(PALUDIS_HERE, "system set should've been handled by ERepository");
    else if ("security" == ss.value())
        return security_set(false);
    else if ("insecurity" == ss.value())
        return security_set(true);

    std::pair<SetName, SetFileSetOperatorMode> s(find_base_set_name_and_suffix_mode(ss));

    if ((_imp->params.setsdir() / (stringify(s.first) + ".conf")).stat().exists())
    {
        FSPath ff(_imp->params.setsdir() / (stringify(s.first) + ".conf"));
        Context context("When loading package set '" + stringify(s.first) + "' from '" + stringify(ff) + "':");

        SetFile f(make_named_values<SetFileParams>(
                    n::environment() = _imp->environment,
                    n::file_name() = ff,
                    n::parser() = std::bind(&parse_user_package_dep_spec, _1, _imp->environment, UserPackageDepSpecOptions() + updso_no_disambiguation + updso_throw_if_set, filter::All()),
                    n::set_operator_mode() = s.second,
                    n::type() = sft_paludis_conf
                    ));

        return f.contents();
    }
    else
        return make_null_shared_ptr();
}

std::shared_ptr<const SetNameSet>
ERepositorySets::sets_list() const
{
    Context context("While generating the list of sets:");

    std::shared_ptr<SetNameSet> result(std::make_shared<SetNameSet>());
    result->insert(SetName("insecurity"));
    result->insert(SetName("security"));
    result->insert(SetName("system"));

    if (_imp->params.setsdir().stat().exists())
    {
        using namespace std::placeholders;

        for (FSIterator f(_imp->params.setsdir(), { }), f_end ; f != f_end ; ++f)
        {
            if (! is_file_with_extension(*f, ".conf", { }))
                continue;

            try
            {
                result->insert(SetName(strip_trailing_string(f->basename(), ".conf")));
            }
            catch (const NameError & e)
            {
                Log::get_instance()->message("e.sets.failure", ll_warning, lc_context) << "Skipping set '"
                    << *f << "' due to exception '" << e.message() << "' (" << e.what() << ")";
            }
        }
    }

    return result;
}

namespace
{
    bool
    match_range(const PackageID & e, const erepository::GLSARange & r, const VersionSpecOptions & ver_options, const ELikePackageDepSpecOptions & pds_options)
    {
        if (r.slot() != "*")
        {
            try
            {
                if ((! e.slot_key()) || (e.slot_key()->value() != SlotName(r.slot())))
                    return false;
            }
            catch (const SlotNameError &)
            {
                throw GLSAError("Got bad slot '" + r.slot() + "'");
            }
        }

        VersionOperatorValue our_op(static_cast<VersionOperatorValue>(-1));
        std::string ver(r.version());
        if (r.op() == "le")
            our_op = vo_less_equal;
        if (r.op() == "lt")
            our_op = vo_less;
        if (r.op() == "eq")
        {
            if (! ver.empty() && '*' == ver.at(ver.length() - 1))
            {
                ver.erase(ver.length() - 1);
                our_op = pds_options[epdso_nice_equal_star] ? vo_nice_equal_star : vo_stupid_equal_star;
            }
            else
                our_op = vo_equal;
        }
        if (r.op() == "gt")
            our_op = vo_greater;
        if (r.op() == "ge")
            our_op = vo_greater_equal;

        if (-1 != our_op)
            return (VersionOperator(our_op).as_version_spec_comparator()(e.version(), VersionSpec(ver, ver_options)));

        if (0 == r.op().compare(0, 1, "r"))
        {
            return e.version().remove_revision() == VersionSpec(ver, ver_options).remove_revision() &&
                match_range(e, make_named_values<erepository::GLSARange>(
                            n::op() = r.op().substr(1),
                            n::slot() = r.slot(),
                            n::version() = r.version()),
                        ver_options, pds_options);
        }

        throw GLSAError("Got bad op '" + r.op() + "'");
    }

    bool
    is_vulnerable(const GLSAPackage & glsa_pkg, const PackageID & c, const VersionSpecOptions & ver_options, const ELikePackageDepSpecOptions & pds_options)
    {
        /* a package is affected if it matches any vulnerable line, except if it matches
         * any unaffected line. */
        bool vulnerable(false);
        for (GLSAPackage::RangesConstIterator r(glsa_pkg.begin_vulnerable()), r_end(glsa_pkg.end_vulnerable()) ;
                r != r_end && ! vulnerable ; ++r)
            if (match_range(c, *r, ver_options, pds_options))
                vulnerable = true;

        if (! vulnerable)
            return false;

        for (GLSAPackage::RangesConstIterator r(glsa_pkg.begin_unaffected()), r_end(glsa_pkg.end_unaffected()) ;
                r != r_end && vulnerable ; ++r)
            if (match_range(c, *r, ver_options, pds_options))
                vulnerable = false;

        return vulnerable;
    }
}

const std::shared_ptr<const SetSpecTree>
ERepositorySets::security_set(bool insecurity) const
{
    Context context("When building security or insecurity package set:");

    std::shared_ptr<SetSpecTree> security_packages(std::make_shared<SetSpecTree>(std::make_shared<AllDepSpec>()));

    if (!_imp->params.securitydir().stat().is_directory_or_symlink_to_directory())
        return security_packages;

    for (FSIterator f(_imp->params.securitydir(), { }), f_end ; f != f_end; ++f)
    {
        if (! is_file_with_prefix_extension(*f, "glsa-", ".xml", { }))
            continue;

        Context local_context("When parsing security advisory '" + stringify(*f) + "':");

        const std::shared_ptr<const EAPI> eapi(EAPIData::get_instance()->eapi_from_string(_imp->e_repository->eapi_for_file(*f)));
        if (! eapi->supported())
            throw GLSAError("Can't use advisory '" + stringify(*f) +
                    "' because it uses an unsupported EAPI");

        const VersionSpecOptions ver_options(eapi->supported()->version_spec_options());
        const ELikePackageDepSpecOptions pds_options(eapi->supported()->package_dep_spec_parse_options());

        try
        {
            std::shared_ptr<const GLSA> glsa(GLSA::create_from_xml_file(stringify(*f)));
            Context local_local_context("When handling GLSA '" + glsa->id() + "' from '" +
                    stringify(*f) + "':");

            for (GLSA::PackagesConstIterator glsa_pkg(glsa->begin_packages()),
                    glsa_pkg_end(glsa->end_packages()) ; glsa_pkg != glsa_pkg_end ; ++glsa_pkg)
            {
                std::shared_ptr<const PackageIDSequence> candidates;
                if (insecurity)
                    candidates = (*_imp->environment)[selection::AllVersionsSorted(generator::Package(glsa_pkg->name()))];
                else
                    candidates = (*_imp->environment)[selection::AllVersionsSorted(
                            generator::Package(glsa_pkg->name()) |
                            filter::InstalledAtRoot(_imp->environment->preferred_root_key()->value()))];

                for (PackageIDSequence::ConstIterator c(candidates->begin()), c_end(candidates->end()) ;
                        c != c_end ; ++c)
                {
                    if (! is_vulnerable(*glsa_pkg, **c, ver_options, pds_options))
                        continue;

                    if (insecurity)
                    {
                        std::shared_ptr<PackageDepSpec> spec(std::make_shared<PackageDepSpec>(
                                    make_package_dep_spec({ })
                                    .package((*c)->name())
                                    .version_requirement(make_named_values<VersionRequirement>(
                                            n::version_operator() = vo_equal,
                                            n::version_spec() = (*c)->version()))
                                    .in_repository((*c)->repository_name())));
                        security_packages->top()->append(spec);
                    }
                    else
                    {
                        Context local_local_local_context("When finding upgrade for '" + stringify(glsa_pkg->name()) + ":"
                                + ((*c)->slot_key() ? stringify((*c)->slot_key()->value()) : "(none)") + "'");

                        /* we need to find the best not vulnerable installable package that isn't masked
                         * that's in the same slot as our vulnerable installed package. */
                        bool ok(false);
                        std::shared_ptr<const PackageIDSequence> available(
                                (*_imp->environment)[selection::AllVersionsSorted(
                                    generator::Package(glsa_pkg->name()) |
                                    filter::SameSlot(*c) |
                                    filter::SupportsAction<InstallAction>() |
                                    filter::NotMasked())]);

                        for (PackageIDSequence::ReverseConstIterator r(available->rbegin()), r_end(available->rend()) ; r != r_end ; ++r)
                        {
                            if (is_vulnerable(*glsa_pkg, **r, ver_options, pds_options))
                            {
                                Log::get_instance()->message("e.glsa.skipping_vulnerable", ll_debug, lc_context)
                                    << "Skipping '" << **r << "' due to is_vulnerable match";
                                continue;
                            }

                            std::shared_ptr<PackageDepSpec> spec(std::make_shared<PackageDepSpec>(make_package_dep_spec({ })
                                        .package((*r)->name())
                                        .version_requirement(make_named_values<VersionRequirement>(
                                                n::version_operator() = vo_equal,
                                                n::version_spec() = (*r)->version()))
                                        .in_repository((*r)->repository_name())));
                            security_packages->top()->append(spec);
                            ok = true;
                            break;
                        }

                        if (! ok)
                            throw GLSAError("Could not determine upgrade path to resolve '"
                                    + glsa->id() + ": " + glsa->title() + "' for package '"
                                    + stringify(**c) + "'");
                    }
                }
            }
        }
        catch (const GLSAError & e)
        {
            Log::get_instance()->message("e.glsa.failure", ll_warning, lc_context)
                << "Cannot use GLSA '" << *f << "' due to exception '" << e.message() << "' (" << e.what() << ")";
        }
        catch (const NameError & e)
        {
            Log::get_instance()->message("e.glsa.failure", ll_warning, lc_context)
                << "Cannot use GLSA '" << *f << "' due to exception '" << e.message() << "' (" << e.what() << ")";
        }
    }

    return security_packages;
}