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