Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : smb2 lib
4 : Copyright (C) Volker Lendecke 2011
5 :
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.
10 :
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.
15 :
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/>.
18 : */
19 :
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 :
26 : struct smb2cli_query_directory_state {
27 : uint8_t fixed[32];
28 : uint8_t dyn_pad[1];
29 : struct iovec *recv_iov;
30 : uint8_t *data;
31 : uint32_t data_length;
32 : };
33 :
34 : static void smb2cli_query_directory_done(struct tevent_req *subreq);
35 :
36 16681 : struct tevent_req *smb2cli_query_directory_send(TALLOC_CTX *mem_ctx,
37 : struct tevent_context *ev,
38 : struct smbXcli_conn *conn,
39 : uint32_t timeout_msec,
40 : struct smbXcli_session *session,
41 : struct smbXcli_tcon *tcon,
42 : uint8_t level,
43 : uint8_t flags,
44 : uint32_t file_index,
45 : uint64_t fid_persistent,
46 : uint64_t fid_volatile,
47 : const char *mask,
48 : uint32_t outbuf_len)
49 : {
50 0 : struct tevent_req *req, *subreq;
51 0 : struct smb2cli_query_directory_state *state;
52 0 : uint8_t *fixed;
53 0 : uint8_t *dyn;
54 0 : size_t dyn_len;
55 :
56 16681 : req = tevent_req_create(mem_ctx, &state,
57 : struct smb2cli_query_directory_state);
58 16681 : if (req == NULL) {
59 0 : return NULL;
60 : }
61 :
62 16681 : if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
63 : mask, strlen(mask),
64 : &dyn, &dyn_len)) {
65 0 : tevent_req_oom(req);
66 0 : return tevent_req_post(req, ev);
67 : }
68 :
69 16681 : if (strlen(mask) == 0) {
70 0 : TALLOC_FREE(dyn);
71 0 : dyn_len = 0;
72 : }
73 :
74 16681 : fixed = state->fixed;
75 16681 : SSVAL(fixed, 0, 33);
76 16681 : SCVAL(fixed, 2, level);
77 16681 : SCVAL(fixed, 3, flags);
78 16681 : SIVAL(fixed, 4, file_index);
79 16681 : SBVAL(fixed, 8, fid_persistent);
80 16681 : SBVAL(fixed, 16, fid_volatile);
81 16681 : SSVAL(fixed, 24, SMB2_HDR_BODY + 32);
82 16681 : SSVAL(fixed, 26, dyn_len);
83 16681 : SIVAL(fixed, 28, outbuf_len);
84 :
85 16681 : if (dyn_len == 0) {
86 0 : dyn = state->dyn_pad;
87 0 : dyn_len = sizeof(state->dyn_pad);
88 : }
89 :
90 16681 : subreq = smb2cli_req_send(state, ev, conn, SMB2_OP_QUERY_DIRECTORY,
91 : 0, 0, /* flags */
92 : timeout_msec,
93 : tcon,
94 : session,
95 16681 : state->fixed, sizeof(state->fixed),
96 : dyn, dyn_len,
97 : outbuf_len); /* max_dyn_len */
98 16681 : if (tevent_req_nomem(subreq, req)) {
99 0 : return tevent_req_post(req, ev);
100 : }
101 16681 : tevent_req_set_callback(subreq, smb2cli_query_directory_done, req);
102 16681 : return req;
103 : }
104 :
105 16681 : static void smb2cli_query_directory_done(struct tevent_req *subreq)
106 : {
107 0 : struct tevent_req *req =
108 16681 : tevent_req_callback_data(subreq,
109 : struct tevent_req);
110 0 : struct smb2cli_query_directory_state *state =
111 16681 : tevent_req_data(req,
112 : struct smb2cli_query_directory_state);
113 0 : NTSTATUS status;
114 0 : struct iovec *iov;
115 0 : uint16_t data_offset;
116 0 : static const struct smb2cli_req_expected_response expected[] = {
117 : {
118 : .status = NT_STATUS_OK,
119 : .body_size = 0x09
120 : }
121 : };
122 :
123 16681 : status = smb2cli_req_recv(subreq, state, &iov,
124 : expected, ARRAY_SIZE(expected));
125 16681 : TALLOC_FREE(subreq);
126 16681 : if (tevent_req_nterror(req, status)) {
127 8424 : return;
128 : }
129 :
130 8257 : data_offset = SVAL(iov[1].iov_base, 2);
131 8257 : state->data_length = IVAL(iov[1].iov_base, 4);
132 :
133 8257 : if ((data_offset != SMB2_HDR_BODY + 8) ||
134 8257 : (state->data_length > iov[2].iov_len)) {
135 0 : tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
136 0 : return;
137 : }
138 :
139 8257 : state->recv_iov = iov;
140 8257 : state->data = (uint8_t *)iov[2].iov_base;
141 8257 : tevent_req_done(req);
142 : }
143 :
144 16681 : NTSTATUS smb2cli_query_directory_recv(struct tevent_req *req,
145 : TALLOC_CTX *mem_ctx,
146 : uint8_t **data,
147 : uint32_t *data_length)
148 : {
149 0 : struct smb2cli_query_directory_state *state =
150 16681 : tevent_req_data(req,
151 : struct smb2cli_query_directory_state);
152 0 : NTSTATUS status;
153 :
154 16681 : if (tevent_req_is_nterror(req, &status)) {
155 8424 : return status;
156 : }
157 8257 : talloc_steal(mem_ctx, state->recv_iov);
158 8257 : *data_length = state->data_length;
159 8257 : *data = state->data;
160 8257 : return NT_STATUS_OK;
161 : }
162 :
163 58 : NTSTATUS smb2cli_query_directory(struct smbXcli_conn *conn,
164 : uint32_t timeout_msec,
165 : struct smbXcli_session *session,
166 : struct smbXcli_tcon *tcon,
167 : uint8_t level,
168 : uint8_t flags,
169 : uint32_t file_index,
170 : uint64_t fid_persistent,
171 : uint64_t fid_volatile,
172 : const char *mask,
173 : uint32_t outbuf_len,
174 : TALLOC_CTX *mem_ctx,
175 : uint8_t **data,
176 : uint32_t *data_length)
177 : {
178 58 : TALLOC_CTX *frame = talloc_stackframe();
179 0 : struct tevent_context *ev;
180 0 : struct tevent_req *req;
181 58 : NTSTATUS status = NT_STATUS_NO_MEMORY;
182 :
183 58 : if (smbXcli_conn_has_async_calls(conn)) {
184 : /*
185 : * Can't use sync call while an async call is in flight
186 : */
187 0 : status = NT_STATUS_INVALID_PARAMETER;
188 0 : goto fail;
189 : }
190 58 : ev = samba_tevent_context_init(frame);
191 58 : if (ev == NULL) {
192 0 : goto fail;
193 : }
194 58 : req = smb2cli_query_directory_send(frame, ev, conn, timeout_msec,
195 : session, tcon,
196 : level, flags,
197 : file_index, fid_persistent,
198 : fid_volatile, mask, outbuf_len);
199 58 : if (req == NULL) {
200 0 : goto fail;
201 : }
202 58 : if (!tevent_req_poll_ntstatus(req, ev, &status)) {
203 0 : goto fail;
204 : }
205 58 : status = smb2cli_query_directory_recv(req, mem_ctx,
206 : data, data_length);
207 58 : fail:
208 58 : TALLOC_FREE(frame);
209 58 : return status;
210 : }
|