From: Andreas Steffen Date: Tue, 27 May 2014 17:02:36 +0000 (+0200) Subject: Completed the command line options of the pt-tls-client X-Git-Tag: 5.2.0dr5~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75498e6b337c1f7aa40dd03ab33edbb85982f0ad;p=thirdparty%2Fstrongswan.git Completed the command line options of the pt-tls-client --- diff --git a/src/pt-tls-client/pt-tls-client.c b/src/pt-tls-client/pt-tls-client.c index 90edb0c8e2..631ae3cce4 100644 --- a/src/pt-tls-client/pt-tls-client.c +++ b/src/pt-tls-client/pt-tls-client.c @@ -1,6 +1,7 @@ /* * 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 * under the terms of the GNU General Public License as published by the @@ -21,6 +22,7 @@ #include #include #include +#include #include #include @@ -35,12 +37,13 @@ /** * Print usage information */ -static void usage(FILE *out, char *cmd) +static void usage(FILE *out) { - fprintf(out, "usage:\n"); - fprintf(out, " %s --connect
[--port ] [--cert ]+\n", cmd); - fprintf(out, " [--client ] [--secret ]\n"); - fprintf(out, " [--optionsfrom ]\n"); + fprintf(out, + "Usage: pt-tls --connect [--port ]\n" + " [--cert ]+ [--key ]\n" + " [--client ] [--secret ]\n" + " [--optionsfrom ] [--quiet] [--debug ]\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, ...) { - if (level <= pt_tls_level) + char buffer[8192]; + char *current = buffer, *next; + va_list args; + + if (level <= default_loglevel) { - va_list args; + if (log_to_stderr) + { + 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; + } + } + } +} - va_start(args, fmt); - vfprintf(stderr, fmt, args); - fprintf(stderr, "\n"); - va_end(args); +/** + * 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"); libtnccs_init(); - dbg = dbg_pt_tls; + init_log("pt-tls-client"); options = options_create(); 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' }, {"cert", required_argument, NULL, 'x' }, {"key", required_argument, NULL, 'k' }, + {"quiet", no_argument, NULL, 'q' }, {"debug", required_argument, NULL, 'd' }, {"optionsfrom", required_argument, NULL, '+' }, {0,0,0,0 } @@ -212,56 +259,59 @@ int main(int argc, char *argv[]) { case EOF: break; - case 'h': - usage(stdout, argv[0]); + case 'h': /* --help */ + usage(stdout); return 0; - case 'x': + case 'x': /* --cert */ if (!load_certificate(optarg)) { return 1; } continue; - case 'k': + case 'k': /* --key */ if (!load_key(optarg)) { return 1; } continue; - case 'c': + case 'c': /* --connect */ if (address) { - usage(stderr, argv[0]); + usage(stderr); return 1; } address = optarg; continue; - case 'i': + case 'i': /* --client */ identity = optarg; continue; - case 's': + case 's': /* --secret */ secret = optarg; continue; - case 'p': + case 'p': /* --port */ port = atoi(optarg); continue; - case 'd': - pt_tls_level = atoi(optarg); + case 'q': /* --quiet */ + log_to_stderr = FALSE; + continue; + case 'd': /* --debug */ + default_loglevel = atoi(optarg); continue; - case '+': /* --optionsfrom */ + case '+': /* --optionsfrom */ if (!options->from(options, optarg, &argc, &argv, optind)) { return 1; } continue; default: - usage(stderr, argv[0]); + usage(stderr); return 1; } break; } if (!address) { - usage(stderr, argv[0]); + usage(stderr); return 1; } if (secret)