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