diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2008-11-28 12:22:54 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-11-28 12:22:54 +0100 |
commit | 9fc0e26e928add6b5124d60ea2c2fe38ac7259f1 (patch) | |
tree | a8128dc744218ad062d9d2dcb3f9600bb9e9e505 | |
parent | 622fb9169d5c554d5f5c8ca42e978f133be5bb45 (diff) | |
download | eurephia-9fc0e26e928add6b5124d60ea2c2fe38ac7259f1.tar.gz eurephia-9fc0e26e928add6b5124d60ea2c2fe38ac7259f1.tar.xz eurephia-9fc0e26e928add6b5124d60ea2c2fe38ac7259f1.zip |
Added first skeleton of a command line admin util
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 7 | ||||
-rwxr-xr-x | configure | 8 | ||||
-rw-r--r-- | eurephiadm/CMakeLists.txt | 13 | ||||
-rw-r--r-- | eurephiadm/eurephiadm.c | 127 |
5 files changed, 156 insertions, 0 deletions
@@ -14,3 +14,4 @@ CMakeFiles/* */*/*/CMakeFiles/* */*/cmake_install.cmake Makefile +eurephiadm/eurephiadm diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c74924..0678a2b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 2.6) OPTION(DEBUG "Add more verbose debug information" OFF) OPTION(SHOW_SECRETS "Show passwords as clear text in logs." OFF) OPTION(SQLITE3 "Build database driver for SQLite3" OFF) +OPTION(EUREPHIADM "Build command line based admin utility" OFF) + SET(OPENVPN_SRC "" CACHE STRING "Path to OpenVPN source code") SET(PREFIX "/etc/openvpn/eurephia" CACHE STRING "Install prefix for eurephia") @@ -14,6 +16,11 @@ IF(SQLITE3) SET(DATABASE ON) ENDIF(SQLITE3) +IF(EUREPHIADM) + message(STATUS "Will build command line based admin utility") + SET(subdirs ${subdirs} eurephiadm) +ENDIF(EUREPHIADM) + IF(NOT DATABASE) message(FATAL_ERROR "Cannot build eurephia without any database drivers.") ENDIF(NOT DATABASE) @@ -15,12 +15,14 @@ configure help for eurephia --db-sqlite3 | -- Build SQLite3 database module --sqlite3-prefix | -sp -- Root directory of SQLite3 eurephia database (default: /etc/openvpn) + --eurephiadm | -A -- Build command line admin utility EOF } PARAMS="" DB="" FW="" +ADMIN=""; OPENVPN_SRC_DIR="" PREFIX="/etc/openvpn/eurephia" SQLITE3PREFIX="/etc/openvpn" @@ -59,6 +61,10 @@ while [ ! -z "$1" ]; do SQLITE3PREFIX="$2" shift ;; + --eurephiadm|-A) + PARAMS="${PARAMS} -DEUREPHIADM=ON" + ADMIN="${ADMIN}eurephiadm " + ;; *) echo "Unkown option: $1" exit 2 @@ -96,11 +102,13 @@ if [ $ec = 0 ]; then echo echo " Database: ${DB}" echo " Firewall: ${FW:-"None"}" + echo " Admin tools: ${ADMIN:-"None"}" echo echo " Install prefix: ${PREFIX}" if [ ${DB} = "SQLite3" ]; then echo " SQLite3 database path: ${SQLITE3PREFIX}" fi + echo echo if [ "$DEBUG_WARN" = 1 ]; then diff --git a/eurephiadm/CMakeLists.txt b/eurephiadm/CMakeLists.txt new file mode 100644 index 0000000..1eae151 --- /dev/null +++ b/eurephiadm/CMakeLists.txt @@ -0,0 +1,13 @@ +PROJECT(eurephiadm C) +cmake_minimum_required(VERSION 2.6) + +SET(efw_ipt_SRC + eurephiadm.c + ../common/eurephia_log.c + ../common/eurephia_getsym.c + ../database/eurephiadb.c +) + +INCLUDE_DIRECTORIES(../common ../database) +ADD_EXECUTABLE(eurephiadm ${efw_ipt_SRC}) +TARGET_LINK_LIBRARIES(eurephiadm dl) diff --git a/eurephiadm/eurephiadm.c b/eurephiadm/eurephiadm.c new file mode 100644 index 0000000..2a7df60 --- /dev/null +++ b/eurephiadm/eurephiadm.c @@ -0,0 +1,127 @@ +/* eurephiadm.c -- CLI based admin program for eurephia + * + * GPLv2 - Copyright (C) 2008 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +#include <eurephia_context.h> +#include <eurephia_nullsafe.h> +#include <eurephia_log.h> +#include <eurephiadb_driver.h> +#include <eurephiadb.h> + +#define MAX_ARGUMENTS 64 + +eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, const char *dbi) { + eurephiaCTX *ctx = NULL; + + ctx = (eurephiaCTX *) malloc(sizeof(eurephiaCTX)+2); + assert(ctx != NULL); + memset(ctx, 0, sizeof(eurephiaCTX)+2); + + ctx->log = log; + ctx->loglevel = loglevel; + + if( !eDBlink_init(ctx, dbi, 2) ){ + eurephia_log(ctx, LOG_PANIC, 0, "Could not load the database driver"); + free_nullsafe(ctx); + return NULL; + } + return ctx; +} + +void eurephiaCTX_destroy(eurephiaCTX *ctx) { + if( ctx == NULL ) { + return; + } + + if( (ctx->dbc != NULL) && (ctx->dbc->dbhandle != NULL) ) { + eDBdisconnect(ctx); + } + + if( ctx->eurephia_driver != NULL ) { + eDBlink_close(ctx); + } + + if( ctx->log != NULL ) { + fflush(ctx->log); + + // Do not close log file if we're on stdout or stderr + if( (ctx->log != stderr) && (ctx->log != stdout) ) { + eurephia_log(ctx, LOG_INFO, 2, "Closing log file"); + fclose(ctx->log); + } + + ctx->log = NULL; + ctx->loglevel = 0; + } + free_nullsafe(ctx); +} + +int eurephia_ConnectDB(eurephiaCTX *ctx, const char *argstr) { + char *delims = " "; + char *cp = NULL; + const char *dbargv[MAX_ARGUMENTS]; + int dbargc = 0; + + if( (argstr == NULL) || (strlen(argstr) < 1) ) { + eurephia_log(ctx, LOG_FATAL, 0, "No database connection string given"); + return 0; + } + + // Split up argstr into separate arguments + cp = strdup(argstr); + assert(cp != NULL); + + dbargc = 0; + dbargv[dbargc] = strtok(cp, delims); + while( (dbargv[dbargc] != NULL) && (dbargc <= MAX_ARGUMENTS) ) { + fprintf(stderr, "[%i] |%s|\n", dbargc, dbargv[dbargc]); + dbargv[++dbargc] = strtok(NULL, delims); + } + + if( !eDBconnect(ctx, dbargc, dbargv) ) { + eurephia_log(ctx, LOG_PANIC, 0, "Could not connect to the database"); + eDBlink_close(ctx); + return 0; + } + + return 1; +} + + +int main(int argc, char **argv) { + eurephiaCTX *ctx = NULL; + + ctx = eurephiaCTX_init(stderr, 10, "../database/sqlite/edb-sqlite.so"); + if( ctx == NULL ) { + fprintf(stderr, "Could not initialise a eurephia context.\n"); + return 1; + } + + if( !eurephia_ConnectDB(ctx, argv[1]) ) { + return 2; + } + + eurephiaCTX_destroy(ctx); + return 0; +} |