Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : test server handling of unexpected client disconnects
5 :
6 : Copyright (C) Andrew Tridgell 2004
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "system/filesys.h"
24 : #include "libcli/raw/libcliraw.h"
25 : #include "libcli/raw/raw_proto.h"
26 : #include "libcli/libcli.h"
27 : #include "torture/util.h"
28 : #include "torture/basic/proto.h"
29 :
30 : #define BASEDIR "\\test_disconnect"
31 :
32 : #define CHECK_STATUS(status, correct) do { \
33 : if (!NT_STATUS_EQUAL(status, correct)) { \
34 : printf("(%s) Incorrect status %s - should be %s\n", \
35 : __location__, nt_errstr(status), nt_errstr(correct)); \
36 : talloc_free(cli); \
37 : return false; \
38 : }} while (0)
39 :
40 : /*
41 : test disconnect after async open
42 : */
43 60 : static bool test_disconnect_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
44 : {
45 0 : union smb_open io;
46 0 : struct smbcli_request *req1, *req2;
47 0 : NTSTATUS status;
48 :
49 60 : printf("trying open/disconnect\n");
50 :
51 60 : io.generic.level = RAW_OPEN_NTCREATEX;
52 60 : io.ntcreatex.in.root_fid.fnum = 0;
53 60 : io.ntcreatex.in.flags = 0;
54 60 : io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
55 60 : io.ntcreatex.in.create_options = 0;
56 60 : io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
57 60 : io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
58 60 : io.ntcreatex.in.alloc_size = 0;
59 60 : io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
60 60 : io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
61 60 : io.ntcreatex.in.security_flags = 0;
62 60 : io.ntcreatex.in.fname = BASEDIR "\\open.dat";
63 60 : status = smb_raw_open(cli->tree, mem_ctx, &io);
64 60 : CHECK_STATUS(status, NT_STATUS_OK);
65 :
66 60 : io.ntcreatex.in.share_access = 0;
67 60 : req1 = smb_raw_open_send(cli->tree, &io);
68 60 : req2 = smb_raw_open_send(cli->tree, &io);
69 60 : if (!req1 || !req2) {
70 0 : printf("test_disconnect_open: smb_raw_open_send() "
71 : "returned NULL\n");
72 0 : return false;
73 : }
74 :
75 60 : status = smbcli_chkpath(cli->tree, "\\");
76 60 : CHECK_STATUS(status, NT_STATUS_OK);
77 :
78 60 : talloc_free(cli);
79 :
80 60 : return true;
81 : }
82 :
83 :
84 : /*
85 : test disconnect with timed lock
86 : */
87 60 : static bool test_disconnect_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
88 : {
89 0 : union smb_lock io;
90 0 : NTSTATUS status;
91 0 : int fnum;
92 0 : struct smbcli_request *req;
93 0 : struct smb_lock_entry lock[1];
94 :
95 60 : printf("trying disconnect with async lock\n");
96 :
97 60 : fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat",
98 : O_RDWR | O_CREAT, DENY_NONE);
99 60 : if (fnum == -1) {
100 0 : printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
101 0 : return false;
102 : }
103 :
104 60 : io.lockx.level = RAW_LOCK_LOCKX;
105 60 : io.lockx.in.file.fnum = fnum;
106 60 : io.lockx.in.mode = 0;
107 60 : io.lockx.in.timeout = 0;
108 60 : io.lockx.in.lock_cnt = 1;
109 60 : io.lockx.in.ulock_cnt = 0;
110 60 : lock[0].pid = 1;
111 60 : lock[0].offset = 0;
112 60 : lock[0].count = 4;
113 60 : io.lockx.in.locks = &lock[0];
114 :
115 60 : status = smb_raw_lock(cli->tree, &io);
116 60 : CHECK_STATUS(status, NT_STATUS_OK);
117 :
118 60 : lock[0].pid = 2;
119 60 : io.lockx.in.timeout = 3000;
120 60 : req = smb_raw_lock_send(cli->tree, &io);
121 60 : if (!req) {
122 0 : printf("test_disconnect_lock: smb_raw_lock_send() "
123 : "returned NULL\n");
124 0 : return false;
125 : }
126 :
127 60 : status = smbcli_chkpath(cli->tree, "\\");
128 60 : CHECK_STATUS(status, NT_STATUS_OK);
129 :
130 60 : talloc_free(cli);
131 :
132 60 : return true;
133 : }
134 :
135 :
136 :
137 : /*
138 : basic testing of disconnects
139 : */
140 6 : bool torture_disconnect(struct torture_context *torture)
141 : {
142 6 : bool ret = true;
143 0 : TALLOC_CTX *mem_ctx;
144 0 : int i;
145 0 : extern int torture_numops;
146 0 : struct smbcli_state *cli;
147 :
148 6 : mem_ctx = talloc_init("torture_raw_mux");
149 :
150 6 : if (!torture_open_connection(&cli, torture, 0)) {
151 0 : return false;
152 : }
153 :
154 6 : torture_assert(torture, torture_setup_dir(cli, BASEDIR), "Failed to setup up test directory: " BASEDIR);
155 :
156 66 : for (i=0;i<torture_numops;i++) {
157 60 : ret &= test_disconnect_lock(cli, mem_ctx);
158 60 : if (!torture_open_connection(&cli, torture, 0)) {
159 0 : return false;
160 : }
161 :
162 60 : ret &= test_disconnect_open(cli, mem_ctx);
163 60 : if (!torture_open_connection(&cli, torture, 0)) {
164 0 : return false;
165 : }
166 :
167 60 : if (torture_setting_bool(torture, "samba3", false)) {
168 : /*
169 : * In Samba3 it might happen that the old smbd from
170 : * test_disconnect_lock is not scheduled before the
171 : * new process comes in. Try to get rid of the random
172 : * failures in the build farm.
173 : */
174 40 : smb_msleep(200);
175 : }
176 : }
177 :
178 6 : smb_raw_exit(cli->session);
179 6 : smbcli_deltree(cli->tree, BASEDIR);
180 6 : talloc_free(mem_ctx);
181 6 : return ret;
182 : }
|