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>
38 #include "tizen-storage-manager.h"
39 #include "gsignond/gsignond-log.h"
40 #include "gsignond/gsignond-utils.h"
42 #define EXTENSION_TIZEN_STORAGE_MANAGER_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
44 EXTENSION_TYPE_TIZEN_STORAGE_MANAGER, \
45 ExtensionTizenStorageManagerPrivate))
47 /* these are limited by ecryptfs */
49 #define KEY_CIPHER "aes"
51 struct _ExtensionTizenStorageManagerPrivate
54 gchar fekey[ECRYPTFS_MAX_PASSPHRASE_BYTES + 1];
55 gchar fesalt[ECRYPTFS_SALT_SIZE + 1];
56 gchar ksig[ECRYPTFS_SIG_SIZE_HEX + 1];
68 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
70 G_DEFINE_TYPE (ExtensionTizenStorageManager,
71 extension_tizen_storage_manager,
72 GSIGNOND_TYPE_STORAGE_MANAGER);
75 _set_config (ExtensionTizenStorageManager *self, GSignondConfig *config)
77 GSignondStorageManager *parent = GSIGNOND_STORAGE_MANAGER (self);
78 g_assert (parent->config == NULL);
79 g_assert (self->priv->cdir == NULL);
80 parent->config = config;
82 gchar *user_dir = g_strdup_printf ("gsignond.%s", g_get_user_name ());
83 const gchar *storage_path = gsignond_config_get_string (
85 GSIGNOND_CONFIG_GENERAL_STORAGE_PATH);
87 storage_path = BASE_STORAGE_DIR;
89 const gchar *env_val = g_getenv("SSO_STORAGE_PATH");
91 storage_path = env_val;
93 parent->location = g_build_filename (storage_path, user_dir, NULL);
95 self->priv->cdir = g_strdup_printf ("%s.efs", parent->location);
96 DBG ("location %s encryption point %s", parent->location, self->priv->cdir);
100 _set_property (GObject *object, guint prop_id, const GValue *value,
103 ExtensionTizenStorageManager *self =
104 EXTENSION_TIZEN_STORAGE_MANAGER (object);
105 ExtensionTizenStorageManagerPrivate *priv = self->priv;
109 _set_config (self, GSIGNOND_CONFIG (g_value_dup_object (value)));
112 g_strlcpy (priv->fekey,
113 g_value_get_string (value),
114 sizeof(priv->fekey));
117 g_strlcpy (priv->fesalt,
118 g_value_get_string (value),
119 sizeof(priv->fesalt));
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 _get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
129 ExtensionTizenStorageManager *self =
130 EXTENSION_TIZEN_STORAGE_MANAGER (object);
131 ExtensionTizenStorageManagerPrivate *priv = self->priv;
135 g_value_set_object (value,
136 GSIGNOND_STORAGE_MANAGER (self)->config);
139 g_value_set_string (value, priv->fekey);
142 g_value_set_string (value, priv->fesalt);
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 _dispose (GObject *object)
152 G_OBJECT_CLASS (extension_tizen_storage_manager_parent_class)->dispose (object);
156 _finalize (GObject *object)
158 ExtensionTizenStorageManager *self =
159 EXTENSION_TIZEN_STORAGE_MANAGER (object);
160 ExtensionTizenStorageManagerPrivate *priv = self->priv;
163 memset(priv->ksig, 0x00, sizeof(priv->ksig));
166 G_OBJECT_CLASS (extension_tizen_storage_manager_parent_class)->finalize (object);
170 _initialize_storage (GSignondStorageManager *parent)
172 ExtensionTizenStorageManager *self =
173 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
174 ExtensionTizenStorageManagerPrivate *priv = self->priv;
176 g_return_val_if_fail (parent->location, FALSE);
177 g_return_val_if_fail (priv->cdir, FALSE);
179 if (g_access (parent->location, R_OK) == 0 &&
180 g_access (priv->cdir, R_OK) == 0)
183 gboolean res = FALSE;
185 uid_t uid = getuid ();
187 WARN ("seteuid() failed");
189 DBG ("create mount point %s", parent->location);
190 if (g_mkdir_with_parents (parent->location, S_IRWXU | S_IRWXG))
193 DBG ("create storage point %s", priv->cdir);
194 if (g_mkdir_with_parents (priv->cdir, S_IRWXU | S_IRWXG))
197 if (chown (parent->location, 0, getegid ()))
198 WARN ("chown() failed");
199 if (chmod (parent->location, S_IRWXU | S_IRWXG))
200 WARN ("chmod() failed");
201 if (chown (priv->cdir, 0, getegid ()))
202 WARN ("chown() failed");
203 if (chmod (priv->cdir, S_IRWXU | S_IRWXG))
204 WARN ("chmod() failed");
209 WARN ("seteuid() failed");
215 _storage_is_initialized (GSignondStorageManager *parent)
217 ExtensionTizenStorageManager *self =
218 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
219 ExtensionTizenStorageManagerPrivate *priv = self->priv;
221 g_return_val_if_fail (priv->cdir, FALSE);
223 if (g_access (priv->cdir, 0) || g_access (parent->location, 0))
230 _mount_filesystem (GSignondStorageManager *parent)
232 gchar *retval = NULL;
233 ExtensionTizenStorageManager *self =
234 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
235 ExtensionTizenStorageManagerPrivate *priv = self->priv;
237 DBG ("add passphrase to kernel keyring");
238 if (ecryptfs_add_passphrase_key_to_keyring (priv->ksig,
243 gchar *mntopts = g_strdup_printf (
244 "ecryptfs_check_dev_ruid" \
245 ",ecryptfs_cipher=%s" \
246 ",ecryptfs_key_bytes=%d" \
247 ",ecryptfs_unlink_sigs" \
249 KEY_CIPHER, KEY_BYTES,
251 DBG ("mount options: %s", mntopts);
252 uid_t uid = getuid ();
254 WARN ("seteuid() failed");
255 DBG ("perform mount %s -> %s", priv->cdir, parent->location);
256 if (mount (priv->cdir, parent->location,
257 "ecryptfs", MS_NOSUID | MS_NODEV, mntopts)) {
258 INFO ("mount failed %d: %s", errno, strerror(errno));
262 DBG ("mount succeeded at %s", parent->location);
263 retval = parent->location;
268 WARN ("seteuid() failed");
274 _unmount_filesystem (GSignondStorageManager *parent)
276 g_return_val_if_fail (parent != NULL, FALSE);
278 uid_t uid = getuid ();
280 WARN ("seteuid() failed");
281 umount (parent->location);
283 WARN ("seteuid() failed");
289 _filesystem_is_mounted (GSignondStorageManager *parent)
291 gboolean retval = FALSE;
292 FILE *mntf = setmntent("/proc/mounts", "r");
293 g_return_val_if_fail (mntf != NULL, FALSE);
296 while ((me = getmntent(mntf))) {
297 if (g_strcmp0 (parent->location, me->mnt_dir) == 0) {
309 _delete_storage (GSignondStorageManager *parent)
311 ExtensionTizenStorageManager *self =
312 EXTENSION_TIZEN_STORAGE_MANAGER (parent);
313 ExtensionTizenStorageManagerPrivate *priv = self->priv;
315 g_return_val_if_fail (priv->cdir, FALSE);
316 g_return_val_if_fail (!_filesystem_is_mounted(parent), FALSE);
318 return (gsignond_wipe_directory (priv->cdir) &&
319 gsignond_wipe_directory (parent->location));
323 extension_tizen_storage_manager_class_init (
324 ExtensionTizenStorageManagerClass *klass)
326 GObjectClass *base = G_OBJECT_CLASS (klass);
328 base->set_property = _set_property;
329 base->get_property = _get_property;
330 base->dispose = _dispose;
331 base->finalize = _finalize;
333 properties[PROP_FEKEY] = g_param_spec_string ("fekey",
335 "File encryption key",
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
338 properties[PROP_FESALT] = g_param_spec_string ("fesalt",
340 "File encryption salt",
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
343 g_object_class_install_properties (base, N_PROPERTIES, properties);
344 g_object_class_override_property (base, PROP_CONFIG, "config");
346 g_type_class_add_private (klass,
347 sizeof(ExtensionTizenStorageManagerPrivate));
349 GSignondStorageManagerClass *parent_class =
350 GSIGNOND_STORAGE_MANAGER_CLASS (klass);
351 parent_class->initialize_storage = _initialize_storage;
352 parent_class->delete_storage = _delete_storage;
353 parent_class->storage_is_initialized = _storage_is_initialized;
354 parent_class->mount_filesystem = _mount_filesystem;
355 parent_class->unmount_filesystem = _unmount_filesystem;
356 parent_class->filesystem_is_mounted = _filesystem_is_mounted;
360 extension_tizen_storage_manager_init (ExtensionTizenStorageManager *self)
362 ExtensionTizenStorageManagerPrivate *priv =
363 EXTENSION_TIZEN_STORAGE_MANAGER_GET_PRIVATE (self);
366 g_strlcpy (priv->fekey, "1234567890", sizeof(priv->fekey));
367 g_strlcpy (priv->fesalt, "0987654321", sizeof(priv->fesalt));