2016-07-07 05:34:55 +02:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright 2016 Red Hat, Inc.
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2016-07-07 05:34:55 +02:00
|
|
|
* 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
|
2017-05-27 18:21:30 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2016-07-07 05:34:55 +02:00
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2022-10-26 23:35:30 +02:00
|
|
|
#include "glib-private.h"
|
2016-07-07 05:34:55 +02:00
|
|
|
#include "gportalsupport.h"
|
2022-10-31 12:07:52 +01:00
|
|
|
#include "gsandbox.h"
|
2016-07-07 05:34:55 +02:00
|
|
|
|
2022-10-26 23:35:30 +02:00
|
|
|
static GSandboxType sandbox_type = G_SANDBOX_TYPE_UNKNOWN;
|
2016-07-07 05:34:55 +02:00
|
|
|
static gboolean use_portal;
|
|
|
|
static gboolean network_available;
|
2019-07-12 17:30:30 +02:00
|
|
|
static gboolean dconf_access;
|
2016-07-07 05:34:55 +02:00
|
|
|
|
2022-11-24 05:54:07 +01:00
|
|
|
#ifdef G_PORTAL_SUPPORT_TEST
|
|
|
|
static const char *snapctl = "snapctl";
|
|
|
|
#else
|
|
|
|
static const char *snapctl = "/usr/bin/snapctl";
|
|
|
|
#endif
|
|
|
|
|
2022-10-26 23:35:30 +02:00
|
|
|
static gboolean
|
|
|
|
snap_plug_is_connected (const gchar *plug_name)
|
|
|
|
{
|
|
|
|
gint wait_status;
|
2022-11-24 05:54:07 +01:00
|
|
|
const gchar *argv[] = { snapctl, "is-connected", plug_name, NULL };
|
2022-10-26 23:35:30 +02:00
|
|
|
|
|
|
|
/* Bail out if our process is privileged - we don't want to pass those
|
2022-11-24 05:54:07 +01:00
|
|
|
* privileges to snapctl. It could be overridden and this would
|
2022-10-26 23:35:30 +02:00
|
|
|
* allow arbitrary code execution.
|
|
|
|
*/
|
|
|
|
if (GLIB_PRIVATE_CALL (g_check_setuid) ())
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!g_spawn_sync (NULL, (gchar **) argv, NULL,
|
2022-11-24 05:54:07 +01:00
|
|
|
#ifdef G_PORTAL_SUPPORT_TEST
|
2022-10-26 23:35:30 +02:00
|
|
|
G_SPAWN_SEARCH_PATH |
|
2022-11-24 05:54:07 +01:00
|
|
|
#endif
|
2022-10-26 23:35:30 +02:00
|
|
|
G_SPAWN_STDOUT_TO_DEV_NULL |
|
|
|
|
G_SPAWN_STDERR_TO_DEV_NULL,
|
|
|
|
NULL, NULL, NULL, NULL, &wait_status,
|
|
|
|
NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return g_spawn_check_wait_status (wait_status, NULL);
|
|
|
|
}
|
|
|
|
|
2016-07-07 05:34:55 +02:00
|
|
|
static void
|
2022-10-27 05:34:21 +02:00
|
|
|
sandbox_info_read (void)
|
2016-07-07 05:34:55 +02:00
|
|
|
{
|
2022-10-26 23:35:30 +02:00
|
|
|
static gsize sandbox_info_is_read = 0;
|
2017-05-01 20:34:51 +02:00
|
|
|
|
2022-10-26 23:35:30 +02:00
|
|
|
/* Sandbox type and Flatpak info is static, so only read once */
|
|
|
|
if (!g_once_init_enter (&sandbox_info_is_read))
|
2016-07-07 05:34:55 +02:00
|
|
|
return;
|
|
|
|
|
2022-10-31 12:07:52 +01:00
|
|
|
sandbox_type = glib_get_sandbox_type ();
|
2022-10-26 23:35:30 +02:00
|
|
|
|
2022-10-31 12:07:52 +01:00
|
|
|
switch (sandbox_type)
|
2016-07-07 05:34:55 +02:00
|
|
|
{
|
2022-10-31 12:07:52 +01:00
|
|
|
case G_SANDBOX_TYPE_FLATPAK:
|
|
|
|
{
|
|
|
|
GKeyFile *keyfile;
|
2022-11-24 19:07:27 +01:00
|
|
|
const char *keyfile_path = "/.flatpak-info";
|
2016-07-07 05:34:55 +02:00
|
|
|
|
|
|
|
use_portal = TRUE;
|
2022-10-31 12:07:52 +01:00
|
|
|
network_available = FALSE;
|
|
|
|
dconf_access = FALSE;
|
|
|
|
|
|
|
|
keyfile = g_key_file_new ();
|
2022-11-24 19:07:27 +01:00
|
|
|
|
|
|
|
#ifdef G_PORTAL_SUPPORT_TEST
|
|
|
|
char *test_key_file =
|
|
|
|
g_build_filename (g_get_user_runtime_dir (), keyfile_path, NULL);
|
|
|
|
keyfile_path = test_key_file;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_key_file_load_from_file (keyfile, keyfile_path, G_KEY_FILE_NONE, NULL))
|
2022-10-31 12:07:52 +01:00
|
|
|
{
|
|
|
|
char **shared = NULL;
|
|
|
|
char *dconf_policy = NULL;
|
|
|
|
|
|
|
|
shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL);
|
|
|
|
if (shared)
|
|
|
|
{
|
2022-11-01 10:20:11 +01:00
|
|
|
network_available = g_strv_contains ((const char *const *) shared, "network");
|
2022-10-31 12:07:52 +01:00
|
|
|
g_strfreev (shared);
|
|
|
|
}
|
|
|
|
|
|
|
|
dconf_policy = g_key_file_get_string (keyfile, "Session Bus Policy", "ca.desrt.dconf", NULL);
|
|
|
|
if (dconf_policy)
|
|
|
|
{
|
|
|
|
if (strcmp (dconf_policy, "talk") == 0)
|
|
|
|
dconf_access = TRUE;
|
|
|
|
g_free (dconf_policy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 19:07:27 +01:00
|
|
|
#ifdef G_PORTAL_SUPPORT_TEST
|
|
|
|
g_clear_pointer (&test_key_file, g_free);
|
|
|
|
#endif
|
|
|
|
|
2022-10-31 12:07:52 +01:00
|
|
|
g_key_file_unref (keyfile);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case G_SANDBOX_TYPE_SNAP:
|
2022-10-26 23:35:30 +02:00
|
|
|
break;
|
|
|
|
case G_SANDBOX_TYPE_UNKNOWN:
|
2022-10-31 12:07:52 +01:00
|
|
|
{
|
|
|
|
const char *var;
|
|
|
|
|
2023-10-02 16:00:23 +02:00
|
|
|
var = g_getenv ("GIO_USE_PORTALS");
|
2022-10-31 12:07:52 +01:00
|
|
|
if (var && var[0] == '1')
|
|
|
|
use_portal = TRUE;
|
|
|
|
network_available = TRUE;
|
|
|
|
dconf_access = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-07 05:34:55 +02:00
|
|
|
}
|
2020-07-13 17:26:44 +02:00
|
|
|
|
2022-10-26 23:35:30 +02:00
|
|
|
g_once_init_leave (&sandbox_info_is_read, 1);
|
2016-07-07 05:34:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
glib_should_use_portal (void)
|
|
|
|
{
|
2022-10-27 05:34:21 +02:00
|
|
|
sandbox_info_read ();
|
2022-10-26 23:35:30 +02:00
|
|
|
|
|
|
|
if (sandbox_type == G_SANDBOX_TYPE_SNAP)
|
|
|
|
return snap_plug_is_connected ("desktop");
|
|
|
|
|
2016-07-07 05:34:55 +02:00
|
|
|
return use_portal;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
glib_network_available_in_sandbox (void)
|
|
|
|
{
|
2022-10-27 05:34:21 +02:00
|
|
|
sandbox_info_read ();
|
2022-10-26 23:35:30 +02:00
|
|
|
|
|
|
|
if (sandbox_type == G_SANDBOX_TYPE_SNAP)
|
|
|
|
{
|
|
|
|
/* FIXME: This is inefficient doing multiple calls to check connections.
|
|
|
|
* See https://github.com/snapcore/snapd/pull/12301 for a proposed
|
|
|
|
* improvement to snapd for this.
|
|
|
|
*/
|
|
|
|
return snap_plug_is_connected ("desktop") ||
|
|
|
|
snap_plug_is_connected ("network-status");
|
|
|
|
}
|
|
|
|
|
2016-07-07 05:34:55 +02:00
|
|
|
return network_available;
|
|
|
|
}
|
|
|
|
|
2019-07-12 17:30:30 +02:00
|
|
|
gboolean
|
|
|
|
glib_has_dconf_access_in_sandbox (void)
|
|
|
|
{
|
2022-10-27 05:34:21 +02:00
|
|
|
sandbox_info_read ();
|
2022-10-26 23:35:30 +02:00
|
|
|
|
|
|
|
if (sandbox_type == G_SANDBOX_TYPE_SNAP)
|
|
|
|
return snap_plug_is_connected ("gsettings");
|
|
|
|
|
2019-07-12 17:30:30 +02:00
|
|
|
return dconf_access;
|
|
|
|
}
|