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
;
35 static void smb2cli_notify_done(struct tevent_req
*subreq
);
37 struct tevent_req
*smb2cli_notify_send(TALLOC_CTX
*mem_ctx
,
38 struct tevent_context
*ev
,
39 struct smbXcli_conn
*conn
,
40 uint32_t timeout_msec
,
41 struct smbXcli_session
*session
,
42 struct smbXcli_tcon
*tcon
,
43 uint32_t output_buffer_length
,
44 uint64_t fid_persistent
,
45 uint64_t fid_volatile
,
46 uint32_t completion_filter
,
49 struct tevent_req
*req
, *subreq
;
50 struct smb2cli_notify_state
*state
;
53 req
= tevent_req_create(mem_ctx
, &state
,
54 struct smb2cli_notify_state
);
60 SSVAL(fixed
, 2, recursive
? SMB2_WATCH_TREE
: 0);
61 SIVAL(fixed
, 4, output_buffer_length
);
62 SBVAL(fixed
, 8, fid_persistent
);
63 SBVAL(fixed
, 16, fid_volatile
);
64 SIVAL(fixed
, 24, completion_filter
);
65 SIVAL(fixed
, 28, 0); /* reserved */
67 subreq
= smb2cli_req_send(state
, ev
, conn
, SMB2_OP_NOTIFY
,
72 state
->fixed
, sizeof(state
->fixed
),
75 if (tevent_req_nomem(subreq
, req
)) {
76 return tevent_req_post(req
, ev
);
78 tevent_req_set_callback(subreq
, smb2cli_notify_done
, req
);
82 static void smb2cli_notify_done(struct tevent_req
*subreq
)
84 struct tevent_req
*req
= tevent_req_callback_data(
85 subreq
, struct tevent_req
);
86 struct smb2cli_notify_state
*state
= tevent_req_data(
87 req
, struct smb2cli_notify_state
);
91 static const struct smb2cli_req_expected_response expected
[] = {
93 .status
= NT_STATUS_OK
,
98 status
= smb2cli_req_recv(subreq
, state
, &iov
,
99 expected
, ARRAY_SIZE(expected
));
101 if (tevent_req_nterror(req
, status
)) {
105 data_offset
= SVAL(iov
[1].iov_base
, 2);
106 state
->data_length
= IVAL(iov
[1].iov_base
, 4);
108 if ((data_offset
!= SMB2_HDR_BODY
+ 8) ||
109 (state
->data_length
> iov
[2].iov_len
)) {
110 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
114 state
->recv_iov
= iov
;
115 state
->data
= (uint8_t *)iov
[2].iov_base
;
116 tevent_req_done(req
);
119 NTSTATUS
smb2cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
120 uint8_t **data
, uint32_t *data_length
)
122 struct smb2cli_notify_state
*state
= tevent_req_data(
123 req
, struct smb2cli_notify_state
);
126 if (tevent_req_is_nterror(req
, &status
)) {
129 talloc_steal(mem_ctx
, state
->recv_iov
);
130 *data_length
= state
->data_length
;
135 NTSTATUS
smb2cli_notify(struct smbXcli_conn
*conn
,
136 uint32_t timeout_msec
,
137 struct smbXcli_session
*session
,
138 struct smbXcli_tcon
*tcon
,
139 uint32_t output_buffer_length
,
140 uint64_t fid_persistent
,
141 uint64_t fid_volatile
,
142 uint32_t completion_filter
,
146 uint32_t *data_length
)
148 TALLOC_CTX
*frame
= talloc_stackframe();
149 struct tevent_context
*ev
;
150 struct tevent_req
*req
;
151 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
153 if (smbXcli_conn_has_async_calls(conn
)) {
155 * Can't use sync call while an async call is in flight
157 status
= NT_STATUS_INVALID_PARAMETER
;
160 ev
= samba_tevent_context_init(frame
);
164 req
= smb2cli_notify_send(frame
, ev
, conn
, timeout_msec
,
165 session
, tcon
, output_buffer_length
,
166 fid_persistent
, fid_volatile
,
167 completion_filter
, recursive
);
171 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
174 status
= smb2cli_notify_recv(req
, mem_ctx
, data
, data_length
);