blacklist.c

Go to the documentation of this file.
00001 /* blacklist.c  --  Functions for processing openvpn_blacklist records
00002  *
00003  *  GPLv2 only - Copyright (C) 2009 - 2010
00004  *               David Sommerseth <dazo@users.sourceforge.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; version 2
00009  *  of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *
00020  */
00021 
00031 #include <string.h>
00032 #include <unistd.h>
00033 #include <assert.h>
00034 
00035 #include <libxml/tree.h>
00036 
00037 #include <sqlite3.h>
00038 
00039 #include <eurephia_nullsafe.h>
00040 #include <eurephia_context.h>
00041 #include <eurephia_log.h>
00042 #include <eurephia_xml.h>
00043 #include <eurephia_values.h>
00044 #include <eurephiadb_session_struct.h>
00045 #include <eurephiadb_mapping.h>
00046 #include <passwd.h>
00047 
00048 #include "../sqlite.h"
00049 
00050 #define FMAP_OVPNBLACKLIST      
00051 #include "../fieldmapping.h"
00052 
00053 
00062 xmlDoc *blacklist_list(eurephiaCTX *ctx, eDBfieldMap *fmap) {
00063         dbresult *res = NULL;
00064         xmlDoc *doc = NULL;
00065         xmlNode *root_n = NULL, *uname_n = NULL, *cert_n = NULL, *remip_n = NULL;
00066         int i = 0;
00067 
00068         // Query the database for registered attempts
00069         res = sqlite_query_mapped(ctx, SQL_SELECT,
00070                                   "SELECT username, lower(digest), remoteip,"
00071                                   "       registered, last_accessed, blid"
00072                                   "  FROM openvpn_blacklist",
00073                                   NULL, fmap, "blid");
00074         if( res == NULL ) {
00075                 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the blacklist register");
00076                 return NULL;
00077         }
00078 
00079         eurephiaXML_CreateDoc(ctx, 1, "blacklist", &doc, &root_n);
00080         xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "list");
00081 
00082         for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
00083                 xmlNode *blist_n = NULL;
00084 
00085                 if( sqlite_get_value(res, i, 0) != NULL ) { // Username
00086                         if( uname_n == NULL ) {
00087                                 uname_n = xmlNewChild(root_n, NULL, (xmlChar *) "username", NULL);
00088                                 assert( uname_n != NULL );
00089                         }
00090                         blist_n = xmlNewChild(uname_n, NULL, (xmlChar *) "blacklisted", NULL);
00091                         sqlite_xml_value(blist_n, XML_NODE, "username", res, i, 0);
00092                 } else if( sqlite_get_value(res, i, 1) != NULL ) { // Digest
00093                         if( cert_n == NULL ) {
00094                                 cert_n  = xmlNewChild(root_n, NULL, (xmlChar *) "certificate", NULL);
00095                                 assert( cert_n != NULL );
00096                         }
00097                         blist_n = xmlNewChild(cert_n, NULL, (xmlChar *) "blacklisted", NULL);
00098                         sqlite_xml_value(blist_n, XML_NODE, "certificate", res, i, 1);
00099                 } else if( sqlite_get_value(res, i, 2) != NULL ) { // IP address
00100                         if( remip_n == NULL ) {
00101                                 remip_n = xmlNewChild(root_n, NULL, (xmlChar *) "ipaddress", NULL);
00102                                 assert( remip_n != NULL );
00103                         }
00104                         blist_n = xmlNewChild(remip_n, NULL, (xmlChar *) "blacklisted", NULL);
00105                         sqlite_xml_value(blist_n, XML_NODE, "ipaddress", res, i, 2);
00106                 } else {
00107                         continue;
00108                 }
00109 
00110                 sqlite_xml_value(blist_n, XML_ATTR, "blid", res, i, 5);
00111                 sqlite_xml_value(blist_n, XML_NODE, "registered", res, i, 3);
00112                 sqlite_xml_value(blist_n, XML_NODE, "last_accessed", res, i, 4);
00113         }
00114         sqlite_free_results(res);
00115         return doc;
00116 }
00117 
00118 
00127 xmlDoc *blacklist_add(eurephiaCTX *ctx, eDBfieldMap *fmap) {
00128         dbresult *res = NULL;
00129         xmlDoc *ret = NULL;
00130         int fields = 0;
00131 
00132         fields = eDBmappingFieldsPresent(fmap);
00133         if( (fields != FIELD_UNAME) && (fields != FIELD_CERTDIGEST) && (fields != FIELD_REMOTEIP) ) {
00134                 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
00135                                              "Missing username, IP address or certificate digest, "
00136                                              "or multiple of these fields were given.");
00137         }
00138 
00139         res = sqlite_query_mapped(ctx, SQL_INSERT, "INSERT INTO openvpn_blacklist", fmap, NULL, NULL);
00140         if( res == NULL ) {
00141                 eurephia_log(ctx, LOG_FATAL, 0, "Could not blacklist the requested data");
00142                 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Blacklisting failed");
00143         } else {
00144                 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Record registered in the blacklist");
00145                 sqlite_free_results(res);
00146         }
00147         return ret;
00148 }
00149 
00150 
00159 xmlDoc *blacklist_delete(eurephiaCTX *ctx, eDBfieldMap *fmap) {
00160         dbresult *res = NULL;
00161         xmlDoc *ret = NULL;
00162         int fields;
00163 
00164         fields = eDBmappingFieldsPresent(fmap);
00165         if( (fields & (FIELD_UNAME | FIELD_CERTDIGEST | FIELD_REMOTEIP | FIELD_RECID)) == 0 ) {
00166                 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
00167                                              "Missing username, IP address, certificate digest or blacklist ID");
00168         }
00169 
00170         res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_blacklist", NULL, fmap, NULL);
00171         if( res == NULL ) {
00172                 eurephia_log(ctx, LOG_FATAL, 0, "Could not remove blacklisting");
00173                 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Failed to remove the blacklisting");
00174         } else {
00175                 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Blacklisting removed");
00176                 sqlite_free_results(res);
00177         }
00178         return ret;
00179 }
00180 
00181 
00185 xmlDoc *eDBadminBlacklist(eurephiaCTX *ctx, xmlDoc *qryxml) {
00186         eDBfieldMap *fmap = NULL;
00187         char *mode = NULL;
00188         xmlDoc *resxml = NULL;
00189         xmlNode *root_n = NULL, *fieldmap_n = NULL;
00190 
00191         DEBUG(ctx, 20, "Function call: eDBadminBlacklist(ctx, {xmlDoc})");
00192         assert( (ctx != NULL) && (qryxml != NULL) );
00193 
00194         if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
00195                 eurephia_log(ctx, LOG_CRITICAL, 0,
00196                              "eurephia admin function call attempted with wrong context type");
00197                 return NULL;
00198         }
00199 
00200         root_n = eurephiaXML_getRoot(ctx, qryxml, "blacklist", 1);
00201         if( root_n == NULL ) {
00202                 eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
00203                 return NULL;
00204         }
00205         mode = xmlGetAttrValue(root_n->properties, "mode");
00206         if( mode == NULL ) {
00207                 eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute");
00208                 return NULL;
00209         }
00210 
00211         fieldmap_n = xmlFindNode(root_n, "fieldMapping");
00212         if( fieldmap_n == NULL ) {
00213                 eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping");
00214         }
00215         fmap = eDBxmlMapping(ctx, tbl_sqlite_blacklist, NULL, fieldmap_n);
00216 
00217         if( strcmp(mode, "list") == 0 ) {
00218                 resxml = blacklist_list(ctx, fmap);
00219         } else if( strcmp(mode, "add") == 0 ) {
00220                 resxml = blacklist_add(ctx, fmap);
00221         } else if( strcmp(mode, "delete") == 0 ) {
00222                 resxml = blacklist_delete(ctx, fmap);
00223         } else {
00224                 eurephia_log(ctx, LOG_ERROR, 0, "Blacklist - Unknown mode: '%s'", mode);
00225                 resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
00226         }
00227         eDBfreeMapping(fmap);
00228         return resxml;
00229 
00230 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines