Debian Codegraph @

src/XCCDF_POLICY/xccdf_policy_substitute.c

raw symbols SCIP
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_Hcxx . . $ `<built-in>:409:9`!
21 #include <config.h>cxx . . $ `<file>/build/config.h`/
22 #endif
23
24 #include <string.h>
25 #include <libxml/tree.h>
26
27 #include "util.h"cxx . . $ `<file>/src/common/util.h`/
28 #include "xml_iterate.h"cxx . . $ `<file>/src/common/xml_iterate.h`/
29 #include "debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
30 #include "_error.h"cxx . . $ `<file>/src/common/_error.h`/
31 #include "XCCDF/elements.h"cxx . . $ `<file>/src/XCCDF/elements.h`/
32 #include "XCCDF/xccdf_impl.h"cxx . . $ `<file>/src/XCCDF/xccdf_impl.h`/
33 #include "xccdf_policy_priv.h"cxx . . $ `<file>/src/XCCDF_POLICY/xccdf_policy_priv.h`/
34 #include "public/xccdf_policy.h"cxx . . $ `<file>/src/XCCDF_POLICY/public/xccdf_policy.h`/
35
36 struct _xccdf_text_substitution_datacxx . . $ _xccdf_text_substitution_data# {
37 struct xccdf_policycxx . . $ xccdf_policy# *policycxx . . $ _xccdf_text_substitution_data#policy.;
38 struct xccdf_rule_resultcxx . . $ xccdf_rule_result# *rule_resultcxx . . $ _xccdf_text_substitution_data#rule_result.;
39 enumcxx . . $ _xccdf_text_substitution_data#$anonymous_type_0# {
40 _TAILORING_TYPEcxx . . $ _xccdf_text_substitution_data#_TAILORING_TYPE. = 1,
41 _DOCUMENT_GENERATION_TYPEcxx . . $ _xccdf_text_substitution_data#_DOCUMENT_GENERATION_TYPE. = 2,
42 _ASSESSMENT_TYPEcxx . . $ _xccdf_text_substitution_data#_ASSESSMENT_TYPE. = 4
43 } processing_typecxx . . $ _xccdf_text_substitution_data#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 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! _xhtml_is_supported_namespacecxx . . $ _xhtml_is_supported_namespace(8796ad3e3951bb1).(xmlNscxx . . $ xmlNs# *ns)
49 {
50 return ns != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).((const char *) ns->hrefcxx . . $ _xmlNs#href., (const char *) XCCDF_XHTML_NAMESPACEcxx . . $ `src/XCCDF/xccdf_impl.h:35:9`!);
51 }
52
53 static int _xccdf_text_substitution_cbcxx . . $ _xccdf_text_substitution_cb(69cca8c36ed9acae).(xmlNodecxx . . $ xmlNode# **node, void *user_data)
54 {
55 struct _xccdf_text_substitution_datacxx . . $ _xccdf_text_substitution_data# *data = (struct _xccdf_text_substitution_datacxx . . $ _xccdf_text_substitution_data# *) user_data;
56 if (node == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || *node == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
57 return 1;
58
59 if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).((const char *) (*node)->namecxx . . $ _xmlNode#name., "sub") && xccdf_is_supported_namespacecxx . . $ xccdf_is_supported_namespace(8796ad3e3951bb1).((*node)->nscxx . . $ _xmlNode#ns.)) {
60 if ((*node)->childrencxx . . $ _xmlNode#children. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
61 dWcxx . . $ `src/common/debug_priv.h:62:9`!("The xccdf:sub element SHALL NOT have any content.");
62 char *sub_idref = (char *) xmlGetPropcxx . . $ xmlGetProp(9828664f081b7881).(*node, BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! "idref");
63 if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(sub_idref, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) {
64 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_XCCDFcxx . . $ `src/common/public/oscap_error.h:62:9`!, "The xccdf:sub MUST have a single @idref attribute.");
65 freecxx . . $ free(5d22bdacc48458e8).(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_benchmarkcxx . . $ xccdf_benchmark# *benchmark = xccdf_policy_get_benchmarkcxx . . $ xccdf_policy_get_benchmark(2b37a6455229dc70).(data->policycxx . . $ _xccdf_text_substitution_data#policy.);
71 if (benchmark == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
72 return 1;
73 struct xccdf_itemcxx . . $ xccdf_item# *value = xccdf_benchmark_get_itemcxx . . $ xccdf_benchmark_get_item(daeed5940f8c8cf2).(benchmark, sub_idref);
74
75 const char *result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
76 if (value != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && xccdf_item_get_typecxx . . $ xccdf_item_get_type(3d8d48c673cd637b).(value) == XCCDF_VALUEcxx . . $ 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 *) xmlGetPropcxx . . $ xmlGetProp(9828664f081b7881).(*node, BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! "use");
80 if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(sub_use, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) || oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(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 freecxx . . $ free(5d22bdacc48458e8).(sub_use);
86 sub_use = oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).((data->processing_typecxx . . $ _xccdf_text_substitution_data#processing_type. & _TAILORING_TYPEcxx . . $ _xccdf_text_substitution_data#_TAILORING_TYPE.) ? "title" : "value");
87 }
88
89 if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(sub_use, "title")) {
90 // TODO: @xml:lang
91 struct oscap_text_iteratorcxx . . $ oscap_text_iterator# *title_it = xccdf_item_get_titlecxx . . $ xccdf_item_get_title(6f56deec8ca52984).(value);
92 if (oscap_text_iterator_has_morecxx . . $ oscap_text_iterator_has_more(7616e6c9867966c1).(title_it))
93 result = oscap_text_get_textcxx . . $ oscap_text_get_text(ba0993cb6b641422).(oscap_text_iterator_nextcxx . . $ oscap_text_iterator_next(fef5008d25b58ff).(title_it));
94 oscap_text_iterator_freecxx . . $ oscap_text_iterator_free(f60c6cfdbf1f29d7).(title_it);
95 } else {
96 if (!oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).(sub_use, "value"))
97 dWcxx . . $ `src/common/debug_priv.h:62:9`!("xccdf:sub/@idref='%s' has incorrect @use='%s'! Using @use='value' instead.", sub_idref, sub_use);
98 result = xccdf_policy_get_value_of_itemcxx . . $ xccdf_policy_get_value_of_item(a5cb590029f1e3ee).(data->policycxx . . $ _xccdf_text_substitution_data#policy., value);
99 }
100 freecxx . . $ free(5d22bdacc48458e8).(sub_use);
101 } else { // This xccdf:sub probably refers to the xccdf:plain-text
102 result = xccdf_benchmark_get_plain_textcxx . . $ xccdf_benchmark_get_plain_text(77f05a18470c256b).(benchmark, sub_idref);
103 }
104
105 if (result == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
106 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_XCCDFcxx . . $ `src/common/public/oscap_error.h:62:9`!, "Could not resolve xccdf:sub/@idref='%s'!", sub_idref);
107 freecxx . . $ free(5d22bdacc48458e8).(sub_idref);
108 return 2;
109 }
110 freecxx . . $ free(5d22bdacc48458e8).(sub_idref);
111
112 xmlNodecxx . . $ xmlNode# *new_node = xmlNewTextcxx . . $ xmlNewText(2586a09a1bfbb25b).(BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! result);
113 xmlReplaceNodecxx . . $ xmlReplaceNode(93e65f4d4ebd68bd).(*node, new_node);
114 xmlFreeNodecxx . . $ xmlFreeNode(5712c9d4a25253).(*node);
115 *node = new_node;
116 return 0;
117 } else if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).((const char *) (*node)->namecxx . . $ _xmlNode#name., "object") && _xhtml_is_supported_namespacecxx . . $ _xhtml_is_supported_namespace(8796ad3e3951bb1).((*node)->nscxx . . $ _xmlNode#ns.)) {
118 char *object_data = (char *) xmlGetPropcxx . . $ xmlGetProp(9828664f081b7881).(*node, BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! "data");
119 if (object_data == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || strncmpcxx . . $ strncmp(fd278af2f295fa2e).(object_data, "#xccdf:", strlencxx . . $ strlen(751346f8406fd082).("#xccdf:")) != 0) {
120 freecxx . . $ free(5d22bdacc48458e8).(object_data);
121 return 0; // Not an error, unless it shall be resolved by XCCDF
122 }
123
124 struct xccdf_benchmarkcxx . . $ xccdf_benchmark# *benchmark = xccdf_policy_get_benchmarkcxx . . $ xccdf_policy_get_benchmark(2b37a6455229dc70).(data->policycxx . . $ _xccdf_text_substitution_data#policy.);
125 if (benchmark == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
126 return 1;
127
128 const char *result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
129 if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(object_data, "#xccdf:value:", strlencxx . . $ strlen(751346f8406fd082).("#xccdf:value:")) == 0) {
130 const char *value_id = object_data + strlencxx . . $ strlen(751346f8406fd082).("#xccdf:value:");
131
132 struct xccdf_itemcxx . . $ xccdf_item# *item = xccdf_benchmark_get_itemcxx . . $ xccdf_benchmark_get_item(daeed5940f8c8cf2).(benchmark, value_id);
133 if (item != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && xccdf_item_get_typecxx . . $ xccdf_item_get_type(3d8d48c673cd637b).(item) == XCCDF_VALUEcxx . . $ XCCDF_VALUE.) {
134 result = xccdf_policy_get_value_of_itemcxx . . $ xccdf_policy_get_value_of_item(a5cb590029f1e3ee).(data->policycxx . . $ _xccdf_text_substitution_data#policy., item);
135 } else {
136 result = xccdf_benchmark_get_plain_textcxx . . $ xccdf_benchmark_get_plain_text(77f05a18470c256b).(benchmark, value_id);
137 if (result == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
138 dWcxx . . $ `src/common/debug_priv.h:62:9`!("Text substitution for xccdf:fact is not supported!"); // TODO.
139 }
140 }
141 }
142 else if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(object_data, "#xccdf:title:", strlencxx . . $ strlen(751346f8406fd082).("#xccdf:title:")) == 0) {
143 const char *title_id = object_data + strlencxx . . $ strlen(751346f8406fd082).("#xccdf:title:");
144
145 struct xccdf_itemcxx . . $ xccdf_item# *item = xccdf_benchmark_get_itemcxx . . $ xccdf_benchmark_get_item(daeed5940f8c8cf2).(benchmark, title_id);
146 if (item != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
147 // TODO: @xml:lang
148 struct oscap_text_iteratorcxx . . $ oscap_text_iterator# *title_it = xccdf_item_get_titlecxx . . $ xccdf_item_get_title(6f56deec8ca52984).(item);
149 if (oscap_text_iterator_has_morecxx . . $ oscap_text_iterator_has_more(7616e6c9867966c1).(title_it))
150 result = oscap_text_get_textcxx . . $ oscap_text_get_text(ba0993cb6b641422).(oscap_text_iterator_nextcxx . . $ oscap_text_iterator_next(fef5008d25b58ff).(title_it));
151 oscap_text_iterator_freecxx . . $ oscap_text_iterator_free(f60c6cfdbf1f29d7).(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 dWcxx . . $ `src/common/debug_priv.h:62:9`!("Unsupported XCCDF uri: xhtml:object/@data='%s'", object_data);
158 freecxx . . $ free(5d22bdacc48458e8).(object_data);
159 return 0;
160 }
161 freecxx . . $ free(5d22bdacc48458e8).(object_data);
162 xmlNodecxx . . $ xmlNode# *new_node = xmlNewTextcxx . . $ xmlNewText(2586a09a1bfbb25b).(BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! result);
163 xmlReplaceNodecxx . . $ xmlReplaceNode(93e65f4d4ebd68bd).(*node, new_node);
164 xmlFreeNodecxx . . $ xmlFreeNode(5712c9d4a25253).(*node);
165 *node = new_node;
166 return 0;
167 } else if (oscap_streqcxx . . $ oscap_streq(b7a7aa4a824b7595).((const char *) (*node)->namecxx . . $ _xmlNode#name., "instance") && xccdf_is_supported_namespacecxx . . $ xccdf_is_supported_namespace(8796ad3e3951bb1).((*node)->nscxx . . $ _xmlNode#ns.)) {
168 const char *result;
169 // <instance> elements
170 if ((*node)->childrencxx . . $ _xmlNode#children. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
171 dWcxx . . $ `src/common/debug_priv.h:62:9`!("The xccdf:instance element SHALL NOT have any content.");
172 if (data->rule_resultcxx . . $ _xccdf_text_substitution_data#rule_result. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
173 return 1;
174 struct xccdf_instance_iteratorcxx . . $ xccdf_instance_iterator# *instances = xccdf_rule_result_get_instancescxx . . $ xccdf_rule_result_get_instances(f5a515bc55a2543d).(data->rule_resultcxx . . $ _xccdf_text_substitution_data#rule_result.);
175 if (xccdf_instance_iterator_has_morecxx . . $ xccdf_instance_iterator_has_more(372a6fb4bd032f6f).(instances)) {
176 struct xccdf_instancecxx . . $ xccdf_instance# *instance = xccdf_instance_iterator_nextcxx . . $ xccdf_instance_iterator_next(4821a53661e5cc19).(instances);
177 result = xccdf_instance_get_contentcxx . . $ xccdf_instance_get_content(875f5045a6eeee78).(instance);
178 xccdf_instance_iterator_freecxx . . $ xccdf_instance_iterator_free(fbce03e4b7bd5fec).(instances);
179 }
180 else {
181 xccdf_instance_iterator_freecxx . . $ xccdf_instance_iterator_free(fbce03e4b7bd5fec).(instances);
182 dWcxx . . $ `src/common/debug_priv.h:62:9`!("The xccdf:rule-result/xccdf:instance element was not found.");
183 return 1;
184 }
185 xmlNodecxx . . $ xmlNode# *new_node = xmlNewTextcxx . . $ xmlNewText(2586a09a1bfbb25b).(BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! result);
186 xmlReplaceNodecxx . . $ xmlReplaceNode(93e65f4d4ebd68bd).(*node, new_node);
187 xmlFreeNodecxx . . $ xmlFreeNode(5712c9d4a25253).(*node);
188 *node = new_node;
189 return 0;
190 } else {
191 return 0;
192 }
193 }
194
195 int xccdf_policy_resolve_fix_substitutioncxx . . $ xccdf_policy_resolve_fix_substitution(a1fa875c053d61fd).(struct xccdf_policycxx . . $ xccdf_policy# *policy, struct xccdf_fixcxx . . $ xccdf_fix# *fix, struct xccdf_rule_resultcxx . . $ xccdf_rule_result# *rule_result, struct xccdf_resultcxx . . $ xccdf_result# *test_result)
196 {
197 struct _xccdf_text_substitution_datacxx . . $ _xccdf_text_substitution_data# data;
198 data.policycxx . . $ _xccdf_text_substitution_data#policy. = policy;
199 data.processing_typecxx . . $ _xccdf_text_substitution_data#processing_type. = _DOCUMENT_GENERATION_TYPEcxx . . $ _xccdf_text_substitution_data#_DOCUMENT_GENERATION_TYPE. | _ASSESSMENT_TYPEcxx . . $ _xccdf_text_substitution_data#_ASSESSMENT_TYPE.;
200 data.rule_resultcxx . . $ _xccdf_text_substitution_data#rule_result. = rule_result;
201
202 char *result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
203 int res = xml_iterate_dfscxx . . $ xml_iterate_dfs(63334c2cd85278c8).(xccdf_fix_get_contentcxx . . $ xccdf_fix_get_content(d4a571f4d5e46502).(fix), &result, _xccdf_text_substitution_cbcxx . . $ _xccdf_text_substitution_cb(69cca8c36ed9acae)., &data);
204 if (res == 0)
205 xccdf_fix_set_contentcxx . . $ xccdf_fix_set_content(fe9b5cf30d42b431).(fix, result);
206 freecxx . . $ free(5d22bdacc48458e8).(result);
207 return res;
208 }
209
210 char* xccdf_policy_substitutecxx . . $ xccdf_policy_substitute(9b374264f47ad06a).(const char *text, struct xccdf_policycxx . . $ xccdf_policy# *policy) {
211 struct _xccdf_text_substitution_datacxx . . $ _xccdf_text_substitution_data# data;
212 data.policycxx . . $ _xccdf_text_substitution_data#policy. = policy;
213 data.rule_resultcxx . . $ _xccdf_text_substitution_data#rule_result. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
214 /* We cannot anticipate processing type. But <title>'s are least probable. */
215 data.processing_typecxx . . $ _xccdf_text_substitution_data#processing_type. = _DOCUMENT_GENERATION_TYPEcxx . . $ _xccdf_text_substitution_data#_DOCUMENT_GENERATION_TYPE. | _ASSESSMENT_TYPEcxx . . $ _xccdf_text_substitution_data#_ASSESSMENT_TYPE.;
216
217 char *resolved_text = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
218 if (xml_iterate_dfscxx . . $ xml_iterate_dfs(63334c2cd85278c8).(text, &resolved_text, _xccdf_text_substitution_cbcxx . . $ _xccdf_text_substitution_cb(69cca8c36ed9acae)., &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 freecxx . . $ free(5d22bdacc48458e8).(resolved_text);
222 resolved_text = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
223 }
224 return resolved_text;
225 }