Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Test some misc Samba3 code paths
5 :
6 : Copyright (C) Volker Lendecke 2006
7 : Copyright (C) Stefan Metzmacher 2019
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "includes.h"
24 : #include "system/time.h"
25 : #include "system/filesys.h"
26 : #include "libcli/smb2/smb2.h"
27 : #include "libcli/smb2/smb2_calls.h"
28 : #include "../libcli/smb/smbXcli_base.h"
29 : #include "torture/torture.h"
30 : #include "torture/smb2/proto.h"
31 : #include "torture/util.h"
32 : #include "lib/events/events.h"
33 : #include "param/param.h"
34 :
35 : #define CHECK_STATUS(status, correct) do { \
36 : const char *_cmt = "(" __location__ ")"; \
37 : torture_assert_ntstatus_equal_goto(tctx,status,correct, \
38 : ret,done,_cmt); \
39 : } while (0)
40 :
41 : #define BASEDIR "samba3misc.smb2"
42 :
43 : #define WAIT_FOR_ASYNC_RESPONSE(req) \
44 : while (!req->cancel.can_cancel && req->state <= SMB2_REQUEST_RECV) { \
45 : if (tevent_loop_once(tctx->ev) != 0) { \
46 : break; \
47 : } \
48 : }
49 :
50 0 : static void torture_smb2_tree_disconnect_timer(struct tevent_context *ev,
51 : struct tevent_timer *te,
52 : struct timeval now,
53 : void *private_data)
54 : {
55 0 : struct smb2_tree *tree =
56 0 : talloc_get_type_abort(private_data,
57 : struct smb2_tree);
58 :
59 0 : smbXcli_conn_disconnect(tree->session->transport->conn,
60 0 : NT_STATUS_CTX_CLIENT_QUERY_TIMEOUT);
61 0 : }
62 :
63 : /*
64 : * Check that Samba3 correctly deals with conflicting local posix byte range
65 : * locks on an underlying file via "normal" SMB2 (without posix extensions).
66 : *
67 : * Note: This test depends on "posix locking = yes".
68 : * Note: To run this test, use "--option=torture:localdir=<LOCALDIR>"
69 : */
70 8 : static bool torture_samba3_localposixlock1(struct torture_context *tctx,
71 : struct smb2_tree *tree)
72 : {
73 0 : NTSTATUS status;
74 8 : bool ret = true;
75 0 : int rc;
76 8 : const char *fname = "posixtimedlock.dat";
77 0 : const char *fpath;
78 0 : const char *localdir;
79 0 : const char *localname;
80 8 : struct smb2_handle h = {{0}};
81 8 : struct smb2_lock lck = {0};
82 8 : struct smb2_lock_element el[1] = {{0}};
83 8 : struct smb2_request *req = NULL;
84 8 : int fd = -1;
85 0 : struct flock posix_lock;
86 0 : struct tevent_timer *te;
87 :
88 8 : status = torture_smb2_testdir(tree, BASEDIR, &h);
89 8 : CHECK_STATUS(status, NT_STATUS_OK);
90 8 : smb2_util_close(tree, h);
91 :
92 8 : status = torture_smb2_testfile(tree, fname, &h);
93 8 : CHECK_STATUS(status, NT_STATUS_OK);
94 :
95 8 : fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fname);
96 8 : torture_assert(tctx, fpath != NULL, "fpath\n");
97 :
98 8 : status = torture_smb2_testfile(tree, fpath, &h);
99 8 : CHECK_STATUS(status, NT_STATUS_OK);
100 :
101 8 : localdir = torture_setting_string(tctx, "localdir", NULL);
102 8 : torture_assert(tctx, localdir != NULL,
103 : "--option=torture:localdir=<LOCALDIR> required\n");
104 :
105 8 : localname = talloc_asprintf(tctx, "%s/%s/%s",
106 : localdir, BASEDIR, fname);
107 8 : torture_assert(tctx, localname != NULL, "localname\n");
108 :
109 : /*
110 : * Lock a byte range from posix
111 : */
112 :
113 8 : torture_comment(tctx, " local open(%s)\n", localname);
114 8 : fd = open(localname, O_RDWR);
115 8 : if (fd == -1) {
116 0 : torture_warning(tctx, "open(%s) failed: %s\n",
117 0 : localname, strerror(errno));
118 0 : torture_assert(tctx, fd != -1, "open localname\n");
119 : }
120 :
121 8 : posix_lock.l_type = F_WRLCK;
122 8 : posix_lock.l_whence = SEEK_SET;
123 8 : posix_lock.l_start = 0;
124 8 : posix_lock.l_len = 1;
125 :
126 8 : torture_comment(tctx, " local fcntl\n");
127 8 : rc = fcntl(fd, F_SETLK, &posix_lock);
128 8 : if (rc == -1) {
129 0 : torture_warning(tctx, "fcntl failed: %s\n", strerror(errno));
130 0 : torture_assert_goto(tctx, rc != -1, ret, done,
131 : "fcntl lock\n");
132 : }
133 :
134 8 : el[0].offset = 0;
135 8 : el[0].length = 1;
136 8 : el[0].reserved = 0x00000000;
137 8 : el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
138 : SMB2_LOCK_FLAG_FAIL_IMMEDIATELY;
139 8 : lck.in.locks = el;
140 8 : lck.in.lock_count = 0x0001;
141 8 : lck.in.lock_sequence = 0x00000000;
142 8 : lck.in.file.handle = h;
143 :
144 8 : torture_comment(tctx, " remote non-blocking lock\n");
145 8 : status = smb2_lock(tree, &lck);
146 8 : CHECK_STATUS(status, NT_STATUS_LOCK_NOT_GRANTED);
147 :
148 8 : torture_comment(tctx, " remote async blocking lock\n");
149 8 : el[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE;
150 8 : req = smb2_lock_send(tree, &lck);
151 8 : torture_assert_goto(tctx, req != NULL, ret, done, "smb2_lock_send()\n");
152 :
153 8 : te = tevent_add_timer(tctx->ev,
154 : tctx, timeval_current_ofs(5, 0),
155 : torture_smb2_tree_disconnect_timer,
156 : tree);
157 8 : torture_assert_goto(tctx, te != NULL, ret, done, "tevent_add_timer\n");
158 :
159 8 : torture_comment(tctx, " remote wait for STATUS_PENDING\n");
160 40 : WAIT_FOR_ASYNC_RESPONSE(req);
161 :
162 8 : torture_comment(tctx, " local close file\n");
163 8 : close(fd);
164 8 : fd = -1;
165 :
166 8 : torture_comment(tctx, " remote lock should now succeed\n");
167 8 : status = smb2_lock_recv(req, &lck);
168 8 : CHECK_STATUS(status, NT_STATUS_OK);
169 :
170 8 : done:
171 8 : if (fd != -1) {
172 0 : close(fd);
173 : }
174 8 : smb2_util_close(tree, h);
175 8 : smb2_deltree(tree, BASEDIR);
176 8 : return ret;
177 : }
178 :
179 2354 : struct torture_suite *torture_smb2_samba3misc_init(TALLOC_CTX *ctx)
180 : {
181 2354 : struct torture_suite *suite = torture_suite_create(ctx, "samba3misc");
182 :
183 2354 : torture_suite_add_1smb2_test(suite, "localposixlock1",
184 : torture_samba3_localposixlock1);
185 :
186 2354 : suite->description = talloc_strdup(suite, "SMB2 Samba3 MISC");
187 :
188 2354 : return suite;
189 : }
|