Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * @file systemdunitdependency_probe.c
3 * @brief implementation of the systemdunitdependency_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 <probe/probe.h>
35
36 #include "probe/entcmp.h"
37 #include "systemdshared.h"
38 #include "common/list.h"
39 #include <string.h>
40 #include "systemdunitdependency_probe.h"
41
42 static void get_all_dependencies_by_unit(DBusConnection *conn, const char *unit, SEXP_t *item, struct oscap_htable *visited_units);
43
44 static char *get_property_by_unit_path(DBusConnection *conn, const char *unit_path, const char *property)
45 {
46 DBusMessage *msg = NULL;
47 DBusPendingCall *pending = NULL;
48 char *ret = NULL;
49
50 msg = dbus_message_new_method_call(
51 "org.freedesktop.systemd1",
52 unit_path,
53 "org.freedesktop.DBus.Properties",
54 "Get"
55 );
56 if (msg == NULL) {
57 dD("Failed to create dbus_message via dbus_message_new_method_call!");
58 goto cleanup;
59 }
60
61 DBusMessageIter args, value_iter;
62
63 const char *interface = "org.freedesktop.systemd1.Unit";
64
65 dbus_message_iter_init_append(msg, &args);
66 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface)) {
67 dD("Failed to append interface '%s' string parameter to dbus message!", interface);
68 goto cleanup;
69 }
70 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &property)) {
71 dD("Failed to append property '%s' string parameter to dbus message!", property);
72 goto cleanup;
73 }
74
75 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
76 dD("Failed to send message via dbus!");
77 goto cleanup;
78 }
79 if (pending == NULL) {
80 dD("Invalid dbus pending call!");
81 goto cleanup;
82 }
83
84 dbus_connection_flush(conn);
85 dbus_message_unref(msg); msg = NULL;
86
87 dbus_pending_call_block(pending);
88 msg = dbus_pending_call_steal_reply(pending);
89 if (msg == NULL) {
90 dD("Failed to steal dbus pending call reply.");
91 goto cleanup;
92 }
93 dbus_pending_call_unref(pending); pending = NULL;
94
95 if (!dbus_message_iter_init(msg, &args)) {
96 dD("Failed to initialize iterator over received dbus message.");
97 goto cleanup;
98 }
99
100 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_VARIANT)
101 {
102 dD("Expected variant argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
103 goto cleanup;
104 }
105
106 dbus_message_iter_recurse(&args, &value_iter);
107 ret = oval_dbus_value_to_string(&value_iter);
108
109 dbus_message_unref(msg); msg = NULL;
110
111 cleanup:
112 if (pending != NULL)
113 dbus_pending_call_unref(pending);
114
115 if (msg != NULL)
116 dbus_message_unref(msg);
117
118 return ret;
119 }
120
121 struct unit_callback_vars {
122 DBusConnection *dbus_conn;
123 probe_ctx *ctx;
124 SEXP_t *unit_entity;
125 };
126
127 static bool is_unit_name_a_target(const char *unit)
128 {
129 const char *suffix = ".target";
130 const size_t suffix_len = strlen(suffix);
131
132 if (!unit)
133 return false;
134
135 const size_t len = strlen(unit);
136 if (suffix_len > len)
137 return false;
138
139 return strncmp(unit + len - suffix_len, suffix, suffix_len) == 0;
140 }
141
142 static int add_unit_dependency(const char *dependency, SEXP_t *item, struct oscap_htable *visited_units)
143 {
144 if (oscap_htable_get(visited_units, dependency) != NULL) {
145 return 1;
146 }
147 oscap_htable_add(visited_units, dependency, (void *) true);
148 SEXP_t *se_dependency = SEXP_string_new(dependency, strlen(dependency));
149 probe_item_ent_add(item, "dependency", NULL, se_dependency);
150 SEXP_free(se_dependency);
151 return 0;
152 }
153
154 static void process_unit_property(const char *property, DBusConnection *conn, const char *path, SEXP_t *item, struct oscap_htable *visited_units)
155 {
156 char *values_s = get_property_by_unit_path(conn, path, property);
157 if (values_s) {
158 char **values = oscap_split(values_s, ", ");
159 for (int i = 0; values[i] != NULL; ++i) {
160 if (oscap_strcmp(values[i], "") == 0) {
161 continue;
162 }
163
164 if (add_unit_dependency(values[i], item, visited_units) == 0) {
165 get_all_dependencies_by_unit(conn, values[i], item, visited_units);
166 }
167 }
168 free(values);
169 }
170 free(values_s);
171 }
172
173 static void get_all_dependencies_by_unit(DBusConnection *conn, const char *unit, SEXP_t *item, struct oscap_htable *visited_units)
174 {
175 if (!unit || strcmp(unit, "(null)") == 0)
176 return;
177
178 // systemctl list-dependencies only recurses into target units
179 if (!is_unit_name_a_target(unit))
180 return;
181
182 char *path = get_path_by_unit(conn, unit);
183
184 process_unit_property("Requires", conn, path, item, visited_units);
185 process_unit_property("Wants", conn, path, item, visited_units);
186
187 free(path);
188 }
189
190 static int unit_callback(const char *unit, void *cbarg)
191 {
192 struct unit_callback_vars *vars = (struct unit_callback_vars *)cbarg;
193 SEXP_t *se_unit = SEXP_string_new(unit, strlen(unit));
194
195 if (probe_entobj_cmp(vars->unit_entity, se_unit) != OVAL_RESULT_TRUE) {
196 /* Do nothing, continue with the next unit */
197 SEXP_free(se_unit);
198 return 0;
199 }
200
201 SEXP_t *item = probe_item_create(OVAL_LINUX_SYSTEMDUNITDEPENDENCY, NULL,
202 "unit", OVAL_DATATYPE_SEXP, se_unit,
203 NULL);
204
205 struct oscap_htable *visited_units = oscap_htable_new();
206 get_all_dependencies_by_unit(vars->dbus_conn, unit, item, visited_units);
207 oscap_htable_free(visited_units, NULL);
208
209 probe_item_collect(vars->ctx, item);
210 SEXP_free(se_unit);
211
212 return 0;
213 }
214
215 int systemdunitdependency_probe_offline_mode_supported(void)
216 {
217 return PROBE_OFFLINE_OWN;
218 }
219
220 int systemdunitdependency_probe_main(probe_ctx *ctx, void *probe_arg)
221 {
222 SEXP_t *unit_entity, *probe_in;
223 oval_schema_version_t oval_version;
224
225 probe_in = probe_ctx_getobject(ctx);
226 oval_version = probe_obj_get_platform_schema_version(probe_in);
227
228 if (oval_schema_version_cmp(oval_version, OVAL_SCHEMA_VERSION(5.11)) < 0) {
229 // OVAL 5.10 and less
230 return PROBE_EOPNOTSUPP;
231 }
232
233 DBusError dbus_error;
234 DBusConnection *dbus_conn;
235
236 dbus_error_init(&dbus_error);
237 dbus_conn = oval_connect_dbus();
238
239 if (dbus_conn == NULL) {
240 dbus_error_free(&dbus_error);
241 SEXP_t *msg = probe_msg_creat(OVAL_MESSAGE_LEVEL_INFO, "DBus connection failed, could not identify systemd units.");
242 probe_cobj_set_flag(probe_ctx_getresult(ctx), ctx->offline_mode == PROBE_OFFLINE_NONE ? SYSCHAR_FLAG_ERROR : SYSCHAR_FLAG_NOT_COLLECTED);
243 probe_cobj_add_msg(probe_ctx_getresult(ctx), msg);
244 SEXP_free(msg);
245 return 0;
246 }
247
248 unit_entity = probe_obj_getent(probe_in, "unit", 1);
249
250 struct unit_callback_vars vars;
251
252 vars.dbus_conn = dbus_conn;
253 vars.ctx = ctx;
254 vars.unit_entity = unit_entity;
255
256 get_all_systemd_units(dbus_conn, unit_callback, &vars);
257
258 SEXP_free(unit_entity);
259 dbus_error_free(&dbus_error);
260 oval_disconnect_dbus(dbus_conn);
261
262 return 0;
263 }