| 1 | /** |
| 2 | * @file oval_probe_session.c |
| 3 | * @brief OVAL probe session API implementation |
| 4 | * @author "Daniel Kopecek" <[email protected]> |
| 5 | * |
| 6 | * @addtogroup PROBESESSION |
| 7 | * @{ |
| 8 | */ |
| 9 | /* |
| 10 | * Copyright 2010 Red Hat Inc., Durham, North Carolina. |
| 11 | * All Rights Reserved. |
| 12 | * |
| 13 | * This library is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU Lesser General Public |
| 15 | * License as published by the Free Software Foundation; either |
| 16 | * version 2.1 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This library is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * Lesser General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU Lesser General Public |
| 24 | * License along with this library; if not, write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | * |
| 27 | * Authors: |
| 28 | * "Daniel Kopecek" <[email protected]> |
| 29 | */ |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <errno.h> |
| 36 | #include <string.h> |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | #include "common/_error.h" |
| 40 | #include "common/bfind.h" |
| 41 | #include "common/debug_priv.h" |
| 42 | |
| 43 | |
| 44 | #include "public/oval_definitions.h" |
| 45 | #include "_oval_probe_session.h" |
| 46 | #include "_oval_probe_handler.h" |
| 47 | #include "oval_probe_impl.h" |
| 48 | #include "oval_probe_ext.h" |
| 49 | #include "probe-table.h" |
| 50 | #include "oval_types.h" |
| 51 | #include "crapi/crapi.h" |
| 52 | |
| 53 | #if defined(OSCAP_THREAD_SAFE) |
| 54 | #include <pthread.h> |
| 55 | static pthread_once_t __oval_probe_session_init_once = PTHREAD_ONCE_INIT; |
| 56 | #else |
| 57 | static volatile int __oval_probe_session_init_once = 0; |
| 58 | #endif /* OSCAP_THREAD_SAFE */ |
| 59 | |
| 60 | /** |
| 61 | * Entity name cache exit hook. This hook is registered using |
| 62 | * atexit(3) during initialization and ensures that the element |
| 63 | * name cache is freed before exit. |
| 64 | */ |
| 65 | static void ncache_libfree(void) |
| 66 | { |
| 67 | probe_ncache_free(OSCAP_GSYM(ncache)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Initialize element name cache and register an exit hook. |
| 72 | */ |
| 73 | static void ncache_libinit(void) |
| 74 | { |
| 75 | if (OSCAP_GSYM(ncache) == NULL) { |
| 76 | OSCAP_GSYM(ncache) = probe_ncache_new(); |
| 77 | atexit(ncache_libfree); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void oval_probe_session_libinit(void) |
| 82 | { |
| 83 | /* HACK: Make sure S-exps are initialized before we initialize anything else |
| 84 | * that uses them. This is needed only if the S-exp library is compiled without |
| 85 | * atomic builtins. In that case it uses a fallback locking mechanism that uses |
| 86 | * a mutex array which needs to be initialized before S-exp are used and freed |
| 87 | * at exit(3). The later is done by registering an atexit(3) function. |
| 88 | * The ncache_libinit function also registers an atexit(3) function and we need |
| 89 | * to make sure that the S-exps are not deinitialized before the ncache atexit |
| 90 | * handler is called. |
| 91 | * TODO: Implement SEXP_libinit() and call it from oscap_init()? |
| 92 | */ |
| 93 | volatile SEXP_t *exp = SEXP_string_new("magic", 5); |
| 94 | SEXP_free((SEXP_t *)exp); |
| 95 | |
| 96 | ncache_libinit(); |
| 97 | /* |
| 98 | * Initialize crypto API |
| 99 | */ |
| 100 | #ifndef OS_WINDOWS |
| 101 | if (crapi_init (NULL) != 0) |
| 102 | return; |
| 103 | #endif |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Initialize the element name cache. This function can be called repeatedly |
| 108 | * from various probe system entry points to ensure that the cache is initialized. |
| 109 | * If OSCAP_THREAD_SAFE is defined at compilation time, the pthread_once call |
| 110 | * is used to ensure that the initialization is done just once. Otherwise a |
| 111 | * volatile integer flag is used. |
| 112 | */ |
| 113 | static void __init_once(void) |
| 114 | { |
| 115 | #if defined(OSCAP_THREAD_SAFE) |
| 116 | if (pthread_once(&__oval_probe_session_init_once, |
| 117 | &oval_probe_session_libinit) != 0) |
| 118 | abort(); |
| 119 | #else |
| 120 | if (__oval_probe_session_init_once == 0) { |
| 121 | oval_probe_session_libinit(); |
| 122 | __oval_probe_session_init_once = 1; |
| 123 | } |
| 124 | #endif |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | static void oval_probe_session_init(oval_probe_session_t *sess, struct oval_syschar_model *model) |
| 129 | { |
| 130 | sess->ph = oval_phtbl_new(); |
| 131 | sess->sys_model = model; |
| 132 | sess->flg = 0; |
| 133 | sess->pext = oval_pext_new(); |
| 134 | sess->pext->model = &sess->sys_model; |
| 135 | sess->pext->sess_ptr = sess; |
| 136 | |
| 137 | __init_once(); |
| 138 | |
| 139 | oval_probe_handler_t *probe_handler; |
| 140 | int probe_count = probe_table_size(); |
| 141 | for (int i = 0; i < probe_count; i++) { |
| 142 | oval_subtype_t type = probe_table_at_index(i); |
| 143 | if (type == OVAL_INDEPENDENT_SYSCHAR_SUBTYPE) { |
| 144 | probe_handler = &oval_probe_sys_handler; |
| 145 | } else { |
| 146 | probe_handler = &oval_probe_ext_handler; |
| 147 | } |
| 148 | oval_probe_handler_set(sess->ph, type, probe_handler, sess->pext); |
| 149 | } |
| 150 | |
| 151 | oval_probe_handler_set(sess->ph, OVAL_SUBTYPE_ALL, oval_probe_ext_handler, sess->pext); /* special case for reset */ |
| 152 | } |
| 153 | |
| 154 | oval_probe_session_t *oval_probe_session_new(struct oval_syschar_model *model) |
| 155 | { |
| 156 | oval_probe_session_t *sess = malloc(sizeof(oval_probe_session_t)); |
| 157 | oval_probe_session_init(sess, model); |
| 158 | return sess; |
| 159 | } |
| 160 | |
| 161 | static void oval_probe_session_free(oval_probe_session_t *sess) |
| 162 | { |
| 163 | if (sess == NULL) { |
| 164 | dE("Invalid session (NULL)"); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | oval_phtbl_free(sess->ph); |
| 169 | oval_pext_free(sess->pext); |
| 170 | } |
| 171 | |
| 172 | void oval_probe_session_reinit(oval_probe_session_t *sess, struct oval_syschar_model *model) |
| 173 | { |
| 174 | oval_probe_session_free(sess); |
| 175 | |
| 176 | oval_probe_session_init(sess, model); |
| 177 | } |
| 178 | |
| 179 | void oval_probe_session_destroy(oval_probe_session_t *sess) |
| 180 | { |
| 181 | oval_probe_session_free(sess); |
| 182 | free(sess); |
| 183 | } |
| 184 | |
| 185 | int oval_probe_session_reset(oval_probe_session_t *sess, struct oval_syschar_model *sysch) |
| 186 | { |
| 187 | oval_ph_t *ph; |
| 188 | |
| 189 | if ((ph = oval_probe_handler_get(sess->ph, OVAL_SUBTYPE_ALL)) == NULL) { |
| 190 | dE("No probe handler for OVAL_SUBTYPE_ALL"); |
| 191 | return (-1); |
| 192 | } |
| 193 | |
| 194 | if (ph->func(OVAL_SUBTYPE_ALL, ph->uptr, PROBE_HANDLER_ACT_RESET) != 0) { |
| 195 | return(-1); |
| 196 | } |
| 197 | if (sysch != NULL) |
| 198 | sess->sys_model = sysch; |
| 199 | |
| 200 | return(0); |
| 201 | } |
| 202 | |
| 203 | int oval_probe_session_abort(oval_probe_session_t *sess) |
| 204 | { |
| 205 | oval_ph_t *ph; |
| 206 | |
| 207 | if ((ph = oval_probe_handler_get(sess->ph, OVAL_SUBTYPE_ALL) ) == NULL) { |
| 208 | dE("No probe handler for OVAL_SUBTYPE_ALL"); |
| 209 | return (-1); |
| 210 | } |
| 211 | |
| 212 | return ph->func(OVAL_SUBTYPE_ALL, ph->uptr, PROBE_HANDLER_ACT_ABORT); |
| 213 | } |
| 214 | |
| 215 | struct oval_syschar_model *oval_probe_session_getmodel(oval_probe_session_t *sess) |
| 216 | { |
| 217 | if (sess == NULL) { |
| 218 | dE("Invalid session (NULL)"); |
| 219 | return (NULL); |
| 220 | } |
| 221 | |
| 222 | return (sess->sys_model); |
| 223 | } |
| 224 | |
| 225 | /// @} |