Debian Codegraph @

src/XCCDF_POLICY/check_engine_plugin.c

raw symbols SCIP
1 /*
2 * Copyright 2013 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 * Author: Martin Preisler <[email protected]>
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "public/check_engine_plugin.h"
27 #include "common/util.h"
28 #include "oscap_helpers.h"
29 #include "common/_error.h"
30
31 #ifndef OS_WINDOWS
32 #include <dlfcn.h>
33 #endif
34
35 #define STRINGIZE_NX(A) #A
36 #define STRINGIZE(A) STRINGIZE_NX(A)
37
38 static struct check_engine_plugin_def *check_engine_plugin_def_new(void)
39 {
40 struct check_engine_plugin_def *ret = (struct check_engine_plugin_def*) calloc(1, sizeof(struct check_engine_plugin_def));
41 return ret;
42 }
43
44 static void check_engine_plugin_def_free(struct check_engine_plugin_def *plugin)
45 {
46 //if (plugin->module_handle)
47 // FIXME: Warning?
48
49 free(plugin);
50 }
51
52 struct check_engine_plugin_def *check_engine_plugin_load2(const char* path, bool quiet)
53 {
54 #ifndef OS_WINDOWS
55 struct check_engine_plugin_def *ret = check_engine_plugin_def_new();
56
57 const char *path_prefix = getenv("OSCAP_CHECK_ENGINE_PLUGIN_DIR");
58 char *full_path = path_prefix ? oscap_sprintf("%s/%s", path_prefix, path) : oscap_strdup(path);
59 // NB: valgrind reports a leak on the next line, I have confirmed this to be a false positive
60 ret->module_handle = dlopen(full_path, RTLD_LAZY);
61 free(full_path);
62
63 char *error = NULL;
64 if (!ret->module_handle) {
65 error = dlerror();
66
67 if (!quiet)
68 oscap_seterr(OSCAP_EFAMILY_GLIBC,
69 "Failed to load extra check engine from '%s'. Details: '%s'.",
70 path, error);
71
72 check_engine_plugin_def_free(ret);
73 return NULL;
74 }
75
76 check_engine_plugin_entry_fn entry_fn = NULL;
77 *(void **)(&entry_fn) = dlsym(ret->module_handle, STRINGIZE(OPENSCAP_CHECK_ENGINE_PLUGIN_ENTRY));
78
79 if ((error = dlerror()) != NULL) {
80 if (!quiet)
81 oscap_seterr(OSCAP_EFAMILY_GLIBC,
82 "Failed to retrieve module entry '%s' from loaded extra check engine '%s'. Details: '%s'.",
83 STRINGIZE(OPENSCAP_CHECK_ENGINE_PLUGIN_ENTRY), path, error);
84
85 dlclose(ret->module_handle);
86 check_engine_plugin_def_free(ret);
87 return NULL;
88 }
89
90 if ((*entry_fn)(ret) != 0) {
91 if (!quiet)
92 oscap_seterr(OSCAP_EFAMILY_GLIBC,
93 "Failed to fill check_engine_plugin_def when loading check engine plugin '%s'.", path);
94
95 dlclose(ret->module_handle);
96 check_engine_plugin_def_free(ret);
97 return NULL;
98 }
99
100 return ret;
101 #else
102 // TODO
103 return NULL;
104 #endif
105 }
106
107 struct check_engine_plugin_def *check_engine_plugin_load(const char* path)
108 {
109 return check_engine_plugin_load2(path, false);
110 }
111
112 void check_engine_plugin_unload(struct check_engine_plugin_def *plugin)
113 {
114 #ifndef OS_WINDOWS
115 if (!plugin->module_handle) {
116 oscap_seterr(OSCAP_EFAMILY_GLIBC,
117 "Failed to unload this check engine plugin. It seems the plugin hasn't been loaded!");
118 }
119 else {
120 dlclose(plugin->module_handle);
121 plugin->module_handle = NULL;
122 }
123 #else
124 // TODO
125 #endif
126
127 check_engine_plugin_def_free(plugin);
128 }
129
130 int check_engine_plugin_register(struct check_engine_plugin_def *plugin, struct xccdf_policy_model *model, const char* path_hint)
131 {
132 if (!plugin->module_handle) {
133 oscap_seterr(OSCAP_EFAMILY_GLIBC,
134 "Failed to register this check engine plugin to given policy_model, the plugin hasn't been loaded!");
135
136 return -1;
137 }
138
139 if (!plugin->register_fn) {
140 oscap_seterr(OSCAP_EFAMILY_GLIBC,
141 "Plugin seems to have been loaded but its register_fn member hasn't been filled. Bad plugin entry function implementation suspected.");
142
143 return -1;
144 }
145
146 return (plugin->register_fn)(model, path_hint, &plugin->user_data);
147 }
148
149 int check_engine_plugin_cleanup(struct check_engine_plugin_def *plugin, struct xccdf_policy_model *model)
150 {
151 if (!plugin->module_handle) {
152 oscap_seterr(OSCAP_EFAMILY_GLIBC,
153 "Failed to cleanup this check engine plugin, the plugin hasn't been loaded!");
154
155 return -1;
156 }
157
158 if (!plugin->cleanup_fn) {
159 oscap_seterr(OSCAP_EFAMILY_GLIBC,
160 "Plugin seems to have been loaded but its cleanup_fn member hasn't been filled. Bad plugin entry function implementation suspected.");
161
162 return -1;
163 }
164
165 return (plugin->cleanup_fn)(model, &plugin->user_data);
166 }
167
168 int check_engine_plugin_export_results(struct check_engine_plugin_def *plugin, struct xccdf_policy_model *model, bool validate, const char *path_hint)
169 {
170 if (!plugin->module_handle) {
171 oscap_seterr(OSCAP_EFAMILY_GLIBC,
172 "Failed to export results from this check engine plugin, the plugin hasn't been loaded!");
173
174 return -1;
175 }
176
177 if (!plugin->export_results_fn) {
178 oscap_seterr(OSCAP_EFAMILY_GLIBC,
179 "Plugin seems to have been loaded but its export_results_fn member hasn't been filled. Bad plugin entry function implementation suspected.");
180
181 return -1;
182 }
183
184 return (plugin->export_results_fn)(model, validate, path_hint, &plugin->user_data);
185 }
186
187 const char *check_engine_plugin_get_capabilities(struct check_engine_plugin_def *plugin)
188 {
189 if (!plugin->module_handle) {
190 oscap_seterr(OSCAP_EFAMILY_GLIBC,
191 "Failed to get capabilities of this check engine plugin, the plugin hasn't been loaded!");
192
193 return NULL;
194 }
195
196 if (!plugin->get_capabilities_fn) {
197 oscap_seterr(OSCAP_EFAMILY_GLIBC,
198 "Plugin seems to have been loaded but its get_capabilities_fn member hasn't been filled. Bad plugin entry function implementation suspected.");
199
200 return NULL;
201 }
202
203 return (plugin->get_capabilities_fn)(&plugin->user_data);
204 }
205
206 #ifndef LT_CURRENT_MINUS_AGE
207 #error "LT_CURRENT_MINUS_AGE has not been defined! It is necessary to figure out plugin paths to load."
208 #endif
209
210 const char * const known_plugins[] = {"libopenscap_sce.so." STRINGIZE(LT_CURRENT_MINUS_AGE), NULL};
211
212 const char * const *check_engine_plugin_get_known_plugins(void)
213 {
214 return known_plugins;
215 }