| 1 | /* |
| 2 | * Copyright 2013 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Šimon Lukašík |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "oval_definitions_impl.h" |
| 28 | |
| 29 | static void _oval_definition_fill_vardef(struct oval_definition *definition, struct oval_string_map *vardef); |
| 30 | static void _oval_criteria_fill_vardef(struct oval_criteria_node *cnode, struct oval_string_map *vardef, const char *definition_id); |
| 31 | static void _oval_test_fill_vardef(struct oval_test *test, struct oval_string_map *vardef, const char *definition_id); |
| 32 | static void _oval_object_fill_vardef(struct oval_object *object, struct oval_string_map *vardef, const char *definition_id); |
| 33 | static void _oval_setobject_fill_vardef(struct oval_setobject *set, struct oval_string_map *vardef, const char *definition_id); |
| 34 | static void _oval_state_fill_vardef(struct oval_state *state, struct oval_string_map *vardef, const char *definition_id); |
| 35 | static void _oval_entity_fill_vardef(struct oval_entity *entity, struct oval_string_map *vardef, const char *definition_id); |
| 36 | static void _vardef_insert(struct oval_string_map *vardef, const char *definition_id, const char *variable_id); |
| 37 | |
| 38 | struct oval_string_map *oval_definition_model_build_vardef_mapping(struct oval_definition_model *model) |
| 39 | { |
| 40 | struct oval_string_map *vardef = oval_string_map_new(); |
| 41 | struct oval_definition_iterator *def_it = oval_definition_model_get_definitions(model); |
| 42 | while (oval_definition_iterator_has_more(def_it)) { |
| 43 | struct oval_definition *definition = oval_definition_iterator_next(def_it); |
| 44 | _oval_definition_fill_vardef(definition, vardef); |
| 45 | } |
| 46 | oval_definition_iterator_free(def_it); |
| 47 | return vardef; |
| 48 | } |
| 49 | |
| 50 | void _oval_definition_fill_vardef(struct oval_definition *definition, struct oval_string_map *vardef) |
| 51 | { |
| 52 | struct oval_criteria_node *cnode = oval_definition_get_criteria(definition); |
| 53 | if (cnode != NULL) |
| 54 | _oval_criteria_fill_vardef(cnode, vardef, oval_definition_get_id(definition)); |
| 55 | } |
| 56 | |
| 57 | void _oval_criteria_fill_vardef(struct oval_criteria_node *cnode, struct oval_string_map *vardef, const char *definition_id) |
| 58 | { |
| 59 | switch (oval_criteria_node_get_type(cnode)) { |
| 60 | case OVAL_NODETYPE_CRITERION:{ |
| 61 | struct oval_test *test = oval_criteria_node_get_test(cnode); |
| 62 | if (test != NULL) |
| 63 | _oval_test_fill_vardef(test, vardef, definition_id); |
| 64 | } break; |
| 65 | case OVAL_NODETYPE_CRITERIA:{ |
| 66 | struct oval_criteria_node_iterator *cnode_it = oval_criteria_node_get_subnodes(cnode); |
| 67 | if (cnode_it != NULL) { |
| 68 | while (oval_criteria_node_iterator_has_more(cnode_it)) { |
| 69 | struct oval_criteria_node *node = oval_criteria_node_iterator_next(cnode_it); |
| 70 | if (node != NULL) |
| 71 | _oval_criteria_fill_vardef(node, vardef, definition_id); |
| 72 | } |
| 73 | oval_criteria_node_iterator_free(cnode_it); |
| 74 | } |
| 75 | } break; |
| 76 | case OVAL_NODETYPE_EXTENDDEF:{ |
| 77 | struct oval_definition *definition = oval_criteria_node_get_definition(cnode); |
| 78 | if (definition != NULL) { |
| 79 | struct oval_criteria_node *node = oval_definition_get_criteria(definition); |
| 80 | if (node != NULL) |
| 81 | _oval_criteria_fill_vardef(node, vardef, definition_id); |
| 82 | } |
| 83 | } break; |
| 84 | case OVAL_NODETYPE_UNKNOWN: |
| 85 | default: |
| 86 | assert(false); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void _oval_test_fill_vardef(struct oval_test *test, struct oval_string_map *vardef, const char *definition_id) |
| 92 | { |
| 93 | struct oval_object *object = oval_test_get_object(test); |
| 94 | if (object != NULL) |
| 95 | _oval_object_fill_vardef(object, vardef, definition_id); |
| 96 | struct oval_state_iterator *ste_it = oval_test_get_states(test); |
| 97 | while (oval_state_iterator_has_more(ste_it)) { |
| 98 | struct oval_state *state = oval_state_iterator_next(ste_it); |
| 99 | if (state != NULL) |
| 100 | _oval_state_fill_vardef(state, vardef, definition_id); |
| 101 | } |
| 102 | oval_state_iterator_free(ste_it); |
| 103 | } |
| 104 | |
| 105 | void _oval_object_fill_vardef(struct oval_object *object, struct oval_string_map *vardef, const char *definition_id) |
| 106 | { |
| 107 | struct oval_object_content_iterator *content_it = oval_object_get_object_contents(object); |
| 108 | while (oval_object_content_iterator_has_more(content_it)) { |
| 109 | struct oval_object_content *content = oval_object_content_iterator_next(content_it); |
| 110 | switch (oval_object_content_get_type(content)) { |
| 111 | case OVAL_OBJECTCONTENT_ENTITY:{ |
| 112 | struct oval_entity *entity = oval_object_content_get_entity(content); |
| 113 | _oval_entity_fill_vardef(entity, vardef, definition_id); |
| 114 | } break; |
| 115 | case OVAL_OBJECTCONTENT_SET:{ |
| 116 | struct oval_setobject *set = oval_object_content_get_setobject(content); |
| 117 | _oval_setobject_fill_vardef(set, vardef, definition_id); |
| 118 | } break; |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | oval_object_content_iterator_free(content_it); |
| 124 | } |
| 125 | |
| 126 | void _oval_setobject_fill_vardef(struct oval_setobject *set, struct oval_string_map *vardef, const char *definition_id) |
| 127 | { |
| 128 | switch (oval_setobject_get_type(set)) { |
| 129 | case OVAL_SET_AGGREGATE:{ |
| 130 | struct oval_setobject_iterator *subset_it = oval_setobject_get_subsets(set); |
| 131 | while (oval_setobject_iterator_has_more(subset_it)) { |
| 132 | struct oval_setobject *subset = oval_setobject_iterator_next(subset_it); |
| 133 | _oval_setobject_fill_vardef(subset, vardef, definition_id); |
| 134 | } |
| 135 | oval_setobject_iterator_free(subset_it); |
| 136 | } break; |
| 137 | case OVAL_SET_COLLECTIVE:{ |
| 138 | struct oval_object_iterator *object_it = oval_setobject_get_objects(set); |
| 139 | while (oval_object_iterator_has_more(object_it)) { |
| 140 | struct oval_object *object = oval_object_iterator_next(object_it); |
| 141 | _oval_object_fill_vardef(object, vardef, definition_id); |
| 142 | } |
| 143 | oval_object_iterator_free(object_it); |
| 144 | |
| 145 | struct oval_filter_iterator *filter_it = oval_setobject_get_filters(set); |
| 146 | while (oval_filter_iterator_has_more(filter_it)) { |
| 147 | struct oval_filter *filter = oval_filter_iterator_next(filter_it); |
| 148 | struct oval_state *state = oval_filter_get_state(filter); |
| 149 | _oval_state_fill_vardef(state, vardef, definition_id); |
| 150 | } |
| 151 | oval_filter_iterator_free(filter_it); |
| 152 | } break; |
| 153 | default: |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void _oval_state_fill_vardef(struct oval_state *state, struct oval_string_map *vardef, const char *definition_id) |
| 159 | { |
| 160 | struct oval_state_content_iterator *content_it = oval_state_get_contents(state); |
| 161 | while (oval_state_content_iterator_has_more(content_it)) { |
| 162 | struct oval_state_content *content = oval_state_content_iterator_next(content_it); |
| 163 | struct oval_entity *entity = oval_state_content_get_entity(content); |
| 164 | if (entity != NULL) |
| 165 | _oval_entity_fill_vardef(entity, vardef, definition_id); |
| 166 | } |
| 167 | oval_state_content_iterator_free(content_it); |
| 168 | } |
| 169 | |
| 170 | void _oval_entity_fill_vardef(struct oval_entity *entity, struct oval_string_map *vardef, const char *definition_id) |
| 171 | { |
| 172 | if (oval_entity_get_varref_type(entity) == OVAL_ENTITY_VARREF_ATTRIBUTE || |
| 173 | oval_entity_get_varref_type(entity) == OVAL_ENTITY_VARREF_ELEMENT) { |
| 174 | struct oval_variable *variable = oval_entity_get_variable(entity); |
| 175 | if (variable != NULL) |
| 176 | _vardef_insert(vardef, definition_id, oval_variable_get_id(variable)); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void _vardef_insert(struct oval_string_map *vardef, const char *definition_id, const char *variable_id) |
| 181 | { |
| 182 | struct oval_string_map *def_list = (struct oval_string_map *) oval_string_map_get_value(vardef, variable_id); |
| 183 | if (def_list == NULL) { |
| 184 | def_list = oval_string_map_new(); |
| 185 | oval_string_map_put(vardef, variable_id, def_list); |
| 186 | } |
| 187 | oval_string_map_put(def_list, definition_id, (void *) ""); |
| 188 | } |