3feaa07abb02ec87b0b04302673404e992936a3f
[metze/samba/wip.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         struct iovec *recv_iov;
31         uint8_t *data;
32         uint32_t data_length;
33 };
34
35 static void smb2cli_query_directory_done(struct tevent_req *subreq);
36
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)
47 {
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;
53
54         req = tevent_req_create(mem_ctx, &state,
55                                 struct smb2cli_query_directory_state);
56         if (req == NULL) {
57                 return NULL;
58         }
59
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);
65         }
66
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);
77
78         subreq = smb2cli_req_send(state, ev, cli, SMB2_OP_FIND,
79                                   0, 0, /* flags */
80                                   cli->smb2.pid,
81                                   cli->smb2.tid,
82                                   cli->smb2.uid,
83                                   state->fixed, sizeof(state->fixed),
84                                   dyn, dyn_len);
85         if (tevent_req_nomem(subreq, req)) {
86                 return tevent_req_post(req, ev);
87         }
88         tevent_req_set_callback(subreq, smb2cli_query_directory_done, req);
89         return req;
90 }
91
92 static void smb2cli_query_directory_done(struct tevent_req *subreq)
93 {
94         struct tevent_req *req =
95                 tevent_req_callback_data(subreq,
96                 struct tevent_req);
97         struct smb2cli_query_directory_state *state =
98                 tevent_req_data(req,
99                 struct smb2cli_query_directory_state);
100         NTSTATUS status;
101         struct iovec *iov;
102         uint16_t data_offset;
103
104         status = smb2cli_req_recv(subreq, state, &iov, 9);
105         if (tevent_req_nterror(req, status)) {
106                 return;
107         }
108
109         data_offset = SVAL(iov[1].iov_base, 2);
110         state->data_length = IVAL(iov[1].iov_base, 4);
111
112         if ((data_offset != SMB2_HDR_BODY + 8) ||
113             (state->data_length > iov[2].iov_len)) {
114                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
115                 return;
116         }
117
118         state->recv_iov = iov;
119         state->data = (uint8_t *)iov[2].iov_base;
120         tevent_req_done(req);
121 }
122
123 NTSTATUS smb2cli_query_directory_recv(struct tevent_req *req,
124                                        TALLOC_CTX *mem_ctx,
125                                        uint8_t **data,
126                                        uint32_t *data_length)
127 {
128         struct smb2cli_query_directory_state *state =
129                 tevent_req_data(req,
130                 struct smb2cli_query_directory_state);
131         NTSTATUS status;
132
133         if (tevent_req_is_nterror(req, &status)) {
134                 return status;
135         }
136         talloc_steal(mem_ctx, state->recv_iov);
137         *data_length = state->data_length;
138         *data = state->data;
139         return NT_STATUS_OK;
140 }
141
142 NTSTATUS smb2cli_query_directory(struct cli_state *cli,
143                                  uint8_t level,
144                                  uint8_t flags,
145                                  uint32_t file_index,
146                                  uint64_t fid_persistent,
147                                  uint64_t fid_volatile,
148                                  const char *mask,
149                                  uint32_t outbuf_len,
150                                  TALLOC_CTX *mem_ctx,
151                                  uint8_t **data,
152                                  uint32_t *data_length)
153 {
154         TALLOC_CTX *frame = talloc_stackframe();
155         struct event_context *ev;
156         struct tevent_req *req;
157         NTSTATUS status = NT_STATUS_NO_MEMORY;
158
159         if (cli_has_async_calls(cli)) {
160                 /*
161                  * Can't use sync call while an async call is in flight
162                  */
163                 status = NT_STATUS_INVALID_PARAMETER;
164                 goto fail;
165         }
166         ev = event_context_init(frame);
167         if (ev == NULL) {
168                 goto fail;
169         }
170         req = smb2cli_query_directory_send(frame, ev, cli, level, flags,
171                                            file_index, fid_persistent,
172                                            fid_volatile, mask, outbuf_len);
173         if (req == NULL) {
174                 goto fail;
175         }
176         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
177                 goto fail;
178         }
179         status = smb2cli_query_directory_recv(req, mem_ctx,
180                                               data, data_length);
181  fail:
182         TALLOC_FREE(frame);
183         return status;
184 }