aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/portage/xml_things.cc
blob: 10dc69418091f3c7146b1a228951edad4c147680 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.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 <libxml/tree.h>
#include <libxml/parser.h>
#include <paludis/repositories/portage/glsa.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/config_file.hh>
#include <paludis/libxml/libxml.hh>
#include <set>

using namespace paludis;

extern "C"
{
    GLSA::Pointer create_glsa_from_xml_file(const std::string &);
}

namespace
{
    class Handler
    {
        private:
            GLSA::Pointer _glsa;

        public:
            Handler() :
                _glsa(new GLSA)
            {
            }

            void handle_glsa_attrs(xmlDocPtr doc, xmlAttr * const attr)
            {
                for (xmlAttr * a(attr) ; a ; a = a->next)
                {
                    if (a->type == XML_ATTRIBUTE_NODE)
                    {
                        std::string name(retarded_libxml_string_to_string(a->name));
                        if (name == "id")
                            _glsa->set_id(normalise(retarded_libxml_string_to_string(xmlNodeListGetString(doc,
                                                a->xmlChildrenNode, 1))));
                    }
                }
            }

            void handle_package_name(xmlDocPtr doc, xmlAttr * const attr, std::string & str)
            {
                for (xmlAttr * a(attr) ; a ; a = a->next)
                {
                    if (a->type == XML_ATTRIBUTE_NODE)
                    {
                        std::string name(retarded_libxml_string_to_string(a->name));
                        if (name == "name")
                            str = normalise(retarded_libxml_string_to_string(xmlNodeListGetString(doc,
                                            a->xmlChildrenNode, 1)));
                    }
                }
            }

            void handle_package_archs(xmlDocPtr doc, xmlAttr * const attr, GLSAPackage::Pointer pkg)
            {
                for (xmlAttr * a(attr) ; a ; a = a->next)
                {
                    if (a->type == XML_ATTRIBUTE_NODE)
                    {
                        std::string name(retarded_libxml_string_to_string(a->name));
                        if (name == "arch")
                        {
                            std::set<std::string> archs;
                            WhitespaceTokeniser::get_instance()->tokenise(retarded_libxml_string_to_string(
                                        xmlNodeListGetString(doc, a->xmlChildrenNode, 1)),
                                    std::inserter(archs, archs.end()));
                            archs.erase("*");
                            for (std::set<std::string>::const_iterator r(archs.begin()), r_end(archs.end()) ;
                                    r != r_end ; ++r)
                                pkg->add_arch(UseFlagName(*r));
                        }
                    }
                }
            }

            void handle_range_range(xmlDocPtr doc, xmlAttr * const attr, std::string & op)
            {
                for (xmlAttr * a(attr) ; a ; a = a->next)
                {
                    if (a->type == XML_ATTRIBUTE_NODE)
                    {
                        std::string name(retarded_libxml_string_to_string(a->name));
                        if (name == "range")
                            op = normalise(retarded_libxml_string_to_string(xmlNodeListGetString(doc,
                                            a->xmlChildrenNode, 1)));
                    }
                }
            }

            void handle_package_children(xmlDocPtr doc, xmlNode * const node, GLSAPackage::Pointer pkg)
            {
                for (xmlNode * n(node) ; n ; n = n->next)
                {
                    if (n->type == XML_ELEMENT_NODE)
                    {
                        std::string name(retarded_libxml_string_to_string(n->name));
                        if (name == "unaffected" || name == "vulnerable")
                        {
                            std::string op;
                            handle_range_range(doc, n->properties, op);
                            std::string version(normalise(retarded_libxml_string_to_string(
                                            xmlNodeListGetString(doc, n->xmlChildrenNode, 1))));
                            ((*pkg).*(name == "unaffected" ? &GLSAPackage::add_unaffected : &GLSAPackage::add_vulnerable))
                                (GLSARange::create().op(op).version(version));
                        }
                        else
                            handle_node(doc, n->children);
                    }
                    else
                        handle_node(doc, n->children);
                }

            }

            void handle_node(xmlDocPtr doc, xmlNode * const node)
            {
                for (xmlNode * n(node) ; n ; n = n->next)
                {
                    if (n->type == XML_ELEMENT_NODE)
                    {
                        std::string name(retarded_libxml_string_to_string(n->name));
                        if (name == "glsa")
                        {
                            handle_glsa_attrs(doc, n->properties);
                            handle_node(doc, n->children);
                        }
                        else if (name == "title")
                            _glsa->set_title(normalise(retarded_libxml_string_to_string(xmlNodeListGetString(doc,
                                                n->xmlChildrenNode, 1))));
                        else if (name == "package")
                        {
                            std::string name;
                            handle_package_name(doc, n->properties, name);
                            GLSAPackage::Pointer pkg(new GLSAPackage(QualifiedPackageName(name)));
                            handle_package_archs(doc, n->properties, pkg);
                            handle_package_children(doc, n->children, pkg);
                            _glsa->add_package(pkg);
                        }
                        else
                            handle_node(doc, n->children);
                    }
                    else
                        handle_node(doc, n->children);
                }

            }

            GLSA::Pointer glsa()
            {
                return _glsa;
            }
    };
}

GLSA::Pointer
create_glsa_from_xml_file(const std::string & filename)
{
    LibXmlPtrHolder<xmlDocPtr> xml_doc(xmlReadFile(filename.c_str(), 0, 0), &xmlFreeDoc);
    if (! xml_doc)
        throw GLSAError("Could not parse GLSA", filename);

    Handler h;
    h.handle_node(xml_doc, xmlDocGetRootElement(xml_doc));
    return h.glsa();
}

#ifndef MONOLITHIC

namespace paludis
{
    class RepositoryMaker;
}

extern "C"
{
    void register_repositories(RepositoryMaker * maker);
}

void register_repositories(RepositoryMaker * maker)
{
}

#endif