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
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2008 Carlos Garcia Campos
*
* 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
* version 2 of the License, or (at your option) any later version.
*
* 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
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Carlos Garcia Campos <carlosgc@gnome.org>
*/
#include <config.h>
#define SECRET_API_SUBJECT_TO_CHANGE 1
#ifdef HAVE_KEYRING
#include <libsecret/secret.h>
#endif
#include "gvfskeyring.h"
gboolean
g_vfs_keyring_is_available (void)
{
#ifdef HAVE_KEYRING
return TRUE;
#else
return FALSE;
#endif
}
#ifdef HAVE_KEYRING
static void
insert_string (const gchar *key,
const gchar *value,
GHashTable **attributes)
{
if (*attributes == NULL)
return;
if (!g_utf8_validate (value, -1, NULL))
{
g_warning ("Non-utf8 value for key %s\n", key);
g_hash_table_unref (*attributes);
*attributes = NULL;
}
g_hash_table_insert (*attributes,
g_strdup (key),
g_strdup (value));
}
static void
insert_int (const gchar *key,
gint value,
GHashTable **attributes)
{
if (*attributes == NULL)
return;
g_hash_table_insert (*attributes,
g_strdup (key),
g_strdup_printf ("%d", value));
}
static GHashTable *
build_network_attributes (const gchar *username,
const gchar *host,
const gchar *domain,
const gchar *protocol,
const gchar *object,
const gchar *authtype,
guint32 port)
{
GHashTable *attributes;
attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
if (username)
insert_string ("user", username, &attributes);
if (host)
insert_string ("server", host, &attributes);
if (domain)
insert_string ("domain", domain, &attributes);
if (protocol)
insert_string ("protocol", protocol, &attributes);
if (object)
insert_string ("object", object, &attributes);
if (authtype)
insert_string ("authtype", authtype, &attributes);
if (port != 0)
insert_int ("port", (gint)port, &attributes);
return attributes;
}
static gchar *
build_network_label (const gchar *user,
const gchar *server,
const gchar *object,
guint32 port)
{
GString *s;
gchar *name;
if (server != NULL)
{
s = g_string_new (NULL);
if (user != NULL)
{
g_string_append_uri_escaped (s, user, G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO, TRUE);
g_string_append (s, "@");
}
g_string_append (s, server);
if (port != 0)
g_string_append_printf (s, ":%d", port);
if (object != NULL)
g_string_append_printf (s, "/%s", object);
name = g_string_free (s, FALSE);
}
else
{
name = g_strdup ("network password");
}
return name;
}
static gint
compare_specificity (gconstpointer a,
gconstpointer b)
{
GHashTable *attributes_a, *attributes_b;
SecretItem *item_a, *item_b;
int res;
item_a = SECRET_ITEM (a);
attributes_a = secret_item_get_attributes (item_a);
item_b = SECRET_ITEM (b);
attributes_b = secret_item_get_attributes (item_b);
res = g_hash_table_size (attributes_a) - g_hash_table_size (attributes_b);
/* Prefer the most recent item if they are equal in specificity. */
if (res == 0)
res = secret_item_get_modified (item_b) - secret_item_get_modified (item_a);
g_hash_table_unref (attributes_a);
g_hash_table_unref (attributes_b);
return res;
}
#endif /* HAVE_KEYRING */
gboolean
g_vfs_keyring_lookup_password (const gchar *username,
const gchar *host,
const gchar *domain,
const gchar *protocol,
const gchar *object,
const gchar *authtype,
guint32 port,
gchar **username_out,
gchar **domain_out,
gchar **password_out)
{
#ifdef HAVE_KEYRING
GHashTable *attributes;
SecretItem *item;
SecretValue *secret;
GList *plist;
GError *error = NULL;
attributes = build_network_attributes (username, host, domain, protocol, object, authtype, port);
plist = secret_service_search_sync (NULL, SECRET_SCHEMA_COMPAT_NETWORK, attributes,
SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS |
SECRET_SEARCH_ALL,
NULL, &error);
g_hash_table_unref (attributes);
if (error != NULL)
{
g_error_free (error);
return FALSE;
}
if (plist == NULL)
return FALSE;
/* We want the least specific result, so we sort the return values.
For instance, given both items for ftp://host:port and ftp://host
in the keyring we always want to use the ftp://host one for
i.e. ftp://host/some/path. */
plist = g_list_sort (plist, compare_specificity);
item = SECRET_ITEM (plist->data);
secret = secret_item_get_secret (item);
attributes = secret_item_get_attributes (item);
g_list_free_full (plist, g_object_unref);
if (secret == NULL)
{
if (attributes)
g_hash_table_unref (attributes);
return FALSE;
}
*password_out = g_strdup (secret_value_get (secret, NULL));
secret_value_unref (secret);
if (username_out)
*username_out = g_strdup (g_hash_table_lookup (attributes, "user"));
if (domain_out)
*domain_out = g_strdup (g_hash_table_lookup (attributes, "domain"));
g_hash_table_unref (attributes);
return TRUE;
#else
return FALSE;
#endif /* HAVE_KEYRING */
}
gboolean
g_vfs_keyring_save_password (const gchar *username,
const gchar *host,
const gchar *domain,
const gchar *protocol,
const gchar *object,
const gchar *authtype,
guint32 port,
const gchar *password,
GPasswordSave flags)
{
#ifdef HAVE_KEYRING
const gchar *keyring;
GHashTable *attributes;
gchar *label;
gboolean ret;
if (flags == G_PASSWORD_SAVE_NEVER)
return FALSE;
keyring = (flags == G_PASSWORD_SAVE_FOR_SESSION) ? SECRET_COLLECTION_SESSION : SECRET_COLLECTION_DEFAULT;
label = build_network_label (username, host, object, port);
attributes = build_network_attributes (username, host, domain, protocol, object, authtype, port);
ret = secret_password_storev_sync (SECRET_SCHEMA_COMPAT_NETWORK, attributes,
keyring, label, password, NULL, NULL);
g_free (label);
g_hash_table_unref (attributes);
return ret;
#else
return FALSE;
#endif /* HAVE_KEYRING */
}
|