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);
89 for (n = 0; a[n] != NULL; n++)
91 if (!_g_dbus_address_parse_entry (a[n],
106 is_valid_unix (const gchar *address_entry,
107 GHashTable *key_value_pairs,
115 const gchar *abstract;
123 keys = g_hash_table_get_keys (key_value_pairs);
124 for (l = keys; l != NULL; l = l->next)
126 const gchar *key = l->data;
127 if (g_strcmp0 (key, "path") == 0)
128 path = g_hash_table_lookup (key_value_pairs, key);
129 else if (g_strcmp0 (key, "tmpdir") == 0)
130 tmpdir = g_hash_table_lookup (key_value_pairs, key);
131 else if (g_strcmp0 (key, "abstract") == 0)
132 abstract = g_hash_table_lookup (key_value_pairs, key);
137 G_IO_ERROR_INVALID_ARGUMENT,
138 _("Unsupported key `%s' in address entry `%s'"),
147 if (tmpdir != NULL || abstract != NULL)
149 /* TODO: validate path */
151 else if (tmpdir != NULL)
153 if (path != NULL || abstract != NULL)
155 /* TODO: validate tmpdir */
157 else if (abstract != NULL)
159 if (path != NULL || tmpdir != NULL)
161 /* TODO: validate abstract */
167 G_IO_ERROR_INVALID_ARGUMENT,
168 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys)"),
180 G_IO_ERROR_INVALID_ARGUMENT,
181 _("Meaningless key/value pair combination in address entry `%s'"),
191 is_valid_nonce_tcp (const gchar *address_entry,
192 GHashTable *key_value_pairs,
201 const gchar *nonce_file;
212 keys = g_hash_table_get_keys (key_value_pairs);
213 for (l = keys; l != NULL; l = l->next)
215 const gchar *key = l->data;
216 if (g_strcmp0 (key, "host") == 0)
217 host = g_hash_table_lookup (key_value_pairs, key);
218 else if (g_strcmp0 (key, "port") == 0)
219 port = g_hash_table_lookup (key_value_pairs, key);
220 else if (g_strcmp0 (key, "family") == 0)
221 family = g_hash_table_lookup (key_value_pairs, key);
222 else if (g_strcmp0 (key, "noncefile") == 0)
223 nonce_file = g_hash_table_lookup (key_value_pairs, key);
228 G_IO_ERROR_INVALID_ARGUMENT,
229 _("Unsupported key `%s' in address entry `%s'"),
238 port_num = strtol (port, &endp, 10);
239 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
243 G_IO_ERROR_INVALID_ARGUMENT,
244 _("Error in address `%s' - the port attribute is malformed"),
250 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
254 G_IO_ERROR_INVALID_ARGUMENT,
255 _("Error in address `%s' - the family attribute is malformed"),
269 is_valid_tcp (const gchar *address_entry,
270 GHashTable *key_value_pairs,
288 keys = g_hash_table_get_keys (key_value_pairs);
289 for (l = keys; l != NULL; l = l->next)
291 const gchar *key = l->data;
292 if (g_strcmp0 (key, "host") == 0)
293 host = g_hash_table_lookup (key_value_pairs, key);
294 else if (g_strcmp0 (key, "port") == 0)
295 port = g_hash_table_lookup (key_value_pairs, key);
296 else if (g_strcmp0 (key, "family") == 0)
297 family = g_hash_table_lookup (key_value_pairs, key);
302 G_IO_ERROR_INVALID_ARGUMENT,
303 _("Unsupported key `%s' in address entry `%s'"),
312 port_num = strtol (port, &endp, 10);
313 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
317 G_IO_ERROR_INVALID_ARGUMENT,
318 _("Error in address `%s' - the port attribute is malformed"),
324 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
328 G_IO_ERROR_INVALID_ARGUMENT,
329 _("Error in address `%s' - the family attribute is malformed"),
343 * g_dbus_is_supported_address:
345 * @error: Return location for error or %NULL.
347 * Like g_dbus_is_address() but also checks if the library suppors the
348 * transports in @string and that key/value pairs for each transport
351 * Returns: %TRUE if @string is a valid D-Bus address that is
352 * supported by this library, %FALSE if @error is set.
357 g_dbus_is_supported_address (const gchar *string,
366 g_return_val_if_fail (string != NULL, FALSE);
367 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
369 a = g_strsplit (string, ";", 0);
370 for (n = 0; a[n] != NULL; n++)
372 gchar *transport_name;
373 GHashTable *key_value_pairs;
376 if (!_g_dbus_address_parse_entry (a[n],
383 if (g_strcmp0 (transport_name, "unix") == 0)
384 supported = is_valid_unix (a[n], key_value_pairs, error);
385 else if (g_strcmp0 (transport_name, "tcp") == 0)
386 supported = is_valid_tcp (a[n], key_value_pairs, error);
387 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
388 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
390 g_free (transport_name);
391 g_hash_table_unref (key_value_pairs);
402 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
408 _g_dbus_address_parse_entry (const gchar *address_entry,
409 gchar **out_transport_name,
410 GHashTable **out_key_value_pairs,
414 GHashTable *key_value_pairs;
415 gchar *transport_name;
422 transport_name = NULL;
423 key_value_pairs = NULL;
425 s = strchr (address_entry, ':');
430 G_IO_ERROR_INVALID_ARGUMENT,
431 _("Address element `%s', does not contain a colon (:)"),
436 transport_name = g_strndup (address_entry, s - address_entry);
437 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
439 kv_pairs = g_strsplit (s + 1, ",", 0);
440 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
442 const gchar *kv_pair = kv_pairs[n];
446 s = strchr (kv_pair, '=');
451 G_IO_ERROR_INVALID_ARGUMENT,
452 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
459 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
460 key = g_uri_unescape_segment (kv_pair, s, NULL);
461 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
462 g_hash_table_insert (key_value_pairs, key, value);
468 g_strfreev (kv_pairs);
471 if (out_transport_name != NULL)
472 *out_transport_name = transport_name;
474 g_free (transport_name);
475 if (out_key_value_pairs != NULL)
476 *out_key_value_pairs = key_value_pairs;
477 else if (key_value_pairs != NULL)
478 g_hash_table_unref (key_value_pairs);
482 g_free (transport_name);
483 if (key_value_pairs != NULL)
484 g_hash_table_unref (key_value_pairs);
489 /* ---------------------------------------------------------------------------------------------------- */
491 /* TODO: Declare an extension point called GDBusTransport (or similar)
492 * and move code below to extensions implementing said extension
493 * point. That way we can implement a D-Bus transport over X11 without
494 * making libgio link to libX11...
497 g_dbus_address_connect (const gchar *address_entry,
498 const gchar *transport_name,
499 GHashTable *key_value_pairs,
500 GCancellable *cancellable,
504 GSocketConnectable *connectable;
505 const gchar *nonce_file;
515 else if (g_strcmp0 (transport_name, "unix") == 0)
518 const gchar *abstract;
519 path = g_hash_table_lookup (key_value_pairs, "path");
520 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
521 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
525 G_IO_ERROR_INVALID_ARGUMENT,
526 _("Error in address `%s' - the unix transport requires exactly one of the "
527 "keys `path' or `abstract' to be set"),
530 else if (path != NULL)
532 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
534 else if (abstract != NULL)
536 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
538 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
542 g_assert_not_reached ();
546 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
554 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
556 host = g_hash_table_lookup (key_value_pairs, "host");
561 G_IO_ERROR_INVALID_ARGUMENT,
562 _("Error in address `%s' - the host attribute is missing or malformed"),
567 s = g_hash_table_lookup (key_value_pairs, "port");
570 port = strtol (s, &endp, 10);
571 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
575 G_IO_ERROR_INVALID_ARGUMENT,
576 _("Error in address `%s' - the port attribute is missing or malformed"),
584 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
585 if (nonce_file == NULL)
589 G_IO_ERROR_INVALID_ARGUMENT,
590 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
596 /* TODO: deal with family */
597 connectable = g_network_address_new (host, port);
603 G_IO_ERROR_INVALID_ARGUMENT,
604 _("Unknown or unsupported transport `%s' for address `%s'"),
609 if (connectable != NULL)
611 GSocketClient *client;
612 GSocketConnection *connection;
614 g_assert (ret == NULL);
615 client = g_socket_client_new ();
616 connection = g_socket_client_connect (client,
620 g_object_unref (connectable);
621 g_object_unref (client);
622 if (connection == NULL)
625 ret = G_IO_STREAM (connection);
627 if (nonce_file != NULL)
629 gchar *nonce_contents;
632 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
633 if (!g_file_get_contents (nonce_file,
638 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
639 g_object_unref (ret);
644 if (nonce_length != 16)
648 G_IO_ERROR_INVALID_ARGUMENT,
649 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
652 g_free (nonce_contents);
653 g_object_unref (ret);
658 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
665 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
666 g_object_unref (ret);
668 g_free (nonce_contents);
671 g_free (nonce_contents);
681 g_dbus_address_try_connect_one (const gchar *address_entry,
683 GCancellable *cancellable,
687 GHashTable *key_value_pairs;
688 gchar *transport_name;
692 transport_name = NULL;
693 key_value_pairs = NULL;
695 if (!_g_dbus_address_parse_entry (address_entry,
701 ret = g_dbus_address_connect (address_entry,
709 /* TODO: validate that guid is of correct format */
710 guid = g_hash_table_lookup (key_value_pairs, "guid");
711 if (guid != NULL && out_guid != NULL)
712 *out_guid = g_strdup (guid);
715 g_free (transport_name);
716 if (key_value_pairs != NULL)
717 g_hash_table_unref (key_value_pairs);
722 /* ---------------------------------------------------------------------------------------------------- */
731 get_stream_data_free (GetStreamData *data)
733 g_free (data->address);
734 if (data->stream != NULL)
735 g_object_unref (data->stream);
741 get_stream_thread_func (GSimpleAsyncResult *res,
743 GCancellable *cancellable)
748 data = g_simple_async_result_get_op_res_gpointer (res);
751 data->stream = g_dbus_address_get_stream_sync (data->address,
755 if (data->stream == NULL)
757 g_simple_async_result_set_from_error (res, error);
758 g_error_free (error);
763 * g_dbus_address_get_stream:
764 * @address: A valid D-Bus address.
765 * @cancellable: A #GCancellable or %NULL.
766 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
767 * @user_data: Data to pass to @callback.
769 * Asynchronously connects to an endpoint specified by @address and
770 * sets up the connection so it is in a state to run the client-side
771 * of the D-Bus authentication conversation.
773 * When the operation is finished, @callback will be invoked. You can
774 * then call g_dbus_address_get_stream_finish() to get the result of
777 * This is an asynchronous failable function. See
778 * g_dbus_address_get_stream_sync() for the synchronous version.
783 g_dbus_address_get_stream (const gchar *address,
784 GCancellable *cancellable,
785 GAsyncReadyCallback callback,
788 GSimpleAsyncResult *res;
791 g_return_if_fail (address != NULL);
793 res = g_simple_async_result_new (NULL,
796 g_dbus_address_get_stream);
797 data = g_new0 (GetStreamData, 1);
798 data->address = g_strdup (address);
799 g_simple_async_result_set_op_res_gpointer (res,
801 (GDestroyNotify) get_stream_data_free);
802 g_simple_async_result_run_in_thread (res,
803 get_stream_thread_func,
806 g_object_unref (res);
810 * g_dbus_address_get_stream_finish:
811 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
812 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
813 * @error: Return location for error or %NULL.
815 * Finishes an operation started with g_dbus_address_get_stream().
817 * Returns: A #GIOStream or %NULL if @error is set.
822 g_dbus_address_get_stream_finish (GAsyncResult *res,
826 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
830 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
831 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
833 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
837 data = g_simple_async_result_get_op_res_gpointer (simple);
838 if (g_simple_async_result_propagate_error (simple, error))
841 ret = g_object_ref (data->stream);
842 if (out_guid != NULL)
843 *out_guid = g_strdup (data->guid);
850 * g_dbus_address_get_stream_sync:
851 * @address: A valid D-Bus address.
852 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
853 * @cancellable: A #GCancellable or %NULL.
854 * @error: Return location for error or %NULL.
856 * Synchronously connects to an endpoint specified by @address and
857 * sets up the connection so it is in a state to run the client-side
858 * of the D-Bus authentication conversation.
860 * This is a synchronous failable function. See
861 * g_dbus_address_get_stream() for the asynchronous version.
863 * Returns: A #GIOStream or %NULL if @error is set.
868 g_dbus_address_get_stream_sync (const gchar *address,
870 GCancellable *cancellable,
878 g_return_val_if_fail (address != NULL, NULL);
879 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
884 addr_array = g_strsplit (address, ";", 0);
885 if (addr_array[0] == NULL)
887 last_error = g_error_new_literal (G_IO_ERROR,
888 G_IO_ERROR_INVALID_ARGUMENT,
889 _("The given address is empty"));
893 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
895 const gchar *addr = addr_array[n];
899 ret = g_dbus_address_try_connect_one (addr,
909 g_assert (this_error != NULL);
910 if (last_error != NULL)
911 g_error_free (last_error);
912 last_error = this_error;
919 if (last_error != NULL)
920 g_error_free (last_error);
924 g_assert (last_error != NULL);
925 g_propagate_error (error, last_error);
928 g_strfreev (addr_array);
932 /* ---------------------------------------------------------------------------------------------------- */
934 /* TODO: implement for UNIX, Win32 and OS X */
936 get_session_address_platform_specific (void)
941 /* ---------------------------------------------------------------------------------------------------- */
944 * g_dbus_address_get_for_bus_sync:
945 * @bus_type: A #GBusType.
946 * @cancellable: A #GCancellable or %NULL.
947 * @error: Return location for error or %NULL.
949 * Synchronously looks up the D-Bus address for the well-known message
950 * bus instance specified by @bus_type. This may involve using various
951 * platform specific mechanisms.
953 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
958 g_dbus_address_get_for_bus_sync (GBusType bus_type,
959 GCancellable *cancellable,
963 const gchar *starter_bus;
965 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
971 case G_BUS_TYPE_SYSTEM:
972 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
975 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
979 case G_BUS_TYPE_SESSION:
980 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
983 ret = get_session_address_platform_specific ();
989 _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
994 case G_BUS_TYPE_STARTER:
995 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
996 if (g_strcmp0 (starter_bus, "session") == 0)
998 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
1001 else if (g_strcmp0 (starter_bus, "system") == 0)
1003 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
1008 if (starter_bus != NULL)
1013 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
1014 " - unknown value `%s'"),
1019 g_set_error_literal (error,
1022 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1023 "variable is not set"));
1032 _("Unknown bus type %d"),
1041 #define __G_DBUS_ADDRESS_C__
1042 #include "gioaliasdef.c"