| 1 | /* |
| 2 | * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * David Niemoller <[email protected]> |
| 21 | * Peter Vrabec <[email protected]> |
| 22 | * Tomas Heinrich <[email protected]> |
| 23 | * Daniel Kopecek <[email protected]> |
| 24 | * Šimon Lukašík |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <inttypes.h> |
| 33 | |
| 34 | #ifdef OS_WINDOWS |
| 35 | #include <winsock2.h> |
| 36 | #else |
| 37 | #include <arpa/inet.h> |
| 38 | #include <sys/socket.h> |
| 39 | #endif |
| 40 | |
| 41 | #include "oval_types.h" |
| 42 | #include "oval_system_characteristics.h" |
| 43 | #include "common/_error.h" |
| 44 | #include "common/debug_priv.h" |
| 45 | |
| 46 | #include "oval_cmp_basic_impl.h" |
| 47 | #include "oval_cmp_evr_string_impl.h" |
| 48 | #include "oval_cmp_ip_address_impl.h" |
| 49 | #include "oval_cmp_impl.h" |
| 50 | |
| 51 | __attribute__((nonnull(1,2))) static bool cstr_to_intmax(const char *cstr, intmax_t *result) |
| 52 | { |
| 53 | char *endptr = NULL; |
| 54 | |
| 55 | errno = 0; |
| 56 | *result = strtoimax(cstr, &endptr, 10); |
| 57 | // Check for underflow/overflow, strtoimax sets ERANGE in such case |
| 58 | if (errno == ERANGE) { |
| 59 | return false; |
| 60 | } |
| 61 | // Check whether there were some digits in the string |
| 62 | if (endptr == cstr) { |
| 63 | errno = EINVAL; |
| 64 | return false; |
| 65 | } |
| 66 | // Check whether the function used the whole string |
| 67 | if (*endptr != '\0') { |
| 68 | errno = EINVAL; |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | __attribute__((nonnull(1,2))) static bool cstr_to_double(const char *cstr, double *result) |
| 75 | { |
| 76 | char *endptr = NULL; |
| 77 | |
| 78 | errno = 0; |
| 79 | *result = strtod(cstr, &endptr); |
| 80 | // Check for underflow/overflow, strtoimax sets ERANGE in such case |
| 81 | if (errno == ERANGE) { |
| 82 | return false; |
| 83 | } |
| 84 | // Check whether there were some digits in the string |
| 85 | if (endptr == cstr) { |
| 86 | errno = EINVAL; |
| 87 | return false; |
| 88 | } |
| 89 | // Check whether the function used the whole string |
| 90 | if (*endptr != '\0') { |
| 91 | errno = EINVAL; |
| 92 | return false; |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | oval_result_t oval_str_cmp_str(char *state_data, oval_datatype_t state_data_type, const char *sys_data, oval_operation_t operation) |
| 98 | { |
| 99 | // finally, we have gotten to the point of comparing system data with a state |
| 100 | |
| 101 | if (state_data_type == OVAL_DATATYPE_STRING) { |
| 102 | return oval_string_cmp(state_data, sys_data, operation); |
| 103 | } else if (state_data_type == OVAL_DATATYPE_INTEGER) { |
| 104 | intmax_t state_val, syschar_val; |
| 105 | |
| 106 | if (!cstr_to_intmax(state_data, &state_val)) { |
| 107 | dW( |
| 108 | "Conversion of the string \"%s\" to an integer (%zu bits) failed: %s", |
| 109 | state_data, sizeof(intmax_t)*8, strerror(errno)); |
| 110 | return OVAL_RESULT_ERROR; |
| 111 | } |
| 112 | |
| 113 | if (!cstr_to_intmax(sys_data, &syschar_val)) { |
| 114 | dW( |
| 115 | "Conversion of the string \"%s\" to an integer (%zu bits) failed: %s", |
| 116 | sys_data, sizeof(intmax_t)*8, strerror(errno)); |
| 117 | return OVAL_RESULT_ERROR; |
| 118 | } |
| 119 | return oval_int_cmp(state_val, syschar_val, operation); |
| 120 | } else if (state_data_type == OVAL_DATATYPE_FLOAT) { |
| 121 | double state_val, sys_val; |
| 122 | |
| 123 | if (!cstr_to_double(state_data, &state_val)) { |
| 124 | dW( |
| 125 | "Conversion of the string \"%s\" to a floating type (double) failed: %s", |
| 126 | state_data, strerror(errno)); |
| 127 | return OVAL_RESULT_ERROR; |
| 128 | } |
| 129 | |
| 130 | if (!cstr_to_double(sys_data, &sys_val)) { |
| 131 | dW( |
| 132 | "Conversion of the string \"%s\" to a floating type (double) failed: %s", |
| 133 | sys_data, strerror(errno)); |
| 134 | return OVAL_RESULT_ERROR; |
| 135 | } |
| 136 | return oval_float_cmp(state_val, sys_val, operation); |
| 137 | } else if (state_data_type == OVAL_DATATYPE_BOOLEAN) { |
| 138 | int state_int; |
| 139 | int sys_int; |
| 140 | state_int = (((strcmp(state_data, "true")) == 0) || ((strcmp(state_data, "1")) == 0)) ? 1 : 0; |
| 141 | sys_int = (((strcmp(sys_data, "true")) == 0) || ((strcmp(sys_data, "1")) == 0)) ? 1 : 0; |
| 142 | return oval_boolean_cmp(state_int, sys_int, operation); |
| 143 | } else if (state_data_type == OVAL_DATATYPE_BINARY) { |
| 144 | return oval_binary_cmp(state_data, sys_data, operation); |
| 145 | } else if (state_data_type == OVAL_DATATYPE_EVR_STRING) { |
| 146 | return oval_evr_string_cmp(state_data, sys_data, operation); |
| 147 | } else if (state_data_type == OVAL_DATATYPE_DEBIAN_EVR_STRING) { |
| 148 | return oval_debian_evr_string_cmp(state_data, sys_data, operation); |
| 149 | } else if (state_data_type == OVAL_DATATYPE_VERSION) { |
| 150 | return oval_versiontype_cmp(state_data, sys_data, operation); |
| 151 | } else if (state_data_type == OVAL_DATATYPE_IPV4ADDR) { |
| 152 | return oval_ipaddr_cmp(AF_INET, state_data, sys_data, operation); |
| 153 | } else if (state_data_type == OVAL_DATATYPE_IPV6ADDR) { |
| 154 | return oval_ipaddr_cmp(AF_INET6, state_data, sys_data, operation); |
| 155 | } else if (state_data_type == OVAL_DATATYPE_FILESET_REVISION |
| 156 | || state_data_type == OVAL_DATATYPE_IOS_VERSION) { |
| 157 | dW("Unsupported data type: %s.", oval_datatype_get_text(state_data_type)); |
| 158 | return OVAL_RESULT_NOT_EVALUATED; |
| 159 | } |
| 160 | |
| 161 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid OVAL data type: %d.", state_data_type); |
| 162 | return OVAL_RESULT_ERROR; |
| 163 | } |