| 1 | /** |
| 2 | * @file oval_variableBinding.c |
| 3 | * \brief Open Vulnerability and Assessment Language |
| 4 | * |
| 5 | * See more details at http://oval.mitre.org/ |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Copyright 2009--2014 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_definitions_impl.h" |
| 40 | #include "adt/oval_collection_impl.h" |
| 41 | #include "common/util.h" |
| 42 | #include "common/debug_priv.h" |
| 43 | #include "common/elements.h" |
| 44 | |
| 45 | typedef struct oval_variable_binding { |
| 46 | struct oval_variable *variable; |
| 47 | struct oval_collection *values; |
| 48 | } oval_variable_binding_t; |
| 49 | |
| 50 | bool oval_variable_binding_iterator_has_more(struct |
| 51 | oval_variable_binding_iterator |
| 52 | *oc_variable_binding) |
| 53 | { |
| 54 | return oval_collection_iterator_has_more((struct oval_iterator *) |
| 55 | oc_variable_binding); |
| 56 | } |
| 57 | |
| 58 | struct oval_variable_binding *oval_variable_binding_iterator_next(struct |
| 59 | oval_variable_binding_iterator |
| 60 | *oc_variable_binding) |
| 61 | { |
| 62 | return (struct oval_variable_binding *) |
| 63 | oval_collection_iterator_next((struct oval_iterator *) |
| 64 | oc_variable_binding); |
| 65 | } |
| 66 | |
| 67 | void oval_variable_binding_iterator_free(struct |
| 68 | oval_variable_binding_iterator |
| 69 | *oc_variable_binding) |
| 70 | { |
| 71 | oval_collection_iterator_free((struct oval_iterator *) |
| 72 | oc_variable_binding); |
| 73 | } |
| 74 | |
| 75 | struct oval_variable *oval_variable_binding_get_variable(struct |
| 76 | oval_variable_binding |
| 77 | *binding) |
| 78 | { |
| 79 | __attribute__nonnull__(binding); |
| 80 | |
| 81 | return ((struct oval_variable_binding *)binding)->variable; |
| 82 | } |
| 83 | |
| 84 | struct oval_string_iterator *oval_variable_binding_get_values(struct oval_variable_binding *binding) |
| 85 | { |
| 86 | __attribute__nonnull__(binding); |
| 87 | |
| 88 | return (struct oval_string_iterator *) |
| 89 | oval_collection_iterator(binding->values); |
| 90 | } |
| 91 | |
| 92 | void oval_variable_binding_set_variable(struct oval_variable_binding *binding, struct oval_variable *variable) |
| 93 | { |
| 94 | __attribute__nonnull__(binding); |
| 95 | |
| 96 | binding->variable = variable; |
| 97 | } |
| 98 | |
| 99 | void oval_variable_binding_add_value(struct oval_variable_binding *binding, char *value) |
| 100 | { |
| 101 | __attribute__nonnull__(binding); |
| 102 | __attribute__nonnull__(value); |
| 103 | |
| 104 | oval_collection_add(binding->values, value); |
| 105 | } |
| 106 | |
| 107 | struct oval_variable_binding *oval_variable_binding_new(struct oval_variable *variable, char *value) |
| 108 | { |
| 109 | oval_variable_binding_t *binding = (oval_variable_binding_t *) malloc(sizeof(oval_variable_binding_t)); |
| 110 | if (binding == NULL) |
| 111 | return NULL; |
| 112 | |
| 113 | binding->variable = variable; |
| 114 | binding->values = oval_collection_new(); |
| 115 | if (value != NULL) |
| 116 | oval_collection_add(binding->values, value); |
| 117 | return binding; |
| 118 | } |
| 119 | |
| 120 | struct oval_variable_binding *oval_variable_binding_clone(struct oval_variable_binding *old_binding, |
| 121 | struct oval_definition_model *def_model) |
| 122 | { |
| 123 | struct oval_variable *old_variable = oval_variable_binding_get_variable(old_binding); |
| 124 | char *varid = oval_variable_get_id(old_variable); |
| 125 | struct oval_variable *new_variable = oval_definition_model_get_variable(def_model, varid); |
| 126 | struct oval_string_iterator *str_itr; |
| 127 | struct oval_variable_binding *new_binding; |
| 128 | |
| 129 | if (new_variable == NULL) |
| 130 | oval_variable_clone(def_model, old_variable); |
| 131 | new_binding = oval_variable_binding_new(new_variable, NULL); |
| 132 | |
| 133 | str_itr = oval_variable_binding_get_values(old_binding); |
| 134 | while (oval_string_iterator_has_more(str_itr)) { |
| 135 | char *s; |
| 136 | |
| 137 | s = oval_string_iterator_next(str_itr); |
| 138 | s = oscap_strdup(s); |
| 139 | oval_variable_binding_add_value(new_binding, s); |
| 140 | } |
| 141 | oval_string_iterator_free(str_itr); |
| 142 | |
| 143 | return new_binding; |
| 144 | } |
| 145 | |
| 146 | void oval_variable_binding_free(struct oval_variable_binding *binding) |
| 147 | { |
| 148 | if (binding) { |
| 149 | oval_collection_free_items(binding->values, (oscap_destruct_func) free); |
| 150 | binding->values = NULL; |
| 151 | binding->variable = NULL; |
| 152 | |
| 153 | free(binding); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void _oval_variable_binding_value_consumer(char *value, void *user) |
| 158 | { |
| 159 | struct oval_variable_binding *binding = (struct oval_variable_binding *) user; |
| 160 | |
| 161 | value = oscap_strdup(value); |
| 162 | oval_variable_binding_add_value(binding, value); |
| 163 | } |
| 164 | |
| 165 | int oval_variable_binding_parse_tag(xmlTextReaderPtr reader, |
| 166 | struct oval_parser_context *context, oval_variable_binding_consumer consumer, |
| 167 | void *client) |
| 168 | { |
| 169 | __attribute__nonnull__(context); |
| 170 | |
| 171 | int return_code = 0; |
| 172 | struct oval_variable_binding *binding = oval_variable_binding_new(NULL, NULL); |
| 173 | /* variable */ |
| 174 | char *variableId = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "variable_id"); |
| 175 | struct oval_variable *variable = oval_definition_model_get_new_variable(context->definition_model, variableId, OVAL_VARIABLE_UNKNOWN); |
| 176 | oval_variable_binding_set_variable(binding, variable); |
| 177 | free(variableId); |
| 178 | |
| 179 | /* bound value */ |
| 180 | return_code = oscap_parser_text_value(reader, &_oval_variable_binding_value_consumer, binding); |
| 181 | |
| 182 | if (return_code == 0) |
| 183 | (*consumer) (binding, client); |
| 184 | |
| 185 | return return_code; |
| 186 | } |
| 187 | |
| 188 | void oval_variable_binding_to_dom(struct oval_variable_binding *binding, xmlDoc *doc, xmlNode *parent_tag) |
| 189 | { |
| 190 | struct oval_string_iterator *val_itr; |
| 191 | xmlNs *ns_syschar; |
| 192 | |
| 193 | __attribute__nonnull__(binding); |
| 194 | |
| 195 | ns_syschar = xmlSearchNsByHref(doc, parent_tag, OVAL_SYSCHAR_NAMESPACE); |
| 196 | |
| 197 | val_itr = oval_variable_binding_get_values(binding); |
| 198 | while (oval_string_iterator_has_more(val_itr)) { |
| 199 | char *val; |
| 200 | xmlNode *binding_tag; |
| 201 | struct oval_variable *var; |
| 202 | |
| 203 | val = oval_string_iterator_next(val_itr); |
| 204 | binding_tag = xmlNewTextChild(parent_tag, ns_syschar, BAD_CAST "variable_value", BAD_CAST val); |
| 205 | var = oval_variable_binding_get_variable(binding); |
| 206 | xmlNewProp(binding_tag, BAD_CAST "variable_id", BAD_CAST oval_variable_get_id(var)); |
| 207 | } |
| 208 | oval_string_iterator_free(val_itr); |
| 209 | } |