| 1 |
/* blacklist.c -- Functions for processing openvpn_blacklist records |
| 2 |
* |
| 3 |
* GPLv2 only - Copyright (C) 2009 - 2012 |
| 4 |
* David Sommerseth <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; version 2 |
| 9 |
* of the License. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* @file sqlite/administration/blacklist.c |
| 24 |
* @author David Sommerseth <[email protected]> |
| 25 |
* @date 2009-05-09 |
| 26 |
* |
| 27 |
* @brief Functions for administering the blacklist table |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <assert.h> |
| 34 |
|
| 35 |
#include <libxml/tree.h> |
| 36 |
|
| 37 |
#include <sqlite3.h> |
| 38 |
|
| 39 |
#include <eurephia_nullsafe.h> |
| 40 |
#include <eurephia_context.h> |
| 41 |
#include <eurephia_log.h> |
| 42 |
#include <eurephia_xml.h> |
| 43 |
#include <eurephia_values.h> |
| 44 |
#include <eurephiadb_session_struct.h> |
| 45 |
#include <eurephiadb_mapping.h> |
| 46 |
#include <passwd.h> |
| 47 |
|
| 48 |
#include "../sqlite.h" |
| 49 |
|
| 50 |
#define FMAP_OVPNBLACKLIST /**< fieldmapping.h: Include declaration of tbl_sqlite_blacklist */ |
| 51 |
#include "../fieldmapping.h" |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Internal function. Retrieves a list of all registered entries in the blacklist table |
| 56 |
* |
| 57 |
* @param ctx eurephiaCTX |
| 58 |
* @param fmap eDBfieldMap containing the search query |
| 59 |
* |
| 60 |
* @return Returns a valid eurephia XML document on success, otherwise NULL |
| 61 |
*/ |
| 62 |
xmlDoc *blacklist_list(eurephiaCTX *ctx, eDBfieldMap *fmap) { |
| 63 |
dbresult *res = NULL; |
| 64 |
xmlDoc *doc = NULL; |
| 65 |
xmlNode *root_n = NULL, *uname_n = NULL, *cert_n = NULL, *remip_n = NULL; |
| 66 |
int i = 0; |
| 67 |
|
| 68 |
// Query the database for registered attempts |
| 69 |
res = sqlite_query_mapped(ctx, SQL_SELECT, |
| 70 |
"SELECT username, lower(digest), remoteip," |
| 71 |
" locdt(registered), locdt(last_accessed), blid" |
| 72 |
" FROM openvpn_blacklist", |
| 73 |
NULL, fmap, "blid"); |
| 74 |
if( sqlite_query_status(res) != dbSUCCESS ) { |
| 75 |
eurephia_log(ctx, LOG_ERROR, 0, "Error querying the blacklist register"); |
| 76 |
sqlite_log_error(ctx, res); |
| 77 |
sqlite_free_results(res); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
eurephiaXML_CreateDoc(ctx, 1, "blacklist", &doc, &root_n); |
| 82 |
xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "list"); |
| 83 |
|
| 84 |
for( i = 0; i < sqlite_get_numtuples(res); i++ ) { |
| 85 |
xmlNode *blist_n = NULL; |
| 86 |
|
| 87 |
if( sqlite_get_value(res, i, 0) != NULL ) { // Username |
| 88 |
if( uname_n == NULL ) { |
| 89 |
uname_n = xmlNewChild(root_n, NULL, (xmlChar *) "username", NULL); |
| 90 |
assert( uname_n != NULL ); |
| 91 |
} |
| 92 |
blist_n = xmlNewChild(uname_n, NULL, (xmlChar *) "blacklisted", NULL); |
| 93 |
sqlite_xml_value(blist_n, XML_NODE, "username", res, i, 0); |
| 94 |
} else if( sqlite_get_value(res, i, 1) != NULL ) { // Digest |
| 95 |
if( cert_n == NULL ) { |
| 96 |
cert_n = xmlNewChild(root_n, NULL, (xmlChar *) "certificate", NULL); |
| 97 |
assert( cert_n != NULL ); |
| 98 |
} |
| 99 |
blist_n = xmlNewChild(cert_n, NULL, (xmlChar *) "blacklisted", NULL); |
| 100 |
sqlite_xml_value(blist_n, XML_NODE, "certificate", res, i, 1); |
| 101 |
} else if( sqlite_get_value(res, i, 2) != NULL ) { // IP address |
| 102 |
if( remip_n == NULL ) { |
| 103 |
remip_n = xmlNewChild(root_n, NULL, (xmlChar *) "ipaddress", NULL); |
| 104 |
assert( remip_n != NULL ); |
| 105 |
} |
| 106 |
blist_n = xmlNewChild(remip_n, NULL, (xmlChar *) "blacklisted", NULL); |
| 107 |
sqlite_xml_value(blist_n, XML_NODE, "ipaddress", res, i, 2); |
| 108 |
} else { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
sqlite_xml_value(blist_n, XML_ATTR, "blid", res, i, 5); |
| 113 |
sqlite_xml_value(blist_n, XML_NODE, "registered", res, i, 3); |
| 114 |
sqlite_xml_value(blist_n, XML_NODE, "last_accessed", res, i, 4); |
| 115 |
} |
| 116 |
sqlite_free_results(res); |
| 117 |
return doc; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Internal function. Registers a new entry in the blacklist. |
| 123 |
* |
| 124 |
* @param ctx eurephiaCTX |
| 125 |
* @param fmap eDBfieldMap containing the entry to be registered |
| 126 |
* |
| 127 |
* @return Returns an eurephia ResultMsg XML document, with success message or an error message |
| 128 |
*/ |
| 129 |
xmlDoc *blacklist_add(eurephiaCTX *ctx, eDBfieldMap *fmap) { |
| 130 |
dbresult *res = NULL; |
| 131 |
xmlDoc *ret = NULL; |
| 132 |
int fields = 0; |
| 133 |
|
| 134 |
fields = eDBmappingFieldsPresent(fmap); |
| 135 |
if( (fields != FIELD_UNAME) && (fields != FIELD_CERTDIGEST) && (fields != FIELD_REMOTEIP) ) { |
| 136 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 137 |
"Missing username, IP address or certificate digest, " |
| 138 |
"or multiple of these fields were given."); |
| 139 |
} |
| 140 |
|
| 141 |
res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_blacklist", fmap, NULL, NULL); |
| 142 |
if( sqlite_query_status(res) == dbSUCCESS ) { |
| 143 |
ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Record registered in the blacklist"); |
| 144 |
} else { |
| 145 |
xmlNode *err_n = NULL; |
| 146 |
|
| 147 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not blacklist the requested data"); |
| 148 |
err_n = sqlite_log_error_xml(ctx, res); |
| 149 |
ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Blacklisting failed"); |
| 150 |
xmlFreeNode(err_n); |
| 151 |
} |
| 152 |
sqlite_free_results(res); |
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Internal function. Deletes an entry from the blacklist |
| 159 |
* |
| 160 |
* @param ctx eurephiaCTX |
| 161 |
* @param fmap eDBfieldMap containing the entry to be deleted |
| 162 |
* |
| 163 |
* @return Returns an eurephia ResultMsg XML document, with success message or an error message |
| 164 |
*/ |
| 165 |
xmlDoc *blacklist_delete(eurephiaCTX *ctx, eDBfieldMap *fmap) { |
| 166 |
dbresult *res = NULL; |
| 167 |
xmlDoc *ret = NULL; |
| 168 |
int fields; |
| 169 |
|
| 170 |
fields = eDBmappingFieldsPresent(fmap); |
| 171 |
if( (fields & (FIELD_UNAME | FIELD_CERTDIGEST | FIELD_REMOTEIP | FIELD_RECID)) == 0 ) { |
| 172 |
return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, |
| 173 |
"Missing username, IP address, certificate digest or blacklist ID"); |
| 174 |
} |
| 175 |
|
| 176 |
res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_blacklist", NULL, fmap, NULL); |
| 177 |
if( sqlite_query_status(res) == dbSUCCESS ) { |
| 178 |
ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Blacklisting removed"); |
| 179 |
} else { |
| 180 |
xmlNode *err_n = NULL; |
| 181 |
|
| 182 |
eurephia_log(ctx, LOG_FATAL, 0, "Could not remove blacklisting"); |
| 183 |
err_n = sqlite_log_error_xml(ctx, res); |
| 184 |
ret = eurephiaXML_ResultMsg(ctx, exmlERROR, err_n, "Failed to remove the blacklisting"); |
| 185 |
xmlFreeNode(err_n); |
| 186 |
} |
| 187 |
sqlite_free_results(res); |
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* @copydoc eDBadminBlacklist() |
| 194 |
*/ |
| 195 |
xmlDoc *eDBadminBlacklist(eurephiaCTX *ctx, xmlDoc *qryxml) { |
| 196 |
eDBfieldMap *fmap = NULL; |
| 197 |
char *mode = NULL; |
| 198 |
xmlDoc *resxml = NULL; |
| 199 |
xmlNode *root_n = NULL, *fieldmap_n = NULL; |
| 200 |
|
| 201 |
DEBUG(ctx, 20, "Function call: eDBadminBlacklist(ctx, {xmlDoc})"); |
| 202 |
assert( (ctx != NULL) && (qryxml != NULL) ); |
| 203 |
|
| 204 |
if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { |
| 205 |
eurephia_log(ctx, LOG_CRITICAL, 0, |
| 206 |
"eurephia admin function call attempted with wrong context type"); |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
root_n = eurephiaXML_getRoot(ctx, qryxml, "blacklist", 1); |
| 211 |
if( root_n == NULL ) { |
| 212 |
eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input."); |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
mode = xmlGetAttrValue(root_n->properties, "mode"); |
| 216 |
if( mode == NULL ) { |
| 217 |
eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute"); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
fieldmap_n = xmlFindNode(root_n, "fieldMapping"); |
| 222 |
if( fieldmap_n == NULL ) { |
| 223 |
eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping"); |
| 224 |
} |
| 225 |
fmap = eDBxmlMapping(ctx, tbl_sqlite_blacklist, NULL, fieldmap_n); |
| 226 |
|
| 227 |
if( strcmp(mode, "list") == 0 ) { |
| 228 |
resxml = blacklist_list(ctx, fmap); |
| 229 |
} else if( strcmp(mode, "add") == 0 ) { |
| 230 |
resxml = blacklist_add(ctx, fmap); |
| 231 |
} else if( strcmp(mode, "delete") == 0 ) { |
| 232 |
resxml = blacklist_delete(ctx, fmap); |
| 233 |
} else { |
| 234 |
eurephia_log(ctx, LOG_ERROR, 0, "Blacklist - Unknown mode: '%s'", mode); |
| 235 |
resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode); |
| 236 |
} |
| 237 |
eDBfreeMapping(fmap); |
| 238 |
return resxml; |
| 239 |
|
| 240 |
} |