winexe: Add support for connecting to a host on an alternate port
authorKarl Lenz <xorangekiller@gmail.com>
Fri, 5 Jul 2019 00:28:33 +0000 (20:28 -0400)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 5 Jul 2019 03:33:19 +0000 (03:33 +0000)
This commit allows an optional port number to be specified after the
hostname on the winexe command line. If no port is given, it defaults
to port 445, just like it used before. Although this is probably a
pretty uncommon use-case, it allows port-forwarding the service through
a firewall to an alternate port, which can occassionally be helpful.

$ ./bin/winexe -U karl%password1 //127.0.0.1:5445 cmd.exe
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\system32>

Signed-off-by: Karl Lenz <xorangekiller@gmail.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
examples/winexe/winexe.c

index b2257852272a28bdd56cda9f12f48d46aa08c0c3..22f748b1d456934773971b0218d15e42e44d15a3 100644 (file)
@@ -58,6 +58,7 @@ static const char version_message_fmt[] = "winexe version %d.%d\n"
 
 struct program_options {
        char *hostname;
 
 struct program_options {
        char *hostname;
+       int port;
        char *cmd;
        struct cli_credentials *credentials;
        char *runas;
        char *cmd;
        struct cli_credentials *credentials;
        char *runas;
@@ -77,6 +78,9 @@ static void parse_args(int argc, const char *argv[],
        int argc_new;
        char **argv_new;
 
        int argc_new;
        char **argv_new;
 
+       int port = 445;
+       char *port_str = NULL;
+
        int flag_interactive = SVC_IGNORE_INTERACTIVE;
        int flag_ostype = 2;
        int flag_reinstall = 0;
        int flag_interactive = SVC_IGNORE_INTERACTIVE;
        int flag_ostype = 2;
        int flag_reinstall = 0;
@@ -212,7 +216,7 @@ static void parse_args(int argc, const char *argv[],
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,
                            0);
 
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,
                            0);
 
-       poptSetOtherOptionHelp(pc, "[OPTION]... //HOST COMMAND\nOptions:");
+       poptSetOtherOptionHelp(pc, "[OPTION]... //HOST[:PORT] COMMAND\nOptions:");
 
        if (((opt = poptGetNextOpt(pc)) != -1) || flag_help || flag_version) {
                fprintf(stderr, version_message_fmt, SAMBA_VERSION_MAJOR,
 
        if (((opt = poptGetNextOpt(pc)) != -1) || flag_help || flag_version) {
                fprintf(stderr, version_message_fmt, SAMBA_VERSION_MAJOR,
@@ -244,6 +248,17 @@ static void parse_args(int argc, const char *argv[],
                exit(1);
        }
 
                exit(1);
        }
 
+       port_str = strchr(argv_new[0], ':');
+       if (port_str) {
+               if (sscanf(port_str + 1, "%d", &port) != 1 || port <= 0) {
+                       fprintf(stderr, version_message_fmt,
+                               SAMBA_VERSION_MAJOR, SAMBA_VERSION_MINOR);
+                       poptPrintHelp(pc, stdout, 0);
+                       exit(1);
+               }
+               *port_str = '\0';
+       }
+
        if (opt_debuglevel) {
                lp_set_cmdline("log level", opt_debuglevel);
        }
        if (opt_debuglevel) {
                lp_set_cmdline("log level", opt_debuglevel);
        }
@@ -304,6 +319,7 @@ static void parse_args(int argc, const char *argv[],
        options->credentials = cred;
 
        options->hostname = argv_new[0] + 2;
        options->credentials = cred;
 
        options->hostname = argv_new[0] + 2;
+       options->port = port;
        options->cmd = argv_new[1];
 
        options->flags = flag_interactive;
        options->cmd = argv_new[1];
 
        options->flags = flag_interactive;
@@ -323,6 +339,7 @@ static void parse_args(int argc, const char *argv[],
 
 static NTSTATUS winexe_svc_upload(
        const char *hostname,
 
 static NTSTATUS winexe_svc_upload(
        const char *hostname,
+       int port,
        const char *service_filename,
        const DATA_BLOB *svc32_exe,
        const DATA_BLOB *svc64_exe,
        const char *service_filename,
        const DATA_BLOB *svc32_exe,
        const DATA_BLOB *svc64_exe,
@@ -339,7 +356,7 @@ static NTSTATUS winexe_svc_upload(
                NULL,
                hostname,
                NULL,
                NULL,
                hostname,
                NULL,
-               445,
+               port,
                "ADMIN$",
                "?????",
                credentials,
                "ADMIN$",
                "?????",
                credentials,
@@ -421,6 +438,7 @@ done:
 static NTSTATUS winexe_svc_install(
        struct cli_state *cli,
        const char *hostname,
 static NTSTATUS winexe_svc_install(
        struct cli_state *cli,
        const char *hostname,
+       int port,
        const char *service_name,
        const char *service_filename,
        const DATA_BLOB *svc32_exe,
        const char *service_name,
        const char *service_filename,
        const DATA_BLOB *svc32_exe,
@@ -628,6 +646,7 @@ static NTSTATUS winexe_svc_install(
        if (need_start) {
                status = winexe_svc_upload(
                        hostname,
        if (need_start) {
                status = winexe_svc_upload(
                        hostname,
+                       port,
                        service_filename,
                        svc32_exe,
                        svc64_exe,
                        service_filename,
                        svc32_exe,
                        svc64_exe,
@@ -1894,7 +1913,7 @@ int main(int argc, const char *argv[])
                NULL,
                options.hostname,
                NULL,
                NULL,
                options.hostname,
                NULL,
-               445,
+               options.port,
                "IPC$",
                "?????",
                options.credentials,
                "IPC$",
                "?????",
                options.credentials,
@@ -1910,6 +1929,7 @@ int main(int argc, const char *argv[])
        status = winexe_svc_install(
                cli,
                options.hostname,
        status = winexe_svc_install(
                cli,
                options.hostname,
+               options.port,
                service_name,
                service_filename,
                winexesvc32_exe,
                service_name,
                service_filename,
                winexesvc32_exe,