daemon: added error structure
authorImran Zaman <imran.zaman@intel.com>
Mon, 3 Dec 2012 10:34:08 +0000 (12:34 +0200)
committerJussi Laako <jussi.laako@linux.intel.com>
Tue, 4 Dec 2012 16:46:39 +0000 (18:46 +0200)
include/gsignond/gsignond-security-context.h [new file with mode: 0644]
src/common/gsignond-security-context.c [new file with mode: 0644]
src/daemon/db/gsignond-db-error.c [new file with mode: 0644]
src/daemon/db/gsignond-db-error.h [new file with mode: 0644]
src/daemon/gsignond-error.c [new file with mode: 0644]
src/daemon/gsignond-error.h [new file with mode: 0644]

diff --git a/include/gsignond/gsignond-security-context.h b/include/gsignond/gsignond-security-context.h
new file mode 100644 (file)
index 0000000..bf80787
--- /dev/null
@@ -0,0 +1,82 @@
+/* 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) 2012 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 _SIGNON_SECURITY_CONTEXT_H_
+#define _SIGNON_SECURITY_CONTEXT_H_
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+/**
+ * Security context descriptor.
+ *
+ * Practically a string tuple.
+ *
+ * @sys_ctx: system context, such as SMACK-label, MSSF token or just a
+ *           binary path.
+ * @app_ctx: application context, such as a script or a web page.
+ */
+typedef struct _SignonSecurityContext
+{
+    gchar *sys_ctx;
+    gchar *app_ctx;
+} SignonSecurityContext;
+
+/**
+ * GList of #SignonSecurityContext items.
+ */
+typedef GList SignonSecurityContextList;
+
+SignonSecurityContext * signon_security_context_new ();
+SignonSecurityContext * signon_security_context_new_from_values (
+                                            const gchar *system_context,
+                                            const gchar *application_context);
+void signon_security_context_free (SignonSecurityContext *ctx);
+SignonSecurityContext * signon_security_context_copy (
+                                        const SignonSecurityContext *src_ctx);
+void signon_security_context_set_system_context (SignonSecurityContext *ctx,
+                                                 const gchar *system_context);
+const gchar * signon_security_context_get_system_context (
+                                                SignonSecurityContext *ctx);
+void signon_security_context_set_application_context (
+                                            SignonSecurityContext *ctx,
+                                            const gchar *application_context);
+const gchar * signon_security_context_get_application_context (
+                                                SignonSecurityContext *ctx);
+
+GVariant * signon_security_context_build_variant (
+                                        const SignonSecurityContextList *list);
+SignonSecurityContextList * signon_security_context_deconstruct_variant (
+                                                            GVariant *variant);
+
+SignonSecurityContextList * signon_security_context_list_copy (
+                                    const SignonSecurityContextList *src_list);
+void signon_security_context_list_free (SignonSecurityContextList *seclist);
+
+G_END_DECLS
+
+#endif  /* _SIGNON_SECURITY_CONTEXT_H_ */
+
diff --git a/src/common/gsignond-security-context.c b/src/common/gsignond-security-context.c
new file mode 100644 (file)
index 0000000..581486e
--- /dev/null
@@ -0,0 +1,275 @@
+/* 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) 2012 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 "gsignond/signon-security-context.h"
+
+static void
+_security_context_free (gpointer ptr)
+{
+    SignonSecurityContext *ctx = (SignonSecurityContext *) ptr;
+
+    signon_security_context_free (ctx);
+}
+
+/**
+ * signon_security_context_new:
+ *
+ * Allocates a new security context item.
+ *
+ * Returns: (transfer full) allocated #SignonSecurityContext.
+ */
+SignonSecurityContext *
+signon_security_context_new ()
+{
+    SignonSecurityContext *ctx;
+
+    ctx = g_new0 (SignonSecurityContext, 1);
+    ctx->sys_ctx = g_strdup ("");
+    ctx->app_ctx = g_strdup ("*");
+
+    return ctx;
+}
+
+/**
+ * signon_security_context_new_from_vaues:
+ * @system_context: system security context (such as SMACK/MSSF label/token).
+ * @application_context: application security context (such as a script name).
+ *
+ * Allocates and initializes a new security context item.
+ *
+ * Returns: (transfer full) allocated #SignonSecurityContext.
+ */
+SignonSecurityContext *
+signon_security_context_new_from_values (const gchar *system_context,
+                                         const gchar *application_context)
+{
+    SignonSecurityContext *ctx;
+
+    g_return_val_if_fail (system_context != NULL, NULL);
+
+    ctx = g_new0 (SignonSecurityContext, 1);
+    ctx->sys_ctx = g_strdup (system_context);
+    if (application_context)
+        ctx->app_ctx = g_strdup (application_context);
+    else
+        ctx->app_ctx = g_strdup ("*");
+
+    return ctx;
+}
+
+/**
+ * signon_security_context_copy:
+ * @src_ctx: source security context to copy.
+ *
+ * Copy a security context item.
+ *
+ * Returns: (transfer full) a copy of the #SignonSecurityContex item.
+ */
+SignonSecurityContext *
+signon_security_context_copy (const SignonSecurityContext *src_ctx)
+{
+    g_return_val_if_fail (src_ctx != NULL, NULL);
+
+    return signon_security_context_new_from_values (src_ctx->sys_ctx,
+                                                    src_ctx->app_ctx);
+}
+
+/**
+ * signon_security_context_free:
+ * @ctx: #SignonSecurityContext to be freed.
+ *
+ * Frees a security context item.
+ */
+void
+signon_security_context_free (SignonSecurityContext *ctx)
+{
+    g_return_if_fail (ctx != NULL);
+
+    g_free (ctx->sys_ctx);
+    g_free (ctx->app_ctx);
+    g_free (ctx);
+}
+
+/**
+ * signon_security_context_set_system_context:
+ * @ctx: #SignonSecurityContext item.
+ * @system_context: system security context.
+ *
+ * Sets the system context part (such as SMACK label or MSSF token) of the
+ * #SignonSecurityContext.
+ */
+void
+signon_security_context_set_system_context (SignonSecurityContext *ctx,
+                                            const gchar *system_context)
+{
+    g_return_if_fail (ctx != NULL);
+
+    g_free (ctx->sys_ctx);
+    ctx->sys_ctx = g_strdup (system_context);
+}
+
+/**
+ * signon_security_context_get_system_context:
+ * @ctx: #SignonSecurityContext item.
+ * 
+ * Get the system context part (such as SMACK label or MSSF token) of the
+ * #SignonSecurityContext.
+ *
+ * Returns: (transfer none) system context.
+ */
+const gchar *
+signon_security_context_get_system_context (SignonSecurityContext *ctx)
+{
+    g_return_val_if_fail (ctx != NULL, NULL);
+
+    return ctx->sys_ctx;
+}
+
+/**
+ * signon_security_context_set_application_context:
+ * @ctx: #SignonSecurityContext item.
+ * @application_context: application security context.
+ *
+ * Sets the application context part (such as a script name or a web page) of
+ * the #SignonSecurityContext.
+ */
+void
+signon_security_context_set_application_context (SignonSecurityContext *ctx,
+                                              const gchar *application_context)
+{
+    g_return_if_fail (ctx != NULL);
+
+    g_free (ctx->app_ctx);
+    ctx->app_ctx = g_strdup (application_context);
+}
+
+/**
+ * signon_security_context_get_application_context:
+ * @ctx: #SignonSecurityContext item.
+ *
+ * Get the application context part (such as script name or a web page) of
+ * the #SignonSecurityContext.
+ *
+ * Returns: (transfer none) application context.
+ */
+const gchar *
+signon_security_context_get_application_context (SignonSecurityContext *ctx)
+{
+    g_return_val_if_fail (ctx != NULL, NULL);
+
+    return ctx->app_ctx;
+}
+
+/**
+ * signon_security_context_build_variant:
+ * @list: #SignonSecurityContextList item.
+ *
+ * Builds a GVariant of type "a(ss)" from a GList of #SignonSecurityContext
+ * items.
+ *
+ * Returns: (transfer full) GVariant construct of a #SignonSecurityContextList.
+ */
+GVariant *
+signon_security_context_build_variant (const SignonSecurityContextList *list)
+{
+    GVariantBuilder *builder;
+    GVariant *variant;
+    SignonSecurityContext *ctx;
+
+    builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
+    for ( ; list != NULL; list = g_list_next (list))
+    {
+        ctx = (SignonSecurityContext *) list->data;
+        g_variant_builder_add (builder, "(ss)", ctx->sys_ctx, ctx->app_ctx);
+    }
+    variant = g_variant_builder_end(builder);
+    return variant;
+}
+
+/**
+ * signon_security_context_deconstruct_variant:
+ * @variant: GVariant item with a list of security context tuples
+ *
+ * Builds a GList of #SignonSecurityContext items from a GVariant of type
+ * "a(ss)".
+ *
+ * Returns: (transfer full) #SignonSecurityContextList item.
+ */
+SignonSecurityContextList *
+signon_security_context_deconstruct_variant (GVariant *variant)
+{
+    SignonSecurityContextList *list = NULL;
+    GVariantIter iter;
+    gchar *sys_ctx;
+    gchar *app_ctx;
+
+    g_return_val_if_fail (variant != NULL, NULL);
+
+    g_variant_iter_init (&iter, variant);
+    while (g_variant_iter_next (&iter, "(ss)", &sys_ctx, &app_ctx))
+    {
+        list = g_list_append (
+            list, signon_security_context_new_from_values (sys_ctx, app_ctx));
+    }
+
+    return list;
+}
+
+/**
+ * signon_security_context_list_copy:
+ * @src_list: source #SignonSecurityContextList.
+ *
+ * Copies a GList of #SignonSecurityContext items.
+ *
+ * Returns: (transfer full) #SignonSecurityContextList item.
+ */
+SignonSecurityContextList *
+signon_security_context_list_copy (const SignonSecurityContextList *src_list)
+{
+    SignonSecurityContext *ctx;
+    SignonSecurityContextList *dst_list = NULL;
+
+    for ( ; src_list != NULL; src_list = g_list_next (src_list))
+    {
+        ctx = (SignonSecurityContext *) src_list->data;
+        dst_list = g_list_append (
+            dst_list, signon_security_context_copy (ctx));
+    }
+
+    return dst_list;
+}
+
+/**
+ * signon_security_context_list_free:
+ * @seclist: (transfer full) #SignonSecurityContextList item.
+ *
+ * Frees all items and the GList of #SignonSecurityContext.
+ */
+void
+signon_security_context_list_free (SignonSecurityContextList *seclist)
+{
+    g_list_free_full (seclist, _security_context_free);
+}
+
diff --git a/src/daemon/db/gsignond-db-error.c b/src/daemon/db/gsignond-db-error.c
new file mode 100644 (file)
index 0000000..02b4bd8
--- /dev/null
@@ -0,0 +1,45 @@
+/* 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 signon
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@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 "gsignond-db-error.h"
+
+#define GSIGNOND_DB_ERROR_DOMAIN_STR   "gsignond_db"
+
+GQuark
+gsignond_db_error_quark (void)
+{
+    static gsize quark = 0;
+
+    if (g_once_init_enter (&quark)) {
+        GQuark domain = g_quark_from_static_string (
+                                                  GSIGNOND_DB_ERROR_DOMAIN_STR);
+        g_assert (sizeof (GQuark) <= sizeof (gsize));
+
+        g_once_init_leave (&quark, domain);
+    }
+
+    return (GQuark) quark;
+}
+
diff --git a/src/daemon/db/gsignond-db-error.h b/src/daemon/db/gsignond-db-error.h
new file mode 100644 (file)
index 0000000..271943c
--- /dev/null
@@ -0,0 +1,52 @@
+/* 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 signon
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@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
+ */
+
+/* inclusion guard */
+#ifndef __GSIGNOND_DB_ERROR_H__
+#define __GSIGNOND_DB_ERROR_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+/**
+ * GSIGNOND_DB_ERROR:
+ *
+ */
+#define GSIGNOND_DB_ERROR   (gsignond_db_error_quark())
+
+typedef enum {
+    GSIGNOND_DB_ERROR_NONE,
+    GSIGNOND_DB_ERROR_NOT_OPEN,             /*!< The DB is not open */
+    GSIGNOND_DB_ERROR_CONNECTION_FAILURE,   /*!< The DB is disconnected */
+    GSIGNOND_DB_ERROR_STATEMENT_FAILURE,    /*!< The last command failed */
+    GSIGNOND_DB_ERROR_UNKNOWN
+} GSignondDbError;
+
+GQuark gsignond_db_error_quark (void);
+
+G_END_DECLS
+
+#endif /* __GSIGNOND_DB_ERROR_H__ */
diff --git a/src/daemon/gsignond-error.c b/src/daemon/gsignond-error.c
new file mode 100644 (file)
index 0000000..0f9f264
--- /dev/null
@@ -0,0 +1,62 @@
+/* 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 signon
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@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 "gsignond-error.h"
+
+GQuark
+gsignond_error_quark (void)
+{
+    static GQuark quark = 0;
+    if (quark == 0) {
+        quark = g_quark_from_static_string (G_LOG_DOMAIN);
+    }
+    return quark;
+}
+
+GString*
+gsignond_concat_domain_and_error (const gchar *str1,
+                                  const gchar *str2)
+{
+    GString *str;
+    g_return_val_if_fail (str1 != NULL && str2 != NULL, NULL);
+    str = g_string_sized_new (strlen(str1)+strlen(str2)-1);
+    g_string_printf (str,"[%s].%s\n",str1,str2);
+    return str;
+}
+
+GString*
+gsignond_prepend_domain_to_error_msg (const GError *err)
+{
+    GString *msg;
+    const gchar *domain;
+    g_return_val_if_fail (err != NULL, NULL);
+    if (err->message != NULL) {
+        domain = g_quark_to_string(err->domain);
+        msg = gsignond_concat_domain_and_error(domain, err->message);
+    }
+    return msg;
+}
+
diff --git a/src/daemon/gsignond-error.h b/src/daemon/gsignond-error.h
new file mode 100644 (file)
index 0000000..ac7e9d8
--- /dev/null
@@ -0,0 +1,73 @@
+/* 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 signon
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@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
+ */
+
+/* inclusion guard */
+#ifndef __GSIGNOND_ERROR_H__
+#define __GSIGNOND_ERROR_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define G_LOG_DOMAIN "gsignond"
+
+/**
+ * GSIGNOND_ERROR:
+ *
+ */
+#define GSIGNOND_ERROR   (gsignond_error_quark())
+
+typedef enum {
+    GSIGNOND_ERROR_NONE,
+    /* Add error codes */
+} GSignondError;
+
+#define gsignond_gerr(error, handler) \
+    G_STMT_START {                 \
+        GString* msg = gsignond_prepend_domain_to_error_msg(error); \
+        handler(msg->str); \
+        g_string_free(msg, TRUE); \
+    } G_STMT_END\
+
+#define gsignond_error_gerr(err)       gsignond_gerr(err, g_error)
+
+#define gsignond_critical_gerr(err)    gsignond_gerr(err, g_critical)
+
+#define gsignond_warning_gerr(err)     gsignond_gerr(err, g_warning)
+
+#define gsignond_message_gerr(err)     gsignond_gerr(err, g_message)
+
+#define gsignond_debug_gerr(err)       gsignond_gerr(err, g_debug)
+
+GQuark      gsignond_error_quark (void);
+
+GString*    gsignond_concat_domain_and_error (const gchar *str1,
+                                              const gchar *str2);
+
+GString*    gsignond_prepend_domain_to_error_msg (const GError *err);
+
+G_END_DECLS
+
+#endif /* __GSIGNOND_ERROR_H__ */