librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_notify.c
blob81aa6152cc5b9a44fad8bdcc9881b825fad87fc2
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 NTSTATUS status;
32 DATA_BLOB out_output_buffer;
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);
46 static void smbd_smb2_request_notify_done(struct tevent_req *subreq);
47 NTSTATUS smbd_smb2_request_process_notify(struct smbd_smb2_request *req)
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;
59 status = smbd_smb2_request_verify_sizes(req, 0x20);
60 if (!NT_STATUS_IS_OK(status)) {
61 return smbd_smb2_request_error(req, status);
63 inbody = SMBD_SMB2_IN_BODY_PTR(req);
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);
72 * 0x00010000 is what Windows 7 uses,
73 * Windows 2008 uses 0x00080000
75 if (in_output_buffer_length > req->sconn->smb2.max_trans) {
76 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
79 status = smbd_smb2_request_verify_creditcharge(req,
80 in_output_buffer_length);
82 if (!NT_STATUS_IS_OK(status)) {
83 return smbd_smb2_request_error(req, status);
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);
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);
99 tevent_req_set_callback(subreq, smbd_smb2_request_notify_done, req);
101 return smbd_smb2_request_pending_queue(req, subreq, 500);
104 static void smbd_smb2_request_notify_done(struct tevent_req *subreq)
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 */
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;
126 return;
129 out_output_buffer_offset = SMB2_HDR_BODY + 0x08;
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;
139 return;
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 */
148 outdyn = out_output_buffer;
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;
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);
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)
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;
178 req = tevent_req_create(mem_ctx, &state,
179 struct smbd_smb2_notify_state);
180 if (req == NULL) {
181 return NULL;
183 state->smb2req = smb2req;
184 state->status = NT_STATUS_INTERNAL_ERROR;
185 state->out_output_buffer = data_blob_null;
187 DEBUG(10,("smbd_smb2_notify_send: %s - %s\n",
188 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
190 smbreq = smbd_smb2_fake_smb_request(smb2req);
191 if (tevent_req_nomem(smbreq, req)) {
192 return tevent_req_post(req, ev);
195 state->smbreq = smbreq;
196 smbreq->async_priv = (void *)req;
199 char *filter_string;
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);
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));
210 TALLOC_FREE(filter_string);
213 if ((!fsp->is_directory) || (conn != fsp->conn)) {
214 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
215 return tevent_req_post(req, ev);
218 if (fsp->notify == NULL) {
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);
231 if (change_notify_fsp_has_changes(fsp)) {
234 * We've got changes pending, respond immediately
238 * TODO: write a torture test to check the filtering behaviour
239 * here.
242 change_notify_reply(smbreq,
243 NT_STATUS_OK,
244 in_output_buffer_length,
245 fsp->notify,
246 smbd_smb2_notify_reply);
249 * change_notify_reply() above has independently
250 * called tevent_req_done().
252 return tevent_req_post(req, ev);
256 * No changes pending, queue the request
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);
269 /* allow this request to be canceled */
270 tevent_req_set_cancel_fn(req, smbd_smb2_notify_cancel);
272 return req;
275 static void smbd_smb2_notify_reply(struct smb_request *smbreq,
276 NTSTATUS error_code,
277 uint8_t *buf, size_t len)
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);
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;
296 tevent_req_defer_callback(req, state->smb2req->sconn->ev_ctx);
298 if (!NT_STATUS_IS_OK(state->status)) {
299 tevent_req_nterror(req, state->status);
300 return;
303 tevent_req_done(req);
306 static bool smbd_smb2_notify_cancel(struct tevent_req *req)
308 struct smbd_smb2_notify_state *state = tevent_req_data(req,
309 struct smbd_smb2_notify_state);
311 smbd_notify_cancel_by_smbreq(state->smbreq);
313 return true;
316 static NTSTATUS smbd_smb2_notify_recv(struct tevent_req *req,
317 TALLOC_CTX *mem_ctx,
318 DATA_BLOB *out_output_buffer)
320 NTSTATUS status;
321 struct smbd_smb2_notify_state *state = tevent_req_data(req,
322 struct smbd_smb2_notify_state);
324 if (tevent_req_is_nterror(req, &status)) {
325 tevent_req_received(req);
326 return status;
329 *out_output_buffer = state->out_output_buffer;
330 talloc_steal(mem_ctx, out_output_buffer->data);
332 tevent_req_received(req);
333 return NT_STATUS_OK;