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>
27 #include <glib/gi18n.h>
29 #include "gdbusutils.h"
30 #include "gdbusaddress.h"
31 #include "gdbuserror.h"
32 #include "gioenumtypes.h"
33 #include "gdbusprivate.h"
36 #include <gio/gunixsocketaddress.h>
40 * SECTION:gdbusaddress
41 * @title: D-Bus Addresses
42 * @short_description: D-Bus connection endpoints
45 * Routines for working with D-Bus addresses.
48 /* ---------------------------------------------------------------------------------------------------- */
54 * Checks if @string is a D-Bus address.
56 * This doesn't check if @string is actually supported by #GDBusServer
57 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
60 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
65 g_dbus_is_address (const gchar *string)
73 g_return_val_if_fail (string != NULL, FALSE);
75 a = g_strsplit (string, ";", 0);
76 for (n = 0; a[n] != NULL; n++)
78 if (!_g_dbus_address_parse_entry (a[n],
93 is_valid_unix (const gchar *address_entry,
94 GHashTable *key_value_pairs,
102 const gchar *abstract;
110 keys = g_hash_table_get_keys (key_value_pairs);
111 for (l = keys; l != NULL; l = l->next)
113 const gchar *key = l->data;
114 if (g_strcmp0 (key, "path") == 0)
115 path = g_hash_table_lookup (key_value_pairs, key);
116 else if (g_strcmp0 (key, "tmpdir") == 0)
117 tmpdir = g_hash_table_lookup (key_value_pairs, key);
118 else if (g_strcmp0 (key, "abstract") == 0)
119 abstract = g_hash_table_lookup (key_value_pairs, key);
124 G_IO_ERROR_INVALID_ARGUMENT,
125 _("Unsupported key `%s' in address entry `%s'"),
134 if (tmpdir != NULL || abstract != NULL)
136 /* TODO: validate path */
138 else if (tmpdir != NULL)
140 if (path != NULL || abstract != NULL)
142 /* TODO: validate tmpdir */
144 else if (abstract != NULL)
146 if (path != NULL || tmpdir != NULL)
148 /* TODO: validate abstract */
154 G_IO_ERROR_INVALID_ARGUMENT,
155 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys"),
167 G_IO_ERROR_INVALID_ARGUMENT,
168 _("Meaningless key/value pair combination in address entry `%s'"),
178 is_valid_nonce_tcp (const gchar *address_entry,
179 GHashTable *key_value_pairs,
188 const gchar *nonce_file;
199 keys = g_hash_table_get_keys (key_value_pairs);
200 for (l = keys; l != NULL; l = l->next)
202 const gchar *key = l->data;
203 if (g_strcmp0 (key, "host") == 0)
204 host = g_hash_table_lookup (key_value_pairs, key);
205 else if (g_strcmp0 (key, "port") == 0)
206 port = g_hash_table_lookup (key_value_pairs, key);
207 else if (g_strcmp0 (key, "family") == 0)
208 family = g_hash_table_lookup (key_value_pairs, key);
209 else if (g_strcmp0 (key, "noncefile") == 0)
210 nonce_file = g_hash_table_lookup (key_value_pairs, key);
215 G_IO_ERROR_INVALID_ARGUMENT,
216 _("Unsupported key `%s' in address entry `%s'"),
225 port_num = strtol (port, &endp, 10);
226 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
230 G_IO_ERROR_INVALID_ARGUMENT,
231 _("Error in address `%s' - the port attribute is malformed"),
237 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
241 G_IO_ERROR_INVALID_ARGUMENT,
242 _("Error in address `%s' - the family attribute is malformed"),
256 is_valid_tcp (const gchar *address_entry,
257 GHashTable *key_value_pairs,
275 keys = g_hash_table_get_keys (key_value_pairs);
276 for (l = keys; l != NULL; l = l->next)
278 const gchar *key = l->data;
279 if (g_strcmp0 (key, "host") == 0)
280 host = g_hash_table_lookup (key_value_pairs, key);
281 else if (g_strcmp0 (key, "port") == 0)
282 port = g_hash_table_lookup (key_value_pairs, key);
283 else if (g_strcmp0 (key, "family") == 0)
284 family = g_hash_table_lookup (key_value_pairs, key);
289 G_IO_ERROR_INVALID_ARGUMENT,
290 _("Unsupported key `%s' in address entry `%s'"),
299 port_num = strtol (port, &endp, 10);
300 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
304 G_IO_ERROR_INVALID_ARGUMENT,
305 _("Error in address `%s' - the port attribute is malformed"),
311 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
315 G_IO_ERROR_INVALID_ARGUMENT,
316 _("Error in address `%s' - the family attribute is malformed"),
330 * g_dbus_is_supported_address:
332 * @error: Return location for error or %NULL.
334 * Like g_dbus_is_address() but also checks if the library suppors the
335 * transports in @string and that key/value pairs for each transport
338 * Returns: %TRUE if @string is a valid D-Bus address that is
339 * supported by this library, %FALSE if @error is set.
344 g_dbus_is_supported_address (const gchar *string,
353 g_return_val_if_fail (string != NULL, FALSE);
354 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
356 a = g_strsplit (string, ";", 0);
357 for (n = 0; a[n] != NULL; n++)
359 gchar *transport_name;
360 GHashTable *key_value_pairs;
363 if (!_g_dbus_address_parse_entry (a[n],
370 if (g_strcmp0 (transport_name, "unix") == 0)
371 supported = is_valid_unix (a[n], key_value_pairs, error);
372 else if (g_strcmp0 (transport_name, "tcp") == 0)
373 supported = is_valid_tcp (a[n], key_value_pairs, error);
374 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
375 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
377 g_free (transport_name);
378 g_hash_table_unref (key_value_pairs);
389 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
395 _g_dbus_address_parse_entry (const gchar *address_entry,
396 gchar **out_transport_name,
397 GHashTable **out_key_value_pairs,
401 GHashTable *key_value_pairs;
402 gchar *transport_name;
409 transport_name = NULL;
410 key_value_pairs = NULL;
412 s = strchr (address_entry, ':');
417 G_IO_ERROR_INVALID_ARGUMENT,
418 _("Address element `%s', does not contain a colon (:)"),
423 transport_name = g_strndup (address_entry, s - address_entry);
424 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
426 kv_pairs = g_strsplit (s + 1, ",", 0);
427 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
429 const gchar *kv_pair = kv_pairs[n];
433 s = strchr (kv_pair, '=');
438 G_IO_ERROR_INVALID_ARGUMENT,
439 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
446 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
447 key = g_uri_unescape_segment (kv_pair, s, NULL);
448 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
449 g_hash_table_insert (key_value_pairs, key, value);
455 g_strfreev (kv_pairs);
458 if (out_transport_name != NULL)
459 *out_transport_name = transport_name;
461 g_free (transport_name);
462 if (out_key_value_pairs != NULL)
463 *out_key_value_pairs = key_value_pairs;
464 else if (key_value_pairs != NULL)
465 g_hash_table_unref (key_value_pairs);
469 g_free (transport_name);
470 if (key_value_pairs != NULL)
471 g_hash_table_unref (key_value_pairs);
476 /* ---------------------------------------------------------------------------------------------------- */
478 /* TODO: Declare an extension point called GDBusTransport (or similar)
479 * and move code below to extensions implementing said extension
480 * point. That way we can implement a D-Bus transport over X11 without
481 * making libgio link to libX11...
484 g_dbus_address_connect (const gchar *address_entry,
485 const gchar *transport_name,
486 GHashTable *key_value_pairs,
487 GCancellable *cancellable,
491 GSocketConnectable *connectable;
492 const gchar *nonce_file;
502 else if (g_strcmp0 (transport_name, "unix") == 0)
505 const gchar *abstract;
506 path = g_hash_table_lookup (key_value_pairs, "path");
507 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
508 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
512 G_IO_ERROR_INVALID_ARGUMENT,
513 _("Error in address `%s' - the unix transport requires exactly one of the "
514 "keys `path' or `abstract' to be set"),
517 else if (path != NULL)
519 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
521 else if (abstract != NULL)
523 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
525 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
529 g_assert_not_reached ();
533 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
541 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
543 host = g_hash_table_lookup (key_value_pairs, "host");
548 G_IO_ERROR_INVALID_ARGUMENT,
549 _("Error in address `%s' - the host attribute is missing or malformed"),
554 s = g_hash_table_lookup (key_value_pairs, "port");
557 port = strtol (s, &endp, 10);
558 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
562 G_IO_ERROR_INVALID_ARGUMENT,
563 _("Error in address `%s' - the port attribute is missing or malformed"),
571 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
572 if (nonce_file == NULL)
576 G_IO_ERROR_INVALID_ARGUMENT,
577 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
583 /* TODO: deal with family */
584 connectable = g_network_address_new (host, port);
590 G_IO_ERROR_INVALID_ARGUMENT,
591 _("Unknown or unsupported transport `%s' for address `%s'"),
596 if (connectable != NULL)
598 GSocketClient *client;
599 GSocketConnection *connection;
601 g_assert (ret == NULL);
602 client = g_socket_client_new ();
603 connection = g_socket_client_connect (client,
607 g_object_unref (connectable);
608 g_object_unref (client);
609 if (connection == NULL)
612 ret = G_IO_STREAM (connection);
614 if (nonce_file != NULL)
616 gchar *nonce_contents;
619 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
620 if (!g_file_get_contents (nonce_file,
625 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
626 g_object_unref (ret);
631 if (nonce_length != 16)
635 G_IO_ERROR_INVALID_ARGUMENT,
636 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
639 g_free (nonce_contents);
640 g_object_unref (ret);
645 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
652 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
653 g_object_unref (ret);
655 g_free (nonce_contents);
658 g_free (nonce_contents);
668 g_dbus_address_try_connect_one (const gchar *address_entry,
670 GCancellable *cancellable,
674 GHashTable *key_value_pairs;
675 gchar *transport_name;
679 transport_name = NULL;
680 key_value_pairs = NULL;
682 if (!_g_dbus_address_parse_entry (address_entry,
688 ret = g_dbus_address_connect (address_entry,
696 /* TODO: validate that guid is of correct format */
697 guid = g_hash_table_lookup (key_value_pairs, "guid");
698 if (guid != NULL && out_guid != NULL)
699 *out_guid = g_strdup (guid);
702 g_free (transport_name);
703 if (key_value_pairs != NULL)
704 g_hash_table_unref (key_value_pairs);
709 /* ---------------------------------------------------------------------------------------------------- */
718 get_stream_data_free (GetStreamData *data)
720 g_free (data->address);
721 if (data->stream != NULL)
722 g_object_unref (data->stream);
728 get_stream_thread_func (GSimpleAsyncResult *res,
730 GCancellable *cancellable)
735 data = g_simple_async_result_get_op_res_gpointer (res);
738 data->stream = g_dbus_address_get_stream_sync (data->address,
742 if (data->stream == NULL)
744 g_simple_async_result_set_from_error (res, error);
745 g_error_free (error);
750 * g_dbus_address_get_stream:
751 * @address: A valid D-Bus address.
752 * @cancellable: A #GCancellable or %NULL.
753 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
754 * @user_data: Data to pass to @callback.
756 * Asynchronously connects to an endpoint specified by @address and
757 * sets up the connection so it is in a state to run the client-side
758 * of the D-Bus authentication conversation.
760 * When the operation is finished, @callback will be invoked. You can
761 * then call g_dbus_address_get_stream_finish() to get the result of
764 * This is an asynchronous failable function. See
765 * g_dbus_address_get_stream_sync() for the synchronous version.
770 g_dbus_address_get_stream (const gchar *address,
771 GCancellable *cancellable,
772 GAsyncReadyCallback callback,
775 GSimpleAsyncResult *res;
778 g_return_if_fail (address != NULL);
780 res = g_simple_async_result_new (NULL,
783 g_dbus_address_get_stream);
784 data = g_new0 (GetStreamData, 1);
785 data->address = g_strdup (address);
786 g_simple_async_result_set_op_res_gpointer (res,
788 (GDestroyNotify) get_stream_data_free);
789 g_simple_async_result_run_in_thread (res,
790 get_stream_thread_func,
793 g_object_unref (res);
797 * g_dbus_address_get_stream_finish:
798 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
799 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
800 * @error: Return location for error or %NULL.
802 * Finishes an operation started with g_dbus_address_get_stream().
804 * Returns: A #GIOStream or %NULL if @error is set.
809 g_dbus_address_get_stream_finish (GAsyncResult *res,
813 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
817 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
818 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
820 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
824 data = g_simple_async_result_get_op_res_gpointer (simple);
825 if (g_simple_async_result_propagate_error (simple, error))
828 ret = g_object_ref (data->stream);
829 if (out_guid != NULL)
830 *out_guid = g_strdup (data->guid);
837 * g_dbus_address_get_stream_sync:
838 * @address: A valid D-Bus address.
839 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
840 * @cancellable: A #GCancellable or %NULL.
841 * @error: Return location for error or %NULL.
843 * Synchronously connects to an endpoint specified by @address and
844 * sets up the connection so it is in a state to run the client-side
845 * of the D-Bus authentication conversation.
847 * This is a synchronous failable function. See
848 * g_dbus_address_get_stream() for the asynchronous version.
850 * Returns: A #GIOStream or %NULL if @error is set.
855 g_dbus_address_get_stream_sync (const gchar *address,
857 GCancellable *cancellable,
865 g_return_val_if_fail (address != NULL, NULL);
866 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
871 addr_array = g_strsplit (address, ";", 0);
873 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
875 const gchar *addr = addr_array[n];
878 ret = g_dbus_address_try_connect_one (addr,
888 g_assert (this_error != NULL);
889 if (last_error != NULL)
890 g_error_free (last_error);
891 last_error = this_error;
898 if (last_error != NULL)
899 g_error_free (last_error);
903 g_assert (last_error != NULL);
904 g_propagate_error (error, last_error);
909 /* ---------------------------------------------------------------------------------------------------- */
911 /* TODO: implement for UNIX, Win32 and OS X */
913 get_session_address_platform_specific (void)
918 /* ---------------------------------------------------------------------------------------------------- */
921 * g_dbus_address_get_for_bus_sync:
922 * @bus_type: A #GBusType.
923 * @cancellable: A #GCancellable or %NULL.
924 * @error: Return location for error or %NULL.
926 * Synchronously looks up the D-Bus address for the well-known message
927 * bus instance specified by @bus_type. This may involve using various
928 * platform specific mechanisms.
930 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
935 g_dbus_address_get_for_bus_sync (GBusType bus_type,
936 GCancellable *cancellable,
940 const gchar *starter_bus;
942 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
948 case G_BUS_TYPE_SYSTEM:
949 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
952 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
956 case G_BUS_TYPE_SESSION:
957 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
960 ret = get_session_address_platform_specific ();
966 _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
971 case G_BUS_TYPE_STARTER:
972 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
973 if (g_strcmp0 (starter_bus, "session") == 0)
975 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
978 else if (g_strcmp0 (starter_bus, "system") == 0)
980 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
985 if (starter_bus != NULL)
990 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
991 " - unknown value `%s'"),
996 g_set_error_literal (error,
999 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1000 "variable is not set"));
1009 _("Unknown bus type %d"),