Debian Codegraph @

src/source/xslt.c

raw symbols SCIP
1 /*
2 * Copyright 2014 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
21 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
22 #include <config.h>cxx . . $ `<file>/build/config.h`/
23 #endif
24
25 #include <libxml/parser.h>
26 #include <libxslt/xslt.h>
27 #include <libxslt/xsltInternals.h>
28 #include <libxslt/transform.h>
29 #include <libxslt/xsltutils.h>
30 #include <libexslt/exslt.h>
31 #include <string.h>
32
33 #ifdef OS_WINDOWS
34 #include <io.h>
35 #else
36 #include <unistd.h>
37 #endif
38
39 #include "common/_error.h"cxx . . $ `<file>/src/common/_error.h`/
40 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
41 #include "oscap.h"cxx . . $ `<file>/src/common/public/oscap.h`/
42 #include "oscap_source.h"cxx . . $ `<file>/src/source/public/oscap_source.h`/
43 #include "source/oscap_source_priv.h"cxx . . $ `<file>/src/source/oscap_source_priv.h`/
44 #include "source/xslt_priv.h"cxx . . $ `<file>/src/source/xslt_priv.h`/
45 #include "oscap_helpers.h"cxx . . $ `<file>/src/common/public/oscap_helpers.h`/
46
47 #define XCCDF11_NScxx . . $ `src/source/xslt.c:47:9`! "http://checklists.nist.gov/xccdf/1.1"
48 #define XCCDF12_NScxx . . $ `src/source/xslt.c:48:9`! "http://checklists.nist.gov/xccdf/1.2"
49
50 /*
51 * Goes through the tree (DFS) and changes namespace of all XCCDF 1.1 elements
52 * to XCCDF 1.2 namespace URI. This ensures that the XCCDF works fine with
53 * XSLTs provided in openscap.
54 */
55 static int xccdf_ns_xslt_workaroundcxx . . $ xccdf_ns_xslt_workaround(ac91bcf1a67cbac6).(xmlDocPtrcxx . . $ xmlDocPtr# doc, xmlNodePtrcxx . . $ xmlNodePtr# node)
56 {
57 if (node == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
58 // nothing to do, this part of the document isn't XCCDF 1.1
59 return 0;
60 }
61
62 if (node->nscxx . . $ _xmlNode#ns. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && strcmpcxx . . $ strcmp(5ae29d726ae57f88).((const char*)node->nscxx . . $ _xmlNode#ns.->hrefcxx . . $ _xmlNs#href., XCCDF11_NScxx . . $ `src/source/xslt.c:47:9`!) == 0) {
63 xmlNsPtrcxx . . $ xmlNsPtr# xccdf12 = xmlNewNscxx . . $ xmlNewNs(f962c8aae91c9a10).(node,
64 BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! XCCDF12_NScxx . . $ `src/source/xslt.c:48:9`!,
65 BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! "cdf12");
66
67 xmlSetNscxx . . $ xmlSetNs(cd680be8abdf9ad3).(node, xccdf12);
68 }
69
70 xmlNodePtrcxx . . $ xmlNodePtr# child = node->childrencxx . . $ _xmlNode#children.;
71
72 for (; child != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; child = child->nextcxx . . $ _xmlNode#next.) {
73 if (child->typecxx . . $ _xmlNode#type. != XML_ELEMENT_NODEcxx . . $ XML_ELEMENT_NODE.)
74 continue;
75
76 const int res = xccdf_ns_xslt_workaroundcxx . . $ xccdf_ns_xslt_workaround(ac91bcf1a67cbac6).(doc, child);
77 if (res != 0)
78 return res;
79 }
80
81 return 0;
82 }
83
84 static inline int save_stylesheet_result_to_filecxx . . $ save_stylesheet_result_to_file(ae92c0ad3de7d149).(xmlDoccxx . . $ xmlDoc# *resulting_doc, xsltStylesheetcxx . . $ xsltStylesheet# *stylesheet, const char *outfile)
85 {
86 #ifdef OS_WINDOWS
87 int fd = _fileno(stdout);
88 #else
89 int fd = STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!;
90 #endif
91 if (outfile) {
92 fd = oscap_open_writablecxx . . $ oscap_open_writable(5c4306d8553b3696).(outfile);
93 }
94 if (fd == -1)
95 return -1;
96
97 int ret = xsltSaveResultToFdcxx . . $ xsltSaveResultToFd(f2dc8c27c56fa923).(fd, resulting_doc, stylesheet);
98 if (ret < 0) {
99 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not save result document");
100 }
101 #ifdef OS_WINDOWS
102 if (fd != _fileno(stdout))
103 #else
104 if (fd != STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!)
105 #endif
106 closecxx . . $ close(7864480464b09eea).(fd);
107 return ret;
108 }
109
110 static xmlDoccxx . . $ xmlDoc# *apply_xslt_path_internalcxx . . $ apply_xslt_path_internal(d6a3e7052d2da00).(struct oscap_sourcecxx . . $ oscap_source# *source, const char *xsltfile, const char **params, const char *path_to_xslt, xsltStylesheetcxx . . $ xsltStylesheet# **stylesheet)
111 {
112 xmlDoccxx . . $ xmlDoc# *doc = oscap_source_get_xmlDoccxx . . $ oscap_source_get_xmlDoc(51aac4e66711e6e6).(source);
113 if (doc == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || stylesheet == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || xsltfile == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
114 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
115 }
116
117 size_t argc = 0;
118 while(params[argc]) argc += 2;
119
120 // Should we change all XCCDF namespaces (versioned) to one?
121 // This is a workaround needed to make XSLTs work with multiple versions.
122 // (currently 1.1 and 1.2)
123 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ns_workaround = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
124
125 /* is it an absolute path? */
126 char *xsltpath;
127 if (strstrcxx . . $ strstr(a713fe7b9a57cf96).(xsltfile, "/") == xsltfile) {
128 xsltpath = oscap_strdupcxx . . $ oscap_strdup(a8b8e32347182212).(xsltfile);
129 if (accesscxx . . $ access(cb7559238bb26814).(xsltpath, R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!)) {
130 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "XSLT file '%s' not found when trying to transform '%s'",
131 xsltfile, oscap_source_readable_origin(source));
132 freecxx . . $ free(5d22bdacc48458e8).(xsltpath);
133 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
134 }
135 }
136 else {
137 xsltpath = oscap_sprintfcxx . . $ oscap_sprintf(a849077ff1b8d812).("%s%s%s", path_to_xslt, "/", xsltfile);
138 if (accesscxx . . $ access(cb7559238bb26814).(xsltpath, R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!)) {
139 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "XSLT file '%s' not found in path '%s' when trying to transform '%s'",
140 xsltfile, path_to_xslt, oscap_source_readable_origin(source));
141 freecxx . . $ free(5d22bdacc48458e8).(xsltpath);
142 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
143 }
144
145 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(xsltfile, "xccdf-report.xsl") == 0 ||
146 strcmpcxx . . $ strcmp(5ae29d726ae57f88).(xsltfile, "legacy-fix.xsl") == 0 ||
147 strcmpcxx . . $ strcmp(5ae29d726ae57f88).(xsltfile, "xccdf-guide.xsl") == 0)
148 ns_workaround = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
149 }
150
151 *stylesheet = xsltParseStylesheetFilecxx . . $ xsltParseStylesheetFile(f30bb31683aa0e11).(BAD_CASTcxx . . $ `<external>/11592416788776295748/xmlstring.h:34:9`! xsltpath);
152 if (*stylesheet == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
153 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not parse XSLT file '%s'", xsltpath);
154 freecxx . . $ free(5d22bdacc48458e8).(xsltpath);
155 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
156 }
157
158 if (ns_workaround) {
159 if (xccdf_ns_xslt_workaroundcxx . . $ xccdf_ns_xslt_workaround(ac91bcf1a67cbac6).(doc, xmlDocGetRootElementcxx . . $ xmlDocGetRootElement(53aa9a676bd496f2).(doc)) != 0) {
160 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Had problems employing XCCDF XSLT namespace workaround for XML document '%s'",
161 oscap_source_readable_origin(source));
162 freecxx . . $ free(5d22bdacc48458e8).(xsltpath);
163 xsltFreeStylesheetcxx . . $ xsltFreeStylesheet(a1c2d48010b0ec33).(*stylesheet);
164 *stylesheet = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
165 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
166 }
167 }
168
169 char **args = calloccxx . . $ calloc(63a7bf2941bfcd7a).(argc + 1, sizeof(char *));
170
171 for (size_t i = 0; i < argc; i += 2) {
172 args[i] = (char*) params[i];
173 if (params[i+1]) args[i+1] = oscap_sprintfcxx . . $ oscap_sprintf(a849077ff1b8d812).("'%s'", params[i+1]);
174 }
175
176 xmlDoccxx . . $ xmlDoc# *transformed = xsltApplyStylesheetcxx . . $ xsltApplyStylesheet(c492d5bb45bcb5f1).(*stylesheet, doc, (const char **) args);
177 for (size_t i = 0; args[i]; i += 2) {
178 freecxx . . $ free(5d22bdacc48458e8).(args[i+1]);
179 }
180 freecxx . . $ free(5d22bdacc48458e8).(args);
181 if (transformed == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
182 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_OSCAPcxx . . $ `src/common/public/oscap_error.h:60:9`!, "Could not apply XSLT %s to XML file: %s", xsltpath,
183 oscap_source_readable_origin(source));
184 freecxx . . $ free(5d22bdacc48458e8).(xsltpath);
185 xsltFreeStylesheetcxx . . $ xsltFreeStylesheet(a1c2d48010b0ec33).(*stylesheet);
186 *stylesheet = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
187 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
188 }
189 freecxx . . $ free(5d22bdacc48458e8).(xsltpath);
190 return transformed;
191
192 }
193
194 int oscap_source_apply_xslt_pathcxx . . $ oscap_source_apply_xslt_path(55db94cfa4db2719).(struct oscap_sourcecxx . . $ oscap_source# *source, const char *xsltfile, const char *outfile, const char **params, const char *path_to_xslt)
195 {
196 xsltStylesheetcxx . . $ xsltStylesheet# *stylesheet = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
197 xmlDocPtrcxx . . $ xmlDocPtr# transformed = apply_xslt_path_internalcxx . . $ apply_xslt_path_internal(d6a3e7052d2da00).(source, xsltfile, params, path_to_xslt, &stylesheet);
198 if (transformed == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
199 return -1;
200 }
201 int ret = save_stylesheet_result_to_filecxx . . $ save_stylesheet_result_to_file(ae92c0ad3de7d149).(transformed, stylesheet, outfile);
202 xsltFreeStylesheetcxx . . $ xsltFreeStylesheet(a1c2d48010b0ec33).(stylesheet);
203 xmlFreeDoccxx . . $ xmlFreeDoc(42de24d0ab3f771).(transformed);
204 return ret;
205 }
206
207 char *oscap_source_apply_xslt_path_memcxx . . $ oscap_source_apply_xslt_path_mem(bfc89f1e7ad1e37f).(struct oscap_sourcecxx . . $ oscap_source# *source, const char *xsltfile, const char **params, const char *path_to_xslt)
208 {
209 xsltStylesheetcxx . . $ xsltStylesheet# *stylesheet = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
210 xmlDocPtrcxx . . $ xmlDocPtr# transformed = apply_xslt_path_internalcxx . . $ apply_xslt_path_internal(d6a3e7052d2da00).(source, xsltfile, params, path_to_xslt, &stylesheet);
211 if (transformed == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
212 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
213 }
214 xmlCharcxx . . $ xmlChar# *result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
215 int len;
216 if (xsltSaveResultToStringcxx . . $ xsltSaveResultToString(f0d8815278a3396).(&result, &len, transformed, stylesheet) != 0) {
217 oscap_seterrcxx . . $ `src/common/_error.h:52:9`!(OSCAP_EFAMILY_XMLcxx . . $ `src/common/public/oscap_error.h:59:9`!, "Could not save transformend content to buffer, after applying XSLT %s",
218 xsltfile);
219 freecxx . . $ free(5d22bdacc48458e8).(result);
220 result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
221 }
222 return (char *)result;
223 }