| 1 | /** |
| 2 | * @file system_info_probe.c |
| 3 | * @brief system_info probe |
| 4 | * @author "Tomas Heinrich" <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 9 | * All Rights Reserved. |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2.1 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this library; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | * |
| 25 | * Authors: |
| 26 | * "Tomas Heinrich" <[email protected]> |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * system_info probe: |
| 31 | * |
| 32 | * (for internal use) |
| 33 | * |
| 34 | * system_info_object |
| 35 | * |
| 36 | * system_info_item |
| 37 | * attrs |
| 38 | * id |
| 39 | * status_enum status |
| 40 | * os_name |
| 41 | * os_version |
| 42 | * architecture |
| 43 | * primary_host_name |
| 44 | * //interfaces |
| 45 | * interface [0..*] |
| 46 | * attrs |
| 47 | * interface_name |
| 48 | * ip_address |
| 49 | * mac_address |
| 50 | * any [0..*] |
| 51 | */ |
| 52 | |
| 53 | #ifdef HAVE_CONFIG_H |
| 54 | #include <config.h> |
| 55 | #endif |
| 56 | |
| 57 | #include "_seap.h" |
| 58 | #include <probe-api.h> |
| 59 | #include <probe/probe.h> |
| 60 | #include <probe/option.h> |
| 61 | #include <debug_priv.h> |
| 62 | |
| 63 | #ifdef OS_WINDOWS |
| 64 | /* By defining WIN32_LEAN_AND_MEAN we ensure that Windows.h won't include |
| 65 | * winsock.h, which would conflict with symbols from WinSock2.h. |
| 66 | */ |
| 67 | #ifndef WIN32_LEAN_AND_MEAN |
| 68 | #define WIN32_LEAN_AND_MEAN |
| 69 | #endif |
| 70 | #include <WinSock2.h> |
| 71 | #include <Iphlpapi.h> |
| 72 | #include <Windows.h> |
| 73 | #include <ws2def.h> |
| 74 | #include <io.h> |
| 75 | #include <winternl.h> |
| 76 | #else |
| 77 | #include <sys/utsname.h> |
| 78 | #include <unistd.h> |
| 79 | #endif |
| 80 | #include <stdlib.h> |
| 81 | #include <string.h> |
| 82 | #include <ctype.h> |
| 83 | #include <errno.h> |
| 84 | #include <limits.h> |
| 85 | #include "system_info_probe.h" |
| 86 | #include "oscap_helpers.h" |
| 87 | |
| 88 | #define _REGEX_RES_VECSIZE 12 |
| 89 | #define MAX_BUFFER_SIZE 4096 |
| 90 | |
| 91 | #if !defined(HOST_NAME_MAX) |
| 92 | #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX |
| 93 | #endif |
| 94 | |
| 95 | #if defined(OS_LINUX) |
| 96 | #include <sys/socket.h> |
| 97 | #include <ifaddrs.h> |
| 98 | #include <netdb.h> |
| 99 | #include <sys/ioctl.h> |
| 100 | #include <string.h> |
| 101 | #include <net/if.h> |
| 102 | #include <arpa/inet.h> |
| 103 | #include <sys/types.h> |
| 104 | #elif defined(OS_FREEBSD) |
| 105 | #include <arpa/inet.h> |
| 106 | #include <ifaddrs.h> |
| 107 | #include <netdb.h> |
| 108 | #include <netinet/in.h> |
| 109 | #include <net/if.h> |
| 110 | #include <sys/ioctl.h> |
| 111 | #endif |
| 112 | |
| 113 | #if defined(OS_LINUX) |
| 114 | static char *get_mac(const struct ifaddrs *ifa, int fd) |
| 115 | { |
| 116 | struct ifreq ifr; |
| 117 | unsigned char mac[6]; |
| 118 | static char mac_buf[20]; |
| 119 | |
| 120 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 121 | strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); |
| 122 | ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 123 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0) { |
| 124 | memcpy(mac, ifr.ifr_hwaddr.sa_data, sizeof(mac)); |
| 125 | snprintf(mac_buf, sizeof(mac_buf), |
| 126 | "%02X:%02X:%02X:%02X:%02X:%02X", |
| 127 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 128 | } else |
| 129 | mac_buf[0] = 0; |
| 130 | |
| 131 | return mac_buf; |
| 132 | } |
| 133 | #elif defined(OS_SOLARIS) |
| 134 | #include <sys/socket.h> |
| 135 | #include <ifaddrs.h> |
| 136 | #include <netdb.h> |
| 137 | #include <sys/ioctl.h> |
| 138 | #include <string.h> |
| 139 | #include <net/if.h> |
| 140 | #include <arpa/inet.h> |
| 141 | #include <sys/sockio.h> |
| 142 | #include <net/if_types.h> |
| 143 | #include <libdlpi.h> |
| 144 | |
| 145 | static char *get_mac(const struct ifaddrs *ifa, int fd) |
| 146 | { |
| 147 | struct lifreq lifr; |
| 148 | size_t physaddrlen = DLPI_PHYSADDR_MAX; |
| 149 | uchar_t physaddr[DLPI_PHYSADDR_MAX]; |
| 150 | static char mac_buf[DLPI_PHYSADDR_MAX]; |
| 151 | char *str; |
| 152 | int retv; |
| 153 | dlpi_handle_t dh; |
| 154 | dlpi_info_t dlinfo; |
| 155 | |
| 156 | memset(mac_buf, 0, sizeof(mac_buf)); |
| 157 | memset(&lifr, 0, sizeof(struct lifreq)); |
| 158 | strlcpy(lifr.lifr_name, ifa->ifa_name, sizeof (lifr.lifr_name)); |
| 159 | if (ioctl(fd, SIOCGLIFFLAGS, &lifr) >= 0) { |
| 160 | |
| 161 | if (lifr.lifr_flags & (IFF_VIRTUAL| IFF_IPMP)) |
| 162 | return (mac_buf); |
| 163 | |
| 164 | if (dlpi_open(lifr.lifr_name, &dh, 0) != DLPI_SUCCESS) |
| 165 | return (NULL); |
| 166 | |
| 167 | retv = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr, |
| 168 | &physaddrlen); |
| 169 | if (retv != DLPI_SUCCESS) { |
| 170 | dlpi_close(dh); |
| 171 | return (NULL); |
| 172 | } |
| 173 | |
| 174 | retv = dlpi_info(dh, &dlinfo, DLPI_INFO_VERSION); |
| 175 | if (retv != DLPI_SUCCESS) { |
| 176 | dlpi_close(dh); |
| 177 | return (NULL); |
| 178 | } |
| 179 | dlpi_close(dh); |
| 180 | str = _link_ntoa(physaddr, NULL, physaddrlen, IFT_OTHER); |
| 181 | |
| 182 | if (str != NULL && physaddrlen != 0) { |
| 183 | switch(dlinfo.di_mactype) { |
| 184 | case DL_IB: |
| 185 | break; |
| 186 | default: |
| 187 | strlcpy(mac_buf, str, sizeof(mac_buf)); |
| 188 | break; |
| 189 | } |
| 190 | free(str); |
| 191 | } |
| 192 | } |
| 193 | return mac_buf; |
| 194 | } |
| 195 | |
| 196 | #elif defined(OS_FREEBSD) |
| 197 | static char *get_mac(const struct ifaddrs *ifa, int fd) |
| 198 | { |
| 199 | struct ifreq ifr; |
| 200 | unsigned char mac[6]; |
| 201 | static char mac_buf[20]; |
| 202 | |
| 203 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 204 | strncpy(ifr.ifr_name, ifa->ifa_name, IFNAMSIZ); |
| 205 | ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 206 | |
| 207 | if (ioctl(fd, SIOCGHWADDR, &ifr) >= 0) { |
| 208 | memcpy(mac, ifr.ifr_addr.sa_data, sizeof(mac)); |
| 209 | snprintf(mac_buf, sizeof(mac_buf), |
| 210 | "%02X:%02X:%02X:%02X:%02X:%02X", |
| 211 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 212 | } else { |
| 213 | mac_buf[0] = 0; |
| 214 | } |
| 215 | |
| 216 | return mac_buf; |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | #if defined(OS_LINUX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) |
| 221 | static int get_ifs(SEXP_t *item) |
| 222 | { |
| 223 | struct ifaddrs *ifaddr, *ifa; |
| 224 | int family, rc=1; |
| 225 | char host[NI_MAXHOST], *mac; |
| 226 | SEXP_t *attrs; |
| 227 | SEXP_t *r0, *r1, *r2; |
| 228 | #if defined(OS_SOLARIS) |
| 229 | int item_added = 0; |
| 230 | #endif |
| 231 | |
| 232 | if (getifaddrs(&ifaddr) == -1) |
| 233 | return rc; |
| 234 | |
| 235 | int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 236 | if (fd < 0) |
| 237 | goto leave1; |
| 238 | |
| 239 | /* Walk through linked list, maintaining head pointer so we |
| 240 | can free list later */ |
| 241 | for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 242 | if (ifa->ifa_addr) { |
| 243 | family = ifa->ifa_addr->sa_family; |
| 244 | if (family != AF_INET && family != AF_INET6) |
| 245 | continue; |
| 246 | } else |
| 247 | continue; |
| 248 | |
| 249 | mac = get_mac(ifa, fd); |
| 250 | #if defined(OS_SOLARIS) |
| 251 | if (mac == NULL) { |
| 252 | rc = 1; |
| 253 | goto leave2; |
| 254 | } |
| 255 | if (mac[0] == '\0') |
| 256 | continue; |
| 257 | #endif |
| 258 | if (family == AF_INET) { |
| 259 | rc = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), |
| 260 | host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); |
| 261 | if (rc) { |
| 262 | rc = 1; |
| 263 | goto leave2; |
| 264 | } |
| 265 | } |
| 266 | else { |
| 267 | struct sockaddr_in6 *sin6p; |
| 268 | sin6p = (struct sockaddr_in6 *) ifa->ifa_addr; |
| 269 | if (! inet_ntop(family, (const void *)&sin6p->sin6_addr, host, NI_MAXHOST)) { |
| 270 | rc = 1; |
| 271 | goto leave2; |
| 272 | } |
| 273 | } |
| 274 | attrs = probe_attr_creat("name", |
| 275 | r0 = SEXP_string_newf("%s", ifa->ifa_name), |
| 276 | "ip_address", |
| 277 | r1 = SEXP_string_newf("%s", host), |
| 278 | "mac_address", |
| 279 | r2 = SEXP_string_newf("%s", mac), |
| 280 | NULL); |
| 281 | probe_item_ent_add(item, "interface", attrs, NULL); |
| 282 | #if defined(OS_SOLARIS) |
| 283 | item_added = 1; |
| 284 | #endif |
| 285 | SEXP_free(attrs); |
| 286 | SEXP_free(r0); |
| 287 | SEXP_free(r1); |
| 288 | SEXP_free(r2); |
| 289 | } |
| 290 | leave2: |
| 291 | close(fd); |
| 292 | #if defined(OS_SOLARIS) |
| 293 | if (item_added == 0) { |
| 294 | attrs = probe_attr_creat("name", |
| 295 | r0 = SEXP_string_newf("dummy0"), |
| 296 | "ip_address", |
| 297 | r1 = SEXP_string_newf("127.0.0.1"), |
| 298 | "mac_address", |
| 299 | r2 = SEXP_string_newf("aa:bb:cc:dd:ee:ff"), |
| 300 | NULL); |
| 301 | probe_item_ent_add(item, "interface", attrs, NULL); |
| 302 | SEXP_free(attrs); |
| 303 | SEXP_free(r0); |
| 304 | SEXP_free(r1); |
| 305 | SEXP_free(r2); |
| 306 | } |
| 307 | /* if not able to get info on interfaces, do not fail. */ |
| 308 | if (rc > 0) |
| 309 | rc = 0; |
| 310 | #endif |
| 311 | leave1: |
| 312 | freeifaddrs(ifaddr); |
| 313 | return rc; |
| 314 | } |
| 315 | |
| 316 | #elif defined(OS_WINDOWS) |
| 317 | |
| 318 | #define VERSION_LEN 32 |
| 319 | /* Microsoft recommends to start with a 15 kB buffer for GetAdaptersAddresses. */ |
| 320 | #define ADDRESSES_BUFFER_SIZE 15000 |
| 321 | #define MAX_TRIES 3 |
| 322 | #define MAX_IP_ADDRESS_STRING_LENGTH 128 |
| 323 | |
| 324 | static int get_ifs(SEXP_t *item) |
| 325 | { |
| 326 | /* |
| 327 | * Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h |
| 328 | * to define the Windows Socket specification version we will use. |
| 329 | * The current version of the Windows Sockets specification is version 2.2. |
| 330 | * Version 2.2 is supported on all systems since Windows 95 OSR2. |
| 331 | */ |
| 332 | WORD requested_version = MAKEWORD(2, 2); |
| 333 | |
| 334 | WSADATA wsa_data; |
| 335 | int err = WSAStartup(requested_version, &wsa_data); |
| 336 | if (err != 0) { |
| 337 | char *error_message = oscap_windows_error_message(err); |
| 338 | dE("Can't initialize Winsock DLL. WSAStartup failed: %s", error_message); |
| 339 | free(error_message); |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | PIP_ADAPTER_ADDRESSES addresses = NULL; |
| 344 | ULONG ret_val = 0; |
| 345 | ULONG addresses_size = ADDRESSES_BUFFER_SIZE; |
| 346 | ULONG family = AF_UNSPEC; // both IPv4 and IPv6 |
| 347 | ULONG flags = GAA_FLAG_INCLUDE_PREFIX; |
| 348 | int iterations = 0; |
| 349 | |
| 350 | /* Try to get information about network adapters and their adresses */ |
| 351 | do { |
| 352 | addresses = malloc(addresses_size); |
| 353 | ret_val = GetAdaptersAddresses(family, flags, NULL, addresses, &addresses_size); |
| 354 | if (ret_val == ERROR_BUFFER_OVERFLOW) { |
| 355 | free(addresses); |
| 356 | addresses = NULL; |
| 357 | } |
| 358 | iterations++; |
| 359 | } while (ret_val == ERROR_BUFFER_OVERFLOW && iterations < MAX_TRIES); |
| 360 | |
| 361 | if (ret_val != NO_ERROR) { |
| 362 | char *error_message = oscap_windows_error_message(err); |
| 363 | dE("Can't get information about network adapters and their adresses. %s", error_message); |
| 364 | free(error_message); |
| 365 | free(addresses); |
| 366 | WSACleanup(); |
| 367 | return 1; |
| 368 | } |
| 369 | |
| 370 | /* Iterate over all network adapters */ |
| 371 | PIP_ADAPTER_ADDRESSES current_address = addresses; |
| 372 | while (current_address != NULL) { |
| 373 | /* MAC address */ |
| 374 | char *mac_address_str; |
| 375 | if (current_address->PhysicalAddressLength != 0) { |
| 376 | /* n bytes of MAC address will be printed as: |
| 377 | (2 * n) hexa numbers + (n - 1) delimiters + 1 terminating null byte = 3 * n */ |
| 378 | const size_t mac_address_str_len = current_address->PhysicalAddressLength * 3; |
| 379 | mac_address_str = malloc(mac_address_str_len); |
| 380 | |
| 381 | for (unsigned i = 0; i < current_address->PhysicalAddressLength; i++) { |
| 382 | if (i == (current_address->PhysicalAddressLength - 1)) { |
| 383 | snprintf(mac_address_str + i * 3, mac_address_str_len, "%.2X", |
| 384 | (int)current_address->PhysicalAddress[i]); |
| 385 | } else { |
| 386 | snprintf(mac_address_str + i * 3, mac_address_str_len, "%.2X-", |
| 387 | (int)current_address->PhysicalAddress[i]); |
| 388 | } |
| 389 | } |
| 390 | } else { |
| 391 | mac_address_str = strdup("unknown"); |
| 392 | } |
| 393 | |
| 394 | /* Find the network intefrace name */ |
| 395 | char *interface_name_str = oscap_windows_wstr_to_str(current_address->FriendlyName); |
| 396 | |
| 397 | /* Iterate over all unicast IP addresses of the network interface */ |
| 398 | PIP_ADAPTER_UNICAST_ADDRESS unicast_address = current_address->FirstUnicastAddress; |
| 399 | while (unicast_address != NULL) { |
| 400 | SOCKET_ADDRESS socket_address = unicast_address->Address; |
| 401 | WCHAR ip_address_wstr[MAX_IP_ADDRESS_STRING_LENGTH]; |
| 402 | DWORD ip_address_wstr_length = MAX_IP_ADDRESS_STRING_LENGTH; |
| 403 | int rc = WSAAddressToStringW(socket_address.lpSockaddr, socket_address.iSockaddrLength, NULL, ip_address_wstr, &ip_address_wstr_length); |
| 404 | if (rc != 0) { |
| 405 | free(mac_address_str); |
| 406 | free(interface_name_str); |
| 407 | free(addresses); |
| 408 | WSACleanup(); |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | /* Add prefix length (mask) */ |
| 413 | char *ip_address_str = oscap_windows_wstr_to_str(ip_address_wstr); |
| 414 | UINT8 prefix = unicast_address->OnLinkPrefixLength; |
| 415 | char *ip_address_with_prefix = oscap_sprintf("%s/%d", ip_address_str, prefix); |
| 416 | free(ip_address_str); |
| 417 | |
| 418 | /* Create probe item */ |
| 419 | SEXP_t *interface_name_sexp = SEXP_string_newf(interface_name_str); |
| 420 | SEXP_t *ip_address_sexp = SEXP_string_newf(ip_address_with_prefix); |
| 421 | SEXP_t *mac_address_sexp = SEXP_string_newf(mac_address_str); |
| 422 | SEXP_t *attrs = probe_attr_creat("name", interface_name_sexp, |
| 423 | "ip_address", ip_address_sexp, |
| 424 | "mac_address", mac_address_sexp, |
| 425 | NULL); |
| 426 | probe_item_ent_add(item, "interface", attrs, NULL); |
| 427 | SEXP_free(attrs); |
| 428 | SEXP_free(interface_name_sexp); |
| 429 | SEXP_free(ip_address_sexp); |
| 430 | SEXP_free(mac_address_sexp); |
| 431 | free(ip_address_with_prefix); |
| 432 | |
| 433 | unicast_address = unicast_address->Next; |
| 434 | } |
| 435 | free(interface_name_str); |
| 436 | free(mac_address_str); |
| 437 | current_address = current_address->Next; |
| 438 | } |
| 439 | |
| 440 | free(addresses); |
| 441 | WSACleanup(); |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | #else |
| 446 | |
| 447 | static int get_ifs(SEXP_t *item) |
| 448 | { |
| 449 | /* TODO */ |
| 450 | |
| 451 | SEXP_t *attrs, *r0, *r1, *r2; |
| 452 | |
| 453 | attrs = probe_attr_creat("name", |
| 454 | r0 = SEXP_string_newf("dummy0"), |
| 455 | "ip_address", |
| 456 | r1 = SEXP_string_newf("127.0.0.1"), |
| 457 | "mac_address", |
| 458 | r2 = SEXP_string_newf("aa:bb:cc:dd:ee:ff"), |
| 459 | NULL); |
| 460 | |
| 461 | probe_item_ent_add(item, "interface", attrs, NULL); |
| 462 | |
| 463 | SEXP_free(attrs); |
| 464 | SEXP_free(r0); |
| 465 | SEXP_free(r1); |
| 466 | SEXP_free(r2); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | #endif |
| 471 | |
| 472 | /* |
| 473 | * If no forbidden chars are found, returns the trimmed length |
| 474 | * of the input string. Otherwise a negative number is returned |
| 475 | */ |
| 476 | static ssize_t __sysinfo_saneval(const char *s) |
| 477 | { |
| 478 | size_t i, space_count, real_length; |
| 479 | |
| 480 | real_length = space_count = i = 0; |
| 481 | |
| 482 | for (; i < strlen(s); ++i) { |
| 483 | /* check for space */ |
| 484 | if (isspace(s[i])) { |
| 485 | if (real_length > 0) { |
| 486 | ++space_count; |
| 487 | continue; |
| 488 | } |
| 489 | } |
| 490 | /* check for printable */ |
| 491 | if (!isprint(s[i])) { |
| 492 | return -1; |
| 493 | } |
| 494 | /* check for forbidden chars */ |
| 495 | switch(s[i]) { |
| 496 | case '\'': |
| 497 | case '"': |
| 498 | case '<': |
| 499 | case '>': |
| 500 | return -1; |
| 501 | } |
| 502 | /* Allowed character found */ |
| 503 | real_length += 1 + space_count; |
| 504 | space_count = 0; |
| 505 | } |
| 506 | return (ssize_t)real_length; |
| 507 | } |
| 508 | |
| 509 | #ifndef OS_WINDOWS |
| 510 | static char *_get_os_release(const char *oscap_probe_root) |
| 511 | { |
| 512 | char os_release_data[MAX_BUFFER_SIZE+1]; |
| 513 | char *ret = NULL; |
| 514 | |
| 515 | FILE *fp = oscap_fopen_with_prefix(oscap_probe_root, "/etc/os-release"); |
| 516 | if (fp == NULL) |
| 517 | goto fail; |
| 518 | |
| 519 | int rc = fread(os_release_data, 1, MAX_BUFFER_SIZE, fp); |
| 520 | if (ferror(fp)) |
| 521 | goto finish; |
| 522 | os_release_data[rc] = '\0'; |
| 523 | ret = strdup(os_release_data); |
| 524 | |
| 525 | finish: |
| 526 | fclose(fp); |
| 527 | fail: |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | static char *_get_os_release_elem(char *os_release_data, const char *elem_name) |
| 532 | { |
| 533 | if (os_release_data == NULL) |
| 534 | return NULL; |
| 535 | |
| 536 | char *ret = NULL; |
| 537 | size_t len = strlen(os_release_data); |
| 538 | |
| 539 | char elem_re[128] = {0}; |
| 540 | snprintf(elem_re, sizeof(elem_re), "%s%s%s", "^", elem_name, "=[\"']?(.*?)[\"']?$"); |
| 541 | |
| 542 | char *error; |
| 543 | int erroffset, ovec[_REGEX_RES_VECSIZE] = {0}; |
| 544 | oscap_pcre_t *re = oscap_pcre_compile(elem_re, OSCAP_PCRE_OPTS_MULTILINE, &error, &erroffset); |
| 545 | if (re == NULL) { |
| 546 | oscap_pcre_err_free(error); |
| 547 | goto finish; |
| 548 | } |
| 549 | |
| 550 | char *ptr = NULL; |
| 551 | int rc = oscap_pcre_exec(re, os_release_data, len, 0, 0, ovec, _REGEX_RES_VECSIZE); |
| 552 | if (rc >= 0) { |
| 553 | /* ovec[0] and ovec[1] - are the start and the end of the whole pattern match (=".....") |
| 554 | * ovec[2] and ovec[3] - are start and end char positions of the capture group (.*?) */ |
| 555 | ptr = strndup(os_release_data+ovec[2], ovec[3]-ovec[2]); |
| 556 | ret = ptr; |
| 557 | } |
| 558 | oscap_pcre_free(re); |
| 559 | |
| 560 | finish: |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | #if defined(OS_FREEBSD) |
| 565 | static char *_offline_get_hname(const char *oscap_probe_root) |
| 566 | { |
| 567 | FILE *fp; |
| 568 | size_t len; |
| 569 | char *strp; |
| 570 | char *entry; |
| 571 | char *hname; |
| 572 | char *ret = NULL; |
| 573 | char *line = NULL; |
| 574 | const char *sep = "\""; |
| 575 | const char *expected_entry = "hostname="; |
| 576 | |
| 577 | fp = oscap_fopen_with_prefix(oscap_probe_root, "/etc/rc.conf"); |
| 578 | |
| 579 | if (!fp) |
| 580 | goto fail; |
| 581 | |
| 582 | while (getline(&line, &len, fp) > 0) { |
| 583 | entry = strtok_r(line, sep, &strp); |
| 584 | if (strcmp(entry, expected_entry) == 0) { |
| 585 | hname = strtok_r(NULL, sep, &strp); |
| 586 | ret = strdup(hname); |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | fclose(fp); |
| 592 | fail: |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | #else |
| 597 | static char *_offline_get_hname(const char *oscap_probe_root) |
| 598 | { |
| 599 | FILE *fp; |
| 600 | char hname[HOST_NAME_MAX+1] = { '\0' }; |
| 601 | char *ret = NULL; |
| 602 | int rc; |
| 603 | |
| 604 | fp = oscap_fopen_with_prefix(oscap_probe_root, "/etc/hostname"); |
| 605 | |
| 606 | if (fp == NULL) |
| 607 | goto fail; |
| 608 | |
| 609 | rc = fread(hname, 1, HOST_NAME_MAX, fp); |
| 610 | /* If file is empty, we don't want to allocate an empty string for it */ |
| 611 | if (ferror(fp) || rc == 0 ) |
| 612 | goto finish; |
| 613 | |
| 614 | hname[strcspn(hname, "\n")] = '\0'; |
| 615 | ret = strdup(hname); |
| 616 | |
| 617 | finish: |
| 618 | fclose(fp); |
| 619 | fail: |
| 620 | return ret; |
| 621 | } |
| 622 | #endif /* ifdef OS_FREEBSD */ |
| 623 | #endif /* ifndef OS_WINDOWS */ |
| 624 | |
| 625 | #ifdef OS_WINDOWS |
| 626 | static char *get_windows_version() |
| 627 | { |
| 628 | /* We can't use GetVersionEx to get Windows version because we want to get |
| 629 | * the real Windows version, but the compatibilty mode lies to applications |
| 630 | * about system version. Moreover the GetVersionEx function is deprecated |
| 631 | * since Windows 8.1. Instead we need to access RtlGetVersion. |
| 632 | * Code inspired by https://github.com/GNOME/glib/blob/master/glib/gwin32.c |
| 633 | */ |
| 634 | OSVERSIONINFOEXW osverinfo; |
| 635 | typedef NTSTATUS(WINAPI f_rtl_get_version) (PRTL_OSVERSIONINFOEXW); |
| 636 | f_rtl_get_version *rtl_get_version; |
| 637 | HMODULE hmodule; |
| 638 | |
| 639 | hmodule = LoadLibraryW(L"ntdll.dll"); |
| 640 | rtl_get_version = (f_rtl_get_version *)GetProcAddress(hmodule, "RtlGetVersion"); |
| 641 | |
| 642 | memset(&osverinfo, 0, sizeof(OSVERSIONINFOEXW)); |
| 643 | osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW); |
| 644 | rtl_get_version(&osverinfo); |
| 645 | |
| 646 | char *version = oscap_sprintf("%d.%d.%d", osverinfo.dwMajorVersion, |
| 647 | osverinfo.dwMinorVersion, osverinfo.dwBuildNumber); |
| 648 | |
| 649 | return version; |
| 650 | } |
| 651 | |
| 652 | static const char *get_windows_architecture(void) |
| 653 | { |
| 654 | SYSTEM_INFO si; |
| 655 | GetNativeSystemInfo(&si); |
| 656 | const char *arch; |
| 657 | switch (si.wProcessorArchitecture) { |
| 658 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 659 | arch = "x64"; |
| 660 | break; |
| 661 | case PROCESSOR_ARCHITECTURE_ARM: |
| 662 | arch = "ARM"; |
| 663 | break; |
| 664 | case PROCESSOR_ARCHITECTURE_ARM64: |
| 665 | arch = "ARM64"; |
| 666 | break; |
| 667 | case PROCESSOR_ARCHITECTURE_IA64: |
| 668 | arch = "Intel Itanium-based"; |
| 669 | break; |
| 670 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 671 | arch = "x86"; |
| 672 | break; |
| 673 | case PROCESSOR_ARCHITECTURE_UNKNOWN: |
| 674 | default: |
| 675 | arch = "unknown"; |
| 676 | break; |
| 677 | } |
| 678 | return arch; |
| 679 | } |
| 680 | #endif |
| 681 | |
| 682 | int system_info_probe_offline_mode_supported() |
| 683 | { |
| 684 | return PROBE_OFFLINE_OWN; |
| 685 | } |
| 686 | |
| 687 | int system_info_probe_main(probe_ctx *ctx, void *arg) |
| 688 | { |
| 689 | SEXP_t *item = NULL; |
| 690 | char *os_name, *architecture, *hname, *os_version = NULL; |
| 691 | const char unknown[] = "Unknown"; |
| 692 | int ret = 0; |
| 693 | (void)arg; |
| 694 | |
| 695 | os_name = architecture = hname = NULL; |
| 696 | |
| 697 | #ifdef OS_WINDOWS |
| 698 | WCHAR computer_name_wstr[MAX_COMPUTERNAME_LENGTH + 1]; |
| 699 | DWORD computer_name_len = MAX_COMPUTERNAME_LENGTH + 1; |
| 700 | GetComputerNameW(computer_name_wstr, &computer_name_len); |
| 701 | |
| 702 | os_name = strdup("Windows"); |
| 703 | os_version = get_windows_version(); |
| 704 | architecture = strdup(get_windows_architecture()); |
| 705 | hname = oscap_windows_wstr_to_str(computer_name_wstr); |
| 706 | #else |
| 707 | const char *oscap_probe_root = ""; |
| 708 | if (ctx->offline_mode & PROBE_OFFLINE_OWN) { |
| 709 | oscap_probe_root = getenv("OSCAP_PROBE_ROOT"); |
| 710 | } |
| 711 | char *os_release_data = _get_os_release(oscap_probe_root); |
| 712 | os_name = _get_os_release_elem(os_release_data, "NAME"); |
| 713 | os_version = _get_os_release_elem(os_release_data, "VERSION"); |
| 714 | if (ctx->offline_mode == PROBE_OFFLINE_NONE) { |
| 715 | struct utsname sname; |
| 716 | if (uname(&sname) == 0) { |
| 717 | architecture = strdup(sname.machine); |
| 718 | hname = strdup(sname.nodename); |
| 719 | } |
| 720 | } else if (ctx->offline_mode & PROBE_OFFLINE_OWN) { |
| 721 | hname = _offline_get_hname(oscap_probe_root); |
| 722 | } |
| 723 | free(os_release_data); |
| 724 | #endif |
| 725 | |
| 726 | /* All four elements are required */ |
| 727 | if (!os_name) |
| 728 | os_name = strdup(unknown); |
| 729 | |
| 730 | if (!os_version) |
| 731 | os_version = strdup(unknown); |
| 732 | |
| 733 | if (!architecture) |
| 734 | architecture = strdup(unknown); |
| 735 | |
| 736 | if (hname && *hname == '\0') { |
| 737 | free(hname); |
| 738 | hname = NULL; |
| 739 | } |
| 740 | if (!hname) { |
| 741 | hname = strdup(unknown); |
| 742 | } |
| 743 | |
| 744 | if (__sysinfo_saneval(os_name) < 1 || |
| 745 | __sysinfo_saneval(os_version) < 1 || |
| 746 | __sysinfo_saneval(architecture) < 1 || |
| 747 | __sysinfo_saneval(hname) < 1) { |
| 748 | ret = PROBE_EINVAL; |
| 749 | goto cleanup; |
| 750 | } |
| 751 | |
| 752 | item = probe_item_create(OVAL_INDEPENDENT_SYSCHAR_SUBTYPE, NULL, |
| 753 | "os_name", OVAL_DATATYPE_STRING, os_name, |
| 754 | "os_version", OVAL_DATATYPE_STRING, os_version, |
| 755 | "os_architecture", OVAL_DATATYPE_STRING, architecture, |
| 756 | "primary_host_name", OVAL_DATATYPE_STRING, hname, |
| 757 | NULL); |
| 758 | cleanup: |
| 759 | free(os_name); |
| 760 | free(os_version); |
| 761 | free(architecture); |
| 762 | free(hname); |
| 763 | |
| 764 | if (ctx->offline_mode == PROBE_OFFLINE_NONE && item) { |
| 765 | if (get_ifs(item)) { |
| 766 | SEXP_free(item); |
| 767 | return PROBE_EUNKNOWN; |
| 768 | } |
| 769 | } |
| 770 | probe_item_collect(ctx, item); |
| 771 | |
| 772 | return ret; |
| 773 | } |