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) 2013 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
29 #include <sys/types.h>
30 #include <sys/mount.h>
32 #include <glib/gstdio.h>
36 #include "tizen-storage-manager.h"
37 #include "gsignond/gsignond-log.h"
38 #include "gsignond/gsignond-utils.h"
40 #define EXTENSION_TIZEN_STORAGE_MANAGER_GET_PRIVATE(obj) \
41 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
42 EXTENSION_TYPE_TIZEN_STORAGE_MANAGER, \
43 ExtensionTizenStorageManagerPrivate))
45 /* these are limited by ecryptfs */
47 #define KEY_CIPHER "aes"
49 struct _ExtensionTizenStorageManagerPrivate
52 gchar fekey[ECRYPTFS_MAX_PASSPHRASE_BYTES + 1];
53 gchar fesalt[ECRYPTFS_SALT_SIZE + 1];
54 gchar ksig[ECRYPTFS_SIG_SIZE_HEX + 1];
66 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
68 G_DEFINE_TYPE (ExtensionTizenStorageManager,
69 extension_tizen_storage_manager,
70 GSIGNOND_TYPE_STORAGE_MANAGER);
73 _set_config (ExtensionTizenStorageManager *self, GSignondConfig *config)
75 GSignondStorageManager *parent = GSIGNOND_STORAGE_MANAGER (self);
76 g_assert (parent->config == NULL);
77 g_assert (self->priv->cdir == NULL);
78 parent->config = config;
80 gchar *user_dir = g_strdup_printf ("gsignond.%s", g_get_user_name ());
81 const gchar *secure_dir = gsignond_config_get_string (
83 GSIGNOND_CONFIG_GENERAL_SECURE_DIR);
85 parent->location = g_build_filename (secure_dir,
89 parent->location = g_build_filename ("/var/db",
93 self->priv->cdir = g_strdup_printf ("%s.efs", parent->location);
94 DBG ("location %s encryption point %s", parent->location, self->priv->cdir);
98 _set_property (GObject *object, guint prop_id, const GValue *value,
101 ExtensionTizenStorageManager *self =
102 EXTENSION_TIZEN_STORAGE_MANAGER (object);
103 ExtensionTizenStorageManagerPrivate *priv = self->priv;
107 _set_config (self, GSIGNOND_CONFIG (g_value_dup_object (value)));
110 g_strlcpy (priv->fekey,
111 g_value_get_string (value),
112 sizeof(priv->fekey));
115 g_strlcpy (priv->fesalt,
116 g_value_get_string (value),
117 sizeof(priv->fesalt));
120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125 _get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
127 ExtensionTizenStorageManager *self =
128 EXTENSION_TIZEN_STORAGE_MANAGER (object);
129 ExtensionTizenStorageManagerPrivate *priv = self->priv;
133 g_value_set_object (value,
134 GSIGNOND_STORAGE_MANAGER (self)->config);
137 g_value_set_string (value, priv->fekey);
140 g_value_set_string (value, priv->fesalt);
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 _dispose (GObject *object)
150 G_OBJECT_CLASS (extension_tizen_storage_manager_parent_class)->dispose (object);
154 _finalize (GObject *object)
156 ExtensionTizenStorageManager *self =
157 EXTENSION_TIZEN_STORAGE_MANAGER (object);
158 ExtensionTizenStorageManagerPrivate *priv = self->priv;
161 memset(priv->ksig, 0x00, sizeof(priv->ksig));
164 G_OBJECT_CLASS (extension_tizen_storage_manager_parent_class)->finalize (object);
168 _initialize_storage (GSignondStorageManager *parent)
170 ExtensionTizenStorageManager *self =
171 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
172 ExtensionTizenStorageManagerPrivate *priv = self->priv;
174 g_return_val_if_fail (parent->location, FALSE);
175 g_return_val_if_fail (priv->cdir, FALSE);
177 if (g_access (parent->location, R_OK) == 0 &&
178 g_access (priv->cdir, R_OK) == 0)
181 gboolean res = FALSE;
183 uid_t uid = getuid ();
185 WARN ("seteuid() failed");
187 DBG ("create mount point %s", parent->location);
188 if (g_mkdir_with_parents (parent->location, S_IRWXU | S_IRWXG))
191 DBG ("create storage point %s", priv->cdir);
192 if (g_mkdir_with_parents (priv->cdir, S_IRWXU | S_IRWXG))
195 if (chown (parent->location, 0, getegid ()))
196 WARN ("chown() failed");
197 if (chmod (parent->location, S_IRWXU | S_IRWXG))
198 WARN ("chmod() failed");
199 if (chown (priv->cdir, 0, getegid ()))
200 WARN ("chown() failed");
201 if (chmod (priv->cdir, S_IRWXU | S_IRWXG))
202 WARN ("chmod() failed");
207 WARN ("seteuid() failed");
213 _storage_is_initialized (GSignondStorageManager *parent)
215 ExtensionTizenStorageManager *self =
216 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
217 ExtensionTizenStorageManagerPrivate *priv = self->priv;
219 g_return_val_if_fail (priv->cdir, FALSE);
221 if (g_access (priv->cdir, 0) || g_access (parent->location, 0))
228 _mount_filesystem (GSignondStorageManager *parent)
230 gchar *retval = NULL;
231 ExtensionTizenStorageManager *self =
232 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
233 ExtensionTizenStorageManagerPrivate *priv = self->priv;
235 DBG ("add passphrase to kernel keyring");
236 if (ecryptfs_add_passphrase_key_to_keyring (priv->ksig,
241 gchar *mntopts = g_strdup_printf (
242 "ecryptfs_check_dev_ruid" \
243 ",ecryptfs_cipher=%s" \
244 ",ecryptfs_key_bytes=%d" \
245 ",ecryptfs_unlink_sigs" \
247 KEY_CIPHER, KEY_BYTES,
249 DBG ("mount options: %s", mntopts);
250 uid_t uid = getuid ();
252 WARN ("seteuid() failed");
253 DBG ("perform mount %s -> %s", priv->cdir, parent->location);
254 if (mount (priv->cdir, parent->location,
255 "ecryptfs", MS_NOSUID | MS_NODEV, mntopts)) {
256 INFO ("mount failed %d: %s", errno, strerror(errno));
260 DBG ("mount succeeded at %s", parent->location);
261 retval = parent->location;
266 WARN ("seteuid() failed");
272 _unmount_filesystem (GSignondStorageManager *parent)
274 g_return_val_if_fail (parent != NULL, FALSE);
276 uid_t uid = getuid ();
278 WARN ("seteuid() failed");
279 umount (parent->location);
281 WARN ("seteuid() failed");
287 _filesystem_is_mounted (GSignondStorageManager *parent)
289 gboolean retval = FALSE;
290 FILE *mntf = setmntent("/proc/mounts", "r");
291 g_return_val_if_fail (mntf != NULL, FALSE);
294 while ((me = getmntent(mntf))) {
295 if (g_strcmp0 (parent->location, me->mnt_dir) == 0) {
307 _delete_storage (GSignondStorageManager *parent)
309 ExtensionTizenStorageManager *self =
310 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
311 ExtensionTizenStorageManagerPrivate *priv = self->priv;
313 g_return_val_if_fail (priv->cdir, FALSE);
314 g_return_val_if_fail (!_filesystem_is_mounted(parent), FALSE);
316 return (gsignond_wipe_directory (priv->cdir) &&
317 gsignond_wipe_directory (parent->location));
321 extension_tizen_storage_manager_class_init (
322 ExtensionTizenStorageManagerClass *klass)
324 GObjectClass *base = G_OBJECT_CLASS (klass);
326 base->set_property = _set_property;
327 base->get_property = _get_property;
328 base->dispose = _dispose;
329 base->finalize = _finalize;
331 properties[PROP_FEKEY] = g_param_spec_string ("fekey",
333 "File encryption key",
335 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
336 properties[PROP_FESALT] = g_param_spec_string ("fesalt",
338 "File encryption salt",
340 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
341 g_object_class_install_properties (base, N_PROPERTIES, properties);
342 g_object_class_override_property (base, PROP_CONFIG, "config");
344 g_type_class_add_private (klass,
345 sizeof(ExtensionTizenStorageManagerPrivate));
347 GSignondStorageManagerClass *parent_class =
348 GSIGNOND_STORAGE_MANAGER_CLASS (klass);
349 parent_class->initialize_storage = _initialize_storage;
350 parent_class->delete_storage = _delete_storage;
351 parent_class->storage_is_initialized = _storage_is_initialized;
352 parent_class->mount_filesystem = _mount_filesystem;
353 parent_class->unmount_filesystem = _unmount_filesystem;
354 parent_class->filesystem_is_mounted = _filesystem_is_mounted;
358 extension_tizen_storage_manager_init (ExtensionTizenStorageManager *self)
360 ExtensionTizenStorageManagerPrivate *priv =
361 EXTENSION_TIZEN_STORAGE_MANAGER_GET_PRIVATE (self);
364 g_strlcpy (priv->fekey, "1234567890", sizeof(priv->fekey));
365 g_strlcpy (priv->fesalt, "0987654321", sizeof(priv->fesalt));