]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
netapi: Pass net's cmdline credentials to libnetapi_net_init()
authorSamuel Cabrero <scabrero@samba.org>
Thu, 7 Sep 2023 12:53:22 +0000 (14:53 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 11 Sep 2023 02:42:41 +0000 (02:42 +0000)
Avoid unnecessary credentials allocation and initialization by passing the
net's cmdline creds to libnetapi_net_init() directly.

Fixes the problem of running cli_credentials_guess() (which runs password
callbacks) twice, one for the net's cmdline creds and a second time for the
creds initialized in libnetapi_net_init(), just to override them immediately
after.

Example:

$ export PASSWD_FD=0
$ ./bin/net offlinejoin composeodj <...>
foo
bar

Password is read from STDIN twice.

Signed-off-by: Samuel Cabrero <scabrero@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source3/lib/netapi/netapi.c
source3/lib/netapi/netapi_net.h
source3/utils/net_dom.c
source3/utils/net_offlinejoin.c
source3/utils/net_rpc.c
source3/utils/net_rpc_shell.c
source4/torture/libnetapi/libnetapi.c

index 28a32180eb8bf4e9201df7de91f33fd3d8220075..e377b533548217ed1bc9ea72a10f4fc71c83dbe7 100644 (file)
@@ -97,7 +97,7 @@ NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
                return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
        }
 
-       ret = libnetapi_net_init(context, lp_ctx);
+       ret = libnetapi_net_init(context, lp_ctx, NULL);
        TALLOC_FREE(frame);
        return ret;
 }
@@ -111,7 +111,8 @@ the command line).
 ****************************************************************/
 
 NET_API_STATUS libnetapi_net_init(struct libnetapi_ctx **context,
-                                 struct loadparm_context *lp_ctx)
+                                 struct loadparm_context *lp_ctx,
+                                 struct cli_credentials *creds)
 {
        NET_API_STATUS status;
        struct libnetapi_ctx *ctx = NULL;
@@ -123,17 +124,19 @@ NET_API_STATUS libnetapi_net_init(struct libnetapi_ctx **context,
                return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
        }
 
-       ctx->creds = cli_credentials_init(ctx);
+       ctx->creds = creds;
        if (ctx->creds == NULL) {
-               TALLOC_FREE(frame);
-               return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
+               ctx->creds = cli_credentials_init(ctx);
+               if (ctx->creds == NULL) {
+                       TALLOC_FREE(frame);
+                       return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
+               }
+               /* Ignore return code, as we might not have a smb.conf */
+               (void)cli_credentials_guess(ctx->creds, lp_ctx);
        }
 
        BlockSignals(True, SIGPIPE);
 
-       /* Ignore return code, as we might not have a smb.conf */
-       (void)cli_credentials_guess(ctx->creds, lp_ctx);
-
        status = libnetapi_init_private_context(ctx);
        if (status != 0) {
                TALLOC_FREE(frame);
index 86dd3abe22998be28f1cc36f4e157afcade6644b..8febc2a8a4c5232073bdca2544e9031b0f259ed8 100644 (file)
@@ -22,4 +22,5 @@
 /* This function is to init the libnetapi subsystem, without
  * re-reading config files or setting debug levels etc */
 NET_API_STATUS libnetapi_net_init(struct libnetapi_ctx **ctx,
-                                 struct loadparm_context *lp_ctx);
+                                 struct loadparm_context *lp_ctx,
+                                 struct cli_credentials *creds);
index 9f052abac4f4eb959eb82cced246dac86db59f23..4342990eaa1aeaefa5c25a865d3ba4557a08afcf 100644 (file)
@@ -376,12 +376,7 @@ int net_dom(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
-       if (status != 0) {
-               return -1;
-       }
-
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
index 72493feb9bef68663f66e3041642a3139080b966..5294fd98ad760b42ebd91ce9344b0295225f384d 100644 (file)
@@ -54,12 +54,7 @@ int net_offlinejoin(struct net_context *c, int argc, const char **argv)
 
        net_warn_member_options();
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
-       if (status != 0) {
-               return -1;
-       }
-
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
index 383afd2eced81506dceaaea54aec5bb0256ff3a8..a55de7d5df8ebfcaf9665c73a3e8c4b3e44a2f82 100644 (file)
@@ -1312,12 +1312,7 @@ int net_rpc_user(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
-       if (status != 0) {
-               return -1;
-       }
-
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
@@ -3505,12 +3500,7 @@ int net_rpc_group(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
-       if (status != 0) {
-               return -1;
-       }
-
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
@@ -5474,17 +5464,11 @@ int net_rpc_share(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
 
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
-       if (status != 0) {
-               return -1;
-       }
-
-
        if (argc == 0) {
                if (c->display_usage) {
                        d_printf("%s\n%s",
@@ -5757,12 +5741,7 @@ int net_rpc_file(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
-       if (status != 0) {
-               return -1;
-       }
-
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
@@ -8369,12 +8348,7 @@ int net_rpc(struct net_context *c, int argc, const char **argv)
                {NULL, NULL, 0, NULL, NULL}
        };
 
-       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx);
-       if (status != 0) {
-               return -1;
-       }
-
-       status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (status != 0) {
                return -1;
        }
index 1c2abd0990c703fd91200e0b7a73c440c64f5636..1ea7080e35dfe11db47d29d59a18ee4c8ce51cf5 100644 (file)
@@ -229,11 +229,7 @@ int net_rpc_shell(struct net_context *c, int argc, const char **argv)
                return -1;
        }
 
-       if (libnetapi_net_init(&c->netapi_ctx, c->lp_ctx) != 0) {
-               return -1;
-       }
-
-       net_api_status = libnetapi_set_creds(c->netapi_ctx, c->creds);
+       net_api_status = libnetapi_net_init(&c->netapi_ctx, c->lp_ctx, c->creds);
        if (net_api_status != 0) {
                return -1;
        }
index 605488d1873b1631183db4024a84602c3f50a030..fa6dcef26373fa79869791380aaafa539491ba76 100644 (file)
@@ -32,6 +32,7 @@ bool torture_libnetapi_init_context(struct torture_context *tctx,
        NET_API_STATUS status;
        struct libnetapi_ctx *ctx;
        TALLOC_CTX *frame = talloc_stackframe();
+       struct cli_credentials *creds = samba_cmdline_get_creds();
 
        if (!lp_load_global(lpcfg_configfile(tctx->lp_ctx))) {
                fprintf(stderr, "error loading %s\n", lpcfg_configfile(tctx->lp_ctx));
@@ -41,17 +42,12 @@ bool torture_libnetapi_init_context(struct torture_context *tctx,
 
        load_interfaces();
 
-       status = libnetapi_net_init(&ctx, tctx->lp_ctx);
+       status = libnetapi_net_init(&ctx, tctx->lp_ctx, creds);
        if (status != 0) {
                talloc_free(frame);
                return false;
        }
 
-       libnetapi_set_username(ctx,
-               cli_credentials_get_username(samba_cmdline_get_creds()));
-       libnetapi_set_password(ctx,
-               cli_credentials_get_password(samba_cmdline_get_creds()));
-
        *ctx_p = ctx;
 
        talloc_free(frame);