| 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 | */ |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
| 24 | #include <string.h> |
| 25 | #include <libxml/tree.h> |
| 26 | |
| 27 | #include "util.h" |
| 28 | #include "xml_iterate.h" |
| 29 | #include "debug_priv.h" |
| 30 | #include "_error.h" |
| 31 | #include "XCCDF/elements.h" |
| 32 | #include "XCCDF/xccdf_impl.h" |
| 33 | #include "xccdf_policy_priv.h" |
| 34 | #include "public/xccdf_policy.h" |
| 35 | |
| 36 | struct _xccdf_text_substitution_data { |
| 37 | struct xccdf_policy *policy; |
| 38 | struct xccdf_rule_result *rule_result; |
| 39 | enum { |
| 40 | _TAILORING_TYPE = 1, |
| 41 | _DOCUMENT_GENERATION_TYPE = 2, |
| 42 | _ASSESSMENT_TYPE = 4 |
| 43 | } processing_type; // Defines behaviour for fix/@use="legacy" |
| 44 | // TODO: this shall carry also the @xml:lang. |
| 45 | // TODO: This shall carry also xccdf:TestResult for xccdf:fact resolution |
| 46 | }; |
| 47 | |
| 48 | static bool _xhtml_is_supported_namespace(xmlNs *ns) |
| 49 | { |
| 50 | return ns != NULL && oscap_streq((const char *) ns->href, (const char *) XCCDF_XHTML_NAMESPACE); |
| 51 | } |
| 52 | |
| 53 | static int _xccdf_text_substitution_cb(xmlNode **node, void *user_data) |
| 54 | { |
| 55 | struct _xccdf_text_substitution_data *data = (struct _xccdf_text_substitution_data *) user_data; |
| 56 | if (node == NULL || *node == NULL || data == NULL) |
| 57 | return 1; |
| 58 | |
| 59 | if (oscap_streq((const char *) (*node)->name, "sub") && xccdf_is_supported_namespace((*node)->ns)) { |
| 60 | if ((*node)->children != NULL) |
| 61 | dW("The xccdf:sub element SHALL NOT have any content."); |
| 62 | char *sub_idref = (char *) xmlGetProp(*node, BAD_CAST "idref"); |
| 63 | if (oscap_streq(sub_idref, NULL)) { |
| 64 | oscap_seterr(OSCAP_EFAMILY_XCCDF, "The xccdf:sub MUST have a single @idref attribute."); |
| 65 | free(sub_idref); // It may be an empty string. |
| 66 | return 2; |
| 67 | } |
| 68 | // Sub element may refer to xccdf:Value or to xccdf:plain-text |
| 69 | |
| 70 | struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(data->policy); |
| 71 | if (benchmark == NULL) |
| 72 | return 1; |
| 73 | struct xccdf_item *value = xccdf_benchmark_get_item(benchmark, sub_idref); |
| 74 | |
| 75 | const char *result = NULL; |
| 76 | if (value != NULL && xccdf_item_get_type(value) == XCCDF_VALUE) { |
| 77 | // When the <xccdf:sub> element's @idref attribute holds the id of an <xccdf:Value> |
| 78 | // element, the <xccdf:sub> element's @use attribute MUST be consulted. |
| 79 | char *sub_use = (char *) xmlGetProp(*node, BAD_CAST "use"); |
| 80 | if (oscap_streq(sub_use, NULL) || oscap_streq(sub_use, "legacy")) { |
| 81 | // If the value of the @use attribute is "legacy", then during Tailoring, |
| 82 | // process the <xccdf:sub> element as if @use was set to "title". but |
| 83 | // during Document Generation or Assessment, process the <xccdf:sub> |
| 84 | // element as if @use was set to "value". |
| 85 | free(sub_use); |
| 86 | sub_use = oscap_strdup((data->processing_type & _TAILORING_TYPE) ? "title" : "value"); |
| 87 | } |
| 88 | |
| 89 | if (oscap_streq(sub_use, "title")) { |
| 90 | // TODO: @xml:lang |
| 91 | struct oscap_text_iterator *title_it = xccdf_item_get_title(value); |
| 92 | if (oscap_text_iterator_has_more(title_it)) |
| 93 | result = oscap_text_get_text(oscap_text_iterator_next(title_it)); |
| 94 | oscap_text_iterator_free(title_it); |
| 95 | } else { |
| 96 | if (!oscap_streq(sub_use, "value")) |
| 97 | dW("xccdf:sub/@idref='%s' has incorrect @use='%s'! Using @use='value' instead.", sub_idref, sub_use); |
| 98 | result = xccdf_policy_get_value_of_item(data->policy, value); |
| 99 | } |
| 100 | free(sub_use); |
| 101 | } else { // This xccdf:sub probably refers to the xccdf:plain-text |
| 102 | result = xccdf_benchmark_get_plain_text(benchmark, sub_idref); |
| 103 | } |
| 104 | |
| 105 | if (result == NULL) { |
| 106 | oscap_seterr(OSCAP_EFAMILY_XCCDF, "Could not resolve xccdf:sub/@idref='%s'!", sub_idref); |
| 107 | free(sub_idref); |
| 108 | return 2; |
| 109 | } |
| 110 | free(sub_idref); |
| 111 | |
| 112 | xmlNode *new_node = xmlNewText(BAD_CAST result); |
| 113 | xmlReplaceNode(*node, new_node); |
| 114 | xmlFreeNode(*node); |
| 115 | *node = new_node; |
| 116 | return 0; |
| 117 | } else if (oscap_streq((const char *) (*node)->name, "object") && _xhtml_is_supported_namespace((*node)->ns)) { |
| 118 | char *object_data = (char *) xmlGetProp(*node, BAD_CAST "data"); |
| 119 | if (object_data == NULL || strncmp(object_data, "#xccdf:", strlen("#xccdf:")) != 0) { |
| 120 | free(object_data); |
| 121 | return 0; // Not an error, unless it shall be resolved by XCCDF |
| 122 | } |
| 123 | |
| 124 | struct xccdf_benchmark *benchmark = xccdf_policy_get_benchmark(data->policy); |
| 125 | if (benchmark == NULL) |
| 126 | return 1; |
| 127 | |
| 128 | const char *result = NULL; |
| 129 | if (strncmp(object_data, "#xccdf:value:", strlen("#xccdf:value:")) == 0) { |
| 130 | const char *value_id = object_data + strlen("#xccdf:value:"); |
| 131 | |
| 132 | struct xccdf_item *item = xccdf_benchmark_get_item(benchmark, value_id); |
| 133 | if (item != NULL && xccdf_item_get_type(item) == XCCDF_VALUE) { |
| 134 | result = xccdf_policy_get_value_of_item(data->policy, item); |
| 135 | } else { |
| 136 | result = xccdf_benchmark_get_plain_text(benchmark, value_id); |
| 137 | if (result == NULL) { |
| 138 | dW("Text substitution for xccdf:fact is not supported!"); // TODO. |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | else if (strncmp(object_data, "#xccdf:title:", strlen("#xccdf:title:")) == 0) { |
| 143 | const char *title_id = object_data + strlen("#xccdf:title:"); |
| 144 | |
| 145 | struct xccdf_item *item = xccdf_benchmark_get_item(benchmark, title_id); |
| 146 | if (item != NULL) { |
| 147 | // TODO: @xml:lang |
| 148 | struct oscap_text_iterator *title_it = xccdf_item_get_title(item); |
| 149 | if (oscap_text_iterator_has_more(title_it)) |
| 150 | result = oscap_text_get_text(oscap_text_iterator_next(title_it)); |
| 151 | oscap_text_iterator_free(title_it); |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | // Let's not consider this as an error. Since in similar cases NISTIR-7275r4 |
| 156 | // suggests to retain the <object> element. |
| 157 | dW("Unsupported XCCDF uri: xhtml:object/@data='%s'", object_data); |
| 158 | free(object_data); |
| 159 | return 0; |
| 160 | } |
| 161 | free(object_data); |
| 162 | xmlNode *new_node = xmlNewText(BAD_CAST result); |
| 163 | xmlReplaceNode(*node, new_node); |
| 164 | xmlFreeNode(*node); |
| 165 | *node = new_node; |
| 166 | return 0; |
| 167 | } else if (oscap_streq((const char *) (*node)->name, "instance") && xccdf_is_supported_namespace((*node)->ns)) { |
| 168 | const char *result; |
| 169 | // <instance> elements |
| 170 | if ((*node)->children != NULL) |
| 171 | dW("The xccdf:instance element SHALL NOT have any content."); |
| 172 | if (data->rule_result == NULL) |
| 173 | return 1; |
| 174 | struct xccdf_instance_iterator *instances = xccdf_rule_result_get_instances(data->rule_result); |
| 175 | if (xccdf_instance_iterator_has_more(instances)) { |
| 176 | struct xccdf_instance *instance = xccdf_instance_iterator_next(instances); |
| 177 | result = xccdf_instance_get_content(instance); |
| 178 | xccdf_instance_iterator_free(instances); |
| 179 | } |
| 180 | else { |
| 181 | xccdf_instance_iterator_free(instances); |
| 182 | dW("The xccdf:rule-result/xccdf:instance element was not found."); |
| 183 | return 1; |
| 184 | } |
| 185 | xmlNode *new_node = xmlNewText(BAD_CAST result); |
| 186 | xmlReplaceNode(*node, new_node); |
| 187 | xmlFreeNode(*node); |
| 188 | *node = new_node; |
| 189 | return 0; |
| 190 | } else { |
| 191 | return 0; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | int xccdf_policy_resolve_fix_substitution(struct xccdf_policy *policy, struct xccdf_fix *fix, struct xccdf_rule_result *rule_result, struct xccdf_result *test_result) |
| 196 | { |
| 197 | struct _xccdf_text_substitution_data data; |
| 198 | data.policy = policy; |
| 199 | data.processing_type = _DOCUMENT_GENERATION_TYPE | _ASSESSMENT_TYPE; |
| 200 | data.rule_result = rule_result; |
| 201 | |
| 202 | char *result = NULL; |
| 203 | int res = xml_iterate_dfs(xccdf_fix_get_content(fix), &result, _xccdf_text_substitution_cb, &data); |
| 204 | if (res == 0) |
| 205 | xccdf_fix_set_content(fix, result); |
| 206 | free(result); |
| 207 | return res; |
| 208 | } |
| 209 | |
| 210 | char* xccdf_policy_substitute(const char *text, struct xccdf_policy *policy) { |
| 211 | struct _xccdf_text_substitution_data data; |
| 212 | data.policy = policy; |
| 213 | data.rule_result = NULL; |
| 214 | /* We cannot anticipate processing type. But <title>'s are least probable. */ |
| 215 | data.processing_type = _DOCUMENT_GENERATION_TYPE | _ASSESSMENT_TYPE; |
| 216 | |
| 217 | char *resolved_text = NULL; |
| 218 | if (xml_iterate_dfs(text, &resolved_text, _xccdf_text_substitution_cb, &data) != 0) { |
| 219 | // Either warning or error occurred. Since prototype of this function |
| 220 | // does not make possible warning notification -> We better scratch that. |
| 221 | free(resolved_text); |
| 222 | resolved_text = NULL; |
| 223 | } |
| 224 | return resolved_text; |
| 225 | } |