Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 3 | * Copyright (C) 2015-2017 Richard Hughes <richard@hughsie.com> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the GNU General Public License Version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 24 | #include <appstream-glib.h> |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 25 | #include <fwupd.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 26 | #include <gio/gunixfdlist.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <locale.h> |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 29 | #include <polkit/polkit.h> |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 31 | |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 32 | #include "fwupd-device-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 | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 35 | #include "fwupd-result-private.h" |
Richard Hughes | d6db6b4 | 2017-04-12 15:03:10 +0100 | [diff] [blame] | 36 | #include "fwupd-resources.h" |
Richard Hughes | 8e9762d | 2016-03-17 10:14:15 +0000 | [diff] [blame] | 37 | |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 38 | #include "fu-common.h" |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 39 | #include "fu-debug.h" |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 40 | #include "fu-device-private.h" |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 41 | #include "fu-engine.h" |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 42 | |
Philip Withnall | bc339aa | 2016-11-22 16:13:22 +0000 | [diff] [blame] | 43 | #ifndef HAVE_POLKIT_0_114 |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 44 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitAuthorizationResult, g_object_unref) |
| 45 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(PolkitSubject, g_object_unref) |
| 46 | #endif |
| 47 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 48 | typedef struct { |
| 49 | GDBusConnection *connection; |
| 50 | GDBusNodeInfo *introspection_daemon; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 51 | GDBusProxy *proxy_uid; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 52 | GMainLoop *loop; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 53 | PolkitAuthority *authority; |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 54 | guint owner_id; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 55 | FuEngine *engine; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 56 | } FuMainPrivate; |
| 57 | |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 58 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 59 | fu_main_engine_changed_cb (FuEngine *engine, FuMainPrivate *priv) |
Richard Hughes | d7022b5 | 2015-03-11 19:47:06 +0000 | [diff] [blame] | 60 | { |
| 61 | /* not yet connected */ |
| 62 | if (priv->connection == NULL) |
| 63 | return; |
| 64 | g_dbus_connection_emit_signal (priv->connection, |
| 65 | NULL, |
| 66 | FWUPD_DBUS_PATH, |
| 67 | FWUPD_DBUS_INTERFACE, |
| 68 | "Changed", |
| 69 | NULL, NULL); |
| 70 | } |
| 71 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 72 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 73 | fu_main_engine_device_added_cb (FuEngine *engine, |
| 74 | FuDevice *device, |
| 75 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 76 | { |
| 77 | GVariant *val; |
| 78 | |
| 79 | /* not yet connected */ |
| 80 | if (priv->connection == NULL) |
| 81 | return; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 82 | val = fwupd_device_to_data (FWUPD_DEVICE (device), "(a{sv})"); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 83 | g_dbus_connection_emit_signal (priv->connection, |
| 84 | NULL, |
| 85 | FWUPD_DBUS_PATH, |
| 86 | FWUPD_DBUS_INTERFACE, |
| 87 | "DeviceAdded", |
| 88 | val, NULL); |
| 89 | } |
| 90 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 91 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 92 | fu_main_engine_device_removed_cb (FuEngine *engine, |
| 93 | FuDevice *device, |
| 94 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 95 | { |
| 96 | GVariant *val; |
| 97 | |
| 98 | /* not yet connected */ |
| 99 | if (priv->connection == NULL) |
| 100 | return; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 101 | val = fwupd_device_to_data (FWUPD_DEVICE (device), "(a{sv})"); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 102 | g_dbus_connection_emit_signal (priv->connection, |
| 103 | NULL, |
| 104 | FWUPD_DBUS_PATH, |
| 105 | FWUPD_DBUS_INTERFACE, |
| 106 | "DeviceRemoved", |
| 107 | val, NULL); |
| 108 | } |
| 109 | |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 110 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 111 | fu_main_engine_device_changed_cb (FuEngine *engine, |
| 112 | FuDevice *device, |
| 113 | FuMainPrivate *priv) |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 114 | { |
| 115 | GVariant *val; |
| 116 | |
| 117 | /* not yet connected */ |
| 118 | if (priv->connection == NULL) |
| 119 | return; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 120 | val = fwupd_device_to_data (FWUPD_DEVICE (device), "(a{sv})"); |
Richard Hughes | 8ca3378 | 2016-04-28 15:04:31 +0100 | [diff] [blame] | 121 | g_dbus_connection_emit_signal (priv->connection, |
| 122 | NULL, |
| 123 | FWUPD_DBUS_PATH, |
| 124 | FWUPD_DBUS_INTERFACE, |
| 125 | "DeviceChanged", |
| 126 | val, NULL); |
| 127 | } |
| 128 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 129 | static void |
| 130 | fu_main_emit_property_changed (FuMainPrivate *priv, |
| 131 | const gchar *property_name, |
| 132 | GVariant *property_value) |
| 133 | { |
| 134 | GVariantBuilder builder; |
| 135 | GVariantBuilder invalidated_builder; |
| 136 | |
| 137 | /* not yet connected */ |
| 138 | if (priv->connection == NULL) |
| 139 | return; |
| 140 | |
| 141 | /* build the dict */ |
| 142 | g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as")); |
| 143 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 144 | g_variant_builder_add (&builder, |
| 145 | "{sv}", |
| 146 | property_name, |
| 147 | property_value); |
| 148 | g_dbus_connection_emit_signal (priv->connection, |
| 149 | NULL, |
| 150 | FWUPD_DBUS_PATH, |
| 151 | "org.freedesktop.DBus.Properties", |
| 152 | "PropertiesChanged", |
| 153 | g_variant_new ("(sa{sv}as)", |
| 154 | FWUPD_DBUS_INTERFACE, |
| 155 | &builder, |
| 156 | &invalidated_builder), |
| 157 | NULL); |
| 158 | g_variant_builder_clear (&builder); |
| 159 | g_variant_builder_clear (&invalidated_builder); |
| 160 | } |
| 161 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 162 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 163 | static void |
| 164 | fu_main_engine_status_changed_cb (FuEngine *engine, |
| 165 | FwupdStatus status, |
| 166 | FuMainPrivate *priv) |
| 167 | { |
Richard Hughes | 8818151 | 2015-03-19 10:57:44 +0000 | [diff] [blame] | 168 | g_debug ("Emitting PropertyChanged('Status'='%s')", |
| 169 | fwupd_status_to_string (status)); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 170 | fu_main_emit_property_changed (priv, "Status", |
| 171 | g_variant_new_uint32 (status)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 174 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 175 | fu_main_engine_percentage_changed_cb (FuEngine *engine, |
| 176 | guint percentage, |
| 177 | FuMainPrivate *priv) |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 178 | { |
Richard Hughes | 876c007 | 2016-08-17 14:51:03 +0100 | [diff] [blame] | 179 | g_debug ("Emitting PropertyChanged('Percentage'='%u%%')", percentage); |
| 180 | fu_main_emit_property_changed (priv, "Percentage", |
| 181 | g_variant_new_uint32 (percentage)); |
| 182 | } |
| 183 | |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 184 | static GVariant * |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 185 | fu_main_device_array_to_variant (GPtrArray *devices) |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 186 | { |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 187 | GVariantBuilder builder; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 188 | g_return_val_if_fail (devices->len > 0, NULL); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 189 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 190 | for (guint i = 0; i < devices->len; i++) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 191 | FuDevice *device = g_ptr_array_index (devices, i); |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 192 | GVariant *tmp = fwupd_device_to_data (FWUPD_DEVICE (device), "{sa{sv}}"); |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 193 | g_variant_builder_add_value (&builder, tmp); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 194 | } |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 195 | return g_variant_new ("(a{sa{sv}})", &builder); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 198 | static GVariant * |
| 199 | fu_main_release_array_to_variant (GPtrArray *results) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 200 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 201 | GVariantBuilder builder; |
| 202 | g_return_val_if_fail (results->len > 0, NULL); |
| 203 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 204 | for (guint i = 0; i < results->len; i++) { |
| 205 | FwupdRelease *rel = g_ptr_array_index (results, i); |
| 206 | GVariant *tmp = fwupd_release_to_data (rel, "a{sv}"); |
| 207 | g_variant_builder_add_value (&builder, tmp); |
| 208 | } |
| 209 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 212 | static GVariant * |
| 213 | fu_main_remote_array_to_variant (GPtrArray *remotes) |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 214 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 215 | GVariantBuilder builder; |
| 216 | g_return_val_if_fail (remotes->len > 0, NULL); |
| 217 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 218 | for (guint i = 0; i < remotes->len; i++) { |
| 219 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
| 220 | GVariant *tmp = fwupd_remote_to_data (remote, "a{sv}"); |
| 221 | g_variant_builder_add_value (&builder, tmp); |
| 222 | } |
| 223 | return g_variant_new ("(aa{sv})", &builder); |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 226 | static GVariant * |
| 227 | fu_main_result_array_to_variant (GPtrArray *results) |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 228 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 229 | GVariantBuilder builder; |
| 230 | g_return_val_if_fail (results->len > 0, NULL); |
| 231 | g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); |
| 232 | for (guint i = 0; i < results->len; i++) { |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame^] | 233 | FwupdDevice *result = g_ptr_array_index (results, i); |
| 234 | GVariant *tmp = fwupd_device_to_data (result, "{sa{sv}}"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 235 | g_variant_builder_add_value (&builder, tmp); |
| 236 | } |
| 237 | return g_variant_new ("(a{sa{sv}})", &builder); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 240 | typedef struct { |
| 241 | GDBusMethodInvocation *invocation; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 242 | AsStore *store; |
Richard Hughes | 2d6e186 | 2016-03-18 09:20:37 +0000 | [diff] [blame] | 243 | FwupdInstallFlags flags; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 244 | GBytes *blob_cab; |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 245 | FuMainPrivate *priv; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 246 | gchar *device_id; |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 247 | gchar *remote_id; |
| 248 | gchar *key; |
| 249 | gchar *value; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 250 | } FuMainAuthHelper; |
| 251 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 252 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 253 | fu_main_auth_helper_free (FuMainAuthHelper *helper) |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 254 | { |
Richard Hughes | 4ced466 | 2016-08-26 11:02:31 +0100 | [diff] [blame] | 255 | if (helper->blob_cab != NULL) |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 256 | g_bytes_unref (helper->blob_cab); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 257 | if (helper->store != NULL) |
| 258 | g_object_unref (helper->store); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 259 | g_free (helper->device_id); |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 260 | g_free (helper->remote_id); |
| 261 | g_free (helper->key); |
| 262 | g_free (helper->value); |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 263 | g_object_unref (helper->invocation); |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 264 | g_free (helper); |
| 265 | } |
| 266 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 267 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainAuthHelper, fu_main_auth_helper_free) |
| 268 | |
| 269 | /* error may or may not already have been set */ |
Richard Hughes | b75c92d | 2016-02-20 20:22:00 +0000 | [diff] [blame] | 270 | static gboolean |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 271 | fu_main_authorization_is_valid (PolkitAuthorizationResult *auth, GError **error) |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 272 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 273 | /* failed */ |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 274 | if (auth == NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 275 | g_autofree gchar *message = g_strdup ((*error)->message); |
| 276 | g_clear_error (error); |
| 277 | g_set_error (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 278 | FWUPD_ERROR, |
| 279 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 280 | "Could not check for auth: %s", message); |
| 281 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* did not auth */ |
| 285 | if (!polkit_authorization_result_get_is_authorized (auth)) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 286 | g_set_error_literal (error, |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 287 | FWUPD_ERROR, |
| 288 | FWUPD_ERROR_AUTH_FAILED, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 289 | "Failed to obtain auth"); |
| 290 | return FALSE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 294 | return TRUE; |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 297 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 298 | fu_main_authorize_unlock_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 299 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 300 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 301 | g_autoptr(GError) error = NULL; |
| 302 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 303 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 304 | /* get result */ |
| 305 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 306 | res, &error); |
| 307 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 308 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 309 | return; |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 310 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 311 | |
| 312 | /* authenticated */ |
| 313 | if (!fu_engine_unlock (helper->priv->engine, helper->device_id, &error)) { |
| 314 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | /* success */ |
| 319 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 3d2fc1e | 2017-06-08 14:26:31 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 323 | 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] | 324 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 325 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 326 | g_autoptr(GError) error = NULL; |
| 327 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 328 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 329 | /* get result */ |
| 330 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 331 | res, &error); |
| 332 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 333 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 334 | return; |
Richard Hughes | f192bf0 | 2016-07-22 08:26:43 +0100 | [diff] [blame] | 335 | } |
Richard Hughes | df7950b | 2016-01-31 10:18:40 +0000 | [diff] [blame] | 336 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 337 | /* authenticated */ |
| 338 | if (!fu_engine_verify_update (helper->priv->engine, helper->device_id, &error)) { |
| 339 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 340 | return; |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 344 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 99147f1 | 2016-05-17 09:35:04 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 347 | static void |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 348 | fu_main_authorize_modify_remote_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
| 349 | { |
| 350 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
| 351 | g_autoptr(GError) error = NULL; |
| 352 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
| 353 | |
| 354 | /* get result */ |
| 355 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 356 | res, &error); |
| 357 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 358 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | /* authenticated */ |
| 363 | if (!fu_engine_modify_remote (helper->priv->engine, |
| 364 | helper->remote_id, |
| 365 | helper->key, |
| 366 | helper->value, |
| 367 | &error)) { |
| 368 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | /* success */ |
| 373 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
| 374 | } |
| 375 | |
| 376 | static void |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 377 | fu_main_authorize_install_cb (GObject *source, GAsyncResult *res, gpointer user_data) |
Richard Hughes | 404cc51 | 2016-12-21 16:09:48 +0000 | [diff] [blame] | 378 | { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 379 | g_autoptr(FuMainAuthHelper) helper = (FuMainAuthHelper *) user_data; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 380 | g_autoptr(GError) error = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 381 | g_autoptr(PolkitAuthorizationResult) auth = NULL; |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 382 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 383 | /* get result */ |
| 384 | auth = polkit_authority_check_authorization_finish (POLKIT_AUTHORITY (source), |
| 385 | res, &error); |
| 386 | if (!fu_main_authorization_is_valid (auth, &error)) { |
| 387 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 388 | return; |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 391 | /* authenticated */ |
| 392 | if (!fu_engine_install (helper->priv->engine, |
| 393 | helper->device_id, |
| 394 | helper->store, |
| 395 | helper->blob_cab, |
| 396 | helper->flags, |
| 397 | &error)) { |
| 398 | g_dbus_method_invocation_return_gerror (helper->invocation, error); |
| 399 | return; |
Richard Hughes | 654f6b8 | 2016-04-25 12:29:48 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* success */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 403 | g_dbus_method_invocation_return_value (helper->invocation, NULL); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 406 | static void |
| 407 | fu_main_daemon_method_call (GDBusConnection *connection, const gchar *sender, |
| 408 | const gchar *object_path, const gchar *interface_name, |
| 409 | const gchar *method_name, GVariant *parameters, |
| 410 | GDBusMethodInvocation *invocation, gpointer user_data) |
| 411 | { |
| 412 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 413 | GVariant *val = NULL; |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 414 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 415 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 416 | if (g_strcmp0 (method_name, "GetDevices") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 417 | g_autoptr(GPtrArray) devices = NULL; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 418 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 419 | devices = fu_engine_get_devices (priv->engine, &error); |
| 420 | if (devices == NULL) { |
| 421 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 422 | return; |
| 423 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 424 | val = fu_main_device_array_to_variant (devices); |
| 425 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 426 | return; |
| 427 | } |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 428 | if (g_strcmp0 (method_name, "GetUpdates") == 0) { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 429 | g_autoptr(GPtrArray) updates = NULL; |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 430 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 431 | updates = fu_engine_get_updates (priv->engine, &error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 432 | if (updates == NULL) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 433 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7708a0f | 2015-07-21 08:41:22 +0100 | [diff] [blame] | 434 | return; |
| 435 | } |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 436 | val = fu_main_result_array_to_variant (updates); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 437 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 438 | return; |
| 439 | } |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 440 | if (g_strcmp0 (method_name, "GetReleases") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 441 | const gchar *device_id; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 442 | g_autoptr(GPtrArray) releases = NULL; |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 443 | g_variant_get (parameters, "(&s)", &device_id); |
| 444 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 445 | releases = fu_engine_get_releases (priv->engine, device_id, &error); |
| 446 | if (releases == NULL) { |
| 447 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 448 | return; |
| 449 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 450 | val = fu_main_release_array_to_variant (releases); |
| 451 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | e4a100c | 2017-06-04 21:23:50 +0100 | [diff] [blame] | 452 | return; |
| 453 | } |
Richard Hughes | 97284b1 | 2017-09-13 17:07:58 +0100 | [diff] [blame] | 454 | if (g_strcmp0 (method_name, "GetDowngrades") == 0) { |
| 455 | const gchar *device_id; |
| 456 | g_autoptr(GPtrArray) releases = NULL; |
| 457 | g_variant_get (parameters, "(&s)", &device_id); |
| 458 | g_debug ("Called %s(%s)", method_name, device_id); |
| 459 | releases = fu_engine_get_downgrades (priv->engine, device_id, &error); |
| 460 | if (releases == NULL) { |
| 461 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 462 | return; |
| 463 | } |
| 464 | val = fu_main_release_array_to_variant (releases); |
| 465 | g_dbus_method_invocation_return_value (invocation, val); |
| 466 | return; |
| 467 | } |
Richard Hughes | a96413a | 2017-09-13 17:19:59 +0100 | [diff] [blame] | 468 | if (g_strcmp0 (method_name, "GetUpgrades") == 0) { |
| 469 | const gchar *device_id; |
| 470 | g_autoptr(GPtrArray) releases = NULL; |
| 471 | g_variant_get (parameters, "(&s)", &device_id); |
| 472 | g_debug ("Called %s(%s)", method_name, device_id); |
| 473 | releases = fu_engine_get_upgrades (priv->engine, device_id, &error); |
| 474 | if (releases == NULL) { |
| 475 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 476 | return; |
| 477 | } |
| 478 | val = fu_main_release_array_to_variant (releases); |
| 479 | g_dbus_method_invocation_return_value (invocation, val); |
| 480 | return; |
| 481 | } |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 482 | if (g_strcmp0 (method_name, "GetRemotes") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 483 | g_autoptr(GPtrArray) remotes = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 484 | g_debug ("Called %s()", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 485 | remotes = fu_engine_get_remotes (priv->engine, &error); |
| 486 | if (remotes == NULL) { |
| 487 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 488 | return; |
| 489 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 490 | val = fu_main_remote_array_to_variant (remotes); |
| 491 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 492 | return; |
| 493 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 494 | if (g_strcmp0 (method_name, "ClearResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 495 | const gchar *device_id; |
| 496 | g_variant_get (parameters, "(&s)", &device_id); |
| 497 | g_debug ("Called %s(%s)", method_name, device_id); |
| 498 | if (!fu_engine_clear_results (priv->engine, device_id, &error)) { |
| 499 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 500 | return; |
| 501 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 502 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 503 | return; |
| 504 | } |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 505 | if (g_strcmp0 (method_name, "GetResults") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 506 | const gchar *device_id = NULL; |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame^] | 507 | g_autoptr(FwupdDevice) result = NULL; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 508 | g_variant_get (parameters, "(&s)", &device_id); |
| 509 | g_debug ("Called %s(%s)", method_name, device_id); |
| 510 | result = fu_engine_get_results (priv->engine, device_id, &error); |
| 511 | if (result == NULL) { |
| 512 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 513 | return; |
| 514 | } |
Richard Hughes | 93b1576 | 2017-09-15 11:05:23 +0100 | [diff] [blame^] | 515 | val = fwupd_device_to_data (result, "(a{sv})"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 516 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 0e883ee | 2015-03-18 17:22:33 +0000 | [diff] [blame] | 517 | return; |
| 518 | } |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 519 | if (g_strcmp0 (method_name, "UpdateMetadataWithId") == 0) { |
| 520 | GDBusMessage *message; |
| 521 | GUnixFDList *fd_list; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 522 | const gchar *remote_id = NULL; |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 523 | gint fd_data; |
| 524 | gint fd_sig; |
| 525 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 526 | g_variant_get (parameters, "(&shh)", &remote_id, &fd_data, &fd_sig); |
| 527 | 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] | 528 | |
Richard Hughes | 5935ebd | 2017-06-16 15:40:31 +0100 | [diff] [blame] | 529 | /* update the metadata store */ |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 530 | message = g_dbus_method_invocation_get_message (invocation); |
| 531 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 532 | if (fd_list == NULL || g_unix_fd_list_get_length (fd_list) != 2) { |
| 533 | g_set_error (&error, |
| 534 | FWUPD_ERROR, |
| 535 | FWUPD_ERROR_INTERNAL, |
| 536 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 537 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 538 | return; |
| 539 | } |
| 540 | fd_data = g_unix_fd_list_get (fd_list, 0, &error); |
| 541 | if (fd_data < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 542 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | fd_sig = g_unix_fd_list_get (fd_list, 1, &error); |
| 546 | if (fd_sig < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 547 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1b50d96 | 2017-06-02 12:23:00 +0100 | [diff] [blame] | 548 | return; |
| 549 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 550 | |
| 551 | /* store new metadata (will close the fds when done) */ |
| 552 | if (!fu_engine_update_metadata (priv->engine, remote_id, |
| 553 | fd_data, fd_sig, &error)) { |
| 554 | g_prefix_error (&error, "Failed to update metadata: "); |
| 555 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 556 | return; |
| 557 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 558 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | ae0efdc | 2015-06-24 16:18:29 +0100 | [diff] [blame] | 559 | return; |
| 560 | } |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 561 | if (g_strcmp0 (method_name, "Unlock") == 0) { |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 562 | FuMainAuthHelper *helper; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 563 | const gchar *device_id = NULL; |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 564 | g_autoptr(PolkitSubject) subject = NULL; |
| 565 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 566 | g_variant_get (parameters, "(&s)", &device_id); |
| 567 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | fe5cc90 | 2016-06-29 10:00:00 +0100 | [diff] [blame] | 568 | |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 569 | /* authenticate */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 570 | helper = g_new0 (FuMainAuthHelper, 1); |
| 571 | helper->priv = priv; |
| 572 | helper->invocation = g_object_ref (invocation); |
| 573 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 574 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 575 | polkit_authority_check_authorization (priv->authority, subject, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 576 | "org.freedesktop.fwupd.device-unlock", |
| 577 | NULL, |
| 578 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 579 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 580 | fu_main_authorize_unlock_cb, |
Richard Hughes | 9a410ce | 2016-02-28 15:58:54 +0000 | [diff] [blame] | 581 | helper); |
| 582 | return; |
| 583 | } |
Richard Hughes | a6bd558 | 2017-09-07 14:32:22 +0100 | [diff] [blame] | 584 | if (g_strcmp0 (method_name, "ModifyRemote") == 0) { |
| 585 | FuMainAuthHelper *helper; |
| 586 | const gchar *remote_id = NULL; |
| 587 | const gchar *key = NULL; |
| 588 | const gchar *value = NULL; |
| 589 | g_autoptr(PolkitSubject) subject = NULL; |
| 590 | |
| 591 | /* check the id exists */ |
| 592 | g_variant_get (parameters, "(&s&s&s)", &remote_id, &key, &value); |
| 593 | g_debug ("Called %s(%s,%s=%s)", method_name, remote_id, key, value); |
| 594 | |
| 595 | /* create helper object */ |
| 596 | helper = g_new0 (FuMainAuthHelper, 1); |
| 597 | helper->invocation = g_object_ref (invocation); |
| 598 | helper->remote_id = g_strdup (remote_id); |
| 599 | helper->key = g_strdup (key); |
| 600 | helper->value = g_strdup (value); |
| 601 | helper->priv = priv; |
| 602 | |
| 603 | /* authenticate */ |
| 604 | subject = polkit_system_bus_name_new (sender); |
| 605 | polkit_authority_check_authorization (helper->priv->authority, subject, |
| 606 | "org.freedesktop.fwupd.modify-remote", |
| 607 | NULL, |
| 608 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 609 | NULL, |
| 610 | fu_main_authorize_modify_remote_cb, |
| 611 | helper); |
| 612 | return; |
| 613 | } |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 614 | if (g_strcmp0 (method_name, "VerifyUpdate") == 0) { |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 615 | FuMainAuthHelper *helper; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 616 | const gchar *device_id = NULL; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 617 | g_autoptr(PolkitSubject) subject = NULL; |
| 618 | |
| 619 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 620 | g_variant_get (parameters, "(&s)", &device_id); |
| 621 | g_debug ("Called %s(%s)", method_name, device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 622 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 623 | /* create helper object */ |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 624 | helper = g_new0 (FuMainAuthHelper, 1); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 625 | helper->invocation = g_object_ref (invocation); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 626 | helper->device_id = g_strdup (device_id); |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 627 | helper->priv = priv; |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 628 | |
| 629 | /* authenticate */ |
| 630 | subject = polkit_system_bus_name_new (sender); |
| 631 | polkit_authority_check_authorization (helper->priv->authority, subject, |
| 632 | "org.freedesktop.fwupd.verify-update", |
| 633 | NULL, |
| 634 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 635 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 636 | fu_main_authorize_verify_update_cb, |
Richard Hughes | 29c220d | 2016-12-14 17:09:54 +0000 | [diff] [blame] | 637 | helper); |
| 638 | return; |
| 639 | } |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 640 | if (g_strcmp0 (method_name, "Verify") == 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 641 | const gchar *device_id = NULL; |
| 642 | g_variant_get (parameters, "(&s)", &device_id); |
| 643 | g_debug ("Called %s(%s)", method_name, device_id); |
| 644 | if (!fu_engine_verify (priv->engine, device_id, &error)) { |
| 645 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 646 | return; |
| 647 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 648 | g_dbus_method_invocation_return_value (invocation, NULL); |
Richard Hughes | a043c2e | 2015-06-29 08:43:18 +0100 | [diff] [blame] | 649 | return; |
| 650 | } |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 651 | if (g_strcmp0 (method_name, "Install") == 0) { |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 652 | FuMainAuthHelper *helper; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 653 | GVariant *prop_value; |
Richard Hughes | a8e8394 | 2015-03-09 17:19:35 +0000 | [diff] [blame] | 654 | const gchar *action_id; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 655 | const gchar *device_id = NULL; |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 656 | gchar *prop_key; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 657 | gint32 fd_handle = 0; |
| 658 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 659 | GDBusMessage *message; |
| 660 | GUnixFDList *fd_list; |
Richard Hughes | 60f48c2 | 2015-10-08 20:25:51 +0100 | [diff] [blame] | 661 | g_autoptr(PolkitSubject) subject = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 662 | g_autoptr(GVariantIter) iter = NULL; |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 663 | |
| 664 | /* check the id exists */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 665 | g_variant_get (parameters, "(&sha{sv})", &device_id, &fd_handle, &iter); |
| 666 | g_debug ("Called %s(%s,%i)", method_name, device_id, fd_handle); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 667 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 668 | /* create helper object */ |
| 669 | helper = g_new0 (FuMainAuthHelper, 1); |
| 670 | helper->invocation = g_object_ref (invocation); |
| 671 | helper->device_id = g_strdup (device_id); |
| 672 | helper->priv = priv; |
| 673 | |
| 674 | /* get flags */ |
| 675 | while (g_variant_iter_next (iter, "{&sv}", &prop_key, &prop_value)) { |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 676 | g_debug ("got option %s", prop_key); |
| 677 | if (g_strcmp0 (prop_key, "offline") == 0 && |
| 678 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 679 | helper->flags |= FWUPD_INSTALL_FLAG_OFFLINE; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 680 | if (g_strcmp0 (prop_key, "allow-older") == 0 && |
| 681 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 682 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_OLDER; |
Richard Hughes | e7c1264 | 2015-03-04 20:28:59 +0000 | [diff] [blame] | 683 | if (g_strcmp0 (prop_key, "allow-reinstall") == 0 && |
| 684 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 685 | helper->flags |= FWUPD_INSTALL_FLAG_ALLOW_REINSTALL; |
Mario Limonciello | 71a5b98 | 2016-05-10 15:38:53 -0500 | [diff] [blame] | 686 | if (g_strcmp0 (prop_key, "force") == 0 && |
| 687 | g_variant_get_boolean (prop_value) == TRUE) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 688 | helper->flags |= FWUPD_INSTALL_FLAG_FORCE; |
Richard Hughes | 1ffde6c | 2015-03-02 22:44:48 +0000 | [diff] [blame] | 689 | g_variant_unref (prop_value); |
Richard Hughes | 74cc217 | 2015-02-27 13:19:46 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 692 | |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 693 | /* get the fd */ |
| 694 | message = g_dbus_method_invocation_get_message (invocation); |
| 695 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 696 | 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] | 697 | g_set_error (&error, |
| 698 | FWUPD_ERROR, |
| 699 | FWUPD_ERROR_INTERNAL, |
| 700 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 701 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 702 | return; |
| 703 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 704 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 705 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 706 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 710 | /* parse the cab file before authenticating so we can work out |
| 711 | * what action ID to use, for instance, if this is trusted -- |
| 712 | * this will also close the fd when done */ |
Richard Hughes | 943d2c9 | 2017-06-21 09:04:39 +0100 | [diff] [blame] | 713 | helper->blob_cab = fu_common_get_contents_fd (fd, FU_ENGINE_FIRMWARE_SIZE_MAX, &error); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 714 | if (helper->blob_cab == NULL) { |
| 715 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 5d14def | 2015-10-07 17:43:10 +0100 | [diff] [blame] | 716 | return; |
| 717 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 718 | helper->store = fu_engine_get_store_from_blob (priv->engine, |
| 719 | helper->blob_cab, |
| 720 | &error); |
| 721 | if (helper->store == NULL) { |
| 722 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | |
Richard Hughes | 67ec898 | 2015-03-03 11:39:27 +0000 | [diff] [blame] | 726 | /* authenticate */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 727 | action_id = fu_engine_get_action_id_for_device (priv->engine, |
| 728 | helper->device_id, |
| 729 | helper->store, |
| 730 | helper->flags, |
| 731 | &error); |
| 732 | if (action_id == NULL) { |
| 733 | g_dbus_method_invocation_return_gerror (invocation, error); |
| 734 | return; |
| 735 | } |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 736 | subject = polkit_system_bus_name_new (sender); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 737 | polkit_authority_check_authorization (priv->authority, subject, |
| 738 | action_id, NULL, |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 739 | POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, |
| 740 | NULL, |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 741 | fu_main_authorize_install_cb, |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 742 | helper); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 743 | return; |
| 744 | } |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 745 | if (g_strcmp0 (method_name, "GetDetailsLocal") == 0) { |
| 746 | GDBusMessage *message; |
| 747 | GUnixFDList *fd_list; |
| 748 | gint32 fd_handle = 0; |
| 749 | gint fd; |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 750 | g_autoptr(GPtrArray) results = NULL; |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 751 | |
| 752 | /* get parameters */ |
| 753 | g_variant_get (parameters, "(h)", &fd_handle); |
| 754 | g_debug ("Called %s(%i)", method_name, fd_handle); |
| 755 | |
| 756 | /* get the fd */ |
| 757 | message = g_dbus_method_invocation_get_message (invocation); |
| 758 | fd_list = g_dbus_message_get_unix_fd_list (message); |
| 759 | 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] | 760 | g_set_error (&error, |
| 761 | FWUPD_ERROR, |
| 762 | FWUPD_ERROR_INTERNAL, |
| 763 | "invalid handle"); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 764 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 765 | return; |
| 766 | } |
Richard Hughes | 7419e96 | 2016-11-22 19:48:06 +0000 | [diff] [blame] | 767 | fd = g_unix_fd_list_get (fd_list, 0, &error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 768 | if (fd < 0) { |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 769 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 770 | return; |
| 771 | } |
| 772 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 773 | /* get details about the file (will close the fd when done) */ |
| 774 | results = fu_engine_get_details_local (priv->engine, fd, &error); |
| 775 | if (results == NULL) { |
| 776 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 777 | return; |
| 778 | } |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 779 | val = fu_main_result_array_to_variant (results); |
| 780 | g_dbus_method_invocation_return_value (invocation, val); |
Richard Hughes | 7289a6b | 2016-05-29 09:27:47 +0100 | [diff] [blame] | 781 | return; |
| 782 | } |
Richard Hughes | 060af61 | 2016-08-17 17:32:34 +0100 | [diff] [blame] | 783 | g_set_error (&error, |
| 784 | G_DBUS_ERROR, |
| 785 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 786 | "no such method %s", method_name); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 787 | g_dbus_method_invocation_return_gerror (invocation, error); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 790 | static GVariant * |
| 791 | fu_main_daemon_get_property (GDBusConnection *connection_, const gchar *sender, |
| 792 | const gchar *object_path, const gchar *interface_name, |
| 793 | const gchar *property_name, GError **error, |
| 794 | gpointer user_data) |
| 795 | { |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 796 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 797 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 798 | if (g_strcmp0 (property_name, "DaemonVersion") == 0) |
| 799 | return g_variant_new_string (VERSION); |
| 800 | |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 801 | if (g_strcmp0 (property_name, "Status") == 0) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 802 | return g_variant_new_uint32 (fu_engine_get_status (priv->engine)); |
Richard Hughes | 773ce98 | 2015-03-09 22:40:57 +0000 | [diff] [blame] | 803 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 804 | /* return an error */ |
| 805 | g_set_error (error, |
Richard Hughes | 8645ec9 | 2015-03-19 10:14:32 +0000 | [diff] [blame] | 806 | G_DBUS_ERROR, |
| 807 | G_DBUS_ERROR_UNKNOWN_PROPERTY, |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 808 | "failed to get daemon property %s", |
| 809 | property_name); |
| 810 | return NULL; |
| 811 | } |
| 812 | |
Richard Hughes | fd46884 | 2015-04-22 16:44:08 +0100 | [diff] [blame] | 813 | static void |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 814 | fu_main_on_bus_acquired_cb (GDBusConnection *connection, |
| 815 | const gchar *name, |
| 816 | gpointer user_data) |
| 817 | { |
| 818 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 819 | guint registration_id; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 820 | g_autoptr(GError) error = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 821 | static const GDBusInterfaceVTable interface_vtable = { |
| 822 | fu_main_daemon_method_call, |
| 823 | fu_main_daemon_get_property, |
| 824 | NULL |
| 825 | }; |
| 826 | |
| 827 | priv->connection = g_object_ref (connection); |
| 828 | registration_id = g_dbus_connection_register_object (connection, |
| 829 | FWUPD_DBUS_PATH, |
| 830 | priv->introspection_daemon->interfaces[0], |
| 831 | &interface_vtable, |
| 832 | priv, /* user_data */ |
| 833 | NULL, /* user_data_free_func */ |
| 834 | NULL); /* GError** */ |
| 835 | g_assert (registration_id > 0); |
Richard Hughes | 1842329 | 2015-03-09 17:10:50 +0000 | [diff] [blame] | 836 | |
| 837 | /* connect to D-Bus directly */ |
| 838 | priv->proxy_uid = |
| 839 | g_dbus_proxy_new_sync (priv->connection, |
| 840 | G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 841 | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 842 | NULL, |
| 843 | "org.freedesktop.DBus", |
| 844 | "/org/freedesktop/DBus", |
| 845 | "org.freedesktop.DBus", |
| 846 | NULL, |
| 847 | &error); |
| 848 | if (priv->proxy_uid == NULL) { |
| 849 | g_warning ("cannot connect to DBus: %s", error->message); |
| 850 | return; |
| 851 | } |
Richard Hughes | 3f23650 | 2015-09-24 15:43:02 +0100 | [diff] [blame] | 852 | |
| 853 | /* dump startup profile data */ |
Richard Hughes | 2a1e75d | 2015-12-18 17:42:53 +0000 | [diff] [blame] | 854 | if (fu_debug_is_verbose ()) |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 855 | fu_engine_profile_dump (priv->engine); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 858 | static void |
| 859 | fu_main_on_name_acquired_cb (GDBusConnection *connection, |
| 860 | const gchar *name, |
| 861 | gpointer user_data) |
| 862 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 863 | g_debug ("FuMain: acquired name: %s", name); |
| 864 | } |
| 865 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 866 | static void |
| 867 | fu_main_on_name_lost_cb (GDBusConnection *connection, |
| 868 | const gchar *name, |
| 869 | gpointer user_data) |
| 870 | { |
| 871 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 872 | g_debug ("FuMain: lost name: %s", name); |
| 873 | g_main_loop_quit (priv->loop); |
| 874 | } |
| 875 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 876 | static gboolean |
| 877 | fu_main_timed_exit_cb (gpointer user_data) |
| 878 | { |
| 879 | GMainLoop *loop = (GMainLoop *) user_data; |
| 880 | g_main_loop_quit (loop); |
| 881 | return G_SOURCE_REMOVE; |
| 882 | } |
| 883 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 884 | static GDBusNodeInfo * |
| 885 | fu_main_load_introspection (const gchar *filename, GError **error) |
| 886 | { |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 887 | g_autoptr(GBytes) data = NULL; |
| 888 | g_autofree gchar *path = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 889 | |
| 890 | /* lookup data */ |
| 891 | path = g_build_filename ("/org/freedesktop/fwupd", filename, NULL); |
| 892 | data = g_resource_lookup_data (fu_get_resource (), |
| 893 | path, |
| 894 | G_RESOURCE_LOOKUP_FLAGS_NONE, |
| 895 | error); |
| 896 | if (data == NULL) |
| 897 | return NULL; |
| 898 | |
| 899 | /* build introspection from XML */ |
| 900 | return g_dbus_node_info_new_for_xml (g_bytes_get_data (data, NULL), error); |
| 901 | } |
| 902 | |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 903 | static gboolean |
| 904 | fu_main_perhaps_own_name (gpointer user_data) |
| 905 | { |
| 906 | FuMainPrivate *priv = (FuMainPrivate *) user_data; |
| 907 | g_autoptr(GError) error = NULL; |
| 908 | |
| 909 | /* are any plugins pending */ |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 910 | if (!fu_engine_check_plugins_pending (priv->engine, &error)) { |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 911 | g_debug ("trying again: %s", error->message); |
| 912 | return G_SOURCE_CONTINUE; |
| 913 | } |
| 914 | |
| 915 | /* own the object */ |
| 916 | g_debug ("registering D-Bus service"); |
| 917 | priv->owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, |
| 918 | FWUPD_DBUS_SERVICE, |
| 919 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 920 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 921 | fu_main_on_bus_acquired_cb, |
| 922 | fu_main_on_name_acquired_cb, |
| 923 | fu_main_on_name_lost_cb, |
| 924 | priv, NULL); |
| 925 | return G_SOURCE_REMOVE; |
| 926 | } |
| 927 | |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 928 | static void |
| 929 | fu_main_private_free (FuMainPrivate *priv) |
| 930 | { |
| 931 | if (priv->loop != NULL) |
| 932 | g_main_loop_unref (priv->loop); |
| 933 | if (priv->owner_id > 0) |
| 934 | g_bus_unown_name (priv->owner_id); |
| 935 | if (priv->proxy_uid != NULL) |
| 936 | g_object_unref (priv->proxy_uid); |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 937 | if (priv->engine != NULL) |
| 938 | g_object_unref (priv->engine); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 939 | if (priv->connection != NULL) |
| 940 | g_object_unref (priv->connection); |
| 941 | if (priv->authority != NULL) |
| 942 | g_object_unref (priv->authority); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 943 | if (priv->introspection_daemon != NULL) |
| 944 | g_dbus_node_info_unref (priv->introspection_daemon); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 945 | g_free (priv); |
| 946 | } |
| 947 | |
| 948 | G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuMainPrivate, fu_main_private_free) |
| 949 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 950 | int |
| 951 | main (int argc, char *argv[]) |
| 952 | { |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 953 | gboolean immediate_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 954 | gboolean timed_exit = FALSE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 955 | const GOptionEntry options[] = { |
| 956 | { "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit, |
| 957 | /* TRANSLATORS: exit after we've started up, used for user profiling */ |
| 958 | _("Exit after a small delay"), NULL }, |
| 959 | { "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit, |
| 960 | /* TRANSLATORS: exit straight away, used for automatic profiling */ |
| 961 | _("Exit after the engine has loaded"), NULL }, |
| 962 | { NULL} |
| 963 | }; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 964 | g_autoptr(FuMainPrivate) priv = NULL; |
Richard Hughes | 4683243 | 2015-09-11 13:43:15 +0100 | [diff] [blame] | 965 | g_autoptr(GError) error = NULL; |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 966 | g_autoptr(GOptionContext) context = NULL; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 967 | |
| 968 | setlocale (LC_ALL, ""); |
| 969 | |
| 970 | bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 971 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 972 | textdomain (GETTEXT_PACKAGE); |
| 973 | |
| 974 | /* TRANSLATORS: program name */ |
Richard Hughes | 63a407a | 2015-07-22 08:54:14 +0100 | [diff] [blame] | 975 | g_set_application_name (_("Firmware Update Daemon")); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 976 | context = g_option_context_new (NULL); |
| 977 | g_option_context_add_main_entries (context, options, NULL); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 978 | g_option_context_add_group (context, fu_debug_get_option_group ()); |
Richard Hughes | 8ded6ca | 2015-03-16 12:51:36 +0000 | [diff] [blame] | 979 | /* TRANSLATORS: program summary */ |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 980 | g_option_context_set_summary (context, _("Firmware Update D-Bus Service")); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 981 | if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 982 | g_printerr ("Failed to parse command line: %s\n", error->message); |
| 983 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /* create new objects */ |
| 987 | priv = g_new0 (FuMainPrivate, 1); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 988 | priv->loop = g_main_loop_new (NULL, FALSE); |
Richard Hughes | 8bbfdf4 | 2015-02-26 22:28:09 +0000 | [diff] [blame] | 989 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 990 | /* load engine */ |
| 991 | priv->engine = fu_engine_new (); |
| 992 | g_signal_connect (priv->engine, "changed", |
| 993 | G_CALLBACK (fu_main_engine_changed_cb), |
| 994 | priv); |
| 995 | g_signal_connect (priv->engine, "device-added", |
| 996 | G_CALLBACK (fu_main_engine_device_added_cb), |
| 997 | priv); |
| 998 | g_signal_connect (priv->engine, "device-removed", |
| 999 | G_CALLBACK (fu_main_engine_device_removed_cb), |
| 1000 | priv); |
| 1001 | g_signal_connect (priv->engine, "device-changed", |
| 1002 | G_CALLBACK (fu_main_engine_device_changed_cb), |
| 1003 | priv); |
| 1004 | g_signal_connect (priv->engine, "status-changed", |
| 1005 | G_CALLBACK (fu_main_engine_status_changed_cb), |
| 1006 | priv); |
| 1007 | g_signal_connect (priv->engine, "percentage-changed", |
| 1008 | G_CALLBACK (fu_main_engine_percentage_changed_cb), |
| 1009 | priv); |
| 1010 | if (!fu_engine_load (priv->engine, &error)) { |
| 1011 | g_printerr ("Failed to load engine: %s\n", error->message); |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1012 | return EXIT_FAILURE; |
Richard Hughes | 804c075 | 2015-08-04 14:53:52 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Richard Hughes | 9945edb | 2017-06-19 10:03:55 +0100 | [diff] [blame] | 1015 | /* keep polling until all the plugins are ready */ |
| 1016 | g_timeout_add (200, fu_main_perhaps_own_name, priv); |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 1017 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1018 | /* load introspection from file */ |
| 1019 | priv->introspection_daemon = fu_main_load_introspection (FWUPD_DBUS_INTERFACE ".xml", |
| 1020 | &error); |
| 1021 | if (priv->introspection_daemon == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1022 | g_printerr ("Failed to load introspection: %s\n", error->message); |
| 1023 | return EXIT_FAILURE; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1026 | /* get authority */ |
| 1027 | priv->authority = polkit_authority_get_sync (NULL, &error); |
| 1028 | if (priv->authority == NULL) { |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1029 | g_printerr ("Failed to load authority: %s\n", error->message); |
| 1030 | return EXIT_FAILURE; |
Richard Hughes | f508e76 | 2015-02-27 12:49:36 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1033 | /* Only timeout and close the mainloop if we have specified it |
| 1034 | * on the command line */ |
| 1035 | if (immediate_exit) |
| 1036 | g_idle_add (fu_main_timed_exit_cb, priv->loop); |
| 1037 | else if (timed_exit) |
| 1038 | g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop); |
| 1039 | |
| 1040 | /* wait */ |
Richard Hughes | 4619f9f | 2017-06-14 13:55:30 +0100 | [diff] [blame] | 1041 | g_message ("Daemon ready for requests"); |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1042 | g_main_loop_run (priv->loop); |
| 1043 | |
| 1044 | /* success */ |
Richard Hughes | f29a6ee | 2017-06-02 19:50:37 +0100 | [diff] [blame] | 1045 | return EXIT_SUCCESS; |
Richard Hughes | 8dbfb1c | 2015-02-26 13:07:40 +0000 | [diff] [blame] | 1046 | } |