summaryrefslogtreecommitdiffstats
path: root/database/eurephiadb.c
blob: 63dded430f1e1d4b4e0fa02275a456ec29243e07 (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
/* eurephiadb.c  --  Loads and initialises the database driver
 *
 *  GPLv2 - Copyright (C) 2008  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; version 2
 *  of the License.
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <stdarg.h>

#include "eurephia_nullsafe.h"
#include "eurephiadb_driver.h"
#include "eurephia_log.h"
#include "eurephia_getsym.h"

#ifdef MEMWATCH
#include <memwatch.h>
#endif


int eDBlink_close(eurephiaCTX *ctx)
{
        if( ctx == NULL ) {
                return 1;
        }

        eurephia_log(ctx, LOG_INFO, 3, "Unloading eurephiaDB driver");
        if( ctx->eurephia_driver != NULL ) {
                dlclose(ctx->eurephia_driver);
                ctx->eurephia_driver = NULL;
        }
        return 1;
}


int eDBlink_init(eurephiaCTX *ctx, const char *dbl, const int minver)
{
#ifdef MEMWATCH
        mwStatistics(3);
#endif

        if( dbl == NULL ) {
                eurephia_log(ctx, LOG_FATAL, 0, "No eurephia eDBlink driver given.  "
                             "eurephia authentication will not be available");
                return 0;
        }
        eurephia_log(ctx, LOG_INFO, 2, "Loading eurephiaDB driver: %s", dbl);

        ctx->eurephia_driver = dlopen(dbl, RTLD_NOW);
        if( ctx->eurephia_driver == NULL ) {
                eurephia_log(ctx, LOG_FATAL, 0, "Could not open the eurephia eDBlink driver (%s)", dbl);
                eurephia_log(ctx, LOG_FATAL, 1, "dlopen error: %s", dlerror());
                return 0;
        }

        // Find mandatory functions containing driver information
        eDB_DriverVersion = eGetSym(ctx, ctx->eurephia_driver, "eDB_DriverVersion");
        eDB_DriverAPIVersion = eGetSym(ctx, ctx->eurephia_driver, "eDB_DriverAPIVersion");

        eurephia_log(ctx, LOG_INFO, 1, "Driver loaded: %s (API version %i)",
                     eDB_DriverVersion(), eDB_DriverAPIVersion());

        // Check that we are using at least a new enough driver
        if( eDB_DriverAPIVersion() < minver ) {
                eurephia_log(ctx, LOG_FATAL, 0,
                             "The requested eurephiaDB driver is too old.  This program needs "
                             "API version %i, but this driver only supports API version %i.\n",
                             minver, eDB_DriverAPIVersion());
                return 0;
        }

        // Configure functions contained in the driver, defined by API version
        switch( (eDB_DriverAPIVersion() > minver ? minver : eDB_DriverAPIVersion()) ) {
        default:
                eurephia_log(ctx, LOG_WARNING, 0,
                             "eurephiaDB driver API is newer than the running eurephia version.  Consider "
                             "to upgrade eurphia to take advantage of newer features in the eurephiaDB driver.");
        case 2:
                eDBadminAuth                   = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAuth");
                eDBadminValidateSession        = eGetSym(ctx, ctx->eurephia_driver, "eDBadminValidateSession");
                eDBadminRegisterLogin          = eGetSym(ctx, ctx->eurephia_driver, "eDBadminRegisterLogin");
                eDBadminLogout                 = eGetSym(ctx, ctx->eurephia_driver, "eDBadminLogout");

                eDBadminConfigSet              = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfigSet");
                eDBadminConfigDelete           = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfigDelete");

                eDBadminGetUserList            = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserList");
                eDBadminGetUserInfo            = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserInfo");
                eDBadminAddUser                = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAddUser");
                eDBadminUpdateUser             = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUpdateUser");
                eDBadminDeleteUser             = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteUser");

                eDBadminGetCertificateList     = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetCertificateList");
                eDBadminGetCertificateInfo     = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetCertificateInfo");
                eDBadminAddCertificate         = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAddCertificate");
                eDBadminDeleteCertificate      = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteCertificate");

                eDBadminGetUserCertsList       = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserCertsList");
                eDBadminUpdateUserCertLink     = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUpdateUserCertLink");

                eDBadminGetLastlog             = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetLastlog");
                eDBadminGetAttemptsLog         = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetAttemptsLog");

        case 1:
                // Setup eDBlink functions
                eDBconnect                     = eGetSym(ctx, ctx->eurephia_driver, "eDBconnect");
                eDBdisconnect                  = eGetSym(ctx, ctx->eurephia_driver, "eDBdisconnect");

                eDBauth_TLS                    = eGetSym(ctx, ctx->eurephia_driver, "eDBauth_TLS");

                eDBauth_user                   = eGetSym(ctx, ctx->eurephia_driver, "eDBauth_user");
                eDBget_uid                     = eGetSym(ctx, ctx->eurephia_driver, "eDBget_uid");

                eDBblacklist_check             = eGetSym(ctx, ctx->eurephia_driver, "eDBblacklist_check");
                eDBregister_attempt            = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_attempt");

                eDBregister_login              = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_login");
                eDBregister_vpnmacaddr         = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_vpnmacaddr");
                eDBregister_logout             = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_logout");

                eDBget_firewall_profile        = eGetSym(ctx, ctx->eurephia_driver, "eDBget_firewall_profile");
                eDBget_blacklisted_ip          = eGetSym(ctx, ctx->eurephia_driver, "eDBget_blacklisted_ip");

                eDBget_sessionkey_seed         = eGetSym(ctx, ctx->eurephia_driver, "eDBget_sessionkey_seed");
                eDBget_sessionkey_macaddr      = eGetSym(ctx, ctx->eurephia_driver, "eDBget_sessionkey_macaddr");
                eDBcheck_sessionkey_uniqueness = eGetSym(ctx, ctx->eurephia_driver,
                                                         "eDBcheck_sessionkey_uniqueness");

                eDBregister_sessionkey         = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_sessionkey");
                eDBload_sessiondata            = eGetSym(ctx, ctx->eurephia_driver, "eDBload_sessiondata");
                eDBstore_session_value         = eGetSym(ctx, ctx->eurephia_driver, "eDBstore_session_value");
                eDBdestroy_session             = eGetSym(ctx, ctx->eurephia_driver, "eDBdestroy_session");
                break;
        }
        if( ctx->fatal_error > 0 ) {
                eurephia_log(ctx, LOG_FATAL, 0, "eurephia eDBlink is not correctly initialised.  "
                             "eurephia authentication will not be available");
                eDBlink_close(ctx);
                return 0;
        }
        return 1;
}