libsmb: Add smb2cli_notify()
[Samba.git] / libcli / smb / smb2cli_notify.c
blob0a23cf9ad033f4a7e0bcc4aef89b67dfc6e3ee59
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;
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,
47 bool recursive)
49 struct tevent_req *req, *subreq;
50 struct smb2cli_notify_state *state;
51 uint8_t *fixed;
53 req = tevent_req_create(mem_ctx, &state,
54 struct smb2cli_notify_state);
55 if (req == NULL) {
56 return NULL;
58 fixed = state->fixed;
59 SSVAL(fixed, 0, 32);
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,
68 0, 0, /* flags */
69 timeout_msec,
70 tcon,
71 session,
72 state->fixed, sizeof(state->fixed),
73 NULL, 0, /* dyn* */
74 0); /* max_dyn_len */
75 if (tevent_req_nomem(subreq, req)) {
76 return tevent_req_post(req, ev);
78 tevent_req_set_callback(subreq, smb2cli_notify_done, req);
79 return 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);
88 NTSTATUS status;
89 struct iovec *iov;
90 uint16_t data_offset;
91 static const struct smb2cli_req_expected_response expected[] = {
93 .status = NT_STATUS_OK,
94 .body_size = 0x09
98 status = smb2cli_req_recv(subreq, state, &iov,
99 expected, ARRAY_SIZE(expected));
100 TALLOC_FREE(subreq);
101 if (tevent_req_nterror(req, status)) {
102 return;
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);
111 return;
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);
124 NTSTATUS status;
126 if (tevent_req_is_nterror(req, &status)) {
127 return status;
129 talloc_steal(mem_ctx, state->recv_iov);
130 *data_length = state->data_length;
131 *data = state->data;
132 return NT_STATUS_OK;
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,
143 bool recursive,
144 TALLOC_CTX *mem_ctx,
145 uint8_t **data,
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;
158 goto fail;
160 ev = samba_tevent_context_init(frame);
161 if (ev == NULL) {
162 goto fail;
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);
168 if (req == NULL) {
169 goto fail;
171 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
172 goto fail;
174 status = smb2cli_notify_recv(req, mem_ctx, data, data_length);
175 fail:
176 TALLOC_FREE(frame);
177 return status;