aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlns <matzeton@googlemail.com>2018-06-08 15:15:51 +0200
committerlns <matzeton@googlemail.com>2018-06-08 15:15:51 +0200
commit0e1a9f70b3e720b506d69db6c920ce017834ae94 (patch)
tree8b6bb2a337af236975f779c2e82ef5d308bb7136
parent18914495e90639c12eeec0805bbc74c82700e205 (diff)
POTD skeleton #98.
Signed-off-by: lns <matzeton@googlemail.com>
-rw-r--r--configure.ac3
-rw-r--r--src/main.c3
-rw-r--r--src/options.c145
-rw-r--r--src/options.h2
4 files changed, 142 insertions, 11 deletions
diff --git a/configure.ac b/configure.ac
index 29ad884..46e219e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -229,5 +229,8 @@ AC_CHECK_FUNCS([epoll_create1 epoll_ctl epoll_pwait], [],
potd_logfile="/var/log/potd.log"
AC_DEFINE_UNQUOTED([POTD_LOGFILE], ["$potd_logfile"],
[default path to the log file])
+potd_defroot="/var/run/potd-root"
+AC_DEFINE_UNQUOTED([POTD_DEFROOT], ["$potd_defroot"],
+ [default path to potd rootfs image/directory])
AC_OUTPUT(Makefile src/Makefile)
diff --git a/src/main.c b/src/main.c
index ebbc2e8..6156bc3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -58,8 +58,7 @@ static void jail_preinit(char jail_hosts[][2][NI_MAXHOST],
jail_hosts[i][0], jail_ports[i][0]);
jail_init_ctx(&ctx[i], MAX_STACKSIZE);
- ctx[i]->newroot = strdup("/home/lns/git/busybox/sysroot");
- //ctx[i]->newroot = strdup("/home/toni/git/busybox/_install");
+ ctx[i]->newroot = getopt_str(OPT_ROOT);
ABORT_ON_FATAL( jail_setup(ctx[i], jail_hosts[i][0], jail_ports[i][0]),
"Jail daemon setup" );
ABORT_ON_FATAL( jail_validate_ctx(ctx[i]),
diff --git a/src/options.c b/src/options.c
index 4b14bf1..343bfc5 100644
--- a/src/options.c
+++ b/src/options.c
@@ -12,7 +12,6 @@
#include <getopt.h>
#include "options.h"
-#include "log.h"
typedef enum opt_type {
OT_INVALID = 0, OT_NOARG, OT_L, OT_LL, OT_STR,
@@ -53,20 +52,43 @@ struct opt {
#define OPT_NOARG(arg, short_help, help) \
OPT(OT_NOARG, .ll = 0, arg, short_help, help)
static struct opt options[OPT_MAX+1] = {
- OPT_NOARG("log-to-file", "short_help", "help"),
- OPT(OT_PATH, .str = POTD_LOGFILE, "log-file", "short help", "help"),
- OPT(OT_STR, .ll = NOTICE, "log-level", "short help", "help"),
- OPT_NOARG("daemon", "short help", "help"),
- OPT_LIST(OT_STR, .ll = 0, "redirect", "short_help", "help"),
- OPT_LIST(OT_STR, .ll = 0, "protocol", "short_help", "help"),
- OPT_LIST(OT_STR, .ll = 0, "jail", "short_help", "help"),
-
+ OPT_NOARG("log-to-file", "log to the default logfile path\n", NULL),
+ OPT(OT_PATH, .str = POTD_LOGFILE, "log-file", "specify a logfile path\n",
+ NULL),
+ OPT(OT_STR, .str = "notice", "log-level", "set the loglevel\n",
+ "error - log only errors\n"
+ "warning - log errors,warnings\n"
+ "notice - log errors,warnings,notices\n"
+ "debug - log all messages\n"),
+ OPT_NOARG("daemon", "fork into background if possible\n", NULL),
+ OPT_LIST(OT_STR, .str = NULL, "redirect", "setup redirector service\n",
+ "format [listen]:[forward-to-protocol]\n"
+ "where [listen] contains [listen-addr]:[listen-port]\n"
+ "and [forward-to-protocol] contains [forward-addr]:[forward-port]\n"
+ "Example: 0.0.0.0:2222:127.0.0.1:22222\n"),
+ OPT_LIST(OT_STR, .str = NULL, "protocol", "setup (ssh) protocol service\n",
+ "format [listen]:[forward-to-jail]\n"
+ "where [listen] contains [listen-addr]:[listen-port]\n"
+ "and [forward-to-jail] contains [forward-addr]:[forward-port]\n"
+ "Example: 0.0.0.0:2222:127.0.0.1:22222\n"),
+ OPT_LIST(OT_STR, .str = NULL, "jail", "setup jail service\n",
+ "format [listen]\n"
+ "where [listen] contains [listen-addr]:[listen-port]\n"
+ "Example: 127.0.0.1:33333\n"),
+ OPT(OT_PATH, .str = POTD_DEFROOT, "rootfs",
+ "path to root directory/image\n", NULL),
+
+ OPT_NOARG("help", "this\n", NULL),
OPT(OT_INVALID, .ll = 0, NULL, NULL, NULL)
};
static int opt_convert(opt_type t, opt_ptr *d);
static int setopt_list(struct opt *o, const char *optarg);
static int setopt(struct opt *o, const char *optarg);
+static size_t snprint_multilined_ljust(const char *prefix,
+ const char *multiline,
+ char *buf, size_t siz);
+static void usage(int print_copyright);
static int opt_convert(opt_type t, opt_ptr *d)
@@ -135,6 +157,104 @@ noarg:
return 0;
}
+static size_t snprint_multilined_ljust(const char *prefix,
+ const char *multiline,
+ char *buf, size_t siz)
+{
+ const char sep[] = "\n";
+ const char *start, *end;
+ size_t off;
+
+ off = 0;
+ start = multiline;
+ end = NULL;
+ do {
+ if (start) {
+ end = strstr(start, sep);
+ if (end) {
+ off += snprintf(buf + off, siz - off, "%s%.*s\n", prefix,
+ (int) (end-start), start);
+ start = end + strlen(sep);
+ }
+ }
+ } while (start && end);
+
+ return off;
+}
+
+static void usage(int print_copyright)
+{
+ int i, has_default;
+ size_t off;
+ char spaces[16];
+ char buf_arg[64];
+ char buf_shorthelp[BUFSIZ];
+ char buf_help[BUFSIZ];
+ char value[32];
+
+#ifdef HAVE_CONFIG_H
+ if (print_copyright)
+ fprintf(stderr, "\n%s (C) 2018 Toni Uhlig <%s>\n\n",
+ PACKAGE_STRING, PACKAGE_BUGREPORT);
+ else
+#endif
+ if (1)
+ fprintf(stderr, "%s", "\n");
+
+ memset(spaces, ' ', sizeof spaces);
+ spaces[sizeof spaces - 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],
+ options[i].short_help,
+ buf_shorthelp,
+ sizeof buf_shorthelp);
+
+ memset(buf_help, 0, sizeof buf_help);
+ off = 0;
+ if (options[i].help)
+ off = snprint_multilined_ljust(spaces, options[i].help,
+ buf_help, sizeof buf_help);
+
+ has_default = 0;
+ switch (options[i].type) {
+ case OT_L:
+ snprintf(value, sizeof value, "default: %lld\n",
+ options[i].def_value.ll);
+ has_default = 1;
+ break;
+ case OT_LL:
+ snprintf(value, sizeof value, "default: %ld\n",
+ options[i].def_value.l);
+ has_default = 1;
+ break;
+ case OT_STR:
+ case OT_PATH:
+ if (options[i].def_value.str) {
+ snprintf(value, sizeof value, "default: %s\n",
+ options[i].def_value.str);
+ has_default = 1;
+ }
+ break;
+ case OT_INVALID:
+ case OT_NOARG:
+ default:
+ break;
+ }
+ if (has_default)
+ snprint_multilined_ljust(spaces, value,
+ buf_help + off, sizeof buf_help - off);
+
+ fprintf(stderr, "%16s %s\n"
+ "%s\n", buf_arg,
+ buf_shorthelp, buf_help);
+ }
+}
+
int parse_cmdline(int argc, char **argv)
{
int rc, i, option, option_index;
@@ -177,6 +297,13 @@ int parse_cmdline(int argc, char **argv)
error:
free(o);
+ if (rc)
+ usage(0);
+ else if (getopt_used(OPT_HELP)) {
+ usage(1);
+ exit(EXIT_SUCCESS);
+ }
+
return rc;
}
diff --git a/src/options.h b/src/options.h
index 1754ccc..5cd7c0d 100644
--- a/src/options.h
+++ b/src/options.h
@@ -9,7 +9,9 @@ typedef enum opt_name {
OPT_REDIRECT,
OPT_PROTOCOL,
OPT_JAIL,
+ OPT_ROOT,
+ OPT_HELP,
OPT_MAX
} opt_name;