r23141: Use the finddcs() library call rather than a winbind-specific version.
[ira/wip.git] / source / winbind / wb_connect_sam.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Connect to the SAMR pipe, and return connection and domain handles.
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26
27 #include "libcli/raw/libcliraw.h"
28 #include "libcli/security/security.h"
29 #include "librpc/gen_ndr/ndr_samr_c.h"
30 #include "winbind/wb_server.h"
31
32
33 /* Helper to initialize SAMR with a specific auth methods. Verify by opening
34  * the SAM handle */
35
36 struct connect_samr_state {
37         struct composite_context *ctx;
38         struct dom_sid *sid;
39
40         struct dcerpc_pipe *samr_pipe;
41         struct policy_handle *connect_handle;
42         struct policy_handle *domain_handle;
43
44         struct samr_Connect2 c;
45         struct samr_OpenDomain o;
46 };
47
48 static void connect_samr_recv_pipe(struct composite_context *ctx);
49 static void connect_samr_recv_conn(struct rpc_request *req);
50 static void connect_samr_recv_open(struct rpc_request *req);
51
52 struct composite_context *wb_connect_samr_send(TALLOC_CTX *mem_ctx,
53                                            struct wbsrv_domain *domain)
54 {
55         struct composite_context *result, *ctx;
56         struct connect_samr_state *state;
57
58         result = composite_create(mem_ctx, domain->netlogon_pipe->conn->event_ctx);
59         if (result == NULL) goto failed;
60
61         state = talloc(result, struct connect_samr_state);
62         if (state == NULL) goto failed;
63         state->ctx = result;
64         result->private_data = state;
65
66         state->sid = dom_sid_dup(state, domain->info->sid);
67         if (state->sid == NULL) goto failed;
68
69         /* this will make the secondary connection on the same IPC$ share, 
70            secured with SPNEGO, NTLMSSP or SCHANNEL */
71         ctx = dcerpc_secondary_connection_send(domain->netlogon_pipe,
72                                                domain->samr_binding);
73         composite_continue(state->ctx, ctx, connect_samr_recv_pipe, state);
74         return result;
75         
76  failed:
77         talloc_free(result);
78         return NULL;
79 }
80
81 static void connect_samr_recv_pipe(struct composite_context *ctx)
82 {
83         struct rpc_request *req;
84         struct connect_samr_state *state =
85                 talloc_get_type(ctx->async.private_data,
86                                 struct connect_samr_state);
87
88         state->ctx->status = dcerpc_secondary_connection_recv(ctx, 
89                                                               &state->samr_pipe);
90         if (!composite_is_ok(state->ctx)) return;
91                         
92         state->connect_handle = talloc(state, struct policy_handle);
93         if (composite_nomem(state->connect_handle, state->ctx)) return;
94
95         state->c.in.system_name =
96                 talloc_asprintf(state, "\\\\%s",
97                                 dcerpc_server_name(state->samr_pipe));
98         state->c.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
99         state->c.out.connect_handle = state->connect_handle;
100
101         req = dcerpc_samr_Connect2_send(state->samr_pipe, state, &state->c);
102         composite_continue_rpc(state->ctx, req, connect_samr_recv_conn, state);
103         return;
104 }
105
106 static void connect_samr_recv_conn(struct rpc_request *req)
107 {
108         struct connect_samr_state *state =
109                 talloc_get_type(req->async.private_data,
110                                 struct connect_samr_state);
111
112         state->ctx->status = dcerpc_ndr_request_recv(req);
113         if (!composite_is_ok(state->ctx)) return;
114         state->ctx->status = state->c.out.result;
115         if (!composite_is_ok(state->ctx)) return;
116
117         state->domain_handle = talloc(state, struct policy_handle);
118         if (composite_nomem(state->domain_handle, state->ctx)) return;
119
120         state->o.in.connect_handle = state->connect_handle;
121         state->o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
122         state->o.in.sid = state->sid;
123         state->o.out.domain_handle = state->domain_handle;
124
125         req = dcerpc_samr_OpenDomain_send(state->samr_pipe, state, &state->o);
126         composite_continue_rpc(state->ctx, req,
127                                connect_samr_recv_open, state);
128 }
129
130 static void connect_samr_recv_open(struct rpc_request *req)
131 {
132         struct connect_samr_state *state =
133                 talloc_get_type(req->async.private_data,
134                                 struct connect_samr_state);
135
136         state->ctx->status = dcerpc_ndr_request_recv(req);
137         if (!composite_is_ok(state->ctx)) return;
138         state->ctx->status = state->o.out.result;
139         if (!composite_is_ok(state->ctx)) return;
140
141         composite_done(state->ctx);
142 }
143
144 NTSTATUS wb_connect_samr_recv(struct composite_context *c,
145                              TALLOC_CTX *mem_ctx,
146                              struct dcerpc_pipe **samr_pipe,
147                              struct policy_handle **connect_handle,
148                              struct policy_handle **domain_handle)
149 {
150         NTSTATUS status = composite_wait(c);
151         if (NT_STATUS_IS_OK(status)) {
152                 struct connect_samr_state *state =
153                         talloc_get_type(c->private_data,
154                                         struct connect_samr_state);
155                 *samr_pipe = talloc_steal(mem_ctx, state->samr_pipe);
156                 *connect_handle = talloc_steal(mem_ctx, state->connect_handle);
157                 *domain_handle = talloc_steal(mem_ctx, state->domain_handle);
158         }
159         talloc_free(c);
160         return status;
161 }
162