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