TODO anon? s3:smb3_sesssetup: close the previous SMB2 session if requested and allowed
[metze/samba/wip.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         status = smbd_smb2_request_verify_creditcharge(req,
81                                                 in_output_buffer_length);
82
83         if (!NT_STATUS_IS_OK(status)) {
84                 return smbd_smb2_request_error(req, status);
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->ev_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, 500);
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         DATA_BLOB outbody;
114         DATA_BLOB outdyn;
115         uint16_t out_output_buffer_offset;
116         DATA_BLOB out_output_buffer = data_blob_null;
117         NTSTATUS status;
118         NTSTATUS error; /* transport error */
119
120         if (req->cancelled) {
121                 struct smbd_smb2_notify_state *state = tevent_req_data(subreq,
122                                                struct smbd_smb2_notify_state);
123                 const uint8_t *inhdr = (const uint8_t *)req->in.vector[i].iov_base;
124                 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
125
126                 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
127                         (unsigned long long)mid ));
128                 error = smbd_smb2_request_error(req, NT_STATUS_CANCELLED);
129                 if (!NT_STATUS_IS_OK(error)) {
130                         smbd_server_connection_terminate(req->sconn,
131                                 nt_errstr(error));
132                         return;
133                 }
134                 TALLOC_FREE(state->im);
135                 return;
136         }
137
138         status = smbd_smb2_notify_recv(subreq,
139                                        req,
140                                        &out_output_buffer);
141         TALLOC_FREE(subreq);
142         if (!NT_STATUS_IS_OK(status)) {
143                 error = smbd_smb2_request_error(req, status);
144                 if (!NT_STATUS_IS_OK(error)) {
145                         smbd_server_connection_terminate(req->sconn,
146                                                          nt_errstr(error));
147                         return;
148                 }
149                 return;
150         }
151
152         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
153
154         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
155         if (outbody.data == NULL) {
156                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
157                 if (!NT_STATUS_IS_OK(error)) {
158                         smbd_server_connection_terminate(req->sconn,
159                                                          nt_errstr(error));
160                         return;
161                 }
162                 return;
163         }
164
165         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
166         SSVAL(outbody.data, 0x02,
167               out_output_buffer_offset);        /* output buffer offset */
168         SIVAL(outbody.data, 0x04,
169               out_output_buffer.length);        /* output buffer length */
170
171         outdyn = out_output_buffer;
172
173         error = smbd_smb2_request_done(req, outbody, &outdyn);
174         if (!NT_STATUS_IS_OK(error)) {
175                 smbd_server_connection_terminate(req->sconn,
176                                                  nt_errstr(error));
177                 return;
178         }
179 }
180
181 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
182                                    NTSTATUS error_code,
183                                    uint8_t *buf, size_t len);
184 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
185                                            struct tevent_immediate *im,
186                                            void *private_data);
187 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
188
189 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
190                                                 struct tevent_context *ev,
191                                                 struct smbd_smb2_request *smb2req,
192                                                 uint16_t in_flags,
193                                                 uint32_t in_output_buffer_length,
194                                                 uint64_t in_file_id_volatile,
195                                                 uint64_t in_completion_filter)
196 {
197         struct tevent_req *req;
198         struct smbd_smb2_notify_state *state;
199         struct smb_request *smbreq;
200         connection_struct *conn = smb2req->tcon->compat;
201         files_struct *fsp;
202         bool recursive = (in_flags & SMB2_WATCH_TREE) ? true : false;
203         NTSTATUS status;
204
205         req = tevent_req_create(mem_ctx, &state,
206                                 struct smbd_smb2_notify_state);
207         if (req == NULL) {
208                 return NULL;
209         }
210         state->smb2req = smb2req;
211         state->status = NT_STATUS_INTERNAL_ERROR;
212         state->out_output_buffer = data_blob_null;
213         state->im = NULL;
214
215         DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
216                   (unsigned long long)in_file_id_volatile));
217
218         smbreq = smbd_smb2_fake_smb_request(smb2req);
219         if (tevent_req_nomem(smbreq, req)) {
220                 return tevent_req_post(req, ev);
221         }
222
223         state->smbreq = smbreq;
224         smbreq->async_priv = (void *)req;
225
226         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
227         if (fsp == NULL) {
228                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
229                 return tevent_req_post(req, ev);
230         }
231         if (conn != fsp->conn) {
232                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
233                 return tevent_req_post(req, ev);
234         }
235         if (smb2req->session->compat->vuid != fsp->vuid) {
236                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
237                 return tevent_req_post(req, ev);
238         }
239
240         {
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 (fsp->notify->num_changes != 0) {
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         state->im = tevent_create_immediate(state);
298         if (tevent_req_nomem(state->im, req)) {
299                 return tevent_req_post(req, ev);
300         }
301
302         /*
303          * No changes pending, queue the request
304          */
305
306         status = change_notify_add_request(smbreq,
307                         in_output_buffer_length,
308                         in_completion_filter,
309                         recursive, fsp,
310                         smbd_smb2_notify_reply);
311         if (!NT_STATUS_IS_OK(status)) {
312                 tevent_req_nterror(req, status);
313                 return tevent_req_post(req, ev);
314         }
315
316         /* allow this request to be canceled */
317         tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
318
319         return req;
320 }
321
322 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
323                                    NTSTATUS error_code,
324                                    uint8_t *buf, size_t len)
325 {
326         struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
327                                                        struct tevent_req);
328         struct smbd_smb2_notify_state *state = tevent_req_data(req,
329                                                struct smbd_smb2_notify_state);
330
331         state->status = error_code;
332         if (!NT_STATUS_IS_OK(error_code)) {
333                 /* nothing */
334         } else if (len == 0) {
335                 state->status = STATUS_NOTIFY_ENUM_DIR;
336         } else {
337                 state->out_output_buffer = data_blob_talloc(state, buf, len);
338                 if (state->out_output_buffer.data == NULL) {
339                         state->status = NT_STATUS_NO_MEMORY;
340                 }
341         }
342
343         if (state->im == NULL) {
344                 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
345                 return;
346         }
347
348         /*
349          * if this is called async, we need to go via an immediate event
350          * because the caller replies on the smb_request (a child of req
351          * being arround after calling this function
352          */
353         tevent_schedule_immediate(state->im,
354                                   state->smb2req->sconn->ev_ctx,
355                                   smbd_smb2_notify_reply_trigger,
356                                   req);
357 }
358
359 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
360                                            struct tevent_immediate *im,
361                                            void *private_data)
362 {
363         struct tevent_req *req = talloc_get_type_abort(private_data,
364                                                        struct tevent_req);
365         struct smbd_smb2_notify_state *state = tevent_req_data(req,
366                                                struct smbd_smb2_notify_state);
367
368         if (!NT_STATUS_IS_OK(state->status)) {
369                 tevent_req_nterror(req, state->status);
370                 return;
371         }
372
373         tevent_req_done(req);
374 }
375
376 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
377 {
378         struct smbd_smb2_notify_state *state = tevent_req_data(req,
379                                                struct smbd_smb2_notify_state);
380
381         smbd_notify_cancel_by_smbreq(state->smbreq);
382
383         state->smb2req->cancelled = true;
384         tevent_req_done(req);
385         return true;
386 }
387
388 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
389                                       TALLOC_CTX *mem_ctx,
390                                       DATA_BLOB *out_output_buffer)
391 {
392         NTSTATUS status;
393         struct smbd_smb2_notify_state *state = tevent_req_data(req,
394                                                struct smbd_smb2_notify_state);
395
396         if (tevent_req_is_nterror(req, &status)) {
397                 tevent_req_received(req);
398                 return status;
399         }
400
401         *out_output_buffer = state->out_output_buffer;
402         talloc_steal(mem_ctx, out_output_buffer->data);
403
404         tevent_req_received(req);
405         return NT_STATUS_OK;
406 }