31 #ifndef OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_
32 #define OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_
40 #include <dbus/dbus.h>
42 #include "oscap_helpers.h"
50 dbus_uint32_t first32;
51 dbus_uint32_t second32;
56 unsigned char bytes[8];
62 #ifdef DBUS_HAVE_INT64
73 static char *get_path_by_unit(DBusConnection *conn,
const char *unit)
75 DBusMessage *msg = NULL;
76 DBusPendingCall *pending = NULL;
80 msg = dbus_message_new_method_call(
81 "org.freedesktop.systemd1",
82 "/org/freedesktop/systemd1",
83 "org.freedesktop.systemd1.Manager",
88 dD(
"LoadUnit: %s", unit);
91 dD(
"Failed to create dbus_message via dbus_message_new_method_call!");
97 dbus_message_iter_init_append(msg, &args);
98 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &unit)) {
99 dD(
"Failed to append unit '%s' string parameter to dbus message!", unit);
103 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
104 dD(
"Failed to send message via dbus!");
107 if (pending == NULL) {
108 dD(
"Invalid dbus pending call!");
112 dbus_connection_flush(conn);
113 dbus_message_unref(msg); msg = NULL;
115 dbus_pending_call_block(pending);
116 msg = dbus_pending_call_steal_reply(pending);
118 dD(
"Failed to steal dbus pending call reply.");
121 dbus_pending_call_unref(pending); pending = NULL;
123 if (!dbus_message_iter_init(msg, &args)) {
124 dD(
"Failed to initialize iterator over received dbus message.");
128 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
129 dD(
"Expected object path argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
133 dbus_message_iter_get_basic(&args, &path);
134 ret = oscap_strdup(path.
str);
135 dbus_message_unref(msg); msg = NULL;
139 dbus_pending_call_unref(pending);
142 dbus_message_unref(msg);
147 static int get_all_systemd_units(DBusConnection* conn,
int(*callback)(
const char *,
void *),
void *cbarg)
149 DBusMessage *msg = NULL;
150 DBusPendingCall *pending = NULL;
153 msg = dbus_message_new_method_call(
154 "org.freedesktop.systemd1",
155 "/org/freedesktop/systemd1",
156 "org.freedesktop.systemd1.Manager",
160 dD(
"Failed to create dbus_message via dbus_message_new_method_call!");
164 DBusMessageIter args, unit_iter;
167 dbus_message_iter_init_append(msg, &args);
169 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
170 dD(
"Failed to send message via dbus!");
173 if (pending == NULL) {
174 dD(
"Invalid dbus pending call!");
178 dbus_connection_flush(conn);
179 dbus_message_unref(msg); msg = NULL;
181 dbus_pending_call_block(pending);
182 msg = dbus_pending_call_steal_reply(pending);
184 dD(
"Failed to steal dbus pending call reply.");
187 dbus_pending_call_unref(pending); pending = NULL;
189 if (!dbus_message_iter_init(msg, &args)) {
190 dD(
"Failed to initialize iterator over received dbus message.");
194 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY) {
195 dD(
"Expected array of structs in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
199 dbus_message_iter_recurse(&args, &unit_iter);
201 if (dbus_message_iter_get_arg_type(&unit_iter) != DBUS_TYPE_STRUCT) {
202 dD(
"Expected unit struct as elements in returned array. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_iter)));
206 DBusMessageIter unit_full_path_and_name;
207 dbus_message_iter_recurse(&unit_iter, &unit_full_path_and_name);
209 if (dbus_message_iter_get_arg_type(&unit_full_path_and_name) != DBUS_TYPE_STRING) {
210 dD(
"Expected string as the first element in the unit struct. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_full_path_and_name)));
215 dbus_message_iter_get_basic(&unit_full_path_and_name, &value);
216 char *unit_name_s = oscap_strdup(basename(value.
str));
217 oscap_strrm(unit_name_s,
"@");
218 int cbret = callback(unit_name_s, cbarg);
224 while (dbus_message_iter_next(&unit_iter));
226 dbus_message_unref(msg); msg = NULL;
232 dbus_pending_call_unref(pending);
235 dbus_message_unref(msg);
240 static char *dbus_value_to_string(DBusMessageIter *iter)
242 const int arg_type = dbus_message_iter_get_arg_type(iter);
243 if (dbus_type_is_basic(arg_type)) {
245 dbus_message_iter_get_basic(iter, &value);
250 return oscap_sprintf(
"%c", value.
byt);
252 case DBUS_TYPE_BOOLEAN:
253 return oscap_strdup(value.
bool_val ?
"true" :
"false");
255 case DBUS_TYPE_INT16:
256 return oscap_sprintf(
"%i", value.
i16);
258 case DBUS_TYPE_UINT16:
259 return oscap_sprintf(
"%u", value.
u16);
261 case DBUS_TYPE_INT32:
262 return oscap_sprintf(
"%i", value.
i32);
264 case DBUS_TYPE_UINT32:
265 return oscap_sprintf(
"%u", value.
u32);
267 #ifdef DBUS_HAVE_INT64
268 case DBUS_TYPE_INT64:
269 return oscap_sprintf(
"%li", value.i64);
271 case DBUS_TYPE_UINT64:
272 return oscap_sprintf(
"%lu", value.u64);
275 case DBUS_TYPE_DOUBLE:
276 return oscap_sprintf(
"%g", value.
dbl);
278 case DBUS_TYPE_STRING:
279 case DBUS_TYPE_OBJECT_PATH:
280 case DBUS_TYPE_SIGNATURE:
281 return oscap_strdup(value.
str);
293 dD(
"Encountered unknown dbus basic type!");
294 return oscap_strdup(
"error, unknown basic type!");
297 else if (arg_type == DBUS_TYPE_ARRAY) {
298 DBusMessageIter array;
299 dbus_message_iter_recurse(iter, &array);
303 char *element = dbus_value_to_string(&array);
310 ret = oscap_sprintf(
"%s", element);
312 ret = oscap_sprintf(
"%s, %s", old_ret, element);
317 while (dbus_message_iter_next(&array));
330 static DBusConnection *connect_dbus()
332 DBusConnection *conn = NULL;
335 dbus_error_init(&err);
337 const char *prefix = getenv(
"OSCAP_PROBE_ROOT");
338 if (prefix != NULL) {
339 char dbus_address[PATH_MAX] = {0};
340 snprintf(dbus_address, PATH_MAX,
"unix:path=%s/run/dbus/system_bus_socket", prefix);
341 setenv(
"DBUS_SYSTEM_BUS_ADDRESS", dbus_address, 0);
346 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
347 if (dbus_error_is_set(&err)) {
348 dD(
"Failed to get DBUS_BUS_SYSTEM connection - %s", err.message);
352 dD(
"DBusConnection == NULL!");
356 dbus_bus_register(conn, &err);
357 if (dbus_error_is_set(&err)) {
358 dD(
"Failed to register on dbus - %s", err.message);
363 dbus_error_free(&err);
368 static void disconnect_dbus(DBusConnection *conn)
oscap debug helpers private header
Definition: systemdshared.h:49
Definition: systemdshared.h:55
dbus_uint16_t u16
as int16
Definition: systemdshared.h:58
dbus_int16_t i16
as int16
Definition: systemdshared.h:57
double dbl
as double
Definition: systemdshared.h:67
_DBus8ByteStruct eight
as 8-byte struct
Definition: systemdshared.h:66
char * str
as char* (string, object path or signature)
Definition: systemdshared.h:69
int fd
as Unix file descriptor
Definition: systemdshared.h:70
dbus_uint32_t u32
as int32
Definition: systemdshared.h:60
dbus_bool_t bool_val
as boolean
Definition: systemdshared.h:61
unsigned char byt
as byte
Definition: systemdshared.h:68
dbus_int32_t i32
as int32
Definition: systemdshared.h:59