attempts.c

Go to the documentation of this file.
00001 /* attempts.c  --  Functions for processing openvpn_attempts 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_OVPNATTEMPTS       
00051 #include "../fieldmapping.h"
00052 
00061 xmlDoc *attempts_list(eurephiaCTX *ctx, eDBfieldMap *fmap) {
00062         dbresult *res = NULL;
00063         xmlDoc *doc = NULL;
00064         xmlNode *root_n = NULL, *uname_n = NULL, *cert_n = NULL, *remip_n = NULL;
00065         int i = 0;
00066 
00067         // Query the database for registered attempts
00068         res = sqlite_query_mapped(ctx, SQL_SELECT,
00069                                   "SELECT username, lower(digest), remoteip, attempts,"
00070                                   "       registered, last_attempt, atpid"
00071                                   "  FROM openvpn_attempts",
00072                                   NULL, fmap, "atpid");
00073         if( res == NULL ) {
00074                 eurephia_log(ctx, LOG_ERROR, 0, "Error querying the attempts log");
00075                 return NULL;
00076         }
00077 
00078         eurephiaXML_CreateDoc(ctx, 1, "attemptslog", &doc, &root_n);
00079         xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "list");
00080 
00081         for( i = 0; i < sqlite_get_numtuples(res); i++ ) {
00082                 xmlNode *atmpt_n = NULL;
00083 
00084                 if( sqlite_get_value(res, i, 0) != NULL ) { // Username
00085                         if( uname_n == NULL ) {
00086                                 uname_n = xmlNewChild(root_n, NULL, (xmlChar *) "username", NULL);
00087                                 assert( uname_n != NULL );
00088                         }
00089                         atmpt_n = xmlNewChild(uname_n, NULL, (xmlChar *) "attempt", NULL);
00090                         sqlite_xml_value(atmpt_n, XML_NODE, "username", res, i, 0);
00091                 } else if( sqlite_get_value(res, i, 1) != NULL ) { // Digest
00092                         if( cert_n == NULL ) {
00093                                 cert_n  = xmlNewChild(root_n, NULL, (xmlChar *) "certificate", NULL);
00094                                 assert( cert_n != NULL );
00095                         }
00096                         atmpt_n = xmlNewChild(cert_n, NULL, (xmlChar *) "attempt", NULL);
00097                         sqlite_xml_value(atmpt_n, XML_NODE, "certificate", res, i, 1);
00098                 } else if( sqlite_get_value(res, i, 2) != NULL ) { // IP address
00099                         if( remip_n == NULL ) {
00100                                 remip_n = xmlNewChild(root_n, NULL, (xmlChar *) "ipaddress", NULL);
00101                                 assert( remip_n != NULL );
00102                         }
00103                         atmpt_n = xmlNewChild(remip_n, NULL, (xmlChar *) "attempt", NULL);
00104                         sqlite_xml_value(atmpt_n, XML_NODE, "ipaddress", res, i, 2);
00105                 } else {
00106                         continue;
00107                 }
00108 
00109                 sqlite_xml_value(atmpt_n, XML_ATTR, "atpid", res, i, 6);
00110                 sqlite_xml_value(atmpt_n, XML_ATTR, "attempts", res, i, 3);
00111                 sqlite_xml_value(atmpt_n, XML_NODE, "registered", res, i, 4);
00112                 sqlite_xml_value(atmpt_n, XML_NODE, "last_attempt", res, i, 5);
00113         }
00114         sqlite_free_results(res);
00115         return doc;
00116 }
00117 
00118 
00119 
00128 xmlDoc *attempts_reset(eurephiaCTX *ctx, eDBfieldMap *fmap) {
00129         dbresult *res = NULL;
00130         xmlDoc *ret = NULL;
00131         int fields = 0;
00132         eDBfieldMap update_vals[] = {
00133                 {TABLE_ATTEMPTS, NULL, FIELD_ATTEMPTS, ft_INT, flt_NOTSET, "attempts",    "0", NULL},
00134                 {0, NULL, 0, ft_UNDEF, flt_NOTSET, NULL, NULL, NULL}
00135         };
00136 
00137         fields = eDBmappingFieldsPresent(fmap);
00138         if( (fields & (FIELD_UNAME | FIELD_CERTDIGEST | FIELD_REMOTEIP | FIELD_RECID)) == 0 ) {
00139                 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
00140                                              "Missing username, IP address, certificate digest or atpid");
00141         }
00142 
00143         res = sqlite_query_mapped(ctx, SQL_UPDATE, "UPDATE openvpn_attempts", update_vals, fmap, NULL);
00144         if( res == NULL ) {
00145                 eurephia_log(ctx, LOG_FATAL, 0, "Could not reset the attempts count");
00146                 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not reset the attempts count");
00147         } else {
00148                 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Attempts count reset");
00149                 sqlite_free_results(res);
00150         }
00151         return ret;
00152 }
00153 
00154 
00163 xmlDoc *attempts_delete(eurephiaCTX *ctx, eDBfieldMap *fmap) {
00164         dbresult *res = NULL;
00165         xmlDoc *ret = NULL;
00166         int fields;
00167 
00168         fields = eDBmappingFieldsPresent(fmap);
00169         if( (fields & (FIELD_UNAME | FIELD_CERTDIGEST | FIELD_REMOTEIP | FIELD_RECID)) == 0 ) {
00170                 return eurephiaXML_ResultMsg(ctx, exmlERROR, NULL,
00171                                              "Missing username, IP address, certificate digest or atpid");
00172         }
00173 
00174         res = sqlite_query_mapped(ctx, SQL_DELETE, "DELETE FROM openvpn_attempts", NULL, fmap, NULL);
00175         if( res == NULL ) {
00176                 eurephia_log(ctx, LOG_FATAL, 0, "Could not remove attempts record");
00177                 ret = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Could not delete the attempts record");
00178         } else {
00179                 ret = eurephiaXML_ResultMsg(ctx, exmlRESULT, NULL, "Attempts record removed");
00180                 sqlite_free_results(res);
00181         }
00182         return ret;
00183 }
00184 
00185 
00189 xmlDoc *eDBadminAttemptsLog(eurephiaCTX *ctx, xmlDoc *qryxml) {
00190         eDBfieldMap *fmap = NULL;
00191         char *mode = NULL;
00192         xmlDoc *resxml = NULL;
00193         xmlNode *root_n = NULL, *fieldmap_n = NULL;
00194 
00195         DEBUG(ctx, 20, "Function call: eDBadminAttemptsLog(ctx, {xmlDoc})");
00196         assert( (ctx != NULL) && (qryxml != NULL) );
00197 
00198         if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) {
00199                 eurephia_log(ctx, LOG_CRITICAL, 0,
00200                              "eurephia admin function call attempted with wrong context type");
00201                 return NULL;
00202         }
00203 
00204         root_n = eurephiaXML_getRoot(ctx, qryxml, "attemptslog", 1);
00205         if( root_n == NULL ) {
00206                 eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input.");
00207                 return NULL;
00208         }
00209         mode = xmlGetAttrValue(root_n->properties, "mode");
00210         if( mode == NULL ) {
00211                 eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute");
00212                 return NULL;
00213         }
00214 
00215         fieldmap_n = xmlFindNode(root_n, "fieldMapping");
00216         if( fieldmap_n == NULL ) {
00217                 eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping");
00218         }
00219         fmap = eDBxmlMapping(ctx, tbl_sqlite_attempts, NULL, fieldmap_n);
00220 
00221         if( strcmp(mode, "list") == 0 ) {
00222                 resxml = attempts_list(ctx, fmap);
00223         } else if( strcmp(mode, "reset") == 0 ) {
00224                 resxml = attempts_reset(ctx, fmap);
00225         } else if( strcmp(mode, "delete") == 0 ) {
00226                 resxml = attempts_delete(ctx, fmap);
00227         } else {
00228                 eurephia_log(ctx, LOG_ERROR, 0, "Attempts - Unknown mode: '%s'", mode);
00229                 resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, NULL, "Unknown mode '%s'", mode);
00230         }
00231         eDBfreeMapping(fmap);
00232         return resxml;
00233 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines