comparison cli-runopts.c @ 485:12d845ab7b5f dbclient-netcat-alike

Rework netcat-alike to be a proper mode, with -B argument.
author Matt Johnston <matt@ucc.asn.au>
date Wed, 17 Sep 2008 14:36:49 +0000
parents f4addc06745b
children d59f628e7baa
comparison
equal deleted inserted replaced
478:d4f32c3443ac 485:12d845ab7b5f
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 #ifdef ENABLE_CLI_PUBKEY_AUTH 37 #ifdef ENABLE_CLI_PUBKEY_AUTH
38 static void loadidentityfile(const char* filename); 38 static void loadidentityfile(const char* filename);
39 #endif 39 #endif
40 #ifdef ENABLE_CLI_ANYTCPFWD 40 #ifdef ENABLE_CLI_ANYTCPFWD
41 static void addforward(char* str, struct TCPFwdList** fwdlist); 41 static void addforward(const char* str, struct TCPFwdList** fwdlist);
42 #endif
43 #ifdef ENABLE_CLI_NETCAT
44 static void add_netcat(const char *str);
42 #endif 45 #endif
43 46
44 static void printhelp() { 47 static void printhelp() {
45 48
46 fprintf(stderr, "Dropbear client v%s\n" 49 fprintf(stderr, "Dropbear client v%s\n"
63 #ifdef ENABLE_CLI_REMOTETCPFWD 66 #ifdef ENABLE_CLI_REMOTETCPFWD
64 "-R <listenport:remotehost:remoteport> Remote port forwarding\n" 67 "-R <listenport:remotehost:remoteport> Remote port forwarding\n"
65 #endif 68 #endif
66 "-W <receive_window_buffer> (default %d, larger may be faster, max 1MB)\n" 69 "-W <receive_window_buffer> (default %d, larger may be faster, max 1MB)\n"
67 "-K <keepalive> (0 is never, default %d)\n" 70 "-K <keepalive> (0 is never, default %d)\n"
71 #ifdef ENABLE_CLI_NETCAT
72 "-B <endhost:endport> Netcat-alike bouncing\n"
73 #endif
68 #ifdef DEBUG_TRACE 74 #ifdef DEBUG_TRACE
69 "-v verbose\n" 75 "-v verbose\n"
70 #endif 76 #endif
71 ,DROPBEAR_VERSION, cli_opts.progname, 77 ,DROPBEAR_VERSION, cli_opts.progname,
72 DEFAULT_RECV_WINDOW, DEFAULT_KEEPALIVE); 78 DEFAULT_RECV_WINDOW, DEFAULT_KEEPALIVE);
84 #ifdef ENABLE_CLI_LOCALTCPFWD 90 #ifdef ENABLE_CLI_LOCALTCPFWD
85 int nextislocal = 0; 91 int nextislocal = 0;
86 #endif 92 #endif
87 #ifdef ENABLE_CLI_REMOTETCPFWD 93 #ifdef ENABLE_CLI_REMOTETCPFWD
88 int nextisremote = 0; 94 int nextisremote = 0;
95 #endif
96 #ifdef ENABLE_CLI_NETCAT
97 int nextisnetcat = 0;
89 #endif 98 #endif
90 char* dummy = NULL; /* Not used for anything real */ 99 char* dummy = NULL; /* Not used for anything real */
91 100
92 char* recv_window_arg = NULL; 101 char* recv_window_arg = NULL;
93 char* keepalive_arg = NULL; 102 char* keepalive_arg = NULL;
142 addforward(argv[i], &cli_opts.localfwds); 151 addforward(argv[i], &cli_opts.localfwds);
143 nextislocal = 0; 152 nextislocal = 0;
144 continue; 153 continue;
145 } 154 }
146 #endif 155 #endif
156 #ifdef ENABLE_CLI_NETCAT
157 if (nextisnetcat) {
158 TRACE(("nextisnetcat true"))
159 add_netcat(argv[i]);
160 nextisnetcat = 0;
161 continue;
162 }
163 #endif
147 if (next) { 164 if (next) {
148 /* The previous flag set a value to assign */ 165 /* The previous flag set a value to assign */
149 *next = argv[i]; 166 *next = argv[i];
150 if (*next == NULL) { 167 if (*next == NULL) {
151 dropbear_exit("Invalid null argument"); 168 dropbear_exit("Invalid null argument");
195 break; 212 break;
196 #endif 213 #endif
197 #ifdef ENABLE_CLI_REMOTETCPFWD 214 #ifdef ENABLE_CLI_REMOTETCPFWD
198 case 'R': 215 case 'R':
199 nextisremote = 1; 216 nextisremote = 1;
217 break;
218 #endif
219 #ifdef ENABLE_CLI_NETCAT
220 case 'B':
221 nextisnetcat = 1;
200 break; 222 break;
201 #endif 223 #endif
202 case 'l': 224 case 'l':
203 next = &cli_opts.username; 225 next = &cli_opts.username;
204 break; 226 break;
349 } 371 }
350 } 372 }
351 #endif 373 #endif
352 374
353 375
354 /* Parses a [user@]hostname argument. userhostarg is the argv[i] corresponding 376 /* Parses a [user@]hostname argument. orighostarg is the argv[i] corresponding */
355 * - note that it will be modified */ 377 static void parsehostname(const char* orighostarg) {
356 static void parsehostname(char* orighostarg) {
357 378
358 uid_t uid; 379 uid_t uid;
359 struct passwd *pw = NULL; 380 struct passwd *pw = NULL;
360 char *userhostarg = NULL; 381 char *userhostarg = NULL;
361 382
362 /* We probably don't want to be editing argvs */
363 userhostarg = m_strdup(orighostarg); 383 userhostarg = m_strdup(orighostarg);
364 384
365 cli_opts.remotehost = strchr(userhostarg, '@'); 385 cli_opts.remotehost = strchr(userhostarg, '@');
366 if (cli_opts.remotehost == NULL) { 386 if (cli_opts.remotehost == NULL) {
367 /* no username portion, the cli-auth.c code can figure the 387 /* no username portion, the cli-auth.c code can figure the
387 if (cli_opts.remotehost[0] == '\0') { 407 if (cli_opts.remotehost[0] == '\0') {
388 dropbear_exit("Bad hostname"); 408 dropbear_exit("Bad hostname");
389 } 409 }
390 } 410 }
391 411
412 #ifdef ENABLE_CLI_NETCAT
413 static void add_netcat(const char* origstr) {
414 char *portstr = NULL;
415
416 char * str = m_strdup(origstr);
417
418 portstr = strchr(str, ':');
419 if (portstr == NULL) {
420 TRACE(("No netcat port"))
421 goto fail;
422 }
423 *portstr = '\0';
424 portstr++;
425
426 if (strchr(portstr, ':')) {
427 TRACE(("Multiple netcat colons"))
428 goto fail;
429 }
430
431 cli_opts.netcat_port = strtoul(portstr, NULL, 10);
432 if (errno != 0) {
433 TRACE(("bad netcat port"))
434 goto fail;
435 }
436
437 if (cli_opts.netcat_port > 65535) {
438 TRACE(("too large netcat port"))
439 goto fail;
440 }
441
442 cli_opts.netcat_host = str;
443 return;
444
445 fail:
446 dropbear_exit("Bad netcat endpoint '%s'", origstr);
447 }
448 #endif
449
392 #ifdef ENABLE_CLI_ANYTCPFWD 450 #ifdef ENABLE_CLI_ANYTCPFWD
393 /* Turn a "listenport:remoteaddr:remoteport" string into into a forwarding 451 /* Turn a "listenport:remoteaddr:remoteport" string into into a forwarding
394 * set, and add it to the forwarding list */ 452 * set, and add it to the forwarding list */
395 static void addforward(char* origstr, struct TCPFwdList** fwdlist) { 453 static void addforward(const char* origstr, struct TCPFwdList** fwdlist) {
396 454
397 char * listenport = NULL; 455 char * listenport = NULL;
398 char * connectport = NULL; 456 char * connectport = NULL;
399 char * connectaddr = NULL; 457 char * connectaddr = NULL;
400 struct TCPFwdList* newfwd = NULL; 458 struct TCPFwdList* newfwd = NULL;
426 484
427 newfwd = (struct TCPFwdList*)m_malloc(sizeof(struct TCPFwdList)); 485 newfwd = (struct TCPFwdList*)m_malloc(sizeof(struct TCPFwdList));
428 486
429 /* Now we check the ports - note that the port ints are unsigned, 487 /* Now we check the ports - note that the port ints are unsigned,
430 * the check later only checks for >= MAX_PORT */ 488 * the check later only checks for >= MAX_PORT */
431 newfwd->listenport = strtol(listenport, NULL, 10); 489 newfwd->listenport = strtoul(listenport, NULL, 10);
432 if (errno != 0) { 490 if (errno != 0) {
433 TRACE(("bad listenport strtol")) 491 TRACE(("bad listenport strtol"))
434 goto fail; 492 goto fail;
435 } 493 }
436 494
437 newfwd->connectport = strtol(connectport, NULL, 10); 495 newfwd->connectport = strtoul(connectport, NULL, 10);
438 if (errno != 0) { 496 if (errno != 0) {
439 TRACE(("bad connectport strtol")) 497 TRACE(("bad connectport strtol"))
440 goto fail; 498 goto fail;
441 } 499 }
442 500