2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Rafal Szczesniak 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "libnet/libnet.h"
24 #include "libcli/libcli.h"
25 #include "librpc/gen_ndr/ndr_lsa.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
29 * Connects rpc pipe on remote server
31 * @param ctx initialised libnet context
32 * @param mem_ctx memory context of this call
33 * @param r data structure containing necessary parameters and return values
34 * @return nt status of the call
37 static NTSTATUS libnet_RpcConnectSrv(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
40 const char *binding = NULL;
42 /* prepare binding string */
44 case LIBNET_RPC_CONNECT_DC:
45 case LIBNET_RPC_CONNECT_PDC:
46 case LIBNET_RPC_CONNECT_SERVER:
47 binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", r->in.name);
50 case LIBNET_RPC_CONNECT_BINDING:
51 binding = r->in.binding;
55 /* connect to remote dcerpc pipe */
56 status = dcerpc_pipe_connect(mem_ctx, &r->out.dcerpc_pipe,
57 binding, r->in.dcerpc_iface,
58 ctx->cred, ctx->event_ctx);
60 if (!NT_STATUS_IS_OK(status)) {
61 r->out.error_string = talloc_asprintf(mem_ctx,
62 "dcerpc_pipe_connect to pipe %s[%s] failed with %s\n",
63 r->in.dcerpc_iface->name, binding, nt_errstr(status));
67 r->out.error_string = NULL;
68 ctx->pipe = r->out.dcerpc_pipe;
75 * Connects rpc pipe on domain pdc
77 * @param ctx initialised libnet context
78 * @param mem_ctx memory context of this call
79 * @param r data structure containing necessary parameters and return values
80 * @return nt status of the call
83 static NTSTATUS libnet_RpcConnectDC(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
86 struct libnet_RpcConnect r2;
87 struct libnet_LookupDCs f;
88 const char *connect_name;
90 f.in.domain_name = r->in.name;
92 case LIBNET_RPC_CONNECT_PDC:
93 f.in.name_type = NBT_NAME_PDC;
95 case LIBNET_RPC_CONNECT_DC:
96 f.in.name_type = NBT_NAME_LOGON;
104 /* find the domain pdc first */
105 status = libnet_LookupDCs(ctx, mem_ctx, &f);
106 if (!NT_STATUS_IS_OK(status)) {
107 r->out.error_string = talloc_asprintf(mem_ctx, "libnet_LookupDCs failed: %s",
112 /* we might not have got back a name. Fall back to the IP */
113 if (f.out.dcs[0].name) {
114 connect_name = f.out.dcs[0].name;
116 connect_name = f.out.dcs[0].address;
119 /* ok, pdc has been found so do attempt to rpc connect */
120 r2.level = LIBNET_RPC_CONNECT_SERVER;
122 /* This will cause yet another name resolution, but at least
123 * we pass the right name down the stack now */
124 r2.in.name = talloc_strdup(mem_ctx, connect_name);
125 r2.in.dcerpc_iface = r->in.dcerpc_iface;
127 status = libnet_RpcConnect(ctx, mem_ctx, &r2);
129 r->out.dcerpc_pipe = r2.out.dcerpc_pipe;
130 r->out.error_string = r2.out.error_string;
132 ctx->pipe = r->out.dcerpc_pipe;
139 * Connects to rpc pipe on remote server or pdc
141 * @param ctx initialised libnet context
142 * @param mem_ctx memory context of this call
143 * @param r data structure containing necessary parameters and return values
144 * @return nt status of the call
147 NTSTATUS libnet_RpcConnect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
150 case LIBNET_RPC_CONNECT_SERVER:
151 return libnet_RpcConnectSrv(ctx, mem_ctx, r);
153 case LIBNET_RPC_CONNECT_BINDING:
154 return libnet_RpcConnectSrv(ctx, mem_ctx, r);
156 case LIBNET_RPC_CONNECT_PDC:
157 case LIBNET_RPC_CONNECT_DC:
158 return libnet_RpcConnectDC(ctx, mem_ctx, r);
161 return NT_STATUS_INVALID_LEVEL;
165 * Connects to rpc pipe on remote server or pdc, and returns info on the domain name, domain sid and realm
167 * @param ctx initialised libnet context
168 * @param r data structure containing necessary parameters and return values. Must be a talloc context
169 * @return nt status of the call
172 NTSTATUS libnet_RpcConnectDCInfo(struct libnet_context *ctx,
173 struct libnet_RpcConnectDCInfo *r)
178 struct libnet_RpcConnect *c;
179 struct lsa_ObjectAttribute attr;
180 struct lsa_QosInfo qos;
181 struct lsa_OpenPolicy2 lsa_open_policy;
182 struct policy_handle lsa_p_handle;
183 struct lsa_QueryInfoPolicy2 lsa_query_info2;
184 struct lsa_QueryInfoPolicy lsa_query_info;
186 struct dcerpc_pipe *lsa_pipe;
188 struct dcerpc_binding *final_binding;
189 struct dcerpc_pipe *final_pipe;
191 tmp_ctx = talloc_new(r);
193 r->out.error_string = NULL;
194 return NT_STATUS_NO_MEMORY;
197 c = talloc(tmp_ctx, struct libnet_RpcConnect);
199 r->out.error_string = NULL;
200 talloc_free(tmp_ctx);
201 return NT_STATUS_NO_MEMORY;
205 if (r->level != LIBNET_RPC_CONNECT_BINDING) {
206 c->in.name = r->in.name;
208 c->in.binding = r->in.binding;
211 c->in.dcerpc_iface = &dcerpc_table_lsarpc;
213 /* connect to the LSA pipe of the PDC */
215 status = libnet_RpcConnect(ctx, c, c);
216 if (!NT_STATUS_IS_OK(status)) {
217 if (r->level != LIBNET_RPC_CONNECT_BINDING) {
218 r->out.error_string = talloc_asprintf(r,
219 "Connection to LSA pipe of DC failed: %s",
220 c->out.error_string);
222 r->out.error_string = talloc_asprintf(r,
223 "Connection to LSA pipe with binding '%s' failed: %s",
224 r->in.binding, c->out.error_string);
226 talloc_free(tmp_ctx);
229 lsa_pipe = c->out.dcerpc_pipe;
231 /* Get an LSA policy handle */
233 ZERO_STRUCT(lsa_p_handle);
235 qos.impersonation_level = 2;
236 qos.context_mode = 1;
237 qos.effective_only = 0;
240 attr.root_dir = NULL;
241 attr.object_name = NULL;
243 attr.sec_desc = NULL;
246 lsa_open_policy.in.attr = &attr;
248 lsa_open_policy.in.system_name = talloc_asprintf(tmp_ctx, "\\");
249 if (!lsa_open_policy.in.system_name) {
250 r->out.error_string = NULL;
251 talloc_free(tmp_ctx);
252 return NT_STATUS_NO_MEMORY;
255 lsa_open_policy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
256 lsa_open_policy.out.handle = &lsa_p_handle;
258 status = dcerpc_lsa_OpenPolicy2(lsa_pipe, tmp_ctx, &lsa_open_policy);
260 /* This now fails on ncacn_ip_tcp against Win2k3 SP1 */
261 if (NT_STATUS_IS_OK(status)) {
262 /* Look to see if this is ADS (a fault indicates NT4 or Samba 3.0) */
264 lsa_query_info2.in.handle = &lsa_p_handle;
265 lsa_query_info2.in.level = LSA_POLICY_INFO_DNS;
267 status = dcerpc_lsa_QueryInfoPolicy2(lsa_pipe, tmp_ctx,
270 if (!NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
271 if (!NT_STATUS_IS_OK(status)) {
272 r->out.error_string = talloc_asprintf(r,
273 "lsa_QueryInfoPolicy2 failed: %s",
275 talloc_free(tmp_ctx);
278 r->out.realm = lsa_query_info2.out.info->dns.dns_domain.string;
279 r->out.guid = talloc(r, struct GUID);
281 r->out.error_string = NULL;
282 return NT_STATUS_NO_MEMORY;
284 *r->out.guid = lsa_query_info2.out.info->dns.domain_guid;
290 /* Grab the domain SID (regardless of the result of the previous call */
292 lsa_query_info.in.handle = &lsa_p_handle;
293 lsa_query_info.in.level = LSA_POLICY_INFO_DOMAIN;
295 status = dcerpc_lsa_QueryInfoPolicy(lsa_pipe, tmp_ctx,
298 if (!NT_STATUS_IS_OK(status)) {
299 r->out.error_string = talloc_asprintf(r,
300 "lsa_QueryInfoPolicy2 failed: %s",
302 talloc_free(tmp_ctx);
306 r->out.domain_sid = lsa_query_info.out.info->domain.sid;
307 r->out.domain_name = lsa_query_info.out.info->domain.name.string;
309 /* Cause the code further down to try this with just SAMR */
310 r->out.domain_sid = NULL;
311 r->out.domain_name = NULL;
315 /* Find the original binding string */
316 final_binding = talloc(tmp_ctx, struct dcerpc_binding);
317 if (!final_binding) {
318 return NT_STATUS_NO_MEMORY;
320 *final_binding = *lsa_pipe->binding;
321 /* Ensure we keep hold of the member elements */
322 talloc_reference(final_binding, lsa_pipe->binding);
324 /* Make binding string for samr, not the other pipe */
325 status = dcerpc_epm_map_binding(tmp_ctx, final_binding,
327 lsa_pipe->conn->event_ctx);
328 if (!NT_STATUS_IS_OK(status)) {
329 r->out.error_string = talloc_asprintf(r,
330 "Failed to map pipe with endpoint mapper - %s",
332 talloc_free(tmp_ctx);
336 /* Now that we have the info setup a final connection to the pipe they wanted */
337 status = dcerpc_secondary_connection(lsa_pipe, &final_pipe, final_binding);
338 if (!NT_STATUS_IS_OK(status)) {
339 r->out.error_string = talloc_asprintf(r,
340 "secondary connection failed: %s",
342 talloc_free(tmp_ctx);
345 r->out.dcerpc_pipe = final_pipe;
347 talloc_steal(r, r->out.realm);
348 talloc_steal(r, r->out.domain_sid);
349 talloc_steal(r, r->out.domain_name);
350 talloc_steal(r, r->out.dcerpc_pipe);
352 /* This should close the LSA pipe, which we don't need now we have the info */
353 talloc_free(tmp_ctx);