diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-24 23:59:29 +0200 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-24 23:59:29 +0200 |
| commit | 78a4ff98774ef8487080fc4180e8fc77f187c293 (patch) | |
| tree | b2f4ff17ec223306905c74abaa4c4b0044ee94ce /database/sqlite/administration | |
| parent | 9a2a8c5216d61dafb4232dba1ab56656390b61b0 (diff) | |
| download | eurephia-78a4ff98774ef8487080fc4180e8fc77f187c293.tar.gz eurephia-78a4ff98774ef8487080fc4180e8fc77f187c293.tar.xz eurephia-78a4ff98774ef8487080fc4180e8fc77f187c293.zip | |
Unified eDBadminUser*() functions into one eDBadminUserAccount() function
Rewritten the database API and the SQLite3 database driver.
This code has yet not been tested.
Diffstat (limited to 'database/sqlite/administration')
| -rw-r--r-- | database/sqlite/administration/useraccount.c | 385 |
1 files changed, 200 insertions, 185 deletions
diff --git a/database/sqlite/administration/useraccount.c b/database/sqlite/administration/useraccount.c index 4f44789..62000f5 100644 --- a/database/sqlite/administration/useraccount.c +++ b/database/sqlite/administration/useraccount.c @@ -61,60 +61,6 @@ void xmlReplaceChars(xmlChar *str, char s, char r); /** - * @copydoc eDBadminGetUserList() - */ -xmlDoc *eDBadminGetUserList(eurephiaCTX *ctx, const char *sortkeys) { - xmlDoc *userlist = NULL; - xmlNode *root_n = NULL, *user_n = NULL; - dbresult *res = NULL; - char *dbsort = NULL, tmp[34]; - int i = 0; - - DEBUG(ctx, 20, "Function call: eDBadminGetUserList(ctx, '%s')", sortkeys); - assert((ctx != NULL) && (ctx->dbc != 0)); - - if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { - eurephia_log(ctx, LOG_CRITICAL, 0, - "eurephia admin function call attempted with wrong context type"); - return NULL; - } - - // Convert the input sort keys to the proper database field names - dbsort = eDBmkSortKeyString(tbl_sqlite_users, sortkeys); - - // Query database for all users - res = sqlite_query(ctx, - "SELECT username, activated, deactivated, last_accessed, uid" - " FROM openvpn_users " - "ORDER BY %s", (sortkeys != NULL ? dbsort : "uid")); - if( res == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Error querying the user database"); - return NULL; - } - - // Prepare a list with all users - memset(&tmp, 0, 34); - eurephiaXML_CreateDoc(ctx, 1, "userlist", &userlist, &root_n); - snprintf(tmp, 32, "%i", sqlite_get_numtuples(res)); - xmlNewProp(root_n, (xmlChar *)"usercount", (xmlChar *)tmp); - - // Register all records - for( i = 0; i < sqlite_get_numtuples(res); i++ ) { - user_n = xmlNewChild(root_n, NULL, (xmlChar *)"user", NULL); - sqlite_xml_value(user_n, XML_ATTR, "uid", res, i, 4); - sqlite_xml_value(user_n, XML_NODE, "username", res, i, 0); - sqlite_xml_value(user_n, XML_NODE, "activated", res, i, 1); - sqlite_xml_value(user_n, XML_NODE, "deactivated", res, i, 2); - sqlite_xml_value(user_n, XML_NODE, "last_accessed", res, i, 3); - } - sqlite_free_results(res); - - // Return a user list - return userlist; -} - - -/** * Internal function. Adds a child node named \<flag\> to an xmlNode containing a flag value * * @param node xmlNode pointer where to add the new flag @@ -123,7 +69,7 @@ xmlDoc *eDBadminGetUserList(eurephiaCTX *ctx, const char *sortkeys) { * * @return Returns the \c flagged value */ -inline int xml_set_flag(xmlNode *node, char *flagname, int flagged) { +static inline int xml_set_flag(xmlNode *node, char *flagname, int flagged) { if( flagged ) { xmlNewChild(node, NULL, (xmlChar *) "flag", (xmlChar *) flagname); } @@ -132,19 +78,24 @@ inline int xml_set_flag(xmlNode *node, char *flagname, int flagged) { /** - * @copydoc eDBadminGetUserInfo() + * Internal function. Retrieves information about user accounts + * + * @param ctx eurephiaCTX + * @param infoType Flags defining which information to be included in the result + * @param usrinf_map eDBfieldMap containing information needed for the new user account + * + * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors, + * NULL is returned. */ -xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { +static xmlDoc *useracc_view(eurephiaCTX *ctx, unsigned int infoType, eDBfieldMap *uinfo_map) { dbresult *uinf = NULL, *qres = NULL; - eDBfieldMap *uinfo_map = NULL; - int flag = 0, uid = 0; + unsigned int flag = 0, uid = 0, i = 0; char *username = NULL; - xmlDoc *doc = NULL; - xmlNode *root_n = NULL, *info_n = NULL, *fieldmap = NULL; + xmlNode *root_n = NULL, *info_n = NULL; DEBUG(ctx, 20, "Function call: eDBadminGetUserUserInfo(ctx, %i, {xmlDoc})", infoType); - assert( (ctx != NULL) && (srch != NULL) ); + assert( (ctx != NULL) && (uinfo_map != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, @@ -152,9 +103,6 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { return NULL; } - fieldmap = eurephiaXML_getRoot(ctx, srch, "fieldMapping", 1); - uinfo_map = eDBxmlMapping(ctx, tbl_sqlite_users, "u", fieldmap); - // Query the database, find the user defined in the user map uinf = sqlite_query_mapped(ctx, SQL_SELECT, "SELECT u.username, u.activated, u.deactivated, u.last_accessed, u.uid," @@ -180,21 +128,22 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { } eDBfreeMapping(uinfo_map); - switch( sqlite_get_numtuples(uinf) ) { - case 0: - sqlite_free_results(uinf); - return 0; // No user found + eurephiaXML_CreateDoc(ctx, 1, "UserAccount", &doc, &root_n); + xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "view"); - case 1: - uid = atoi_nullsafe(sqlite_get_value(uinf, 0, 4)); - username = sqlite_get_value(uinf, 0, 0); + for( i = 0; i < sqlite_get_numtuples(uinf); i++ ) { + xmlNode *user_n = xmlNewChild(root_n, NULL, (xmlChar *) "UserAccount", NULL); + assert( user_n != NULL ); - eurephiaXML_CreateDoc(ctx, 1, "user", &doc, &root_n); + sqlite_xml_value(user_n, XML_ATTR, "uid", uinf, 0, 4); sqlite_xml_value(root_n, XML_NODE, "username", uinf, 0, 0); - sqlite_xml_value(root_n, XML_ATTR, "uid", uinf, 0, 4); - if( (infoType & USERINFO_user) == USERINFO_user ) { - info_n = xmlNewChild(root_n, NULL, (xmlChar *) "flags", NULL); + uid = atoi_nullsafe(sqlite_get_value(uinf, 0, 4)); + username = sqlite_get_value(uinf, 0, 0); + + if( infoType & USERINFO_user ) { + info_n = xmlNewChild(user_n, NULL, (xmlChar *) "flags", NULL); + assert( info_n != NULL ); // set DEACTIVATED flag, if deactivated field is not NULL xml_set_flag(info_n, "DEACTIVATED", (sqlite_get_value(uinf, 0, 2) != NULL)); @@ -219,13 +168,13 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { xml_set_flag(info_n, "RSETLOGINCNT", ((atoi_nullsafe(sqlite_get_value(uinf,0, 7))==0) && (sqlite_get_value(uinf,0,3)) != NULL)); - sqlite_xml_value(root_n, XML_NODE, "activated", uinf, 0, 1); - sqlite_xml_value(root_n, XML_NODE, "deactivated", uinf, 0, 2); - info_n = sqlite_xml_value(root_n, XML_NODE, "last_accessed", uinf, 0, 3); - sqlite_xml_value(info_n, XML_ATTR, "logincount", uinf, 0, 7); + sqlite_xml_value(user_n, XML_NODE, "activated", uinf, 0, 1); + sqlite_xml_value(user_n, XML_NODE, "deactivated", uinf, 0, 2); + info_n = sqlite_xml_value(user_n, XML_NODE, "last_accessed", uinf, 0, 3); + sqlite_xml_value(user_n, XML_ATTR, "logincount", uinf, 0, 7); } - if( (infoType & USERINFO_certs) == USERINFO_certs ) { + if( infoType & USERINFO_certs ) { // Extract certificate info qres = sqlite_query(ctx, "SELECT depth, digest, common_name, organisation, email, " @@ -237,7 +186,8 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { " ON (uc.accessprofile = a.accessprofile)" " WHERE uid = '%i' ORDER BY c.certid DESC", uid); - info_n = xmlNewChild(root_n, NULL, (xmlChar *) "certificates", NULL); + info_n = xmlNewChild(user_n, NULL, (xmlChar *) "certificates", NULL); + assert( info_n != NULL ); if( (qres != NULL) && (sqlite_get_numtuples(qres) > 0) ) { int i; xmlNode *cert, *acpr; @@ -245,6 +195,7 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { for( i = 0; i < sqlite_get_numtuples(qres); i++ ) { cert = xmlNewChild(info_n, NULL, (xmlChar *) "certificate", NULL); + assert( cert != NULL ); sqlite_xml_value(cert, XML_ATTR, "certid", qres, 0, 6); sqlite_xml_value(cert, XML_ATTR, "depth", qres, 0, 0); @@ -266,19 +217,16 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { sqlite_xml_value(acpr, XML_ATTR, "fwdestination", qres, 0, 9); } } - - if( qres != NULL ) { - sqlite_free_results(qres); - } + sqlite_free_results(qres); } - if( (infoType & USERINFO_lastlog) == USERINFO_lastlog ) { + if( infoType & USERINFO_lastlog ) { int i = 0; xmlNode *lastl = NULL, *sess = NULL, *tmp1 = NULL, *tmp2 = NULL; xmlChar *tmp = NULL; qres = sqlite_query(ctx, - "SELECT llid, ll.certid, protocol, remotehost, remoteport, macaddr," + "SELECT llid, ll.certid,protocol,remotehost,remoteport,macaddr," " vpnipaddr, vpnipmask, sessionstatus, sessionkey," " login, logout, session_duration, session_deleted," " bytes_sent, bytes_received, uicid, accessprofile," @@ -287,7 +235,7 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { " FROM openvpn_lastlog ll" " LEFT JOIN openvpn_usercerts USING (uid, certid)" " LEFT JOIN openvpn_accesses USING (accessprofile)" - " LEFT JOIN openvpn_certificates cert ON (ll.certid = cert.certid)" + " LEFT JOIN openvpn_certificates cert ON(ll.certid=cert.certid)" " WHERE uid = '%i' ORDER BY login, logout", uid); if( qres == NULL ) { @@ -296,10 +244,12 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { return NULL; } - lastl = xmlNewChild(root_n, NULL, (xmlChar *) "lastlog", NULL); + lastl = xmlNewChild(user_n, NULL, (xmlChar *) "lastlog", NULL); for( i = 0; i < sqlite_get_numtuples(qres); i++ ) { sess = xmlNewChild(lastl, NULL, (xmlChar*) "session", NULL); + assert( sess != NULL ); + sqlite_xml_value(sess, XML_ATTR, "llid", qres, i, 0); xmlNewProp(sess, (xmlChar *) "session_status", (xmlChar *)SESSION_STATUS[atoi_nullsafe(sqlite_get_value(qres, i, 8))]); @@ -310,6 +260,7 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { sqlite_xml_value(sess, XML_NODE, "session_closed", qres, i, 13); tmp1 = xmlNewChild(sess, NULL, (xmlChar *) "connection", NULL); + assert( tmp1 != NULL ); sqlite_xml_value(tmp1, XML_ATTR, "bytes_sent", qres, i, 14); sqlite_xml_value(tmp1, XML_ATTR, "bytes_received", qres, i, 15); sqlite_xml_value(tmp1, XML_NODE, "protocol", qres, i, 2); @@ -320,6 +271,7 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { sqlite_xml_value(tmp1, XML_NODE, "vpn_netmask", qres, i, 7); tmp1 = xmlNewChild(sess, NULL, (xmlChar *) "certificate", NULL); + assert( tmp1 != NULL ); sqlite_xml_value(tmp1, XML_ATTR, "certid", qres, i, 1); sqlite_xml_value(tmp1, XML_ATTR, "uicid", qres, i, 16); sqlite_xml_value(tmp1, XML_ATTR, "depth", qres, i, 20); @@ -342,7 +294,7 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { sqlite_free_results(qres); } - if( (infoType & USERINFO_attempts) == USERINFO_attempts ) { + if( infoType & USERINFO_attempts ) { xmlNode *atmpt = NULL; qres = sqlite_query(ctx, @@ -357,7 +309,9 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { return NULL; } - atmpt = xmlNewChild(root_n, NULL, (xmlChar *) "attempts", NULL); + atmpt = xmlNewChild(user_n, NULL, (xmlChar *) "attempts", NULL); + assert( atmpt != NULL ); + if( sqlite_get_numtuples(qres) == 1 ) { sqlite_xml_value(atmpt, XML_ATTR, "atpid", qres, 0, 3); sqlite_xml_value(atmpt, XML_ATTR, "attempts", qres, 0, 0); @@ -367,7 +321,7 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { sqlite_free_results(qres); } - if( (infoType & USERINFO_blacklist) == USERINFO_blacklist ) { + if( infoType & USERINFO_blacklist ) { xmlNode *atmpt = NULL; qres = sqlite_query(ctx, @@ -382,7 +336,9 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { return NULL; } - atmpt = xmlNewChild(root_n, NULL, (xmlChar *) "blacklist", NULL); + atmpt = xmlNewChild(user_n, NULL, (xmlChar *) "blacklist", NULL); + assert( atmpt != NULL ); + if( sqlite_get_numtuples(qres) == 1 ) { sqlite_xml_value(atmpt, XML_ATTR, "blid", qres, 0, 2); sqlite_xml_value(atmpt, XML_NODE, "blacklisted", qres, 0, 0); @@ -391,103 +347,92 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int infoType, xmlDoc *srch) { sqlite_free_results(qres); } - sqlite_free_results(uinf); - return doc; - default: - sqlite_free_results(uinf); - eurephia_log(ctx, LOG_ERROR, 0, "Too many user records was found."); - return NULL; } + sqlite_free_results(uinf); + return doc; } /** - * @copydoc eDBadminAddUser() + * Internal function. Creates a new user account in the database. + * + * @param ctx eurephiaCTX + * @param usrinf_map eDBfieldMap containing information needed for the new user account + * + * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors, + * NULL is returned. */ -int eDBadminAddUser(eurephiaCTX *ctx, xmlDoc *userinfo) { +static xmlDoc *useracc_add(eurephiaCTX *ctx, eDBfieldMap *usrinf_map) { + xmlDoc *res_d = NULL; dbresult *res = NULL; - xmlNode *usrinf_n = NULL; - eDBfieldMap *usrinf_map = NULL; - int uid = 0; - DEBUG(ctx, 20, "Function call: eDBadminAddUser(ctx, xmlDoc)"); - assert( (ctx != NULL) && (userinfo != NULL) ); + DEBUG(ctx, 21, "Function call: useracc_add(ctx, eDBfieldMap)"); + assert( (ctx != NULL) && (usrinf_map != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, "eurephia admin function call attempted with wrong context type"); - return 0; - } - - // Get the add_user node, and then find the fieldMapping node - usrinf_n = eurephiaXML_getRoot(ctx, userinfo, "add_user", 1); - if( usrinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find proper add user XML document"); - return 0; - } - usrinf_n = xmlFindNode(usrinf_n, "fieldMapping"); - if( usrinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find proper add user XML document"); - return 0; + return NULL; } - // Get a proper field mapping to be used by the database - usrinf_map = eDBxmlMapping(ctx, tbl_sqlite_users, NULL, usrinf_n); - assert( usrinf_map != NULL ); - // Register the user res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_users", usrinf_map, NULL, NULL); - if( res == NULL ) { + if( (res == NULL) || (sqlite_get_affected_rows(res) == 0) ) { eurephia_log(ctx, LOG_FATAL, 0, "Could not register the new user account"); - uid = -1; + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + "Failed to register the user account"); } else { - uid = res->last_insert_id; + xmlChar *certid = malloc_nullsafe(ctx, 34); + xmlNode *info_n = NULL; + assert( certid != NULL ); + + // Prepare an information tag/node with the certid value + xmlStrPrintf(certid, 32, (xmlChar *) "%ld", res->last_insert_id); + info_n = xmlNewNode(NULL, (xmlChar *)"UserAccount"); + xmlNewProp(info_n, (xmlChar *) "mode", (xmlChar *) "add"); + xmlNewProp(info_n, (xmlChar *) "uid", certid); + + eurephia_log(ctx, LOG_INFO, 0, "New user account created (uid %i)", res->last_insert_id); + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, info_n, + "New user account created with uid %i", res->last_insert_id); + xmlFreeNode(info_n); } sqlite_free_results(res); eDBfreeMapping(usrinf_map); - return uid; + return res_d; } /** - * @copydoc eDBadminUpdateUser() - */ -int eDBadminUpdateUser(eurephiaCTX *ctx, const int uid, xmlDoc *userinfo) { + * Internal function. Updates a user account + * + * @param ctx eurephiaCTX + * @param uid Numeric user id to be updated + * @param value_map eDBfieldMap containing new values + * + * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors, + * NULL is returned. +*/ +static xmlDoc *useracc_update(eurephiaCTX *ctx, const int uid, eDBfieldMap *value_map) { dbresult *uinf = NULL; - xmlDoc *srch_xml = NULL; - xmlNode *root_n = NULL, *srch_n = NULL, *values_n = NULL; - eDBfieldMap *value_map = NULL, *srch_map = NULL; - xmlChar *xmluid = 0; + xmlDoc *res_d = NULL, *srch_xml = NULL; + xmlNode *srch_n = NULL; + xmlChar *xmluid = NULL; + eDBfieldMap *srch_map = NULL; - DEBUG(ctx, 20, "Function call: eDBadminUpdateUser(ctx, %i, xmlDoc)", uid); - assert( (ctx != NULL) && (userinfo != NULL) ); + DEBUG(ctx, 21, "Function call: useracc_update(ctx, %i, eDBfieldMap)", uid); + assert( (ctx != NULL) && (value_map != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, "eurephia admin function call attempted with wrong context type"); - return 0; - } - - // Get the update_user node - root_n = eurephiaXML_getRoot(ctx, userinfo, "update_user", 1); - if( root_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find proper XML element for user update"); - return 0; - } - - // Double check that we are going to update the right user - xmluid = (xmlChar *)xmlGetAttrValue(root_n->properties, "uid"); - if( atoi_nullsafe((char *)xmluid) != uid ) { - eurephia_log(ctx, LOG_ERROR, 0, "Mismatch between uid given as parameter and uid in XML"); - return 0; + return NULL; } - // Grab the fieldMapping node and create a eDBfieldMap structure for it - values_n = xmlFindNode(root_n, "fieldMapping"); - value_map = eDBxmlMapping(ctx, tbl_sqlite_users, NULL, values_n); - - // Create an eDBfieldMap structure for the srch_map (used for WHERE clause) + // Create an eDBfieldMap structure for the srch_map (to be used in the WHERE clause) + xmluid = (xmlChar *) malloc_nullsafe(ctx, 34); + xmlStrPrintf(xmluid, 32, (xmlChar *) "%ld", uid); eurephiaXML_CreateDoc(ctx, 1, "fieldMapping", &srch_xml, &srch_n); xmlNewProp(srch_n, (xmlChar *) "table", (xmlChar *) "users"); xmlNewChild(srch_n, NULL, (xmlChar *) "uid", xmluid); // Add uid as the only criteria @@ -499,28 +444,72 @@ int eDBadminUpdateUser(eurephiaCTX *ctx, const int uid, xmlDoc *userinfo) { if( uinf == NULL ) { eurephia_log(ctx, LOG_ERROR, 0, "Error querying the database for a user"); - return 0; + eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Failed to update user (uid %i)", uid); + } 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); + } else { + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, + "User account with uid %i is updated", uid); } sqlite_free_results(uinf); - - eDBfreeMapping(srch_map); - eDBfreeMapping(value_map); xmlFreeDoc(srch_xml); - return 1; + return res_d; } + /** - * @copydoc eDBadminDeleteUser() + * Internal function. Deletes a user account from the users table + * + * @param ctx eurephiaCTX + * @param uid Numeric user id of user to be deleted. + * + * @return Returns an eurephia ResultMsg XML document with status of the operation. On fatal errors, + * NULL is returned. */ -int eDBadminDeleteUser(eurephiaCTX *ctx, const int uid, xmlDoc *userinfo) { +static xmlDoc *useracc_delete(eurephiaCTX *ctx, const unsigned int uid) { + xmlDoc *res_d = NULL; dbresult *res = NULL; - xmlNode *usrinf_n = NULL; - char *uid_str = NULL; - int rc = 0; - DEBUG(ctx, 20, "Function call: eDBadminDeleteUser(ctx, %i, xmlDoc)", uid); - assert( (ctx != NULL) && (userinfo != NULL) ); + DEBUG(ctx, 21, "Function call: useracc_delete(ctx, %i)", uid); + assert( ctx != NULL ); + + if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { + eurephia_log(ctx, LOG_CRITICAL, 0, + "eurephia admin function call attempted with wrong context type"); + return NULL; + } + + // Delete the user + res = sqlite_query(ctx, "DELETE FROM openvpn_users WHERE uid = '%i'", uid); + if( res == NULL ) { + eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the user account (uid %i)", uid); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + "Failed to delete the user account (uid %i)", uid); + } 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); + } else { + res_d = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, + "User account with uid %i is deleted", uid); + } + sqlite_free_results(res); + return res_d; +} + + +/** + * @copydoc eDBadminUserAccount() + */ +xmlDoc *eDBadminUserAccount(eurephiaCTX *ctx, xmlDoc *qryxml) { + xmlDoc *res_d = NULL; + xmlNode *qry_n = NULL, *fmap_n = NULL; + eDBfieldMap *fmap_m = NULL; + char *mode = NULL; + int uid; + DEBUG(ctx, 20, "Function call: eDBadminUserAccount(ctx, xmlDoc)"); + assert( (ctx != NULL) && (qryxml != NULL) ); if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { eurephia_log(ctx, LOG_CRITICAL, 0, @@ -528,30 +517,55 @@ int eDBadminDeleteUser(eurephiaCTX *ctx, const int uid, xmlDoc *userinfo) { return 0; } - // Get the delete_user node - usrinf_n = eurephiaXML_getRoot(ctx, userinfo, "delete_user", 1); - if( usrinf_n == NULL ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find proper delete user XML document"); + qry_n = eurephiaXML_getRoot(ctx, qryxml, "admin_access", 1); + if( qry_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not find a valid XML for the user-certs link request"); + return 0; + } + mode = xmlGetAttrValue(qry_n->properties, "mode"); + if( mode == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Invalid edit admin access request (1)."); return 0; } - // Get the uid from the XML and compare it with the uid in the function argument - uid_str = xmlGetAttrValue(usrinf_n->properties, "uid"); - if( (uid_str == NULL) || (atoi_nullsafe(uid_str) != uid) ) { - eurephia_log(ctx, LOG_ERROR, 0, "Could not find proper delete user XML document. (uid mismatch)"); + fmap_n = xmlFindNode(qry_n, "fieldMapping"); + if( fmap_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Invalid edit admin access request (2)."); return 0; } - // Delete the user - res = sqlite_query(ctx, "DELETE FROM openvpn_users WHERE uid = '%i'", uid); - if( res == NULL ) { - eurephia_log(ctx, LOG_FATAL, 0, "Could not delete the user account"); - rc = 0; + // Extract the value of the uid attribute in the UserAccount tag. If not found, set value to -1. + uid = atoi_nullsafe(defaultValue(xmlGetAttrValue(qry_n->properties, "uid"), "-1")); + + fmap_m = eDBxmlMapping(ctx, tbl_sqlite_users, NULL, fmap_n); + assert(fmap_m != NULL); + + if( strcmp(mode, "view") == 0 ) { + unsigned int flags = atoi_nullsafe(defaultValue(xmlGetNodeContent(qry_n,"extractFlag"),"0")); + res_d = useracc_view(ctx, flags, fmap_m); + } else if( strcmp(mode, "add") == 0 ) { + res_d = useracc_add(ctx, fmap_m); + } else if( strcmp(mode, "update") == 0 ) { + if( uid == -1 ) { + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + "Can not update user account without an uid value"); + } else { + res_d = useracc_update(ctx, uid, fmap_m); + } + } else if( strcmp(mode, "delete") == 0 ) { + if( uid == -1 ) { + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, + "Can not delete user account without an uid value"); + } else { + res_d = useracc_delete(ctx, uid); + } } else { - rc = 1; + eurephia_log(ctx, LOG_ERROR, 0, "UserAccount - Unknown mode: '%s'", mode); + res_d = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode); } - sqlite_free_results(res); - return rc; + eDBfreeMapping(fmap_m); + + return res_d; } @@ -613,6 +627,7 @@ xmlDoc *adminacclvl_Get(eurephiaCTX *ctx, eDBfieldMap *fmap) { return doc; } + /** * @copydoc eDBadminAccessLevel() */ |
