~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to daemon/dbus/gkd-dbus-secrets.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/* gkd-dbus-secrets.c - dbus secret service
 
3
 
 
4
   Copyright (C) 2009, Stefan Walter
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
20
 
 
21
   Author: Stef Walter <stef@memberwebs.com>
 
22
*/
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include "gkd-dbus.h"
 
27
#include "gkd-dbus-private.h"
 
28
#include "gkd-secret-service.h"
 
29
 
 
30
#include "daemon/pkcs11/gkd-pkcs11.h"
 
31
 
 
32
#include "egg/egg-cleanup.h"
 
33
 
 
34
#include "gp11/gp11.h"
 
35
 
 
36
static DBusConnection *dbus_conn = NULL;
 
37
static GkdSecretService *secrets_service = NULL;
 
38
 
 
39
static GP11Slot*
 
40
calculate_secrets_slot (void)
 
41
{
 
42
        GP11Slot *slot = NULL;
 
43
        GP11Module *module;
 
44
        GList *slots, *l;
 
45
        GP11SlotInfo *info;
 
46
 
 
47
        module = gp11_module_new (gkd_pkcs11_get_functions ());
 
48
        g_return_val_if_fail (module, NULL);
 
49
 
 
50
        /*
 
51
         * Find the right slot.
 
52
         *
 
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.
 
56
         */
 
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);
 
63
        }
 
64
        gp11_list_unref_free (slots);
 
65
 
 
66
        return slot;
 
67
}
 
68
 
 
69
gboolean
 
70
gkd_dbus_secrets_startup (void)
 
71
{
 
72
        DBusError error = DBUS_ERROR_INIT;
 
73
        dbus_uint32_t result = 0;
 
74
        const gchar *service = NULL;
 
75
        unsigned int flags = 0;
 
76
        GP11Slot *slot;
 
77
 
 
78
        g_return_val_if_fail (dbus_conn, FALSE);
 
79
 
 
80
#ifdef WITH_TESTS
 
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;
 
84
        else
 
85
#endif
 
86
                service = SECRET_SERVICE;
 
87
 
 
88
        /* Figure out which slot to use */
 
89
        slot = calculate_secrets_slot ();
 
90
        g_return_val_if_fail (slot, FALSE);
 
91
 
 
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);
 
98
 
 
99
        } else {
 
100
                switch (result) {
 
101
 
 
102
                /* We acquired the service name */
 
103
                case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
 
104
                        break;
 
105
 
 
106
                /* We already acquired the service name. */
 
107
                case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
 
108
                        break;
 
109
 
 
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");
 
114
                        break;
 
115
 
 
116
                default:
 
117
                        g_return_val_if_reached (FALSE);
 
118
                        break;
 
119
                };
 
120
        }
 
121
 
 
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);
 
125
 
 
126
        g_object_unref (slot);
 
127
        return TRUE;
 
128
}
 
129
 
 
130
static void
 
131
cleanup_dbus_conn (gpointer unused)
 
132
{
 
133
        g_assert (dbus_conn);
 
134
        dbus_connection_unref (dbus_conn);
 
135
        dbus_conn = NULL;
 
136
}
 
137
 
 
138
void
 
139
gkd_dbus_secrets_init (DBusConnection *conn)
 
140
{
 
141
        dbus_conn = dbus_connection_ref (conn);
 
142
        egg_cleanup_register (cleanup_dbus_conn, NULL);
 
143
}
 
144
 
 
145
void
 
146
gkd_dbus_secrets_cleanup (DBusConnection *conn)
 
147
{
 
148
        if (secrets_service) {
 
149
                g_object_run_dispose (G_OBJECT (secrets_service));
 
150
                g_object_unref (secrets_service);
 
151
                secrets_service = NULL;
 
152
        }
 
153
}