diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2009-04-09 18:19:55 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2009-04-09 18:19:55 +0200 |
commit | 8f747e26fb8629ea290f88cf70112770e90a168a (patch) | |
tree | ad67d50938cc5f8eadc47629ede790dc9f506120 /database/sqlite | |
parent | 6a9c621e5f3f0854d6eca650723f76af368e2d6a (diff) | |
download | eurephia-8f747e26fb8629ea290f88cf70112770e90a168a.tar.gz eurephia-8f747e26fb8629ea290f88cf70112770e90a168a.tar.xz eurephia-8f747e26fb8629ea290f88cf70112770e90a168a.zip |
sqlite3 driver: Implemented listing of openvpn_attempts table
Diffstat (limited to 'database/sqlite')
-rw-r--r-- | database/sqlite/CMakeLists.txt | 1 | ||||
-rw-r--r-- | database/sqlite/administration.c | 65 | ||||
-rw-r--r-- | database/sqlite/attempts.c | 110 | ||||
-rw-r--r-- | database/sqlite/fieldmapping.h | 12 |
4 files changed, 184 insertions, 4 deletions
diff --git a/database/sqlite/CMakeLists.txt b/database/sqlite/CMakeLists.txt index c2774ff..03dab63 100644 --- a/database/sqlite/CMakeLists.txt +++ b/database/sqlite/CMakeLists.txt @@ -39,6 +39,7 @@ IF(ADMIN_ENABLED) ../eurephiadb_mapping.c administration.c firewalladmin.c + attempts.c ) ENDIF(ADMIN_ENABLED) diff --git a/database/sqlite/administration.c b/database/sqlite/administration.c index 18752a6..6def2e8 100644 --- a/database/sqlite/administration.c +++ b/database/sqlite/administration.c @@ -53,6 +53,7 @@ #define FMAP_USERCERTS #define FMAP_ADMINACCESS #define FMAP_LASTLOG +#define FMAP_OVPNATTEMPTS #include "fieldmapping.h" #if DRIVERAPIVERSION > 1 @@ -1562,10 +1563,66 @@ xmlDoc *eDBadminGetLastlog(eurephiaCTX *ctx, xmlDoc *srch, const char *sortkeys) return doc; } -xmlDoc *eDBadminGetAttemptsLog(eurephiaCTX *ctx, xmlDoc *usersrch, xmlDoc *certsrch, - const char *sortkeys) -{ - return NULL; +// The search XML document format is: +// <eurephia format="1"> +// <attempts mode="{search|add|delete}"> +// <fieldMapping table="attempts"> +// <{field name}>{field value}</{field field}> +// </fieldMapping> +// </attempts> +// </eurehpia> +// +// It can be several search field tags to limit the search even more. +// +xmlDoc *attempts_list(eurephiaCTX *ctx, eDBfieldMap *fmap); +xmlDoc *attempts_add(eurephiaCTX *ctx, eDBfieldMap *fmap); +xmlDoc *attempts_delete(eurephiaCTX *ctx, eDBfieldMap *fmap); + +xmlDoc *eDBadminAttemptsLog(eurephiaCTX *ctx, xmlDoc *qryxml) { + eDBfieldMap *fmap = NULL; + char *mode = NULL; + xmlDoc *resxml = NULL; + xmlNode *root_n = NULL, *fieldmap_n = NULL; + + DEBUG(ctx, 20, "Function call: eDBadminAttemptsLog(ctx, {xmlDoc})"); + assert( (ctx != NULL) && (qryxml != NULL) ); + + if( (ctx->context_type != ECTX_ADMIN_CONSOLE) && (ctx->context_type != ECTX_ADMIN_WEB) ) { + eurephia_log(ctx, LOG_CRITICAL, 0, + "eurephia admin function call attempted with wrong context type"); + return NULL; + } + + root_n = eurephiaXML_getRoot(ctx, qryxml, "attempts", 1); + if( root_n == NULL ) { + eurephia_log(ctx, LOG_CRITICAL, 0, "Invalid XML input."); + return NULL; + } + mode = xmlGetAttrValue(root_n->properties, "mode"); + if( mode == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Missing mode attribute"); + return NULL; + } + + fieldmap_n = xmlFindNode(root_n, "fieldMapping"); + if( fieldmap_n == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Missing fieldMapping"); + } + fmap = eDBxmlMapping(ctx, tbl_sqlite_attempts, NULL, fieldmap_n); + + if( strcmp(mode, "list") == 0 ) { + resxml = attempts_list(ctx, fmap); + } else if( strcmp(mode, "add") == 0 ) { + resxml = attempts_add(ctx, fmap); + } else if( strcmp(mode, "delete") == 0 ) { + resxml = attempts_delete(ctx, fmap); + } else { + eurephia_log(ctx, LOG_ERROR, 0, "FirewallProfiles - Unknown mode: '%s'", mode); + resxml = eurephiaXML_ResultMsg(ctx, exmlERROR, "Unknown mode '%s'", mode); + } + eDBfreeMapping(fmap); + return resxml; + } #endif diff --git a/database/sqlite/attempts.c b/database/sqlite/attempts.c new file mode 100644 index 0000000..7e8e95b --- /dev/null +++ b/database/sqlite/attempts.c @@ -0,0 +1,110 @@ +/* attempts.c -- Functions for processing openvpn_attempts records + * + * GPLv2 only - Copyright (C) 2008, 2009 + * David Sommerseth <dazo@users.sourceforge.net> + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 + * of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include <string.h> +#include <unistd.h> +#include <assert.h> + +#include <libxml/tree.h> + +#ifndef DRIVERAPIVERSION +# define DRIVERAPIVERSION 2 +#endif + +#include <sqlite3.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephia_admin_struct.h> +#include <eurephia_log.h> +#include <eurephia_xml.h> +#include <eurephia_values.h> +#include <eurephiadb_session_struct.h> +#include <eurephiadb_mapping.h> +#include <passwd.h> + +#ifndef DRIVER_MODE +#define DRIVER_MODE +#endif + +#include "sqlite.h" + + +xmlDoc *attempts_list(eurephiaCTX *ctx, eDBfieldMap *fmap) { + dbresult *res = NULL; + xmlDoc *doc = NULL; + xmlNode *root_n = NULL, *uname_n = NULL, *cert_n = NULL, *remip_n = NULL; + int i = 0; + + // Query the database for registered attempts + res = sqlite_query_mapped(ctx, SQL_SELECT, + "SELECT username, digest, remoteip, attempts," + " registered, last_attempt, atpid" + " FROM openvpn_attempts", + NULL, fmap, "atpid"); + if( res == NULL ) { + eurephia_log(ctx, LOG_ERROR, 0, "Error querying the attempts log"); + return 0; + } + + eurephiaXML_CreateDoc(ctx, 1, "attempts", &doc, &root_n); + xmlNewProp(root_n, (xmlChar *) "mode", (xmlChar *) "list"); + + uname_n = xmlNewChild(root_n, NULL, (xmlChar *) "username", NULL); + cert_n = xmlNewChild(root_n, NULL, (xmlChar *) "certificate", NULL); + remip_n = xmlNewChild(root_n, NULL, (xmlChar *) "ipaddress", NULL); + assert( (uname_n != NULL) && (cert_n != NULL) && (remip_n != NULL) ); + + for( i = 0; i < sqlite_get_numtuples(res); i++ ) { + xmlNode *atmpt_n = NULL; + + if( sqlite_get_value(res, i, 0) != NULL ) { // Username + atmpt_n = xmlNewChild(uname_n, NULL, (xmlChar *) "attempt", NULL); + sqlite_xml_value(atmpt_n, XML_NODE, "username", res, i, 0); + } else if( sqlite_get_value(res, i, 1) != NULL ) { // Digest + atmpt_n = xmlNewChild(cert_n, NULL, (xmlChar *) "attempt", NULL); + sqlite_xml_value(atmpt_n, XML_NODE, "certificate", res, i, 1); + } else if( sqlite_get_value(res, i, 2) != NULL ) { // IP address + atmpt_n = xmlNewChild(remip_n, NULL, (xmlChar *) "attempt", NULL); + sqlite_xml_value(atmpt_n, XML_NODE, "ipaddress", res, i, 2); + } else { + continue; + } + + sqlite_xml_value(atmpt_n, XML_ATTR, "atpid", res, i, 6); + sqlite_xml_value(atmpt_n, XML_ATTR, "attempts", res, i, 3); + sqlite_xml_value(atmpt_n, XML_NODE, "registered", res, i, 4); + sqlite_xml_value(atmpt_n, XML_NODE, "last_attempt", res, i, 5); + } + sqlite_free_results(res); + return doc; +} + + +xmlDoc *attempts_add(eurephiaCTX *ctx, eDBfieldMap *fmap) { + return NULL; +} + +xmlDoc *attempts_delete(eurephiaCTX *ctx, eDBfieldMap *fmap) { + return NULL; +} + + diff --git a/database/sqlite/fieldmapping.h b/database/sqlite/fieldmapping.h index 3a84f8d..f64079b 100644 --- a/database/sqlite/fieldmapping.h +++ b/database/sqlite/fieldmapping.h @@ -102,4 +102,16 @@ static eDBfieldMap tbl_sqlite_openvpnaccesses[] = { }; #endif +#ifdef FMAP_OVPNATTEMPTS +static eDBfieldMap tbl_sqlite_attempts[] = { + {TABLE_ATTEMPTS, NULL, FIELD_UNAME, ft_STRING, flt_EQ, "username", NULL, NULL}, + {TABLE_ATTEMPTS, NULL, FIELD_REMOTEIP, ft_STRING, flt_EQ, "remoteip", NULL, NULL}, + {TABLE_ATTEMPTS, NULL, FIELD_ATTEMPTS, ft_INT, flt_EQ, "attempts", NULL, NULL}, + {TABLE_ATTEMPTS, NULL, FIELD_REGISTERED, ft_DATETIME, flt_EQ, "registered", NULL, NULL}, + {TABLE_ATTEMPTS, NULL, FIELD_LASTATTEMPT, ft_DATETIME, flt_EQ, "last_attempt", NULL, NULL}, + {TABLE_ATTEMPTS, NULL, FIELD_RECID, ft_INT, flt_EQ, "id", NULL, NULL }, + {0, NULL, FIELD_NONE, ft_UNDEF, flt_NOTSET, NULL, NULL, NULL} +}; +#endif + #endif /* !FIELDMAPPING_H_ */ |