configuration.c

Go to the documentation of this file.
00001 /* configuration.c   --  Functions for setting and deleting
00002  *                       configuration parameters in the database
00003  *
00004  *  GPLv2 only - Copyright (C) 2008 - 2010
00005  *               David Sommerseth <dazo@users.sourceforge.net>
00006  *
00007  *  This program is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU General Public License
00009  *  as published by the Free Software Foundation; version 2
00010  *  of the License.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  *
00021  */
00022 
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <assert.h>
00036 #include <sqlite3.h>
00037 
00038 #include <eurephia_nullsafe.h>
00039 #include <eurephia_context.h>
00040 #include <eurephia_log.h>
00041 #include <eurephia_values.h>
00042 #include <eurephia_xml.h>
00043 
00044 #include "../sqlite.h"
00045 
00057 static int config_set(eurephiaCTX *ctx, const char *key, const char *val) {
00058         dbresult *res = NULL;
00059         int found = 0;
00060 
00061         DEBUG(ctx, 20, "Function call: eDBadminConfigSet(ctx, '%s', '%s')", key, val);
00062         assert((ctx != NULL) && (ctx->dbc != NULL));
00063 
00064         if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
00065                 eurephia_log(ctx, LOG_CRITICAL, 0,
00066                              "eurephia admin function call attempted with wrong context type");
00067                 return 0;
00068         }
00069 
00070         res = sqlite_query(ctx, "SELECT count(*) FROM openvpn_config WHERE datakey = '%q'", key);
00071         if( !res ) {
00072                 eurephia_log(ctx, LOG_ERROR, 0, "Could not query configuration table");
00073                 return 0;
00074         }
00075         found = atoi_nullsafe(sqlite_get_value(res, 0, 0));
00076         sqlite_free_results(res);
00077 
00078         if( found == 0 ) {
00079                 res = sqlite_query(ctx,
00080                                    "INSERT INTO openvpn_config (datakey, dataval) VALUES ('%q','%q')",
00081                                    key, val);
00082         } else {
00083                 res = sqlite_query(ctx, "UPDATE openvpn_config SET dataval = '%q' WHERE datakey = '%q'",
00084                                    val, key);
00085         }
00086 
00087         if( res == NULL ) {
00088                 eurephia_log(ctx, LOG_ERROR, 0, "Could not register configuration entry (%s = '%s'", key, val);
00089                 return 0;
00090         }
00091         sqlite_free_results(res);
00092         eAdd_value(ctx, ctx->dbc->config, key, val);
00093         return 1;
00094 }
00095 
00096 
00105 static int config_delete(eurephiaCTX *ctx, const char *key) {
00106         dbresult *res = NULL;
00107         eurephiaVALUES *cfgptr = NULL;
00108 
00109         DEBUG(ctx, 20, "Function call: eDBadminConfigDelete(ctx, '%s') ", key);
00110         assert((ctx != NULL) && (ctx->dbc != NULL));
00111 
00112         if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
00113                 eurephia_log(ctx, LOG_CRITICAL, 0,
00114                              "eurephia admin function call attempted with wrong context type");
00115                 return 0;
00116         }
00117 
00118         // Find the config parameter in the in-memory stack
00119         cfgptr = eGet_valuestruct(ctx->dbc->config, key);
00120         if( cfgptr == NULL ) {
00121                 eurephia_log(ctx, LOG_WARNING, 0,
00122                              "Could not find the configuration parameter '%s'", key);
00123                 return 0;
00124         }
00125 
00126         // Delete the config parameter from the database
00127         res = sqlite_query(ctx, "DELETE FROM openvpn_config WHERE datakey = '%q'", key);
00128         if( !res ) {
00129                 eurephia_log(ctx, LOG_ERROR, 0, "Could delete config configuration entry (%s)", key);
00130                 return 0;
00131         }
00132         sqlite_free_results(res);
00133 
00134         // Since the database call worked, remove it from memory as well
00135         ctx->dbc->config = eRemove_value(ctx, ctx->dbc->config, cfgptr->evgid, cfgptr->evid);
00136 
00137         return 1;
00138 }
00139 
00140 
00151 xmlDoc *validate_key_value(eurephiaCTX *ctx, const char *key, const char *value) {
00152         int k_null = 0, v_null = 0;
00153 
00154         k_null = (key == NULL ? 1 : 0);
00155         v_null = (value == NULL ? 1 : 0);
00156 
00157         if( k_null || v_null ) {
00158                 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "%s%s%s",
00159                                              (k_null ? "The key attribute was not set" : ""),
00160                                              (k_null && v_null ? " and " : ""),
00161                                              (v_null ? "The value tag was not set" : "")
00162                                              );
00163         }
00164         return NULL;
00165 }
00166 
00170 xmlDoc *eDBadminConfiguration(eurephiaCTX *ctx, xmlDoc *cfgxml) {
00171         xmlDoc *resxml = NULL;
00172         xmlNode *root_n = NULL, *cfg_n = NULL;
00173         char *key = NULL, *value = NULL;
00174 
00175         DEBUG(ctx, 20, "Function call: eDBadminConfiguration(ctx, {xmlDoc})");
00176         assert( (ctx != NULL) && (cfgxml != NULL) );
00177 
00178         if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
00179                 eurephia_log(ctx, LOG_CRITICAL, 0,
00180                              "eurephia admin function call attempted with wrong context type");
00181                 return NULL;
00182         }
00183 
00184         root_n = eurephiaXML_getRoot(ctx, cfgxml, "configuration", 1);
00185         if( root_n == NULL ) {
00186                 eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
00187                 return NULL;
00188         }
00189 
00190         // Look if we recevied a <set> tag
00191         cfg_n = xmlFindNode(root_n, "set");
00192         if( cfg_n != NULL ) {
00193                 key = xmlGetAttrValue(cfg_n->properties, "key");
00194                 value = xmlExtractContent(cfg_n);
00195 
00196                 resxml = validate_key_value(ctx, key, value);
00197                 if( resxml ) {
00198                         // Input data was not approved, and we have an error message
00199                         return resxml;
00200                 }
00201 
00202                 if( config_set(ctx, key, value) ) {
00203                         resxml = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
00204                                                        "Configuration key '%s' was set to '%s'",
00205                                                        key, value);
00206                 } else {
00207                         resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
00208                                                        "Failed to set the key '%s' to '%s'",
00209                                                        key, value);
00210                 }
00211                 return resxml;
00212         };
00213 
00214         // If not, look if we recevied a <delete> tag
00215         cfg_n = xmlFindNode(root_n, "delete");
00216         if( cfg_n != NULL ) {
00217                 key = xmlGetAttrValue(cfg_n->properties, "key");
00218 
00219                 resxml = validate_key_value(ctx, key, ""); // Do not use NULL for value
00220                 if( resxml ) {
00221                         // Input data was not approved, and we have an error message
00222                         return resxml;
00223                 }
00224 
00225                 if( config_delete(ctx, key) ) {
00226                         resxml = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL,
00227                                                        "Configuration key '%s' was deleted", key);
00228                 } else {
00229                         resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
00230                                                        "Failed to delete the key '%s'", key);
00231                 }
00232                 return resxml;
00233         }
00234 
00235         // If not, it's an invalid input
00236         return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unkown XML tag received");
00237 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines