2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
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.
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.
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.
22 #include "libcli/nbt/libnbt.h"
23 #include "libnet/libnet.h"
26 * Finds a domain pdc (generic part)
28 * @param ctx initialised libnet context
29 * @param mem_ctx memory context of this call
30 * @param r data structure containing necessary parameters and return values
31 * @return nt status of the call
34 static NTSTATUS libnet_find_pdc_generic(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
35 union libnet_find_pdc *r)
41 if (is_ipaddress(r->generic.in.domain_name)) {
42 r->generic.out.pdc_name = r->generic.in.domain_name;
46 make_nbt_name(&name, r->generic.in.domain_name, NBT_NAME_PDC);
48 status = resolve_name(&name, mem_ctx, &address);
49 if (!NT_STATUS_IS_OK(status)) {
50 name.type = NBT_NAME_SERVER;
51 status = resolve_name(&name, mem_ctx, &address);
53 NT_STATUS_NOT_OK_RETURN(status);
55 r->generic.out.pdc_name = talloc_strdup(mem_ctx, address);
62 * Finds a domain pdc function
64 * @param ctx initialised libnet context
65 * @param mem_ctx memory context of this call
66 * @param r data structure containing necessary parameters and return values
67 * @return nt status of the call
70 NTSTATUS libnet_find_pdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, union libnet_find_pdc *r)
72 switch (r->generic.level) {
73 case LIBNET_FIND_PDC_GENERIC:
74 return libnet_find_pdc_generic(ctx, mem_ctx, r);
77 return NT_STATUS_INVALID_LEVEL;
82 * Connects rpc pipe on remote server
84 * @param ctx initialised libnet context
85 * @param mem_ctx memory context of this call
86 * @param r data structure containing necessary parameters and return values
87 * @return nt status of the call
90 static NTSTATUS libnet_RpcConnectSrv(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
93 const char *binding = NULL;
95 binding = talloc_asprintf(mem_ctx, "ncacn_np:%s", r->in.domain_name);
97 status = dcerpc_pipe_connect(mem_ctx, &r->out.dcerpc_pipe,
98 binding, r->in.dcerpc_iface_uuid,r->in.dcerpc_iface_version,
99 ctx->cred, ctx->event_ctx);
101 if (!NT_STATUS_IS_OK(status)) {
102 r->out.error_string = talloc_asprintf(mem_ctx,
103 "dcerpc_pipe_connect to pipe %s failed with %s\n",
104 r->in.dcerpc_iface_name, binding);
108 r->out.error_string = NULL;
109 ctx->pipe = r->out.dcerpc_pipe;
116 * Connects rpc pipe on domain pdc
118 * @param ctx initialised libnet context
119 * @param mem_ctx memory context of this call
120 * @param r data structure containing necessary parameters and return values
121 * @return nt status of the call
124 static NTSTATUS libnet_RpcConnectPdc(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
127 struct libnet_RpcConnect r2;
128 struct libnet_Lookup f;
129 const char *address = talloc_array(ctx, const char, 16);
131 f.in.hostname = r->in.domain_name;
133 f.out.address = &address;
135 status = libnet_LookupPdc(ctx, mem_ctx, &f);
136 if (!NT_STATUS_IS_OK(status)) {
137 r->out.error_string = talloc_asprintf(mem_ctx, "libnet_LookupPdc failed: %s",
142 r2.level = LIBNET_RPC_CONNECT_SERVER;
143 r2.in.domain_name = talloc_strdup(mem_ctx, *f.out.address);
144 r2.in.dcerpc_iface_name = r->in.dcerpc_iface_name;
145 r2.in.dcerpc_iface_uuid = r->in.dcerpc_iface_uuid;
146 r2.in.dcerpc_iface_version = r->in.dcerpc_iface_version;
148 status = libnet_RpcConnect(ctx, mem_ctx, &r2);
150 r->out.dcerpc_pipe = r2.out.dcerpc_pipe;
151 r->out.error_string = r2.out.error_string;
153 ctx->pipe = r->out.dcerpc_pipe;
160 * Connects to rpc pipe on remote server or pdc
162 * @param ctx initialised libnet context
163 * @param mem_ctx memory context of this call
164 * @param r data structure containing necessary parameters and return values
165 * @return nt status of the call
168 NTSTATUS libnet_RpcConnect(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_RpcConnect *r)
173 case LIBNET_RPC_CONNECT_SERVER:
174 return libnet_RpcConnectSrv(ctx, mem_ctx, r);
175 case LIBNET_RPC_CONNECT_PDC:
176 return libnet_RpcConnectPdc(ctx, mem_ctx, r);
179 return NT_STATUS_INVALID_LEVEL;