| 1 |
/* |
| 2 |
* gnome-keyring |
| 3 |
* |
| 4 |
* Copyright (C) 2009 Stefan Walter |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "gkm-secret-data.h" |
| 24 |
|
| 25 |
#include "gkm/gkm-secret.h" |
| 26 |
#include "gkm/gkm-transaction.h" |
| 27 |
#include "gkm/gkm-util.h" |
| 28 |
|
| 29 |
#include "egg/egg-secure-memory.h" |
| 30 |
|
| 31 |
#include <glib/gi18n.h> |
| 32 |
|
| 33 |
struct _GkmSecretData { |
| 34 |
GObject parent; |
| 35 |
GHashTable *secrets; |
| 36 |
GkmSecret *master; |
| 37 |
}; |
| 38 |
|
| 39 |
G_DEFINE_TYPE (GkmSecretData, gkm_secret_data, G_TYPE_OBJECT); |
| 40 |
|
| 41 |
/* ----------------------------------------------------------------------------- |
| 42 |
* INTERNAL |
| 43 |
*/ |
| 44 |
|
| 45 |
typedef struct _set_secret_args { |
| 46 |
gchar *identifier; |
| 47 |
GkmSecret *old_secret; |
| 48 |
} set_secret_args; |
| 49 |
|
| 50 |
static gboolean |
| 51 |
complete_set_secret (GkmTransaction *transaction, GObject *obj, gpointer user_data) |
| 52 |
{ |
| 53 |
GkmSecretData *self = GKM_SECRET_DATA (obj); |
| 54 |
set_secret_args *args = user_data; |
| 55 |
|
| 56 |
/* If the transaction failed, revert */ |
| 57 |
if (gkm_transaction_get_failed (transaction)) { |
| 58 |
if (!args->old_secret) { |
| 59 |
g_hash_table_remove (self->secrets, args->identifier); |
| 60 |
} else { |
| 61 |
g_hash_table_replace (self->secrets, args->identifier, args->old_secret); |
| 62 |
args->identifier = NULL; /* hash table took ownership */ |
| 63 |
args->old_secret = NULL; /* ditto */ |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/* Free up transaction data */ |
| 68 |
g_free (args->identifier); |
| 69 |
if (args->old_secret) |
| 70 |
g_object_unref (args->old_secret); |
| 71 |
g_slice_free (set_secret_args, args); |
| 72 |
|
| 73 |
return TRUE; |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
begin_set_secret (GkmSecretData *self, GkmTransaction *transaction, |
| 78 |
const gchar *identifier, GkmSecret *secret) |
| 79 |
{ |
| 80 |
set_secret_args *args; |
| 81 |
|
| 82 |
g_assert (GKM_IS_SECRET_DATA (self)); |
| 83 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 84 |
g_assert (identifier); |
| 85 |
g_assert (GKM_IS_SECRET (secret)); |
| 86 |
|
| 87 |
args = g_slice_new0 (set_secret_args); |
| 88 |
|
| 89 |
/* Take ownership of the old data, if present */ |
| 90 |
if (g_hash_table_lookup_extended (self->secrets, identifier, |
| 91 |
(gpointer*)&args->identifier, |
| 92 |
(gpointer*)&args->old_secret)) { |
| 93 |
if (!g_hash_table_steal (self->secrets, args->identifier)) |
| 94 |
g_assert_not_reached (); |
| 95 |
} else { |
| 96 |
args->identifier = g_strdup (identifier); |
| 97 |
} |
| 98 |
|
| 99 |
/* Replace with our new data */ |
| 100 |
g_hash_table_replace (self->secrets, g_strdup (identifier), |
| 101 |
g_object_ref (secret)); |
| 102 |
|
| 103 |
/* Track in the transaction */ |
| 104 |
gkm_transaction_add (transaction, self, complete_set_secret, args); |
| 105 |
} |
| 106 |
|
| 107 |
/* ----------------------------------------------------------------------------- |
| 108 |
* OBJECT |
| 109 |
*/ |
| 110 |
|
| 111 |
static void |
| 112 |
gkm_secret_data_init (GkmSecretData *self) |
| 113 |
{ |
| 114 |
self->secrets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
gkm_secret_data_finalize (GObject *obj) |
| 119 |
{ |
| 120 |
GkmSecretData *self = GKM_SECRET_DATA (obj); |
| 121 |
|
| 122 |
if (self->secrets) |
| 123 |
g_hash_table_destroy (self->secrets); |
| 124 |
self->secrets = NULL; |
| 125 |
|
| 126 |
if (self->master) |
| 127 |
g_object_unref (self->master); |
| 128 |
self->master = NULL; |
| 129 |
|
| 130 |
G_OBJECT_CLASS (gkm_secret_data_parent_class)->finalize (obj); |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
gkm_secret_data_class_init (GkmSecretDataClass *klass) |
| 135 |
{ |
| 136 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 137 |
gkm_secret_data_parent_class = g_type_class_peek_parent (klass); |
| 138 |
gobject_class->finalize = gkm_secret_data_finalize; |
| 139 |
} |
| 140 |
|
| 141 |
/* ----------------------------------------------------------------------------- |
| 142 |
* PUBLIC |
| 143 |
*/ |
| 144 |
|
| 145 |
GkmSecret* |
| 146 |
gkm_secret_data_get_secret (GkmSecretData *self, const gchar *identifier) |
| 147 |
{ |
| 148 |
g_return_val_if_fail (GKM_IS_SECRET_DATA (self), NULL); |
| 149 |
g_return_val_if_fail (identifier, NULL); |
| 150 |
return g_hash_table_lookup (self->secrets, identifier); |
| 151 |
} |
| 152 |
|
| 153 |
const guchar* |
| 154 |
gkm_secret_data_get_raw (GkmSecretData *self, const gchar *identifier, |
| 155 |
gsize *n_result) |
| 156 |
{ |
| 157 |
GkmSecret *secret; |
| 158 |
|
| 159 |
g_return_val_if_fail (GKM_IS_SECRET_DATA (self), NULL); |
| 160 |
g_return_val_if_fail (identifier, NULL); |
| 161 |
g_return_val_if_fail (n_result, NULL); |
| 162 |
|
| 163 |
secret = gkm_secret_data_get_secret (self, identifier); |
| 164 |
if (secret == NULL) |
| 165 |
return NULL; |
| 166 |
|
| 167 |
return gkm_secret_get (secret, n_result); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
void |
| 172 |
gkm_secret_data_set_secret (GkmSecretData *self, const gchar *identifier, |
| 173 |
GkmSecret *secret) |
| 174 |
{ |
| 175 |
g_return_if_fail (GKM_IS_SECRET_DATA (self)); |
| 176 |
g_return_if_fail (identifier); |
| 177 |
g_return_if_fail (GKM_IS_SECRET (secret)); |
| 178 |
g_hash_table_replace (self->secrets, g_strdup (identifier), |
| 179 |
g_object_ref (secret)); |
| 180 |
} |
| 181 |
|
| 182 |
void |
| 183 |
gkm_secret_data_set_transacted (GkmSecretData *self, GkmTransaction *transaction, |
| 184 |
const gchar *identifier, GkmSecret *secret) |
| 185 |
{ |
| 186 |
g_return_if_fail (GKM_IS_SECRET_DATA (self)); |
| 187 |
g_return_if_fail (GKM_IS_TRANSACTION (transaction)); |
| 188 |
g_return_if_fail (!gkm_transaction_get_failed (transaction)); |
| 189 |
g_return_if_fail (identifier); |
| 190 |
g_return_if_fail (GKM_IS_SECRET (secret)); |
| 191 |
|
| 192 |
begin_set_secret (self, transaction, identifier, secret); |
| 193 |
} |
| 194 |
|
| 195 |
void |
| 196 |
gkm_secret_data_remove_secret (GkmSecretData *self, const gchar *identifier) |
| 197 |
{ |
| 198 |
g_return_if_fail (GKM_IS_SECRET_DATA (self)); |
| 199 |
g_return_if_fail (identifier); |
| 200 |
g_hash_table_remove (self->secrets, identifier); |
| 201 |
} |
| 202 |
|
| 203 |
GkmSecret* |
| 204 |
gkm_secret_data_get_master (GkmSecretData *self) |
| 205 |
{ |
| 206 |
g_return_val_if_fail (GKM_IS_SECRET_DATA (self), NULL); |
| 207 |
return self->master; |
| 208 |
} |
| 209 |
|
| 210 |
void |
| 211 |
gkm_secret_data_set_master (GkmSecretData *self, GkmSecret *master) |
| 212 |
{ |
| 213 |
g_return_if_fail (GKM_IS_SECRET_DATA (self)); |
| 214 |
g_return_if_fail (!master || GKM_IS_SECRET (master)); |
| 215 |
|
| 216 |
if (master) |
| 217 |
g_object_ref (master); |
| 218 |
if (self->master) |
| 219 |
g_object_unref (self->master); |
| 220 |
self->master = master; |
| 221 |
} |