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