s4:torture/raw/samba3misc - Add "discard_const_p" macro before a string
[ira/wip.git] / source4 / torture / raw / samba3misc.c
index 15a7f6c4a32cbbe6a3bc11af774809600b0b0a91..2e85a34761c87dccf9daa1545383d8b459dda64b 100644 (file)
@@ -56,7 +56,7 @@ bool torture_samba3_checkfsp(struct torture_context *torture)
 
        if (!torture_open_connection_share(
                    torture, &cli, torture, torture_setting_string(torture, "host", NULL),
-                   torture_setting_string(torture, "share", NULL), NULL)) {
+                   torture_setting_string(torture, "share", NULL), torture->ev)) {
                d_printf("torture_open_connection_share failed\n");
                ret = false;
                goto done;
@@ -90,7 +90,7 @@ bool torture_samba3_checkfsp(struct torture_context *torture)
                union smb_open io;
                io.generic.level = RAW_OPEN_NTCREATEX;
                io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
-               io.ntcreatex.in.root_fid = 0;
+               io.ntcreatex.in.root_fid.fnum = 0;
                io.ntcreatex.in.security_flags = 0;
                io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
                io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
@@ -99,6 +99,7 @@ bool torture_samba3_checkfsp(struct torture_context *torture)
                io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
                io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
                io.ntcreatex.in.create_options = 0;
+               io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
                io.ntcreatex.in.fname = dirname;
                status = smb_raw_open(cli->tree, mem_ctx, &io);
                if (!NT_STATUS_IS_OK(status)) {
@@ -303,7 +304,7 @@ static NTSTATUS raw_smbcli_ntcreate(struct smbcli_tree *tree, const char *fname,
        memset(&io, '\0', sizeof(io));
         io.generic.level = RAW_OPEN_NTCREATEX;
        io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
        io.ntcreatex.in.alloc_size = 0;
        io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
@@ -669,9 +670,41 @@ bool torture_samba3_caseinsensitive(struct torture_context *torture)
        return ret;
 }
 
+static void close_locked_file(struct tevent_context *ev,
+                             struct tevent_timer *te,
+                             struct timeval now,
+                             void *private_data)
+{
+       int *pfd = (int *)private_data;
+
+       TALLOC_FREE(te);
+
+       if (*pfd != -1) {
+               close(*pfd);
+               *pfd = -1;
+       }
+}
+
+struct lock_result_state {
+       NTSTATUS status;
+       bool done;
+};
+
+static void receive_lock_result(struct smbcli_request *req)
+{
+       struct lock_result_state *state =
+               (struct lock_result_state *)req->async.private_data;
+
+       state->status = smbcli_request_simple_recv(req);
+       state->done = true;
+}
+
 /*
  * Check that Samba3 correctly deals with conflicting posix byte range locks
  * on an underlying file
+ *
+ * Note: This test depends on "posix locking = yes".
+ * Note: To run this test, use "--option=torture:localdir=<LOCALDIR>"
  */
 
 bool torture_samba3_posixtimedlock(struct torture_context *tctx)
@@ -692,6 +725,9 @@ bool torture_samba3_posixtimedlock(struct torture_context *tctx)
        union smb_lock io;
        struct smb_lock_entry lock_entry;
        struct smbcli_request *req;
+       struct lock_result_state lock_result;
+
+       struct tevent_timer *te;
 
        if (!torture_open_connection(&cli, tctx, 0)) {
                ret = false;
@@ -796,17 +832,30 @@ bool torture_samba3_posixtimedlock(struct torture_context *tctx)
                goto done;
        }
 
-       /*
-        * Ship the async timed request to the server
-        */
-       event_loop_once(req->transport->socket->event.ctx);
-       msleep(500);
+       lock_result.done = false;
+       req->async.fn = receive_lock_result;
+       req->async.private_data = &lock_result;
 
-       close(fd);
+       te = tevent_add_timer(req->transport->socket->event.ctx,
+                             tctx, timeval_current_ofs(1, 0),
+                             close_locked_file, &fd);
+       if (te == NULL) {
+               torture_warning(tctx, "tevent_add_timer failed\n");
+               ret = false;
+               goto done;
+       }
 
-       status = smbcli_request_simple_recv(req);
+       while ((fd != -1) || (!lock_result.done)) {
+               if (tevent_loop_once(req->transport->socket->event.ctx)
+                   == -1) {
+                       torture_warning(tctx, "tevent_loop_once failed: %s\n",
+                                       strerror(errno));
+                       ret = false;
+                       goto done;
+               }
+       }
 
-       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_STATUS(lock_result.status, NT_STATUS_OK);
 
  done:
        if (fnum != -1) {
@@ -838,7 +887,7 @@ bool torture_samba3_rootdirfid(struct torture_context *tctx)
        ZERO_STRUCT(io);
        io.generic.level = RAW_OPEN_NTCREATEX;
        io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.security_flags = 0;
        io.ntcreatex.in.access_mask =
                SEC_STD_SYNCHRONIZE | SEC_FILE_EXECUTE;
@@ -849,6 +898,7 @@ bool torture_samba3_rootdirfid(struct torture_context *tctx)
                | NTCREATEX_SHARE_ACCESS_READ;
        io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
        io.ntcreatex.in.create_options = 0;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
        io.ntcreatex.in.fname = "\\";
        status = smb_raw_open(cli->tree, tctx, &io);
        if (!NT_STATUS_IS_OK(status)) {
@@ -862,7 +912,7 @@ bool torture_samba3_rootdirfid(struct torture_context *tctx)
        io.ntcreatex.in.flags =
                NTCREATEX_FLAGS_REQUEST_OPLOCK
                | NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK;
-       io.ntcreatex.in.root_fid = dnum;
+       io.ntcreatex.in.root_fid.fnum = dnum;
        io.ntcreatex.in.security_flags = 0;
        io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OVERWRITE_IF;
        io.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
@@ -870,6 +920,7 @@ bool torture_samba3_rootdirfid(struct torture_context *tctx)
        io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
        io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
        io.ntcreatex.in.create_options = 0;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
        io.ntcreatex.in.fname = fname;
 
        status = smb_raw_open(cli->tree, tctx, &io);
@@ -889,3 +940,84 @@ bool torture_samba3_rootdirfid(struct torture_context *tctx)
        return ret;
 }
 
+bool torture_samba3_oplock_logoff(struct torture_context *tctx)
+{
+       struct smbcli_state *cli;
+       NTSTATUS status;
+       uint16_t fnum1;
+       union smb_open io;
+       const char *fname = "testfile";
+       bool ret = false;
+       struct smbcli_request *req;
+       struct smb_echo echo_req;
+
+       if (!torture_open_connection(&cli, tctx, 0)) {
+               ret = false;
+               goto done;
+       }
+
+       smbcli_unlink(cli->tree, fname);
+
+       ZERO_STRUCT(io);
+       io.generic.level = RAW_OPEN_NTCREATEX;
+       io.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
+       io.ntcreatex.in.root_fid.fnum = 0;
+       io.ntcreatex.in.security_flags = 0;
+       io.ntcreatex.in.access_mask =
+               SEC_STD_SYNCHRONIZE | SEC_FILE_EXECUTE;
+       io.ntcreatex.in.alloc_size = 0;
+       io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
+       io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
+       io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
+       io.ntcreatex.in.create_options = 0;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
+       io.ntcreatex.in.fname = "testfile";
+       status = smb_raw_open(cli->tree, tctx, &io);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("first smb_open failed: %s\n", nt_errstr(status));
+               ret = false;
+               goto done;
+       }
+       fnum1 = io.ntcreatex.out.file.fnum;
+
+       /*
+        * Create a conflicting open, causing the one-second delay
+        */
+
+       req = smb_raw_open_send(cli->tree, &io);
+       if (req == NULL) {
+               d_printf("smb_raw_open_send failed\n");
+               ret = false;
+               goto done;
+       }
+
+       /*
+        * Pull the VUID from under that request. As of Nov 3, 2008 all Samba3
+        * versions (3.0, 3.2 and master) would spin sending ERRinvuid errors
+        * as long as the client is still connected.
+        */
+
+       status = smb_raw_ulogoff(cli->session);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("ulogoff failed: %s\n", nt_errstr(status));
+               ret = false;
+               goto done;
+       }
+
+       echo_req.in.repeat_count = 1;
+       echo_req.in.size = 1;
+       echo_req.in.data = discard_const_p(uint8_t, "");
+
+       status = smb_raw_echo(cli->session->transport, &echo_req);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("smb_raw_echo returned %s\n",
+                        nt_errstr(status));
+               ret = false;
+               goto done;
+       }
+
+       ret = true;
+ done:
+       return ret;
+}