r12089: Couple of fixes in cases of memory outage before we sort
[bbaumbach/samba-autobuild/.git] / source4 / 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 void continue_pipe_open_smb(struct composite_context *ctx)
47 {
48         struct composite_context *c = talloc_get_type(ctx->async.private_data,
49                                                       struct composite_context);
50         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
51                                                       struct pipe_np_smb_state);
52
53         c->status = dcerpc_pipe_open_smb_recv(ctx);
54         if (!NT_STATUS_IS_OK(c->status)) {
55
56                 DEBUG(0,("Failed to open pipe %s - %s\n", s->io.pipe_name, nt_errstr(c->status)));
57                 composite_error(c, c->status);
58                 return;
59         }
60
61         composite_done(c);
62 }
63
64
65 void continue_smb_connect(struct composite_context *ctx)
66 {
67         struct composite_context *open_ctx;
68         struct composite_context *c = talloc_get_type(ctx->async.private_data,
69                                                       struct composite_context);
70         struct pipe_np_smb_state *s = talloc_get_type(c->private_data,
71                                                       struct pipe_np_smb_state);
72         
73         c->status = smb_composite_connect_recv(ctx, c);
74         if (!NT_STATUS_IS_OK(c->status)) {
75
76                 DEBUG(0,("Failed to connect to %s - %s\n", s->io.binding->host, nt_errstr(c->status)));
77                 composite_error(c, c->status);
78                 return;
79         }
80
81         s->tree         = s->conn.out.tree;
82         s->io.pipe_name = s->io.binding->endpoint;
83
84         open_ctx = dcerpc_pipe_open_smb_send(s->io.pipe->conn, s->tree, s->io.pipe_name);
85         if (open_ctx == NULL) {
86                 composite_error(c, NT_STATUS_NO_MEMORY);
87                 return;
88         }
89
90         composite_continue(c, open_ctx, continue_pipe_open_smb, c);
91 }
92
93
94 /* open a rpc connection to a rpc pipe on SMB using the binding
95    structure to determine the endpoint and options */
96 struct composite_context *dcerpc_pipe_connect_ncacn_np_smb_send(TALLOC_CTX *tmp_ctx, 
97                                                                 struct dcerpc_pipe_connect *io)
98 {
99         struct composite_context *c;
100         struct pipe_np_smb_state *s;
101         struct composite_context *conn_req;
102         struct smb_composite_connect *conn;
103
104         c = talloc_zero(tmp_ctx, struct composite_context);
105         if (c == NULL) return NULL;
106
107         s = talloc_zero(c, struct pipe_np_smb_state);
108         if (s == NULL) {
109                 composite_error(c, NT_STATUS_NO_MEMORY);
110                 goto done;
111         }
112
113         c->state = COMPOSITE_STATE_IN_PROGRESS;
114         c->private_data = s;
115         c->event_ctx = io->pipe->conn->event_ctx;
116
117         s->io  = *io;
118         conn   = &s->conn;
119
120         conn->in.dest_host              = s->io.binding->host;
121         conn->in.port                   = 0;
122         conn->in.called_name            = strupper_talloc(tmp_ctx, s->io.binding->host);
123         conn->in.service                = "IPC$";
124         conn->in.service_type           = NULL;
125         conn->in.fallback_to_anonymous  = False;
126         conn->in.workgroup              = lp_workgroup();
127
128         if (s->io.binding->flags & DCERPC_SCHANNEL) {
129                 struct cli_credentials *anon_creds;
130
131                 anon_creds = cli_credentials_init(tmp_ctx);
132                 if (!anon_creds) {
133                         composite_error(c, NT_STATUS_NO_MEMORY);
134                         goto done;
135                 }
136
137                 cli_credentials_set_anonymous(anon_creds);
138                 cli_credentials_guess(anon_creds);
139
140                 s->conn.in.credentials = anon_creds;
141
142         } else {
143                 s->conn.in.credentials = s->io.creds;
144         }
145
146         conn_req = smb_composite_connect_send(conn, s->io.pipe->conn, s->io.pipe->conn->event_ctx);
147         if (!conn_req) {
148                 composite_error(c, NT_STATUS_NO_MEMORY);
149                 goto done;
150         }
151
152         composite_continue(c, conn_req, continue_smb_connect, c);
153
154 done:
155         return c;
156 }
157
158
159 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb_recv(struct composite_context *c)
160 {
161         NTSTATUS status = composite_wait(c);
162
163         talloc_free(c);
164         return status;
165 }
166
167
168 NTSTATUS dcerpc_pipe_connect_ncacn_np_smb(TALLOC_CTX *tmp_ctx,
169                                           struct dcerpc_pipe_connect *io)
170 {
171         struct composite_context *c;
172         c = dcerpc_pipe_connect_ncacn_np_smb_send(tmp_ctx, io);
173         return dcerpc_pipe_connect_ncacn_np_smb_recv(c);
174 }