aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/portage/portage_repository_sets.cc
blob: 0699cef3b7337c8c7de0dda14069740858c79cbf (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
364
365
366
367
368
369
370
371
372
373
374
375
376
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2005, 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 * Copyright (c) 2006 Danny van Dyk <kugelfang@gentoo.org>
 *
 * 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/portage/portage_repository.hh>
#include <paludis/repositories/portage/portage_repository_sets.hh>
#include <paludis/repositories/portage/glsa.hh>

#include <paludis/dep_list/dep_list.hh>
#include <paludis/environment.hh>
#include <paludis/config_file.hh>
#include <paludis/query.hh>
#include <paludis/portage_dep_parser.hh>
#include <paludis/util/collection_concrete.hh>
#include <paludis/util/dir_iterator.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/log.hh>
#include <paludis/util/tokeniser.hh>

#include <list>
#include <set>

#include "config.h"

using namespace paludis;

namespace paludis
{
    /**
     * Implementation data for PortageRepositorySets.
     *
     * \ingroup grpportagerepository
     */
    template<>
    struct Implementation<PortageRepositorySets>
    {
        const Environment * const environment;
        const PortageRepository * const portage_repository;
        const PortageRepositoryParams params;

        Implementation(const Environment * const e, const PortageRepository * const p,
                const PortageRepositoryParams & k) :
            environment(e),
            portage_repository(p),
            params(k)
        {
        }
    };
}

PortageRepositorySets::PortageRepositorySets(const Environment * const e, const PortageRepository * const p,
        const PortageRepositoryParams & k) :
    PrivateImplementationPattern<PortageRepositorySets>(new Implementation<PortageRepositorySets>(e, p, k))
{
}

PortageRepositorySets::~PortageRepositorySets()
{
}


std::tr1::shared_ptr<DepAtom>
PortageRepositorySets::package_set(const SetName & s) const
{
    if ("system" == s.data())
        throw InternalError(PALUDIS_HERE, "system set should've been handled by PortageRepository");
    else if ("security" == s.data())
        return security_set(false);
    else if ("insecurity" == s.data())
        return security_set(true);
    else if ((_imp->params.setsdir / (stringify(s) + ".conf")).exists())
    {
        std::tr1::shared_ptr<GeneralSetDepTag> tag(new GeneralSetDepTag(s, stringify(_imp->portage_repository->name())));

        FSEntry ff(_imp->params.setsdir / (stringify(s) + ".conf"));
        Context context("When loading package set '" + stringify(s) + "' from '" + stringify(ff) + "':");

        std::tr1::shared_ptr<AllDepAtom> result(new AllDepAtom);
        LineConfigFile f(ff);
        for (LineConfigFile::Iterator line(f.begin()), line_end(f.end()) ;
                line != line_end ; ++line)
        {
            std::vector<std::string> tokens;
            WhitespaceTokeniser::get_instance()->tokenise(*line, std::back_inserter(tokens));
            if (tokens.empty())
                continue;

            if (1 == tokens.size())
            {
                Log::get_instance()->message(ll_warning, lc_context,
                        "Line '" + *line + "' in set file '"
                        + stringify(ff) + "' does not specify '*' or '?', assuming '*'");
                std::tr1::shared_ptr<PackageDepAtom> atom(new PackageDepAtom(tokens.at(0)));
                atom->set_tag(tag);
                result->add_child(atom);
            }
            else if ("*" == tokens.at(0))
            {
                std::tr1::shared_ptr<PackageDepAtom> atom(new PackageDepAtom(tokens.at(1)));
                atom->set_tag(tag);
                result->add_child(atom);
            }
            else if ("?" == tokens.at(0))
            {
                std::tr1::shared_ptr<PackageDepAtom> p(new PackageDepAtom(tokens.at(1)));
                p->set_tag(tag);
                if (! _imp->environment->package_database()->query(
                            query::Package(p->package()) & query::InstalledAtRoot(
                                _imp->params.environment->root()), qo_whatever)->empty())
                    result->add_child(p);
            }
            else
                Log::get_instance()->message(ll_warning, lc_context,
                        "Line '" + *line + "' in set file '"
                        + stringify(ff) + "' does not start with '*' or '?' token, skipping");

            if (tokens.size() > 2)
                Log::get_instance()->message(ll_warning, lc_context,
                        "Line '" + *line + "' in set file '"
                        + stringify(ff) + "' has trailing garbage");
        }

        return result;
    }
    else
        return std::tr1::shared_ptr<DepAtom>();
}

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

    std::tr1::shared_ptr<SetsCollection> result(new SetsCollection::Concrete);
    result->insert(SetName("insecurity"));
    result->insert(SetName("security"));
    result->insert(SetName("system"));

    try
    {
        std::list<FSEntry> repo_sets;
        std::copy(DirIterator(_imp->params.setsdir), DirIterator(),
            filter_inserter(std::back_inserter(repo_sets),
            IsFileWithExtension(".conf")));

        std::list<FSEntry>::const_iterator f(repo_sets.begin()),
            f_end(repo_sets.end());

        for ( ; f != f_end ; ++f)
            try
            {
                result->insert(SetName(stringify(*f)));
            }
            catch (const NameError & e)
            {
                Log::get_instance()->message(ll_warning, lc_context, "Skipping set '"
                        + stringify(*f) + "' due to exception '" + stringify(e.message()) + "' ("
                        + stringify(e.what()) + ")");
            }
    }
    catch (const paludis::DirOpenError &)
    {
    }

    return result;
}

PackageDatabaseEntryCollection::Iterator
PortageRepositorySets::find_best(PackageDatabaseEntryCollection & c, const PackageDatabaseEntry & e) const
{
    Context local("When finding best update for '" + stringify(e.name) + "-" +
            stringify(e.version) + "':");
    // Find an entry in c that matches e best. e is not in c.
    QualifiedPackageName n(e.name);
    SlotName s(_imp->environment->package_database()->fetch_repository(
                e.repository)->version_metadata(e.name, e.version)->slot);
    PackageDatabaseEntryCollection::Iterator i(c.begin()), i_end(c.end()), i_best(c.end());
    for ( ; i != i_end; ++i)
    {
        if (n != i->name)
            continue;
        if (s != _imp->environment->package_database()->fetch_repository(
                    i->repository)->version_metadata(
                    i->name, i->version)->slot)
            continue;

        i_best = i;
    }

    return i_best;
}

namespace
{
    bool
    match_range(const PackageDatabaseEntry & e, const GLSARange & r)
    {
        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 = vo_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 (e.version.*(VersionOperator(our_op).as_version_spec_operator()))(VersionSpec(ver));

        if (0 == r.op.compare(0, 1, "r"))
        {
            return (e.version.*(VersionOperator(vo_tilde).as_version_spec_operator()))(VersionSpec(ver)) &&
                match_range(e, GLSARange::create().op(r.op.substr(1)).version(r.version));
        }

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

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

        if (! vulnerable)
            return false;

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

        return vulnerable;
    }
}

std::tr1::shared_ptr<DepAtom>
PortageRepositorySets::security_set(bool insecurity) const
{
    Context context("When building security or insecurity package set:");
    std::tr1::shared_ptr<AllDepAtom> security_packages(new AllDepAtom);

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

    std::map<std::string, std::tr1::shared_ptr<GLSADepTag> > glsa_tags;

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

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

        try
        {
            std::tr1::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::PackagesIterator glsa_pkg(glsa->begin_packages()),
                    glsa_pkg_end(glsa->end_packages()) ; glsa_pkg != glsa_pkg_end ; ++glsa_pkg)
            {
                std::tr1::shared_ptr<const PackageDatabaseEntryCollection> candidates;
                if (insecurity)
                    candidates = _imp->environment->package_database()->query(query::Package(glsa_pkg->name()), qo_order_by_version);
                else
                    candidates = _imp->environment->package_database()->query(
                            query::Package(glsa_pkg->name()) & query::RepositoryHasInstalledInterface(), qo_order_by_version);

                for (PackageDatabaseEntryCollection::Iterator c(candidates->begin()), c_end(candidates->end()) ;
                        c != c_end ; ++c)
                {
                    if (! is_vulnerable(*glsa_pkg, *c))
                        continue;

                    if (glsa_tags.end() == glsa_tags.find(glsa->id()))
                        glsa_tags.insert(std::make_pair(glsa->id(), std::tr1::shared_ptr<GLSADepTag>(
                                        new GLSADepTag(glsa->id(), glsa->title()))));

                    if (insecurity)
                    {
                        std::tr1::shared_ptr<PackageDepAtom> atom(new PackageDepAtom(
                                    "=" + stringify(c->name) + "-" + stringify(c->version) +
                                    "::" + stringify(c->repository)));
                        atom->set_tag(glsa_tags.find(glsa->id())->second);
                        security_packages->add_child(atom);
                    }
                    else
                    {
                        /* 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);
                        SlotName wanted_slot(_imp->environment->package_database()->fetch_repository(
                                    c->repository)->version_metadata(c->name, c->version)->slot);

                        std::tr1::shared_ptr<const PackageDatabaseEntryCollection> available(
                                _imp->environment->package_database()->query(PackageDepAtom(glsa_pkg->name()), is_installable_only,
                                    qo_order_by_version));
                        for (PackageDatabaseEntryCollection::ReverseIterator r(available->rbegin()),
                                r_end(available->rend()) ; r != r_end ; ++r)
                        {
                            if (_imp->environment->mask_reasons(*r).any())
                                continue;
                            if (_imp->environment->package_database()->fetch_repository(r->repository)->version_metadata(
                                        r->name, r->version)->slot != wanted_slot)
                                continue;
                            if (is_vulnerable(*glsa_pkg, *r))
                                continue;

                            std::tr1::shared_ptr<PackageDepAtom> atom(new PackageDepAtom(
                                        "=" + stringify(r->name) + "-" + stringify(r->version) +
                                        "::" + stringify(r->repository)));
                            atom->set_tag(glsa_tags.find(glsa->id())->second);
                            security_packages->add_child(atom);
                            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(ll_warning, lc_context, "Cannot use GLSA '" +
                    stringify(*f) + "' due to exception '" + e.message() + "' (" + e.what() + ")");
        }
        catch (const NameError & e)
        {
            Log::get_instance()->message(ll_warning, lc_context, "Cannot use GLSA '" +
                    stringify(*f) + "' due to exception '" + e.message() + "' (" + e.what() + ")");
        }
    }

    return security_packages;
}