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