comparison cli-runopts.c @ 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 c745f720ae2e
children 2907c658fa76
comparison
equal deleted inserted replaced
1195:8a5e9a97bd7a 1205:978887222d29
44 static void addforward(const char* str, m_list *fwdlist); 44 static void addforward(const char* str, m_list *fwdlist);
45 #endif 45 #endif
46 #ifdef ENABLE_CLI_NETCAT 46 #ifdef ENABLE_CLI_NETCAT
47 static void add_netcat(const char *str); 47 static void add_netcat(const char *str);
48 #endif 48 #endif
49 static void add_extendedopt(const char *str);
49 50
50 static void printhelp() { 51 static void printhelp() {
51 52
52 fprintf(stderr, "Dropbear SSH client v%s https://matt.ucc.asn.au/dropbear/dropbear.html\n" 53 fprintf(stderr, "Dropbear SSH client v%s https://matt.ucc.asn.au/dropbear/dropbear.html\n"
53 #ifdef ENABLE_CLI_MULTIHOP 54 #ifdef ENABLE_CLI_MULTIHOP
62 "-N Don't run a remote command\n" 63 "-N Don't run a remote command\n"
63 "-f Run in background after auth\n" 64 "-f Run in background after auth\n"
64 "-y Always accept remote host key if unknown\n" 65 "-y Always accept remote host key if unknown\n"
65 "-y -y Don't perform any remote host key checking (caution)\n" 66 "-y -y Don't perform any remote host key checking (caution)\n"
66 "-s Request a subsystem (use by external sftp)\n" 67 "-s Request a subsystem (use by external sftp)\n"
68 "-o option Set option in OpenSSH-like format ('-o help' to list options)\n"
67 #ifdef ENABLE_CLI_PUBKEY_AUTH 69 #ifdef ENABLE_CLI_PUBKEY_AUTH
68 "-i <identityfile> (multiple allowed, default %s)\n" 70 "-i <identityfile> (multiple allowed, default %s)\n"
69 #endif 71 #endif
70 #ifdef ENABLE_CLI_AGENTFWD 72 #ifdef ENABLE_CLI_AGENTFWD
71 "-A Enable agent auth forwarding\n" 73 "-A Enable agent auth forwarding\n"
104 106
105 void cli_getopts(int argc, char ** argv) { 107 void cli_getopts(int argc, char ** argv) {
106 unsigned int i, j; 108 unsigned int i, j;
107 char ** next = 0; 109 char ** next = 0;
108 enum { 110 enum {
111 OPT_EXTENDED_OPTIONS,
109 #ifdef ENABLE_CLI_PUBKEY_AUTH 112 #ifdef ENABLE_CLI_PUBKEY_AUTH
110 OPT_AUTHKEY, 113 OPT_AUTHKEY,
111 #endif 114 #endif
112 #ifdef ENABLE_CLI_LOCALTCPFWD 115 #ifdef ENABLE_CLI_LOCALTCPFWD
113 OPT_LOCALTCPFWD, 116 OPT_LOCALTCPFWD,
222 cli_opts.backgrounded = 1; 225 cli_opts.backgrounded = 1;
223 break; 226 break;
224 case 's': 227 case 's':
225 cli_opts.is_subsystem = 1; 228 cli_opts.is_subsystem = 1;
226 break; 229 break;
230 case 'o':
231 opt = OPT_EXTENDED_OPTIONS;
232 break;
227 #ifdef ENABLE_CLI_LOCALTCPFWD 233 #ifdef ENABLE_CLI_LOCALTCPFWD
228 case 'L': 234 case 'L':
229 opt = OPT_LOCALTCPFWD; 235 opt = OPT_LOCALTCPFWD;
230 break; 236 break;
231 case 'g': 237 case 'g':
299 #endif 305 #endif
300 case 'V': 306 case 'V':
301 print_version(); 307 print_version();
302 exit(EXIT_SUCCESS); 308 exit(EXIT_SUCCESS);
303 break; 309 break;
304 case 'o':
305 case 'b': 310 case 'b':
306 next = &dummy; 311 next = &dummy;
307 default: 312 default:
308 fprintf(stderr, 313 fprintf(stderr,
309 "WARNING: Ignoring unknown option -%c\n", c); 314 "WARNING: Ignoring unknown option -%c\n", c);
319 j = 0; 324 j = 0;
320 if (!argv[i]) 325 if (!argv[i])
321 dropbear_exit("Missing argument"); 326 dropbear_exit("Missing argument");
322 } 327 }
323 328
329 if (opt == OPT_EXTENDED_OPTIONS) {
330 TRACE(("opt extended"))
331 add_extendedopt(&argv[i][j]);
332 }
333 else
324 #ifdef ENABLE_CLI_PUBKEY_AUTH 334 #ifdef ENABLE_CLI_PUBKEY_AUTH
325 if (opt == OPT_AUTHKEY) { 335 if (opt == OPT_AUTHKEY) {
326 TRACE(("opt authkey")) 336 TRACE(("opt authkey"))
327 loadidentityfile(&argv[i][j], 1); 337 loadidentityfile(&argv[i][j], 1);
328 } 338 }
804 814
805 badport: 815 badport:
806 dropbear_exit("Bad TCP port in '%s'", origstr); 816 dropbear_exit("Bad TCP port in '%s'", origstr);
807 } 817 }
808 #endif 818 #endif
819
820 static int match_extendedopt(const char** strptr, const char *optname) {
821 int seen_eq = 0;
822 int optlen = strlen(optname);
823 const char *str = *strptr;
824
825 while (isspace(*str))
826 ++str;
827
828 if (strncasecmp(str, optname, optlen) != 0)
829 return DROPBEAR_FAILURE;
830
831 str += optlen;
832
833 while (isspace(*str) || (!seen_eq && *str == '=')) {
834 if (*str == '=')
835 seen_eq = 1;
836 ++str;
837 }
838
839 *strptr = str;
840 return DROPBEAR_SUCCESS;
841 }
842
843 static int parse_flag_value(const char *value)
844 {
845 if (strcmp(value, "yes") == 0 || strcmp(value, "true") == 0)
846 return 1;
847 else if (strcmp(value, "no") == 0 || strcmp(value, "false") == 0)
848 return 0;
849
850 dropbear_exit("Bad yes/no argument '%s'", value);
851 }
852
853 static void add_extendedopt(const char* origstr) {
854 const char *optstr = origstr;
855
856 if (strcmp(origstr, "help") == 0) {
857 dropbear_log(LOG_INFO, "No options available\n");
858 exit(EXIT_SUCCESS);
859 }
860
861 dropbear_exit("Bad configuration option '%s'", origstr);
862 }