31#ifndef OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_
32#define OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_
42#include "oscap_helpers.h"
50 dbus_uint32_t first32;
51 dbus_uint32_t second32;
56 unsigned char bytes[8];
73static 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",
89 dD(
"Failed to create dbus_message via dbus_message_new_method_call!");
95 dbus_message_iter_init_append(msg, &args);
96 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &unit)) {
97 dD(
"Failed to append unit '%s' string parameter to dbus message!", unit);
101 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
102 dD(
"Failed to send message via dbus!");
105 if (pending == NULL) {
106 dD(
"Invalid dbus pending call!");
110 dbus_connection_flush(conn);
111 dbus_message_unref(msg); msg = NULL;
113 dbus_pending_call_block(pending);
114 msg = dbus_pending_call_steal_reply(pending);
116 dD(
"Failed to steal dbus pending call reply.");
119 dbus_pending_call_unref(pending); pending = NULL;
121 if (!dbus_message_iter_init(msg, &args)) {
122 dD(
"Failed to initialize iterator over received dbus message.");
126 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
127 dD(
"Expected string argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
131 dbus_message_iter_get_basic(&args, &path);
132 ret = oscap_strdup(path.
str);
133 dbus_message_unref(msg); msg = NULL;
137 dbus_pending_call_unref(pending);
140 dbus_message_unref(msg);
145static int get_all_systemd_units(DBusConnection* conn,
int(*callback)(
const char *,
void *),
void *cbarg)
147 DBusMessage *msg = NULL;
148 DBusPendingCall *pending = NULL;
151 msg = dbus_message_new_method_call(
152 "org.freedesktop.systemd1",
153 "/org/freedesktop/systemd1",
154 "org.freedesktop.systemd1.Manager",
158 dD(
"Failed to create dbus_message via dbus_message_new_method_call!");
162 DBusMessageIter args, unit_iter;
165 dbus_message_iter_init_append(msg, &args);
167 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
168 dD(
"Failed to send message via dbus!");
171 if (pending == NULL) {
172 dD(
"Invalid dbus pending call!");
176 dbus_connection_flush(conn);
177 dbus_message_unref(msg); msg = NULL;
179 dbus_pending_call_block(pending);
180 msg = dbus_pending_call_steal_reply(pending);
182 dD(
"Failed to steal dbus pending call reply.");
185 dbus_pending_call_unref(pending); pending = NULL;
187 if (!dbus_message_iter_init(msg, &args)) {
188 dD(
"Failed to initialize iterator over received dbus message.");
192 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY) {
193 dD(
"Expected array of structs in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
197 dbus_message_iter_recurse(&args, &unit_iter);
199 if (dbus_message_iter_get_arg_type(&unit_iter) != DBUS_TYPE_STRUCT) {
200 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)));
204 DBusMessageIter unit_name;
205 dbus_message_iter_recurse(&unit_iter, &unit_name);
207 if (dbus_message_iter_get_arg_type(&unit_name) != DBUS_TYPE_STRING) {
208 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_name)));
213 dbus_message_iter_get_basic(&unit_name, &value);
214 char *unit_name_s = oscap_strdup(value.
str);
215 int cbret = callback(unit_name_s, cbarg);
221 while (dbus_message_iter_next(&unit_iter));
223 dbus_message_unref(msg); msg = NULL;
229 dbus_pending_call_unref(pending);
232 dbus_message_unref(msg);
237static char *dbus_value_to_string(DBusMessageIter *iter)
239 const int arg_type = dbus_message_iter_get_arg_type(iter);
240 if (dbus_type_is_basic(arg_type)) {
242 dbus_message_iter_get_basic(iter, &value);
247 return oscap_sprintf(
"%c", value.
byt);
249 case DBUS_TYPE_BOOLEAN:
250 return oscap_strdup(value.
bool_val ?
"true" :
"false");
252 case DBUS_TYPE_INT16:
253 return oscap_sprintf(
"%i", value.
i16);
255 case DBUS_TYPE_UINT16:
256 return oscap_sprintf(
"%u", value.
u16);
258 case DBUS_TYPE_INT32:
259 return oscap_sprintf(
"%i", value.
i32);
261 case DBUS_TYPE_UINT32:
262 return oscap_sprintf(
"%u", value.
u32);
264#ifdef DBUS_HAVE_INT64
265 case DBUS_TYPE_INT64:
266 return oscap_sprintf(
"%li", value.i64);
268 case DBUS_TYPE_UINT64:
269 return oscap_sprintf(
"%lu", value.u64);
272 case DBUS_TYPE_DOUBLE:
273 return oscap_sprintf(
"%g", value.
dbl);
275 case DBUS_TYPE_STRING:
276 case DBUS_TYPE_OBJECT_PATH:
277 case DBUS_TYPE_SIGNATURE:
278 return oscap_strdup(value.
str);
290 dD(
"Encountered unknown dbus basic type!");
291 return oscap_strdup(
"error, unknown basic type!");
294 else if (arg_type == DBUS_TYPE_ARRAY) {
295 DBusMessageIter array;
296 dbus_message_iter_recurse(iter, &array);
300 char *element = dbus_value_to_string(&array);
307 ret = oscap_sprintf(
"%s", element);
309 ret = oscap_sprintf(
"%s, %s", old_ret, element);
314 while (dbus_message_iter_next(&array));
327static DBusConnection *connect_dbus()
329 DBusConnection *conn = NULL;
332 dbus_error_init(&err);
334 const char *prefix = getenv(
"OSCAP_PROBE_ROOT");
335 if (prefix != NULL) {
336 char dbus_address[PATH_MAX] = {0};
337 snprintf(dbus_address, PATH_MAX,
"unix:path=%s/run/dbus/system_bus_socket", prefix);
338 setenv(
"DBUS_SYSTEM_BUS_ADDRESS", dbus_address, 0);
343 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
344 if (dbus_error_is_set(&err)) {
345 dD(
"Failed to get DBUS_BUS_SYSTEM connection - %s", err.message);
349 dD(
"DBusConnection == NULL!");
353 dbus_bus_register(conn, &err);
354 if (dbus_error_is_set(&err)) {
355 dD(
"Failed to register on dbus - %s", err.message);
360 dbus_error_free(&err);
365static 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