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