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