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