| 1 | /* |
| 2 | * Copyright 2010 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 <math.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #ifdef OS_WINDOWS |
| 31 | /* By defining WIN32_LEAN_AND_MEAN we ensure that Windows.h won't include |
| 32 | * winsock.h, which would conflict with symbols from WinSock2.h. |
| 33 | */ |
| 34 | #ifndef WIN32_LEAN_AND_MEAN |
| 35 | #define WIN32_LEAN_AND_MEAN |
| 36 | #endif |
| 37 | #include <winsock2.h> |
| 38 | #include <iphlpapi.h> |
| 39 | #include <windows.h> |
| 40 | #include <ws2def.h> |
| 41 | #include <io.h> |
| 42 | #include <winternl.h> |
| 43 | #include <lmcons.h> |
| 44 | #else |
| 45 | #include <unistd.h> |
| 46 | #endif |
| 47 | |
| 48 | #ifdef OSCAP_UNIX |
| 49 | #include <sys/utsname.h> |
| 50 | #include <arpa/inet.h> |
| 51 | #include <netdb.h> |
| 52 | #include <sys/socket.h> |
| 53 | #include <pwd.h> |
| 54 | #include <sys/types.h> |
| 55 | #endif |
| 56 | |
| 57 | #if defined(OS_LINUX) |
| 58 | #include <ifaddrs.h> |
| 59 | #include <net/if.h> |
| 60 | #include <sys/ioctl.h> |
| 61 | #endif |
| 62 | |
| 63 | #if defined(OS_FREEBSD) |
| 64 | #include <arpa/inet.h> |
| 65 | #include <arpa/inet.h> |
| 66 | #include <ifaddrs.h> |
| 67 | #include <net/if.h> |
| 68 | #include <netinet/in.h> |
| 69 | #include <sys/ioctl.h> |
| 70 | #include <sys/sockio.h> |
| 71 | #endif |
| 72 | |
| 73 | #include "item.h" |
| 74 | #include "helpers.h" |
| 75 | #include "xccdf_impl.h" |
| 76 | #include "common/_error.h" |
| 77 | #include "oscap_text.h" |
| 78 | #include "common/debug_priv.h" |
| 79 | #include "source/oscap_source_priv.h" |
| 80 | #include "oscap_helpers.h" |
| 81 | |
| 82 | #define XCCDF_NUMERIC_SIZE 32 |
| 83 | |
| 84 | #ifdef OS_WINDOWS |
| 85 | #define timezone _timezone |
| 86 | #endif |
| 87 | |
| 88 | #ifndef HOST_NAME_MAX |
| 89 | #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX |
| 90 | #endif |
| 91 | |
| 92 | // References containing STIG Rule IDs can be found by their href attribute, it must match the following url |
| 93 | static const char *DISA_STIG_VIEWER_HREF[] = { "http://iase.disa.mil/stigs/Pages/stig-viewing-guidance.aspx", |
| 94 | "https://public.cyber.mil/stigs/srg-stig-tools/", |
| 95 | "https://www.cyber.mil/stigs/srg-stig-tools/" }; |
| 96 | |
| 97 | // constants |
| 98 | static const xccdf_numeric XCCDF_SCORE_MAX_DAFAULT = 100.0f; |
| 99 | static const char *XCCDF_INSTANCE_DEFAULT_CONTEXT = "undefined"; |
| 100 | const char *XCCDF_NUMERIC_FORMAT = "%f"; |
| 101 | |
| 102 | // prototypes |
| 103 | void xccdf_rule_result_free(struct xccdf_rule_result *rr); |
| 104 | void xccdf_identity_free(struct xccdf_identity *identity); |
| 105 | void xccdf_score_free(struct xccdf_score *score); |
| 106 | void xccdf_target_fact_free(struct xccdf_target_fact *fact); |
| 107 | void xccdf_message_free(struct xccdf_message *msg); |
| 108 | void xccdf_instance_free(struct xccdf_instance *inst); |
| 109 | void xccdf_override_free(struct xccdf_override *oride); |
| 110 | |
| 111 | struct xccdf_result *xccdf_result_new(void) |
| 112 | { |
| 113 | struct xccdf_item *result = xccdf_item_new(XCCDF_RESULT, NULL); |
| 114 | oscap_create_lists(&result->sub.result.identities, &result->sub.result.targets, |
| 115 | &result->sub.result.remarks, &result->sub.result.target_addresses, |
| 116 | &result->sub.result.target_facts, &result->sub.result.target_id_refs, &result->sub.result.applicable_platforms, |
| 117 | &result->sub.result.setvalues, &result->sub.result.organizations, |
| 118 | &result->sub.result.rule_results, &result->sub.result.scores, NULL); |
| 119 | return XRESULT(result); |
| 120 | } |
| 121 | |
| 122 | struct xccdf_result * xccdf_result_clone(const struct xccdf_result * result) |
| 123 | { |
| 124 | struct xccdf_item *new_result = calloc(1, sizeof(struct xccdf_item) + sizeof(struct xccdf_result_item)); |
| 125 | struct xccdf_item *old = XITEM(result); |
| 126 | xccdf_item_base_clone(&new_result->item, &(old->item)); |
| 127 | new_result->type = old->type; |
| 128 | xccdf_result_item_clone(&new_result->sub.result, &old->sub.result); |
| 129 | return XRESULT(new_result); |
| 130 | } |
| 131 | |
| 132 | static inline void xccdf_result_free_impl(struct xccdf_item *result) |
| 133 | { |
| 134 | if (result != NULL) { |
| 135 | free(result->sub.result.start_time); |
| 136 | free(result->sub.result.end_time); |
| 137 | free(result->sub.result.test_system); |
| 138 | free(result->sub.result.benchmark_uri); |
| 139 | free(result->sub.result.profile); |
| 140 | |
| 141 | oscap_list_free(result->sub.result.identities, (oscap_destruct_func) xccdf_identity_free); |
| 142 | oscap_list_free(result->sub.result.target_facts, (oscap_destruct_func) xccdf_target_fact_free); |
| 143 | oscap_list_free(result->sub.result.target_id_refs, (oscap_destruct_func) xccdf_target_identifier_free); |
| 144 | oscap_list_free(result->sub.result.applicable_platforms, free); |
| 145 | oscap_list_free(result->sub.result.targets, free); |
| 146 | oscap_list_free(result->sub.result.scores, (oscap_destruct_func) xccdf_score_free); |
| 147 | oscap_list_free(result->sub.result.remarks, (oscap_destruct_func) oscap_text_free); |
| 148 | oscap_list_free(result->sub.result.target_addresses, free); |
| 149 | oscap_list_free(result->sub.result.setvalues, (oscap_destruct_func) xccdf_setvalue_free); |
| 150 | oscap_list_free(result->sub.result.rule_results, (oscap_destruct_func) xccdf_rule_result_free); |
| 151 | oscap_list_free(result->sub.result.organizations, free); |
| 152 | |
| 153 | xccdf_item_release(result); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | XCCDF_FREE_GEN(result) |
| 158 | |
| 159 | XCCDF_ACCESSOR_SIMPLE(result, const struct xccdf_version_info*, schema_version); |
| 160 | XCCDF_ACCESSOR_STRING(result, start_time) |
| 161 | XCCDF_ACCESSOR_STRING(result, end_time) |
| 162 | XCCDF_ACCESSOR_STRING(result, test_system) |
| 163 | XCCDF_ACCESSOR_STRING(result, benchmark_uri) |
| 164 | XCCDF_ACCESSOR_STRING(result, profile) |
| 165 | XCCDF_LISTMANIP(result, identity, identities) |
| 166 | XCCDF_LISTMANIP_STRING(result, target, targets) |
| 167 | XCCDF_LISTMANIP_STRING(result, target_address, target_addresses) |
| 168 | XCCDF_LISTMANIP_STRING(result, organization, organizations) |
| 169 | XCCDF_LISTMANIP_TEXT(result, remark, remarks) |
| 170 | XCCDF_LISTMANIP(result, target_fact, target_facts) |
| 171 | XCCDF_LISTMANIP(result, target_identifier, target_id_refs) |
| 172 | XCCDF_LISTMANIP_STRING(result, applicable_platform, applicable_platforms) |
| 173 | XCCDF_LISTMANIP(result, setvalue, setvalues) |
| 174 | XCCDF_LISTMANIP(result, rule_result, rule_results) |
| 175 | XCCDF_LISTMANIP(result, score, scores) |
| 176 | OSCAP_ITERATOR_GEN(xccdf_result) |
| 177 | OSCAP_ITERATOR_REMOVE_F(xccdf_result) |
| 178 | |
| 179 | static inline void _xccdf_result_add_target_fact_uniq(struct xccdf_result *result, struct xccdf_target_fact *fact) |
| 180 | { |
| 181 | struct xccdf_target_fact_iterator *target_facts = xccdf_result_get_target_facts(result); |
| 182 | while (xccdf_target_fact_iterator_has_more(target_facts)) { |
| 183 | struct xccdf_target_fact *target_fact = xccdf_target_fact_iterator_next(target_facts); |
| 184 | if (target_fact->type == fact->type) |
| 185 | if (target_fact->name != NULL && fact->name != NULL && !strcmp(target_fact->name, fact->name)) |
| 186 | if (target_fact->value != NULL && fact->value != NULL && !strcmp(target_fact->value, fact->value)) { |
| 187 | xccdf_target_fact_free(fact); |
| 188 | goto exit; |
| 189 | } |
| 190 | } |
| 191 | xccdf_result_add_target_fact(result, fact); |
| 192 | exit: |
| 193 | xccdf_target_fact_iterator_free(target_facts); |
| 194 | } |
| 195 | |
| 196 | static inline void _xccdf_result_fill_scanner(struct xccdf_result *result) |
| 197 | { |
| 198 | struct xccdf_target_fact *fact = NULL; |
| 199 | fact = xccdf_target_fact_new(); |
| 200 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:scanner:name"); |
| 201 | xccdf_target_fact_set_string(fact, "OpenSCAP"); |
| 202 | xccdf_result_add_target_fact(result, fact); |
| 203 | |
| 204 | fact = xccdf_target_fact_new(); |
| 205 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:scanner:version"); |
| 206 | xccdf_target_fact_set_string(fact, oscap_get_version()); |
| 207 | xccdf_result_add_target_fact(result, fact); |
| 208 | } |
| 209 | |
| 210 | #if defined(OSCAP_UNIX) |
| 211 | static inline char *_unix_get_name_fallback(void) { |
| 212 | struct passwd *passwd; |
| 213 | |
| 214 | errno = 0; |
| 215 | passwd = getpwuid(getuid()); |
| 216 | if (passwd == NULL || passwd->pw_name == NULL) { |
| 217 | dW("Error when calling getpwuid(): %d, %s\n", errno, strerror(errno)); |
| 218 | return "\0"; |
| 219 | } |
| 220 | |
| 221 | return passwd->pw_name; |
| 222 | } |
| 223 | #endif |
| 224 | |
| 225 | static inline void _xccdf_result_fill_identity(struct xccdf_result *result) |
| 226 | { |
| 227 | struct xccdf_identity *id = xccdf_identity_new(); |
| 228 | #if defined(OS_WINDOWS) |
| 229 | TCHAR w32_username[UNLEN + 1]; |
| 230 | DWORD w32_usernamesize = UNLEN + 1; |
| 231 | #endif |
| 232 | // Reasoning here is that OpenSCAP does not authenticate user |
| 233 | // nor it grants she any additional privileges |
| 234 | xccdf_identity_set_authenticated(id, 0); |
| 235 | xccdf_identity_set_privileged(id, 0); |
| 236 | #ifdef OSCAP_UNIX |
| 237 | char *name = getlogin(); |
| 238 | if (name == NULL) |
| 239 | name = _unix_get_name_fallback(); |
| 240 | xccdf_identity_set_name(id, name); |
| 241 | #elif defined(OS_WINDOWS) |
| 242 | GetUserName((TCHAR *) w32_username, &w32_usernamesize); /* XXX: Check the return value? */ |
| 243 | xccdf_identity_set_name(id, w32_username); |
| 244 | #else |
| 245 | # warning "_xccdf_result_fill_identity: no support for identity on this OS" |
| 246 | #endif |
| 247 | xccdf_result_add_identity(result, id); |
| 248 | } |
| 249 | |
| 250 | static inline void _xccdf_result_clear_metadata(struct xccdf_item *result) |
| 251 | { |
| 252 | oscap_list_free(result->sub.result.targets, free); |
| 253 | oscap_list_free(result->sub.result.target_addresses, free); |
| 254 | oscap_list_free(result->sub.result.target_facts, (oscap_destruct_func) xccdf_target_fact_free); |
| 255 | oscap_list_free(result->sub.result.identities, (oscap_destruct_func) xccdf_identity_free); |
| 256 | oscap_create_lists( |
| 257 | &result->sub.result.targets, |
| 258 | &result->sub.result.target_addresses, |
| 259 | &result->sub.result.target_facts, |
| 260 | &result->sub.result.identities, |
| 261 | NULL); |
| 262 | } |
| 263 | |
| 264 | #ifdef OSCAP_UNIX |
| 265 | static char *_get_etc_hostname(const char *oscap_probe_root) |
| 266 | { |
| 267 | FILE *fp; |
| 268 | char hname[HOST_NAME_MAX+1] = { '\0' }; |
| 269 | char *ret = NULL; |
| 270 | int rc; |
| 271 | |
| 272 | fp = oscap_fopen_with_prefix(oscap_probe_root, "/etc/hostname"); |
| 273 | |
| 274 | if (fp == NULL) { |
| 275 | dD("Trying to use /proc/sys/kernel/hostname instead of /etc/hostname"); |
| 276 | fp = oscap_fopen_with_prefix(oscap_probe_root, "/proc/sys/kernel/hostname"); |
| 277 | } |
| 278 | |
| 279 | if (fp == NULL) |
| 280 | goto fail; |
| 281 | |
| 282 | rc = fread(hname, 1, HOST_NAME_MAX, fp); |
| 283 | /* If file is empty, we don't want to allocate an empty string for it */ |
| 284 | if (ferror(fp) || rc == 0 ) |
| 285 | goto finish; |
| 286 | |
| 287 | hname[strcspn(hname, "\n")] = '\0'; |
| 288 | ret = strdup(hname); |
| 289 | |
| 290 | finish: |
| 291 | fclose(fp); |
| 292 | fail: |
| 293 | return ret; |
| 294 | } |
| 295 | #endif |
| 296 | |
| 297 | void xccdf_result_fill_sysinfo(struct xccdf_result *result) |
| 298 | { |
| 299 | #if defined(OS_LINUX) || defined(OS_FREEBSD) |
| 300 | struct ifaddrs *ifaddr, *ifa; |
| 301 | int fd; |
| 302 | #endif |
| 303 | struct xccdf_target_fact *fact = NULL; |
| 304 | const char *probe_root = getenv("OSCAP_PROBE_ROOT"); |
| 305 | |
| 306 | _xccdf_result_clear_metadata(XITEM(result)); |
| 307 | _xccdf_result_fill_scanner(result); |
| 308 | |
| 309 | #ifdef OSCAP_UNIX |
| 310 | char *hostname = NULL; |
| 311 | char *fqdn = NULL; |
| 312 | if (probe_root) { |
| 313 | hostname = _get_etc_hostname(probe_root); |
| 314 | } else { |
| 315 | char hname[_POSIX_HOST_NAME_MAX+1] = {0}; |
| 316 | if (gethostname(hname, _POSIX_HOST_NAME_MAX)) { |
| 317 | dW("Unable to get hostname: %s", strerror(errno)); |
| 318 | } else { |
| 319 | hostname = strdup(hname); |
| 320 | } |
| 321 | |
| 322 | if (hostname) { |
| 323 | struct addrinfo hints, *info, *p; |
| 324 | int gai_res; |
| 325 | |
| 326 | memset(&hints, 0, sizeof hints); |
| 327 | hints.ai_family = AF_UNSPEC; |
| 328 | hints.ai_socktype = SOCK_STREAM; |
| 329 | hints.ai_flags = AI_CANONNAME; |
| 330 | |
| 331 | if (!(gai_res = getaddrinfo(hostname, NULL, &hints, &info))) { |
| 332 | for(p = info; p != NULL; p = p->ai_next) { |
| 333 | if (!p->ai_canonname) |
| 334 | continue; |
| 335 | fact = xccdf_target_fact_new(); |
| 336 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:asset:identifier:fqdn"); |
| 337 | xccdf_target_fact_set_string(fact, p->ai_canonname); |
| 338 | /* store FQDN under XCCDF1.2 (6.6.3) predefined name */ |
| 339 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 340 | if (!fqdn) |
| 341 | fqdn = strdup(p->ai_canonname); |
| 342 | } |
| 343 | freeaddrinfo(info); |
| 344 | } else { |
| 345 | dI("Unable to get FQDN(s) via getaddrinfo: %s", gai_strerror(gai_res)); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | /* store target's host name */ |
| 350 | xccdf_result_add_target(result, fqdn ? fqdn : (hostname ? hostname : "unknown")); |
| 351 | if (hostname) { |
| 352 | fact = xccdf_target_fact_new(); |
| 353 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:asset:identifier:host_name"); |
| 354 | xccdf_target_fact_set_string(fact, hostname); |
| 355 | /* store host name under XCCDF1.2 (6.6.3) predefined name */ |
| 356 | xccdf_result_add_target_fact(result, fact); |
| 357 | } |
| 358 | free(hostname); |
| 359 | free(fqdn); |
| 360 | #elif defined(OS_WINDOWS) |
| 361 | TCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1]; |
| 362 | DWORD computer_name_size = MAX_COMPUTERNAME_LENGTH + 1; |
| 363 | GetComputerName(computer_name, &computer_name_size); |
| 364 | /* store target name */ |
| 365 | xccdf_result_add_target(result, computer_name); |
| 366 | |
| 367 | fact = xccdf_target_fact_new(); |
| 368 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:asset:identifier:host_name"); |
| 369 | xccdf_target_fact_set_string(fact, computer_name); |
| 370 | /* store host name under XCCDF1.2 (6.6.3) predefined name */ |
| 371 | xccdf_result_add_target_fact(result, fact); |
| 372 | #endif |
| 373 | |
| 374 | const char *ev_target = getenv("OSCAP_EVALUATION_TARGET"); |
| 375 | if (ev_target) { |
| 376 | fact = xccdf_target_fact_new(); |
| 377 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:identifier"); |
| 378 | xccdf_target_fact_set_string(fact, ev_target); |
| 379 | xccdf_result_add_target_fact(result, fact); |
| 380 | |
| 381 | fact = xccdf_target_fact_new(); |
| 382 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:asset:identifier:ein"); |
| 383 | xccdf_target_fact_set_string(fact, ev_target); |
| 384 | /* store target id under XCCDF1.2 (6.6.3) predefined name */ |
| 385 | xccdf_result_add_target_fact(result, fact); |
| 386 | } |
| 387 | |
| 388 | if (!probe_root) |
| 389 | _xccdf_result_fill_identity(result); |
| 390 | |
| 391 | #if defined(OS_LINUX) || defined(OS_FREEBSD) |
| 392 | if (!probe_root) { |
| 393 | /* get network interfaces */ |
| 394 | if (getifaddrs(&ifaddr) == -1) |
| 395 | return; |
| 396 | |
| 397 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 398 | if (fd == -1) |
| 399 | goto out1; |
| 400 | |
| 401 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 402 | int family; |
| 403 | char hostip[NI_MAXHOST]; |
| 404 | struct ifreq ifr; |
| 405 | |
| 406 | if (!ifa->ifa_addr) |
| 407 | continue; |
| 408 | family = ifa->ifa_addr->sa_family; |
| 409 | |
| 410 | if (family == AF_INET || family == AF_INET6) { |
| 411 | if (family == AF_INET) { |
| 412 | if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), |
| 413 | hostip, sizeof(hostip), NULL, 0, NI_NUMERICHOST)) |
| 414 | goto out2; |
| 415 | } else if (family == AF_INET6) { |
| 416 | struct sockaddr_in6 *sin6; |
| 417 | |
| 418 | sin6 = (struct sockaddr_in6 *) ifa->ifa_addr; |
| 419 | if (!inet_ntop(family, (const void *) &sin6->sin6_addr, |
| 420 | hostip, sizeof(hostip))) |
| 421 | goto out2; |
| 422 | } |
| 423 | /* store ip address */ |
| 424 | xccdf_result_add_target_address(result, hostip); |
| 425 | fact = xccdf_target_fact_new(); |
| 426 | xccdf_target_fact_set_name(fact, family == AF_INET ? "urn:xccdf:fact:asset:identifier:ipv4" : "urn:xccdf:fact:asset:identifier:ipv6"); |
| 427 | xccdf_target_fact_set_string(fact, hostip); |
| 428 | /* store ipv4(6) address under XCCDF1.2 (6.6.3) predefined name */ |
| 429 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 430 | } |
| 431 | |
| 432 | memset(&ifr, 0, sizeof(ifr)); |
| 433 | strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); |
| 434 | ifr.ifr_name[IFNAMSIZ - 1] = 0; |
| 435 | #if defined(OS_LINUX) |
| 436 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0) { |
| 437 | unsigned char mac[6]; |
| 438 | char macbuf[20]; |
| 439 | |
| 440 | memcpy(mac, ifr.ifr_hwaddr.sa_data, sizeof(mac)); |
| 441 | snprintf(macbuf, sizeof(macbuf), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 442 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 443 | #elif defined(OS_FREEBSD) |
| 444 | if (ioctl(fd, SIOCGHWADDR, &ifr) >= 0) { |
| 445 | unsigned char mac[6]; |
| 446 | char macbuf[20]; |
| 447 | |
| 448 | memcpy(mac, ifr.ifr_addr.sa_data, sizeof(mac)); |
| 449 | snprintf(macbuf, sizeof(macbuf), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 450 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 451 | #endif |
| 452 | fact = xccdf_target_fact_new(); |
| 453 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:ethernet:MAC"); |
| 454 | xccdf_target_fact_set_string(fact, macbuf); |
| 455 | /* store mac address */ |
| 456 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 457 | |
| 458 | fact = xccdf_target_fact_new(); |
| 459 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:asset:identifier:mac"); |
| 460 | xccdf_target_fact_set_string(fact, macbuf); |
| 461 | /* store mac address under XCCDF1.2 (6.6.3) predefined name */ |
| 462 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | out2: |
| 467 | close(fd); |
| 468 | out1: |
| 469 | freeifaddrs(ifaddr); |
| 470 | } |
| 471 | #elif defined(OS_WINDOWS) |
| 472 | |
| 473 | #define VERSION_LEN 32 |
| 474 | /* Microsoft recommends to start with a 15 kB buffer for GetAdaptersAddresses. */ |
| 475 | #define ADDRESSES_BUFFER_SIZE 15000 |
| 476 | #define MAX_TRIES 3 |
| 477 | #define MAX_IP_ADDRESS_STRING_LENGTH 128 |
| 478 | |
| 479 | /* |
| 480 | * Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h |
| 481 | * to define the Windows Socket specification version we will use. |
| 482 | * The current version of the Windows Sockets specification is version 2.2. |
| 483 | * Version 2.2 is supported on all systems since Windows 95 OSR2. |
| 484 | */ |
| 485 | WORD requested_version = MAKEWORD(2, 2); |
| 486 | |
| 487 | WSADATA wsa_data; |
| 488 | int err = WSAStartup(requested_version, &wsa_data); |
| 489 | if (err != 0) { |
| 490 | char *error_message = oscap_windows_error_message(err); |
| 491 | dE("Can't initialize Winsock DLL. WSAStartup failed: %s", error_message); |
| 492 | free(error_message); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | PIP_ADAPTER_ADDRESSES addresses = NULL; |
| 497 | ULONG ret_val = 0; |
| 498 | ULONG addresses_size = ADDRESSES_BUFFER_SIZE; |
| 499 | ULONG family = AF_UNSPEC; // both IPv4 and IPv6 |
| 500 | ULONG flags = GAA_FLAG_INCLUDE_PREFIX; |
| 501 | int iterations = 0; |
| 502 | |
| 503 | /* Try to get information about network adapters and their adresses */ |
| 504 | do { |
| 505 | addresses = malloc(addresses_size); |
| 506 | ret_val = GetAdaptersAddresses(family, flags, NULL, addresses, &addresses_size); |
| 507 | if (ret_val == ERROR_BUFFER_OVERFLOW) { |
| 508 | free(addresses); |
| 509 | addresses = NULL; |
| 510 | } |
| 511 | iterations++; |
| 512 | } while (ret_val == ERROR_BUFFER_OVERFLOW && iterations < MAX_TRIES); |
| 513 | |
| 514 | if (ret_val != NO_ERROR) { |
| 515 | char *error_message = oscap_windows_error_message(err); |
| 516 | dE("Can't get information about network adapters and their adresses. %s", error_message); |
| 517 | free(error_message); |
| 518 | free(addresses); |
| 519 | WSACleanup(); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | /* Iterate over all network adapters */ |
| 524 | PIP_ADAPTER_ADDRESSES current_address = addresses; |
| 525 | while (current_address != NULL) { |
| 526 | /* MAC address */ |
| 527 | char *mac_address_str = NULL; |
| 528 | if (current_address->PhysicalAddressLength != 0) { |
| 529 | /* n bytes of MAC address will be printed as: |
| 530 | (2 * n) hexa numbers + (n - 1) delimiters + 1 terminating null byte = 3 * n */ |
| 531 | const size_t mac_address_str_len = current_address->PhysicalAddressLength * 3; |
| 532 | mac_address_str = malloc(mac_address_str_len); |
| 533 | |
| 534 | for (unsigned i = 0; i < current_address->PhysicalAddressLength; i++) { |
| 535 | if (i == (current_address->PhysicalAddressLength - 1)) { |
| 536 | snprintf(mac_address_str + i * 3, mac_address_str_len, "%.2X", |
| 537 | (int)current_address->PhysicalAddress[i]); |
| 538 | } |
| 539 | else { |
| 540 | snprintf(mac_address_str + i * 3, mac_address_str_len, "%.2X-", |
| 541 | (int)current_address->PhysicalAddress[i]); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* Add the MAC address to XCCDF TestResult/target-facts */ |
| 546 | struct xccdf_target_fact *fact = xccdf_target_fact_new(); |
| 547 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:ethernet:MAC"); |
| 548 | xccdf_target_fact_set_string(fact, mac_address_str); |
| 549 | /* store mac address */ |
| 550 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 551 | |
| 552 | fact = xccdf_target_fact_new(); |
| 553 | xccdf_target_fact_set_name(fact, "urn:xccdf:fact:asset:identifier:mac"); |
| 554 | xccdf_target_fact_set_string(fact, mac_address_str); |
| 555 | /* store mac address under XCCDF1.2 (6.6.3) predefined name */ |
| 556 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 557 | |
| 558 | free(mac_address_str); |
| 559 | } |
| 560 | |
| 561 | /* Iterate over all unicast IP addresses of the network interface */ |
| 562 | PIP_ADAPTER_UNICAST_ADDRESS unicast_address = current_address->FirstUnicastAddress; |
| 563 | while (unicast_address != NULL) { |
| 564 | SOCKET_ADDRESS socket_address = unicast_address->Address; |
| 565 | WCHAR ip_address_wstr[MAX_IP_ADDRESS_STRING_LENGTH]; |
| 566 | DWORD ip_address_wstr_length = MAX_IP_ADDRESS_STRING_LENGTH; |
| 567 | int rc = WSAAddressToStringW(socket_address.lpSockaddr, socket_address.iSockaddrLength, NULL, ip_address_wstr, &ip_address_wstr_length); |
| 568 | if (rc != 0) { |
| 569 | free(addresses); |
| 570 | WSACleanup(); |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | /* Add the IP address to XCCDF TestResult/target-address */ |
| 575 | char *ip_address_str = oscap_windows_wstr_to_str(ip_address_wstr); |
| 576 | xccdf_result_add_target_address(result, ip_address_str); |
| 577 | |
| 578 | fact = xccdf_target_fact_new(); |
| 579 | xccdf_target_fact_set_name(fact, socket_address.lpSockaddr->sa_family == AF_INET ? "urn:xccdf:fact:asset:identifier:ipv4" : "urn:xccdf:fact:asset:identifier:ipv6"); |
| 580 | xccdf_target_fact_set_string(fact, ip_address_str); |
| 581 | /* store ipv4(6) address under XCCDF1.2 (6.6.3) predefined name */ |
| 582 | _xccdf_result_add_target_fact_uniq(result, fact); |
| 583 | |
| 584 | free(ip_address_str); |
| 585 | |
| 586 | unicast_address = unicast_address->Next; |
| 587 | } |
| 588 | current_address = current_address->Next; |
| 589 | } |
| 590 | |
| 591 | free(addresses); |
| 592 | WSACleanup(); |
| 593 | return 0; |
| 594 | |
| 595 | #endif |
| 596 | } |
| 597 | |
| 598 | struct xccdf_rule_result *xccdf_rule_result_new(void) |
| 599 | { |
| 600 | struct xccdf_rule_result *rr = calloc(1, sizeof(struct xccdf_rule_result)); |
| 601 | oscap_create_lists(&rr->overrides, &rr->idents, &rr->messages, |
| 602 | &rr->instances, &rr->fixes, &rr->checks, NULL); |
| 603 | return rr; |
| 604 | } |
| 605 | |
| 606 | void xccdf_rule_result_free(struct xccdf_rule_result *rr) |
| 607 | { |
| 608 | if (rr != NULL) { |
| 609 | free(rr->idref); |
| 610 | free(rr->version); |
| 611 | free(rr->time); |
| 612 | |
| 613 | oscap_list_free(rr->overrides, (oscap_destruct_func) xccdf_override_free); |
| 614 | oscap_list_free(rr->idents, (oscap_destruct_func) xccdf_ident_free); |
| 615 | oscap_list_free(rr->messages, (oscap_destruct_func) xccdf_message_free); |
| 616 | oscap_list_free(rr->instances, (oscap_destruct_func) xccdf_instance_free); |
| 617 | oscap_list_free(rr->fixes, (oscap_destruct_func) xccdf_fix_free); |
| 618 | oscap_list_free(rr->checks, (oscap_destruct_func) xccdf_check_free); |
| 619 | |
| 620 | free(rr); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | OSCAP_ACCESSOR_SIMPLE(xccdf_role_t, xccdf_rule_result, role) |
| 625 | OSCAP_ACCESSOR_SIMPLE(float, xccdf_rule_result, weight) |
| 626 | OSCAP_ACCESSOR_SIMPLE(xccdf_level_t, xccdf_rule_result, severity) |
| 627 | OSCAP_ACCESSOR_SIMPLE(xccdf_test_result_type_t, xccdf_rule_result, result) |
| 628 | OSCAP_ACCESSOR_STRING(xccdf_rule_result, time) |
| 629 | OSCAP_ACCESSOR_STRING(xccdf_rule_result, version) |
| 630 | OSCAP_ACCESSOR_STRING(xccdf_rule_result, idref) |
| 631 | OSCAP_IGETINS(xccdf_ident, xccdf_rule_result, idents, ident) |
| 632 | OSCAP_IGETINS(xccdf_fix, xccdf_rule_result, fixes, fix) |
| 633 | OSCAP_IGETINS(xccdf_check, xccdf_rule_result, checks, check) |
| 634 | OSCAP_IGETINS_GEN(xccdf_override, xccdf_rule_result, overrides, override) |
| 635 | OSCAP_ITERATOR_REMOVE_F(xccdf_override) |
| 636 | OSCAP_IGETINS_GEN(xccdf_message, xccdf_rule_result, messages, message) |
| 637 | OSCAP_ITERATOR_REMOVE_F(xccdf_message) |
| 638 | OSCAP_IGETINS_GEN(xccdf_instance, xccdf_rule_result, instances, instance) |
| 639 | OSCAP_ITERATOR_REMOVE_F(xccdf_instance) |
| 640 | OSCAP_ITERATOR_GEN(xccdf_rule_result) |
| 641 | OSCAP_ITERATOR_REMOVE_F(xccdf_rule_result) |
| 642 | |
| 643 | |
| 644 | struct xccdf_identity *xccdf_identity_new(void) |
| 645 | { |
| 646 | return calloc(1, sizeof(struct xccdf_identity)); |
| 647 | } |
| 648 | |
| 649 | void xccdf_identity_free(struct xccdf_identity *identity) |
| 650 | { |
| 651 | if (identity != NULL) { |
| 652 | free(identity->name); |
| 653 | free(identity); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | OSCAP_ACCESSOR_EXP(bool, xccdf_identity, authenticated, sub.authenticated) |
| 658 | OSCAP_ACCESSOR_EXP(bool, xccdf_identity, privileged, sub.privileged) |
| 659 | OSCAP_ACCESSOR_STRING(xccdf_identity, name) |
| 660 | OSCAP_ITERATOR_GEN(xccdf_identity) |
| 661 | OSCAP_ITERATOR_REMOVE_F(xccdf_identity) |
| 662 | |
| 663 | struct xccdf_score *xccdf_score_new(void) |
| 664 | { |
| 665 | struct xccdf_score *score = calloc(1, sizeof(struct xccdf_score)); |
| 666 | score->score = NAN; |
| 667 | score->maximum = XCCDF_SCORE_MAX_DAFAULT; |
| 668 | return score; |
| 669 | } |
| 670 | |
| 671 | void xccdf_score_free(struct xccdf_score *score) |
| 672 | { |
| 673 | if (score != NULL) { |
| 674 | free(score->system); |
| 675 | free(score); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | OSCAP_ACCESSOR_SIMPLE(xccdf_numeric, xccdf_score, maximum) |
| 680 | OSCAP_ACCESSOR_SIMPLE(xccdf_numeric, xccdf_score, score) |
| 681 | OSCAP_ACCESSOR_STRING(xccdf_score, system) |
| 682 | OSCAP_ITERATOR_GEN(xccdf_score) |
| 683 | OSCAP_ITERATOR_REMOVE_F(xccdf_score) |
| 684 | |
| 685 | struct xccdf_override *xccdf_override_new(void) |
| 686 | { |
| 687 | return calloc(1, sizeof(struct xccdf_override)); |
| 688 | } |
| 689 | |
| 690 | void xccdf_override_free(struct xccdf_override *oride) |
| 691 | { |
| 692 | if (oride != NULL) { |
| 693 | free(oride->authority); |
| 694 | oscap_text_free(oride->remark); |
| 695 | free(oride); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | OSCAP_ACCESSOR_SIMPLE(xccdf_test_result_type_t, xccdf_override, new_result) |
| 700 | OSCAP_ACCESSOR_SIMPLE(xccdf_test_result_type_t, xccdf_override, old_result) |
| 701 | OSCAP_ACCESSOR_STRING(xccdf_override, time) |
| 702 | OSCAP_ACCESSOR_STRING(xccdf_override, authority) |
| 703 | OSCAP_ACCESSOR_TEXT(xccdf_override, remark) |
| 704 | |
| 705 | struct xccdf_message *xccdf_message_new(void) |
| 706 | { |
| 707 | return calloc(1, sizeof(struct xccdf_message)); |
| 708 | } |
| 709 | |
| 710 | void xccdf_message_free(struct xccdf_message *msg) |
| 711 | { |
| 712 | if (msg != NULL) { |
| 713 | free(msg->content); |
| 714 | free(msg); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | bool xccdf_message_set_content(struct xccdf_message *obj, const char *newval){ |
| 719 | // in debug mode, we handle all new messages in case where the xccdf_message is never displayed |
| 720 | dD("XCCDF_SET_MESSAGE: %s", newval); |
| 721 | free(obj->content); |
| 722 | obj->content=oscap_strdup(newval); |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | OSCAP_ACCESSOR_SIMPLE(xccdf_message_severity_t, xccdf_message, severity) |
| 727 | OSCAP_GETTER(const char*, xccdf_message, content) |
| 728 | |
| 729 | struct xccdf_target_fact* xccdf_target_fact_new(void) |
| 730 | { |
| 731 | return calloc(1, sizeof(struct xccdf_target_fact)); |
| 732 | } |
| 733 | |
| 734 | void xccdf_target_fact_free(struct xccdf_target_fact *fact) |
| 735 | { |
| 736 | if (fact != NULL) { |
| 737 | free(fact->name); |
| 738 | free(fact->value); |
| 739 | free(fact); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | static inline bool xccdf_target_fact_set_value(struct xccdf_target_fact *fact, xccdf_value_type_t type, const char *str) |
| 744 | { |
| 745 | assert(fact != NULL); |
| 746 | free(fact->value); |
| 747 | fact->value = oscap_strdup(str); |
| 748 | fact->type = type; |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | bool xccdf_target_fact_set_string(struct xccdf_target_fact *fact, const char *str) |
| 753 | { |
| 754 | return xccdf_target_fact_set_value(fact, XCCDF_TYPE_STRING, str); |
| 755 | } |
| 756 | |
| 757 | bool xccdf_target_fact_set_number(struct xccdf_target_fact *fact, xccdf_numeric val) |
| 758 | { |
| 759 | char buff[XCCDF_NUMERIC_SIZE]; |
| 760 | sprintf(buff, XCCDF_NUMERIC_FORMAT, val); |
| 761 | return xccdf_target_fact_set_value(fact, XCCDF_TYPE_NUMBER, buff); |
| 762 | } |
| 763 | |
| 764 | bool xccdf_target_fact_set_boolean(struct xccdf_target_fact *fact, bool val) |
| 765 | { |
| 766 | return xccdf_target_fact_set_value(fact, XCCDF_TYPE_BOOLEAN, oscap_enum_to_string(OSCAP_BOOL_MAP, val)); |
| 767 | } |
| 768 | |
| 769 | OSCAP_GETTER(xccdf_value_type_t, xccdf_target_fact, type) |
| 770 | OSCAP_GETTER(const char *, xccdf_target_fact, value) |
| 771 | OSCAP_ACCESSOR_STRING(xccdf_target_fact, name) |
| 772 | OSCAP_ITERATOR_GEN(xccdf_target_fact) |
| 773 | OSCAP_ITERATOR_REMOVE_F(xccdf_target_fact) |
| 774 | |
| 775 | struct xccdf_target_identifier* xccdf_target_identifier_new(void) |
| 776 | { |
| 777 | return calloc(1, sizeof(struct xccdf_target_identifier)); |
| 778 | } |
| 779 | |
| 780 | struct xccdf_target_identifier* xccdf_target_identifier_clone(const struct xccdf_target_identifier* ti) |
| 781 | { |
| 782 | struct xccdf_target_identifier* ret = xccdf_target_identifier_new(); |
| 783 | |
| 784 | if (xccdf_target_identifier_get_xml_node(ti)) { |
| 785 | xccdf_target_identifier_set_xml_node(ret, xccdf_target_identifier_get_xml_node(ti)); |
| 786 | } |
| 787 | else { |
| 788 | xccdf_target_identifier_set_system(ret, xccdf_target_identifier_get_system(ti)); |
| 789 | xccdf_target_identifier_set_name(ret, xccdf_target_identifier_get_name(ti)); |
| 790 | xccdf_target_identifier_set_href(ret, xccdf_target_identifier_get_href(ti)); |
| 791 | } |
| 792 | |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | void xccdf_target_identifier_free(struct xccdf_target_identifier *ti) |
| 797 | { |
| 798 | if (ti != NULL) { |
| 799 | if (ti->any_element) { |
| 800 | xmlFreeNode(ti->element); |
| 801 | } |
| 802 | else { |
| 803 | free(ti->system); |
| 804 | free(ti->href); |
| 805 | free(ti->name); |
| 806 | } |
| 807 | |
| 808 | free(ti); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | bool xccdf_target_identifier_set_xml_node(struct xccdf_target_identifier *ti, void* node) |
| 813 | { |
| 814 | if (!ti->any_element) { |
| 815 | free(ti->system); |
| 816 | free(ti->href); |
| 817 | free(ti->name); |
| 818 | } |
| 819 | else if (ti->element) |
| 820 | xmlFreeNode(ti->element); |
| 821 | |
| 822 | ti->any_element = true; |
| 823 | ti->element = (xmlNodePtr)node; |
| 824 | |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | void* xccdf_target_identifier_get_xml_node(const struct xccdf_target_identifier* ti) |
| 829 | { |
| 830 | if (!ti->any_element) |
| 831 | return NULL; |
| 832 | |
| 833 | return ti->element; |
| 834 | } |
| 835 | |
| 836 | bool xccdf_target_identifier_set_system(struct xccdf_target_identifier *ti, const char *newval) |
| 837 | { |
| 838 | if (ti->any_element) { |
| 839 | if (ti->element) |
| 840 | xmlFreeNode(ti->element); |
| 841 | } |
| 842 | |
| 843 | ti->any_element = false; |
| 844 | free(ti->system); |
| 845 | ti->system = oscap_strdup(newval); |
| 846 | |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | const char* xccdf_target_identifier_get_system(const struct xccdf_target_identifier* ti) |
| 851 | { |
| 852 | if (ti->any_element) |
| 853 | return NULL; |
| 854 | |
| 855 | return ti->system; |
| 856 | } |
| 857 | |
| 858 | bool xccdf_target_identifier_set_href(struct xccdf_target_identifier *ti, const char *newval) |
| 859 | { |
| 860 | if (ti->any_element) { |
| 861 | if (ti->element) |
| 862 | xmlFreeNode(ti->element); |
| 863 | } |
| 864 | |
| 865 | ti->any_element = false; |
| 866 | free(ti->href); |
| 867 | ti->href = oscap_strdup(newval); |
| 868 | |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | const char* xccdf_target_identifier_get_href(const struct xccdf_target_identifier* ti) |
| 873 | { |
| 874 | if (ti->any_element) |
| 875 | return NULL; |
| 876 | |
| 877 | return ti->href; |
| 878 | } |
| 879 | |
| 880 | bool xccdf_target_identifier_set_name(struct xccdf_target_identifier *ti, const char *newval) |
| 881 | { |
| 882 | if (ti->any_element) { |
| 883 | if (ti->element) |
| 884 | xmlFreeNode(ti->element); |
| 885 | } |
| 886 | |
| 887 | ti->any_element = false; |
| 888 | free(ti->name); |
| 889 | ti->name = oscap_strdup(newval); |
| 890 | |
| 891 | return true; |
| 892 | } |
| 893 | |
| 894 | const char* xccdf_target_identifier_get_name(const struct xccdf_target_identifier* ti) |
| 895 | { |
| 896 | if (ti->any_element) |
| 897 | return NULL; |
| 898 | |
| 899 | return ti->name; |
| 900 | } |
| 901 | |
| 902 | OSCAP_ITERATOR_GEN(xccdf_target_identifier) |
| 903 | OSCAP_ITERATOR_REMOVE_F(xccdf_target_identifier) |
| 904 | |
| 905 | struct xccdf_instance *xccdf_instance_new(void) |
| 906 | { |
| 907 | struct xccdf_instance *inst = calloc(1, sizeof(struct xccdf_instance)); |
| 908 | inst->context = oscap_strdup(XCCDF_INSTANCE_DEFAULT_CONTEXT); |
| 909 | return inst; |
| 910 | } |
| 911 | |
| 912 | void xccdf_instance_free(struct xccdf_instance *inst) |
| 913 | { |
| 914 | if (inst != NULL) { |
| 915 | free(inst->context); |
| 916 | free(inst->parent_context); |
| 917 | free(inst->content); |
| 918 | free(inst); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | OSCAP_ACCESSOR_STRING(xccdf_instance, context) |
| 923 | OSCAP_ACCESSOR_STRING(xccdf_instance, parent_context) |
| 924 | OSCAP_ACCESSOR_STRING(xccdf_instance, content) |
| 925 | |
| 926 | /* ------ string maps ------ */ |
| 927 | |
| 928 | const struct oscap_string_map XCCDF_FACT_TYPE_MAP[] = { |
| 929 | { XCCDF_TYPE_BOOLEAN, "boolean" }, |
| 930 | { XCCDF_TYPE_STRING, "string" }, |
| 931 | { XCCDF_TYPE_NUMBER, "number" }, |
| 932 | { XCCDF_TYPE_BOOLEAN, NULL } |
| 933 | }; |
| 934 | |
| 935 | const struct oscap_string_map XCCDF_RESULT_MAP[] = { |
| 936 | { XCCDF_RESULT_PASS, "pass" }, |
| 937 | { XCCDF_RESULT_FAIL, "fail" }, |
| 938 | { XCCDF_RESULT_ERROR, "error" }, |
| 939 | { XCCDF_RESULT_UNKNOWN, "unknown" }, |
| 940 | { XCCDF_RESULT_NOT_APPLICABLE, "notapplicable" }, |
| 941 | { XCCDF_RESULT_NOT_CHECKED, "notchecked" }, |
| 942 | { XCCDF_RESULT_NOT_SELECTED, "notselected" }, |
| 943 | { XCCDF_RESULT_INFORMATIONAL, "informational" }, |
| 944 | { XCCDF_RESULT_FIXED, "fixed" }, |
| 945 | { 0, NULL } |
| 946 | }; |
| 947 | |
| 948 | /* --------- import -------- */ |
| 949 | |
| 950 | static struct xccdf_identity *xccdf_identity_new_parse(xmlTextReaderPtr reader); |
| 951 | static struct xccdf_target_fact *xccdf_target_fact_new_parse(xmlTextReaderPtr reader); |
| 952 | static struct xccdf_target_identifier *xccdf_target_identifier_new_parse(xmlTextReaderPtr reader); |
| 953 | static struct xccdf_score *xccdf_score_new_parse(xmlTextReaderPtr reader); |
| 954 | static struct xccdf_rule_result *xccdf_rule_result_new_parse(xmlTextReaderPtr reader); |
| 955 | static struct xccdf_override *xccdf_override_new_parse(xmlTextReaderPtr reader); |
| 956 | static struct xccdf_message *xccdf_message_new_parse(xmlTextReaderPtr reader); |
| 957 | static struct xccdf_instance *xccdf_instance_new_parse(xmlTextReaderPtr reader); |
| 958 | |
| 959 | struct xccdf_result *xccdf_result_import_source(struct oscap_source *source) |
| 960 | { |
| 961 | xmlTextReader *reader = oscap_source_get_xmlTextReader(source); |
| 962 | if (reader == NULL) { |
| 963 | return NULL; |
| 964 | } |
| 965 | while (xmlTextReaderRead(reader) == 1 |
| 966 | && xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT); |
| 967 | struct xccdf_result *result = xccdf_result_new_parse(reader); |
| 968 | xmlFreeTextReader(reader); |
| 969 | return result; |
| 970 | } |
| 971 | |
| 972 | struct xccdf_result *xccdf_result_new_parse(xmlTextReaderPtr reader) |
| 973 | { |
| 974 | assert(reader != NULL); |
| 975 | if (xccdf_element_get(reader) != XCCDFE_TESTRESULT) { |
| 976 | oscap_seterr(OSCAP_EFAMILY_XCCDF, "Expected 'TestResult' element while found '%s'.", |
| 977 | xmlTextReaderConstLocalName(reader)); |
| 978 | return NULL; |
| 979 | } |
| 980 | |
| 981 | struct xccdf_item *res = XITEM(xccdf_result_new()); |
| 982 | xccdf_result_set_schema_version(XRESULT(res), xccdf_detect_version_parser(reader)); |
| 983 | |
| 984 | if (!xccdf_item_process_attributes(res, reader)) |
| 985 | goto fail; |
| 986 | |
| 987 | /* end time attribute is mandatory */ |
| 988 | if (!xccdf_attribute_has(reader, XCCDFA_END_TIME)) { |
| 989 | oscap_seterr(OSCAP_EFAMILY_XCCDF, "Missing end-time attribute in TestResult element: %s", xccdf_item_get_id(res)); |
| 990 | goto fail; |
| 991 | } |
| 992 | res->sub.result.end_time = xccdf_attribute_copy(reader, XCCDFA_END_TIME); |
| 993 | res->sub.result.start_time = xccdf_attribute_copy(reader, XCCDFA_START_TIME); |
| 994 | |
| 995 | res->item.version = xccdf_attribute_copy(reader, XCCDFA_VERSION); |
| 996 | res->sub.result.test_system = xccdf_attribute_copy(reader, XCCDFA_TEST_SYSTEM); |
| 997 | |
| 998 | int depth = oscap_element_depth(reader) + 1; |
| 999 | |
| 1000 | while (oscap_to_start_element(reader, depth)) { |
| 1001 | switch (xccdf_element_get(reader)) { |
| 1002 | case XCCDFE_ORGANIZATION: |
| 1003 | oscap_list_add(res->sub.result.organizations, oscap_element_string_copy(reader)); |
| 1004 | break; |
| 1005 | case XCCDFE_IDENTITY: |
| 1006 | oscap_list_add(res->sub.result.identities, xccdf_identity_new_parse(reader)); |
| 1007 | break; |
| 1008 | case XCCDFE_RESULT_PROFILE: |
| 1009 | if (res->sub.result.profile == NULL) |
| 1010 | res->sub.result.profile = xccdf_attribute_copy(reader, XCCDFA_IDREF); |
| 1011 | break; |
| 1012 | case XCCDFE_TARGET: |
| 1013 | oscap_list_add(res->sub.result.targets, oscap_element_string_copy(reader)); |
| 1014 | break; |
| 1015 | case XCCDFE_TARGET_ADDRESS: |
| 1016 | oscap_list_add(res->sub.result.target_addresses, oscap_element_string_copy(reader)); |
| 1017 | break; |
| 1018 | case XCCDFE_TARGET_FACTS: |
| 1019 | // we have to read inside the target-facts element |
| 1020 | xmlTextReaderRead(reader); |
| 1021 | |
| 1022 | // iterate over all inner elements of the target-facts element |
| 1023 | while (oscap_to_start_element(reader, depth + 1)) { |
| 1024 | if (xccdf_element_get(reader) == XCCDFE_FACT) |
| 1025 | oscap_list_add(res->sub.result.target_facts, xccdf_target_fact_new_parse(reader)); |
| 1026 | xmlTextReaderRead(reader); |
| 1027 | } |
| 1028 | break; |
| 1029 | case XCCDFE_TARGET_IDENTIFIER: |
| 1030 | oscap_list_add(res->sub.result.target_id_refs, xccdf_target_identifier_new_parse(reader)); |
| 1031 | break; |
| 1032 | case XCCDFE_SET_VALUE: |
| 1033 | oscap_list_add(res->sub.result.setvalues, xccdf_setvalue_new_parse(reader)); |
| 1034 | break; |
| 1035 | case XCCDFE_RULE_RESULT: |
| 1036 | oscap_list_add(res->sub.result.rule_results, xccdf_rule_result_new_parse(reader)); |
| 1037 | break; |
| 1038 | case XCCDFE_SCORE: |
| 1039 | oscap_list_add(res->sub.result.scores, xccdf_score_new_parse(reader)); |
| 1040 | break; |
| 1041 | case XCCDFE_RESULT_BENCHMARK: |
| 1042 | if (res->sub.result.benchmark_uri == NULL) |
| 1043 | res->sub.result.benchmark_uri = xccdf_attribute_copy(reader, XCCDFA_HREF); |
| 1044 | break; |
| 1045 | default: |
| 1046 | if (!xccdf_item_process_element(res, reader)) |
| 1047 | dW("Encountered an unknown element '%s' while parsing XCCDF result.", |
| 1048 | xmlTextReaderConstLocalName(reader)); |
| 1049 | break; |
| 1050 | |
| 1051 | // TODO: any element from other namespace is supposed to go into xccdf_target_identifier |
| 1052 | } |
| 1053 | xmlTextReaderRead(reader); |
| 1054 | } |
| 1055 | |
| 1056 | return XRESULT(res); |
| 1057 | |
| 1058 | fail: |
| 1059 | xccdf_result_free(XRESULT(res)); |
| 1060 | return NULL; |
| 1061 | } |
| 1062 | |
| 1063 | struct oscap_source *xccdf_result_export_source(struct xccdf_result *result, const char *filepath) |
| 1064 | { |
| 1065 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
| 1066 | if (doc == NULL) { |
| 1067 | oscap_setxmlerr(xmlGetLastError()); |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | |
| 1071 | xccdf_result_to_dom(result, NULL, doc, NULL, false); |
| 1072 | return oscap_source_new_from_xmlDoc(doc, filepath); |
| 1073 | } |
| 1074 | |
| 1075 | struct oscap_source *xccdf_result_stig_viewer_export_source(struct xccdf_result *result, const char *filepath) |
| 1076 | { |
| 1077 | xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); |
| 1078 | if (doc == NULL) { |
| 1079 | oscap_setxmlerr(xmlGetLastError()); |
| 1080 | return NULL; |
| 1081 | } |
| 1082 | |
| 1083 | xccdf_result_to_dom(result, NULL, doc, NULL, true); |
| 1084 | return oscap_source_new_from_xmlDoc(doc, filepath); |
| 1085 | } |
| 1086 | |
| 1087 | void xccdf_result_to_dom(struct xccdf_result *result, xmlNode *result_node, xmlDoc *doc, xmlNode *parent, bool use_stig_rule_id) |
| 1088 | { |
| 1089 | xmlNs *ns_xccdf = NULL; |
| 1090 | struct xccdf_benchmark *associated_benchmark = xccdf_result_get_benchmark(result); |
| 1091 | const char *benchmark_ref_uri = xccdf_result_get_benchmark_uri(result); |
| 1092 | const struct xccdf_version_info* version_info = xccdf_item_get_schema_version(XITEM(result)); |
| 1093 | if (parent) { |
| 1094 | ns_xccdf = lookup_xccdf_ns(doc, parent, version_info); |
| 1095 | } else { |
| 1096 | if (!result_node) result_node = xccdf_item_to_dom(XITEM(result), doc, NULL, version_info); |
| 1097 | ns_xccdf = xmlNewNs(result_node, |
| 1098 | (const xmlChar*)xccdf_version_info_get_namespace_uri(version_info), NULL); |
| 1099 | xmlDocSetRootElement(doc, result_node); |
| 1100 | |
| 1101 | // TestResult is the root element, we have to provide reference to |
| 1102 | // the benchmark that was the source of this test result |
| 1103 | |
| 1104 | if (benchmark_ref_uri == NULL) { |
| 1105 | oscap_seterr(OSCAP_EFAMILY_XCCDF, |
| 1106 | "Exporting TestResult as root element with no benchmark URI available. " |
| 1107 | "Specification requires us to provide a reference to the Benchmark in this " |
| 1108 | "case, this will result in XML file that won't be valid!"); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | if (benchmark_ref_uri) { |
| 1113 | // We need to prepend this previous to the "title" element |
| 1114 | // to satisfy the specification. |
| 1115 | |
| 1116 | xmlNodePtr title_node = NULL; |
| 1117 | xmlNodePtr title_node_candidate = result_node->children; |
| 1118 | while (title_node_candidate) { |
| 1119 | if (title_node_candidate->type == XML_ELEMENT_NODE && |
| 1120 | strcmp((const char*)title_node_candidate->name, "title") == 0) { |
| 1121 | title_node = title_node_candidate; |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | title_node_candidate = title_node_candidate->next; |
| 1126 | } |
| 1127 | |
| 1128 | if (title_node == NULL) { |
| 1129 | oscap_seterr(OSCAP_EFAMILY_XCCDF, |
| 1130 | "Can't find title node in the exported TestResult. This is most likely " |
| 1131 | "an internal issue with openscap, please report the bug!"); |
| 1132 | } |
| 1133 | else { |
| 1134 | xmlNodePtr benchmark_ref = xmlNewNode(ns_xccdf, BAD_CAST "benchmark"); |
| 1135 | xmlNewProp(benchmark_ref, BAD_CAST "href", BAD_CAST benchmark_ref_uri); |
| 1136 | |
| 1137 | // @id is disallowed in XCCDF 1.1 and optional in XCCDF 1.2 |
| 1138 | if (xccdf_version_cmp(xccdf_item_get_schema_version(XITEM(result)), "1.2") >= 0 && associated_benchmark) { |
| 1139 | xmlNewProp(benchmark_ref, BAD_CAST "id", BAD_CAST xccdf_benchmark_get_id(associated_benchmark)); |
| 1140 | } |
| 1141 | |
| 1142 | xmlAddPrevSibling(title_node, benchmark_ref); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* Handle attributes */ |
| 1147 | const char * start = xccdf_result_get_start_time(result); |
| 1148 | if (start) { |
| 1149 | xmlNewProp(result_node, BAD_CAST "start-time", BAD_CAST start); |
| 1150 | } |
| 1151 | xmlNewProp(result_node, BAD_CAST "end-time", BAD_CAST xccdf_result_get_end_time(result)); |
| 1152 | const char *version = xccdf_result_get_version(result); |
| 1153 | if (version != NULL) |
| 1154 | xmlNewProp(result_node, BAD_CAST "version", BAD_CAST version); |
| 1155 | const char *test_system = xccdf_result_get_test_system(result); |
| 1156 | if (test_system != NULL) { |
| 1157 | xmlNewProp(result_node, BAD_CAST "test-system", BAD_CAST test_system); |
| 1158 | } |
| 1159 | |
| 1160 | /* Handle children */ |
| 1161 | xccdf_texts_to_dom(xccdf_result_get_remarks(result), result_node, "remark"); |
| 1162 | |
| 1163 | struct oscap_string_iterator *orgs = xccdf_result_get_organizations(result); |
| 1164 | while (oscap_string_iterator_has_more(orgs)) { |
| 1165 | const char *org = oscap_string_iterator_next(orgs); |
| 1166 | xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "organization", BAD_CAST org); |
| 1167 | } |
| 1168 | oscap_string_iterator_free(orgs); |
| 1169 | |
| 1170 | struct xccdf_identity_iterator *identities = xccdf_result_get_identities(result); |
| 1171 | while (xccdf_identity_iterator_has_more(identities)) { |
| 1172 | struct xccdf_identity *identity = xccdf_identity_iterator_next(identities); |
| 1173 | xmlNode *identity_node = xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "identity", BAD_CAST xccdf_identity_get_name(identity)); |
| 1174 | |
| 1175 | if (xccdf_identity_get_authenticated(identity)) |
| 1176 | xmlNewProp(identity_node, BAD_CAST "authenticated", BAD_CAST "true"); |
| 1177 | else |
| 1178 | xmlNewProp(identity_node, BAD_CAST "authenticated", BAD_CAST "false"); |
| 1179 | |
| 1180 | if (xccdf_identity_get_privileged(identity)) |
| 1181 | xmlNewProp(identity_node, BAD_CAST "privileged", BAD_CAST "true"); |
| 1182 | else |
| 1183 | xmlNewProp(identity_node, BAD_CAST "privileged", BAD_CAST "false"); |
| 1184 | } |
| 1185 | xccdf_identity_iterator_free(identities); |
| 1186 | |
| 1187 | const char *profile = xccdf_result_get_profile(result); |
| 1188 | if (profile) { |
| 1189 | xmlNode *prof_node = xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "profile", NULL); |
| 1190 | xmlNewProp(prof_node, BAD_CAST "idref", BAD_CAST profile); |
| 1191 | } |
| 1192 | |
| 1193 | struct oscap_string_iterator *targets = xccdf_result_get_targets(result); |
| 1194 | while (oscap_string_iterator_has_more(targets)) { |
| 1195 | const char *target = oscap_string_iterator_next(targets); |
| 1196 | xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "target", BAD_CAST target); |
| 1197 | } |
| 1198 | oscap_string_iterator_free(targets); |
| 1199 | |
| 1200 | struct oscap_string_iterator *target_addresses = xccdf_result_get_target_addresses(result); |
| 1201 | while (oscap_string_iterator_has_more(target_addresses)) { |
| 1202 | const char *target_address = oscap_string_iterator_next(target_addresses); |
| 1203 | if (strchr(target_address, ':') != NULL) { // a very simplistic check for IPv6 |
| 1204 | char *expanded_ipv6 = oscap_expand_ipv6(target_address); |
| 1205 | // Not all but a lot of datastream or even XCCDF test result schematrons |
| 1206 | // require a fully expanded IPv6. It doesn't hurt anything to expand it |
| 1207 | // here. All tools should be able to consume both shorthand and |
| 1208 | // expanded IPv6 addresses. |
| 1209 | |
| 1210 | xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "target-address", BAD_CAST expanded_ipv6); |
| 1211 | free(expanded_ipv6); |
| 1212 | } |
| 1213 | else { |
| 1214 | xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "target-address", BAD_CAST target_address); |
| 1215 | } |
| 1216 | } |
| 1217 | oscap_string_iterator_free(target_addresses); |
| 1218 | |
| 1219 | // platform |
| 1220 | |
| 1221 | struct xccdf_target_fact_iterator *target_facts = xccdf_result_get_target_facts(result); |
| 1222 | if (xccdf_target_fact_iterator_has_more(target_facts)) { |
| 1223 | xmlNode *target_node = xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "target-facts", NULL); |
| 1224 | while (xccdf_target_fact_iterator_has_more(target_facts)) { |
| 1225 | struct xccdf_target_fact *target_fact = xccdf_target_fact_iterator_next(target_facts); |
| 1226 | xmlNode *fact_node = xmlNewTextChild(target_node, ns_xccdf, BAD_CAST "fact", BAD_CAST target_fact->value); |
| 1227 | |
| 1228 | const char *name = xccdf_target_fact_get_name(target_fact); |
| 1229 | if (name) |
| 1230 | xmlNewProp(fact_node, BAD_CAST "name", BAD_CAST name); |
| 1231 | |
| 1232 | xccdf_value_type_t value = xccdf_target_fact_get_type(target_fact); |
| 1233 | xmlNewProp(fact_node, BAD_CAST "type", BAD_CAST XCCDF_FACT_TYPE_MAP[value - 1].string); |
| 1234 | } |
| 1235 | } |
| 1236 | xccdf_target_fact_iterator_free(target_facts); |
| 1237 | |
| 1238 | struct xccdf_target_identifier_iterator *target_id_refs = xccdf_result_get_target_id_refs(result); |
| 1239 | while (xccdf_target_identifier_iterator_has_more(target_id_refs)) { |
| 1240 | struct xccdf_target_identifier *target_identifier = xccdf_target_identifier_iterator_next(target_id_refs); |
| 1241 | xccdf_target_identifier_to_dom(target_identifier, doc, result_node, version_info); |
| 1242 | } |
| 1243 | xccdf_target_identifier_iterator_free(target_id_refs); |
| 1244 | |
| 1245 | struct oscap_string_iterator *applicable_platforms = xccdf_result_get_applicable_platforms(result); |
| 1246 | while (oscap_string_iterator_has_more(applicable_platforms)) { |
| 1247 | const char *platform = oscap_string_iterator_next(applicable_platforms); |
| 1248 | xmlNode *platform_element = xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "platform", NULL); |
| 1249 | xmlNewProp(platform_element, BAD_CAST "idref", BAD_CAST platform); |
| 1250 | } |
| 1251 | oscap_string_iterator_free(applicable_platforms); |
| 1252 | |
| 1253 | struct xccdf_setvalue_iterator *setvalues = xccdf_result_get_setvalues(result); |
| 1254 | while (xccdf_setvalue_iterator_has_more(setvalues)) { |
| 1255 | struct xccdf_setvalue *setvalue = xccdf_setvalue_iterator_next(setvalues); |
| 1256 | xccdf_setvalue_to_dom(setvalue, doc, result_node, version_info); |
| 1257 | } |
| 1258 | xccdf_setvalue_iterator_free(setvalues); |
| 1259 | |
| 1260 | struct oscap_htable *nodes_by_rule_id = oscap_htable_new(); |
| 1261 | |
| 1262 | struct xccdf_rule_result_iterator *rule_results = xccdf_result_get_rule_results(result); |
| 1263 | if (use_stig_rule_id) { |
| 1264 | while (xccdf_rule_result_iterator_has_more(rule_results)) { |
| 1265 | struct xccdf_rule_result *rule_result = xccdf_rule_result_iterator_next(rule_results); |
| 1266 | |
| 1267 | const char *idref = xccdf_rule_result_get_idref(rule_result); |
| 1268 | if (!idref) |
| 1269 | continue; |
| 1270 | |
| 1271 | xccdf_test_result_type_t test_res = xccdf_rule_result_get_result(rule_result); |
| 1272 | |
| 1273 | const struct xccdf_item *item = xccdf_benchmark_get_member(associated_benchmark, XCCDF_RULE, idref); |
| 1274 | if (!item) |
| 1275 | continue; |
| 1276 | |
| 1277 | struct oscap_reference_iterator *references = xccdf_item_get_references(item); |
| 1278 | while (oscap_reference_iterator_has_more(references)) { |
| 1279 | struct oscap_reference *ref = oscap_reference_iterator_next(references); |
| 1280 | const char *href = oscap_reference_get_href(ref); |
| 1281 | if (href && (strcmp(href, DISA_STIG_VIEWER_HREF[0]) == 0 || |
| 1282 | strcmp(href, DISA_STIG_VIEWER_HREF[1]) == 0 || |
| 1283 | strcmp(href, DISA_STIG_VIEWER_HREF[2]) == 0)) { |
| 1284 | const char *stig_rule_id = oscap_reference_get_title(ref); |
| 1285 | |
| 1286 | xccdf_test_result_type_t other_res = (xccdf_test_result_type_t)oscap_htable_detach(nodes_by_rule_id, stig_rule_id); |
| 1287 | xccdf_test_result_type_t wanted_res; |
| 1288 | if (other_res == 0) { |
| 1289 | wanted_res = test_res; |
| 1290 | } else { |
| 1291 | // if one test passed, and the other didn't, the other one should win |
| 1292 | if (test_res == XCCDF_RESULT_PASS) { |
| 1293 | wanted_res = other_res; |
| 1294 | } else if (other_res == XCCDF_RESULT_PASS) { |
| 1295 | wanted_res = test_res; |
| 1296 | // if one had an error, that should win |
| 1297 | } else if (test_res == XCCDF_RESULT_ERROR || other_res == XCCDF_RESULT_ERROR) { |
| 1298 | wanted_res = XCCDF_RESULT_ERROR; |
| 1299 | // next prio: failures |
| 1300 | } else if (test_res == XCCDF_RESULT_FAIL || other_res == XCCDF_RESULT_FAIL) { |
| 1301 | wanted_res = XCCDF_RESULT_FAIL; |
| 1302 | // next prio: unknown |
| 1303 | } else if (test_res == XCCDF_RESULT_UNKNOWN || other_res == XCCDF_RESULT_UNKNOWN) { |
| 1304 | wanted_res = XCCDF_RESULT_UNKNOWN; |
| 1305 | // otherwise, just pick the lower one (more or less arbitrarily) |
| 1306 | } else { |
| 1307 | wanted_res = (test_res < other_res) ? test_res : other_res; |
| 1308 | } |
| 1309 | } |
| 1310 | oscap_htable_add(nodes_by_rule_id, stig_rule_id, (void*)wanted_res); |
| 1311 | } |
| 1312 | } |
| 1313 | oscap_reference_iterator_free(references); |
| 1314 | } |
| 1315 | xccdf_rule_result_iterator_reset(rule_results); |
| 1316 | } |
| 1317 | while (xccdf_rule_result_iterator_has_more(rule_results)) { |
| 1318 | struct xccdf_rule_result *rule_result = xccdf_rule_result_iterator_next(rule_results); |
| 1319 | xccdf_rule_result_to_dom(rule_result, doc, result_node, version_info, associated_benchmark, use_stig_rule_id, nodes_by_rule_id); |
| 1320 | } |
| 1321 | xccdf_rule_result_iterator_free(rule_results); |
| 1322 | |
| 1323 | oscap_htable_free0(nodes_by_rule_id); |
| 1324 | |
| 1325 | struct xccdf_score_iterator *scores = xccdf_result_get_scores(result); |
| 1326 | while (xccdf_score_iterator_has_more(scores)) { |
| 1327 | struct xccdf_score *score = xccdf_score_iterator_next(scores); |
| 1328 | |
| 1329 | float value = xccdf_score_get_score(score); |
| 1330 | char *value_str = oscap_sprintf("%f", value); |
| 1331 | xmlNode *score_node = xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "score", BAD_CAST value_str); |
| 1332 | free(value_str); |
| 1333 | |
| 1334 | const char *sys = xccdf_score_get_system(score); |
| 1335 | if (sys) |
| 1336 | xmlNewProp(score_node, BAD_CAST "system", BAD_CAST sys); |
| 1337 | |
| 1338 | float max = xccdf_score_get_maximum(score); |
| 1339 | char *max_str = oscap_sprintf("%f", max); |
| 1340 | xmlNewProp(score_node, BAD_CAST "maximum", BAD_CAST max_str); |
| 1341 | free(max_str); |
| 1342 | } |
| 1343 | xccdf_score_iterator_free(scores); |
| 1344 | } |
| 1345 | |
| 1346 | static struct xccdf_identity *xccdf_identity_new_parse(xmlTextReaderPtr reader) |
| 1347 | { |
| 1348 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_IDENTITY); |
| 1349 | |
| 1350 | struct xccdf_identity *identity = xccdf_identity_new(); |
| 1351 | identity->sub.authenticated = xccdf_attribute_get_bool(reader, XCCDFA_AUTHENTICATED); |
| 1352 | identity->sub.privileged = xccdf_attribute_get_bool(reader, XCCDFA_PRIVILEDGED); |
| 1353 | identity->name = oscap_element_string_copy(reader); |
| 1354 | return identity; |
| 1355 | } |
| 1356 | |
| 1357 | static struct xccdf_target_fact *xccdf_target_fact_new_parse(xmlTextReaderPtr reader) |
| 1358 | { |
| 1359 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_FACT); |
| 1360 | |
| 1361 | struct xccdf_target_fact *fact = xccdf_target_fact_new(); |
| 1362 | fact->type = oscap_string_to_enum(XCCDF_FACT_TYPE_MAP, xccdf_attribute_get(reader, XCCDFA_TYPE)); |
| 1363 | fact->name = xccdf_attribute_copy(reader, XCCDFA_NAME); |
| 1364 | fact->value = oscap_element_string_copy(reader); |
| 1365 | return fact; |
| 1366 | } |
| 1367 | |
| 1368 | static struct xccdf_target_identifier *xccdf_target_identifier_new_parse(xmlTextReaderPtr reader) |
| 1369 | { |
| 1370 | struct xccdf_target_identifier *ret = xccdf_target_identifier_new(); |
| 1371 | |
| 1372 | if (xccdf_element_get(reader) == XCCDFE_TARGET_IDENTIFIER) { |
| 1373 | xccdf_target_identifier_set_system(ret, xccdf_attribute_get(reader, XCCDFA_SYSTEM)); |
| 1374 | xccdf_target_identifier_set_href(ret, xccdf_attribute_get(reader, XCCDFA_HREF)); |
| 1375 | xccdf_target_identifier_set_name(ret, xccdf_attribute_get(reader, XCCDFA_NAME)); |
| 1376 | } |
| 1377 | else { |
| 1378 | // this is OK because we clone the node in the setter |
| 1379 | xccdf_target_identifier_set_xml_node(ret, xmlTextReaderCurrentNode(reader)); |
| 1380 | } |
| 1381 | |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
| 1385 | static struct xccdf_score *xccdf_score_new_parse(xmlTextReaderPtr reader) |
| 1386 | { |
| 1387 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_SCORE); |
| 1388 | |
| 1389 | struct xccdf_score *score = xccdf_score_new(); |
| 1390 | score->system = xccdf_attribute_copy(reader, XCCDFA_SYSTEM); |
| 1391 | if (xccdf_attribute_has(reader, XCCDFA_MAXIMUM)) |
| 1392 | score->maximum = xccdf_attribute_get_float(reader, XCCDFA_MAXIMUM); |
| 1393 | else score->maximum = XCCDF_SCORE_MAX_DAFAULT; |
| 1394 | score->score = atof(oscap_element_string_get(reader)); |
| 1395 | return score; |
| 1396 | } |
| 1397 | |
| 1398 | static struct xccdf_rule_result *xccdf_rule_result_new_parse(xmlTextReaderPtr reader) |
| 1399 | { |
| 1400 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_RULE_RESULT); |
| 1401 | |
| 1402 | struct xccdf_rule_result *rr = xccdf_rule_result_new(); |
| 1403 | |
| 1404 | rr->idref = xccdf_attribute_copy(reader, XCCDFA_IDREF); |
| 1405 | rr->role = oscap_string_to_enum(XCCDF_ROLE_MAP, xccdf_attribute_get(reader, XCCDFA_ROLE)); |
| 1406 | rr->time = xccdf_attribute_copy(reader, XCCDFA_TIME); |
| 1407 | rr->version = xccdf_attribute_copy(reader, XCCDFA_VERSION); |
| 1408 | rr->weight = xccdf_attribute_get_float(reader, XCCDFA_WEIGHT); |
| 1409 | rr->severity = oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_SEVERITY)); |
| 1410 | |
| 1411 | int depth = oscap_element_depth(reader) + 1; |
| 1412 | |
| 1413 | while (oscap_to_start_element(reader, depth)) { |
| 1414 | switch (xccdf_element_get(reader)) { |
| 1415 | case XCCDFE_RESULT: |
| 1416 | rr->result = oscap_string_to_enum(XCCDF_RESULT_MAP, oscap_element_string_get(reader)); |
| 1417 | break; |
| 1418 | case XCCDFE_OVERRIDE: |
| 1419 | oscap_list_add(rr->overrides, xccdf_override_new_parse(reader)); |
| 1420 | break; |
| 1421 | case XCCDFE_IDENT: |
| 1422 | oscap_list_add(rr->idents, xccdf_ident_parse(reader)); |
| 1423 | break; |
| 1424 | case XCCDFE_MESSAGE: |
| 1425 | oscap_list_add(rr->messages, xccdf_message_new_parse(reader)); |
| 1426 | break; |
| 1427 | case XCCDFE_INSTANCE: |
| 1428 | oscap_list_add(rr->instances, xccdf_instance_new_parse(reader)); |
| 1429 | break; |
| 1430 | case XCCDFE_FIX: |
| 1431 | oscap_list_add(rr->fixes, xccdf_fix_parse(reader)); |
| 1432 | break; |
| 1433 | case XCCDFE_CHECK: |
| 1434 | oscap_list_add(rr->checks, xccdf_check_parse(reader)); |
| 1435 | break; |
| 1436 | default: break; |
| 1437 | } |
| 1438 | xmlTextReaderRead(reader); |
| 1439 | } |
| 1440 | |
| 1441 | return rr; |
| 1442 | } |
| 1443 | |
| 1444 | xmlNode *xccdf_target_identifier_to_dom(const struct xccdf_target_identifier *ti, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info) |
| 1445 | { |
| 1446 | if (ti->any_element) { |
| 1447 | // we have to copy since we are adding the node to an existing document |
| 1448 | // and that document will take ownership |
| 1449 | xmlNodePtr ret = xmlCopyNode(ti->element, 1); |
| 1450 | xmlAddChild(parent, ret); |
| 1451 | return ret; |
| 1452 | } |
| 1453 | else { |
| 1454 | xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info); |
| 1455 | |
| 1456 | xmlNode *target_idref_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "target-id-ref", NULL); |
| 1457 | |
| 1458 | /* Handle attributes */ |
| 1459 | const char *psystem = xccdf_target_identifier_get_system(ti); |
| 1460 | if (psystem) |
| 1461 | xmlNewProp(target_idref_node, BAD_CAST "system", BAD_CAST psystem); |
| 1462 | |
| 1463 | const char *href = xccdf_target_identifier_get_href(ti); |
| 1464 | if (href) |
| 1465 | xmlNewProp(target_idref_node, BAD_CAST "href", BAD_CAST href); |
| 1466 | |
| 1467 | const char *name = xccdf_target_identifier_get_name(ti); |
| 1468 | if (name) |
| 1469 | xmlNewProp(target_idref_node, BAD_CAST "name", BAD_CAST name); |
| 1470 | |
| 1471 | return target_idref_node; |
| 1472 | } |
| 1473 | } |
| 1474 | static void _xccdf_rule_result_to_dom_idref(struct xccdf_rule_result *result, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info, struct xccdf_benchmark *benchmark, const char *idref); |
| 1475 | void xccdf_rule_result_to_dom(struct xccdf_rule_result *result, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info, struct xccdf_benchmark *benchmark, bool use_stig_rule_id, struct oscap_htable *nodes_by_rule_id) |
| 1476 | { |
| 1477 | const char *idref = xccdf_rule_result_get_idref(result); |
| 1478 | if (use_stig_rule_id) { |
| 1479 | // Don't output rules with no stig ids |
| 1480 | if (!idref || !benchmark) |
| 1481 | return; |
| 1482 | |
| 1483 | const struct xccdf_item *item = xccdf_benchmark_get_member(benchmark, XCCDF_RULE, idref); |
| 1484 | if (!item) |
| 1485 | return; |
| 1486 | |
| 1487 | struct oscap_reference_iterator *references = xccdf_item_get_references(item); |
| 1488 | while (oscap_reference_iterator_has_more(references)) { |
| 1489 | struct oscap_reference *ref = oscap_reference_iterator_next(references); |
| 1490 | const char *href = oscap_reference_get_href(ref); |
| 1491 | if (href && (strcmp(href, DISA_STIG_VIEWER_HREF[0]) == 0 || |
| 1492 | strcmp(href, DISA_STIG_VIEWER_HREF[1]) == 0 || |
| 1493 | strcmp(href, DISA_STIG_VIEWER_HREF[2]) == 0)) { |
| 1494 | const char *stig_rule_id = oscap_reference_get_title(ref); |
| 1495 | |
| 1496 | xccdf_test_result_type_t expected_res = (xccdf_test_result_type_t)oscap_htable_get(nodes_by_rule_id, stig_rule_id); |
| 1497 | xccdf_test_result_type_t test_res = xccdf_rule_result_get_result(result); |
| 1498 | if (expected_res == test_res) { |
| 1499 | oscap_htable_detach(nodes_by_rule_id, stig_rule_id); |
| 1500 | _xccdf_rule_result_to_dom_idref(result, doc, parent, version_info, benchmark, stig_rule_id); |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | oscap_reference_iterator_free(references); |
| 1505 | } else { |
| 1506 | _xccdf_rule_result_to_dom_idref(result, doc, parent, version_info, benchmark, idref); |
| 1507 | } |
| 1508 | } |
| 1509 | static void _xccdf_rule_result_to_dom_idref(struct xccdf_rule_result *result, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info, struct xccdf_benchmark *benchmark, const char *idref) { |
| 1510 | xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info); |
| 1511 | |
| 1512 | xmlNode *result_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "rule-result", NULL); |
| 1513 | |
| 1514 | /* Handle attributes */ |
| 1515 | if (idref) |
| 1516 | xmlNewProp(result_node, BAD_CAST "idref", BAD_CAST idref); |
| 1517 | |
| 1518 | xccdf_role_t role = xccdf_rule_result_get_role(result); |
| 1519 | if (role != 0) |
| 1520 | xmlNewProp(result_node, BAD_CAST "role", BAD_CAST XCCDF_ROLE_MAP[role - 1].string); |
| 1521 | |
| 1522 | const char * tm = xccdf_rule_result_get_time(result); |
| 1523 | if (tm) { |
| 1524 | xmlNewProp(result_node, BAD_CAST "time", BAD_CAST tm); |
| 1525 | } |
| 1526 | |
| 1527 | xccdf_level_t severity = xccdf_rule_result_get_severity(result); |
| 1528 | if (severity != XCCDF_LEVEL_NOT_DEFINED) |
| 1529 | xmlNewProp(result_node, BAD_CAST "severity", BAD_CAST XCCDF_LEVEL_MAP[severity - 1].string); |
| 1530 | |
| 1531 | const char *version = xccdf_rule_result_get_version(result); |
| 1532 | if (version) |
| 1533 | xmlNewProp(result_node, BAD_CAST "version", BAD_CAST version); |
| 1534 | |
| 1535 | float weight = xccdf_rule_result_get_weight(result); |
| 1536 | char *weight_str = oscap_sprintf("%f", weight); |
| 1537 | xmlNewProp(result_node, BAD_CAST "weight", BAD_CAST weight_str); |
| 1538 | free(weight_str); |
| 1539 | |
| 1540 | /* Handle children */ |
| 1541 | xccdf_test_result_type_t test_res = xccdf_rule_result_get_result(result); |
| 1542 | if (test_res != 0) |
| 1543 | xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "result", BAD_CAST XCCDF_RESULT_MAP[test_res - 1].string); |
| 1544 | |
| 1545 | struct xccdf_override_iterator *overrides = xccdf_rule_result_get_overrides(result); |
| 1546 | while (xccdf_override_iterator_has_more(overrides)) { |
| 1547 | struct xccdf_override *override = xccdf_override_iterator_next(overrides); |
| 1548 | xccdf_override_to_dom(override, doc, result_node, version_info); |
| 1549 | } |
| 1550 | xccdf_override_iterator_free(overrides); |
| 1551 | |
| 1552 | struct xccdf_ident_iterator *idents = xccdf_rule_result_get_idents(result); |
| 1553 | while (xccdf_ident_iterator_has_more(idents)) { |
| 1554 | struct xccdf_ident *ident = xccdf_ident_iterator_next(idents); |
| 1555 | xccdf_ident_to_dom(ident, doc, result_node, version_info); |
| 1556 | } |
| 1557 | xccdf_ident_iterator_free(idents); |
| 1558 | |
| 1559 | struct xccdf_message_iterator *messages = xccdf_rule_result_get_messages(result); |
| 1560 | while (xccdf_message_iterator_has_more(messages)) { |
| 1561 | struct xccdf_message *message = xccdf_message_iterator_next(messages); |
| 1562 | const char *content = xccdf_message_get_content(message); |
| 1563 | xmlChar *encoded_content = xmlEncodeEntitiesReentrant(doc, BAD_CAST content); |
| 1564 | xmlNode *message_node = xmlNewChild(result_node, ns_xccdf, BAD_CAST "message", encoded_content); |
| 1565 | xmlFree(encoded_content); |
| 1566 | |
| 1567 | xccdf_message_severity_t message_severity = xccdf_message_get_severity(message); |
| 1568 | if (message_severity != XCCDF_MSG_NOT_DEFINED) |
| 1569 | xmlNewProp(message_node, BAD_CAST "severity", BAD_CAST XCCDF_LEVEL_MAP[message_severity - 1].string); |
| 1570 | } |
| 1571 | xccdf_message_iterator_free(messages); |
| 1572 | |
| 1573 | struct xccdf_instance_iterator *instances = xccdf_rule_result_get_instances(result); |
| 1574 | while (xccdf_instance_iterator_has_more(instances)) { |
| 1575 | struct xccdf_instance *instance = xccdf_instance_iterator_next(instances); |
| 1576 | const char *content = xccdf_instance_get_content(instance); |
| 1577 | xmlNode *instance_node = xmlNewTextChild(result_node, ns_xccdf, BAD_CAST "instance", BAD_CAST content); |
| 1578 | |
| 1579 | const char *context = xccdf_instance_get_context(instance); |
| 1580 | if (context) |
| 1581 | xmlNewProp(instance_node, BAD_CAST "context", BAD_CAST context); |
| 1582 | |
| 1583 | const char *parent_context = xccdf_instance_get_context(instance); |
| 1584 | if (parent_context) |
| 1585 | xmlNewProp(instance_node, BAD_CAST "parentContext", BAD_CAST context); |
| 1586 | } |
| 1587 | xccdf_instance_iterator_free(instances); |
| 1588 | |
| 1589 | struct xccdf_fix_iterator *fixes = xccdf_rule_result_get_fixes(result); |
| 1590 | while (xccdf_fix_iterator_has_more(fixes)) { |
| 1591 | struct xccdf_fix *fix = xccdf_fix_iterator_next(fixes); |
| 1592 | xccdf_fix_to_dom(fix, doc, result_node, version_info); |
| 1593 | } |
| 1594 | xccdf_fix_iterator_free(fixes); |
| 1595 | |
| 1596 | struct xccdf_check_iterator *checks = xccdf_rule_result_get_checks(result); |
| 1597 | while (xccdf_check_iterator_has_more(checks)) { |
| 1598 | struct xccdf_check *check = xccdf_check_iterator_next(checks); |
| 1599 | xccdf_check_to_dom(check, doc, result_node, version_info); |
| 1600 | } |
| 1601 | xccdf_check_iterator_free(checks); |
| 1602 | } |
| 1603 | |
| 1604 | bool xccdf_rule_result_override(struct xccdf_rule_result *rule_result, xccdf_test_result_type_t new_result, const char *waiver_time, const char *authority, struct oscap_text *remark) |
| 1605 | { |
| 1606 | struct xccdf_override *o= xccdf_override_new(); |
| 1607 | xccdf_override_set_old_result(o, xccdf_rule_result_get_result(rule_result)); |
| 1608 | xccdf_override_set_new_result(o, new_result); |
| 1609 | xccdf_override_set_time(o, waiver_time); |
| 1610 | xccdf_override_set_authority(o, authority); |
| 1611 | xccdf_override_set_remark(o, remark); |
| 1612 | if (!xccdf_rule_result_add_override(rule_result, o)) { |
| 1613 | xccdf_override_free(o); |
| 1614 | return false; |
| 1615 | } |
| 1616 | return xccdf_rule_result_set_result(rule_result, new_result); |
| 1617 | } |
| 1618 | |
| 1619 | static struct xccdf_override *xccdf_override_new_parse(xmlTextReaderPtr reader) |
| 1620 | { |
| 1621 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_OVERRIDE); |
| 1622 | |
| 1623 | struct xccdf_override *override = xccdf_override_new(); |
| 1624 | |
| 1625 | override->time = xccdf_attribute_copy(reader, XCCDFA_TIME); |
| 1626 | override->authority = xccdf_attribute_copy(reader, XCCDFA_AUTHORITY); |
| 1627 | |
| 1628 | int depth = oscap_element_depth(reader) + 1; |
| 1629 | |
| 1630 | while (oscap_to_start_element(reader, depth)) { |
| 1631 | switch (xccdf_element_get(reader)) { |
| 1632 | case XCCDFE_OLD_RESULT: |
| 1633 | override->old_result = oscap_string_to_enum(XCCDF_RESULT_MAP, oscap_element_string_get(reader)); |
| 1634 | break; |
| 1635 | case XCCDFE_NEW_RESULT: |
| 1636 | override->new_result = oscap_string_to_enum(XCCDF_RESULT_MAP, oscap_element_string_get(reader)); |
| 1637 | break; |
| 1638 | case XCCDFE_REMARK: |
| 1639 | if (override->remark == NULL) |
| 1640 | override->remark = oscap_text_new_parse(XCCDF_TEXT_PLAIN, reader); |
| 1641 | break; |
| 1642 | default: break; |
| 1643 | } |
| 1644 | xmlTextReaderRead(reader); |
| 1645 | } |
| 1646 | |
| 1647 | return override; |
| 1648 | } |
| 1649 | |
| 1650 | xmlNode *xccdf_override_to_dom(struct xccdf_override *override, xmlDoc *doc, xmlNode *parent, const struct xccdf_version_info* version_info) |
| 1651 | { |
| 1652 | xmlNs *ns_xccdf = lookup_xccdf_ns(doc, parent, version_info); |
| 1653 | |
| 1654 | xmlNode *override_node = xmlNewTextChild(parent, ns_xccdf, BAD_CAST "override", NULL); |
| 1655 | |
| 1656 | /* Handle attributes */ |
| 1657 | const char *tm = xccdf_override_get_time(override); |
| 1658 | if(tm) { |
| 1659 | xmlNewProp(override_node, BAD_CAST "date", BAD_CAST tm); |
| 1660 | } |
| 1661 | |
| 1662 | const char *authority = xccdf_override_get_authority(override); |
| 1663 | if (authority) |
| 1664 | xmlNewProp(override_node, BAD_CAST "authority", BAD_CAST authority); |
| 1665 | |
| 1666 | /* Handle children */ |
| 1667 | xccdf_test_result_type_t old = xccdf_override_get_old_result(override); |
| 1668 | if (old != 0) |
| 1669 | xmlNewTextChild(override_node, ns_xccdf, BAD_CAST "old-result", BAD_CAST XCCDF_RESULT_MAP[old - 1].string); |
| 1670 | |
| 1671 | xccdf_test_result_type_t new = xccdf_override_get_new_result(override); |
| 1672 | if (new != 0) |
| 1673 | xmlNewTextChild(override_node, ns_xccdf, BAD_CAST "new-result", BAD_CAST XCCDF_RESULT_MAP[new - 1].string); |
| 1674 | |
| 1675 | oscap_text_to_dom(xccdf_override_get_remark(override), override_node, "remark"); |
| 1676 | |
| 1677 | return override_node; |
| 1678 | } |
| 1679 | |
| 1680 | static struct xccdf_message *xccdf_message_new_parse(xmlTextReaderPtr reader) |
| 1681 | { |
| 1682 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_MESSAGE); |
| 1683 | |
| 1684 | struct xccdf_message *msg = xccdf_message_new(); |
| 1685 | msg->severity = oscap_string_to_enum(XCCDF_LEVEL_MAP, xccdf_attribute_get(reader, XCCDFA_SEVERITY)); |
| 1686 | msg->content = oscap_element_string_copy(reader); |
| 1687 | return msg; |
| 1688 | } |
| 1689 | |
| 1690 | static struct xccdf_instance *xccdf_instance_new_parse(xmlTextReaderPtr reader) |
| 1691 | { |
| 1692 | XCCDF_ASSERT_ELEMENT(reader, XCCDFE_INSTANCE); |
| 1693 | |
| 1694 | struct xccdf_instance *inst = xccdf_instance_new(); |
| 1695 | if (xccdf_attribute_has(reader, XCCDFA_CONTEXT)) |
| 1696 | xccdf_instance_set_context(inst, xccdf_attribute_get(reader, XCCDFA_CONTEXT)); |
| 1697 | inst->parent_context = xccdf_attribute_copy(reader, XCCDFA_PARENTCONTEXT); |
| 1698 | inst->content = oscap_element_string_copy(reader); |
| 1699 | return inst; |
| 1700 | } |
| 1701 | |
| 1702 | const char * xccdf_test_result_type_get_text(xccdf_test_result_type_t id) |
| 1703 | { |
| 1704 | if (id == 0) return NULL; |
| 1705 | |
| 1706 | return XCCDF_RESULT_MAP[id - 1].string; |
| 1707 | } |
| 1708 | |
| 1709 | struct xccdf_rule_result * xccdf_result_get_rule_result_by_id(struct xccdf_result * result, const char * id) |
| 1710 | { |
| 1711 | struct xccdf_rule_result * rule_result = NULL; |
| 1712 | |
| 1713 | struct xccdf_rule_result_iterator * rr_it = xccdf_result_get_rule_results(result); |
| 1714 | while (xccdf_rule_result_iterator_has_more(rr_it)) { |
| 1715 | rule_result = xccdf_rule_result_iterator_next(rr_it); |
| 1716 | if (oscap_streq(xccdf_rule_result_get_idref(rule_result), id)) |
| 1717 | break; |
| 1718 | else rule_result = NULL; |
| 1719 | } |
| 1720 | xccdf_rule_result_iterator_free(rr_it); |
| 1721 | |
| 1722 | return rule_result; |
| 1723 | } |
| 1724 | |
| 1725 | /* -------- DUMP ---------- */ |
| 1726 | |
| 1727 | static void xccdf_rule_result_dump(struct xccdf_rule_result *res, int depth) |
| 1728 | { |
| 1729 | xccdf_print_depth(depth); printf("Rule result: %s\n", xccdf_rule_result_get_idref(res)); ++depth; |
| 1730 | xccdf_print_depth(depth); printf("version: %s\n", xccdf_rule_result_get_version(res)); |
| 1731 | xccdf_print_depth(depth); printf("weight: %f\n", xccdf_rule_result_get_weight(res)); |
| 1732 | } |
| 1733 | |
| 1734 | void xccdf_result_dump(struct xccdf_result *res, int depth) |
| 1735 | { |
| 1736 | xccdf_print_depth(depth); printf("TestResult : %s\n", (res ? xccdf_result_get_id(res) : "(NULL)")); |
| 1737 | if (res != NULL) { |
| 1738 | ++depth; xccdf_item_print(XITEM(res), depth); |
| 1739 | xccdf_print_depth(depth); printf("test system: %s\n", xccdf_result_get_test_system(res)); |
| 1740 | xccdf_print_depth(depth); printf("benchmark URI: %s\n", xccdf_result_get_benchmark_uri(res)); |
| 1741 | xccdf_print_depth(depth); printf("profile ID: %s\n", xccdf_result_get_profile(res)); |
| 1742 | // start time, end time... |
| 1743 | //xccdf_print_depth(depth); printf("identities"); |
| 1744 | //oscap_list_dump(XITEM(res)->result.identities, NULL, depth+1); |
| 1745 | xccdf_print_depth(depth); printf("targets"); |
| 1746 | oscap_list_dump(XITEM(res)->sub.result.targets, xccdf_cstring_dump, depth+1); |
| 1747 | xccdf_print_depth(depth); printf("organizations"); |
| 1748 | oscap_list_dump(XITEM(res)->sub.result.organizations, xccdf_cstring_dump, depth+1); |
| 1749 | xccdf_print_depth(depth); printf("remarks"); |
| 1750 | xccdf_print_textlist(xccdf_result_get_remarks(res), depth+1, 80, "..."); |
| 1751 | xccdf_print_depth(depth); printf("target addresses"); |
| 1752 | oscap_list_dump(XITEM(res)->sub.result.target_addresses, xccdf_cstring_dump, depth+1); |
| 1753 | //xccdf_print_depth(depth); printf("target_facts"); |
| 1754 | //xccdf_print_depth(depth); printf("target_id_refs"); |
| 1755 | xccdf_print_depth(depth); printf("setvalues"); |
| 1756 | oscap_list_dump(XITEM(res)->sub.result.setvalues, xccdf_setvalue_dump, depth+1); |
| 1757 | xccdf_print_depth(depth); printf("rule results"); |
| 1758 | oscap_list_dump(XITEM(res)->sub.result.rule_results, (oscap_dump_func) xccdf_rule_result_dump, depth+1); |
| 1759 | //xccdf_print_depth(depth); printf("scores"); |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * @returns pointer to text representation of the current local time |
| 1765 | * and NULL on failure. The string is statically allocated and might |
| 1766 | * be overwritten on subsequent calls to this function |
| 1767 | */ |
| 1768 | static inline const char *_get_timestamp(void) |
| 1769 | { |
| 1770 | static char timestamp[] = "yyyy-mm-ddThh:mm:ss+zz:zz"; |
| 1771 | time_t tm; |
| 1772 | struct tm *lt; |
| 1773 | struct tm result; |
| 1774 | int tz_diff; |
| 1775 | char tz_sign; |
| 1776 | |
| 1777 | tm = time(NULL); |
| 1778 | lt = localtime_r(&tm, &result); |
| 1779 | |
| 1780 | if (!lt) |
| 1781 | return NULL; |
| 1782 | |
| 1783 | #if defined(OS_FREEBSD) |
| 1784 | tz_diff = lt->tm_gmtoff; |
| 1785 | |
| 1786 | if (tz_diff < 0) { |
| 1787 | tz_sign = '-'; |
| 1788 | tz_diff *= -1; |
| 1789 | } else { |
| 1790 | tz_sign = '+'; |
| 1791 | } |
| 1792 | |
| 1793 | /* glibc's timezone offset does not account for daylight savings time. |
| 1794 | * So we match that behavior here by adding 3600 seconds |
| 1795 | */ |
| 1796 | if (lt->tm_isdst) |
| 1797 | tz_diff += 3600; |
| 1798 | #else |
| 1799 | /* timezone is a global variable set by localtime(3) */ |
| 1800 | if (timezone <= 0) { |
| 1801 | tz_sign = '+'; |
| 1802 | tz_diff = -timezone; |
| 1803 | } else { |
| 1804 | tz_sign = '-'; |
| 1805 | tz_diff = timezone; |
| 1806 | } |
| 1807 | #endif |
| 1808 | tz_diff /= 60; |
| 1809 | |
| 1810 | int ret = snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", |
| 1811 | 1900 + lt->tm_year, 1 + lt->tm_mon, lt->tm_mday, |
| 1812 | lt->tm_hour, lt->tm_min, lt->tm_sec, tz_sign, tz_diff / 60, tz_diff % 60); |
| 1813 | |
| 1814 | if (ret < 0) { |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | |
| 1818 | return timestamp; |
| 1819 | } |
| 1820 | |
| 1821 | #define XCCDF_CURRENT_TIME_SETTER(TYPE, BOUND) \ |
| 1822 | int xccdf_##TYPE##_set##BOUND##time_current(struct xccdf_##TYPE *r) \ |
| 1823 | { return xccdf_##TYPE##_set##BOUND##time(r, _get_timestamp()); } |
| 1824 | |
| 1825 | XCCDF_CURRENT_TIME_SETTER(rule_result,_) |
| 1826 | XCCDF_CURRENT_TIME_SETTER(result,_start_) |
| 1827 | XCCDF_CURRENT_TIME_SETTER(result,_end_) |