s3-libsmb: introduce new cli_query_secdesc() which returns NTSTATUS
[Samba/gebeck_regimport.git] / source3 / libsmb / smb2cli_query_directory.c
blobf9a1bfe4ee3cc7571eae5bb0ba59313c7d6045f7
1 /*
2 Unix SMB/CIFS implementation.
3 smb2 lib
4 Copyright (C) Volker Lendecke 2011
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 "client.h"
22 #include "async_smb.h"
23 #include "smb2cli_base.h"
24 #include "smb2cli.h"
25 #include "libsmb/proto.h"
26 #include "lib/util/tevent_ntstatus.h"
28 struct smb2cli_query_directory_state {
29 uint8_t fixed[32];
30 struct iovec *recv_iov;
31 uint8_t *data;
32 uint32_t data_length;
35 static void smb2cli_query_directory_done(struct tevent_req *subreq);
37 struct tevent_req *smb2cli_query_directory_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 struct cli_state *cli,
40 uint8_t level,
41 uint8_t flags,
42 uint32_t file_index,
43 uint64_t fid_persistent,
44 uint64_t fid_volatile,
45 const char *mask,
46 uint32_t outbuf_len)
48 struct tevent_req *req, *subreq;
49 struct smb2cli_query_directory_state *state;
50 uint8_t *fixed;
51 uint8_t *dyn;
52 size_t dyn_len;
54 req = tevent_req_create(mem_ctx, &state,
55 struct smb2cli_query_directory_state);
56 if (req == NULL) {
57 return NULL;
60 if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
61 mask, strlen(mask)+1,
62 &dyn, &dyn_len)) {
63 tevent_req_oom(req);
64 return tevent_req_post(req, ev);
67 fixed = state->fixed;
68 SSVAL(fixed, 0, 33);
69 SCVAL(fixed, 2, level);
70 SCVAL(fixed, 3, flags);
71 SIVAL(fixed, 4, file_index);
72 SBVAL(fixed, 8, fid_persistent);
73 SBVAL(fixed, 16, fid_volatile);
74 SSVAL(fixed, 24, SMB2_HDR_BODY + 32);
75 SSVAL(fixed, 26, dyn_len);
76 SSVAL(fixed, 28, outbuf_len);
78 subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_FIND, 0,
79 state->fixed, sizeof(state->fixed),
80 dyn, dyn_len);
81 if (tevent_req_nomem(subreq, req)) {
82 return tevent_req_post(req, ev);
84 tevent_req_set_callback(subreq, smb2cli_query_directory_done, req);
85 return req;
88 static void smb2cli_query_directory_done(struct tevent_req *subreq)
90 struct tevent_req *req =
91 tevent_req_callback_data(subreq,
92 struct tevent_req);
93 struct smb2cli_query_directory_state *state =
94 tevent_req_data(req,
95 struct smb2cli_query_directory_state);
96 NTSTATUS status;
97 struct iovec *iov;
98 uint16_t data_offset;
100 status = smb2cli_req_recv(subreq, state, &iov, 9);
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_query_directory_recv(struct tevent_req *req,
120 TALLOC_CTX *mem_ctx,
121 uint8_t **data,
122 uint32_t *data_length)
124 struct smb2cli_query_directory_state *state =
125 tevent_req_data(req,
126 struct smb2cli_query_directory_state);
127 NTSTATUS status;
129 if (tevent_req_is_nterror(req, &status)) {
130 return status;
132 talloc_steal(mem_ctx, state->recv_iov);
133 *data_length = state->data_length;
134 *data = state->data;
135 return NT_STATUS_OK;
138 NTSTATUS smb2cli_query_directory(struct cli_state *cli,
139 uint8_t level,
140 uint8_t flags,
141 uint32_t file_index,
142 uint64_t fid_persistent,
143 uint64_t fid_volatile,
144 const char *mask,
145 uint32_t outbuf_len,
146 TALLOC_CTX *mem_ctx,
147 uint8_t **data,
148 uint32_t *data_length)
150 TALLOC_CTX *frame = talloc_stackframe();
151 struct event_context *ev;
152 struct tevent_req *req;
153 NTSTATUS status = NT_STATUS_NO_MEMORY;
155 if (cli_has_async_calls(cli)) {
157 * Can't use sync call while an async call is in flight
159 status = NT_STATUS_INVALID_PARAMETER;
160 goto fail;
162 ev = event_context_init(frame);
163 if (ev == NULL) {
164 goto fail;
166 req = smb2cli_query_directory_send(frame, ev, cli, level, flags,
167 file_index, fid_persistent,
168 fid_volatile, mask, outbuf_len);
169 if (req == NULL) {
170 goto fail;
172 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
173 goto fail;
175 status = smb2cli_query_directory_recv(req, mem_ctx,
176 data, data_length);
177 fail:
178 TALLOC_FREE(frame);
179 return status;