| 1 |
/* configuration.c -- Functions for setting and deleting |
| 2 |
* configuration parameters in the database |
| 3 |
* |
| 4 |
* GPLv2 only - Copyright (C) 2008 - 2012 |
| 5 |
* David Sommerseth <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License |
| 9 |
* as published by the Free Software Foundation; version 2 |
| 10 |
* of the License. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* @file sqlite/administration/configuration.c |
| 25 |
* @author David Sommerseth <[email protected]> |
| 26 |
* @date 2009-09-13 |
| 27 |
* |
| 28 |
* @brief Functions for setting and deleting configuration |
| 29 |
* parameters in the database |
| 30 |
* |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <string.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <assert.h> |
| 36 |
#include <sqlite3.h> |
| 37 |
|
| 38 |
#include <eurephia_nullsafe.h> |
| 39 |
#include <eurephia_context.h> |
| 40 |
#include <eurephia_log.h> |
| 41 |
#include <eurephia_values.h> |
| 42 |
#include <eurephia_xml.h> |
| 43 |
|
| 44 |
#include "../sqlite.h" |
| 45 |
|
| 46 |
/** |
| 47 |
* Internal function. Sets a configuration parameter in the database. If the key already |
| 48 |
* exists, it will be replaced. This operation will immediately update the in-memory copy |
| 49 |
* of the setting. |
| 50 |
* |
| 51 |
* @param ctx eurephiaCTX |
| 52 |
* @param key String (char *) containing the key name of the value |
| 53 |
* @param val String (char *) with the value to be stored |
| 54 |
* |
| 55 |
* @return Returns 1 on success, otherwise 0 |
| 56 |
*/ |
| 57 |
static int config_set(eurephiaCTX *ctx, const char *key, const char *val) { |
| 58 |
dbresult *res = NULL; |
| 59 |
int found = 0; |
| 60 |
|
| 61 |
DEBUG(ctx, 20, "Function call: eDBadminConfigSet(ctx, '%s', '%s')", key, val); |
| 62 |
assert((ctx != NULL) && (ctx->dbc != NULL)); |
| 63 |
|
| 64 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 65 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 66 |
"eurephia admin function call attempted with wrong context type"); |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
res = sqlite_query(ctx, "SELECT count(*) FROM openvpn_config WHERE datakey = '%q'", key); |
| 71 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 72 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not query configuration table"); |
| 73 |
sqlite_log_error(ctx, res); |
| 74 |
sqlite_free_results(res); |
| 75 |
return 0; |
| 76 |
} |
| 77 |
found = atoi_nullsafe(sqlite_get_value(res, 0, 0)); |
| 78 |
sqlite_free_results(res); |
| 79 |
|
| 80 |
if( found == 0 ) { |
| 81 |
res = sqlite_query(ctx, |
| 82 |
"INSERT INTO openvpn_config (datakey, dataval) VALUES ('%q','%q')", |
| 83 |
key, val); |
| 84 |
} else { |
| 85 |
res = sqlite_query(ctx, "UPDATE openvpn_config SET dataval = '%q' WHERE datakey = '%q'", |
| 86 |
val, key); |
| 87 |
} |
| 88 |
|
| 89 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 90 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not register configuration entry (%s = '%s'", key, val); |
| 91 |
sqlite_log_error(ctx, res); |
| 92 |
sqlite_free_results(res); |
| 93 |
return 0; |
| 94 |
} |
| 95 |
sqlite_free_results(res); |
| 96 |
eAdd_value(ctx, ctx->dbc->config, key, val); |
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Internal function. Deletes a configuration parameter from the database. |
| 103 |
* |
| 104 |
* @param ctx eurephiaCTX |
| 105 |
* @param key String (char *) containing the key to be deleted. |
| 106 |
* |
| 107 |
* @return Returns 1 on success, otherwise 0 |
| 108 |
*/ |
| 109 |
static int config_delete(eurephiaCTX *ctx, const char *key) { |
| 110 |
dbresult *res = NULL; |
| 111 |
eurephiaVALUES *cfgptr = NULL; |
| 112 |
|
| 113 |
DEBUG(ctx, 20, "Function call: eDBadminConfigDelete(ctx, '%s') ", key); |
| 114 |
assert((ctx != NULL) && (ctx->dbc != NULL)); |
| 115 |
|
| 116 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 117 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 118 |
"eurephia admin function call attempted with wrong context type"); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
// Find the config parameter in the in-memory stack |
| 123 |
cfgptr = eGet_valuestruct(ctx->dbc->config, key); |
| 124 |
if( cfgptr == NULL ) { |
| 125 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 126 |
"Could not find the configuration parameter '%s'", key); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
// Delete the config parameter from the database |
| 131 |
res = sqlite_query(ctx, "DELETE FROM openvpn_config WHERE datakey = '%q'", key); |
| 132 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 133 |
eurephia_log(ctx, LOG_ERROR, 0, "Could not delete config configuration entry (%s)", key); |
| 134 |
sqlite_log_error(ctx, res); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
sqlite_free_results(res); |
| 138 |
|
| 139 |
// Since the database call worked, remove it from memory as well |
| 140 |
ctx->dbc->config = eRemove_value(ctx, ctx->dbc->config, cfgptr->evgid, cfgptr->evid); |
| 141 |
|
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Internal function. Validates if key or value strings are NULL or not. |
| 148 |
* |
| 149 |
* @param ctx eurephiaCTX |
| 150 |
* @param key char pointer to the key string |
| 151 |
* @param value char pointer to the value string |
| 152 |
* |
| 153 |
* @return Returns NULL if key and value is not NULL. Otherwise an eurephia XML document with an error |
| 154 |
* description will be returned. |
| 155 |
*/ |
| 156 |
xmlDoc *validate_key_value(eurephiaCTX *ctx, const char *key, const char *value) { |
| 157 |
int k_null = 0, v_null = 0; |
| 158 |
|
| 159 |
k_null = (key == NULL ? 1 : 0); |
| 160 |
v_null = (value == NULL ? 1 : 0); |
| 161 |
|
| 162 |
if( k_null || v_null ) { |
| 163 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "%s%s%s", |
| 164 |
(k_null ? "The key attribute was not set" : ""), |
| 165 |
(k_null && v_null ? " and " : ""), |
| 166 |
(v_null ? "The value tag was not set" : "") |
| 167 |
); |
| 168 |
} |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @copydoc eDBadminConfiguration() |
| 174 |
*/ |
| 175 |
xmlDoc *eDBadminConfiguration(eurephiaCTX *ctx, xmlDoc *cfgxml) { |
| 176 |
xmlDoc *resxml = NULL; |
| 177 |
xmlNode *root_n = NULL, *cfg_n = NULL; |
| 178 |
char *key = NULL, *value = NULL; |
| 179 |
|
| 180 |
DEBUG(ctx, 20, "Function call: eDBadminConfiguration(ctx, {xmlDoc})"); |
| 181 |
assert( (ctx != NULL) && (cfgxml != NULL) ); |
| 182 |
|
| 183 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 184 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 185 |
"eurephia admin function call attempted with wrong context type"); |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
|
| 189 |
root_n = eurephiaXML_getRoot(ctx, cfgxml, "configuration", 1); |
| 190 |
if( root_n == NULL ) { |
| 191 |
eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input."); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
// Look if we recevied a <set> tag |
| 196 |
cfg_n = xmlFindNode(root_n, "set"); |
| 197 |
if( cfg_n != NULL ) { |
| 198 |
key = xmlGetAttrValue(cfg_n->properties, "key"); |
| 199 |
value = xmlExtractContent(cfg_n); |
| 200 |
|
| 201 |
resxml = validate_key_value(ctx, key, value); |
| 202 |
if( resxml ) { |
| 203 |
// Input data was not approved, and we have an error message |
| 204 |
return resxml; |
| 205 |
} |
| 206 |
|
| 207 |
if( config_set(ctx, key, value) ) { |
| 208 |
resxml = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, |
| 209 |
"Configuration key '%s' was set to '%s'", |
| 210 |
key, value); |
| 211 |
} else { |
| 212 |
resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 213 |
"Failed to set the key '%s' to '%s'", |
| 214 |
key, value); |
| 215 |
} |
| 216 |
return resxml; |
| 217 |
}; |
| 218 |
|
| 219 |
// If not, look if we recevied a <delete> tag |
| 220 |
cfg_n = xmlFindNode(root_n, "delete"); |
| 221 |
if( cfg_n != NULL ) { |
| 222 |
key = xmlGetAttrValue(cfg_n->properties, "key"); |
| 223 |
|
| 224 |
resxml = validate_key_value(ctx, key, ""); // Do not use NULL for value |
| 225 |
if( resxml ) { |
| 226 |
// Input data was not approved, and we have an error message |
| 227 |
return resxml; |
| 228 |
} |
| 229 |
|
| 230 |
if( config_delete(ctx, key) ) { |
| 231 |
resxml = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, |
| 232 |
"Configuration key '%s' was deleted", key); |
| 233 |
} else { |
| 234 |
resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 235 |
"Failed to delete the key '%s'", key); |
| 236 |
} |
| 237 |
return resxml; |
| 238 |
} |
| 239 |
|
| 240 |
// If not, it's an invalid input |
| 241 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown XML tag received"); |
| 242 |
} |