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_Hcxx . . $ `<built-in>:409:9`!
30 #include <config.h>cxx . . $ `<file>/build/config.h`/
31 #endif
32
33 #include <probe-api.h>cxx . . $ `<file>/src/OVAL/probes/public/probe-api.h`/
34 #include <probe/probe.h>cxx . . $ `<file>/src/OVAL/probes/probe/probe.h`/
35
36 #include "probe/entcmp.h"cxx . . $ `<file>/src/OVAL/probes/probe/entcmp.h`/
37 #include "systemdshared.h"cxx . . $ `<file>/src/OVAL/probes/unix/linux/systemdshared.h`/
38 #include "common/list.h"cxx . . $ `<file>/src/common/list.h`/
39 #include <string.h>
40 #include "systemdunitdependency_probe.h"cxx . . $ `<file>/src/OVAL/probes/unix/linux/systemdunitdependency_probe.h`/
41
42 static void get_all_dependencies_by_unitcxx . . $ get_all_dependencies_by_unit(8d858bc84a8d61fb).(DBusConnectioncxx . . $ DBusConnection# *conn, const char *unit, SEXP_tcxx . . $ SEXP_t# *item, struct oscap_htablecxx . . $ oscap_htable# *visited_units);
43
44 static char *get_property_by_unit_pathcxx . . $ get_property_by_unit_path(adeb16e293c1133d).(DBusConnectioncxx . . $ DBusConnection# *conn, const char *unit_path, const char *property)
45 {
46 DBusMessagecxx . . $ DBusMessage# *msg = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
47 DBusPendingCallcxx . . $ DBusPendingCall# *pending = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
48 char *ret = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
49
50 msg = dbus_message_new_method_callcxx . . $ dbus_message_new_method_call(a937a8c9de1028ff).(
51 "org.freedesktop.systemd1",
52 unit_path,
53 "org.freedesktop.DBus.Properties",
54 "Get"
55 );
56 if (msg == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
57 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Failed to create dbus_message via dbus_message_new_method_call!");
58 goto cleanup;
59 }
60
61 DBusMessageItercxx . . $ DBusMessageIter# args, value_iter;
62
63 const char *interface = "org.freedesktop.systemd1.Unit";
64
65 dbus_message_iter_init_appendcxx . . $ dbus_message_iter_init_append(bdfd43ed78e92566).(msg, &args);
66 if (!dbus_message_iter_append_basiccxx . . $ dbus_message_iter_append_basic(dea9ad080b919705).(&args, DBUS_TYPE_STRINGcxx . . $ `<external>/9999628393408380398/dbus-protocol.h:104:9`!, &interface)) {
67 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Failed to append interface '%s' string parameter to dbus message!", interface);
68 goto cleanup;
69 }
70 if (!dbus_message_iter_append_basiccxx . . $ dbus_message_iter_append_basic(dea9ad080b919705).(&args, DBUS_TYPE_STRINGcxx . . $ `<external>/9999628393408380398/dbus-protocol.h:104:9`!, &property)) {
71 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Failed to append property '%s' string parameter to dbus message!", property);
72 goto cleanup;
73 }
74
75 if (!dbus_connection_send_with_replycxx . . $ dbus_connection_send_with_reply(ab3797bd4df020e).(conn, msg, &pending, -1)) {
76 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Failed to send message via dbus!");
77 goto cleanup;
78 }
79 if (pending == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
80 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Invalid dbus pending call!");
81 goto cleanup;
82 }
83
84 dbus_connection_flushcxx . . $ dbus_connection_flush(5ffe6e60e141c747).(conn);
85 dbus_message_unrefcxx . . $ dbus_message_unref(ef6f40ad6aaa7be8).(msg); msg = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
86
87 dbus_pending_call_blockcxx . . $ dbus_pending_call_block(f8094f87eabead78).(pending);
88 msg = dbus_pending_call_steal_replycxx . . $ dbus_pending_call_steal_reply(5c89f41a44f43d9f).(pending);
89 if (msg == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
90 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Failed to steal dbus pending call reply.");
91 goto cleanup;
92 }
93 dbus_pending_call_unrefcxx . . $ dbus_pending_call_unref(f8094f87eabead78).(pending); pending = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
94
95 if (!dbus_message_iter_initcxx . . $ dbus_message_iter_init(ffe71547a19cf1fb).(msg, &args)) {
96 dDcxx . . $ `src/common/debug_priv.h:64:9`!("Failed to initialize iterator over received dbus message.");
97 goto cleanup;
98 }
99
100 if (dbus_message_iter_get_arg_typecxx . . $ dbus_message_iter_get_arg_type(f50db427ad27749a).(&args) != DBUS_TYPE_VARIANTcxx . . $ `<external>/9999628393408380398/dbus-protocol.h:126:9`!)
101 {
102 dDcxx . . $ `src/common/debug_priv.h:64:9`!("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_recursecxx . . $ dbus_message_iter_recurse(6e42dd501f62eaf0).(&args, &value_iter);
107 ret = oval_dbus_value_to_stringcxx . . $ oval_dbus_value_to_string(c8641a873d86c3b).(&value_iter);
108
109 dbus_message_unrefcxx . . $ dbus_message_unref(ef6f40ad6aaa7be8).(msg); msg = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
110
111 cleanup:
112 if (pending != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
113 dbus_pending_call_unrefcxx . . $ dbus_pending_call_unref(f8094f87eabead78).(pending);
114
115 if (msg != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
116 dbus_message_unrefcxx . . $ dbus_message_unref(ef6f40ad6aaa7be8).(msg);
117
118 return ret;
119 }
120
121 struct unit_callback_varscxx . . $ unit_callback_vars# {
122 DBusConnectioncxx . . $ DBusConnection# *dbus_conncxx . . $ unit_callback_vars#dbus_conn.;
123 probe_ctxcxx . . $ probe_ctx# *ctxcxx . . $ unit_callback_vars#ctx.;
124 SEXP_tcxx . . $ SEXP_t# *unit_entitycxx . . $ unit_callback_vars#unit_entity.;
125 };
126
127 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! is_unit_name_a_targetcxx . . $ is_unit_name_a_target(c5f7da30cbc0f4e9).(const char *unit)
128 {
129 const char *suffix = ".target";
130 const size_t suffix_len = strlencxx . . $ strlen(751346f8406fd082).(suffix);
131
132 if (!unit)
133 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
134
135 const size_t len = strlencxx . . $ strlen(751346f8406fd082).(unit);
136 if (suffix_len > len)
137 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
138
139 return strncmpcxx . . $ strncmp(fd278af2f295fa2e).(unit + len - suffix_len, suffix, suffix_len) == 0;
140 }
141
142 static int add_unit_dependencycxx . . $ add_unit_dependency(c155ee2d278021b0).(const char *dependency, SEXP_tcxx . . $ SEXP_t# *item, struct oscap_htablecxx . . $ oscap_htable# *visited_units)
143 {
144 if (oscap_htable_getcxx . . $ oscap_htable_get(f5c7e8ff6c970cd3).(visited_units, dependency) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
145 return 1;
146 }
147 oscap_htable_addcxx . . $ oscap_htable_add(d9abe78f59fa00eb).(visited_units, dependency, (void *) truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!);
148 SEXP_tcxx . . $ SEXP_t# *se_dependency = SEXP_string_newcxx . . $ SEXP_string_new(a588a58d7514eee2).(dependency, strlencxx . . $ strlen(751346f8406fd082).(dependency));
149 probe_item_ent_addcxx . . $ probe_item_ent_add(9f488d3b029fc9d1).(item, "dependency", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, se_dependency);
150 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(se_dependency);
151 return 0;
152 }
153
154 static void process_unit_propertycxx . . $ process_unit_property(ac77c8a532d6b761).(const char *property, DBusConnectioncxx . . $ DBusConnection# *conn, const char *path, SEXP_tcxx . . $ SEXP_t# *item, struct oscap_htablecxx . . $ oscap_htable# *visited_units)
155 {
156 char *values_s = get_property_by_unit_pathcxx . . $ get_property_by_unit_path(adeb16e293c1133d).(conn, path, property);
157 if (values_s) {
158 char **values = oscap_splitcxx . . $ oscap_split(25e857aacd75b4b6).(values_s, ", ");
159 for (int i = 0; values[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; ++i) {
160 if (oscap_strcmpcxx . . $ oscap_strcmp(5ae29d726ae57f88).(values[i], "") == 0) {
161 continue;
162 }
163
164 if (add_unit_dependencycxx . . $ add_unit_dependency(c155ee2d278021b0).(values[i], item, visited_units) == 0) {
165 get_all_dependencies_by_unitcxx . . $ get_all_dependencies_by_unit(8d858bc84a8d61fb).(conn, values[i], item, visited_units);
166 }
167 }
168 freecxx . . $ free(5d22bdacc48458e8).(values);
169 }
170 freecxx . . $ free(5d22bdacc48458e8).(values_s);
171 }
172
173 static void get_all_dependencies_by_unitcxx . . $ get_all_dependencies_by_unit(8d858bc84a8d61fb).(DBusConnectioncxx . . $ DBusConnection# *conn, const char *unit, SEXP_tcxx . . $ SEXP_t# *item, struct oscap_htablecxx . . $ oscap_htable# *visited_units)
174 {
175 if (!unit || strcmpcxx . . $ strcmp(5ae29d726ae57f88).(unit, "(null)") == 0)
176 return;
177
178 // systemctl list-dependencies only recurses into target units
179 if (!is_unit_name_a_targetcxx . . $ is_unit_name_a_target(c5f7da30cbc0f4e9).(unit))
180 return;
181
182 char *path = get_path_by_unitcxx . . $ get_path_by_unit(9da5f9108eb37f8b).(conn, unit);
183
184 process_unit_propertycxx . . $ process_unit_property(ac77c8a532d6b761).("Requires", conn, path, item, visited_units);
185 process_unit_propertycxx . . $ process_unit_property(ac77c8a532d6b761).("Wants", conn, path, item, visited_units);
186
187 freecxx . . $ free(5d22bdacc48458e8).(path);
188 }
189
190 static int unit_callbackcxx . . $ unit_callback(53df3a489c338db2).(const char *unit, void *cbarg)
191 {
192 struct unit_callback_varscxx . . $ unit_callback_vars# *vars = (struct unit_callback_varscxx . . $ unit_callback_vars# *)cbarg;
193 SEXP_tcxx . . $ SEXP_t# *se_unit = SEXP_string_newcxx . . $ SEXP_string_new(a588a58d7514eee2).(unit, strlencxx . . $ strlen(751346f8406fd082).(unit));
194
195 if (probe_entobj_cmpcxx . . $ probe_entobj_cmp(e245451a1311e799).(vars->unit_entitycxx . . $ unit_callback_vars#unit_entity., se_unit) != OVAL_RESULT_TRUEcxx . . $ OVAL_RESULT_TRUE.) {
196 /* Do nothing, continue with the next unit */
197 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(se_unit);
198 return 0;
199 }
200
201 SEXP_tcxx . . $ SEXP_t# *item = probe_item_createcxx . . $ probe_item_create(36b16a6622050a9c).(OVAL_LINUX_SYSTEMDUNITDEPENDENCYcxx . . $ OVAL_LINUX_SYSTEMDUNITDEPENDENCY., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!,
202 "unit", OVAL_DATATYPE_SEXPcxx . . $ OVAL_DATATYPE_SEXP., se_unit,
203 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
204
205 struct oscap_htablecxx . . $ oscap_htable# *visited_units = oscap_htable_newcxx . . $ oscap_htable_new(3c1cb2b6f390ad38).();
206 get_all_dependencies_by_unitcxx . . $ get_all_dependencies_by_unit(8d858bc84a8d61fb).(vars->dbus_conncxx . . $ unit_callback_vars#dbus_conn., unit, item, visited_units);
207 oscap_htable_freecxx . . $ oscap_htable_free(efa5da5da5d523d).(visited_units, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
208
209 probe_item_collectcxx . . $ probe_item_collect(11bb80e0c30650d7).(vars->ctxcxx . . $ unit_callback_vars#ctx., item);
210 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(se_unit);
211
212 return 0;
213 }
214
215 int systemdunitdependency_probe_offline_mode_supportedcxx . . $ systemdunitdependency_probe_offline_mode_supported(b126dc7c1de90089).(void)
216 {
217 return PROBE_OFFLINE_OWNcxx . . $ PROBE_OFFLINE_OWN.;
218 }
219
220 int systemdunitdependency_probe_maincxx . . $ systemdunitdependency_probe_main(470536d3ffacaf10).(probe_ctxcxx . . $ probe_ctx# *ctx, void *probe_arg)
221 {
222 SEXP_tcxx . . $ SEXP_t# *unit_entity, *probe_in;
223 oval_schema_version_tcxx . . $ oval_schema_version_t# oval_version;
224
225 probe_in = probe_ctx_getobjectcxx . . $ probe_ctx_getobject(4299b6b53fa804a5).(ctx);
226 oval_version = probe_obj_get_platform_schema_versioncxx . . $ probe_obj_get_platform_schema_version(5bacca80194debea).(probe_in);
227
228 if (oval_schema_version_cmpcxx . . $ oval_schema_version_cmp(b4d0c2bfb97f274f).(oval_version, OVAL_SCHEMA_VERSIONcxx . . $ `src/OVAL/public/oval_schema_version.h:39:9`!(5.11)) < 0) {
229 // OVAL 5.10 and less
230 return PROBE_EOPNOTSUPPcxx . . $ `src/OVAL/probes/public/probe-common.h:14:9`!;
231 }
232
233 DBusErrorcxx . . $ DBusError# dbus_error;
234 DBusConnectioncxx . . $ DBusConnection# *dbus_conn;
235
236 dbus_error_initcxx . . $ dbus_error_init(b79b632916b634d5).(&dbus_error);
237 dbus_conn = oval_connect_dbuscxx . . $ oval_connect_dbus(f89cc4e9350282f7).();
238
239 if (dbus_conn == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
240 dbus_error_freecxx . . $ dbus_error_free(b79b632916b634d5).(&dbus_error);
241 SEXP_tcxx . . $ SEXP_t# *msg = probe_msg_creatcxx . . $ probe_msg_creat(c07e3aa4e7ab9719).(OVAL_MESSAGE_LEVEL_INFOcxx . . $ OVAL_MESSAGE_LEVEL_INFO., "DBus connection failed, could not identify systemd units.");
242 probe_cobj_set_flagcxx . . $ probe_cobj_set_flag(644bab5dffe689c).(probe_ctx_getresultcxx . . $ probe_ctx_getresult(4299b6b53fa804a5).(ctx), ctx->offline_modecxx . . $ probe_ctx#offline_mode. == PROBE_OFFLINE_NONEcxx . . $ PROBE_OFFLINE_NONE. ? SYSCHAR_FLAG_ERRORcxx . . $ SYSCHAR_FLAG_ERROR. : SYSCHAR_FLAG_NOT_COLLECTEDcxx . . $ SYSCHAR_FLAG_NOT_COLLECTED.);
243 probe_cobj_add_msgcxx . . $ probe_cobj_add_msg(ef7ee42b5817e0be).(probe_ctx_getresultcxx . . $ probe_ctx_getresult(4299b6b53fa804a5).(ctx), msg);
244 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(msg);
245 return 0;
246 }
247
248 unit_entity = probe_obj_getentcxx . . $ probe_obj_getent(7907a01f2ff841f3).(probe_in, "unit", 1);
249
250 struct unit_callback_varscxx . . $ unit_callback_vars# vars;
251
252 vars.dbus_conncxx . . $ unit_callback_vars#dbus_conn. = dbus_conn;
253 vars.ctxcxx . . $ unit_callback_vars#ctx. = ctx;
254 vars.unit_entitycxx . . $ unit_callback_vars#unit_entity. = unit_entity;
255
256 get_all_systemd_unitscxx . . $ get_all_systemd_units(cc44debc175259da).(dbus_conn, unit_callbackcxx . . $ unit_callback(53df3a489c338db2)., &vars);
257
258 SEXP_freecxx . . $ SEXP_free(c6b28547479798f4).(unit_entity);
259 dbus_error_freecxx . . $ dbus_error_free(b79b632916b634d5).(&dbus_error);
260 oval_disconnect_dbuscxx . . $ oval_disconnect_dbus(5ffe6e60e141c747).(dbus_conn);
261
262 return 0;
263 }