| 1 | /** |
| 2 | * @file oval_resultItem.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2013 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 | |
| 37 | #include "oval_agent_api_impl.h" |
| 38 | #include "results/oval_results_impl.h" |
| 39 | #include "adt/oval_collection_impl.h" |
| 40 | #include "oval_system_characteristics_impl.h" |
| 41 | #include "common/util.h" |
| 42 | #include "common/debug_priv.h" |
| 43 | |
| 44 | typedef struct oval_result_item { |
| 45 | struct oval_result_system *sys; |
| 46 | oval_result_t result; |
| 47 | struct oval_collection *messages; |
| 48 | struct oval_sysitem *sysitem; |
| 49 | } oval_result_item_t; |
| 50 | |
| 51 | struct oval_result_item *oval_result_item_new(struct oval_result_system *sys, char *item_id) { |
| 52 | oval_result_item_t *item = (oval_result_item_t *) |
| 53 | malloc(sizeof(oval_result_item_t)); |
| 54 | if (item == NULL) |
| 55 | return NULL; |
| 56 | |
| 57 | struct oval_syschar_model *syschar_model = oval_result_system_get_syschar_model(sys); |
| 58 | struct oval_sysitem *sysitem = oval_syschar_model_get_new_sysitem(syschar_model, item_id); |
| 59 | |
| 60 | item->sysitem = sysitem; |
| 61 | item->messages = oval_collection_new(); |
| 62 | item->result = OVAL_RESULT_NOT_EVALUATED; |
| 63 | item->sys = sys; |
| 64 | |
| 65 | return item; |
| 66 | } |
| 67 | |
| 68 | struct oval_result_item *oval_result_item_clone |
| 69 | (struct oval_result_system *new_system, struct oval_result_item *old_item) { |
| 70 | struct oval_sysitem *old_sysitem = oval_result_item_get_sysitem(old_item); |
| 71 | char *datid = oval_sysitem_get_id(old_sysitem); |
| 72 | struct oval_result_item *new_item = oval_result_item_new(new_system, datid); |
| 73 | struct oval_message_iterator *old_messages = oval_result_item_get_messages(old_item); |
| 74 | while (oval_message_iterator_has_more(old_messages)) { |
| 75 | struct oval_message *old_message = oval_message_iterator_next(old_messages); |
| 76 | struct oval_message *new_message = oval_message_clone(old_message); |
| 77 | oval_result_item_add_message(new_item, new_message); |
| 78 | } |
| 79 | oval_message_iterator_free(old_messages); |
| 80 | |
| 81 | oval_result_item_set_result(old_item, oval_result_item_get_result(old_item)); |
| 82 | |
| 83 | return new_item; |
| 84 | } |
| 85 | |
| 86 | void oval_result_item_free(struct oval_result_item *item) |
| 87 | { |
| 88 | if (item == NULL) |
| 89 | return; |
| 90 | |
| 91 | oval_collection_free_items(item->messages, (oscap_destruct_func) oval_message_free); |
| 92 | |
| 93 | item->messages = NULL; |
| 94 | item->result = OVAL_RESULT_NOT_EVALUATED; |
| 95 | item->sysitem = NULL; |
| 96 | |
| 97 | free(item); |
| 98 | } |
| 99 | |
| 100 | bool oval_result_item_iterator_has_more(struct oval_result_item_iterator * oc_result_item) |
| 101 | { |
| 102 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 103 | oc_result_item); |
| 104 | } |
| 105 | |
| 106 | struct oval_result_item *oval_result_item_iterator_next(struct |
| 107 | oval_result_item_iterator |
| 108 | *oc_result_item) |
| 109 | { |
| 110 | return (struct oval_result_item *) |
| 111 | oval_collection_iterator_next((struct oval_iterator *) |
| 112 | oc_result_item); |
| 113 | } |
| 114 | |
| 115 | void oval_result_item_iterator_free(struct |
| 116 | oval_result_item_iterator |
| 117 | *oc_result_item) |
| 118 | { |
| 119 | oval_collection_iterator_free((struct oval_iterator *) |
| 120 | oc_result_item); |
| 121 | } |
| 122 | |
| 123 | struct oval_sysitem *oval_result_item_get_sysitem(struct oval_result_item *item) |
| 124 | { |
| 125 | __attribute__nonnull__(item); |
| 126 | |
| 127 | return item->sysitem; |
| 128 | } |
| 129 | |
| 130 | oval_result_t oval_result_item_get_result(struct oval_result_item * item) |
| 131 | { |
| 132 | __attribute__nonnull__(item); |
| 133 | |
| 134 | return ((struct oval_result_item *)item)->result; |
| 135 | } |
| 136 | |
| 137 | struct oval_message_iterator *oval_result_item_get_messages(struct oval_result_item *item) { |
| 138 | __attribute__nonnull__(item); |
| 139 | |
| 140 | return (struct oval_message_iterator *) |
| 141 | oval_collection_iterator(item->messages); |
| 142 | } |
| 143 | |
| 144 | void oval_result_item_set_result(struct oval_result_item *item, oval_result_t result) |
| 145 | { |
| 146 | __attribute__nonnull__(item); |
| 147 | item->result = result; |
| 148 | } |
| 149 | |
| 150 | void oval_result_item_add_message(struct oval_result_item *item, struct oval_message *message) |
| 151 | { |
| 152 | __attribute__nonnull__(item); |
| 153 | oval_collection_add(item->messages, message); |
| 154 | } |
| 155 | |
| 156 | static void _oval_result_item_message_consumer(struct oval_message *message, struct oval_result_item *item) { |
| 157 | oval_result_item_add_message(item, message); |
| 158 | } |
| 159 | |
| 160 | static int _oval_result_item_message_parse |
| 161 | (xmlTextReaderPtr reader, struct oval_parser_context *context, struct oval_result_item *item) { |
| 162 | return oval_message_parse_tag(reader, context, (oscap_consumer_func) _oval_result_item_message_consumer, item); |
| 163 | } |
| 164 | |
| 165 | int oval_result_item_parse_tag |
| 166 | (xmlTextReaderPtr reader, struct oval_parser_context *context, |
| 167 | struct oval_result_system *sys, oscap_consumer_func consumer, void *user) { |
| 168 | int return_code = 0; |
| 169 | |
| 170 | xmlChar *item_id = xmlTextReaderGetAttribute(reader, BAD_CAST "item_id"); |
| 171 | struct oval_result_item *item = oval_result_item_new(sys, (char *)item_id); |
| 172 | |
| 173 | oval_result_t result = oval_result_parse(reader, "result", 0); |
| 174 | oval_result_item_set_result(item, result); |
| 175 | |
| 176 | return_code = oval_parser_parse_tag |
| 177 | (reader, context, (oval_xml_tag_parser) _oval_result_item_message_parse, item); |
| 178 | |
| 179 | (*consumer) (item, user); |
| 180 | |
| 181 | free(item_id); |
| 182 | return return_code; |
| 183 | } |
| 184 | |
| 185 | xmlNode *oval_result_item_to_dom(struct oval_result_item * rslt_item, xmlDocPtr doc, xmlNode * parent) { |
| 186 | xmlNs *ns_results = xmlSearchNsByHref(doc, parent, OVAL_RESULTS_NAMESPACE); |
| 187 | xmlNode *item_node = xmlNewTextChild(parent, ns_results, BAD_CAST "tested_item", NULL); |
| 188 | |
| 189 | struct oval_sysitem *oval_sysitem = oval_result_item_get_sysitem(rslt_item); |
| 190 | char *item_id = oval_sysitem_get_id(oval_sysitem); |
| 191 | xmlNewProp(item_node, BAD_CAST "item_id", BAD_CAST item_id); |
| 192 | |
| 193 | oval_result_t result = oval_result_item_get_result(rslt_item); |
| 194 | xmlNewProp(item_node, BAD_CAST "result", BAD_CAST oval_result_get_text(result)); |
| 195 | |
| 196 | return item_node; |
| 197 | } |