bbb44b670fa572630eb7fd1706bc4a20977525f0
[platform/upstream/gsignond.git] / src / common / gsignond-storage-manager.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2012 Intel Corporation.
7  *
8  * Contact: Jussi Laako <jussi.laako@linux.intel.com>
9  *
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.
14  *
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.
19  *
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
23  * 02110-1301 USA
24  */
25
26 #include <sys/stat.h>
27
28 #include <glib/gstdio.h>
29
30 #include "gsignond/gsignond-log.h"
31 #include "gsignond/gsignond-storage-manager.h"
32 #include "gsignond/gsignond-utils.h"
33
34 #define GSIGNOND_STORAGE_MANAGER_GET_PRIVATE(obj) \
35     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
36                                   GSIGNOND_TYPE_STORAGE_MANAGER, \
37                                   GSignondStorageManagerPrivate))
38
39 struct _GSignondStorageManagerPrivate
40 {
41 };
42
43 enum
44 {
45     PROP_0,
46     PROP_CONFIG,
47     N_PROPERTIES
48 };
49
50 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
51
52 G_DEFINE_TYPE (GSignondStorageManager, gsignond_storage_manager, G_TYPE_OBJECT);
53
54 static void
55 _set_config (GSignondStorageManager *self, GSignondConfig *config)
56 {
57     g_assert (self->config == NULL);
58     self->config = config;
59
60     const gchar *secure_dir = gsignond_config_get_string (
61                                         self->config,
62                                         GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
63     if (secure_dir)
64         self->location = g_build_filename (secure_dir,
65                                            "gsignond.secret",
66                                            NULL);
67     else
68         self->location = g_build_filename (g_get_user_data_dir (),
69                                            "gsignond.secret",
70                                            NULL);
71     DBG ("secure dir %s", self->location);
72 }
73
74 static void
75 _set_property (GObject *object, guint prop_id, const GValue *value,
76                GParamSpec *pspec)
77 {
78     GSignondStorageManager *self =
79         GSIGNOND_STORAGE_MANAGER (object);
80
81     switch (prop_id) {
82         case PROP_CONFIG:
83             g_assert (self->config == NULL);
84             _set_config (self, GSIGNOND_CONFIG (g_value_dup_object (value)));
85             break;
86         default:
87             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88     }
89 }
90
91 static void
92 _get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
93 {
94     GSignondStorageManager *self =
95         GSIGNOND_STORAGE_MANAGER (object);
96
97     switch (prop_id) {
98         case PROP_CONFIG:
99             g_value_set_object (value, self->config);
100             break;
101         default:
102             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103     }
104 }
105
106 static void
107 _dispose (GObject *object)
108 {
109     GSignondStorageManager *self =
110         GSIGNOND_STORAGE_MANAGER (object);
111
112     /* unmount mounted filesystem */
113     if (gsignond_storage_manager_filesystem_is_mounted (self)) {
114         gsignond_storage_manager_unmount_filesystem (self);
115     }
116
117     if (self->config) {
118         g_object_unref (self->config);
119         self->config = NULL;
120     }
121
122     G_OBJECT_CLASS (gsignond_storage_manager_parent_class)->dispose (object);
123 }
124
125 static void
126 _finalize (GObject *object)
127 {
128     GSignondStorageManager *self = 
129         GSIGNOND_STORAGE_MANAGER (object);
130
131     if (self->location) {
132         g_free (self->location);
133         self->location = NULL;
134     }
135
136     G_OBJECT_CLASS (gsignond_storage_manager_parent_class)->finalize (object);
137 }
138
139 static gboolean
140 _initialize_storage (GSignondStorageManager *self)
141 {
142     g_return_val_if_fail (self != NULL, FALSE);
143     g_return_val_if_fail (self->location, FALSE);
144
145     if (g_mkdir_with_parents (self->location, S_IRWXU))
146         return FALSE;
147
148     return TRUE;
149 }
150
151 static gboolean
152 _delete_storage (GSignondStorageManager *self)
153 {
154     g_return_val_if_fail (self != NULL, FALSE);
155     g_return_val_if_fail (self->location, FALSE);
156
157     return gsignond_wipe_directory (self->location);
158 }
159
160 static gboolean
161 _storage_is_initialized (GSignondStorageManager *self)
162 {
163     g_return_val_if_fail (self != NULL, FALSE);
164     g_return_val_if_fail (self->location, FALSE);
165
166     if (g_access (self->location, 0))  /* 0 should equal to F_OK */
167         return FALSE;
168
169     return TRUE;
170 }
171
172 static const gchar *
173 _mount_filesystem (GSignondStorageManager *self)
174 {
175     g_return_val_if_fail (self != NULL, NULL);
176
177     return self->location;
178 }
179
180 static gboolean
181 _unmount_filesystem (GSignondStorageManager *self)
182 {
183     g_return_val_if_fail (self != NULL, FALSE);
184
185     return TRUE;
186 }
187
188 static gboolean
189 _filesystem_is_mounted (GSignondStorageManager *self)
190 {
191     return _storage_is_initialized (self);
192 }
193
194 static void
195 gsignond_storage_manager_class_init (GSignondStorageManagerClass *klass)
196 {
197     GObjectClass *base = G_OBJECT_CLASS (klass);
198
199     base->set_property = _set_property;
200     base->get_property = _get_property;
201     base->dispose = _dispose;
202     base->finalize = _finalize;
203     properties[PROP_CONFIG] = g_param_spec_object ("config",
204                                                    "config",
205                                                    "Configuration object",
206                                                    GSIGNOND_TYPE_CONFIG,
207                                                    G_PARAM_CONSTRUCT_ONLY|
208                                                    G_PARAM_READWRITE|
209                                                    G_PARAM_STATIC_STRINGS);
210     g_object_class_install_properties (base, N_PROPERTIES, properties);
211
212     /*g_type_class_add_private (klass, sizeof(GSignondStorageManagerPrivate));*/
213  
214     klass->initialize_storage = _initialize_storage;
215     klass->delete_storage = _delete_storage;
216     klass->storage_is_initialized = _storage_is_initialized;
217     klass->mount_filesystem = _mount_filesystem;
218     klass->unmount_filesystem = _unmount_filesystem;
219     klass->filesystem_is_mounted = _filesystem_is_mounted;
220 }
221
222 static void
223 gsignond_storage_manager_init (GSignondStorageManager *self)
224 {
225     /*self->priv = GSIGNOND_STORAGE_MANAGER_GET_PRIVATE (self);*/
226
227     self->location = NULL;
228     self->config = NULL;
229 }
230
231 /**
232  * gsignond_storage_manager_initialize_storage:
233  * @self: object instance.
234  *
235  * Initialize encryption storage. Initiali key should be set using
236  * #gsignond_storage_manager_set_encryption_key before calling this.
237  *
238  * Returns: success?
239  */
240 gboolean
241 gsignond_storage_manager_initialize_storage (GSignondStorageManager *self)
242 {
243     return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
244         initialize_storage (self);
245 }
246
247 /**
248  * gsignond_storage_manager_delete_storage:
249  * @self: object instance.
250  *
251  * Destroys all the encryption keys and wipes the storage.
252  *
253  * Returns: success?
254  */
255 gboolean
256 gsignond_storage_manager_delete_storage (GSignondStorageManager *self)
257 {
258     return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
259         delete_storage (self);
260 }
261
262 /**
263  * gsignond_storage_manager_storage_is_initialized:
264  * @self: object instance.
265  *
266  * Checks if the storage exists, and if possible if it has been initialized.
267  *
268  * Returns: storage has been initialized?
269  */
270 gboolean
271 gsignond_storage_manager_storage_is_initialized (GSignondStorageManager *self)
272 {
273     return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
274         storage_is_initialized (self);
275 }
276
277 /**
278  * gsignond_storage_manager_mount_filesystem:
279  * @self: object instance.
280  *
281  * Mounts an encrypted storage and returns filesystem path of the storage
282  * mount point.
283  *
284  * Returns: (transfer none): path of the storage mount point.
285  */
286 const gchar *
287 gsignond_storage_manager_mount_filesystem (GSignondStorageManager *self)
288 {
289     return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
290         mount_filesystem (self);
291 }
292
293 /**
294  * gsignond_storage_manager_unmount_filesystem:
295  * @self: object instance.
296  *
297  * Unmounts a previously mounted storage filesystem.
298  *
299  * Returns: success?
300  */
301 gboolean
302 gsignond_storage_manager_unmount_filesystem (GSignondStorageManager *self)
303 {
304     return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
305         unmount_filesystem (self);
306 }
307
308 /**
309  * gsignond_storage_manager_filesystem_is_mounted:
310  * @self: object instance.
311  *
312  * Checks if the filesystem is currently mounted.
313  *
314  * Returns: filesystem is currently mounted?
315  */
316 gboolean
317 gsignond_storage_manager_filesystem_is_mounted (GSignondStorageManager *self)
318 {
319     return GSIGNOND_STORAGE_MANAGER_GET_CLASS (self)->
320         filesystem_is_mounted (self);
321 }
322