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>
42 * SECTION:gdbusaddress
43 * @title: D-Bus Addresses
44 * @short_description: D-Bus connection endpoints
47 * Routines for working with D-Bus addresses.
50 /* ---------------------------------------------------------------------------------------------------- */
56 * Checks if @string is a D-Bus address.
58 * This doesn't check if @string is actually supported by #GDBusServer
59 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
62 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
67 g_dbus_is_address (const gchar *string)
75 g_return_val_if_fail (string != NULL, FALSE);
77 a = g_strsplit (string, ";", 0);
78 for (n = 0; a[n] != NULL; n++)
80 if (!_g_dbus_address_parse_entry (a[n],
95 is_valid_unix (const gchar *address_entry,
96 GHashTable *key_value_pairs,
104 const gchar *abstract;
112 keys = g_hash_table_get_keys (key_value_pairs);
113 for (l = keys; l != NULL; l = l->next)
115 const gchar *key = l->data;
116 if (g_strcmp0 (key, "path") == 0)
117 path = g_hash_table_lookup (key_value_pairs, key);
118 else if (g_strcmp0 (key, "tmpdir") == 0)
119 tmpdir = g_hash_table_lookup (key_value_pairs, key);
120 else if (g_strcmp0 (key, "abstract") == 0)
121 abstract = g_hash_table_lookup (key_value_pairs, key);
126 G_IO_ERROR_INVALID_ARGUMENT,
127 _("Unsupported key `%s' in address entry `%s'"),
136 if (tmpdir != NULL || abstract != NULL)
138 /* TODO: validate path */
140 else if (tmpdir != NULL)
142 if (path != NULL || abstract != NULL)
144 /* TODO: validate tmpdir */
146 else if (abstract != NULL)
148 if (path != NULL || tmpdir != NULL)
150 /* TODO: validate abstract */
156 G_IO_ERROR_INVALID_ARGUMENT,
157 _("Address `%s' is invalid (need exactly one of path, tmpdir or abstract keys"),
169 G_IO_ERROR_INVALID_ARGUMENT,
170 _("Meaningless key/value pair combination in address entry `%s'"),
180 is_valid_nonce_tcp (const gchar *address_entry,
181 GHashTable *key_value_pairs,
190 const gchar *nonce_file;
201 keys = g_hash_table_get_keys (key_value_pairs);
202 for (l = keys; l != NULL; l = l->next)
204 const gchar *key = l->data;
205 if (g_strcmp0 (key, "host") == 0)
206 host = g_hash_table_lookup (key_value_pairs, key);
207 else if (g_strcmp0 (key, "port") == 0)
208 port = g_hash_table_lookup (key_value_pairs, key);
209 else if (g_strcmp0 (key, "family") == 0)
210 family = g_hash_table_lookup (key_value_pairs, key);
211 else if (g_strcmp0 (key, "noncefile") == 0)
212 nonce_file = g_hash_table_lookup (key_value_pairs, key);
217 G_IO_ERROR_INVALID_ARGUMENT,
218 _("Unsupported key `%s' in address entry `%s'"),
227 port_num = strtol (port, &endp, 10);
228 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
232 G_IO_ERROR_INVALID_ARGUMENT,
233 _("Error in address `%s' - the port attribute is malformed"),
239 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
243 G_IO_ERROR_INVALID_ARGUMENT,
244 _("Error in address `%s' - the family attribute is malformed"),
258 is_valid_tcp (const gchar *address_entry,
259 GHashTable *key_value_pairs,
277 keys = g_hash_table_get_keys (key_value_pairs);
278 for (l = keys; l != NULL; l = l->next)
280 const gchar *key = l->data;
281 if (g_strcmp0 (key, "host") == 0)
282 host = g_hash_table_lookup (key_value_pairs, key);
283 else if (g_strcmp0 (key, "port") == 0)
284 port = g_hash_table_lookup (key_value_pairs, key);
285 else if (g_strcmp0 (key, "family") == 0)
286 family = g_hash_table_lookup (key_value_pairs, key);
291 G_IO_ERROR_INVALID_ARGUMENT,
292 _("Unsupported key `%s' in address entry `%s'"),
301 port_num = strtol (port, &endp, 10);
302 if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
306 G_IO_ERROR_INVALID_ARGUMENT,
307 _("Error in address `%s' - the port attribute is malformed"),
313 if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
317 G_IO_ERROR_INVALID_ARGUMENT,
318 _("Error in address `%s' - the family attribute is malformed"),
332 * g_dbus_is_supported_address:
334 * @error: Return location for error or %NULL.
336 * Like g_dbus_is_address() but also checks if the library suppors the
337 * transports in @string and that key/value pairs for each transport
340 * Returns: %TRUE if @string is a valid D-Bus address that is
341 * supported by this library, %FALSE if @error is set.
346 g_dbus_is_supported_address (const gchar *string,
355 g_return_val_if_fail (string != NULL, FALSE);
356 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
358 a = g_strsplit (string, ";", 0);
359 for (n = 0; a[n] != NULL; n++)
361 gchar *transport_name;
362 GHashTable *key_value_pairs;
365 if (!_g_dbus_address_parse_entry (a[n],
372 if (g_strcmp0 (transport_name, "unix") == 0)
373 supported = is_valid_unix (a[n], key_value_pairs, error);
374 else if (g_strcmp0 (transport_name, "tcp") == 0)
375 supported = is_valid_tcp (a[n], key_value_pairs, error);
376 else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
377 supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
379 g_free (transport_name);
380 g_hash_table_unref (key_value_pairs);
391 g_assert (ret || (!ret && (error == NULL || *error != NULL)));
397 _g_dbus_address_parse_entry (const gchar *address_entry,
398 gchar **out_transport_name,
399 GHashTable **out_key_value_pairs,
403 GHashTable *key_value_pairs;
404 gchar *transport_name;
411 transport_name = NULL;
412 key_value_pairs = NULL;
414 s = strchr (address_entry, ':');
419 G_IO_ERROR_INVALID_ARGUMENT,
420 _("Address element `%s', does not contain a colon (:)"),
425 transport_name = g_strndup (address_entry, s - address_entry);
426 key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
428 kv_pairs = g_strsplit (s + 1, ",", 0);
429 for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
431 const gchar *kv_pair = kv_pairs[n];
435 s = strchr (kv_pair, '=');
440 G_IO_ERROR_INVALID_ARGUMENT,
441 _("Key/Value pair %d, `%s', in address element `%s', does not contain an equal sign"),
448 /* TODO: actually validate that no illegal characters are present before and after then '=' sign */
449 key = g_uri_unescape_segment (kv_pair, s, NULL);
450 value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
451 g_hash_table_insert (key_value_pairs, key, value);
457 g_strfreev (kv_pairs);
460 if (out_transport_name != NULL)
461 *out_transport_name = transport_name;
463 g_free (transport_name);
464 if (out_key_value_pairs != NULL)
465 *out_key_value_pairs = key_value_pairs;
466 else if (key_value_pairs != NULL)
467 g_hash_table_unref (key_value_pairs);
471 g_free (transport_name);
472 if (key_value_pairs != NULL)
473 g_hash_table_unref (key_value_pairs);
478 /* ---------------------------------------------------------------------------------------------------- */
480 /* TODO: Declare an extension point called GDBusTransport (or similar)
481 * and move code below to extensions implementing said extension
482 * point. That way we can implement a D-Bus transport over X11 without
483 * making libgio link to libX11...
486 g_dbus_address_connect (const gchar *address_entry,
487 const gchar *transport_name,
488 GHashTable *key_value_pairs,
489 GCancellable *cancellable,
493 GSocketConnectable *connectable;
494 const gchar *nonce_file;
504 else if (g_strcmp0 (transport_name, "unix") == 0)
507 const gchar *abstract;
508 path = g_hash_table_lookup (key_value_pairs, "path");
509 abstract = g_hash_table_lookup (key_value_pairs, "abstract");
510 if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
514 G_IO_ERROR_INVALID_ARGUMENT,
515 _("Error in address `%s' - the unix transport requires exactly one of the "
516 "keys `path' or `abstract' to be set"),
519 else if (path != NULL)
521 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
523 else if (abstract != NULL)
525 connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
527 G_UNIX_SOCKET_ADDRESS_ABSTRACT));
531 g_assert_not_reached ();
535 else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
543 is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
545 host = g_hash_table_lookup (key_value_pairs, "host");
550 G_IO_ERROR_INVALID_ARGUMENT,
551 _("Error in address `%s' - the host attribute is missing or malformed"),
556 s = g_hash_table_lookup (key_value_pairs, "port");
559 port = strtol (s, &endp, 10);
560 if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
564 G_IO_ERROR_INVALID_ARGUMENT,
565 _("Error in address `%s' - the port attribute is missing or malformed"),
573 nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
574 if (nonce_file == NULL)
578 G_IO_ERROR_INVALID_ARGUMENT,
579 _("Error in address `%s' - the noncefile attribute is missing or malformed"),
585 /* TODO: deal with family */
586 connectable = g_network_address_new (host, port);
592 G_IO_ERROR_INVALID_ARGUMENT,
593 _("Unknown or unsupported transport `%s' for address `%s'"),
598 if (connectable != NULL)
600 GSocketClient *client;
601 GSocketConnection *connection;
603 g_assert (ret == NULL);
604 client = g_socket_client_new ();
605 connection = g_socket_client_connect (client,
609 g_object_unref (connectable);
610 g_object_unref (client);
611 if (connection == NULL)
614 ret = G_IO_STREAM (connection);
616 if (nonce_file != NULL)
618 gchar *nonce_contents;
621 /* TODO: too dangerous to read the entire file? (think denial-of-service etc.) */
622 if (!g_file_get_contents (nonce_file,
627 g_prefix_error (error, _("Error reading nonce file `%s':"), nonce_file);
628 g_object_unref (ret);
633 if (nonce_length != 16)
637 G_IO_ERROR_INVALID_ARGUMENT,
638 _("The nonce-file `%s' was %" G_GSIZE_FORMAT " bytes. Expected 16 bytes."),
641 g_free (nonce_contents);
642 g_object_unref (ret);
647 if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
654 g_prefix_error (error, _("Error write contents of nonce file `%s' to stream:"), nonce_file);
655 g_object_unref (ret);
657 g_free (nonce_contents);
660 g_free (nonce_contents);
670 g_dbus_address_try_connect_one (const gchar *address_entry,
672 GCancellable *cancellable,
676 GHashTable *key_value_pairs;
677 gchar *transport_name;
681 transport_name = NULL;
682 key_value_pairs = NULL;
684 if (!_g_dbus_address_parse_entry (address_entry,
690 ret = g_dbus_address_connect (address_entry,
698 /* TODO: validate that guid is of correct format */
699 guid = g_hash_table_lookup (key_value_pairs, "guid");
700 if (guid != NULL && out_guid != NULL)
701 *out_guid = g_strdup (guid);
704 g_free (transport_name);
705 if (key_value_pairs != NULL)
706 g_hash_table_unref (key_value_pairs);
711 /* ---------------------------------------------------------------------------------------------------- */
720 get_stream_data_free (GetStreamData *data)
722 g_free (data->address);
723 if (data->stream != NULL)
724 g_object_unref (data->stream);
730 get_stream_thread_func (GSimpleAsyncResult *res,
732 GCancellable *cancellable)
737 data = g_simple_async_result_get_op_res_gpointer (res);
740 data->stream = g_dbus_address_get_stream_sync (data->address,
744 if (data->stream == NULL)
746 g_simple_async_result_set_from_error (res, error);
747 g_error_free (error);
752 * g_dbus_address_get_stream:
753 * @address: A valid D-Bus address.
754 * @cancellable: A #GCancellable or %NULL.
755 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
756 * @user_data: Data to pass to @callback.
758 * Asynchronously connects to an endpoint specified by @address and
759 * sets up the connection so it is in a state to run the client-side
760 * of the D-Bus authentication conversation.
762 * When the operation is finished, @callback will be invoked. You can
763 * then call g_dbus_address_get_stream_finish() to get the result of
766 * This is an asynchronous failable function. See
767 * g_dbus_address_get_stream_sync() for the synchronous version.
772 g_dbus_address_get_stream (const gchar *address,
773 GCancellable *cancellable,
774 GAsyncReadyCallback callback,
777 GSimpleAsyncResult *res;
780 g_return_if_fail (address != NULL);
782 res = g_simple_async_result_new (NULL,
785 g_dbus_address_get_stream);
786 data = g_new0 (GetStreamData, 1);
787 data->address = g_strdup (address);
788 g_simple_async_result_set_op_res_gpointer (res,
790 (GDestroyNotify) get_stream_data_free);
791 g_simple_async_result_run_in_thread (res,
792 get_stream_thread_func,
795 g_object_unref (res);
799 * g_dbus_address_get_stream_finish:
800 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
801 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
802 * @error: Return location for error or %NULL.
804 * Finishes an operation started with g_dbus_address_get_stream().
806 * Returns: A #GIOStream or %NULL if @error is set.
811 g_dbus_address_get_stream_finish (GAsyncResult *res,
815 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
819 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
820 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
822 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_address_get_stream);
826 data = g_simple_async_result_get_op_res_gpointer (simple);
827 if (g_simple_async_result_propagate_error (simple, error))
830 ret = g_object_ref (data->stream);
831 if (out_guid != NULL)
832 *out_guid = g_strdup (data->guid);
839 * g_dbus_address_get_stream_sync:
840 * @address: A valid D-Bus address.
841 * @out_guid: %NULL or return location to store the GUID extracted from @address, if any.
842 * @cancellable: A #GCancellable or %NULL.
843 * @error: Return location for error or %NULL.
845 * Synchronously connects to an endpoint specified by @address and
846 * sets up the connection so it is in a state to run the client-side
847 * of the D-Bus authentication conversation.
849 * This is a synchronous failable function. See
850 * g_dbus_address_get_stream() for the asynchronous version.
852 * Returns: A #GIOStream or %NULL if @error is set.
857 g_dbus_address_get_stream_sync (const gchar *address,
859 GCancellable *cancellable,
867 g_return_val_if_fail (address != NULL, NULL);
868 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
873 addr_array = g_strsplit (address, ";", 0);
875 for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
877 const gchar *addr = addr_array[n];
880 ret = g_dbus_address_try_connect_one (addr,
890 g_assert (this_error != NULL);
891 if (last_error != NULL)
892 g_error_free (last_error);
893 last_error = this_error;
900 if (last_error != NULL)
901 g_error_free (last_error);
905 g_assert (last_error != NULL);
906 g_propagate_error (error, last_error);
911 /* ---------------------------------------------------------------------------------------------------- */
913 /* TODO: implement for UNIX, Win32 and OS X */
915 get_session_address_platform_specific (void)
920 /* ---------------------------------------------------------------------------------------------------- */
923 * g_dbus_address_get_for_bus_sync:
924 * @bus_type: A #GBusType.
925 * @cancellable: A #GCancellable or %NULL.
926 * @error: Return location for error or %NULL.
928 * Synchronously looks up the D-Bus address for the well-known message
929 * bus instance specified by @bus_type. This may involve using various
930 * platform specific mechanisms.
932 * Returns: A valid D-Bus address string for @bus_type or %NULL if @error is set.
937 g_dbus_address_get_for_bus_sync (GBusType bus_type,
938 GCancellable *cancellable,
942 const gchar *starter_bus;
944 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
950 case G_BUS_TYPE_SYSTEM:
951 ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
954 ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
958 case G_BUS_TYPE_SESSION:
959 ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
962 ret = get_session_address_platform_specific ();
968 _("Cannot determine session bus address (TODO: run dbus-launch to find out)"));
973 case G_BUS_TYPE_STARTER:
974 starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
975 if (g_strcmp0 (starter_bus, "session") == 0)
977 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, error);
980 else if (g_strcmp0 (starter_bus, "system") == 0)
982 ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
987 if (starter_bus != NULL)
992 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
993 " - unknown value `%s'"),
998 g_set_error_literal (error,
1001 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
1002 "variable is not set"));
1011 _("Unknown bus type %d"),
1020 #define __G_DBUS_ADDRESS_C__
1021 #include "gioaliasdef.c"