| 1 | /* |
| 2 | * Copyright 2015 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library counter 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 counter 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 | * "Jan Černý" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include "../common/util.h" |
| 29 | #include "oval_system_characteristics.h" |
| 30 | #include "results/oval_status_counter.h" |
| 31 | #include "common/_error.h" |
| 32 | |
| 33 | void oval_status_counter_clear(struct oval_status_counter *counter) |
| 34 | { |
| 35 | memset(counter, 0, sizeof(*counter)); |
| 36 | } |
| 37 | |
| 38 | void oval_status_counter_add_status(struct oval_status_counter *counter, oval_syschar_status_t status) |
| 39 | { |
| 40 | switch(status) { |
| 41 | case SYSCHAR_STATUS_ERROR: |
| 42 | counter->error_cnt++; |
| 43 | break; |
| 44 | case SYSCHAR_STATUS_EXISTS: |
| 45 | counter->exists_cnt++; |
| 46 | break; |
| 47 | case SYSCHAR_STATUS_DOES_NOT_EXIST: |
| 48 | counter->does_not_exist_cnt++; |
| 49 | break; |
| 50 | case SYSCHAR_STATUS_NOT_COLLECTED: |
| 51 | counter->not_collected_cnt++; |
| 52 | break; |
| 53 | default: |
| 54 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid oval status type: %s.", oval_syschar_status_get_text(status)); |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | oval_result_t oval_status_counter_get_result(struct oval_status_counter *counter, oval_existence_t check_existence) |
| 60 | { |
| 61 | oval_result_t result = OVAL_RESULT_UNKNOWN; |
| 62 | switch (check_existence) { |
| 63 | case OVAL_ALL_EXIST: |
| 64 | if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt == 0 |
| 65 | && counter->error_cnt == 0 && counter->not_collected_cnt == 0) { |
| 66 | result = OVAL_RESULT_TRUE; |
| 67 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt == 0 |
| 68 | && counter->error_cnt == 0 && counter->not_collected_cnt == 0) { |
| 69 | result = OVAL_RESULT_FALSE; |
| 70 | } else if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt >= 1 |
| 71 | && counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) { |
| 72 | result = OVAL_RESULT_FALSE; |
| 73 | } else if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt == 0 |
| 74 | && counter->error_cnt >= 1 && counter->not_collected_cnt >= 0) { |
| 75 | result = OVAL_RESULT_ERROR; |
| 76 | } else if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt == 0 |
| 77 | && counter->error_cnt == 0 && counter->not_collected_cnt >= 1) { |
| 78 | result = OVAL_RESULT_UNKNOWN; |
| 79 | } |
| 80 | break; |
| 81 | case OVAL_ANY_EXIST: |
| 82 | if (counter->exists_cnt >= 0 && counter->does_not_exist_cnt >= 0 |
| 83 | && counter->error_cnt == 0 && counter->not_collected_cnt >= 0) { |
| 84 | result = OVAL_RESULT_TRUE; |
| 85 | } else if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt >= 0 |
| 86 | && counter->error_cnt >=1 && counter->not_collected_cnt >= 0) { |
| 87 | result = OVAL_RESULT_TRUE; |
| 88 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 89 | && counter->error_cnt >=1 && counter->not_collected_cnt >= 0) { |
| 90 | result = OVAL_RESULT_ERROR; |
| 91 | } |
| 92 | break; |
| 93 | case OVAL_AT_LEAST_ONE_EXISTS: |
| 94 | if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt >= 0 |
| 95 | && counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) { |
| 96 | result = OVAL_RESULT_TRUE; |
| 97 | } else if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt == 0 |
| 98 | && counter->error_cnt == 0 && counter->not_collected_cnt == 0) { |
| 99 | result = OVAL_RESULT_FALSE; |
| 100 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 101 | && counter->error_cnt >= 1 && counter->not_collected_cnt >= 0) { |
| 102 | result = OVAL_RESULT_ERROR; |
| 103 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 104 | && counter->error_cnt == 0 && counter->not_collected_cnt >= 1) { |
| 105 | result = OVAL_RESULT_UNKNOWN; |
| 106 | } |
| 107 | break; |
| 108 | case OVAL_NONE_EXIST: |
| 109 | if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 110 | && counter->error_cnt == 0 && counter->not_collected_cnt == 0) { |
| 111 | result = OVAL_RESULT_TRUE; |
| 112 | } else if (counter->exists_cnt >= 1 && counter->does_not_exist_cnt >= 0 |
| 113 | && counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) { |
| 114 | result = OVAL_RESULT_FALSE; |
| 115 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 116 | && counter->error_cnt >= 1 && counter->not_collected_cnt >= 0) { |
| 117 | result = OVAL_RESULT_ERROR; |
| 118 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 119 | && counter->error_cnt == 0 && counter->not_collected_cnt >= 1) { |
| 120 | result = OVAL_RESULT_UNKNOWN; |
| 121 | } |
| 122 | break; |
| 123 | case OVAL_ONLY_ONE_EXISTS: |
| 124 | if (counter->exists_cnt == 1 && counter->does_not_exist_cnt >= 0 |
| 125 | && counter->error_cnt == 0 && counter->not_collected_cnt == 0) { |
| 126 | result = OVAL_RESULT_TRUE; |
| 127 | } else if (counter->exists_cnt >= 2 && counter->does_not_exist_cnt >= 0 |
| 128 | && counter->error_cnt >= 0 && counter->not_collected_cnt >= 0) { |
| 129 | result = OVAL_RESULT_FALSE; |
| 130 | } else if (counter->exists_cnt == 0 && counter->does_not_exist_cnt >= 0 |
| 131 | && counter->error_cnt == 0 && counter->not_collected_cnt == 1) { |
| 132 | result = OVAL_RESULT_FALSE; |
| 133 | } else if ((counter->exists_cnt == 0 || counter->exists_cnt == 1) |
| 134 | && counter->does_not_exist_cnt >= 0 && counter->error_cnt >= 1 |
| 135 | && counter->not_collected_cnt >= 0) { |
| 136 | result = OVAL_RESULT_ERROR; |
| 137 | } else if ((counter->exists_cnt == 0 || counter->exists_cnt == 1) |
| 138 | && counter->does_not_exist_cnt >= 0 && counter->error_cnt == 0 |
| 139 | && counter->not_collected_cnt >= 1) { |
| 140 | result = OVAL_RESULT_ERROR; |
| 141 | } |
| 142 | break; |
| 143 | default: |
| 144 | oscap_seterr(OSCAP_EFAMILY_OVAL, "Invalid check_existence value: %s.", |
| 145 | oval_existence_get_text(check_existence)); |
| 146 | result = OVAL_RESULT_ERROR; |
| 147 | break; |
| 148 | } |
| 149 | return result; |
| 150 | } |