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