Don't free the entire tevent_req on cancel, only the part we need (the immediate...
[ddiss/samba.git] / source3 / smbd / smb2_notify.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
25 struct smbd_smb2_notify_state {
26         struct smbd_smb2_request *smb2req;
27         struct smb_request *smbreq;
28         struct tevent_immediate *im;
29         NTSTATUS status;
30         DATA_BLOB out_output_buffer;
31 };
32
33 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
34                                                 struct tevent_context *ev,
35                                                 struct smbd_smb2_request *smb2req,
36                                                 uint16_t in_flags,
37                                                 uint32_t in_output_buffer_length,
38                                                 uint64_t in_file_id_volatile,
39                                                 uint64_t in_completion_filter);
40 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
41                                       TALLOC_CTX *mem_ctx,
42                                       DATA_BLOB *out_output_buffer);
43
44 static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
45 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
46 {
47         const uint8_t *inhdr;
48         const uint8_t *inbody;
49         int i = req->current_idx;
50         size_t expected_body_size = 0x20;
51         size_t body_size;
52         uint16_t in_flags;
53         uint32_t in_output_buffer_length;
54         uint64_t in_file_id_persistent;
55         uint64_t in_file_id_volatile;
56         uint64_t in_completion_filter;
57         struct tevent_req *subreq;
58
59         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
60         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
61                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
62         }
63
64         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
65
66         body_size = SVAL(inbody, 0x00);
67         if (body_size != expected_body_size) {
68                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
69         }
70
71         in_flags                = SVAL(inbody, 0x02);
72         in_output_buffer_length = IVAL(inbody, 0x04);
73         in_file_id_persistent   = BVAL(inbody, 0x08);
74         in_file_id_volatile     = BVAL(inbody, 0x10);
75         in_completion_filter    = IVAL(inbody, 0x18);
76
77         /*
78          * 0x00010000 is what Windows 7 uses,
79          * Windows 2008 uses 0x00080000
80          */
81         if (in_output_buffer_length > lp_smb2_max_trans()) {
82                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83         }
84
85         if (req->compat_chain_fsp) {
86                 /* skip check */
87         } else if (in_file_id_persistent != 0) {
88                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
89         }
90
91         subreq = smbd_smb2_notify_send(req,
92                                        req->sconn->smb2.event_ctx,
93                                        req,
94                                        in_flags,
95                                        in_output_buffer_length,
96                                        in_file_id_volatile,
97                                        in_completion_filter);
98         if (subreq == NULL) {
99                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
100         }
101         tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
102
103         return smbd_smb2_request_pending_queue(req, subreq);
104 }
105
106 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
107 {
108         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
109                                         struct smbd_smb2_request);
110         int i = req->current_idx;
111         uint8_t *outhdr;
112         DATA_BLOB outbody;
113         DATA_BLOB outdyn;
114         uint16_t out_output_buffer_offset;
115         DATA_BLOB out_output_buffer = data_blob_null;
116         NTSTATUS status;
117         NTSTATUS error; /* transport error */
118
119         if (req->cancelled) {
120                 struct smbd_smb2_notify_state *state = tevent_req_data(subreq,
121                                                struct smbd_smb2_notify_state);
122                 const uint8_t *inhdr = (const uint8_t *)req->in.vector[i].iov_base;
123                 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
124
125                 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
126                         (unsigned long long)mid ));
127                 error = smbd_smb2_request_error(req, NT_STATUS_CANCELLED);
128                 if (!NT_STATUS_IS_OK(error)) {
129                         smbd_server_connection_terminate(req->sconn,
130                                 nt_errstr(error));
131                         return;
132                 }
133                 TALLOC_FREE(state->im);
134                 return;
135         }
136
137         status = smbd_smb2_notify_recv(subreq,
138                                        req,
139                                        &out_output_buffer);
140         TALLOC_FREE(subreq);
141         if (!NT_STATUS_IS_OK(status)) {
142                 error = smbd_smb2_request_error(req, status);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->sconn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
152
153         outhdr = (uint8_t *)req->out.vector[i].iov_base;
154
155         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
156         if (outbody.data == NULL) {
157                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
158                 if (!NT_STATUS_IS_OK(error)) {
159                         smbd_server_connection_terminate(req->sconn,
160                                                          nt_errstr(error));
161                         return;
162                 }
163                 return;
164         }
165
166         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
167         SSVAL(outbody.data, 0x02,
168               out_output_buffer_offset);        /* output buffer offset */
169         SIVAL(outbody.data, 0x04,
170               out_output_buffer.length);        /* output buffer length */
171
172         outdyn = out_output_buffer;
173
174         error = smbd_smb2_request_done(req, outbody, &outdyn);
175         if (!NT_STATUS_IS_OK(error)) {
176                 smbd_server_connection_terminate(req->sconn,
177                                                  nt_errstr(error));
178                 return;
179         }
180 }
181
182 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
183                                    NTSTATUS error_code,
184                                    uint8_t *buf, size_t len);
185 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
186                                            struct tevent_immediate *im,
187                                            void *private_data);
188 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
189
190 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
191                                                 struct tevent_context *ev,
192                                                 struct smbd_smb2_request *smb2req,
193                                                 uint16_t in_flags,
194                                                 uint32_t in_output_buffer_length,
195                                                 uint64_t in_file_id_volatile,
196                                                 uint64_t in_completion_filter)
197 {
198         struct tevent_req *req;
199         struct smbd_smb2_notify_state *state;
200         struct smb_request *smbreq;
201         connection_struct *conn = smb2req->tcon->compat_conn;
202         files_struct *fsp;
203         bool recursive = (in_flags & 0x0001) ? true : false;
204         NTSTATUS status;
205
206         req = tevent_req_create(mem_ctx, &state,
207                                 struct smbd_smb2_notify_state);
208         if (req == NULL) {
209                 return NULL;
210         }
211         state->smb2req = smb2req;
212         state->status = NT_STATUS_INTERNAL_ERROR;
213         state->out_output_buffer = data_blob_null;
214         state->im = NULL;
215
216         DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
217                   (unsigned long long)in_file_id_volatile));
218
219         smbreq = smbd_smb2_fake_smb_request(smb2req);
220         if (tevent_req_nomem(smbreq, req)) {
221                 return tevent_req_post(req, ev);
222         }
223
224         state->smbreq = smbreq;
225         smbreq->async_priv = (void *)req;
226
227         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
228         if (fsp == NULL) {
229                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
230                 return tevent_req_post(req, ev);
231         }
232         if (conn != fsp->conn) {
233                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
234                 return tevent_req_post(req, ev);
235         }
236         if (smb2req->session->vuid != fsp->vuid) {
237                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
238                 return tevent_req_post(req, ev);
239         }
240
241         {
242                 char *filter_string;
243
244                 filter_string = notify_filter_string(NULL, in_completion_filter);
245                 if (tevent_req_nomem(filter_string, req)) {
246                         return tevent_req_post(req, ev);
247                 }
248
249                 DEBUG(3,("smbd_smb2_notify_send: notify change "
250                          "called on %s, filter = %s, recursive = %d\n",
251                          fsp_str_dbg(fsp), filter_string, recursive));
252
253                 TALLOC_FREE(filter_string);
254         }
255
256         if ((!fsp->is_directory) || (conn != fsp->conn)) {
257                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
258                 return tevent_req_post(req, ev);
259         }
260
261         if (fsp->notify == NULL) {
262
263                 status = change_notify_create(fsp,
264                                               in_completion_filter,
265                                               recursive);
266                 if (!NT_STATUS_IS_OK(status)) {
267                         DEBUG(10, ("change_notify_create returned %s\n",
268                                    nt_errstr(status)));
269                         tevent_req_nterror(req, status);
270                         return tevent_req_post(req, ev);
271                 }
272         }
273
274         if (fsp->notify->num_changes != 0) {
275
276                 /*
277                  * We've got changes pending, respond immediately
278                  */
279
280                 /*
281                  * TODO: write a torture test to check the filtering behaviour
282                  * here.
283                  */
284
285                 change_notify_reply(fsp->conn, smbreq,
286                                     NT_STATUS_OK,
287                                     in_output_buffer_length,
288                                     fsp->notify,
289                                     smbd_smb2_notify_reply);
290
291                 /*
292                  * change_notify_reply() above has independently
293                  * called tevent_req_done().
294                  */
295                 return tevent_req_post(req, ev);
296         }
297
298         state->im = tevent_create_immediate(state);
299         if (tevent_req_nomem(state->im, req)) {
300                 return tevent_req_post(req, ev);
301         }
302
303         /*
304          * No changes pending, queue the request
305          */
306
307         status = change_notify_add_request(smbreq,
308                         in_output_buffer_length,
309                         in_completion_filter,
310                         recursive, fsp,
311                         smbd_smb2_notify_reply);
312         if (!NT_STATUS_IS_OK(status)) {
313                 tevent_req_nterror(req, status);
314                 return tevent_req_post(req, ev);
315         }
316
317         /* allow this request to be canceled */
318         tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
319
320         return req;
321 }
322
323 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
324                                    NTSTATUS error_code,
325                                    uint8_t *buf, size_t len)
326 {
327         struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
328                                                        struct tevent_req);
329         struct smbd_smb2_notify_state *state = tevent_req_data(req,
330                                                struct smbd_smb2_notify_state);
331
332         state->status = error_code;
333         if (!NT_STATUS_IS_OK(error_code)) {
334                 /* nothing */
335         } else if (len == 0) {
336                 state->status = STATUS_NOTIFY_ENUM_DIR;
337         } else {
338                 state->out_output_buffer = data_blob_talloc(state, buf, len);
339                 if (state->out_output_buffer.data == NULL) {
340                         state->status = NT_STATUS_NO_MEMORY;
341                 }
342         }
343
344         if (state->im == NULL) {
345                 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
346                 return;
347         }
348
349         /*
350          * if this is called async, we need to go via an immediate event
351          * because the caller replies on the smb_request (a child of req
352          * being arround after calling this function
353          */
354         tevent_schedule_immediate(state->im,
355                                   state->smb2req->sconn->smb2.event_ctx,
356                                   smbd_smb2_notify_reply_trigger,
357                                   req);
358 }
359
360 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
361                                            struct tevent_immediate *im,
362                                            void *private_data)
363 {
364         struct tevent_req *req = talloc_get_type_abort(private_data,
365                                                        struct tevent_req);
366         struct smbd_smb2_notify_state *state = tevent_req_data(req,
367                                                struct smbd_smb2_notify_state);
368
369         if (!NT_STATUS_IS_OK(state->status)) {
370                 tevent_req_nterror(req, state->status);
371                 return;
372         }
373
374         tevent_req_done(req);
375 }
376
377 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
378 {
379         struct smbd_smb2_notify_state *state = tevent_req_data(req,
380                                                struct smbd_smb2_notify_state);
381
382         smbd_notify_cancel_by_smbreq(state->smb2req->sconn,
383                                      state->smbreq);
384
385         state->smb2req->cancelled = true;
386         tevent_req_done(req);
387         return true;
388 }
389
390 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
391                                       TALLOC_CTX *mem_ctx,
392                                       DATA_BLOB *out_output_buffer)
393 {
394         NTSTATUS status;
395         struct smbd_smb2_notify_state *state = tevent_req_data(req,
396                                                struct smbd_smb2_notify_state);
397
398         if (tevent_req_is_nterror(req, &status)) {
399                 tevent_req_received(req);
400                 return status;
401         }
402
403         *out_output_buffer = state->out_output_buffer;
404         talloc_steal(mem_ctx, out_output_buffer->data);
405
406         tevent_req_received(req);
407         return NT_STATUS_OK;
408 }