s3:smb2cli: SMB2_QUERY_DIRECTORY needs one dyn byte to that the structure size check...
[sfrench/samba-autobuild/.git] / source3 / libsmb / smb2cli_query_directory.c
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 "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"
27
28 struct smb2cli_query_directory_state {
29         uint8_t fixed[32];
30         uint8_t dyn_pad[1];
31         struct iovec *recv_iov;
32         uint8_t *data;
33         uint32_t data_length;
34 };
35
36 static void smb2cli_query_directory_done(struct tevent_req *subreq);
37
38 struct tevent_req *smb2cli_query_directory_send(TALLOC_CTX *mem_ctx,
39                                                 struct tevent_context *ev,
40                                                 struct cli_state *cli,
41                                                 uint8_t level,
42                                                 uint8_t flags,
43                                                 uint32_t file_index,
44                                                 uint64_t fid_persistent,
45                                                 uint64_t fid_volatile,
46                                                 const char *mask,
47                                                 uint32_t outbuf_len)
48 {
49         struct tevent_req *req, *subreq;
50         struct smb2cli_query_directory_state *state;
51         uint8_t *fixed;
52         uint8_t *dyn;
53         size_t dyn_len;
54
55         req = tevent_req_create(mem_ctx, &state,
56                                 struct smb2cli_query_directory_state);
57         if (req == NULL) {
58                 return NULL;
59         }
60
61         if (!convert_string_talloc(state, CH_UNIX, CH_UTF16,
62                                    mask, strlen(mask)+1,
63                                    &dyn, &dyn_len)) {
64                 tevent_req_oom(req);
65                 return tevent_req_post(req, ev);
66         }
67
68         fixed = state->fixed;
69         SSVAL(fixed, 0, 33);
70         SCVAL(fixed, 2, level);
71         SCVAL(fixed, 3, flags);
72         SIVAL(fixed, 4, file_index);
73         SBVAL(fixed, 8, fid_persistent);
74         SBVAL(fixed, 16, fid_volatile);
75         SSVAL(fixed, 24, SMB2_HDR_BODY + 32);
76         SSVAL(fixed, 26, dyn_len);
77         SSVAL(fixed, 28, outbuf_len);
78
79         if (dyn_len == 0) {
80                 dyn = state->dyn_pad;
81                 dyn_len = sizeof(state->dyn_pad);
82         }
83
84         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_FIND,
85                                   0, 0, /* flags */
86                                   cli->smb2.pid,
87                                   cli->smb2.tid,
88                                   cli->smb2.uid,
89                                   state->fixed, sizeof(state->fixed),
90                                   dyn, dyn_len);
91         if (tevent_req_nomem(subreq, req)) {
92                 return tevent_req_post(req, ev);
93         }
94         tevent_req_set_callback(subreq, smb2cli_query_directory_done, req);
95         return req;
96 }
97
98 static void smb2cli_query_directory_done(struct tevent_req *subreq)
99 {
100         struct tevent_req *req =
101                 tevent_req_callback_data(subreq,
102                 struct tevent_req);
103         struct smb2cli_query_directory_state *state =
104                 tevent_req_data(req,
105                 struct smb2cli_query_directory_state);
106         NTSTATUS status;
107         struct iovec *iov;
108         uint16_t data_offset;
109
110         status = smb2cli_req_recv(subreq, state, &iov, 9);
111         if (tevent_req_nterror(req, status)) {
112                 return;
113         }
114
115         data_offset = SVAL(iov[1].iov_base, 2);
116         state->data_length = IVAL(iov[1].iov_base, 4);
117
118         if ((data_offset != SMB2_HDR_BODY + 8) ||
119             (state->data_length > iov[2].iov_len)) {
120                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
121                 return;
122         }
123
124         state->recv_iov = iov;
125         state->data = (uint8_t *)iov[2].iov_base;
126         tevent_req_done(req);
127 }
128
129 NTSTATUS smb2cli_query_directory_recv(struct tevent_req *req,
130                                        TALLOC_CTX *mem_ctx,
131                                        uint8_t **data,
132                                        uint32_t *data_length)
133 {
134         struct smb2cli_query_directory_state *state =
135                 tevent_req_data(req,
136                 struct smb2cli_query_directory_state);
137         NTSTATUS status;
138
139         if (tevent_req_is_nterror(req, &status)) {
140                 return status;
141         }
142         talloc_steal(mem_ctx, state->recv_iov);
143         *data_length = state->data_length;
144         *data = state->data;
145         return NT_STATUS_OK;
146 }
147
148 NTSTATUS smb2cli_query_directory(struct cli_state *cli,
149                                  uint8_t level,
150                                  uint8_t flags,
151                                  uint32_t file_index,
152                                  uint64_t fid_persistent,
153                                  uint64_t fid_volatile,
154                                  const char *mask,
155                                  uint32_t outbuf_len,
156                                  TALLOC_CTX *mem_ctx,
157                                  uint8_t **data,
158                                  uint32_t *data_length)
159 {
160         TALLOC_CTX *frame = talloc_stackframe();
161         struct event_context *ev;
162         struct tevent_req *req;
163         NTSTATUS status = NT_STATUS_NO_MEMORY;
164
165         if (cli_has_async_calls(cli)) {
166                 /*
167                  * Can't use sync call while an async call is in flight
168                  */
169                 status = NT_STATUS_INVALID_PARAMETER;
170                 goto fail;
171         }
172         ev = event_context_init(frame);
173         if (ev == NULL) {
174                 goto fail;
175         }
176         req = smb2cli_query_directory_send(frame, ev, cli, level, flags,
177                                            file_index, fid_persistent,
178                                            fid_volatile, mask, outbuf_len);
179         if (req == NULL) {
180                 goto fail;
181         }
182         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
183                 goto fail;
184         }
185         status = smb2cli_query_directory_recv(req, mem_ctx,
186                                               data, data_length);
187  fail:
188         TALLOC_FREE(frame);
189         return status;
190 }