daemon: add file and directory wipe functinality (and fix some typos)
authorJussi Laako <jussi.laako@linux.intel.com>
Wed, 24 Apr 2013 15:17:49 +0000 (18:17 +0300)
committerJussi Laako <jussi.laako@linux.intel.com>
Thu, 2 May 2013 12:55:37 +0000 (15:55 +0300)
include/gsignond/gsignond-utils.h [new file with mode: 0644]
src/common/Makefile.am
src/common/gsignond-storage-manager.c
src/common/gsignond-utils.c [new file with mode: 0644]
src/daemon/dbus/gsignond-dbus-auth-service-adapter.c
src/daemon/dbus/gsignond-dbus-auth-service-adapter.h
src/extensions/tizen/tizen-storage-manager.c

diff --git a/include/gsignond/gsignond-utils.h b/include/gsignond/gsignond-utils.h
new file mode 100644 (file)
index 0000000..5334732
--- /dev/null
@@ -0,0 +1,42 @@
+/* vi: set et sw=4 ts=4 cino=t0,(0: */
+/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of gsignond
+ *
+ * Copyright (C) 2013 Intel Corporation.
+ *
+ * Contact: Jussi Laako <jussi.laako@linux.intel.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef _GSIGNOND_UTILS_H_
+#define _GSIGNOND_UTILS_H_
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gboolean
+gsignond_wipe_file (const gchar *);
+
+gboolean
+gsignond_wipe_directory (const gchar *);
+
+G_END_DECLS
+
+#endif  /* _SGINOND_UTILS_H_ */
+
index c41230e..704d0ed 100644 (file)
@@ -58,6 +58,7 @@ libgsignond_common_la_SOURCES = \
     gsignond-session-data.c \
     gsignond-signonui-data.c \
     gsignond-plugin-loader.c \
+    gsignond-utils.c \
     $(BUILT_SOURCES) \
     $(NULL)
 
index 9a4c715..3ac715e 100644 (file)
@@ -28,6 +28,7 @@
 #include <glib/gstdio.h>
 
 #include "gsignond/gsignond-storage-manager.h"
+#include "gsignond/gsignond-utils.h"
 
 #define GSIGNOND_STORAGE_MANAGER_GET_PRIVATE(obj) \
     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
@@ -143,23 +144,10 @@ _initialize_storage (GSignondStorageManager *self)
 static gboolean
 _delete_storage (GSignondStorageManager *self)
 {
-    const gchar *filename;
-    GDir *storage_dir;
-
     g_return_val_if_fail (self != NULL, FALSE);
     g_return_val_if_fail (self->location, FALSE);
 
-    storage_dir = g_dir_open (self->location, 0, NULL);
-    if (!storage_dir)
-        return FALSE;
-    while ((filename = g_dir_read_name (storage_dir)) != NULL) {
-        g_remove (filename);
-    }
-    g_dir_close (storage_dir);
-    if (g_rmdir (self->location))
-        return FALSE;
-
-    return TRUE;
+    return gsignond_wipe_directory (self->location);
 }
 
 static gboolean
diff --git a/src/common/gsignond-utils.c b/src/common/gsignond-utils.c
new file mode 100644 (file)
index 0000000..0f4b658
--- /dev/null
@@ -0,0 +1,148 @@
+/* vi: set et sw=4 ts=4 cino=t0,(0: */
+/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of gsignond
+ *
+ * Copyright (C) 2013 Intel Corporation.
+ *
+ * Contact: Jussi Laako <jussi.laako@linux.intel.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+#include "gsignond/gsignond-utils.h"
+
+
+static size_t pagesize = 0;
+
+
+gboolean
+gsignond_wipe_file (const gchar *filename)
+{
+    gboolean retval = FALSE;
+    int rngfd;
+    int wipefd;
+    size_t sizeleft;
+    size_t writesize;
+    ssize_t sizewritten;
+    struct stat filestat;
+    guint8 *wipebuf;
+
+    if (!pagesize) {
+        long confval = sysconf (_SC_PAGE_SIZE);
+        if (confval <= 0)
+            return FALSE;
+        pagesize = (size_t) confval;
+    }
+
+    rngfd = open ("/dev/urandom", O_RDONLY);
+    if (rngfd < 0)
+        return FALSE;
+
+    wipefd = open (filename, O_WRONLY | O_SYNC);
+    if (rngfd < 0)
+        goto _rng_exit;
+    wipebuf = g_malloc (pagesize);
+    if (fstat (wipefd, &filestat))
+        goto _wipe_exit;
+
+    /* write all 1's */
+    sizeleft = filestat.st_size;
+    memset (wipebuf, 0xff, pagesize);
+    while (sizeleft) {
+        writesize = (sizeleft < pagesize) ? sizeleft : pagesize;
+        sizewritten = write (wipefd, wipebuf, writesize);
+        if (sizewritten != (ssize_t) writesize)
+            goto _wipe_exit;
+        sizeleft -= sizewritten;
+    }
+
+    if (lseek (wipefd, 0, SEEK_SET) == (off_t) -1)
+        goto _wipe_exit;
+
+    /* write all 0's */
+    sizeleft = filestat.st_size;
+    memset (wipebuf, 0x00, pagesize);
+    while (sizeleft) {
+        writesize = (sizeleft < pagesize) ? sizeleft : pagesize;
+        sizewritten = write (wipefd, wipebuf, writesize);
+        if (sizewritten != (ssize_t) writesize)
+            goto _wipe_exit;
+        sizeleft -= sizewritten;
+    }
+
+    if (lseek (wipefd, 0, SEEK_SET) == (off_t) -1)
+        goto _wipe_exit;
+
+    /* write random */
+    sizeleft = filestat.st_size;
+    while (sizeleft) {
+        writesize = (sizeleft < pagesize) ? sizeleft : pagesize;
+        if (read (rngfd, wipebuf, writesize) != (ssize_t) writesize)
+            goto _wipe_exit;
+        sizewritten = write (wipefd, wipebuf, writesize);
+        if (sizewritten != (ssize_t) writesize)
+            goto _wipe_exit;
+        sizeleft -= sizewritten;
+    }
+
+    /* don't leave traces of last pattern to the memory */
+    memset (wipebuf, 0x00, pagesize);
+
+    /* remove the file and set return value on success */
+    if (unlink (filename) == 0)
+        retval = TRUE;
+
+_wipe_exit:
+    g_free (wipebuf);
+    close (wipefd);
+_rng_exit:
+    close (rngfd);
+    return retval;
+}
+
+
+gboolean
+gsignond_wipe_directory (const gchar *dirname)
+{
+    gboolean retval = FALSE;
+    gboolean wiperes;
+    const gchar *filename;
+    gchar *filepath;
+    GDir *dirctx;
+
+    dirctx = g_dir_open (dirname, 0, NULL);
+    if (!dirctx)
+        return FALSE;
+    while ((filename = g_dir_read_name (dirctx))) {
+        filepath = g_build_filename (dirname, filename, NULL);
+        wiperes = gsignond_wipe_file (filepath);
+        g_free (filepath);
+        if (!wiperes)
+            goto _dir_exit;
+    }
+    retval = TRUE;
+
+_dir_exit:
+    g_dir_close (dirctx);
+    return retval;
+}
+
index d2f6cd2..b82df81 100644 (file)
@@ -533,7 +533,7 @@ gsignond_dbus_auth_service_adapter_new_with_connection (GDBusConnection *bus_con
         g_object_unref (adapter);
         return NULL;
     }
-    DBG("(+) started auth service '%p' at path '%s' on conneciton '%p'", adapter, GSIGNOND_DAEMON_OBJECTPATH, bus_connection);
+    DBG("(+) started auth service '%p' at path '%s' on connection '%p'", adapter, GSIGNOND_DAEMON_OBJECTPATH, bus_connection);
 
     timeout = gsignond_daemon_get_timeout (adapter->priv->auth_service);
     if (timeout) {
index ab032ce..9323f1d 100644 (file)
@@ -61,7 +61,7 @@ struct _GSignondDbusAuthServiceAdapterClass
 GType gsignond_dbus_auth_service_adapter_get_type (void) G_GNUC_CONST;
 
 GSignondDbusAuthServiceAdapter *
-gsignond_dbus_auth_service_adapter_new_with_connection (GDBusConnection *conneciton,
+gsignond_dbus_auth_service_adapter_new_with_connection (GDBusConnection *connection,
                                                         GSignondDaemon *daemon);
 
 #ifndef USE_P2P
index d5b4ce3..2ae4217 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "tizen-storage-manager.h"
 #include "gsignond/gsignond-log.h"
+#include "gsignond/gsignond-utils.h"
 
 #define EXTENSION_TIZEN_STORAGE_MANAGER_GET_PRIVATE(obj) \
     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
@@ -177,40 +178,6 @@ _initialize_storage (GSignondStorageManager *parent)
 }
 
 static gboolean
-_delete_storage (GSignondStorageManager *parent)
-{
-    ExtensionTizenStorageManager *self =
-        EXTENSION_TIZEN_STORAGE_MANAGER (parent);
-    ExtensionTizenStorageManagerPrivate *priv = self->priv;
-
-    g_return_val_if_fail (priv->cdir, FALSE);
-
-    gboolean retval = FALSE;
-    const gchar *filename;
-    GDir *storage_dir;
-
-    storage_dir = g_dir_open (priv->cdir, 0, NULL);
-    if (!storage_dir)
-        goto _delete_exit;
-    while ((filename = g_dir_read_name (storage_dir)) != NULL) {
-        DBG ("remove file %s", filename);
-        g_remove (filename);
-    }
-    g_dir_close (storage_dir);
-    DBG ("remove directory %s", priv->cdir);
-    if (g_rmdir (priv->cdir))
-        goto _delete_exit;
-    DBG ("remove directory %s", parent->location);
-    if (g_rmdir (parent->location))
-        goto _delete_exit;
-
-    retval = TRUE;
-
-_delete_exit:
-    return retval;
-}
-
-static gboolean
 _storage_is_initialized (GSignondStorageManager *parent)
 {
     ExtensionTizenStorageManager *self =
@@ -306,6 +273,20 @@ _filesystem_is_mounted (GSignondStorageManager *parent)
     return retval;
 }
 
+static gboolean
+_delete_storage (GSignondStorageManager *parent)
+{
+    ExtensionTizenStorageManager *self =
+        EXTENSION_TIZEN_STORAGE_MANAGER (parent);
+    ExtensionTizenStorageManagerPrivate *priv = self->priv;
+
+    g_return_val_if_fail (priv->cdir, FALSE);
+    g_return_val_if_fail (!_filesystem_is_mounted(parent), FALSE);
+
+    return (gsignond_wipe_directory (priv->cdir) &&
+            gsignond_wipe_directory (parent->location));
+}
+
 static void
 extension_tizen_storage_manager_class_init (
                                       ExtensionTizenStorageManagerClass *klass)