| 1 |
/* eurephiadb.c -- Loads and initialises the database driver |
| 2 |
* |
| 3 |
* GPLv2 only - Copyright (C) 2008 - 2012 |
| 4 |
* David Sommerseth <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; version 2 |
| 9 |
* of the License. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* @file eurephiadb.c |
| 24 |
* @author David Sommerseth <[email protected]> |
| 25 |
* @date 2008-08-06 |
| 26 |
* |
| 27 |
* @brief Handles loading and initialising of the database drivers |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <dlfcn.h> |
| 33 |
|
| 34 |
#include "eurephia_nullsafe.h" |
| 35 |
#include "eurephiadb_driver.h" |
| 36 |
#include "eurephia_log.h" |
| 37 |
#include "eurephia_getsym.h" |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Unloads the database driver |
| 42 |
* |
| 43 |
* @param ctx eurephiaCTX |
| 44 |
* |
| 45 |
* @return Returns always 1. |
| 46 |
*/ |
| 47 |
int eDBlink_close(eurephiaCTX *ctx) |
| 48 |
{ |
| 49 |
if( ctx == NULL ) { |
| 50 |
return 1; |
| 51 |
} |
| 52 |
|
| 53 |
eurephia_log(ctx, LOG_INFO, 3, "Unloading eurephia database driver"); |
| 54 |
if( ctx->eurephia_driver != NULL ) { |
| 55 |
dlclose(ctx->eurephia_driver); |
| 56 |
ctx->eurephia_driver = NULL; |
| 57 |
} |
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Loads and initialises the database driver. |
| 64 |
* |
| 65 |
* @param ctx eurephiaCTX to which the database driver will be associated with |
| 66 |
* @param dbdriver Full path to the driver file (.so) |
| 67 |
* @param minver The required minimum API level of the driver |
| 68 |
* |
| 69 |
* @return Returns 1 on success, otherwise 0. |
| 70 |
*/ |
| 71 |
int eDBlink_init(eurephiaCTX *ctx, const char *dbdriver, const int minver) |
| 72 |
{ |
| 73 |
int apiver = -1; |
| 74 |
if( dbdriver == NULL ) { |
| 75 |
eurephia_log(ctx, LOG_FATAL, 0, "No eurephia database driver configured. " |
| 76 |
"eurephia authentication will not be available"); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
eurephia_log(ctx, LOG_INFO, 2, "Loading eurephia database driver: %s", dbdriver); |
| 80 |
|
| 81 |
ctx->eurephia_driver = dlopen(dbdriver, RTLD_NOW); |
| 82 |
if( ctx->eurephia_driver == NULL ) { |
| 83 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not open the eurephia database driver (%s)", dbdriver); |
| 84 |
eurephia_log(ctx, LOG_FATAL, 1, "dlopen error: %s", dlerror()); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
// Find mandatory functions containing driver information |
| 89 |
eDB_DriverVersion = eGetSym(ctx, ctx->eurephia_driver, "eDB_DriverVersion"); |
| 90 |
eDB_DriverAPIVersion = eGetSym(ctx, ctx->eurephia_driver, "eDB_DriverAPIVersion"); |
| 91 |
|
| 92 |
eurephia_log(ctx, LOG_INFO, 1, "Driver loaded: %s (API version %i)", |
| 93 |
eDB_DriverVersion(), eDB_DriverAPIVersion()); |
| 94 |
|
| 95 |
// Check that we are using at least a new enough driver |
| 96 |
if( eDB_DriverAPIVersion() < minver ) { |
| 97 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 98 |
"The requested eurephia database driver is too old. This program needs " |
| 99 |
"API version %i, but this driver only supports API version %i.\n", |
| 100 |
minver, eDB_DriverAPIVersion()); |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
// Configure functions contained in the driver, defined by API version |
| 105 |
apiver = (eDB_DriverAPIVersion() > minver ? minver : eDB_DriverAPIVersion()); |
| 106 |
switch( apiver ) { |
| 107 |
case -1: |
| 108 |
eurephia_log(ctx, LOG_FATAL, 0, "Something unexpected happened - apiver==-1"); |
| 109 |
ctx->fatal_error = 1; |
| 110 |
break; |
| 111 |
|
| 112 |
default: |
| 113 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 114 |
"eurephia database driver API is newer than the running eurephia version. Consider " |
| 115 |
"to upgrade eurephia to take advantage of newer features in the driver."); |
| 116 |
case 3: |
| 117 |
eDBregister_vpnclientaddr = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_vpnclientaddr"); |
| 118 |
|
| 119 |
case 2: |
| 120 |
#ifdef ENABLE_EUREPHIADM |
| 121 |
if( (ctx->context_type != ECTX_NO_PRIVILEGES) && (ctx->context_type != ECTX_PLUGIN_AUTH) ) { |
| 122 |
// These functions are only available in admin context |
| 123 |
eDBadminAuthenticate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAuthenticate"); |
| 124 |
eDBadminConfiguration = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfiguration"); |
| 125 |
eDBadminUserAccount = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUserAccount"); |
| 126 |
eDBadminCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminCertificate"); |
| 127 |
eDBadminUserCertsLink = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUserCertsLink"); |
| 128 |
eDBadminAccessLevel = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAccessLevel"); |
| 129 |
eDBadminFirewallProfiles = eGetSym(ctx, ctx->eurephia_driver, |
| 130 |
"eDBadminFirewallProfiles"); |
| 131 |
eDBadminGetLastlog = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetLastlog"); |
| 132 |
eDBadminAttemptsLog = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAttemptsLog"); |
| 133 |
eDBadminBlacklist = eGetSym(ctx, ctx->eurephia_driver, "eDBadminBlacklist"); |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
case 1: |
| 138 |
// Setup eDBlink functions |
| 139 |
eDBconnect = eGetSym(ctx, ctx->eurephia_driver, "eDBconnect"); |
| 140 |
eDBdisconnect = eGetSym(ctx, ctx->eurephia_driver, "eDBdisconnect"); |
| 141 |
|
| 142 |
eDBauth_TLS = eGetSym(ctx, ctx->eurephia_driver, "eDBauth_TLS"); |
| 143 |
|
| 144 |
eDBauth_user = eGetSym(ctx, ctx->eurephia_driver, "eDBauth_user"); |
| 145 |
eDBget_uid = eGetSym(ctx, ctx->eurephia_driver, "eDBget_uid"); |
| 146 |
|
| 147 |
eDBblacklist_check = eGetSym(ctx, ctx->eurephia_driver, "eDBblacklist_check"); |
| 148 |
eDBregister_attempt = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_attempt"); |
| 149 |
|
| 150 |
eDBregister_login = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_login"); |
| 151 |
|
| 152 |
// If api version is 3, this function is replaced by eDBregister_vpnclientaddr() |
| 153 |
eDBregister_vpnmacaddr = (apiver < 3 ? |
| 154 |
eGetSym(ctx, ctx->eurephia_driver, "eDBregister_vpnmacaddr") : NULL); |
| 155 |
eDBregister_logout = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_logout"); |
| 156 |
|
| 157 |
eDBget_firewall_profile = eGetSym(ctx, ctx->eurephia_driver, "eDBget_firewall_profile"); |
| 158 |
eDBget_blacklisted_ip = eGetSym(ctx, ctx->eurephia_driver, "eDBget_blacklisted_ip"); |
| 159 |
|
| 160 |
eDBget_sessionkey_seed = eGetSym(ctx, ctx->eurephia_driver, "eDBget_sessionkey_seed"); |
| 161 |
eDBget_sessionkey_macaddr = eGetSym(ctx, ctx->eurephia_driver, "eDBget_sessionkey_macaddr"); |
| 162 |
eDBcheck_sessionkey_uniqueness = eGetSym(ctx, ctx->eurephia_driver, |
| 163 |
"eDBcheck_sessionkey_uniqueness"); |
| 164 |
|
| 165 |
eDBregister_sessionkey = eGetSym(ctx, ctx->eurephia_driver, "eDBregister_sessionkey"); |
| 166 |
eDBload_sessiondata = eGetSym(ctx, ctx->eurephia_driver, "eDBload_sessiondata"); |
| 167 |
eDBstore_session_value = eGetSym(ctx, ctx->eurephia_driver, "eDBstore_session_value"); |
| 168 |
eDBdestroy_session = eGetSym(ctx, ctx->eurephia_driver, "eDBdestroy_session"); |
| 169 |
break; |
| 170 |
} |
| 171 |
if( ctx->fatal_error > 0 ) { |
| 172 |
eurephia_log(ctx, LOG_FATAL, 0, "The eurephia database driver is not correctly initialised. " |
| 173 |
"eurephia authentication will not be available"); |
| 174 |
eDBlink_close(ctx); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
return 1; |
| 178 |
} |