s3:winbindd: add idmap_find_domain_with_sid()
[kai/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         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                                                 struct files_struct *in_fsp,
39                                                 uint16_t in_flags,
40                                                 uint32_t in_output_buffer_length,
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         NTSTATUS status;
50         const uint8_t *inbody;
51         uint16_t in_flags;
52         uint32_t in_output_buffer_length;
53         uint64_t in_file_id_persistent;
54         uint64_t in_file_id_volatile;
55         struct files_struct *in_fsp;
56         uint64_t in_completion_filter;
57         struct tevent_req *subreq;
58
59         status = smbd_smb2_request_verify_sizes(req, 0x20);
60         if (!NT_STATUS_IS_OK(status)) {
61                 return smbd_smb2_request_error(req, status);
62         }
63         inbody = SMBD_SMB2_IN_BODY_PTR(req);
64
65         in_flags                = SVAL(inbody, 0x02);
66         in_output_buffer_length = IVAL(inbody, 0x04);
67         in_file_id_persistent   = BVAL(inbody, 0x08);
68         in_file_id_volatile     = BVAL(inbody, 0x10);
69         in_completion_filter    = IVAL(inbody, 0x18);
70
71         /*
72          * 0x00010000 is what Windows 7 uses,
73          * Windows 2008 uses 0x00080000
74          */
75         if (in_output_buffer_length > req->sconn->smb2.max_trans) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         status = smbd_smb2_request_verify_creditcharge(req,
80                                                 in_output_buffer_length);
81
82         if (!NT_STATUS_IS_OK(status)) {
83                 return smbd_smb2_request_error(req, status);
84         }
85
86         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
87         if (in_fsp == NULL) {
88                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
89         }
90
91         subreq = smbd_smb2_notify_send(req, req->sconn->ev_ctx,
92                                        req, in_fsp,
93                                        in_flags,
94                                        in_output_buffer_length,
95                                        in_completion_filter);
96         if (subreq == NULL) {
97                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
98         }
99         tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
100
101         return smbd_smb2_request_pending_queue(req, subreq, 500);
102 }
103
104 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
105 {
106         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
107                                         struct smbd_smb2_request);
108         DATA_BLOB outbody;
109         DATA_BLOB outdyn;
110         uint16_t out_output_buffer_offset;
111         DATA_BLOB out_output_buffer = data_blob_null;
112         NTSTATUS status;
113         NTSTATUS error; /* transport error */
114
115         status = smbd_smb2_notify_recv(subreq,
116                                        req,
117                                        &out_output_buffer);
118         TALLOC_FREE(subreq);
119         if (!NT_STATUS_IS_OK(status)) {
120                 error = smbd_smb2_request_error(req, status);
121                 if (!NT_STATUS_IS_OK(error)) {
122                         smbd_server_connection_terminate(req->sconn,
123                                                          nt_errstr(error));
124                         return;
125                 }
126                 return;
127         }
128
129         out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
130
131         outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
132         if (outbody.data == NULL) {
133                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
134                 if (!NT_STATUS_IS_OK(error)) {
135                         smbd_server_connection_terminate(req->sconn,
136                                                          nt_errstr(error));
137                         return;
138                 }
139                 return;
140         }
141
142         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
143         SSVAL(outbody.data, 0x02,
144               out_output_buffer_offset);        /* output buffer offset */
145         SIVAL(outbody.data, 0x04,
146               out_output_buffer.length);        /* output buffer length */
147
148         outdyn = out_output_buffer;
149
150         error = smbd_smb2_request_done(req, outbody, &outdyn);
151         if (!NT_STATUS_IS_OK(error)) {
152                 smbd_server_connection_terminate(req->sconn,
153                                                  nt_errstr(error));
154                 return;
155         }
156 }
157
158 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
159                                    NTSTATUS error_code,
160                                    uint8_t *buf, size_t len);
161 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
162
163 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
164                                                 struct tevent_context *ev,
165                                                 struct smbd_smb2_request *smb2req,
166                                                 struct files_struct *fsp,
167                                                 uint16_t in_flags,
168                                                 uint32_t in_output_buffer_length,
169                                                 uint64_t in_completion_filter)
170 {
171         struct tevent_req *req;
172         struct smbd_smb2_notify_state *state;
173         struct smb_request *smbreq;
174         connection_struct *conn = smb2req->tcon->compat;
175         bool recursive = (in_flags & SMB2_WATCH_TREE) ? true : false;
176         NTSTATUS status;
177
178         req = tevent_req_create(mem_ctx, &state,
179                                 struct smbd_smb2_notify_state);
180         if (req == NULL) {
181                 return NULL;
182         }
183         state->smb2req = smb2req;
184         state->status = NT_STATUS_INTERNAL_ERROR;
185         state->out_output_buffer = data_blob_null;
186
187         DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
188                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
189
190         smbreq = smbd_smb2_fake_smb_request(smb2req);
191         if (tevent_req_nomem(smbreq, req)) {
192                 return tevent_req_post(req, ev);
193         }
194
195         state->smbreq = smbreq;
196         smbreq->async_priv = (void *)req;
197
198         {
199                 char *filter_string;
200
201                 filter_string = notify_filter_string(NULL, in_completion_filter);
202                 if (tevent_req_nomem(filter_string, req)) {
203                         return tevent_req_post(req, ev);
204                 }
205
206                 DEBUG(3,("smbd_smb2_notify_send: notify change "
207                          "called on %s, filter = %s, recursive = %d\n",
208                          fsp_str_dbg(fsp), filter_string, recursive));
209
210                 TALLOC_FREE(filter_string);
211         }
212
213         if ((!fsp->is_directory) || (conn != fsp->conn)) {
214                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
215                 return tevent_req_post(req, ev);
216         }
217
218         if (fsp->notify == NULL) {
219
220                 status = change_notify_create(fsp,
221                                               in_completion_filter,
222                                               recursive);
223                 if (!NT_STATUS_IS_OK(status)) {
224                         DEBUG(10, ("change_notify_create returned %s\n",
225                                    nt_errstr(status)));
226                         tevent_req_nterror(req, status);
227                         return tevent_req_post(req, ev);
228                 }
229         }
230
231         if (change_notify_fsp_has_changes(fsp)) {
232
233                 /*
234                  * We've got changes pending, respond immediately
235                  */
236
237                 /*
238                  * TODO: write a torture test to check the filtering behaviour
239                  * here.
240                  */
241
242                 change_notify_reply(smbreq,
243                                     NT_STATUS_OK,
244                                     in_output_buffer_length,
245                                     fsp->notify,
246                                     smbd_smb2_notify_reply);
247
248                 /*
249                  * change_notify_reply() above has independently
250                  * called tevent_req_done().
251                  */
252                 return tevent_req_post(req, ev);
253         }
254
255         /*
256          * No changes pending, queue the request
257          */
258
259         status = change_notify_add_request(smbreq,
260                         in_output_buffer_length,
261                         in_completion_filter,
262                         recursive, fsp,
263                         smbd_smb2_notify_reply);
264         if (!NT_STATUS_IS_OK(status)) {
265                 tevent_req_nterror(req, status);
266                 return tevent_req_post(req, ev);
267         }
268
269         /* allow this request to be canceled */
270         tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
271
272         return req;
273 }
274
275 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
276                                    NTSTATUS error_code,
277                                    uint8_t *buf, size_t len)
278 {
279         struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
280                                                        struct tevent_req);
281         struct smbd_smb2_notify_state *state = tevent_req_data(req,
282                                                struct smbd_smb2_notify_state);
283
284         state->status = error_code;
285         if (!NT_STATUS_IS_OK(error_code)) {
286                 /* nothing */
287         } else if (len == 0) {
288                 state->status = STATUS_NOTIFY_ENUM_DIR;
289         } else {
290                 state->out_output_buffer = data_blob_talloc(state, buf, len);
291                 if (state->out_output_buffer.data == NULL) {
292                         state->status = NT_STATUS_NO_MEMORY;
293                 }
294         }
295
296         tevent_req_defer_callback(req, state->smb2req->sconn->ev_ctx);
297
298         if (!NT_STATUS_IS_OK(state->status)) {
299                 tevent_req_nterror(req, state->status);
300                 return;
301         }
302
303         tevent_req_done(req);
304 }
305
306 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
307 {
308         struct smbd_smb2_notify_state *state = tevent_req_data(req,
309                                                struct smbd_smb2_notify_state);
310
311         smbd_notify_cancel_by_smbreq(state->smbreq);
312
313         return true;
314 }
315
316 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
317                                       TALLOC_CTX *mem_ctx,
318                                       DATA_BLOB *out_output_buffer)
319 {
320         NTSTATUS status;
321         struct smbd_smb2_notify_state *state = tevent_req_data(req,
322                                                struct smbd_smb2_notify_state);
323
324         if (tevent_req_is_nterror(req, &status)) {
325                 tevent_req_received(req);
326                 return status;
327         }
328
329         *out_output_buffer = state->out_output_buffer;
330         talloc_steal(mem_ctx, out_output_buffer->data);
331
332         tevent_req_received(req);
333         return NT_STATUS_OK;
334 }