1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
29 #include "gdbusutils.h"
30 #include "gdbusaddress.h"
31 #include "gdbuserror.h"
32 #include "gioenumtypes.h"
33 #include "gnetworkaddress.h"
34 #include "gsocketclient.h"
35 #include "giostream.h"
36 #include "gasyncresult.h"
37 #include "gsimpleasyncresult.h"
38 #include "gdbusprivate.h"
41 #include <gio/gunixsocketaddress.h>
48 * SECTION:gdbusaddress
49 * @title: D-Bus Addresses
50 * @short_description: D-Bus connection endpoints
53 * Routines for working with D-Bus addresses. A D-Bus address is a string
54 * like "unix:tmpdir=/tmp/my-app-name". The exact format of addresses
55 * is explained in detail in the <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html#addresses">D-Bus specification</link>.
58 /* ---------------------------------------------------------------------------------------------------- */
64 * Checks if @string is a D-Bus address.
66 * This doesn't check if @string is actually supported by #GDBusServer
67 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
70 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
75 g_dbus_is_address (const gchar *string)
83 g_return_val_if_fail (string != NULL, FALSE);
85 a = g_strsplit (string, ";", 0);
86 for (n = 0; a[n] != NULL; n++)
88 if (!_g_dbus_address_parse_entry (a[n],
103 is_valid_unix (const gchar *address_entry,
104 GHashTable *key_value_pairs,
112 const gchar *abstract;
120 keys = g_hash_table_get_keys (key_value_pairs);
121 for (l = keys; l != NULL; l = l->next)
123 const gchar *key = l->data;
124 if (g_strcmp0 (key, "path") == 0)
125 path = g_hash_table_lookup (key_value_pairs, key);
126 else if (g_strcmp0 (key, "tmpdir") == 0)
127 tmpdir = g_hash_table_lookup (key_value_pairs, key);
128 else if (g_strcmp0 (key, "abstract") == 0)
129 abstract = g_hash_table_lookup (key_value_pairs, key);
134 G_IO_ERROR_INVALID_ARGUMENT,
135 _("Unsupported key `%s' in address entry `%s'"),
144 if (tmpdir != NULL || abstract != NULL)
146 /* TODO: validate path */
148 else if (tmpdir != NULL)
150 if (path != NULL || abstract != NULL)
152 /* TODO: validate tmpdir */
154 else if (abstract != NULL)
156 if (path != NULL || tmpdir != NULL)
158 /* TODO: validate abstract */
164 G_IO_ERROR_INVALID_ARGUMENT,
165 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys)"),
177 G_IO_ERROR_INVALID_ARGUMENT,
178 _("Meaningless key/value pair combination in address entry `%s'"),
188 is_valid_nonce_tcp (const gchar *address_entry,
189 GHashTable *key_value_pairs,
198 const gchar *nonce_file;
209 keys = g_hash_table_get_keys (key_value_pairs);
210 for (l = keys; l != NULL; l = l->next)
212 const gchar *key = l->data;
213 if (g_strcmp0 (key, "host") == 0)
214 host = g_hash_table_lookup (key_value_pairs, key);
215 else if (g_strcmp0 (key, "port") == 0)
216 port = g_hash_table_lookup (key_value_pairs, key);
217 else if (g_strcmp0 (key, "family") == 0)
218 family = g_hash_table_lookup (key_value_pairs, key);
219 else if (g_strcmp0 (key, "noncefile") == 0)
220 nonce_file = g_hash_table_lookup (key_value_pairs, key);
225 G_IO_ERROR_INVALID_ARGUMENT,
226 _("Unsupported key `%s' in address entry `%s'"),
235 port_num = strtol (port, &endp, 10);
236 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
240 G_IO_ERROR_INVALID_ARGUMENT,
241 _("Error in address `%s' - the port attribute is malformed"),
247 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
251 G_IO_ERROR_INVALID_ARGUMENT,
252 _("Error in address `%s' - the family attribute is malformed"),
266 is_valid_tcp (const gchar *address_entry,
267 GHashTable *key_value_pairs,
285 keys = g_hash_table_get_keys (key_value_pairs);
286 for (l = keys; l != NULL; l = l->next)
288 const gchar *key = l->data;
289 if (g_strcmp0 (key, "host") == 0)
290 host = g_hash_table_lookup (key_value_pairs, key);
291 else if (g_strcmp0 (key, "port") == 0)
292 port = g_hash_table_lookup (key_value_pairs, key);
293 else if (g_strcmp0 (key, "family") == 0)
294 family = g_hash_table_lookup (key_value_pairs, key);
299 G_IO_ERROR_INVALID_ARGUMENT,
300 _("Unsupported key `%s' in address entry `%s'"),
309 port_num = strtol (port, &endp, 10);
310 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
314 G_IO_ERROR_INVALID_ARGUMENT,
315 _("Error in address `%s' - the port attribute is malformed"),
321 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
325 G_IO_ERROR_INVALID_ARGUMENT,
326 _("Error in address `%s' - the family attribute is malformed"),
340 * g_dbus_is_supported_address:
342 * @error: Return location for error or %NULL.
344 * Like g_dbus_is_address() but also checks if the library suppors the
345 * transports in @string and that key/value pairs for each transport
348 * Returns: %TRUE if @string is a valid D-Bus address that is
349 * supported by this library, %FALSE if @error is set.
354 g_dbus_is_supported_address (const gchar *string,
363 g_return_val_if_fail (string != NULL, FALSE);
364 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
366 a = g_strsplit (string, ";", 0);
367 for (n = 0; a[n] != NULL; n++)
369 gchar *transport_name;
370 GHashTable *key_value_pairs;
373 if (!_g_dbus_address_parse_entry (a[n],
380 if (g_strcmp0 (transport_name, "unix") == 0)
381 supported = is_valid_unix (a[n], key_value_pairs, error);
382 else if (g_strcmp0 (transport_name, "tcp") == 0)
383 supported = is_valid_tcp (a[n], key_value_pairs, error);
384 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
385 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
387 g_free (transport_name);
388 g_hash_table_unref (key_value_pairs);
399 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
405 _g_dbus_address_parse_entry (const gchar *address_entry,
406 gchar **out_transport_name,
407 GHashTable **out_key_value_pairs,
411 GHashTable *key_value_pairs;
412 gchar *transport_name;
419 transport_name = NULL;
420 key_value_pairs = NULL;
422 s = strchr (address_entry, ':');
427 G_IO_ERROR_INVALID_ARGUMENT,
428 _("Address element `%s', does not contain a colon (:)"),
433 transport_name = g_strndup (address_entry, s - address_entry);
434 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
436 kv_pairs = g_strsplit (s + 1, ",", 0);
437 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
439 const gchar *kv_pair = kv_pairs[n];
443 s = strchr (kv_pair, '=');
448 G_IO_ERROR_INVALID_ARGUMENT,
449 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
456 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
457 key = g_uri_unescape_segment (kv_pair, s, NULL);
458 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
459 g_hash_table_insert (key_value_pairs, key, value);
465 g_strfreev (kv_pairs);
468 if (out_transport_name != NULL)
469 *out_transport_name = transport_name;
471 g_free (transport_name);
472 if (out_key_value_pairs != NULL)
473 *out_key_value_pairs = key_value_pairs;
474 else if (key_value_pairs != NULL)
475 g_hash_table_unref (key_value_pairs);
479 g_free (transport_name);
480 if (key_value_pairs != NULL)
481 g_hash_table_unref (key_value_pairs);
486 /* ---------------------------------------------------------------------------------------------------- */
488 /* TODO: Declare an extension point called GDBusTransport (or similar)
489 * and move code below to extensions implementing said extension
490 * point. That way we can implement a D-Bus transport over X11 without
491 * making libgio link to libX11...
494 g_dbus_address_connect (const gchar *address_entry,
495 const gchar *transport_name,
496 GHashTable *key_value_pairs,
497 GCancellable *cancellable,
501 GSocketConnectable *connectable;
502 const gchar *nonce_file;
512 else if (g_strcmp0 (transport_name, "unix") == 0)
515 const gchar *abstract;
516 path = g_hash_table_lookup (key_value_pairs, "path");
517 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
518 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
522 G_IO_ERROR_INVALID_ARGUMENT,
523 _("Error in address `%s' - the unix transport requires exactly one of the "
524 "keys `path' or `abstract' to be set"),
527 else if (path != NULL)
529 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
531 else if (abstract != NULL)
533 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
535 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
539 g_assert_not_reached ();
543 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
551 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
553 host = g_hash_table_lookup (key_value_pairs, "host");
558 G_IO_ERROR_INVALID_ARGUMENT,
559 _("Error in address `%s' - the host attribute is missing or malformed"),
564 s = g_hash_table_lookup (key_value_pairs, "port");
567 port = strtol (s, &endp, 10);
568 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
572 G_IO_ERROR_INVALID_ARGUMENT,
573 _("Error in address `%s' - the port attribute is missing or malformed"),
581 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
582 if (nonce_file == NULL)
586 G_IO_ERROR_INVALID_ARGUMENT,
587 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
593 /* TODO: deal with family */
594 connectable = g_network_address_new (host, port);
600 G_IO_ERROR_INVALID_ARGUMENT,
601 _("Unknown or unsupported transport `%s' for address `%s'"),
606 if (connectable != NULL)
608 GSocketClient *client;
609 GSocketConnection *connection;
611 g_assert (ret == NULL);
612 client = g_socket_client_new ();
613 connection = g_socket_client_connect (client,
617 g_object_unref (connectable);
618 g_object_unref (client);
619 if (connection == NULL)
622 ret = G_IO_STREAM (connection);
624 if (nonce_file != NULL)
626 gchar *nonce_contents;
629 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
630 if (!g_file_get_contents (nonce_file,
635 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
636 g_object_unref (ret);
641 if (nonce_length != 16)
645 G_IO_ERROR_INVALID_ARGUMENT,
646 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
649 g_free (nonce_contents);
650 g_object_unref (ret);
655 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
662 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
663 g_object_unref (ret);
665 g_free (nonce_contents);
668 g_free (nonce_contents);
678 g_dbus_address_try_connect_one (const gchar *address_entry,
680 GCancellable *cancellable,
684 GHashTable *key_value_pairs;
685 gchar *transport_name;
689 transport_name = NULL;
690 key_value_pairs = NULL;
692 if (!_g_dbus_address_parse_entry (address_entry,
698 ret = g_dbus_address_connect (address_entry,
706 /* TODO: validate that guid is of correct format */
707 guid = g_hash_table_lookup (key_value_pairs, "guid");
708 if (guid != NULL && out_guid != NULL)
709 *out_guid = g_strdup (guid);
712 g_free (transport_name);
713 if (key_value_pairs != NULL)
714 g_hash_table_unref (key_value_pairs);
719 /* ---------------------------------------------------------------------------------------------------- */
728 get_stream_data_free (GetStreamData *data)
730 g_free (data->address);
731 if (data->stream != NULL)
732 g_object_unref (data->stream);
738 get_stream_thread_func (GSimpleAsyncResult *res,
740 GCancellable *cancellable)
745 data = g_simple_async_result_get_op_res_gpointer (res);
748 data->stream = g_dbus_address_get_stream_sync (data->address,
752 if (data->stream == NULL)
754 g_simple_async_result_set_from_error (res, error);
755 g_error_free (error);
760 * g_dbus_address_get_stream:
761 * @address: A valid D-Bus address.
762 * @cancellable: A #GCancellable or %NULL.
763 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
764 * @user_data: Data to pass to @callback.
766 * Asynchronously connects to an endpoint specified by @address and
767 * sets up the connection so it is in a state to run the client-side
768 * of the D-Bus authentication conversation.
770 * When the operation is finished, @callback will be invoked. You can
771 * then call g_dbus_address_get_stream_finish() to get the result of
774 * This is an asynchronous failable function. See
775 * g_dbus_address_get_stream_sync() for the synchronous version.
780 g_dbus_address_get_stream (const gchar *address,
781 GCancellable *cancellable,
782 GAsyncReadyCallback callback,
785 GSimpleAsyncResult *res;
788 g_return_if_fail (address != NULL);
790 res = g_simple_async_result_new (NULL,
793 g_dbus_address_get_stream);
794 data = g_new0 (GetStreamData, 1);
795 data->address = g_strdup (address);
796 g_simple_async_result_set_op_res_gpointer (res,
798 (GDestroyNotify) get_stream_data_free);
799 g_simple_async_result_run_in_thread (res,
800 get_stream_thread_func,
803 g_object_unref (res);
807 * g_dbus_address_get_stream_finish:
808 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
809 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
810 * @error: Return location for error or %NULL.
812 * Finishes an operation started with g_dbus_address_get_stream().
814 * Returns: A #GIOStream or %NULL if @error is set.
819 g_dbus_address_get_stream_finish (GAsyncResult *res,
823 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
827 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
828 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
830 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
834 data = g_simple_async_result_get_op_res_gpointer (simple);
835 if (g_simple_async_result_propagate_error (simple, error))
838 ret = g_object_ref (data->stream);
839 if (out_guid != NULL)
840 *out_guid = g_strdup (data->guid);
847 * g_dbus_address_get_stream_sync:
848 * @address: A valid D-Bus address.
849 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
850 * @cancellable: A #GCancellable or %NULL.
851 * @error: Return location for error or %NULL.
853 * Synchronously connects to an endpoint specified by @address and
854 * sets up the connection so it is in a state to run the client-side
855 * of the D-Bus authentication conversation.
857 * This is a synchronous failable function. See
858 * g_dbus_address_get_stream() for the asynchronous version.
860 * Returns: A #GIOStream or %NULL if @error is set.
865 g_dbus_address_get_stream_sync (const gchar *address,
867 GCancellable *cancellable,
875 g_return_val_if_fail (address != NULL, NULL);
876 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
881 addr_array = g_strsplit (address, ";", 0);
883 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
885 const gchar *addr = addr_array[n];
888 ret = g_dbus_address_try_connect_one (addr,
898 g_assert (this_error != NULL);
899 if (last_error != NULL)
900 g_error_free (last_error);
901 last_error = this_error;
908 if (last_error != NULL)
909 g_error_free (last_error);
913 g_assert (last_error != NULL);
914 g_propagate_error (error, last_error);
917 g_strfreev (addr_array);
921 /* ---------------------------------------------------------------------------------------------------- */
923 /* TODO: implement for UNIX, Win32 and OS X */
925 get_session_address_platform_specific (void)
930 /* ---------------------------------------------------------------------------------------------------- */
933 * g_dbus_address_get_for_bus_sync:
934 * @bus_type: A #GBusType.
935 * @cancellable: A #GCancellable or %NULL.
936 * @error: Return location for error or %NULL.
938 * Synchronously looks up the D-Bus address for the well-known message
939 * bus instance specified by @bus_type. This may involve using various
940 * platform specific mechanisms.
942 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
947 g_dbus_address_get_for_bus_sync (GBusType bus_type,
948 GCancellable *cancellable,
952 const gchar *starter_bus;
954 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
960 case G_BUS_TYPE_SYSTEM:
961 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
964 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
968 case G_BUS_TYPE_SESSION:
969 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
972 ret = get_session_address_platform_specific ();
978 _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
983 case G_BUS_TYPE_STARTER:
984 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
985 if (g_strcmp0 (starter_bus, "session") == 0)
987 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
990 else if (g_strcmp0 (starter_bus, "system") == 0)
992 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
997 if (starter_bus != NULL)
1002 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1003 " - unknown value `%s'"),
1008 g_set_error_literal (error,
1011 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1012 "variable is not set"));
1021 _("Unknown bus type %d"),
1030 #define __G_DBUS_ADDRESS_C__
1031 #include "gioaliasdef.c"