diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-05-31 14:57:42 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-05-31 14:57:42 +0200 |
commit | b41711082314d3dcc838f3adf73cc75e89bde7cc (patch) | |
tree | c0a3f299b35325db82e645cfa88904c10d2adc9d | |
parent | 9badf738fcd67ff8b61c56f28cdb15ac614eff7a (diff) |
POTD skeleton #85.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | README | 14 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/main.c | 21 | ||||
-rw-r--r-- | src/options.c | 40 | ||||
-rw-r--r-- | src/options.h | 2 | ||||
-rw-r--r-- | src/utils.c | 4 |
6 files changed, 61 insertions, 22 deletions
@@ -0,0 +1,14 @@ +honey[potd]aemon +================ + +This project is part of a BA thesis. It is all but not finished yet. + +Suits perfect for your favoured Desktop/Server/OpenWrt Linux system. + + +TODOs +===== + +1. implement more protocols such as SCADA/MySQL/telnet +2. improved event handling (maybe libevent?) +3. more/other jail options (e.g. per jail filesystem w/ image managment) diff --git a/configure.ac b/configure.ac index a124a8c..76a3a4e 100644 --- a/configure.ac +++ b/configure.ac @@ -168,7 +168,7 @@ dnl minimum required functions AC_CHECK_FUNCS([open read write close malloc free memset memcpy fork unshare \ getpwnam getgrnam setreuid setregid \ wait waitpid isprint remove unlink mkdir access stat chroot chdir mount umount mknod \ - strdup strncat strncpy snprintf vsnprintf printf fprintf getpid \ + strdup strcasecmp strncat strncpy snprintf vsnprintf printf fprintf getpid \ prctl signal signalfd fcntl getenv kill exit \ setsockopt socket connect accept bind listen \ time difftime strtol strtoll getopt_long_only], [], @@ -113,6 +113,7 @@ static pid_t rdr_init(redirector_ctx *ctx[], const size_t siz) int main(int argc, char *argv[]) { + char *value; const size_t rdr_siz = 3; const size_t proto_siz = 2; const size_t jail_siz = 2; @@ -131,7 +132,7 @@ int main(int argc, char *argv[]) arg0 = argv[0]; if (options_cmdline(argc, argv)) { - fprintf(stderr, "%s: parsing command line failed\n", argv[0]); + fprintf(stderr, "%s: command line parsing failed\n", argv[0]); exit(EXIT_FAILURE); } @@ -141,7 +142,21 @@ int main(int argc, char *argv[]) } else { LOG_SET_FUNCS_VA(LOG_COLORED_FUNCS); } - //log_prio = DEBUG; + if (getopt_used(OPT_LOGLEVEL)) { + value = getopt_str(OPT_LOGLEVEL); + if (!strcasecmp(value, "debug")) + log_prio = DEBUG; + else if (!strcasecmp(value, "notice")) + log_prio = NOTICE; + else if (!strcasecmp(value, "warning")) + log_prio = WARNING; + else if (!strcasecmp(value, "error")) + log_prio = ERROR; + else { + fprintf(stderr, "%s: unknown loglevel '%s'\n", argv[0], value); + exit(EXIT_FAILURE); + } + } if (log_open()) exit(EXIT_FAILURE); @@ -161,7 +176,7 @@ int main(int argc, char *argv[]) pseccomp_free(&psc); D("%s", "Forking into background/foreground"); - daemon_pid = daemonize(1); + daemon_pid = daemonize(!getopt_used(OPT_DAEMON)); ABORT_ON_FATAL( daemon_pid > 0, "Forking" ); if (daemon_pid == 0) { set_procname("[potd] main"); diff --git a/src/options.c b/src/options.c index 8d2248a..b9cd4f1 100644 --- a/src/options.c +++ b/src/options.c @@ -39,6 +39,7 @@ struct opt { static struct opt options[OPT_MAX+1] = { OPT_STR("./potd.log", 0, "log", "short help", "help"), OPT_STR(NULL, 0, "log-level", "short help", "help"), + OPT_NOARG("daemon", "short help", "help"), OPT(OT_INVALID, .ll = 0, 0, NULL, NULL, NULL) }; @@ -50,8 +51,11 @@ static int setopt(struct opt *o, const char *optarg) { char *endptr = NULL; + assert(o && o->type != OT_INVALID); if (o->used && !o->is_list) return 1; + if (!optarg || o->type == OT_NOARG) + goto noarg; switch (o->type) { case OT_L: @@ -71,37 +75,44 @@ static int setopt(struct opt *o, const char *optarg) if (endptr && *endptr != 0) return 1; +noarg: + o->used = 1; + return 0; } int options_cmdline(int argc, char **argv) { - int i, option, option_index; + int rc, i, option, option_index; struct option *o = (struct option *) calloc(OPT_MAX+1, sizeof *o); assert(o); for (i = 0; i < OPT_MAX; ++i) { o[i].name = options[i].arg_name; - o[i].has_arg = - (options[i].type == OT_NOARG ? no_argument : optional_argument); + if (options[i].def_value.ll) + o[i].has_arg = optional_argument; + else + o[i].has_arg = + (options[i].type == OT_NOARG ? no_argument : required_argument); } + rc = 0; while (1) { option_index = -1; option = getopt_long_only(argc, argv, "", o, &option_index); - if (option == -1 || option_index == -1) + + if (option_index == -1 && option != -1) { + rc = 1; + continue; + } + if (option == -1) break; if (!option) { - options[option_index].used = 1; - - if (optarg && options[option_index].type != OT_INVALID && - options[option_index].type != OT_NOARG) - { - if (setopt(&options[option_index], optarg)) { - goto error; - } - } else goto error; + if (setopt(&options[option_index], optarg)) { + rc = 1; + goto error; + } } else { fprintf(stderr, "%s: unknown option '%c' [0x%X]\n", argv[0], option, option); @@ -111,7 +122,7 @@ int options_cmdline(int argc, char **argv) error: free(o); - return 0; + return rc; } int getopt_used(opt_name on) @@ -129,6 +140,7 @@ getopt_str(opt_name on) str = options[on].value.str_dup; if (!str) str = options[on].def_value.str_dup; + assert(str); return str; } diff --git a/src/options.h b/src/options.h index bd6c139..f36e393 100644 --- a/src/options.h +++ b/src/options.h @@ -2,7 +2,7 @@ #define POTD_OPTIONS_H 1 typedef enum opt_name { - OPT_LOGFILE = 0, OPT_LOGLEVEL, + OPT_LOGFILE = 0, OPT_LOGLEVEL, OPT_DAEMON, OPT_MAX } opt_name; diff --git a/src/utils.c b/src/utils.c index b070be2..a0d2f64 100644 --- a/src/utils.c +++ b/src/utils.c @@ -189,8 +189,6 @@ int close_fds_except(int fds, ...) max_fd = sysconf(_SC_OPEN_MAX) - 1; if (max_fd <= 0) return 1; - if (fds < 0) - return 1; va_start(ap, fds); { @@ -206,7 +204,7 @@ int close_fds_except(int fds, ...) for (fd = max_fd; fd >= 0; --fd) { found = 0; - for (i = 0; i < except_count; ++i) { + for (i = 0; i < except_count && fds >= 0; ++i) { if (fd == all_fds[i]) found++; } |