Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-trivial
[sfrench/samba-autobuild/.git] / source4 / libcli / smb_composite / fetchfile.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Volker Lendecke 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 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   a composite API for loading a whole file into memory
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/smb_composite/smb_composite.h"
26 #include "param/param.h"
27 #include "libcli/resolve/resolve.h"
28
29 enum fetchfile_stage {FETCHFILE_CONNECT,
30                       FETCHFILE_READ};
31
32 struct fetchfile_state {
33         enum fetchfile_stage stage;
34         struct smb_composite_fetchfile *io;
35         struct composite_context *creq;
36         struct smb_composite_connect *connect;
37         struct smb_composite_loadfile *loadfile;
38 };
39
40 static void fetchfile_composite_handler(struct composite_context *req);
41
42 static NTSTATUS fetchfile_connect(struct composite_context *c,
43                                   struct smb_composite_fetchfile *io)
44 {
45         NTSTATUS status;
46         struct fetchfile_state *state;
47         state = talloc_get_type(c->private_data, struct fetchfile_state);
48
49         status = smb_composite_connect_recv(state->creq, c);
50         NT_STATUS_NOT_OK_RETURN(status);
51
52         state->loadfile = talloc(state, struct smb_composite_loadfile);
53         NT_STATUS_HAVE_NO_MEMORY(state->loadfile);
54
55         state->loadfile->in.fname = io->in.filename;
56
57         state->creq = smb_composite_loadfile_send(state->connect->out.tree,
58                                                  state->loadfile);
59         NT_STATUS_HAVE_NO_MEMORY(state->creq);
60
61         state->creq->async.private_data = c;
62         state->creq->async.fn = fetchfile_composite_handler;
63
64         state->stage = FETCHFILE_READ;
65         c->event_ctx = talloc_reference(c, state->creq->event_ctx);
66
67         return NT_STATUS_OK;
68 }
69
70 static NTSTATUS fetchfile_read(struct composite_context *c,
71                                struct smb_composite_fetchfile *io)
72 {
73         NTSTATUS status;
74         struct fetchfile_state *state;
75         state = talloc_get_type(c->private_data, struct fetchfile_state);
76
77         status = smb_composite_loadfile_recv(state->creq, NULL);
78         NT_STATUS_NOT_OK_RETURN(status);
79
80         io->out.data = state->loadfile->out.data;
81         io->out.size = state->loadfile->out.size;
82
83         c->state = COMPOSITE_STATE_DONE;
84         if (c->async.fn)
85                 c->async.fn(c);
86
87         return NT_STATUS_OK;
88 }
89
90 static void fetchfile_state_handler(struct composite_context *c)
91 {
92         struct fetchfile_state *state;
93         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
94         
95         state = talloc_get_type(c->private_data, struct fetchfile_state);
96
97         /* when this handler is called, the stage indicates what
98            call has just finished */
99         switch (state->stage) {
100         case FETCHFILE_CONNECT:
101                 status = fetchfile_connect(c, state->io);
102                 break;
103         case FETCHFILE_READ:
104                 status = fetchfile_read(c, state->io);
105                 break;
106         }
107
108         if (!NT_STATUS_IS_OK(status)) {
109                 c->status = status;
110                 c->state = COMPOSITE_STATE_ERROR;
111                 if (c->async.fn) {
112                         c->async.fn(c);
113                 }
114         }
115 }
116
117 static void fetchfile_composite_handler(struct composite_context *creq)
118 {
119         struct composite_context *c = talloc_get_type(creq->async.private_data, 
120                                                       struct composite_context);
121         fetchfile_state_handler(c);
122 }
123
124 struct composite_context *smb_composite_fetchfile_send(struct smb_composite_fetchfile *io,
125                                                        struct event_context *event_ctx)
126 {
127         struct composite_context *c;
128         struct fetchfile_state *state;
129
130         c = talloc_zero(NULL, struct composite_context);
131         if (c == NULL) goto failed;
132
133         state = talloc(c, struct fetchfile_state);
134         if (state == NULL) goto failed;
135
136         state->connect = talloc(state, struct smb_composite_connect);
137         if (state->connect == NULL) goto failed;
138
139         state->io = io;
140
141         state->connect->in.dest_host    = io->in.dest_host;
142         state->connect->in.dest_ports   = io->in.ports;
143         state->connect->in.called_name  = io->in.called_name;
144         state->connect->in.service      = io->in.service;
145         state->connect->in.service_type = io->in.service_type;
146         state->connect->in.credentials  = io->in.credentials;
147         state->connect->in.fallback_to_anonymous = false;
148         state->connect->in.workgroup    = io->in.workgroup;
149
150         state->connect->in.options      = io->in.options;
151
152         state->creq = smb_composite_connect_send(state->connect, state, 
153                                                  lp_resolve_context(global_loadparm), event_ctx);
154         if (state->creq == NULL) goto failed;
155
156         state->creq->async.private_data = c;
157         state->creq->async.fn = fetchfile_composite_handler;
158
159         c->state = COMPOSITE_STATE_IN_PROGRESS;
160         state->stage = FETCHFILE_CONNECT;
161         c->event_ctx = talloc_reference(c, state->creq->event_ctx);
162         c->private_data = state;
163
164         return c;
165  failed:
166         talloc_free(c);
167         return NULL;
168 }
169
170 NTSTATUS smb_composite_fetchfile_recv(struct composite_context *c,
171                                       TALLOC_CTX *mem_ctx)
172 {
173         NTSTATUS status;
174
175         status = composite_wait(c);
176
177         if (NT_STATUS_IS_OK(status)) {
178                 struct fetchfile_state *state = talloc_get_type(c->private_data, struct fetchfile_state);
179                 talloc_steal(mem_ctx, state->io->out.data);
180         }
181
182         talloc_free(c);
183         return status;
184 }
185
186 NTSTATUS smb_composite_fetchfile(struct smb_composite_fetchfile *io,
187                                  TALLOC_CTX *mem_ctx)
188 {
189         struct composite_context *c = smb_composite_fetchfile_send(io, NULL);
190         return smb_composite_fetchfile_recv(c, mem_ctx);
191 }