Richard Hughes | 02c90d8 | 2018-08-09 12:13:03 +0100 | [diff] [blame] | 1 | /* |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 Richard Hughes <richard@hughsie.com> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 3 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Richard Hughes | b08e7bc | 2018-09-11 10:51:13 +0100 | [diff] [blame] | 7 | #define G_LOG_DOMAIN "FuMain" |
| 8 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 9 | #include "config.h" |
| 10 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 11 | #include <xmlb.h> |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 12 | #include <fwupd.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 13 | #include <gio/gunixfdlist.h> |
| 14 | #include <glib/gi18n.h> |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 15 | #include <glib-unix.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 16 | #include <locale.h> |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 17 | #include <polkit/polkit.h> |
Mario Limonciello | eb4c764 | 2019-11-11 10:31:29 -0600 | [diff] [blame] | 18 | #include <stdio.h> |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 20 | #include <jcat.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 21 | |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 22 | #include "fwupd-device-private.h" |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 23 | #include "fwupd-security-attr-private.h" |
Richard Hughes | 1642b3b | 2017-06-05 17:40:08 +0100 | [diff] [blame] | 24 | #include "fwupd-release-private.h" |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 25 | #include "fwupd-remote-private.h" |
Richard Hughes | d6db6b4 | 2017-04-12 15:03:10 +0100 | [diff] [blame] | 26 | #include "fwupd-resources.h" |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 27 | |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 28 | #include "fu-common.h" |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 29 | #include "fu-debug.h" |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 30 | #include "fu-device-private.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 31 | #include "fu-engine.h" |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 32 | #include "fu-install-task.h" |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 33 | #include "fu-security-attrs-private.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 34 | |
Philip Withnall | bc339aa | 2016-11-22 16:13:22 +0000 | [diff] [blame] | 35 | #ifndef HAVE_POLKIT_0_114 |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 36 | #pragma clang diagnostic push |
| 37 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 38 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthorizationResult, g_object_unref) |
| 39 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitSubject, g_object_unref) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 40 | #pragma clang diagnostic pop |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 43 | typedef struct { |
| 44 | GDBusConnection *connection; |
| 45 | GDBusNodeInfo *introspection_daemon; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 46 | GDBusProxy *proxy_uid; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 47 | GMainLoop *loop; |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame] | 48 | GFileMonitor *argv0_monitor; |
Richard Hughes | 603e4f6 | 2019-12-11 13:44:09 +0000 | [diff] [blame] | 49 | #if GLIB_CHECK_VERSION(2,63,3) |
| 50 | GMemoryMonitor *memory_monitor; |
| 51 | #endif |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 52 | PolkitAuthority *authority; |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 53 | guint owner_id; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 54 | FuEngine *engine; |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 55 | gboolean update_in_progress; |
| 56 | gboolean pending_sigterm; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 57 | } FuMainPrivate; |
| 58 | |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 59 | static gboolean |
| 60 | fu_main_sigterm_cb (gpointer user_data) |
| 61 | { |
| 62 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 63 | if (!priv->update_in_progress) { |
| 64 | g_main_loop_quit (priv->loop); |
| 65 | return G_SOURCE_REMOVE; |
| 66 | } |
| 67 | g_warning ("Received SIGTERM during a firmware update, ignoring"); |
| 68 | priv->pending_sigterm = TRUE; |
| 69 | return G_SOURCE_CONTINUE; |
| 70 | } |
| 71 | |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 72 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 73 | fu_main_engine_changed_cb (FuEngine *engine, FuMainPrivate *priv) |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 74 | { |
| 75 | /* not yet connected */ |
| 76 | if (priv->connection == NULL) |
| 77 | return; |
| 78 | g_dbus_connection_emit_signal (priv->connection, |
| 79 | NULL, |
| 80 | FWUPD_DBUS_PATH, |
| 81 | FWUPD_DBUS_INTERFACE, |
| 82 | "Changed", |
| 83 | NULL, NULL); |
| 84 | } |
| 85 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 86 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 87 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 88 | FuDevice *device, |
| 89 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 90 | { |
| 91 | GVariant *val; |
| 92 | |
| 93 | /* not yet connected */ |
| 94 | if (priv->connection == NULL) |
| 95 | return; |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 96 | val = fwupd_device_to_variant (FWUPD_DEVICE (device)); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 97 | g_dbus_connection_emit_signal (priv->connection, |
| 98 | NULL, |
| 99 | FWUPD_DBUS_PATH, |
| 100 | FWUPD_DBUS_INTERFACE, |
| 101 | "DeviceAdded", |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 102 | g_variant_new_tuple (&val, 1), NULL); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 105 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 106 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 107 | FuDevice *device, |
| 108 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 109 | { |
| 110 | GVariant *val; |
| 111 | |
| 112 | /* not yet connected */ |
| 113 | if (priv->connection == NULL) |
| 114 | return; |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 115 | val = fwupd_device_to_variant (FWUPD_DEVICE (device)); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 116 | g_dbus_connection_emit_signal (priv->connection, |
| 117 | NULL, |
| 118 | FWUPD_DBUS_PATH, |
| 119 | FWUPD_DBUS_INTERFACE, |
| 120 | "DeviceRemoved", |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 121 | g_variant_new_tuple (&val, 1), NULL); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 124 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 125 | fu_main_engine_device_changed_cb (FuEngine *engine, |
| 126 | FuDevice *device, |
| 127 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 128 | { |
| 129 | GVariant *val; |
| 130 | |
| 131 | /* not yet connected */ |
| 132 | if (priv->connection == NULL) |
| 133 | return; |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 134 | val = fwupd_device_to_variant (FWUPD_DEVICE (device)); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 135 | g_dbus_connection_emit_signal (priv->connection, |
| 136 | NULL, |
| 137 | FWUPD_DBUS_PATH, |
| 138 | FWUPD_DBUS_INTERFACE, |
| 139 | "DeviceChanged", |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 140 | g_variant_new_tuple (&val, 1), NULL); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 143 | static void |
| 144 | fu_main_emit_property_changed (FuMainPrivate *priv, |
| 145 | const gchar *property_name, |
| 146 | GVariant *property_value) |
| 147 | { |
| 148 | GVariantBuilder builder; |
| 149 | GVariantBuilder invalidated_builder; |
| 150 | |
| 151 | /* not yet connected */ |
Richard Hughes | 34fcc02 | 2018-09-19 16:16:15 +0100 | [diff] [blame] | 152 | if (priv->connection == NULL) { |
| 153 | g_variant_unref (g_variant_ref_sink (property_value)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 154 | return; |
Richard Hughes | 34fcc02 | 2018-09-19 16:16:15 +0100 | [diff] [blame] | 155 | } |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 156 | |
| 157 | /* build the dict */ |
| 158 | g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as")); |
Richard Hughes | 8356a83 | 2019-03-21 17:04:38 +0000 | [diff] [blame] | 159 | g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 160 | g_variant_builder_add (&builder, |
| 161 | "{sv}", |
| 162 | property_name, |
| 163 | property_value); |
| 164 | g_dbus_connection_emit_signal (priv->connection, |
| 165 | NULL, |
| 166 | FWUPD_DBUS_PATH, |
| 167 | "org.freedesktop.DBus.Properties", |
| 168 | "PropertiesChanged", |
| 169 | g_variant_new ("(sa{sv}as)", |
| 170 | FWUPD_DBUS_INTERFACE, |
| 171 | &builder, |
| 172 | &invalidated_builder), |
| 173 | NULL); |
| 174 | g_variant_builder_clear (&builder); |
| 175 | g_variant_builder_clear (&invalidated_builder); |
| 176 | } |
| 177 | |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 178 | static void |
| 179 | fu_main_set_status (FuMainPrivate *priv, FwupdStatus status) |
| 180 | { |
| 181 | g_debug ("Emitting PropertyChanged('Status'='%s')", |
| 182 | fwupd_status_to_string (status)); |
| 183 | fu_main_emit_property_changed (priv, "Status", |
| 184 | g_variant_new_uint32 (status)); |
| 185 | } |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 186 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 187 | static void |
| 188 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 189 | FwupdStatus status, |
| 190 | FuMainPrivate *priv) |
| 191 | { |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 192 | fu_main_set_status (priv, status); |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 193 | |
| 194 | /* engine has gone idle */ |
| 195 | if (status == FWUPD_STATUS_SHUTDOWN) |
| 196 | g_main_loop_quit (priv->loop); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 199 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 200 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 201 | guint percentage, |
| 202 | FuMainPrivate *priv) |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 203 | { |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 204 | g_debug ("Emitting PropertyChanged('Percentage'='%u%%')", percentage); |
| 205 | fu_main_emit_property_changed (priv, "Percentage", |
| 206 | g_variant_new_uint32 (percentage)); |
| 207 | } |
| 208 | |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 209 | static gboolean |
| 210 | fu_main_get_device_flags_for_sender (FuMainPrivate *priv, const char *sender, |
| 211 | FwupdDeviceFlags *flags, GError **error) |
| 212 | { |
| 213 | uid_t calling_uid; |
| 214 | g_autoptr(GVariant) value = NULL; |
| 215 | |
| 216 | g_return_val_if_fail (sender != NULL, FALSE); |
| 217 | g_return_val_if_fail (flags != NULL, FALSE); |
| 218 | |
| 219 | value = g_dbus_proxy_call_sync (priv->proxy_uid, |
| 220 | "GetConnectionUnixUser", |
| 221 | g_variant_new ("(s)", sender), |
| 222 | G_DBUS_CALL_FLAGS_NONE, |
| 223 | 2000, |
| 224 | NULL, |
| 225 | error); |
| 226 | if (value == NULL) { |
| 227 | g_prefix_error (error, "failed to read user id of caller: "); |
| 228 | return FALSE; |
| 229 | } |
| 230 | g_variant_get (value, "(u)", &calling_uid); |
| 231 | if (calling_uid == 0) |
| 232 | *flags |= FWUPD_DEVICE_FLAG_TRUSTED; |
| 233 | |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 237 | static GVariant * |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 238 | fu_main_device_array_to_variant (FuMainPrivate *priv, const gchar *sender, |
| 239 | GPtrArray *devices, GError **error) |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 240 | { |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 241 | GVariantBuilder builder; |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 242 | FwupdDeviceFlags flags = FWUPD_DEVICE_FLAG_NONE; |
| 243 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 244 | g_return_val_if_fail (devices->len > 0, NULL); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 245 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 246 | |
| 247 | if (!fu_main_get_device_flags_for_sender (priv, sender, &flags, error)) |
Richard Hughes | 83cce1b | 2018-09-10 16:42:30 +0100 | [diff] [blame] | 248 | return NULL; |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 249 | |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 250 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 251 | FuDevice *device = g_ptr_array_index (devices, i); |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 252 | GVariant *tmp = fwupd_device_to_variant_full (FWUPD_DEVICE (device), |
| 253 | flags); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 254 | g_variant_builder_add_value (&builder, tmp); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 255 | } |
Richard Hughes | 9e1b140 | 2017-09-15 16:29:54 +0100 | [diff] [blame] | 256 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 259 | static GVariant * |
| 260 | fu_main_release_array_to_variant (GPtrArray *results) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 261 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 262 | GVariantBuilder builder; |
| 263 | g_return_val_if_fail (results->len > 0, NULL); |
| 264 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 265 | for (guint i = 0; i < results->len; i++) { |
| 266 | FwupdRelease *rel = g_ptr_array_index (results, i); |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 267 | GVariant *tmp = fwupd_release_to_variant (rel); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 268 | g_variant_builder_add_value (&builder, tmp); |
| 269 | } |
| 270 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 273 | static GVariant * |
| 274 | fu_main_remote_array_to_variant (GPtrArray *remotes) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 275 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 276 | GVariantBuilder builder; |
| 277 | g_return_val_if_fail (remotes->len > 0, NULL); |
| 278 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 279 | for (guint i = 0; i < remotes->len; i++) { |
| 280 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 281 | GVariant *tmp = fwupd_remote_to_variant (remote); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 282 | g_variant_builder_add_value (&builder, tmp); |
| 283 | } |
| 284 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 287 | static GVariant * |
| 288 | fu_main_result_array_to_variant (GPtrArray *results) |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 289 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 290 | GVariantBuilder builder; |
| 291 | g_return_val_if_fail (results->len > 0, NULL); |
| 292 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 293 | for (guint i = 0; i < results->len; i++) { |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 294 | FwupdDevice *result = g_ptr_array_index (results, i); |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 295 | GVariant *tmp = fwupd_device_to_variant (result); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 296 | g_variant_builder_add_value (&builder, tmp); |
| 297 | } |
Richard Hughes | 9e1b140 | 2017-09-15 16:29:54 +0100 | [diff] [blame] | 298 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 301 | typedef struct { |
| 302 | GDBusMethodInvocation *invocation; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 303 | PolkitSubject *subject; |
| 304 | GPtrArray *install_tasks; |
| 305 | GPtrArray *action_ids; |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 306 | GPtrArray *checksums; |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 307 | guint64 flags; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 308 | GBytes *blob_cab; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 309 | FuMainPrivate *priv; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 310 | gchar *device_id; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 311 | gchar *remote_id; |
| 312 | gchar *key; |
| 313 | gchar *value; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 314 | XbSilo *silo; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 315 | } FuMainAuthHelper; |
| 316 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 317 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 318 | fu_main_auth_helper_free (FuMainAuthHelper *helper) |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 319 | { |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 320 | if (helper->blob_cab != NULL) |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 321 | g_bytes_unref (helper->blob_cab); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 322 | if (helper->subject != NULL) |
| 323 | g_object_unref (helper->subject); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 324 | if (helper->silo != NULL) |
| 325 | g_object_unref (helper->silo); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 326 | if (helper->install_tasks != NULL) |
| 327 | g_ptr_array_unref (helper->install_tasks); |
| 328 | if (helper->action_ids != NULL) |
| 329 | g_ptr_array_unref (helper->action_ids); |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 330 | if (helper->checksums != NULL) |
| 331 | g_ptr_array_unref (helper->checksums); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 332 | g_free (helper->device_id); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 333 | g_free (helper->remote_id); |
| 334 | g_free (helper->key); |
| 335 | g_free (helper->value); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 336 | g_object_unref (helper->invocation); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 337 | g_free (helper); |
| 338 | } |
| 339 | |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 340 | #pragma clang diagnostic push |
| 341 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 342 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainAuthHelper, fu_main_auth_helper_free) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 343 | #pragma clang diagnostic pop |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 344 | |
| 345 | /* error may or may not already have been set */ |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 346 | static gboolean |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 347 | fu_main_authorization_is_valid (PolkitAuthorizationResult *auth, GError **error) |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 348 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 349 | /* failed */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 350 | if (auth == NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 351 | g_autofree gchar *message = g_strdup ((*error)->message); |
| 352 | g_clear_error (error); |
| 353 | g_set_error (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 354 | FWUPD_ERROR, |
| 355 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 356 | "Could not check for auth: %s", message); |
| 357 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* did not auth */ |
| 361 | if (!polkit_authorization_result_get_is_authorized (auth)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 362 | g_set_error_literal (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 363 | FWUPD_ERROR, |
| 364 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 365 | "Failed to obtain auth"); |
| 366 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 370 | return TRUE; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 373 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 374 | fu_main_authorize_unlock_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 375 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 376 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 377 | g_autoptr(GError) error = NULL; |
| 378 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 379 | |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 380 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 381 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 382 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 383 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 384 | res, &error); |
| 385 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 386 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 387 | return; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 388 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 389 | |
| 390 | /* authenticated */ |
| 391 | if (!fu_engine_unlock (helper->priv->engine, helper->device_id, &error)) { |
| 392 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | /* success */ |
| 397 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static void |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 401 | fu_main_authorize_set_approved_firmware_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 402 | { |
| 403 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 404 | g_autoptr(GError) error = NULL; |
| 405 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 406 | |
| 407 | /* get result */ |
| 408 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
| 409 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 410 | res, &error); |
| 411 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 412 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | /* success */ |
| 417 | for (guint i = 0; i < helper->checksums->len; i++) { |
| 418 | const gchar *csum = g_ptr_array_index (helper->checksums, i); |
| 419 | fu_engine_add_approved_firmware (helper->priv->engine, csum); |
| 420 | } |
| 421 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 422 | } |
| 423 | |
| 424 | static void |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 425 | fu_main_authorize_self_sign_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 426 | { |
| 427 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 428 | g_autofree gchar *sig = NULL; |
| 429 | g_autoptr(GError) error = NULL; |
| 430 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 431 | |
| 432 | /* get result */ |
| 433 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
| 434 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 435 | res, &error); |
| 436 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 437 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | /* authenticated */ |
| 442 | sig = fu_engine_self_sign (helper->priv->engine, helper->value, helper->flags, &error); |
| 443 | if (sig == NULL) { |
| 444 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | /* success */ |
| 449 | g_dbus_method_invocation_return_value (helper->invocation, g_variant_new ("(s)", sig)); |
| 450 | } |
| 451 | |
| 452 | static void |
Mario Limonciello | bfcf75b | 2019-04-17 15:05:39 +0100 | [diff] [blame] | 453 | fu_main_modify_config_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 454 | { |
| 455 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 456 | g_autoptr(GError) error = NULL; |
| 457 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 458 | |
| 459 | /* get result */ |
| 460 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 461 | res, &error); |
| 462 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 463 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | if (!fu_engine_modify_config (helper->priv->engine, helper->key, helper->value, &error)) { |
| 468 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | /* success */ |
| 473 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 474 | } |
| 475 | |
| 476 | static void |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 477 | fu_main_authorize_activate_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 478 | { |
| 479 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 480 | g_autoptr(GError) error = NULL; |
| 481 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 482 | |
| 483 | /* get result */ |
| 484 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
| 485 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 486 | res, &error); |
| 487 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 488 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | /* authenticated */ |
| 493 | if (!fu_engine_activate (helper->priv->engine, helper->device_id, &error)) { |
| 494 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | /* success */ |
| 499 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 500 | } |
| 501 | |
| 502 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 503 | fu_main_authorize_verify_update_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 504 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 505 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 506 | g_autoptr(GError) error = NULL; |
| 507 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 508 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 509 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 510 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 511 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 512 | res, &error); |
| 513 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 514 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 515 | return; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 516 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 517 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 518 | /* authenticated */ |
| 519 | if (!fu_engine_verify_update (helper->priv->engine, helper->device_id, &error)) { |
| 520 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 521 | return; |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 525 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 528 | static void |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 529 | fu_main_authorize_modify_remote_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 530 | { |
| 531 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 532 | g_autoptr(GError) error = NULL; |
| 533 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 534 | |
| 535 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 536 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 537 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 538 | res, &error); |
| 539 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 540 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | /* authenticated */ |
| 545 | if (!fu_engine_modify_remote (helper->priv->engine, |
| 546 | helper->remote_id, |
| 547 | helper->key, |
| 548 | helper->value, |
| 549 | &error)) { |
| 550 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | /* success */ |
| 555 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 556 | } |
| 557 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 558 | static void fu_main_authorize_install_queue (FuMainAuthHelper *helper); |
| 559 | |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 560 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 561 | fu_main_authorize_install_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 562 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 563 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 564 | g_autoptr(GError) error = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 565 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 566 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 567 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 568 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 569 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 570 | res, &error); |
| 571 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 572 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 573 | return; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 576 | /* do the next authentication action ID */ |
| 577 | fu_main_authorize_install_queue (g_steal_pointer (&helper)); |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | fu_main_authorize_install_queue (FuMainAuthHelper *helper_ref) |
| 582 | { |
| 583 | FuMainPrivate *priv = helper_ref->priv; |
| 584 | g_autoptr(FuMainAuthHelper) helper = helper_ref; |
| 585 | g_autoptr(GError) error = NULL; |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 586 | gboolean ret; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 587 | |
| 588 | /* still more things to to authenticate */ |
| 589 | if (helper->action_ids->len > 0) { |
| 590 | g_autofree gchar *action_id = g_strdup (g_ptr_array_index (helper->action_ids, 0)); |
| 591 | g_autoptr(PolkitSubject) subject = g_object_ref (helper->subject); |
| 592 | g_ptr_array_remove_index (helper->action_ids, 0); |
| 593 | polkit_authority_check_authorization (priv->authority, subject, |
| 594 | action_id, NULL, |
| 595 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 596 | NULL, |
| 597 | fu_main_authorize_install_cb, |
| 598 | g_steal_pointer (&helper)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 599 | return; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 602 | /* all authenticated, so install all the things */ |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 603 | priv->update_in_progress = TRUE; |
| 604 | ret = fu_engine_install_tasks (helper->priv->engine, |
| 605 | helper->install_tasks, |
| 606 | helper->blob_cab, |
| 607 | helper->flags, |
| 608 | &error); |
| 609 | priv->update_in_progress = FALSE; |
| 610 | if (priv->pending_sigterm) |
| 611 | g_main_loop_quit (priv->loop); |
| 612 | if (!ret) { |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 613 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 614 | return; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 617 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 618 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 621 | #if !GLIB_CHECK_VERSION(2,54,0) |
| 622 | static gboolean |
| 623 | g_ptr_array_find (GPtrArray *haystack, gconstpointer needle, guint *index_) |
| 624 | { |
| 625 | for (guint i = 0; i < haystack->len; i++) { |
| 626 | gconstpointer *tmp = g_ptr_array_index (haystack, i); |
| 627 | if (tmp == needle) { |
| 628 | if (index_ != NULL) { |
| 629 | *index_ = i; |
| 630 | return TRUE; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | return FALSE; |
| 635 | } |
| 636 | #endif |
| 637 | |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 638 | static gint |
| 639 | fu_main_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 640 | { |
| 641 | FuInstallTask *task_a = *((FuInstallTask **) a); |
| 642 | FuInstallTask *task_b = *((FuInstallTask **) b); |
Richard Hughes | c02cb83 | 2018-05-20 10:31:04 +0100 | [diff] [blame] | 643 | return fu_install_task_compare (task_a, task_b); |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 646 | static GPtrArray * |
| 647 | fu_main_get_device_family (FuMainAuthHelper *helper, GError **error) |
| 648 | { |
| 649 | FuDevice *parent; |
| 650 | GPtrArray *children; |
| 651 | g_autoptr(FuDevice) device = NULL; |
| 652 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 653 | |
| 654 | /* get the device */ |
| 655 | device = fu_engine_get_device (helper->priv->engine, helper->device_id, error); |
| 656 | if (device == NULL) |
| 657 | return NULL; |
| 658 | |
| 659 | /* device itself */ |
| 660 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 661 | g_ptr_array_add (devices_possible, g_object_ref (device)); |
| 662 | |
| 663 | /* add device children */ |
| 664 | children = fu_device_get_children (device); |
| 665 | for (guint i = 0; i < children->len; i++) { |
| 666 | FuDevice *child = g_ptr_array_index (children, i); |
| 667 | g_ptr_array_add (devices_possible, g_object_ref (child)); |
| 668 | } |
| 669 | |
| 670 | /* add parent and siblings, not including the device itself */ |
| 671 | parent = fu_device_get_parent (device); |
| 672 | if (parent != NULL) { |
| 673 | GPtrArray *siblings = fu_device_get_children (parent); |
| 674 | g_ptr_array_add (devices_possible, g_object_ref (parent)); |
| 675 | for (guint i = 0; i < siblings->len; i++) { |
| 676 | FuDevice *sibling = g_ptr_array_index (siblings, i); |
| 677 | if (sibling == device) |
| 678 | continue; |
| 679 | g_ptr_array_add (devices_possible, g_object_ref (sibling)); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* success */ |
| 684 | return g_steal_pointer (&devices_possible); |
| 685 | } |
| 686 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 687 | static gboolean |
| 688 | fu_main_install_with_helper (FuMainAuthHelper *helper_ref, GError **error) |
| 689 | { |
| 690 | FuMainPrivate *priv = helper_ref->priv; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 691 | g_autoptr(FuMainAuthHelper) helper = helper_ref; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 692 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 693 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 694 | g_autoptr(GPtrArray) errors = NULL; |
| 695 | |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 696 | /* get a list of devices that in some way match the device_id */ |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 697 | if (g_strcmp0 (helper->device_id, FWUPD_DEVICE_ID_ANY) == 0) { |
| 698 | devices_possible = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 699 | if (devices_possible == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 700 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 701 | } else { |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 702 | devices_possible = fu_main_get_device_family (helper, error); |
| 703 | if (devices_possible == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 704 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 707 | /* parse silo */ |
| 708 | helper->silo = fu_engine_get_silo_from_blob (priv->engine, |
| 709 | helper->blob_cab, |
| 710 | error); |
| 711 | if (helper->silo == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 712 | return FALSE; |
| 713 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 714 | /* for each component in the silo */ |
Mario Limonciello | 51ddf18 | 2019-01-26 00:31:58 -0600 | [diff] [blame] | 715 | components = xb_silo_query (helper->silo, "components/component", 0, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 716 | if (components == NULL) |
| 717 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 718 | helper->action_ids = g_ptr_array_new_with_free_func (g_free); |
| 719 | helper->install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 720 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 721 | for (guint i = 0; i < components->len; i++) { |
| 722 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 723 | |
| 724 | /* do any devices pass the requirements */ |
| 725 | for (guint j = 0; j < devices_possible->len; j++) { |
| 726 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 727 | const gchar *action_id; |
| 728 | g_autoptr(FuInstallTask) task = NULL; |
| 729 | g_autoptr(GError) error_local = NULL; |
| 730 | |
| 731 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 732 | task = fu_install_task_new (device, component); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 733 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 734 | task, |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 735 | helper->flags | FWUPD_INSTALL_FLAG_FORCE, |
| 736 | &error_local)) { |
| 737 | g_debug ("first pass requirement on %s:%s failed: %s", |
| 738 | fu_device_get_id (device), |
| 739 | xb_node_query_text (component, "id", NULL), |
| 740 | error_local->message); |
| 741 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 742 | continue; |
| 743 | } |
| 744 | |
| 745 | /* make a second pass using possibly updated version format now */ |
| 746 | fu_engine_md_refresh_device_from_component (priv->engine, device, component); |
| 747 | if (!fu_engine_check_requirements (priv->engine, |
| 748 | task, |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 749 | helper->flags, |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 750 | &error_local)) { |
Mario Limonciello | 537da0e | 2020-03-09 15:38:17 -0500 | [diff] [blame] | 751 | g_debug ("second pass requirement on %s:%s failed: %s", |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 752 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 753 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 754 | error_local->message); |
| 755 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 756 | continue; |
| 757 | } |
| 758 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 759 | /* if component should have an update message from CAB */ |
| 760 | fu_device_incorporate_from_component (device, component); |
| 761 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 762 | /* get the action IDs for the valid device */ |
| 763 | action_id = fu_install_task_get_action_id (task); |
| 764 | if (!g_ptr_array_find (helper->action_ids, action_id, NULL)) |
| 765 | g_ptr_array_add (helper->action_ids, g_strdup (action_id)); |
| 766 | g_ptr_array_add (helper->install_tasks, g_steal_pointer (&task)); |
| 767 | } |
| 768 | } |
| 769 | |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 770 | /* order the install tasks by the device priority */ |
| 771 | g_ptr_array_sort (helper->install_tasks, fu_main_install_task_sort_cb); |
| 772 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 773 | /* nothing suitable */ |
| 774 | if (helper->install_tasks->len == 0) { |
Richard Hughes | e82eef3 | 2018-05-20 10:41:26 +0100 | [diff] [blame] | 775 | GError *error_tmp = fu_common_error_array_get_best (errors); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 776 | g_propagate_error (error, error_tmp); |
| 777 | return FALSE; |
| 778 | } |
| 779 | |
| 780 | /* authenticate all things in the action_ids */ |
| 781 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 782 | fu_main_authorize_install_queue (g_steal_pointer (&helper)); |
| 783 | return TRUE; |
| 784 | } |
| 785 | |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 786 | static gboolean |
| 787 | fu_main_device_id_valid (const gchar *device_id, GError **error) |
| 788 | { |
| 789 | if (g_strcmp0 (device_id, FWUPD_DEVICE_ID_ANY) == 0) |
| 790 | return TRUE; |
| 791 | if (device_id != NULL && strlen (device_id) >= 4) |
| 792 | return TRUE; |
| 793 | g_set_error (error, |
| 794 | FWUPD_ERROR, |
| 795 | FWUPD_ERROR_INTERNAL, |
| 796 | "invalid device ID: %s", |
| 797 | device_id); |
| 798 | return FALSE; |
| 799 | } |
| 800 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 801 | static void |
| 802 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 803 | const gchar *object_path, const gchar *interface_name, |
| 804 | const gchar *method_name, GVariant *parameters, |
| 805 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 806 | { |
| 807 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 808 | GVariant *val = NULL; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 809 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 810 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 811 | /* activity */ |
| 812 | fu_engine_idle_reset (priv->engine); |
| 813 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 814 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 815 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 816 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 817 | devices = fu_engine_get_devices (priv->engine, &error); |
| 818 | if (devices == NULL) { |
| 819 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 820 | return; |
| 821 | } |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 822 | val = fu_main_device_array_to_variant (priv, sender, devices, &error); |
| 823 | if (val == NULL) { |
| 824 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 825 | return; |
| 826 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 827 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 828 | return; |
| 829 | } |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 830 | if (g_strcmp0 (method_name, "GetReleases") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 831 | const gchar *device_id; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 832 | g_autoptr(GPtrArray) releases = NULL; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 833 | g_variant_get (parameters, "(&s)", &device_id); |
| 834 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 835 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 836 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 837 | return; |
| 838 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 839 | releases = fu_engine_get_releases (priv->engine, device_id, &error); |
| 840 | if (releases == NULL) { |
| 841 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 842 | return; |
| 843 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 844 | val = fu_main_release_array_to_variant (releases); |
| 845 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 846 | return; |
| 847 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 848 | if (g_strcmp0 (method_name, "GetApprovedFirmware") == 0) { |
| 849 | GVariantBuilder builder; |
| 850 | GPtrArray *checksums = fu_engine_get_approved_firmware (priv->engine); |
| 851 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); |
| 852 | for (guint i = 0; i < checksums->len; i++) { |
| 853 | const gchar *checksum = g_ptr_array_index (checksums, i); |
| 854 | g_variant_builder_add_value (&builder, g_variant_new_string (checksum)); |
| 855 | } |
| 856 | val = g_variant_builder_end (&builder); |
| 857 | g_dbus_method_invocation_return_value (invocation, |
| 858 | g_variant_new_tuple (&val, 1)); |
| 859 | return; |
| 860 | } |
Richard Hughes | 6ecc4ca | 2020-05-20 18:42:46 +0100 | [diff] [blame^] | 861 | if (g_strcmp0 (method_name, "GetReportMetadata") == 0) { |
| 862 | GHashTableIter iter; |
| 863 | GVariantBuilder builder; |
| 864 | const gchar *key; |
| 865 | const gchar *value; |
| 866 | g_autoptr(GHashTable) metadata = NULL; |
| 867 | |
| 868 | metadata = fu_engine_get_report_metadata (priv->engine, &error); |
| 869 | if (metadata == NULL) { |
| 870 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 871 | return; |
| 872 | } |
| 873 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); |
| 874 | g_hash_table_iter_init (&iter, metadata); |
| 875 | while (g_hash_table_iter_next (&iter, |
| 876 | (gpointer *) &key, |
| 877 | (gpointer *) &value)) { |
| 878 | g_variant_builder_add_value (&builder, |
| 879 | g_variant_new ("{ss}", key, value)); |
| 880 | } |
| 881 | val = g_variant_builder_end (&builder); |
| 882 | g_dbus_method_invocation_return_value (invocation, |
| 883 | g_variant_new_tuple (&val, 1)); |
| 884 | return; |
| 885 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 886 | if (g_strcmp0 (method_name, "SetApprovedFirmware") == 0) { |
| 887 | g_autofree gchar *checksums_str = NULL; |
| 888 | g_auto(GStrv) checksums = NULL; |
| 889 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 890 | g_autoptr(PolkitSubject) subject = NULL; |
| 891 | |
| 892 | g_variant_get (parameters, "(^as)", &checksums); |
| 893 | checksums_str = g_strjoinv (",", checksums); |
| 894 | g_debug ("Called %s(%s)", method_name, checksums_str); |
| 895 | |
| 896 | /* authenticate */ |
| 897 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 898 | helper = g_new0 (FuMainAuthHelper, 1); |
| 899 | helper->priv = priv; |
| 900 | helper->invocation = g_object_ref (invocation); |
| 901 | helper->checksums = g_ptr_array_new_with_free_func (g_free); |
| 902 | for (guint i = 0; checksums[i] != NULL; i++) |
| 903 | g_ptr_array_add (helper->checksums, g_strdup (checksums[i])); |
| 904 | subject = polkit_system_bus_name_new (sender); |
| 905 | polkit_authority_check_authorization (priv->authority, subject, |
| 906 | "org.freedesktop.fwupd.set-approved-firmware", |
| 907 | NULL, |
| 908 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 909 | NULL, |
| 910 | fu_main_authorize_set_approved_firmware_cb, |
| 911 | g_steal_pointer (&helper)); |
| 912 | return; |
| 913 | } |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 914 | if (g_strcmp0 (method_name, "SelfSign") == 0) { |
| 915 | GVariant *prop_value; |
| 916 | gchar *prop_key; |
| 917 | g_autofree gchar *value = NULL; |
| 918 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 919 | g_autoptr(PolkitSubject) subject = NULL; |
| 920 | g_autoptr(GVariantIter) iter = NULL; |
| 921 | |
| 922 | g_variant_get (parameters, "(sa{sv})", &value, &iter); |
| 923 | g_debug ("Called %s(%s)", method_name, value); |
| 924 | |
| 925 | /* get flags */ |
| 926 | helper = g_new0 (FuMainAuthHelper, 1); |
| 927 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
| 928 | g_debug ("got option %s", prop_key); |
| 929 | if (g_strcmp0 (prop_key, "add-timestamp") == 0 && |
| 930 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 931 | helper->flags |= JCAT_SIGN_FLAG_ADD_TIMESTAMP; |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 932 | if (g_strcmp0 (prop_key, "add-cert") == 0 && |
| 933 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | d5aab65 | 2020-02-25 12:47:50 +0000 | [diff] [blame] | 934 | helper->flags |= JCAT_SIGN_FLAG_ADD_CERT; |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 935 | g_variant_unref (prop_value); |
| 936 | } |
| 937 | |
| 938 | /* authenticate */ |
| 939 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 940 | helper->priv = priv; |
| 941 | helper->value = g_steal_pointer (&value); |
| 942 | helper->invocation = g_object_ref (invocation); |
| 943 | subject = polkit_system_bus_name_new (sender); |
| 944 | polkit_authority_check_authorization (priv->authority, subject, |
| 945 | "org.freedesktop.fwupd.self-sign", |
| 946 | NULL, |
| 947 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 948 | NULL, |
| 949 | fu_main_authorize_self_sign_cb, |
| 950 | g_steal_pointer (&helper)); |
| 951 | return; |
| 952 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 953 | if (g_strcmp0 (method_name, "GetDowngrades") == 0) { |
| 954 | const gchar *device_id; |
| 955 | g_autoptr(GPtrArray) releases = NULL; |
| 956 | g_variant_get (parameters, "(&s)", &device_id); |
| 957 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 958 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 959 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 960 | return; |
| 961 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 962 | releases = fu_engine_get_downgrades (priv->engine, device_id, &error); |
| 963 | if (releases == NULL) { |
| 964 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 965 | return; |
| 966 | } |
| 967 | val = fu_main_release_array_to_variant (releases); |
| 968 | g_dbus_method_invocation_return_value (invocation, val); |
| 969 | return; |
| 970 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 971 | if (g_strcmp0 (method_name, "GetUpgrades") == 0) { |
| 972 | const gchar *device_id; |
| 973 | g_autoptr(GPtrArray) releases = NULL; |
| 974 | g_variant_get (parameters, "(&s)", &device_id); |
| 975 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 976 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 977 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 978 | return; |
| 979 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 980 | releases = fu_engine_get_upgrades (priv->engine, device_id, &error); |
| 981 | if (releases == NULL) { |
| 982 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 983 | return; |
| 984 | } |
| 985 | val = fu_main_release_array_to_variant (releases); |
| 986 | g_dbus_method_invocation_return_value (invocation, val); |
| 987 | return; |
| 988 | } |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 989 | if (g_strcmp0 (method_name, "GetRemotes") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 990 | g_autoptr(GPtrArray) remotes = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 991 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 992 | remotes = fu_engine_get_remotes (priv->engine, &error); |
| 993 | if (remotes == NULL) { |
| 994 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 995 | return; |
| 996 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 997 | val = fu_main_remote_array_to_variant (remotes); |
| 998 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 999 | return; |
| 1000 | } |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 1001 | if (g_strcmp0 (method_name, "GetHistory") == 0) { |
| 1002 | g_autoptr(GPtrArray) devices = NULL; |
| 1003 | g_debug ("Called %s()", method_name); |
| 1004 | devices = fu_engine_get_history (priv->engine, &error); |
| 1005 | if (devices == NULL) { |
| 1006 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1007 | return; |
| 1008 | } |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 1009 | val = fu_main_device_array_to_variant (priv, sender, devices, &error); |
| 1010 | if (val == NULL) { |
| 1011 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1012 | return; |
| 1013 | } |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 1014 | g_dbus_method_invocation_return_value (invocation, val); |
| 1015 | return; |
| 1016 | } |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 1017 | if (g_strcmp0 (method_name, "GetHostSecurityAttrs") == 0) { |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 1018 | g_autoptr(FuSecurityAttrs) attrs = NULL; |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 1019 | g_debug ("Called %s()", method_name); |
Richard Hughes | 56e7ae5 | 2020-05-17 21:00:23 +0100 | [diff] [blame] | 1020 | attrs = fu_engine_get_host_security_attrs (priv->engine); |
Richard Hughes | f58ac73 | 2020-05-12 15:23:44 +0100 | [diff] [blame] | 1021 | val = fu_security_attrs_to_variant (attrs); |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 1022 | g_dbus_method_invocation_return_value (invocation, val); |
| 1023 | return; |
| 1024 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1025 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1026 | const gchar *device_id; |
| 1027 | g_variant_get (parameters, "(&s)", &device_id); |
| 1028 | g_debug ("Called %s(%s)", method_name, device_id); |
| 1029 | if (!fu_engine_clear_results (priv->engine, device_id, &error)) { |
| 1030 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1033 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | } |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 1036 | if (g_strcmp0 (method_name, "ModifyDevice") == 0) { |
| 1037 | const gchar *device_id; |
| 1038 | const gchar *key = NULL; |
| 1039 | const gchar *value = NULL; |
| 1040 | |
| 1041 | /* check the id exists */ |
| 1042 | g_variant_get (parameters, "(&s&s&s)", &device_id, &key, &value); |
| 1043 | g_debug ("Called %s(%s,%s=%s)", method_name, device_id, key, value); |
| 1044 | if (!fu_engine_modify_device (priv->engine, device_id, key, value, &error)) { |
| 1045 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1046 | return; |
| 1047 | } |
| 1048 | g_dbus_method_invocation_return_value (invocation, NULL); |
| 1049 | return; |
| 1050 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1051 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1052 | const gchar *device_id = NULL; |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 1053 | g_autoptr(FwupdDevice) result = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1054 | g_variant_get (parameters, "(&s)", &device_id); |
| 1055 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1056 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1057 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1058 | return; |
| 1059 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1060 | result = fu_engine_get_results (priv->engine, device_id, &error); |
| 1061 | if (result == NULL) { |
| 1062 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1063 | return; |
| 1064 | } |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 1065 | val = fwupd_device_to_variant (result); |
| 1066 | g_dbus_method_invocation_return_value (invocation, |
| 1067 | g_variant_new_tuple (&val, 1)); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 1068 | return; |
| 1069 | } |
Richard Hughes | ba73c76 | 2017-09-15 14:31:17 +0100 | [diff] [blame] | 1070 | if (g_strcmp0 (method_name, "UpdateMetadata") == 0) { |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1071 | GDBusMessage *message; |
| 1072 | GUnixFDList *fd_list; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1073 | const gchar *remote_id = NULL; |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1074 | gint fd_data; |
| 1075 | gint fd_sig; |
| 1076 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1077 | g_variant_get (parameters, "(&shh)", &remote_id, &fd_data, &fd_sig); |
| 1078 | g_debug ("Called %s(%s,%i,%i)", method_name, remote_id, fd_data, fd_sig); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1079 | |
Richard Hughes | 5935ebd | 2017-06-16 15:40:31 +0100 | [diff] [blame] | 1080 | /* update the metadata store */ |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1081 | message = g_dbus_method_invocation_get_message (invocation); |
| 1082 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1083 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 2) { |
| 1084 | g_set_error (&error, |
| 1085 | FWUPD_ERROR, |
| 1086 | FWUPD_ERROR_INTERNAL, |
| 1087 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1088 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1089 | return; |
| 1090 | } |
| 1091 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 1092 | if (fd_data < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1093 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 1097 | if (fd_sig < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1098 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1099 | return; |
| 1100 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1101 | |
| 1102 | /* store new metadata (will close the fds when done) */ |
| 1103 | if (!fu_engine_update_metadata (priv->engine, remote_id, |
| 1104 | fd_data, fd_sig, &error)) { |
Richard Hughes | f3d46c6 | 2017-11-28 14:01:30 +0000 | [diff] [blame] | 1105 | g_prefix_error (&error, "Failed to update metadata for %s: ", remote_id); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1106 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1107 | return; |
| 1108 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1109 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1110 | return; |
| 1111 | } |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1112 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1113 | const gchar *device_id = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1114 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1115 | g_autoptr(PolkitSubject) subject = NULL; |
| 1116 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1117 | g_variant_get (parameters, "(&s)", &device_id); |
| 1118 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1119 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1120 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1121 | return; |
| 1122 | } |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1123 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1124 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 1125 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1126 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1127 | helper->priv = priv; |
| 1128 | helper->invocation = g_object_ref (invocation); |
| 1129 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1130 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1131 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1132 | "org.freedesktop.fwupd.device-unlock", |
| 1133 | NULL, |
| 1134 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1135 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1136 | fu_main_authorize_unlock_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1137 | g_steal_pointer (&helper)); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1138 | return; |
| 1139 | } |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1140 | if (g_strcmp0 (method_name, "Activate") == 0) { |
| 1141 | const gchar *device_id = NULL; |
| 1142 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 1143 | g_autoptr(PolkitSubject) subject = NULL; |
| 1144 | |
| 1145 | g_variant_get (parameters, "(&s)", &device_id); |
| 1146 | g_debug ("Called %s(%s)", method_name, device_id); |
| 1147 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1148 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | /* authenticate */ |
| 1153 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 1154 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1155 | helper->priv = priv; |
| 1156 | helper->invocation = g_object_ref (invocation); |
| 1157 | helper->device_id = g_strdup (device_id); |
| 1158 | subject = polkit_system_bus_name_new (sender); |
| 1159 | polkit_authority_check_authorization (priv->authority, subject, |
| 1160 | "org.freedesktop.fwupd.device-activate", |
| 1161 | NULL, |
| 1162 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1163 | NULL, |
| 1164 | fu_main_authorize_activate_cb, |
| 1165 | g_steal_pointer (&helper)); |
| 1166 | return; |
| 1167 | } |
Mario Limonciello | bfcf75b | 2019-04-17 15:05:39 +0100 | [diff] [blame] | 1168 | if (g_strcmp0 (method_name, "ModifyConfig") == 0) { |
| 1169 | g_autofree gchar *key = NULL; |
| 1170 | g_autofree gchar *value = NULL; |
| 1171 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 1172 | g_autoptr(PolkitSubject) subject = NULL; |
| 1173 | |
| 1174 | g_variant_get (parameters, "(ss)", &key, &value); |
| 1175 | g_debug ("Called %s(%s=%s)", method_name, key, value); |
| 1176 | |
| 1177 | /* authenticate */ |
| 1178 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1179 | helper->priv = priv; |
| 1180 | helper->key = g_steal_pointer (&key); |
| 1181 | helper->value = g_steal_pointer (&value); |
| 1182 | helper->invocation = g_object_ref (invocation); |
| 1183 | subject = polkit_system_bus_name_new (sender); |
| 1184 | polkit_authority_check_authorization (priv->authority, subject, |
| 1185 | "org.freedesktop.fwupd.modify-config", |
| 1186 | NULL, |
| 1187 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1188 | NULL, |
| 1189 | fu_main_modify_config_cb, |
| 1190 | g_steal_pointer (&helper)); |
| 1191 | return; |
| 1192 | } |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1193 | if (g_strcmp0 (method_name, "ModifyRemote") == 0) { |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1194 | const gchar *remote_id = NULL; |
| 1195 | const gchar *key = NULL; |
| 1196 | const gchar *value = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1197 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1198 | g_autoptr(PolkitSubject) subject = NULL; |
| 1199 | |
| 1200 | /* check the id exists */ |
| 1201 | g_variant_get (parameters, "(&s&s&s)", &remote_id, &key, &value); |
| 1202 | g_debug ("Called %s(%s,%s=%s)", method_name, remote_id, key, value); |
| 1203 | |
| 1204 | /* create helper object */ |
| 1205 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1206 | helper->invocation = g_object_ref (invocation); |
| 1207 | helper->remote_id = g_strdup (remote_id); |
| 1208 | helper->key = g_strdup (key); |
| 1209 | helper->value = g_strdup (value); |
| 1210 | helper->priv = priv; |
| 1211 | |
| 1212 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 1213 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1214 | subject = polkit_system_bus_name_new (sender); |
Mario Limonciello | 6b9f07c | 2018-04-16 13:52:09 -0500 | [diff] [blame] | 1215 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1216 | "org.freedesktop.fwupd.modify-remote", |
| 1217 | NULL, |
| 1218 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1219 | NULL, |
| 1220 | fu_main_authorize_modify_remote_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1221 | g_steal_pointer (&helper)); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1222 | return; |
| 1223 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1224 | if (g_strcmp0 (method_name, "VerifyUpdate") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1225 | const gchar *device_id = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1226 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1227 | g_autoptr(PolkitSubject) subject = NULL; |
| 1228 | |
| 1229 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1230 | g_variant_get (parameters, "(&s)", &device_id); |
| 1231 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1232 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1233 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1234 | return; |
| 1235 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1236 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1237 | /* create helper object */ |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1238 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1239 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1240 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1241 | helper->priv = priv; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1242 | |
| 1243 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 1244 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1245 | subject = polkit_system_bus_name_new (sender); |
Mario Limonciello | 6b9f07c | 2018-04-16 13:52:09 -0500 | [diff] [blame] | 1246 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1247 | "org.freedesktop.fwupd.verify-update", |
| 1248 | NULL, |
| 1249 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1250 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1251 | fu_main_authorize_verify_update_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1252 | g_steal_pointer (&helper)); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1253 | return; |
| 1254 | } |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1255 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1256 | const gchar *device_id = NULL; |
| 1257 | g_variant_get (parameters, "(&s)", &device_id); |
| 1258 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1259 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1260 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1261 | return; |
| 1262 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1263 | if (!fu_engine_verify (priv->engine, device_id, &error)) { |
| 1264 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1265 | return; |
| 1266 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1267 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1268 | return; |
| 1269 | } |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1270 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1271 | GVariant *prop_value; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1272 | const gchar *device_id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1273 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1274 | gint32 fd_handle = 0; |
| 1275 | gint fd; |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 1276 | guint64 archive_size_max; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1277 | GDBusMessage *message; |
| 1278 | GUnixFDList *fd_list; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1279 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1280 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1283 | g_variant_get (parameters, "(&sha{sv})", &device_id, &fd_handle, &iter); |
| 1284 | g_debug ("Called %s(%s,%i)", method_name, device_id, fd_handle); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1285 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1286 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1287 | return; |
| 1288 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1289 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1290 | /* create helper object */ |
| 1291 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1292 | helper->invocation = g_object_ref (invocation); |
| 1293 | helper->device_id = g_strdup (device_id); |
| 1294 | helper->priv = priv; |
| 1295 | |
| 1296 | /* get flags */ |
| 1297 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1298 | g_debug ("got option %s", prop_key); |
| 1299 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 1300 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1301 | helper->flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1302 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 1303 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1304 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1305 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 1306 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1307 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 1308 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 1309 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1310 | helper->flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 76e0f94 | 2018-05-14 16:24:00 +0100 | [diff] [blame] | 1311 | if (g_strcmp0 (prop_key, "no-history") == 0 && |
| 1312 | g_variant_get_boolean (prop_value) == TRUE) |
| 1313 | helper->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 1314 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1317 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1318 | /* get the fd */ |
| 1319 | message = g_dbus_method_invocation_get_message (invocation); |
| 1320 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1321 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1322 | g_set_error (&error, |
| 1323 | FWUPD_ERROR, |
| 1324 | FWUPD_ERROR_INTERNAL, |
| 1325 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1326 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1327 | return; |
| 1328 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 1329 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1330 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1331 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1335 | /* parse the cab file before authenticating so we can work out |
| 1336 | * what action ID to use, for instance, if this is trusted -- |
| 1337 | * this will also close the fd when done */ |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 1338 | archive_size_max = fu_engine_get_archive_size_max (priv->engine); |
| 1339 | helper->blob_cab = fu_common_get_contents_fd (fd, archive_size_max, &error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1340 | if (helper->blob_cab == NULL) { |
| 1341 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1342 | return; |
| 1343 | } |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 1344 | |
| 1345 | /* install all the things in the store */ |
| 1346 | helper->subject = polkit_system_bus_name_new (sender); |
| 1347 | if (!fu_main_install_with_helper (g_steal_pointer (&helper), &error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1348 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1349 | return; |
| 1350 | } |
| 1351 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 1352 | /* async return */ |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1353 | return; |
| 1354 | } |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 1355 | if (g_strcmp0 (method_name, "GetDetails") == 0) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1356 | GDBusMessage *message; |
| 1357 | GUnixFDList *fd_list; |
| 1358 | gint32 fd_handle = 0; |
| 1359 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1360 | g_autoptr(GPtrArray) results = NULL; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1361 | |
| 1362 | /* get parameters */ |
| 1363 | g_variant_get (parameters, "(h)", &fd_handle); |
| 1364 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 1365 | |
| 1366 | /* get the fd */ |
| 1367 | message = g_dbus_method_invocation_get_message (invocation); |
| 1368 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1369 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 1) { |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1370 | g_set_error (&error, |
| 1371 | FWUPD_ERROR, |
| 1372 | FWUPD_ERROR_INTERNAL, |
| 1373 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1374 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1375 | return; |
| 1376 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 1377 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1378 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1379 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1380 | return; |
| 1381 | } |
| 1382 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1383 | /* get details about the file (will close the fd when done) */ |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 1384 | results = fu_engine_get_details (priv->engine, fd, &error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1385 | if (results == NULL) { |
| 1386 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1387 | return; |
| 1388 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1389 | val = fu_main_result_array_to_variant (results); |
| 1390 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1393 | g_set_error (&error, |
| 1394 | G_DBUS_ERROR, |
| 1395 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 1396 | "no such method %s", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1397 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1400 | static GVariant * |
| 1401 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 1402 | const gchar *object_path, const gchar *interface_name, |
| 1403 | const gchar *property_name, GError **error, |
| 1404 | gpointer user_data) |
| 1405 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1406 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1407 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1408 | /* activity */ |
| 1409 | fu_engine_idle_reset (priv->engine); |
| 1410 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1411 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
Richard Hughes | fe4b3ea | 2020-03-30 10:53:20 +0100 | [diff] [blame] | 1412 | return g_variant_new_string (SOURCE_VERSION); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1413 | |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 1414 | if (g_strcmp0 (property_name, "Tainted") == 0) |
| 1415 | return g_variant_new_boolean (fu_engine_get_tainted (priv->engine)); |
| 1416 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1417 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1418 | return g_variant_new_uint32 (fu_engine_get_status (priv->engine)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1419 | |
Mario Limonciello | 20cc9ee | 2019-09-05 07:27:26 -0500 | [diff] [blame] | 1420 | if (g_strcmp0 (property_name, "HostProduct") == 0) |
| 1421 | return g_variant_new_string (fu_engine_get_host_product (priv->engine)); |
| 1422 | |
Richard Hughes | 0917fb6 | 2019-09-21 12:55:37 +0100 | [diff] [blame] | 1423 | if (g_strcmp0 (property_name, "HostMachineId") == 0) |
| 1424 | return g_variant_new_string (fu_engine_get_host_machine_id (priv->engine)); |
| 1425 | |
Richard Hughes | 196c6c6 | 2020-05-11 19:42:47 +0100 | [diff] [blame] | 1426 | if (g_strcmp0 (property_name, "HostSecurityId") == 0) |
| 1427 | return g_variant_new_string (fu_engine_get_host_security_id (priv->engine)); |
| 1428 | |
Mario Limonciello | eb4c764 | 2019-11-11 10:31:29 -0600 | [diff] [blame] | 1429 | if (g_strcmp0 (property_name, "Interactive") == 0) |
| 1430 | return g_variant_new_boolean (isatty (fileno (stdout)) != 0); |
| 1431 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1432 | /* return an error */ |
| 1433 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1434 | G_DBUS_ERROR, |
| 1435 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1436 | "failed to get daemon property %s", |
| 1437 | property_name); |
| 1438 | return NULL; |
| 1439 | } |
| 1440 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 1441 | static void |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1442 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 1443 | const gchar *name, |
| 1444 | gpointer user_data) |
| 1445 | { |
| 1446 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1447 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1448 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1449 | static const GDBusInterfaceVTable interface_vtable = { |
| 1450 | fu_main_daemon_method_call, |
| 1451 | fu_main_daemon_get_property, |
| 1452 | NULL |
| 1453 | }; |
| 1454 | |
| 1455 | priv->connection = g_object_ref (connection); |
| 1456 | registration_id = g_dbus_connection_register_object (connection, |
| 1457 | FWUPD_DBUS_PATH, |
| 1458 | priv->introspection_daemon->interfaces[0], |
| 1459 | &interface_vtable, |
| 1460 | priv, /* user_data */ |
| 1461 | NULL, /* user_data_free_func */ |
| 1462 | NULL); /* GError** */ |
| 1463 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* connect to D-Bus directly */ |
| 1466 | priv->proxy_uid = |
| 1467 | g_dbus_proxy_new_sync (priv->connection, |
| 1468 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 1469 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 1470 | NULL, |
| 1471 | "org.freedesktop.DBus", |
| 1472 | "/org/freedesktop/DBus", |
| 1473 | "org.freedesktop.DBus", |
| 1474 | NULL, |
| 1475 | &error); |
| 1476 | if (priv->proxy_uid == NULL) { |
| 1477 | g_warning ("cannot connect to DBus: %s", error->message); |
| 1478 | return; |
| 1479 | } |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1482 | static void |
| 1483 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 1484 | const gchar *name, |
| 1485 | gpointer user_data) |
| 1486 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1487 | g_debug ("FuMain: acquired name: %s", name); |
| 1488 | } |
| 1489 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1490 | static void |
| 1491 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 1492 | const gchar *name, |
| 1493 | gpointer user_data) |
| 1494 | { |
| 1495 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1496 | g_debug ("FuMain: lost name: %s", name); |
| 1497 | g_main_loop_quit (priv->loop); |
| 1498 | } |
| 1499 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1500 | static gboolean |
| 1501 | fu_main_timed_exit_cb (gpointer user_data) |
| 1502 | { |
| 1503 | GMainLoop *loop = (GMainLoop *) user_data; |
| 1504 | g_main_loop_quit (loop); |
| 1505 | return G_SOURCE_REMOVE; |
| 1506 | } |
| 1507 | |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame] | 1508 | static void |
| 1509 | fu_main_argv_changed_cb (GFileMonitor *monitor, GFile *file, GFile *other_file, |
| 1510 | GFileMonitorEvent event_type, gpointer user_data) |
| 1511 | { |
| 1512 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1513 | |
| 1514 | /* can do straight away? */ |
| 1515 | if (priv->update_in_progress) { |
| 1516 | g_warning ("binary changed during a firmware update, ignoring"); |
| 1517 | return; |
| 1518 | } |
| 1519 | g_debug ("binary changed, shutting down"); |
| 1520 | g_main_loop_quit (priv->loop); |
| 1521 | } |
| 1522 | |
Richard Hughes | 603e4f6 | 2019-12-11 13:44:09 +0000 | [diff] [blame] | 1523 | #if GLIB_CHECK_VERSION(2,63,3) |
| 1524 | static void |
| 1525 | fu_main_memory_monitor_warning_cb (GMemoryMonitor *memory_monitor, |
| 1526 | GMemoryMonitorWarningLevel level, |
| 1527 | FuMainPrivate *priv) |
| 1528 | { |
| 1529 | /* can do straight away? */ |
| 1530 | if (priv->update_in_progress) { |
| 1531 | g_warning ("OOM during a firmware update, ignoring"); |
| 1532 | priv->pending_sigterm = TRUE; |
| 1533 | return; |
| 1534 | } |
| 1535 | g_debug ("OOM event, shutting down"); |
| 1536 | g_main_loop_quit (priv->loop); |
| 1537 | } |
| 1538 | #endif |
| 1539 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1540 | static GDBusNodeInfo * |
| 1541 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 1542 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1543 | g_autoptr(GBytes) data = NULL; |
| 1544 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1545 | |
| 1546 | /* lookup data */ |
| 1547 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 1548 | data = g_resource_lookup_data (fu_get_resource (), |
| 1549 | path, |
| 1550 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 1551 | error); |
| 1552 | if (data == NULL) |
| 1553 | return NULL; |
| 1554 | |
| 1555 | /* build introspection from XML */ |
| 1556 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 1557 | } |
| 1558 | |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1559 | static void |
| 1560 | fu_main_private_free (FuMainPrivate *priv) |
| 1561 | { |
| 1562 | if (priv->loop != NULL) |
| 1563 | g_main_loop_unref (priv->loop); |
| 1564 | if (priv->owner_id > 0) |
| 1565 | g_bus_unown_name (priv->owner_id); |
| 1566 | if (priv->proxy_uid != NULL) |
| 1567 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1568 | if (priv->engine != NULL) |
| 1569 | g_object_unref (priv->engine); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1570 | if (priv->connection != NULL) |
| 1571 | g_object_unref (priv->connection); |
| 1572 | if (priv->authority != NULL) |
| 1573 | g_object_unref (priv->authority); |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame] | 1574 | if (priv->argv0_monitor != NULL) |
| 1575 | g_object_unref (priv->argv0_monitor); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1576 | if (priv->introspection_daemon != NULL) |
| 1577 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | 603e4f6 | 2019-12-11 13:44:09 +0000 | [diff] [blame] | 1578 | #if GLIB_CHECK_VERSION(2,63,3) |
| 1579 | if (priv->memory_monitor != NULL) |
| 1580 | g_object_unref (priv->memory_monitor); |
| 1581 | #endif |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1582 | g_free (priv); |
| 1583 | } |
| 1584 | |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 1585 | #pragma clang diagnostic push |
| 1586 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1587 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainPrivate, fu_main_private_free) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 1588 | #pragma clang diagnostic pop |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1589 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1590 | int |
| 1591 | main (int argc, char *argv[]) |
| 1592 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1593 | gboolean immediate_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1594 | gboolean timed_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1595 | const GOptionEntry options[] = { |
| 1596 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 1597 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 1598 | _("Exit after a small delay"), NULL }, |
| 1599 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 1600 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 1601 | _("Exit after the engine has loaded"), NULL }, |
| 1602 | { NULL} |
| 1603 | }; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1604 | g_autoptr(FuMainPrivate) priv = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1605 | g_autoptr(GError) error = NULL; |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame] | 1606 | g_autoptr(GFile) argv0_file = g_file_new_for_path (argv[0]); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1607 | g_autoptr(GOptionContext) context = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1608 | |
| 1609 | setlocale (LC_ALL, ""); |
| 1610 | |
Richard Hughes | 668ee21 | 2019-11-22 09:17:46 +0000 | [diff] [blame] | 1611 | bindtextdomain (GETTEXT_PACKAGE, FWUPD_LOCALEDIR); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1612 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 1613 | textdomain (GETTEXT_PACKAGE); |
| 1614 | |
| 1615 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1616 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1617 | context = g_option_context_new (NULL); |
| 1618 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1619 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 1620 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1621 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1622 | if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 1623 | g_printerr ("Failed to parse command line: %s\n", error->message); |
| 1624 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /* create new objects */ |
| 1628 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1629 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1630 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1631 | /* load engine */ |
Richard Hughes | 5b5f655 | 2018-05-18 10:22:39 +0100 | [diff] [blame] | 1632 | priv->engine = fu_engine_new (FU_APP_FLAGS_NONE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1633 | g_signal_connect (priv->engine, "changed", |
| 1634 | G_CALLBACK (fu_main_engine_changed_cb), |
| 1635 | priv); |
| 1636 | g_signal_connect (priv->engine, "device-added", |
| 1637 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1638 | priv); |
| 1639 | g_signal_connect (priv->engine, "device-removed", |
| 1640 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1641 | priv); |
| 1642 | g_signal_connect (priv->engine, "device-changed", |
| 1643 | G_CALLBACK (fu_main_engine_device_changed_cb), |
| 1644 | priv); |
| 1645 | g_signal_connect (priv->engine, "status-changed", |
| 1646 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1647 | priv); |
| 1648 | g_signal_connect (priv->engine, "percentage-changed", |
| 1649 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1650 | priv); |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 1651 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NONE, &error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1652 | g_printerr ("Failed to load engine: %s\n", error->message); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1653 | return EXIT_FAILURE; |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 1654 | } |
| 1655 | |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 1656 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1657 | SIGTERM, fu_main_sigterm_cb, |
| 1658 | priv, NULL); |
| 1659 | |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame] | 1660 | /* restart the daemon if the binary gets replaced */ |
| 1661 | priv->argv0_monitor = g_file_monitor_file (argv0_file, G_FILE_MONITOR_NONE, |
| 1662 | NULL, &error); |
| 1663 | g_signal_connect (priv->argv0_monitor, "changed", |
| 1664 | G_CALLBACK (fu_main_argv_changed_cb), priv); |
| 1665 | |
Richard Hughes | 603e4f6 | 2019-12-11 13:44:09 +0000 | [diff] [blame] | 1666 | #if GLIB_CHECK_VERSION(2,63,3) |
| 1667 | /* shut down on low memory event as we can just rescan hardware */ |
| 1668 | priv->memory_monitor = g_memory_monitor_dup_default (); |
| 1669 | g_signal_connect (G_OBJECT (priv->memory_monitor), "low-memory-warning", |
| 1670 | G_CALLBACK (fu_main_memory_monitor_warning_cb), priv); |
| 1671 | #endif |
| 1672 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1673 | /* load introspection from file */ |
| 1674 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 1675 | &error); |
| 1676 | if (priv->introspection_daemon == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1677 | g_printerr ("Failed to load introspection: %s\n", error->message); |
| 1678 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1681 | /* get authority */ |
| 1682 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 1683 | if (priv->authority == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1684 | g_printerr ("Failed to load authority: %s\n", error->message); |
| 1685 | return EXIT_FAILURE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Richard Hughes | ebae396 | 2018-09-09 13:40:49 +0100 | [diff] [blame] | 1688 | /* own the object */ |
| 1689 | priv->owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 1690 | FWUPD_DBUS_SERVICE, |
| 1691 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 1692 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 1693 | fu_main_on_bus_acquired_cb, |
| 1694 | fu_main_on_name_acquired_cb, |
| 1695 | fu_main_on_name_lost_cb, |
| 1696 | priv, NULL); |
| 1697 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1698 | /* Only timeout and close the mainloop if we have specified it |
| 1699 | * on the command line */ |
| 1700 | if (immediate_exit) |
| 1701 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 1702 | else if (timed_exit) |
| 1703 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 1704 | |
Mario Limonciello | 46bb4e9 | 2019-01-07 09:44:31 -0600 | [diff] [blame] | 1705 | g_debug ("Started with locale %s", g_getenv ("LANG")); |
| 1706 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1707 | /* wait */ |
Richard Hughes | 4619f9f | 2017-06-14 13:55:30 +0100 | [diff] [blame] | 1708 | g_message ("Daemon ready for requests"); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1709 | g_main_loop_run (priv->loop); |
| 1710 | |
| 1711 | /* success */ |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1712 | return EXIT_SUCCESS; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1713 | } |