r4911: make sure we fill in the transport called name on port 445 as well
[sfrench/samba-autobuild/.git] / source4 / libcli / composite / connect.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 making a full SMB connection
22 */
23
24 #include "includes.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27
28 /* the stages of this call */
29 enum connect_stage {CONNECT_RESOLVE, 
30                     CONNECT_SOCKET, 
31                     CONNECT_SESSION_REQUEST, 
32                     CONNECT_NEGPROT,
33                     CONNECT_SESSION_SETUP,
34                     CONNECT_TCON};
35
36 struct connect_state {
37         struct smbcli_socket *sock;
38         struct smbcli_transport *transport;
39         struct smbcli_session *session;
40         struct smb_composite_connect *io;
41         union smb_tcon *io_tcon;
42         struct smb_composite_sesssetup *io_setup;
43         struct smbcli_request *req;
44         struct smbcli_composite *creq;
45 };
46
47
48 static void request_handler(struct smbcli_request *);
49 static void composite_handler(struct smbcli_composite *);
50
51 /*
52   setup a negprot send 
53 */
54 static NTSTATUS connect_send_negprot(struct smbcli_composite *c, 
55                                      struct smb_composite_connect *io)
56 {
57         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
58
59         state->req = smb_raw_negotiate_send(state->transport, lp_maxprotocol());
60         NT_STATUS_HAVE_NO_MEMORY(state->req);
61
62         state->req->async.fn = request_handler;
63         state->req->async.private = c;
64         c->stage = CONNECT_NEGPROT;
65         
66         return NT_STATUS_OK;
67 }
68
69
70 /*
71   a tree connect request has competed
72 */
73 static NTSTATUS connect_tcon(struct smbcli_composite *c, 
74                              struct smb_composite_connect *io)
75 {
76         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
77         NTSTATUS status;
78
79         status = smb_tree_connect_recv(state->req, c, state->io_tcon);
80         NT_STATUS_NOT_OK_RETURN(status);
81
82         io->out.tree->tid = state->io_tcon->tconx.out.tid;
83         if (state->io_tcon->tconx.out.dev_type) {
84                 io->out.tree->device = talloc_strdup(io->out.tree, 
85                                                      state->io_tcon->tconx.out.dev_type);
86         }
87         if (state->io_tcon->tconx.out.fs_type) {
88                 io->out.tree->fs_type = talloc_strdup(io->out.tree, 
89                                                       state->io_tcon->tconx.out.fs_type);
90         }
91
92         /* all done! */
93         c->state = SMBCLI_REQUEST_DONE;
94         if (c->async.fn) {
95                 c->async.fn(c);
96         }
97
98         return NT_STATUS_OK;
99 }
100
101
102 /*
103   a session setup request has competed
104 */
105 static NTSTATUS connect_session_setup(struct smbcli_composite *c, 
106                                       struct smb_composite_connect *io)
107 {
108         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
109         NTSTATUS status;
110
111         status = smb_composite_sesssetup_recv(state->creq);
112         NT_STATUS_NOT_OK_RETURN(status);
113         
114         state->session->vuid = state->io_setup->out.vuid;
115         
116         /* setup for a tconx */
117         io->out.tree = smbcli_tree_init(state->session);
118         NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
119
120         state->io_tcon = talloc(c, union smb_tcon);
121         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
122
123         /* connect to a share using a tree connect */
124         state->io_tcon->generic.level = RAW_TCON_TCONX;
125         state->io_tcon->tconx.in.flags = 0;
126         state->io_tcon->tconx.in.password = data_blob(NULL, 0); 
127         
128         state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
129                                                  "\\\\%s\\%s", 
130                                                  io->in.called_name, 
131                                                  io->in.service);
132         NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
133         if (!io->in.service_type) {
134                 state->io_tcon->tconx.in.device = "?????";
135         } else {
136                 state->io_tcon->tconx.in.device = io->in.service_type;
137         }
138
139         state->req = smb_tree_connect_send(io->out.tree, state->io_tcon);
140         NT_STATUS_HAVE_NO_MEMORY(state->req);
141
142         state->req->async.fn = request_handler;
143         state->req->async.private = c;
144         c->stage = CONNECT_TCON;
145
146         return NT_STATUS_OK;
147 }
148
149 /*
150   a negprot request has competed
151 */
152 static NTSTATUS connect_negprot(struct smbcli_composite *c, 
153                                 struct smb_composite_connect *io)
154 {
155         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
156         NTSTATUS status;
157
158         status = smb_raw_negotiate_recv(state->req);
159         NT_STATUS_NOT_OK_RETURN(status);
160
161         /* next step is a session setup */
162         state->session = smbcli_session_init(state->transport);
163         NT_STATUS_HAVE_NO_MEMORY(state->session);
164
165         /* get rid of the extra reference to the transport */
166         talloc_free(state->transport);
167
168         state->io_setup = talloc(c, struct smb_composite_sesssetup);
169         NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
170
171         /* prepare a session setup to establish a security context */
172         state->io_setup->in.sesskey      = state->transport->negotiate.sesskey;
173         state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
174         state->io_setup->in.domain       = io->in.domain;
175         state->io_setup->in.user         = io->in.user;
176         state->io_setup->in.password     = io->in.password;
177
178         state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
179         NT_STATUS_HAVE_NO_MEMORY(state->creq);
180
181         state->creq->async.fn = composite_handler;
182         state->creq->async.private = c;
183         c->stage = CONNECT_SESSION_SETUP;
184         
185         return NT_STATUS_OK;
186 }
187
188
189 /*
190   a session request operation has competed
191 */
192 static NTSTATUS connect_session_request(struct smbcli_composite *c, 
193                                         struct smb_composite_connect *io)
194 {
195         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
196         NTSTATUS status;
197
198         status = smbcli_transport_connect_recv(state->req);
199         NT_STATUS_NOT_OK_RETURN(status);
200
201         /* next step is a negprot */
202         return connect_send_negprot(c, io);
203 }
204
205 /*
206   a socket connection operation has competed
207 */
208 static NTSTATUS connect_socket(struct smbcli_composite *c, 
209                                struct smb_composite_connect *io)
210 {
211         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
212         NTSTATUS status;
213         struct nbt_name calling, called;
214
215         status = smbcli_sock_connect_recv(state->creq);
216         NT_STATUS_NOT_OK_RETURN(status);
217
218         /* the socket is up - we can initialise the smbcli transport layer */
219         state->transport = smbcli_transport_init(state->sock);
220         NT_STATUS_HAVE_NO_MEMORY(state->transport);
221
222         calling.name = io->in.calling_name;
223         calling.type = NBT_NAME_CLIENT;
224         calling.scope = NULL;
225
226         nbt_choose_called_name(state, &called, io->in.called_name, NBT_NAME_SERVER);
227
228         /* we have a connected socket - next step is a session
229            request, if needed. Port 445 doesn't need it, so it goes
230            straight to the negprot */
231         if (state->sock->port == 445) {
232                 status = nbt_name_dup(state->transport, &called, 
233                                       &state->transport->called);
234                 NT_STATUS_NOT_OK_RETURN(status);
235                 return connect_send_negprot(c, io);
236         }
237
238         state->req = smbcli_transport_connect_send(state->transport, &calling, &called);
239         NT_STATUS_HAVE_NO_MEMORY(state->req);
240
241         state->req->async.fn = request_handler;
242         state->req->async.private = c;
243         c->stage = CONNECT_SESSION_REQUEST;
244
245         return NT_STATUS_OK;
246 }
247
248
249 /*
250   called when name resolution is finished
251 */
252 static NTSTATUS connect_resolve(struct smbcli_composite *c, 
253                                 struct smb_composite_connect *io)
254 {
255         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
256         NTSTATUS status;
257         const char *address;
258
259         status = resolve_name_recv(state->creq, state, &address);
260         NT_STATUS_NOT_OK_RETURN(status);
261
262         state->creq = smbcli_sock_connect_send(state->sock, address, state->io->in.port);
263         NT_STATUS_HAVE_NO_MEMORY(state->creq);
264
265         c->stage = CONNECT_SOCKET;
266         state->creq->async.private = c;
267         state->creq->async.fn = composite_handler;
268
269         return NT_STATUS_OK;
270 }
271
272
273 /*
274   handle and dispatch state transitions
275 */
276 static void state_handler(struct smbcli_composite *c)
277 {
278         struct connect_state *state = talloc_get_type(c->private, struct connect_state);
279
280         switch (c->stage) {
281         case CONNECT_RESOLVE:
282                 c->status = connect_resolve(c, state->io);
283                 break;
284         case CONNECT_SOCKET:
285                 c->status = connect_socket(c, state->io);
286                 break;
287         case CONNECT_SESSION_REQUEST:
288                 c->status = connect_session_request(c, state->io);
289                 break;
290         case CONNECT_NEGPROT:
291                 c->status = connect_negprot(c, state->io);
292                 break;
293         case CONNECT_SESSION_SETUP:
294                 c->status = connect_session_setup(c, state->io);
295                 break;
296         case CONNECT_TCON:
297                 c->status = connect_tcon(c, state->io);
298                 break;
299         }
300
301         if (!NT_STATUS_IS_OK(c->status)) {
302                 c->state = SMBCLI_REQUEST_ERROR;
303                 if (c->async.fn) {
304                         c->async.fn(c);
305                 }
306         }
307 }
308
309
310 /*
311   handler for completion of a smbcli_request sub-request
312 */
313 static void request_handler(struct smbcli_request *req)
314 {
315         struct smbcli_composite *c = talloc_get_type(req->async.private, 
316                                                      struct smbcli_composite);
317         return state_handler(c);
318 }
319
320 /*
321   handler for completion of a smbcli_composite sub-request
322 */
323 static void composite_handler(struct smbcli_composite *req)
324 {
325         struct smbcli_composite *c = talloc_get_type(req->async.private, 
326                                                      struct smbcli_composite);
327         return state_handler(c);
328 }
329
330 /*
331   a function to establish a smbcli_tree from scratch
332 */
333 struct smbcli_composite *smb_composite_connect_send(struct smb_composite_connect *io)
334 {
335         struct smbcli_composite *c;
336         struct connect_state *state;
337         struct nbt_name name;
338
339         c = talloc_zero(NULL, struct smbcli_composite);
340         if (c == NULL) goto failed;
341
342         state = talloc(c, struct connect_state);
343         if (state == NULL) goto failed;
344
345         state->sock = smbcli_sock_init(state);
346         if (state->sock == NULL) goto failed;
347
348         state->io = io;
349
350         c->state = SMBCLI_REQUEST_SEND;
351         c->stage = CONNECT_RESOLVE;
352         c->event_ctx = state->sock->event.ctx;
353         c->private = state;
354
355         name.name = io->in.dest_host;
356         name.type = NBT_NAME_SERVER;
357         name.scope = NULL;
358
359         state->creq = resolve_name_send(&name, c->event_ctx);
360         if (state->creq == NULL) goto failed;
361
362         state->creq->async.private = c;
363         state->creq->async.fn = composite_handler;
364
365         return c;
366 failed:
367         talloc_free(c);
368         return NULL;
369 }
370
371 /*
372   recv half of async composite connect code
373 */
374 NTSTATUS smb_composite_connect_recv(struct smbcli_composite *c, TALLOC_CTX *mem_ctx)
375 {
376         NTSTATUS status;
377
378         status = smb_composite_wait(c);
379
380         if (NT_STATUS_IS_OK(status)) {
381                 struct connect_state *state = talloc_get_type(c->private, struct connect_state);
382                 talloc_steal(mem_ctx, state->io->out.tree);
383         }
384
385         talloc_free(c);
386         return status;
387 }
388
389 /*
390   sync version of smb_composite_connect 
391 */
392 NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx)
393 {
394         struct smbcli_composite *c = smb_composite_connect_send(io);
395         return smb_composite_connect_recv(c, mem_ctx);
396 }