s3-registry: fix upgrade code
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_notify.c
blob49c6a5449722484d562d53d3f23d7a5a27cff232
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
6 Copyright (C) Jeremy Allison 2010
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.
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.
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/>.
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"
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;
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);
47 static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
48 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
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;
60 status = smbd_smb2_request_verify_sizes(req, 0x20);
61 if (!NT_STATUS_IS_OK(status)) {
62 return smbd_smb2_request_error(req, status);
64 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
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);
73 * 0x00010000 is what Windows 7 uses,
74 * Windows 2008 uses 0x00080000
76 if (in_output_buffer_length > req->sconn->smb2.max_trans) {
77 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
80 if (req->compat_chain_fsp) {
81 /* skip check */
82 } else if (in_file_id_persistent != in_file_id_volatile) {
83 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
86 subreq = smbd_smb2_notify_send(req,
87 req->sconn->smb2.event_ctx,
88 req,
89 in_flags,
90 in_output_buffer_length,
91 in_file_id_volatile,
92 in_completion_filter);
93 if (subreq == NULL) {
94 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
96 tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
98 return smbd_smb2_request_pending_queue(req, subreq);
101 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
103 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
104 struct smbd_smb2_request);
105 int i = req->current_idx;
106 uint8_t *outhdr;
107 DATA_BLOB outbody;
108 DATA_BLOB outdyn;
109 uint16_t out_output_buffer_offset;
110 DATA_BLOB out_output_buffer = data_blob_null;
111 NTSTATUS status;
112 NTSTATUS error; /* transport error */
114 if (req->cancelled) {
115 struct smbd_smb2_notify_state *state = tevent_req_data(subreq,
116 struct smbd_smb2_notify_state);
117 const uint8_t *inhdr = (const uint8_t *)req->in.vector[i].iov_base;
118 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
120 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
121 (unsigned long long)mid ));
122 error = smbd_smb2_request_error(req, NT_STATUS_CANCELLED);
123 if (!NT_STATUS_IS_OK(error)) {
124 smbd_server_connection_terminate(req->sconn,
125 nt_errstr(error));
126 return;
128 TALLOC_FREE(state->im);
129 return;
132 status = smbd_smb2_notify_recv(subreq,
133 req,
134 &out_output_buffer);
135 TALLOC_FREE(subreq);
136 if (!NT_STATUS_IS_OK(status)) {
137 error = smbd_smb2_request_error(req, status);
138 if (!NT_STATUS_IS_OK(error)) {
139 smbd_server_connection_terminate(req->sconn,
140 nt_errstr(error));
141 return;
143 return;
146 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
148 outhdr = (uint8_t *)req->out.vector[i].iov_base;
150 outbody = data_blob_talloc(req->out.vector, NULL, 0x08);
151 if (outbody.data == NULL) {
152 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
153 if (!NT_STATUS_IS_OK(error)) {
154 smbd_server_connection_terminate(req->sconn,
155 nt_errstr(error));
156 return;
158 return;
161 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
162 SSVAL(outbody.data, 0x02,
163 out_output_buffer_offset); /* output buffer offset */
164 SIVAL(outbody.data, 0x04,
165 out_output_buffer.length); /* output buffer length */
167 outdyn = out_output_buffer;
169 error = smbd_smb2_request_done(req, outbody, &outdyn);
170 if (!NT_STATUS_IS_OK(error)) {
171 smbd_server_connection_terminate(req->sconn,
172 nt_errstr(error));
173 return;
177 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
178 NTSTATUS error_code,
179 uint8_t *buf, size_t len);
180 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
181 struct tevent_immediate *im,
182 void *private_data);
183 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
185 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
186 struct tevent_context *ev,
187 struct smbd_smb2_request *smb2req,
188 uint16_t in_flags,
189 uint32_t in_output_buffer_length,
190 uint64_t in_file_id_volatile,
191 uint64_t in_completion_filter)
193 struct tevent_req *req;
194 struct smbd_smb2_notify_state *state;
195 struct smb_request *smbreq;
196 connection_struct *conn = smb2req->tcon->compat_conn;
197 files_struct *fsp;
198 bool recursive = (in_flags & 0x0001) ? true : false;
199 NTSTATUS status;
201 req = tevent_req_create(mem_ctx, &state,
202 struct smbd_smb2_notify_state);
203 if (req == NULL) {
204 return NULL;
206 state->smb2req = smb2req;
207 state->status = NT_STATUS_INTERNAL_ERROR;
208 state->out_output_buffer = data_blob_null;
209 state->im = NULL;
211 DEBUG(10,("smbd_smb2_notify_send: file_id[0x%016llX]\n",
212 (unsigned long long)in_file_id_volatile));
214 smbreq = smbd_smb2_fake_smb_request(smb2req);
215 if (tevent_req_nomem(smbreq, req)) {
216 return tevent_req_post(req, ev);
219 state->smbreq = smbreq;
220 smbreq->async_priv = (void *)req;
222 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
223 if (fsp == NULL) {
224 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
225 return tevent_req_post(req, ev);
227 if (conn != fsp->conn) {
228 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
229 return tevent_req_post(req, ev);
231 if (smb2req->session->vuid != fsp->vuid) {
232 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
233 return tevent_req_post(req, ev);
237 char *filter_string;
239 filter_string = notify_filter_string(NULL, in_completion_filter);
240 if (tevent_req_nomem(filter_string, req)) {
241 return tevent_req_post(req, ev);
244 DEBUG(3,("smbd_smb2_notify_send: notify change "
245 "called on %s, filter = %s, recursive = %d\n",
246 fsp_str_dbg(fsp), filter_string, recursive));
248 TALLOC_FREE(filter_string);
251 if ((!fsp->is_directory) || (conn != fsp->conn)) {
252 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
253 return tevent_req_post(req, ev);
256 if (fsp->notify == NULL) {
258 status = change_notify_create(fsp,
259 in_completion_filter,
260 recursive);
261 if (!NT_STATUS_IS_OK(status)) {
262 DEBUG(10, ("change_notify_create returned %s\n",
263 nt_errstr(status)));
264 tevent_req_nterror(req, status);
265 return tevent_req_post(req, ev);
269 if (fsp->notify->num_changes != 0) {
272 * We've got changes pending, respond immediately
276 * TODO: write a torture test to check the filtering behaviour
277 * here.
280 change_notify_reply(smbreq,
281 NT_STATUS_OK,
282 in_output_buffer_length,
283 fsp->notify,
284 smbd_smb2_notify_reply);
287 * change_notify_reply() above has independently
288 * called tevent_req_done().
290 return tevent_req_post(req, ev);
293 state->im = tevent_create_immediate(state);
294 if (tevent_req_nomem(state->im, req)) {
295 return tevent_req_post(req, ev);
299 * No changes pending, queue the request
302 status = change_notify_add_request(smbreq,
303 in_output_buffer_length,
304 in_completion_filter,
305 recursive, fsp,
306 smbd_smb2_notify_reply);
307 if (!NT_STATUS_IS_OK(status)) {
308 tevent_req_nterror(req, status);
309 return tevent_req_post(req, ev);
312 /* allow this request to be canceled */
313 tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
315 return req;
318 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
319 NTSTATUS error_code,
320 uint8_t *buf, size_t len)
322 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
323 struct tevent_req);
324 struct smbd_smb2_notify_state *state = tevent_req_data(req,
325 struct smbd_smb2_notify_state);
327 state->status = error_code;
328 if (!NT_STATUS_IS_OK(error_code)) {
329 /* nothing */
330 } else if (len == 0) {
331 state->status = STATUS_NOTIFY_ENUM_DIR;
332 } else {
333 state->out_output_buffer = data_blob_talloc(state, buf, len);
334 if (state->out_output_buffer.data == NULL) {
335 state->status = NT_STATUS_NO_MEMORY;
339 if (state->im == NULL) {
340 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
341 return;
345 * if this is called async, we need to go via an immediate event
346 * because the caller replies on the smb_request (a child of req
347 * being arround after calling this function
349 tevent_schedule_immediate(state->im,
350 state->smb2req->sconn->smb2.event_ctx,
351 smbd_smb2_notify_reply_trigger,
352 req);
355 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
356 struct tevent_immediate *im,
357 void *private_data)
359 struct tevent_req *req = talloc_get_type_abort(private_data,
360 struct tevent_req);
361 struct smbd_smb2_notify_state *state = tevent_req_data(req,
362 struct smbd_smb2_notify_state);
364 if (!NT_STATUS_IS_OK(state->status)) {
365 tevent_req_nterror(req, state->status);
366 return;
369 tevent_req_done(req);
372 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
374 struct smbd_smb2_notify_state *state = tevent_req_data(req,
375 struct smbd_smb2_notify_state);
377 smbd_notify_cancel_by_smbreq(state->smbreq);
379 state->smb2req->cancelled = true;
380 tevent_req_done(req);
381 return true;
384 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
385 TALLOC_CTX *mem_ctx,
386 DATA_BLOB *out_output_buffer)
388 NTSTATUS status;
389 struct smbd_smb2_notify_state *state = tevent_req_data(req,
390 struct smbd_smb2_notify_state);
392 if (tevent_req_is_nterror(req, &status)) {
393 tevent_req_received(req);
394 return status;
397 *out_output_buffer = state->out_output_buffer;
398 talloc_steal(mem_ctx, out_output_buffer->data);
400 tevent_req_received(req);
401 return NT_STATUS_OK;