s3:smb2_server: use xconn->smb2.server.max_* instead of sconn->smb2.max_*
[Samba.git] / source3 / smbd / smb2_notify.c
blob3e33d42bcee218ed149b958955496c4faba8842e
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 bool has_request;
32 bool skip_reply;
33 NTSTATUS status;
34 DATA_BLOB out_output_buffer;
37 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct smbd_smb2_request *smb2req,
40 struct files_struct *in_fsp,
41 uint16_t in_flags,
42 uint32_t in_output_buffer_length,
43 uint64_t in_completion_filter);
44 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
45 TALLOC_CTX *mem_ctx,
46 DATA_BLOB *out_output_buffer);
48 static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
49 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
51 struct smbXsrv_connection *xconn = req->sconn->conn;
52 NTSTATUS status;
53 const uint8_t *inbody;
54 uint16_t in_flags;
55 uint32_t in_output_buffer_length;
56 uint64_t in_file_id_persistent;
57 uint64_t in_file_id_volatile;
58 struct files_struct *in_fsp;
59 uint64_t in_completion_filter;
60 struct tevent_req *subreq;
62 status = smbd_smb2_request_verify_sizes(req, 0x20);
63 if (!NT_STATUS_IS_OK(status)) {
64 return smbd_smb2_request_error(req, status);
66 inbody = SMBD_SMB2_IN_BODY_PTR(req);
68 in_flags = SVAL(inbody, 0x02);
69 in_output_buffer_length = IVAL(inbody, 0x04);
70 in_file_id_persistent = BVAL(inbody, 0x08);
71 in_file_id_volatile = BVAL(inbody, 0x10);
72 in_completion_filter = IVAL(inbody, 0x18);
75 * 0x00010000 is what Windows 7 uses,
76 * Windows 2008 uses 0x00080000
78 if (in_output_buffer_length > xconn->smb2.server.max_trans) {
79 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
82 status = smbd_smb2_request_verify_creditcharge(req,
83 in_output_buffer_length);
85 if (!NT_STATUS_IS_OK(status)) {
86 return smbd_smb2_request_error(req, status);
89 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
90 if (in_fsp == NULL) {
91 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
94 subreq = smbd_smb2_notify_send(req, req->sconn->ev_ctx,
95 req, in_fsp,
96 in_flags,
97 in_output_buffer_length,
98 in_completion_filter);
99 if (subreq == NULL) {
100 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
102 tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
104 return smbd_smb2_request_pending_queue(req, subreq, 500);
107 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
109 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
110 struct smbd_smb2_request);
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 */
118 status = smbd_smb2_notify_recv(subreq,
119 req,
120 &out_output_buffer);
121 TALLOC_FREE(subreq);
122 if (!NT_STATUS_IS_OK(status)) {
123 error = smbd_smb2_request_error(req, status);
124 if (!NT_STATUS_IS_OK(error)) {
125 smbd_server_connection_terminate(req->sconn,
126 nt_errstr(error));
127 return;
129 return;
132 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
134 outbody = smbd_smb2_generate_outbody(req, 0x08);
135 if (outbody.data == NULL) {
136 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
137 if (!NT_STATUS_IS_OK(error)) {
138 smbd_server_connection_terminate(req->sconn,
139 nt_errstr(error));
140 return;
142 return;
145 SSVAL(outbody.data, 0x00, 0x08 + 1); /* struct size */
146 SSVAL(outbody.data, 0x02,
147 out_output_buffer_offset); /* output buffer offset */
148 SIVAL(outbody.data, 0x04,
149 out_output_buffer.length); /* output buffer length */
151 outdyn = out_output_buffer;
153 error = smbd_smb2_request_done(req, outbody, &outdyn);
154 if (!NT_STATUS_IS_OK(error)) {
155 smbd_server_connection_terminate(req->sconn,
156 nt_errstr(error));
157 return;
161 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
162 NTSTATUS error_code,
163 uint8_t *buf, size_t len);
164 static bool smbd_smb2_notify_cancel(struct tevent_req *req);
166 static int smbd_smb2_notify_state_destructor(struct smbd_smb2_notify_state *state)
168 if (!state->has_request) {
169 return 0;
172 state->skip_reply = true;
173 smbd_notify_cancel_by_smbreq(state->smbreq);
174 return 0;
177 static int smbd_smb2_notify_smbreq_destructor(struct smb_request *smbreq)
179 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
180 struct tevent_req);
181 struct smbd_smb2_notify_state *state = tevent_req_data(req,
182 struct smbd_smb2_notify_state);
185 * Our temporary parent from change_notify_add_request()
186 * goes away.
188 state->has_request = false;
191 * move it back to its original parent,
192 * which means we no longer need the destructor
193 * to protect it.
195 talloc_steal(smbreq->smb2req, smbreq);
196 talloc_set_destructor(smbreq, NULL);
199 * We want to keep smbreq!
201 return -1;
204 static struct tevent_req *smbd_smb2_notify_send(TALLOC_CTX *mem_ctx,
205 struct tevent_context *ev,
206 struct smbd_smb2_request *smb2req,
207 struct files_struct *fsp,
208 uint16_t in_flags,
209 uint32_t in_output_buffer_length,
210 uint64_t in_completion_filter)
212 struct tevent_req *req;
213 struct smbd_smb2_notify_state *state;
214 struct smb_request *smbreq;
215 connection_struct *conn = smb2req->tcon->compat;
216 bool recursive = (in_flags & SMB2_WATCH_TREE) ? true : false;
217 NTSTATUS status;
219 req = tevent_req_create(mem_ctx, &state,
220 struct smbd_smb2_notify_state);
221 if (req == NULL) {
222 return NULL;
224 state->smb2req = smb2req;
225 state->status = NT_STATUS_INTERNAL_ERROR;
226 state->out_output_buffer = data_blob_null;
227 talloc_set_destructor(state, smbd_smb2_notify_state_destructor);
229 DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
230 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
232 smbreq = smbd_smb2_fake_smb_request(smb2req);
233 if (tevent_req_nomem(smbreq, req)) {
234 return tevent_req_post(req, ev);
237 state->smbreq = smbreq;
238 smbreq->async_priv = (void *)req;
240 if (DEBUGLEVEL >= 3) {
241 char *filter_string;
243 filter_string = notify_filter_string(NULL, in_completion_filter);
244 if (tevent_req_nomem(filter_string, req)) {
245 return tevent_req_post(req, ev);
248 DEBUG(3,("smbd_smb2_notify_send: notify change "
249 "called on %s, filter = %s, recursive = %d\n",
250 fsp_str_dbg(fsp), filter_string, recursive));
252 TALLOC_FREE(filter_string);
255 if ((!fsp->is_directory) || (conn != fsp->conn)) {
256 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
257 return tevent_req_post(req, ev);
260 if (fsp->notify == NULL) {
262 status = change_notify_create(fsp,
263 in_completion_filter,
264 recursive);
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(10, ("change_notify_create returned %s\n",
267 nt_errstr(status)));
268 tevent_req_nterror(req, status);
269 return tevent_req_post(req, ev);
273 if (change_notify_fsp_has_changes(fsp)) {
276 * We've got changes pending, respond immediately
280 * TODO: write a torture test to check the filtering behaviour
281 * here.
284 change_notify_reply(smbreq,
285 NT_STATUS_OK,
286 in_output_buffer_length,
287 fsp->notify,
288 smbd_smb2_notify_reply);
291 * change_notify_reply() above has independently
292 * called tevent_req_done().
294 return tevent_req_post(req, ev);
298 * No changes pending, queue the request
301 status = change_notify_add_request(smbreq,
302 in_output_buffer_length,
303 in_completion_filter,
304 recursive, fsp,
305 smbd_smb2_notify_reply);
306 if (!NT_STATUS_IS_OK(status)) {
307 tevent_req_nterror(req, status);
308 return tevent_req_post(req, ev);
312 * This is a HACK!
314 * change_notify_add_request() talloc_moves()
315 * smbreq away from us, so we need a destructor
316 * which moves it back at the end.
318 state->has_request = true;
319 talloc_set_destructor(smbreq, smbd_smb2_notify_smbreq_destructor);
321 /* allow this request to be canceled */
322 tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
324 return req;
327 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
328 NTSTATUS error_code,
329 uint8_t *buf, size_t len)
331 struct tevent_req *req = talloc_get_type_abort(smbreq->async_priv,
332 struct tevent_req);
333 struct smbd_smb2_notify_state *state = tevent_req_data(req,
334 struct smbd_smb2_notify_state);
336 if (state->skip_reply) {
337 return;
340 state->status = error_code;
341 if (!NT_STATUS_IS_OK(error_code)) {
342 /* nothing */
343 } else if (len == 0) {
344 state->status = STATUS_NOTIFY_ENUM_DIR;
345 } else {
346 state->out_output_buffer = data_blob_talloc(state, buf, len);
347 if (state->out_output_buffer.data == NULL) {
348 state->status = NT_STATUS_NO_MEMORY;
352 tevent_req_defer_callback(req, state->smb2req->sconn->ev_ctx);
354 if (!NT_STATUS_IS_OK(state->status)) {
355 tevent_req_nterror(req, state->status);
356 return;
359 tevent_req_done(req);
362 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
364 struct smbd_smb2_notify_state *state = tevent_req_data(req,
365 struct smbd_smb2_notify_state);
367 smbd_notify_cancel_by_smbreq(state->smbreq);
369 return true;
372 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
373 TALLOC_CTX *mem_ctx,
374 DATA_BLOB *out_output_buffer)
376 NTSTATUS status;
377 struct smbd_smb2_notify_state *state = tevent_req_data(req,
378 struct smbd_smb2_notify_state);
380 if (tevent_req_is_nterror(req, &status)) {
381 tevent_req_received(req);
382 return status;
385 *out_output_buffer = state->out_output_buffer;
386 talloc_steal(mem_ctx, out_output_buffer->data);
388 tevent_req_received(req);
389 return NT_STATUS_OK;