1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Core SMB2 server
|
---|
4 |
|
---|
5 | Copyright (C) Stefan Metzmacher 2009
|
---|
6 | Copyright (C) Jeremy Allison 2010
|
---|
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 "smbd/smbd.h"
|
---|
24 | #include "smbd/globals.h"
|
---|
25 | #include "../libcli/smb/smb_common.h"
|
---|
26 | #include "../lib/util/tevent_ntstatus.h"
|
---|
27 |
|
---|
28 | struct smbd_smb2_notify_state {
|
---|
29 | struct smbd_smb2_request *smb2req;
|
---|
30 | struct smb_request *smbreq;
|
---|
31 | bool has_request;
|
---|
32 | bool skip_reply;
|
---|
33 | NTSTATUS status;
|
---|
34 | DATA_BLOB out_output_buffer;
|
---|
35 | };
|
---|
36 |
|
---|
37 | static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
|
---|
38 | struct tevent_context *ev,
|
---|
39 | struct smbd_smb2_request *smb2req,
|
---|
40 | struct files_struct *in_fsp,
|
---|
41 | uint16_t in_flags,
|
---|
42 | uint32_t in_output_buffer_length,
|
---|
43 | uint64_t in_completion_filter);
|
---|
44 | static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
|
---|
45 | TALLOC_CTX *mem_ctx,
|
---|
46 | DATA_BLOB *out_output_buffer);
|
---|
47 |
|
---|
48 | static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
|
---|
49 | NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
|
---|
50 | {
|
---|
51 | struct smbXsrv_connection *xconn = req->xconn;
|
---|
52 | NTSTATUS status;
|
---|
53 | const uint8_t *inbody;
|
---|
54 | uint16_t in_flags;
|
---|
55 | uint32_t in_output_buffer_length;
|
---|
56 | uint64_t in_file_id_persistent;
|
---|
57 | uint64_t in_file_id_volatile;
|
---|
58 | struct files_struct *in_fsp;
|
---|
59 | uint64_t in_completion_filter;
|
---|
60 | struct tevent_req *subreq;
|
---|
61 |
|
---|
62 | status = smbd_smb2_request_verify_sizes(req, 0x20);
|
---|
63 | if (!NT_STATUS_IS_OK(status)) {
|
---|
64 | return smbd_smb2_request_error(req, status);
|
---|
65 | }
|
---|
66 | inbody = SMBD_SMB2_IN_BODY_PTR(req);
|
---|
67 |
|
---|
68 | in_flags = SVAL(inbody, 0x02);
|
---|
69 | in_output_buffer_length = IVAL(inbody, 0x04);
|
---|
70 | in_file_id_persistent = BVAL(inbody, 0x08);
|
---|
71 | in_file_id_volatile = BVAL(inbody, 0x10);
|
---|
72 | in_completion_filter = IVAL(inbody, 0x18);
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * 0x00010000 is what Windows 7 uses,
|
---|
76 | * Windows 2008 uses 0x00080000
|
---|
77 | */
|
---|
78 | if (in_output_buffer_length > xconn->smb2.server.max_trans) {
|
---|
79 | return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
80 | }
|
---|
81 |
|
---|
82 | status = smbd_smb2_request_verify_creditcharge(req,
|
---|
83 | in_output_buffer_length);
|
---|
84 |
|
---|
85 | if (!NT_STATUS_IS_OK(status)) {
|
---|
86 | return smbd_smb2_request_error(req, status);
|
---|
87 | }
|
---|
88 |
|
---|
89 | in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
|
---|
90 | if (in_fsp == NULL) {
|
---|
91 | return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
|
---|
92 | }
|
---|
93 |
|
---|
94 | subreq = smbd_smb2_notify_send(req, req->sconn->ev_ctx,
|
---|
95 | req, in_fsp,
|
---|
96 | in_flags,
|
---|
97 | in_output_buffer_length,
|
---|
98 | in_completion_filter);
|
---|
99 | if (subreq == NULL) {
|
---|
100 | return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
|
---|
101 | }
|
---|
102 | tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
|
---|
103 |
|
---|
104 | return smbd_smb2_request_pending_queue(req, subreq, 500);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
|
---|
108 | {
|
---|
109 | struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
|
---|
110 | struct smbd_smb2_request);
|
---|
111 | DATA_BLOB outbody;
|
---|
112 | DATA_BLOB outdyn;
|
---|
113 | uint16_t out_output_buffer_offset;
|
---|
114 | DATA_BLOB out_output_buffer = data_blob_null;
|
---|
115 | NTSTATUS status;
|
---|
116 | NTSTATUS error; /* transport error */
|
---|
117 |
|
---|
118 | status = smbd_smb2_notify_recv(subreq,
|
---|
119 | req,
|
---|
120 | &out_output_buffer);
|
---|
121 | TALLOC_FREE(subreq);
|
---|
122 | if (!NT_STATUS_IS_OK(status)) {
|
---|
123 | error = smbd_smb2_request_error(req, status);
|
---|
124 | if (!NT_STATUS_IS_OK(error)) {
|
---|
125 | smbd_server_connection_terminate(req->xconn,
|
---|
126 | nt_errstr(error));
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
|
---|
133 |
|
---|
134 | outbody = smbd_smb2_generate_outbody(req, 0x08);
|
---|
135 | if (outbody.data == NULL) {
|
---|
136 | error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
|
---|
137 | if (!NT_STATUS_IS_OK(error)) {
|
---|
138 | smbd_server_connection_terminate(req->xconn,
|
---|
139 | nt_errstr(error));
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | return;
|
---|
143 | }
|
---|
144 |
|
---|
145 | SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
|
---|
146 | SSVAL(outbody.data, 0x02,
|
---|
147 | out_output_buffer_offset); /* output buffer offset */
|
---|
148 | SIVAL(outbody.data, 0x04,
|
---|
149 | out_output_buffer.length); /* output buffer length */
|
---|
150 |
|
---|
151 | outdyn = out_output_buffer;
|
---|
152 |
|
---|
153 | error = smbd_smb2_request_done(req, outbody, &outdyn);
|
---|
154 | if (!NT_STATUS_IS_OK(error)) {
|
---|
155 | smbd_server_connection_terminate(req->xconn,
|
---|
156 | nt_errstr(error));
|
---|
157 | return;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | static void smbd_smb2_notify_reply(struct smb_request *smbreq,
|
---|
162 | NTSTATUS error_code,
|
---|
163 | uint8_t *buf, size_t len);
|
---|
164 | static bool smbd_smb2_notify_cancel(struct tevent_req *req);
|
---|
165 |
|
---|
166 | static int smbd_smb2_notify_state_destructor(struct smbd_smb2_notify_state *state)
|
---|
167 | {
|
---|
168 | if (!state->has_request) {
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | state->skip_reply = true;
|
---|
173 | smbd_notify_cancel_by_smbreq(state->smbreq);
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static int smbd_smb2_notify_smbreq_destructor(struct smb_request *smbreq)
|
---|
178 | {
|
---|
179 | struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
|
---|
180 | struct tevent_req);
|
---|
181 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
182 | struct smbd_smb2_notify_state);
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Our temporary parent from change_notify_add_request()
|
---|
186 | * goes away.
|
---|
187 | */
|
---|
188 | state->has_request = false;
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * move it back to its original parent,
|
---|
192 | * which means we no longer need the destructor
|
---|
193 | * to protect it.
|
---|
194 | */
|
---|
195 | talloc_steal(smbreq->smb2req, smbreq);
|
---|
196 | talloc_set_destructor(smbreq, NULL);
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * We want to keep smbreq!
|
---|
200 | */
|
---|
201 | return -1;
|
---|
202 | }
|
---|
203 |
|
---|
204 | static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
|
---|
205 | struct tevent_context *ev,
|
---|
206 | struct smbd_smb2_request *smb2req,
|
---|
207 | struct files_struct *fsp,
|
---|
208 | uint16_t in_flags,
|
---|
209 | uint32_t in_output_buffer_length,
|
---|
210 | uint64_t in_completion_filter)
|
---|
211 | {
|
---|
212 | struct tevent_req *req;
|
---|
213 | struct smbd_smb2_notify_state *state;
|
---|
214 | struct smb_request *smbreq;
|
---|
215 | connection_struct *conn = smb2req->tcon->compat;
|
---|
216 | bool recursive = (in_flags & SMB2_WATCH_TREE) ? true : false;
|
---|
217 | NTSTATUS status;
|
---|
218 |
|
---|
219 | req = tevent_req_create(mem_ctx, &state,
|
---|
220 | struct smbd_smb2_notify_state);
|
---|
221 | if (req == NULL) {
|
---|
222 | return NULL;
|
---|
223 | }
|
---|
224 | state->smb2req = smb2req;
|
---|
225 | state->status = NT_STATUS_INTERNAL_ERROR;
|
---|
226 | state->out_output_buffer = data_blob_null;
|
---|
227 | talloc_set_destructor(state, smbd_smb2_notify_state_destructor);
|
---|
228 |
|
---|
229 | DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
|
---|
230 | fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
|
---|
231 |
|
---|
232 | smbreq = smbd_smb2_fake_smb_request(smb2req);
|
---|
233 | if (tevent_req_nomem(smbreq, req)) {
|
---|
234 | return tevent_req_post(req, ev);
|
---|
235 | }
|
---|
236 |
|
---|
237 | state->smbreq = smbreq;
|
---|
238 | smbreq->async_priv = (void *)req;
|
---|
239 |
|
---|
240 | if (DEBUGLEVEL >= 3) {
|
---|
241 | char *filter_string;
|
---|
242 |
|
---|
243 | filter_string = notify_filter_string(NULL, in_completion_filter);
|
---|
244 | if (tevent_req_nomem(filter_string, req)) {
|
---|
245 | return tevent_req_post(req, ev);
|
---|
246 | }
|
---|
247 |
|
---|
248 | DEBUG(3,("smbd_smb2_notify_send: notify change "
|
---|
249 | "called on %s, filter = %s, recursive = %d\n",
|
---|
250 | fsp_str_dbg(fsp), filter_string, recursive));
|
---|
251 |
|
---|
252 | TALLOC_FREE(filter_string);
|
---|
253 | }
|
---|
254 |
|
---|
255 | if ((!fsp->is_directory) || (conn != fsp->conn)) {
|
---|
256 | tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
|
---|
257 | return tevent_req_post(req, ev);
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (fsp->notify == NULL) {
|
---|
261 |
|
---|
262 | status = change_notify_create(fsp,
|
---|
263 | in_completion_filter,
|
---|
264 | recursive);
|
---|
265 | if (!NT_STATUS_IS_OK(status)) {
|
---|
266 | DEBUG(10, ("change_notify_create returned %s\n",
|
---|
267 | nt_errstr(status)));
|
---|
268 | tevent_req_nterror(req, status);
|
---|
269 | return tevent_req_post(req, ev);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (change_notify_fsp_has_changes(fsp)) {
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * We've got changes pending, respond immediately
|
---|
277 | */
|
---|
278 |
|
---|
279 | /*
|
---|
280 | * TODO: write a torture test to check the filtering behaviour
|
---|
281 | * here.
|
---|
282 | */
|
---|
283 |
|
---|
284 | change_notify_reply(smbreq,
|
---|
285 | NT_STATUS_OK,
|
---|
286 | in_output_buffer_length,
|
---|
287 | fsp->notify,
|
---|
288 | smbd_smb2_notify_reply);
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * change_notify_reply() above has independently
|
---|
292 | * called tevent_req_done().
|
---|
293 | */
|
---|
294 | return tevent_req_post(req, ev);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * No changes pending, queue the request
|
---|
299 | */
|
---|
300 |
|
---|
301 | status = change_notify_add_request(smbreq,
|
---|
302 | in_output_buffer_length,
|
---|
303 | in_completion_filter,
|
---|
304 | recursive, fsp,
|
---|
305 | smbd_smb2_notify_reply);
|
---|
306 | if (!NT_STATUS_IS_OK(status)) {
|
---|
307 | tevent_req_nterror(req, status);
|
---|
308 | return tevent_req_post(req, ev);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*
|
---|
312 | * This is a HACK!
|
---|
313 | *
|
---|
314 | * change_notify_add_request() talloc_moves()
|
---|
315 | * smbreq away from us, so we need a destructor
|
---|
316 | * which moves it back at the end.
|
---|
317 | */
|
---|
318 | state->has_request = true;
|
---|
319 | talloc_set_destructor(smbreq, smbd_smb2_notify_smbreq_destructor);
|
---|
320 |
|
---|
321 | /* allow this request to be canceled */
|
---|
322 | tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
|
---|
323 |
|
---|
324 | SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(state->smb2req->profile);
|
---|
325 | return req;
|
---|
326 | }
|
---|
327 |
|
---|
328 | static void smbd_smb2_notify_reply(struct smb_request *smbreq,
|
---|
329 | NTSTATUS error_code,
|
---|
330 | uint8_t *buf, size_t len)
|
---|
331 | {
|
---|
332 | struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
|
---|
333 | struct tevent_req);
|
---|
334 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
335 | struct smbd_smb2_notify_state);
|
---|
336 |
|
---|
337 | if (state->skip_reply) {
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
341 | SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(state->smb2req->profile);
|
---|
342 |
|
---|
343 | state->status = error_code;
|
---|
344 | if (!NT_STATUS_IS_OK(error_code)) {
|
---|
345 | /* nothing */
|
---|
346 | } else if (len == 0) {
|
---|
347 | state->status = STATUS_NOTIFY_ENUM_DIR;
|
---|
348 | } else {
|
---|
349 | state->out_output_buffer = data_blob_talloc(state, buf, len);
|
---|
350 | if (state->out_output_buffer.data == NULL) {
|
---|
351 | state->status = NT_STATUS_NO_MEMORY;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | tevent_req_defer_callback(req, state->smb2req->sconn->ev_ctx);
|
---|
356 |
|
---|
357 | if (!NT_STATUS_IS_OK(state->status)) {
|
---|
358 | tevent_req_nterror(req, state->status);
|
---|
359 | return;
|
---|
360 | }
|
---|
361 |
|
---|
362 | tevent_req_done(req);
|
---|
363 | }
|
---|
364 |
|
---|
365 | static bool smbd_smb2_notify_cancel(struct tevent_req *req)
|
---|
366 | {
|
---|
367 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
368 | struct smbd_smb2_notify_state);
|
---|
369 |
|
---|
370 | smbd_notify_cancel_by_smbreq(state->smbreq);
|
---|
371 |
|
---|
372 | return true;
|
---|
373 | }
|
---|
374 |
|
---|
375 | static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
|
---|
376 | TALLOC_CTX *mem_ctx,
|
---|
377 | DATA_BLOB *out_output_buffer)
|
---|
378 | {
|
---|
379 | NTSTATUS status;
|
---|
380 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
381 | struct smbd_smb2_notify_state);
|
---|
382 |
|
---|
383 | if (tevent_req_is_nterror(req, &status)) {
|
---|
384 | tevent_req_received(req);
|
---|
385 | return status;
|
---|
386 | }
|
---|
387 |
|
---|
388 | *out_output_buffer = state->out_output_buffer;
|
---|
389 | talloc_steal(mem_ctx, out_output_buffer->data);
|
---|
390 |
|
---|
391 | tevent_req_received(req);
|
---|
392 | return NT_STATUS_OK;
|
---|
393 | }
|
---|