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_H
22 #include <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"
40 #include "common/util.h"
41 #include "oscap.h"
42 #include "oscap_source.h"
43 #include "source/oscap_source_priv.h"
44 #include "source/xslt_priv.h"
45 #include "oscap_helpers.h"
46
47 #define XCCDF11_NS "http://checklists.nist.gov/xccdf/1.1"
48 #define XCCDF12_NS "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_workaround(xmlDocPtr doc, xmlNodePtr node)
56 {
57 if (node == NULL) {
58 // nothing to do, this part of the document isn't XCCDF 1.1
59 return 0;
60 }
61
62 if (node->ns != NULL && strcmp((const char*)node->ns->href, XCCDF11_NS) == 0) {
63 xmlNsPtr xccdf12 = xmlNewNs(node,
64 BAD_CAST XCCDF12_NS,
65 BAD_CAST "cdf12");
66
67 xmlSetNs(node, xccdf12);
68 }
69
70 xmlNodePtr child = node->children;
71
72 for (; child != NULL; child = child->next) {
73 if (child->type != XML_ELEMENT_NODE)
74 continue;
75
76 const int res = xccdf_ns_xslt_workaround(doc, child);
77 if (res != 0)
78 return res;
79 }
80
81 return 0;
82 }
83
84 static inline int save_stylesheet_result_to_file(xmlDoc *resulting_doc, xsltStylesheet *stylesheet, const char *outfile)
85 {
86 #ifdef OS_WINDOWS
87 int fd = _fileno(stdout);
88 #else
89 int fd = STDOUT_FILENO;
90 #endif
91 if (outfile) {
92 fd = oscap_open_writable(outfile);
93 }
94 if (fd == -1)
95 return -1;
96
97 int ret = xsltSaveResultToFd(fd, resulting_doc, stylesheet);
98 if (ret < 0) {
99 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not save result document");
100 }
101 #ifdef OS_WINDOWS
102 if (fd != _fileno(stdout))
103 #else
104 if (fd != STDOUT_FILENO)
105 #endif
106 close(fd);
107 return ret;
108 }
109
110 static xmlDoc *apply_xslt_path_internal(struct oscap_source *source, const char *xsltfile, const char **params, const char *path_to_xslt, xsltStylesheet **stylesheet)
111 {
112 xmlDoc *doc = oscap_source_get_xmlDoc(source);
113 if (doc == NULL || stylesheet == NULL || xsltfile == NULL) {
114 return NULL;
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 bool ns_workaround = false;
124
125 /* is it an absolute path? */
126 char *xsltpath;
127 if (strstr(xsltfile, "/") == xsltfile) {
128 xsltpath = oscap_strdup(xsltfile);
129 if (access(xsltpath, R_OK)) {
130 oscap_seterr(OSCAP_EFAMILY_OSCAP, "XSLT file '%s' not found when trying to transform '%s'",
131 xsltfile, oscap_source_readable_origin(source));
132 free(xsltpath);
133 return NULL;
134 }
135 }
136 else {
137 xsltpath = oscap_sprintf("%s%s%s", path_to_xslt, "/", xsltfile);
138 if (access(xsltpath, R_OK)) {
139 oscap_seterr(OSCAP_EFAMILY_OSCAP, "XSLT file '%s' not found in path '%s' when trying to transform '%s'",
140 xsltfile, path_to_xslt, oscap_source_readable_origin(source));
141 free(xsltpath);
142 return NULL;
143 }
144
145 if (strcmp(xsltfile, "xccdf-report.xsl") == 0 ||
146 strcmp(xsltfile, "legacy-fix.xsl") == 0 ||
147 strcmp(xsltfile, "xccdf-guide.xsl") == 0)
148 ns_workaround = true;
149 }
150
151 *stylesheet = xsltParseStylesheetFile(BAD_CAST xsltpath);
152 if (*stylesheet == NULL) {
153 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not parse XSLT file '%s'", xsltpath);
154 free(xsltpath);
155 return NULL;
156 }
157
158 if (ns_workaround) {
159 if (xccdf_ns_xslt_workaround(doc, xmlDocGetRootElement(doc)) != 0) {
160 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Had problems employing XCCDF XSLT namespace workaround for XML document '%s'",
161 oscap_source_readable_origin(source));
162 free(xsltpath);
163 xsltFreeStylesheet(*stylesheet);
164 *stylesheet = NULL;
165 return NULL;
166 }
167 }
168
169 char **args = calloc(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_sprintf("'%s'", params[i+1]);
174 }
175
176 xmlDoc *transformed = xsltApplyStylesheet(*stylesheet, doc, (const char **) args);
177 for (size_t i = 0; args[i]; i += 2) {
178 free(args[i+1]);
179 }
180 free(args);
181 if (transformed == NULL) {
182 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not apply XSLT %s to XML file: %s", xsltpath,
183 oscap_source_readable_origin(source));
184 free(xsltpath);
185 xsltFreeStylesheet(*stylesheet);
186 *stylesheet = NULL;
187 return NULL;
188 }
189 free(xsltpath);
190 return transformed;
191
192 }
193
194 int oscap_source_apply_xslt_path(struct oscap_source *source, const char *xsltfile, const char *outfile, const char **params, const char *path_to_xslt)
195 {
196 xsltStylesheet *stylesheet = NULL;
197 xmlDocPtr transformed = apply_xslt_path_internal(source, xsltfile, params, path_to_xslt, &stylesheet);
198 if (transformed == NULL) {
199 return -1;
200 }
201 int ret = save_stylesheet_result_to_file(transformed, stylesheet, outfile);
202 xsltFreeStylesheet(stylesheet);
203 xmlFreeDoc(transformed);
204 return ret;
205 }
206
207 char *oscap_source_apply_xslt_path_mem(struct oscap_source *source, const char *xsltfile, const char **params, const char *path_to_xslt)
208 {
209 xsltStylesheet *stylesheet = NULL;
210 xmlDocPtr transformed = apply_xslt_path_internal(source, xsltfile, params, path_to_xslt, &stylesheet);
211 if (transformed == NULL) {
212 return NULL;
213 }
214 xmlChar *result = NULL;
215 int len;
216 if (xsltSaveResultToString(&result, &len, transformed, stylesheet) != 0) {
217 oscap_seterr(OSCAP_EFAMILY_XML, "Could not save transformend content to buffer, after applying XSLT %s",
218 xsltfile);
219 free(result);
220 result = NULL;
221 }
222 return (char *)result;
223 }