summaryrefslogtreecommitdiffstats
path: root/database
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2009-09-24 23:59:29 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2009-09-24 23:59:29 +0200
commit78a4ff98774ef8487080fc4180e8fc77f187c293 (patch)
treeb2f4ff17ec223306905c74abaa4c4b0044ee94ce /database
parent9a2a8c5216d61dafb4232dba1ab56656390b61b0 (diff)
downloadeurephia-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')
-rw-r--r--database/eurephiadb.c6
-rw-r--r--database/eurephiadb_driver.h148
-rw-r--r--database/sqlite/administration/useraccount.c385
3 files changed, 236 insertions, 303 deletions
diff --git a/database/eurephiadb.c b/database/eurephiadb.c
index 6a12aea..e939725 100644
--- a/database/eurephiadb.c
+++ b/database/eurephiadb.c
@@ -115,11 +115,7 @@ int eDBlink_init(eurephiaCTX *ctx, const char *dbdriver, const int minver)
eDBadminConfiguration = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfiguration");
- eDBadminGetUserList = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserList");
- eDBadminGetUserInfo = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserInfo");
- eDBadminAddUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminAddUser");
- eDBadminUpdateUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUpdateUser");
- eDBadminDeleteUser = eGetSym(ctx, ctx->eurephia_driver, "eDBadminDeleteUser");
+ eDBadminUserAccount = eGetSym(ctx, ctx->eurephia_driver, "eDBadminUserAccount");
eDBadminCertificate = eGetSym(ctx, ctx->eurephia_driver, "eDBadminCertificate");
diff --git a/database/eurephiadb_driver.h b/database/eurephiadb_driver.h
index e9c757c..2c8d3b5 100644
--- a/database/eurephiadb_driver.h
+++ b/database/eurephiadb_driver.h
@@ -439,129 +439,47 @@ xmlDoc *(*eDBadminConfiguration)(eurephiaCTX *ctx, xmlDoc *cfgxml);
/**
- * Retrieve a list over all users in the database.
+ * Function for view, add, update or delete user accounts
*
- * @version API version level 2
- * @param ctx eurephiaCTX
- * @param sortkeys String containing the sort order of the fields
- *
- * @return Returns an XML document on success with all users, otherwise NULL
- * @see eurephiaXML_CreateDoc(), eurephiaXML_getRoot()
- */
-xmlDoc *(*eDBadminGetUserList) (eurephiaCTX *ctx, const char *sortkeys);
-
-
-/**
- * This function will search up a user, based on information given in a fieldMapping structure.
- * It will return an XML document containing the user information requested, controlled by the
- * infoType flag. These flags are defined in eurephiadb_driver.h
- *
- * @version API version level 2
- * @param ctx eurephiaCTX
- * @param infoType Flags of what information to extract. Valid flags are: USERINFO_user,
- * USERINFO_certs, USERINFO_lastlog, USERINFO_attempts, USERINFO_blacklist.
- * These flags can be bit-wise OR'ed together to extract more information
- * at once.
- *
- * @param srch XML document describing the search criteria
- *
- * Skeleton of an XML search document
- * @code
- * <eurephia format="1">
- * <fieldMapping table="users">
- * <{search field}>{search value}</{search field}>
- * </fieldMapping>
- * </eurephia>
- * @endcode
- *
- * @return Returns an XML document containing the requested user information on success, otherwise
- * NULL is returned.
- * @see eurephiaXML_CreateDoc(), eurephiaXML_getRoot()
- * @see USERINFO_user, USERINFO_certs, USERINFO_lastlog, USERINFO_attempts, USERINFO_blacklist
- */
-xmlDoc *(*eDBadminGetUserInfo) (eurephiaCTX *ctx, int infoType, xmlDoc *srch);
-
-
-/**
- * This function will add a user to the openvpn_users table, based on the
- * XML document given.
- *
- * @version API version level 2
* @param ctx eurephiaCTX
- * @param userinfo XML document containing information about the new user
+ * @param qryxml XML document with information about the operation
*
- * Skeleton of an XML document for adding a new user
- * @code
- * <eurephia format="1">
- * <add_user>
- * <fieldMapping table="users">
- * <username>{user name}</username>
- * <password pwhash="{none|sha512}">{password}</password>
- * </fieldMapping>
- * </add_user>
- * </eurephia>
- * @endcode
- *
- * The password tag can use either a clear-text password (by setting pwhash="none") or a pre-hashed
- * SHA512 password (by setting pwhash="sha512"). Beware that the SHA512 hash must be hashed with
- * the eurephia_pwd_crypt() function.
- *
- * @return Returns the user ID (uid) of the new user account on success, otherwise -1 is returned.
- * @see eurephiaXML_CreateDoc(), eurephia_pwd_crypt()
- */
-int (*eDBadminAddUser) (eurephiaCTX *ctx, xmlDoc *userinfo);
-
-
-/**
- * This function will update a user account based on the XML document sent in as a parameter.
- * The function will double check that the uid in the argument list and the uid in the XML
- * document is coherent.
- *
- * @version API version level 2
- * @param ctx eurephiaCTX
- * @param uid Numeric user ID of the user account being updated
- * @param userinfo XML document containing the new information for the user account
- *
- * Skeleton of an XML document updating a user
* @code
* <eurephia format="1">
- * <update_user uid="{uid}">
- * <fieldMapping table="users">
- * <{field name}>{new value}</{field name}>
- * </fieldMapping>
- * </update_user>
+ * <UserAccount mode="{view|add|update|delete}" [uid="{uid}"]>
+ * <fieldMapping table="users">
+ * <{field name}>{field value}</{field name}>
+ * ...
+ * <{field name}>{field value}</{field name}>
+ * </fieldMapping>
+ * [<sortkeys>{field name}[, {field name}...]</sortkeys>]
+ * [<extractFlags>{extract value (int)}</extractFlags>]
+ * </UserAccount>
* </eurephia>
* @endcode
- * @remarks Beware that the uid attribute in the update_user tag must be the same user ID given as
- * argument to the function.
+ * Valid field names are: uid, username, password, activated, deactivated, lastaccess
*
- * @return Returns 1 on success, otherwise 0.
- * @see eurephiaXML_CreateDoc()
- */
-int (*eDBadminUpdateUser) (eurephiaCTX *ctx, const int uid, xmlDoc *userinfo);
-
-
-/**
- * This function will delete a user to the openvpn_users table, based on the
- * XML document given.
+ * If no field names and values are given in "view" mode, all user accounts will be returned. Adding
+ * field name tags will narrow down the query.
*
- * @version API version level 2
- * @param ctx eurephiaCTX
- * @param uid Numeric user ID of the user account being updated
- * @param userinfo XML document containing information about the account which is going to be deleted
+ * For "add" mode, username and password tag is required. For the password tag, the attribute "pwhash"
+ * can be given to indicate what kind of hashing the value provided is using. If this is not set, the
+ * password value is expected to come in clear text and will be hashed automatically with a SHA512
+ * algorithm.
*
- * @code
- * <eurephia format="1">
- * <delete_user uid="{uid}"/>
- * </eurephia>
- * @endcode
- * @remarks The uid of the account to be deleted must also be sent
- * as a separate parameter, as a security feature
+ * For the "update" mode, the uid attribute in the UserAccount tag is required. This will be the
+ * key for which record to update. The values being updated need to be set in the fieldMapping tags.
*
- * @return Returns 1 on success, otherwise 0.
- * @see eurephiaXML_CreateDoc()
+ * For "delete" mode, the "uid" attribute in the UserAccount tag is required. This mode will also ignore
+ * any fieldMapping tags.
+ *
+ * @return When mode is "view", it will return an XML document with user account information on success.
+ * On errors in "view" mode or the other modes in general, it will return an eurephia ResultMsg
+ * XML document with the result of the operation, or NULL on fatal errors.
+ *
+ * @see eurephiaXML_CreateDoc(), eurephiaXML_ParseResultMsg(), eurephiaXML_getRoot()
*/
-int (*eDBadminDeleteUser) (eurephiaCTX *ctx, const int uid, xmlDoc *userinfo);
+xmlDoc *(*eDBadminUserAccount) (eurephiaCTX *ctx, xmlDoc *qryxml);
/**
@@ -569,7 +487,7 @@ int (*eDBadminDeleteUser) (eurephiaCTX *ctx, const int uid, xmlDoc *userinfo);
*
* @version API version level 2
* @param ctx eurephiaCTX
- * @param qryxml XML document with the certificate information to be deleted
+ * @param qryxml XML document with information about the operation
*
* Skeleton of an XML document used for eDBadminCertificate()
* @code
@@ -580,13 +498,17 @@ int (*eDBadminDeleteUser) (eurephiaCTX *ctx, const int uid, xmlDoc *userinfo);
* ...
* <{field name}>{field value}</{field name}>
* </fieldMapping>
- * </certificate_info>
+ * [<sortkeys>{field name}[, {field name}...]</sortkeys>]
+ * </certificates>
* </eurephia>
* @endcode
* Valid field names are: depth, digest, common_name, org, email and certid.
* For list and delete mode, all field names can be used to narrow the search query.
* In register mode all fields are required, except certid which must not be given.
*
+ * The sortkeys tag will only be used in list mode, and it takes a list of comma separated
+ * field names.
+ *
* @return When mode is "list", it will return an XML document with all found certificates, otherwise NULL.
* If mode is "register" or "delete" an eurephia ResultMsg XML document will be returned with
* the result of the operation, or NULL on fatal errors.
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()
*/