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 | struct tevent_immediate *im;
|
---|
32 | NTSTATUS status;
|
---|
33 | DATA_BLOB out_output_buffer;
|
---|
34 | };
|
---|
35 |
|
---|
36 | static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
|
---|
37 | struct tevent_context *ev,
|
---|
38 | struct smbd_smb2_request *smb2req,
|
---|
39 | struct files_struct *in_fsp,
|
---|
40 | uint16_t in_flags,
|
---|
41 | uint32_t in_output_buffer_length,
|
---|
42 | uint64_t in_completion_filter);
|
---|
43 | static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
|
---|
44 | TALLOC_CTX *mem_ctx,
|
---|
45 | DATA_BLOB *out_output_buffer);
|
---|
46 |
|
---|
47 | static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
|
---|
48 | NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
|
---|
49 | {
|
---|
50 | NTSTATUS status;
|
---|
51 | const uint8_t *inbody;
|
---|
52 | int i = req->current_idx;
|
---|
53 | uint16_t in_flags;
|
---|
54 | uint32_t in_output_buffer_length;
|
---|
55 | uint64_t in_file_id_persistent;
|
---|
56 | uint64_t in_file_id_volatile;
|
---|
57 | struct files_struct *in_fsp;
|
---|
58 | uint64_t in_completion_filter;
|
---|
59 | struct tevent_req *subreq;
|
---|
60 |
|
---|
61 | status = smbd_smb2_request_verify_sizes(req, 0x20);
|
---|
62 | if (!NT_STATUS_IS_OK(status)) {
|
---|
63 | return smbd_smb2_request_error(req, status);
|
---|
64 | }
|
---|
65 | inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
|
---|
66 |
|
---|
67 | in_flags = SVAL(inbody, 0x02);
|
---|
68 | in_output_buffer_length = IVAL(inbody, 0x04);
|
---|
69 | in_file_id_persistent = BVAL(inbody, 0x08);
|
---|
70 | in_file_id_volatile = BVAL(inbody, 0x10);
|
---|
71 | in_completion_filter = IVAL(inbody, 0x18);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * 0x00010000 is what Windows 7 uses,
|
---|
75 | * Windows 2008 uses 0x00080000
|
---|
76 | */
|
---|
77 | if (in_output_buffer_length > req->sconn->smb2.max_trans) {
|
---|
78 | return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
|
---|
79 | }
|
---|
80 |
|
---|
81 | in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
|
---|
82 | if (in_fsp == NULL) {
|
---|
83 | return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
|
---|
84 | }
|
---|
85 |
|
---|
86 | subreq = smbd_smb2_notify_send(req, req->sconn->smb2.event_ctx,
|
---|
87 | req, in_fsp,
|
---|
88 | in_flags,
|
---|
89 | in_output_buffer_length,
|
---|
90 | in_completion_filter);
|
---|
91 | if (subreq == NULL) {
|
---|
92 | return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
|
---|
93 | }
|
---|
94 | tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
|
---|
95 |
|
---|
96 | return smbd_smb2_request_pending_queue(req, subreq);
|
---|
97 | }
|
---|
98 |
|
---|
99 | static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
|
---|
100 | {
|
---|
101 | struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
|
---|
102 | struct smbd_smb2_request);
|
---|
103 | int i = req->current_idx;
|
---|
104 | uint8_t *outhdr;
|
---|
105 | DATA_BLOB outbody;
|
---|
106 | DATA_BLOB outdyn;
|
---|
107 | uint16_t out_output_buffer_offset;
|
---|
108 | DATA_BLOB out_output_buffer = data_blob_null;
|
---|
109 | NTSTATUS status;
|
---|
110 | NTSTATUS error; /* transport error */
|
---|
111 |
|
---|
112 | if (req->cancelled) {
|
---|
113 | struct smbd_smb2_notify_state *state = tevent_req_data(subreq,
|
---|
114 | struct smbd_smb2_notify_state);
|
---|
115 | const uint8_t *inhdr = (const uint8_t *)req->in.vector[i].iov_base;
|
---|
116 | uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
|
---|
117 |
|
---|
118 | DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
|
---|
119 | (unsigned long long)mid ));
|
---|
120 | error = smbd_smb2_request_error(req, NT_STATUS_CANCELLED);
|
---|
121 | if (!NT_STATUS_IS_OK(error)) {
|
---|
122 | smbd_server_connection_terminate(req->sconn,
|
---|
123 | nt_errstr(error));
|
---|
124 | return;
|
---|
125 | }
|
---|
126 | TALLOC_FREE(state->im);
|
---|
127 | return;
|
---|
128 | }
|
---|
129 |
|
---|
130 | status = smbd_smb2_notify_recv(subreq,
|
---|
131 | req,
|
---|
132 | &out_output_buffer);
|
---|
133 | TALLOC_FREE(subreq);
|
---|
134 | if (!NT_STATUS_IS_OK(status)) {
|
---|
135 | error = smbd_smb2_request_error(req, status);
|
---|
136 | if (!NT_STATUS_IS_OK(error)) {
|
---|
137 | smbd_server_connection_terminate(req->sconn,
|
---|
138 | nt_errstr(error));
|
---|
139 | return;
|
---|
140 | }
|
---|
141 | return;
|
---|
142 | }
|
---|
143 |
|
---|
144 | out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
|
---|
145 |
|
---|
146 | outhdr = (uint8_t *)req->out.vector[i].iov_base;
|
---|
147 |
|
---|
148 | outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
|
---|
149 | if (outbody.data == NULL) {
|
---|
150 | error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
|
---|
151 | if (!NT_STATUS_IS_OK(error)) {
|
---|
152 | smbd_server_connection_terminate(req->sconn,
|
---|
153 | nt_errstr(error));
|
---|
154 | return;
|
---|
155 | }
|
---|
156 | return;
|
---|
157 | }
|
---|
158 |
|
---|
159 | SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
|
---|
160 | SSVAL(outbody.data, 0x02,
|
---|
161 | out_output_buffer_offset); /* output buffer offset */
|
---|
162 | SIVAL(outbody.data, 0x04,
|
---|
163 | out_output_buffer.length); /* output buffer length */
|
---|
164 |
|
---|
165 | outdyn = out_output_buffer;
|
---|
166 |
|
---|
167 | error = smbd_smb2_request_done(req, outbody, &outdyn);
|
---|
168 | if (!NT_STATUS_IS_OK(error)) {
|
---|
169 | smbd_server_connection_terminate(req->sconn,
|
---|
170 | nt_errstr(error));
|
---|
171 | return;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | static void smbd_smb2_notify_reply(struct smb_request *smbreq,
|
---|
176 | NTSTATUS error_code,
|
---|
177 | uint8_t *buf, size_t len);
|
---|
178 | static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
|
---|
179 | struct tevent_immediate *im,
|
---|
180 | void *private_data);
|
---|
181 | static bool smbd_smb2_notify_cancel(struct tevent_req *req);
|
---|
182 |
|
---|
183 | static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
|
---|
184 | struct tevent_context *ev,
|
---|
185 | struct smbd_smb2_request *smb2req,
|
---|
186 | struct files_struct *fsp,
|
---|
187 | uint16_t in_flags,
|
---|
188 | uint32_t in_output_buffer_length,
|
---|
189 | uint64_t in_completion_filter)
|
---|
190 | {
|
---|
191 | struct tevent_req *req;
|
---|
192 | struct smbd_smb2_notify_state *state;
|
---|
193 | struct smb_request *smbreq;
|
---|
194 | connection_struct *conn = smb2req->tcon->compat_conn;
|
---|
195 | bool recursive = (in_flags & 0x0001) ? true : false;
|
---|
196 | NTSTATUS status;
|
---|
197 |
|
---|
198 | req = tevent_req_create(mem_ctx, &state,
|
---|
199 | struct smbd_smb2_notify_state);
|
---|
200 | if (req == NULL) {
|
---|
201 | return NULL;
|
---|
202 | }
|
---|
203 | state->smb2req = smb2req;
|
---|
204 | state->status = NT_STATUS_INTERNAL_ERROR;
|
---|
205 | state->out_output_buffer = data_blob_null;
|
---|
206 | state->im = NULL;
|
---|
207 |
|
---|
208 | DEBUG(10,("smbd_smb2_notify_send: %s - fnum[%d]\n",
|
---|
209 | fsp_str_dbg(fsp), fsp->fnum));
|
---|
210 |
|
---|
211 | smbreq = smbd_smb2_fake_smb_request(smb2req);
|
---|
212 | if (tevent_req_nomem(smbreq, req)) {
|
---|
213 | return tevent_req_post(req, ev);
|
---|
214 | }
|
---|
215 |
|
---|
216 | state->smbreq = smbreq;
|
---|
217 | smbreq->async_priv = (void *)req;
|
---|
218 |
|
---|
219 | {
|
---|
220 | char *filter_string;
|
---|
221 |
|
---|
222 | filter_string = notify_filter_string(NULL, in_completion_filter);
|
---|
223 | if (tevent_req_nomem(filter_string, req)) {
|
---|
224 | return tevent_req_post(req, ev);
|
---|
225 | }
|
---|
226 |
|
---|
227 | DEBUG(3,("smbd_smb2_notify_send: notify change "
|
---|
228 | "called on %s, filter = %s, recursive = %d\n",
|
---|
229 | fsp_str_dbg(fsp), filter_string, recursive));
|
---|
230 |
|
---|
231 | TALLOC_FREE(filter_string);
|
---|
232 | }
|
---|
233 |
|
---|
234 | if ((!fsp->is_directory) || (conn != fsp->conn)) {
|
---|
235 | tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
|
---|
236 | return tevent_req_post(req, ev);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (fsp->notify == NULL) {
|
---|
240 |
|
---|
241 | status = change_notify_create(fsp,
|
---|
242 | in_completion_filter,
|
---|
243 | recursive);
|
---|
244 | if (!NT_STATUS_IS_OK(status)) {
|
---|
245 | DEBUG(10, ("change_notify_create returned %s\n",
|
---|
246 | nt_errstr(status)));
|
---|
247 | tevent_req_nterror(req, status);
|
---|
248 | return tevent_req_post(req, ev);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (fsp->notify->num_changes != 0) {
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * We've got changes pending, respond immediately
|
---|
256 | */
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * TODO: write a torture test to check the filtering behaviour
|
---|
260 | * here.
|
---|
261 | */
|
---|
262 |
|
---|
263 | change_notify_reply(smbreq,
|
---|
264 | NT_STATUS_OK,
|
---|
265 | in_output_buffer_length,
|
---|
266 | fsp->notify,
|
---|
267 | smbd_smb2_notify_reply);
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * change_notify_reply() above has independently
|
---|
271 | * called tevent_req_done().
|
---|
272 | */
|
---|
273 | return tevent_req_post(req, ev);
|
---|
274 | }
|
---|
275 |
|
---|
276 | state->im = tevent_create_immediate(state);
|
---|
277 | if (tevent_req_nomem(state->im, req)) {
|
---|
278 | return tevent_req_post(req, ev);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * No changes pending, queue the request
|
---|
283 | */
|
---|
284 |
|
---|
285 | status = change_notify_add_request(smbreq,
|
---|
286 | in_output_buffer_length,
|
---|
287 | in_completion_filter,
|
---|
288 | recursive, fsp,
|
---|
289 | smbd_smb2_notify_reply);
|
---|
290 | if (!NT_STATUS_IS_OK(status)) {
|
---|
291 | tevent_req_nterror(req, status);
|
---|
292 | return tevent_req_post(req, ev);
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* allow this request to be canceled */
|
---|
296 | tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
|
---|
297 |
|
---|
298 | return req;
|
---|
299 | }
|
---|
300 |
|
---|
301 | static void smbd_smb2_notify_reply(struct smb_request *smbreq,
|
---|
302 | NTSTATUS error_code,
|
---|
303 | uint8_t *buf, size_t len)
|
---|
304 | {
|
---|
305 | struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
|
---|
306 | struct tevent_req);
|
---|
307 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
308 | struct smbd_smb2_notify_state);
|
---|
309 |
|
---|
310 | state->status = error_code;
|
---|
311 | if (!NT_STATUS_IS_OK(error_code)) {
|
---|
312 | /* nothing */
|
---|
313 | } else if (len == 0) {
|
---|
314 | state->status = STATUS_NOTIFY_ENUM_DIR;
|
---|
315 | } else {
|
---|
316 | state->out_output_buffer = data_blob_talloc(state, buf, len);
|
---|
317 | if (state->out_output_buffer.data == NULL) {
|
---|
318 | state->status = NT_STATUS_NO_MEMORY;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | if (state->im == NULL) {
|
---|
323 | smbd_smb2_notify_reply_trigger(NULL, NULL, req);
|
---|
324 | return;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /*
|
---|
328 | * if this is called async, we need to go via an immediate event
|
---|
329 | * because the caller replies on the smb_request (a child of req
|
---|
330 | * being arround after calling this function
|
---|
331 | */
|
---|
332 | tevent_schedule_immediate(state->im,
|
---|
333 | state->smb2req->sconn->smb2.event_ctx,
|
---|
334 | smbd_smb2_notify_reply_trigger,
|
---|
335 | req);
|
---|
336 | }
|
---|
337 |
|
---|
338 | static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
|
---|
339 | struct tevent_immediate *im,
|
---|
340 | void *private_data)
|
---|
341 | {
|
---|
342 | struct tevent_req *req = talloc_get_type_abort(private_data,
|
---|
343 | struct tevent_req);
|
---|
344 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
345 | struct smbd_smb2_notify_state);
|
---|
346 |
|
---|
347 | if (!NT_STATUS_IS_OK(state->status)) {
|
---|
348 | tevent_req_nterror(req, state->status);
|
---|
349 | return;
|
---|
350 | }
|
---|
351 |
|
---|
352 | tevent_req_done(req);
|
---|
353 | }
|
---|
354 |
|
---|
355 | static bool smbd_smb2_notify_cancel(struct tevent_req *req)
|
---|
356 | {
|
---|
357 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
358 | struct smbd_smb2_notify_state);
|
---|
359 |
|
---|
360 | state->smb2req->cancelled = true;
|
---|
361 | smbd_notify_cancel_by_smbreq(state->smbreq);
|
---|
362 |
|
---|
363 | return true;
|
---|
364 | }
|
---|
365 |
|
---|
366 | static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
|
---|
367 | TALLOC_CTX *mem_ctx,
|
---|
368 | DATA_BLOB *out_output_buffer)
|
---|
369 | {
|
---|
370 | NTSTATUS status;
|
---|
371 | struct smbd_smb2_notify_state *state = tevent_req_data(req,
|
---|
372 | struct smbd_smb2_notify_state);
|
---|
373 |
|
---|
374 | if (tevent_req_is_nterror(req, &status)) {
|
---|
375 | tevent_req_received(req);
|
---|
376 | return status;
|
---|
377 | }
|
---|
378 |
|
---|
379 | *out_output_buffer = state->out_output_buffer;
|
---|
380 | talloc_steal(mem_ctx, out_output_buffer->data);
|
---|
381 |
|
---|
382 | tevent_req_received(req);
|
---|
383 | return NT_STATUS_OK;
|
---|
384 | }
|
---|