| 1 | /** |
| 2 | * @file oval_sysItem.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 | #include <string.h> |
| 37 | |
| 38 | #include "oval_agent_api_impl.h" |
| 39 | #include "oval_system_characteristics_impl.h" |
| 40 | #include "adt/oval_collection_impl.h" |
| 41 | #include "oval_definitions_impl.h" |
| 42 | #include "common/util.h" |
| 43 | #include "common/debug_priv.h" |
| 44 | |
| 45 | typedef struct oval_sysitem { |
| 46 | //oval_family_enum family; |
| 47 | struct oval_syschar_model *model; |
| 48 | oval_subtype_t subtype; |
| 49 | char *id; |
| 50 | struct oval_collection *messages; |
| 51 | struct oval_collection *sysents; |
| 52 | oval_syschar_status_t status; |
| 53 | } oval_sysitem_t; ///< Represents a single <*_item> element |
| 54 | |
| 55 | struct oval_sysitem *oval_sysitem_new(struct oval_syschar_model *model, const char *id) |
| 56 | { |
| 57 | __attribute__nonnull__(model); |
| 58 | oval_sysitem_t *sysitem; |
| 59 | |
| 60 | sysitem = (oval_sysitem_t *) malloc(sizeof(oval_sysitem_t)); |
| 61 | if (sysitem == NULL) |
| 62 | return NULL; |
| 63 | |
| 64 | sysitem->id = oscap_strdup(id); |
| 65 | sysitem->subtype = OVAL_SUBTYPE_UNKNOWN; |
| 66 | sysitem->status = SYSCHAR_STATUS_UNKNOWN; |
| 67 | sysitem->messages = oval_collection_new(); |
| 68 | sysitem->sysents = oval_collection_new(); |
| 69 | sysitem->model = model; |
| 70 | |
| 71 | oval_syschar_model_add_sysitem(model, sysitem); |
| 72 | |
| 73 | return sysitem; |
| 74 | } |
| 75 | |
| 76 | struct oval_sysitem *oval_sysitem_clone(struct oval_syschar_model *new_model, struct oval_sysitem *old_item) |
| 77 | { |
| 78 | struct oval_sysitem *new_item = oval_sysitem_new(new_model, oval_sysitem_get_id(old_item)); |
| 79 | |
| 80 | struct oval_message_iterator *old_messages = oval_sysitem_get_messages(old_item); |
| 81 | while (oval_message_iterator_has_more(old_messages)) { |
| 82 | struct oval_message *old_message = oval_message_iterator_next(old_messages); |
| 83 | struct oval_message *new_message = oval_message_clone(old_message); |
| 84 | oval_sysitem_add_message(new_item, new_message); |
| 85 | } |
| 86 | oval_message_iterator_free(old_messages); |
| 87 | |
| 88 | oval_sysitem_set_status(new_item, oval_sysitem_get_status(old_item)); |
| 89 | oval_sysitem_set_subtype(new_item, oval_sysitem_get_subtype(old_item)); |
| 90 | |
| 91 | struct oval_sysent_iterator *old_sysent_itr = oval_sysitem_get_sysents(old_item); |
| 92 | while (oval_sysent_iterator_has_more(old_sysent_itr)) { |
| 93 | struct oval_sysent *old_sysent = oval_sysent_iterator_next(old_sysent_itr); |
| 94 | struct oval_sysent *new_sysent = oval_sysent_clone(new_model, old_sysent); |
| 95 | oval_sysitem_add_sysent(new_item, new_sysent); |
| 96 | } |
| 97 | oval_sysent_iterator_free(old_sysent_itr); |
| 98 | |
| 99 | return new_item; |
| 100 | } |
| 101 | |
| 102 | void oval_sysitem_free(struct oval_sysitem *sysitem) |
| 103 | { |
| 104 | if (sysitem == NULL) |
| 105 | return; |
| 106 | |
| 107 | oval_collection_free_items(sysitem->messages, (oscap_destruct_func) oval_message_free); |
| 108 | oval_collection_free_items(sysitem->sysents, (oscap_destruct_func) oval_sysent_free); |
| 109 | free(sysitem->id); |
| 110 | |
| 111 | sysitem->id = NULL; |
| 112 | sysitem->sysents = NULL; |
| 113 | sysitem->messages = NULL; |
| 114 | free(sysitem); |
| 115 | } |
| 116 | |
| 117 | bool oval_sysitem_iterator_has_more(struct oval_sysitem_iterator *oc_sysitem) |
| 118 | { |
| 119 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 120 | oc_sysitem); |
| 121 | } |
| 122 | |
| 123 | struct oval_sysitem *oval_sysitem_iterator_next(struct oval_sysitem_iterator |
| 124 | *oc_sysitem) |
| 125 | { |
| 126 | return (struct oval_sysitem *) |
| 127 | oval_collection_iterator_next((struct oval_iterator *)oc_sysitem); |
| 128 | } |
| 129 | |
| 130 | void oval_sysitem_iterator_free(struct oval_sysitem_iterator |
| 131 | *oc_sysitem) |
| 132 | { |
| 133 | oval_collection_iterator_free((struct oval_iterator *)oc_sysitem); |
| 134 | } |
| 135 | |
| 136 | oval_subtype_t oval_sysitem_get_subtype(struct oval_sysitem *sysitem) |
| 137 | { |
| 138 | __attribute__nonnull__(sysitem); |
| 139 | |
| 140 | return sysitem->subtype; |
| 141 | } |
| 142 | |
| 143 | void oval_sysitem_set_subtype(struct oval_sysitem *sysitem, oval_subtype_t subtype) |
| 144 | { |
| 145 | __attribute__nonnull__(sysitem); |
| 146 | sysitem->subtype = subtype; |
| 147 | } |
| 148 | |
| 149 | char *oval_sysitem_get_id(struct oval_sysitem *item) |
| 150 | { |
| 151 | __attribute__nonnull__(item); |
| 152 | return item->id; |
| 153 | } |
| 154 | |
| 155 | struct oval_message_iterator *oval_sysitem_get_messages(struct oval_sysitem *item) |
| 156 | { |
| 157 | __attribute__nonnull__(item); |
| 158 | return (struct oval_message_iterator *)oval_collection_iterator(item->messages); |
| 159 | } |
| 160 | |
| 161 | void oval_sysitem_add_message(struct oval_sysitem *item, struct oval_message *message) |
| 162 | { |
| 163 | __attribute__nonnull__(item); |
| 164 | oval_collection_add(item->messages, message); |
| 165 | } |
| 166 | |
| 167 | struct oval_sysent_iterator *oval_sysitem_get_sysents(struct oval_sysitem *sysitem) |
| 168 | { |
| 169 | __attribute__nonnull__(sysitem); |
| 170 | return (struct oval_sysent_iterator *)oval_collection_iterator(sysitem->sysents); |
| 171 | } |
| 172 | |
| 173 | void oval_sysitem_add_sysent(struct oval_sysitem *sysitem, struct oval_sysent *sysent) |
| 174 | { |
| 175 | __attribute__nonnull__(sysitem); |
| 176 | oval_collection_add(sysitem->sysents, sysent); |
| 177 | } |
| 178 | |
| 179 | oval_syschar_status_t oval_sysitem_get_status(struct oval_sysitem *data) |
| 180 | { |
| 181 | __attribute__nonnull__(data); |
| 182 | |
| 183 | return data->status; |
| 184 | } |
| 185 | |
| 186 | void oval_sysitem_set_status(struct oval_sysitem *data, oval_syschar_status_t status) |
| 187 | { |
| 188 | __attribute__nonnull__(data); |
| 189 | data->status = status; |
| 190 | } |
| 191 | |
| 192 | static void _oval_sysitem_parse_subtag_message_consumer(struct oval_message *message, void *sysitem) |
| 193 | { |
| 194 | oval_sysitem_add_message(sysitem, message); |
| 195 | } |
| 196 | |
| 197 | static void _oval_sysitem_parse_subtag_sysent_consumer(struct oval_sysent *sysent, void *sysitem) |
| 198 | { |
| 199 | oval_sysitem_add_sysent(sysitem, sysent); |
| 200 | } |
| 201 | |
| 202 | static int _oval_sysitem_parse_subtag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *client) |
| 203 | { |
| 204 | struct oval_sysitem *sysitem = client; |
| 205 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 206 | char *namespace = (char *)xmlTextReaderNamespaceUri(reader); |
| 207 | int return_code = 0; |
| 208 | if (oscap_strcmp((const char *)OVAL_SYSCHAR_NAMESPACE, namespace) == 0) { |
| 209 | /* This is a message */ |
| 210 | return_code = oval_message_parse_tag(reader, context, (oscap_consumer_func) _oval_sysitem_parse_subtag_message_consumer, sysitem); |
| 211 | } else { |
| 212 | /*typedef *(oval_sysent_consumer)(struct oval_sysent *, void* client); */ |
| 213 | return_code = oval_sysent_parse_tag(reader, context, _oval_sysitem_parse_subtag_sysent_consumer, sysitem); |
| 214 | } |
| 215 | free(tagname); |
| 216 | free(namespace); |
| 217 | return return_code; |
| 218 | } |
| 219 | |
| 220 | int oval_sysitem_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, void *usr) |
| 221 | { |
| 222 | __attribute__nonnull__(context); |
| 223 | |
| 224 | char *tagname = (char *)xmlTextReaderLocalName(reader); |
| 225 | oval_subtype_t subtype = oval_subtype_parse(reader); |
| 226 | int return_code = 0; |
| 227 | if (subtype != OVAL_SUBTYPE_UNKNOWN) { |
| 228 | char *item_id = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); |
| 229 | struct oval_sysitem *sysitem = oval_syschar_model_get_new_sysitem(context->syschar_model, item_id); |
| 230 | free(item_id); |
| 231 | |
| 232 | oval_sysitem_set_subtype(sysitem, subtype); |
| 233 | |
| 234 | oval_syschar_status_t status_enum = oval_syschar_status_parse(reader, "status", SYSCHAR_STATUS_EXISTS); |
| 235 | oval_sysitem_set_status(sysitem, status_enum); |
| 236 | |
| 237 | return_code = oval_parser_parse_tag(reader, context, &_oval_sysitem_parse_subtag, sysitem); |
| 238 | } else { |
| 239 | dW("Unknown sysitem: %s", tagname); |
| 240 | return_code = oval_parser_skip_tag(reader, context); |
| 241 | } |
| 242 | |
| 243 | if (return_code != 0) { |
| 244 | dW("Parsing of <%s> terminated by an error at line %d.", tagname, xmlTextReaderGetParserLineNumber(reader)); |
| 245 | } |
| 246 | |
| 247 | free(tagname); |
| 248 | |
| 249 | return return_code; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void oval_sysitem_to_dom(struct oval_sysitem *sysitem, xmlDoc * doc, xmlNode * parent) |
| 254 | { |
| 255 | if (sysitem) { |
| 256 | oval_subtype_t subtype = oval_sysitem_get_subtype(sysitem); |
| 257 | if (subtype) { |
| 258 | /* get item subtype */ |
| 259 | const char *subtype_text = oval_subtype_get_text(subtype); |
| 260 | char *tagname = malloc(strlen(subtype_text) + 6); |
| 261 | sprintf(tagname, "%s_item", subtype_text); |
| 262 | |
| 263 | oval_family_t family = oval_subtype_get_family(subtype); |
| 264 | |
| 265 | /* search namespace & create child */ |
| 266 | xmlNs *ns_family = oval_family_to_namespace(family, (const char *) OVAL_SYSCHAR_NAMESPACE, doc, parent); |
| 267 | xmlNode *tag_sysitem = xmlNewTextChild(parent, ns_family, BAD_CAST tagname, NULL); |
| 268 | free(tagname); |
| 269 | |
| 270 | /* attributes */ |
| 271 | xmlNewProp(tag_sysitem, BAD_CAST "id", BAD_CAST oval_sysitem_get_id(sysitem)); |
| 272 | oval_syschar_status_t status_index = oval_sysitem_get_status(sysitem); |
| 273 | const char *status = oval_syschar_status_get_text(status_index); |
| 274 | xmlNewProp(tag_sysitem, BAD_CAST "status", BAD_CAST status); |
| 275 | |
| 276 | /* messages */ |
| 277 | struct oval_message_iterator *messages = oval_sysitem_get_messages(sysitem); |
| 278 | while (oval_message_iterator_has_more(messages)) { |
| 279 | struct oval_message *message = oval_message_iterator_next(messages); |
| 280 | oval_message_to_dom(message, doc, tag_sysitem); |
| 281 | } |
| 282 | oval_message_iterator_free(messages); |
| 283 | |
| 284 | /* sysents */ |
| 285 | struct oval_sysent_iterator *sysent_itr = oval_sysitem_get_sysents(sysitem); |
| 286 | while (oval_sysent_iterator_has_more(sysent_itr)) { |
| 287 | struct oval_sysent *sysent = oval_sysent_iterator_next(sysent_itr); |
| 288 | oval_sysent_to_dom(sysent, doc, tag_sysitem); |
| 289 | } |
| 290 | oval_sysent_iterator_free(sysent_itr); |
| 291 | } |
| 292 | } |
| 293 | } |