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
43 * @include: gdbus/gdbus.h
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.
63 g_dbus_is_address (const gchar *string)
71 g_return_val_if_fail (string != NULL, FALSE);
73 a = g_strsplit (string, ";", 0);
74 for (n = 0; a[n] != NULL; n++)
76 if (!_g_dbus_address_parse_entry (a[n],
91 is_valid_unix (const gchar *address_entry,
92 GHashTable *key_value_pairs,
100 const gchar *abstract;
108 keys = g_hash_table_get_keys (key_value_pairs);
109 for (l = keys; l != NULL; l = l->next)
111 const gchar *key = l->data;
112 if (g_strcmp0 (key, "path") == 0)
113 path = g_hash_table_lookup (key_value_pairs, key);
114 else if (g_strcmp0 (key, "tmpdir") == 0)
115 tmpdir = g_hash_table_lookup (key_value_pairs, key);
116 else if (g_strcmp0 (key, "abstract") == 0)
117 abstract = g_hash_table_lookup (key_value_pairs, key);
122 G_IO_ERROR_INVALID_ARGUMENT,
123 _("Unsupported key `%s' in address entry `%s'"),
132 if (tmpdir != NULL || abstract != NULL)
134 /* TODO: validate path */
136 else if (tmpdir != NULL)
138 if (path != NULL || abstract != NULL)
140 /* TODO: validate tmpdir */
142 else if (abstract != NULL)
144 if (path != NULL || tmpdir != NULL)
146 /* TODO: validate abstract */
152 G_IO_ERROR_INVALID_ARGUMENT,
153 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys"),
165 G_IO_ERROR_INVALID_ARGUMENT,
166 _("Meaningless key/value pair combination in address entry `%s'"),
176 is_valid_nonce_tcp (const gchar *address_entry,
177 GHashTable *key_value_pairs,
186 const gchar *nonce_file;
197 keys = g_hash_table_get_keys (key_value_pairs);
198 for (l = keys; l != NULL; l = l->next)
200 const gchar *key = l->data;
201 if (g_strcmp0 (key, "host") == 0)
202 host = g_hash_table_lookup (key_value_pairs, key);
203 else if (g_strcmp0 (key, "port") == 0)
204 port = g_hash_table_lookup (key_value_pairs, key);
205 else if (g_strcmp0 (key, "family") == 0)
206 family = g_hash_table_lookup (key_value_pairs, key);
207 else if (g_strcmp0 (key, "noncefile") == 0)
208 nonce_file = g_hash_table_lookup (key_value_pairs, key);
213 G_IO_ERROR_INVALID_ARGUMENT,
214 _("Unsupported key `%s' in address entry `%s'"),
223 port_num = strtol (port, &endp, 10);
224 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
228 G_IO_ERROR_INVALID_ARGUMENT,
229 _("Error in address `%s' - the port attribute is malformed"),
235 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
239 G_IO_ERROR_INVALID_ARGUMENT,
240 _("Error in address `%s' - the family attribute is malformed"),
254 is_valid_tcp (const gchar *address_entry,
255 GHashTable *key_value_pairs,
273 keys = g_hash_table_get_keys (key_value_pairs);
274 for (l = keys; l != NULL; l = l->next)
276 const gchar *key = l->data;
277 if (g_strcmp0 (key, "host") == 0)
278 host = g_hash_table_lookup (key_value_pairs, key);
279 else if (g_strcmp0 (key, "port") == 0)
280 port = g_hash_table_lookup (key_value_pairs, key);
281 else if (g_strcmp0 (key, "family") == 0)
282 family = g_hash_table_lookup (key_value_pairs, key);
287 G_IO_ERROR_INVALID_ARGUMENT,
288 _("Unsupported key `%s' in address entry `%s'"),
297 port_num = strtol (port, &endp, 10);
298 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
302 G_IO_ERROR_INVALID_ARGUMENT,
303 _("Error in address `%s' - the port attribute is malformed"),
309 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
313 G_IO_ERROR_INVALID_ARGUMENT,
314 _("Error in address `%s' - the family attribute is malformed"),
328 * g_dbus_is_supported_address:
330 * @error: Return location for error or %NULL.
332 * Like g_dbus_is_address() but also checks if the library suppors the
333 * transports in @string and that key/value pairs for each transport
336 * Returns: %TRUE if @string is a valid D-Bus address that is
337 * supported by this library, %FALSE if @error is set.
340 g_dbus_is_supported_address (const gchar *string,
349 g_return_val_if_fail (string != NULL, FALSE);
350 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
352 a = g_strsplit (string, ";", 0);
353 for (n = 0; a[n] != NULL; n++)
355 gchar *transport_name;
356 GHashTable *key_value_pairs;
359 if (!_g_dbus_address_parse_entry (a[n],
366 if (g_strcmp0 (transport_name, "unix") == 0)
367 supported = is_valid_unix (a[n], key_value_pairs, error);
368 else if (g_strcmp0 (transport_name, "tcp") == 0)
369 supported = is_valid_tcp (a[n], key_value_pairs, error);
370 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
371 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
373 g_free (transport_name);
374 g_hash_table_unref (key_value_pairs);
385 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
391 _g_dbus_address_parse_entry (const gchar *address_entry,
392 gchar **out_transport_name,
393 GHashTable **out_key_value_pairs,
397 GHashTable *key_value_pairs;
398 gchar *transport_name;
405 transport_name = NULL;
406 key_value_pairs = NULL;
408 s = strchr (address_entry, ':');
413 G_IO_ERROR_INVALID_ARGUMENT,
414 _("Address element `%s', does not contain a colon (:)"),
419 transport_name = g_strndup (address_entry, s - address_entry);
420 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
422 kv_pairs = g_strsplit (s + 1, ",", 0);
423 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
425 const gchar *kv_pair = kv_pairs[n];
429 s = strchr (kv_pair, '=');
434 G_IO_ERROR_INVALID_ARGUMENT,
435 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
442 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
443 key = g_uri_unescape_segment (kv_pair, s, NULL);
444 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
445 g_hash_table_insert (key_value_pairs, key, value);
451 g_strfreev (kv_pairs);
454 if (out_transport_name != NULL)
455 *out_transport_name = transport_name;
457 g_free (transport_name);
458 if (out_key_value_pairs != NULL)
459 *out_key_value_pairs = key_value_pairs;
460 else if (key_value_pairs != NULL)
461 g_hash_table_unref (key_value_pairs);
465 g_free (transport_name);
466 if (key_value_pairs != NULL)
467 g_hash_table_unref (key_value_pairs);
472 /* ---------------------------------------------------------------------------------------------------- */
474 /* TODO: Declare an extension point called GDBusTransport (or similar)
475 * and move code below to extensions implementing said extension
476 * point. That way we can implement a D-Bus transport over X11 without
477 * making libgio link to libX11...
480 g_dbus_address_connect (const gchar *address_entry,
481 const gchar *transport_name,
482 GHashTable *key_value_pairs,
483 GCancellable *cancellable,
487 GSocketConnectable *connectable;
488 const gchar *nonce_file;
498 else if (g_strcmp0 (transport_name, "unix") == 0)
501 const gchar *abstract;
502 path = g_hash_table_lookup (key_value_pairs, "path");
503 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
504 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
508 G_IO_ERROR_INVALID_ARGUMENT,
509 _("Error in address `%s' - the unix transport requires exactly one of the "
510 "keys `path' or `abstract' to be set"),
513 else if (path != NULL)
515 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
517 else if (abstract != NULL)
519 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
521 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
525 g_assert_not_reached ();
529 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
537 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
539 host = g_hash_table_lookup (key_value_pairs, "host");
544 G_IO_ERROR_INVALID_ARGUMENT,
545 _("Error in address `%s' - the host attribute is missing or malformed"),
550 s = g_hash_table_lookup (key_value_pairs, "port");
553 port = strtol (s, &endp, 10);
554 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
558 G_IO_ERROR_INVALID_ARGUMENT,
559 _("Error in address `%s' - the port attribute is missing or malformed"),
567 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
568 if (nonce_file == NULL)
572 G_IO_ERROR_INVALID_ARGUMENT,
573 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
579 /* TODO: deal with family */
580 connectable = g_network_address_new (host, port);
586 G_IO_ERROR_INVALID_ARGUMENT,
587 _("Unknown or unsupported transport `%s' for address `%s'"),
592 if (connectable != NULL)
594 GSocketClient *client;
595 GSocketConnection *connection;
597 g_assert (ret == NULL);
598 client = g_socket_client_new ();
599 connection = g_socket_client_connect (client,
603 g_object_unref (connectable);
604 g_object_unref (client);
605 if (connection == NULL)
608 ret = G_IO_STREAM (connection);
610 if (nonce_file != NULL)
612 gchar *nonce_contents;
615 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
616 if (!g_file_get_contents (nonce_file,
621 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
622 g_object_unref (ret);
627 if (nonce_length != 16)
631 G_IO_ERROR_INVALID_ARGUMENT,
632 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
635 g_free (nonce_contents);
636 g_object_unref (ret);
641 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
648 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
649 g_object_unref (ret);
651 g_free (nonce_contents);
654 g_free (nonce_contents);
664 g_dbus_address_try_connect_one (const gchar *address_entry,
666 GCancellable *cancellable,
670 GHashTable *key_value_pairs;
671 gchar *transport_name;
675 transport_name = NULL;
676 key_value_pairs = NULL;
678 if (!_g_dbus_address_parse_entry (address_entry,
684 ret = g_dbus_address_connect (address_entry,
692 /* TODO: validate that guid is of correct format */
693 guid = g_hash_table_lookup (key_value_pairs, "guid");
694 if (guid != NULL && out_guid != NULL)
695 *out_guid = g_strdup (guid);
698 g_free (transport_name);
699 if (key_value_pairs != NULL)
700 g_hash_table_unref (key_value_pairs);
705 /* ---------------------------------------------------------------------------------------------------- */
714 get_stream_data_free (GetStreamData *data)
716 g_free (data->address);
717 if (data->stream != NULL)
718 g_object_unref (data->stream);
724 get_stream_thread_func (GSimpleAsyncResult *res,
726 GCancellable *cancellable)
731 data = g_simple_async_result_get_op_res_gpointer (res);
734 data->stream = g_dbus_address_get_stream_sync (data->address,
738 if (data->stream == NULL)
740 g_simple_async_result_set_from_error (res, error);
741 g_error_free (error);
746 * g_dbus_address_get_stream:
747 * @address: A valid D-Bus address.
748 * @cancellable: A #GCancellable or %NULL.
749 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
750 * @user_data: Data to pass to @callback.
752 * Asynchronously connects to an endpoint specified by @address and
753 * sets up the connection so it is in a state to run the client-side
754 * of the D-Bus authentication conversation.
756 * When the operation is finished, @callback will be invoked. You can
757 * then call g_dbus_address_get_stream_finish() to get the result of
760 * This is an asynchronous failable function. See
761 * g_dbus_address_get_stream_sync() for the synchronous version.
764 g_dbus_address_get_stream (const gchar *address,
765 GCancellable *cancellable,
766 GAsyncReadyCallback callback,
769 GSimpleAsyncResult *res;
772 g_return_if_fail (address != NULL);
774 res = g_simple_async_result_new (NULL,
777 g_dbus_address_get_stream);
778 data = g_new0 (GetStreamData, 1);
779 data->address = g_strdup (address);
780 g_simple_async_result_set_op_res_gpointer (res,
782 (GDestroyNotify) get_stream_data_free);
783 g_simple_async_result_run_in_thread (res,
784 get_stream_thread_func,
787 g_object_unref (res);
791 * g_dbus_address_get_stream_finish:
792 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
793 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
794 * @error: Return location for error or %NULL.
796 * Finishes an operation started with g_dbus_address_get_stream().
798 * Returns: A #GIOStream or %NULL if @error is set.
801 g_dbus_address_get_stream_finish (GAsyncResult *res,
805 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
809 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
810 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
812 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
816 data = g_simple_async_result_get_op_res_gpointer (simple);
817 if (g_simple_async_result_propagate_error (simple, error))
820 ret = g_object_ref (data->stream);
821 if (out_guid != NULL)
822 *out_guid = g_strdup (data->guid);
829 * g_dbus_address_get_stream_sync:
830 * @address: A valid D-Bus address.
831 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
832 * @cancellable: A #GCancellable or %NULL.
833 * @error: Return location for error or %NULL.
835 * Synchronously connects to an endpoint specified by @address and
836 * sets up the connection so it is in a state to run the client-side
837 * of the D-Bus authentication conversation.
839 * This is a synchronous failable function. See
840 * g_dbus_address_get_stream() for the asynchronous version.
842 * Returns: A #GIOStream or %NULL if @error is set.
845 g_dbus_address_get_stream_sync (const gchar *address,
847 GCancellable *cancellable,
855 g_return_val_if_fail (address != NULL, NULL);
856 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
861 addr_array = g_strsplit (address, ";", 0);
863 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
865 const gchar *addr = addr_array[n];
868 ret = g_dbus_address_try_connect_one (addr,
878 g_assert (this_error != NULL);
879 if (last_error != NULL)
880 g_error_free (last_error);
881 last_error = this_error;
888 if (last_error != NULL)
889 g_error_free (last_error);
893 g_assert (last_error != NULL);
894 g_propagate_error (error, last_error);
899 /* ---------------------------------------------------------------------------------------------------- */
901 /* TODO: implement for UNIX, Win32 and OS X */
903 get_session_address_platform_specific (void)
908 /* ---------------------------------------------------------------------------------------------------- */
911 * g_dbus_address_get_for_bus_sync:
912 * @bus_type: A #GBusType.
913 * @cancellable: A #GCancellable or %NULL.
914 * @error: Return location for error or %NULL.
916 * Synchronously looks up the D-Bus address for the well-known message
917 * bus instance specified by @bus_type. This may involve using various
918 * platform specific mechanisms.
920 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
923 g_dbus_address_get_for_bus_sync (GBusType bus_type,
924 GCancellable *cancellable,
928 const gchar *starter_bus;
930 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
936 case G_BUS_TYPE_SYSTEM:
937 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
940 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
944 case G_BUS_TYPE_SESSION:
945 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
948 ret = get_session_address_platform_specific ();
954 _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
959 case G_BUS_TYPE_STARTER:
960 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
961 if (g_strcmp0 (starter_bus, "session") == 0)
963 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
966 else if (g_strcmp0 (starter_bus, "system") == 0)
968 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
973 if (starter_bus != NULL)
978 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
979 " - unknown value `%s'"),
984 g_set_error_literal (error,
987 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
988 "variable is not set"));
997 _("Unknown bus type %d"),