Blame src/OVAL/results/oval_cmp.c

Packit Service 569379
/*
Packit Service 569379
 * Copyright 2009--2014 Red Hat Inc., Durham, North Carolina.
Packit Service 569379
 * All Rights Reserved.
Packit Service 569379
 *
Packit Service 569379
 * This library is free software; you can redistribute it and/or
Packit Service 569379
 * modify it under the terms of the GNU Lesser General Public
Packit Service 569379
 * License as published by the Free Software Foundation; either
Packit Service 569379
 * version 2.1 of the License, or (at your option) any later version.
Packit Service 569379
 *
Packit Service 569379
 * This library is distributed in the hope that it will be useful,
Packit Service 569379
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 569379
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 569379
 * Lesser General Public License for more details.
Packit Service 569379
 *
Packit Service 569379
 * You should have received a copy of the GNU Lesser General Public
Packit Service 569379
 * License along with this library; if not, write to the Free Software
Packit Service 569379
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service 569379
 *
Packit Service 569379
 * Authors:
Packit Service 569379
 *     David Niemoller <David.Niemoller@g2-inc.com>
Packit Service 569379
 *     Peter Vrabec <pvrabec@redhat.com>
Packit Service 569379
 *     Tomas Heinrich <theinric@redhat.com>
Packit Service 569379
 *     Daniel Kopecek <dkopecek@redhat.com>
Packit Service 569379
 *     Šimon Lukašík
Packit Service 569379
 */
Packit Service 569379
Packit Service 569379
#ifdef HAVE_CONFIG_H
Packit Service 569379
#include <config.h>
Packit Service 569379
#endif
Packit Service 569379
Packit Service 569379
#include <string.h>
Packit Service 569379
#include <inttypes.h>
Packit Service 569379
Packit Service 569379
#ifdef OS_WINDOWS
Packit Service 569379
#include <winsock2.h>
Packit Service 569379
#else
Packit Service 569379
#include <arpa/inet.h>
Packit Service 569379
#include <sys/socket.h>
Packit Service 569379
#endif
Packit Service 569379
Packit Service 569379
#include "oval_types.h"
Packit Service 569379
#include "oval_system_characteristics.h"
Packit Service 569379
#include "common/_error.h"
Packit Service 569379
#include "common/debug_priv.h"
Packit Service 569379
Packit Service 569379
#include "oval_cmp_basic_impl.h"
Packit Service 569379
#include "oval_cmp_evr_string_impl.h"
Packit Service 569379
#include "oval_cmp_ip_address_impl.h"
Packit Service 569379
#include "oval_cmp_impl.h"
Packit Service 569379
Packit Service 569379
__attribute__((nonnull(1,2))) static bool cstr_to_intmax(const char *cstr, intmax_t *result)
Packit Service 569379
{
Packit Service 569379
	char *endptr = NULL;
Packit Service 569379
Packit Service 569379
	errno = 0;
Packit Service 569379
	*result = strtoimax(cstr, &endptr, 10);
Packit Service 569379
	// Check for underflow/overflow, strtoimax sets ERANGE in such case
Packit Service 569379
	if (errno == ERANGE) {
Packit Service 569379
		return false;
Packit Service 569379
	}
Packit Service 569379
	// Check whether there were some digits in the string
Packit Service 569379
	if (endptr == cstr) {
Packit Service 569379
		errno = EINVAL;
Packit Service 569379
		return false;
Packit Service 569379
	}
Packit Service 569379
	// Check whether the function used the whole string
Packit Service 569379
	if (*endptr != '\0') {
Packit Service 569379
		errno = EINVAL;
Packit Service 569379
		return false;
Packit Service 569379
	}
Packit Service 569379
	return true;
Packit Service 569379
}
Packit Service 569379
Packit Service 569379
__attribute__((nonnull(1,2))) static bool cstr_to_double(const char *cstr, double *result)
Packit Service 569379
{
Packit Service 569379
	char *endptr = NULL;
Packit Service 569379
Packit Service 569379
	errno = 0;
Packit Service 569379
	*result = strtod(cstr, &endptr);
Packit Service 569379
	// Check for underflow/overflow, strtoimax sets ERANGE in such case
Packit Service 569379
	if (errno == ERANGE) {
Packit Service 569379
		return false;
Packit Service 569379
	}
Packit Service 569379
	// Check whether there were some digits in the string
Packit Service 569379
	if (endptr == cstr) {
Packit Service 569379
		errno = EINVAL;
Packit Service 569379
		return false;
Packit Service 569379
	}
Packit Service 569379
	// Check whether the function used the whole string
Packit Service 569379
	if (*endptr != '\0') {
Packit Service 569379
		errno = EINVAL;
Packit Service 569379
		return false;
Packit Service 569379
	}
Packit Service 569379
	return true;
Packit Service 569379
}
Packit Service 569379
Packit Service 569379
oval_result_t oval_str_cmp_str(char *state_data, oval_datatype_t state_data_type, const char *sys_data, oval_operation_t operation)
Packit Service 569379
{
Packit Service 569379
	// finally, we have gotten to the point of comparing system data with a state
Packit Service 569379
Packit Service 569379
	if (state_data_type == OVAL_DATATYPE_STRING) {
Packit Service 569379
		return oval_string_cmp(state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_INTEGER) {
Packit Service 569379
		intmax_t state_val, syschar_val;
Packit Service 569379
Packit Service 569379
		if (!cstr_to_intmax(state_data, &state_val)) {
Packit Service 569379
			oscap_seterr(OSCAP_EFAMILY_OVAL,
Packit Service 569379
				"Conversion of the string \"%s\" to an integer (%u bits) failed: %s",
Packit Service 569379
				state_data, sizeof(intmax_t)*8, strerror(errno));
Packit Service 569379
			return OVAL_RESULT_ERROR;
Packit Service 569379
		}
Packit Service 569379
Packit Service 569379
		if (!cstr_to_intmax(sys_data, &syschar_val)) {
Packit Service 569379
			oscap_seterr(OSCAP_EFAMILY_OVAL,
Packit Service 569379
				"Conversion of the string \"%s\" to an integer (%u bits) failed: %s",
Packit Service 569379
				sys_data, sizeof(intmax_t)*8, strerror(errno));
Packit Service 569379
			return OVAL_RESULT_ERROR;
Packit Service 569379
		}
Packit Service 569379
		return oval_int_cmp(state_val, syschar_val, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_FLOAT) {
Packit Service 569379
		double state_val, sys_val;
Packit Service 569379
Packit Service 569379
		if (!cstr_to_double(state_data, &state_val)) {
Packit Service 569379
			oscap_seterr(OSCAP_EFAMILY_OVAL,
Packit Service 569379
				"Conversion of the string \"%s\" to a floating type (double) failed: %s",
Packit Service 569379
				state_data, strerror(errno));
Packit Service 569379
			return OVAL_RESULT_ERROR;
Packit Service 569379
		}
Packit Service 569379
Packit Service 569379
		if (!cstr_to_double(sys_data, &sys_val)) {
Packit Service 569379
			oscap_seterr(OSCAP_EFAMILY_OVAL,
Packit Service 569379
				"Conversion of the string \"%s\" to a floating type (double) failed: %s",
Packit Service 569379
				sys_data, strerror(errno));
Packit Service 569379
			return OVAL_RESULT_ERROR;
Packit Service 569379
		}
Packit Service 569379
		return oval_float_cmp(state_val, sys_val, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_BOOLEAN) {
Packit Service 569379
		int state_int;
Packit Service 569379
		int sys_int;
Packit Service 569379
		state_int = (((strcmp(state_data, "true")) == 0) || ((strcmp(state_data, "1")) == 0)) ? 1 : 0;
Packit Service 569379
		sys_int = (((strcmp(sys_data, "true")) == 0) || ((strcmp(sys_data, "1")) == 0)) ? 1 : 0;
Packit Service 569379
		return oval_boolean_cmp(state_int, sys_int, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_BINARY) {
Packit Service 569379
		return oval_binary_cmp(state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_EVR_STRING) {
Packit Service 569379
		return oval_evr_string_cmp(state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_DEBIAN_EVR_STRING) {
Packit Service 569379
		return oval_evr_string_cmp(state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_VERSION) {
Packit Service 569379
		return oval_versiontype_cmp(state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_IPV4ADDR) {
Packit Service 569379
		return oval_ipaddr_cmp(AF_INET, state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_IPV6ADDR) {
Packit Service 569379
		return oval_ipaddr_cmp(AF_INET6, state_data, sys_data, operation);
Packit Service 569379
	} else if (state_data_type == OVAL_DATATYPE_FILESET_REVISION
Packit Service 569379
			|| state_data_type == OVAL_DATATYPE_IOS_VERSION) {
Packit Service 569379
		dW("Unsupported data type: %s.", oval_datatype_get_text(state_data_type));
Packit Service 569379
		return OVAL_RESULT_NOT_EVALUATED;
Packit Service 569379
	}
Packit Service 569379
Packit Service 569379
	oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid OVAL data type: %d.", state_data_type);
Packit Service 569379
	return OVAL_RESULT_ERROR;
Packit Service 569379
}