r12103: Comments and a bit of formatting.
[samba.git] / source / librpc / rpc / dcerpc_connect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc connect functions
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
9    Copyright (C) Rafal Szczesniak  2005
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26
27 #include "includes.h"
28 #include "system/network.h"
29 #include "librpc/gen_ndr/ndr_epmapper.h"
30 #include "librpc/gen_ndr/ndr_dcerpc.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "libcli/raw/libcliraw.h"
33 #include "libcli/composite/composite.h"
34 #include "libcli/smb_composite/smb_composite.h"
35
36
37 struct dcerpc_pipe_connect;
38
39 struct pipe_np_smb_state {
40         struct smb_composite_connect conn;
41         struct smbcli_tree *tree;
42         struct dcerpc_pipe_connect io;
43 };
44
45
46 /*
47   Stage 3 of ncacn_np_smb: Named pipe opened (?)
48 */
49 void continue_pipe_open_smb(struct composite_context *ctx)
50 {
51         struct composite_context *c = talloc_get_type(ctx->async.private_data,
52                                                       struct composite_context);
53         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
54                                                       struct pipe_np_smb_state);
55         /* receive result of named pipe open request */
56         c->status = dcerpc_pipe_open_smb_recv(ctx);
57         if (!NT_STATUS_IS_OK(c->status)) {
58                 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
59                 composite_error(c, c->status);
60                 return;
61         }
62
63         composite_done(c);
64 }
65
66 /*
67   Stage 2 of ncacn_np_smb: Open a named pipe after successful smb connection
68 */
69 void continue_smb_connect(struct composite_context *ctx)
70 {
71         struct composite_context *open_ctx;
72         struct composite_context *c = talloc_get_type(ctx->async.private_data,
73                                                       struct composite_context);
74         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
75                                                       struct pipe_np_smb_state);
76         
77         /* receive result of smb connect request */
78         c->status = smb_composite_connect_recv(ctx, c);
79         if (!NT_STATUS_IS_OK(c->status)) {
80                 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
81                 composite_error(c, c->status);
82                 return;
83         }
84
85         /* prepare named pipe open parameters */
86         s->tree         = s->conn.out.tree;
87         s->io.pipe_name = s->io.binding->endpoint;
88
89         /* send named pipe open request */
90         open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
91         if (open_ctx == NULL) {
92                 composite_error(c, NT_STATUS_NO_MEMORY);
93                 return;
94         }
95
96         composite_continue(c, open_ctx, continue_pipe_open_smb, c);
97 }
98
99
100 /*
101   Initiate async open of a rpc connection to a rpc pipe on SMB using
102   the binding structure to determine the endpoint and options
103 */
104 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *tmp_ctx, 
105                                                                 struct dcerpc_pipe_connect *io)
106 {
107         struct composite_context *c;
108         struct pipe_np_smb_state *s;
109         struct composite_context *conn_req;
110         struct smb_composite_connect *conn;
111
112         /* composite context allocation and setup */
113         c = talloc_zero(tmp_ctx, struct composite_context);
114         if (c == NULL) return NULL;
115
116         s = talloc_zero(c, struct pipe_np_smb_state);
117         if (s == NULL) {
118                 composite_error(c, NT_STATUS_NO_MEMORY);
119                 goto done;
120         }
121
122         c->state = COMPOSITE_STATE_IN_PROGRESS;
123         c->private_data = s;
124         c->event_ctx = io->pipe->conn->event_ctx;
125
126         s->io  = *io;
127         conn   = &s->conn;
128
129         /* prepare smb connection parameters: we're connecting to IPC$ share on
130            remote rpc server */
131         conn->in.dest_host              = s->io.binding->host;
132         conn->in.port                   = 0;
133         conn->in.called_name            = strupper_talloc(tmp_ctx, s->io.binding->host);
134         conn->in.service                = "IPC$";
135         conn->in.service_type           = NULL;
136         conn->in.fallback_to_anonymous  = False;
137         conn->in.workgroup              = lp_workgroup();
138
139         /*
140          * provide proper credentials - user supplied or anonymous in case this is
141          * schannel connection
142          */
143         if (s->io.binding->flags & DCERPC_SCHANNEL) {
144                 struct cli_credentials *anon_creds;
145
146                 anon_creds = cli_credentials_init(tmp_ctx);
147                 if (!anon_creds) {
148                         composite_error(c, NT_STATUS_NO_MEMORY);
149                         goto done;
150                 }
151
152                 cli_credentials_set_anonymous(anon_creds);
153                 cli_credentials_guess(anon_creds);
154
155                 s->conn.in.credentials = anon_creds;
156
157         } else {
158                 s->conn.in.credentials = s->io.creds;
159         }
160
161         /* send smb connect request */
162         conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
163         if (!conn_req) {
164                 composite_error(c, NT_STATUS_NO_MEMORY);
165                 goto done;
166         }
167
168         composite_continue(c, conn_req, continue_smb_connect, c);
169
170 done:
171         return c;
172 }
173
174
175 /*
176   Receive result of a rpc connection to a rpc pipe on SMB
177 */
178 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
179 {
180         NTSTATUS status = composite_wait(c);
181
182         talloc_free(c);
183         return status;
184 }
185
186
187 /*
188   Sync version of a rpc connection to a rpc pipe on SMB
189 */
190 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *tmp_ctx,
191                                           struct dcerpc_pipe_connect *io)
192 {
193         struct composite_context *c;
194         c = dcerpc_pipe_connect_ncacn_np_smb_send(tmp_ctx, io);
195         return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
196 }