Coverage Report

Created: 2025-07-18 06:10

/src/tinysparql/subprojects/glib-2.80.3/gio/gdbusaddress.c
Line
Count
Source (jump to first uncovered line)
1
/* GDBus - GLib D-Bus Library
2
 *
3
 * Copyright (C) 2008-2010 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library 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 GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: David Zeuthen <davidz@redhat.com>
21
 */
22
23
#include "config.h"
24
25
#include <stdlib.h>
26
#include <string.h>
27
#include <stdio.h>
28
#include <errno.h>
29
30
#include "gioerror.h"
31
#include "gdbusutils.h"
32
#include "gdbusaddress.h"
33
#include "gdbuserror.h"
34
#include "gioenumtypes.h"
35
#include "glib-private.h"
36
#include "gnetworkaddress.h"
37
#include "gsocketclient.h"
38
#include "giostream.h"
39
#include "gasyncresult.h"
40
#include "gtask.h"
41
#include "glib-private.h"
42
#include "gdbusprivate.h"
43
#include "gstdio.h"
44
45
#ifdef HAVE_UNISTD_H
46
#include <unistd.h>
47
#endif
48
#include <sys/stat.h>
49
#include <sys/types.h>
50
#include <gio/gunixsocketaddress.h>
51
52
#ifdef G_OS_WIN32
53
#include <windows.h>
54
#endif
55
56
#ifdef G_OS_WIN32
57
#define FO_CLOEXEC ""
58
#else
59
0
#define FO_CLOEXEC "e"
60
#endif
61
62
#include "glibintl.h"
63
64
/**
65
 * GDBusAddress:
66
 *
67
 * Routines for working with D-Bus addresses. A D-Bus address is a string
68
 * like `unix:tmpdir=/tmp/my-app-name`. The exact format of addresses
69
 * is explained in detail in the
70
 * [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
71
 *
72
 * TCP D-Bus connections are supported, but accessing them via a proxy is
73
 * currently not supported.
74
 *
75
 * Since GLib 2.72, `unix:` addresses are supported on Windows with `AF_UNIX`
76
 * support (Windows 10).
77
 */
78
79
static gchar *get_session_address_platform_specific (GError **error);
80
static gchar *get_session_address_dbus_launch       (GError **error);
81
82
/* ---------------------------------------------------------------------------------------------------- */
83
84
/**
85
 * g_dbus_is_address:
86
 * @string: A string.
87
 *
88
 * Checks if @string is a
89
 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
90
 *
91
 * This doesn't check if @string is actually supported by #GDBusServer
92
 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
93
 * checks.
94
 *
95
 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
96
 *
97
 * Since: 2.26
98
 */
99
gboolean
100
g_dbus_is_address (const gchar *string)
101
0
{
102
0
  guint n;
103
0
  gchar **a;
104
0
  gboolean ret;
105
106
0
  ret = FALSE;
107
108
0
  g_return_val_if_fail (string != NULL, FALSE);
109
110
0
  a = g_strsplit (string, ";", 0);
111
0
  if (a[0] == NULL)
112
0
    goto out;
113
114
0
  for (n = 0; a[n] != NULL; n++)
115
0
    {
116
0
      if (!_g_dbus_address_parse_entry (a[n],
117
0
                                        NULL,
118
0
                                        NULL,
119
0
                                        NULL))
120
0
        goto out;
121
0
    }
122
123
0
  ret = TRUE;
124
125
0
 out:
126
0
  g_strfreev (a);
127
0
  return ret;
128
0
}
129
130
static gboolean
131
is_valid_unix (const gchar  *address_entry,
132
               GHashTable   *key_value_pairs,
133
               GError      **error)
134
0
{
135
0
  gboolean ret;
136
0
  GPtrArray *keys;
137
0
  const gchar *path;
138
0
  const gchar *dir;
139
0
  const gchar *tmpdir;
140
0
  const gchar *abstract;
141
142
0
  ret = FALSE;
143
0
  path = NULL;
144
0
  dir = NULL;
145
0
  tmpdir = NULL;
146
0
  abstract = NULL;
147
148
0
  keys = g_hash_table_get_keys_as_ptr_array (key_value_pairs);
149
0
  for (guint i = 0; i < keys->len; ++i)
150
0
    {
151
0
      const gchar *key = g_ptr_array_index (keys, i);
152
0
      if (g_strcmp0 (key, "path") == 0)
153
0
        path = g_hash_table_lookup (key_value_pairs, key);
154
0
      else if (g_strcmp0 (key, "dir") == 0)
155
0
        dir = g_hash_table_lookup (key_value_pairs, key);
156
0
      else if (g_strcmp0 (key, "tmpdir") == 0)
157
0
        tmpdir = g_hash_table_lookup (key_value_pairs, key);
158
0
      else if (g_strcmp0 (key, "abstract") == 0)
159
0
        abstract = g_hash_table_lookup (key_value_pairs, key);
160
0
      else if (g_strcmp0 (key, "guid") != 0)
161
0
        {
162
0
          g_set_error (error,
163
0
                       G_IO_ERROR,
164
0
                       G_IO_ERROR_INVALID_ARGUMENT,
165
0
                       _("Unsupported key “%s” in address entry “%s”"),
166
0
                       key,
167
0
                       address_entry);
168
0
          goto out;
169
0
        }
170
0
    }
171
172
  /* Exactly one key must be set */
173
0
  if ((path != NULL) + (dir != NULL) + (tmpdir != NULL) + (abstract != NULL) > 1)
174
0
    {
175
0
      g_set_error (error,
176
0
             G_IO_ERROR,
177
0
             G_IO_ERROR_INVALID_ARGUMENT,
178
0
             _("Meaningless key/value pair combination in address entry “%s”"),
179
0
             address_entry);
180
0
      goto out;
181
0
    }
182
0
  else if (path == NULL && dir == NULL && tmpdir == NULL && abstract == NULL)
183
0
    {
184
0
      g_set_error (error,
185
0
                   G_IO_ERROR,
186
0
                   G_IO_ERROR_INVALID_ARGUMENT,
187
0
                   _("Address “%s” is invalid (need exactly one of path, dir, tmpdir, or abstract keys)"),
188
0
                   address_entry);
189
0
      goto out;
190
0
    }
191
192
0
  ret = TRUE;
193
194
0
 out:
195
0
  g_ptr_array_unref (keys);
196
197
0
  return ret;
198
0
}
199
200
static gboolean
201
is_valid_nonce_tcp (const gchar  *address_entry,
202
                    GHashTable   *key_value_pairs,
203
                    GError      **error)
204
0
{
205
0
  gboolean ret;
206
0
  GPtrArray *keys;
207
0
  const gchar *host;
208
0
  const gchar *port;
209
0
  const gchar *family;
210
0
  const gchar *nonce_file;
211
0
  gint port_num;
212
0
  gchar *endp;
213
214
0
  ret = FALSE;
215
0
  host = NULL;
216
0
  port = NULL;
217
0
  family = NULL;
218
0
  nonce_file = NULL;
219
220
0
  keys = g_hash_table_get_keys_as_ptr_array (key_value_pairs);
221
0
  for (guint i = 0; i < keys->len; ++i)
222
0
    {
223
0
      const gchar *key = g_ptr_array_index (keys, i);
224
0
      if (g_strcmp0 (key, "host") == 0)
225
0
        host = g_hash_table_lookup (key_value_pairs, key);
226
0
      else if (g_strcmp0 (key, "port") == 0)
227
0
        port = g_hash_table_lookup (key_value_pairs, key);
228
0
      else if (g_strcmp0 (key, "family") == 0)
229
0
        family = g_hash_table_lookup (key_value_pairs, key);
230
0
      else if (g_strcmp0 (key, "noncefile") == 0)
231
0
        nonce_file = g_hash_table_lookup (key_value_pairs, key);
232
0
      else if (g_strcmp0 (key, "guid") != 0)
233
0
        {
234
0
          g_set_error (error,
235
0
                       G_IO_ERROR,
236
0
                       G_IO_ERROR_INVALID_ARGUMENT,
237
0
                       _("Unsupported key “%s” in address entry “%s”"),
238
0
                       key,
239
0
                       address_entry);
240
0
          goto out;
241
0
        }
242
0
    }
243
244
0
  if (port != NULL)
245
0
    {
246
0
      port_num = strtol (port, &endp, 10);
247
0
      if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
248
0
        {
249
0
          g_set_error (error,
250
0
                       G_IO_ERROR,
251
0
                       G_IO_ERROR_INVALID_ARGUMENT,
252
0
                       _("Error in address “%s” — the “%s” attribute is malformed"),
253
0
                       address_entry, "port");
254
0
          goto out;
255
0
        }
256
0
    }
257
258
0
  if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
259
0
    {
260
0
      g_set_error (error,
261
0
                   G_IO_ERROR,
262
0
                   G_IO_ERROR_INVALID_ARGUMENT,
263
0
                   _("Error in address “%s” — the “%s” attribute is malformed"),
264
0
                   address_entry, "family");
265
0
      goto out;
266
0
    }
267
268
0
  if (host != NULL)
269
0
    {
270
      /* TODO: validate host */
271
0
    }
272
273
0
  if (nonce_file != NULL && *nonce_file == '\0')
274
0
    {
275
0
      g_set_error (error,
276
0
                   G_IO_ERROR,
277
0
                   G_IO_ERROR_INVALID_ARGUMENT,
278
0
                   _("Error in address “%s” — the “%s” attribute is malformed"),
279
0
                   address_entry, "noncefile");
280
0
      goto out;
281
0
    }
282
283
0
  ret = TRUE;
284
285
0
 out:
286
0
  g_ptr_array_unref (keys);
287
288
0
  return ret;
289
0
}
290
291
static gboolean
292
is_valid_tcp (const gchar  *address_entry,
293
              GHashTable   *key_value_pairs,
294
              GError      **error)
295
0
{
296
0
  gboolean ret;
297
0
  GPtrArray *keys;
298
0
  const gchar *host;
299
0
  const gchar *port;
300
0
  const gchar *family;
301
0
  gint port_num;
302
0
  gchar *endp;
303
304
0
  ret = FALSE;
305
0
  host = NULL;
306
0
  port = NULL;
307
0
  family = NULL;
308
309
0
  keys = g_hash_table_get_keys_as_ptr_array (key_value_pairs);
310
0
  for (guint i = 0; i < keys->len; ++i)
311
0
    {
312
0
      const gchar *key = g_ptr_array_index (keys, i);
313
0
      if (g_strcmp0 (key, "host") == 0)
314
0
        host = g_hash_table_lookup (key_value_pairs, key);
315
0
      else if (g_strcmp0 (key, "port") == 0)
316
0
        port = g_hash_table_lookup (key_value_pairs, key);
317
0
      else if (g_strcmp0 (key, "family") == 0)
318
0
        family = g_hash_table_lookup (key_value_pairs, key);
319
0
      else if (g_strcmp0 (key, "guid") != 0)
320
0
        {
321
0
          g_set_error (error,
322
0
                       G_IO_ERROR,
323
0
                       G_IO_ERROR_INVALID_ARGUMENT,
324
0
                       _("Unsupported key “%s” in address entry “%s”"),
325
0
                       key,
326
0
                       address_entry);
327
0
          goto out;
328
0
        }
329
0
    }
330
331
0
  if (port != NULL)
332
0
    {
333
0
      port_num = strtol (port, &endp, 10);
334
0
      if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
335
0
        {
336
0
          g_set_error (error,
337
0
                       G_IO_ERROR,
338
0
                       G_IO_ERROR_INVALID_ARGUMENT,
339
0
                       _("Error in address “%s” — the “%s” attribute is malformed"),
340
0
                       address_entry, "port");
341
0
          goto out;
342
0
        }
343
0
    }
344
345
0
  if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
346
0
    {
347
0
      g_set_error (error,
348
0
                   G_IO_ERROR,
349
0
                   G_IO_ERROR_INVALID_ARGUMENT,
350
0
                   _("Error in address “%s” — the “%s” attribute is malformed"),
351
0
                   address_entry, "family");
352
0
      goto out;
353
0
    }
354
355
0
  if (host != NULL)
356
0
    {
357
      /* TODO: validate host */
358
0
    }
359
360
0
  ret= TRUE;
361
362
0
 out:
363
0
  g_ptr_array_unref (keys);
364
365
0
  return ret;
366
0
}
367
368
/**
369
 * g_dbus_is_supported_address:
370
 * @string: A string.
371
 * @error: Return location for error or %NULL.
372
 *
373
 * Like g_dbus_is_address() but also checks if the library supports the
374
 * transports in @string and that key/value pairs for each transport
375
 * are valid. See the specification of the
376
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
377
 *
378
 * Returns: %TRUE if @string is a valid D-Bus address that is
379
 * supported by this library, %FALSE if @error is set.
380
 *
381
 * Since: 2.26
382
 */
383
gboolean
384
g_dbus_is_supported_address (const gchar  *string,
385
                             GError      **error)
386
0
{
387
0
  guint n;
388
0
  gchar **a;
389
0
  gboolean ret;
390
391
0
  ret = FALSE;
392
393
0
  g_return_val_if_fail (string != NULL, FALSE);
394
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
395
396
0
  a = g_strsplit (string, ";", 0);
397
0
  for (n = 0; a[n] != NULL; n++)
398
0
    {
399
0
      gchar *transport_name;
400
0
      GHashTable *key_value_pairs;
401
0
      gboolean supported;
402
403
0
      if (!_g_dbus_address_parse_entry (a[n],
404
0
                                        &transport_name,
405
0
                                        &key_value_pairs,
406
0
                                        error))
407
0
        goto out;
408
409
0
      supported = FALSE;
410
0
      if (g_strcmp0 (transport_name, "unix") == 0)
411
0
        supported = is_valid_unix (a[n], key_value_pairs, error);
412
0
      else if (g_strcmp0 (transport_name, "tcp") == 0)
413
0
        supported = is_valid_tcp (a[n], key_value_pairs, error);
414
0
      else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
415
0
        supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
416
0
      else if (g_strcmp0 (a[n], "autolaunch:") == 0)
417
0
        supported = TRUE;
418
0
      else
419
0
        g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
420
0
                     _("Unknown or unsupported transport “%s” for address “%s”"),
421
0
                     transport_name, a[n]);
422
423
0
      g_free (transport_name);
424
0
      g_hash_table_unref (key_value_pairs);
425
426
0
      if (!supported)
427
0
        goto out;
428
0
    }
429
430
0
  ret = TRUE;
431
432
0
 out:
433
0
  g_strfreev (a);
434
435
0
  g_assert (ret || (!ret && (error == NULL || *error != NULL)));
436
437
0
  return ret;
438
0
}
439
440
gboolean
441
_g_dbus_address_parse_entry (const gchar  *address_entry,
442
                             gchar       **out_transport_name,
443
                             GHashTable  **out_key_value_pairs,
444
                             GError      **error)
445
0
{
446
0
  gboolean ret;
447
0
  GHashTable *key_value_pairs;
448
0
  gchar *transport_name;
449
0
  gchar **kv_pairs;
450
0
  const gchar *s;
451
0
  guint n;
452
453
0
  ret = FALSE;
454
0
  kv_pairs = NULL;
455
0
  transport_name = NULL;
456
0
  key_value_pairs = NULL;
457
458
0
  s = strchr (address_entry, ':');
459
0
  if (s == NULL)
460
0
    {
461
0
      g_set_error (error,
462
0
                   G_IO_ERROR,
463
0
                   G_IO_ERROR_INVALID_ARGUMENT,
464
0
                   _("Address element “%s” does not contain a colon (:)"),
465
0
                   address_entry);
466
0
      goto out;
467
0
    }
468
0
  else if (s == address_entry)
469
0
    {
470
0
      g_set_error (error,
471
0
                   G_IO_ERROR,
472
0
                   G_IO_ERROR_INVALID_ARGUMENT,
473
0
                   _("Transport name in address element “%s” must not be empty"),
474
0
                   address_entry);
475
0
      goto out;
476
0
    }
477
478
0
  transport_name = g_strndup (address_entry, s - address_entry);
479
0
  key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
480
481
0
  kv_pairs = g_strsplit (s + 1, ",", 0);
482
0
  for (n = 0; kv_pairs[n] != NULL; n++)
483
0
    {
484
0
      const gchar *kv_pair = kv_pairs[n];
485
0
      gchar *key;
486
0
      gchar *value;
487
488
0
      s = strchr (kv_pair, '=');
489
0
      if (s == NULL)
490
0
        {
491
0
          g_set_error (error,
492
0
                       G_IO_ERROR,
493
0
                       G_IO_ERROR_INVALID_ARGUMENT,
494
0
                       _("Key/Value pair %d, “%s”, in address element “%s” does not contain an equal sign"),
495
0
                       n,
496
0
                       kv_pair,
497
0
                       address_entry);
498
0
          goto out;
499
0
        }
500
0
      else if (s == kv_pair)
501
0
        {
502
0
          g_set_error (error,
503
0
                       G_IO_ERROR,
504
0
                       G_IO_ERROR_INVALID_ARGUMENT,
505
0
                       _("Key/Value pair %d, “%s”, in address element “%s” must not have an empty key"),
506
0
                       n,
507
0
                       kv_pair,
508
0
                       address_entry);
509
0
          goto out;
510
0
        }
511
512
0
      key = g_uri_unescape_segment (kv_pair, s, NULL);
513
0
      value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
514
0
      if (key == NULL || value == NULL)
515
0
        {
516
0
          g_set_error (error,
517
0
                       G_IO_ERROR,
518
0
                       G_IO_ERROR_INVALID_ARGUMENT,
519
0
                       _("Error unescaping key or value in Key/Value pair %d, “%s”, in address element “%s”"),
520
0
                       n,
521
0
                       kv_pair,
522
0
                       address_entry);
523
0
          g_free (key);
524
0
          g_free (value);
525
0
          goto out;
526
0
        }
527
0
      g_hash_table_insert (key_value_pairs, key, value);
528
0
    }
529
530
0
  ret = TRUE;
531
532
0
out:
533
0
  if (ret)
534
0
    {
535
0
      if (out_transport_name != NULL)
536
0
        *out_transport_name = g_steal_pointer (&transport_name);
537
0
      if (out_key_value_pairs != NULL)
538
0
        *out_key_value_pairs = g_steal_pointer (&key_value_pairs);
539
0
    }
540
541
0
  g_clear_pointer (&key_value_pairs, g_hash_table_unref);
542
0
  g_free (transport_name);
543
0
  g_strfreev (kv_pairs);
544
545
0
  return ret;
546
0
}
547
548
/* ---------------------------------------------------------------------------------------------------- */
549
550
static GIOStream *
551
g_dbus_address_try_connect_one (const gchar   *address_entry,
552
                                gchar        **out_guid,
553
                                GCancellable  *cancellable,
554
                                GError       **error);
555
556
/* TODO: Declare an extension point called GDBusTransport (or similar)
557
 * and move code below to extensions implementing said extension
558
 * point. That way we can implement a D-Bus transport over X11 without
559
 * making libgio link to libX11...
560
 */
561
static GIOStream *
562
g_dbus_address_connect (const gchar   *address_entry,
563
                        const gchar   *transport_name,
564
                        GHashTable    *key_value_pairs,
565
                        GCancellable  *cancellable,
566
                        GError       **error)
567
0
{
568
0
  GIOStream *ret;
569
0
  GSocketConnectable *connectable;
570
0
  const gchar *nonce_file;
571
572
0
  connectable = NULL;
573
0
  ret = NULL;
574
0
  nonce_file = NULL;
575
576
0
  if (g_strcmp0 (transport_name, "unix") == 0)
577
0
    {
578
0
      const gchar *path;
579
0
      const gchar *abstract;
580
0
      path = g_hash_table_lookup (key_value_pairs, "path");
581
0
      abstract = g_hash_table_lookup (key_value_pairs, "abstract");
582
0
      if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
583
0
        {
584
0
          g_set_error (error,
585
0
                       G_IO_ERROR,
586
0
                       G_IO_ERROR_INVALID_ARGUMENT,
587
0
                       _("Error in address “%s” — the unix transport requires exactly one of the "
588
0
                         "keys “path” or “abstract” to be set"),
589
0
                       address_entry);
590
0
        }
591
0
      else if (path != NULL)
592
0
        {
593
0
          connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
594
0
        }
595
0
      else if (abstract != NULL)
596
0
        {
597
0
          connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
598
0
                                                                                   -1,
599
0
                                                                                   G_UNIX_SOCKET_ADDRESS_ABSTRACT));
600
0
        }
601
0
      else
602
0
        {
603
0
          g_assert_not_reached ();
604
0
        }
605
0
    }
606
0
  else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
607
0
    {
608
0
      const gchar *s;
609
0
      const gchar *host;
610
0
      glong port;
611
0
      gchar *endp;
612
0
      gboolean is_nonce;
613
614
0
      is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
615
616
0
      host = g_hash_table_lookup (key_value_pairs, "host");
617
0
      if (host == NULL)
618
0
        {
619
0
          g_set_error (error,
620
0
                       G_IO_ERROR,
621
0
                       G_IO_ERROR_INVALID_ARGUMENT,
622
0
                       _("Error in address “%s” — the host attribute is missing or malformed"),
623
0
                       address_entry);
624
0
          goto out;
625
0
        }
626
627
0
      s = g_hash_table_lookup (key_value_pairs, "port");
628
0
      if (s == NULL)
629
0
        s = "0";
630
0
      port = strtol (s, &endp, 10);
631
0
      if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
632
0
        {
633
0
          g_set_error (error,
634
0
                       G_IO_ERROR,
635
0
                       G_IO_ERROR_INVALID_ARGUMENT,
636
0
                       _("Error in address “%s” — the port attribute is missing or malformed"),
637
0
                       address_entry);
638
0
          goto out;
639
0
        }
640
641
642
0
      if (is_nonce)
643
0
        {
644
0
          nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
645
0
          if (nonce_file == NULL)
646
0
            {
647
0
              g_set_error (error,
648
0
                           G_IO_ERROR,
649
0
                           G_IO_ERROR_INVALID_ARGUMENT,
650
0
                           _("Error in address “%s” — the noncefile attribute is missing or malformed"),
651
0
                           address_entry);
652
0
              goto out;
653
0
            }
654
0
        }
655
656
      /* TODO: deal with family key/value-pair */
657
0
      connectable = g_network_address_new (host, port);
658
0
    }
659
0
  else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
660
0
    {
661
0
      gchar *autolaunch_address;
662
0
      autolaunch_address = get_session_address_dbus_launch (error);
663
0
      if (autolaunch_address != NULL)
664
0
        {
665
0
          ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
666
0
          g_free (autolaunch_address);
667
0
          goto out;
668
0
        }
669
0
      else
670
0
        {
671
0
          g_prefix_error (error, _("Error auto-launching: "));
672
0
        }
673
0
    }
674
0
  else
675
0
    {
676
0
      g_set_error (error,
677
0
                   G_IO_ERROR,
678
0
                   G_IO_ERROR_INVALID_ARGUMENT,
679
0
                   _("Unknown or unsupported transport “%s” for address “%s”"),
680
0
                   transport_name,
681
0
                   address_entry);
682
0
    }
683
684
0
  if (connectable != NULL)
685
0
    {
686
0
      GSocketClient *client;
687
0
      GSocketConnection *connection;
688
689
0
      g_assert (ret == NULL);
690
0
      client = g_socket_client_new ();
691
692
      /* Disable proxy support to prevent a deadlock on startup, since loading a
693
       * proxy resolver causes the GIO modules to be loaded, and there will
694
       * almost certainly be one of them which then tries to use GDBus.
695
       * See: https://bugzilla.gnome.org/show_bug.cgi?id=792499 */
696
0
      g_socket_client_set_enable_proxy (client, FALSE);
697
698
0
      connection = g_socket_client_connect (client,
699
0
                                            connectable,
700
0
                                            cancellable,
701
0
                                            error);
702
0
      g_object_unref (connectable);
703
0
      g_object_unref (client);
704
0
      if (connection == NULL)
705
0
        goto out;
706
707
0
      ret = G_IO_STREAM (connection);
708
709
0
      if (nonce_file != NULL)
710
0
        {
711
0
          gchar nonce_contents[16 + 1];
712
0
          size_t num_bytes_read;
713
0
          FILE *f;
714
0
          int errsv;
715
716
          /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
717
0
          f = fopen (nonce_file, "rb" FO_CLOEXEC);
718
0
          errsv = errno;
719
0
          if (f == NULL)
720
0
            {
721
0
              g_set_error (error,
722
0
                           G_IO_ERROR,
723
0
                           G_IO_ERROR_INVALID_ARGUMENT,
724
0
                           _("Error opening nonce file “%s”: %s"),
725
0
                           nonce_file,
726
0
                           g_strerror (errsv));
727
0
              g_object_unref (ret);
728
0
              ret = NULL;
729
0
              goto out;
730
0
            }
731
0
          num_bytes_read = fread (nonce_contents,
732
0
                                  sizeof (gchar),
733
0
                                  16 + 1,
734
0
                                  f);
735
0
          errsv = errno;
736
0
          if (num_bytes_read != 16)
737
0
            {
738
0
              if (num_bytes_read == 0)
739
0
                {
740
0
                  g_set_error (error,
741
0
                               G_IO_ERROR,
742
0
                               G_IO_ERROR_INVALID_ARGUMENT,
743
0
                               _("Error reading from nonce file “%s”: %s"),
744
0
                               nonce_file,
745
0
                               g_strerror (errsv));
746
0
                }
747
0
              else
748
0
                {
749
0
                  g_set_error (error,
750
0
                               G_IO_ERROR,
751
0
                               G_IO_ERROR_INVALID_ARGUMENT,
752
0
                               _("Error reading from nonce file “%s”, expected 16 bytes, got %d"),
753
0
                               nonce_file,
754
0
                               (gint) num_bytes_read);
755
0
                }
756
0
              g_object_unref (ret);
757
0
              ret = NULL;
758
0
              fclose (f);
759
0
              goto out;
760
0
            }
761
0
          fclose (f);
762
763
0
          if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
764
0
                                          nonce_contents,
765
0
                                          16,
766
0
                                          NULL,
767
0
                                          cancellable,
768
0
                                          error))
769
0
            {
770
0
              g_prefix_error (error, _("Error writing contents of nonce file “%s” to stream:"), nonce_file);
771
0
              g_object_unref (ret);
772
0
              ret = NULL;
773
0
              goto out;
774
0
            }
775
0
        }
776
0
    }
777
778
0
 out:
779
780
0
  return ret;
781
0
}
782
783
static GIOStream *
784
g_dbus_address_try_connect_one (const gchar   *address_entry,
785
                                gchar        **out_guid,
786
                                GCancellable  *cancellable,
787
                                GError       **error)
788
0
{
789
0
  GIOStream *ret;
790
0
  GHashTable *key_value_pairs;
791
0
  gchar *transport_name;
792
0
  const gchar *guid;
793
794
0
  ret = NULL;
795
0
  transport_name = NULL;
796
0
  key_value_pairs = NULL;
797
798
0
  if (!_g_dbus_address_parse_entry (address_entry,
799
0
                                    &transport_name,
800
0
                                    &key_value_pairs,
801
0
                                    error))
802
0
    goto out;
803
804
0
  ret = g_dbus_address_connect (address_entry,
805
0
                                transport_name,
806
0
                                key_value_pairs,
807
0
                                cancellable,
808
0
                                error);
809
0
  if (ret == NULL)
810
0
    goto out;
811
812
0
  guid = g_hash_table_lookup (key_value_pairs, "guid");
813
0
  if (guid != NULL && out_guid != NULL)
814
0
    *out_guid = g_strdup (guid);
815
816
0
out:
817
0
  g_free (transport_name);
818
0
  if (key_value_pairs != NULL)
819
0
    g_hash_table_unref (key_value_pairs);
820
0
  return ret;
821
0
}
822
823
824
/* ---------------------------------------------------------------------------------------------------- */
825
826
typedef struct {
827
  gchar *address;
828
  gchar *guid;
829
} GetStreamData;
830
831
static void
832
get_stream_data_free (GetStreamData *data)
833
0
{
834
0
  g_free (data->address);
835
0
  g_free (data->guid);
836
0
  g_free (data);
837
0
}
838
839
static void
840
get_stream_thread_func (GTask         *task,
841
                        gpointer       source_object,
842
                        gpointer       task_data,
843
                        GCancellable  *cancellable)
844
0
{
845
0
  GetStreamData *data = task_data;
846
0
  GIOStream *stream;
847
0
  GError *error = NULL;
848
849
0
  stream = g_dbus_address_get_stream_sync (data->address,
850
0
                                           &data->guid,
851
0
                                           cancellable,
852
0
                                           &error);
853
0
  if (stream)
854
0
    g_task_return_pointer (task, stream, g_object_unref);
855
0
  else
856
0
    g_task_return_error (task, error);
857
0
}
858
859
/**
860
 * g_dbus_address_get_stream:
861
 * @address: A valid D-Bus address.
862
 * @cancellable: (nullable): A #GCancellable or %NULL.
863
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
864
 * @user_data: Data to pass to @callback.
865
 *
866
 * Asynchronously connects to an endpoint specified by @address and
867
 * sets up the connection so it is in a state to run the client-side
868
 * of the D-Bus authentication conversation. @address must be in the
869
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
870
 *
871
 * When the operation is finished, @callback will be invoked. You can
872
 * then call g_dbus_address_get_stream_finish() to get the result of
873
 * the operation.
874
 *
875
 * This is an asynchronous failable function. See
876
 * g_dbus_address_get_stream_sync() for the synchronous version.
877
 *
878
 * Since: 2.26
879
 */
880
void
881
g_dbus_address_get_stream (const gchar         *address,
882
                           GCancellable        *cancellable,
883
                           GAsyncReadyCallback  callback,
884
                           gpointer             user_data)
885
0
{
886
0
  GTask *task;
887
0
  GetStreamData *data;
888
889
0
  g_return_if_fail (address != NULL);
890
891
0
  data = g_new0 (GetStreamData, 1);
892
0
  data->address = g_strdup (address);
893
894
0
  task = g_task_new (NULL, cancellable, callback, user_data);
895
0
  g_task_set_source_tag (task, g_dbus_address_get_stream);
896
0
  g_task_set_task_data (task, data, (GDestroyNotify) get_stream_data_free);
897
0
  g_task_run_in_thread (task, get_stream_thread_func);
898
0
  g_object_unref (task);
899
0
}
900
901
/**
902
 * g_dbus_address_get_stream_finish:
903
 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
904
 * @out_guid: (optional) (out) (nullable): %NULL or return location to store the GUID extracted from @address, if any.
905
 * @error: Return location for error or %NULL.
906
 *
907
 * Finishes an operation started with g_dbus_address_get_stream().
908
 *
909
 * A server is not required to set a GUID, so @out_guid may be set to %NULL
910
 * even on success.
911
 *
912
 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
913
 *
914
 * Since: 2.26
915
 */
916
GIOStream *
917
g_dbus_address_get_stream_finish (GAsyncResult        *res,
918
                                  gchar              **out_guid,
919
                                  GError             **error)
920
0
{
921
0
  GTask *task;
922
0
  GetStreamData *data;
923
0
  GIOStream *ret;
924
925
0
  g_return_val_if_fail (g_task_is_valid (res, NULL), NULL);
926
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
927
928
0
  task = G_TASK (res);
929
0
  ret = g_task_propagate_pointer (task, error);
930
931
0
  if (ret != NULL && out_guid != NULL)
932
0
    {
933
0
      data = g_task_get_task_data (task);
934
0
      *out_guid = data->guid;
935
0
      data->guid = NULL;
936
0
    }
937
938
0
  return ret;
939
0
}
940
941
/**
942
 * g_dbus_address_get_stream_sync:
943
 * @address: A valid D-Bus address.
944
 * @out_guid: (optional) (out) (nullable): %NULL or return location to store the GUID extracted from @address, if any.
945
 * @cancellable: (nullable): A #GCancellable or %NULL.
946
 * @error: Return location for error or %NULL.
947
 *
948
 * Synchronously connects to an endpoint specified by @address and
949
 * sets up the connection so it is in a state to run the client-side
950
 * of the D-Bus authentication conversation. @address must be in the
951
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
952
 *
953
 * A server is not required to set a GUID, so @out_guid may be set to %NULL
954
 * even on success.
955
 *
956
 * This is a synchronous failable function. See
957
 * g_dbus_address_get_stream() for the asynchronous version.
958
 *
959
 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
960
 *
961
 * Since: 2.26
962
 */
963
GIOStream *
964
g_dbus_address_get_stream_sync (const gchar   *address,
965
                                gchar        **out_guid,
966
                                GCancellable  *cancellable,
967
                                GError       **error)
968
0
{
969
0
  GIOStream *ret;
970
0
  gchar **addr_array;
971
0
  guint n;
972
0
  GError *last_error;
973
974
0
  g_return_val_if_fail (address != NULL, NULL);
975
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
976
977
0
  ret = NULL;
978
0
  last_error = NULL;
979
980
0
  addr_array = g_strsplit (address, ";", 0);
981
0
  if (addr_array[0] == NULL)
982
0
    {
983
0
      last_error = g_error_new_literal (G_IO_ERROR,
984
0
                                        G_IO_ERROR_INVALID_ARGUMENT,
985
0
                                        _("The given address is empty"));
986
0
      goto out;
987
0
    }
988
989
0
  for (n = 0; addr_array[n] != NULL; n++)
990
0
    {
991
0
      const gchar *addr = addr_array[n];
992
0
      GError *this_error;
993
994
0
      this_error = NULL;
995
0
      ret = g_dbus_address_try_connect_one (addr,
996
0
                                            out_guid,
997
0
                                            cancellable,
998
0
                                            &this_error);
999
0
      if (ret != NULL)
1000
0
        {
1001
0
          goto out;
1002
0
        }
1003
0
      else
1004
0
        {
1005
0
          g_assert (this_error != NULL);
1006
0
          if (last_error != NULL)
1007
0
            g_error_free (last_error);
1008
0
          last_error = this_error;
1009
0
        }
1010
0
    }
1011
1012
0
 out:
1013
0
  if (ret != NULL)
1014
0
    {
1015
0
      if (last_error != NULL)
1016
0
        g_error_free (last_error);
1017
0
    }
1018
0
  else
1019
0
    {
1020
0
      g_assert (last_error != NULL);
1021
0
      g_propagate_error (error, last_error);
1022
0
    }
1023
1024
0
  g_strfreev (addr_array);
1025
0
  return ret;
1026
0
}
1027
1028
/* ---------------------------------------------------------------------------------------------------- */
1029
1030
/*
1031
 * Return the address of XDG_RUNTIME_DIR/bus if it exists, belongs to
1032
 * us, and is a socket, and we are on Unix.
1033
 */
1034
static gchar *
1035
get_session_address_xdg (void)
1036
0
{
1037
0
#ifdef G_OS_UNIX
1038
0
  gchar *ret = NULL;
1039
0
  gchar *bus;
1040
0
  gchar *tmp;
1041
0
  GStatBuf buf;
1042
1043
0
  bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL);
1044
1045
  /* if ENOENT, EPERM, etc., quietly don't use it */
1046
0
  if (g_stat (bus, &buf) < 0)
1047
0
    goto out;
1048
1049
  /* if it isn't ours, we have incorrectly inherited someone else's
1050
   * XDG_RUNTIME_DIR; silently don't use it
1051
   */
1052
0
  if (buf.st_uid != geteuid ())
1053
0
    goto out;
1054
1055
  /* if it isn't a socket, silently don't use it */
1056
0
  if ((buf.st_mode & S_IFMT) != S_IFSOCK)
1057
0
    goto out;
1058
1059
0
  tmp = g_dbus_address_escape_value (bus);
1060
0
  ret = g_strconcat ("unix:path=", tmp, NULL);
1061
0
  g_free (tmp);
1062
1063
0
out:
1064
0
  g_free (bus);
1065
0
  return ret;
1066
#else
1067
  return NULL;
1068
#endif
1069
0
}
1070
1071
/* ---------------------------------------------------------------------------------------------------- */
1072
1073
#ifdef G_OS_UNIX
1074
static gchar *
1075
get_session_address_dbus_launch (GError **error)
1076
0
{
1077
0
  gchar *ret;
1078
0
  gchar *machine_id;
1079
0
  gchar *command_line;
1080
0
  gchar *launch_stdout;
1081
0
  gchar *launch_stderr;
1082
0
  gint wait_status;
1083
0
  gchar *old_dbus_verbose;
1084
0
  gboolean restore_dbus_verbose;
1085
1086
0
  ret = NULL;
1087
0
  machine_id = NULL;
1088
0
  command_line = NULL;
1089
0
  launch_stdout = NULL;
1090
0
  launch_stderr = NULL;
1091
0
  restore_dbus_verbose = FALSE;
1092
0
  old_dbus_verbose = NULL;
1093
1094
  /* Don't run binaries as root if we're setuid. */
1095
0
  if (GLIB_PRIVATE_CALL (g_check_setuid) ())
1096
0
    {
1097
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1098
0
                   _("Cannot spawn a message bus when AT_SECURE is set"));
1099
0
      goto out;
1100
0
    }
1101
1102
0
  machine_id = _g_dbus_get_machine_id (error);
1103
0
  if (machine_id == NULL)
1104
0
    {
1105
0
      g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
1106
0
      goto out;
1107
0
    }
1108
1109
0
  if (g_getenv ("DISPLAY") == NULL)
1110
0
    {
1111
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1112
0
                   _("Cannot autolaunch D-Bus without X11 $DISPLAY"));
1113
0
      goto out;
1114
0
    }
1115
1116
  /* We're using private libdbus facilities here. When everything
1117
   * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
1118
   * X11 property is correctly documented right now) we should
1119
   * consider using the spec instead of dbus-launch.
1120
   *
1121
   *   --autolaunch=MACHINEID
1122
   *          This option implies that dbus-launch should scan  for  a  previ‐
1123
   *          ously-started  session  and  reuse the values found there. If no
1124
   *          session is found, it will start a new session. The  --exit-with-
1125
   *          session option is implied if --autolaunch is given.  This option
1126
   *          is for the exclusive use of libdbus, you do not want to  use  it
1127
   *          manually. It may change in the future.
1128
   */
1129
1130
  /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
1131
0
  command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
1132
1133
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1134
0
    {
1135
0
      _g_dbus_debug_print_lock ();
1136
0
      g_print ("GDBus-debug:Address: Running '%s' to get bus address (possibly autolaunching)\n", command_line);
1137
0
      old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
1138
0
      restore_dbus_verbose = TRUE;
1139
0
      g_setenv ("DBUS_VERBOSE", "1", TRUE);
1140
0
      _g_dbus_debug_print_unlock ();
1141
0
    }
1142
1143
0
  if (!g_spawn_command_line_sync (command_line,
1144
0
                                  &launch_stdout,
1145
0
                                  &launch_stderr,
1146
0
                                  &wait_status,
1147
0
                                  error))
1148
0
    {
1149
0
      goto out;
1150
0
    }
1151
1152
0
  if (!g_spawn_check_wait_status (wait_status, error))
1153
0
    {
1154
0
      g_prefix_error (error, _("Error spawning command line “%s”: "), command_line);
1155
0
      goto out;
1156
0
    }
1157
1158
  /* From the dbus-launch(1) man page:
1159
   *
1160
   *   --binary-syntax Write to stdout a nul-terminated bus address,
1161
   *   then the bus PID as a binary integer of size sizeof(pid_t),
1162
   *   then the bus X window ID as a binary integer of size
1163
   *   sizeof(long).  Integers are in the machine's byte order, not
1164
   *   network byte order or any other canonical byte order.
1165
   */
1166
0
  ret = g_strdup (launch_stdout);
1167
1168
0
 out:
1169
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1170
0
    {
1171
0
      gchar *s;
1172
0
      _g_dbus_debug_print_lock ();
1173
0
      g_print ("GDBus-debug:Address: dbus-launch output:");
1174
0
      if (launch_stdout != NULL)
1175
0
        {
1176
0
          s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
1177
0
          g_print ("\n%s", s);
1178
0
          g_free (s);
1179
0
        }
1180
0
      else
1181
0
        {
1182
0
          g_print (" (none)\n");
1183
0
        }
1184
0
      g_print ("GDBus-debug:Address: dbus-launch stderr output:");
1185
0
      if (launch_stderr != NULL)
1186
0
        g_print ("\n%s", launch_stderr);
1187
0
      else
1188
0
        g_print (" (none)\n");
1189
0
      _g_dbus_debug_print_unlock ();
1190
0
    }
1191
1192
0
  g_free (machine_id);
1193
0
  g_free (command_line);
1194
0
  g_free (launch_stdout);
1195
0
  g_free (launch_stderr);
1196
0
  if (G_UNLIKELY (restore_dbus_verbose))
1197
0
    {
1198
0
      if (old_dbus_verbose != NULL)
1199
0
        g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
1200
0
      else
1201
0
        g_unsetenv ("DBUS_VERBOSE");
1202
0
    }
1203
0
  g_free (old_dbus_verbose);
1204
0
  return ret;
1205
0
}
1206
1207
/* end of G_OS_UNIX case */
1208
#elif defined(G_OS_WIN32)
1209
1210
static gchar *
1211
get_session_address_dbus_launch (GError **error)
1212
{
1213
  return _g_dbus_win32_get_session_address_dbus_launch (error);
1214
}
1215
1216
#else /* neither G_OS_UNIX nor G_OS_WIN32 */
1217
static gchar *
1218
get_session_address_dbus_launch (GError **error)
1219
{
1220
  g_set_error (error,
1221
               G_IO_ERROR,
1222
               G_IO_ERROR_FAILED,
1223
               _("Cannot determine session bus address (not implemented for this OS)"));
1224
  return NULL;
1225
}
1226
#endif /* neither G_OS_UNIX nor G_OS_WIN32 */
1227
1228
/* ---------------------------------------------------------------------------------------------------- */
1229
1230
static gchar *
1231
get_session_address_platform_specific (GError **error)
1232
0
{
1233
0
  gchar *ret;
1234
1235
  /* Use XDG_RUNTIME_DIR/bus if it exists and is suitable. This is appropriate
1236
   * for systems using the "a session is a user-session" model described in
1237
   * <http://lists.freedesktop.org/archives/dbus/2015-January/016522.html>,
1238
   * and implemented in dbus >= 1.9.14 and sd-bus.
1239
   *
1240
   * On systems following the more traditional "a session is a login-session"
1241
   * model, this will fail and we'll fall through to X11 autolaunching
1242
   * (dbus-launch) below.
1243
   */
1244
0
  ret = get_session_address_xdg ();
1245
1246
0
  if (ret != NULL)
1247
0
    return ret;
1248
1249
  /* TODO (#694472): try launchd on OS X, like
1250
   * _dbus_lookup_session_address_launchd() does, since
1251
   * 'dbus-launch --autolaunch' probably won't work there
1252
   */
1253
1254
  /* As a last resort, try the "autolaunch:" transport. On Unix this means
1255
   * X11 autolaunching; on Windows this means a different autolaunching
1256
   * mechanism based on shared memory.
1257
   */
1258
0
  return get_session_address_dbus_launch (error);
1259
0
}
1260
1261
/* ---------------------------------------------------------------------------------------------------- */
1262
1263
/**
1264
 * g_dbus_address_get_for_bus_sync:
1265
 * @bus_type: a #GBusType
1266
 * @cancellable: (nullable): a #GCancellable or %NULL
1267
 * @error: return location for error or %NULL
1268
 *
1269
 * Synchronously looks up the D-Bus address for the well-known message
1270
 * bus instance specified by @bus_type. This may involve using various
1271
 * platform specific mechanisms.
1272
 *
1273
 * The returned address will be in the
1274
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
1275
 *
1276
 * Returns: (transfer full): a valid D-Bus address string for @bus_type or
1277
 *     %NULL if @error is set
1278
 *
1279
 * Since: 2.26
1280
 */
1281
gchar *
1282
g_dbus_address_get_for_bus_sync (GBusType       bus_type,
1283
                                 GCancellable  *cancellable,
1284
                                 GError       **error)
1285
0
{
1286
0
  gboolean has_elevated_privileges = GLIB_PRIVATE_CALL (g_check_setuid) ();
1287
0
  gchar *ret, *s = NULL;
1288
0
  const gchar *starter_bus;
1289
0
  GError *local_error;
1290
1291
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1292
1293
0
  ret = NULL;
1294
0
  local_error = NULL;
1295
1296
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1297
0
    {
1298
0
      guint n;
1299
0
      _g_dbus_debug_print_lock ();
1300
0
      s = _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type);
1301
0
      g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type '%s'\n",
1302
0
               s);
1303
0
      g_free (s);
1304
0
      for (n = 0; n < 3; n++)
1305
0
        {
1306
0
          const gchar *k;
1307
0
          const gchar *v;
1308
0
          switch (n)
1309
0
            {
1310
0
            case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
1311
0
            case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
1312
0
            case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
1313
0
            default: g_assert_not_reached ();
1314
0
            }
1315
0
          v = g_getenv (k);
1316
0
          g_print ("GDBus-debug:Address: env var %s", k);
1317
0
          if (v != NULL)
1318
0
            g_print ("='%s'\n", v);
1319
0
          else
1320
0
            g_print (" is not set\n");
1321
0
        }
1322
0
      _g_dbus_debug_print_unlock ();
1323
0
    }
1324
1325
  /* Don’t load the addresses from the environment if running as setuid, as they
1326
   * come from an unprivileged caller. */
1327
0
  switch (bus_type)
1328
0
    {
1329
0
    case G_BUS_TYPE_SYSTEM:
1330
0
      if (has_elevated_privileges)
1331
0
        ret = NULL;
1332
0
      else
1333
0
        ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
1334
1335
0
      if (ret == NULL)
1336
0
        {
1337
          /* While the D-Bus specification says this must be `/var/run/dbus/system_bus_socket`,
1338
           * a footnote allows it to use localstatedir:
1339
           * https://dbus.freedesktop.org/doc/dbus-specification.html#ftn.id-1.13.6.4.3.3
1340
           * or, on systems where /run is the same as /var/run, runstatedir:
1341
           * https://gitlab.freedesktop.org/dbus/dbus/-/merge_requests/209 */
1342
0
          ret = g_strdup ("unix:path=" GLIB_RUNSTATEDIR "/dbus/system_bus_socket");
1343
0
        }
1344
0
      break;
1345
1346
0
    case G_BUS_TYPE_SESSION:
1347
0
      if (has_elevated_privileges)
1348
0
        ret = NULL;
1349
0
      else
1350
0
        ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
1351
1352
0
      if (ret == NULL)
1353
0
        {
1354
0
          ret = get_session_address_platform_specific (&local_error);
1355
0
        }
1356
0
      break;
1357
1358
0
    case G_BUS_TYPE_STARTER:
1359
0
      starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
1360
0
      if (g_strcmp0 (starter_bus, "session") == 0)
1361
0
        {
1362
0
          ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
1363
0
          goto out;
1364
0
        }
1365
0
      else if (g_strcmp0 (starter_bus, "system") == 0)
1366
0
        {
1367
0
          ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
1368
0
          goto out;
1369
0
        }
1370
0
      else
1371
0
        {
1372
0
          if (starter_bus != NULL)
1373
0
            {
1374
0
              g_set_error (&local_error,
1375
0
                           G_IO_ERROR,
1376
0
                           G_IO_ERROR_FAILED,
1377
0
                           _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1378
0
                             " — unknown value “%s”"),
1379
0
                           starter_bus);
1380
0
            }
1381
0
          else
1382
0
            {
1383
0
              g_set_error_literal (&local_error,
1384
0
                                   G_IO_ERROR,
1385
0
                                   G_IO_ERROR_FAILED,
1386
0
                                   _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1387
0
                                     "variable is not set"));
1388
0
            }
1389
0
        }
1390
0
      break;
1391
1392
0
    default:
1393
0
      g_set_error (&local_error,
1394
0
                   G_IO_ERROR,
1395
0
                   G_IO_ERROR_FAILED,
1396
0
                   _("Unknown bus type %d"),
1397
0
                   bus_type);
1398
0
      break;
1399
0
    }
1400
1401
0
 out:
1402
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1403
0
    {
1404
0
      _g_dbus_debug_print_lock ();
1405
0
      s = _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type);
1406
0
      if (ret != NULL)
1407
0
        {
1408
0
          g_print ("GDBus-debug:Address: Returning address '%s' for bus type '%s'\n",
1409
0
                   ret, s);
1410
0
        }
1411
0
      else
1412
0
        {
1413
0
          g_print ("GDBus-debug:Address: Cannot look-up address bus type '%s': %s\n",
1414
0
                   s, local_error ? local_error->message : "");
1415
0
        }
1416
0
      g_free (s);
1417
0
      _g_dbus_debug_print_unlock ();
1418
0
    }
1419
1420
0
  if (local_error != NULL)
1421
0
    g_propagate_error (error, local_error);
1422
1423
0
  return ret;
1424
0
}
1425
1426
/**
1427
 * g_dbus_address_escape_value:
1428
 * @string: an unescaped string to be included in a D-Bus address
1429
 *     as the value in a key-value pair
1430
 *
1431
 * Escape @string so it can appear in a D-Bus address as the value
1432
 * part of a key-value pair.
1433
 *
1434
 * For instance, if @string is `/run/bus-for-:0`,
1435
 * this function would return `/run/bus-for-%3A0`,
1436
 * which could be used in a D-Bus address like
1437
 * `unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0`.
1438
 *
1439
 * Returns: (transfer full): a copy of @string with all
1440
 *     non-optionally-escaped bytes escaped
1441
 *
1442
 * Since: 2.36
1443
 */
1444
gchar *
1445
g_dbus_address_escape_value (const gchar *string)
1446
0
{
1447
0
  GString *s;
1448
0
  gsize i;
1449
1450
0
  g_return_val_if_fail (string != NULL, NULL);
1451
1452
  /* There will often not be anything needing escaping at all. */
1453
0
  s = g_string_sized_new (strlen (string));
1454
1455
  /* D-Bus address escaping is mostly the same as URI escaping... */
1456
0
  g_string_append_uri_escaped (s, string, "\\/", FALSE);
1457
1458
  /* ... but '~' is an unreserved character in URIs, but a
1459
   * non-optionally-escaped character in D-Bus addresses. */
1460
0
  for (i = 0; i < s->len; i++)
1461
0
    {
1462
0
      if (G_UNLIKELY (s->str[i] == '~'))
1463
0
        {
1464
0
          s->str[i] = '%';
1465
0
          g_string_insert (s, i + 1, "7E");
1466
0
          i += 2;
1467
0
        }
1468
0
    }
1469
1470
0
  return g_string_free (s, FALSE);
1471
0
}