| 1 | /* |
| 2 | * Copyright 2023 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Evgenii Kolesnikov <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <limits.h> |
| 28 | #include "common/util.h" |
| 29 | #include "oscap_helpers.h" |
| 30 | #include "common/debug_priv.h" |
| 31 | #include "oval_dbus.h" |
| 32 | |
| 33 | |
| 34 | char *oval_dbus_value_to_string(DBusMessageIter *iter) |
| 35 | { |
| 36 | const int arg_type = dbus_message_iter_get_arg_type(iter); |
| 37 | if (dbus_type_is_basic(arg_type)) { |
| 38 | _DBusBasicValue value; |
| 39 | dbus_message_iter_get_basic(iter, &value); |
| 40 | |
| 41 | switch (arg_type) |
| 42 | { |
| 43 | case DBUS_TYPE_BYTE: |
| 44 | return oscap_sprintf("%c", value.byt); |
| 45 | |
| 46 | case DBUS_TYPE_BOOLEAN: |
| 47 | return oscap_strdup(value.bool_val ? "true" : "false"); |
| 48 | |
| 49 | case DBUS_TYPE_INT16: |
| 50 | return oscap_sprintf("%i", value.i16); |
| 51 | |
| 52 | case DBUS_TYPE_UINT16: |
| 53 | return oscap_sprintf("%u", value.u16); |
| 54 | |
| 55 | case DBUS_TYPE_INT32: |
| 56 | return oscap_sprintf("%i", value.i32); |
| 57 | |
| 58 | case DBUS_TYPE_UINT32: |
| 59 | return oscap_sprintf("%u", value.u32); |
| 60 | |
| 61 | #ifdef DBUS_HAVE_INT64 |
| 62 | case DBUS_TYPE_INT64: |
| 63 | return oscap_sprintf("%li", value.i64); |
| 64 | |
| 65 | case DBUS_TYPE_UINT64: |
| 66 | return oscap_sprintf("%lu", value.u64); |
| 67 | #endif |
| 68 | |
| 69 | case DBUS_TYPE_DOUBLE: |
| 70 | return oscap_sprintf("%g", value.dbl); |
| 71 | |
| 72 | case DBUS_TYPE_STRING: |
| 73 | case DBUS_TYPE_OBJECT_PATH: |
| 74 | case DBUS_TYPE_SIGNATURE: |
| 75 | return oscap_strdup(value.str); |
| 76 | |
| 77 | // We skip non-basic types for now |
| 78 | //case DBUS_TYPE_ARRAY: |
| 79 | //case DBUS_TYPE_STRUCT: |
| 80 | //case DBUS_TYPE_DICT_ENTRY: |
| 81 | //case DBUS_TYPE_VARIANT: |
| 82 | //case DBUS_TYPE_UNIX_FD: |
| 83 | // return oscap_sprintf("%i", value.fd); |
| 84 | |
| 85 | default: |
| 86 | dD("Encountered unknown D-Bus basic type: %d!", arg_type); |
| 87 | return oscap_strdup("error, unknown basic type!"); |
| 88 | } |
| 89 | } else if (arg_type == DBUS_TYPE_ARRAY) { |
| 90 | DBusMessageIter array; |
| 91 | dbus_message_iter_recurse(iter, &array); |
| 92 | |
| 93 | char *ret = NULL; |
| 94 | do { |
| 95 | char *element = oval_dbus_value_to_string(&array); |
| 96 | |
| 97 | if (element == NULL) |
| 98 | continue; |
| 99 | |
| 100 | char *old_ret = ret; |
| 101 | if (old_ret == NULL) |
| 102 | ret = oscap_sprintf("%s", element); |
| 103 | else |
| 104 | ret = oscap_sprintf("%s, %s", old_ret, element); |
| 105 | |
| 106 | free(old_ret); |
| 107 | free(element); |
| 108 | } |
| 109 | while (dbus_message_iter_next(&array)); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | DBusConnection *oval_connect_dbus(void) |
| 118 | { |
| 119 | DBusConnection *conn = NULL; |
| 120 | |
| 121 | DBusError err; |
| 122 | dbus_error_init(&err); |
| 123 | |
| 124 | const char *prefix = getenv("OSCAP_PROBE_ROOT"); |
| 125 | if (prefix != NULL) { |
| 126 | char dbus_address[PATH_MAX] = {0}; |
| 127 | snprintf(dbus_address, PATH_MAX, "unix:path=%s/run/dbus/system_bus_socket", prefix); |
| 128 | setenv("DBUS_SYSTEM_BUS_ADDRESS", dbus_address, 0); |
| 129 | /* We won't overwrite DBUS_SYSTEM_BUS_ADDRESS so that |
| 130 | * user could have a way to define some non-standard system bus socket location */ |
| 131 | } |
| 132 | |
| 133 | conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); |
| 134 | if (dbus_error_is_set(&err)) { |
| 135 | dD("Failed to get DBUS_BUS_SYSTEM connection - %s", err.message); |
| 136 | goto cleanup; |
| 137 | } |
| 138 | if (conn == NULL) { |
| 139 | dD("DBusConnection == NULL!"); |
| 140 | goto cleanup; |
| 141 | } |
| 142 | |
| 143 | dbus_bus_register(conn, &err); |
| 144 | if (dbus_error_is_set(&err)) { |
| 145 | dD("Failed to register on dbus - %s", err.message); |
| 146 | goto cleanup; |
| 147 | } |
| 148 | |
| 149 | cleanup: |
| 150 | dbus_error_free(&err); |
| 151 | |
| 152 | return conn; |
| 153 | } |
| 154 | |
| 155 | void oval_disconnect_dbus(DBusConnection *conn) |
| 156 | { |
| 157 | // NOOP |
| 158 | |
| 159 | // Connections retrieved via dbus_bus_get shall not be destroyed, |
| 160 | // these connections are shared. |
| 161 | } |