2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "lib/util/tevent_ntstatus.h"
23 #include "smb_common.h"
24 #include "smbXcli_base.h"
25 #include "librpc/gen_ndr/ndr_notify.h"
27 struct smb2cli_notify_state
{
30 struct iovec
*recv_iov
;
34 struct tevent_req
*subreq
;
35 struct tevent_req
*timeout_subreq
;
38 static void smb2cli_notify_done(struct tevent_req
*subreq
);
39 static void smb2cli_notify_timedout(struct tevent_req
*subreq
);
40 static bool smb2cli_notify_cancel(struct tevent_req
*req
);
42 struct tevent_req
*smb2cli_notify_send(TALLOC_CTX
*mem_ctx
,
43 struct tevent_context
*ev
,
44 struct smbXcli_conn
*conn
,
45 uint32_t timeout_msec
,
46 struct smbXcli_session
*session
,
47 struct smbXcli_tcon
*tcon
,
48 uint32_t output_buffer_length
,
49 uint64_t fid_persistent
,
50 uint64_t fid_volatile
,
51 uint32_t completion_filter
,
54 struct tevent_req
*req
;
55 struct smb2cli_notify_state
*state
;
59 req
= tevent_req_create(mem_ctx
, &state
,
60 struct smb2cli_notify_state
);
65 watch_tree
= recursive
? SMB2_WATCH_TREE
: 0;
68 SSVAL(fixed
, 2, watch_tree
);
69 SIVAL(fixed
, 4, output_buffer_length
);
70 SBVAL(fixed
, 8, fid_persistent
);
71 SBVAL(fixed
, 16, fid_volatile
);
72 SIVAL(fixed
, 24, completion_filter
);
73 SIVAL(fixed
, 28, 0); /* reserved */
75 state
->subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_NOTIFY
,
80 state
->fixed
, sizeof(state
->fixed
),
83 if (tevent_req_nomem(state
->subreq
, req
)) {
84 return tevent_req_post(req
, ev
);
86 tevent_req_set_callback(state
->subreq
, smb2cli_notify_done
, req
);
88 if (timeout_msec
!= 0) {
89 state
->timeout_subreq
= tevent_wakeup_send(
90 state
, ev
, timeval_current_ofs_msec(timeout_msec
));
91 if (tevent_req_nomem(state
->timeout_subreq
, req
)) {
92 return tevent_req_post(req
, ev
);
94 tevent_req_set_callback(
95 state
->timeout_subreq
, smb2cli_notify_timedout
, req
);
98 tevent_req_set_cancel_fn(req
, smb2cli_notify_cancel
);
103 static bool smb2cli_notify_cancel(struct tevent_req
*req
)
105 struct smb2cli_notify_state
*state
= tevent_req_data(
106 req
, struct smb2cli_notify_state
);
109 TALLOC_FREE(state
->timeout_subreq
);
111 ok
= tevent_req_cancel(state
->subreq
);
115 static void smb2cli_notify_timedout(struct tevent_req
*subreq
)
117 struct tevent_req
*req
= tevent_req_callback_data(
118 subreq
, struct tevent_req
);
119 struct smb2cli_notify_state
*state
= tevent_req_data(
120 req
, struct smb2cli_notify_state
);
123 ok
= tevent_wakeup_recv(subreq
);
125 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
129 ok
= tevent_req_cancel(state
->subreq
);
131 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
136 static void smb2cli_notify_done(struct tevent_req
*subreq
)
138 struct tevent_req
*req
= tevent_req_callback_data(
139 subreq
, struct tevent_req
);
140 struct smb2cli_notify_state
*state
= tevent_req_data(
141 req
, struct smb2cli_notify_state
);
144 uint16_t data_offset
;
145 static const struct smb2cli_req_expected_response expected
[] = {
147 .status
= NT_STATUS_OK
,
152 status
= smb2cli_req_recv(subreq
, state
, &iov
,
153 expected
, ARRAY_SIZE(expected
));
156 if (NT_STATUS_EQUAL(status
, NT_STATUS_CANCELLED
)) {
157 status
= NT_STATUS_IO_TIMEOUT
;
159 if (tevent_req_nterror(req
, status
)) {
163 data_offset
= SVAL(iov
[1].iov_base
, 2);
164 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
166 if ((data_offset
!= SMB2_HDR_BODY
+ 8) ||
167 (state
->data_length
> iov
[2].iov_len
)) {
168 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
172 state
->recv_iov
= iov
;
173 state
->data
= (uint8_t *)iov
[2].iov_base
;
174 tevent_req_done(req
);
177 NTSTATUS
smb2cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
178 uint8_t **data
, uint32_t *data_length
)
180 struct smb2cli_notify_state
*state
= tevent_req_data(
181 req
, struct smb2cli_notify_state
);
184 if (tevent_req_is_nterror(req
, &status
)) {
187 talloc_steal(mem_ctx
, state
->recv_iov
);
188 *data_length
= state
->data_length
;
193 NTSTATUS
smb2cli_notify(struct smbXcli_conn
*conn
,
194 uint32_t timeout_msec
,
195 struct smbXcli_session
*session
,
196 struct smbXcli_tcon
*tcon
,
197 uint32_t output_buffer_length
,
198 uint64_t fid_persistent
,
199 uint64_t fid_volatile
,
200 uint32_t completion_filter
,
204 uint32_t *data_length
)
206 TALLOC_CTX
*frame
= talloc_stackframe();
207 struct tevent_context
*ev
;
208 struct tevent_req
*req
;
209 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
211 if (smbXcli_conn_has_async_calls(conn
)) {
213 * Can't use sync call while an async call is in flight
215 status
= NT_STATUS_INVALID_PARAMETER
;
218 ev
= samba_tevent_context_init(frame
);
222 req
= smb2cli_notify_send(frame
, ev
, conn
, timeout_msec
,
223 session
, tcon
, output_buffer_length
,
224 fid_persistent
, fid_volatile
,
225 completion_filter
, recursive
);
229 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
232 status
= smb2cli_notify_recv(req
, mem_ctx
, data
, data_length
);