diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-13 21:46:38 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-09-13 21:46:38 +0200 |
commit | 5d0772225abdff04580a14128b8a86d2dc4f4fd4 (patch) | |
tree | bdf7a7c33a2bcb11b50fafde750481a70ce680a3 /database/sqlite/administration/configuration.c | |
parent | 76e216dd61168b2e75435ff67f26fe02bdc93187 (diff) | |
download | eurephia-5d0772225abdff04580a14128b8a86d2dc4f4fd4.tar.gz eurephia-5d0772225abdff04580a14128b8a86d2dc4f4fd4.tar.xz eurephia-5d0772225abdff04580a14128b8a86d2dc4f4fd4.zip |
Moved eDBadminConfigSet() and eDBadminConfigDelete() into its own file
Diffstat (limited to 'database/sqlite/administration/configuration.c')
-rw-r--r-- | database/sqlite/administration/configuration.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/database/sqlite/administration/configuration.c b/database/sqlite/administration/configuration.c new file mode 100644 index 0000000..d2837f0 --- /dev/null +++ b/database/sqlite/administration/configuration.c @@ -0,0 +1,110 @@ +/* administration.c -- Functions for setting and deleting + * configuration parameters in the database + * + * GPLv2 only - Copyright (C) 2008, 2009 + * David Sommerseth <dazo@users.sourceforge.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +/** + * @file configuration.c + * @author David Sommerseth <dazo@users.sourceforge.net> + * @date 2009-09-13 + * + * @brief Functions for setting and deleting configuration + * parameters in the database + * + */ + +#include <string.h> +#include <unistd.h> +#include <assert.h> +#include <sqlite3.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephia_log.h> +#include <eurephia_values.h> + +#include "../sqlite.h" + +/** + * @copydoc eDBadminConfigSet() + */ +int eDBadminConfigSet(eurephiaCTX *ctx, const char *key, const char *val) { + dbresult *res = NULL; + int found = 0; + + DEBUG(ctx, 20, "Function call: eDBadminConfigSet(ctx, '%s', '%s')", key, val); + assert((ctx != NULL) && (ctx->dbc != 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; + } + + res = sqlite_query(ctx, "SELECT count(*) FROM openvpn_config WHERE datakey = '%q'", key); + if( !res ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not query configuration table"); + return 0; + } + found = atoi_nullsafe(sqlite_get_value(res, 0, 0)); + sqlite_free_results(res); + + if( found == 0 ) { + res = sqlite_query(ctx, + "INSERT INTO openvpn_config (datakey, dataval) VALUES ('%q','%q')", + key, val); + } else { + res = sqlite_query(ctx, "UPDATE openvpn_config SET dataval = '%q' WHERE datakey = '%q'", + val, key); + } + + if( res == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Could not register configuration entry (%s = '%s'", key, val); + return 0; + } + sqlite_free_results(res); + eAdd_value(ctx, ctx->dbc->config, key, val); + return 1; +} + + +/** + * @copydoc eDBadminConfigDelete() + */ +int eDBadminConfigDelete(eurephiaCTX *ctx, const char *key) { + dbresult *res = NULL; + + DEBUG(ctx, 20, "Function call: eDBadminConfigDelete(ctx, '%s') ", key); + assert((ctx != NULL) && (ctx->dbc != 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; + } + + 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); + return 1; +} |