summaryrefslogtreecommitdiff
path: root/gio/gportalsupport.c
blob: 7e1da227334ce1d7ce050f31a2dda35d78d3a361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright 2016 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "glib-private.h"
#include "gportalsupport.h"
#include "gsandbox.h"

static GSandboxType sandbox_type = G_SANDBOX_TYPE_UNKNOWN;
static gboolean use_portal;
static gboolean network_available;
static gboolean dconf_access;

#ifdef G_PORTAL_SUPPORT_TEST
static const char *snapctl = "snapctl";
#else
static const char *snapctl = "/usr/bin/snapctl";
#endif

static gboolean
snap_plug_is_connected (const gchar *plug_name)
{
  gint wait_status;
  const gchar *argv[] = { snapctl, "is-connected", plug_name, NULL };

  /* Bail out if our process is privileged - we don't want to pass those
   * privileges to snapctl. It could be overridden and this would
   * allow arbitrary code execution.
   */
  if (GLIB_PRIVATE_CALL (g_check_setuid) ())
    return FALSE;

  if (!g_spawn_sync (NULL, (gchar **) argv, NULL,
#ifdef G_PORTAL_SUPPORT_TEST
                     G_SPAWN_SEARCH_PATH |
#endif
                         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);
}

static void
sandbox_info_read (void)
{
  static gsize sandbox_info_is_read = 0;

  /* Sandbox type and Flatpak info is static, so only read once */
  if (!g_once_init_enter (&sandbox_info_is_read))
    return;

  sandbox_type = glib_get_sandbox_type ();

  switch (sandbox_type)
    {
    case G_SANDBOX_TYPE_FLATPAK:
      {
        GKeyFile *keyfile;
        const char *keyfile_path = "/.flatpak-info";

        use_portal = TRUE;
        network_available = FALSE;
        dconf_access = FALSE;

        keyfile = g_key_file_new ();

#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))
          {
            char **shared = NULL;
            char *dconf_policy = NULL;

            shared = g_key_file_get_string_list (keyfile, "Context", "shared", NULL, NULL);
            if (shared)
              {
                network_available = g_strv_contains ((const char *const *) shared, "network");
                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);
              }
          }

#ifdef G_PORTAL_SUPPORT_TEST
        g_clear_pointer (&test_key_file, g_free);
#endif

        g_key_file_unref (keyfile);
      }
      break;
    case G_SANDBOX_TYPE_SNAP:
      break;
    case G_SANDBOX_TYPE_UNKNOWN:
      {
        const char *var;

        var = g_getenv ("GTK_USE_PORTAL");
        if (var && var[0] == '1')
          use_portal = TRUE;
        network_available = TRUE;
        dconf_access = TRUE;
      }
      break;
    }

  g_once_init_leave (&sandbox_info_is_read, 1);
}

gboolean
glib_should_use_portal (void)
{
  sandbox_info_read ();

  if (sandbox_type == G_SANDBOX_TYPE_SNAP)
    return snap_plug_is_connected ("desktop");

  return use_portal;
}

gboolean
glib_network_available_in_sandbox (void)
{
  sandbox_info_read ();

  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");
    }

  return network_available;
}

gboolean
glib_has_dconf_access_in_sandbox (void)
{
  sandbox_info_read ();

  if (sandbox_type == G_SANDBOX_TYPE_SNAP)
    return snap_plug_is_connected ("gsettings");

  return dconf_access;
}