Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
| 3 | * Copyright (C) 2017 Richard Hughes <richard@hughsie.com> |
| 4 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 5 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "config.h" |
| 9 | |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 10 | #include <appstream-glib.h> |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 11 | #include <glib-object.h> |
| 12 | #include <gio/gio.h> |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 13 | #include <glib/gi18n.h> |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 14 | |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame^] | 15 | #include "fu-common.h" |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 16 | #include "fu-config.h" |
| 17 | |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 18 | #include "fwupd-common.h" |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 19 | #include "fwupd-error.h" |
| 20 | #include "fwupd-remote-private.h" |
| 21 | |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 22 | static void fu_config_finalize (GObject *obj); |
| 23 | |
| 24 | struct _FuConfig |
| 25 | { |
| 26 | GObject parent_instance; |
| 27 | GKeyFile *keyfile; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 28 | GPtrArray *remotes; |
| 29 | GPtrArray *monitors; |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 30 | GPtrArray *blacklist_devices; |
| 31 | GPtrArray *blacklist_plugins; |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 32 | guint64 archive_size_max; |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 33 | AsStore *store_remotes; |
| 34 | GHashTable *os_release; |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | G_DEFINE_TYPE (FuConfig, fu_config, G_TYPE_OBJECT) |
| 38 | |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 39 | static GPtrArray * |
| 40 | fu_config_get_config_paths (void) |
| 41 | { |
| 42 | GPtrArray *paths = g_ptr_array_new_with_free_func (g_free); |
| 43 | const gchar *remotes_dir; |
| 44 | const gchar *system_prefixlibdir = "/usr/lib/fwupd"; |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame^] | 45 | g_autofree gchar *configdir = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 46 | |
| 47 | /* only set by the self test program */ |
| 48 | remotes_dir = g_getenv ("FU_SELF_TEST_REMOTES_DIR"); |
| 49 | if (remotes_dir != NULL) { |
| 50 | g_ptr_array_add (paths, g_strdup (remotes_dir)); |
| 51 | return paths; |
| 52 | } |
| 53 | |
| 54 | /* use sysconfig, and then fall back to /etc */ |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame^] | 55 | configdir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG); |
| 56 | if (g_file_test (configdir, G_FILE_TEST_EXISTS)) |
| 57 | g_ptr_array_add (paths, g_steal_pointer (&configdir)); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 58 | |
| 59 | /* add in system-wide locations */ |
| 60 | if (g_file_test (system_prefixlibdir, G_FILE_TEST_EXISTS)) |
| 61 | g_ptr_array_add (paths, g_strdup (system_prefixlibdir)); |
| 62 | |
| 63 | return paths; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | fu_config_monitor_changed_cb (GFileMonitor *monitor, |
| 68 | GFile *file, |
| 69 | GFile *other_file, |
| 70 | GFileMonitorEvent event_type, |
| 71 | gpointer user_data) |
| 72 | { |
| 73 | FuConfig *self = FU_CONFIG (user_data); |
| 74 | g_autoptr(GError) error = NULL; |
| 75 | g_autofree gchar *filename = g_file_get_path (file); |
| 76 | g_debug ("%s changed, reloading all configs", filename); |
| 77 | if (!fu_config_load (self, &error)) |
| 78 | g_warning ("failed to rescan config: %s", error->message); |
| 79 | } |
| 80 | |
Richard Hughes | feb8027 | 2017-06-16 20:24:24 +0100 | [diff] [blame] | 81 | static guint64 |
| 82 | fu_config_get_remote_mtime (FuConfig *self, FwupdRemote *remote) |
| 83 | { |
Richard Hughes | feb8027 | 2017-06-16 20:24:24 +0100 | [diff] [blame] | 84 | g_autoptr(GFile) file = NULL; |
| 85 | g_autoptr(GFileInfo) info = NULL; |
| 86 | |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 87 | file = g_file_new_for_path (fwupd_remote_get_filename_cache (remote)); |
Richard Hughes | feb8027 | 2017-06-16 20:24:24 +0100 | [diff] [blame] | 88 | if (!g_file_query_exists (file, NULL)) |
| 89 | return G_MAXUINT64; |
| 90 | info = g_file_query_info (file, |
| 91 | G_FILE_ATTRIBUTE_TIME_MODIFIED, |
| 92 | G_FILE_QUERY_INFO_NONE, |
| 93 | NULL, NULL); |
| 94 | if (info == NULL) |
| 95 | return G_MAXUINT64; |
| 96 | return g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED); |
| 97 | } |
| 98 | |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 99 | static gboolean |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 100 | fu_config_add_inotify (FuConfig *self, const gchar *filename, GError **error) |
| 101 | { |
| 102 | GFileMonitor *monitor; |
| 103 | g_autoptr(GFile) file = g_file_new_for_path (filename); |
| 104 | |
| 105 | /* set up a notify watch */ |
| 106 | monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, error); |
| 107 | if (monitor == NULL) |
| 108 | return FALSE; |
| 109 | g_signal_connect (monitor, "changed", |
| 110 | G_CALLBACK (fu_config_monitor_changed_cb), self); |
| 111 | g_ptr_array_add (self->monitors, monitor); |
| 112 | return TRUE; |
| 113 | } |
| 114 | |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 115 | static GString * |
| 116 | fu_config_get_remote_agreement_default (FwupdRemote *self, GError **error) |
| 117 | { |
| 118 | GString *str = g_string_new (NULL); |
| 119 | |
| 120 | /* this is designed as a fallback; the actual warning should ideally |
| 121 | * come from the LVFS instance that is serving the remote */ |
| 122 | g_string_append_printf (str, "%s\n", |
| 123 | /* TRANSLATORS: show the user a warning */ |
| 124 | _("Your distributor may not have verified any of " |
| 125 | "the firmware updates for compatibility with your " |
| 126 | "system or connected devices.")); |
| 127 | g_string_append_printf (str, "%s\n", |
| 128 | /* TRANSLATORS: show the user a warning */ |
| 129 | _("Enabling this remote is done at your own risk.")); |
| 130 | return str; |
| 131 | } |
| 132 | |
| 133 | static GString * |
| 134 | fu_config_get_remote_agreement_for_app (FwupdRemote *self, AsApp *app, GError **error) |
| 135 | { |
| 136 | #if AS_CHECK_VERSION(0,7,8) |
| 137 | const gchar *tmp = NULL; |
| 138 | AsAgreement *agreement; |
| 139 | AsAgreementSection *section; |
| 140 | |
| 141 | /* get the default agreement section */ |
| 142 | agreement = as_app_get_agreement_default (app); |
| 143 | if (agreement == NULL) { |
| 144 | g_set_error_literal (error, |
| 145 | FWUPD_ERROR, |
| 146 | FWUPD_ERROR_NOT_FOUND, |
| 147 | "No agreement found"); |
| 148 | return NULL; |
| 149 | } |
| 150 | section = as_agreement_get_section_default (agreement); |
| 151 | if (section == NULL) { |
| 152 | g_set_error_literal (error, |
| 153 | FWUPD_ERROR, |
| 154 | FWUPD_ERROR_NOT_FOUND, |
| 155 | "No default section for agreement found"); |
| 156 | return NULL; |
| 157 | } |
| 158 | tmp = as_agreement_section_get_description (section, NULL); |
| 159 | if (tmp == NULL) { |
| 160 | g_set_error_literal (error, |
| 161 | FWUPD_ERROR, |
| 162 | FWUPD_ERROR_NOT_FOUND, |
| 163 | "No description found in agreement section"); |
| 164 | return NULL; |
| 165 | } |
| 166 | return g_string_new (tmp); |
| 167 | #else |
| 168 | AsFormat *format; |
| 169 | GNode *n; |
| 170 | g_autoptr(AsNode) root = NULL; |
| 171 | g_autoptr(GFile) file = NULL; |
| 172 | |
| 173 | /* parse the XML file */ |
| 174 | format = as_app_get_format_default (app); |
| 175 | if (format == NULL) { |
| 176 | g_set_error_literal (error, |
| 177 | FWUPD_ERROR, |
| 178 | FWUPD_ERROR_NOT_FOUND, |
| 179 | "No format for Metainfo file"); |
| 180 | return NULL; |
| 181 | } |
| 182 | file = g_file_new_for_path (as_format_get_filename (format)); |
| 183 | root = as_node_from_file (file, AS_NODE_FROM_XML_FLAG_NONE, NULL, error); |
| 184 | if (root == NULL) |
| 185 | return NULL; |
| 186 | |
| 187 | /* manually find the first agreement section */ |
| 188 | n = as_node_find (root, "component/agreement/agreement_section/description"); |
| 189 | if (n == NULL) { |
| 190 | g_set_error_literal (error, |
| 191 | FWUPD_ERROR, |
| 192 | FWUPD_ERROR_NOT_FOUND, |
| 193 | "No agreement description found"); |
| 194 | return NULL; |
| 195 | } |
| 196 | return as_node_to_xml (n->children, AS_NODE_TO_XML_FLAG_INCLUDE_SIBLINGS); |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | static gchar * |
| 201 | fu_config_build_remote_component_id (FwupdRemote *remote) |
| 202 | { |
| 203 | return g_strdup_printf ("org.freedesktop.fwupd.remotes.%s", |
| 204 | fwupd_remote_get_id (remote)); |
| 205 | } |
| 206 | |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 207 | static gboolean |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 208 | fu_config_add_remotes_for_path (FuConfig *self, const gchar *path, GError **error) |
| 209 | { |
| 210 | const gchar *tmp; |
| 211 | g_autofree gchar *path_remotes = NULL; |
| 212 | g_autoptr(GDir) dir = NULL; |
| 213 | |
| 214 | path_remotes = g_build_filename (path, "remotes.d", NULL); |
| 215 | if (!g_file_test (path_remotes, G_FILE_TEST_EXISTS)) |
| 216 | return TRUE; |
Richard Hughes | e7b902e | 2018-03-05 16:50:35 +0000 | [diff] [blame] | 217 | if (!fu_config_add_inotify (self, path_remotes, error)) |
| 218 | return FALSE; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 219 | dir = g_dir_open (path_remotes, 0, error); |
| 220 | if (dir == NULL) |
| 221 | return FALSE; |
| 222 | while ((tmp = g_dir_read_name (dir)) != NULL) { |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 223 | g_autofree gchar *filename = g_build_filename (path_remotes, tmp, NULL); |
| 224 | g_autoptr(FwupdRemote) remote = fwupd_remote_new (); |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 225 | |
Richard Hughes | 49938e6 | 2017-09-16 13:24:41 +0100 | [diff] [blame] | 226 | /* skip invalid files */ |
| 227 | if (!g_str_has_suffix (tmp, ".conf")) { |
| 228 | g_debug ("skipping invalid file %s", filename); |
| 229 | continue; |
| 230 | } |
| 231 | |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 232 | /* load from keyfile */ |
Richard Hughes | 49938e6 | 2017-09-16 13:24:41 +0100 | [diff] [blame] | 233 | g_debug ("loading config from %s", filename); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 234 | if (!fwupd_remote_load_from_filename (remote, filename, |
Richard Hughes | 49938e6 | 2017-09-16 13:24:41 +0100 | [diff] [blame] | 235 | NULL, error)) { |
| 236 | g_prefix_error (error, "failed to load %s: ", filename); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 237 | return FALSE; |
Richard Hughes | 49938e6 | 2017-09-16 13:24:41 +0100 | [diff] [blame] | 238 | } |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 239 | |
| 240 | /* watch the config file and the XML file itself */ |
| 241 | if (!fu_config_add_inotify (self, filename, error)) |
| 242 | return FALSE; |
| 243 | if (!fu_config_add_inotify (self, fwupd_remote_get_filename_cache (remote), error)) |
| 244 | return FALSE; |
| 245 | |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 246 | /* try to find a custom agreement, falling back to a generic warning */ |
| 247 | if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_DOWNLOAD) { |
| 248 | g_autoptr(GString) agreement_markup = NULL; |
| 249 | g_autofree gchar *component_id = fu_config_build_remote_component_id (remote); |
| 250 | AsApp *app = as_store_get_app_by_id (self->store_remotes, component_id); |
| 251 | if (app != NULL) { |
| 252 | agreement_markup = fu_config_get_remote_agreement_for_app (remote, app, error); |
| 253 | } else { |
| 254 | agreement_markup = fu_config_get_remote_agreement_default (remote, error); |
| 255 | } |
| 256 | if (agreement_markup == NULL) |
| 257 | return FALSE; |
| 258 | |
| 259 | /* replace any dynamic values from os-release */ |
Richard Hughes | f674bc9 | 2018-05-30 10:32:21 +0100 | [diff] [blame] | 260 | tmp = g_hash_table_lookup (self->os_release, "NAME"); |
| 261 | if (tmp == NULL) |
| 262 | tmp = "this distribution"; |
| 263 | as_utils_string_replace (agreement_markup, "$OS_RELEASE:NAME$", tmp); |
| 264 | tmp = g_hash_table_lookup (self->os_release, "BUG_REPORT_URL"); |
| 265 | if (tmp == NULL) |
| 266 | tmp = "https://github.com/hughsie/fwupd/issues"; |
| 267 | as_utils_string_replace (agreement_markup, "$OS_RELEASE:BUG_REPORT_URL$", tmp); |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 268 | fwupd_remote_set_agreement (remote, agreement_markup->str); |
| 269 | } |
| 270 | |
Richard Hughes | cda1cdf | 2017-06-16 21:49:35 +0100 | [diff] [blame] | 271 | /* set mtime */ |
Richard Hughes | feb8027 | 2017-06-16 20:24:24 +0100 | [diff] [blame] | 272 | fwupd_remote_set_mtime (remote, fu_config_get_remote_mtime (self, remote)); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 273 | g_ptr_array_add (self->remotes, g_steal_pointer (&remote)); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 274 | } |
| 275 | return TRUE; |
| 276 | } |
| 277 | |
| 278 | static gint |
| 279 | fu_config_remote_sort_cb (gconstpointer a, gconstpointer b) |
| 280 | { |
| 281 | FwupdRemote *remote_a = *((FwupdRemote **) a); |
| 282 | FwupdRemote *remote_b = *((FwupdRemote **) b); |
| 283 | |
| 284 | /* use priority first */ |
| 285 | if (fwupd_remote_get_priority (remote_a) < fwupd_remote_get_priority (remote_b)) |
| 286 | return 1; |
| 287 | if (fwupd_remote_get_priority (remote_a) > fwupd_remote_get_priority (remote_b)) |
| 288 | return -1; |
| 289 | |
| 290 | /* fall back to name */ |
| 291 | return g_strcmp0 (fwupd_remote_get_id (remote_a), |
| 292 | fwupd_remote_get_id (remote_b)); |
| 293 | } |
| 294 | |
| 295 | static FwupdRemote * |
| 296 | fu_config_get_remote_by_id_noref (GPtrArray *remotes, const gchar *remote_id) |
| 297 | { |
| 298 | for (guint i = 0; i < remotes->len; i++) { |
| 299 | FwupdRemote *remote = g_ptr_array_index (remotes, i); |
| 300 | if (g_strcmp0 (remote_id, fwupd_remote_get_id (remote)) == 0) |
| 301 | return remote; |
| 302 | } |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | static guint |
| 307 | fu_config_remotes_depsolve_with_direction (FuConfig *self, gint inc) |
| 308 | { |
| 309 | guint cnt = 0; |
| 310 | for (guint i = 0; i < self->remotes->len; i++) { |
| 311 | FwupdRemote *remote = g_ptr_array_index (self->remotes, i); |
| 312 | gchar **order = inc < 0 ? fwupd_remote_get_order_after (remote) : |
| 313 | fwupd_remote_get_order_before (remote); |
| 314 | if (order == NULL) |
| 315 | continue; |
| 316 | for (guint j = 0; order[j] != NULL; j++) { |
| 317 | FwupdRemote *remote2; |
| 318 | if (g_strcmp0 (order[j], fwupd_remote_get_id (remote)) == 0) { |
Richard Hughes | 3cca1c6 | 2017-07-21 13:38:20 +0100 | [diff] [blame] | 319 | g_debug ("ignoring self-dep remote %s", order[j]); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 320 | continue; |
| 321 | } |
| 322 | remote2 = fu_config_get_remote_by_id_noref (self->remotes, order[j]); |
| 323 | if (remote2 == NULL) { |
Richard Hughes | 3cca1c6 | 2017-07-21 13:38:20 +0100 | [diff] [blame] | 324 | g_debug ("ignoring unfound remote %s", order[j]); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | if (fwupd_remote_get_priority (remote) > fwupd_remote_get_priority (remote2)) |
| 328 | continue; |
| 329 | g_debug ("ordering %s=%s+%i", |
| 330 | fwupd_remote_get_id (remote), |
| 331 | fwupd_remote_get_id (remote2), |
| 332 | inc); |
| 333 | fwupd_remote_set_priority (remote, fwupd_remote_get_priority (remote2) + inc); |
| 334 | |
| 335 | /* increment changes counter */ |
| 336 | cnt++; |
| 337 | } |
| 338 | } |
| 339 | return cnt; |
| 340 | } |
| 341 | |
| 342 | static gboolean |
| 343 | fu_config_load_remotes (FuConfig *self, GError **error) |
| 344 | { |
| 345 | guint depsolve_check; |
| 346 | g_autoptr(GPtrArray) paths = NULL; |
| 347 | |
| 348 | /* get a list of all config paths */ |
| 349 | paths = fu_config_get_config_paths (); |
| 350 | if (paths->len == 0) { |
| 351 | g_set_error_literal (error, |
| 352 | FWUPD_ERROR, |
| 353 | FWUPD_ERROR_NOT_FOUND, |
| 354 | "No search paths found"); |
| 355 | return FALSE; |
| 356 | } |
| 357 | |
| 358 | /* look for all remotes */ |
| 359 | for (guint i = 0; i < paths->len; i++) { |
| 360 | const gchar *path = g_ptr_array_index (paths, i); |
| 361 | g_debug ("using config path of %s", path); |
| 362 | if (!fu_config_add_remotes_for_path (self, path, error)) |
| 363 | return FALSE; |
| 364 | } |
| 365 | |
| 366 | /* depsolve */ |
| 367 | for (depsolve_check = 0; depsolve_check < 100; depsolve_check++) { |
| 368 | guint cnt = 0; |
| 369 | cnt += fu_config_remotes_depsolve_with_direction (self, 1); |
| 370 | cnt += fu_config_remotes_depsolve_with_direction (self, -1); |
| 371 | if (cnt == 0) |
| 372 | break; |
| 373 | } |
| 374 | if (depsolve_check == 100) { |
| 375 | g_set_error_literal (error, |
| 376 | FWUPD_ERROR, |
| 377 | FWUPD_ERROR_INTERNAL, |
| 378 | "Cannot depsolve remotes ordering"); |
| 379 | return FALSE; |
| 380 | } |
| 381 | |
| 382 | /* order these by priority, then name */ |
| 383 | g_ptr_array_sort (self->remotes, fu_config_remote_sort_cb); |
| 384 | |
| 385 | /* success */ |
| 386 | return TRUE; |
| 387 | } |
| 388 | |
Mario Limonciello | d82e3b5 | 2018-05-22 10:18:27 -0500 | [diff] [blame] | 389 | static gboolean |
| 390 | fu_config_load_from_file (FuConfig *self, const gchar *config_file, |
| 391 | GError **error) |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 392 | { |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 393 | GFileMonitor *monitor; |
Richard Hughes | 321f77a | 2018-02-14 10:25:53 +0000 | [diff] [blame] | 394 | guint64 archive_size_max; |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 395 | g_auto(GStrv) devices = NULL; |
| 396 | g_auto(GStrv) plugins = NULL; |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 397 | g_autoptr(GFile) file = NULL; |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 398 | |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 399 | /* ensure empty in case we're called from a monitor change */ |
| 400 | g_ptr_array_set_size (self->blacklist_devices, 0); |
| 401 | g_ptr_array_set_size (self->blacklist_plugins, 0); |
| 402 | g_ptr_array_set_size (self->monitors, 0); |
| 403 | g_ptr_array_set_size (self->remotes, 0); |
| 404 | |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 405 | g_debug ("loading config values from %s", config_file); |
| 406 | if (!g_key_file_load_from_file (self->keyfile, config_file, |
| 407 | G_KEY_FILE_NONE, error)) |
| 408 | return FALSE; |
| 409 | |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 410 | /* set up a notify watch */ |
| 411 | file = g_file_new_for_path (config_file); |
| 412 | monitor = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, error); |
| 413 | if (monitor == NULL) |
| 414 | return FALSE; |
| 415 | g_signal_connect (monitor, "changed", |
| 416 | G_CALLBACK (fu_config_monitor_changed_cb), self); |
| 417 | g_ptr_array_add (self->monitors, monitor); |
| 418 | |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 419 | /* get blacklisted devices */ |
| 420 | devices = g_key_file_get_string_list (self->keyfile, |
| 421 | "fwupd", |
| 422 | "BlacklistDevices", |
| 423 | NULL, /* length */ |
| 424 | NULL); |
| 425 | if (devices != NULL) { |
| 426 | for (guint i = 0; devices[i] != NULL; i++) { |
| 427 | g_ptr_array_add (self->blacklist_devices, |
| 428 | g_strdup (devices[i])); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* get blacklisted plugins */ |
| 433 | plugins = g_key_file_get_string_list (self->keyfile, |
| 434 | "fwupd", |
| 435 | "BlacklistPlugins", |
| 436 | NULL, /* length */ |
| 437 | NULL); |
| 438 | if (plugins != NULL) { |
| 439 | for (guint i = 0; plugins[i] != NULL; i++) { |
| 440 | g_ptr_array_add (self->blacklist_plugins, |
| 441 | g_strdup (plugins[i])); |
| 442 | } |
| 443 | } |
| 444 | |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 445 | /* get maximum archive size, defaulting to something sane */ |
Richard Hughes | 321f77a | 2018-02-14 10:25:53 +0000 | [diff] [blame] | 446 | archive_size_max = g_key_file_get_uint64 (self->keyfile, |
| 447 | "fwupd", |
| 448 | "ArchiveSizeMax", |
| 449 | NULL); |
| 450 | if (archive_size_max > 0) |
| 451 | self->archive_size_max = archive_size_max *= 0x100000; |
Mario Limonciello | d82e3b5 | 2018-05-22 10:18:27 -0500 | [diff] [blame] | 452 | return TRUE; |
| 453 | } |
| 454 | |
| 455 | gboolean |
| 456 | fu_config_load (FuConfig *self, GError **error) |
| 457 | { |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame^] | 458 | g_autofree gchar *datadir = NULL; |
| 459 | g_autofree gchar *configdir = NULL; |
Mario Limonciello | d82e3b5 | 2018-05-22 10:18:27 -0500 | [diff] [blame] | 460 | g_autofree gchar *metainfo_path = NULL; |
| 461 | g_autofree gchar *config_file = NULL; |
| 462 | |
| 463 | g_return_val_if_fail (FU_IS_CONFIG (self), FALSE); |
| 464 | |
| 465 | /* load the main daemon config file */ |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame^] | 466 | configdir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG); |
| 467 | config_file = g_build_filename (configdir, "daemon.conf", NULL); |
Mario Limonciello | d82e3b5 | 2018-05-22 10:18:27 -0500 | [diff] [blame] | 468 | if (g_file_test (config_file, G_FILE_TEST_EXISTS)) { |
| 469 | if (!fu_config_load_from_file (self, config_file, error)) |
| 470 | return FALSE; |
| 471 | } else { |
| 472 | g_warning ("Daemon configuration %s not found", config_file); |
| 473 | } |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 474 | |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 475 | /* load AppStream about the remotes */ |
| 476 | self->os_release = fwupd_get_os_release (error); |
| 477 | if (self->os_release == NULL) |
| 478 | return FALSE; |
| 479 | as_store_add_filter (self->store_remotes, AS_APP_KIND_SOURCE); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame^] | 480 | datadir = fu_common_get_path (FU_PATH_KIND_DATADIR_PKG); |
| 481 | metainfo_path = g_build_filename (datadir, "metainfo", NULL); |
Richard Hughes | 9b7ad9a | 2018-05-15 21:51:30 +0100 | [diff] [blame] | 482 | if (g_file_test (metainfo_path, G_FILE_TEST_EXISTS)) { |
| 483 | if (!as_store_load_path (self->store_remotes, |
| 484 | metainfo_path, |
| 485 | NULL, /* cancellable */ |
| 486 | error)) |
| 487 | return FALSE; |
| 488 | } |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 489 | |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 490 | /* load remotes */ |
| 491 | if (!fu_config_load_remotes (self, error)) |
| 492 | return FALSE; |
| 493 | |
| 494 | /* success */ |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 495 | return TRUE; |
| 496 | } |
| 497 | |
| 498 | GPtrArray * |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 499 | fu_config_get_remotes (FuConfig *self) |
| 500 | { |
| 501 | g_return_val_if_fail (FU_IS_CONFIG (self), NULL); |
| 502 | return self->remotes; |
| 503 | } |
| 504 | |
Richard Hughes | 5935ebd | 2017-06-16 15:40:31 +0100 | [diff] [blame] | 505 | FwupdRemote * |
| 506 | fu_config_get_remote_by_id (FuConfig *self, const gchar *remote_id) |
| 507 | { |
| 508 | g_return_val_if_fail (FU_IS_CONFIG (self), NULL); |
| 509 | return fu_config_get_remote_by_id_noref (self->remotes, remote_id); |
| 510 | } |
| 511 | |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 512 | GPtrArray * |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 513 | fu_config_get_blacklist_devices (FuConfig *self) |
| 514 | { |
| 515 | g_return_val_if_fail (FU_IS_CONFIG (self), NULL); |
| 516 | return self->blacklist_devices; |
| 517 | } |
| 518 | |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 519 | guint64 |
| 520 | fu_config_get_archive_size_max (FuConfig *self) |
| 521 | { |
| 522 | g_return_val_if_fail (FU_IS_CONFIG (self), 0); |
| 523 | return self->archive_size_max; |
| 524 | } |
| 525 | |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 526 | GPtrArray * |
| 527 | fu_config_get_blacklist_plugins (FuConfig *self) |
| 528 | { |
| 529 | g_return_val_if_fail (FU_IS_CONFIG (self), NULL); |
| 530 | return self->blacklist_plugins; |
| 531 | } |
| 532 | |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 533 | static void |
| 534 | fu_config_class_init (FuConfigClass *klass) |
| 535 | { |
| 536 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 537 | object_class->finalize = fu_config_finalize; |
| 538 | } |
| 539 | |
| 540 | static void |
| 541 | fu_config_init (FuConfig *self) |
| 542 | { |
Richard Hughes | c7bbbc2 | 2018-01-02 22:22:25 +0000 | [diff] [blame] | 543 | self->archive_size_max = 512 * 0x100000; |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 544 | self->keyfile = g_key_file_new (); |
| 545 | self->blacklist_devices = g_ptr_array_new_with_free_func (g_free); |
| 546 | self->blacklist_plugins = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 547 | self->remotes = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 548 | self->monitors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 549 | self->store_remotes = as_store_new (); |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static void |
| 553 | fu_config_finalize (GObject *obj) |
| 554 | { |
| 555 | FuConfig *self = FU_CONFIG (obj); |
| 556 | |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 557 | if (self->os_release != NULL) |
| 558 | g_hash_table_unref (self->os_release); |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 559 | g_key_file_unref (self->keyfile); |
| 560 | g_ptr_array_unref (self->blacklist_devices); |
| 561 | g_ptr_array_unref (self->blacklist_plugins); |
Richard Hughes | 4c36970 | 2017-06-16 15:31:38 +0100 | [diff] [blame] | 562 | g_ptr_array_unref (self->remotes); |
| 563 | g_ptr_array_unref (self->monitors); |
Richard Hughes | 1fd3ecf | 2018-04-16 10:53:46 +0100 | [diff] [blame] | 564 | g_object_unref (self->store_remotes); |
Richard Hughes | 980ef14 | 2017-06-16 12:45:27 +0100 | [diff] [blame] | 565 | |
| 566 | G_OBJECT_CLASS (fu_config_parent_class)->finalize (obj); |
| 567 | } |
| 568 | |
| 569 | FuConfig * |
| 570 | fu_config_new (void) |
| 571 | { |
| 572 | FuConfig *self; |
| 573 | self = g_object_new (FU_TYPE_CONFIG, NULL); |
| 574 | return FU_CONFIG (self); |
| 575 | } |