From dfed515573dd0ae804778ebd7654a0c15ca5219c Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Fri, 2 Jun 2017 12:13:07 +0100 Subject: [PATCH] Allow downloading metadata from more than just the LVFS Add the concept of 'remotes' that can dropped into /etc and used as firmware metadata sources. This may be desirable when firmware is only accessable with a valid support contract or from behind a VPN. --- contrib/fwupd.spec.in | 4 + data/fwupd.conf | 6 +- data/meson.build | 4 + data/remotes.d/lvfs-testing.conf | 5 + data/remotes.d/lvfs.conf | 3 + libfwupd/fwupd-client.c | 175 ++++++++++++++- libfwupd/fwupd-client.h | 11 +- libfwupd/fwupd-remote-private.h | 37 ++++ libfwupd/fwupd-remote.c | 358 +++++++++++++++++++++++++++++++ libfwupd/fwupd-remote.h | 46 ++++ libfwupd/fwupd-self-test.c | 49 +++++ libfwupd/fwupd.h | 1 + libfwupd/meson.build | 26 ++- meson.build | 1 + src/fu-util.c | 99 +++++---- 15 files changed, 760 insertions(+), 65 deletions(-) create mode 100644 data/remotes.d/lvfs-testing.conf create mode 100644 data/remotes.d/lvfs.conf create mode 100644 libfwupd/fwupd-remote-private.h create mode 100644 libfwupd/fwupd-remote.c create mode 100644 libfwupd/fwupd-remote.h diff --git a/contrib/fwupd.spec.in b/contrib/fwupd.spec.in index 3126dfec7..0984c96eb 100644 --- a/contrib/fwupd.spec.in +++ b/contrib/fwupd.spec.in @@ -143,6 +143,10 @@ mkdir -p --mode=0700 $RPM_BUILD_ROOT%{_localstatedir}/lib/fwupd/gnupg %dir %{_libexecdir}/fwupd %{_libexecdir}/fwupd/fwupd %{_bindir}/fwupdmgr +%dir %{_sysconfdir}/fwupd +%dir %{_sysconfdir}/fwupd/remotes.d +%{_sysconfdir}/fwupd/remotes.d/lvfs.conf +%{_sysconfdir}/fwupd/remotes.d/lvfs-testing.conf %{_sysconfdir}/pki/fwupd %{_sysconfdir}/pki/fwupd-metadata %{_sysconfdir}/dbus-1/system.d/org.freedesktop.fwupd.conf diff --git a/data/fwupd.conf b/data/fwupd.conf index 158f112b4..f3dc70d40 100644 --- a/data/fwupd.conf +++ b/data/fwupd.conf @@ -1,9 +1,7 @@ [fwupd] -# The download URI to use for LVFS metadata -# -# If you want to use testing firmware then change this value to: -# https://s3.amazonaws.com/lvfsbucket/downloads/firmware-testing.xml.gz +# The download URI to use for LVFS metadata -- this is only used as a +# fallback and clients should use FwupdRemote objects instead DownloadURI=https://s3.amazonaws.com/lvfsbucket/downloads/firmware.xml.gz # If we should verify option ROM images diff --git a/data/meson.build b/data/meson.build index bd062d720..5858a3a35 100644 --- a/data/meson.build +++ b/data/meson.build @@ -5,6 +5,10 @@ install_data(['fwupd.conf'], install_dir : get_option('sysconfdir') ) +install_subdir('remotes.d', + install_dir: join_paths(get_option('sysconfdir'), 'fwupd') +) + install_data(['org.freedesktop.fwupd.conf'], install_dir : join_paths(get_option('sysconfdir'), 'dbus-1', 'system.d') ) diff --git a/data/remotes.d/lvfs-testing.conf b/data/remotes.d/lvfs-testing.conf new file mode 100644 index 000000000..bf090c115 --- /dev/null +++ b/data/remotes.d/lvfs-testing.conf @@ -0,0 +1,5 @@ +[fwupd Remote] +Enabled=false +Url=https://s3.amazonaws.com/lvfsbucket/downloads/firmware-testing.xml.gz +Username= +Password= diff --git a/data/remotes.d/lvfs.conf b/data/remotes.d/lvfs.conf new file mode 100644 index 000000000..4aefbbbd3 --- /dev/null +++ b/data/remotes.d/lvfs.conf @@ -0,0 +1,3 @@ +[fwupd Remote] +Enabled=true +Url=https://s3.amazonaws.com/lvfsbucket/downloads/firmware.xml.gz diff --git a/libfwupd/fwupd-client.c b/libfwupd/fwupd-client.c index 6b6faae05..5375feb4a 100644 --- a/libfwupd/fwupd-client.c +++ b/libfwupd/fwupd-client.c @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- * - * Copyright (C) 2016 Richard Hughes + * Copyright (C) 2016-2017 Richard Hughes * * Licensed under the GNU Lesser General Public License Version 2.1 * @@ -33,6 +33,7 @@ #include "fwupd-client.h" #include "fwupd-enums.h" #include "fwupd-error.h" +#include "fwupd-remote-private.h" #include "fwupd-result.h" static void fwupd_client_finalize (GObject *object); @@ -1034,6 +1035,178 @@ fwupd_client_update_metadata (FwupdClient *client, return TRUE; } +static GPtrArray * +fwupd_client_get_config_paths (void) +{ + GPtrArray *paths = g_ptr_array_new_with_free_func (g_free); + const gchar *remotes_dir; + const gchar *system_prefixlibdir = "/usr/lib/fwupd"; + const gchar *system_sysconfdir = "/etc/fwupd"; + g_autofree gchar *sysconfdir = NULL; + + /* only set by the self test program */ + remotes_dir = g_getenv ("FU_SELF_TEST_REMOTES_DIR"); + if (remotes_dir != NULL) { + g_ptr_array_add (paths, g_strdup (remotes_dir)); + return paths; + } + + /* use sysconfig, and then fall back to /etc */ + sysconfdir = g_build_filename (SYSCONFDIR, "fwupd", NULL); + if (g_file_test (sysconfdir, G_FILE_TEST_EXISTS)) { + g_ptr_array_add (paths, g_steal_pointer (&sysconfdir)); + } else { + g_debug ("falling back to system path"); + if (g_file_test (system_sysconfdir, G_FILE_TEST_EXISTS)) + g_ptr_array_add (paths, g_strdup (system_sysconfdir)); + } + + /* add in system-wide locations */ + if (g_file_test (system_prefixlibdir, G_FILE_TEST_EXISTS)) + g_ptr_array_add (paths, g_strdup (system_prefixlibdir)); + + return paths; +} + +static gboolean +fwupd_client_add_remotes_for_path (FwupdClient *client, + GPtrArray *remotes, + const gchar *path, + GCancellable *cancellable, + GError **error) +{ + const gchar *tmp; + g_autofree gchar *path_remotes = NULL; + g_autoptr(GDir) dir = NULL; + + path_remotes = g_build_filename (path, "remotes.d", NULL); + dir = g_dir_open (path_remotes, 0, error); + if (dir == NULL) + return FALSE; + while ((tmp = g_dir_read_name (dir)) != NULL) { + g_autofree gchar *filename = g_build_filename (path_remotes, tmp, NULL); + g_autoptr(FwupdRemote) remote = fwupd_remote_new (); + g_debug ("loading from %s", filename); + if (!fwupd_remote_load_from_filename (remote, filename, + cancellable, error)) + return FALSE; + g_ptr_array_add (remotes, g_steal_pointer (&remote)); + } + return TRUE; +} + +static gint +fwupd_client_remote_sort_cb (gconstpointer a, gconstpointer b) +{ + FwupdRemote *remote_a = *((FwupdRemote **) a); + FwupdRemote *remote_b = *((FwupdRemote **) b); + return g_strcmp0 (fwupd_remote_get_id (remote_a), + fwupd_remote_get_id (remote_b)); +} + +/** + * fwupd_client_get_remotes: + * @client: A #FwupdClient + * @cancellable: the #GCancellable, or %NULL + * @error: the #GError, or %NULL + * + * Gets the list of remotes that have been configured for the system. + * + * Returns: (element-type FwupdRemote) (transfer container): list of remotes, or %NULL + * + * Since: 0.9.3 + **/ +GPtrArray * +fwupd_client_get_remotes (FwupdClient *client, GCancellable *cancellable, GError **error) +{ + g_autoptr(GPtrArray) paths = NULL; + g_autoptr(GPtrArray) remotes = NULL; + + g_return_val_if_fail (FWUPD_IS_CLIENT (client), NULL); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + /* get a list of all config paths */ + paths = fwupd_client_get_config_paths (); + if (paths->len == 0) { + g_set_error_literal (error, + FWUPD_ERROR, + FWUPD_ERROR_NOT_FOUND, + "No search paths found"); + return NULL; + } + + /* look for all remotes */ + remotes = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + for (guint i = 0; i < paths->len; i++) { + const gchar *path = g_ptr_array_index (paths, i); + g_debug ("using config path of %s", path); + if (!fwupd_client_add_remotes_for_path (client, remotes, path, + cancellable, error)) + return FALSE; + } + + /* nothing found */ + if (remotes->len == 0) { + g_set_error_literal (error, + FWUPD_ERROR, + FWUPD_ERROR_NOT_FOUND, + "No remotes found in search paths"); + return NULL; + } + + /* order these by name */ + g_ptr_array_sort (remotes, fwupd_client_remote_sort_cb); + + /* success */ + return g_steal_pointer (&remotes); +} + +/** + * fwupd_client_get_remote_by_id: + * @client: A #FwupdClient + * @remote_id: the remote ID, e.g. "lvfs-testing" + * @cancellable: the #GCancellable, or %NULL + * @error: the #GError, or %NULL + * + * Gets a specific remote that has been configured for the system. + * + * Returns: (transfer full): a #FwupdRemote, or %NULL if not found + * + * Since: 0.9.3 + **/ +FwupdRemote * +fwupd_client_get_remote_by_id (FwupdClient *client, + const gchar *remote_id, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GPtrArray) remotes = NULL; + + g_return_val_if_fail (FWUPD_IS_CLIENT (client), NULL); + g_return_val_if_fail (remote_id != NULL, NULL); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + /* find remote in list */ + remotes = fwupd_client_get_remotes (client, cancellable, error); + if (remotes == NULL) + return NULL; + for (guint i = 0; i < remotes->len; i++) { + FwupdRemote *remote = g_ptr_array_index (remotes, i); + if (g_strcmp0 (remote_id, fwupd_remote_get_id (remote)) == 0) + return g_object_ref (remote); + } + + /* nothing found */ + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_NOT_FOUND, + "No remote '%s' found in search paths", + remote_id); + return NULL; +} + static void fwupd_client_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) diff --git a/libfwupd/fwupd-client.h b/libfwupd/fwupd-client.h index 7acd8dd2b..022500a1f 100644 --- a/libfwupd/fwupd-client.h +++ b/libfwupd/fwupd-client.h @@ -1,6 +1,6 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- * - * Copyright (C) 2016 Richard Hughes + * Copyright (C) 2016-2017 Richard Hughes * * Licensed under the GNU Lesser General Public License Version 2.1 * @@ -26,6 +26,7 @@ #include #include "fwupd-enums.h" +#include "fwupd-remote.h" #include "fwupd-result.h" G_BEGIN_DECLS @@ -105,6 +106,14 @@ gboolean fwupd_client_update_metadata (FwupdClient *client, FwupdStatus fwupd_client_get_status (FwupdClient *client); guint fwupd_client_get_percentage (FwupdClient *client); +GPtrArray *fwupd_client_get_remotes (FwupdClient *client, + GCancellable *cancellable, + GError **error); +FwupdRemote *fwupd_client_get_remote_by_id (FwupdClient *client, + const gchar *remote_id, + GCancellable *cancellable, + GError **error); + G_END_DECLS #endif /* __FWUPD_CLIENT_H */ diff --git a/libfwupd/fwupd-remote-private.h b/libfwupd/fwupd-remote-private.h new file mode 100644 index 000000000..0de7f74a6 --- /dev/null +++ b/libfwupd/fwupd-remote-private.h @@ -0,0 +1,37 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2017 Richard Hughes + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __FWUPD_REMOTE_PRIVATE_H +#define __FWUPD_REMOTE_PRIVATE_H + +#include "fwupd-remote.h" + +G_BEGIN_DECLS + +gboolean fwupd_remote_load_from_filename (FwupdRemote *self, + const gchar *filename, + GCancellable *cancellable, + GError **error); + +G_END_DECLS + +#endif /* __FWUPD_REMOTE_PRIVATE_H */ + diff --git a/libfwupd/fwupd-remote.c b/libfwupd/fwupd-remote.c new file mode 100644 index 000000000..93001e7c6 --- /dev/null +++ b/libfwupd/fwupd-remote.c @@ -0,0 +1,358 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2017 Richard Hughes + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "fwupd-error.h" +#include "fwupd-remote-private.h" + +static void fwupd_remote_finalize (GObject *obj); + +struct _FwupdRemote +{ + GObject parent_instance; + gchar *id; + gchar *filename; + gchar *filename_asc; + gboolean enabled; + SoupURI *uri; + SoupURI *uri_asc; +}; + +enum { + PROP_0, + PROP_ID, + PROP_ENABLED, + PROP_LAST +}; + +G_DEFINE_TYPE (FwupdRemote, fwupd_remote, G_TYPE_OBJECT) + +/** + * fwupd_remote_load_from_filename: + * @self: A #FwupdRemote + * @cancellable: the #GCancellable, or %NULL + * @error: the #GError, or %NULL + * + * Sets up the self ready for use. Most other methods call this + * for you, and do you only need to call this if you are just watching + * the self. + * + * Returns: %TRUE for success + * + * Since: 0.9.3 + **/ +gboolean +fwupd_remote_load_from_filename (FwupdRemote *self, + const gchar *filename, + GCancellable *cancellable, + GError **error) +{ + const gchar *group = "fwupd Remote"; + g_autofree gchar *basename = NULL; + g_autofree gchar *basename_asc = NULL; + g_autofree gchar *url = NULL; + g_autofree gchar *url_asc = NULL; + g_autofree gchar *username = NULL; + g_autofree gchar *password = NULL; + g_autoptr(GKeyFile) kf = NULL; + + g_return_val_if_fail (FWUPD_IS_REMOTE (self), FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + /* set ID */ + self->id = g_path_get_basename (filename); + g_strdelimit (self->id, ".", '\0'); + + /* load file */ + kf = g_key_file_new (); + if (!g_key_file_load_from_file (kf, filename, G_KEY_FILE_NONE, error)) + return FALSE; + + /* extract data */ + self->enabled = g_key_file_get_boolean (kf, group, "Enabled", NULL); + url = g_key_file_get_string (kf, group, "Url", error); + if (url == NULL) + return FALSE; + self->uri = soup_uri_new (url); + if (self->uri == NULL) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INVALID_FILE, + "Failed to parse URI '%s' in %s", + url, filename); + return FALSE; + } + + /* username and password are optional */ + username = g_key_file_get_string (kf, group, "Username", NULL); + if (username != NULL && username[0] != '\0') + soup_uri_set_user (self->uri, username); + password = g_key_file_get_string (kf, group, "Password", NULL); + if (password != NULL && password[0] != '\0') + soup_uri_set_password (self->uri, password); + + /* generate the signature URI too */ + url_asc = g_strdup_printf ("%s.asc", url); + self->uri_asc = fwupd_remote_build_uri (self, url_asc, error); + if (self->uri_asc == NULL) + return FALSE; + + /* generate some plausible local filenames */ + basename = g_path_get_basename (soup_uri_get_path (self->uri)); + self->filename = g_strdup_printf ("%s-%s", self->id, basename); + basename_asc = g_path_get_basename (soup_uri_get_path (self->uri_asc)); + self->filename_asc = g_strdup_printf ("%s-%s", self->id, basename_asc); + + /* success */ + return TRUE; +} + +const gchar * +fwupd_remote_get_filename (FwupdRemote *self) +{ + g_return_val_if_fail (FWUPD_IS_REMOTE (self), NULL); + return self->filename; +} + +const gchar * +fwupd_remote_get_filename_asc (FwupdRemote *self) +{ + g_return_val_if_fail (FWUPD_IS_REMOTE (self), NULL); + return self->filename_asc; +} + +/** + * fwupd_remote_build_uri: + * @self: A #FwupdRemote + * @url: the URL to use + * @error: the #GError, or %NULL + * + * Builds a URI for the URL using the username and password set for the remote. + * + * Returns: (transfer full): a #SoupURI, or %NULL for error + * + * Since: 0.9.3 + **/ +SoupURI * +fwupd_remote_build_uri (FwupdRemote *self, const gchar *url, GError **error) +{ + SoupURI *uri; + + g_return_val_if_fail (FWUPD_IS_REMOTE (self), NULL); + g_return_val_if_fail (url != NULL, NULL); + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + /* create URI */ + uri = soup_uri_new (url); + if (uri == NULL) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INVALID_FILE, + "Failed to parse URI '%s'", url); + return FALSE; + } + + /* set the username and password from the metadata URI */ + if (self->uri != NULL) { + soup_uri_set_user (uri, soup_uri_get_user (self->uri)); + soup_uri_set_password (uri, soup_uri_get_password (self->uri)); + } + return uri; +} + +/** + * fwupd_remote_get_uri: + * @self: A #FwupdRemote + * + * Gets the URI for the remote metadata. + * + * Returns: a #SoupURI, or %NULL for invalid. + * + * Since: 0.9.3 + **/ +SoupURI * +fwupd_remote_get_uri (FwupdRemote *self) +{ + g_return_val_if_fail (FWUPD_IS_REMOTE (self), NULL); + return self->uri; +} + +/** + * fwupd_remote_get_uri_asc: + * @self: A #FwupdRemote + * + * Gets the URI for the remote signature. + * + * Returns: a #SoupURI, or %NULL for invalid. + * + * Since: 0.9.3 + **/ +SoupURI * +fwupd_remote_get_uri_asc (FwupdRemote *self) +{ + g_return_val_if_fail (FWUPD_IS_REMOTE (self), NULL); + return self->uri_asc; +} + +/** + * fwupd_remote_get_enabled: + * @self: A #FwupdRemote + * + * Gets if the remote is enabled and should be used. + * + * Returns: a #TRUE if the remote is enabled + * + * Since: 0.9.3 + **/ +gboolean +fwupd_remote_get_enabled (FwupdRemote *self) +{ + g_return_val_if_fail (FWUPD_IS_REMOTE (self), FALSE); + return self->enabled; +} + +/** + * fwupd_remote_get_id: + * @self: A #FwupdRemote + * + * Gets the remote ID, e.g. "lvfs-testing". + * + * Returns: a string, or %NULL if unset + * + * Since: 0.9.3 + **/ +const gchar * +fwupd_remote_get_id (FwupdRemote *self) +{ + g_return_val_if_fail (FWUPD_IS_REMOTE (self), NULL); + return self->id; +} + +static void +fwupd_remote_get_property (GObject *obj, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + FwupdRemote *self = FWUPD_REMOTE (obj); + + switch (prop_id) { + case PROP_ENABLED: + g_value_set_boolean (value, self->enabled); + break; + case PROP_ID: + g_value_set_string (value, self->id); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); + break; + } +} + +static void +fwupd_remote_set_property (GObject *obj, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + FwupdRemote *self = FWUPD_REMOTE (obj); + + switch (prop_id) { + case PROP_ENABLED: + self->enabled = g_value_get_boolean (value); + break; + case PROP_ID: + self->id = g_value_get_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec); + break; + } +} + +static void +fwupd_remote_class_init (FwupdRemoteClass *klass) +{ + GParamSpec *pspec; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->finalize = fwupd_remote_finalize; + object_class->get_property = fwupd_remote_get_property; + object_class->set_property = fwupd_remote_set_property; + + /** + * FwupdRemote:id: + * + * The remote ID. + * + * Since: 0.9.3 + */ + pspec = g_param_spec_string ("id", NULL, NULL, + NULL, G_PARAM_READWRITE); + g_object_class_install_property (object_class, PROP_ID, pspec); + + /** + * FwupdRemote:enabled: + * + * If the remote is enabled and should be used. + * + * Since: 0.9.3 + */ + pspec = g_param_spec_boolean ("enabled", NULL, NULL, + FALSE, G_PARAM_READWRITE); + g_object_class_install_property (object_class, PROP_ENABLED, pspec); +} + +static void +fwupd_remote_init (FwupdRemote *self) +{ +} + +static void +fwupd_remote_finalize (GObject *obj) +{ + FwupdRemote *self = FWUPD_REMOTE (obj); + + g_free (self->id); + g_free (self->filename); + g_free (self->filename_asc); + if (self->uri != NULL) + soup_uri_free (self->uri); + if (self->uri_asc != NULL) + soup_uri_free (self->uri_asc); + + G_OBJECT_CLASS (fwupd_remote_parent_class)->finalize (obj); +} + +/** + * fwupd_remote_new: + * + * Creates a new fwupd remote. + * + * Returns: a new #FwupdRemote + * + * Since: 0.9.3 + **/ +FwupdRemote * +fwupd_remote_new (void) +{ + FwupdRemote *self; + self = g_object_new (FWUPD_TYPE_REMOTE, NULL); + return FWUPD_REMOTE (self); +} diff --git a/libfwupd/fwupd-remote.h b/libfwupd/fwupd-remote.h new file mode 100644 index 000000000..ad625d8af --- /dev/null +++ b/libfwupd/fwupd-remote.h @@ -0,0 +1,46 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2017 Richard Hughes + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __FWUPD_REMOTE_H +#define __FWUPD_REMOTE_H + +#include + +G_BEGIN_DECLS + +#define FWUPD_TYPE_REMOTE (fwupd_remote_get_type ()) +G_DECLARE_FINAL_TYPE (FwupdRemote, fwupd_remote, FWUPD, REMOTE, GObject) + +FwupdRemote *fwupd_remote_new (void); +const gchar *fwupd_remote_get_id (FwupdRemote *self); +const gchar *fwupd_remote_get_filename (FwupdRemote *self); +const gchar *fwupd_remote_get_filename_asc (FwupdRemote *self); +gboolean fwupd_remote_get_enabled (FwupdRemote *self); +SoupURI *fwupd_remote_get_uri (FwupdRemote *self); +SoupURI *fwupd_remote_get_uri_asc (FwupdRemote *self); +SoupURI *fwupd_remote_build_uri (FwupdRemote *self, + const gchar *url, + GError **error); + +G_END_DECLS + +#endif /* __FWUPD_REMOTE_H */ + diff --git a/libfwupd/fwupd-self-test.c b/libfwupd/fwupd-self-test.c index 23eddebf3..c2946c279 100644 --- a/libfwupd/fwupd-self-test.c +++ b/libfwupd/fwupd-self-test.c @@ -27,6 +27,7 @@ #include "fwupd-client.h" #include "fwupd-enums.h" #include "fwupd-error.h" +#include "fwupd-remote.h" #include "fwupd-result.h" static gboolean @@ -179,6 +180,53 @@ fwupd_client_devices_func (void) g_assert_cmpstr (fwupd_result_get_device_id (res), !=, NULL); } +static void +fwupd_client_remotes_func (void) +{ + FwupdRemote *remote; + g_autoptr(FwupdClient) client = NULL; + g_autoptr(FwupdRemote) remote2 = NULL; + g_autoptr(FwupdRemote) remote3 = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(GPtrArray) array = NULL; + + g_setenv ("FU_SELF_TEST_REMOTES_DIR", FU_SELF_TEST_REMOTES_DIR, TRUE); + + client = fwupd_client_new (); + array = fwupd_client_get_remotes (client, NULL, &error); + g_assert_no_error (error); + g_assert (array != NULL); + g_assert_cmpint (array->len, ==, 2); + + /* check remote */ + remote = g_ptr_array_index (array, 0); + g_assert (FWUPD_IS_REMOTE (remote)); + g_assert_cmpstr (fwupd_remote_get_id (remote), ==, "lvfs"); + g_assert (fwupd_remote_get_enabled (remote)); + g_assert (fwupd_remote_get_uri (remote) != NULL); + g_assert (fwupd_remote_get_uri_asc (remote) != NULL); + g_assert_cmpstr (fwupd_remote_get_filename (remote), ==, "lvfs-firmware.xml.gz"); + g_assert_cmpstr (fwupd_remote_get_filename_asc (remote), ==, "lvfs-firmware.xml.gz.asc"); + remote = g_ptr_array_index (array, 1); + g_assert_cmpstr (fwupd_remote_get_id (remote), ==, "lvfs-testing"); + g_assert (!fwupd_remote_get_enabled (remote)); + g_assert (fwupd_remote_get_uri (remote)!= NULL); + g_assert (fwupd_remote_get_uri_asc (remote)!= NULL); + + /* check we can find the right thing */ + remote2 = fwupd_client_get_remote_by_id (client, "lvfs", NULL, &error); + g_assert_no_error (error); + g_assert (remote2 != NULL); + g_assert_cmpstr (fwupd_remote_get_id (remote2), ==, "lvfs"); + g_assert (fwupd_remote_get_enabled (remote2)); + g_assert (fwupd_remote_get_uri (remote2) != NULL); + + /* check we set an error when unfound */ + remote3 = fwupd_client_get_remote_by_id (client, "XXXX", NULL, &error); + g_assert_error (error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND); + g_assert (remote3 == NULL); +} + static void fwupd_client_updates_func (void) { @@ -229,6 +277,7 @@ main (int argc, char **argv) g_test_add_func ("/fwupd/enums", fwupd_enums_func); g_test_add_func ("/fwupd/result", fwupd_result_func); if (fwupd_has_system_bus ()) { + g_test_add_func ("/fwupd/client{remotes}", fwupd_client_remotes_func); g_test_add_func ("/fwupd/client{devices}", fwupd_client_devices_func); g_test_add_func ("/fwupd/client{updates}", fwupd_client_updates_func); } diff --git a/libfwupd/fwupd.h b/libfwupd/fwupd.h index 74d9653be..2dc26f627 100644 --- a/libfwupd/fwupd.h +++ b/libfwupd/fwupd.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include diff --git a/libfwupd/meson.build b/libfwupd/meson.build index be4baa2e2..8a46a06a6 100644 --- a/libfwupd/meson.build +++ b/libfwupd/meson.build @@ -17,16 +17,13 @@ install_headers([ 'fwupd-client.h', 'fwupd-enums.h', 'fwupd-error.h', + 'fwupd-remote.h', 'fwupd-result.h', fwupd_version_h, ], subdir : 'fwupd-1/libfwupd', ) -deps = [ - giounix, -] - mapfile = 'fwupd.map' vflag = '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(), mapfile) fwupd = shared_library( @@ -35,11 +32,15 @@ fwupd = shared_library( 'fwupd-client.c', 'fwupd-enums.c', 'fwupd-error.c', + 'fwupd-remote.c', 'fwupd-result.c', ], soversion : lt_current, version : lt_version, - dependencies : deps, + dependencies : [ + giounix, + soup, + ], c_args : cargs, include_directories : include_directories('..'), link_args : vflag, @@ -50,7 +51,7 @@ fwupd = shared_library( pkgg = import('pkgconfig') pkgg.generate( libraries : fwupd, - requires : [ 'gio-2.0' ], + requires : [ 'gio-2.0', 'libsoup-2.4' ], subdirs : 'fwupd-1', version : meson.project_version(), name : 'fwupd', @@ -64,6 +65,7 @@ gnome.generate_gir(fwupd, 'fwupd-enums.c', 'fwupd-enums.h', 'fwupd-error.c', + 'fwupd-remote.c', 'fwupd-result.c', ], nsversion : '1.0', @@ -71,13 +73,17 @@ gnome.generate_gir(fwupd, symbol_prefix : 'fwupd', identifier_prefix : 'Fwupd', export_packages : 'fwupd', - dependencies : deps, + dependencies : [ + giounix, + soup, + ], includes : [ 'GObject-2.0', ], install : true ) +testdatadir = join_paths(meson.source_root(), 'data') e = executable( 'fwupd-self-test', sources : [ @@ -88,8 +94,12 @@ e = executable( ], dependencies : [ gio, + soup, ], link_with : fwupd, - c_args : cargs + c_args : [ + cargs, + '-DFU_SELF_TEST_REMOTES_DIR="' + testdatadir + '"', + ], ) test('fwupd-self-test', e) diff --git a/meson.build b/meson.build index 4d9470649..4110e15d8 100644 --- a/meson.build +++ b/meson.build @@ -204,6 +204,7 @@ plugin_deps += gio plugin_deps += giounix plugin_deps += gmodule plugin_deps += gusb +plugin_deps += soup subdir('data') subdir('docs') diff --git a/src/fu-util.c b/src/fu-util.c index 8c4cf5e0d..436c200d0 100644 --- a/src/fu-util.c +++ b/src/fu-util.c @@ -582,7 +582,7 @@ fu_util_verify_update (FuUtilPrivate *priv, gchar **values, GError **error) static gboolean fu_util_download_file (FuUtilPrivate *priv, - const gchar *uri, + SoupURI *uri, const gchar *fn, const gchar *checksum_expected, GChecksumType checksum_type, @@ -593,6 +593,7 @@ fu_util_download_file (FuUtilPrivate *priv, g_autoptr(GError) error_local = NULL; g_autofree gchar *checksum_actual = NULL; g_autofree gchar *user_agent = NULL; + g_autofree gchar *uri_str = NULL; g_autoptr(SoupMessage) msg = NULL; g_autoptr(SoupSession) session = NULL; @@ -627,13 +628,14 @@ fu_util_download_file (FuUtilPrivate *priv, soup_session_remove_feature_by_type (session, SOUP_TYPE_CONTENT_DECODER); /* download data */ - g_debug ("downloading %s to %s:", uri, fn); - msg = soup_message_new (SOUP_METHOD_GET, uri); + uri_str = soup_uri_to_string (uri, FALSE); + g_debug ("downloading %s to %s", uri_str, fn); + msg = soup_message_new_from_uri (SOUP_METHOD_GET, uri); if (msg == NULL) { g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE, - "Failed to parse URI %s", uri); + "Failed to parse URI %s", uri_str); return FALSE; } status_code = soup_session_send_message (session, msg); @@ -642,7 +644,7 @@ fu_util_download_file (FuUtilPrivate *priv, FWUPD_ERROR, FWUPD_ERROR_INVALID_FILE, "Failed to download %s: %s", - uri, soup_status_get_phrase (status_code)); + uri_str, soup_status_get_phrase (status_code)); return FALSE; } @@ -686,60 +688,53 @@ fu_util_mkdir_with_parents (const gchar *path, GError **error) } static gboolean -fu_util_download_metadata (FuUtilPrivate *priv, GError **error) +fu_util_download_metadata_for_remote (FuUtilPrivate *priv, + FwupdRemote *remote, + GError **error) { g_autofree gchar *cache_dir = NULL; - g_autofree gchar *config_fn = NULL; - g_autofree gchar *data_fn = NULL; - g_autofree gchar *data_uri = NULL; - g_autofree gchar *sig_fn = NULL; - g_autofree gchar *sig_uri = NULL; - g_autoptr(GKeyFile) config = NULL; - - /* read config file */ - config = g_key_file_new (); - config_fn = g_build_filename (SYSCONFDIR, "fwupd.conf", NULL); - if (!g_file_test (config_fn, G_FILE_TEST_EXISTS)) { - g_warning ("falling back to system config as %s missing", - config_fn); - g_free (config_fn); - config_fn = g_build_filename ("/etc", "fwupd.conf", NULL); - } - if (!g_key_file_load_from_file (config, config_fn, G_KEY_FILE_NONE, error)) { - g_prefix_error (error, "Failed to load %s: ", config_fn); - return FALSE; - } + g_autofree gchar *filename = NULL; + g_autofree gchar *filename_asc = NULL; /* ensure cache directory exists */ cache_dir = g_build_filename (g_get_user_cache_dir (), "fwupdmgr", NULL); if (!fu_util_mkdir_with_parents (cache_dir, error)) return FALSE; + /* download the metadata */ + filename = g_build_filename (cache_dir, fwupd_remote_get_filename (remote), NULL); + if (!fu_util_download_file (priv, fwupd_remote_get_uri (remote), + filename, NULL, 0, error)) + return FALSE; + /* download the signature */ - data_uri = g_key_file_get_string (config, "fwupd", "DownloadURI", error); - if (data_uri == NULL) - return FALSE; - if (data_uri[0] == '\0') { - g_set_error (error, - FWUPD_ERROR, - FWUPD_ERROR_NOT_SUPPORTED, - "Nothing set as DownloadURI in %s", - config_fn); - return FALSE; - - } - sig_uri = g_strdup_printf ("%s.asc", data_uri); - data_fn = g_build_filename (cache_dir, "firmware.xml.gz", NULL); - sig_fn = g_strdup_printf ("%s.asc", data_fn); - if (!fu_util_download_file (priv, sig_uri, sig_fn, NULL, 0, error)) - return FALSE; - - /* download the payload */ - if (!fu_util_download_file (priv, data_uri, data_fn, NULL, 0, error)) + filename_asc = g_build_filename (cache_dir, fwupd_remote_get_filename_asc (remote), NULL); + if (!fu_util_download_file (priv, fwupd_remote_get_uri_asc (remote), + filename_asc, NULL, 0, error)) return FALSE; /* send all this to fwupd */ - return fwupd_client_update_metadata (priv->client, data_fn, sig_fn, NULL, error); + return fwupd_client_update_metadata (priv->client, + filename, + filename_asc, + NULL, error); +} + +static gboolean +fu_util_download_metadata (FuUtilPrivate *priv, GError **error) +{ + g_autoptr(GPtrArray) remotes = NULL; + remotes = fwupd_client_get_remotes (priv->client, NULL, error); + if (remotes == NULL) + return FALSE; + for (guint i = 0; i < remotes->len; i++) { + FwupdRemote *remote = g_ptr_array_index (remotes, i); + if (!fwupd_remote_get_enabled (remote)) + continue; + if (!fu_util_download_metadata_for_remote (priv, remote, error)) + return FALSE; + } + return TRUE; } static gboolean @@ -1024,9 +1019,10 @@ fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) for (guint i = 0; i < results->len; i++) { GChecksumType checksum_type; const gchar *checksum; - const gchar *uri; + const gchar *uri_tmp; g_autofree gchar *basename = NULL; g_autofree gchar *fn = NULL; + g_autoptr(SoupURI) uri = NULL; FwupdResult *res = g_ptr_array_index (results, i); @@ -1034,13 +1030,14 @@ fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) checksum = fwupd_result_get_update_checksum (res); if (checksum == NULL) continue; - uri = fwupd_result_get_update_uri (res); - if (uri == NULL) + uri_tmp = fwupd_result_get_update_uri (res); + if (uri_tmp == NULL) continue; + uri = soup_uri_new (uri_tmp); g_print ("Downloading %s for %s...\n", fwupd_result_get_update_version (res), fwupd_result_get_device_name (res)); - basename = g_path_get_basename (uri); + basename = g_path_get_basename (uri_tmp); fn = g_build_filename (g_get_tmp_dir (), basename, NULL); checksum_type = fwupd_result_get_update_checksum_kind (res); if (!fu_util_download_file (priv, uri, fn, checksum, checksum_type, error))