The suricatta part of commit commit 14bed359464b ("cli: fail if
non-option arguments passed to sub-arguments") was not correct because
suricatta splits its parsing between start_suricatta() and each server's
server_start() functions, so the common code cannot error on leftover
non-option arguments (which actually are options' parameters)
Instead, add the check to server_general that knows what arguments to
expect (server_hawkbit already had the check, server_lua would need to
check in the user's lua code)
Note that unlike the downloader/mongoose added in previous commit, this
does not fail on unknown arguments (e.g. --foobar would be silently
ignored)
In practice, this already only works because 'S:' is declared in getopt
parameters, so we could added e/d as well and also check for invalid
arguments without too much effort if desired.
(If we did not have 'S:' in getopt arguments, we'd be left with -S'
parameter as positional argument, which I assume is why it was added in
the first place)
While here, harmonize the style of server_hawkbit's check:
- check optind < argc like the rest of the code for easier grepping
(the case compares optind >= argc with optind first so it also makes
sense from this point of view)
- use ERROR instead of fprintf like server_general
suricatta/server_general.c | 5 +++++
suricatta/server_hawkbit.c | 14 ++++++--------
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/suricatta/server_general.c b/suricatta/server_general.c
index f106bce13437..f78ae6b609eb 100644
--- a/suricatta/server_general.c
+++ b/suricatta/server_general.c
@@ -685,6 +685,11 @@ static server_op_res_t server_start(const char *fname, int argc, char *argv[])
suricatta_print_help();
return SERVER_EINIT;
}
+ if (optind < argc) {
+ ERROR("Unused argument(s) given to suricatta (-u), see --help.");
+ suricatta_print_help();
+ return SERVER_EINIT;
+ }
channel_data_defaults.headers_to_send = &server_general.httpheaders_to_send;
diff --git a/suricatta/server_hawkbit.c b/suricatta/server_hawkbit.c
index 342670cd1ca4..519638c17eda 100644
--- a/suricatta/server_hawkbit.c
+++ b/suricatta/server_hawkbit.c
@@ -1826,9 +1826,7 @@ static server_op_res_t server_start(const char *fname, int argc, char *argv[])
case STATE_WAIT:
break;
default:
- fprintf(
- stderr,
- "Error: Invalid update status given.\n");
+ ERROR("Error: Invalid update status given.\n");
suricatta_print_help();
exit(EXIT_FAILURE);
}
@@ -1929,12 +1927,12 @@ static server_op_res_t server_start(const char *fname, int argc, char *argv[])
}
if (mandatory_argument_count != ALL_MANDATORY_SET) {
- fprintf(stderr, "Mandatory arguments missing!\n");
+ ERROR("Mandatory arguments missing!\n");
suricatta_print_help();
return SERVER_EINIT;
}
- if (argc > optind) {
- fprintf(stderr, "Unused arguments.\n");
+ if (optind < argc) {
+ ERROR("Unused argument(s) given to suricatta (-u), see --help.");
suricatta_print_help();
return SERVER_EINIT;
}
@@ -1951,8 +1949,8 @@ static server_op_res_t server_start(const char *fname, int argc, char *argv[])
* accept target and gateway token at the same time
*/
if (server_hawkbit.targettoken != NULL && server_hawkbit.gatewaytoken != NULL) {
- fprintf(stderr, "Error: both target and gateway tokens have been provided, "
- "but just one at a time is supported.\n");
+ ERROR("both target and gateway tokens have been provided, "
+ "but just one at a time is supported.\n");
exit(EXIT_FAILURE);
}
server_hawkbit_settoken("TargetToken", server_hawkbit.targettoken);
--
2.47.2