Coverage Report

Created: 2025-07-01 07:09

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