1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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>
28 #include "gdbusutils.h"
29 #include "gdbusaddress.h"
30 #include "gdbuserror.h"
31 #include "gioenumtypes.h"
32 #include "gdbusprivate.h"
35 #include <gio/gunixsocketaddress.h>
41 * SECTION:gdbusaddress
42 * @title: D-Bus Addresses
43 * @short_description: D-Bus connection endpoints
46 * Routines for working with D-Bus addresses.
49 /* ---------------------------------------------------------------------------------------------------- */
55 * Checks if @string is a D-Bus address.
57 * This doesn't check if @string is actually supported by #GDBusServer
58 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
61 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
66 g_dbus_is_address (const gchar *string)
74 g_return_val_if_fail (string != NULL, FALSE);
76 a = g_strsplit (string, ";", 0);
77 for (n = 0; a[n] != NULL; n++)
79 if (!_g_dbus_address_parse_entry (a[n],
94 is_valid_unix (const gchar *address_entry,
95 GHashTable *key_value_pairs,
103 const gchar *abstract;
111 keys = g_hash_table_get_keys (key_value_pairs);
112 for (l = keys; l != NULL; l = l->next)
114 const gchar *key = l->data;
115 if (g_strcmp0 (key, "path") == 0)
116 path = g_hash_table_lookup (key_value_pairs, key);
117 else if (g_strcmp0 (key, "tmpdir") == 0)
118 tmpdir = g_hash_table_lookup (key_value_pairs, key);
119 else if (g_strcmp0 (key, "abstract") == 0)
120 abstract = g_hash_table_lookup (key_value_pairs, key);
125 G_IO_ERROR_INVALID_ARGUMENT,
126 _("Unsupported key `%s' in address entry `%s'"),
135 if (tmpdir != NULL || abstract != NULL)
137 /* TODO: validate path */
139 else if (tmpdir != NULL)
141 if (path != NULL || abstract != NULL)
143 /* TODO: validate tmpdir */
145 else if (abstract != NULL)
147 if (path != NULL || tmpdir != NULL)
149 /* TODO: validate abstract */
155 G_IO_ERROR_INVALID_ARGUMENT,
156 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys"),
168 G_IO_ERROR_INVALID_ARGUMENT,
169 _("Meaningless key/value pair combination in address entry `%s'"),
179 is_valid_nonce_tcp (const gchar *address_entry,
180 GHashTable *key_value_pairs,
189 const gchar *nonce_file;
200 keys = g_hash_table_get_keys (key_value_pairs);
201 for (l = keys; l != NULL; l = l->next)
203 const gchar *key = l->data;
204 if (g_strcmp0 (key, "host") == 0)
205 host = g_hash_table_lookup (key_value_pairs, key);
206 else if (g_strcmp0 (key, "port") == 0)
207 port = g_hash_table_lookup (key_value_pairs, key);
208 else if (g_strcmp0 (key, "family") == 0)
209 family = g_hash_table_lookup (key_value_pairs, key);
210 else if (g_strcmp0 (key, "noncefile") == 0)
211 nonce_file = g_hash_table_lookup (key_value_pairs, key);
216 G_IO_ERROR_INVALID_ARGUMENT,
217 _("Unsupported key `%s' in address entry `%s'"),
226 port_num = strtol (port, &endp, 10);
227 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
231 G_IO_ERROR_INVALID_ARGUMENT,
232 _("Error in address `%s' - the port attribute is malformed"),
238 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
242 G_IO_ERROR_INVALID_ARGUMENT,
243 _("Error in address `%s' - the family attribute is malformed"),
257 is_valid_tcp (const gchar *address_entry,
258 GHashTable *key_value_pairs,
276 keys = g_hash_table_get_keys (key_value_pairs);
277 for (l = keys; l != NULL; l = l->next)
279 const gchar *key = l->data;
280 if (g_strcmp0 (key, "host") == 0)
281 host = g_hash_table_lookup (key_value_pairs, key);
282 else if (g_strcmp0 (key, "port") == 0)
283 port = g_hash_table_lookup (key_value_pairs, key);
284 else if (g_strcmp0 (key, "family") == 0)
285 family = g_hash_table_lookup (key_value_pairs, key);
290 G_IO_ERROR_INVALID_ARGUMENT,
291 _("Unsupported key `%s' in address entry `%s'"),
300 port_num = strtol (port, &endp, 10);
301 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
305 G_IO_ERROR_INVALID_ARGUMENT,
306 _("Error in address `%s' - the port attribute is malformed"),
312 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
316 G_IO_ERROR_INVALID_ARGUMENT,
317 _("Error in address `%s' - the family attribute is malformed"),
331 * g_dbus_is_supported_address:
333 * @error: Return location for error or %NULL.
335 * Like g_dbus_is_address() but also checks if the library suppors the
336 * transports in @string and that key/value pairs for each transport
339 * Returns: %TRUE if @string is a valid D-Bus address that is
340 * supported by this library, %FALSE if @error is set.
345 g_dbus_is_supported_address (const gchar *string,
354 g_return_val_if_fail (string != NULL, FALSE);
355 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
357 a = g_strsplit (string, ";", 0);
358 for (n = 0; a[n] != NULL; n++)
360 gchar *transport_name;
361 GHashTable *key_value_pairs;
364 if (!_g_dbus_address_parse_entry (a[n],
371 if (g_strcmp0 (transport_name, "unix") == 0)
372 supported = is_valid_unix (a[n], key_value_pairs, error);
373 else if (g_strcmp0 (transport_name, "tcp") == 0)
374 supported = is_valid_tcp (a[n], key_value_pairs, error);
375 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
376 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
378 g_free (transport_name);
379 g_hash_table_unref (key_value_pairs);
390 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
396 _g_dbus_address_parse_entry (const gchar *address_entry,
397 gchar **out_transport_name,
398 GHashTable **out_key_value_pairs,
402 GHashTable *key_value_pairs;
403 gchar *transport_name;
410 transport_name = NULL;
411 key_value_pairs = NULL;
413 s = strchr (address_entry, ':');
418 G_IO_ERROR_INVALID_ARGUMENT,
419 _("Address element `%s', does not contain a colon (:)"),
424 transport_name = g_strndup (address_entry, s - address_entry);
425 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
427 kv_pairs = g_strsplit (s + 1, ",", 0);
428 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
430 const gchar *kv_pair = kv_pairs[n];
434 s = strchr (kv_pair, '=');
439 G_IO_ERROR_INVALID_ARGUMENT,
440 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
447 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
448 key = g_uri_unescape_segment (kv_pair, s, NULL);
449 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
450 g_hash_table_insert (key_value_pairs, key, value);
456 g_strfreev (kv_pairs);
459 if (out_transport_name != NULL)
460 *out_transport_name = transport_name;
462 g_free (transport_name);
463 if (out_key_value_pairs != NULL)
464 *out_key_value_pairs = key_value_pairs;
465 else if (key_value_pairs != NULL)
466 g_hash_table_unref (key_value_pairs);
470 g_free (transport_name);
471 if (key_value_pairs != NULL)
472 g_hash_table_unref (key_value_pairs);
477 /* ---------------------------------------------------------------------------------------------------- */
479 /* TODO: Declare an extension point called GDBusTransport (or similar)
480 * and move code below to extensions implementing said extension
481 * point. That way we can implement a D-Bus transport over X11 without
482 * making libgio link to libX11...
485 g_dbus_address_connect (const gchar *address_entry,
486 const gchar *transport_name,
487 GHashTable *key_value_pairs,
488 GCancellable *cancellable,
492 GSocketConnectable *connectable;
493 const gchar *nonce_file;
503 else if (g_strcmp0 (transport_name, "unix") == 0)
506 const gchar *abstract;
507 path = g_hash_table_lookup (key_value_pairs, "path");
508 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
509 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
513 G_IO_ERROR_INVALID_ARGUMENT,
514 _("Error in address `%s' - the unix transport requires exactly one of the "
515 "keys `path' or `abstract' to be set"),
518 else if (path != NULL)
520 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
522 else if (abstract != NULL)
524 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
526 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
530 g_assert_not_reached ();
534 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
542 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
544 host = g_hash_table_lookup (key_value_pairs, "host");
549 G_IO_ERROR_INVALID_ARGUMENT,
550 _("Error in address `%s' - the host attribute is missing or malformed"),
555 s = g_hash_table_lookup (key_value_pairs, "port");
558 port = strtol (s, &endp, 10);
559 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
563 G_IO_ERROR_INVALID_ARGUMENT,
564 _("Error in address `%s' - the port attribute is missing or malformed"),
572 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
573 if (nonce_file == NULL)
577 G_IO_ERROR_INVALID_ARGUMENT,
578 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
584 /* TODO: deal with family */
585 connectable = g_network_address_new (host, port);
591 G_IO_ERROR_INVALID_ARGUMENT,
592 _("Unknown or unsupported transport `%s' for address `%s'"),
597 if (connectable != NULL)
599 GSocketClient *client;
600 GSocketConnection *connection;
602 g_assert (ret == NULL);
603 client = g_socket_client_new ();
604 connection = g_socket_client_connect (client,
608 g_object_unref (connectable);
609 g_object_unref (client);
610 if (connection == NULL)
613 ret = G_IO_STREAM (connection);
615 if (nonce_file != NULL)
617 gchar *nonce_contents;
620 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
621 if (!g_file_get_contents (nonce_file,
626 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
627 g_object_unref (ret);
632 if (nonce_length != 16)
636 G_IO_ERROR_INVALID_ARGUMENT,
637 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
640 g_free (nonce_contents);
641 g_object_unref (ret);
646 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
653 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
654 g_object_unref (ret);
656 g_free (nonce_contents);
659 g_free (nonce_contents);
669 g_dbus_address_try_connect_one (const gchar *address_entry,
671 GCancellable *cancellable,
675 GHashTable *key_value_pairs;
676 gchar *transport_name;
680 transport_name = NULL;
681 key_value_pairs = NULL;
683 if (!_g_dbus_address_parse_entry (address_entry,
689 ret = g_dbus_address_connect (address_entry,
697 /* TODO: validate that guid is of correct format */
698 guid = g_hash_table_lookup (key_value_pairs, "guid");
699 if (guid != NULL && out_guid != NULL)
700 *out_guid = g_strdup (guid);
703 g_free (transport_name);
704 if (key_value_pairs != NULL)
705 g_hash_table_unref (key_value_pairs);
710 /* ---------------------------------------------------------------------------------------------------- */
719 get_stream_data_free (GetStreamData *data)
721 g_free (data->address);
722 if (data->stream != NULL)
723 g_object_unref (data->stream);
729 get_stream_thread_func (GSimpleAsyncResult *res,
731 GCancellable *cancellable)
736 data = g_simple_async_result_get_op_res_gpointer (res);
739 data->stream = g_dbus_address_get_stream_sync (data->address,
743 if (data->stream == NULL)
745 g_simple_async_result_set_from_error (res, error);
746 g_error_free (error);
751 * g_dbus_address_get_stream:
752 * @address: A valid D-Bus address.
753 * @cancellable: A #GCancellable or %NULL.
754 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
755 * @user_data: Data to pass to @callback.
757 * Asynchronously connects to an endpoint specified by @address and
758 * sets up the connection so it is in a state to run the client-side
759 * of the D-Bus authentication conversation.
761 * When the operation is finished, @callback will be invoked. You can
762 * then call g_dbus_address_get_stream_finish() to get the result of
765 * This is an asynchronous failable function. See
766 * g_dbus_address_get_stream_sync() for the synchronous version.
771 g_dbus_address_get_stream (const gchar *address,
772 GCancellable *cancellable,
773 GAsyncReadyCallback callback,
776 GSimpleAsyncResult *res;
779 g_return_if_fail (address != NULL);
781 res = g_simple_async_result_new (NULL,
784 g_dbus_address_get_stream);
785 data = g_new0 (GetStreamData, 1);
786 data->address = g_strdup (address);
787 g_simple_async_result_set_op_res_gpointer (res,
789 (GDestroyNotify) get_stream_data_free);
790 g_simple_async_result_run_in_thread (res,
791 get_stream_thread_func,
794 g_object_unref (res);
798 * g_dbus_address_get_stream_finish:
799 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
800 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
801 * @error: Return location for error or %NULL.
803 * Finishes an operation started with g_dbus_address_get_stream().
805 * Returns: A #GIOStream or %NULL if @error is set.
810 g_dbus_address_get_stream_finish (GAsyncResult *res,
814 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
818 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
819 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
821 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
825 data = g_simple_async_result_get_op_res_gpointer (simple);
826 if (g_simple_async_result_propagate_error (simple, error))
829 ret = g_object_ref (data->stream);
830 if (out_guid != NULL)
831 *out_guid = g_strdup (data->guid);
838 * g_dbus_address_get_stream_sync:
839 * @address: A valid D-Bus address.
840 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
841 * @cancellable: A #GCancellable or %NULL.
842 * @error: Return location for error or %NULL.
844 * Synchronously connects to an endpoint specified by @address and
845 * sets up the connection so it is in a state to run the client-side
846 * of the D-Bus authentication conversation.
848 * This is a synchronous failable function. See
849 * g_dbus_address_get_stream() for the asynchronous version.
851 * Returns: A #GIOStream or %NULL if @error is set.
856 g_dbus_address_get_stream_sync (const gchar *address,
858 GCancellable *cancellable,
866 g_return_val_if_fail (address != NULL, NULL);
867 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
872 addr_array = g_strsplit (address, ";", 0);
874 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
876 const gchar *addr = addr_array[n];
879 ret = g_dbus_address_try_connect_one (addr,
889 g_assert (this_error != NULL);
890 if (last_error != NULL)
891 g_error_free (last_error);
892 last_error = this_error;
899 if (last_error != NULL)
900 g_error_free (last_error);
904 g_assert (last_error != NULL);
905 g_propagate_error (error, last_error);
910 /* ---------------------------------------------------------------------------------------------------- */
912 /* TODO: implement for UNIX, Win32 and OS X */
914 get_session_address_platform_specific (void)
919 /* ---------------------------------------------------------------------------------------------------- */
922 * g_dbus_address_get_for_bus_sync:
923 * @bus_type: A #GBusType.
924 * @cancellable: A #GCancellable or %NULL.
925 * @error: Return location for error or %NULL.
927 * Synchronously looks up the D-Bus address for the well-known message
928 * bus instance specified by @bus_type. This may involve using various
929 * platform specific mechanisms.
931 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
936 g_dbus_address_get_for_bus_sync (GBusType bus_type,
937 GCancellable *cancellable,
941 const gchar *starter_bus;
943 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
949 case G_BUS_TYPE_SYSTEM:
950 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
953 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
957 case G_BUS_TYPE_SESSION:
958 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
961 ret = get_session_address_platform_specific ();
967 _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
972 case G_BUS_TYPE_STARTER:
973 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
974 if (g_strcmp0 (starter_bus, "session") == 0)
976 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
979 else if (g_strcmp0 (starter_bus, "system") == 0)
981 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
986 if (starter_bus != NULL)
991 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
992 " - unknown value `%s'"),
997 g_set_error_literal (error,
1000 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1001 "variable is not set"));
1010 _("Unknown bus type %d"),