summaryrefslogtreecommitdiffstats
path: root/auth/eurephia_authplugin.c
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2013-03-03 00:46:23 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2013-03-03 00:58:09 +0100
commit25e5147c7d3e16ec96713c214dc28e398b3be10c (patch)
treeb43c9612580f7070608a8848727cc5a358f0a5a8 /auth/eurephia_authplugin.c
parent262442599d5352f8c48018d975b7f50a53dab33c (diff)
downloadeurephia-25e5147c7d3e16ec96713c214dc28e398b3be10c.tar.gz
eurephia-25e5147c7d3e16ec96713c214dc28e398b3be10c.tar.xz
eurephia-25e5147c7d3e16ec96713c214dc28e398b3be10c.zip
Added the first stab of an authentication plug-in framework
This enables a run-time loadable support for other authentication modules. This can be used to make eurephia authenticate user's passwords against other sources than the local eurephia database itself. Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
Diffstat (limited to 'auth/eurephia_authplugin.c')
-rw-r--r--auth/eurephia_authplugin.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/auth/eurephia_authplugin.c b/auth/eurephia_authplugin.c
new file mode 100644
index 0000000..996e6ba
--- /dev/null
+++ b/auth/eurephia_authplugin.c
@@ -0,0 +1,162 @@
+/* eurephia_authplugin.c
+ *
+ * Copyright (C) 2013 David Sommerseth <dazo@users.sourceforge.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/**
+ * @file eurephia_authplugin.c
+ * @author David Sommerseth <dazo@users.sourceforge.net>
+ * @date 2013-02-15
+ *
+ * @brief Authentication plug-in framework for eurephia
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <dlfcn.h>
+
+#define EUREPHIA_AUTHPLUGIN_C_
+#define EUREPHIA_CONTEXT_H_
+typedef struct __eurephiaCTX eurephiaCTX;
+
+#include <eurephia_authplugin_driver.h>
+#include <eurephia_authplugin_context.h>
+#include <eurephia_nullsafe.h>
+#include <eurephia_values_struct.h>
+#include <eurephia_values.h>
+#include <eurephia_log.h>
+#include <eurephiadb_driver.h>
+#include <eurephia_getsym.h>
+
+eAuthPlugin * __eAuthPlugin_Setup(eurephiaCTX * ctx, const char * dlfilename, void * dlhandle);
+
+
+eAuthPluginCTX * eAuthPlugin_Init(eurephiaCTX * ctx)
+{
+ eurephiaVALUES *plgs = NULL, *plgptr;
+ eAuthPluginCTX *apctx = NULL, *apctxhead = NULL;
+
+ // Query the database for authentication plugins
+ plgs = eDBget_plugins(ctx, "auth");
+ if (plgs == NULL) {
+ return NULL;
+ }
+
+ // Parse the result, load plugins and save them into
+ // separate auth plug-in contexts
+ for (plgptr = plgs; plgptr != NULL; plgptr = plgptr->next) {
+ // Ensure that we have value plug-in information
+ if (plgptr == NULL || plgptr->key == NULL || plgptr->val == NULL) {
+ continue;
+ }
+
+ // Create a plug-in context
+ apctx = malloc_nullsafe(ctx, sizeof(eAuthPluginCTX)+2);
+
+ // Save some metadata about this plugin
+ apctx->filename = strdup(plgptr->val);
+ apctx->plgid = atoi_nullsafe(plgptr->key);
+ apctx->next = NULL;
+ // Open the plugin
+ apctx->dlhandle = dlopen(apctx->filename, RTLD_NOW);
+ if (apctx->dlhandle != NULL) {
+ // If we got a handle to the plug-in, it up
+ apctx->plugin = __eAuthPlugin_Setup(ctx, apctx->filename, apctx->dlhandle);
+ if (apctx->plugin != NULL) {
+ eurephia_log(ctx, LOG_INFO, 1,
+ "Loaded eurephia plugin %s",
+ apctx->plugin->PluginInfo());
+ // On success, add this plug-in to the context chain
+ if( apctxhead != NULL ) {
+ apctx->next = apctxhead;
+ }
+ apctxhead = apctx;
+ } else {
+ // On failure to setup the plug-in, disable it
+ eurephia_log(ctx, LOG_FATAL, 0,
+ "Failed to initialse eurephia plugin %s. "
+ "Plug-in disabled.",
+ apctx->filename);
+ free_nullsafe(ctx, apctx->filename);
+ dlclose(apctx->dlhandle);
+ apctx->plgid = 0;
+ apctx->next = NULL;
+ free(apctx);
+ }
+ } else {
+ // On failure to load the plug-in ... Report error and ignore plug-in.
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not load eurephia plugin %s",
+ apctx->filename);
+ eurephia_log(ctx, LOG_FATAL, 1, "dlopen error: %s", dlerror());
+ free_nullsafe(ctx, apctx->filename);
+ apctx->plgid = 0;
+ apctx->next = NULL;
+ free(apctx);
+ }
+ }
+ eFree_values(ctx, plgs);
+
+ // Return the plug-in context chain
+ return apctxhead;
+}
+
+
+eAuthPlugin * eAuthPlugin_Get(eAuthPluginCTX *apctx, const int plgid)
+{
+ eAuthPluginCTX *ptr = apctx;
+
+ if (apctx == NULL) {
+ return NULL;
+ }
+
+ while (ptr) {
+ // Return a pointer to the authentication plug-in functions
+ // if found
+ if (ptr->plgid == plgid) {
+ return ptr->plugin;
+ }
+ ptr = ptr->next;
+ }
+ return NULL;
+}
+
+
+void _eAuthPlugin_Close(eurephiaCTX *ctx, eAuthPluginCTX *apctx)
+{
+ eAuthPluginCTX *ptr = apctx, *next = NULL;
+
+ while (ptr) {
+ // Save a pointe to the next plug-in context
+ next = ptr->next;
+ ptr->next = NULL;
+
+ // Close the plug-in handle
+ if (ptr->dlhandle) {
+ dlclose(ptr->dlhandle);
+ }
+
+ // Clean up memory allocated by the plug-in context
+ free_nullsafe(ctx, ptr->filename);
+ ptr->plgid = 0;
+ free_nullsafe(ctx, ptr->plugin);
+ free_nullsafe(ctx, ptr);
+
+ // Go to next plug-in context
+ ptr = next;
+ }
+}
+