Debian Codegraph @

src/OVAL/probes/unix/linux/systemdunitproperty_probe.c

raw symbols SCIP
1 /**
2 * @file systemdunitproperty_probe.c
3 * @brief implementation of the systemdunitproperty_object
4 * @author
5 */
6
7 /*
8 * Copyright 2014 Red Hat Inc., Durham, North Carolina.
9 * All Rights Reserved.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 *
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <probe-api.h>
34 #include <string.h>
35 #include <probe/probe.h>
36
37 #include "probe/entcmp.h"
38 #include "systemdshared.h"
39 #include "systemdunitproperty_probe.h"
40
41 static int get_all_properties_by_unit_path(DBusConnection *conn, const char *unit_path, int(*callback)(const char *name, const char *value, void *arg), void *cbarg)
42 {
43 int ret = 1;
44 DBusMessage *msg = NULL;
45 DBusPendingCall *pending = NULL;
46
47 msg = dbus_message_new_method_call(
48 "org.freedesktop.systemd1",
49 unit_path,
50 "org.freedesktop.DBus.Properties",
51 "GetAll"
52 );
53 if (msg == NULL) {
54 dD("Failed to create dbus_message via dbus_message_new_method_call!");
55 goto cleanup;
56 }
57
58 DBusMessageIter args, property_iter;
59
60 const char *interface = "org.freedesktop.systemd1.Unit";
61
62 dbus_message_iter_init_append(msg, &args);
63 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface)) {
64 dD("Failed to append interface '%s' string parameter to dbus message!", interface);
65 goto cleanup;
66 }
67
68 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
69 dD("Failed to send message via dbus!");
70 goto cleanup;
71 }
72 if (pending == NULL) {
73 dD("Invalid dbus pending call!");
74 goto cleanup;
75 }
76
77 dbus_connection_flush(conn);
78 dbus_message_unref(msg); msg = NULL;
79
80 dbus_pending_call_block(pending);
81 msg = dbus_pending_call_steal_reply(pending);
82 if (msg == NULL) {
83 dD("Failed to steal dbus pending call reply.");
84 goto cleanup;
85 }
86 dbus_pending_call_unref(pending); pending = NULL;
87
88 if (!dbus_message_iter_init(msg, &args)) {
89 dD("Failed to initialize iterator over received dbus message.");
90 goto cleanup;
91 }
92
93 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY && dbus_message_iter_get_element_type(&args) != DBUS_TYPE_DICT_ENTRY) {
94 dD("Expected array of dict_entry argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
95 goto cleanup;
96 }
97
98 dbus_message_iter_recurse(&args, &property_iter);
99 do {
100 DBusMessageIter dict_entry, value_variant;
101 dbus_message_iter_recurse(&property_iter, &dict_entry);
102
103 if (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_STRING) {
104 dD("Expected string as key in dict_entry. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&dict_entry)));
105 goto cleanup;
106 }
107
108 _DBusBasicValue value;
109 dbus_message_iter_get_basic(&dict_entry, &value);
110 char *property_name = oscap_strdup(value.str);
111
112 if (dbus_message_iter_next(&dict_entry) == false) {
113 dW("Expected another field in dict_entry.");
114 free(property_name);
115 goto cleanup;
116 }
117
118 if (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_VARIANT) {
119 dD("Expected variant as value in dict_entry. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&dict_entry)));
120 free(property_name);
121 goto cleanup;
122 }
123
124 dbus_message_iter_recurse(&dict_entry, &value_variant);
125
126 int cbret = 0;
127 const int arg_type = dbus_message_iter_get_arg_type(&value_variant);
128 // DBUS_TYPE_ARRAY is a special case, we report each element as one value entry
129 if (arg_type == DBUS_TYPE_ARRAY) {
130 DBusMessageIter array;
131 dbus_message_iter_recurse(&value_variant, &array);
132
133 do {
134 char *element = oval_dbus_value_to_string(&array);
135 if (element == NULL)
136 continue;
137
138 const int elementcbret = callback(property_name, element, cbarg);
139 if (elementcbret > cbret)
140 cbret = elementcbret;
141
142 free(element);
143 }
144 while (dbus_message_iter_next(&array));
145 }
146 else {
147 char *property_value = oval_dbus_value_to_string(&value_variant);
148 cbret = callback(property_name, property_value, cbarg);
149 free(property_value);
150 }
151
152 free(property_name);
153 if (cbret != 0) {
154 goto cleanup;
155 }
156 }
157 while (dbus_message_iter_next(&property_iter));
158
159 dbus_message_unref(msg); msg = NULL;
160 ret = 0;
161
162 cleanup:
163 if (pending != NULL)
164 dbus_pending_call_unref(pending);
165
166 if (msg != NULL)
167 dbus_message_unref(msg);
168
169 return ret;
170 }
171
172 struct unit_callback_vars {
173 DBusConnection *dbus_conn;
174 probe_ctx *ctx;
175 SEXP_t *unit_entity;
176 SEXP_t *property_entity;
177 SEXP_t *se_unit;
178 SEXP_t *se_property;
179 SEXP_t *item;
180 };
181
182 static int property_callback(const char *property, const char *value, void *cbarg)
183 {
184 struct unit_callback_vars *vars = (struct unit_callback_vars *)cbarg;
185
186 if (vars->se_property != NULL) {
187 //
188 // Compare the previously matched entity to the current one
189 // If they are the same, continue to fill the current item
190 // with property values. If not, collect the item and create
191 // a new one for the current property.
192 //
193 if (SEXP_strcmp(vars->se_property, property) == 0) {
194 SEXP_t *se_value = SEXP_string_new(value, strlen(value));
195 probe_item_ent_add(vars->item, "value", NULL, se_value);
196 SEXP_free(se_value);
197 return 0;
198 } else {
199 probe_item_collect(vars->ctx, vars->item);
200 vars->item = NULL;
201 SEXP_free(vars->se_property);
202 vars->se_property = NULL;
203 }
204 }
205
206 SEXP_t *se_property = SEXP_string_new(property, strlen(property));
207
208 if (probe_entobj_cmp(vars->property_entity, se_property) != OVAL_RESULT_TRUE) {
209 SEXP_free(se_property);
210 return 0;
211 }
212
213 vars->se_property = se_property;
214 vars->item = probe_item_create(OVAL_LINUX_SYSTEMDUNITPROPERTY, NULL,
215 "unit", OVAL_DATATYPE_SEXP, vars->se_unit,
216 "property", OVAL_DATATYPE_SEXP, vars->se_property,
217 "value", OVAL_DATATYPE_STRING, value,
218 NULL);
219 return 0;
220 }
221
222 static int unit_callback(const char *unit, void *cbarg)
223 {
224 struct unit_callback_vars *vars = (struct unit_callback_vars *)cbarg;
225 SEXP_t *se_unit = SEXP_string_new(unit, strlen(unit));
226
227 if (probe_entobj_cmp(vars->unit_entity, se_unit) != OVAL_RESULT_TRUE) {
228 /* Do nothing, continue with the next unit */
229 SEXP_free(se_unit);
230 return 0;
231 }
232
233 vars->se_unit = se_unit;
234 vars->se_property = NULL;
235 vars->item = NULL;
236
237 char *unit_path = get_path_by_unit(vars->dbus_conn, unit);
238
239 if (unit_path == NULL) {
240 return 1;
241 }
242
243 get_all_properties_by_unit_path(vars->dbus_conn, unit_path,
244 property_callback, vars);
245 free(unit_path);
246
247 if (vars->item != NULL) {
248 probe_item_collect(vars->ctx, vars->item);
249 vars->item = NULL;
250 SEXP_free(vars->se_property);
251 vars->se_property = NULL;
252 }
253
254 SEXP_free(se_unit);
255 return 0;
256 }
257
258 int systemdunitproperty_probe_offline_mode_supported(void)
259 {
260 return PROBE_OFFLINE_OWN;
261 }
262
263 int systemdunitproperty_probe_main(probe_ctx *ctx, void *probe_arg)
264 {
265 SEXP_t *unit_entity, *probe_in, *property_entity;
266 oval_schema_version_t oval_version;
267
268 probe_in = probe_ctx_getobject(ctx);
269 oval_version = probe_obj_get_platform_schema_version(probe_in);
270
271 if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.11)) < 0) {
272 // OVAL 5.10 and less
273 return PROBE_EOPNOTSUPP;
274 }
275
276 DBusError dbus_error;
277 DBusConnection *dbus_conn;
278
279 dbus_error_init(&dbus_error);
280 dbus_conn = oval_connect_dbus();
281
282 if (dbus_conn == NULL) {
283 dbus_error_free(&dbus_error);
284 SEXP_t *msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_INFO, "DBus connection failed, could not identify systemd units.");
285 probe_cobj_set_flag(probe_ctx_getresult(ctx), ctx->offline_mode == PROBE_OFFLINE_NONE ? SYSCHAR_FLAG_ERROR : SYSCHAR_FLAG_NOT_COLLECTED);
286 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
287 SEXP_free(msg);
288 return 0;
289 }
290
291 unit_entity = probe_obj_getent(probe_in, "unit", 1);
292 property_entity = probe_obj_getent(probe_in, "property", 1);
293
294 struct unit_callback_vars vars;
295
296 vars.dbus_conn = dbus_conn;
297 vars.ctx = ctx;
298 vars.unit_entity = unit_entity;
299 vars.property_entity = property_entity;
300
301 get_all_systemd_units(dbus_conn, unit_callback, &vars);
302
303 SEXP_free(unit_entity);
304 SEXP_free(property_entity);
305 dbus_error_free(&dbus_error);
306 oval_disconnect_dbus(dbus_conn);
307
308 return 0;
309 }