Blame src/OVAL/results/oval_status_counter.c

Packit Service 569379
/*
Packit Service 569379
 * Copyright 2015 Red Hat Inc., Durham, North Carolina.
Packit Service 569379
 * All Rights Reserved.
Packit Service 569379
 *
Packit Service 569379
 * This library counter 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 counter 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
 *      "Jan Černý" <jcerny@redhat.com>
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 "../common/util.h"
Packit Service 569379
#include "oval_system_characteristics.h"
Packit Service 569379
#include "results/oval_status_counter.h"
Packit Service 569379
#include "common/_error.h"
Packit Service 569379
Packit Service 569379
void oval_status_counter_clear(struct oval_status_counter *counter)
Packit Service 569379
{
Packit Service 569379
	memset(counter, 0, sizeof(*counter));
Packit Service 569379
}
Packit Service 569379
Packit Service 569379
void oval_status_counter_add_status(struct oval_status_counter *counter, oval_syschar_status_t status)
Packit Service 569379
{
Packit Service 569379
	switch(status) {
Packit Service 569379
	case SYSCHAR_STATUS_ERROR:
Packit Service 569379
		counter->error_cnt++;
Packit Service 569379
		break;
Packit Service 569379
	case SYSCHAR_STATUS_EXISTS:
Packit Service 569379
		counter->exists_cnt++;
Packit Service 569379
		break;
Packit Service 569379
	case SYSCHAR_STATUS_DOES_NOT_EXIST:
Packit Service 569379
		counter->does_not_exist_cnt++;
Packit Service 569379
		break;
Packit Service 569379
	case SYSCHAR_STATUS_NOT_COLLECTED:
Packit Service 569379
		counter->not_collected_cnt++;
Packit Service 569379
		break;
Packit Service 569379
	default:
Packit Service 569379
		oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid oval status type: %s.", oval_syschar_status_get_text(status));
Packit Service 569379
		break;
Packit Service 569379
	}
Packit Service 569379
}
Packit Service 569379
Packit Service 569379
oval_result_t oval_status_counter_get_result(struct oval_status_counter *counter, oval_existence_t check_existence)
Packit Service 569379
{
Packit Service 569379
	oval_result_t result = OVAL_RESULT_UNKNOWN;
Packit Service 569379
	switch (check_existence) {
Packit Service 569379
	case OVAL_ALL_EXIST:
Packit Service 569379
		if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt == 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt == 0) {
Packit Service 569379
			result = OVAL_RESULT_TRUE;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt == 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt == 0) {
Packit Service 569379
			result = OVAL_RESULT_FALSE;
Packit Service 569379
		} else if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt >= 1
Packit Service 569379
			&& counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_FALSE;
Packit Service 569379
		} else if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt == 0
Packit Service 569379
			&& counter->error_cnt >= 1 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_ERROR;
Packit Service 569379
		} else if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt == 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt >= 1) {
Packit Service 569379
			result = OVAL_RESULT_UNKNOWN;
Packit Service 569379
		}
Packit Service 569379
		break;
Packit Service 569379
	case OVAL_ANY_EXIST:
Packit Service 569379
		if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_TRUE;
Packit Service 569379
		} else if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >=1 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_TRUE;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >=1 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_ERROR;
Packit Service 569379
		}
Packit Service 569379
		break;
Packit Service 569379
	case OVAL_AT_LEAST_ONE_EXISTS:
Packit Service 569379
		if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_TRUE;
Packit Service 569379
		} else if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt == 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt == 0) {
Packit Service 569379
			result = OVAL_RESULT_FALSE;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >= 1 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_ERROR;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt >= 1) {
Packit Service 569379
			result = OVAL_RESULT_UNKNOWN;
Packit Service 569379
		}
Packit Service 569379
		break;
Packit Service 569379
	case OVAL_NONE_EXIST:
Packit Service 569379
		if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt == 0) {
Packit Service 569379
			result = OVAL_RESULT_TRUE;
Packit Service 569379
		} else if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_FALSE;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >= 1 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_ERROR;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt >= 1) {
Packit Service 569379
			result = OVAL_RESULT_UNKNOWN;
Packit Service 569379
		}
Packit Service 569379
		break;
Packit Service 569379
	case OVAL_ONLY_ONE_EXISTS:
Packit Service 569379
		if (counter->exists_cnt == 1 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt == 0) {
Packit Service 569379
			result = OVAL_RESULT_TRUE;
Packit Service 569379
		} else if (counter->exists_cnt >= 2 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_FALSE;
Packit Service 569379
		} else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0
Packit Service 569379
			&& counter->error_cnt == 0 && counter->not_collected_cnt == 1) {
Packit Service 569379
			result = OVAL_RESULT_FALSE;
Packit Service 569379
		} else if ((counter->exists_cnt == 0 || counter->exists_cnt == 1)
Packit Service 569379
			&& counter->does_not_exist_cnt >= 0 && counter->error_cnt >= 1
Packit Service 569379
			&& counter->not_collected_cnt >= 0) {
Packit Service 569379
			result = OVAL_RESULT_ERROR;
Packit Service 569379
		} else if ((counter->exists_cnt == 0 || counter->exists_cnt == 1)
Packit Service 569379
			&& counter->does_not_exist_cnt >= 0 && counter->error_cnt == 0
Packit Service 569379
			&& counter->not_collected_cnt >= 1) {
Packit Service 569379
			result = OVAL_RESULT_ERROR;
Packit Service 569379
		}
Packit Service 569379
		break;
Packit Service 569379
	default:
Packit Service 569379
		oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid check_existence value: %s.",
Packit Service 569379
				oval_existence_get_text(check_existence));
Packit Service 569379
		result = OVAL_RESULT_ERROR;
Packit Service 569379
		break;
Packit Service 569379
	}
Packit Service 569379
	return result;
Packit Service 569379
}