Coverage Report

Created: 2025-11-16 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/gdbusaddress.c
Line
Count
Source
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
                       /* Translators: The first placeholder is a D-Bus connection address,
623
                        * the second is the literal name of an attribute in the address.
624
                        */
625
0
                       _("Error in address “%s” — the %s attribute is missing or malformed"),
626
0
                       address_entry,
627
0
                       "host");
628
0
          goto out;
629
0
        }
630
631
0
      s = g_hash_table_lookup (key_value_pairs, "port");
632
0
      if (s == NULL)
633
0
        s = "0";
634
0
      port = strtol (s, &endp, 10);
635
0
      if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
636
0
        {
637
0
          g_set_error (error,
638
0
                       G_IO_ERROR,
639
0
                       G_IO_ERROR_INVALID_ARGUMENT,
640
0
                       _("Error in address “%s” — the %s attribute is missing or malformed"),
641
0
                       address_entry,
642
0
                       "port");
643
0
          goto out;
644
0
        }
645
646
647
0
      if (is_nonce)
648
0
        {
649
0
          nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
650
0
          if (nonce_file == NULL)
651
0
            {
652
0
              g_set_error (error,
653
0
                           G_IO_ERROR,
654
0
                           G_IO_ERROR_INVALID_ARGUMENT,
655
0
                           _("Error in address “%s” — the %s attribute is missing or malformed"),
656
0
                           address_entry,
657
0
                           "noncefile");
658
0
              goto out;
659
0
            }
660
0
        }
661
662
      /* TODO: deal with family key/value-pair */
663
0
      connectable = g_network_address_new (host, port);
664
0
    }
665
0
  else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
666
0
    {
667
0
      gchar *autolaunch_address;
668
0
      autolaunch_address = get_session_address_dbus_launch (error);
669
0
      if (autolaunch_address != NULL)
670
0
        {
671
0
          ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
672
0
          g_free (autolaunch_address);
673
0
          goto out;
674
0
        }
675
0
      else
676
0
        {
677
0
          g_prefix_error (error, _("Error auto-launching: "));
678
0
        }
679
0
    }
680
0
  else
681
0
    {
682
0
      g_set_error (error,
683
0
                   G_IO_ERROR,
684
0
                   G_IO_ERROR_INVALID_ARGUMENT,
685
0
                   _("Unknown or unsupported transport “%s” for address “%s”"),
686
0
                   transport_name,
687
0
                   address_entry);
688
0
    }
689
690
0
  if (connectable != NULL)
691
0
    {
692
0
      GSocketClient *client;
693
0
      GSocketConnection *connection;
694
695
0
      g_assert (ret == NULL);
696
0
      client = g_socket_client_new ();
697
698
      /* Disable proxy support to prevent a deadlock on startup, since loading a
699
       * proxy resolver causes the GIO modules to be loaded, and there will
700
       * almost certainly be one of them which then tries to use GDBus.
701
       * See: https://bugzilla.gnome.org/show_bug.cgi?id=792499 */
702
0
      g_socket_client_set_enable_proxy (client, FALSE);
703
704
0
      connection = g_socket_client_connect (client,
705
0
                                            connectable,
706
0
                                            cancellable,
707
0
                                            error);
708
0
      g_object_unref (connectable);
709
0
      g_object_unref (client);
710
0
      if (connection == NULL)
711
0
        goto out;
712
713
0
      ret = G_IO_STREAM (connection);
714
715
0
      if (nonce_file != NULL)
716
0
        {
717
0
          gchar nonce_contents[16 + 1];
718
0
          size_t num_bytes_read;
719
0
          FILE *f;
720
0
          int errsv;
721
722
          /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
723
0
          f = fopen (nonce_file, "rb" FO_CLOEXEC);
724
0
          errsv = errno;
725
0
          if (f == NULL)
726
0
            {
727
0
              g_set_error (error,
728
0
                           G_IO_ERROR,
729
0
                           G_IO_ERROR_INVALID_ARGUMENT,
730
0
                           _("Error opening nonce file “%s”: %s"),
731
0
                           nonce_file,
732
0
                           g_strerror (errsv));
733
0
              g_object_unref (ret);
734
0
              ret = NULL;
735
0
              goto out;
736
0
            }
737
0
          num_bytes_read = fread (nonce_contents,
738
0
                                  sizeof (gchar),
739
0
                                  16 + 1,
740
0
                                  f);
741
0
          errsv = errno;
742
0
          if (num_bytes_read != 16)
743
0
            {
744
0
              if (num_bytes_read == 0)
745
0
                {
746
0
                  g_set_error (error,
747
0
                               G_IO_ERROR,
748
0
                               G_IO_ERROR_INVALID_ARGUMENT,
749
0
                               _("Error reading from nonce file “%s”: %s"),
750
0
                               nonce_file,
751
0
                               g_strerror (errsv));
752
0
                }
753
0
              else
754
0
                {
755
0
                  g_set_error (error,
756
0
                               G_IO_ERROR,
757
0
                               G_IO_ERROR_INVALID_ARGUMENT,
758
0
                               _("Error reading from nonce file “%s”, expected 16 bytes, got %d"),
759
0
                               nonce_file,
760
0
                               (gint) num_bytes_read);
761
0
                }
762
0
              g_object_unref (ret);
763
0
              ret = NULL;
764
0
              fclose (f);
765
0
              goto out;
766
0
            }
767
0
          fclose (f);
768
769
0
          if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
770
0
                                          nonce_contents,
771
0
                                          16,
772
0
                                          NULL,
773
0
                                          cancellable,
774
0
                                          error))
775
0
            {
776
0
              g_prefix_error (error, _("Error writing contents of nonce file “%s” to stream:"), nonce_file);
777
0
              g_object_unref (ret);
778
0
              ret = NULL;
779
0
              goto out;
780
0
            }
781
0
        }
782
0
    }
783
784
0
 out:
785
786
0
  return ret;
787
0
}
788
789
static GIOStream *
790
g_dbus_address_try_connect_one (const gchar   *address_entry,
791
                                gchar        **out_guid,
792
                                GCancellable  *cancellable,
793
                                GError       **error)
794
0
{
795
0
  GIOStream *ret;
796
0
  GHashTable *key_value_pairs;
797
0
  gchar *transport_name;
798
0
  const gchar *guid;
799
800
0
  ret = NULL;
801
0
  transport_name = NULL;
802
0
  key_value_pairs = NULL;
803
804
0
  if (!_g_dbus_address_parse_entry (address_entry,
805
0
                                    &transport_name,
806
0
                                    &key_value_pairs,
807
0
                                    error))
808
0
    goto out;
809
810
0
  ret = g_dbus_address_connect (address_entry,
811
0
                                transport_name,
812
0
                                key_value_pairs,
813
0
                                cancellable,
814
0
                                error);
815
0
  if (ret == NULL)
816
0
    goto out;
817
818
0
  guid = g_hash_table_lookup (key_value_pairs, "guid");
819
0
  if (guid != NULL && out_guid != NULL)
820
0
    *out_guid = g_strdup (guid);
821
822
0
out:
823
0
  g_free (transport_name);
824
0
  if (key_value_pairs != NULL)
825
0
    g_hash_table_unref (key_value_pairs);
826
0
  return ret;
827
0
}
828
829
830
/* ---------------------------------------------------------------------------------------------------- */
831
832
typedef struct {
833
  gchar *address;
834
  gchar *guid;
835
} GetStreamData;
836
837
static void
838
get_stream_data_free (GetStreamData *data)
839
0
{
840
0
  g_free (data->address);
841
0
  g_free (data->guid);
842
0
  g_free (data);
843
0
}
844
845
static void
846
get_stream_thread_func (GTask         *task,
847
                        gpointer       source_object,
848
                        gpointer       task_data,
849
                        GCancellable  *cancellable)
850
0
{
851
0
  GetStreamData *data = task_data;
852
0
  GIOStream *stream;
853
0
  GError *error = NULL;
854
855
0
  stream = g_dbus_address_get_stream_sync (data->address,
856
0
                                           &data->guid,
857
0
                                           cancellable,
858
0
                                           &error);
859
0
  if (stream)
860
0
    g_task_return_pointer (task, stream, g_object_unref);
861
0
  else
862
0
    g_task_return_error (task, error);
863
0
}
864
865
/**
866
 * g_dbus_address_get_stream:
867
 * @address: A valid D-Bus address.
868
 * @cancellable: (nullable): A #GCancellable or %NULL.
869
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
870
 * @user_data: Data to pass to @callback.
871
 *
872
 * Asynchronously connects to an endpoint specified by @address and
873
 * sets up the connection so it is in a state to run the client-side
874
 * of the D-Bus authentication conversation. @address must be in the
875
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
876
 *
877
 * When the operation is finished, @callback will be invoked. You can
878
 * then call g_dbus_address_get_stream_finish() to get the result of
879
 * the operation.
880
 *
881
 * This is an asynchronous failable function. See
882
 * g_dbus_address_get_stream_sync() for the synchronous version.
883
 *
884
 * Since: 2.26
885
 */
886
void
887
g_dbus_address_get_stream (const gchar         *address,
888
                           GCancellable        *cancellable,
889
                           GAsyncReadyCallback  callback,
890
                           gpointer             user_data)
891
0
{
892
0
  GTask *task;
893
0
  GetStreamData *data;
894
895
0
  g_return_if_fail (address != NULL);
896
897
0
  data = g_new0 (GetStreamData, 1);
898
0
  data->address = g_strdup (address);
899
900
0
  task = g_task_new (NULL, cancellable, callback, user_data);
901
0
  g_task_set_source_tag (task, g_dbus_address_get_stream);
902
0
  g_task_set_task_data (task, data, (GDestroyNotify) get_stream_data_free);
903
0
  g_task_run_in_thread (task, get_stream_thread_func);
904
0
  g_object_unref (task);
905
0
}
906
907
/**
908
 * g_dbus_address_get_stream_finish:
909
 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
910
 * @out_guid: (optional) (out) (nullable): %NULL or return location to store the GUID extracted from @address, if any.
911
 * @error: Return location for error or %NULL.
912
 *
913
 * Finishes an operation started with g_dbus_address_get_stream().
914
 *
915
 * A server is not required to set a GUID, so @out_guid may be set to %NULL
916
 * even on success.
917
 *
918
 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
919
 *
920
 * Since: 2.26
921
 */
922
GIOStream *
923
g_dbus_address_get_stream_finish (GAsyncResult        *res,
924
                                  gchar              **out_guid,
925
                                  GError             **error)
926
0
{
927
0
  GTask *task;
928
0
  GetStreamData *data;
929
0
  GIOStream *ret;
930
931
0
  g_return_val_if_fail (g_task_is_valid (res, NULL), NULL);
932
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
933
934
0
  task = G_TASK (res);
935
0
  ret = g_task_propagate_pointer (task, error);
936
937
0
  if (ret != NULL && out_guid != NULL)
938
0
    {
939
0
      data = g_task_get_task_data (task);
940
0
      *out_guid = data->guid;
941
0
      data->guid = NULL;
942
0
    }
943
944
0
  return ret;
945
0
}
946
947
/**
948
 * g_dbus_address_get_stream_sync:
949
 * @address: A valid D-Bus address.
950
 * @out_guid: (optional) (out) (nullable): %NULL or return location to store the GUID extracted from @address, if any.
951
 * @cancellable: (nullable): A #GCancellable or %NULL.
952
 * @error: Return location for error or %NULL.
953
 *
954
 * Synchronously connects to an endpoint specified by @address and
955
 * sets up the connection so it is in a state to run the client-side
956
 * of the D-Bus authentication conversation. @address must be in the
957
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
958
 *
959
 * A server is not required to set a GUID, so @out_guid may be set to %NULL
960
 * even on success.
961
 *
962
 * This is a synchronous failable function. See
963
 * g_dbus_address_get_stream() for the asynchronous version.
964
 *
965
 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
966
 *
967
 * Since: 2.26
968
 */
969
GIOStream *
970
g_dbus_address_get_stream_sync (const gchar   *address,
971
                                gchar        **out_guid,
972
                                GCancellable  *cancellable,
973
                                GError       **error)
974
0
{
975
0
  GIOStream *ret;
976
0
  gchar **addr_array;
977
0
  guint n;
978
0
  GError *last_error;
979
980
0
  g_return_val_if_fail (address != NULL, NULL);
981
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
982
983
0
  ret = NULL;
984
0
  last_error = NULL;
985
986
0
  addr_array = g_strsplit (address, ";", 0);
987
0
  if (addr_array[0] == NULL)
988
0
    {
989
0
      last_error = g_error_new_literal (G_IO_ERROR,
990
0
                                        G_IO_ERROR_INVALID_ARGUMENT,
991
0
                                        _("The given address is empty"));
992
0
      goto out;
993
0
    }
994
995
0
  for (n = 0; addr_array[n] != NULL; n++)
996
0
    {
997
0
      const gchar *addr = addr_array[n];
998
0
      GError *this_error;
999
1000
0
      this_error = NULL;
1001
0
      ret = g_dbus_address_try_connect_one (addr,
1002
0
                                            out_guid,
1003
0
                                            cancellable,
1004
0
                                            &this_error);
1005
0
      if (ret != NULL)
1006
0
        {
1007
0
          goto out;
1008
0
        }
1009
0
      else
1010
0
        {
1011
0
          g_assert (this_error != NULL);
1012
0
          if (last_error != NULL)
1013
0
            g_error_free (last_error);
1014
0
          last_error = this_error;
1015
0
        }
1016
0
    }
1017
1018
0
 out:
1019
0
  if (ret != NULL)
1020
0
    {
1021
0
      if (last_error != NULL)
1022
0
        g_error_free (last_error);
1023
0
    }
1024
0
  else
1025
0
    {
1026
0
      g_assert (last_error != NULL);
1027
0
      g_propagate_error (error, last_error);
1028
0
    }
1029
1030
0
  g_strfreev (addr_array);
1031
0
  return ret;
1032
0
}
1033
1034
/* ---------------------------------------------------------------------------------------------------- */
1035
1036
/*
1037
 * Return the address of XDG_RUNTIME_DIR/bus if it exists, belongs to
1038
 * us, and is a socket, and we are on Unix.
1039
 */
1040
static gchar *
1041
get_session_address_xdg (void)
1042
0
{
1043
0
#ifdef G_OS_UNIX
1044
0
  gchar *ret = NULL;
1045
0
  gchar *bus;
1046
0
  gchar *tmp;
1047
0
  GStatBuf buf;
1048
1049
0
  bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL);
1050
1051
  /* if ENOENT, EPERM, etc., quietly don't use it */
1052
0
  if (g_stat (bus, &buf) < 0)
1053
0
    goto out;
1054
1055
  /* if it isn't ours, we have incorrectly inherited someone else's
1056
   * XDG_RUNTIME_DIR; silently don't use it
1057
   */
1058
0
  if (buf.st_uid != geteuid ())
1059
0
    goto out;
1060
1061
  /* if it isn't a socket, silently don't use it */
1062
0
  if ((buf.st_mode & S_IFMT) != S_IFSOCK)
1063
0
    goto out;
1064
1065
0
  tmp = g_dbus_address_escape_value (bus);
1066
0
  ret = g_strconcat ("unix:path=", tmp, NULL);
1067
0
  g_free (tmp);
1068
1069
0
out:
1070
0
  g_free (bus);
1071
0
  return ret;
1072
#else
1073
  return NULL;
1074
#endif
1075
0
}
1076
1077
/* ---------------------------------------------------------------------------------------------------- */
1078
1079
#ifdef G_OS_UNIX
1080
static gchar *
1081
get_session_address_dbus_launch (GError **error)
1082
0
{
1083
0
  gchar *ret;
1084
0
  gchar *machine_id;
1085
0
  gchar *command_line;
1086
0
  gchar *launch_stdout;
1087
0
  gchar *launch_stderr;
1088
0
  gint wait_status;
1089
0
  gchar *old_dbus_verbose;
1090
0
  gboolean restore_dbus_verbose;
1091
1092
0
  ret = NULL;
1093
0
  machine_id = NULL;
1094
0
  command_line = NULL;
1095
0
  launch_stdout = NULL;
1096
0
  launch_stderr = NULL;
1097
0
  restore_dbus_verbose = FALSE;
1098
0
  old_dbus_verbose = NULL;
1099
1100
  /* Don't run binaries as root if we're setuid. */
1101
0
  if (GLIB_PRIVATE_CALL (g_check_setuid) ())
1102
0
    {
1103
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1104
0
                   _("Cannot spawn a message bus when AT_SECURE is set"));
1105
0
      goto out;
1106
0
    }
1107
1108
0
  machine_id = _g_dbus_get_machine_id (error);
1109
0
  if (machine_id == NULL)
1110
0
    {
1111
0
      g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
1112
0
      goto out;
1113
0
    }
1114
1115
0
  if (g_getenv ("DISPLAY") == NULL)
1116
0
    {
1117
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
1118
0
                   _("Cannot autolaunch D-Bus without X11 $DISPLAY"));
1119
0
      goto out;
1120
0
    }
1121
1122
  /* We're using private libdbus facilities here. When everything
1123
   * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
1124
   * X11 property is correctly documented right now) we should
1125
   * consider using the spec instead of dbus-launch.
1126
   *
1127
   *   --autolaunch=MACHINEID
1128
   *          This option implies that dbus-launch should scan  for  a  previ‐
1129
   *          ously-started  session  and  reuse the values found there. If no
1130
   *          session is found, it will start a new session. The  --exit-with-
1131
   *          session option is implied if --autolaunch is given.  This option
1132
   *          is for the exclusive use of libdbus, you do not want to  use  it
1133
   *          manually. It may change in the future.
1134
   */
1135
1136
  /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
1137
0
  command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
1138
1139
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1140
0
    {
1141
0
      _g_dbus_debug_print_lock ();
1142
0
      g_print ("GDBus-debug:Address: Running '%s' to get bus address (possibly autolaunching)\n", command_line);
1143
0
      old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
1144
0
      restore_dbus_verbose = TRUE;
1145
0
      g_setenv ("DBUS_VERBOSE", "1", TRUE);
1146
0
      _g_dbus_debug_print_unlock ();
1147
0
    }
1148
1149
0
  if (!g_spawn_command_line_sync (command_line,
1150
0
                                  &launch_stdout,
1151
0
                                  &launch_stderr,
1152
0
                                  &wait_status,
1153
0
                                  error))
1154
0
    {
1155
0
      goto out;
1156
0
    }
1157
1158
0
  if (!g_spawn_check_wait_status (wait_status, error))
1159
0
    {
1160
0
      g_prefix_error (error, _("Error spawning command line “%s”: "), command_line);
1161
0
      goto out;
1162
0
    }
1163
1164
  /* From the dbus-launch(1) man page:
1165
   *
1166
   *   --binary-syntax Write to stdout a nul-terminated bus address,
1167
   *   then the bus PID as a binary integer of size sizeof(pid_t),
1168
   *   then the bus X window ID as a binary integer of size
1169
   *   sizeof(long).  Integers are in the machine's byte order, not
1170
   *   network byte order or any other canonical byte order.
1171
   */
1172
0
  ret = g_strdup (launch_stdout);
1173
1174
0
 out:
1175
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1176
0
    {
1177
0
      gchar *s;
1178
0
      _g_dbus_debug_print_lock ();
1179
0
      g_print ("GDBus-debug:Address: dbus-launch output:");
1180
0
      if (launch_stdout != NULL)
1181
0
        {
1182
0
          s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
1183
0
          g_print ("\n%s", s);
1184
0
          g_free (s);
1185
0
        }
1186
0
      else
1187
0
        {
1188
0
          g_print (" (none)\n");
1189
0
        }
1190
0
      g_print ("GDBus-debug:Address: dbus-launch stderr output:");
1191
0
      if (launch_stderr != NULL)
1192
0
        g_print ("\n%s", launch_stderr);
1193
0
      else
1194
0
        g_print (" (none)\n");
1195
0
      _g_dbus_debug_print_unlock ();
1196
0
    }
1197
1198
0
  g_free (machine_id);
1199
0
  g_free (command_line);
1200
0
  g_free (launch_stdout);
1201
0
  g_free (launch_stderr);
1202
0
  if (G_UNLIKELY (restore_dbus_verbose))
1203
0
    {
1204
0
      if (old_dbus_verbose != NULL)
1205
0
        g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
1206
0
      else
1207
0
        g_unsetenv ("DBUS_VERBOSE");
1208
0
    }
1209
0
  g_free (old_dbus_verbose);
1210
0
  return ret;
1211
0
}
1212
1213
/* end of G_OS_UNIX case */
1214
#elif defined(G_OS_WIN32)
1215
1216
static gchar *
1217
get_session_address_dbus_launch (GError **error)
1218
{
1219
  return _g_dbus_win32_get_session_address_dbus_launch (error);
1220
}
1221
1222
#else /* neither G_OS_UNIX nor G_OS_WIN32 */
1223
static gchar *
1224
get_session_address_dbus_launch (GError **error)
1225
{
1226
  g_set_error (error,
1227
               G_IO_ERROR,
1228
               G_IO_ERROR_FAILED,
1229
               _("Cannot determine session bus address (not implemented for this OS)"));
1230
  return NULL;
1231
}
1232
#endif /* neither G_OS_UNIX nor G_OS_WIN32 */
1233
1234
/* ---------------------------------------------------------------------------------------------------- */
1235
1236
static gchar *
1237
get_session_address_platform_specific (GError **error)
1238
0
{
1239
0
  gchar *ret;
1240
1241
  /* Use XDG_RUNTIME_DIR/bus if it exists and is suitable. This is appropriate
1242
   * for systems using the "a session is a user-session" model described in
1243
   * <http://lists.freedesktop.org/archives/dbus/2015-January/016522.html>,
1244
   * and implemented in dbus >= 1.9.14 and sd-bus.
1245
   *
1246
   * On systems following the more traditional "a session is a login-session"
1247
   * model, this will fail and we'll fall through to X11 autolaunching
1248
   * (dbus-launch) below.
1249
   */
1250
0
  ret = get_session_address_xdg ();
1251
1252
0
  if (ret != NULL)
1253
0
    return ret;
1254
1255
  /* TODO (#694472): try launchd on OS X, like
1256
   * _dbus_lookup_session_address_launchd() does, since
1257
   * 'dbus-launch --autolaunch' probably won't work there
1258
   */
1259
1260
  /* As a last resort, try the "autolaunch:" transport. On Unix this means
1261
   * X11 autolaunching; on Windows this means a different autolaunching
1262
   * mechanism based on shared memory.
1263
   */
1264
0
  return get_session_address_dbus_launch (error);
1265
0
}
1266
1267
/* ---------------------------------------------------------------------------------------------------- */
1268
1269
/**
1270
 * g_dbus_address_get_for_bus_sync:
1271
 * @bus_type: a #GBusType
1272
 * @cancellable: (nullable): a #GCancellable or %NULL
1273
 * @error: return location for error or %NULL
1274
 *
1275
 * Synchronously looks up the D-Bus address for the well-known message
1276
 * bus instance specified by @bus_type. This may involve using various
1277
 * platform specific mechanisms.
1278
 *
1279
 * The returned address will be in the
1280
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
1281
 *
1282
 * Returns: (transfer full): a valid D-Bus address string for @bus_type or
1283
 *     %NULL if @error is set
1284
 *
1285
 * Since: 2.26
1286
 */
1287
gchar *
1288
g_dbus_address_get_for_bus_sync (GBusType       bus_type,
1289
                                 GCancellable  *cancellable,
1290
                                 GError       **error)
1291
0
{
1292
0
  gboolean has_elevated_privileges = GLIB_PRIVATE_CALL (g_check_setuid) ();
1293
0
  gchar *ret, *s = NULL;
1294
0
  const gchar *starter_bus;
1295
0
  GError *local_error;
1296
1297
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1298
1299
0
  ret = NULL;
1300
0
  local_error = NULL;
1301
1302
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1303
0
    {
1304
0
      guint n;
1305
0
      _g_dbus_debug_print_lock ();
1306
0
      s = _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type);
1307
0
      g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type '%s'\n",
1308
0
               s);
1309
0
      g_free (s);
1310
0
      for (n = 0; n < 3; n++)
1311
0
        {
1312
0
          const gchar *k;
1313
0
          const gchar *v;
1314
0
          switch (n)
1315
0
            {
1316
0
            case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
1317
0
            case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
1318
0
            case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
1319
0
            default: g_assert_not_reached ();
1320
0
            }
1321
0
          v = g_getenv (k);
1322
0
          g_print ("GDBus-debug:Address: env var %s", k);
1323
0
          if (v != NULL)
1324
0
            g_print ("='%s'\n", v);
1325
0
          else
1326
0
            g_print (" is not set\n");
1327
0
        }
1328
0
      _g_dbus_debug_print_unlock ();
1329
0
    }
1330
1331
  /* Don’t load the addresses from the environment if running as setuid, as they
1332
   * come from an unprivileged caller. */
1333
0
  switch (bus_type)
1334
0
    {
1335
0
    case G_BUS_TYPE_SYSTEM:
1336
0
      if (has_elevated_privileges)
1337
0
        ret = NULL;
1338
0
      else
1339
0
        ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
1340
1341
0
      if (ret == NULL)
1342
0
        {
1343
          /* While the D-Bus specification says this must be `/var/run/dbus/system_bus_socket`,
1344
           * a footnote allows it to use localstatedir:
1345
           * https://dbus.freedesktop.org/doc/dbus-specification.html#ftn.id-1.13.6.4.3.3
1346
           * or, on systems where /run is the same as /var/run, runstatedir:
1347
           * https://gitlab.freedesktop.org/dbus/dbus/-/merge_requests/209 */
1348
0
          ret = g_strdup ("unix:path=" GLIB_RUNSTATEDIR "/dbus/system_bus_socket");
1349
0
        }
1350
0
      break;
1351
1352
0
    case G_BUS_TYPE_SESSION:
1353
0
      if (has_elevated_privileges)
1354
0
        ret = NULL;
1355
0
      else
1356
0
        ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
1357
1358
0
      if (ret == NULL)
1359
0
        {
1360
0
          ret = get_session_address_platform_specific (&local_error);
1361
0
        }
1362
0
      break;
1363
1364
0
    case G_BUS_TYPE_STARTER:
1365
0
      starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
1366
0
      if (g_strcmp0 (starter_bus, "session") == 0)
1367
0
        {
1368
0
          ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
1369
0
          goto out;
1370
0
        }
1371
0
      else if (g_strcmp0 (starter_bus, "system") == 0)
1372
0
        {
1373
0
          ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
1374
0
          goto out;
1375
0
        }
1376
0
      else
1377
0
        {
1378
0
          if (starter_bus != NULL)
1379
0
            {
1380
0
              g_set_error (&local_error,
1381
0
                           G_IO_ERROR,
1382
0
                           G_IO_ERROR_FAILED,
1383
0
                           _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1384
0
                             " — unknown value “%s”"),
1385
0
                           starter_bus);
1386
0
            }
1387
0
          else
1388
0
            {
1389
0
              g_set_error_literal (&local_error,
1390
0
                                   G_IO_ERROR,
1391
0
                                   G_IO_ERROR_FAILED,
1392
0
                                   _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1393
0
                                     "variable is not set"));
1394
0
            }
1395
0
        }
1396
0
      break;
1397
1398
0
    default:
1399
0
      g_set_error (&local_error,
1400
0
                   G_IO_ERROR,
1401
0
                   G_IO_ERROR_FAILED,
1402
0
                   _("Unknown bus type %d"),
1403
0
                   bus_type);
1404
0
      break;
1405
0
    }
1406
1407
0
 out:
1408
0
  if (G_UNLIKELY (_g_dbus_debug_address ()))
1409
0
    {
1410
0
      _g_dbus_debug_print_lock ();
1411
0
      s = _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type);
1412
0
      if (ret != NULL)
1413
0
        {
1414
0
          g_print ("GDBus-debug:Address: Returning address '%s' for bus type '%s'\n",
1415
0
                   ret, s);
1416
0
        }
1417
0
      else
1418
0
        {
1419
0
          g_print ("GDBus-debug:Address: Cannot look-up address bus type '%s': %s\n",
1420
0
                   s, local_error ? local_error->message : "");
1421
0
        }
1422
0
      g_free (s);
1423
0
      _g_dbus_debug_print_unlock ();
1424
0
    }
1425
1426
0
  if (local_error != NULL)
1427
0
    g_propagate_error (error, local_error);
1428
1429
0
  return ret;
1430
0
}
1431
1432
/**
1433
 * g_dbus_address_escape_value:
1434
 * @string: an unescaped string to be included in a D-Bus address
1435
 *     as the value in a key-value pair
1436
 *
1437
 * Escape @string so it can appear in a D-Bus address as the value
1438
 * part of a key-value pair.
1439
 *
1440
 * For instance, if @string is `/run/bus-for-:0`,
1441
 * this function would return `/run/bus-for-%3A0`,
1442
 * which could be used in a D-Bus address like
1443
 * `unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0`.
1444
 *
1445
 * Returns: (transfer full): a copy of @string with all
1446
 *     non-optionally-escaped bytes escaped
1447
 *
1448
 * Since: 2.36
1449
 */
1450
gchar *
1451
g_dbus_address_escape_value (const gchar *string)
1452
0
{
1453
0
  GString *s;
1454
0
  gsize i;
1455
1456
0
  g_return_val_if_fail (string != NULL, NULL);
1457
1458
  /* There will often not be anything needing escaping at all. */
1459
0
  s = g_string_sized_new (strlen (string));
1460
1461
  /* D-Bus address escaping is mostly the same as URI escaping... */
1462
0
  g_string_append_uri_escaped (s, string, "\\/", FALSE);
1463
1464
  /* ... but '~' is an unreserved character in URIs, but a
1465
   * non-optionally-escaped character in D-Bus addresses. */
1466
0
  for (i = 0; i < s->len; i++)
1467
0
    {
1468
0
      if (G_UNLIKELY (s->str[i] == '~'))
1469
0
        {
1470
0
          s->str[i] = '%';
1471
0
          g_string_insert (s, i + 1, "7E");
1472
0
          i += 2;
1473
0
        }
1474
0
    }
1475
1476
0
  return g_string_free (s, FALSE);
1477
0
}