diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2008-09-24 17:11:46 +0200 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-09-24 17:11:46 +0200 |
commit | f5c8208613cad929aa5c5e3b7a063eba09737fde (patch) | |
tree | d67279be33c2342a34b6de3cfb614bf2e1f6f39e /eurephia.c | |
parent | 7a1ba700e0b021f040bfe83dd89368d97a477397 (diff) | |
download | eurephia-f5c8208613cad929aa5c5e3b7a063eba09737fde.tar.gz eurephia-f5c8208613cad929aa5c5e3b7a063eba09737fde.tar.xz eurephia-f5c8208613cad929aa5c5e3b7a063eba09737fde.zip |
Improved argument parsing for the eurephia-auth arguments passed from the config file.
Using getopt_long(...) to handle arguments. The following options are available:
[--log-destination | -l] {<filename> | openvpn: | none: }
If openvpn: is given, all errors will be written to stderr which
OpenVPN then will take care for getting logged
If none: is given, no logging will happen in eurephia-auth
In all other cases, a file with the given name will be opened
and all logs will go here.
If no --log-destination is given, it will let openvpn do the
logging.
[--log-level | -L ] <log level>
Sets log thresold level
[--database-interface | -i] <database driver>
Tells eurephia-auth which database driver (.so) file to load for
database handling
After these options, a '--' (double ones!) should be given, at least to clarify
that we are done with arguments for eurephia-auth. All arguments after '--' will
be sent directly and unparsed further to the database module. The database module
can then use it's own argument parsing.
For the eurephiadb-sqlite.so, it takes only one parameter - database file.
OpenVPN config example:
plugin /etc/openvpn/eurephia/eurephia-auth.so --log-destination /var/log/eurephia-auth.log --log-level 2 --database-interface /etc/openvpn/eurephia/eurephiadb-sqlite.so -- /etc/openvpn/eurephia/eurephiadb
Or a shorter version would be:
plugin /etc/openvpn/eurephia/eurephia-auth.so -l /var/log/eurephia-auth.log -L 2 -i /etc/openvpn/eurephia/eurephiadb-sqlite.so -- /etc/openvpn/eurephia/eurephiadb
Diffstat (limited to 'eurephia.c')
-rw-r--r-- | eurephia.c | 111 |
1 files changed, 75 insertions, 36 deletions
@@ -21,6 +21,7 @@ #include <stdarg.h> #include <stdlib.h> #include <string.h> +#include <getopt.h> #include <eurephiadb.h> #include <eurephiadb_driver.h> @@ -29,8 +30,7 @@ #include <eurephia_values.h> #include <certinfo.h> -#define MAX_ARGUMENTS 25 - +#define MAX_ARGUMENTS 64 // Get value of a environment variable @@ -75,54 +75,88 @@ const char *get_env(eurephiaCTX *ctx, const char *envp[], const char *fmt, ... ) // 1 2 3 4..... eurephiaCTX *eurephiaInit(const char **argv) { + static struct option eurephia_opts[] = { + {"log-destination", required_argument, 0, 'l'}, + {"log-level", required_argument, 0, 'L'}, + {"database-interface", required_argument, 0, 'i'}, + {0, 0, 0 ,0} + }; + int argc = 0, error = 0, loglvl = 0, dbargc = 0; + const char *dbargv[MAX_ARGUMENTS]; + const char *fwintf = NULL, *logfile = NULL, *dbi = NULL; eurephiaCTX *ctx = NULL; - int argc = 0, error=0; - const char *dbargs[MAX_ARGUMENTS]; - const char *fwintf = NULL; - - // Count how many arguments we have, and copy - // db arguments from argc >= 3 - while( argv[argc] != NULL ) { - argc++; - if( (argc > 2) ) { - // Make a copy to the argument table - // when we have gotten our three first - // arguments for logging. - // These arguments in dbargs, will - // be sent directly to the - // eDBconnect(...) function - dbargs[argc-3] = argv[argc]; + + // + // Parse input arguments + // + + // Count arguments + for( argc = 0; argv[argc] != NULL; argc++ ) {} + + while(1) { + int opt_idx = 0; + int c = 0; + + c = getopt_long(argc, (char **)argv, "l:L:i:", eurephia_opts, &opt_idx); + if( c == -1 ) { + break; + } + + switch( c ) { + case 'l': + logfile = optarg; + break; + + case 'L': + loglvl = atoi_nullsafe(optarg); + break; + + case 'i': + dbi = optarg; + break; + + default: + fprintf(stderr, "Error parsing eurephia-auth arguments.\n"); + return NULL; + break; } } - - // We need at least 3 arguments - logfile, loglevel and db driver - if( argc < 3 ) { - return NULL; + + // Put the rest of the arguments into an own array which will be the db module arguments + if( optind < argc ) { + // copy arguments, but make sure we do not exceed our limit + while( (optind < argc) && (dbargc < MAX_ARGUMENTS) ) { + dbargv[dbargc] = argv[optind++]; + dbargc++; + dbargv[dbargc] = NULL; + } } + // End of argument parsing + // Prepare a context area for eurephia-auth ctx = (eurephiaCTX *) malloc(sizeof(eurephiaCTX)+2); memset(ctx, 0, sizeof(eurephiaCTX)+2); // Open a log file - if( strlen_nullsafe(argv[1]) > 0) { - if( strcmp(argv[1], "none") != 0 ) { - ctx->log = fopen(argv[1], "aw"); - if( ctx->log == NULL ) { - fprintf(stderr, "Could not open eurephia log file: %s\n", argv[1]); - return NULL; - } + if( strcmp(logfile, "openvpn:") == 0 ) { // Let openvpn do the logging + ctx->log = stderr; + } else if( strcmp(logfile, "none:") == 0 ) { // Do not perform any logging + ctx->log = NULL; + } else { // if no hit on these ones, open a file with the given name + ctx->log = fopen(logfile, "aw"); + if( ctx->log == NULL ) { + fprintf(stderr, "Could not open eurephia log file: %s\n", argv[1]); + return NULL; } - } else { - ctx->log = fopen("/var/log/eurephia.log", "aw"); - error = (ctx->log == NULL); } // Set log verbosity ctx->loglevel = atoi_nullsafe(argv[2]); // Load the database driver - if( (error == 0) && eDBlink_init(ctx, argv[3]) ) { - if( !eDBconnect(ctx, argc-4, dbargs) ) { + if( (error == 0) && eDBlink_init(ctx, dbi) ) { + // Connect to the database + if( !eDBconnect(ctx, dbargc, dbargv) ) { eurephia_log(ctx, LOG_PANIC, 0, "Could not connect to the database"); error = 1; eDBlink_close(ctx); @@ -182,9 +216,14 @@ int eurephiaShutdown(eurephiaCTX *ctx) } if( ctx->log != NULL ) { - eurephia_log(ctx, LOG_INFO, 2, "Closing log file"); fflush(ctx->log); - fclose(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; } |