summaryrefslogtreecommitdiffstats
path: root/auth/eurephia_authplugin.c
blob: f459e7b39662297da6d3380f90c9d92d8dd90d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*  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 <stdlib.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) {
                                ePluginInfo *plginfo = apctx->plugin->PluginInfo();

                                eurephia_log(ctx, LOG_INFO, 1,
                                             "Loaded eurephia %s plugin: %s (v%s) %s",
                                             ePluginTypeString(plginfo),
                                             plginfo->name, plginfo->version, plginfo->copyright);
                                // 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;
        }
}