summaryrefslogtreecommitdiffstats
path: root/database
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2009-09-14 19:46:45 +0200
committerDavid Sommerseth <dazo@users.sourceforge.net>2009-09-14 19:46:45 +0200
commitacd6fad0322c9e078fe17c55cffc428a15efe66f (patch)
treed0985d9185da04d0b0bf5fc5829c0af86da4259b /database
parent4fac4f2b5af3d46d39d1ab36f38f10862544c783 (diff)
downloadeurephia-acd6fad0322c9e078fe17c55cffc428a15efe66f.tar.gz
eurephia-acd6fad0322c9e078fe17c55cffc428a15efe66f.tar.xz
eurephia-acd6fad0322c9e078fe17c55cffc428a15efe66f.zip
Unified database driver functions eDBadminConfigSet() and eDBadminConfigDelete() into eDBadminConfiguration()
Diffstat (limited to 'database')
-rw-r--r--database/eurephiadb.c4
-rw-r--r--database/eurephiadb_driver.h37
-rw-r--r--database/sqlite/administration/configuration.c139
3 files changed, 151 insertions, 29 deletions
diff --git a/database/eurephiadb.c b/database/eurephiadb.c
index 7b593fc..8109675 100644
--- a/database/eurephiadb.c
+++ b/database/eurephiadb.c
@@ -113,8 +113,7 @@ int eDBlink_init(eurephiaCTX *ctx, const char *dbdriver, const int minver)
eDBadminRegisterLogin = eGetSym(ctx, ctx->eurephia_driver, "eDBadminRegisterLogin");
eDBadminLogout = eGetSym(ctx, ctx->eurephia_driver, "eDBadminLogout");
- eDBadminConfigSet = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfigSet");
- eDBadminConfigDelete = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfigDelete");
+ eDBadminConfiguration = eGetSym(ctx, ctx->eurephia_driver, "eDBadminConfiguration");
eDBadminGetUserList = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserList");
eDBadminGetUserInfo = eGetSym(ctx, ctx->eurephia_driver, "eDBadminGetUserInfo");
@@ -177,4 +176,3 @@ int eDBlink_init(eurephiaCTX *ctx, const char *dbdriver, const int minver)
}
return 1;
}
-
diff --git a/database/eurephiadb_driver.h b/database/eurephiadb_driver.h
index 5a3d89a..4640ba6 100644
--- a/database/eurephiadb_driver.h
+++ b/database/eurephiadb_driver.h
@@ -413,33 +413,30 @@ int (*eDBadminRegisterLogin) (eurephiaCTX *ctx, eurephiaSESSION *session);
int (*eDBadminLogout) (eurephiaCTX *ctx, const char *sessionkey);
+#ifdef HAVE_LIBXML2
/**
- * Sets a configuration parameter in the database. If the key already exists, it will
- * be replaced. This operation will immediately update the in-memory copy of the setting.
+ * Set or delete configuration parameters in the database. This operation will
+ * also update the in-memory copy of the configuration
*
- * @version API version level 2
- * @param ctx eurephiaCTX
- * @param key String (char *) containing the key name of the value
- * @param val String (char *) with the value to be stored
- *
- * @return Returns 1 on success, otherwise 0
- */
-int (*eDBadminConfigSet) (eurephiaCTX *ctx, const char *key, const char *val);
-
-
-/**
- * Deletes a configuration parameter from the database.
+ * @param ctx eurephiaCTX
+ * @param cfgxml XML document specifying the operation
*
- * @version API version level 2
- * @param ctx eurephiaCTX
- * @param key String (char *) containing the key to be deleted.
+ * XML format skeleton for setting or deleting configuration parameters
+ * @code
+ * <eurephia format="1">
+ * <configuration>
+ * <set key="{name of the key}">{Value to be assigned}</set>
+ * <delete key="{name of the key"}/>
+ * </configuration>
+ * </eurephia>
+ * @endcode
+ * Only one operation can be called per request to this function.
*
- * @return Returns 1 on success, otherwise 0
+ * @return Returns a valid XML document with information about the operation, or NULL on fatal errors.
*/
-int (*eDBadminConfigDelete) (eurephiaCTX *ctx, const char *key);
+xmlDoc *(*eDBadminConfiguration)(eurephiaCTX *ctx, xmlDoc *cfgxml);
-#ifdef HAVE_LIBXML2
/**
* Retrieve a list over all users in the database.
*
diff --git a/database/sqlite/administration/configuration.c b/database/sqlite/administration/configuration.c
index d2837f0..e27b257 100644
--- a/database/sqlite/administration/configuration.c
+++ b/database/sqlite/administration/configuration.c
@@ -1,4 +1,4 @@
-/* administration.c -- Functions for setting and deleting
+/* configuration.c -- Functions for setting and deleting
* configuration parameters in the database
*
* GPLv2 only - Copyright (C) 2008, 2009
@@ -21,7 +21,7 @@
*/
/**
- * @file configuration.c
+ * @file sqlite/administration/configuration.c
* @author David Sommerseth <dazo@users.sourceforge.net>
* @date 2009-09-13
*
@@ -39,13 +39,22 @@
#include <eurephia_context.h>
#include <eurephia_log.h>
#include <eurephia_values.h>
+#include <eurephia_xml.h>
#include "../sqlite.h"
/**
- * @copydoc eDBadminConfigSet()
+ * Internal function. Sets a configuration parameter in the database. If the key already
+ * exists, it will be replaced. This operation will immediately update the in-memory copy
+ * of the setting.
+ *
+ * @param ctx eurephiaCTX
+ * @param key String (char *) containing the key name of the value
+ * @param val String (char *) with the value to be stored
+ *
+ * @return Returns 1 on success, otherwise 0
*/
-int eDBadminConfigSet(eurephiaCTX *ctx, const char *key, const char *val) {
+static int config_set(eurephiaCTX *ctx, const char *key, const char *val) {
dbresult *res = NULL;
int found = 0;
@@ -86,10 +95,16 @@ int eDBadminConfigSet(eurephiaCTX *ctx, const char *key, const char *val) {
/**
- * @copydoc eDBadminConfigDelete()
+ * Internal function. Deletes a configuration parameter from the database.
+ *
+ * @param ctx eurephiaCTX
+ * @param key String (char *) containing the key to be deleted.
+ *
+ * @return Returns 1 on success, otherwise 0
*/
-int eDBadminConfigDelete(eurephiaCTX *ctx, const char *key) {
+static int config_delete(eurephiaCTX *ctx, const char *key) {
dbresult *res = NULL;
+ eurephiaVALUES *cfgptr = NULL;
DEBUG(ctx, 20, "Function call: eDBadminConfigDelete(ctx, '%s') ", key);
assert((ctx != NULL) && (ctx->dbc != NULL));
@@ -100,11 +115,123 @@ int eDBadminConfigDelete(eurephiaCTX *ctx, const char *key) {
return 0;
}
+ // Find the config parameter in the in-memory stack
+ cfgptr = eGet_valuestruct(ctx->dbc->config, key);
+ if( cfgptr == NULL ) {
+ eurephia_log(ctx, LOG_WARNING, 0,
+ "Could not find the configuration parameter '%s'", key);
+ return 0;
+ }
+
+ // Delete the config parameter from the database
res = sqlite_query(ctx, "DELETE FROM openvpn_config WHERE datakey = '%q'", key);
if( !res ) {
eurephia_log(ctx, LOG_ERROR, 0, "Could delete config configuration entry (%s)", key);
return 0;
}
sqlite_free_results(res);
+
+ // Since the database call worked, remove it from memory as well
+ ctx->dbc->config = eRemove_value(ctx, ctx->dbc->config, cfgptr->evgid, cfgptr->evid);
+
return 1;
}
+
+
+/**
+ * Internal function. Validates if key or value strings are NULL or not.
+ *
+ * @param ctx eurephiaCTX
+ * @param key char pointer to the key string
+ * @param value char pointer to the value string
+ *
+ * @return Returns NULL if key and value is not NULL. Otherwise an eurephia XML document with an error
+ * description will be returned.
+ */
+xmlDoc *validate_key_value(eurephiaCTX *ctx, const char *key, const char *value) {
+ int k_null = 0, v_null = 0;
+
+ k_null = (key == NULL ? 1 : 0);
+ v_null = (value == NULL ? 1 : 0);
+
+ if( k_null || v_null ) {
+ return eurephiaXML_ResultMsg(ctx, exmlERROR, "%s%s%s",
+ (k_null ? "The key attribute was not set" : ""),
+ (k_null && v_null ? " and " : ""),
+ (v_null ? "The value tag was not set" : "")
+ );
+ }
+ return NULL;
+}
+
+/**
+ * @copydoc eDBadminConfiguration()
+ */
+xmlDoc *eDBadminConfiguration(eurephiaCTX *ctx, xmlDoc *cfgxml) {
+ xmlDoc *resxml = NULL;
+ xmlNode *root_n = NULL, *cfg_n = NULL;
+ char *key = NULL, *value = NULL;
+
+ DEBUG(ctx, 20, "Function call: eDBadminConfiguration(ctx, {xmlDoc})");
+ assert( (ctx != NULL) && (cfgxml != 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;
+ }
+
+ root_n = eurephiaXML_getRoot(ctx, cfgxml, "configuration", 1);
+ if( root_n == NULL ) {
+ eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
+ return NULL;
+ }
+
+ // Look if we recevied a <set> tag
+ cfg_n = xmlFindNode(root_n, "set");
+ if( cfg_n != NULL ) {
+ key = xmlGetAttrValue(cfg_n->properties, "key");
+ value = xmlExtractContent(cfg_n);
+
+ resxml = validate_key_value(ctx, key, value);
+ if( resxml ) {
+ // Input data was not approved, and we have an error message
+ return resxml;
+ }
+
+ if( config_set(ctx, key, value) ) {
+ resxml = eurephiaXML_ResultMsg(ctx, exmlRESULT,
+ "Configuration key '%s' was set to '%s'",
+ key, value);
+ } else {
+ resxml = eurephiaXML_ResultMsg(ctx, exmlERROR,
+ "Failed to set the key '%s' to '%s'",
+ key, value);
+ }
+ return resxml;
+ };
+
+ // If not, look if we recevied a <delete> tag
+ cfg_n = xmlFindNode(root_n, "delete");
+ if( cfg_n != NULL ) {
+ key = xmlGetAttrValue(cfg_n->properties, "key");
+
+ resxml = validate_key_value(ctx, key, ""); // Do not use NULL for value
+ if( resxml ) {
+ // Input data was not approved, and we have an error message
+ return resxml;
+ }
+
+ if( config_delete(ctx, key) ) {
+ resxml = eurephiaXML_ResultMsg(ctx, exmlRESULT,
+ "Configuration key '%s' was deleted", key);
+ } else {
+ resxml = eurephiaXML_ResultMsg(ctx, exmlERROR,
+ "Failed to delete the key '%s'", key);
+ }
+ return resxml;
+ }
+
+ // If not, it's an invalid input
+ return eurephiaXML_ResultMsg(ctx, exmlERROR, "Unkown XML tag received");
+}