From 8d2f8d68e6ae9726fdd2d941e55a7377e3cdf927 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Sun, 9 Jan 2011 23:39:08 +0100 Subject: 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 --- database/sqlite/administration/useraccount.c | 99 ++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 27 deletions(-) (limited to 'database/sqlite/administration/useraccount.c') diff --git a/database/sqlite/administration/useraccount.c b/database/sqlite/administration/useraccount.c index a936294..a989257 100644 --- a/database/sqlite/administration/useraccount.c +++ b/database/sqlite/administration/useraccount.c @@ -123,9 +123,12 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, " ON (os.uid = users.uid)", NULL, uinfo_map, sortkeys); - if( uinf == NULL ) { + if( sqlite_query_status(uinf) != dbSUCCESS ) { eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a user"); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Failed to query the user database"); + info_n = sqlite_log_error_xml(ctx, uinf); + doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n, "Failed to query the user database"); + xmlFreeNode(info_n); + goto exit; } eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &doc, &root_n); @@ -188,7 +191,7 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, info_n = xmlNewChild(user_n, NULL, (xmlChar *) "certificates", NULL); assert( info_n != NULL ); - if( (qres != NULL) && (sqlite_get_numtuples(qres) > 0) ) { + if( (sqlite_query_status(qres) == dbSUCCESS) && (sqlite_get_numtuples(qres) > 0) ) { int i; xmlNode *cert, *acpr; xmlChar *tmp = NULL; @@ -216,6 +219,8 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, sqlite_xml_value(acpr, XML_ATTR, "accessprofile", qres, i, 7); sqlite_xml_value(acpr, XML_ATTR, "fwdestination", qres, i, 9); } + } else if( sqlite_query_status(qres) == dbERROR ) { + sqlite_log_error(ctx, qres); } sqlite_free_results(qres); } @@ -238,11 +243,15 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, " LEFT JOIN openvpn_certificates cert ON(ll.certid=cert.certid)" " WHERE uid = '%i' ORDER BY login, logout", uid); - if( qres == NULL ) { + if( sqlite_query_status(qres) != dbSUCCESS ) { eurephia_log(ctx, LOG_ERROR, 0, "Querying the lastlog failed"); xmlFreeDoc(doc); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + info_n = sqlite_log_error_xml(ctx, qres); + doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n, "Failed to query the lastlog"); + sqlite_free_results(qres); + xmlFreeNode(info_n); + return doc; } lastl = xmlNewChild(user_n, NULL, (xmlChar *) "lastlog", NULL); @@ -303,12 +312,18 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, " FROM openvpn_attempts " " WHERE username = '%q'", username); - if( (qres == NULL) || (sqlite_get_numtuples(qres) > 1) ) { + if( (sqlite_query_status(qres) != dbSUCCESS) || (sqlite_get_numtuples(qres) > 1) ) { eurephia_log(ctx, LOG_ERROR, 0, "Querying for login attempts failed"); - sqlite_free_results(qres); + info_n = NULL; + if( sqlite_query_status(qres) == dbERROR ) { + info_n = sqlite_log_error_xml(ctx, qres); + } xmlFreeDoc(doc); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n, "Failed to query the login attempts log"); + sqlite_free_results(qres); + xmlFreeNode(info_n); + return doc; } atmpt = xmlNewChild(user_n, NULL, (xmlChar *) "attempts", NULL); @@ -331,12 +346,18 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, " FROM openvpn_blacklist " " WHERE username = '%q'", username); - if( (qres == NULL) || (sqlite_get_numtuples(qres) > 1) ) { - eurephia_log(ctx, LOG_ERROR, 0, "Querying blacklist log failed"); - sqlite_free_results(qres); + if( (sqlite_query_status(qres) != dbSUCCESS) || (sqlite_get_numtuples(qres) > 1) ) { xmlFreeDoc(doc); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + eurephia_log(ctx, LOG_ERROR, 0, "Querying blacklist log failed"); + info_n = NULL; + if( sqlite_query_status(qres) == dbERROR ) { + info_n = sqlite_log_error_xml(ctx, qres); + } + doc = eurephiaXML_ResultMsg(ctx, exmlERROR, info_n, "Failed to query the blacklist log"); + sqlite_free_results(qres); + xmlFreeNode(info_n); + return doc; } atmpt = xmlNewChild(user_n, NULL, (xmlChar *) "blacklist", NULL); @@ -351,6 +372,7 @@ static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, } } + exit: sqlite_free_results(uinf); return doc; } @@ -380,10 +402,16 @@ static xmlDoc *useracc_add(eurephiaCTX *ctx, eDBfieldMap *usrinf_map) { // Register the user res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_users", usrinf_map, NULL, NULL); - if( (res == NULL) || (sqlite_get_affected_rows(res) == 0) ) { + if( (sqlite_query_status(res) != dbSUCCESS) || (sqlite_get_affected_rows(res) == 0) ) { + xmlNode *err_n = NULL; + eurephia_log(ctx, LOG_FATAL, 0, "Could not register the new user account"); - res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + if( sqlite_query_status(res) == dbERROR ) { + err_n = sqlite_log_error_xml(ctx, res); + } + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to register the user account"); + xmlFreeNode(err_n); } else { xmlChar *uid = malloc_nullsafe(ctx, 34); xmlNode *info_n = NULL; @@ -445,9 +473,13 @@ static xmlDoc *useracc_update(eurephiaCTX *ctx, const int uid, eDBfieldMap *valu // UPDATE the database uinf = sqlite_query_mapped(ctx, SQL_UPDATE, "UPDATE openvpn_users", value_map, srch_map, NULL); - if( uinf == NULL ) { + if( sqlite_query_status(uinf) != dbSUCCESS ) { + xmlNode *err_n = NULL; + eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a user"); - eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Failed to update user (uid %i)", uid); + err_n = sqlite_log_error_xml(ctx, uinf); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to update user (uid %i)", uid); + xmlFreeNode(err_n); } else if( sqlite_get_affected_rows(uinf) == 0 ) { res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not find any user account with uid %i", uid); @@ -488,10 +520,14 @@ static xmlDoc *useracc_delete(eurephiaCTX *ctx, const unsigned int uid) { // Delete the user res = sqlite_query(ctx, "DELETE FROM openvpn_users WHERE uid = '%i'", uid); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { + xmlNode *err_n = NULL; + eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the user account (uid %i)", uid); - res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + err_n = sqlite_log_error_xml(ctx, res); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to delete the user account (uid %i)", uid); + xmlFreeNode(err_n); } else if( sqlite_get_affected_rows(res) == 0 ) { res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not find any user account with uid %i", uid); @@ -606,10 +642,13 @@ xmlDoc *adminacclvl_Get(eurephiaCTX *ctx, eDBfieldMap *fmap) { " FROM eurephia_adminaccess eac" " LEFT JOIN openvpn_users USING(uid)", NULL, fmap, "uid, interface, access"); - if( res == NULL ) { + if( sqlite_query_status(res) != dbSUCCESS ) { eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a access levels"); - return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + tmp_n = sqlite_log_error_xml(ctx, res); + doc = eurephiaXML_ResultMsg(ctx, exmlERROR, tmp_n, "Error querying the database for a access levels"); + xmlFreeNode(tmp_n); + goto exit; } eurephiaXML_CreateDoc(ctx, 1, "admin_access_list", &doc, &root_n); @@ -628,6 +667,7 @@ xmlDoc *adminacclvl_Get(eurephiaCTX *ctx, eDBfieldMap *fmap) { tmp_n = sqlite_xml_value(acl_n, XML_NODE, "access", res, i, 3); sqlite_xml_value(tmp_n, XML_ATTR, "interface", res, i, 2); } + exit: sqlite_free_results(res); return doc; } @@ -639,7 +679,7 @@ xmlDoc *adminacclvl_Get(eurephiaCTX *ctx, eDBfieldMap *fmap) { xmlDoc *eDBadminAccessLevel(eurephiaCTX *ctx, xmlDoc *qryxml) { dbresult *sqlres = NULL; xmlDoc *res_d = NULL; - xmlNode *qry_n = NULL, *fmap_n = NULL; + xmlNode *qry_n = NULL, *fmap_n = NULL, *err_n = NULL; eDBfieldMap *fmap_m = NULL; char *mode = NULL; @@ -675,17 +715,19 @@ xmlDoc *eDBadminAccessLevel(eurephiaCTX *ctx, xmlDoc *qryxml) { if( strcmp(mode, "grant") == 0 ) { sqlres = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO eurephia_adminaccess", fmap_m, NULL, NULL); - if( sqlres && (sqlite_get_affected_rows(sqlres) > 0) ) { + if( (sqlite_query_status(sqlres) == dbSUCCESS) && (sqlite_get_affected_rows(sqlres) > 0) ) { res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Access level %s (%s) was granted to uid %s", eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL), eDBmappingGetValue(fmap_m, FIELD_INTERFACE), eDBmappingGetValue(fmap_m, FIELD_UID)); + } else if( sqlite_query_status(sqlres) == dbERROR ) { + err_n = sqlite_log_error_xml(ctx, sqlres); } } else if( strcmp(mode, "revoke") == 0 ) { sqlres = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM eurephia_adminaccess", NULL, fmap_m, NULL); - if( sqlres && (sqlite_get_affected_rows(sqlres) > 0) ) { + if( (sqlite_query_status(sqlres) == dbSUCCESS) && (sqlite_get_affected_rows(sqlres) > 0) ) { const char *uid = eDBmappingGetValue(fmap_m, FIELD_UID); const char *acclvl = eDBmappingGetValue(fmap_m, FIELD_ACCESSLVL); @@ -699,18 +741,21 @@ xmlDoc *eDBadminAccessLevel(eurephiaCTX *ctx, xmlDoc *qryxml) { "%i access levels was removed from uid %s", sqlite_get_affected_rows(sqlres), uid); } - } + } else if( sqlite_query_status(sqlres) == dbERROR ) { + err_n = sqlite_log_error_xml(ctx, sqlres); + } } else if( strcmp(mode, "list") == 0 ) { res_d = adminacclvl_Get(ctx, fmap_m); } if( res_d == NULL ) { eurephia_log(ctx, LOG_ERROR, 0, "Failed to update admin access"); - res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Failed to complete %s operation", mode); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to complete %s operation", mode); } - if( sqlres ) { - sqlite_free_results(sqlres); + if( err_n != NULL ) { + xmlFreeNode(err_n); } + sqlite_free_results(sqlres); eDBfreeMapping(fmap_m); return res_d; -- cgit