summaryrefslogtreecommitdiffstats
path: root/database/sqlite/edb-sqlite.c
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2011-01-09 23:39:08 +0100
committerDavid Sommerseth <davids@redhat.com>2011-12-19 11:05:38 +0100
commit8d2f8d68e6ae9726fdd2d941e55a7377e3cdf927 (patch)
treea778cb9ee6f06b41f256a22450af8fd7916a3ed8 /database/sqlite/edb-sqlite.c
parentf0434a3ad51bf1159a78003a020eeb82a26dfc7f (diff)
downloadeurephia-8d2f8d68e6ae9726fdd2d941e55a7377e3cdf927.tar.gz
eurephia-8d2f8d68e6ae9726fdd2d941e55a7377e3cdf927.tar.xz
eurephia-8d2f8d68e6ae9726fdd2d941e55a7377e3cdf927.zip
Modified the whole edb-sqlite driver to use a better error handling
This will change the driver to use the new error routines made available in the SQLite3 framework. Some of the code is also restructured a little bit to simplify the code with these changes. The functionality should be the same as for, but better error messages are now sent back to the caller on the functions supporting XML. Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
Diffstat (limited to 'database/sqlite/edb-sqlite.c')
-rw-r--r--database/sqlite/edb-sqlite.c226
1 files changed, 140 insertions, 86 deletions
diff --git a/database/sqlite/edb-sqlite.c b/database/sqlite/edb-sqlite.c
index 1727eb3..d9d4af6 100644
--- a/database/sqlite/edb-sqlite.c
+++ b/database/sqlite/edb-sqlite.c
@@ -37,7 +37,7 @@
#include <unistd.h>
#include <assert.h>
-#define DRIVERVERSION "1.1" /**< Defines the software version of this driver */
+#define DRIVERVERSION "1.2" /**< Defines the software version of this driver */
#ifndef DRIVERAPIVERSION
# define DRIVERAPIVERSION 2 /**< Sets the API version level of this driver */
#endif
@@ -102,7 +102,6 @@ int eDB_DriverAPIVersion() {
* local functions
*/
-
/**
* Internal driver function for simplifying update of openvpn_blacklist. It will simply just
* update the 'last_accessed' field in the blacklist table.
@@ -117,9 +116,10 @@ void update_attempts(eurephiaCTX *ctx, const char *blid) {
res = sqlite_query(ctx,
"UPDATE openvpn_blacklist "
" SET last_accessed = CURRENT_TIMESTAMP WHERE blid = %q", blid);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not update openvpn_blacklist.last_accessed for blid=%s", blid);
+ sqlite_log_error(ctx, res);
}
sqlite_free_results(res);
}
@@ -167,7 +167,7 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
// Load configuration parameters into memory
eurephia_log(ctx, LOG_INFO, 1, "Reading config from database (openvpn_config)");
res = sqlite_query(ctx, "SELECT datakey, dataval FROM openvpn_config");
- if( res != NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
int i = 0;
eurephiaVALUES *cfg = NULL;
@@ -180,9 +180,11 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
eAdd_value(ctx, cfg, sqlite_get_value(res, i, 0), sqlite_get_value(res, i, 1));
}
- sqlite_free_results(res);
ctx->dbc->config = cfg;
- }
+ } else {
+ sqlite_log_error(ctx, res);
+ }
+ sqlite_free_results(res);
return 1;
}
@@ -238,10 +240,9 @@ int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char
" AND email='%q' AND depth='%i' AND lower(cert.digest)=lower('%q')%c",
org, cname, email, depth, digest, 0);
- if( res != NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
certid = atoi_nullsafe(sqlite_get_value(res, 0, 0));
blid = strdup_nullsafe(sqlite_get_value(res, 0, 1));
- sqlite_free_results(res);
// Check if the certificate is blacklisted or not. blid != NULL when blacklisted
if( blid != NULL ) {
@@ -254,7 +255,9 @@ int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char
free_nullsafe(ctx, blid);
} else {
eurephia_log(ctx, LOG_FATAL, 0, "Could not look up certificate information");
+ sqlite_log_error(ctx, res);
}
+ sqlite_free_results(res);
DEBUG(ctx, 20, "Result function call: eDBauth_TLS(ctx, '%s', '%s', '%s', '%s', %i) - %i",
org, cname, email, digest, depth, certid);
@@ -289,9 +292,11 @@ int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const
certid, username);
memset(crpwd, 0, strlen_nullsafe(crpwd));
free_nullsafe(ctx, crpwd);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not lookup user in database (certid %i, username '%s'", certid, username);
+ sqlite_log_error(ctx, res);
+ sqlite_free_results(res);
return 0;
}
@@ -347,12 +352,12 @@ int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const
upd = sqlite_query(ctx,
"UPDATE openvpn_users SET last_accessed = CURRENT_TIMESTAMP"
" WHERE uid = %i", uid);
- if( upd == NULL ) {
+ if( sqlite_query_status(upd) != dbSUCCESS) {
eurephia_log(ctx, LOG_ERROR, 0,
"Could not update last access status for uid %i", uid);
- } else {
- sqlite_free_results(upd);
+ sqlite_log_error(ctx, upd);
}
+ sqlite_free_results(upd);
}
} else {
eurephia_log(ctx, LOG_WARNING, 0, "Authentication failed for user '%s'. "
@@ -384,8 +389,11 @@ int eDBget_uid(eurephiaCTX *ctx, const int certid, const char *username)
" JOIN openvpn_users USING (uid) "
" WHERE certid = '%i' AND username = '%q'",
certid, username);
- if( (res == NULL) || (sqlite_get_numtuples(res) != 1) ) {
+ if( (sqlite_query_status(res) != dbSUCCESS) || (sqlite_get_numtuples(res) != 1) ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not lookup userid for user '%s'", username);
+ if( sqlite_query_status(res) == dbERROR ) {
+ sqlite_log_error(ctx, res);
+ }
ret = -1;
} else {
ret = atoi_nullsafe(sqlite_get_value(res, 0, 0));
@@ -414,10 +422,8 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
(strlen_nullsafe(eDBattempt_types[type].value_func) > 0 ? "(" : ""),
val,
(strlen_nullsafe(eDBattempt_types[type].value_func) > 0 ? ")" : ""));
- if( blr != NULL ) {
+ if( sqlite_query_status(blr) == dbSUCCESS ) {
blid = strdup_nullsafe(sqlite_get_value(blr, 0, 0));
- sqlite_free_results(blr);
- blr = NULL;
if( blid != NULL ) {
eurephia_log(ctx, LOG_WARNING, 0, "Attempt from blacklisted %s: %s",
@@ -429,7 +435,9 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
} else {
eurephia_log(ctx, LOG_FATAL, 0, "Querying openvpn_blacklist for blacklisted %s failed",
eDBattempt_types[type].descr);
+ sqlite_log_error(ctx, blr);
}
+ sqlite_free_results(blr);
if( blacklisted == 0 ) {
// Check if this [type] has been attempted earlier - if it has reaced the maximum
@@ -439,11 +447,10 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
defaultValue(eGet_value(ctx->dbc->config, eDBattempt_types[type].allow_cfg),
eDBattempt_types[type].default_value),
eDBattempt_types[type].colname_where, val);
- if( atpr != NULL ) {
+
+ if( sqlite_query_status(atpr) == dbSUCCESS ) {
atpid = strdup_nullsafe(sqlite_get_value(atpr, 0, 0));
atpexceed = atoi_nullsafe(sqlite_get_value(atpr, 0, 1));
- sqlite_free_results(atpr);
- atpr = NULL;
// If [type] has reached attempt limit and it is not black listed, black list it
if( (atpexceed > 0) && (blid == NULL) ) {
@@ -453,20 +460,22 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
blr = sqlite_query(ctx,
"INSERT INTO openvpn_blacklist (%s) VALUES ('%q')",
eDBattempt_types[type].colname, val);
- if( blr == NULL ) {
+ if( sqlite_query_status(blr) != dbSUCCESS ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not blacklist %s (%s)",
eDBattempt_types[type].descr, val);
+ sqlite_log_error(ctx, blr);
}
- sqlite_free_results(blr);
blacklisted = 1; // [type] is blacklisted
+ sqlite_free_results(blr);
}
free_nullsafe(ctx, atpid);
} else {
eurephia_log(ctx, LOG_CRITICAL, 0, "Querying openvpn_attempts for blacklisted %s failed",
eDBattempt_types[type].descr);
+ sqlite_log_error(ctx, blr);
}
- free_nullsafe(ctx, atpr);
+ sqlite_free_results(atpr);
}
free_nullsafe(ctx, blid);
@@ -506,8 +515,10 @@ void eDBregister_attempt(eurephiaCTX *ctx, int type, int mode, const char *value
value,
(strlen_nullsafe(eDBattempt_types[type].value_func) > 0 ? ")" : "")
);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not look up atpid in openvpn_attempts");
+ sqlite_log_error(ctx, res);
+ sqlite_free_results(res);
return;
}
@@ -545,10 +556,11 @@ void eDBregister_attempt(eurephiaCTX *ctx, int type, int mode, const char *value
break;
}
}
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not update openvpn_attempts for %s = %s",
eDBattempt_types[type].colname, value);
+ sqlite_log_error(ctx, res);
}
sqlite_free_results(res);
@@ -559,9 +571,10 @@ void eDBregister_attempt(eurephiaCTX *ctx, int type, int mode, const char *value
eDBattempt_types[type].descr, value);
res = sqlite_query(ctx, "INSERT INTO openvpn_blacklist (%s) VALUES ('%q')",
eDBattempt_types[type].colname, value);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not blacklist %s: %s", eDBattempt_types[type].descr, value);
+ sqlite_log_error(ctx, res);
}
sqlite_free_results(res);
}
@@ -579,6 +592,7 @@ int eDBregister_login(eurephiaCTX *ctx, eurephiaSESSION *skey, const int certid,
const char *vpnipaddr, const char *vpnipmask)
{
dbresult *res = NULL;
+ int ret = 0;
DEBUG(ctx, 20, "Function call: eDBregister_login(ctx, '%s', %i, %i, '%s','%s','%s','%s','%s')",
skey->sessionkey, certid, uid, proto, remipaddr, remport, vpnipaddr, vpnipmask);
@@ -595,13 +609,17 @@ int eDBregister_login(eurephiaCTX *ctx, eurephiaSESSION *skey, const int certid,
" sessionstatus, sessionkey, login) "
"VALUES (%i, %i, '%q','%q','%q','%q','%q', 1,'%q', CURRENT_TIMESTAMP)",
uid, certid, proto, remipaddr, remport, vpnipaddr, vpnipmask, skey->sessionkey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not insert new session into openvpn_lastlog");
- return 0;
+ sqlite_log_error(ctx, res);
+ ret = 0;
+ } else {
+ skey->sessionstatus = SESSION_REGISTERED;
+ ret = 1;
}
sqlite_free_results(res);
- skey->sessionstatus = SESSION_REGISTERED;
- return 1;
+
+ return ret;
}
/**
@@ -610,6 +628,7 @@ int eDBregister_login(eurephiaCTX *ctx, eurephiaSESSION *skey, const int certid,
int eDBregister_vpnmacaddr(eurephiaCTX *ctx, eurephiaSESSION *session, const char *macaddr)
{
dbresult *res = NULL;
+ int ret = 0;
DEBUG(ctx, 20, "Function call: eDBregister_vpnmacaddr(ctx, '%s', '%s')",
session->sessionkey, macaddr);
@@ -622,9 +641,11 @@ int eDBregister_vpnmacaddr(eurephiaCTX *ctx, eurephiaSESSION *session, const cha
// Register MAC address into history table
res = sqlite_query(ctx, "INSERT INTO openvpn_macaddr_history (sessionkey, macaddr) VALUES ('%q','%q')",
session->sessionkey, macaddr);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0, "Failed to log new MAC address for session");
- return 0;
+ sqlite_log_error(ctx, res);
+ ret = 0;
+ goto exit;
}
sqlite_free_results(res);
@@ -632,20 +653,23 @@ int eDBregister_vpnmacaddr(eurephiaCTX *ctx, eurephiaSESSION *session, const cha
res = sqlite_query(ctx,
"UPDATE openvpn_lastlog SET sessionstatus = 2, macaddr = '%q' "
" WHERE sessionkey = '%q' AND sessionstatus = 1", macaddr, session->sessionkey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ // Save the MAC address in the session values register - needed for the destroy session
+ if( eDBset_session_value(ctx, session, "macaddr", macaddr) == 0 ) {
+ eurephia_log(ctx, LOG_FATAL, 0, "Could not save MAC address into session variables");
+ ret = 0;
+ } else {
+ ret = 1;
+ }
+ } else {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update lastlog with new MAC address for session");
- return 0;
-
- }
- sqlite_free_results(res);
-
- // Save the MAC address in the session values register - needed for the destroy session
- if( eDBset_session_value(ctx, session, "macaddr", macaddr) == 0 ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Could not save MAC address into session variables");
- return 0;
+ sqlite_log_error(ctx, res);
+ ret = 0;
}
- return 1;
+ exit:
+ sqlite_free_results(res);
+ return ret;
}
@@ -656,6 +680,7 @@ int eDBregister_logout(eurephiaCTX *ctx, eurephiaSESSION *skey,
const char *bytes_sent, const char *bytes_received, const char *duration)
{
dbresult *res = NULL;
+ int ret = 0;
DEBUG(ctx, 20, "Function call: eDBregister_logout(ctx, '%s', %s, %s)",
skey->sessionkey, bytes_sent, bytes_received);
@@ -667,14 +692,16 @@ int eDBregister_logout(eurephiaCTX *ctx, eurephiaSESSION *skey,
" WHERE sessionkey = '%q' AND sessionstatus = 2",
atoi_nullsafe(bytes_sent), atoi_nullsafe(bytes_received),
atoi_nullsafe(duration), skey->sessionkey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ skey->sessionstatus = SESSION_LOGGEDOUT;
+ ret = 1;
+ } else {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update lastlog with logout information (%s)",
skey->sessionkey);
- return 0;
+ ret = 0;
}
sqlite_free_results(res);
- skey->sessionstatus = SESSION_LOGGEDOUT;
- return 1;
+ return ret;
}
@@ -719,14 +746,16 @@ char *eDBget_sessionkey_seed(eurephiaCTX *ctx, sessionType type, const char *ses
return NULL;
}
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ if( sqlite_get_numtuples(res) == 1 ) {
+ skey = strdup_nullsafe(sqlite_get_value(res, 0, 0));
+ } else {
+ skey = NULL;
+ }
+ } else {
eurephia_log(ctx, LOG_FATAL, 0,"Could not retrieve sessionkey from openvpn_sessionkeys (%s)",
sessionseed);
- return NULL;
- }
- if( sqlite_get_numtuples(res) == 1 ) {
- skey = strdup_nullsafe(sqlite_get_value(res, 0, 0));
- } else {
+ sqlite_log_error(ctx, res);
skey = NULL;
}
sqlite_free_results(res);
@@ -751,12 +780,14 @@ char *eDBget_sessionkey_macaddr(eurephiaCTX *ctx, const char *macaddr) {
" WHERE sessionstatus = 3 "
" AND datakey = 'macaddr'"
" AND dataval = '%q'", macaddr);
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ skey = strdup_nullsafe(sqlite_get_value(res, 0, 0));
+ } else {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not remove session from database (MAC addr: %s)", macaddr);
- return 0;
+ sqlite_log_error(ctx, res);
+ skey = NULL;
}
- skey = strdup_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
return skey;
@@ -797,12 +828,14 @@ int eDBcheck_sessionkey_uniqueness(eurephiaCTX *ctx, const char *seskey) {
break;
}
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ uniq = atoi_nullsafe(sqlite_get_value(res, 0, 0));
+ } else {
eurephia_log(ctx, LOG_FATAL, 0,
"eDBcheck_sessionkey_uniqness: Could not check uniqueness of sessionkey");
- return 0;
+ sqlite_log_error(ctx, res);
+ uniq = 0;
}
- uniq = atoi_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
return uniq;
@@ -814,6 +847,7 @@ int eDBcheck_sessionkey_uniqueness(eurephiaCTX *ctx, const char *seskey) {
*/
int eDBregister_sessionkey(eurephiaCTX *ctx, const char *seed, const char *seskey) {
dbresult *res;
+ int ret = 0;
DEBUG(ctx, 20, "eDBregister_sessionkey(ctx, '%s', '%s')", seed, seskey);
if( (seed == NULL) || (seskey == NULL) ) {
@@ -825,13 +859,16 @@ int eDBregister_sessionkey(eurephiaCTX *ctx, const char *seed, const char *seske
res = sqlite_query(ctx,
"INSERT INTO openvpn_sessionkeys (sessionseed, sessionkey) VALUES('%q','%q')",
seed, seskey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ ret = 1;
+ } else {
eurephia_log(ctx, LOG_FATAL, 0,
"eDBregister_sessionkey: Error registering sessionkey into openvpn_sessionkeys");
- return 0;
+ sqlite_log_error(ctx, res);
+ ret = 0;
}
sqlite_free_results(res);
- return 1;
+ return ret;
}
/**
@@ -845,6 +882,7 @@ int eDBregister_sessionkey(eurephiaCTX *ctx, const char *seed, const char *seske
*/
int eDBremove_sessionkey(eurephiaCTX *ctx, const char *seskey) {
dbresult *res;
+ int ret = 0;
DEBUG(ctx, 20, "eDBremove_sessionkey(ctx, '%s')", seskey);
if( seskey == NULL ) {
@@ -854,13 +892,15 @@ int eDBremove_sessionkey(eurephiaCTX *ctx, const char *seskey) {
}
res = sqlite_query(ctx, "DELETE FROM openvpn_sessionkeys WHERE sessionkey = '%q'", seskey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ ret = 1;
+ } else {
eurephia_log(ctx, LOG_FATAL, 0,
"eDBremove_sessionkey: Error removing sessionkey from openvpn_sessionkeys");
- return 0;
+ ret = 0;
}
sqlite_free_results(res);
- return 1;
+ return ret;
}
/**
@@ -881,7 +921,7 @@ eurephiaVALUES *eDBload_sessiondata(eurephiaCTX *ctx, const char *sesskey) {
res = sqlite_query(ctx, "SELECT datakey, dataval FROM openvpn_sessions WHERE sessionkey = '%q'",
sesskey);
- if( (res != NULL) || (sqlite_get_numtuples(res) > 0) ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
eAdd_value(ctx, sessvals,
sqlite_get_value(res, i, 0),
@@ -890,7 +930,7 @@ eurephiaVALUES *eDBload_sessiondata(eurephiaCTX *ctx, const char *sesskey) {
} else {
eurephia_log(ctx, LOG_CRITICAL, 0,
"Could not load session values for session '%s'", sesskey);
-
+ sqlite_log_error(ctx,res);
}
sqlite_free_results(res);
return sessvals;
@@ -902,6 +942,7 @@ eurephiaVALUES *eDBload_sessiondata(eurephiaCTX *ctx, const char *sesskey) {
int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode, const char *key, const char *val)
{
dbresult *res = NULL;
+ int ret = 0;
if( session == NULL ) {
DEBUG(ctx, 20,
@@ -917,11 +958,12 @@ int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode,
res = sqlite_query(ctx,
"INSERT INTO openvpn_sessions (sessionkey, datakey, dataval) "
"VALUES ('%q','%q','%q')", session->sessionkey, key, val);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not register new session variable into database: [%s] %s = %s",
session->sessionkey, key, val);
- return 0;
+ sqlite_log_error(ctx, res);
+ goto exit;
}
break;
@@ -930,10 +972,11 @@ int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode,
"UPDATE openvpn_sessions SET dataval = '%q' "
" WHERE sessionkey = '%q' AND datakey = '%q'",
val, session->sessionkey, key);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not update session variable: [%s] %s = %s ",
session->sessionkey, key, val);
- return 0;
+ sqlite_log_error(ctx, res);
+ goto exit;
}
break;
@@ -942,10 +985,11 @@ int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode,
"DELETE FROM openvpn_sessions "
" WHERE sessionkey = '%q' AND datakey = '%q'",
session->sessionkey, key);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0, "Could not delete session variable: [%s] %s",
session->sessionkey, key);
- return 0;
+ sqlite_log_error(ctx, res);
+ goto exit;
}
break;
@@ -953,8 +997,10 @@ int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode,
eurephia_log(ctx, LOG_FATAL, 0, "Unknown eDBstore_session_value mode '%i'", mode);
return 0;
}
+ exit:
+ ret = (sqlite_query_status(res) == dbSUCCESS ? 1 : 0);
sqlite_free_results(res);
- return 1;
+ return ret;
}
@@ -977,9 +1023,11 @@ int eDBdestroy_session(eurephiaCTX *ctx, eurephiaSESSION *session) {
"UPDATE openvpn_lastlog "
" SET sessionstatus = 4, session_deleted = CURRENT_TIMESTAMP "
" WHERE sessionkey = '%q' AND sessionstatus = 3", session->sessionkey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not update session status in lastlog (%s))", session->sessionkey);
+ sqlite_log_error(ctx, res);
+ sqlite_free_results(res);
return 0;
}
sqlite_free_results(res);
@@ -987,9 +1035,11 @@ int eDBdestroy_session(eurephiaCTX *ctx, eurephiaSESSION *session) {
// Delete session variables
res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", session->sessionkey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) != dbSUCCESS ) {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not delete session variables (%s))", session->sessionkey);
+ sqlite_log_error(ctx, res);
+ sqlite_free_results(res);
return 0;
}
sqlite_free_results(res);
@@ -1019,12 +1069,14 @@ char *eDBget_firewall_profile(eurephiaCTX *ctx, eurephiaSESSION *session)
" JOIN openvpn_usercerts USING(certid, uid)"
" JOIN openvpn_accesses USING(accessprofile)"
" WHERE sessionkey = '%q'", session->sessionkey);
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ ret = strdup_nullsafe(sqlite_get_value(res, 0, 0));
+ } else {
eurephia_log(ctx, LOG_FATAL, 0, "Could not retrieve firewall profile for session '%s'",
session->sessionkey);
- return NULL;
+ sqlite_log_error(ctx, res);
+ ret = NULL;
}
- ret = strdup_nullsafe(sqlite_get_value(res, 0, 0));
sqlite_free_results(res);
return ret;
}
@@ -1042,16 +1094,18 @@ eurephiaVALUES *eDBget_blacklisted_ip(eurephiaCTX *ctx) {
DEBUG(ctx, 20, "Function call: eDBget_blacklisted_ip(ctx)");
res = sqlite_query(ctx, "SELECT remoteip FROM openvpn_blacklist WHERE remoteip IS NOT NULL");
- if( res == NULL ) {
+ if( sqlite_query_status(res) == dbSUCCESS ) {
+ ret = eCreate_value_space(ctx, 21);
+ for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
+ if( (ip = sqlite_get_value(res, i, 0)) != NULL ) {
+ eAdd_value(ctx, ret, NULL, ip);
+ }
+ }
+ } else {
eurephia_log(ctx, LOG_FATAL, 0,
"Could not retrieve blacklisted IP addresses from the database");
- return NULL;
- }
- ret = eCreate_value_space(ctx, 21);
- for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
- if( (ip = sqlite_get_value(res, i, 0)) != NULL ) {
- eAdd_value(ctx, ret, NULL, ip);
- }
+ sqlite_log_error(ctx, res);
+ ret = NULL;
}
sqlite_free_results(res);