Debian Codegraph @

src/OVAL/oval_affected.c

raw symbols SCIP
1 /**
2 * @file oval_affected.c
3 * \brief Open Vulnerability and Assessment Language
4 *
5 * See more details at http://oval.mitre.org/
6 */
7
8 /*
9 * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina.
10 * All Rights Reserved.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 * Authors:
27 * "David Niemoller" <[email protected]>
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "oval_definitions_impl.h"
39 #include "adt/oval_collection_impl.h"
40 #include "oval_parser_impl.h"
41 #include "adt/oval_string_map_impl.h"
42
43 #include "common/util.h"
44 #include "common/debug_priv.h"
45 #include "common/elements.h"
46 #include "common/_error.h"
47
48 /***************************************************************************/
49 /* Variable definitions
50 * */
51
52 typedef struct oval_affected {
53 struct oval_definition_model *model;
54 oval_affected_family_t family;
55 struct oval_collection *platforms;
56 struct oval_collection *products;
57 } oval_affected_t;
58
59 static const struct oscap_string_map OVAL_ODAFAMILY_MAP[] = {
60 {OVAL_AFCFML_CATOS, "catos"},
61 {OVAL_AFCFML_IOS, "ios"},
62 {OVAL_AFCFML_MACOS, "macos"},
63 {OVAL_AFCFML_PIXOS, "pixos"},
64 {OVAL_AFCFML_UNDEFINED, "undefined"},
65 {OVAL_AFCFML_UNIX, "unix"},
66 {OVAL_AFCFML_WINDOWS, "windows"},
67 // the "unknown" is not a valid OVAL family value, we use it internally
68 // when the supplied value is invalid
69 {OVAL_AFCFML_UNKNOWN, NULL}
70 };
71
72 /* End of variable definitions
73 * */
74 /***************************************************************************/
75
76 bool oval_affected_iterator_has_more(struct oval_affected_iterator *oc_affected)
77 {
78 return oval_collection_iterator_has_more((struct oval_iterator *)
79 oc_affected);
80 }
81
82 struct oval_affected *oval_affected_iterator_next(struct oval_affected_iterator
83 *oc_affected)
84 {
85 return (struct oval_affected *)
86 oval_collection_iterator_next((struct oval_iterator *)oc_affected);
87 }
88
89 void oval_affected_iterator_free(struct oval_affected_iterator
90 *oc_affected)
91 {
92 oval_collection_iterator_free((struct oval_iterator *)oc_affected);
93 }
94
95 oval_affected_family_t oval_affected_get_family(struct oval_affected *affected)
96 {
97 __attribute__nonnull__(affected);
98
99 return ((struct oval_affected *)affected)->family;
100 }
101
102 struct oval_string_iterator *oval_affected_get_platforms(struct oval_affected *affected)
103 {
104 __attribute__nonnull__(affected);
105
106 return (struct oval_string_iterator *)
107 oval_collection_iterator(affected->platforms);
108 }
109
110 struct oval_string_iterator *oval_affected_get_products(struct oval_affected *affected)
111 {
112 __attribute__nonnull__(affected);
113
114 return (struct oval_string_iterator *)
115 oval_collection_iterator(affected->products);
116 }
117
118 struct oval_affected *oval_affected_new(struct oval_definition_model *model)
119 {
120 struct oval_affected *affected = (struct oval_affected *)malloc(sizeof(oval_affected_t));
121 if (affected == NULL)
122 return NULL;
123
124 affected->model = model;
125 affected->family = OVAL_AFCFML_UNKNOWN;
126 affected->platforms = oval_collection_new();
127 affected->products = oval_collection_new();
128 return affected;
129 }
130
131 struct oval_affected *oval_affected_clone(struct oval_definition_model *new_model, struct oval_affected *old_affected)
132 {
133 __attribute__nonnull__(old_affected);
134
135 struct oval_affected *new_affected = oval_affected_new(new_model);
136
137 oval_affected_set_family(new_affected, old_affected->family);
138
139 struct oval_string_iterator *platforms = oval_affected_get_platforms(old_affected);
140 while (oval_string_iterator_has_more(platforms)) {
141 char *platform = oval_string_iterator_next(platforms);
142 oval_affected_add_platform(new_affected, platform);
143 }
144 oval_string_iterator_free(platforms);
145
146 struct oval_string_iterator *products = oval_affected_get_products(old_affected);
147 while (oval_string_iterator_has_more(products)) {
148 char *product = oval_string_iterator_next(products);
149 oval_affected_add_product(new_affected, product);
150 }
151 oval_string_iterator_free(products);
152
153 return new_affected;
154 }
155
156 void oval_affected_free(struct oval_affected *affected)
157 {
158 __attribute__nonnull__(affected);
159
160 oval_collection_free_items(affected->platforms, (oscap_destruct_func) free);
161 affected->platforms = NULL;
162 oval_collection_free_items(affected->products, (oscap_destruct_func) free);
163 affected->products = NULL;
164 free(affected);
165 }
166
167 void oval_affected_set_family(struct oval_affected *affected, oval_affected_family_t family)
168 {
169 __attribute__nonnull__(affected);
170
171 affected->family = family;
172 }
173
174 void oval_affected_add_platform(struct oval_affected *affected, char *platform)
175 {
176 __attribute__nonnull__(affected);
177
178 oval_collection_add(affected->platforms, (void *)oscap_strdup(platform));
179 }
180
181 void oval_affected_add_product(struct oval_affected *affected, char *product)
182 {
183 __attribute__nonnull__(affected);
184
185 oval_collection_add(affected->products, (void *)oscap_strdup(product));
186 }
187
188 static oval_affected_family_t _odafamily(char *family)
189 {
190 return oscap_string_to_enum(OVAL_ODAFAMILY_MAP, family);
191 }
192
193 const char *oval_affected_family_get_text(oval_affected_family_t family)
194 {
195 return oscap_enum_to_string(OVAL_ODAFAMILY_MAP, family);
196 }
197
198 static int _oval_affected_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user)
199 {
200 struct oval_affected *affected = (struct oval_affected *)user;
201 int return_code;
202 xmlChar *tagname = xmlTextReaderLocalName(reader);
203 //xmlChar *namespace = xmlTextReaderNamespaceUri(reader);
204
205 if (strcmp((char *)tagname, "platform") == 0) {
206 char *platform = NULL;
207 return_code = oscap_parser_text_value(reader, &oscap_text_consumer, &platform);
208 if (platform != NULL) {
209 oval_affected_add_platform(affected, platform);
210 free(platform);
211 }
212 } else if (strcmp((char *)tagname, "product") == 0) {
213 char *product = NULL;
214 return_code = oscap_parser_text_value(reader, &oscap_text_consumer, &product);
215 if (product != NULL) {
216 oval_affected_add_product(affected, product);
217 free(product);
218 }
219 } else {
220 dD("Skipping tag: %s", tagname);
221 return_code = oval_parser_skip_tag(reader, context);
222 }
223 free(tagname);
224 return return_code;
225 }
226
227 int oval_affected_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, oval_affected_consumer consumer, void *user)
228 {
229 __attribute__nonnull__(context);
230
231 struct oval_affected *affected = oval_affected_new(context->definition_model);
232 char *family = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "family");
233 oval_affected_set_family(affected, _odafamily(family));
234 free(family);
235 (*consumer) (affected, user);
236 return oval_parser_parse_tag(reader, context, &_oval_affected_parse_tag, affected);
237 }
238