summaryrefslogtreecommitdiff
path: root/daemon/dbus/gkd-dbus.c
blob: d927a8ad8a8d686337a92da4df16e7a42170a189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gkd-dbus.c - hook into dbus, call other bits

   Copyright (C) 2007, 2009, Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "gkd-daemon-generated.h"
#include "gkd-dbus.h"
#include "gkd-dbus-private.h"

#include "daemon/gkd-main.h"
#include "daemon/gkd-util.h"

#include "egg/egg-cleanup.h"

#include <glib.h>
#include <gio/gio.h>

static GDBusConnection *dbus_conn = NULL;
static gboolean object_registered = FALSE;
static gboolean acquired_asked = FALSE;
static gboolean acquired_service = FALSE;

#define GNOME_KEYRING_DAEMON_SERVICE    "org.gnome.keyring"
#define GNOME_KEYRING_DAEMON_PATH       "/org/gnome/keyring/daemon"
#define GNOME_KEYRING_DAEMON_INTERFACE  "org.gnome.keyring.Daemon"

static void
cleanup_session_bus (gpointer unused)
{
	if (!dbus_conn)
		return;

	g_clear_object (&dbus_conn);
}

static void
on_connection_close (gpointer user_data)
{
	g_debug ("dbus connection closed, exiting");
	gkd_main_quit ();
}

static gboolean
connect_to_session_bus (void)
{
	GError *error = NULL;

	if (dbus_conn)
		return TRUE;

	dbus_conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
	if (!dbus_conn) {
		g_message ("couldn't connect to dbus session bus: %s", error->message);
		g_error_free (error);
		return FALSE;
	}

	g_signal_connect (dbus_conn, "closed",
			  G_CALLBACK (on_connection_close), NULL);
	egg_cleanup_register (cleanup_session_bus, NULL);
	return TRUE;
}

static gboolean
handle_get_environment (GkdExportedDaemon *skeleton,
			GDBusMethodInvocation *invocation,
			gpointer user_data)
{
	const gchar **env;
	gchar **parts;
	GVariantBuilder builder;

	env = gkd_util_get_environment ();
	g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));

	while (*env) {
		parts = g_strsplit (*env, "=", 2);
		g_variant_builder_add (&builder, "{ss}", parts[0], parts[1]);
		g_strfreev (parts);
	}

	gkd_exported_daemon_complete_get_environment (skeleton, invocation,
						      g_variant_builder_end (&builder));
	return TRUE;
}

static gboolean
handle_get_control_directory (GkdExportedDaemon *skeleton,
			      GDBusMethodInvocation *invocation,
			      gpointer user_data)
{
	gkd_exported_daemon_complete_get_control_directory (skeleton, invocation,
							    gkd_util_get_master_directory ());
	return TRUE;
}

static void
cleanup_singleton (gpointer user_data)
{
	GkdExportedDaemon *skeleton = user_data;

	g_return_if_fail (dbus_conn);
	if (object_registered) {
		g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (skeleton), dbus_conn);
		g_object_unref (skeleton);
	}
	object_registered = FALSE;
}

gboolean
gkd_dbus_singleton_acquire (gboolean *acquired)
{
	const gchar *service = NULL;
	GBusNameOwnerFlags flags = G_BUS_NAME_OWNER_FLAGS_NONE;
	GVariant *acquire_variant;
	guint res;
	GError *error = NULL;
	GkdExportedDaemon *skeleton;

	g_assert (acquired);

	if (!connect_to_session_bus ())
		return FALSE;

	/* First register the object */
	if (!object_registered) {
		skeleton = gkd_exported_daemon_skeleton_new ();

		g_signal_connect (skeleton, "handle-get-control-directory",
				  G_CALLBACK (handle_get_control_directory), NULL);
		g_signal_connect (skeleton, "handle-get-environment",
				  G_CALLBACK (handle_get_environment), NULL);

		g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton), dbus_conn,
						  GNOME_KEYRING_DAEMON_PATH, &error);

		if (error == NULL) {
			object_registered = TRUE;
			egg_cleanup_register (cleanup_singleton, skeleton);
		} else {
			g_message ("couldn't register dbus object path: %s", error->message);
			g_clear_error (&error);
		}
	}

	/* Try and grab our name */
	if (!acquired_asked) {
#ifdef WITH_DEBUG
		service = g_getenv ("GNOME_KEYRING_TEST_SERVICE");
		if (service && service[0])
			flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE;
		else
#endif
			service = GNOME_KEYRING_DAEMON_SERVICE;

		/* attempt to acquire the name */
		acquire_variant = g_dbus_connection_call_sync (dbus_conn,
							       "org.freedesktop.DBus",  /* bus name */
							       "/org/freedesktop/DBus", /* object path */
							       "org.freedesktop.DBus",  /* interface name */
							       "RequestName",           /* method name */
							       g_variant_new ("(su)",
									      service,
									      flags),
							       G_VARIANT_TYPE ("(u)"),
							       G_DBUS_CALL_FLAGS_NONE,
							       -1, NULL, &error);

		if (error != NULL) {
			g_message ("couldn't request name '%s' on session bus: %s", service, error->message);
			g_error_free (error);
			return FALSE;
		}

		acquired_asked = TRUE;
		g_variant_get (acquire_variant, "(u)", &res);
		g_variant_unref (acquire_variant);

		switch (res) {
               /* We acquired the service name */
		case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
		case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
                       acquired_service = TRUE;
                       break;
               /* Another daemon is running */
		case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
		case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
                       acquired_service = FALSE;
                       break;
               default:
                       acquired_service = FALSE;
                       g_return_val_if_reached (FALSE);
                       break;
               };
	}

	*acquired = acquired_service;

	return TRUE;
}

gchar*
gkd_dbus_singleton_control (void)
{
	gchar *control = NULL;
	GError *error = NULL;
	GVariant *control_variant;

	/* If tried to aquire the service must have failed */
	g_return_val_if_fail (!acquired_service, NULL);

	if (!connect_to_session_bus ())
		return NULL;

	control_variant = g_dbus_connection_call_sync (dbus_conn,
						       GNOME_KEYRING_DAEMON_SERVICE,
						       GNOME_KEYRING_DAEMON_PATH,
						       GNOME_KEYRING_DAEMON_INTERFACE,
						       "GetControlDirectory",
						       NULL, NULL,
						       G_DBUS_CALL_FLAGS_NO_AUTO_START,
						       1000, NULL, &error);

	if (error != NULL) {
		if (!g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER))
			g_message ("couldn't communicate with already running daemon: %s", error->message);
		g_error_free (error);
		return NULL;
	}

	g_variant_get (control_variant, "(s)", &control);
	g_variant_unref (control_variant);

	return control;
}

static void
dbus_cleanup (gpointer unused)
{
	g_return_if_fail (dbus_conn);
	gkd_dbus_secrets_cleanup (dbus_conn);
	gkd_dbus_session_cleanup (dbus_conn);
	gkd_dbus_environment_cleanup (dbus_conn);
}

gboolean
gkd_dbus_setup (void)
{
	gboolean unused;

	if (!connect_to_session_bus ())
		return FALSE;

	/* Our singleton, and internal service API */
	gkd_dbus_singleton_acquire (&unused);

	/* Session stuff */
	gkd_dbus_environment_init (dbus_conn);
	gkd_dbus_session_init (dbus_conn);

	/* Secrets API */
	gkd_dbus_secrets_init (dbus_conn);

	egg_cleanup_register (dbus_cleanup, NULL);
	return TRUE;
}