aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-06-01 22:00:07 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-06-01 22:00:07 +0200
commitceae0f10b1d03ec6ba2748357c4bf5232817e9fa (patch)
tree1aa4653752a439e057f85b66f2d66d2f9cb35509
parent666ff89a231b8753f790e833b37537d9d09d5778 (diff)
POTD skeleton #89.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--.gitignore1
-rw-r--r--src/main.c48
-rw-r--r--src/options.c6
-rw-r--r--src/options.h8
-rw-r--r--src/utils.c32
-rw-r--r--src/utils.h3
6 files changed, 93 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5761abc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.o
diff --git a/src/main.c b/src/main.c
index 88deb7f..f0892fb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -28,6 +28,7 @@ static void ssh_protocol_init(protocol_ctx *ctx[], const size_t siz);
static void rdr_preinit(const char *rdr_ports[], redirector_ctx *ctx[],
const size_t siz);
static pid_t rdr_init(redirector_ctx *ctx[], const size_t siz);
+static int setup_instances(int validate_only);
static void jail_preinit(const char *jail_ports[], jail_ctx *ctx[],
@@ -111,6 +112,46 @@ static pid_t rdr_init(redirector_ctx *ctx[], const size_t siz)
return rdr_pid;
}
+static int setup_instances(int validate_only)
+{
+ char *value = NULL;
+ struct opt_list *ol = NULL;
+ char *hostport[2];
+ size_t hostport_siz[2];
+ size_t siz;
+
+ if (getopt_used(OPT_REDIRECT))
+ while ((value = getopt_strlist(OPT_REDIRECT, &ol))) {
+ siz = parse_hostport(value, hostport, hostport_siz);
+ if (!siz) {
+ fprintf(stderr, "%s: invalid redtirector host:port combination: '%s'\n",
+ arg0, value);
+ return 1;
+ }
+ }
+ if (getopt_used(OPT_PROTOCOL))
+ while ((value = getopt_strlist(OPT_PROTOCOL, &ol))) {
+ siz = parse_hostport(value, hostport, hostport_siz);
+ if (!siz) {
+ fprintf(stderr, "%s: invalid protocol host:port combination: '%s'\n",
+ arg0, value);
+ return 1;
+ }
+ }
+ if (getopt_used(OPT_JAIL))
+ while ((value = getopt_strlist(OPT_JAIL, &ol))) {
+ siz = parse_hostport(value, hostport, hostport_siz);
+ if (!siz) {
+ fprintf(stderr, "%s: invalid jail host:port combination: '%s'\n",
+ arg0, value);
+ return 1;
+ }
+ }
+ /* TODO: setup redirect/protocol/jail instances if validate_only == 0 */
+
+ return 0;
+}
+
int main(int argc, char *argv[])
{
char *value;
@@ -131,11 +172,16 @@ int main(int argc, char *argv[])
(void) argv;
arg0 = argv[0];
- if (options_cmdline(argc, argv)) {
+ if (parse_cmdline(argc, argv)) {
fprintf(stderr, "%s: command line parsing failed\n", argv[0]);
exit(EXIT_FAILURE);
}
+ if (setup_instances(1)) {
+ fprintf(stderr, "%s: invalid config detected\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
if (getopt_used(OPT_LOGTOFILE) || getopt_used(OPT_LOGFILE)) {
log_file = getopt_str(OPT_LOGFILE);
LOG_SET_FUNCS_VA(LOG_FILE_FUNCS);
diff --git a/src/options.c b/src/options.c
index 28c9a58..084751d 100644
--- a/src/options.c
+++ b/src/options.c
@@ -54,7 +54,9 @@ static struct opt options[OPT_MAX+1] = {
OPT(OT_PATH, .str = POTD_LOGFILE, "log-file", "short help", "help"),
OPT(OT_STR, .ll = 0, "log-level", "short help", "help"),
OPT_NOARG("daemon", "short help", "help"),
- OPT_LIST(OT_STR, .ll = 0, "test", "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(OT_INVALID, .ll = 0, NULL, NULL, NULL)
};
@@ -130,7 +132,7 @@ noarg:
return 0;
}
-int options_cmdline(int argc, char **argv)
+int parse_cmdline(int argc, char **argv)
{
int rc, i, option, option_index;
struct option *o = (struct option *) calloc(OPT_MAX+1, sizeof *o);
diff --git a/src/options.h b/src/options.h
index 1b3fe5b..1754ccc 100644
--- a/src/options.h
+++ b/src/options.h
@@ -6,13 +6,17 @@ struct opt_list;
typedef enum opt_name {
OPT_LOGTOFILE = 0, OPT_LOGFILE, OPT_LOGLEVEL,
OPT_DAEMON,
- OPT_TEST,
+ OPT_REDIRECT,
+ OPT_PROTOCOL,
+ OPT_JAIL,
OPT_MAX
} opt_name;
+typedef int check_opt;
-int options_cmdline(int argc, char **argv);
+
+int parse_cmdline(int argc, char **argv);
int getopt_used(opt_name on);
diff --git a/src/utils.c b/src/utils.c
index a0d2f64..de757af 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -637,3 +637,35 @@ void escape_ascii_string(const char ascii[], size_t siz, char **dest, size_t *ne
(*dest)[ns] = 0;
}
+
+size_t parse_hostport(char *str, char *result[2],
+ size_t siz[2])
+{
+ size_t i;
+ char *hostend = strchr(str, ':');
+ char *portend;
+ char sep[] = ": \t\n\0";
+
+ result[0] = NULL;
+ result[1] = NULL;
+ siz[0] = 0;
+ siz[1] = 0;
+
+ if (!hostend)
+ return 0;
+ hostend++;
+ for (i = 0; i < SIZEOF(sep); ++i) {
+ portend = strchr(hostend, sep[i]);
+ if (portend)
+ break;
+ }
+ if (!portend)
+ return 0;
+
+ result[0] = str;
+ result[1] = hostend;
+ siz[0] = hostend - str - 1;
+ siz[1] = portend - hostend;
+
+ return siz[0] + siz[1] + 1;
+}
diff --git a/src/utils.h b/src/utils.h
index c46f9e5..75f3c92 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -62,4 +62,7 @@ int update_setgroups_self(int allow);
void escape_ascii_string(const char ascii[], size_t siz,
char **dest, size_t *newsiz);
+size_t parse_hostport(char *str, char *result[2],
+ size_t siz[2]);
+
#endif