comparison cli-runopts.c @ 489:79c657a673ec

- Allow specifying a port with host/port. - Rearrange multihop hostname parsing
author Matt Johnston <matt@ucc.asn.au>
date Thu, 18 Sep 2008 16:01:59 +0000
parents 2e17f82a7330
children b956d6151600
comparison
equal deleted inserted replaced
488:a24139ba08c8 489:79c657a673ec
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(const char* orighostarg, const char* argv0); 36 static void parse_hostname(const char* orighostarg);
37 static void parse_multihop_hostname(const char* orighostarg, const char* argv0);
37 static void fill_own_user(); 38 static void fill_own_user();
38 #ifdef ENABLE_CLI_PUBKEY_AUTH 39 #ifdef ENABLE_CLI_PUBKEY_AUTH
39 static void loadidentityfile(const char* filename); 40 static void loadidentityfile(const char* filename);
40 #endif 41 #endif
41 #ifdef ENABLE_CLI_ANYTCPFWD 42 #ifdef ENABLE_CLI_ANYTCPFWD
46 #endif 47 #endif
47 48
48 static void printhelp() { 49 static void printhelp() {
49 50
50 fprintf(stderr, "Dropbear client v%s\n" 51 fprintf(stderr, "Dropbear client v%s\n"
51 "Usage: %s [options] [user@]host [command]\n" 52 "Usage: %s [options] [user@]host[/port] [command]\n"
52 "Options are:\n" 53 "Options are:\n"
53 "-p <remoteport>\n" 54 "-p <remoteport>\n"
54 "-l <username>\n" 55 "-l <username>\n"
55 "-t Allocate a pty\n" 56 "-t Allocate a pty\n"
56 "-T Don't allocate a pty\n" 57 "-T Don't allocate a pty\n"
288 TRACE(("non-flag arg: '%s'", argv[i])) 289 TRACE(("non-flag arg: '%s'", argv[i]))
289 290
290 /* Either the hostname or commands */ 291 /* Either the hostname or commands */
291 292
292 if (cli_opts.remotehost == NULL) { 293 if (cli_opts.remotehost == NULL) {
293 294 #ifdef ENABLE_CLI_MULTIHOP
294 parsehostname(argv[i], argv[0]); 295 parse_multihop_hostname(argv[i], argv[0]);
295 296 #else
297 parse_hostname(argv[i]);
298 #endif
296 } else { 299 } else {
297 300
298 /* this is part of the commands to send - after this we 301 /* this is part of the commands to send - after this we
299 * don't parse any more options, and flags are sent as the 302 * don't parse any more options, and flags are sent as the
300 * command */ 303 * command */
391 } 394 }
392 #endif 395 #endif
393 396
394 #ifdef ENABLE_CLI_MULTIHOP 397 #ifdef ENABLE_CLI_MULTIHOP
395 398
396 /* Sets up 'onion-forwarding' connections. 399 /* Sets up 'onion-forwarding' connections. This will spawn
400 * a separate dbclient process for each hop.
397 * As an example, if the cmdline is 401 * As an example, if the cmdline is
398 * dbclient wrt,madako,canyons 402 * dbclient wrt,madako,canyons
399 * then we want to run: 403 * then we want to run:
400 * dbclient -J "dbclient -B canyons:22 wrt,madako" canyons 404 * dbclient -J "dbclient -B canyons:22 wrt,madako" canyons
401 * and then the inner dbclient will recursively run: 405 * and then the inner dbclient will recursively run:
402 * dbclient -J "dbclient -B madako:22 wrt" madako 406 * dbclient -J "dbclient -B madako:22 wrt" madako
403 * etc for as many hosts as we want. 407 * etc for as many hosts as we want.
408 *
409 * Ports for hosts can be specified as host/port.
404 */ 410 */
405 static void parsehostname(const char* orighostarg, const char* argv0) { 411 static void parse_multihop_hostname(const char* orighostarg, const char* argv0) {
406 char *userhostarg = NULL; 412 char *userhostarg = NULL;
407 char *last_hop = NULL;; 413 char *last_hop = NULL;;
408 char *remainder = NULL; 414 char *remainder = NULL;
409 415
410 userhostarg = m_strdup(orighostarg); 416 userhostarg = m_strdup(orighostarg);
417 *last_hop = '\0'; 423 *last_hop = '\0';
418 last_hop++; 424 last_hop++;
419 remainder = userhostarg; 425 remainder = userhostarg;
420 userhostarg = last_hop; 426 userhostarg = last_hop;
421 } 427 }
428
429 parse_hostname(userhostarg);
430
431 if (last_hop) {
432 /* Set up the proxycmd */
433 unsigned int cmd_len = 0;
434 if (cli_opts.proxycmd) {
435 dropbear_exit("-J can't be used with multihop mode");
436 }
437 if (cli_opts.remoteport == NULL) {
438 cli_opts.remoteport = "22";
439 }
440 cmd_len = strlen(remainder)
441 + strlen(cli_opts.remotehost) + strlen(cli_opts.remoteport)
442 + strlen(argv0) + 30;
443 cli_opts.proxycmd = m_malloc(cmd_len);
444 snprintf(cli_opts.proxycmd, cmd_len, "%s -B %s:%s %s",
445 argv0, cli_opts.remotehost, cli_opts.remoteport, remainder);
446 }
447 }
448 #endif /* !ENABLE_CLI_MULTIHOP */
449
450 /* Parses a [user@]hostname[/port] argument. */
451 static void parse_hostname(const char* orighostarg) {
452 char *userhostarg = NULL;
453 char *port = NULL;
454
455 userhostarg = m_strdup(orighostarg);
422 456
423 cli_opts.remotehost = strchr(userhostarg, '@'); 457 cli_opts.remotehost = strchr(userhostarg, '@');
424 if (cli_opts.remotehost == NULL) { 458 if (cli_opts.remotehost == NULL) {
425 /* no username portion, the cli-auth.c code can figure the 459 /* no username portion, the cli-auth.c code can figure the
426 * local user's name */ 460 * local user's name */
433 467
434 if (cli_opts.username == NULL) { 468 if (cli_opts.username == NULL) {
435 cli_opts.username = m_strdup(cli_opts.own_user); 469 cli_opts.username = m_strdup(cli_opts.own_user);
436 } 470 }
437 471
472 port = strchr(cli_opts.remotehost, '/');
473 if (port) {
474 *port = '\0';
475 cli_opts.remoteport = port+1;
476 }
477
438 if (cli_opts.remotehost[0] == '\0') { 478 if (cli_opts.remotehost[0] == '\0') {
439 dropbear_exit("Bad hostname"); 479 dropbear_exit("Bad hostname");
440 } 480 }
441 481 }
442 if (last_hop) {
443 /* Set up the proxycmd */
444 unsigned int cmd_len = 0;
445 if (cli_opts.proxycmd) {
446 dropbear_exit("-J can't be used with multihop mode");
447 }
448 if (cli_opts.remoteport == NULL) {
449 cli_opts.remoteport = "22";
450 }
451 cmd_len = strlen(remainder)
452 + strlen(cli_opts.remotehost) + strlen(cli_opts.remoteport)
453 + strlen(argv0) + 30;
454 cli_opts.proxycmd = m_malloc(cmd_len);
455 snprintf(cli_opts.proxycmd, cmd_len, "%s -B %s:%s %s",
456 argv0, cli_opts.remotehost, cli_opts.remoteport, remainder);
457 dropbear_log(LOG_INFO, "proxycmd: '%s'", cli_opts.proxycmd);
458 }
459 }
460
461 #else /* !ENABLE_CLI_MULTIHOP */
462
463 /* Parses a [user@]hostname argument. orighostarg is the argv[i] corresponding */
464 static void parsehostname(const char* orighostarg, const char* argv0) {
465 char *userhostarg = NULL;
466
467 userhostarg = m_strdup(orighostarg);
468
469 cli_opts.remotehost = strchr(userhostarg, '@');
470 if (cli_opts.remotehost == NULL) {
471 /* no username portion, the cli-auth.c code can figure the
472 * local user's name */
473 cli_opts.remotehost = userhostarg;
474 } else {
475 cli_opts.remotehost[0] = '\0'; /* Split the user/host */
476 cli_opts.remotehost++;
477 cli_opts.username = userhostarg;
478 }
479
480 if (cli_opts.username == NULL) {
481 cli_opts.username = m_strdup(cli_opts.own_user);
482 }
483
484 if (cli_opts.remotehost[0] == '\0') {
485 dropbear_exit("Bad hostname");
486 }
487 }
488
489 #endif /* !ENABLE_CLI_MULTIHOP */
490 482
491 #ifdef ENABLE_CLI_NETCAT 483 #ifdef ENABLE_CLI_NETCAT
492 static void add_netcat(const char* origstr) { 484 static void add_netcat(const char* origstr) {
493 char *portstr = NULL; 485 char *portstr = NULL;
494 486