changeset 1205:978887222d29

Added OpenSSH-like -o command line option to dbclient. Like in OpenSSH, whitespaces are ignored, key and value may be separated by one '=' character. For now only yes/no flag parsing is implemented.
author Konstantin Tokarev <ktokarev@smartlabs.tv>
date Mon, 30 Nov 2015 20:36:15 +0300
parents 8a5e9a97bd7a
children 2907c658fa76
files cli-runopts.c
diffstat 1 files changed, 55 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/cli-runopts.c	Mon Nov 30 21:13:03 2015 +0800
+++ b/cli-runopts.c	Mon Nov 30 20:36:15 2015 +0300
@@ -46,6 +46,7 @@
 #ifdef ENABLE_CLI_NETCAT
 static void add_netcat(const char *str);
 #endif
+static void add_extendedopt(const char *str);
 
 static void printhelp() {
 
@@ -64,6 +65,7 @@
 					"-y    Always accept remote host key if unknown\n"
 					"-y -y Don't perform any remote host key checking (caution)\n"
 					"-s    Request a subsystem (use by external sftp)\n"
+					"-o option     Set option in OpenSSH-like format ('-o help' to list options)\n"
 #ifdef ENABLE_CLI_PUBKEY_AUTH
 					"-i <identityfile>   (multiple allowed, default %s)\n"
 #endif
@@ -106,6 +108,7 @@
 	unsigned int i, j;
 	char ** next = 0;
 	enum {
+		OPT_EXTENDED_OPTIONS,
 #ifdef ENABLE_CLI_PUBKEY_AUTH
 		OPT_AUTHKEY,
 #endif
@@ -224,6 +227,9 @@
 				case 's':
 					cli_opts.is_subsystem = 1;
 					break;
+				case 'o':
+					opt = OPT_EXTENDED_OPTIONS;
+					break;
 #ifdef ENABLE_CLI_LOCALTCPFWD
 				case 'L':
 					opt = OPT_LOCALTCPFWD;
@@ -301,7 +307,6 @@
 					print_version();
 					exit(EXIT_SUCCESS);
 					break;
-				case 'o':
 				case 'b':
 					next = &dummy;
 				default:
@@ -321,6 +326,11 @@
 				dropbear_exit("Missing argument");
 		}
 
+		if (opt == OPT_EXTENDED_OPTIONS) {
+			TRACE(("opt extended"))
+			add_extendedopt(&argv[i][j]);
+		}
+		else
 #ifdef ENABLE_CLI_PUBKEY_AUTH
 		if (opt == OPT_AUTHKEY) {
 			TRACE(("opt authkey"))
@@ -806,3 +816,47 @@
 	dropbear_exit("Bad TCP port in '%s'", origstr);
 }
 #endif
+
+static int match_extendedopt(const char** strptr, const char *optname) {
+	int seen_eq = 0;
+	int optlen = strlen(optname);
+	const char *str = *strptr;
+
+	while (isspace(*str))
+		++str;
+
+	if (strncasecmp(str, optname, optlen) != 0)
+		return DROPBEAR_FAILURE;
+
+	str += optlen;
+
+	while (isspace(*str) || (!seen_eq && *str == '=')) {
+		if (*str == '=')
+			seen_eq = 1;
+		++str;
+	}
+
+	*strptr = str;
+	return DROPBEAR_SUCCESS;
+}
+
+static int parse_flag_value(const char *value)
+{
+	if (strcmp(value, "yes") == 0 || strcmp(value, "true") == 0)
+		return 1;
+	else if (strcmp(value, "no") == 0 || strcmp(value, "false") == 0)
+		return 0;
+
+	dropbear_exit("Bad yes/no argument '%s'", value);
+}
+
+static void add_extendedopt(const char* origstr) {
+	const char *optstr = origstr;
+
+	if (strcmp(origstr, "help") == 0) {
+		dropbear_log(LOG_INFO, "No options available\n");
+		exit(EXIT_SUCCESS);
+	}
+
+	dropbear_exit("Bad configuration option '%s'", origstr);
+}