comparison cli-runopts.c @ 486:d59f628e7baa

propagate from branch 'au.asn.ucc.matt.dropbear.dbclient-netcat-alike' (head b02ba6628cf2095017c60d202ac630f231e6b553) to branch 'au.asn.ucc.matt.dropbear' (head 636506b73e973b004cc058b07e6f36a25ff902f8)
author Matt Johnston <matt@ucc.asn.au>
date Wed, 17 Sep 2008 14:49:12 +0000
parents effb4a25b1ae 12d845ab7b5f
children 2e17f82a7330
comparison
equal deleted inserted replaced
484:effb4a25b1ae 486:d59f628e7baa
31 #include "tcpfwd.h" 31 #include "tcpfwd.h"
32 32
33 cli_runopts cli_opts; /* GLOBAL */ 33 cli_runopts cli_opts; /* GLOBAL */
34 34
35 static void printhelp(); 35 static void printhelp();
36 static void parsehostname(char* userhostarg); 36 static void parsehostname(const char* orighostarg);
37 static void fill_own_user(); 37 static void fill_own_user();
38 #ifdef ENABLE_CLI_PUBKEY_AUTH 38 #ifdef ENABLE_CLI_PUBKEY_AUTH
39 static void loadidentityfile(const char* filename); 39 static void loadidentityfile(const char* filename);
40 #endif 40 #endif
41 #ifdef ENABLE_CLI_ANYTCPFWD 41 #ifdef ENABLE_CLI_ANYTCPFWD
42 static void addforward(char* str, struct TCPFwdList** fwdlist); 42 static void addforward(const char* str, struct TCPFwdList** fwdlist);
43 #endif
44 #ifdef ENABLE_CLI_NETCAT
45 static void add_netcat(const char *str);
43 #endif 46 #endif
44 47
45 static void printhelp() { 48 static void printhelp() {
46 49
47 fprintf(stderr, "Dropbear client v%s\n" 50 fprintf(stderr, "Dropbear client v%s\n"
64 #ifdef ENABLE_CLI_REMOTETCPFWD 67 #ifdef ENABLE_CLI_REMOTETCPFWD
65 "-R <listenport:remotehost:remoteport> Remote port forwarding\n" 68 "-R <listenport:remotehost:remoteport> Remote port forwarding\n"
66 #endif 69 #endif
67 "-W <receive_window_buffer> (default %d, larger may be faster, max 1MB)\n" 70 "-W <receive_window_buffer> (default %d, larger may be faster, max 1MB)\n"
68 "-K <keepalive> (0 is never, default %d)\n" 71 "-K <keepalive> (0 is never, default %d)\n"
72 #ifdef ENABLE_CLI_NETCAT
73 "-B <endhost:endport> Netcat-alike bouncing\n"
74 #endif
69 #ifdef ENABLE_CLI_PROXYCMD 75 #ifdef ENABLE_CLI_PROXYCMD
70 "-J <proxy_program> Use program rather than tcp connection\n" 76 "-J <proxy_program> Use program rather than tcp connection\n"
71 #endif 77 #endif
72 #ifdef DEBUG_TRACE 78 #ifdef DEBUG_TRACE
73 "-v verbose\n" 79 "-v verbose\n"
88 #ifdef ENABLE_CLI_LOCALTCPFWD 94 #ifdef ENABLE_CLI_LOCALTCPFWD
89 int nextislocal = 0; 95 int nextislocal = 0;
90 #endif 96 #endif
91 #ifdef ENABLE_CLI_REMOTETCPFWD 97 #ifdef ENABLE_CLI_REMOTETCPFWD
92 int nextisremote = 0; 98 int nextisremote = 0;
99 #endif
100 #ifdef ENABLE_CLI_NETCAT
101 int nextisnetcat = 0;
93 #endif 102 #endif
94 char* dummy = NULL; /* Not used for anything real */ 103 char* dummy = NULL; /* Not used for anything real */
95 104
96 char* recv_window_arg = NULL; 105 char* recv_window_arg = NULL;
97 char* keepalive_arg = NULL; 106 char* keepalive_arg = NULL;
151 addforward(argv[i], &cli_opts.localfwds); 160 addforward(argv[i], &cli_opts.localfwds);
152 nextislocal = 0; 161 nextislocal = 0;
153 continue; 162 continue;
154 } 163 }
155 #endif 164 #endif
165 #ifdef ENABLE_CLI_NETCAT
166 if (nextisnetcat) {
167 TRACE(("nextisnetcat true"))
168 add_netcat(argv[i]);
169 nextisnetcat = 0;
170 continue;
171 }
172 #endif
156 if (next) { 173 if (next) {
157 /* The previous flag set a value to assign */ 174 /* The previous flag set a value to assign */
158 *next = argv[i]; 175 *next = argv[i];
159 if (*next == NULL) { 176 if (*next == NULL) {
160 dropbear_exit("Invalid null argument"); 177 dropbear_exit("Invalid null argument");
204 break; 221 break;
205 #endif 222 #endif
206 #ifdef ENABLE_CLI_REMOTETCPFWD 223 #ifdef ENABLE_CLI_REMOTETCPFWD
207 case 'R': 224 case 'R':
208 nextisremote = 1; 225 nextisremote = 1;
226 break;
227 #endif
228 #ifdef ENABLE_CLI_NETCAT
229 case 'B':
230 nextisnetcat = 1;
209 break; 231 break;
210 #endif 232 #endif
211 #ifdef ENABLE_CLI_PROXYCMD 233 #ifdef ENABLE_CLI_PROXYCMD
212 case 'J': 234 case 'J':
213 next = &cli_opts.proxycmd; 235 next = &cli_opts.proxycmd;
360 } 382 }
361 } 383 }
362 #endif 384 #endif
363 385
364 386
365 /* Parses a [user@]hostname argument. userhostarg is the argv[i] corresponding 387 /* Parses a [user@]hostname argument. orighostarg is the argv[i] corresponding */
366 * - note that it will be modified */ 388 static void parsehostname(const char* orighostarg) {
367 static void parsehostname(char* orighostarg) { 389
390 uid_t uid;
391 struct passwd *pw = NULL;
368 char *userhostarg = NULL; 392 char *userhostarg = NULL;
369 393
370 /* We probably don't want to be editing argvs */
371 userhostarg = m_strdup(orighostarg); 394 userhostarg = m_strdup(orighostarg);
372 395
373 cli_opts.remotehost = strchr(userhostarg, '@'); 396 cli_opts.remotehost = strchr(userhostarg, '@');
374 if (cli_opts.remotehost == NULL) { 397 if (cli_opts.remotehost == NULL) {
375 /* no username portion, the cli-auth.c code can figure the 398 /* no username portion, the cli-auth.c code can figure the
388 if (cli_opts.remotehost[0] == '\0') { 411 if (cli_opts.remotehost[0] == '\0') {
389 dropbear_exit("Bad hostname"); 412 dropbear_exit("Bad hostname");
390 } 413 }
391 } 414 }
392 415
416 #ifdef ENABLE_CLI_NETCAT
417 static void add_netcat(const char* origstr) {
418 char *portstr = NULL;
419
420 char * str = m_strdup(origstr);
421
422 portstr = strchr(str, ':');
423 if (portstr == NULL) {
424 TRACE(("No netcat port"))
425 goto fail;
426 }
427 *portstr = '\0';
428 portstr++;
429
430 if (strchr(portstr, ':')) {
431 TRACE(("Multiple netcat colons"))
432 goto fail;
433 }
434
435 cli_opts.netcat_port = strtoul(portstr, NULL, 10);
436 if (errno != 0) {
437 TRACE(("bad netcat port"))
438 goto fail;
439 }
440
441 if (cli_opts.netcat_port > 65535) {
442 TRACE(("too large netcat port"))
443 goto fail;
444 }
445
446 cli_opts.netcat_host = str;
447 return;
448
449 fail:
450 dropbear_exit("Bad netcat endpoint '%s'", origstr);
451 }
452 #endif
453
393 static void fill_own_user() { 454 static void fill_own_user() {
394 uid_t uid; 455 uid_t uid;
395 struct passwd *pw = NULL; 456 struct passwd *pw = NULL;
396 457
397 uid = getuid(); 458 uid = getuid();
405 } 466 }
406 467
407 #ifdef ENABLE_CLI_ANYTCPFWD 468 #ifdef ENABLE_CLI_ANYTCPFWD
408 /* Turn a "listenport:remoteaddr:remoteport" string into into a forwarding 469 /* Turn a "listenport:remoteaddr:remoteport" string into into a forwarding
409 * set, and add it to the forwarding list */ 470 * set, and add it to the forwarding list */
410 static void addforward(char* origstr, struct TCPFwdList** fwdlist) { 471 static void addforward(const char* origstr, struct TCPFwdList** fwdlist) {
411 472
412 char * listenport = NULL; 473 char * listenport = NULL;
413 char * connectport = NULL; 474 char * connectport = NULL;
414 char * connectaddr = NULL; 475 char * connectaddr = NULL;
415 struct TCPFwdList* newfwd = NULL; 476 struct TCPFwdList* newfwd = NULL;
441 502
442 newfwd = (struct TCPFwdList*)m_malloc(sizeof(struct TCPFwdList)); 503 newfwd = (struct TCPFwdList*)m_malloc(sizeof(struct TCPFwdList));
443 504
444 /* Now we check the ports - note that the port ints are unsigned, 505 /* Now we check the ports - note that the port ints are unsigned,
445 * the check later only checks for >= MAX_PORT */ 506 * the check later only checks for >= MAX_PORT */
446 newfwd->listenport = strtol(listenport, NULL, 10); 507 newfwd->listenport = strtoul(listenport, NULL, 10);
447 if (errno != 0) { 508 if (errno != 0) {
448 TRACE(("bad listenport strtol")) 509 TRACE(("bad listenport strtol"))
449 goto fail; 510 goto fail;
450 } 511 }
451 512
452 newfwd->connectport = strtol(connectport, NULL, 10); 513 newfwd->connectport = strtoul(connectport, NULL, 10);
453 if (errno != 0) { 514 if (errno != 0) {
454 TRACE(("bad connectport strtol")) 515 TRACE(("bad connectport strtol"))
455 goto fail; 516 goto fail;
456 } 517 }
457 518