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