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
;
37 static void smb2cli_notify_done(struct tevent_req
*subreq
);
38 static void smb2cli_notify_timedout(struct tevent_req
*subreq
);
40 struct tevent_req
*smb2cli_notify_send(TALLOC_CTX
*mem_ctx
,
41 struct tevent_context
*ev
,
42 struct smbXcli_conn
*conn
,
43 uint32_t timeout_msec
,
44 struct smbXcli_session
*session
,
45 struct smbXcli_tcon
*tcon
,
46 uint32_t output_buffer_length
,
47 uint64_t fid_persistent
,
48 uint64_t fid_volatile
,
49 uint32_t completion_filter
,
52 struct tevent_req
*req
, *subreq
;
53 struct smb2cli_notify_state
*state
;
57 req
= tevent_req_create(mem_ctx
, &state
,
58 struct smb2cli_notify_state
);
63 watch_tree
= recursive
? SMB2_WATCH_TREE
: 0;
66 SSVAL(fixed
, 2, watch_tree
);
67 SIVAL(fixed
, 4, output_buffer_length
);
68 SBVAL(fixed
, 8, fid_persistent
);
69 SBVAL(fixed
, 16, fid_volatile
);
70 SIVAL(fixed
, 24, completion_filter
);
71 SIVAL(fixed
, 28, 0); /* reserved */
73 state
->subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_NOTIFY
,
78 state
->fixed
, sizeof(state
->fixed
),
81 if (tevent_req_nomem(state
->subreq
, req
)) {
82 return tevent_req_post(req
, ev
);
84 tevent_req_set_callback(state
->subreq
, smb2cli_notify_done
, req
);
86 subreq
= tevent_wakeup_send(state
, ev
,
87 timeval_current_ofs_msec(timeout_msec
));
88 if (tevent_req_nomem(subreq
, req
)) {
89 return tevent_req_post(req
, ev
);
91 tevent_req_set_callback(subreq
, smb2cli_notify_timedout
, req
);
96 static void smb2cli_notify_timedout(struct tevent_req
*subreq
)
98 struct tevent_req
*req
= tevent_req_callback_data(
99 subreq
, struct tevent_req
);
100 struct smb2cli_notify_state
*state
= tevent_req_data(
101 req
, struct smb2cli_notify_state
);
104 ok
= tevent_wakeup_recv(subreq
);
106 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
110 ok
= tevent_req_cancel(state
->subreq
);
112 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
117 static void smb2cli_notify_done(struct tevent_req
*subreq
)
119 struct tevent_req
*req
= tevent_req_callback_data(
120 subreq
, struct tevent_req
);
121 struct smb2cli_notify_state
*state
= tevent_req_data(
122 req
, struct smb2cli_notify_state
);
125 uint16_t data_offset
;
126 static const struct smb2cli_req_expected_response expected
[] = {
128 .status
= NT_STATUS_OK
,
133 status
= smb2cli_req_recv(subreq
, state
, &iov
,
134 expected
, ARRAY_SIZE(expected
));
137 if (NT_STATUS_EQUAL(status
, NT_STATUS_CANCELLED
)) {
138 status
= NT_STATUS_IO_TIMEOUT
;
140 if (tevent_req_nterror(req
, status
)) {
144 data_offset
= SVAL(iov
[1].iov_base
, 2);
145 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
147 if ((data_offset
!= SMB2_HDR_BODY
+ 8) ||
148 (state
->data_length
> iov
[2].iov_len
)) {
149 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
153 state
->recv_iov
= iov
;
154 state
->data
= (uint8_t *)iov
[2].iov_base
;
155 tevent_req_done(req
);
158 NTSTATUS
smb2cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
159 uint8_t **data
, uint32_t *data_length
)
161 struct smb2cli_notify_state
*state
= tevent_req_data(
162 req
, struct smb2cli_notify_state
);
165 if (tevent_req_is_nterror(req
, &status
)) {
168 talloc_steal(mem_ctx
, state
->recv_iov
);
169 *data_length
= state
->data_length
;
174 NTSTATUS
smb2cli_notify(struct smbXcli_conn
*conn
,
175 uint32_t timeout_msec
,
176 struct smbXcli_session
*session
,
177 struct smbXcli_tcon
*tcon
,
178 uint32_t output_buffer_length
,
179 uint64_t fid_persistent
,
180 uint64_t fid_volatile
,
181 uint32_t completion_filter
,
185 uint32_t *data_length
)
187 TALLOC_CTX
*frame
= talloc_stackframe();
188 struct tevent_context
*ev
;
189 struct tevent_req
*req
;
190 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
192 if (smbXcli_conn_has_async_calls(conn
)) {
194 * Can't use sync call while an async call is in flight
196 status
= NT_STATUS_INVALID_PARAMETER
;
199 ev
= samba_tevent_context_init(frame
);
203 req
= smb2cli_notify_send(frame
, ev
, conn
, timeout_msec
,
204 session
, tcon
, output_buffer_length
,
205 fid_persistent
, fid_volatile
,
206 completion_filter
, recursive
);
210 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
213 status
= smb2cli_notify_recv(req
, mem_ctx
, data
, data_length
);