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

« back to all changes in this revision

Viewing changes to daemon/keyrings/gkr-keyring-login.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
 
/* gkr-keyrings-login.c - get secrets to automatically unlock keyrings or keys
3
 
 
4
 
   Copyright (C) 2007 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 "gkr-keyring-login.h"
27
 
 
28
 
#include "gkr-keyring.h"
29
 
#include "gkr-keyring-item.h"
30
 
#include "gkr-keyrings.h"
31
 
 
32
 
#include "egg/egg-secure-memory.h"
33
 
 
34
 
#include "library/gnome-keyring.h"
35
 
 
36
 
#include "ui/gkr-ask-daemon.h"
37
 
#include "ui/gkr-ask-request.h"
38
 
 
39
 
#include "util/gkr-location.h"
40
 
 
41
 
#include <glib.h>
42
 
#include <glib/gi18n.h>
43
 
 
44
 
#include <stdarg.h>
45
 
#include <unistd.h>
46
 
 
47
 
gboolean
48
 
gkr_keyring_login_is_unlocked (void)
49
 
{
50
 
        GkrKeyring *login = gkr_keyrings_get_login ();
51
 
        return (login && !login->locked);
52
 
}
53
 
 
54
 
gboolean
55
 
gkr_keyring_login_is_usable (void)
56
 
{
57
 
        /*
58
 
         * We only flag this as usable by our internals if the keyring will
59
 
         * be encrypted when on disk. 
60
 
         */
61
 
        GkrKeyring *login = gkr_keyrings_get_login ();
62
 
        return (login && !login->locked && !gkr_keyring_is_insecure (login));   
63
 
}
64
 
 
65
 
static gboolean 
66
 
check_ask_request (GkrAskRequest* ask)
67
 
{
68
 
        GkrKeyring *keyring;
69
 
        
70
 
        keyring = GKR_KEYRING (gkr_ask_request_get_object (ask));
71
 
        g_assert (GKR_IS_KEYRING (keyring));
72
 
 
73
 
        if (!keyring->locked) {
74
 
                ask->response = GKR_ASK_RESPONSE_ALLOW;
75
 
                return GKR_ASK_STOP_REQUEST;
76
 
        }
77
 
        
78
 
        /* If they typed a password, try it out */
79
 
        if (ask->response >= GKR_ASK_RESPONSE_ALLOW) {
80
 
                
81
 
                g_assert (ask->typed_password);
82
 
                if (!gkr_keyring_unlock (keyring, ask->typed_password)) {
83
 
                        /* Bad password, try again */
84
 
                        ask->response = GKR_ASK_RESPONSE_NONE;
85
 
                        return GKR_ASK_CONTINUE_REQUEST;
86
 
                }
87
 
        }
88
 
        
89
 
        return GKR_ASK_DONT_CARE;
90
 
}
91
 
 
92
 
static gboolean
93
 
request_login_access (GkrKeyring* keyring)
94
 
{
95
 
        GkrAskRequest *ask;
96
 
        gboolean ret;
97
 
        
98
 
        /* And put together the ask request */
99
 
        ask = gkr_ask_request_new (_("Unlock Login Keyring"), _("Enter login password to unlock keyring"),
100
 
                                   GKR_ASK_REQUEST_PROMPT_PASSWORD);
101
 
        gkr_ask_request_set_secondary (ask, _("Your login keyring was not automatically unlocked when you logged into this computer."));
102
 
        gkr_ask_request_set_object (ask, G_OBJECT (keyring));
103
 
        
104
 
        /* Intercept item access requests to see if we still need to prompt */
105
 
        g_signal_connect (ask, "check-request", G_CALLBACK (check_ask_request), NULL);
106
 
        
107
 
        /* And do the prompt */
108
 
        gkr_ask_daemon_process (ask);
109
 
        ret = ask->response >= GKR_ASK_RESPONSE_ALLOW;
110
 
        g_object_unref (ask);
111
 
        
112
 
        return ret;
113
 
}
114
 
 
115
 
static gboolean
116
 
request_login_new (gchar **password)
117
 
{
118
 
        GkrAskRequest* ask;
119
 
        gboolean ret;
120
 
        
121
 
        g_assert (password);
122
 
        g_assert (!*password);
123
 
 
124
 
        /* And put together the ask request */
125
 
        ask = gkr_ask_request_new (_("Create Login Keyring"), _("Enter your login password"),
126
 
                                   GKR_ASK_REQUEST_NEW_PASSWORD);
127
 
        gkr_ask_request_set_secondary (ask, _("Your login keyring was not automatically created when you logged "
128
 
                                              "into this computer. It will now be created."));
129
 
                               
130
 
        /* And do the prompt */
131
 
        gkr_ask_daemon_process (ask);
132
 
        ret = ask->response >= GKR_ASK_RESPONSE_ALLOW;
133
 
        if (ret)
134
 
                *password = egg_secure_strdup (ask->typed_password);
135
 
        g_object_unref (ask);
136
 
        return ret;
137
 
}
138
 
 
139
 
gboolean
140
 
gkr_keyring_login_unlock (const gchar *password)
141
 
{
142
 
        GkrKeyring *login = gkr_keyrings_get_login ();
143
 
        gchar *new_password = NULL;
144
 
        
145
 
        /* Make sure its loaded */
146
 
        if (!login) {
147
 
                gkr_keyrings_update ();
148
 
                login = gkr_keyrings_get_login ();
149
 
        }
150
 
        
151
 
        if (login && !login->locked)
152
 
                return TRUE;
153
 
 
154
 
        /* Try to unlock the keyring that exists */
155
 
        if (login) {
156
 
                if (!password)
157
 
                        return request_login_access (login);
158
 
                        
159
 
                if (!gkr_keyring_unlock (login, password)) {
160
 
                        g_message ("Couldn't unlock login keyring with provided password");
161
 
                        return FALSE;
162
 
                }
163
 
                
164
 
                return TRUE; 
165
 
        } 
166
 
        
167
 
        /* No such keyring exists, and we don't have a password. */
168
 
        if (!password) {
169
 
                if (!request_login_new (&new_password))
170
 
                        return FALSE;
171
 
                g_return_val_if_fail (new_password, FALSE);
172
 
                password = new_password;
173
 
        }
174
 
        
175
 
        /* No such keyring exists, so create one */
176
 
        login = gkr_keyring_create (GKR_LOCATION_VOLUME_LOCAL, "login", password);
177
 
        egg_secure_strfree (new_password);
178
 
        
179
 
        if (!login) {
180
 
                g_warning ("Failed to create login keyring");
181
 
                return FALSE;
182
 
        }
183
 
 
184
 
        g_assert (!login->locked);
185
 
        
186
 
        gkr_keyrings_add (login);
187
 
        g_return_val_if_fail (gkr_keyrings_get_login () == login, FALSE);
188
 
        
189
 
        g_object_unref (login);
190
 
        return TRUE;
191
 
}
192
 
 
193
 
void
194
 
gkr_keyring_login_lock (void)
195
 
{
196
 
        GkrKeyring *login = gkr_keyrings_get_login ();
197
 
        if (login)
198
 
                gkr_keyring_lock (login);
199
 
}
200
 
 
201
 
static GnomeKeyringAttributeList*
202
 
string_attribute_list_va (va_list args)
203
 
{
204
 
        GnomeKeyringAttributeList *attributes;
205
 
        GnomeKeyringAttribute attribute;
206
 
        
207
 
        attributes = g_array_new (FALSE, FALSE, sizeof (GnomeKeyringAttribute));
208
 
        
209
 
        while ((attribute.name = va_arg (args, char *)) != NULL) {
210
 
                attribute.name = g_strdup (attribute.name);
211
 
                attribute.value.string = g_strdup (va_arg (args, char *));
212
 
                attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
213
 
                g_array_append_val (attributes, attribute);
214
 
        }
215
 
        
216
 
        return attributes;
217
 
}
218
 
 
219
 
const gchar*
220
 
gkr_keyring_login_master (void)
221
 
{
222
 
        GkrKeyring *login;
223
 
        
224
 
        login = gkr_keyrings_get_login ();
225
 
        if (!login || login->locked)
226
 
                return NULL;
227
 
        
228
 
        if (gkr_keyring_is_insecure (login))
229
 
                return NULL;
230
 
        
231
 
        return login->password;
232
 
}
233
 
 
234
 
void
235
 
gkr_keyring_login_attach_secret (GnomeKeyringItemType type, const gchar *display_name, 
236
 
                                 const gchar *secret, ...)
237
 
{
238
 
        GkrKeyring *login;
239
 
        GnomeKeyringAttributeList *attrs;
240
 
        GkrKeyringItem *item;
241
 
        va_list args;
242
 
        
243
 
        login = gkr_keyrings_get_login ();
244
 
        if (!login || login->locked)
245
 
                return;
246
 
                
247
 
        va_start (args, secret);
248
 
        attrs = string_attribute_list_va (args);
249
 
        va_end (args);
250
 
        
251
 
        item = gkr_keyring_find_item (login, type, attrs, TRUE);
252
 
        
253
 
        if (!item) {
254
 
                item = gkr_keyring_item_create (login, type);
255
 
                gkr_keyring_add_item (login, item);
256
 
                g_object_unref (item);
257
 
        }
258
 
        
259
 
        g_free (item->display_name);
260
 
        item->display_name = g_strdup (display_name); 
261
 
        
262
 
        egg_secure_strfree (item->secret);
263
 
        item->secret = egg_secure_strdup (secret);
264
 
        
265
 
        gnome_keyring_attribute_list_free (item->attributes);
266
 
        item->attributes = attrs;
267
 
        
268
 
        gkr_keyring_save_to_disk (login);
269
 
}
270
 
 
271
 
const gchar*
272
 
gkr_keyring_login_lookup_secret (GnomeKeyringItemType type, ...)
273
 
{
274
 
        GkrKeyring *login;
275
 
        GkrKeyringItem *item;
276
 
        GnomeKeyringAttributeList *attrs;
277
 
        va_list args;
278
 
        
279
 
        login = gkr_keyrings_get_login ();
280
 
        if (!login || login->locked)
281
 
                return NULL;
282
 
 
283
 
        if (!login->location)
284
 
                return NULL;
285
 
 
286
 
        va_start (args, type);
287
 
        attrs = string_attribute_list_va (args);
288
 
        va_end (args);
289
 
                                
290
 
        item = gkr_keyring_find_item (login, type, attrs, TRUE);
291
 
        gnome_keyring_attribute_list_free (attrs);
292
 
        
293
 
        if (item)
294
 
                return item->secret;
295
 
                
296
 
        return NULL;
297
 
}
298
 
                                                 
299
 
void
300
 
gkr_keyring_login_remove_secret (GnomeKeyringItemType type, ...)
301
 
{
302
 
        GkrKeyring *login;
303
 
        GkrKeyringItem *item;
304
 
        GnomeKeyringAttributeList *attrs;
305
 
        va_list args;
306
 
        
307
 
        login = gkr_keyrings_get_login ();
308
 
        if (!login || login->locked)
309
 
                return;
310
 
                
311
 
        if (!login->location)
312
 
                return;
313
 
 
314
 
        va_start (args, type);
315
 
        attrs = string_attribute_list_va (args);
316
 
        va_end (args);
317
 
                
318
 
        item = gkr_keyring_find_item (login, type, attrs, TRUE);
319
 
        gnome_keyring_attribute_list_free (attrs);
320
 
        
321
 
        if (item) {
322
 
                gkr_keyring_remove_item (login, item);
323
 
                gkr_keyring_save_to_disk (login);
324
 
        }
325
 
}