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 | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 446 | fu_main_authorize_activate_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 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
| 454 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 455 | res, &error); |
| 456 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 457 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | /* authenticated */ |
| 462 | if (!fu_engine_activate (helper->priv->engine, helper->device_id, &error)) { |
| 463 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | /* success */ |
| 468 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 469 | } |
| 470 | |
| 471 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 472 | 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] | 473 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 474 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 475 | g_autoptr(GError) error = NULL; |
| 476 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 477 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 478 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 479 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 480 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 481 | res, &error); |
| 482 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 483 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 484 | return; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 485 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 486 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 487 | /* authenticated */ |
| 488 | if (!fu_engine_verify_update (helper->priv->engine, helper->device_id, &error)) { |
| 489 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 490 | return; |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 494 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 497 | static void |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 498 | fu_main_authorize_modify_remote_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 499 | { |
| 500 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 501 | g_autoptr(GError) error = NULL; |
| 502 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 503 | |
| 504 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 505 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 506 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 507 | res, &error); |
| 508 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 509 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | /* authenticated */ |
| 514 | if (!fu_engine_modify_remote (helper->priv->engine, |
| 515 | helper->remote_id, |
| 516 | helper->key, |
| 517 | helper->value, |
| 518 | &error)) { |
| 519 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | /* success */ |
| 524 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 525 | } |
| 526 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 527 | static void fu_main_authorize_install_queue (FuMainAuthHelper *helper); |
| 528 | |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 529 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 530 | fu_main_authorize_install_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 531 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 532 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 533 | g_autoptr(GError) error = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 534 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 535 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 536 | /* get result */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 537 | fu_main_set_status (helper->priv, FWUPD_STATUS_IDLE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 538 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 539 | res, &error); |
| 540 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 541 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 542 | return; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 545 | /* do the next authentication action ID */ |
| 546 | fu_main_authorize_install_queue (g_steal_pointer (&helper)); |
| 547 | } |
| 548 | |
| 549 | static void |
| 550 | fu_main_authorize_install_queue (FuMainAuthHelper *helper_ref) |
| 551 | { |
| 552 | FuMainPrivate *priv = helper_ref->priv; |
| 553 | g_autoptr(FuMainAuthHelper) helper = helper_ref; |
| 554 | g_autoptr(GError) error = NULL; |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 555 | gboolean ret; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 556 | |
| 557 | /* still more things to to authenticate */ |
| 558 | if (helper->action_ids->len > 0) { |
| 559 | g_autofree gchar *action_id = g_strdup (g_ptr_array_index (helper->action_ids, 0)); |
| 560 | g_autoptr(PolkitSubject) subject = g_object_ref (helper->subject); |
| 561 | g_ptr_array_remove_index (helper->action_ids, 0); |
| 562 | polkit_authority_check_authorization (priv->authority, subject, |
| 563 | action_id, NULL, |
| 564 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 565 | NULL, |
| 566 | fu_main_authorize_install_cb, |
| 567 | g_steal_pointer (&helper)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 568 | return; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 571 | /* all authenticated, so install all the things */ |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 572 | priv->update_in_progress = TRUE; |
| 573 | ret = fu_engine_install_tasks (helper->priv->engine, |
| 574 | helper->install_tasks, |
| 575 | helper->blob_cab, |
| 576 | helper->flags, |
| 577 | &error); |
| 578 | priv->update_in_progress = FALSE; |
| 579 | if (priv->pending_sigterm) |
| 580 | g_main_loop_quit (priv->loop); |
| 581 | if (!ret) { |
Richard Hughes | dbd8c76 | 2018-06-15 20:31:40 +0100 | [diff] [blame] | 582 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 583 | return; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 584 | } |
| 585 | |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 586 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 587 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 590 | #if !GLIB_CHECK_VERSION(2,54,0) |
| 591 | static gboolean |
| 592 | g_ptr_array_find (GPtrArray *haystack, gconstpointer needle, guint *index_) |
| 593 | { |
| 594 | for (guint i = 0; i < haystack->len; i++) { |
| 595 | gconstpointer *tmp = g_ptr_array_index (haystack, i); |
| 596 | if (tmp == needle) { |
| 597 | if (index_ != NULL) { |
| 598 | *index_ = i; |
| 599 | return TRUE; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | return FALSE; |
| 604 | } |
| 605 | #endif |
| 606 | |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 607 | static gint |
| 608 | fu_main_install_task_sort_cb (gconstpointer a, gconstpointer b) |
| 609 | { |
| 610 | FuInstallTask *task_a = *((FuInstallTask **) a); |
| 611 | FuInstallTask *task_b = *((FuInstallTask **) b); |
Richard Hughes | c02cb83 | 2018-05-20 10:31:04 +0100 | [diff] [blame] | 612 | return fu_install_task_compare (task_a, task_b); |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 615 | static GPtrArray * |
| 616 | fu_main_get_device_family (FuMainAuthHelper *helper, GError **error) |
| 617 | { |
| 618 | FuDevice *parent; |
| 619 | GPtrArray *children; |
| 620 | g_autoptr(FuDevice) device = NULL; |
| 621 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 622 | |
| 623 | /* get the device */ |
| 624 | device = fu_engine_get_device (helper->priv->engine, helper->device_id, error); |
| 625 | if (device == NULL) |
| 626 | return NULL; |
| 627 | |
| 628 | /* device itself */ |
| 629 | devices_possible = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 630 | g_ptr_array_add (devices_possible, g_object_ref (device)); |
| 631 | |
| 632 | /* add device children */ |
| 633 | children = fu_device_get_children (device); |
| 634 | for (guint i = 0; i < children->len; i++) { |
| 635 | FuDevice *child = g_ptr_array_index (children, i); |
| 636 | g_ptr_array_add (devices_possible, g_object_ref (child)); |
| 637 | } |
| 638 | |
| 639 | /* add parent and siblings, not including the device itself */ |
| 640 | parent = fu_device_get_parent (device); |
| 641 | if (parent != NULL) { |
| 642 | GPtrArray *siblings = fu_device_get_children (parent); |
| 643 | g_ptr_array_add (devices_possible, g_object_ref (parent)); |
| 644 | for (guint i = 0; i < siblings->len; i++) { |
| 645 | FuDevice *sibling = g_ptr_array_index (siblings, i); |
| 646 | if (sibling == device) |
| 647 | continue; |
| 648 | g_ptr_array_add (devices_possible, g_object_ref (sibling)); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /* success */ |
| 653 | return g_steal_pointer (&devices_possible); |
| 654 | } |
| 655 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 656 | static gboolean |
| 657 | fu_main_install_with_helper (FuMainAuthHelper *helper_ref, GError **error) |
| 658 | { |
| 659 | FuMainPrivate *priv = helper_ref->priv; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 660 | g_autoptr(FuMainAuthHelper) helper = helper_ref; |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 661 | g_autoptr(GPtrArray) components = NULL; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 662 | g_autoptr(GPtrArray) devices_possible = NULL; |
| 663 | g_autoptr(GPtrArray) errors = NULL; |
| 664 | |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 665 | /* 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] | 666 | if (g_strcmp0 (helper->device_id, FWUPD_DEVICE_ID_ANY) == 0) { |
| 667 | devices_possible = fu_engine_get_devices (priv->engine, error); |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 668 | if (devices_possible == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 669 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 670 | } else { |
Mario Limonciello | 2dd731b | 2018-10-09 15:25:18 -0500 | [diff] [blame] | 671 | devices_possible = fu_main_get_device_family (helper, error); |
| 672 | if (devices_possible == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 673 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 674 | } |
| 675 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 676 | /* parse silo */ |
| 677 | helper->silo = fu_engine_get_silo_from_blob (priv->engine, |
| 678 | helper->blob_cab, |
| 679 | error); |
| 680 | if (helper->silo == NULL) |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 681 | return FALSE; |
| 682 | |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 683 | /* for each component in the silo */ |
Mario Limonciello | 51ddf18 | 2019-01-26 00:31:58 -0600 | [diff] [blame] | 684 | components = xb_silo_query (helper->silo, "components/component", 0, error); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 685 | if (components == NULL) |
| 686 | return FALSE; |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 687 | helper->action_ids = g_ptr_array_new_with_free_func (g_free); |
| 688 | helper->install_tasks = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 689 | errors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_error_free); |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 690 | for (guint i = 0; i < components->len; i++) { |
| 691 | XbNode *component = g_ptr_array_index (components, i); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 692 | |
| 693 | /* do any devices pass the requirements */ |
| 694 | for (guint j = 0; j < devices_possible->len; j++) { |
| 695 | FuDevice *device = g_ptr_array_index (devices_possible, j); |
| 696 | const gchar *action_id; |
| 697 | g_autoptr(FuInstallTask) task = NULL; |
| 698 | g_autoptr(GError) error_local = NULL; |
| 699 | |
| 700 | /* is this component valid for the device */ |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 701 | task = fu_install_task_new (device, component); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 702 | if (!fu_engine_check_requirements (priv->engine, |
Richard Hughes | 1d1f5cf | 2018-05-19 23:06:03 +0100 | [diff] [blame] | 703 | task, |
| 704 | helper->flags, |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 705 | &error_local)) { |
| 706 | g_debug ("requirement on %s:%s failed: %s", |
| 707 | fu_device_get_id (device), |
Richard Hughes | 481aa2a | 2018-09-18 20:51:46 +0100 | [diff] [blame] | 708 | xb_node_query_text (component, "id", NULL), |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 709 | error_local->message); |
| 710 | g_ptr_array_add (errors, g_steal_pointer (&error_local)); |
| 711 | continue; |
| 712 | } |
| 713 | |
Mario Limonciello | 7a3df4b | 2019-01-31 10:27:22 -0600 | [diff] [blame] | 714 | /* if component should have an update message from CAB */ |
| 715 | fu_device_incorporate_from_component (device, component); |
| 716 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 717 | /* get the action IDs for the valid device */ |
| 718 | action_id = fu_install_task_get_action_id (task); |
| 719 | if (!g_ptr_array_find (helper->action_ids, action_id, NULL)) |
| 720 | g_ptr_array_add (helper->action_ids, g_strdup (action_id)); |
| 721 | g_ptr_array_add (helper->install_tasks, g_steal_pointer (&task)); |
| 722 | } |
| 723 | } |
| 724 | |
Richard Hughes | 9f86ade | 2018-05-10 21:11:22 +0100 | [diff] [blame] | 725 | /* order the install tasks by the device priority */ |
| 726 | g_ptr_array_sort (helper->install_tasks, fu_main_install_task_sort_cb); |
| 727 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 728 | /* nothing suitable */ |
| 729 | if (helper->install_tasks->len == 0) { |
Richard Hughes | e82eef3 | 2018-05-20 10:41:26 +0100 | [diff] [blame] | 730 | GError *error_tmp = fu_common_error_array_get_best (errors); |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 731 | g_propagate_error (error, error_tmp); |
| 732 | return FALSE; |
| 733 | } |
| 734 | |
| 735 | /* authenticate all things in the action_ids */ |
| 736 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 737 | fu_main_authorize_install_queue (g_steal_pointer (&helper)); |
| 738 | return TRUE; |
| 739 | } |
| 740 | |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 741 | static gboolean |
| 742 | fu_main_device_id_valid (const gchar *device_id, GError **error) |
| 743 | { |
| 744 | if (g_strcmp0 (device_id, FWUPD_DEVICE_ID_ANY) == 0) |
| 745 | return TRUE; |
| 746 | if (device_id != NULL && strlen (device_id) >= 4) |
| 747 | return TRUE; |
| 748 | g_set_error (error, |
| 749 | FWUPD_ERROR, |
| 750 | FWUPD_ERROR_INTERNAL, |
| 751 | "invalid device ID: %s", |
| 752 | device_id); |
| 753 | return FALSE; |
| 754 | } |
| 755 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 756 | static void |
| 757 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 758 | const gchar *object_path, const gchar *interface_name, |
| 759 | const gchar *method_name, GVariant *parameters, |
| 760 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 761 | { |
| 762 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 763 | GVariant *val = NULL; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 764 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 765 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 766 | /* activity */ |
| 767 | fu_engine_idle_reset (priv->engine); |
| 768 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 769 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 770 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 771 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 772 | devices = fu_engine_get_devices (priv->engine, &error); |
| 773 | if (devices == NULL) { |
| 774 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 775 | return; |
| 776 | } |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 777 | val = fu_main_device_array_to_variant (priv, sender, devices, &error); |
| 778 | if (val == NULL) { |
| 779 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 780 | return; |
| 781 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 782 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 783 | return; |
| 784 | } |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 785 | if (g_strcmp0 (method_name, "GetReleases") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 786 | const gchar *device_id; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 787 | g_autoptr(GPtrArray) releases = NULL; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 788 | g_variant_get (parameters, "(&s)", &device_id); |
| 789 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 790 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 791 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 792 | return; |
| 793 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 794 | releases = fu_engine_get_releases (priv->engine, device_id, &error); |
| 795 | if (releases == NULL) { |
| 796 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 797 | return; |
| 798 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 799 | val = fu_main_release_array_to_variant (releases); |
| 800 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 801 | return; |
| 802 | } |
Richard Hughes | 8dd4c1c | 2019-03-03 18:27:57 +0000 | [diff] [blame] | 803 | if (g_strcmp0 (method_name, "GetApprovedFirmware") == 0) { |
| 804 | GVariantBuilder builder; |
| 805 | GPtrArray *checksums = fu_engine_get_approved_firmware (priv->engine); |
| 806 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); |
| 807 | for (guint i = 0; i < checksums->len; i++) { |
| 808 | const gchar *checksum = g_ptr_array_index (checksums, i); |
| 809 | g_variant_builder_add_value (&builder, g_variant_new_string (checksum)); |
| 810 | } |
| 811 | val = g_variant_builder_end (&builder); |
| 812 | g_dbus_method_invocation_return_value (invocation, |
| 813 | g_variant_new_tuple (&val, 1)); |
| 814 | return; |
| 815 | } |
| 816 | if (g_strcmp0 (method_name, "SetApprovedFirmware") == 0) { |
| 817 | g_autofree gchar *checksums_str = NULL; |
| 818 | g_auto(GStrv) checksums = NULL; |
| 819 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 820 | g_autoptr(PolkitSubject) subject = NULL; |
| 821 | |
| 822 | g_variant_get (parameters, "(^as)", &checksums); |
| 823 | checksums_str = g_strjoinv (",", checksums); |
| 824 | g_debug ("Called %s(%s)", method_name, checksums_str); |
| 825 | |
| 826 | /* authenticate */ |
| 827 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 828 | helper = g_new0 (FuMainAuthHelper, 1); |
| 829 | helper->priv = priv; |
| 830 | helper->invocation = g_object_ref (invocation); |
| 831 | helper->checksums = g_ptr_array_new_with_free_func (g_free); |
| 832 | for (guint i = 0; checksums[i] != NULL; i++) |
| 833 | g_ptr_array_add (helper->checksums, g_strdup (checksums[i])); |
| 834 | subject = polkit_system_bus_name_new (sender); |
| 835 | polkit_authority_check_authorization (priv->authority, subject, |
| 836 | "org.freedesktop.fwupd.set-approved-firmware", |
| 837 | NULL, |
| 838 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 839 | NULL, |
| 840 | fu_main_authorize_set_approved_firmware_cb, |
| 841 | g_steal_pointer (&helper)); |
| 842 | return; |
| 843 | } |
Richard Hughes | 3d60762 | 2019-03-07 16:59:27 +0000 | [diff] [blame] | 844 | if (g_strcmp0 (method_name, "SelfSign") == 0) { |
| 845 | GVariant *prop_value; |
| 846 | gchar *prop_key; |
| 847 | g_autofree gchar *value = NULL; |
| 848 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 849 | g_autoptr(PolkitSubject) subject = NULL; |
| 850 | g_autoptr(GVariantIter) iter = NULL; |
| 851 | |
| 852 | g_variant_get (parameters, "(sa{sv})", &value, &iter); |
| 853 | g_debug ("Called %s(%s)", method_name, value); |
| 854 | |
| 855 | /* get flags */ |
| 856 | helper = g_new0 (FuMainAuthHelper, 1); |
| 857 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
| 858 | g_debug ("got option %s", prop_key); |
| 859 | if (g_strcmp0 (prop_key, "add-timestamp") == 0 && |
| 860 | g_variant_get_boolean (prop_value) == TRUE) |
| 861 | helper->flags |= FU_KEYRING_SIGN_FLAG_ADD_TIMESTAMP; |
| 862 | if (g_strcmp0 (prop_key, "add-cert") == 0 && |
| 863 | g_variant_get_boolean (prop_value) == TRUE) |
| 864 | helper->flags |= FU_KEYRING_SIGN_FLAG_ADD_CERT; |
| 865 | g_variant_unref (prop_value); |
| 866 | } |
| 867 | |
| 868 | /* authenticate */ |
| 869 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 870 | helper->priv = priv; |
| 871 | helper->value = g_steal_pointer (&value); |
| 872 | helper->invocation = g_object_ref (invocation); |
| 873 | subject = polkit_system_bus_name_new (sender); |
| 874 | polkit_authority_check_authorization (priv->authority, subject, |
| 875 | "org.freedesktop.fwupd.self-sign", |
| 876 | NULL, |
| 877 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 878 | NULL, |
| 879 | fu_main_authorize_self_sign_cb, |
| 880 | g_steal_pointer (&helper)); |
| 881 | return; |
| 882 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 883 | if (g_strcmp0 (method_name, "GetDowngrades") == 0) { |
| 884 | const gchar *device_id; |
| 885 | g_autoptr(GPtrArray) releases = NULL; |
| 886 | g_variant_get (parameters, "(&s)", &device_id); |
| 887 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 888 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 889 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 890 | return; |
| 891 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 892 | releases = fu_engine_get_downgrades (priv->engine, device_id, &error); |
| 893 | if (releases == NULL) { |
| 894 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 895 | return; |
| 896 | } |
| 897 | val = fu_main_release_array_to_variant (releases); |
| 898 | g_dbus_method_invocation_return_value (invocation, val); |
| 899 | return; |
| 900 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 901 | if (g_strcmp0 (method_name, "GetUpgrades") == 0) { |
| 902 | const gchar *device_id; |
| 903 | g_autoptr(GPtrArray) releases = NULL; |
| 904 | g_variant_get (parameters, "(&s)", &device_id); |
| 905 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 906 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 907 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 908 | return; |
| 909 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 910 | releases = fu_engine_get_upgrades (priv->engine, device_id, &error); |
| 911 | if (releases == NULL) { |
| 912 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 913 | return; |
| 914 | } |
| 915 | val = fu_main_release_array_to_variant (releases); |
| 916 | g_dbus_method_invocation_return_value (invocation, val); |
| 917 | return; |
| 918 | } |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 919 | if (g_strcmp0 (method_name, "GetRemotes") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 920 | g_autoptr(GPtrArray) remotes = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 921 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 922 | remotes = fu_engine_get_remotes (priv->engine, &error); |
| 923 | if (remotes == NULL) { |
| 924 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 925 | return; |
| 926 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 927 | val = fu_main_remote_array_to_variant (remotes); |
| 928 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 929 | return; |
| 930 | } |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 931 | if (g_strcmp0 (method_name, "GetHistory") == 0) { |
| 932 | g_autoptr(GPtrArray) devices = NULL; |
| 933 | g_debug ("Called %s()", method_name); |
| 934 | devices = fu_engine_get_history (priv->engine, &error); |
| 935 | if (devices == NULL) { |
| 936 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 937 | return; |
| 938 | } |
Mario Limonciello | e301660 | 2018-09-06 11:20:59 -0500 | [diff] [blame] | 939 | val = fu_main_device_array_to_variant (priv, sender, devices, &error); |
| 940 | if (val == NULL) { |
| 941 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 942 | return; |
| 943 | } |
Richard Hughes | 476363a | 2018-01-11 10:08:58 +0000 | [diff] [blame] | 944 | g_dbus_method_invocation_return_value (invocation, val); |
| 945 | return; |
| 946 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 947 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 948 | const gchar *device_id; |
| 949 | g_variant_get (parameters, "(&s)", &device_id); |
| 950 | g_debug ("Called %s(%s)", method_name, device_id); |
| 951 | if (!fu_engine_clear_results (priv->engine, device_id, &error)) { |
| 952 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 953 | return; |
| 954 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 955 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
Richard Hughes | 6b22295 | 2018-01-11 10:20:48 +0000 | [diff] [blame] | 958 | if (g_strcmp0 (method_name, "ModifyDevice") == 0) { |
| 959 | const gchar *device_id; |
| 960 | const gchar *key = NULL; |
| 961 | const gchar *value = NULL; |
| 962 | |
| 963 | /* check the id exists */ |
| 964 | g_variant_get (parameters, "(&s&s&s)", &device_id, &key, &value); |
| 965 | g_debug ("Called %s(%s,%s=%s)", method_name, device_id, key, value); |
| 966 | if (!fu_engine_modify_device (priv->engine, device_id, key, value, &error)) { |
| 967 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 968 | return; |
| 969 | } |
| 970 | g_dbus_method_invocation_return_value (invocation, NULL); |
| 971 | return; |
| 972 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 973 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 974 | const gchar *device_id = NULL; |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame] | 975 | g_autoptr(FwupdDevice) result = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 976 | g_variant_get (parameters, "(&s)", &device_id); |
| 977 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 978 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 979 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 980 | return; |
| 981 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 982 | result = fu_engine_get_results (priv->engine, device_id, &error); |
| 983 | if (result == NULL) { |
| 984 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 985 | return; |
| 986 | } |
Richard Hughes | e0bd53e | 2017-09-17 08:29:02 +0100 | [diff] [blame] | 987 | val = fwupd_device_to_variant (result); |
| 988 | g_dbus_method_invocation_return_value (invocation, |
| 989 | g_variant_new_tuple (&val, 1)); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 990 | return; |
| 991 | } |
Richard Hughes | ba73c76 | 2017-09-15 14:31:17 +0100 | [diff] [blame] | 992 | if (g_strcmp0 (method_name, "UpdateMetadata") == 0) { |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 993 | GDBusMessage *message; |
| 994 | GUnixFDList *fd_list; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 995 | const gchar *remote_id = NULL; |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 996 | gint fd_data; |
| 997 | gint fd_sig; |
| 998 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 999 | g_variant_get (parameters, "(&shh)", &remote_id, &fd_data, &fd_sig); |
| 1000 | 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] | 1001 | |
Richard Hughes | 5935ebd | 2017-06-16 15:40:31 +0100 | [diff] [blame] | 1002 | /* update the metadata store */ |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1003 | message = g_dbus_method_invocation_get_message (invocation); |
| 1004 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1005 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 2) { |
| 1006 | g_set_error (&error, |
| 1007 | FWUPD_ERROR, |
| 1008 | FWUPD_ERROR_INTERNAL, |
| 1009 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1010 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1011 | return; |
| 1012 | } |
| 1013 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 1014 | if (fd_data < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1015 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1016 | return; |
| 1017 | } |
| 1018 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 1019 | if (fd_sig < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1020 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 1021 | return; |
| 1022 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1023 | |
| 1024 | /* store new metadata (will close the fds when done) */ |
| 1025 | if (!fu_engine_update_metadata (priv->engine, remote_id, |
| 1026 | fd_data, fd_sig, &error)) { |
Richard Hughes | f3d46c6 | 2017-11-28 14:01:30 +0000 | [diff] [blame] | 1027 | g_prefix_error (&error, "Failed to update metadata for %s: ", remote_id); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1028 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1029 | return; |
| 1030 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1031 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 1032 | return; |
| 1033 | } |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1034 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1035 | const gchar *device_id = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1036 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1037 | g_autoptr(PolkitSubject) subject = NULL; |
| 1038 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1039 | g_variant_get (parameters, "(&s)", &device_id); |
| 1040 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1041 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1042 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1043 | return; |
| 1044 | } |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 1045 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1046 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 1047 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1048 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1049 | helper->priv = priv; |
| 1050 | helper->invocation = g_object_ref (invocation); |
| 1051 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1052 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1053 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1054 | "org.freedesktop.fwupd.device-unlock", |
| 1055 | NULL, |
| 1056 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1057 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1058 | fu_main_authorize_unlock_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1059 | g_steal_pointer (&helper)); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 1060 | return; |
| 1061 | } |
Mario Limonciello | 96a0dd5 | 2019-02-25 13:50:03 -0600 | [diff] [blame] | 1062 | if (g_strcmp0 (method_name, "Activate") == 0) { |
| 1063 | const gchar *device_id = NULL; |
| 1064 | g_autoptr(FuMainAuthHelper) helper = NULL; |
| 1065 | g_autoptr(PolkitSubject) subject = NULL; |
| 1066 | |
| 1067 | g_variant_get (parameters, "(&s)", &device_id); |
| 1068 | g_debug ("Called %s(%s)", method_name, device_id); |
| 1069 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1070 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | /* authenticate */ |
| 1075 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
| 1076 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1077 | helper->priv = priv; |
| 1078 | helper->invocation = g_object_ref (invocation); |
| 1079 | helper->device_id = g_strdup (device_id); |
| 1080 | subject = polkit_system_bus_name_new (sender); |
| 1081 | polkit_authority_check_authorization (priv->authority, subject, |
| 1082 | "org.freedesktop.fwupd.device-activate", |
| 1083 | NULL, |
| 1084 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1085 | NULL, |
| 1086 | fu_main_authorize_activate_cb, |
| 1087 | g_steal_pointer (&helper)); |
| 1088 | return; |
| 1089 | } |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1090 | if (g_strcmp0 (method_name, "ModifyRemote") == 0) { |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1091 | const gchar *remote_id = NULL; |
| 1092 | const gchar *key = NULL; |
| 1093 | const gchar *value = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1094 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1095 | g_autoptr(PolkitSubject) subject = NULL; |
| 1096 | |
| 1097 | /* check the id exists */ |
| 1098 | g_variant_get (parameters, "(&s&s&s)", &remote_id, &key, &value); |
| 1099 | g_debug ("Called %s(%s,%s=%s)", method_name, remote_id, key, value); |
| 1100 | |
| 1101 | /* create helper object */ |
| 1102 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1103 | helper->invocation = g_object_ref (invocation); |
| 1104 | helper->remote_id = g_strdup (remote_id); |
| 1105 | helper->key = g_strdup (key); |
| 1106 | helper->value = g_strdup (value); |
| 1107 | helper->priv = priv; |
| 1108 | |
| 1109 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 1110 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1111 | subject = polkit_system_bus_name_new (sender); |
Mario Limonciello | 6b9f07c | 2018-04-16 13:52:09 -0500 | [diff] [blame] | 1112 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1113 | "org.freedesktop.fwupd.modify-remote", |
| 1114 | NULL, |
| 1115 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1116 | NULL, |
| 1117 | fu_main_authorize_modify_remote_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1118 | g_steal_pointer (&helper)); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 1119 | return; |
| 1120 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1121 | if (g_strcmp0 (method_name, "VerifyUpdate") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1122 | const gchar *device_id = NULL; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1123 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1124 | g_autoptr(PolkitSubject) subject = NULL; |
| 1125 | |
| 1126 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1127 | g_variant_get (parameters, "(&s)", &device_id); |
| 1128 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1129 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1130 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1131 | return; |
| 1132 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1133 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1134 | /* create helper object */ |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1135 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1136 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1137 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1138 | helper->priv = priv; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* authenticate */ |
Richard Hughes | 87f8a4a | 2017-10-02 09:42:06 +0100 | [diff] [blame] | 1141 | fu_main_set_status (priv, FWUPD_STATUS_WAITING_FOR_AUTH); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1142 | subject = polkit_system_bus_name_new (sender); |
Mario Limonciello | 6b9f07c | 2018-04-16 13:52:09 -0500 | [diff] [blame] | 1143 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1144 | "org.freedesktop.fwupd.verify-update", |
| 1145 | NULL, |
| 1146 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 1147 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1148 | fu_main_authorize_verify_update_cb, |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1149 | g_steal_pointer (&helper)); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 1150 | return; |
| 1151 | } |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1152 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1153 | const gchar *device_id = NULL; |
| 1154 | g_variant_get (parameters, "(&s)", &device_id); |
| 1155 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1156 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1157 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1158 | return; |
| 1159 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1160 | if (!fu_engine_verify (priv->engine, device_id, &error)) { |
| 1161 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1162 | return; |
| 1163 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1164 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 1165 | return; |
| 1166 | } |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1167 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1168 | GVariant *prop_value; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1169 | const gchar *device_id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1170 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1171 | gint32 fd_handle = 0; |
| 1172 | gint fd; |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 1173 | guint64 archive_size_max; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1174 | GDBusMessage *message; |
| 1175 | GUnixFDList *fd_list; |
Richard Hughes | 747b9ee | 2018-04-16 16:46:58 +0100 | [diff] [blame] | 1176 | g_autoptr(FuMainAuthHelper) helper = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1177 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1180 | g_variant_get (parameters, "(&sha{sv})", &device_id, &fd_handle, &iter); |
| 1181 | g_debug ("Called %s(%s,%i)", method_name, device_id, fd_handle); |
Richard Hughes | b6f7955 | 2017-11-11 07:58:17 +0000 | [diff] [blame] | 1182 | if (!fu_main_device_id_valid (device_id, &error)) { |
| 1183 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 1184 | return; |
| 1185 | } |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1186 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1187 | /* create helper object */ |
| 1188 | helper = g_new0 (FuMainAuthHelper, 1); |
| 1189 | helper->invocation = g_object_ref (invocation); |
| 1190 | helper->device_id = g_strdup (device_id); |
| 1191 | helper->priv = priv; |
| 1192 | |
| 1193 | /* get flags */ |
| 1194 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1195 | g_debug ("got option %s", prop_key); |
| 1196 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 1197 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1198 | helper->flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1199 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 1200 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1201 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 1202 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 1203 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1204 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 1205 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 1206 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1207 | helper->flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 76e0f94 | 2018-05-14 16:24:00 +0100 | [diff] [blame] | 1208 | if (g_strcmp0 (prop_key, "no-history") == 0 && |
| 1209 | g_variant_get_boolean (prop_value) == TRUE) |
| 1210 | helper->flags |= FWUPD_INSTALL_FLAG_NO_HISTORY; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 1211 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1214 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1215 | /* get the fd */ |
| 1216 | message = g_dbus_method_invocation_get_message (invocation); |
| 1217 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1218 | 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] | 1219 | g_set_error (&error, |
| 1220 | FWUPD_ERROR, |
| 1221 | FWUPD_ERROR_INTERNAL, |
| 1222 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1223 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1224 | return; |
| 1225 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 1226 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1227 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1228 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1229 | return; |
| 1230 | } |
| 1231 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1232 | /* parse the cab file before authenticating so we can work out |
| 1233 | * what action ID to use, for instance, if this is trusted -- |
| 1234 | * this will also close the fd when done */ |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 1235 | archive_size_max = fu_engine_get_archive_size_max (priv->engine); |
| 1236 | 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] | 1237 | if (helper->blob_cab == NULL) { |
| 1238 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 1239 | return; |
| 1240 | } |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 1241 | |
| 1242 | /* install all the things in the store */ |
| 1243 | helper->subject = polkit_system_bus_name_new (sender); |
| 1244 | if (!fu_main_install_with_helper (g_steal_pointer (&helper), &error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1245 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1246 | return; |
| 1247 | } |
| 1248 | |
Richard Hughes | 4ad41f0 | 2018-05-08 14:35:36 +0100 | [diff] [blame] | 1249 | /* async return */ |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1250 | return; |
| 1251 | } |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 1252 | if (g_strcmp0 (method_name, "GetDetails") == 0) { |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1253 | GDBusMessage *message; |
| 1254 | GUnixFDList *fd_list; |
| 1255 | gint32 fd_handle = 0; |
| 1256 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1257 | g_autoptr(GPtrArray) results = NULL; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1258 | |
| 1259 | /* get parameters */ |
| 1260 | g_variant_get (parameters, "(h)", &fd_handle); |
| 1261 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 1262 | |
| 1263 | /* get the fd */ |
| 1264 | message = g_dbus_method_invocation_get_message (invocation); |
| 1265 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 1266 | 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] | 1267 | g_set_error (&error, |
| 1268 | FWUPD_ERROR, |
| 1269 | FWUPD_ERROR_INTERNAL, |
| 1270 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1271 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1272 | return; |
| 1273 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 1274 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1275 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1276 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1277 | return; |
| 1278 | } |
| 1279 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1280 | /* get details about the file (will close the fd when done) */ |
Richard Hughes | 07f963a | 2017-09-15 14:28:47 +0100 | [diff] [blame] | 1281 | results = fu_engine_get_details (priv->engine, fd, &error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1282 | if (results == NULL) { |
| 1283 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1284 | return; |
| 1285 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1286 | val = fu_main_result_array_to_variant (results); |
| 1287 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 1288 | return; |
| 1289 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 1290 | g_set_error (&error, |
| 1291 | G_DBUS_ERROR, |
| 1292 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 1293 | "no such method %s", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1294 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1297 | static GVariant * |
| 1298 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 1299 | const gchar *object_path, const gchar *interface_name, |
| 1300 | const gchar *property_name, GError **error, |
| 1301 | gpointer user_data) |
| 1302 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1303 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1304 | |
Richard Hughes | 75b965d | 2018-11-15 13:51:21 +0000 | [diff] [blame] | 1305 | /* activity */ |
| 1306 | fu_engine_idle_reset (priv->engine); |
| 1307 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1308 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
| 1309 | return g_variant_new_string (VERSION); |
| 1310 | |
Richard Hughes | f425d29 | 2019-01-18 17:57:39 +0000 | [diff] [blame] | 1311 | if (g_strcmp0 (property_name, "Tainted") == 0) |
| 1312 | return g_variant_new_boolean (fu_engine_get_tainted (priv->engine)); |
| 1313 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1314 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1315 | return g_variant_new_uint32 (fu_engine_get_status (priv->engine)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 1316 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1317 | /* return an error */ |
| 1318 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 1319 | G_DBUS_ERROR, |
| 1320 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1321 | "failed to get daemon property %s", |
| 1322 | property_name); |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | |
Richard Hughes | f517c9a | 2019-03-22 19:50:35 +0000 | [diff] [blame] | 1326 | static gboolean |
| 1327 | fu_main_is_running_offline_update (FuMainPrivate *priv) |
| 1328 | { |
| 1329 | const gchar *default_target = NULL; |
| 1330 | g_autoptr(GError) error = NULL; |
| 1331 | g_autoptr(GVariant) val = NULL; |
| 1332 | |
| 1333 | val = g_dbus_connection_call_sync (priv->connection, |
| 1334 | "org.freedesktop.systemd1", |
| 1335 | "/org/freedesktop/systemd1", |
| 1336 | "org.freedesktop.systemd1.Manager", |
| 1337 | "GetDefaultTarget", |
| 1338 | NULL, NULL, |
| 1339 | G_DBUS_CALL_FLAGS_NONE, |
| 1340 | 1500, NULL, &error); |
| 1341 | if (val == NULL) { |
| 1342 | g_warning ("failed to get default.target: %s", error->message); |
| 1343 | return FALSE; |
| 1344 | } |
| 1345 | g_variant_get (val, "(&s)", &default_target); |
| 1346 | return g_strcmp0 (default_target, "system-update.target") == 0; |
| 1347 | } |
| 1348 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 1349 | static void |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1350 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 1351 | const gchar *name, |
| 1352 | gpointer user_data) |
| 1353 | { |
| 1354 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1355 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1356 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1357 | static const GDBusInterfaceVTable interface_vtable = { |
| 1358 | fu_main_daemon_method_call, |
| 1359 | fu_main_daemon_get_property, |
| 1360 | NULL |
| 1361 | }; |
| 1362 | |
| 1363 | priv->connection = g_object_ref (connection); |
| 1364 | registration_id = g_dbus_connection_register_object (connection, |
| 1365 | FWUPD_DBUS_PATH, |
| 1366 | priv->introspection_daemon->interfaces[0], |
| 1367 | &interface_vtable, |
| 1368 | priv, /* user_data */ |
| 1369 | NULL, /* user_data_free_func */ |
| 1370 | NULL); /* GError** */ |
| 1371 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1372 | |
Richard Hughes | f517c9a | 2019-03-22 19:50:35 +0000 | [diff] [blame] | 1373 | /* are we running in the offline target */ |
| 1374 | if (fu_main_is_running_offline_update (priv)) |
Richard Hughes | 86923b9 | 2019-03-23 13:57:51 +0000 | [diff] [blame] | 1375 | fu_engine_add_app_flag (priv->engine, FU_APP_FLAGS_IS_OFFLINE); |
Richard Hughes | f517c9a | 2019-03-22 19:50:35 +0000 | [diff] [blame] | 1376 | |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 1377 | /* connect to D-Bus directly */ |
| 1378 | priv->proxy_uid = |
| 1379 | g_dbus_proxy_new_sync (priv->connection, |
| 1380 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 1381 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 1382 | NULL, |
| 1383 | "org.freedesktop.DBus", |
| 1384 | "/org/freedesktop/DBus", |
| 1385 | "org.freedesktop.DBus", |
| 1386 | NULL, |
| 1387 | &error); |
| 1388 | if (priv->proxy_uid == NULL) { |
| 1389 | g_warning ("cannot connect to DBus: %s", error->message); |
| 1390 | return; |
| 1391 | } |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1394 | static void |
| 1395 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 1396 | const gchar *name, |
| 1397 | gpointer user_data) |
| 1398 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1399 | g_debug ("FuMain: acquired name: %s", name); |
| 1400 | } |
| 1401 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1402 | static void |
| 1403 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 1404 | const gchar *name, |
| 1405 | gpointer user_data) |
| 1406 | { |
| 1407 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1408 | g_debug ("FuMain: lost name: %s", name); |
| 1409 | g_main_loop_quit (priv->loop); |
| 1410 | } |
| 1411 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1412 | static gboolean |
| 1413 | fu_main_timed_exit_cb (gpointer user_data) |
| 1414 | { |
| 1415 | GMainLoop *loop = (GMainLoop *) user_data; |
| 1416 | g_main_loop_quit (loop); |
| 1417 | return G_SOURCE_REMOVE; |
| 1418 | } |
| 1419 | |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame^] | 1420 | static void |
| 1421 | fu_main_argv_changed_cb (GFileMonitor *monitor, GFile *file, GFile *other_file, |
| 1422 | GFileMonitorEvent event_type, gpointer user_data) |
| 1423 | { |
| 1424 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 1425 | |
| 1426 | /* can do straight away? */ |
| 1427 | if (priv->update_in_progress) { |
| 1428 | g_warning ("binary changed during a firmware update, ignoring"); |
| 1429 | return; |
| 1430 | } |
| 1431 | g_debug ("binary changed, shutting down"); |
| 1432 | g_main_loop_quit (priv->loop); |
| 1433 | } |
| 1434 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1435 | static GDBusNodeInfo * |
| 1436 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 1437 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1438 | g_autoptr(GBytes) data = NULL; |
| 1439 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1440 | |
| 1441 | /* lookup data */ |
| 1442 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 1443 | data = g_resource_lookup_data (fu_get_resource (), |
| 1444 | path, |
| 1445 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 1446 | error); |
| 1447 | if (data == NULL) |
| 1448 | return NULL; |
| 1449 | |
| 1450 | /* build introspection from XML */ |
| 1451 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 1452 | } |
| 1453 | |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1454 | static void |
| 1455 | fu_main_private_free (FuMainPrivate *priv) |
| 1456 | { |
| 1457 | if (priv->loop != NULL) |
| 1458 | g_main_loop_unref (priv->loop); |
| 1459 | if (priv->owner_id > 0) |
| 1460 | g_bus_unown_name (priv->owner_id); |
| 1461 | if (priv->proxy_uid != NULL) |
| 1462 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1463 | if (priv->engine != NULL) |
| 1464 | g_object_unref (priv->engine); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1465 | if (priv->connection != NULL) |
| 1466 | g_object_unref (priv->connection); |
| 1467 | if (priv->authority != NULL) |
| 1468 | g_object_unref (priv->authority); |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame^] | 1469 | if (priv->argv0_monitor != NULL) |
| 1470 | g_object_unref (priv->argv0_monitor); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1471 | if (priv->introspection_daemon != NULL) |
| 1472 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1473 | g_free (priv); |
| 1474 | } |
| 1475 | |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 1476 | #pragma clang diagnostic push |
| 1477 | #pragma clang diagnostic ignored "-Wunused-function" |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1478 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainPrivate, fu_main_private_free) |
Mario Limonciello | a98df55 | 2018-04-16 12:15:51 -0500 | [diff] [blame] | 1479 | #pragma clang diagnostic pop |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1480 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1481 | int |
| 1482 | main (int argc, char *argv[]) |
| 1483 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1484 | gboolean immediate_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1485 | gboolean timed_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1486 | const GOptionEntry options[] = { |
| 1487 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 1488 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 1489 | _("Exit after a small delay"), NULL }, |
| 1490 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 1491 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 1492 | _("Exit after the engine has loaded"), NULL }, |
| 1493 | { NULL} |
| 1494 | }; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1495 | g_autoptr(FuMainPrivate) priv = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 1496 | g_autoptr(GError) error = NULL; |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame^] | 1497 | g_autoptr(GFile) argv0_file = g_file_new_for_path (argv[0]); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1498 | g_autoptr(GOptionContext) context = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1499 | |
| 1500 | setlocale (LC_ALL, ""); |
| 1501 | |
| 1502 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 1503 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 1504 | textdomain (GETTEXT_PACKAGE); |
| 1505 | |
| 1506 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 1507 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1508 | context = g_option_context_new (NULL); |
| 1509 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1510 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 1511 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1512 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1513 | if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 1514 | g_printerr ("Failed to parse command line: %s\n", error->message); |
| 1515 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* create new objects */ |
| 1519 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1520 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 1521 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1522 | /* load engine */ |
Richard Hughes | 5b5f655 | 2018-05-18 10:22:39 +0100 | [diff] [blame] | 1523 | priv->engine = fu_engine_new (FU_APP_FLAGS_NONE); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1524 | g_signal_connect (priv->engine, "changed", |
| 1525 | G_CALLBACK (fu_main_engine_changed_cb), |
| 1526 | priv); |
| 1527 | g_signal_connect (priv->engine, "device-added", |
| 1528 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 1529 | priv); |
| 1530 | g_signal_connect (priv->engine, "device-removed", |
| 1531 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1532 | priv); |
| 1533 | g_signal_connect (priv->engine, "device-changed", |
| 1534 | G_CALLBACK (fu_main_engine_device_changed_cb), |
| 1535 | priv); |
| 1536 | g_signal_connect (priv->engine, "status-changed", |
| 1537 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1538 | priv); |
| 1539 | g_signal_connect (priv->engine, "percentage-changed", |
| 1540 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1541 | priv); |
Richard Hughes | c8cc77c | 2019-03-07 11:57:24 +0000 | [diff] [blame] | 1542 | if (!fu_engine_load (priv->engine, FU_ENGINE_LOAD_FLAG_NONE, &error)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1543 | g_printerr ("Failed to load engine: %s\n", error->message); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1544 | return EXIT_FAILURE; |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Mario Limonciello | 6754f5a | 2018-10-11 10:50:03 -0500 | [diff] [blame] | 1547 | g_unix_signal_add_full (G_PRIORITY_DEFAULT, |
| 1548 | SIGTERM, fu_main_sigterm_cb, |
| 1549 | priv, NULL); |
| 1550 | |
Richard Hughes | f3dc162 | 2019-03-27 12:48:39 +0000 | [diff] [blame^] | 1551 | /* restart the daemon if the binary gets replaced */ |
| 1552 | priv->argv0_monitor = g_file_monitor_file (argv0_file, G_FILE_MONITOR_NONE, |
| 1553 | NULL, &error); |
| 1554 | g_signal_connect (priv->argv0_monitor, "changed", |
| 1555 | G_CALLBACK (fu_main_argv_changed_cb), priv); |
| 1556 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1557 | /* load introspection from file */ |
| 1558 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 1559 | &error); |
| 1560 | if (priv->introspection_daemon == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1561 | g_printerr ("Failed to load introspection: %s\n", error->message); |
| 1562 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1565 | /* get authority */ |
| 1566 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 1567 | if (priv->authority == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1568 | g_printerr ("Failed to load authority: %s\n", error->message); |
| 1569 | return EXIT_FAILURE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Richard Hughes | ebae396 | 2018-09-09 13:40:49 +0100 | [diff] [blame] | 1572 | /* own the object */ |
| 1573 | priv->owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 1574 | FWUPD_DBUS_SERVICE, |
| 1575 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 1576 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 1577 | fu_main_on_bus_acquired_cb, |
| 1578 | fu_main_on_name_acquired_cb, |
| 1579 | fu_main_on_name_lost_cb, |
| 1580 | priv, NULL); |
| 1581 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1582 | /* Only timeout and close the mainloop if we have specified it |
| 1583 | * on the command line */ |
| 1584 | if (immediate_exit) |
| 1585 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 1586 | else if (timed_exit) |
| 1587 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 1588 | |
Mario Limonciello | 46bb4e9 | 2019-01-07 09:44:31 -0600 | [diff] [blame] | 1589 | g_debug ("Started with locale %s", g_getenv ("LANG")); |
| 1590 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1591 | /* wait */ |
Richard Hughes | 4619f9f | 2017-06-14 13:55:30 +0100 | [diff] [blame] | 1592 | g_message ("Daemon ready for requests"); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1593 | g_main_loop_run (priv->loop); |
| 1594 | |
| 1595 | /* success */ |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1596 | return EXIT_SUCCESS; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1597 | } |