2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 a composite API for loading a whole file into memory
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "librpc/gen_ndr/ndr_security.h"
30 /* the stages of this call */
31 enum loadfile_stage {LOADFILE_OPEN, LOADFILE_READ, LOADFILE_CLOSE};
34 static void loadfile_handler(struct smbcli_request *req);
36 struct loadfile_state {
37 enum loadfile_stage stage;
38 struct smb_composite_loadfile *io;
39 struct smbcli_request *req;
40 union smb_open *io_open;
41 union smb_read *io_read;
47 static NTSTATUS setup_close(struct composite_context *c,
48 struct smbcli_tree *tree, uint16_t fnum)
50 struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
51 union smb_close *io_close;
53 /* nothing to read, setup the close */
54 io_close = talloc(c, union smb_close);
55 NT_STATUS_HAVE_NO_MEMORY(io_close);
57 io_close->close.level = RAW_CLOSE_CLOSE;
58 io_close->close.in.fnum = fnum;
59 io_close->close.in.write_time = 0;
61 state->req = smb_raw_close_send(tree, io_close);
62 NT_STATUS_HAVE_NO_MEMORY(state->req);
64 /* call the handler again when the close is done */
65 state->req->async.fn = loadfile_handler;
66 state->req->async.private = c;
67 state->stage = LOADFILE_CLOSE;
73 called when the open is done - pull the results and setup for the
74 first readx, or close if the file is zero size
76 static NTSTATUS loadfile_open(struct composite_context *c,
77 struct smb_composite_loadfile *io)
79 struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
80 struct smbcli_tree *tree = state->req->tree;
83 status = smb_raw_open_recv(state->req, c, state->io_open);
84 NT_STATUS_NOT_OK_RETURN(status);
86 /* don't allow stupidly large loads */
87 if (state->io_open->ntcreatex.out.size > 100*1000*1000) {
88 return NT_STATUS_INSUFFICIENT_RESOURCES;
91 /* allocate space for the file data */
92 io->out.size = state->io_open->ntcreatex.out.size;
93 io->out.data = talloc_array(c, uint8_t, io->out.size);
94 NT_STATUS_HAVE_NO_MEMORY(io->out.data);
96 if (io->out.size == 0) {
97 return setup_close(c, tree, state->io_open->ntcreatex.out.fnum);
100 /* setup for the read */
101 state->io_read = talloc(c, union smb_read);
102 NT_STATUS_HAVE_NO_MEMORY(state->io_read);
104 state->io_read->readx.level = RAW_READ_READX;
105 state->io_read->readx.in.fnum = state->io_open->ntcreatex.out.fnum;
106 state->io_read->readx.in.offset = 0;
107 state->io_read->readx.in.mincnt = MIN(32768, io->out.size);
108 state->io_read->readx.in.maxcnt = state->io_read->readx.in.mincnt;
109 state->io_read->readx.in.remaining = 0;
110 state->io_read->readx.out.data = io->out.data;
112 state->req = smb_raw_read_send(tree, state->io_read);
113 NT_STATUS_HAVE_NO_MEMORY(state->req);
115 /* call the handler again when the first read is done */
116 state->req->async.fn = loadfile_handler;
117 state->req->async.private = c;
118 state->stage = LOADFILE_READ;
120 talloc_free(state->io_open);
127 called when a read is done - pull the results and setup for the
128 next read, or close if the file is all done
130 static NTSTATUS loadfile_read(struct composite_context *c,
131 struct smb_composite_loadfile *io)
133 struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
134 struct smbcli_tree *tree = state->req->tree;
137 status = smb_raw_read_recv(state->req, state->io_read);
138 NT_STATUS_NOT_OK_RETURN(status);
140 /* we might be done */
141 if (state->io_read->readx.in.offset +
142 state->io_read->readx.out.nread == io->out.size) {
143 return setup_close(c, tree, state->io_read->readx.in.fnum);
146 /* setup for the next read */
147 state->io_read->readx.in.offset += state->io_read->readx.out.nread;
148 state->io_read->readx.in.mincnt = MIN(32768, io->out.size - state->io_read->readx.in.offset);
149 state->io_read->readx.out.data = io->out.data + state->io_read->readx.in.offset;
151 state->req = smb_raw_read_send(tree, state->io_read);
152 NT_STATUS_HAVE_NO_MEMORY(state->req);
154 /* call the handler again when the read is done */
155 state->req->async.fn = loadfile_handler;
156 state->req->async.private = c;
162 called when the close is done, check the status and cleanup
164 static NTSTATUS loadfile_close(struct composite_context *c,
165 struct smb_composite_loadfile *io)
167 struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
170 status = smbcli_request_simple_recv(state->req);
171 NT_STATUS_NOT_OK_RETURN(status);
173 c->state = COMPOSITE_STATE_DONE;
180 handler for completion of a sub-request in loadfile
182 static void loadfile_handler(struct smbcli_request *req)
184 struct composite_context *c = req->async.private;
185 struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
187 /* when this handler is called, the stage indicates what
188 call has just finished */
189 switch (state->stage) {
191 c->status = loadfile_open(c, state->io);
195 c->status = loadfile_read(c, state->io);
199 c->status = loadfile_close(c, state->io);
203 if (!NT_STATUS_IS_OK(c->status)) {
204 c->state = COMPOSITE_STATE_ERROR;
207 if (c->state >= COMPOSITE_STATE_DONE &&
214 composite loadfile call - does an openx followed by a number of readx calls,
217 struct composite_context *smb_composite_loadfile_send(struct smbcli_tree *tree,
218 struct smb_composite_loadfile *io)
220 struct composite_context *c;
221 struct loadfile_state *state;
223 c = talloc_zero(tree, struct composite_context);
224 if (c == NULL) goto failed;
226 state = talloc(c, struct loadfile_state);
227 if (state == NULL) goto failed;
231 c->private_data = state;
232 c->state = COMPOSITE_STATE_IN_PROGRESS;
233 c->event_ctx = tree->session->transport->socket->event.ctx;
235 /* setup for the open */
236 state->io_open = talloc_zero(c, union smb_open);
237 if (state->io_open == NULL) goto failed;
239 state->io_open->ntcreatex.level = RAW_OPEN_NTCREATEX;
240 state->io_open->ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED;
241 state->io_open->ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
242 state->io_open->ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
243 state->io_open->ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
244 state->io_open->ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
245 state->io_open->ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
246 state->io_open->ntcreatex.in.fname = io->in.fname;
248 /* send the open on its way */
249 state->req = smb_raw_open_send(tree, state->io_open);
250 if (state->req == NULL) goto failed;
252 /* setup the callback handler */
253 state->req->async.fn = loadfile_handler;
254 state->req->async.private = c;
255 state->stage = LOADFILE_OPEN;
266 composite loadfile call - recv side
268 NTSTATUS smb_composite_loadfile_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
272 status = composite_wait(c);
274 if (NT_STATUS_IS_OK(status)) {
275 struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
276 talloc_steal(mem_ctx, state->io->out.data);
285 composite loadfile call - sync interface
287 NTSTATUS smb_composite_loadfile(struct smbcli_tree *tree,
289 struct smb_composite_loadfile *io)
291 struct composite_context *c = smb_composite_loadfile_send(tree, io);
292 return smb_composite_loadfile_recv(c, mem_ctx);