r10504: - seperate implementation specific stuff, from the generic composite
[tprouty/samba.git] / source4 / libcli / smb_composite / loadfile.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 /*
21   a composite API for loading a whole file into memory
22 */
23
24 #include "includes.h"
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"
29
30 /* the stages of this call */
31 enum loadfile_stage {LOADFILE_OPEN, LOADFILE_READ, LOADFILE_CLOSE};
32
33
34 static void loadfile_handler(struct smbcli_request *req);
35
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;
42 };
43
44 /*
45   setup for the close
46 */
47 static NTSTATUS setup_close(struct composite_context *c, 
48                             struct smbcli_tree *tree, uint16_t fnum)
49 {
50         struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
51         union smb_close *io_close;
52
53         /* nothing to read, setup the close */
54         io_close = talloc(c, union smb_close);
55         NT_STATUS_HAVE_NO_MEMORY(io_close);
56         
57         io_close->close.level = RAW_CLOSE_CLOSE;
58         io_close->close.in.fnum = fnum;
59         io_close->close.in.write_time = 0;
60
61         state->req = smb_raw_close_send(tree, io_close);
62         NT_STATUS_HAVE_NO_MEMORY(state->req);
63
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;
68
69         return NT_STATUS_OK;
70 }
71
72 /*
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
75 */
76 static NTSTATUS loadfile_open(struct composite_context *c, 
77                               struct smb_composite_loadfile *io)
78 {
79         struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
80         struct smbcli_tree *tree = state->req->tree;
81         NTSTATUS status;
82
83         status = smb_raw_open_recv(state->req, c, state->io_open);
84         NT_STATUS_NOT_OK_RETURN(status);
85         
86         /* don't allow stupidly large loads */
87         if (state->io_open->ntcreatex.out.size > 100*1000*1000) {
88                 return NT_STATUS_INSUFFICIENT_RESOURCES;
89         }
90
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);
95
96         if (io->out.size == 0) {
97                 return setup_close(c, tree, state->io_open->ntcreatex.out.fnum);
98         }
99
100         /* setup for the read */
101         state->io_read = talloc(c, union smb_read);
102         NT_STATUS_HAVE_NO_MEMORY(state->io_read);
103         
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;
111
112         state->req = smb_raw_read_send(tree, state->io_read);
113         NT_STATUS_HAVE_NO_MEMORY(state->req);
114
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;
119
120         talloc_free(state->io_open);
121
122         return NT_STATUS_OK;
123 }
124
125
126 /*
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
129 */
130 static NTSTATUS loadfile_read(struct composite_context *c, 
131                               struct smb_composite_loadfile *io)
132 {
133         struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
134         struct smbcli_tree *tree = state->req->tree;
135         NTSTATUS status;
136
137         status = smb_raw_read_recv(state->req, state->io_read);
138         NT_STATUS_NOT_OK_RETURN(status);
139         
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);
144         }
145
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;
150
151         state->req = smb_raw_read_send(tree, state->io_read);
152         NT_STATUS_HAVE_NO_MEMORY(state->req);
153
154         /* call the handler again when the read is done */
155         state->req->async.fn = loadfile_handler;
156         state->req->async.private = c;
157
158         return NT_STATUS_OK;
159 }
160
161 /*
162   called when the close is done, check the status and cleanup
163 */
164 static NTSTATUS loadfile_close(struct composite_context *c, 
165                                struct smb_composite_loadfile *io)
166 {
167         struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
168         NTSTATUS status;
169
170         status = smbcli_request_simple_recv(state->req);
171         NT_STATUS_NOT_OK_RETURN(status);
172         
173         c->state = COMPOSITE_STATE_DONE;
174
175         return NT_STATUS_OK;
176 }
177                                                      
178
179 /*
180   handler for completion of a sub-request in loadfile
181 */
182 static void loadfile_handler(struct smbcli_request *req)
183 {
184         struct composite_context *c = req->async.private;
185         struct loadfile_state *state = talloc_get_type(c->private_data, struct loadfile_state);
186
187         /* when this handler is called, the stage indicates what
188            call has just finished */
189         switch (state->stage) {
190         case LOADFILE_OPEN:
191                 c->status = loadfile_open(c, state->io);
192                 break;
193
194         case LOADFILE_READ:
195                 c->status = loadfile_read(c, state->io);
196                 break;
197
198         case LOADFILE_CLOSE:
199                 c->status = loadfile_close(c, state->io);
200                 break;
201         }
202
203         if (!NT_STATUS_IS_OK(c->status)) {
204                 c->state = COMPOSITE_STATE_ERROR;
205         }
206
207         if (c->state >= COMPOSITE_STATE_DONE &&
208             c->async.fn) {
209                 c->async.fn(c);
210         }
211 }
212
213 /*
214   composite loadfile call - does an openx followed by a number of readx calls,
215   followed by a close
216 */
217 struct composite_context *smb_composite_loadfile_send(struct smbcli_tree *tree, 
218                                                      struct smb_composite_loadfile *io)
219 {
220         struct composite_context *c;
221         struct loadfile_state *state;
222
223         c = talloc_zero(tree, struct composite_context);
224         if (c == NULL) goto failed;
225
226         state = talloc(c, struct loadfile_state);
227         if (state == NULL) goto failed;
228
229         state->io = io;
230
231         c->private_data = state;
232         c->state = COMPOSITE_STATE_IN_PROGRESS;
233         c->event_ctx = tree->session->transport->socket->event.ctx;
234
235         /* setup for the open */
236         state->io_open = talloc_zero(c, union smb_open);
237         if (state->io_open == NULL) goto failed;
238         
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;
247
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;
251
252         /* setup the callback handler */
253         state->req->async.fn = loadfile_handler;
254         state->req->async.private = c;
255         state->stage = LOADFILE_OPEN;
256
257         return c;
258
259 failed:
260         talloc_free(c);
261         return NULL;
262 }
263
264
265 /*
266   composite loadfile call - recv side
267 */
268 NTSTATUS smb_composite_loadfile_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
269 {
270         NTSTATUS status;
271
272         status = composite_wait(c);
273
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);
277         }
278
279         talloc_free(c);
280         return status;
281 }
282
283
284 /*
285   composite loadfile call - sync interface
286 */
287 NTSTATUS smb_composite_loadfile(struct smbcli_tree *tree, 
288                                 TALLOC_CTX *mem_ctx,
289                                 struct smb_composite_loadfile *io)
290 {
291         struct composite_context *c = smb_composite_loadfile_send(tree, io);
292         return smb_composite_loadfile_recv(c, mem_ctx);
293 }
294