diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2011-01-09 23:39:08 +0100 |
|---|---|---|
| committer | David Sommerseth <davids@redhat.com> | 2011-12-19 11:05:38 +0100 |
| commit | 8d2f8d68e6ae9726fdd2d941e55a7377e3cdf927 (patch) | |
| tree | a778cb9ee6f06b41f256a22450af8fd7916a3ed8 /database/sqlite/administration/authentication.c | |
| parent | f0434a3ad51bf1159a78003a020eeb82a26dfc7f (diff) | |
| download | eurephia-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/administration/authentication.c')
| -rw-r--r-- | database/sqlite/administration/authentication.c | 93 |
1 files changed, 69 insertions, 24 deletions
diff --git a/database/sqlite/administration/authentication.c b/database/sqlite/administration/authentication.c index 5587b20..3a612d4 100644 --- a/database/sqlite/administration/authentication.c +++ b/database/sqlite/administration/authentication.c @@ -124,9 +124,15 @@ static xmlDoc *auth_user(eurephiaCTX *ctx, const char *req_access, const char *u " WHERE ou.username = '%q'", uname); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { + xmlNode *err_n = NULL; + eurephia_log(ctx, LOG_FATAL, 0, "Could not authenticate user against the database"); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Authentication failed"); + err_n = sqlite_log_error_xml(ctx, res); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Authentication failed"); + xmlFreeNode(err_n); + sqlite_free_results(res); + return res_d; } if( sqlite_get_numtuples(res) == 1 ) { @@ -185,9 +191,15 @@ static xmlDoc *auth_user(eurephiaCTX *ctx, const char *req_access, const char *u " WHERE uid = '%q' AND interface = '%c' AND access = '%q'", uid, interface, req_access); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { + xmlNode *err_n = NULL; + eurephia_log(ctx, LOG_FATAL, 0, "Could not check access level"); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,"Failed to validate access level"); + err_n = sqlite_log_error_xml(ctx, res); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n,"Failed to validate access level"); + xmlFreeNode(err_n); + sqlite_free_results(res); + return res_d; } access = atoi_nullsafe(sqlite_get_value(res, 0, 0)); sqlite_free_results(res); @@ -230,6 +242,8 @@ static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *r dbresult *res = NULL; int valid = 0, access = 0, expire_time = 0; char interface; + xmlDoc *ret_d = NULL; + xmlNode *err_n = NULL; DEBUG(ctx, 21, "Function call: auth_session(ctx, '%s, '%s')", sesskey, req_access); assert( (ctx != NULL) && (sesskey != NULL) ); @@ -264,9 +278,13 @@ static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *r " AND interface = '%c'", expire_time, sesskey, req_access, interface); - if( (res == NULL) ) { + if( sqlite_query_status(res) != dbSUCCESS ) { eurephia_log(ctx, LOG_FATAL, 0, "Could not validate session"); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Session authentication failed"); + err_n = sqlite_log_error_xml(ctx, res); + ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Session authentication failed"); + sqlite_free_results(res); + xmlFreeNode(err_n); + return ret_d; } valid = (atoi_nullsafe(sqlite_get_value(res, 0, 0)) == 0); @@ -279,8 +297,9 @@ static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *r "UPDATE eurephia_adminlog" " SET last_action = CURRENT_TIMESTAMP, status = 2" " WHERE sessionkey = '%q'", sesskey); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { eurephia_log(ctx, LOG_ERROR, 0, "Could not register session activity"); + err_n = sqlite_log_error_xml(ctx, res); } sqlite_free_results(res); @@ -291,17 +310,19 @@ static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *r " SET logout = CURRENT_TIMESTAMP, status = %i" " WHERE sessionkey = '%q'", (access ? 4 : 5), sesskey); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { eurephia_log(ctx, LOG_ERROR, 0, "Could not register old session as logged out"); + err_n = sqlite_log_error_xml(ctx, res); } sqlite_free_results(res); // Delete session variables res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", sesskey); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { eurephia_log(ctx, LOG_ERROR, 0, "Could not delete session variables (%s))", sesskey); + sqlite_log_error(ctx, res); } else if( !access ) { eurephia_log(ctx, LOG_WARNING, 0, "User account is lacking privileges"); } @@ -309,11 +330,14 @@ static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *r } if (valid && access) { - return eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session authenticated"); + ret_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, err_n, "Session authenticated"); } else { - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Session authentication failed"); - + ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Session authentication failed"); + } + if( err_n != NULL ) { + xmlFreeNode(err_n); } + return ret_d; } @@ -328,6 +352,7 @@ static xmlDoc *auth_session(eurephiaCTX *ctx, const char *sesskey, const char *r * @return Returns an eurephia ResultMsg XML document with the result. On fatal errors, NULL is returned */ static xmlDoc *register_login(eurephiaCTX *ctx, const int uid, const char *sesskey) { + xmlDoc *ret_d = NULL; dbresult *res = NULL; char interface; @@ -354,13 +379,19 @@ static xmlDoc *register_login(eurephiaCTX *ctx, const int uid, const char *sessk " (uid, interface, status, login, last_action, sessionkey) " "VALUES ('%i','%c',1,CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '%q')", uid, interface, sesskey); - if( !res ) { + if( sqlite_query_status(res) == dbSUCCESS ) { + ret_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session is registered as logged in"); + } else { + xmlNode *err_n = NULL; + eurephia_log(ctx, LOG_FATAL, 0, "Failed to register the session in the database"); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, - "Failed to register the session in the database"); + err_n = sqlite_log_error_xml(ctx, res); + ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, + "Failed to register the session in the database"); + xmlFreeNode(err_n); } sqlite_free_results(res); - return eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session is registered as logged in"); + return ret_d; } @@ -375,6 +406,8 @@ static xmlDoc *register_login(eurephiaCTX *ctx, const int uid, const char *sessk */ static xmlDoc *register_logout(eurephiaCTX *ctx, const char *sessionkey) { dbresult *res = NULL; + xmlDoc *ret_d = NULL; + xmlNode *err_n = NULL; DEBUG(ctx, 21, "Function call: register_logout(ctx, '%s')", sessionkey); assert((ctx != NULL) && (sessionkey != NULL)); @@ -391,26 +424,38 @@ static xmlDoc *register_logout(eurephiaCTX *ctx, const char *sessionkey) { " SET logout = CURRENT_TIMESTAMP, status = 3" " WHERE sessionkey = '%q'", sessionkey); - if( !res || (sqlite_get_affected_rows(res) == 0) ) { + if( (sqlite_query_status(res) != dbSUCCESS) || (sqlite_get_affected_rows(res) == 0) ) { eurephia_log(ctx, LOG_FATAL, 0, "Failed to register the session as logged out (updated %i rows)", sqlite_get_affected_rows(res)); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, - "Failed to register the session as logged out"); + if( sqlite_query_status(res) == dbERROR ) { + err_n = sqlite_log_error_xml(ctx, res); + } + ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + "Failed to register the session as logged out"); + xmlFreeNode(err_n); + goto exit; } sqlite_free_results(res); // Delete session variables res = sqlite_query(ctx, "DELETE FROM openvpn_sessions WHERE sessionkey = '%q'", sessionkey); - if( !res || (sqlite_get_affected_rows(res) == 0) ) { + if( (sqlite_query_status(res) == dbSUCCESS) && (sqlite_get_affected_rows(res) > 0) ) { + ret_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session is logged out"); + } else { eurephia_log(ctx, LOG_ERROR, 0, "Could not delete session variables (%s))", sessionkey); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, - "Could not delete session variables (%s))", sessionkey); + if( sqlite_query_status(res) == dbERROR ) { + err_n = sqlite_log_error_xml(ctx, res); + } + ret_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, + "Could not delete session variables (%s)", sessionkey); + xmlFreeNode(err_n); } - sqlite_free_results(res); - return eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Session is logged out"); + exit: + sqlite_free_results(res); + return ret_d; } |
