2010-05-06 20:13:59 +02:00
|
|
|
/* GDBus - GLib D-Bus Library
|
|
|
|
*
|
2010-05-09 19:14:55 +02:00
|
|
|
* Copyright (C) 2008-2010 Red Hat, Inc.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
|
|
|
* Author: David Zeuthen <davidz@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2010-05-06 22:34:23 +02:00
|
|
|
#include <string.h>
|
2010-05-06 20:13:59 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "giotypes.h"
|
2010-05-20 16:51:00 +02:00
|
|
|
#include "gioerror.h"
|
2010-05-06 20:13:59 +02:00
|
|
|
#include "gdbusaddress.h"
|
|
|
|
#include "gdbusutils.h"
|
|
|
|
#include "gdbusconnection.h"
|
|
|
|
#include "gdbusserver.h"
|
|
|
|
#include "gioenumtypes.h"
|
|
|
|
#include "gdbusprivate.h"
|
|
|
|
#include "gdbusauthobserver.h"
|
2010-05-06 22:34:23 +02:00
|
|
|
#include "ginitable.h"
|
|
|
|
#include "gsocketservice.h"
|
2010-05-20 16:51:00 +02:00
|
|
|
#include "gthreadedsocketservice.h"
|
|
|
|
#include "gresolver.h"
|
2013-01-25 18:05:26 +01:00
|
|
|
#include "glib/gstdio.h"
|
2010-05-20 16:51:00 +02:00
|
|
|
#include "ginetaddress.h"
|
|
|
|
#include "ginetsocketaddress.h"
|
|
|
|
#include "ginputstream.h"
|
|
|
|
#include "giostream.h"
|
2019-05-31 04:29:18 +02:00
|
|
|
#include "gmarshal-internal.h"
|
2010-05-06 22:34:23 +02:00
|
|
|
|
2013-11-24 04:51:21 +01:00
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2010-05-06 22:34:23 +02:00
|
|
|
#include "gunixsocketaddress.h"
|
|
|
|
|
|
|
|
#include "glibintl.h"
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2021-02-08 12:57:45 +01:00
|
|
|
#define G_DBUS_SERVER_FLAGS_ALL \
|
|
|
|
(G_DBUS_SERVER_FLAGS_RUN_IN_THREAD | \
|
2020-12-15 14:00:28 +01:00
|
|
|
G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS | \
|
|
|
|
G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
|
2021-02-08 12:57:45 +01:00
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
/**
|
2023-10-24 11:52:39 +02:00
|
|
|
* GDBusServer:
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
2023-10-24 11:52:39 +02:00
|
|
|
* `GDBusServer` is a helper for listening to and accepting D-Bus
|
2011-04-04 10:01:51 +02:00
|
|
|
* connections. This can be used to create a new D-Bus server, allowing two
|
|
|
|
* peers to use the D-Bus protocol for their own specialized communication.
|
|
|
|
* A server instance provided in this way will not perform message routing or
|
2023-10-24 11:52:39 +02:00
|
|
|
* implement the
|
|
|
|
* [`org.freedesktop.DBus` interface](https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-messages).
|
2011-04-04 10:01:51 +02:00
|
|
|
*
|
|
|
|
* To just export an object on a well-known name on a message bus, such as the
|
2023-10-24 11:52:39 +02:00
|
|
|
* session or system bus, you should instead use [func@Gio.bus_own_name].
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
2020-10-26 15:28:15 +01:00
|
|
|
* An example of peer-to-peer communication with GDBus can be found
|
2021-06-07 14:16:50 +02:00
|
|
|
* in [gdbus-example-peer.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-peer.c).
|
2019-06-05 14:44:10 +02:00
|
|
|
*
|
2023-10-24 11:52:39 +02:00
|
|
|
* Note that a minimal `GDBusServer` will accept connections from any
|
|
|
|
* peer. In many use-cases it will be necessary to add a
|
|
|
|
* [class@Gio.DBusAuthObserver] that only accepts connections that have
|
|
|
|
* successfully authenticated as the same user that is running the
|
|
|
|
* `GDBusServer`. Since GLib 2.68 this can be achieved more simply by passing
|
|
|
|
* the `G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER` flag to the
|
|
|
|
* server.
|
2010-07-07 21:57:37 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
2010-07-07 22:35:17 +02:00
|
|
|
struct _GDBusServer
|
2010-07-07 21:57:37 +02:00
|
|
|
{
|
|
|
|
/*< private >*/
|
2010-07-07 22:35:17 +02:00
|
|
|
GObject parent_instance;
|
2010-07-07 21:57:37 +02:00
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
GDBusServerFlags flags;
|
|
|
|
gchar *address;
|
|
|
|
gchar *guid;
|
|
|
|
|
|
|
|
guchar *nonce;
|
|
|
|
gchar *nonce_file;
|
|
|
|
|
|
|
|
gchar *client_address;
|
|
|
|
|
2019-06-12 20:55:06 +02:00
|
|
|
gchar *unix_socket_path;
|
2010-05-06 20:13:59 +02:00
|
|
|
GSocketListener *listener;
|
|
|
|
gboolean is_using_listener;
|
2010-07-20 18:14:30 +02:00
|
|
|
gulong run_signal_handler_id;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
/* The result of g_main_context_ref_thread_default() when the object
|
2010-05-06 20:13:59 +02:00
|
|
|
* was created (the GObject _init() function) - this is used for delivery
|
|
|
|
* of the :new-connection GObject signal.
|
|
|
|
*/
|
|
|
|
GMainContext *main_context_at_construction;
|
|
|
|
|
|
|
|
gboolean active;
|
|
|
|
|
|
|
|
GDBusAuthObserver *authentication_observer;
|
|
|
|
};
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
typedef struct _GDBusServerClass GDBusServerClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServerClass:
|
|
|
|
* @new_connection: Signal class handler for the #GDBusServer::new-connection signal.
|
|
|
|
*
|
|
|
|
* Class structure for #GDBusServer.
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
*/
|
|
|
|
struct _GDBusServerClass
|
|
|
|
{
|
|
|
|
/*< private >*/
|
|
|
|
GObjectClass parent_class;
|
|
|
|
|
|
|
|
/*< public >*/
|
|
|
|
/* Signals */
|
2010-09-09 20:00:46 +02:00
|
|
|
gboolean (*new_connection) (GDBusServer *server,
|
|
|
|
GDBusConnection *connection);
|
2010-07-07 22:35:17 +02:00
|
|
|
};
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ADDRESS,
|
|
|
|
PROP_CLIENT_ADDRESS,
|
|
|
|
PROP_FLAGS,
|
|
|
|
PROP_GUID,
|
|
|
|
PROP_ACTIVE,
|
|
|
|
PROP_AUTHENTICATION_OBSERVER,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
NEW_CONNECTION_SIGNAL,
|
|
|
|
LAST_SIGNAL,
|
|
|
|
};
|
|
|
|
|
2012-02-24 02:12:08 +01:00
|
|
|
static guint _signals[LAST_SIGNAL] = {0};
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
static void initable_iface_init (GInitableIface *initable_iface);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GDBusServer, g_dbus_server, G_TYPE_OBJECT,
|
2014-10-17 12:54:02 +02:00
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2019-06-12 20:29:52 +02:00
|
|
|
static void
|
|
|
|
g_dbus_server_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GDBusServer *server = G_DBUS_SERVER (object);
|
|
|
|
|
|
|
|
if (server->active)
|
|
|
|
g_dbus_server_stop (server);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_dbus_server_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
static void
|
|
|
|
g_dbus_server_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GDBusServer *server = G_DBUS_SERVER (object);
|
|
|
|
|
2019-10-28 21:42:19 +01:00
|
|
|
g_assert (!server->active);
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (server->authentication_observer != NULL)
|
|
|
|
g_object_unref (server->authentication_observer);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-07-20 18:14:30 +02:00
|
|
|
if (server->run_signal_handler_id > 0)
|
|
|
|
g_signal_handler_disconnect (server->listener, server->run_signal_handler_id);
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (server->listener != NULL)
|
|
|
|
g_object_unref (server->listener);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
g_free (server->address);
|
|
|
|
g_free (server->guid);
|
|
|
|
g_free (server->client_address);
|
|
|
|
if (server->nonce != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
memset (server->nonce, '\0', 16);
|
|
|
|
g_free (server->nonce);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
2019-06-12 20:55:06 +02:00
|
|
|
|
2019-10-28 21:42:19 +01:00
|
|
|
g_free (server->unix_socket_path);
|
|
|
|
g_free (server->nonce_file);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
g_main_context_unref (server->main_context_at_construction);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-05-10 04:13:18 +02:00
|
|
|
G_OBJECT_CLASS (g_dbus_server_parent_class)->finalize (object);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_dbus_server_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GDBusServer *server = G_DBUS_SERVER (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_FLAGS:
|
2010-07-07 22:35:17 +02:00
|
|
|
g_value_set_flags (value, server->flags);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_GUID:
|
2010-07-07 22:35:17 +02:00
|
|
|
g_value_set_string (value, server->guid);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ADDRESS:
|
2010-07-07 22:35:17 +02:00
|
|
|
g_value_set_string (value, server->address);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CLIENT_ADDRESS:
|
2010-07-07 22:35:17 +02:00
|
|
|
g_value_set_string (value, server->client_address);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ACTIVE:
|
2010-07-07 22:35:17 +02:00
|
|
|
g_value_set_boolean (value, server->active);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_AUTHENTICATION_OBSERVER:
|
2010-07-07 22:35:17 +02:00
|
|
|
g_value_set_object (value, server->authentication_observer);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_dbus_server_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GDBusServer *server = G_DBUS_SERVER (object);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_FLAGS:
|
2010-07-07 22:35:17 +02:00
|
|
|
server->flags = g_value_get_flags (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_GUID:
|
2010-07-07 22:35:17 +02:00
|
|
|
server->guid = g_value_dup_string (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ADDRESS:
|
2010-07-07 22:35:17 +02:00
|
|
|
server->address = g_value_dup_string (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_AUTHENTICATION_OBSERVER:
|
2010-07-07 22:35:17 +02:00
|
|
|
server->authentication_observer = g_value_dup_object (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_dbus_server_class_init (GDBusServerClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2019-06-12 20:29:52 +02:00
|
|
|
gobject_class->dispose = g_dbus_server_dispose;
|
2010-05-06 20:13:59 +02:00
|
|
|
gobject_class->finalize = g_dbus_server_finalize;
|
|
|
|
gobject_class->set_property = g_dbus_server_set_property;
|
|
|
|
gobject_class->get_property = g_dbus_server_get_property;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer:flags:
|
|
|
|
*
|
|
|
|
* Flags from the #GDBusServerFlags enumeration.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_FLAGS,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_flags ("flags", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
G_TYPE_DBUS_SERVER_FLAGS,
|
|
|
|
G_DBUS_SERVER_FLAGS_NONE,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_WRITABLE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_NAME |
|
|
|
|
G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer:guid:
|
|
|
|
*
|
2021-06-10 13:36:25 +02:00
|
|
|
* The GUID of the server.
|
|
|
|
*
|
|
|
|
* See #GDBusConnection:guid for more details.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_GUID,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_string ("guid", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
NULL,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_WRITABLE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_NAME |
|
|
|
|
G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer:address:
|
|
|
|
*
|
|
|
|
* The D-Bus address to listen on.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_ADDRESS,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_string ("address", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
NULL,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_WRITABLE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_NAME |
|
|
|
|
G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer:client-address:
|
|
|
|
*
|
|
|
|
* The D-Bus address that clients can use.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_CLIENT_ADDRESS,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_string ("client-address", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
NULL,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_STATIC_NAME |
|
|
|
|
G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer:active:
|
|
|
|
*
|
|
|
|
* Whether the server is currently active.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_ACTIVE,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_boolean ("active", NULL, NULL,
|
2010-07-28 18:58:04 +02:00
|
|
|
FALSE,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_STATIC_NAME |
|
|
|
|
G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer:authentication-observer:
|
|
|
|
*
|
|
|
|
* A #GDBusAuthObserver object to assist in the authentication process or %NULL.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_AUTHENTICATION_OBSERVER,
|
2023-04-28 01:59:26 +02:00
|
|
|
g_param_spec_object ("authentication-observer", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
G_TYPE_DBUS_AUTH_OBSERVER,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_WRITABLE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_NAME |
|
|
|
|
G_PARAM_STATIC_BLURB |
|
|
|
|
G_PARAM_STATIC_NICK));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GDBusServer::new-connection:
|
|
|
|
* @server: The #GDBusServer emitting the signal.
|
|
|
|
* @connection: A #GDBusConnection for the new connection.
|
|
|
|
*
|
|
|
|
* Emitted when a new authenticated connection has been made. Use
|
|
|
|
* g_dbus_connection_get_peer_credentials() to figure out what
|
|
|
|
* identity (if any), was authenticated.
|
|
|
|
*
|
2010-09-09 20:00:46 +02:00
|
|
|
* If you want to accept the connection, take a reference to the
|
|
|
|
* @connection object and return %TRUE. When you are done with the
|
|
|
|
* connection call g_dbus_connection_close() and give up your
|
|
|
|
* reference. Note that the other peer may disconnect at any time -
|
|
|
|
* a typical thing to do when accepting a connection is to listen to
|
|
|
|
* the #GDBusConnection::closed signal.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
|
|
|
* If #GDBusServer:flags contains %G_DBUS_SERVER_FLAGS_RUN_IN_THREAD
|
|
|
|
* then the signal is emitted in a new thread dedicated to the
|
2014-02-08 18:26:56 +01:00
|
|
|
* connection. Otherwise the signal is emitted in the
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
* of the thread that @server was constructed in.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
2010-06-30 17:43:42 +02:00
|
|
|
* You are guaranteed that signal handlers for this signal runs
|
|
|
|
* before incoming messages on @connection are processed. This means
|
|
|
|
* that it's suitable to call g_dbus_connection_register_object() or
|
|
|
|
* similar from the signal handler.
|
|
|
|
*
|
2010-09-09 20:00:46 +02:00
|
|
|
* Returns: %TRUE to claim @connection, %FALSE to let other handlers
|
|
|
|
* run.
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
2015-09-12 06:00:40 +02:00
|
|
|
_signals[NEW_CONNECTION_SIGNAL] = g_signal_new (I_("new-connection"),
|
2010-05-06 20:13:59 +02:00
|
|
|
G_TYPE_DBUS_SERVER,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GDBusServerClass, new_connection),
|
2010-09-09 20:00:46 +02:00
|
|
|
g_signal_accumulator_true_handled,
|
|
|
|
NULL, /* accu_data */
|
2019-05-31 04:29:18 +02:00
|
|
|
_g_cclosure_marshal_BOOLEAN__OBJECT,
|
2010-09-09 20:00:46 +02:00
|
|
|
G_TYPE_BOOLEAN,
|
2010-05-06 20:13:59 +02:00
|
|
|
1,
|
|
|
|
G_TYPE_DBUS_CONNECTION);
|
2019-05-31 04:29:18 +02:00
|
|
|
g_signal_set_va_marshaller (_signals[NEW_CONNECTION_SIGNAL],
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
_g_cclosure_marshal_BOOLEAN__OBJECTv);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_dbus_server_init (GDBusServer *server)
|
|
|
|
{
|
2011-10-05 16:46:57 +02:00
|
|
|
server->main_context_at_construction = g_main_context_ref_thread_default ();
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
on_run (GSocketService *service,
|
|
|
|
GSocketConnection *socket_connection,
|
|
|
|
GObject *source_object,
|
|
|
|
gpointer user_data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_new_sync:
|
|
|
|
* @address: A D-Bus address.
|
|
|
|
* @flags: Flags from the #GDBusServerFlags enumeration.
|
|
|
|
* @guid: A D-Bus GUID.
|
2016-10-29 03:29:02 +02:00
|
|
|
* @observer: (nullable): A #GDBusAuthObserver or %NULL.
|
|
|
|
* @cancellable: (nullable): A #GCancellable or %NULL.
|
2010-05-06 20:13:59 +02:00
|
|
|
* @error: Return location for server or %NULL.
|
|
|
|
*
|
|
|
|
* Creates a new D-Bus server that listens on the first address in
|
|
|
|
* @address that works.
|
|
|
|
*
|
|
|
|
* Once constructed, you can use g_dbus_server_get_client_address() to
|
|
|
|
* get a D-Bus address string that clients can use to connect.
|
|
|
|
*
|
2019-06-05 14:44:10 +02:00
|
|
|
* To have control over the available authentication mechanisms and
|
|
|
|
* the users that are authorized to connect, it is strongly recommended
|
|
|
|
* to provide a non-%NULL #GDBusAuthObserver.
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
* Connect to the #GDBusServer::new-connection signal to handle
|
|
|
|
* incoming connections.
|
|
|
|
*
|
|
|
|
* The returned #GDBusServer isn't active - you have to start it with
|
|
|
|
* g_dbus_server_start().
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
* #GDBusServer is used in this [example][gdbus-peer-to-peer].
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
2019-06-12 13:48:49 +02:00
|
|
|
* This is a synchronous failable constructor. There is currently no
|
|
|
|
* asynchronous version.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
|
|
|
* Returns: A #GDBusServer or %NULL if @error is set. Free with
|
|
|
|
* g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
GDBusServer *
|
2010-05-10 04:13:18 +02:00
|
|
|
g_dbus_server_new_sync (const gchar *address,
|
|
|
|
GDBusServerFlags flags,
|
|
|
|
const gchar *guid,
|
|
|
|
GDBusAuthObserver *observer,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
|
|
|
GDBusServer *server;
|
|
|
|
|
|
|
|
g_return_val_if_fail (address != NULL, NULL);
|
|
|
|
g_return_val_if_fail (g_dbus_is_guid (guid), NULL);
|
2021-02-08 12:57:45 +01:00
|
|
|
g_return_val_if_fail ((flags & ~G_DBUS_SERVER_FLAGS_ALL) == 0, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
|
|
server = g_initable_new (G_TYPE_DBUS_SERVER,
|
|
|
|
cancellable,
|
|
|
|
error,
|
|
|
|
"address", address,
|
|
|
|
"flags", flags,
|
|
|
|
"guid", guid,
|
|
|
|
"authentication-observer", observer,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_get_client_address:
|
|
|
|
* @server: A #GDBusServer.
|
|
|
|
*
|
2017-02-08 16:06:00 +01:00
|
|
|
* Gets a
|
|
|
|
* [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
|
|
|
|
* string that can be used by clients to connect to @server.
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
2021-05-27 16:06:05 +02:00
|
|
|
* This is valid and non-empty if initializing the #GDBusServer succeeded.
|
|
|
|
*
|
|
|
|
* Returns: (not nullable): A D-Bus address string. Do not free, the string is owned
|
2010-05-06 20:13:59 +02:00
|
|
|
* by @server.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
const gchar *
|
|
|
|
g_dbus_server_get_client_address (GDBusServer *server)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
|
2010-07-07 22:35:17 +02:00
|
|
|
return server->client_address;
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_get_guid:
|
|
|
|
* @server: A #GDBusServer.
|
|
|
|
*
|
2021-05-27 16:06:05 +02:00
|
|
|
* Gets the GUID for @server, as provided to g_dbus_server_new_sync().
|
2010-05-06 20:13:59 +02:00
|
|
|
*
|
2021-05-27 16:06:05 +02:00
|
|
|
* Returns: (not nullable): A D-Bus GUID. Do not free this string, it is owned by @server.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
const gchar *
|
|
|
|
g_dbus_server_get_guid (GDBusServer *server)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_SERVER (server), NULL);
|
2010-07-07 22:35:17 +02:00
|
|
|
return server->guid;
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_get_flags:
|
|
|
|
* @server: A #GDBusServer.
|
|
|
|
*
|
|
|
|
* Gets the flags for @server.
|
|
|
|
*
|
|
|
|
* Returns: A set of flags from the #GDBusServerFlags enumeration.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
GDBusServerFlags
|
|
|
|
g_dbus_server_get_flags (GDBusServer *server)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
|
2010-07-07 22:35:17 +02:00
|
|
|
return server->flags;
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_is_active:
|
|
|
|
* @server: A #GDBusServer.
|
|
|
|
*
|
|
|
|
* Gets whether @server is active.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if server is active, %FALSE otherwise.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
g_dbus_server_is_active (GDBusServer *server)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_SERVER (server), G_DBUS_SERVER_FLAGS_NONE);
|
2010-07-07 22:35:17 +02:00
|
|
|
return server->active;
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_start:
|
|
|
|
* @server: A #GDBusServer.
|
|
|
|
*
|
|
|
|
* Starts @server.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_dbus_server_start (GDBusServer *server)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_DBUS_SERVER (server));
|
2010-07-07 22:35:17 +02:00
|
|
|
if (server->active)
|
2010-05-06 20:13:59 +02:00
|
|
|
return;
|
|
|
|
/* Right now we don't have any transport not using the listener... */
|
2010-07-07 22:35:17 +02:00
|
|
|
g_assert (server->is_using_listener);
|
2019-10-28 21:44:07 +01:00
|
|
|
server->run_signal_handler_id = g_signal_connect_data (G_SOCKET_SERVICE (server->listener),
|
|
|
|
"run",
|
|
|
|
G_CALLBACK (on_run),
|
|
|
|
g_object_ref (server),
|
|
|
|
(GClosureNotify) g_object_unref,
|
2022-06-23 10:41:21 +02:00
|
|
|
G_CONNECT_DEFAULT);
|
2010-07-07 22:35:17 +02:00
|
|
|
g_socket_service_start (G_SOCKET_SERVICE (server->listener));
|
|
|
|
server->active = TRUE;
|
2010-05-06 20:13:59 +02:00
|
|
|
g_object_notify (G_OBJECT (server), "active");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dbus_server_stop:
|
|
|
|
* @server: A #GDBusServer.
|
|
|
|
*
|
|
|
|
* Stops @server.
|
2010-05-06 22:02:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
g_dbus_server_stop (GDBusServer *server)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_DBUS_SERVER (server));
|
2010-07-07 22:35:17 +02:00
|
|
|
if (!server->active)
|
2010-05-06 20:13:59 +02:00
|
|
|
return;
|
|
|
|
/* Right now we don't have any transport not using the listener... */
|
2010-07-07 22:35:17 +02:00
|
|
|
g_assert (server->is_using_listener);
|
2010-07-20 18:14:30 +02:00
|
|
|
g_assert (server->run_signal_handler_id > 0);
|
2019-05-21 19:10:01 +02:00
|
|
|
g_clear_signal_handler (&server->run_signal_handler_id, server->listener);
|
2010-07-07 22:35:17 +02:00
|
|
|
g_socket_service_stop (G_SOCKET_SERVICE (server->listener));
|
|
|
|
server->active = FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
g_object_notify (G_OBJECT (server), "active");
|
2019-10-28 21:42:19 +01:00
|
|
|
|
|
|
|
if (server->unix_socket_path)
|
|
|
|
{
|
|
|
|
if (g_unlink (server->unix_socket_path) != 0)
|
|
|
|
g_warning ("Failed to delete %s: %s", server->unix_socket_path, g_strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server->nonce_file)
|
|
|
|
{
|
|
|
|
if (g_unlink (server->nonce_file) != 0)
|
|
|
|
g_warning ("Failed to delete %s: %s", server->nonce_file, g_strerror (errno));
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gint
|
|
|
|
random_ascii (void)
|
|
|
|
{
|
|
|
|
gint ret;
|
|
|
|
ret = g_random_int_range (0, 60);
|
|
|
|
if (ret < 25)
|
|
|
|
ret += 'A';
|
|
|
|
else if (ret < 50)
|
|
|
|
ret += 'a' - 25;
|
|
|
|
else
|
|
|
|
ret += '0' - 50;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-12 05:35:19 +02:00
|
|
|
/* note that address_entry has already been validated => exactly one of path, dir, tmpdir, or abstract keys are set */
|
2010-05-06 20:13:59 +02:00
|
|
|
static gboolean
|
|
|
|
try_unix (GDBusServer *server,
|
|
|
|
const gchar *address_entry,
|
|
|
|
GHashTable *key_value_pairs,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
const gchar *path;
|
2019-06-12 05:35:19 +02:00
|
|
|
const gchar *dir;
|
2010-05-06 20:13:59 +02:00
|
|
|
const gchar *tmpdir;
|
|
|
|
const gchar *abstract;
|
|
|
|
GSocketAddress *address;
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
address = NULL;
|
|
|
|
|
|
|
|
path = g_hash_table_lookup (key_value_pairs, "path");
|
2019-06-12 05:35:19 +02:00
|
|
|
dir = g_hash_table_lookup (key_value_pairs, "dir");
|
2010-05-06 20:13:59 +02:00
|
|
|
tmpdir = g_hash_table_lookup (key_value_pairs, "tmpdir");
|
|
|
|
abstract = g_hash_table_lookup (key_value_pairs, "abstract");
|
|
|
|
|
|
|
|
if (path != NULL)
|
|
|
|
{
|
|
|
|
address = g_unix_socket_address_new (path);
|
|
|
|
}
|
2019-06-12 05:35:19 +02:00
|
|
|
else if (dir != NULL || tmpdir != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
|
|
|
gint n;
|
|
|
|
GString *s;
|
|
|
|
GError *local_error;
|
|
|
|
|
|
|
|
retry:
|
2019-06-12 05:35:19 +02:00
|
|
|
s = g_string_new (tmpdir != NULL ? tmpdir : dir);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_string_append (s, "/dbus-");
|
|
|
|
for (n = 0; n < 8; n++)
|
|
|
|
g_string_append_c (s, random_ascii ());
|
|
|
|
|
2022-10-21 16:52:19 +02:00
|
|
|
address = g_unix_socket_address_new (s->str);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
|
|
|
local_error = NULL;
|
2010-07-07 22:35:17 +02:00
|
|
|
if (!g_socket_listener_add_address (server->listener,
|
2010-05-06 20:13:59 +02:00
|
|
|
address,
|
|
|
|
G_SOCKET_TYPE_STREAM,
|
|
|
|
G_SOCKET_PROTOCOL_DEFAULT,
|
|
|
|
NULL, /* source_object */
|
|
|
|
NULL, /* effective_address */
|
|
|
|
&local_error))
|
|
|
|
{
|
|
|
|
if (local_error->domain == G_IO_ERROR && local_error->code == G_IO_ERROR_ADDRESS_IN_USE)
|
|
|
|
{
|
|
|
|
g_error_free (local_error);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
g_propagate_error (error, local_error);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = TRUE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (abstract != NULL)
|
|
|
|
{
|
|
|
|
if (!g_unix_socket_address_abstract_names_supported ())
|
|
|
|
{
|
|
|
|
g_set_error_literal (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_NOT_SUPPORTED,
|
2019-06-12 20:38:51 +02:00
|
|
|
_("Abstract namespace not supported"));
|
2010-05-06 20:13:59 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
address = g_unix_socket_address_new_with_type (abstract,
|
|
|
|
-1,
|
|
|
|
G_UNIX_SOCKET_ADDRESS_ABSTRACT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (!g_socket_listener_add_address (server->listener,
|
2010-05-06 20:13:59 +02:00
|
|
|
address,
|
|
|
|
G_SOCKET_TYPE_STREAM,
|
|
|
|
G_SOCKET_PROTOCOL_DEFAULT,
|
|
|
|
NULL, /* source_object */
|
|
|
|
NULL, /* effective_address */
|
|
|
|
error))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
/* Fill out client_address if the connection attempt worked */
|
|
|
|
if (ret)
|
|
|
|
{
|
2019-06-13 20:04:17 +02:00
|
|
|
const char *address_path;
|
|
|
|
char *escaped_path;
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
server->is_using_listener = TRUE;
|
2019-06-13 20:04:17 +02:00
|
|
|
address_path = g_unix_socket_address_get_path (G_UNIX_SOCKET_ADDRESS (address));
|
|
|
|
escaped_path = g_dbus_address_escape_value (address_path);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
switch (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (address)))
|
|
|
|
{
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
|
2019-06-13 20:04:17 +02:00
|
|
|
server->client_address = g_strdup_printf ("unix:abstract=%s", escaped_path);
|
2010-05-06 20:13:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case G_UNIX_SOCKET_ADDRESS_PATH:
|
2019-06-13 20:04:17 +02:00
|
|
|
server->client_address = g_strdup_printf ("unix:path=%s", escaped_path);
|
|
|
|
server->unix_socket_path = g_strdup (address_path);
|
|
|
|
break;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2019-06-13 20:04:17 +02:00
|
|
|
|
|
|
|
g_free (escaped_path);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
g_object_unref (address);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* note that address_entry has already been validated =>
|
2011-08-29 20:49:32 +02:00
|
|
|
* both host and port (guaranteed to be a number in [0, 65535]) are set (family is optional)
|
2010-05-06 20:13:59 +02:00
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
try_tcp (GDBusServer *server,
|
|
|
|
const gchar *address_entry,
|
|
|
|
GHashTable *key_value_pairs,
|
|
|
|
gboolean do_nonce,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
const gchar *host;
|
|
|
|
const gchar *port;
|
|
|
|
gint port_num;
|
|
|
|
GResolver *resolver;
|
|
|
|
GList *resolved_addresses;
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
resolver = NULL;
|
|
|
|
resolved_addresses = NULL;
|
|
|
|
|
|
|
|
host = g_hash_table_lookup (key_value_pairs, "host");
|
|
|
|
port = g_hash_table_lookup (key_value_pairs, "port");
|
2011-04-08 21:44:25 +02:00
|
|
|
/* family = g_hash_table_lookup (key_value_pairs, "family"); */
|
2010-05-06 20:13:59 +02:00
|
|
|
if (g_hash_table_lookup (key_value_pairs, "noncefile") != NULL)
|
|
|
|
{
|
|
|
|
g_set_error_literal (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
_("Cannot specify nonce file when creating a server"));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host == NULL)
|
|
|
|
host = "localhost";
|
|
|
|
if (port == NULL)
|
|
|
|
port = "0";
|
|
|
|
port_num = strtol (port, NULL, 10);
|
|
|
|
|
|
|
|
resolver = g_resolver_get_default ();
|
|
|
|
resolved_addresses = g_resolver_lookup_by_name (resolver,
|
|
|
|
host,
|
|
|
|
NULL,
|
|
|
|
error);
|
|
|
|
if (resolved_addresses == NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
goto out;
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
/* TODO: handle family */
|
|
|
|
for (l = resolved_addresses; l != NULL; l = l->next)
|
|
|
|
{
|
|
|
|
GInetAddress *address = G_INET_ADDRESS (l->data);
|
|
|
|
GSocketAddress *socket_address;
|
|
|
|
GSocketAddress *effective_address;
|
|
|
|
|
|
|
|
socket_address = g_inet_socket_address_new (address, port_num);
|
2010-07-07 22:35:17 +02:00
|
|
|
if (!g_socket_listener_add_address (server->listener,
|
2010-05-06 20:13:59 +02:00
|
|
|
socket_address,
|
|
|
|
G_SOCKET_TYPE_STREAM,
|
|
|
|
G_SOCKET_PROTOCOL_TCP,
|
|
|
|
NULL, /* GObject *source_object */
|
|
|
|
&effective_address,
|
|
|
|
error))
|
|
|
|
{
|
|
|
|
g_object_unref (socket_address);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (port_num == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
/* make sure we allocate the same port number for other listeners */
|
|
|
|
port_num = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (effective_address));
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
g_object_unref (effective_address);
|
|
|
|
g_object_unref (socket_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_nonce)
|
|
|
|
{
|
|
|
|
gint fd;
|
|
|
|
guint n;
|
|
|
|
gsize bytes_written;
|
|
|
|
gsize bytes_remaining;
|
2012-04-17 15:50:53 +02:00
|
|
|
char *file_escaped;
|
2019-06-13 20:04:17 +02:00
|
|
|
char *host_escaped;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
server->nonce = g_new0 (guchar, 16);
|
2010-05-06 20:13:59 +02:00
|
|
|
for (n = 0; n < 16; n++)
|
2010-07-07 22:35:17 +02:00
|
|
|
server->nonce[n] = g_random_int_range (0, 256);
|
2010-05-06 20:13:59 +02:00
|
|
|
fd = g_file_open_tmp ("gdbus-nonce-file-XXXXXX",
|
2010-07-07 22:35:17 +02:00
|
|
|
&server->nonce_file,
|
2010-05-06 20:13:59 +02:00
|
|
|
error);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
g_socket_listener_close (server->listener);
|
2010-05-06 20:13:59 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
again:
|
|
|
|
bytes_written = 0;
|
|
|
|
bytes_remaining = 16;
|
|
|
|
while (bytes_remaining > 0)
|
|
|
|
{
|
2022-01-19 18:31:31 +01:00
|
|
|
gssize size;
|
2017-07-31 12:30:55 +02:00
|
|
|
int errsv;
|
|
|
|
|
2022-01-19 18:31:31 +01:00
|
|
|
size = write (fd, server->nonce + bytes_written, bytes_remaining);
|
2017-07-31 12:30:55 +02:00
|
|
|
errsv = errno;
|
2022-01-19 18:31:31 +01:00
|
|
|
if (size == -1)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
2017-07-31 12:30:55 +02:00
|
|
|
if (errsv == EINTR)
|
2010-05-06 20:13:59 +02:00
|
|
|
goto again;
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
2017-07-31 12:30:55 +02:00
|
|
|
g_io_error_from_errno (errsv),
|
2016-09-30 05:47:15 +02:00
|
|
|
_("Error writing nonce file at “%s”: %s"),
|
2010-07-07 22:35:17 +02:00
|
|
|
server->nonce_file,
|
2017-07-31 12:30:55 +02:00
|
|
|
g_strerror (errsv));
|
2010-05-06 20:13:59 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2022-01-19 18:31:31 +01:00
|
|
|
bytes_written += size;
|
|
|
|
bytes_remaining -= size;
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
2013-01-25 18:05:26 +01:00
|
|
|
if (!g_close (fd, error))
|
|
|
|
goto out;
|
2019-06-13 20:04:17 +02:00
|
|
|
host_escaped = g_dbus_address_escape_value (host);
|
|
|
|
file_escaped = g_dbus_address_escape_value (server->nonce_file);
|
2010-07-07 22:35:17 +02:00
|
|
|
server->client_address = g_strdup_printf ("nonce-tcp:host=%s,port=%d,noncefile=%s",
|
2019-06-13 20:04:17 +02:00
|
|
|
host_escaped,
|
2010-07-07 22:35:17 +02:00
|
|
|
port_num,
|
2012-04-17 15:50:53 +02:00
|
|
|
file_escaped);
|
2019-06-13 20:04:17 +02:00
|
|
|
g_free (host_escaped);
|
2012-04-17 15:50:53 +02:00
|
|
|
g_free (file_escaped);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
server->client_address = g_strdup_printf ("tcp:host=%s,port=%d", host, port_num);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
2010-07-07 22:35:17 +02:00
|
|
|
server->is_using_listener = TRUE;
|
2010-05-06 20:13:59 +02:00
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
out:
|
2012-01-02 16:30:11 +01:00
|
|
|
g_list_free_full (resolved_addresses, g_object_unref);
|
2013-11-28 21:41:49 +01:00
|
|
|
if (resolver)
|
|
|
|
g_object_unref (resolver);
|
2010-05-06 20:13:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GDBusServer *server;
|
|
|
|
GDBusConnection *connection;
|
|
|
|
} EmitIdleData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
emit_idle_data_free (EmitIdleData *data)
|
|
|
|
{
|
|
|
|
g_object_unref (data->server);
|
|
|
|
g_object_unref (data->connection);
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
emit_new_connection_in_idle (gpointer user_data)
|
|
|
|
{
|
|
|
|
EmitIdleData *data = user_data;
|
2010-09-09 20:00:46 +02:00
|
|
|
gboolean claimed;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-09-09 20:00:46 +02:00
|
|
|
claimed = FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
g_signal_emit (data->server,
|
|
|
|
_signals[NEW_CONNECTION_SIGNAL],
|
|
|
|
0,
|
2010-09-09 20:00:46 +02:00
|
|
|
data->connection,
|
|
|
|
&claimed);
|
|
|
|
|
|
|
|
if (claimed)
|
|
|
|
g_dbus_connection_start_message_processing (data->connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_object_unref (data->connection);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called in new thread */
|
|
|
|
static gboolean
|
|
|
|
on_run (GSocketService *service,
|
|
|
|
GSocketConnection *socket_connection,
|
|
|
|
GObject *source_object,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GDBusServer *server = G_DBUS_SERVER (user_data);
|
|
|
|
GDBusConnection *connection;
|
|
|
|
GDBusConnectionFlags connection_flags;
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (server->nonce != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
|
|
|
gchar buf[16];
|
|
|
|
gsize bytes_read;
|
|
|
|
|
|
|
|
if (!g_input_stream_read_all (g_io_stream_get_input_stream (G_IO_STREAM (socket_connection)),
|
|
|
|
buf,
|
|
|
|
16,
|
|
|
|
&bytes_read,
|
|
|
|
NULL, /* GCancellable */
|
|
|
|
NULL)) /* GError */
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (bytes_read != 16)
|
|
|
|
goto out;
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (memcmp (buf, server->nonce, 16) != 0)
|
2010-05-06 20:13:59 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-06-30 17:43:42 +02:00
|
|
|
connection_flags =
|
|
|
|
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER |
|
|
|
|
G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING;
|
2010-07-07 22:35:17 +02:00
|
|
|
if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)
|
2010-05-06 20:13:59 +02:00
|
|
|
connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
|
2020-12-15 14:00:28 +01:00
|
|
|
if (server->flags & G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
|
|
|
|
connection_flags |= G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
|
2010-07-07 22:35:17 +02:00
|
|
|
server->guid,
|
2010-05-06 20:13:59 +02:00
|
|
|
connection_flags,
|
2010-07-07 22:35:17 +02:00
|
|
|
server->authentication_observer,
|
2010-05-06 20:13:59 +02:00
|
|
|
NULL, /* GCancellable */
|
|
|
|
NULL); /* GError */
|
|
|
|
if (connection == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (server->flags & G_DBUS_SERVER_FLAGS_RUN_IN_THREAD)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
2010-12-22 10:11:11 +01:00
|
|
|
gboolean claimed;
|
|
|
|
|
|
|
|
claimed = FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
g_signal_emit (server,
|
|
|
|
_signals[NEW_CONNECTION_SIGNAL],
|
|
|
|
0,
|
2010-12-22 10:11:11 +01:00
|
|
|
connection,
|
|
|
|
&claimed);
|
|
|
|
if (claimed)
|
|
|
|
g_dbus_connection_start_message_processing (connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_object_unref (connection);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSource *idle_source;
|
|
|
|
EmitIdleData *data;
|
|
|
|
|
|
|
|
data = g_new0 (EmitIdleData, 1);
|
|
|
|
data->server = g_object_ref (server);
|
|
|
|
data->connection = g_object_ref (connection);
|
|
|
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
emit_new_connection_in_idle,
|
|
|
|
data,
|
|
|
|
(GDestroyNotify) emit_idle_data_free);
|
2021-07-26 11:53:02 +02:00
|
|
|
g_source_set_static_name (idle_source, "[gio] emit_new_connection_in_idle");
|
2010-07-07 22:35:17 +02:00
|
|
|
g_source_attach (idle_source, server->main_context_at_construction);
|
2010-05-06 20:13:59 +02:00
|
|
|
g_source_unref (idle_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-05-10 04:13:18 +02:00
|
|
|
initable_init (GInitable *initable,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
|
|
|
GDBusServer *server = G_DBUS_SERVER (initable);
|
|
|
|
gboolean ret;
|
|
|
|
guint n;
|
|
|
|
gchar **addr_array;
|
|
|
|
GError *last_error;
|
|
|
|
|
|
|
|
ret = FALSE;
|
2010-09-04 19:24:50 +02:00
|
|
|
addr_array = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
last_error = NULL;
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
if (!g_dbus_is_guid (server->guid))
|
2010-05-06 20:13:59 +02:00
|
|
|
{
|
|
|
|
g_set_error (&last_error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The string “%s” is not a valid D-Bus GUID"),
|
2010-07-07 22:35:17 +02:00
|
|
|
server->guid);
|
2010-05-06 20:13:59 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
server->listener = G_SOCKET_LISTENER (g_threaded_socket_service_new (-1));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
addr_array = g_strsplit (server->address, ";", 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
last_error = NULL;
|
|
|
|
for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
|
|
|
|
{
|
|
|
|
const gchar *address_entry = addr_array[n];
|
|
|
|
GHashTable *key_value_pairs;
|
|
|
|
gchar *transport_name;
|
|
|
|
GError *this_error;
|
|
|
|
|
|
|
|
this_error = NULL;
|
|
|
|
if (g_dbus_is_supported_address (address_entry,
|
|
|
|
&this_error) &&
|
|
|
|
_g_dbus_address_parse_entry (address_entry,
|
|
|
|
&transport_name,
|
|
|
|
&key_value_pairs,
|
|
|
|
&this_error))
|
|
|
|
{
|
|
|
|
|
|
|
|
if (FALSE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (g_strcmp0 (transport_name, "unix") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
ret = try_unix (server, address_entry, key_value_pairs, &this_error);
|
2010-05-06 20:13:59 +02:00
|
|
|
else if (g_strcmp0 (transport_name, "tcp") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
|
2010-05-06 20:13:59 +02:00
|
|
|
else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
ret = try_tcp (server, address_entry, key_value_pairs, TRUE, &this_error);
|
2010-05-06 20:13:59 +02:00
|
|
|
else
|
2010-05-10 04:13:18 +02:00
|
|
|
g_set_error (&this_error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("Cannot listen on unsupported transport “%s”"),
|
2010-05-10 04:13:18 +02:00
|
|
|
transport_name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
g_free (transport_name);
|
|
|
|
if (key_value_pairs != NULL)
|
|
|
|
g_hash_table_unref (key_value_pairs);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
g_assert (this_error == NULL);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this_error != NULL)
|
|
|
|
{
|
|
|
|
if (last_error != NULL)
|
|
|
|
g_error_free (last_error);
|
|
|
|
last_error = this_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2010-08-30 16:16:31 +02:00
|
|
|
|
|
|
|
g_strfreev (addr_array);
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
if (ret)
|
|
|
|
{
|
2019-10-28 21:44:07 +01:00
|
|
|
g_clear_error (&last_error);
|
2010-05-06 20:13:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_assert (last_error != NULL);
|
|
|
|
g_propagate_error (error, last_error);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
initable_iface_init (GInitableIface *initable_iface)
|
|
|
|
{
|
|
|
|
initable_iface->init = initable_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|