summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2011-07-26 00:02:21 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2013-06-13 01:01:55 +0200
commit452b54e3105d3f5cb4f8f35ee228ac582106e394 (patch)
tree026e7708e4257750344dcef9c8bd870b19450941
parentb4cba21ff4448b823c9f81c72c56c496e741f2a0 (diff)
downloadeurephia-452b54e3105d3f5cb4f8f35ee228ac582106e394.tar.gz
eurephia-452b54e3105d3f5cb4f8f35ee228ac582106e394.tar.xz
eurephia-452b54e3105d3f5cb4f8f35ee228ac582106e394.zip
edb-pgsql: Reworked PostgreSQL error handling
Added a generic error reporting function, to stream line this process. Signed-off-by: David Sommerseth <dazo@users.sourceforge.net>
-rw-r--r--database/postgresql/edb-pgsql.c196
1 files changed, 101 insertions, 95 deletions
diff --git a/database/postgresql/edb-pgsql.c b/database/postgresql/edb-pgsql.c
index 3ee2b39..7fea328 100644
--- a/database/postgresql/edb-pgsql.c
+++ b/database/postgresql/edb-pgsql.c
@@ -31,6 +31,7 @@
#include <string.h>
#include <unistd.h>
+#include <stdarg.h>
#include <assert.h>
#include <libpq-fe.h>
@@ -82,6 +83,30 @@ static inline char *PGgetValue(PGresult *res, int row, int col) {
}
}
+
+static void PGerrorMessage(eurephiaCTX *ctx, PGresult *dbr, int logdst, int loglvl,
+ const char *prepsql, const char *fmt, ...)
+{
+ char msgfmt[514];
+ va_list ap;
+
+ memset(&msgfmt, 0, 514);
+ va_start(ap, fmt);
+ snprintf(msgfmt, 512, "[%s] SQL query failed: %s: %s",
+ prepsql, fmt,
+ (dbr != NULL ? PQresultErrorMessage(dbr)
+ : (ctx->dbc != NULL ? PQerrorMessage(ctx->dbc->dbhandle) : "[unknown error]")
+ ));
+ veurephia_log(ctx, logdst, loglvl, ap, msgfmt);
+ va_end(ap);
+ memset(&msgfmt, 0, 514);
+ PQclear(dbr);
+ if( (loglvl == LOG_EMERG) && (ctx->dbc != NULL) ) {
+ PQfinish(ctx->dbc->dbhandle);
+ }
+}
+
+
#define PREPSQL_TLS_AUTH "eurephia_tls_auth"
#define PREPSQL_USERPWD_AUTH "eurephia_userpwd_auth"
#define PREPSQL_BLACKLIST_ATTEMPTUPD "eurephia_blacklist_attupd"
@@ -125,11 +150,9 @@ static inline void update_blacklist_attempt(eurephiaCTX *ctx, const char *blid)
"UPDATE blacklist "
" SET last_accessed = CURRENT_TIMESTAMP WHERE blid = $1", 1, NULL);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_CRITICAL, 0,
- "Could not prepare blacklist last attempt update query: %s",
- (dbr == NULL ? PQresultErrorMessage(dbr)
- : PQerrorMessage(ctx->dbc->dbhandle)));
- goto exit;
+ PGerrorMessage(ctx, dbr, LOG_CRITICAL, 0, PREPSQL_BLACKLIST_ATTEMPTUPD,
+ "Preparing last attempt query update failed");
+ return;
}
PQclear(dbr);
@@ -139,18 +162,13 @@ static inline void update_blacklist_attempt(eurephiaCTX *ctx, const char *blid)
qry_args, NULL, NULL, 0);
free_nullsafe(ctx, qry_args);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_CRITICAL, 0,
- "Failed to update blaclist.last_access timestamp for blid %s (%s)",
- blid,
- (dbr ? PQresultErrorMessage(dbr)
- : PQerrorMessage(ctx->dbc->dbhandle)));
- }
- exit:
- if( dbr ) {
- PQclear(dbr);
+ PGerrorMessage(ctx, dbr, LOG_CRITICAL, 0, PREPSQL_BLACKLIST_ATTEMPTUPD,
+ "Failed to update blaclist.last_access timestamp for blid %s",
+ blid);
+ return;
}
+ PQclear(dbr);
}
- return;
}
@@ -167,7 +185,7 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
DEBUG(ctx, 20, "Function call: eDBconnect(ctx, %i, '%s')", argc, argv[0]);
if( (ctx == NULL) || (argc != 1) || (argv[0] == NULL) || (strlen(argv[0]) < 1) ) {
- eurephia_log(ctx, LOG_PANIC, 0,
+ eurephia_log(ctx, LOG_EMERG, 0,
"edb-pgsql: Missing configuration file argument. "
"No database configured.");
return 0;
@@ -175,10 +193,11 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
// Parse the config file with PostgreSQL connection information
dbc = (eDBconn *) malloc_nullsafe(ctx, sizeof(eDBconn)+2);
+ ctx->dbc = dbc;
assert( dbc != NULL );
dbc->dbparams = ecfg_ReadConfig(ctx, argv[0]);
if( dbc->dbparams == NULL ) {
- eurephia_log(ctx, LOG_FATAL, 0,
+ eurephia_log(ctx, LOG_EMERG, 0,
"edb-pgsql: Failed to parse the edb-pgsql configuration file (%s)",
argv[0]);
free_nullsafe(ctx, dbc);
@@ -190,21 +209,24 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
dbuser = eGet_value(dbc->dbparams, "user");
dbpwd = eGet_value(dbc->dbparams, "password");
if( dbname == NULL ) {
- eurephia_log(ctx, LOG_FATAL, 0, "edb-pgsql: Missing required database name");
+ eurephia_log(ctx, LOG_EMERG, 0, "edb-pgsql: Missing required database name");
return 0;
}
dbc->dbhandle = PQsetdbLogin(dbhost, dbport, NULL, NULL, dbname, dbuser, dbpwd);
if( dbc->dbhandle == NULL ) {
- eurephia_log(ctx, LOG_FATAL, 0,
+ eurephia_log(ctx, LOG_EMERG, 0,
"Failed to connect to the PostgreSQL database: Unknown error");
+ ctx->dbc = NULL;
free_nullsafe(ctx, dbc);
return 0;
}
if( PQstatus(dbc->dbhandle) != CONNECTION_OK ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Failed to connect to the PostgreSQL database: %s",
- PQerrorMessage(dbc->dbhandle));
+ PGerrorMessage(ctx, NULL, LOG_EMERG, 0, "DB-INIT",
+ "Failed to connect to the PostgreSQL database '%s'",
+ dbname);
+ ctx->dbc = NULL;
free_nullsafe(ctx, dbc);
return 0;
}
@@ -219,7 +241,7 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
cfg = eCreate_value_space(ctx, 11);
if( cfg == NULL ) {
- eurephia_log(ctx, LOG_FATAL, 0,
+ eurephia_log(ctx, LOG_EMERG, 0,
"Could not allocate memory for config variables");
PQclear(res);
PQfinish(dbc->dbhandle);
@@ -232,17 +254,12 @@ int eDBconnect(eurephiaCTX *ctx, const int argc, const char **argv)
dbc->config = cfg;
PQclear(res);
} else {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to query the eurephia configuration from the database: %s",
- (res ? PQresultErrorMessage(res)
- : PQerrorMessage(ctx->dbc->dbhandle)));
- PQclear(res);
- PQfinish(dbc->dbhandle);
+ PGerrorMessage(ctx, res, LOG_EMERG, 0, "DB-INIT",
+ "Failed to the configuration from the database");
eFree_values(ctx, dbc->dbparams);
free_nullsafe(ctx, dbc);
-
+ return 0;
}
- ctx->dbc = dbc;
return 1;
}
@@ -290,7 +307,7 @@ int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char
org, cname, email, digest, depth);
if( !ctx || !ctx->dbc || !ctx->dbc->dbhandle ) {
- eurephia_log(ctx, LOG_FATAL, 0, "System error in eDBauth_TLS()");
+ eurephia_log(ctx, LOG_EMERG, 0, "System error in eDBauth_TLS()");
return 0;
}
@@ -303,11 +320,7 @@ int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char
" AND lower(cert.digest)=lower($5::varchar)\0",
5, NULL);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Failed to prepare " PREPSQL_TLS_AUTH " SQL query: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
- if( dbr ) {
- PQclear(dbr);
- }
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_TLS_AUTH, "Failed to prepare SQL query");
return 0;
}
PQclear(dbr);
@@ -324,11 +337,10 @@ int eDBauth_TLS(eurephiaCTX *ctx, const char *org, const char *cname, const char
dbr = PQexecPrepared(ctx->dbc->dbhandle, PREPSQL_TLS_AUTH, 5, qry_args, NULL, NULL, 0);
free_nullsafe(ctx, qry_args);
if( !dbr || (PQresultStatus(dbr) != PGRES_TUPLES_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Could not lookup certificate information: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
- if( dbr ) {
- PQclear(dbr);
- }
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_TLS_AUTH,
+ "Could not lookup certificate information "
+ "[O=%s/CN=%s/emailAddress=%s - depth %s, digest=%s]",
+ org, cname, email, depth_str, digest);
return 0;
}
@@ -382,9 +394,8 @@ int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const
" JOIN blacklist USING(digest)) bl2 ON(uc.certid = bl2.certid)"
" WHERE uc.certid = $1::INTEGER AND ou.username = $2::VARCHAR", 2, NULL);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to prepare " PREPSQL_USERPWD_AUTH " SQL query: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_USERPWD_AUTH,
+ "Failed to prepare SQL query");
uicid = 0;
goto exit;
}
@@ -399,8 +410,9 @@ int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const
dbr = PQexecPrepared(ctx->dbc->dbhandle, PREPSQL_USERPWD_AUTH, 2, qry_args, NULL, NULL, 0);
free_nullsafe(ctx, qry_args);
if( !dbr || (PQresultStatus(dbr) != PGRES_TUPLES_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Could not lookup user account information: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0,
+ "Failed to lookup user account information [username=%s, certid=%s]",
+ username, certid_str);
uicid = 0;
goto exit;
}
@@ -465,11 +477,9 @@ int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const
upd = PQprepare(ctx->dbc->dbhandle, PREPSQL_USERS_LASTACC_UPD,
"UPDATE users SET last_accessed = CURRENT_TIMESTAMP"
" WHERE uid = $1::INTEGER", 1, NULL);
- if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to prepare " PREPSQL_USERS_LASTACC_UPD " SQL query: %s",
- (dbr ? PQresultErrorMessage(dbr)
- : PQerrorMessage(ctx->dbc->dbhandle)));
+ if( !dbr || (PQresultStatus(upd) != PGRES_COMMAND_OK) ) {
+ PGerrorMessage(ctx, upd, LOG_FATAL, 0, PREPSQL_USERS_LASTACC_UPD,
+ "Failed to prepare SQL query");
} else if( dbr ) {
PQclear(upd);
@@ -479,19 +489,19 @@ int eDBauth_user(eurephiaCTX *ctx, const int certid, const char *username, const
1, qry_args, NULL, NULL, 0);
free_nullsafe(ctx, qry_args);
if( !upd || (PQresultStatus(upd) != PGRES_TUPLES_OK) ) {
- eurephia_log(ctx, LOG_ERROR, 0,
- "Could not update last access status for uid %s", uid);
+ PGerrorMessage(ctx, upd, LOG_FATAL, 0, PREPSQL_USERS_LASTACC_UPD
+ "Failed to update last access status for uid %s", uid);
}
if( upd ) {
PQclear(upd);
}
}
}
-
- exit:
if( dbr ) {
PQclear(dbr);
}
+
+ exit:
DEBUG(ctx, 20, "Result function call: eDBauth_user(ctx, %i, '%s','xxxxxxxx') - %i",
certid, username, uicid);
@@ -523,9 +533,8 @@ int eDBget_uid(eurephiaCTX *ctx, const int certid, const char *username)
" WHERE certid = $1::INTEGER AND username = $1::VARCHAR",
2, NULL);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to prepare " PREPSQL_USERS_GETUID " SQL query: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_USERS_GETUID,
+ "Failed to prepare SQL query");
ret = -1;
goto exit;
}
@@ -538,9 +547,8 @@ int eDBget_uid(eurephiaCTX *ctx, const int certid, const char *username)
dbr = PQexecPrepared(ctx->dbc->dbhandle, PREPSQL_USERPWD_AUTH, 2, qry_args, NULL, NULL, 0);
free_nullsafe(ctx, qry_args);
if( !dbr || (PQresultStatus(dbr) != PGRES_TUPLES_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Failed to lookup userid for user '%s': %s",
- username,
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_USERPWD_AUTH,
+ "Failed to lookup userid for user '%s'", username);
ret = -1;
goto exit;
}
@@ -556,17 +564,16 @@ int eDBget_uid(eurephiaCTX *ctx, const int certid, const char *username)
ret = atoi_nullsafe(PGgetValue(dbr,0, 0));
break;
default:
- eurephia_log(ctx, LOG_FATAL, 0, "Failed to lookup userid for user '%s'",
+ eurephia_log(ctx, LOG_CRIT, 0, "Failed to lookup userid for user '%s'",
username);
ret = -1;
break;
}
- exit:
if( dbr ) {
PQclear(dbr);
}
-
+ exit:
DEBUG(ctx, 20, "Result function call: eDBget_uid(ctx, %i, '%s') - %i", certid, username, ret);
return ret;
}
@@ -591,17 +598,16 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
snprintf(sql, 4096,
"SELECT blid"
" FROM blacklist"
- " WHERE %s = %s%s$1%s%c",
+ " WHERE %s = %s%s$1%s",
eDBattempt_types[type].colname_where,
defaultValue(eDBattempt_types[type].value_func, ""),
- (strlen_nullsafe(eDBattempt_types[type].value_func) > 0 ? "(" : ""),
- (strlen_nullsafe(eDBattempt_types[type].value_func) > 0 ? ")" : ""), 0);
+ (eDBattempt_types[type].value_func ? "(" : ""),
+ (eDBattempt_types[type].value_func ? ")" : ""));
dbr = PQprepare(ctx->dbc->dbhandle, PREPSQL_BLACKLIST_CHECK, sql, 1, NULL);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to prepare " PREPSQL_BLACKLIST_CHECK " SQL query: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_BLACKLIST_CHECK,
+ "Failed to prepare SQL query");
blacklisted = -1;
goto exit;
}
@@ -609,9 +615,9 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
dbr = PQexecPrepared(ctx->dbc->dbhandle, PREPSQL_BLACKLIST_CHECK, 2, qry_args, NULL, NULL, 0);
if( !dbr || (PQresultStatus(dbr) != PGRES_TUPLES_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0, "Failed to lookup %s in the blacklist for '%s': %s",
- eDBattempt_types[type].descr, val,
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_BLACKLIST_CHECK,
+ "Failed to lookup %s in the blacklist for '%s'",
+ eDBattempt_types[type].descr, val);
blacklisted = -1;
goto exit;
}
@@ -624,11 +630,12 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
blacklisted = 1; // [type] is blacklisted
}
update_blacklist_attempt(ctx, blid);
+ PQclear(dbr);
} else {
- eurephia_log(ctx, LOG_FATAL, 0, "Querying blacklist for %s failed",
- eDBattempt_types[type].descr);
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_BLACKLIST_CHECK,
+ "Blacklist query for %s failed (%s)",
+ eDBattempt_types[type].descr, val);
};
- PQclear(dbr);
// Check if this [type] have been tried before and consider if it should be blacklisted
if( blacklisted == 0) {
@@ -644,9 +651,8 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
eDBattempt_types[type].colname_where);
dbr = PQprepare(ctx->dbc->dbhandle, PREPSQL_ATTEMPTS_CHECK, sql, 1, NULL);
if( !dbr || (PQresultStatus(dbr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to prepare " PREPSQL_ATTEMPTS_CHECK " SQL query: %s",
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_ATTEMPTS_CHECK,
+ "Failed to prepare SQL query");
blacklisted = -1;
goto exit;
}
@@ -654,10 +660,9 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
dbr = PQexecPrepared(ctx->dbc->dbhandle, PREPSQL_BLACKLIST_CHECK, 1, qry_args, NULL, NULL, 0);
if( !dbr || (PQresultStatus(dbr) != PGRES_TUPLES_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed look up attempts counter for %s in the blacklist check on '%s': %s",
- eDBattempt_types[type].descr, val,
- (dbr ? PQresultErrorMessage(dbr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_BLACKLIST_CHECK,
+ "Failed look up attempts counter for %s in the blacklist check on '%s'",
+ eDBattempt_types[type].descr, val);
blacklisted = -1;
goto exit;
}
@@ -678,28 +683,30 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
eDBattempt_types[type].colname);
blr = PQprepare(ctx->dbc->dbhandle, PREPSQL_BLACKLIST_REGISTER, sql, 1, NULL);
if( !blr || (PQresultStatus(blr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to prepare " PREPSQL_ATTEMPTS_CHECK " SQL query: %s",
- (blr ? PQresultErrorMessage(blr) : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, blr, LOG_FATAL, 0, PREPSQL_BLACKLIST_REGISTER,
+ "Failed to prepare SQL query");
} else {
PQclear(blr);
- blr = PQexecPrepared(ctx->dbc->dbhandle, PREPSQL_BLACKLIST_REGISTER, 1, qry_args,
+ blr = PQexecPrepared(ctx->dbc->dbhandle,
+ PREPSQL_BLACKLIST_REGISTER, 1, qry_args,
NULL, NULL, 0);
if( !blr || (PQresultStatus(blr) != PGRES_COMMAND_OK) ) {
- eurephia_log(ctx, LOG_FATAL, 0,
- "Failed to register %s value '%s' in the blacklist: %s",
- eDBattempt_types[type].descr, val,
- (blr ? PQresultErrorMessage(blr)
- : PQerrorMessage(ctx->dbc->dbhandle)));
+ PGerrorMessage(ctx, blr, LOG_FATAL, 0,
+ PREPSQL_BLACKLIST_REGISTER,
+ "Failed to register %s value '%s' in the "
+ "blacklist:",
+ eDBattempt_types[type].descr, val);
}
}
PQclear(blr);
blacklisted = 1; // [type] is blacklisted
}
atpid = NULL;
+ PQclear(dbr);
} else {
- eurephia_log(ctx, LOG_CRITICAL, 0, "Querying attempts counts for blacklisted %s failed",
- eDBattempt_types[type].descr);
+ PGerrorMessage(ctx, dbr, LOG_FATAL, 0, PREPSQL_BLACKLIST_CHECK,
+ "Querying attempts counts for blacklisted %s failed (%s)",
+ eDBattempt_types[type].descr, val);
}
}
DEBUG(ctx, 20, "Result - function call: eDBblacklist_check(ctx, '%s', '%s') - %i",
@@ -707,7 +714,6 @@ int eDBblacklist_check(eurephiaCTX *ctx, const int type, const char *val)
exit:
blid = NULL;
- PQclear(dbr);
free_nullsafe(ctx, qry_args);
return blacklisted;