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