Debian Codegraph @

src/source/validate.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 <libxml/xmlerror.h>
27 #include <libxml/xmlschemas.h>
28 #include <string.h>
29 #ifdef OS_WINDOWS
30 #include <io.h>
31 #else
32 #include <unistd.h>
33 #endif
34
35 #include "common/_error.h"
36 #include "common/util.h"
37 #include "oscap.h"
38 #include "oscap_source.h"
39 #include "source/oscap_source_priv.h"
40 #include "source/validate_priv.h"
41 #include "oscap_helpers.h"
42
43 struct ctxt {
44 xml_reporter reporter;
45 void *arg;
46 char *filename;
47 };
48
49 static void oscap_xml_validity_handler(void *user, const xmlError *error)
50 {
51 struct ctxt * context = (struct ctxt *) user;
52
53 if (context == NULL || context->reporter == NULL)
54 return;
55
56 if (error == NULL)
57 error = xmlGetLastError();
58
59 if (error->code == 3083) {
60 /*
61 libxml2 outputs a warning for something that is completely harmless
62 and happens very often, clogging the screen and making real issues
63 hard to spot.
64
65 The message of the warning is:
66
67 Skipping import of schema located at ... for the namespace ...,
68 since this namespace was already imported with the schema located at ..."
69
70 We can't prevent this with the schemas because they are interdependent
71 and it is not a good idea to alter XSD schemas comming from standard
72 bodies anyways.
73 */
74
75 // ignore the warning as if it never existed
76 return;
77 }
78
79 const char *file = error->file;
80 if (file == NULL)
81 file = context->filename;
82
83 context->reporter(file, error->line, error->message, context->arg);
84 }
85
86 static inline int oscap_validate_xml(struct oscap_source *source, const char *schemafile, xml_reporter reporter, void *arg)
87 {
88 int result = -1;
89 xmlSchemaParserCtxtPtr parser_ctxt = NULL;
90 xmlSchemaPtr schema = NULL;
91 xmlSchemaValidCtxtPtr ctxt = NULL;
92 xmlDocPtr doc = NULL;
93
94 struct ctxt context = { reporter, arg, (void*) oscap_source_readable_origin(source)};
95
96 if (schemafile == NULL) {
97 oscap_seterr(OSCAP_EFAMILY_OSCAP, "'schemafile' == NULL");
98 return -1;
99 }
100
101 char * schemapath = oscap_sprintf("%s%s%s", oscap_path_to_schemas(), "/", schemafile);
102 if (access(schemapath, R_OK)) {
103 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Schema file '%s' not found in path '%s' when trying to validate '%s'",
104 schemafile, oscap_path_to_schemas(), oscap_source_readable_origin(source));
105 goto cleanup;
106 }
107
108 parser_ctxt = xmlSchemaNewParserCtxt(schemapath);
109 if (parser_ctxt == NULL) {
110 oscap_seterr(OSCAP_EFAMILY_XML, "Could not create parser context for validation");
111 goto cleanup;
112 }
113
114 xmlSchemaSetParserStructuredErrors(parser_ctxt, (xmlStructuredErrorFunc) oscap_xml_validity_handler, &context);
115
116 schema = xmlSchemaParse(parser_ctxt);
117 if (schema == NULL) {
118 oscap_seterr(OSCAP_EFAMILY_XML, "Could not parse XML schema");
119 goto cleanup;
120 }
121
122 ctxt = xmlSchemaNewValidCtxt(schema);
123 if (ctxt == NULL) {
124 oscap_seterr(OSCAP_EFAMILY_XML, "Could not create validation context");
125 goto cleanup;
126 }
127
128 xmlSchemaSetValidStructuredErrors(ctxt, (xmlStructuredErrorFunc) oscap_xml_validity_handler, &context);
129
130 doc = oscap_source_get_xmlDoc(source);
131 if (!doc)
132 goto cleanup;
133
134 result = xmlSchemaValidateDoc(ctxt, doc);
135
136 /*
137 * xmlSchemaValidateFile() returns "-1" if document is not well formed
138 * thefore we ignore libxml internal errors here and map return code to
139 * either pass or fail.
140 */
141 if (result != 0)
142 result = 1;
143 /* This would be nicer
144 * if (result == -1)
145 * oscap_setxmlerr(xmlGetLastError());
146 */
147
148 cleanup:
149 if (ctxt)
150 xmlSchemaFreeValidCtxt(ctxt);
151 if (schema)
152 xmlSchemaFree(schema);
153 if (parser_ctxt)
154 xmlSchemaFreeParserCtxt(parser_ctxt);
155 free(schemapath);
156
157 return result;
158 }
159
160 // TODO do not hardcode versions... (duplicities)
161 // patch version fragments are intentionally left out, we strive to ship
162 // schemas of the newest patch versions of that particular minor.major version
163 // todo: ugly
164 // FIXME: we ship OCIL schemes but they aren't supported in openscap
165 // and aren't in this list
166 struct oscap_schema_table_entry OSCAP_SCHEMAS_TABLE[] = {
167 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.3", "oval/5.3/oval-definitions-schema.xsd"},
168 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.4", "oval/5.4/oval-definitions-schema.xsd"},
169 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.5", "oval/5.5/oval-definitions-schema.xsd"},
170 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.6", "oval/5.6/oval-definitions-schema.xsd"},
171 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.7", "oval/5.7/oval-definitions-schema.xsd"},
172 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.8", "oval/5.8/oval-definitions-schema.xsd"},
173 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.9", "oval/5.9/oval-definitions-schema.xsd"},
174 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.10", "oval/5.10/oval-definitions-schema.xsd"},
175 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.10.1","oval/5.10.1/oval-definitions-schema.xsd"},
176 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11", "oval/5.11/oval-definitions-schema.xsd"},
177 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11.1", "oval/5.11.1/oval-definitions-schema.xsd"},
178 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11.2", "oval/5.11.2/oval-definitions-schema.xsd"},
179 {OSCAP_DOCUMENT_OVAL_DEFINITIONS, "5.11.3", "oval/5.11.3/oval-definitions-schema.xsd"},
180 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.8", "oval/5.8/oval-directives-schema.xsd"},
181 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.9", "oval/5.9/oval-directives-schema.xsd"},
182 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.10", "oval/5.10/oval-directives-schema.xsd"},
183 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.10.1","oval/5.10.1/oval-directives-schema.xsd"},
184 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11", "oval/5.11/oval-directives-schema.xsd"},
185 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11.1", "oval/5.11.1/oval-directives-schema.xsd"},
186 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11.2", "oval/5.11.2/oval-directives-schema.xsd"},
187 {OSCAP_DOCUMENT_OVAL_DIRECTIVES, "5.11.3", "oval/5.11.3/oval-directives-schema.xsd"},
188 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.3", "oval/5.3/oval-results-schema.xsd"},
189 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.4", "oval/5.4/oval-results-schema.xsd"},
190 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.5", "oval/5.5/oval-results-schema.xsd"},
191 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.6", "oval/5.6/oval-results-schema.xsd"},
192 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.7", "oval/5.7/oval-results-schema.xsd"},
193 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.8", "oval/5.8/oval-results-schema.xsd"},
194 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.9", "oval/5.9/oval-results-schema.xsd"},
195 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.10", "oval/5.10/oval-results-schema.xsd"},
196 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.10.1","oval/5.10.1/oval-results-schema.xsd"},
197 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11", "oval/5.11/oval-results-schema.xsd"},
198 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11.1", "oval/5.11.1/oval-results-schema.xsd"},
199 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11.2", "oval/5.11.2/oval-results-schema.xsd"},
200 {OSCAP_DOCUMENT_OVAL_RESULTS, "5.11.3", "oval/5.11.3/oval-results-schema.xsd"},
201 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.3", "oval/5.3/oval-system-characteristics-schema.xsd"},
202 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.4", "oval/5.4/oval-system-characteristics-schema.xsd"},
203 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.5", "oval/5.5/oval-system-characteristics-schema.xsd"},
204 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.6", "oval/5.6/oval-system-characteristics-schema.xsd"},
205 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.7", "oval/5.7/oval-system-characteristics-schema.xsd"},
206 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.8", "oval/5.8/oval-system-characteristics-schema.xsd"},
207 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.9", "oval/5.9/oval-system-characteristics-schema.xsd"},
208 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.10", "oval/5.10/oval-system-characteristics-schema.xsd"},
209 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.10.1","oval/5.10.1/oval-system-characteristics-schema.xsd"},
210 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11", "oval/5.11/oval-system-characteristics-schema.xsd"},
211 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11.1", "oval/5.11.1/oval-system-characteristics-schema.xsd"},
212 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11.2", "oval/5.11.2/oval-system-characteristics-schema.xsd"},
213 {OSCAP_DOCUMENT_OVAL_SYSCHAR, "5.11.3", "oval/5.11.3/oval-system-characteristics-schema.xsd"},
214 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.3", "oval/5.3/oval-variables-schema.xsd"},
215 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.4", "oval/5.4/oval-variables-schema.xsd"},
216 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.5", "oval/5.5/oval-variables-schema.xsd"},
217 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.6", "oval/5.6/oval-variables-schema.xsd"},
218 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.7", "oval/5.7/oval-variables-schema.xsd"},
219 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.8", "oval/5.8/oval-variables-schema.xsd"},
220 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.9", "oval/5.9/oval-variables-schema.xsd"},
221 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.10", "oval/5.10/oval-variables-schema.xsd"},
222 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.10.1","oval/5.10.1/oval-variables-schema.xsd"},
223 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11", "oval/5.10.1/oval-variables-schema.xsd"},
224 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11.1", "oval/5.11.1/oval-variables-schema.xsd"},
225 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11.2", "oval/5.11.2/oval-variables-schema.xsd"},
226 {OSCAP_DOCUMENT_OVAL_VARIABLES, "5.11.3", "oval/5.11.3/oval-variables-schema.xsd"},
227 {OSCAP_DOCUMENT_SCE_RESULT, "1.0", "sce/1.0/sce-result-schema.xsd"},
228 {OSCAP_DOCUMENT_XCCDF, "1.2", "xccdf/1.2/xccdf_1.2.xsd"},
229 {OSCAP_DOCUMENT_XCCDF, "1.1", "xccdf/1.1/xccdf-schema.xsd"},
230 {OSCAP_DOCUMENT_XCCDF_TAILORING, "1.2", "xccdf/1.2/xccdf_1.2.xsd"},
231 {OSCAP_DOCUMENT_XCCDF_TAILORING, "1.1", "xccdf/1.1-tailoring/xccdf-1.1-tailoring.xsd"}, // unofficial openscap extension!
232 {OSCAP_DOCUMENT_SDS, "1.2", "sds/1.2/scap-source-data-stream_1.2.xsd"},
233 {OSCAP_DOCUMENT_SDS, "1.3", "sds/1.3/scap-source-data-stream_1.3.xsd"},
234 {OSCAP_DOCUMENT_ARF, "1.1", "arf/1.1/asset-reporting-format_1.1.0.xsd"},
235 {OSCAP_DOCUMENT_CPE_DICTIONARY, "2.0", "cpe/2.0/cpe-dictionary_2.0.xsd"},
236 {OSCAP_DOCUMENT_CPE_DICTIONARY, "2.1", "cpe/2.1/cpe-dictionary_2.1.xsd"},
237 {OSCAP_DOCUMENT_CPE_DICTIONARY, "2.2", "cpe/2.2/cpe-dictionary_2.2.xsd"},
238 {OSCAP_DOCUMENT_CPE_DICTIONARY, "2.3", "cpe/2.3/cpe-dictionary_2.3.xsd"},
239 {OSCAP_DOCUMENT_CPE_LANGUAGE, "2.3", "cpe/2.3/cpe-language_2.3.xsd"},
240 {0, NULL, NULL }
241 };
242
243 int oscap_source_validate_priv(struct oscap_source *source, oscap_document_type_t doc_type, const char *version, xml_reporter reporter, void *user)
244 {
245 if (version == NULL) {
246 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Could not determine version for file: %s", oscap_source_readable_origin(source));
247 return -1;
248 }
249
250 /* find a right schema file */
251 for (struct oscap_schema_table_entry *entry = OSCAP_SCHEMAS_TABLE; entry->doc_type != 0; ++entry) {
252 if (entry->doc_type != doc_type || strcmp(entry->schema_version, version))
253 continue;
254
255 return oscap_validate_xml(source, entry->schema_path, reporter, user);
256 }
257
258 oscap_seterr(OSCAP_EFAMILY_OSCAP, "Schema file not found when trying to validate '%s'", oscap_source_readable_origin(source));
259 return -1;
260 }