1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of gsignond
6 * Copyright (C) 2012 Intel Corporation.
8 * Contact: Jussi Laako <jussi.laako@linux.intel.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include <glib/gstdio.h>
30 #include "gsignond/gsignond-log.h"
31 #include "gsignond/gsignond-storage-manager.h"
32 #include "gsignond/gsignond-utils.h"
35 * SECTION:gsignond-storage-manager
36 * @short_description: manages encrypted disk storage for storing the secret database
37 * @include: gsignond/gsignond-plugin-interface.h
39 * #GSignondStorageManager manages encrypted disk storage for storing the secret
40 * database (as provided by #GSignondSecretStorage). The default implementation
41 * is a stub that does nothing, but gSSO can be configured to use a custom extension
42 * that provides a subclassed implementation of #GSignondStorageManager
43 * (see #GSignondExtension for instructions and pointers to examples).
46 * GSignondStorageManager:
48 * Opaque #GSignondStorageManager data structure.
52 #define GSIGNOND_STORAGE_MANAGER_GET_PRIVATE(obj) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
54 GSIGNOND_TYPE_STORAGE_MANAGER, \
55 GSignondStorageManagerPrivate))
57 struct _GSignondStorageManagerPrivate
68 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
70 G_DEFINE_TYPE (GSignondStorageManager, gsignond_storage_manager, G_TYPE_OBJECT);
73 _set_config (GSignondStorageManager *self, GSignondConfig *config)
75 g_assert (self->config == NULL);
76 self->config = config;
78 const gchar *secure_dir = gsignond_config_get_string (
80 GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
82 self->location = g_build_filename (secure_dir,
86 self->location = g_build_filename (g_get_user_data_dir (),
89 DBG ("secure dir %s", self->location);
93 _set_property (GObject *object, guint prop_id, const GValue *value,
96 GSignondStorageManager *self =
97 GSIGNOND_STORAGE_MANAGER (object);
101 g_assert (self->config == NULL);
102 _set_config (self, GSIGNOND_CONFIG (g_value_dup_object (value)));
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 _get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
112 GSignondStorageManager *self =
113 GSIGNOND_STORAGE_MANAGER (object);
117 g_value_set_object (value, self->config);
120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125 _dispose (GObject *object)
127 GSignondStorageManager *self =
128 GSIGNOND_STORAGE_MANAGER (object);
130 /* unmount mounted filesystem */
131 if (gsignond_storage_manager_filesystem_is_mounted (self)) {
132 gsignond_storage_manager_unmount_filesystem (self);
136 g_object_unref (self->config);
140 G_OBJECT_CLASS (gsignond_storage_manager_parent_class)->dispose (object);
144 _finalize (GObject *object)
146 GSignondStorageManager *self =
147 GSIGNOND_STORAGE_MANAGER (object);
149 if (self->location) {
150 g_free (self->location);
151 self->location = NULL;
154 G_OBJECT_CLASS (gsignond_storage_manager_parent_class)->finalize (object);
158 _initialize_storage (GSignondStorageManager *self)
160 g_return_val_if_fail (self != NULL, FALSE);
161 g_return_val_if_fail (self->location, FALSE);
163 if (g_mkdir_with_parents (self->location, S_IRWXU))
170 _delete_storage (GSignondStorageManager *self)
172 g_return_val_if_fail (self != NULL, FALSE);
173 g_return_val_if_fail (self->location, FALSE);
175 return gsignond_wipe_directory (self->location);
179 _storage_is_initialized (GSignondStorageManager *self)
181 g_return_val_if_fail (self != NULL, FALSE);
182 g_return_val_if_fail (self->location, FALSE);
184 if (g_access (self->location, 0)) /* 0 should equal to F_OK */
191 _mount_filesystem (GSignondStorageManager *self)
193 g_return_val_if_fail (self != NULL, NULL);
195 return self->location;
199 _unmount_filesystem (GSignondStorageManager *self)
201 g_return_val_if_fail (self != NULL, FALSE);
207 _filesystem_is_mounted (GSignondStorageManager *self)
209 return _storage_is_initialized (self);
213 * GSignondStorageManagerClass:
214 * @parent_class: parent class.
215 * @initialize_storage: an implementation of gsignond_storage_manager_initialize_storage()
216 * @delete_storage: an implementation of gsignond_storage_manager_delete_storage()
217 * @storage_is_initialized: an implementation of gsignond_storage_manager_storage_is_initialized()
218 * @mount_filesystem: an implementation of gsignond_storage_manager_mount_filesystem()
219 * @unmount_filesystem: an implementation of gsignond_storage_manager_unmount_filesystem()
220 * @filesystem_is_mounted: an implementation of gsignond_storage_manager_filesystem_is_mounted()
222 * #GSignondStorageManagerClass class containing pointers to class methods.
225 gsignond_storage_manager_class_init (GSignondStorageManagerClass *klass)
227 GObjectClass *base = G_OBJECT_CLASS (klass);
229 base->set_property = _set_property;
230 base->get_property = _get_property;
231 base->dispose = _dispose;
232 base->finalize = _finalize;
233 properties[PROP_CONFIG] = g_param_spec_object ("config",
235 "Configuration object",
236 GSIGNOND_TYPE_CONFIG,
237 G_PARAM_CONSTRUCT_ONLY|
239 G_PARAM_STATIC_STRINGS);
240 g_object_class_install_properties (base, N_PROPERTIES, properties);
242 /*g_type_class_add_private (klass, sizeof(GSignondStorageManagerPrivate));*/
244 klass->initialize_storage = _initialize_storage;
245 klass->delete_storage = _delete_storage;
246 klass->storage_is_initialized = _storage_is_initialized;
247 klass->mount_filesystem = _mount_filesystem;
248 klass->unmount_filesystem = _unmount_filesystem;
249 klass->filesystem_is_mounted = _filesystem_is_mounted;
253 gsignond_storage_manager_init (GSignondStorageManager *self)
255 /*self->priv = GSIGNOND_STORAGE_MANAGER_GET_PRIVATE (self);*/
257 self->location = NULL;
262 * gsignond_storage_manager_initialize_storage:
263 * @self: object instance.
265 * Initialize encryption storage. This means making sure that the
266 * necessary directories exist and are accessible.
271 gsignond_storage_manager_initialize_storage (GSignondStorageManager *self)
273 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
274 initialize_storage (self);
278 * gsignond_storage_manager_delete_storage:
279 * @self: object instance.
281 * Destroys all the encryption keys and wipes the storage. gsignond_wipe_directory()
282 * is typically used for the latter.
287 gsignond_storage_manager_delete_storage (GSignondStorageManager *self)
289 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
290 delete_storage (self);
294 * gsignond_storage_manager_storage_is_initialized:
295 * @self: object instance.
297 * Checks if the storage has been initialized.
299 * Returns: storage has been initialized?
302 gsignond_storage_manager_storage_is_initialized (GSignondStorageManager *self)
304 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
305 storage_is_initialized (self);
309 * gsignond_storage_manager_mount_filesystem:
310 * @self: object instance.
312 * Mounts an encrypted storage and returns the filesystem path of the storage
313 * mount point. This path will be used to access the secret database via
314 * #GSignondSecretStorage.
316 * The default implemenation does nothing, and immediately returns the path for the
319 * Returns: (transfer none): path of the storage mount point.
322 gsignond_storage_manager_mount_filesystem (GSignondStorageManager *self)
324 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
325 mount_filesystem (self);
329 * gsignond_storage_manager_unmount_filesystem:
330 * @self: object instance.
332 * Unmounts a previously mounted encrypted storage filesystem.
337 gsignond_storage_manager_unmount_filesystem (GSignondStorageManager *self)
339 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
340 unmount_filesystem (self);
344 * gsignond_storage_manager_filesystem_is_mounted:
345 * @self: object instance.
347 * Checks if the encrypted storage filesystem is currently mounted.
349 * Returns: filesystem is currently mounted?
352 gsignond_storage_manager_filesystem_is_mounted (GSignondStorageManager *self)
354 return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
355 filesystem_is_mounted (self);