| 1 | /* |
| 2 | * Copyright 2009--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 | * Authors: |
| 20 | * Lukas Kuklinek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <ctype.h> |
| 30 | #include <math.h> |
| 31 | |
| 32 | #include "public/xccdf_benchmark.h" |
| 33 | |
| 34 | #include "elements.h" |
| 35 | #include "common/elements.h" |
| 36 | #include "common/_error.h" |
| 37 | #include "common/debug_priv.h" |
| 38 | #include "common/xmlns_priv.h" |
| 39 | #include "source/oscap_source_priv.h" |
| 40 | #include "source/public/oscap_source.h" |
| 41 | |
| 42 | struct xccdf_version_info { |
| 43 | const char* version; ///< MAJOR.MINOR, for example "1.1" or "1.2" |
| 44 | const char* namespace_uri; ///< XML namespace URI of that particular version |
| 45 | const char* cpe_version; ///< CPE version used in that particular XCCDF version |
| 46 | }; |
| 47 | |
| 48 | const char* xccdf_version_info_get_version(const struct xccdf_version_info* v) |
| 49 | { |
| 50 | if (v == NULL) |
| 51 | return "unknown"; |
| 52 | |
| 53 | return v->version; |
| 54 | } |
| 55 | |
| 56 | const char* xccdf_version_info_get_namespace_uri(const struct xccdf_version_info* v) |
| 57 | { |
| 58 | if (v == NULL) |
| 59 | return "unknown"; |
| 60 | |
| 61 | return v->namespace_uri; |
| 62 | } |
| 63 | |
| 64 | const char* xccdf_version_info_get_cpe_version(const struct xccdf_version_info* v) |
| 65 | { |
| 66 | if (v == NULL) |
| 67 | return "unknown"; |
| 68 | |
| 69 | return v->cpe_version; |
| 70 | } |
| 71 | |
| 72 | /// Map of XCCDF versions and their properties |
| 73 | static const struct xccdf_version_info XCCDF_VERSION_MAP[] = { |
| 74 | {"1.2", "http://checklists.nist.gov/xccdf/1.2", "2.3"}, |
| 75 | {"1.1", "http://checklists.nist.gov/xccdf/1.1", "2.0"}, |
| 76 | {"1.1", "http://open-scap.org/page/Xccdf-1.1-tailoring", "2.0"}, |
| 77 | {NULL, NULL, NULL} |
| 78 | }; |
| 79 | |
| 80 | const struct xccdf_version_info *xccdf_version_info_find(const char *version) |
| 81 | { |
| 82 | const struct xccdf_version_info *mapptr; |
| 83 | for (mapptr = XCCDF_VERSION_MAP; mapptr->version != 0; ++mapptr) { |
| 84 | if (!strcmp(mapptr->version, version)) |
| 85 | return mapptr; |
| 86 | } |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | static const struct xccdf_version_info *_namespace_get_xccdf_version_info(const char *namespace_uri) |
| 91 | { |
| 92 | const struct xccdf_version_info *mapptr; |
| 93 | for (mapptr = XCCDF_VERSION_MAP; mapptr->version != 0; ++mapptr) { |
| 94 | if (!strcmp(mapptr->namespace_uri, namespace_uri)) |
| 95 | return mapptr; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | const struct xccdf_version_info* xccdf_detect_version_parser(xmlTextReaderPtr reader) |
| 101 | { |
| 102 | const struct xccdf_version_info *mapptr; |
| 103 | |
| 104 | const char* namespace_uri = (const char*)xmlTextReaderConstNamespaceUri(reader); |
| 105 | if (!namespace_uri) { |
| 106 | oscap_setxmlerr(xmlGetLastError()); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | mapptr = _namespace_get_xccdf_version_info(namespace_uri); |
| 111 | if (mapptr == NULL) |
| 112 | oscap_seterr(OSCAP_EFAMILY_XML, "This is not known XCCDF namespace: %s.", namespace_uri); |
| 113 | return mapptr; |
| 114 | } |
| 115 | |
| 116 | bool xccdf_is_supported_namespace(xmlNs *ns) |
| 117 | { |
| 118 | return ns != NULL && ns->href != NULL && |
| 119 | _namespace_get_xccdf_version_info((const char *) ns->href) != NULL; |
| 120 | } |
| 121 | |
| 122 | char *xccdf_detect_version_priv(xmlTextReader *reader) |
| 123 | { |
| 124 | while (xmlTextReaderRead(reader) == 1 && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT); |
| 125 | const struct xccdf_version_info *ver_info = xccdf_detect_version_parser(reader); |
| 126 | if (ver_info == NULL) { |
| 127 | return NULL; |
| 128 | } |
| 129 | return oscap_strdup(xccdf_version_info_get_version(ver_info)); |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | xccdf_version_cmp(const struct xccdf_version_info *actual, const char *desired) |
| 134 | { |
| 135 | /* Since the "unknown" value is strdup-ed during clone, we cannot just use common strcmp. |
| 136 | * We need to handle "unknown" version first. |
| 137 | */ |
| 138 | if (actual == NULL) |
| 139 | return -1; |
| 140 | if (oscap_streq(xccdf_version_info_get_version(actual), "unknown")) |
| 141 | return 1; |
| 142 | if (desired == NULL) |
| 143 | return 1; |
| 144 | if (oscap_streq(desired, "") || oscap_streq(desired, "unknown")) |
| 145 | return -1; |
| 146 | |
| 147 | #ifdef __USE_GNU |
| 148 | return strverscmp(xccdf_version_info_get_version(actual), desired); |
| 149 | #else |
| 150 | return strcmp(xccdf_version_info_get_version(actual), desired); |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | struct xccdf_element_spec { |
| 155 | xccdf_element_t id; ///< element ID |
| 156 | const char *ns; ///< namespace URI |
| 157 | const char *name; ///< element name |
| 158 | }; |
| 159 | |
| 160 | #define XCCDF_XMLNS_PREFIX "http://checklists.nist.gov/xccdf" |
| 161 | #define XCCDF_OR_TAILORING_EXTENSION_XMLNS "http://open-scap.org/page/Xccdf-1.1-tailoring" |
| 162 | |
| 163 | /// Mapping of the element names to the symbolic constants |
| 164 | static const struct xccdf_element_spec XCCDF_ELEMENT_MAP[] = { |
| 165 | {XCCDFE_BENCHMARK, XCCDF_XMLNS_PREFIX, "Benchmark"}, |
| 166 | {XCCDFE_GROUP, XCCDF_XMLNS_PREFIX, "Group"}, |
| 167 | {XCCDFE_RULE, XCCDF_XMLNS_PREFIX, "Rule"}, |
| 168 | {XCCDFE_VALUE, XCCDF_XMLNS_PREFIX, "Value"}, |
| 169 | {XCCDFE_PROFILE, XCCDF_XMLNS_PREFIX, "Profile"}, |
| 170 | {XCCDFE_TESTRESULT, XCCDF_XMLNS_PREFIX, "TestResult"}, |
| 171 | {XCCDFE_RESULT_BENCHMARK, XCCDF_OR_TAILORING_EXTENSION_XMLNS, "benchmark"}, ///< this is also XCCDFE_BENCHMARK_REF |
| 172 | {XCCDFE_CHECK, XCCDF_XMLNS_PREFIX, "check"}, |
| 173 | {XCCDFE_CHECK_IMPORT, XCCDF_XMLNS_PREFIX, "check-import"}, |
| 174 | {XCCDFE_CHECK_EXPORT, XCCDF_XMLNS_PREFIX, "check-export"}, |
| 175 | {XCCDFE_CHECK_CONTENT, XCCDF_XMLNS_PREFIX, "check-content"}, |
| 176 | {XCCDFE_CHECK_CONTENT_REF, XCCDF_XMLNS_PREFIX, "check-content-ref"}, |
| 177 | {XCCDFE_CHOICES, XCCDF_XMLNS_PREFIX, "choices"}, |
| 178 | {XCCDFE_CHOICE, XCCDF_XMLNS_PREFIX, "choice"}, |
| 179 | {XCCDFE_COMPLEX_CHECK, XCCDF_XMLNS_PREFIX, "complex-check"}, |
| 180 | {XCCDFE_CONFLICTS, XCCDF_XMLNS_PREFIX, "conflicts"}, |
| 181 | {XCCDFE_CPE_LIST, XMLNS_CPE1D, "cpe-list"}, |
| 182 | {XCCDFE_DC_STATUS, XCCDF_XMLNS_PREFIX, "dc-status"}, |
| 183 | {XCCDFE_DEFAULT, XCCDF_XMLNS_PREFIX, "default"}, |
| 184 | {XCCDFE_DESCRIPTION, XCCDF_XMLNS_PREFIX, "description"}, |
| 185 | {XCCDFE_FACT, XCCDF_XMLNS_PREFIX, "fact"}, |
| 186 | {XCCDFE_FIX, XCCDF_XMLNS_PREFIX, "fix"}, |
| 187 | {XCCDFE_FIXTEXT, XCCDF_XMLNS_PREFIX, "fixtext"}, |
| 188 | {XCCDFE_FRONT_MATTER, XCCDF_XMLNS_PREFIX, "front-matter"}, |
| 189 | {XCCDFE_IDENT, XCCDF_XMLNS_PREFIX, "ident"}, |
| 190 | {XCCDFE_IDENTITY, XCCDF_XMLNS_PREFIX, "identity"}, |
| 191 | {XCCDFE_IMPACT_METRIC, XCCDF_XMLNS_PREFIX, "impact-metric"}, |
| 192 | {XCCDFE_INSTANCE, XCCDF_XMLNS_PREFIX, "instance"}, |
| 193 | {XCCDFE_LOWER_BOUND, XCCDF_XMLNS_PREFIX, "lower-bound"}, |
| 194 | {XCCDFE_MATCH, XCCDF_XMLNS_PREFIX, "match"}, |
| 195 | {XCCDFE_MESSAGE, XCCDF_XMLNS_PREFIX, "message"}, |
| 196 | {XCCDFE_METADATA, XCCDF_XMLNS_PREFIX, "metadata"}, |
| 197 | {XCCDFE_MODEL, XCCDF_XMLNS_PREFIX, "model"}, |
| 198 | {XCCDFE_NEW_RESULT, XCCDF_XMLNS_PREFIX, "new-result"}, |
| 199 | {XCCDFE_NOTICE, XCCDF_XMLNS_PREFIX, "notice"}, |
| 200 | {XCCDFE_OLD_RESULT, XCCDF_XMLNS_PREFIX, "old-result"}, |
| 201 | {XCCDFE_ORGANIZATION, XCCDF_XMLNS_PREFIX, "organization"}, |
| 202 | {XCCDFE_OVERRIDE, XCCDF_XMLNS_PREFIX, "override"}, |
| 203 | {XCCDFE_PARAM, XCCDF_XMLNS_PREFIX, "param"}, |
| 204 | {XCCDFE_PLAIN_TEXT, XCCDF_XMLNS_PREFIX, "plain-text"}, |
| 205 | {XCCDFE_PLATFORM, XCCDF_XMLNS_PREFIX, "platform"}, |
| 206 | {XCCDFE_CPE2_PLATFORMSPEC, XMLNS_CPE2L, "platform-specification"}, |
| 207 | {XCCDFE_RESULT_PROFILE, XCCDF_XMLNS_PREFIX, "profile"}, |
| 208 | {XCCDFE_PROFILE_NOTE, XCCDF_XMLNS_PREFIX, "profile-note"}, |
| 209 | {XCCDFE_QUESTION, XCCDF_XMLNS_PREFIX, "question"}, |
| 210 | {XCCDFE_RATIONALE, XCCDF_XMLNS_PREFIX, "rationale"}, |
| 211 | {XCCDFE_REAR_MATTER, XCCDF_XMLNS_PREFIX, "rear-matter"}, |
| 212 | {XCCDFE_REFERENCE, XCCDF_XMLNS_PREFIX, "reference"}, |
| 213 | {XCCDFE_REFINE_RULE, XCCDF_XMLNS_PREFIX, "refine-rule"}, |
| 214 | {XCCDFE_REFINE_VALUE, XCCDF_XMLNS_PREFIX, "refine-value"}, |
| 215 | {XCCDFE_REMARK, XCCDF_XMLNS_PREFIX, "remark"}, |
| 216 | {XCCDFE_REQUIRES, XCCDF_XMLNS_PREFIX, "requires"}, |
| 217 | {XCCDFE_RESULT, XCCDF_XMLNS_PREFIX, "result"}, |
| 218 | {XCCDFE_RULE_RESULT, XCCDF_XMLNS_PREFIX, "rule-result"}, |
| 219 | {XCCDFE_SCORE, XCCDF_XMLNS_PREFIX, "score"}, |
| 220 | {XCCDFE_SELECT, XCCDF_XMLNS_PREFIX, "select"}, |
| 221 | {XCCDFE_SET_VALUE, XCCDF_XMLNS_PREFIX, "set-value"}, |
| 222 | {XCCDFE_SIGNATURE, XCCDF_XMLNS_PREFIX, "signature"}, |
| 223 | {XCCDFE_SOURCE, XCCDF_XMLNS_PREFIX, "source"}, |
| 224 | {XCCDFE_STATUS, XCCDF_XMLNS_PREFIX, "status"}, |
| 225 | {XCCDFE_SUB, XCCDF_XMLNS_PREFIX, "sub"}, |
| 226 | {XCCDFE_TAILORING, XCCDF_OR_TAILORING_EXTENSION_XMLNS, "Tailoring"}, |
| 227 | {XCCDFE_TARGET, XCCDF_XMLNS_PREFIX, "target"}, |
| 228 | {XCCDFE_TARGET_ADDRESS, XCCDF_XMLNS_PREFIX, "target-address"}, |
| 229 | {XCCDFE_TARGET_FACTS, XCCDF_XMLNS_PREFIX, "target-facts"}, |
| 230 | {XCCDFE_TARGET_IDENTIFIER, XCCDF_XMLNS_PREFIX, "target-id-ref"}, |
| 231 | {XCCDFE_TITLE, XCCDF_XMLNS_PREFIX, "title"}, |
| 232 | {XCCDFE_UPPER_BOUND, XCCDF_XMLNS_PREFIX, "upper-bound"}, |
| 233 | {XCCDFE_VALUE_VAL, XCCDF_XMLNS_PREFIX, "value"}, |
| 234 | {XCCDFE_VERSION, XCCDF_OR_TAILORING_EXTENSION_XMLNS, "version"}, |
| 235 | {XCCDFE_WARNING, XCCDF_XMLNS_PREFIX, "warning"}, |
| 236 | {0, NULL, NULL} |
| 237 | }; |
| 238 | |
| 239 | const char *xccdf_element_to_str(xccdf_element_t element) |
| 240 | { |
| 241 | for (const struct xccdf_element_spec *mapptr = XCCDF_ELEMENT_MAP; mapptr->id != 0; ++mapptr) { |
| 242 | if (element == mapptr->id) { |
| 243 | return mapptr->name; |
| 244 | } |
| 245 | } |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | xccdf_element_t xccdf_element_get(xmlTextReaderPtr reader) |
| 250 | { |
| 251 | if (xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT) |
| 252 | return XCCDFE_ERROR; |
| 253 | |
| 254 | const struct xccdf_element_spec *mapptr; |
| 255 | const char *name = (const char *)xmlTextReaderConstLocalName(reader); |
| 256 | const char *nsuri = (const char *)xmlTextReaderConstNamespaceUri(reader); |
| 257 | |
| 258 | for (mapptr = XCCDF_ELEMENT_MAP; mapptr->id != 0; ++mapptr) { |
| 259 | if (name && strcmp(mapptr->name, name) == 0) { |
| 260 | // element name exists and matches |
| 261 | |
| 262 | if (!nsuri && !mapptr->ns) |
| 263 | // no namespace for the element and no namespace defined in the table |
| 264 | return mapptr->id; |
| 265 | |
| 266 | if (nsuri && strcmp(mapptr->ns, nsuri) == 0) |
| 267 | // namespace exists and matches the table |
| 268 | return mapptr->id; |
| 269 | |
| 270 | if (nsuri && (strcmp(mapptr->ns, XCCDF_XMLNS_PREFIX) == 0 || strcmp(mapptr->ns, XCCDF_OR_TAILORING_EXTENSION_XMLNS) == 0) && |
| 271 | strncmp(XCCDF_XMLNS_PREFIX, nsuri, strlen(XCCDF_XMLNS_PREFIX)) == 0) |
| 272 | // Namespace exists and XCCDF version-less namespace prefix matches |
| 273 | // it. This is done to support multiple XCCDF versions. |
| 274 | // |
| 275 | // XSD schema will make sure there is no XCCDF version mixing in |
| 276 | // the document. The only thing we are solving here is making sure |
| 277 | // the element is some kind of an XCCDF element. |
| 278 | return mapptr->id; |
| 279 | |
| 280 | if (nsuri && strcmp(mapptr->ns, XCCDF_OR_TAILORING_EXTENSION_XMLNS) == 0 && |
| 281 | strcmp(XCCDF_OR_TAILORING_EXTENSION_XMLNS, nsuri) == 0) |
| 282 | // In case we are supposed to accept XCCDF tailoring extension |
| 283 | return mapptr->id; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return XCCDFE_UNMATCHED; |
| 288 | } |
| 289 | |
| 290 | struct xccdf_attribute_spec { |
| 291 | xccdf_attribute_t id; ///< element ID |
| 292 | const char *ns; ///< namespace URI |
| 293 | const char *name; ///< element name |
| 294 | }; |
| 295 | |
| 296 | static const struct xccdf_attribute_spec XCCDF_ATTRIBUTE_MAP[] = { |
| 297 | {XCCDFA_ABSTRACT, XCCDF_XMLNS_PREFIX, "abstract"}, |
| 298 | {XCCDFA_AUTHENTICATED, XCCDF_XMLNS_PREFIX, "authenticated"}, |
| 299 | {XCCDFA_AUTHORITY, XCCDF_XMLNS_PREFIX, "authority"}, |
| 300 | {XCCDFA_CATEGORY, XCCDF_XMLNS_PREFIX, "category"}, |
| 301 | {XCCDFA_CLUSTER_ID, XCCDF_XMLNS_PREFIX, "cluster-id"}, |
| 302 | {XCCDFA_COMPLEXITY, XCCDF_XMLNS_PREFIX, "complexity"}, |
| 303 | {XCCDFA_CONTEXT, XCCDF_XMLNS_PREFIX, "context"}, |
| 304 | {XCCDFA_DATE, XCCDF_XMLNS_PREFIX, "date"}, |
| 305 | {XCCDFA_DISRUPTION, XCCDF_XMLNS_PREFIX, "disruption"}, |
| 306 | {XCCDFA_END_TIME, XCCDF_XMLNS_PREFIX, "end-time"}, |
| 307 | {XCCDFA_EXPORT_NAME, XCCDF_XMLNS_PREFIX, "export-name"}, |
| 308 | {XCCDFA_EXTENDS, XCCDF_XMLNS_PREFIX, "extends"}, |
| 309 | {XCCDFA_FIXREF, XCCDF_XMLNS_PREFIX, "fixref"}, |
| 310 | {XCCDFA_HIDDEN, XCCDF_XMLNS_PREFIX, "hidden"}, |
| 311 | {XCCDFA_HREF, XCCDF_OR_TAILORING_EXTENSION_XMLNS, "href"}, |
| 312 | {XCCDFA_ID, XCCDF_XMLNS_PREFIX, "id"}, |
| 313 | {XCCDFA_IDREF, XCCDF_XMLNS_PREFIX, "idref"}, |
| 314 | {XCCDFA_IID, XCCDF_XMLNS_PREFIX, "Id"}, |
| 315 | {XCCDFA_IMPORT_NAME, XCCDF_XMLNS_PREFIX, "import-name"}, |
| 316 | {XCCDFA_IMPORT_XPATH, XCCDF_XMLNS_PREFIX, "import-xpath"}, |
| 317 | {XCCDFA_INTERACTIVE, XCCDF_XMLNS_PREFIX, "interactive"}, |
| 318 | {XCCDFA_INTERFACEHINT, XCCDF_XMLNS_PREFIX, "interfaceHint"}, |
| 319 | {XCCDFA_MAXIMUM, XCCDF_XMLNS_PREFIX, "maximum"}, |
| 320 | {XCCDFA_MULTICHECK, XCCDF_XMLNS_PREFIX, "multi-check"}, |
| 321 | {XCCDFA_MULTIPLE, XCCDF_XMLNS_PREFIX, "multiple"}, |
| 322 | {XCCDFA_MUSTMATCH, XCCDF_XMLNS_PREFIX, "mustMatch"}, |
| 323 | {XCCDFA_NAME, XCCDF_XMLNS_PREFIX, "name"}, |
| 324 | {XCCDFA_NEGATE, XCCDF_XMLNS_PREFIX, "negate"}, |
| 325 | {XCCDFA_NOTE_TAG, XCCDF_XMLNS_PREFIX, "note-tag"}, |
| 326 | {XCCDFA_OPERATOR, XCCDF_XMLNS_PREFIX, "operator"}, |
| 327 | {XCCDFA_OVERRIDE, XCCDF_XMLNS_PREFIX, "override"}, |
| 328 | {XCCDFA_PARENTCONTEXT, XCCDF_XMLNS_PREFIX, "parentContext"}, |
| 329 | {XCCDFA_PLATFORM, XCCDF_XMLNS_PREFIX, "platform"}, |
| 330 | {XCCDFA_PRIVILEDGED, XCCDF_XMLNS_PREFIX, "priviledged"}, |
| 331 | {XCCDFA_PROHIBITCHANGES, XCCDF_XMLNS_PREFIX, "prohibitChanges"}, |
| 332 | {XCCDFA_REBOOT, XCCDF_XMLNS_PREFIX, "reboot"}, |
| 333 | {XCCDFA_RESOLVED, XCCDF_XMLNS_PREFIX, "resolved"}, |
| 334 | {XCCDFA_ROLE, XCCDF_XMLNS_PREFIX, "role"}, |
| 335 | {XCCDFA_SELECTED, XCCDF_XMLNS_PREFIX, "selected"}, |
| 336 | {XCCDFA_SELECTOR, XCCDF_XMLNS_PREFIX, "selector"}, |
| 337 | {XCCDFA_SEVERITY, XCCDF_XMLNS_PREFIX, "severity"}, |
| 338 | {XCCDFA_START_TIME, XCCDF_XMLNS_PREFIX, "start-time"}, |
| 339 | {XCCDFA_STRATEGY, XCCDF_XMLNS_PREFIX, "strategy"}, |
| 340 | {XCCDFA_STYLE, XCCDF_XMLNS_PREFIX, "style"}, |
| 341 | {XCCDFA_STYLE_HREF, XCCDF_XMLNS_PREFIX, "style-href"}, |
| 342 | {XCCDFA_SYSTEM, XCCDF_XMLNS_PREFIX, "system"}, |
| 343 | {XCCDFA_TAG, XCCDF_XMLNS_PREFIX, "tag"}, |
| 344 | {XCCDFA_TEST_SYSTEM, XCCDF_XMLNS_PREFIX, "test-system"}, |
| 345 | {XCCDFA_TIME, XCCDF_OR_TAILORING_EXTENSION_XMLNS, "time"}, |
| 346 | {XCCDFA_TYPE, XCCDF_XMLNS_PREFIX, "type"}, |
| 347 | {XCCDFA_UPDATE, XCCDF_XMLNS_PREFIX, "update"}, |
| 348 | {XCCDFA_URI, XCCDF_XMLNS_PREFIX, "uri"}, |
| 349 | {XCCDFA_VALUE_ID, XCCDF_XMLNS_PREFIX, "value-id"}, |
| 350 | {XCCDFA_VERSION, XCCDF_XMLNS_PREFIX, "version"}, |
| 351 | {XCCDFA_WEIGHT, XCCDF_XMLNS_PREFIX, "weight"}, |
| 352 | {0, NULL, NULL} |
| 353 | }; |
| 354 | |
| 355 | bool xccdf_attribute_has(xmlTextReaderPtr reader, xccdf_attribute_t attr) |
| 356 | { |
| 357 | return xccdf_attribute_get(reader, attr) != NULL; |
| 358 | } |
| 359 | |
| 360 | const char *xccdf_attribute_get(xmlTextReaderPtr reader, xccdf_attribute_t attr) |
| 361 | { |
| 362 | bool found = false; |
| 363 | const struct xccdf_attribute_spec *mapptr = XCCDF_ATTRIBUTE_MAP; |
| 364 | |
| 365 | while (mapptr->id) { |
| 366 | if (attr == mapptr->id) { |
| 367 | found = true; |
| 368 | break; |
| 369 | } |
| 370 | ++mapptr; |
| 371 | } |
| 372 | |
| 373 | if (!found) |
| 374 | return NULL; |
| 375 | |
| 376 | if (xmlTextReaderMoveToAttribute(reader, BAD_CAST mapptr->name) != 1) |
| 377 | return NULL; |
| 378 | // do not check the XML namespace for now... maybe a libxml bug? |
| 379 | // if (strcmp((const char*)xmlTextReaderConstNamespaceUri(reader), BAD_CAST mapptr->ns) != 0) return NULL; |
| 380 | |
| 381 | return (const char *)xmlTextReaderConstValue(reader); |
| 382 | } |
| 383 | |
| 384 | char *xccdf_attribute_copy(xmlTextReaderPtr reader, xccdf_attribute_t attr) |
| 385 | { |
| 386 | const char *ret = xccdf_attribute_get(reader, attr); |
| 387 | return oscap_strdup(ret); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | bool xccdf_attribute_get_bool(xmlTextReaderPtr reader, xccdf_attribute_t attr) |
| 392 | { |
| 393 | return oscap_string_to_enum(OSCAP_BOOL_MAP, xccdf_attribute_get(reader, attr)); |
| 394 | } |
| 395 | |
| 396 | float xccdf_attribute_get_float(xmlTextReaderPtr reader, xccdf_attribute_t attr) |
| 397 | { |
| 398 | float res; |
| 399 | if (xccdf_attribute_has(reader, attr) && sscanf(xccdf_attribute_get(reader, attr), "%f", &res) == 1) |
| 400 | return res; |
| 401 | else |
| 402 | return NAN; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | void xccdf_print_depth(int depth) |
| 407 | { |
| 408 | while (depth--) |
| 409 | printf(" "); |
| 410 | } |
| 411 | |
| 412 | void xccdf_print_max_text(const struct oscap_text *txt, int max, const char *ellipsis) |
| 413 | { |
| 414 | if (txt == NULL) return; |
| 415 | |
| 416 | printf("[%c%c%c|%s] ", |
| 417 | oscap_text_get_is_html(txt) ? 'h' : '-', |
| 418 | oscap_text_get_can_override(txt) ? (oscap_text_get_overrides(txt) ? 'O' : 'o') : '-', |
| 419 | oscap_text_get_can_substitute(txt) ? 's' : '-', |
| 420 | oscap_text_get_lang(txt)); |
| 421 | xccdf_print_max(oscap_text_get_text(txt), max, ellipsis); |
| 422 | } |
| 423 | |
| 424 | void xccdf_print_textlist(struct oscap_text_iterator *txt, int depth, int max, const char *ellipsis) |
| 425 | { |
| 426 | if (txt == NULL) return; |
| 427 | |
| 428 | printf("\n"); |
| 429 | |
| 430 | while (oscap_text_iterator_has_more(txt)) { |
| 431 | struct oscap_text *text = oscap_text_iterator_next(txt); |
| 432 | xccdf_print_depth(depth); |
| 433 | xccdf_print_max_text(text, max, ellipsis); |
| 434 | printf("\n"); |
| 435 | } |
| 436 | |
| 437 | oscap_text_iterator_free(txt); |
| 438 | } |
| 439 | |
| 440 | void xccdf_print_max(const char *str, int max, const char *ellipsis) |
| 441 | { |
| 442 | if (str) |
| 443 | while (isspace(*str)) |
| 444 | ++str; |
| 445 | int len = strlen("(null)");; |
| 446 | char buf[32]; |
| 447 | if (str) |
| 448 | len = strlen(str); |
| 449 | sprintf(buf, "%%.%ds", max); |
| 450 | printf(buf, str); |
| 451 | if (len > max) |
| 452 | printf("%s", ellipsis); |
| 453 | } |
| 454 | |
| 455 | xmlNs *lookup_xccdf_ns(xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info *version_info) |
| 456 | { |
| 457 | assert(parent != NULL); |
| 458 | |
| 459 | xmlNs *ns_xccdf = xmlSearchNsByHref(doc, parent, |
| 460 | (const xmlChar *)xccdf_version_info_get_namespace_uri(version_info)); |
| 461 | if (ns_xccdf == NULL) { |
| 462 | ns_xccdf = xmlNewNs(parent, |
| 463 | (const xmlChar *) xccdf_version_info_get_namespace_uri(version_info), NULL); |
| 464 | } |
| 465 | return ns_xccdf; |
| 466 | } |