Completed the command line options of the pt-tls-client

This commit is contained in:
Andreas Steffen
2014-05-31 20:37:56 +02:00
parent 34cd3e102e
commit 75498e6b33
+80 -30
View File
@@ -1,6 +1,7 @@
/* /*
* Copyright (C) 2010-2013 Martin Willi, revosec AG * Copyright (C) 2010-2013 Martin Willi, revosec AG
* Copyright (C) 2013 Andreas Steffen, HSR Hochschule für Technik Rapperswil * Copyright (C) 2013-2014 Andreas Steffen
* HSR Hochschule für Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * 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 * under the terms of the GNU General Public License as published by the
@@ -21,6 +22,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <syslog.h>
#include <pt_tls.h> #include <pt_tls.h>
#include <pt_tls_client.h> #include <pt_tls_client.h>
@@ -35,12 +37,13 @@
/** /**
* Print usage information * Print usage information
*/ */
static void usage(FILE *out, char *cmd) static void usage(FILE *out)
{ {
fprintf(out, "usage:\n"); fprintf(out,
fprintf(out, " %s --connect <address> [--port <port>] [--cert <file>]+\n", cmd); "Usage: pt-tls --connect <hostname|address> [--port <port>]\n"
fprintf(out, " [--client <client-id>] [--secret <password>]\n"); " [--cert <file>]+ [--key <file>]\n"
fprintf(out, " [--optionsfrom <filename>]\n"); " [--client <client-id>] [--secret <password>]\n"
" [--optionsfrom <filename>] [--quiet] [--debug <level>]\n");
} }
/** /**
@@ -121,20 +124,63 @@ static bool load_key(char *filename)
} }
/** /**
* Debug level * Logging and debug level
*/ */
static level_t pt_tls_level = 1; static bool log_to_stderr = TRUE;
static bool log_to_syslog = TRUE;
static level_t default_loglevel = 1;
static void dbg_pt_tls(debug_t group, level_t level, char *fmt, ...) static void dbg_pt_tls(debug_t group, level_t level, char *fmt, ...)
{ {
if (level <= pt_tls_level) char buffer[8192];
{ char *current = buffer, *next;
va_list args; va_list args;
va_start(args, fmt); if (level <= default_loglevel)
vfprintf(stderr, fmt, args); {
fprintf(stderr, "\n"); if (log_to_stderr)
va_end(args); {
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
if (log_to_syslog)
{
/* write in memory buffer first */
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
/* do a syslog with every line */
while (current)
{
next = strchr(current, '\n');
if (next)
{
*(next++) = '\0';
}
syslog(LOG_INFO, "%s\n", current);
current = next;
}
}
}
}
/**
* Initialize logging to stderr/syslog
*/
static void init_log(const char *program)
{
dbg = dbg_pt_tls;
if (log_to_stderr)
{
setbuf(stderr, NULL);
}
if (log_to_syslog)
{
openlog(program, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
} }
} }
@@ -169,7 +215,7 @@ static void init()
library_init(NULL, "pt-tls-client"); library_init(NULL, "pt-tls-client");
libtnccs_init(); libtnccs_init();
dbg = dbg_pt_tls; init_log("pt-tls-client");
options = options_create(); options = options_create();
lib->plugins->add_static_features(lib->plugins, "pt-tls-client", features, lib->plugins->add_static_features(lib->plugins, "pt-tls-client", features,
@@ -204,6 +250,7 @@ int main(int argc, char *argv[])
{"port", required_argument, NULL, 'p' }, {"port", required_argument, NULL, 'p' },
{"cert", required_argument, NULL, 'x' }, {"cert", required_argument, NULL, 'x' },
{"key", required_argument, NULL, 'k' }, {"key", required_argument, NULL, 'k' },
{"quiet", no_argument, NULL, 'q' },
{"debug", required_argument, NULL, 'd' }, {"debug", required_argument, NULL, 'd' },
{"optionsfrom", required_argument, NULL, '+' }, {"optionsfrom", required_argument, NULL, '+' },
{0,0,0,0 } {0,0,0,0 }
@@ -212,56 +259,59 @@ int main(int argc, char *argv[])
{ {
case EOF: case EOF:
break; break;
case 'h': case 'h': /* --help */
usage(stdout, argv[0]); usage(stdout);
return 0; return 0;
case 'x': case 'x': /* --cert <file> */
if (!load_certificate(optarg)) if (!load_certificate(optarg))
{ {
return 1; return 1;
} }
continue; continue;
case 'k': case 'k': /* --key <file> */
if (!load_key(optarg)) if (!load_key(optarg))
{ {
return 1; return 1;
} }
continue; continue;
case 'c': case 'c': /* --connect <hostname|address> */
if (address) if (address)
{ {
usage(stderr, argv[0]); usage(stderr);
return 1; return 1;
} }
address = optarg; address = optarg;
continue; continue;
case 'i': case 'i': /* --client <client-id> */
identity = optarg; identity = optarg;
continue; continue;
case 's': case 's': /* --secret <password> */
secret = optarg; secret = optarg;
continue; continue;
case 'p': case 'p': /* --port <port> */
port = atoi(optarg); port = atoi(optarg);
continue; continue;
case 'd': case 'q': /* --quiet */
pt_tls_level = atoi(optarg); log_to_stderr = FALSE;
continue; continue;
case '+': /* --optionsfrom <filename> */ case 'd': /* --debug <level> */
default_loglevel = atoi(optarg);
continue;
case '+': /* --optionsfrom <filename> */
if (!options->from(options, optarg, &argc, &argv, optind)) if (!options->from(options, optarg, &argc, &argv, optind))
{ {
return 1; return 1;
} }
continue; continue;
default: default:
usage(stderr, argv[0]); usage(stderr);
return 1; return 1;
} }
break; break;
} }
if (!address) if (!address)
{ {
usage(stderr, argv[0]); usage(stderr);
return 1; return 1;
} }
if (secret) if (secret)