[PATCH] Enable bundling in svr-runopts's svr_getopts.

Guilhem Moulin guilhem at fripost.org
Fri Oct 30 04:41:31 AWST 2015


On Wed, 28 Oct 2015 at 21:47:24 +0800, Matt Johnston wrote:
> I've changed the code to just print a warning for the time being. I'm
> intending for the next release to be soon with small bugfixes. Using getopt
> would probably be good though would require checking availability for the
> platforms where Dropbear is used.

In fact the current code can easily be tweaked to enable bundling.
(I've only touched svr-runopts for now; will proceed with cli-runopts if
you're fine with that patch.)  Refactoring the code to use getopt is
actually cumpersome due to the #ifdef changing the option string.

> By backwards compatibility I just meant the issue where the behaviour would
> change slightly.

---
 svr-runopts.c | 59 +++++++++++++++++++++++++++++++----------------------------
 1 file changed, 31 insertions(+), 28 deletions(-)

diff --git a/svr-runopts.c b/svr-runopts.c
index 26c199b..ec29883 100644
--- a/svr-runopts.c
+++ b/svr-runopts.c
@@ -112,13 +112,14 @@ static void printhelp(const char * progname) {
 
 void svr_getopts(int argc, char ** argv) {
 
-	unsigned int i;
+	unsigned int i, j;
 	char ** next = 0;
 	int nextisport = 0;
 	char* recv_window_arg = NULL;
 	char* keepalive_arg = NULL;
 	char* idle_timeout_arg = NULL;
 	char* keyfile = NULL;
+	char c;
 
 
 	/* see printhelp() for options */
@@ -168,33 +169,10 @@ void svr_getopts(int argc, char ** argv) {
 #endif
 
 	for (i = 1; i < (unsigned int)argc; i++) {
-		if (nextisport) {
-			addportandaddress(argv[i]);
-			nextisport = 0;
-			continue;
-		}
-	  
-		if (next) {
-			*next = argv[i];
-			if (*next == NULL) {
-				dropbear_exit("Invalid null argument");
-			}
-			next = 0x00;
+		if (argv[i][0] != '-' || argv[i][1] == '\0')
+			dropbear_exit("Invalid argument: %s", argv[i]);
 
-			if (keyfile) {
-				addhostkey(keyfile);
-				keyfile = NULL;
-			}
-			continue;
-		}
-
-		if (argv[i][0] == '-') {
-			char c = argv[i][1];
-			if (strlen(argv[i]) != 2) {
-				/* We only handle one flag per hyphen */
-				fprintf(stderr, "Warning, trailing '%s' of '%s' is ignored.\n",
-					&argv[i][2], argv[i]);
-			}
+		for (j = 1; (c = argv[i][j]) != '\0' && !next && !nextisport; j++) {
 			switch (c) {
 				case 'b':
 					next = &svr_opts.bannerfile;
@@ -284,12 +262,37 @@ void svr_getopts(int argc, char ** argv) {
 					exit(EXIT_SUCCESS);
 					break;
 				default:
-					fprintf(stderr, "Unknown argument %s\n", argv[i]);
+					fprintf(stderr, "Invalid option -%c\n", c);
 					printhelp(argv[0]);
 					exit(EXIT_FAILURE);
 					break;
 			}
 		}
+
+		if (!next && !nextisport)
+			continue;
+
+		if (c == '\0') {
+			i++;
+			j = 0;
+		}
+
+		if (nextisport) {
+			addportandaddress(&argv[i][j]);
+			nextisport = 0;
+		}
+		else if (next) {
+			*next = &argv[i][j];
+			if (*next == NULL) {
+				dropbear_exit("Invalid null argument");
+			}
+			next = 0x00;
+
+			if (keyfile) {
+				addhostkey(keyfile);
+				keyfile = NULL;
+			}
+		}
 	}
 
 	/* Set up listening ports */
-- 
2.6.2



More information about the Dropbear mailing list