s3:libads: Add net ads leave keep-account option
[Samba.git] / libcli / smb / smb2cli_notify.c
blob1a2a2793a32b06623c29b6894c66c0648b495564
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
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/>.
20 #include "includes.h"
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 {
28 uint8_t fixed[32];
30 struct iovec *recv_iov;
31 uint8_t *data;
32 uint32_t data_length;
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,
50 bool recursive)
52 struct tevent_req *req, *subreq;
53 struct smb2cli_notify_state *state;
54 uint8_t *fixed;
55 uint16_t watch_tree;
57 req = tevent_req_create(mem_ctx, &state,
58 struct smb2cli_notify_state);
59 if (req == NULL) {
60 return NULL;
63 watch_tree = recursive ? SMB2_WATCH_TREE : 0;
64 fixed = state->fixed;
65 SSVAL(fixed, 0, 32);
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,
74 0, 0, /* flags */
75 0, /* timeout_msec */
76 tcon,
77 session,
78 state->fixed, sizeof(state->fixed),
79 NULL, 0, /* dyn* */
80 0); /* max_dyn_len */
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);
93 return 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);
102 bool ok;
104 ok = tevent_wakeup_recv(subreq);
105 if (!ok) {
106 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
107 return;
110 ok = tevent_req_cancel(state->subreq);
111 if (!ok) {
112 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
113 return;
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);
123 NTSTATUS status;
124 struct iovec *iov;
125 uint16_t data_offset;
126 static const struct smb2cli_req_expected_response expected[] = {
128 .status = NT_STATUS_OK,
129 .body_size = 0x09
133 status = smb2cli_req_recv(subreq, state, &iov,
134 expected, ARRAY_SIZE(expected));
135 TALLOC_FREE(subreq);
137 if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) {
138 status = NT_STATUS_IO_TIMEOUT;
140 if (tevent_req_nterror(req, status)) {
141 return;
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);
150 return;
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);
163 NTSTATUS status;
165 if (tevent_req_is_nterror(req, &status)) {
166 return status;
168 talloc_steal(mem_ctx, state->recv_iov);
169 *data_length = state->data_length;
170 *data = state->data;
171 return NT_STATUS_OK;
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,
182 bool recursive,
183 TALLOC_CTX *mem_ctx,
184 uint8_t **data,
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;
197 goto fail;
199 ev = samba_tevent_context_init(frame);
200 if (ev == NULL) {
201 goto fail;
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);
207 if (req == NULL) {
208 goto fail;
210 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
211 goto fail;
213 status = smb2cli_notify_recv(req, mem_ctx, data, data_length);
214 fail:
215 TALLOC_FREE(frame);
216 return status;