s4:torture: add a rpc.samba3.smb2-reauth2 test
[kai/samba.git] / source4 / torture / rpc / samba3rpc.c
index e1950bd0d1a2becfbc6c9b8c98288828b1f0453c..69cc00cb9aa7779d1dd9e61af6fdeb11062a853a 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/*f
    Unix SMB/CIFS implementation.
 
    dcerpc torture tests, designed to walk Samba3 code paths
 #include "param/param.h"
 #include "lib/registry/registry.h"
 #include "libcli/resolve/resolve.h"
+#include "torture/ndr/ndr.h"
+#include "libcli/smb2/smb2.h"
+#include "libcli/smb2/smb2_calls.h"
+#include "librpc/rpc/dcerpc.h"
+#include "librpc/rpc/dcerpc_proto.h"
 
 /*
  * This tests a RPC call using an invalid vuid
@@ -90,7 +95,7 @@ bool torture_bind_authcontext(struct torture_context *torture)
                goto done;
        }
 
-       lsa_pipe = dcerpc_pipe_init(mem_ctx, cli->transport->socket->event.ctx);
+       lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
        if (lsa_pipe == NULL) {
                torture_comment(torture, "dcerpc_pipe_init failed\n");
                goto done;
@@ -220,8 +225,7 @@ static bool bindtest(struct torture_context *tctx,
                return false;
        }
 
-       lsa_pipe = dcerpc_pipe_init(mem_ctx,
-                                   cli->transport->socket->event.ctx); 
+       lsa_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
        if (lsa_pipe == NULL) {
                torture_comment(tctx, "dcerpc_pipe_init failed\n");
                goto done;
@@ -391,8 +395,7 @@ static bool get_usr_handle(struct torture_context *tctx,
        struct samr_CreateUser2 c;
        uint32_t user_rid,access_granted;
 
-       samr_pipe = dcerpc_pipe_init(mem_ctx,
-                                    cli->transport->socket->event.ctx);
+       samr_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
        torture_assert(tctx, samr_pipe, "dcerpc_pipe_init failed");
 
        samr_handle = samr_pipe->binding_handle;
@@ -946,8 +949,7 @@ static bool auth2(struct torture_context *tctx,
                return false;
        }
 
-       net_pipe = dcerpc_pipe_init(mem_ctx,
-                                   cli->transport->socket->event.ctx);
+       net_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
        if (net_pipe == NULL) {
                torture_comment(tctx, "dcerpc_pipe_init failed\n");
                goto done;
@@ -1062,8 +1064,7 @@ static bool schan(struct torture_context *tctx,
                return false;
        }
 
-       net_pipe = dcerpc_pipe_init(mem_ctx,
-                                   cli->transport->socket->event.ctx);
+       net_pipe = dcerpc_pipe_init(mem_ctx, tctx->ev);
        if (net_pipe == NULL) {
                torture_comment(tctx, "dcerpc_pipe_init failed\n");
                goto done;
@@ -1295,9 +1296,7 @@ static bool leave(struct torture_context *tctx,
 
 static bool torture_netlogon_samba3(struct torture_context *torture)
 {
-       TALLOC_CTX *mem_ctx;
        NTSTATUS status;
-       bool ret = false;
        struct smbcli_state *cli;
        struct cli_credentials *anon_creds;
        struct cli_credentials *wks_creds;
@@ -1311,22 +1310,14 @@ static bool torture_netlogon_samba3(struct torture_context *torture)
                wks_name = get_myname(torture);
        }
 
-       mem_ctx = talloc_init("torture_netlogon_samba3");
-
-       if (mem_ctx == NULL) {
-               torture_comment(torture, "talloc_init failed\n");
-               return false;
-       }
-
-       if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
-               torture_comment(torture, "create_anon_creds failed\n");
-               goto done;
+       if (!(anon_creds = cli_credentials_init_anon(torture))) {
+               torture_fail(torture, "create_anon_creds failed\n");
        }
 
        lpcfg_smbcli_options(torture->lp_ctx, &options);
        lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
 
-       status = smbcli_full_connection(mem_ctx, &cli,
+       status = smbcli_full_connection(torture, &cli,
                                        torture_setting_string(torture, "host", NULL),
                                        lpcfg_smb_ports(torture->lp_ctx),
                                        "IPC$", NULL,
@@ -1335,16 +1326,11 @@ static bool torture_netlogon_samba3(struct torture_context *torture)
                                        lpcfg_resolve_context(torture->lp_ctx),
                                        torture->ev, &options, &session_options,
                                        lpcfg_gensec_settings(torture, torture->lp_ctx));
-       if (!NT_STATUS_IS_OK(status)) {
-               torture_comment(torture, "smbcli_full_connection failed: %s\n",
-                        nt_errstr(status));
-               goto done;
-       }
+       torture_assert_ntstatus_ok(torture, status, "smbcli_full_connection failed\n");
 
-       wks_creds = cli_credentials_init(mem_ctx);
+       wks_creds = cli_credentials_init(torture);
        if (wks_creds == NULL) {
-               torture_comment(torture, "cli_credentials_init failed\n");
-               goto done;
+               torture_fail(torture, "cli_credentials_init failed\n");
        }
 
        cli_credentials_set_conf(wks_creds, torture->lp_ctx);
@@ -1355,10 +1341,9 @@ static bool torture_netlogon_samba3(struct torture_context *torture)
                                     generate_random_password(wks_creds, 8, 255),
                                     CRED_SPECIFIED);
 
-       if (!join3(torture, cli, false, cmdline_credentials, wks_creds)) {
-               torture_comment(torture, "join failed\n");
-               goto done;
-       }
+       torture_assert(torture,
+               join3(torture, cli, false, cmdline_credentials, wks_creds),
+               "join failed");
 
        cli_credentials_set_domain(
                cmdline_credentials, cli_credentials_get_domain(wks_creds),
@@ -1372,29 +1357,22 @@ static bool torture_netlogon_samba3(struct torture_context *torture)
 
                int j;
 
-               if (!auth2(torture, cli, wks_creds)) {
-                       torture_comment(torture, "auth2 failed\n");
-                       goto done;
-               }
+               torture_assert(torture,
+                       auth2(torture, cli, wks_creds),
+                       "auth2 failed");
 
                for (j=0; j<2; j++) {
-                       if (!schan(torture, cli, wks_creds, cmdline_credentials)) {
-                               torture_comment(torture, "schan failed\n");
-                               goto done;
-                       }
+                       torture_assert(torture,
+                               schan(torture, cli, wks_creds, cmdline_credentials),
+                               "schan failed");
                }
        }
 
-       if (!leave(torture, cli, cmdline_credentials, wks_creds)) {
-               torture_comment(torture, "leave failed\n");
-               goto done;
-       }
-
-       ret = true;
+       torture_assert(torture,
+               leave(torture, cli, cmdline_credentials, wks_creds),
+               "leave failed");
 
- done:
-       talloc_free(mem_ctx);
-       return ret;
+       return true;
 }
 
 /*
@@ -1530,8 +1508,7 @@ static NTSTATUS pipe_bind_smb(struct torture_context *tctx,
        struct dcerpc_pipe *result;
        NTSTATUS status;
 
-       if (!(result = dcerpc_pipe_init(
-                     mem_ctx, tree->session->transport->socket->event.ctx))) {
+       if (!(result = dcerpc_pipe_init(mem_ctx, tctx->ev))) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1777,7 +1754,6 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
 {
        NTSTATUS status;
        struct smbcli_state *cli;
-       TALLOC_CTX *mem_ctx;
        bool ret = true;
        struct dom_sid *user_sid;
        struct dom_sid *created_sid;
@@ -1787,71 +1763,45 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
        struct smbcli_options options;
        struct smbcli_session_options session_options;
 
-       if (!(mem_ctx = talloc_new(torture))) {
-               return false;
-       }
-
        lpcfg_smbcli_options(torture->lp_ctx, &options);
        lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
 
        status = smbcli_full_connection(
-               mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
+               torture, &cli, torture_setting_string(torture, "host", NULL),
                lpcfg_smb_ports(torture->lp_ctx),
                "IPC$", NULL, lpcfg_socket_options(torture->lp_ctx), cmdline_credentials,
                lpcfg_resolve_context(torture->lp_ctx), torture->ev, &options,
                &session_options, lpcfg_gensec_settings(torture, torture->lp_ctx));
-       if (!NT_STATUS_IS_OK(status)) {
-               torture_warning(torture, "smbcli_full_connection failed: %s\n",
-                        nt_errstr(status));
-               ret = false;
-               goto done;
-       }
+       torture_assert_ntstatus_ok(torture, status, "smbcli_full_connection failed\n");
 
-       if (!(user_sid = whoami(torture, mem_ctx, cli->tree))) {
-               torture_warning(torture, "whoami on auth'ed connection failed\n");
-               ret = false;
+       if (!(user_sid = whoami(torture, torture, cli->tree))) {
+               torture_fail(torture, "whoami on auth'ed connection failed\n");
        }
 
        talloc_free(cli);
 
-       if (!(anon_creds = cli_credentials_init_anon(mem_ctx))) {
-               torture_warning(torture, "create_anon_creds failed\n");
-               ret = false;
-               goto done;
+       if (!(anon_creds = cli_credentials_init_anon(torture))) {
+               torture_fail(torture, "create_anon_creds failed\n");
        }
 
        status = smbcli_full_connection(
-               mem_ctx, &cli, torture_setting_string(torture, "host", NULL),
+               torture, &cli, torture_setting_string(torture, "host", NULL),
                lpcfg_smb_ports(torture->lp_ctx), "IPC$", NULL,
                lpcfg_socket_options(torture->lp_ctx), anon_creds,
                lpcfg_resolve_context(torture->lp_ctx),
                torture->ev, &options, &session_options,
                lpcfg_gensec_settings(torture, torture->lp_ctx));
-       if (!NT_STATUS_IS_OK(status)) {
-               torture_warning(torture, "anon smbcli_full_connection failed: %s\n",
-                        nt_errstr(status));
-               ret = false;
-               goto done;
-       }
+       torture_assert_ntstatus_ok(torture, status, "anon smbcli_full_connection failed\n");
 
-       if (!(user_sid = whoami(torture, mem_ctx, cli->tree))) {
-               torture_warning(torture, "whoami on anon connection failed\n");
-               ret = false;
-               goto done;
+       if (!(user_sid = whoami(torture, torture, cli->tree))) {
+               torture_fail(torture, "whoami on anon connection failed\n");
        }
 
-       if (!dom_sid_equal(user_sid,
-                          dom_sid_parse_talloc(mem_ctx, "s-1-5-7"))) {
-               torture_warning(torture, "Anon lsa_GetUserName returned %s, expected "
-                        "S-1-5-7",
-                        dom_sid_string(mem_ctx, user_sid));
-               ret = false;
-       }
+       torture_assert_sid_equal(torture, user_sid, dom_sid_parse_talloc(torture, "s-1-5-7"),
+               "Anon lsa_GetUserName returned unexpected SID");
 
-       if (!(user_creds = cli_credentials_init(mem_ctx))) {
-               torture_warning(torture, "cli_credentials_init failed\n");
-               ret = false;
-               goto done;
+       if (!(user_creds = cli_credentials_init(torture))) {
+               torture_fail(torture, "cli_credentials_init failed\n");
        }
 
        cli_credentials_set_conf(user_creds, torture->lp_ctx);
@@ -1861,13 +1811,11 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
                                     generate_random_password(user_creds, 8, 255),
                                     CRED_SPECIFIED);
 
-       if (!create_user(torture, mem_ctx, cli, cmdline_credentials,
+       if (!create_user(torture, torture, cli, cmdline_credentials,
                         cli_credentials_get_username(user_creds),
                         cli_credentials_get_password(user_creds),
                         &domain_name, &created_sid)) {
-               torture_warning(torture, "create_user failed\n");
-               ret = false;
-               goto done;
+               torture_fail(torture, "create_user failed\n");
        }
 
        cli_credentials_set_domain(user_creds, domain_name,
@@ -1878,10 +1826,9 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
                struct smb_composite_sesssetup setup;
                struct smbcli_tree *tree;
 
-               session2 = smbcli_session_init(cli->transport, mem_ctx, false, session_options);
+               session2 = smbcli_session_init(cli->transport, torture, false, session_options);
                if (session2 == NULL) {
-                       torture_warning(torture, "smbcli_session_init failed\n");
-                       goto done;
+                       torture_fail(torture, "smbcli_session_init failed\n");
                }
 
                setup.in.sesskey = cli->transport->negotiate.sesskey;
@@ -1891,23 +1838,17 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
                setup.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
 
                status = smb_composite_sesssetup(session2, &setup);
-               if (!NT_STATUS_IS_OK(status)) {
-                       torture_warning(torture, "session setup with new user failed: "
-                                "%s\n", nt_errstr(status));
-                       ret = false;
-                       goto done;
-               }
+               torture_assert_ntstatus_ok(torture, status, "session setup with new user failed");
+
                session2->vuid = setup.out.vuid;
 
-               if (!NT_STATUS_IS_OK(secondary_tcon(torture, mem_ctx, session2,
+               if (!NT_STATUS_IS_OK(secondary_tcon(torture, torture, session2,
                                                    "IPC$", &tree))) {
-                       torture_warning(torture, "secondary_tcon failed\n");
-                       ret = false;
-                       goto done;
+                       torture_fail(torture, "secondary_tcon failed\n");
                }
 
-               if (!(user_sid = whoami(torture, mem_ctx, tree))) {
-                       torture_warning(torture, "whoami on user connection failed\n");
+               if (!(user_sid = whoami(torture, torture, tree))) {
+                       torture_fail_goto(torture, del, "whoami on user connection failed\n");
                        ret = false;
                        goto del;
                }
@@ -1916,8 +1857,8 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
        }
 
        torture_comment(torture, "Created %s, found %s\n",
-                dom_sid_string(mem_ctx, created_sid),
-                dom_sid_string(mem_ctx, user_sid));
+                dom_sid_string(torture, created_sid),
+                dom_sid_string(torture, user_sid));
 
        if (!dom_sid_equal(created_sid, user_sid)) {
                ret = false;
@@ -1927,12 +1868,9 @@ static bool torture_samba3_rpc_getusername(struct torture_context *torture)
        if (!delete_user(torture, cli,
                         cmdline_credentials,
                         cli_credentials_get_username(user_creds))) {
-               torture_warning(torture, "delete_user failed\n");
-               ret = false;
+               torture_fail(torture, "delete_user failed\n");
        }
 
- done:
-       talloc_free(mem_ctx);
        return ret;
 }
 
@@ -2139,8 +2077,7 @@ static bool torture_samba3_rpc_randomauth2(struct torture_context *torture)
                goto done;
        }
 
-       if (!(net_pipe = dcerpc_pipe_init(
-                     mem_ctx, cli->transport->socket->event.ctx))) {
+       if (!(net_pipe = dcerpc_pipe_init(mem_ctx, torture->ev))) {
                torture_comment(torture, "dcerpc_pipe_init failed\n");
                goto done;
        }
@@ -2445,48 +2382,43 @@ bool try_tcon(struct torture_context *tctx,
 
 static bool torture_samba3_rpc_sharesec(struct torture_context *torture)
 {
-       TALLOC_CTX *mem_ctx;
-       bool ret = true;
        struct smbcli_state *cli;
        struct security_descriptor *sd;
        struct dom_sid *user_sid;
 
-       if (!(mem_ctx = talloc_new(torture))) {
-               return false;
-       }
-
        if (!(torture_open_connection_share(
-                     mem_ctx, &cli, torture, torture_setting_string(torture, "host", NULL),
+                     torture, &cli, torture, torture_setting_string(torture, "host", NULL),
                      "IPC$", torture->ev))) {
-               torture_comment(torture, "IPC$ connection failed\n");
-               talloc_free(mem_ctx);
-               return false;
+               torture_fail(torture, "IPC$ connection failed\n");
        }
 
-       if (!(user_sid = whoami(torture, mem_ctx, cli->tree))) {
-               torture_comment(torture, "whoami failed\n");
-               talloc_free(mem_ctx);
-               return false;
+       if (!(user_sid = whoami(torture, torture, cli->tree))) {
+               torture_fail(torture, "whoami failed\n");
        }
 
-       sd = get_sharesec(torture, mem_ctx, cli->session,
+       sd = get_sharesec(torture, torture, cli->session,
                          torture_setting_string(torture, "share", NULL));
 
-       ret &= try_tcon(torture, mem_ctx, sd, cli->session,
+       torture_assert(torture, try_tcon(
+                       torture, torture, sd, cli->session,
                        torture_setting_string(torture, "share", NULL),
-                       user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK);
+                       user_sid, 0, NT_STATUS_ACCESS_DENIED, NT_STATUS_OK),
+                       "failed to test tcon with 0 access_mask");
 
-       ret &= try_tcon(torture, mem_ctx, sd, cli->session,
+       torture_assert(torture, try_tcon(
+                       torture, torture, sd, cli->session,
                        torture_setting_string(torture, "share", NULL),
                        user_sid, SEC_FILE_READ_DATA, NT_STATUS_OK,
-                       NT_STATUS_MEDIA_WRITE_PROTECTED);
+                       NT_STATUS_MEDIA_WRITE_PROTECTED),
+                       "failed to test tcon with SEC_FILE_READ_DATA access_mask");
 
-       ret &= try_tcon(torture, mem_ctx, sd, cli->session,
+       torture_assert(torture, try_tcon(
+                       torture, torture, sd, cli->session,
                        torture_setting_string(torture, "share", NULL),
-                       user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK);
+                       user_sid, SEC_FILE_ALL, NT_STATUS_OK, NT_STATUS_OK),
+                       "failed to test tcon with SEC_FILE_ALL access_mask")
 
-       talloc_free(mem_ctx);
-       return ret;
+       return true;
 }
 
 static bool torture_samba3_rpc_lsa(struct torture_context *torture)
@@ -2546,6 +2478,7 @@ static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
        struct rap_WserverGetInfo r;
        NTSTATUS status;
        char servername[17];
+       size_t converted_size;
 
        r.in.level = 0;
        r.in.bufsize = 0xffff;
@@ -2558,7 +2491,7 @@ static NTSTATUS get_servername(TALLOC_CTX *mem_ctx, struct smbcli_tree *tree,
        memcpy(servername, r.out.info.info0.name, 16);
        servername[16] = '\0';
 
-       if (!pull_ascii_talloc(mem_ctx, name, servername, NULL)) {
+       if (!pull_ascii_talloc(mem_ctx, name, servername, &converted_size)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -3343,6 +3276,604 @@ bool torture_samba3_getaliasmembership_0(struct torture_context *torture)
        return true;
 }
 
+/**
+ * Test smb reauthentication while rpc pipe is in use.
+ */
+static bool torture_rpc_smb_reauth1(struct torture_context *torture)
+{
+       TALLOC_CTX *mem_ctx;
+       NTSTATUS status;
+       bool ret = false;
+       struct smbcli_state *cli;
+       struct smbcli_options options;
+       struct smbcli_session_options session_options;
+
+       struct dcerpc_pipe *lsa_pipe;
+       struct dcerpc_binding_handle *lsa_handle;
+       struct lsa_GetUserName r;
+       struct lsa_String *authority_name_p = NULL;
+       char *authority_name_saved = NULL;
+       struct lsa_String *account_name_p = NULL;
+       char *account_name_saved = NULL;
+       struct cli_credentials *anon_creds = NULL;
+       struct smb_composite_sesssetup io;
+
+       mem_ctx = talloc_init("torture_samba3_reauth");
+       torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
+
+       lpcfg_smbcli_options(torture->lp_ctx, &options);
+       lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
+
+       status = smbcli_full_connection(mem_ctx, &cli,
+                                       torture_setting_string(torture, "host", NULL),
+                                       lpcfg_smb_ports(torture->lp_ctx),
+                                       "IPC$", NULL,
+                                       lpcfg_socket_options(torture->lp_ctx),
+                                       cmdline_credentials,
+                                       lpcfg_resolve_context(torture->lp_ctx),
+                                       torture->ev, &options, &session_options,
+                                       lpcfg_gensec_settings(torture, torture->lp_ctx));
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "smbcli_full_connection failed");
+
+       lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
+       torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
+                           "dcerpc_pipe_init failed");
+       lsa_handle = lsa_pipe->binding_handle;
+
+       status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_pipe_open failed");
+
+       status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_bind_auth_none failed");
+
+       /* lsa getusername */
+
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
+                       authority_name_p->string,
+                       account_name_p->string);
+
+       account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
+       torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
+                           "talloc failed");
+       authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
+       torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
+                           "talloc failed");
+
+       /* smb re-authenticate as anonymous */
+
+       anon_creds = cli_credentials_init_anon(mem_ctx);
+
+       ZERO_STRUCT(io);
+       io.in.sesskey         = cli->transport->negotiate.sesskey;
+       io.in.capabilities    = cli->transport->negotiate.capabilities;
+       io.in.credentials     = anon_creds;
+       io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
+       io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
+
+       status = smb_composite_sesssetup(cli->session, &io);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* re-do lsa getusername after reauth */
+
+       TALLOC_FREE(authority_name_p);
+       TALLOC_FREE(account_name_p);
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
+                           ret, done, "authority_name not equal after reauth to anon");
+       torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
+                           ret, done, "account_name not equal after reauth to anon");
+
+       /* smb re-auth again to the original user */
+
+       ZERO_STRUCT(io);
+       io.in.sesskey         = cli->transport->negotiate.sesskey;
+       io.in.capabilities    = cli->transport->negotiate.capabilities;
+       io.in.credentials     = cmdline_credentials;
+       io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
+       io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
+
+       status = smb_composite_sesssetup(cli->session, &io);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* re-do lsa getusername */
+
+       TALLOC_FREE(authority_name_p);
+       TALLOC_FREE(account_name_p);
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
+                           ret, done, "authority_name not equal after reauth to anon");
+       torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
+                           ret, done, "account_name not equal after reauth to anon");
+
+       ret = true;
+
+done:
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+/**
+ * Test smb reauthentication while rpc pipe is in use.
+ * Open a second lsa bind after reauth to anon.
+ * Do lsa getusername on that second bind.
+ */
+static bool torture_rpc_smb_reauth2(struct torture_context *torture)
+{
+       TALLOC_CTX *mem_ctx;
+       NTSTATUS status;
+       bool ret = false;
+       struct smbcli_state *cli;
+       struct smbcli_options options;
+       struct smbcli_session_options session_options;
+
+       struct dcerpc_pipe *lsa_pipe;
+       struct dcerpc_binding_handle *lsa_handle;
+       struct lsa_GetUserName r;
+       struct lsa_String *authority_name_p = NULL;
+       char *authority_name_saved = NULL;
+       struct lsa_String *account_name_p = NULL;
+       char *account_name_saved = NULL;
+       struct cli_credentials *anon_creds = NULL;
+       struct smb_composite_sesssetup io;
+
+       mem_ctx = talloc_init("torture_samba3_reauth");
+       torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
+
+       lpcfg_smbcli_options(torture->lp_ctx, &options);
+       lpcfg_smbcli_session_options(torture->lp_ctx, &session_options);
+
+       status = smbcli_full_connection(mem_ctx, &cli,
+                                       torture_setting_string(torture, "host", NULL),
+                                       lpcfg_smb_ports(torture->lp_ctx),
+                                       "IPC$", NULL,
+                                       lpcfg_socket_options(torture->lp_ctx),
+                                       cmdline_credentials,
+                                       lpcfg_resolve_context(torture->lp_ctx),
+                                       torture->ev, &options, &session_options,
+                                       lpcfg_gensec_settings(torture, torture->lp_ctx));
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "smbcli_full_connection failed");
+
+       /* smb re-authenticate as anonymous */
+
+       anon_creds = cli_credentials_init_anon(mem_ctx);
+
+       ZERO_STRUCT(io);
+       io.in.sesskey         = cli->transport->negotiate.sesskey;
+       io.in.capabilities    = cli->transport->negotiate.capabilities;
+       io.in.credentials     = anon_creds;
+       io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
+       io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
+
+       status = smb_composite_sesssetup(cli->session, &io);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* open the lsa pipe */
+
+       lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
+       torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
+                           "dcerpc_pipe_init failed");
+       lsa_handle = lsa_pipe->binding_handle;
+
+       status = dcerpc_pipe_open_smb(lsa_pipe, cli->tree, "\\lsarpc");
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_pipe_open failed");
+
+       status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_bind_auth_none failed");
+
+       /* lsa getusername */
+
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
+                       authority_name_p->string,
+                       account_name_p->string);
+
+       account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
+       torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
+                           "talloc failed");
+       authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
+       torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
+                           "talloc failed");
+
+       /* smb re-auth again to the original user */
+
+       ZERO_STRUCT(io);
+       io.in.sesskey         = cli->transport->negotiate.sesskey;
+       io.in.capabilities    = cli->transport->negotiate.capabilities;
+       io.in.credentials     = cmdline_credentials;
+       io.in.workgroup       = lpcfg_workgroup(torture->lp_ctx);
+       io.in.gensec_settings = lpcfg_gensec_settings(torture, torture->lp_ctx);
+
+       status = smb_composite_sesssetup(cli->session, &io);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* re-do lsa getusername after reauth */
+
+       TALLOC_FREE(authority_name_p);
+       TALLOC_FREE(account_name_p);
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
+                           ret, done, "authority_name not equal after reauth to anon");
+       torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
+                           ret, done, "account_name not equal after reauth to anon");
+
+       ret = true;
+
+done:
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+/**
+ * Test smb2 reauthentication while rpc pipe is in use.
+ */
+static bool torture_rpc_smb2_reauth1(struct torture_context *torture)
+{
+       TALLOC_CTX *mem_ctx;
+       NTSTATUS status;
+       bool ret = false;
+       struct smbcli_options options;
+
+       struct dcerpc_pipe *lsa_pipe;
+       struct dcerpc_binding_handle *lsa_handle;
+       struct lsa_GetUserName r;
+       struct lsa_String *authority_name_p = NULL;
+       char *authority_name_saved = NULL;
+       struct lsa_String *account_name_p = NULL;
+       char *account_name_saved = NULL;
+       struct cli_credentials *anon_creds = NULL;
+       const char *host = torture_setting_string(torture, "host", NULL);
+       struct smb2_tree *tree;
+
+       mem_ctx = talloc_init("torture_samba3_reauth");
+       torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
+
+       lpcfg_smbcli_options(torture->lp_ctx, &options);
+
+       status = smb2_connect(mem_ctx,
+                             host,
+                             lpcfg_smb_ports(torture->lp_ctx),
+                             "IPC$",
+                             lpcfg_resolve_context(torture->lp_ctx),
+                             cmdline_credentials,
+                             &tree,
+                             torture->ev,
+                             &options,
+                             lpcfg_socket_options(torture->lp_ctx),
+                             lpcfg_gensec_settings(torture, torture->lp_ctx)
+                             );
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "smb2_connect failed");
+
+       lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
+       torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
+                           "dcerpc_pipe_init failed");
+       lsa_handle = lsa_pipe->binding_handle;
+
+       status = dcerpc_pipe_open_smb2(lsa_pipe, tree, "lsarpc");
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_pipe_open_smb2 failed");
+
+       status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_bind_auth_none failed");
+
+       /* lsa getusername */
+
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
+                       authority_name_p->string,
+                       account_name_p->string);
+
+       account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
+       torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
+                           "talloc failed");
+       authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
+       torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
+                           "talloc failed");
+
+       /* smb re-authenticate as anonymous */
+
+       anon_creds = cli_credentials_init_anon(mem_ctx);
+
+       status = smb2_session_setup_spnego(tree->session,
+                                          anon_creds,
+                                          0 /* previous_session_id */);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* re-do lsa getusername after reauth */
+
+       TALLOC_FREE(authority_name_p);
+       TALLOC_FREE(account_name_p);
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
+                           ret, done, "authority_name not equal after reauth to anon");
+       torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
+                           ret, done, "account_name not equal after reauth to anon");
+
+       /* smb re-auth again to the original user */
+
+       status = smb2_session_setup_spnego(tree->session,
+                                          cmdline_credentials,
+                                          0 /* previous_session_id */);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* re-do lsa getusername */
+
+       TALLOC_FREE(authority_name_p);
+       TALLOC_FREE(account_name_p);
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
+                           ret, done, "authority_name not equal after reauth to anon");
+       torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
+                           ret, done, "account_name not equal after reauth to anon");
+
+       ret = true;
+
+done:
+       talloc_free(mem_ctx);
+       return ret;
+}
+
+/**
+ * Test smb2reauthentication while rpc pipe is in use.
+ * Open a second lsa bind after reauth to anon.
+ * Do lsa getusername on that second bind.
+ */
+static bool torture_rpc_smb2_reauth2(struct torture_context *torture)
+{
+       TALLOC_CTX *mem_ctx;
+       NTSTATUS status;
+       bool ret = false;
+       struct smbcli_options options;
+
+       struct dcerpc_pipe *lsa_pipe;
+       struct dcerpc_binding_handle *lsa_handle;
+       struct lsa_GetUserName r;
+       struct lsa_String *authority_name_p = NULL;
+       char *authority_name_saved = NULL;
+       struct lsa_String *account_name_p = NULL;
+       char *account_name_saved = NULL;
+       struct cli_credentials *anon_creds = NULL;
+       const char *host = torture_setting_string(torture, "host", NULL);
+       struct smb2_tree *tree;
+
+       mem_ctx = talloc_init("torture_samba3_reauth");
+       torture_assert(torture, (mem_ctx != NULL), "talloc_init failed");
+
+       lpcfg_smbcli_options(torture->lp_ctx, &options);
+
+       status = smb2_connect(mem_ctx,
+                             host,
+                             lpcfg_smb_ports(torture->lp_ctx),
+                             "IPC$",
+                             lpcfg_resolve_context(torture->lp_ctx),
+                             cmdline_credentials,
+                             &tree,
+                             torture->ev,
+                             &options,
+                             lpcfg_socket_options(torture->lp_ctx),
+                             lpcfg_gensec_settings(torture, torture->lp_ctx)
+                             );
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "smb2_connect failed");
+
+       /* smb re-authenticate as anonymous */
+
+       anon_creds = cli_credentials_init_anon(mem_ctx);
+
+       status = smb2_session_setup_spnego(tree->session,
+                                          anon_creds,
+                                          0 /* previous_session_id */);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* open the lsa pipe */
+
+       lsa_pipe = dcerpc_pipe_init(mem_ctx, torture->ev);
+       torture_assert_goto(torture, (lsa_pipe != NULL), ret, done,
+                           "dcerpc_pipe_init failed");
+       lsa_handle = lsa_pipe->binding_handle;
+
+       status = dcerpc_pipe_open_smb2(lsa_pipe, tree, "lsarpc");
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_pipe_open_smb2 failed");
+
+       status = dcerpc_bind_auth_none(lsa_pipe, &ndr_table_lsarpc);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "dcerpc_bind_auth_none failed");
+
+       /* lsa getusername */
+
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_comment(torture, "lsa_GetUserName gave '%s\\%s'\n",
+                       authority_name_p->string,
+                       account_name_p->string);
+
+       account_name_saved = talloc_strdup(mem_ctx, account_name_p->string);
+       torture_assert_goto(torture, (account_name_saved != NULL), ret, done,
+                           "talloc failed");
+       authority_name_saved = talloc_strdup(mem_ctx, authority_name_p->string);
+       torture_assert_goto(torture, (authority_name_saved != NULL), ret, done,
+                           "talloc failed");
+
+       /* smb re-auth again to the original user */
+
+       status = smb2_session_setup_spnego(tree->session,
+                                          cmdline_credentials,
+                                          0 /* previous_session_id */);
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "session reauth to anon failed");
+
+       /* re-do lsa getusername */
+
+       TALLOC_FREE(authority_name_p);
+       TALLOC_FREE(account_name_p);
+       ZERO_STRUCT(r);
+       r.in.system_name = "\\";
+       r.in.account_name = &account_name_p;
+       r.in.authority_name = &authority_name_p;
+       r.out.account_name = &account_name_p;
+
+       status = dcerpc_lsa_GetUserName_r(lsa_handle, mem_ctx, &r);
+
+       authority_name_p = *r.out.authority_name;
+
+       torture_assert_ntstatus_ok_goto(torture, status, ret, done,
+                                       "GetUserName failed");
+       torture_assert_ntstatus_ok_goto(torture, r.out.result, ret, done,
+                                       "GetUserName failed");
+
+       torture_assert_goto(torture, (strcmp(authority_name_p->string, authority_name_saved) == 0),
+                           ret, done, "authority_name not equal after reauth to anon");
+       torture_assert_goto(torture, (strcmp(account_name_p->string, account_name_saved) == 0),
+                           ret, done, "account_name not equal after reauth to anon");
+
+       ret = true;
+
+done:
+       talloc_free(mem_ctx);
+       return ret;
+}
+
 struct torture_suite *torture_rpc_samba3(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite = torture_suite_create(mem_ctx, "samba3");
@@ -3360,6 +3891,10 @@ struct torture_suite *torture_rpc_samba3(TALLOC_CTX *mem_ctx)
        torture_suite_add_simple_test(suite, "winreg", torture_samba3_rpc_winreg);
        torture_suite_add_simple_test(suite, "getaliasmembership-0", torture_samba3_getaliasmembership_0);
        torture_suite_add_simple_test(suite, "regconfig", torture_samba3_regconfig);
+       torture_suite_add_simple_test(suite, "smb-reauth1", torture_rpc_smb_reauth1);
+       torture_suite_add_simple_test(suite, "smb-reauth2", torture_rpc_smb_reauth2);
+       torture_suite_add_simple_test(suite, "smb2-reauth1", torture_rpc_smb2_reauth1);
+       torture_suite_add_simple_test(suite, "smb2-reauth2", torture_rpc_smb2_reauth2);
 
        suite->description = talloc_strdup(suite, "samba3 DCERPC interface tests");