| 1 | /** |
| 2 | * @file oval_stateContent.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_definitions_impl.h" |
| 39 | #include "adt/oval_collection_impl.h" |
| 40 | #include "oval_agent_api_impl.h" |
| 41 | #include "common/util.h" |
| 42 | #include "common/debug_priv.h" |
| 43 | #include "oval_schema_version.h" |
| 44 | |
| 45 | typedef struct oval_state_content { |
| 46 | struct oval_definition_model *model; |
| 47 | struct oval_entity *entity; |
| 48 | struct oval_collection *record_fields; |
| 49 | oval_check_t ent_check; |
| 50 | oval_check_t var_check; |
| 51 | oval_existence_t check_existence; |
| 52 | } oval_state_content_t; |
| 53 | |
| 54 | bool oval_state_content_iterator_has_more(struct oval_state_content_iterator |
| 55 | *oc_state_content) |
| 56 | { |
| 57 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 58 | oc_state_content); |
| 59 | } |
| 60 | |
| 61 | struct oval_state_content *oval_state_content_iterator_next(struct |
| 62 | oval_state_content_iterator |
| 63 | *oc_state_content) |
| 64 | { |
| 65 | return (struct oval_state_content *) |
| 66 | oval_collection_iterator_next((struct oval_iterator *) |
| 67 | oc_state_content); |
| 68 | } |
| 69 | |
| 70 | void oval_state_content_iterator_free(struct |
| 71 | oval_state_content_iterator |
| 72 | *oc_state_content) |
| 73 | { |
| 74 | oval_collection_iterator_free((struct oval_iterator *) |
| 75 | oc_state_content); |
| 76 | } |
| 77 | |
| 78 | struct oval_entity *oval_state_content_get_entity(struct oval_state_content |
| 79 | *content) |
| 80 | { |
| 81 | __attribute__nonnull__(content); |
| 82 | |
| 83 | return content->entity; |
| 84 | } |
| 85 | |
| 86 | struct oval_record_field_iterator *oval_state_content_get_record_fields(struct oval_state_content *content) |
| 87 | { |
| 88 | if (!content->record_fields) |
| 89 | return (struct oval_record_field_iterator *) |
| 90 | oval_collection_iterator_new(); |
| 91 | return (struct oval_record_field_iterator *) |
| 92 | oval_collection_iterator(content->record_fields); |
| 93 | } |
| 94 | |
| 95 | oval_check_t oval_state_content_get_var_check(struct oval_state_content * content) |
| 96 | { |
| 97 | __attribute__nonnull__(content); |
| 98 | |
| 99 | return content->var_check; |
| 100 | } |
| 101 | |
| 102 | oval_check_t oval_state_content_get_ent_check(struct oval_state_content * content) |
| 103 | { |
| 104 | __attribute__nonnull__(content); |
| 105 | |
| 106 | return content->ent_check; |
| 107 | } |
| 108 | |
| 109 | oval_existence_t oval_state_content_get_check_existence(struct oval_state_content *content) |
| 110 | { |
| 111 | __attribute__nonnull__(content); |
| 112 | |
| 113 | return content->check_existence; |
| 114 | } |
| 115 | |
| 116 | struct oval_state_content *oval_state_content_new(struct oval_definition_model *model) |
| 117 | { |
| 118 | oval_state_content_t *content = (oval_state_content_t *) |
| 119 | malloc(sizeof(oval_state_content_t)); |
| 120 | if (content == NULL) |
| 121 | return NULL; |
| 122 | |
| 123 | content->entity = NULL; |
| 124 | content->record_fields = oval_collection_new(); |
| 125 | content->ent_check = OVAL_CHECK_UNKNOWN; |
| 126 | content->var_check = OVAL_CHECK_UNKNOWN; |
| 127 | content->check_existence = OVAL_EXISTENCE_UNKNOWN; |
| 128 | content->model = model; |
| 129 | return content; |
| 130 | } |
| 131 | |
| 132 | struct oval_state_content *oval_state_content_clone |
| 133 | (struct oval_definition_model *new_model, struct oval_state_content *old_content) { |
| 134 | struct oval_state_content *new_content = oval_state_content_new(new_model); |
| 135 | oval_check_t echeck = oval_state_content_get_ent_check(old_content); |
| 136 | oval_state_content_set_entcheck(new_content, echeck); |
| 137 | struct oval_entity *entity = oval_state_content_get_entity(old_content); |
| 138 | oval_state_content_set_entity(new_content, oval_entity_clone(new_model, entity)); |
| 139 | oval_check_t vcheck = oval_state_content_get_var_check(old_content); |
| 140 | oval_state_content_set_varcheck(new_content, vcheck); |
| 141 | oval_existence_t check_existence = oval_state_content_get_check_existence(old_content); |
| 142 | oval_state_content_set_check_existence(new_content, check_existence); |
| 143 | return new_content; |
| 144 | } |
| 145 | |
| 146 | void oval_state_content_free(struct oval_state_content *content) |
| 147 | { |
| 148 | __attribute__nonnull__(content); |
| 149 | |
| 150 | if (content->entity) |
| 151 | oval_entity_free(content->entity); |
| 152 | if (content->record_fields) |
| 153 | oval_collection_free_items(content->record_fields, (oscap_destruct_func) oval_record_field_free); |
| 154 | free(content); |
| 155 | } |
| 156 | |
| 157 | void oval_state_content_set_entity(struct oval_state_content *content, struct oval_entity *entity) |
| 158 | { |
| 159 | __attribute__nonnull__(content); |
| 160 | if (content->entity) |
| 161 | oval_entity_free(content->entity); |
| 162 | content->entity = entity; |
| 163 | } |
| 164 | |
| 165 | void oval_state_content_add_record_field(struct oval_state_content *content, struct oval_record_field *rf) |
| 166 | { |
| 167 | oval_collection_add(content->record_fields, rf); |
| 168 | } |
| 169 | |
| 170 | void oval_state_content_set_varcheck(struct oval_state_content *content, oval_check_t check) |
| 171 | { |
| 172 | __attribute__nonnull__(content); |
| 173 | content->var_check = check; |
| 174 | } |
| 175 | |
| 176 | void oval_state_content_set_entcheck(struct oval_state_content *content, oval_check_t check) |
| 177 | { |
| 178 | __attribute__nonnull__(content); |
| 179 | content->ent_check = check; |
| 180 | } |
| 181 | |
| 182 | void oval_state_content_set_check_existence(struct oval_state_content *content, oval_existence_t existence) |
| 183 | { |
| 184 | __attribute__nonnull__(content); |
| 185 | content->check_existence = existence; |
| 186 | } |
| 187 | |
| 188 | static void _oval_state_content_entity_consumer(struct oval_entity *entity, struct oval_state_content *content) { |
| 189 | oval_state_content_set_entity(content, entity); |
| 190 | } |
| 191 | |
| 192 | static void _oval_state_content_record_field_consumer(struct oval_record_field *rf, struct oval_state_content *content) |
| 193 | { |
| 194 | oval_state_content_add_record_field(content, rf); |
| 195 | } |
| 196 | |
| 197 | static int _oval_state_content_parse_record_field(xmlTextReaderPtr reader, struct oval_parser_context *context, void *user) |
| 198 | { |
| 199 | return oval_record_field_parse_tag(reader, context, (oscap_consumer_func) |
| 200 | &_oval_state_content_record_field_consumer, |
| 201 | user, OVAL_RECORD_FIELD_STATE); |
| 202 | } |
| 203 | |
| 204 | int oval_state_content_parse_tag(xmlTextReaderPtr reader, |
| 205 | struct oval_parser_context *context, oscap_consumer_func consumer, void *user) |
| 206 | { |
| 207 | __attribute__nonnull__(context); |
| 208 | |
| 209 | oval_datatype_t ent_datatype; |
| 210 | struct oval_state_content *content = oval_state_content_new(context->definition_model); |
| 211 | int retcode = oval_entity_parse_tag |
| 212 | (reader, context, (oscap_consumer_func) _oval_state_content_entity_consumer, content); |
| 213 | |
| 214 | oval_check_t var_check = oval_check_parse(reader, "var_check", OVAL_CHECK_ALL); |
| 215 | oval_check_t ent_check = oval_check_parse(reader, "entity_check", OVAL_CHECK_ALL); |
| 216 | /* "check_existence" is new optional attribute in OVAL 5.11.1 |
| 217 | * if this attribute is not present, the default value |
| 218 | * of "at_least_one_exists" is used |
| 219 | */ |
| 220 | oval_existence_t check_existence = oval_existence_parse(reader, |
| 221 | "check_existence", OVAL_AT_LEAST_ONE_EXISTS); |
| 222 | |
| 223 | ent_datatype = oval_entity_get_datatype(content->entity); |
| 224 | if (ent_datatype == OVAL_DATATYPE_RECORD) { |
| 225 | retcode = oval_parser_parse_tag(reader, context, |
| 226 | &_oval_state_content_parse_record_field, content); |
| 227 | } |
| 228 | |
| 229 | oval_state_content_set_varcheck(content, var_check); |
| 230 | oval_state_content_set_entcheck(content, ent_check); |
| 231 | oval_state_content_set_check_existence(content, check_existence); |
| 232 | |
| 233 | (*consumer) (content, user); |
| 234 | return retcode; |
| 235 | } |
| 236 | |
| 237 | xmlNode *oval_state_content_to_dom(struct oval_state_content * content, xmlDoc * doc, xmlNode * parent) { |
| 238 | __attribute__nonnull__(content); |
| 239 | |
| 240 | struct oval_record_field_iterator *rf_itr; |
| 241 | bool parent_mask; |
| 242 | xmlNode *content_node = oval_entity_to_dom(content->entity, doc, parent); |
| 243 | |
| 244 | parent_mask = oval_entity_get_mask(content->entity); |
| 245 | |
| 246 | rf_itr = oval_state_content_get_record_fields(content); |
| 247 | if (oval_record_field_iterator_has_more(rf_itr)) { |
| 248 | xmlNsPtr field_ns = NULL; |
| 249 | field_ns = xmlSearchNsByHref(doc, xmlDocGetRootElement(doc), OVAL_DEFINITIONS_NAMESPACE); |
| 250 | if (field_ns == NULL) { |
| 251 | field_ns = xmlNewNs(xmlDocGetRootElement(doc), OVAL_DEFINITIONS_NAMESPACE, BAD_CAST "oval-def"); |
| 252 | } |
| 253 | |
| 254 | while (oval_record_field_iterator_has_more(rf_itr)) { |
| 255 | struct oval_record_field *rf; |
| 256 | |
| 257 | rf = oval_record_field_iterator_next(rf_itr); |
| 258 | oval_record_field_to_dom(rf, parent_mask, doc, content_node, field_ns); |
| 259 | } |
| 260 | } |
| 261 | oval_record_field_iterator_free(rf_itr); |
| 262 | |
| 263 | oval_check_t var_check = oval_state_content_get_var_check(content); |
| 264 | if (var_check != OVAL_CHECK_ALL || xmlHasProp(content_node, BAD_CAST "var_ref")) |
| 265 | xmlNewProp(content_node, BAD_CAST "var_check", BAD_CAST oval_check_get_text(var_check)); |
| 266 | |
| 267 | oval_check_t ent_check = oval_state_content_get_ent_check(content); |
| 268 | if (ent_check != OVAL_CHECK_ALL) |
| 269 | xmlNewProp(content_node, BAD_CAST "entity_check", BAD_CAST oval_check_get_text(ent_check)); |
| 270 | oval_schema_version_t ver = oval_definition_model_get_core_schema_version(content->model); |
| 271 | if (oval_schema_version_cmp(ver, OVAL_SCHEMA_VERSION(5.11.1)) >= 0) { |
| 272 | oval_existence_t check_existence = oval_state_content_get_check_existence(content); |
| 273 | if (check_existence != OVAL_AT_LEAST_ONE_EXISTS) { |
| 274 | // at_least_one_exists is default value |
| 275 | xmlNewProp(content_node, BAD_CAST "check_existence", BAD_CAST oval_existence_get_text(check_existence)); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return content_node; |
| 280 | } |