1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/* gkd-dbus-secrets.c - dbus secret service
4
Copyright (C) 2009, Stefan Walter
6
The Gnome Keyring Library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public License as
8
published by the Free Software Foundation; either version 2 of the
9
License, or (at your option) any later version.
11
The Gnome Keyring Library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public
17
License along with the Gnome Library; see the file COPYING.LIB. If not,
18
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA.
21
Author: Stef Walter <stef@memberwebs.com>
27
#include "gkd-dbus-private.h"
28
#include "gkd-secret-service.h"
30
#include "daemon/pkcs11/gkd-pkcs11.h"
32
#include "egg/egg-cleanup.h"
34
#include "gp11/gp11.h"
36
static DBusConnection *dbus_conn = NULL;
37
static GkdSecretService *secrets_service = NULL;
40
calculate_secrets_slot (void)
42
GP11Slot *slot = NULL;
47
module = gp11_module_new (gkd_pkcs11_get_functions ());
48
g_return_val_if_fail (module, NULL);
51
* Find the right slot.
53
* TODO: This isn't necessarily the best way to do this.
54
* A good function could be added to gp11 library.
55
* But needs more thought on how to do this.
57
slots = gp11_module_get_slots (module, TRUE);
58
for (l = slots; !slot && l; l = g_list_next (l)) {
59
info = gp11_slot_get_info (l->data);
60
if (g_ascii_strcasecmp ("Secret Store", info->slot_description) == 0)
61
slot = g_object_ref (l->data);
62
gp11_slot_info_free (info);
64
gp11_list_unref_free (slots);
70
gkd_dbus_secrets_startup (void)
72
DBusError error = DBUS_ERROR_INIT;
73
dbus_uint32_t result = 0;
74
const gchar *service = NULL;
75
unsigned int flags = 0;
78
g_return_val_if_fail (dbus_conn, FALSE);
81
service = g_getenv ("GNOME_KEYRING_TEST_SERVICE");
82
if (service && service[0])
83
flags = DBUS_NAME_FLAG_ALLOW_REPLACEMENT | DBUS_NAME_FLAG_REPLACE_EXISTING;
86
service = SECRET_SERVICE;
88
/* Figure out which slot to use */
89
slot = calculate_secrets_slot ();
90
g_return_val_if_fail (slot, FALSE);
92
/* Try and grab our name */
93
result = dbus_bus_request_name (dbus_conn, service, flags, &error);
94
if (dbus_error_is_set (&error)) {
95
g_message ("couldn't request name '%s' on session bus: %s",
96
service, error.message);
97
dbus_error_free (&error);
102
/* We acquired the service name */
103
case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
106
/* We already acquired the service name. */
107
case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
110
/* Another daemon is running */
111
case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
112
case DBUS_REQUEST_NAME_REPLY_EXISTS:
113
g_message ("another secret service is running");
117
g_return_val_if_reached (FALSE);
122
g_return_val_if_fail (!secrets_service, FALSE);
123
secrets_service = g_object_new (GKD_SECRET_TYPE_SERVICE,
124
"connection", dbus_conn, "pkcs11-slot", slot, NULL);
126
g_object_unref (slot);
131
cleanup_dbus_conn (gpointer unused)
133
g_assert (dbus_conn);
134
dbus_connection_unref (dbus_conn);
139
gkd_dbus_secrets_init (DBusConnection *conn)
141
dbus_conn = dbus_connection_ref (conn);
142
egg_cleanup_register (cleanup_dbus_conn, NULL);
146
gkd_dbus_secrets_cleanup (DBusConnection *conn)
148
if (secrets_service) {
149
g_object_run_dispose (G_OBJECT (secrets_service));
150
g_object_unref (secrets_service);
151
secrets_service = NULL;