aboutsummaryrefslogtreecommitdiff
path: root/src/clients/adjutrix/find_insecure_packages.cc
blob: 44da9ba56d1dc0d49aa5567e2aa1f40de1f6d1f0 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
 *
 * 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 "find_insecure_packages.hh"
#include "command_line.hh"

#include <paludis/util/tokeniser.hh>
#include <paludis/dep_spec.hh>
#include <paludis/dep_tag.hh>
#include <paludis/spec_tree.hh>
#include <paludis/package_id.hh>
#include <paludis/package_database.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/selection.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/accept_visitor.hh>
#include <paludis/util/make_null_shared_ptr.hh>

#include <set>
#include <map>
#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace paludis;
using std::cout;
using std::cerr;
using std::endl;

namespace
{
    const unsigned col_width_package = 30;
    const unsigned col_width_id      = 40;

    void
    write_repository_header(const RepositoryName & repo)
    {
        std::string s("Insecure packages from advisories in repository '" + stringify(repo) + "'");
        cout << std::string(s.length(), '=') << endl;
        cout << s << endl;
        cout << std::string(s.length(), '=') << endl;
        cout << endl;

        cout << std::left
            << std::setw(col_width_package) << "package"
            << std::setw(col_width_id) << "GLSA IDs"
            << endl;

        cout
            << std::string(col_width_package - 1, '-') << " "
            << std::string(col_width_id - 1, '-') << " "
            << endl;
    }

    class ListInsecureVisitor
    {
        private:
            const Environment & _env;
            std::multimap<std::shared_ptr<const PackageID>, std::string, PackageIDSetComparator> _found;
            std::set<SetName> recursing_sets;

        public:
            ListInsecureVisitor(const Environment & e) :
                _env(e)
            {
            }

            void visit(const SetSpecTree::NodeType<PackageDepSpec>::Type & node)
            {
                std::shared_ptr<const PackageIDSequence> insecure(_env[selection::AllVersionsSorted(
                            generator::Matches(*node.spec(), make_null_shared_ptr(), { }))]);
                for (PackageIDSequence::ConstIterator i(insecure->begin()),
                        i_end(insecure->end()) ; i != i_end ; ++i)
                    if (node.spec()->tag())
                        _found.insert(std::make_pair(*i, node.spec()->tag()->short_text()));
                    else
                        throw InternalError(PALUDIS_HERE, "didn't get a tag");
            }

            void visit(const SetSpecTree::NodeType<NamedSetDepSpec>::Type & node)
            {
                Context context("When expanding named set '" + stringify(*node.spec()) + "':");

                std::shared_ptr<const SetSpecTree> set(_env.set(node.spec()->name()));

                if (! set)
                {
                    Log::get_instance()->message("adjutrix.find_insecure_packages.unknown_set", ll_warning, lc_context)
                        << "Unknown set '" << node.spec()->name() << "'";
                    return;
                }

                if (! recursing_sets.insert(node.spec()->name()).second)
                {
                    Log::get_instance()->message("adjutrix.find_insecure_packages.recursive_set", ll_warning, lc_context)
                        << "Recursively defined set '" << node.spec()->name() << "'";
                    return;
                }

                set->top()->accept(*this);

                recursing_sets.erase(node.spec()->name());
            }

            void visit(const SetSpecTree::NodeType<AllDepSpec>::Type & node)
            {
                std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
            }

            friend std::ostream & operator<< (std::ostream &, const ListInsecureVisitor &);
    };

    std::ostream & operator<< (std::ostream & s, const ListInsecureVisitor & v)
    {
        QualifiedPackageName old_name("dormouse/teapot");
        for (std::multimap<std::shared_ptr<const PackageID>, std::string, PackageIDSetComparator>::const_iterator
                f(v._found.begin()), f_end(v._found.end()) ; f != f_end ; ++f)
        {
            if (f->first->name() != old_name)
                s << std::setw(col_width_package) << (stringify(f->first->name()) + " ") << endl;
            old_name = f->first->name();
            s << std::setw(col_width_package) << ("  " + stringify(f->first->canonical_form(idcf_version)) + " ")
                << f->second;
            while (next(f) != f_end)
            {
                if (*next(f)->first != *f->first)
                    break;
                cout << " " << f->second;
                ++f;
            }
            cout << endl;
        }

        return s;
    }
}

void do_find_insecure_packages(const NoConfigEnvironment & env)
{
    Context context("When performing find-insecure-packages action:");

    for (IndirectIterator<PackageDatabase::RepositoryConstIterator, const Repository>
            r(env.package_database()->begin_repositories()),
            r_end(env.package_database()->end_repositories()) ; r != r_end ; ++r)
    {
        if (r->name() == RepositoryName("virtuals"))
            continue;
        if (env.master_repository() && r->name() == env.master_repository()->name())
            continue;

        std::shared_ptr<const SetSpecTree> all_insecure(env.set(SetName("insecurity::"
                        + stringify(r->name()))));
        if (! all_insecure)
            continue;

        write_repository_header(r->name());

        ListInsecureVisitor v(env);
        all_insecure->top()->accept(v);
        cout << v << endl;
    }
}