aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2018-06-09 18:15:47 +0200
committerlns <matzeton@googlemail.com>2018-06-09 18:15:47 +0200
commit531e07c02848deec10eb1c9057c6d6b3d444ad73 (patch)
tree8bb375f11bf0c4d9bafa78e82aab04709d70a1f1
parentef10351f70c3d41e536c024b71d811bad6e98884 (diff)
POTD skeleton #100.
Signed-off-by: lns <matzeton@googlemail.com>
-rw-r--r--src/forward.c6
-rw-r--r--src/main.c15
-rw-r--r--src/options.c15
-rw-r--r--src/utils.c47
4 files changed, 67 insertions, 16 deletions
diff --git a/src/forward.c b/src/forward.c
index 590892b..ec71995 100644
--- a/src/forward.c
+++ b/src/forward.c
@@ -82,17 +82,17 @@ int fwd_setup_server(forward_ctx *ctx, const char *listen_addr,
s = socket_init_in(listen_addr, listen_port, &fwd_addr);
if (s) {
- E_GAIERR(s, "Could not initialise server forward socket");
+ E_GAIERR(s, "Initialising server forward socket");
return 1;
}
if (socket_bind_in(&ctx->sock, &fwd_addr)) {
- E_STRERR("Could not bind forward server socket to %s:%s",
+ E_STRERR("Binding forward server socket to %s:%s",
listen_addr, listen_port);
return 1;
}
s = socket_addrtostr_in(&ctx->sock, ctx->host_buf, ctx->service_buf);
if (s) {
- E_GAIERR(s, "Could not convert server forward socket address");
+ E_GAIERR(s, "Convert server forward socket address");
return 1;
}
diff --git a/src/main.c b/src/main.c
index 6156bc3..ad6c1a0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -196,11 +196,12 @@ static size_t validate_hostport_option(opt_name on, int process_forward)
return rc;
}
+#define POSITIVE_VALIDATIONS 3
static int process_options(int validate_only)
{
char *value = NULL;
struct opt_list *ol;
- size_t i, siz;
+ size_t i, siz, rc = 0;
siz = validate_hostport_option(OPT_JAIL, 0);
if (siz && !validate_only) {
@@ -223,6 +224,8 @@ static int process_options(int validate_only)
jail_preinit(hosts, ports, &jl_ctx, jl_siz);
jl_pid = jail_init(&jl_ctx, jl_siz);
}
+ if (siz)
+ rc++;
siz = validate_hostport_option(OPT_PROTOCOL, 1);
if (siz && !validate_only) {
@@ -246,6 +249,8 @@ static int process_options(int validate_only)
ssh_protocol_preinit(hosts, ports, &prt_ctx, prt_siz);
ssh_protocol_init(&prt_ctx, prt_siz);
}
+ if (siz)
+ rc++;
siz = validate_hostport_option(OPT_REDIRECT, 1);
if (siz && !validate_only) {
@@ -269,8 +274,10 @@ static int process_options(int validate_only)
rdr_preinit(hosts, ports, &rdr_ctx, rdr_siz);
rdr_init(&rdr_ctx, rdr_siz);
}
+ if (siz)
+ rc++;
- return 0;
+ return rc;
}
int main(int argc, char *argv[])
@@ -289,7 +296,7 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if (process_options(1)) {
+ if (process_options(1) != POSITIVE_VALIDATIONS) {
fprintf(stderr, "%s: invalid config detected\n", argv[0]);
exit(EXIT_FAILURE);
}
@@ -346,7 +353,7 @@ int main(int argc, char *argv[])
ABORT_ON_FATAL( set_master_sighandler(),
"Master sighandler" );
- ABORT_ON_FATAL( process_options(0),
+ ABORT_ON_FATAL( process_options(0) != POSITIVE_VALIDATIONS,
"Setup redirect/protocol/jail instances");
while (1) {
diff --git a/src/options.c b/src/options.c
index f06cbb1..7b83909 100644
--- a/src/options.c
+++ b/src/options.c
@@ -188,7 +188,8 @@ static void usage(const char *arg0, int print_copyright)
{
int i, has_default;
size_t off;
- char spaces[16];
+ char spaces[6];
+ char spaces_long[28];
char buf_arg[64];
char buf_shorthelp[BUFSIZ];
char buf_help[BUFSIZ];
@@ -205,13 +206,15 @@ static void usage(const char *arg0, int print_copyright)
memset(spaces, ' ', sizeof spaces);
spaces[sizeof spaces - 1] = 0;
+ memset(spaces_long, ' ', sizeof spaces_long);
+ spaces_long[sizeof spaces_long - 1] = 0;
for (i = 0; i < OPT_MAX; ++i) {
snprintf(buf_arg, sizeof buf_arg, "--%s", options[i].arg_name);
memset(buf_shorthelp, 0, sizeof buf_shorthelp);
if (options[i].short_help)
- snprint_multilined_ljust(&spaces[sizeof spaces / 2],
+ snprint_multilined_ljust(spaces,
options[i].short_help,
buf_shorthelp,
sizeof buf_shorthelp);
@@ -219,7 +222,7 @@ static void usage(const char *arg0, int print_copyright)
memset(buf_help, 0, sizeof buf_help);
off = 0;
if (options[i].help)
- off = snprint_multilined_ljust(spaces, options[i].help,
+ off = snprint_multilined_ljust(spaces_long, options[i].help,
buf_help, sizeof buf_help);
has_default = 0;
@@ -248,10 +251,10 @@ static void usage(const char *arg0, int print_copyright)
break;
}
if (has_default)
- snprint_multilined_ljust(spaces, value,
- buf_help + off, sizeof buf_help - off);
+ snprint_multilined_ljust(&spaces_long[sizeof spaces_long / 2],
+ value, buf_help + off, sizeof buf_help - off);
- fprintf(stderr, "%16s %s\n"
+ fprintf(stderr, "%16s %s"
"%s\n", buf_arg,
buf_shorthelp, buf_help);
}
diff --git a/src/utils.c b/src/utils.c
index 049e3d5..3de57a2 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,3 +1,7 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -34,6 +38,8 @@ static const char *_cgmem = NULL;
static const char *_cgcpu = NULL;
static const char *_cgpid = NULL;
+static char *
+sig_to_str(int signo, char *buf, size_t siz);
static void sighandler_child(int signo);
static void sighandler_master(int signo);
static int cgroups_write_file(const char *cdir, const char *csub,
@@ -55,8 +61,37 @@ int set_fd_nonblock(int fd)
return 0;
}
+static char *
+sig_to_str(int signo, char *buf, size_t siz)
+{
+ switch (signo) {
+ case SIGCHLD:
+ strncpy(buf, "SIGCHLD", siz-1); break;
+ case SIGPIPE:
+ strncpy(buf, "SIGPIPE", siz-1); break;
+ case SIGABRT:
+ strncpy(buf, "SIGABRT", siz-1); break;
+ case SIGSEGV:
+ strncpy(buf, "SIGSEGV", siz-1); break;
+ case SIGTERM:
+ strncpy(buf, "SIGTERM", siz-1); break;
+ case SIGINT:
+ strncpy(buf, "SIGINT", siz-1); break;
+ case SIGHUP:
+ strncpy(buf, "SIGHUP", siz-1); break;
+ default:
+ strncpy(buf, "UNKNOWN", siz-1); break;
+ }
+ buf[siz - 1] = 0;
+
+ return buf;
+}
+
static void sighandler_child(int signo)
{
+ char buf[16] = {0};
+
+ W("Got signal[%d]: %s", signo, sig_to_str(signo, &buf[0], sizeof buf));
switch (signo) {
case SIGABRT:
exit(EXIT_FAILURE);
@@ -67,7 +102,11 @@ static void sighandler_child(int signo)
}
break;
case SIGSEGV:
- E("%s", "Got a SIGSEGV signal, abort ..");
+#ifdef HAVE_CONFIG_H
+ E("Segmentation fault .. please report to <%s>", PACKAGE_BUGREPORT);
+#else
+ E("%s", "Segmentation fault ..");
+#endif
exit(EXIT_FAILURE);
}
}
@@ -87,7 +126,9 @@ int set_child_sighandler(void)
static void sighandler_master(int signo)
{
static int exiting = 0;
+ char buf[16] = {0};
+ W("Got signal[%d]: %s", signo, sig_to_str(signo, &buf[0], sizeof buf));
switch (signo) {
case SIGSEGV:
case SIGINT:
@@ -96,7 +137,6 @@ static void sighandler_master(int signo)
if (exiting)
break;
exiting = 1;
- W("Got signal %d, exiting", signo);
kill(0, SIGTERM);
exit(EXIT_FAILURE);
}
@@ -448,7 +488,8 @@ int setup_network_namespace(const char *name)
E_STRERR("Create namespace file '%s'", netns_path);
return 1;
}
- close(fd);
+ if (fd >= 0)
+ close(fd);
if (unshare(CLONE_NEWNET) < 0) {
E_STRERR("Create network namespace '%s'", name);