| 1 | /** |
| 2 | * @file oval_sysEnt.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_parser_impl.h" |
| 42 | #include "oval_definitions_impl.h" |
| 43 | |
| 44 | #include "common/util.h" |
| 45 | #include "common/debug_priv.h" |
| 46 | #include "common/elements.h" |
| 47 | |
| 48 | typedef struct oval_sysent { |
| 49 | struct oval_syschar_model *model; |
| 50 | char *name; |
| 51 | char *value; |
| 52 | struct oval_collection *record_fields; |
| 53 | int mask; |
| 54 | oval_datatype_t datatype; |
| 55 | oval_syschar_status_t status; |
| 56 | } oval_sysent_t; |
| 57 | |
| 58 | struct oval_sysent *oval_sysent_new(struct oval_syschar_model *model) |
| 59 | { |
| 60 | oval_sysent_t *sysent = (oval_sysent_t *) malloc(sizeof(oval_sysent_t)); |
| 61 | if (sysent == NULL) |
| 62 | return NULL; |
| 63 | |
| 64 | sysent->name = NULL; |
| 65 | sysent->value = NULL; |
| 66 | sysent->record_fields = NULL; |
| 67 | sysent->status = SYSCHAR_STATUS_UNKNOWN; |
| 68 | sysent->datatype = OVAL_DATATYPE_UNKNOWN; |
| 69 | sysent->mask = 0; |
| 70 | sysent->model = model; |
| 71 | return sysent; |
| 72 | } |
| 73 | |
| 74 | struct oval_sysent *oval_sysent_clone(struct oval_syschar_model *new_model, struct oval_sysent *old_item) |
| 75 | { |
| 76 | struct oval_sysent *new_item = oval_sysent_new(new_model); |
| 77 | |
| 78 | char *old_value = oval_sysent_get_value(old_item); |
| 79 | if (old_value) { |
| 80 | oval_sysent_set_value(new_item, old_value); |
| 81 | } |
| 82 | |
| 83 | char *old_name = oval_sysent_get_name(old_item); |
| 84 | if (old_name) { |
| 85 | oval_sysent_set_name(new_item, oscap_strdup(old_name)); |
| 86 | } |
| 87 | |
| 88 | oval_sysent_set_datatype(new_item, oval_sysent_get_datatype(old_item)); |
| 89 | oval_sysent_set_mask(new_item, oval_sysent_get_mask(old_item)); |
| 90 | oval_sysent_set_status(new_item, oval_sysent_get_status(old_item)); |
| 91 | |
| 92 | return new_item; |
| 93 | } |
| 94 | |
| 95 | void oval_sysent_free(struct oval_sysent *sysent) |
| 96 | { |
| 97 | if (sysent == NULL) |
| 98 | return; |
| 99 | |
| 100 | if (sysent->name != NULL) |
| 101 | free(sysent->name); |
| 102 | if (sysent->value != NULL) |
| 103 | free(sysent->value); |
| 104 | if (sysent->record_fields) |
| 105 | oval_collection_free_items(sysent->record_fields, (oscap_destruct_func) oval_record_field_free); |
| 106 | |
| 107 | sysent->name = NULL; |
| 108 | sysent->value = NULL; |
| 109 | |
| 110 | free(sysent); |
| 111 | } |
| 112 | |
| 113 | bool oval_sysent_iterator_has_more(struct oval_sysent_iterator *oc_sysent) |
| 114 | { |
| 115 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 116 | oc_sysent); |
| 117 | } |
| 118 | |
| 119 | struct oval_sysent *oval_sysent_iterator_next(struct oval_sysent_iterator *oc_sysent) |
| 120 | { |
| 121 | return (struct oval_sysent *) |
| 122 | oval_collection_iterator_next((struct oval_iterator *)oc_sysent); |
| 123 | } |
| 124 | |
| 125 | void oval_sysent_iterator_free(struct oval_sysent_iterator *oc_sysent) |
| 126 | { |
| 127 | oval_collection_iterator_free((struct oval_iterator *)oc_sysent); |
| 128 | } |
| 129 | |
| 130 | char *oval_sysent_get_name(struct oval_sysent *sysent) |
| 131 | { |
| 132 | __attribute__nonnull__(sysent); |
| 133 | |
| 134 | return sysent->name; |
| 135 | } |
| 136 | |
| 137 | oval_syschar_status_t oval_sysent_get_status(struct oval_sysent * sysent) |
| 138 | { |
| 139 | __attribute__nonnull__(sysent); |
| 140 | |
| 141 | return sysent->status; |
| 142 | } |
| 143 | |
| 144 | char *oval_sysent_get_value(struct oval_sysent *sysent) |
| 145 | { |
| 146 | __attribute__nonnull__(sysent); |
| 147 | |
| 148 | return sysent->value; |
| 149 | } |
| 150 | |
| 151 | struct oval_record_field_iterator *oval_sysent_get_record_fields(struct oval_sysent *sysent) |
| 152 | { |
| 153 | if (!sysent->record_fields) |
| 154 | return (struct oval_record_field_iterator *) |
| 155 | oval_collection_iterator_new(); |
| 156 | return (struct oval_record_field_iterator *) |
| 157 | oval_collection_iterator(sysent->record_fields); |
| 158 | } |
| 159 | |
| 160 | oval_datatype_t oval_sysent_get_datatype(struct oval_sysent * sysent) |
| 161 | { |
| 162 | __attribute__nonnull__(sysent); |
| 163 | |
| 164 | return sysent->datatype; |
| 165 | } |
| 166 | |
| 167 | int oval_sysent_get_mask(struct oval_sysent *sysent) |
| 168 | { |
| 169 | __attribute__nonnull__(sysent); |
| 170 | |
| 171 | return sysent->mask; |
| 172 | } |
| 173 | |
| 174 | void oval_sysent_set_name(struct oval_sysent *sysent, char *name) |
| 175 | { |
| 176 | __attribute__nonnull__(sysent); |
| 177 | if (sysent->name != NULL) |
| 178 | free(sysent->name); |
| 179 | sysent->name = name; |
| 180 | } |
| 181 | |
| 182 | void oval_sysent_set_status(struct oval_sysent *sysent, oval_syschar_status_t status) |
| 183 | { |
| 184 | __attribute__nonnull__(sysent); |
| 185 | sysent->status = status; |
| 186 | } |
| 187 | |
| 188 | void oval_sysent_set_datatype(struct oval_sysent *sysent, oval_datatype_t datatype) |
| 189 | { |
| 190 | __attribute__nonnull__(sysent); |
| 191 | sysent->datatype = datatype; |
| 192 | } |
| 193 | |
| 194 | void oval_sysent_set_mask(struct oval_sysent *sysent, int mask) |
| 195 | { |
| 196 | __attribute__nonnull__(sysent); |
| 197 | sysent->mask = mask; |
| 198 | } |
| 199 | |
| 200 | void oval_sysent_set_value(struct oval_sysent *sysent, char *value) |
| 201 | { |
| 202 | __attribute__nonnull__(sysent); |
| 203 | if (sysent->value != NULL) |
| 204 | free(sysent->value); |
| 205 | sysent->value = oscap_strdup(value); |
| 206 | } |
| 207 | |
| 208 | void oval_sysent_add_record_field(struct oval_sysent *sysent, struct oval_record_field *rf) |
| 209 | { |
| 210 | if (sysent->record_fields == NULL) |
| 211 | sysent->record_fields = oval_collection_new(); |
| 212 | oval_collection_add(sysent->record_fields, rf); |
| 213 | } |
| 214 | |
| 215 | static void oval_sysent_value_consumer_(char *value, void *sysent) |
| 216 | { |
| 217 | oval_sysent_set_value(sysent, value); |
| 218 | } |
| 219 | |
| 220 | static void _oval_sysent_record_field_consumer(struct oval_record_field *rf, |
| 221 | struct oval_sysent *sysent) |
| 222 | { |
| 223 | oval_sysent_add_record_field(sysent, rf); |
| 224 | } |
| 225 | |
| 226 | static int _oval_sysent_parse_record_field(xmlTextReaderPtr reader, |
| 227 | struct oval_parser_context *context, |
| 228 | void *user) |
| 229 | { |
| 230 | return oval_record_field_parse_tag(reader, context, (oscap_consumer_func) |
| 231 | &_oval_sysent_record_field_consumer, |
| 232 | user, OVAL_RECORD_FIELD_ITEM); |
| 233 | } |
| 234 | |
| 235 | int oval_sysent_parse_tag(xmlTextReaderPtr reader, struct oval_parser_context *context, |
| 236 | oval_sysent_consumer consumer, void *user) |
| 237 | { |
| 238 | int ret, mask; |
| 239 | char *tagname; |
| 240 | struct oval_sysent *sysent; |
| 241 | oval_datatype_t datatype; |
| 242 | oval_syschar_status_t status; |
| 243 | |
| 244 | __attribute__nonnull__(context); |
| 245 | |
| 246 | tagname = (char *) xmlTextReaderLocalName(reader); |
| 247 | if (!strcmp("#text", tagname)) { |
| 248 | xmlFree(tagname); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | sysent = oval_sysent_new(context->syschar_model); |
| 253 | oval_sysent_set_name(sysent, tagname); |
| 254 | |
| 255 | mask = oval_parser_boolean_attribute(reader, "mask", 0); |
| 256 | oval_sysent_set_mask(sysent, mask); |
| 257 | |
| 258 | datatype = oval_datatype_parse(reader, "datatype", OVAL_DATATYPE_STRING); |
| 259 | oval_sysent_set_datatype(sysent, datatype); |
| 260 | |
| 261 | status = oval_syschar_status_parse(reader, "status", SYSCHAR_STATUS_EXISTS); |
| 262 | oval_sysent_set_status(sysent, status); |
| 263 | |
| 264 | if (datatype == OVAL_DATATYPE_RECORD) |
| 265 | ret = oval_parser_parse_tag(reader, context, &_oval_sysent_parse_record_field, sysent); |
| 266 | else |
| 267 | ret = oscap_parser_text_value(reader, &oval_sysent_value_consumer_, sysent); |
| 268 | |
| 269 | if (ret == 0) |
| 270 | (*consumer) (sysent, user); |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | void oval_sysent_to_dom(struct oval_sysent *sysent, xmlDoc * doc, xmlNode * parent) |
| 277 | { |
| 278 | struct oval_record_field_iterator *rf_itr; |
| 279 | xmlNsPtr ent_ns = parent->ns; |
| 280 | xmlNodePtr root_node = xmlDocGetRootElement(doc); |
| 281 | xmlNode *sysent_tag = NULL; |
| 282 | |
| 283 | char *tagname = oval_sysent_get_name(sysent); |
| 284 | char *content = oval_sysent_get_value(sysent); |
| 285 | bool mask = oval_sysent_get_mask(sysent); |
| 286 | |
| 287 | /* omit the value in oval_results if mask=true */ |
| 288 | if (mask && !xmlStrcmp(root_node->name, BAD_CAST OVAL_ROOT_ELM_RESULTS)) { |
| 289 | sysent_tag = xmlNewTextChild(parent, ent_ns, BAD_CAST tagname, BAD_CAST ""); |
| 290 | } else { |
| 291 | xmlChar *encoded_content = xmlEncodeEntitiesReentrant(doc, BAD_CAST content); |
| 292 | sysent_tag = xmlNewChild(parent, ent_ns, BAD_CAST tagname, encoded_content); |
| 293 | xmlFree(encoded_content); |
| 294 | } |
| 295 | |
| 296 | if (mask) { |
| 297 | xmlNewProp(sysent_tag, BAD_CAST "mask", BAD_CAST "true"); |
| 298 | } |
| 299 | |
| 300 | oval_datatype_t datatype_index = oval_sysent_get_datatype(sysent); |
| 301 | if (datatype_index != OVAL_DATATYPE_STRING) { |
| 302 | xmlNewProp(sysent_tag, BAD_CAST "datatype", BAD_CAST oval_datatype_get_text(datatype_index)); |
| 303 | } |
| 304 | |
| 305 | oval_syschar_status_t status_index = oval_sysent_get_status(sysent); |
| 306 | if (status_index != SYSCHAR_STATUS_EXISTS) { |
| 307 | xmlNewProp(sysent_tag, BAD_CAST "status", BAD_CAST oval_syschar_status_get_text(status_index)); |
| 308 | } |
| 309 | |
| 310 | rf_itr = oval_sysent_get_record_fields(sysent); |
| 311 | if (oval_record_field_iterator_has_more(rf_itr)) { |
| 312 | xmlNsPtr field_ns = xmlSearchNsByHref(doc, sysent_tag, OVAL_SYSCHAR_NAMESPACE); |
| 313 | if (field_ns == NULL) { |
| 314 | field_ns = xmlNewNs(xmlDocGetRootElement(doc), OVAL_SYSCHAR_NAMESPACE, NULL); |
| 315 | } |
| 316 | |
| 317 | while (oval_record_field_iterator_has_more(rf_itr)) { |
| 318 | struct oval_record_field *rf; |
| 319 | |
| 320 | rf = oval_record_field_iterator_next(rf_itr); |
| 321 | oval_record_field_to_dom(rf, mask, doc, sysent_tag, field_ns); |
| 322 | } |
| 323 | } |
| 324 | oval_record_field_iterator_free(rf_itr); |
| 325 | } |