| 1 | /* |
| 2 | * Copyright 2013--2014 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Martin Preisler <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "public/scap_ds.h" |
| 28 | #include "common/list.h" |
| 29 | #include "common/_error.h" |
| 30 | #include "common/util.h" |
| 31 | #include "common/elements.h" |
| 32 | #include "common/debug_priv.h" |
| 33 | #include "rds_index_priv.h" |
| 34 | |
| 35 | |
| 36 | #include <libxml/xmlreader.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | struct rds_index |
| 40 | { |
| 41 | struct oscap_list *report_requests; |
| 42 | struct oscap_list *assets; |
| 43 | struct oscap_list *reports; |
| 44 | }; |
| 45 | |
| 46 | struct rds_index *rds_index_new(void) |
| 47 | { |
| 48 | struct rds_index *ret = calloc(1, sizeof(struct rds_index)); |
| 49 | ret->report_requests = oscap_list_new(); |
| 50 | ret->assets = oscap_list_new(); |
| 51 | ret->reports = oscap_list_new(); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | void rds_index_free(struct rds_index *s) |
| 57 | { |
| 58 | if (s != NULL) { |
| 59 | oscap_list_free(s->report_requests, (oscap_destruct_func)rds_report_request_index_free); |
| 60 | oscap_list_free(s->assets, (oscap_destruct_func)rds_asset_index_free); |
| 61 | oscap_list_free(s->reports, (oscap_destruct_func)rds_report_index_free); |
| 62 | free(s); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void rds_index_add_report_request(struct rds_index *s, struct rds_report_request_index *rr_index) |
| 67 | { |
| 68 | oscap_list_add(s->report_requests, rr_index); |
| 69 | } |
| 70 | |
| 71 | struct rds_report_request_index_iterator *rds_index_get_report_requests(struct rds_index *s) |
| 72 | { |
| 73 | return (struct rds_report_request_index_iterator*)oscap_iterator_new(s->report_requests); |
| 74 | } |
| 75 | |
| 76 | static void rds_index_add_asset(struct rds_index *s, struct rds_asset_index *a_index) |
| 77 | { |
| 78 | oscap_list_add(s->assets, a_index); |
| 79 | } |
| 80 | |
| 81 | struct rds_asset_index_iterator *rds_index_get_assets(struct rds_index *s) |
| 82 | { |
| 83 | return (struct rds_asset_index_iterator*)oscap_iterator_new(s->assets); |
| 84 | } |
| 85 | |
| 86 | static void rds_index_add_report(struct rds_index* s, struct rds_report_index *r_index) |
| 87 | { |
| 88 | oscap_list_add(s->reports, r_index); |
| 89 | } |
| 90 | |
| 91 | struct rds_report_index_iterator *rds_index_get_reports(struct rds_index* s) |
| 92 | { |
| 93 | return (struct rds_report_index_iterator*)oscap_iterator_new(s->reports); |
| 94 | } |
| 95 | |
| 96 | struct rds_report_request_index* rds_index_get_report_request(struct rds_index* rds, const char* id) |
| 97 | { |
| 98 | struct rds_report_request_index* ret = NULL; |
| 99 | |
| 100 | struct rds_report_request_index_iterator* it = rds_index_get_report_requests(rds); |
| 101 | while (rds_report_request_index_iterator_has_more(it)) |
| 102 | { |
| 103 | struct rds_report_request_index* rr_index = rds_report_request_index_iterator_next(it); |
| 104 | if (strcmp(rds_report_request_index_get_id(rr_index), id) == 0) { |
| 105 | ret = rr_index; |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | rds_report_request_index_iterator_free(it); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | struct rds_asset_index* rds_index_get_asset(struct rds_index *rds, const char *id) |
| 116 | { |
| 117 | struct rds_asset_index *ret = NULL; |
| 118 | |
| 119 | if (id == NULL) |
| 120 | return ret; |
| 121 | |
| 122 | struct rds_asset_index_iterator *it = rds_index_get_assets(rds); |
| 123 | while (rds_asset_index_iterator_has_more(it)) |
| 124 | { |
| 125 | struct rds_asset_index *a_index = rds_asset_index_iterator_next(it); |
| 126 | if (strcmp(rds_asset_index_get_id(a_index), id) == 0) { |
| 127 | ret = a_index; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | rds_asset_index_iterator_free(it); |
| 133 | |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | struct rds_report_index *rds_index_get_report(struct rds_index *rds, const char *id) |
| 138 | { |
| 139 | struct rds_report_index *ret = NULL; |
| 140 | |
| 141 | struct rds_report_index_iterator *it = rds_index_get_reports(rds); |
| 142 | while (rds_report_index_iterator_has_more(it)) |
| 143 | { |
| 144 | struct rds_report_index *r_index = rds_report_index_iterator_next(it); |
| 145 | if (strcmp(rds_report_index_get_id(r_index), id) == 0) { |
| 146 | ret = r_index; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | rds_report_index_iterator_free(it); |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static xmlChar *relationship_get_inner_ref(xmlNodePtr node) |
| 157 | { |
| 158 | xmlChar *ret = NULL; |
| 159 | xmlNodePtr ref_node = node->children; |
| 160 | |
| 161 | for (; ref_node != NULL; ref_node = ref_node->next) |
| 162 | { |
| 163 | if (ref_node->type != XML_ELEMENT_NODE) |
| 164 | continue; |
| 165 | |
| 166 | if (strcmp((const char*)(ref_node->name), "ref") != 0) |
| 167 | continue; // TODO: Warning? |
| 168 | |
| 169 | if (ret) { |
| 170 | oscap_seterr(OSCAP_EFAMILY_XML, |
| 171 | "Multiple <core:ref> elements found in a <core:relationship> element.\n" |
| 172 | "Only the first ref will be used!\n" |
| 173 | "Make sure the Result DataStream is valid!"); |
| 174 | } |
| 175 | else |
| 176 | ret = xmlNodeGetContent(ref_node); |
| 177 | } |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static inline void _parse_relationships_node(struct rds_index *ret, xmlNodePtr relationships_node) |
| 183 | { |
| 184 | if (relationships_node == NULL) { |
| 185 | oscap_seterr(OSCAP_EFAMILY_XML, "There is no <core:relationships> element in the Result DataStream.\n"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | xmlNodePtr relationship_node = relationships_node->children; |
| 190 | for (; relationship_node != NULL; relationship_node = relationship_node->next) |
| 191 | { |
| 192 | if (relationship_node->type != XML_ELEMENT_NODE) |
| 193 | continue; |
| 194 | |
| 195 | if (strcmp((const char*)(relationship_node->name), "relationship") != 0) |
| 196 | continue; // TODO: Warning? |
| 197 | |
| 198 | xmlChar *type_attr = xmlGetProp(relationship_node, BAD_CAST "type"); |
| 199 | xmlChar *subject_attr = xmlGetProp(relationship_node, BAD_CAST "subject"); |
| 200 | xmlChar *inner_ref = relationship_get_inner_ref(relationship_node); |
| 201 | |
| 202 | // We now only use arfvocab: but arfrel: is kept here for compatibility |
| 203 | if (oscap_str_startswith((const char *) type_attr, "arfvocab:") |
| 204 | || oscap_str_startswith((const char *) type_attr, "arfrel:")) { |
| 205 | if (oscap_str_endswith((const char*)type_attr, ":isAbout")) { |
| 206 | struct rds_asset_index* asset = rds_index_get_asset(ret, (const char*)inner_ref); |
| 207 | struct rds_report_index* report = rds_index_get_report(ret, (const char*)subject_attr); |
| 208 | |
| 209 | rds_asset_index_add_report_ref(asset, report); |
| 210 | } else if (oscap_str_endswith((const char*)type_attr, ":createdFor")) { |
| 211 | struct rds_report_request_index *request = rds_index_get_report_request(ret, (const char*)inner_ref); |
| 212 | struct rds_report_index *report = rds_index_get_report(ret, (const char*)subject_attr); |
| 213 | |
| 214 | // This is based on the assumption that every report has at most 1 request |
| 215 | // it was "created for". |
| 216 | rds_report_index_set_request(report, request); |
| 217 | } else { |
| 218 | dW("Unsupported core:relationship/@type='%s'", (const char *) type_attr); |
| 219 | } |
| 220 | } else { |
| 221 | dW("Unsupported core:relationship/@type='%s'", (const char *) type_attr); |
| 222 | } |
| 223 | |
| 224 | xmlFree(type_attr); |
| 225 | xmlFree(subject_attr); |
| 226 | xmlFree(inner_ref); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | struct rds_index *rds_index_parse(xmlTextReaderPtr reader) |
| 231 | { |
| 232 | if (!oscap_to_start_element(reader, 0)) { |
| 233 | oscap_seterr(OSCAP_EFAMILY_XML, |
| 234 | "Expected to to have start of <arf:asset-report-collection> at document root, " |
| 235 | "no elements were found! I refuse to parse!"); |
| 236 | |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT || |
| 241 | strcmp((const char*)xmlTextReaderConstLocalName(reader), "asset-report-collection") != 0) { |
| 242 | oscap_seterr(OSCAP_EFAMILY_XML, |
| 243 | "Expected to to have start of <arf:asset-report-collection> at document root, " |
| 244 | "the current event is '%i' at '%s' instead. I refuse to parse!", |
| 245 | xmlTextReaderNodeType(reader), (const char*)xmlTextReaderConstLocalName(reader)); |
| 246 | |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | if (xmlTextReaderRead(reader) != 1) { |
| 251 | oscap_setxmlerr(xmlGetLastError()); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | struct rds_index* ret = rds_index_new(); |
| 256 | xmlNodePtr relationships_node = NULL; |
| 257 | |
| 258 | while (oscap_to_start_element(reader, 1)) |
| 259 | { |
| 260 | const char *name = (const char*)xmlTextReaderConstLocalName(reader); |
| 261 | |
| 262 | if (strcmp(name, "report-requests") == 0) { |
| 263 | if (xmlTextReaderRead(reader) != 1) { |
| 264 | oscap_setxmlerr(xmlGetLastError()); |
| 265 | } |
| 266 | |
| 267 | while (oscap_to_start_element(reader, 2)) |
| 268 | { |
| 269 | if (strcmp((const char*)xmlTextReaderConstLocalName(reader), "report-request") != 0) { |
| 270 | // TODO: warning? |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | struct rds_report_request_index* rr_index = rds_report_request_index_parse(reader); |
| 275 | rds_index_add_report_request(ret, rr_index); |
| 276 | |
| 277 | if (xmlTextReaderRead(reader) != 1) { |
| 278 | oscap_setxmlerr(xmlGetLastError()); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | else if (strcmp(name, "assets") == 0) |
| 283 | { |
| 284 | if (xmlTextReaderRead(reader) != 1) { |
| 285 | oscap_setxmlerr(xmlGetLastError()); |
| 286 | } |
| 287 | |
| 288 | while (oscap_to_start_element(reader, 2)) |
| 289 | { |
| 290 | if (strcmp((const char*)xmlTextReaderConstLocalName(reader), "asset") != 0) { |
| 291 | // TODO: warning? |
| 292 | continue; |
| 293 | } |
| 294 | |
| 295 | struct rds_asset_index* a_index = rds_asset_index_parse(reader); |
| 296 | rds_index_add_asset(ret, a_index); |
| 297 | |
| 298 | if (xmlTextReaderRead(reader) != 1) { |
| 299 | oscap_setxmlerr(xmlGetLastError()); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | else if (strcmp(name, "reports") == 0) |
| 304 | { |
| 305 | if (xmlTextReaderRead(reader) != 1) { |
| 306 | oscap_setxmlerr(xmlGetLastError()); |
| 307 | } |
| 308 | |
| 309 | while (oscap_to_start_element(reader, 2)) |
| 310 | { |
| 311 | if (strcmp((const char*)xmlTextReaderConstLocalName(reader), "report") != 0) { |
| 312 | // TODO: warning? |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | struct rds_report_index* r_index = rds_report_index_parse(reader); |
| 317 | rds_index_add_report(ret, r_index); |
| 318 | |
| 319 | if (xmlTextReaderRead(reader) != 1) { |
| 320 | oscap_setxmlerr(xmlGetLastError()); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | else if (strcmp(name, "relationships") == 0) { |
| 325 | // We have to copy the node, xmlTextReader destroys it when |
| 326 | // xmlTextReaderRead is next called. |
| 327 | // |
| 328 | // extended = 1 means that we want to copy all including children |
| 329 | xmlNodePtr new_relationships_node = xmlCopyNode(xmlTextReaderExpand(reader), 1); |
| 330 | |
| 331 | if (relationships_node) |
| 332 | { |
| 333 | oscap_seterr(OSCAP_EFAMILY_XML, "There is more than 1 <core:relationships> element in the Result DataStream.\n" |
| 334 | "Please make sure the input file is valid! Only the first element will be used to build the index!"); |
| 335 | xmlFreeNode(new_relationships_node); |
| 336 | } |
| 337 | else |
| 338 | relationships_node = new_relationships_node; |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | oscap_seterr(OSCAP_EFAMILY_XML, "Unknown element '%s' encountered while parsing Result DataStream to rds_index, skipping...", name); |
| 343 | } |
| 344 | |
| 345 | xmlTextReaderRead(reader); |
| 346 | } |
| 347 | |
| 348 | _parse_relationships_node(ret, relationships_node); |
| 349 | xmlFreeNode(relationships_node); |
| 350 | |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | int rds_index_select_report(struct rds_index *s, const char **report_id) |
| 355 | { |
| 356 | int ret = 1; |
| 357 | |
| 358 | struct rds_report_index_iterator *reports_it = rds_index_get_reports(s); |
| 359 | while (rds_report_index_iterator_has_more(reports_it)) |
| 360 | { |
| 361 | struct rds_report_index* report_idx = rds_report_index_iterator_next(reports_it); |
| 362 | const char *report_idx_id = rds_report_index_get_id(report_idx); |
| 363 | |
| 364 | if (!*report_id || strcmp(report_idx_id, *report_id) == 0) { |
| 365 | *report_id = report_idx_id; |
| 366 | ret = 0; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | rds_report_index_iterator_free(reports_it); |
| 371 | |
| 372 | return ret; |
| 373 | } |