| 1 | /* |
| 2 | * Copyright 2009-2010 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 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <limits.h> |
| 31 | #include <errno.h> |
| 32 | #if defined(OSCAP_THREAD_SAFE) |
| 33 | # include <pthread.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "oscap_helpers.h" |
| 37 | #include "oval_probe.h" |
| 38 | #include "oval_system_characteristics.h" |
| 39 | #include "common/_error.h" |
| 40 | |
| 41 | #include "oval_probe_impl.h" |
| 42 | #include "oval_system_characteristics_impl.h" |
| 43 | #include "common/util.h" |
| 44 | #include "common/bfind.h" |
| 45 | #include "common/debug_priv.h" |
| 46 | |
| 47 | #include "_oval_probe_session.h" |
| 48 | #include "_oval_probe_handler.h" |
| 49 | #include "oval_probe_ext.h" |
| 50 | #include "collectVarRefs_impl.h" |
| 51 | |
| 52 | #ifdef OS_WINDOWS |
| 53 | #define X_OK 0 |
| 54 | #endif |
| 55 | |
| 56 | /* |
| 57 | * Library side entity name cache. Initialization needs to be |
| 58 | * thread-safe and is done by oval_probe_session_new. Freeing |
| 59 | * of memory used by this cache is done at exit using a hook |
| 60 | * registered with atexit(). |
| 61 | */ |
| 62 | probe_ncache_t *OSCAP_GSYM(ncache) = NULL; |
| 63 | struct id_desc_t OSCAP_GSYM(id_desc); |
| 64 | |
| 65 | #define __ERRBUF_SIZE 128 |
| 66 | |
| 67 | static void _syschar_add_bindings(struct oval_syschar *sc, struct oval_string_map *vm) |
| 68 | { |
| 69 | struct oval_iterator *var_itr; |
| 70 | |
| 71 | var_itr = oval_string_map_values(vm); |
| 72 | while (oval_collection_iterator_has_more(var_itr)) { |
| 73 | struct oval_variable *var; |
| 74 | struct oval_value_iterator *val_itr; |
| 75 | struct oval_variable_binding *binding; |
| 76 | |
| 77 | var = oval_collection_iterator_next(var_itr); |
| 78 | binding = oval_variable_binding_new(var, NULL); |
| 79 | |
| 80 | val_itr = oval_variable_get_values(var); |
| 81 | while (oval_value_iterator_has_more(val_itr)) { |
| 82 | struct oval_value *val; |
| 83 | char *txt; |
| 84 | |
| 85 | val = oval_value_iterator_next(val_itr); |
| 86 | txt = oval_value_get_text(val); |
| 87 | txt = oscap_strdup(txt); |
| 88 | oval_variable_binding_add_value(binding, txt); |
| 89 | } |
| 90 | oval_value_iterator_free(val_itr); |
| 91 | |
| 92 | oval_syschar_add_variable_binding(sc, binding); |
| 93 | } |
| 94 | oval_collection_iterator_free(var_itr); |
| 95 | } |
| 96 | |
| 97 | int oval_probe_query_object(oval_probe_session_t *psess, struct oval_object *object, int flags, struct oval_syschar **out_syschar) |
| 98 | { |
| 99 | char *oid; |
| 100 | struct oval_syschar *sysc; |
| 101 | oval_subtype_t type; |
| 102 | const char *type_name; |
| 103 | oval_ph_t *ph; |
| 104 | struct oval_string_map *vm; |
| 105 | struct oval_syschar_model *model; |
| 106 | int ret; |
| 107 | |
| 108 | oid = oval_object_get_id(object); |
| 109 | model = psess->sys_model; |
| 110 | |
| 111 | type = oval_object_get_subtype(object); |
| 112 | type_name = oval_subtype_get_text(type); |
| 113 | dI("Querying %s object '%s', flags: %u.", type_name, oid, flags); |
| 114 | |
| 115 | sysc = oval_syschar_model_get_syschar(model, oid); |
| 116 | if (sysc != NULL) { |
| 117 | int variable_instance_hint = oval_syschar_get_variable_instance_hint(sysc); |
| 118 | if (oval_syschar_get_variable_instance_hint(sysc) != oval_syschar_get_variable_instance(sysc)) { |
| 119 | dI("Creating another syschar for variable_instance=%d)", variable_instance_hint); |
| 120 | sysc = oval_syschar_new(model, object); |
| 121 | oval_syschar_set_variable_instance(sysc, variable_instance_hint); |
| 122 | oval_syschar_set_variable_instance_hint(sysc, variable_instance_hint); |
| 123 | } |
| 124 | else { |
| 125 | oval_syschar_collection_flag_t sc_flg = oval_syschar_get_flag(sysc); |
| 126 | const char *flag_text = oval_syschar_collection_flag_get_text(sc_flg); |
| 127 | dI("System characteristics for %s_object '%s' already exist, flag: %s.", type_name, oid, flag_text); |
| 128 | |
| 129 | if (sc_flg != SYSCHAR_FLAG_UNKNOWN || (flags & OVAL_PDFLAG_NOREPLY)) { |
| 130 | if (out_syschar) |
| 131 | *out_syschar = sysc; |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | dI("Creating new syschar for %s_object '%s'.", type_name, oid); |
| 137 | sysc = oval_syschar_new(model, object); |
| 138 | } |
| 139 | |
| 140 | if (out_syschar) |
| 141 | *out_syschar = sysc; |
| 142 | |
| 143 | ph = oval_probe_handler_get(psess->ph, type); |
| 144 | |
| 145 | if (ph == NULL) { |
| 146 | char *msg = oscap_sprintf("OVAL object '%s_object' is not supported.", type_name); |
| 147 | |
| 148 | dW("%s", msg); |
| 149 | oval_syschar_add_new_message(sysc, msg, OVAL_MESSAGE_LEVEL_WARNING); |
| 150 | free(msg); |
| 151 | oval_syschar_set_flag(sysc, SYSCHAR_FLAG_NOT_COLLECTED); |
| 152 | |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | if ((ret = oval_probe_ext_handler(type, ph->uptr, PROBE_HANDLER_ACT_EVAL, sysc, flags)) != 0) { |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | if (!(flags & OVAL_PDFLAG_NOREPLY)) { |
| 161 | vm = oval_string_map_new(); |
| 162 | oval_obj_collect_var_refs(object, vm); |
| 163 | _syschar_add_bindings(sysc, vm); |
| 164 | oval_string_map_free(vm, NULL); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | int oval_probe_query_sysinfo(oval_probe_session_t *sess, struct oval_sysinfo **out_sysinfo) |
| 171 | { |
| 172 | struct oval_sysinfo *sysinf; |
| 173 | oval_ph_t *ph; |
| 174 | int ret; |
| 175 | |
| 176 | dI("Querying system information."); |
| 177 | |
| 178 | ph = oval_probe_handler_get(sess->ph, OVAL_INDEPENDENT_SYSCHAR_SUBTYPE); |
| 179 | |
| 180 | if (ph == NULL) { |
| 181 | oscap_seterr (OSCAP_EFAMILY_OVAL, "OVAL object not supported"); |
| 182 | return(-1); |
| 183 | } |
| 184 | |
| 185 | if (ph->func == NULL) { |
| 186 | oscap_seterr (OSCAP_EFAMILY_OVAL, "OVAL object not correctly defined"); |
| 187 | return(-1); |
| 188 | } |
| 189 | |
| 190 | sysinf = NULL; |
| 191 | |
| 192 | ret = oval_probe_sys_handler(OVAL_INDEPENDENT_SYSCHAR_SUBTYPE, ph->uptr, PROBE_HANDLER_ACT_EVAL, NULL, &sysinf, 0); |
| 193 | if (ret != 0) |
| 194 | return(ret); |
| 195 | |
| 196 | *out_sysinfo = sysinf; |
| 197 | return(0); |
| 198 | } |
| 199 | |
| 200 | static int oval_probe_query_var_ref(oval_probe_session_t *sess, struct oval_state *state) |
| 201 | { |
| 202 | struct oval_state_content_iterator *contents = oval_state_get_contents(state); |
| 203 | while (oval_state_content_iterator_has_more(contents)) { |
| 204 | struct oval_state_content *content = oval_state_content_iterator_next(contents); |
| 205 | struct oval_entity * entity = oval_state_content_get_entity(content); |
| 206 | if (oval_entity_get_varref_type(entity) == OVAL_ENTITY_VARREF_ATTRIBUTE) { |
| 207 | oval_syschar_collection_flag_t flag; |
| 208 | struct oval_variable *var = oval_entity_get_variable(entity); |
| 209 | const char *state_id = oval_state_get_id(state); |
| 210 | oval_variable_type_t var_type = oval_variable_get_type(var); |
| 211 | const char *var_type_text = oval_variable_type_get_text(var_type); |
| 212 | const char *var_id = oval_variable_get_id(var); |
| 213 | dI("State '%s' references %s '%s'.", state_id, |
| 214 | var_type_text, var_id); |
| 215 | |
| 216 | int ret = oval_probe_query_variable(sess, var); |
| 217 | if (ret == -1) { |
| 218 | oval_state_content_iterator_free(contents); |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | flag = oval_variable_get_collection_flag(var); |
| 223 | switch (flag) { |
| 224 | case SYSCHAR_FLAG_COMPLETE: |
| 225 | case SYSCHAR_FLAG_INCOMPLETE: |
| 226 | break; |
| 227 | default: |
| 228 | oval_state_content_iterator_free(contents); |
| 229 | return 0; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | oval_state_content_iterator_free(contents); |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | int oval_probe_query_test(oval_probe_session_t *sess, struct oval_test *test) |
| 238 | { |
| 239 | struct oval_object *object; |
| 240 | struct oval_state_iterator *ste_itr; |
| 241 | int ret; |
| 242 | oval_subtype_t test_subtype, object_subtype; |
| 243 | |
| 244 | object = oval_test_get_object(test); |
| 245 | if (object == NULL) |
| 246 | return 0; |
| 247 | test_subtype = oval_test_get_subtype(test); |
| 248 | object_subtype = oval_object_get_subtype(object); |
| 249 | if (test_subtype != object_subtype) { |
| 250 | oscap_seterr(OSCAP_EFAMILY_OVAL, "%s_test '%s' is not compatible with %s_object '%s'.", |
| 251 | oval_subtype_to_str(test_subtype), oval_test_get_id(test), |
| 252 | oval_subtype_to_str(object_subtype), oval_object_get_id(object)); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* probe object */ |
| 257 | ret = oval_probe_query_object(sess, object, 0, NULL); |
| 258 | if (ret == -1) |
| 259 | return ret; |
| 260 | /* probe objects referenced like this: test->state->variable->object */ |
| 261 | ste_itr = oval_test_get_states(test); |
| 262 | while (oval_state_iterator_has_more(ste_itr)) { |
| 263 | struct oval_state *state = oval_state_iterator_next(ste_itr); |
| 264 | ret = oval_probe_query_var_ref(sess, state); |
| 265 | if (ret < 1) { |
| 266 | oval_state_iterator_free(ste_itr); |
| 267 | return ret; |
| 268 | } |
| 269 | } |
| 270 | oval_state_iterator_free(ste_itr); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 |