diff options
| author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-18 10:53:02 +0100 |
|---|---|---|
| committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-18 10:53:02 +0100 |
| commit | f5ca695e325ce546ba54f3f8b46a01c23ee8f9b1 (patch) | |
| tree | 894e485b1f3a54e7e87604f0f16e6e3e0e996635 /database/sqlite | |
| parent | a053a6580b09f1d22eed54f4ebcc09ccfd0f62cd (diff) | |
| download | eurephia-f5ca695e325ce546ba54f3f8b46a01c23ee8f9b1.tar.gz eurephia-f5ca695e325ce546ba54f3f8b46a01c23ee8f9b1.tar.xz eurephia-f5ca695e325ce546ba54f3f8b46a01c23ee8f9b1.zip | |
Added SQLite3 admin functions for adding and deleting users
Diffstat (limited to 'database/sqlite')
| -rw-r--r-- | database/sqlite/administration.c | 92 |
1 files changed, 90 insertions, 2 deletions
diff --git a/database/sqlite/administration.c b/database/sqlite/administration.c index 6c586ec..af8612b 100644 --- a/database/sqlite/administration.c +++ b/database/sqlite/administration.c @@ -551,8 +551,56 @@ xmlDoc *eDBadminGetUserInfo(eurephiaCTX *ctx, int getInfo, xmlDoc *srch) { } } + +// This function will add a user to the openvpn_users table, based on the +// XML document given. +// +// XML format: +// <eurephia format="1"> +// <add_user> +// <fieldMapping table="users"> +// <username>{user name}</username> +// <password pwhash="{none|sha512}">{password}</password>" +// </fieldMapping> +// </add_user> +// </eurephia> +// int eDBadminAddUser(eurephiaCTX *ctx, xmlDoc *usrinf) { - return 0; + dbresult *res = NULL; + xmlNode *usrinf_n = NULL; + eDBfieldMap *usrinf_map = NULL; + int rc = 0; + + assert( (ctx != NULL) && (usrinf != NULL) ); + + // Get the add_user node, and then find the fieldMapping node + usrinf_n = eurephiaXML_getRoot(ctx, usrinf, "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; + } + + // 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); + if( res == NULL ) { + eurephia_log(ctx, LOG_FATAL, 0, "Could not register the new user account"); + rc = 0; + } else { + rc = 1; + } + sqlite_free_results(res); + eDBfreeMapping(usrinf_map); + + return rc; } @@ -575,6 +623,7 @@ int eDBadminUpdateUser(eurephiaCTX *ctx, const int uid, xmlDoc *usrinf) { xmlNode *root_n = NULL, *srch_n = NULL, *values_n = NULL; eDBfieldMap *value_map = NULL, *srch_map = NULL; xmlChar *xmluid = 0; + assert( (ctx != NULL) && (usrinf != NULL) ); // Get the update_user node @@ -618,8 +667,47 @@ int eDBadminUpdateUser(eurephiaCTX *ctx, const int uid, xmlDoc *usrinf) { return 1; } +// This function will delete a user to the openvpn_users table, based on the +// XML document given. The uid of the account to be deleted must also be sent +// as a separate parameter, as a security feature +// +// XML format: +// <eurephia format="1"> +// <delete_user uid="{uid}"/> +// </eurephia> +// int eDBadminDeleteUser(eurephiaCTX *ctx, const int uid, xmlDoc *usrinf) { - return 0; + dbresult *res = NULL; + xmlNode *usrinf_n = NULL; + char *uid_str = NULL; + int rc = 0; + + assert( (ctx != NULL) && (usrinf != NULL) ); + + // Get the delete_user node + usrinf_n = eurephiaXML_getRoot(ctx, usrinf, "delete_user", 1); + if( usrinf_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not find proper delete user XML document"); + 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)"); + 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; + } else { + rc = 1; + } + sqlite_free_results(res); + return rc; } |
