s3: Fix the vfs_solarisacl module to compile.
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_notify.c
blob886bc89e99cd0179cb839409d8a6a60ecb796a7b
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 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);
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 uint16_t in_flags;
53 uint32_t in_output_buffer_length;
54 uint64_t in_file_id_persistent;
55 uint64_t in_file_id_volatile;
56 struct files_struct *in_fsp;
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 = SMBD_SMB2_IN_BODY_PTR(req);
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 status = smbd_smb2_request_verify_creditcharge(req,
81 in_output_buffer_length);
83 if (!NT_STATUS_IS_OK(status)) {
84 return smbd_smb2_request_error(req, status);
87 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
88 if (in_fsp == NULL) {
89 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
92 subreq = smbd_smb2_notify_send(req, req->sconn->ev_ctx,
93 req, in_fsp,
94 in_flags,
95 in_output_buffer_length,
96 in_completion_filter);
97 if (subreq == NULL) {
98 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
100 tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
102 return smbd_smb2_request_pending_queue(req, subreq, 500);
105 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
107 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
108 struct smbd_smb2_request);
109 DATA_BLOB outbody;
110 DATA_BLOB outdyn;
111 uint16_t out_output_buffer_offset;
112 DATA_BLOB out_output_buffer = data_blob_null;
113 NTSTATUS status;
114 NTSTATUS error; /* transport error */
116 if (req->cancelled) {
117 struct smbd_smb2_notify_state *state = tevent_req_data(subreq,
118 struct smbd_smb2_notify_state);
119 const uint8_t *inhdr = SMBD_SMB2_IN_HDR_PTR(req);
120 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
122 DEBUG(10,("smbd_smb2_request_notify_done: cancelled mid %llu\n",
123 (unsigned long long)mid ));
124 error = smbd_smb2_request_error(req, NT_STATUS_CANCELLED);
125 if (!NT_STATUS_IS_OK(error)) {
126 smbd_server_connection_terminate(req->sconn,
127 nt_errstr(error));
128 return;
130 TALLOC_FREE(state->im);
131 return;
134 status = smbd_smb2_notify_recv(subreq,
135 req,
136 &out_output_buffer);
137 TALLOC_FREE(subreq);
138 if (!NT_STATUS_IS_OK(status)) {
139 error = smbd_smb2_request_error(req, status);
140 if (!NT_STATUS_IS_OK(error)) {
141 smbd_server_connection_terminate(req->sconn,
142 nt_errstr(error));
143 return;
145 return;
148 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
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 struct files_struct *fsp,
189 uint16_t in_flags,
190 uint32_t in_output_buffer_length,
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;
197 bool recursive = (in_flags & SMB2_WATCH_TREE) ? true : false;
198 NTSTATUS status;
200 req = tevent_req_create(mem_ctx, &state,
201 struct smbd_smb2_notify_state);
202 if (req == NULL) {
203 return NULL;
205 state->smb2req = smb2req;
206 state->status = NT_STATUS_INTERNAL_ERROR;
207 state->out_output_buffer = data_blob_null;
208 state->im = NULL;
210 DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
211 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
213 smbreq = smbd_smb2_fake_smb_request(smb2req);
214 if (tevent_req_nomem(smbreq, req)) {
215 return tevent_req_post(req, ev);
218 state->smbreq = smbreq;
219 smbreq->async_priv = (void *)req;
222 char *filter_string;
224 filter_string = notify_filter_string(NULL, in_completion_filter);
225 if (tevent_req_nomem(filter_string, req)) {
226 return tevent_req_post(req, ev);
229 DEBUG(3,("smbd_smb2_notify_send: notify change "
230 "called on %s, filter = %s, recursive = %d\n",
231 fsp_str_dbg(fsp), filter_string, recursive));
233 TALLOC_FREE(filter_string);
236 if ((!fsp->is_directory) || (conn != fsp->conn)) {
237 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
238 return tevent_req_post(req, ev);
241 if (fsp->notify == NULL) {
243 status = change_notify_create(fsp,
244 in_completion_filter,
245 recursive);
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(10, ("change_notify_create returned %s\n",
248 nt_errstr(status)));
249 tevent_req_nterror(req, status);
250 return tevent_req_post(req, ev);
254 if (change_notify_fsp_has_changes(fsp)) {
257 * We've got changes pending, respond immediately
261 * TODO: write a torture test to check the filtering behaviour
262 * here.
265 change_notify_reply(smbreq,
266 NT_STATUS_OK,
267 in_output_buffer_length,
268 fsp->notify,
269 smbd_smb2_notify_reply);
272 * change_notify_reply() above has independently
273 * called tevent_req_done().
275 return tevent_req_post(req, ev);
278 state->im = tevent_create_immediate(state);
279 if (tevent_req_nomem(state->im, req)) {
280 return tevent_req_post(req, ev);
284 * No changes pending, queue the request
287 status = change_notify_add_request(smbreq,
288 in_output_buffer_length,
289 in_completion_filter,
290 recursive, fsp,
291 smbd_smb2_notify_reply);
292 if (!NT_STATUS_IS_OK(status)) {
293 tevent_req_nterror(req, status);
294 return tevent_req_post(req, ev);
297 /* allow this request to be canceled */
298 tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
300 return req;
303 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
304 NTSTATUS error_code,
305 uint8_t *buf, size_t len)
307 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
308 struct tevent_req);
309 struct smbd_smb2_notify_state *state = tevent_req_data(req,
310 struct smbd_smb2_notify_state);
312 state->status = error_code;
313 if (!NT_STATUS_IS_OK(error_code)) {
314 /* nothing */
315 } else if (len == 0) {
316 state->status = STATUS_NOTIFY_ENUM_DIR;
317 } else {
318 state->out_output_buffer = data_blob_talloc(state, buf, len);
319 if (state->out_output_buffer.data == NULL) {
320 state->status = NT_STATUS_NO_MEMORY;
324 if (state->im == NULL) {
325 smbd_smb2_notify_reply_trigger(NULL, NULL, req);
326 return;
330 * if this is called async, we need to go via an immediate event
331 * because the caller replies on the smb_request (a child of req
332 * being arround after calling this function
334 tevent_schedule_immediate(state->im,
335 state->smb2req->sconn->ev_ctx,
336 smbd_smb2_notify_reply_trigger,
337 req);
340 static void smbd_smb2_notify_reply_trigger(struct tevent_context *ctx,
341 struct tevent_immediate *im,
342 void *private_data)
344 struct tevent_req *req = talloc_get_type_abort(private_data,
345 struct tevent_req);
346 struct smbd_smb2_notify_state *state = tevent_req_data(req,
347 struct smbd_smb2_notify_state);
349 if (!NT_STATUS_IS_OK(state->status)) {
350 tevent_req_nterror(req, state->status);
351 return;
354 tevent_req_done(req);
357 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
359 struct smbd_smb2_notify_state *state = tevent_req_data(req,
360 struct smbd_smb2_notify_state);
362 smbd_notify_cancel_by_smbreq(state->smbreq);
364 state->smb2req->cancelled = true;
365 tevent_req_done(req);
366 return true;
369 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
370 TALLOC_CTX *mem_ctx,
371 DATA_BLOB *out_output_buffer)
373 NTSTATUS status;
374 struct smbd_smb2_notify_state *state = tevent_req_data(req,
375 struct smbd_smb2_notify_state);
377 if (tevent_req_is_nterror(req, &status)) {
378 tevent_req_received(req);
379 return status;
382 *out_output_buffer = state->out_output_buffer;
383 talloc_steal(mem_ctx, out_output_buffer->data);
385 tevent_req_received(req);
386 return NT_STATUS_OK;