summaryrefslogtreecommitdiff
path: root/source4/utils/net/drs/net_drs.c
blob: 1ba6eea5765b1d2ef3a06093cd75e80d0fbbee28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
   Samba Unix/Linux SMB client library

   Implements functions offered by repadmin.exe tool under Windows

   Copyright (C) Kamen Mazdrashki <kamen.mazdrashki@postpath.com> 2010

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "utils/net/net.h"
#include "librpc/gen_ndr/ndr_drsuapi_c.h"
#include "utils/net/drs/net_drs.h"
#include "lib/ldb/include/ldb.h"
#include "lib/ldb_wrap.h"
#include "system/filesys.h"


/**
 * 'net drs' supported sub-commands
 */
static const struct net_functable net_drs_functable[] = {
	{ "bind", "Display replication features for a domain controller\n", net_drs_bind_cmd, net_drs_bind_usage },
	{ "kcc", "Forces the KCC to recalculate replication topology for a specified domain controller\n",
			net_drs_kcc_cmd, net_drs_kcc_usage },
	{ "replicate", "Triggers replication event for the specified naming context between the source and destination domain controllers.\n",
			net_drs_replicate_cmd, net_drs_replicate_usage },
	{ NULL, NULL }
};

/**
 * 'net drs' entry point
 */
int net_drs(struct net_context *ctx, int argc, const char **argv)
{
	return net_run_function(ctx, argc, argv, net_drs_functable, net_drs_usage);
}

/**
 * 'net drs' usage message
 */
int net_drs_usage(struct net_context *ctx, int argc, const char **argv)
{
	d_printf("net drs <command> [options]\n");
	d_printf("\n");
	d_printf("Currently implemented commands:\n");
	d_printf("  bind      - Display DC replication features\n");
	d_printf("  kcc       - Forces the KCC to recalculate replication topology for a specified domain controller\n");
	d_printf("  replicate - Triggers replication event for the specified naming context between the source and destination domain controllers.\n");
	return 0;
}

/**
 * Create drsuapi connection to remote DC
 * and fill-in DC capabilities
 */
static bool net_drs_DsBind(struct net_drs_context *drs_ctx, struct net_drs_connection *conn)
{
	NTSTATUS status;
	struct GUID bind_guid;
	struct drsuapi_DsBind req;
	struct drsuapi_DsBindInfoCtr in_bind_ctr;
	union drsuapi_DsBindInfo *bind_info;

	SMB_ASSERT(conn->binding != NULL);

	status = dcerpc_pipe_connect_b(conn,
				       &conn->drs_pipe,
				       conn->binding,
				       &ndr_table_drsuapi,
				       drs_ctx->net_ctx->credentials,
				       drs_ctx->net_ctx->event_ctx,
				       drs_ctx->net_ctx->lp_ctx);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("Failed to connect to server: %s\n", nt_errstr(status));
		return false;
	}

	ZERO_STRUCT(in_bind_ctr);
	in_bind_ctr.length = 48;
	in_bind_ctr.info.info48.pid = (uint32_t)getpid();
	GUID_from_string(DRSUAPI_DS_BIND_GUID, &bind_guid);
	req.in.bind_guid = &bind_guid;
	req.in.bind_info = &in_bind_ctr;
	req.out.bind_handle = &conn->bind_handle;

	status = dcerpc_drsuapi_DsBind(conn->drs_pipe, conn, &req);
	if (!NT_STATUS_IS_OK(status)) {
		const char *errstr = nt_errstr(status);
		if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
			errstr = dcerpc_errstr(conn, conn->drs_pipe->last_fault_code);
		}
		d_printf("dcerpc_drsuapi_DsBind failed - %s\n", errstr);
		return false;
	} else if (!W_ERROR_IS_OK(req.out.result)) {
		d_printf("DsBind failed - %s\n", win_errstr(req.out.result));
		return false;
	}

	/* fill-in remote DC capabilities */
	ZERO_STRUCT(conn->info48);
	bind_info = &req.out.bind_info->info;
	conn->bind_info_len = req.out.bind_info->length;
	switch (conn->bind_info_len) {
	case 48:
		conn->info48.supported_extensions_ext = bind_info->info48.supported_extensions_ext;
		conn->info48.config_dn_guid = bind_info->info48.config_dn_guid;
	case 28:
		conn->info48.repl_epoch = bind_info->info28.repl_epoch;
	case 24:
		conn->info48.supported_extensions = bind_info->info24.supported_extensions;
		conn->info48.site_guid = bind_info->info24.site_guid;
		conn->info48.pid = bind_info->info24.pid;
		break;
	default:
		d_printf("Error: server returned BindInfo length %d", req.out.bind_info->length);
		return false;
	}

	return true;
}

/**
 * Close DRSUAPI connection to remote DC
 */
static bool net_drs_DsUnbind(struct net_drs_connection *conn)
{
	struct drsuapi_DsUnbind r;
	struct policy_handle bind_handle;

	SMB_ASSERT(conn->drs_pipe);

	ZERO_STRUCT(r);
	r.out.bind_handle = &bind_handle;

	r.in.bind_handle = &conn->bind_handle;
	dcerpc_drsuapi_DsUnbind(conn->drs_pipe, conn, &r);

	/* free dcerpc pipe in case we get called more than once */
	talloc_free(conn->drs_pipe);
	conn->drs_pipe = NULL;

	return true;
}

/**
 * Destroy drsuapi connection
 */
static int net_drs_connection_destructor(struct net_drs_connection *conn)
{
	if (conn->drs_pipe) {
		net_drs_DsUnbind(conn);
	}
	return 0;
}

/**
 * Create DRSUAPI connection to target DC
 * @return ptr to net_drs_connection or NULL on failure
 */
struct net_drs_connection * net_drs_connect_dc(struct net_drs_context *drs_ctx, const char *dc_name)
{
	struct net_drs_connection *conn = NULL;

	conn = talloc_zero(drs_ctx, struct net_drs_connection);
	NET_DRS_NOMEM_GOTO(conn, failed);

	/* init binding */
	conn->binding = talloc_zero(conn, struct dcerpc_binding);
	conn->binding->transport = NCACN_IP_TCP;
	conn->binding->flags = drs_ctx->drs_conn->binding->flags;
	conn->binding->host = talloc_strdup(conn, dc_name);
	conn->binding->target_hostname = conn->binding->host;

	if (!net_drs_DsBind(drs_ctx, conn)) {
		goto failed;
	}

	talloc_set_destructor(conn, net_drs_connection_destructor);

	return conn;

failed:
	talloc_free(conn);
	return NULL;
}

/**
 * Open secured LDAP connection to remote DC
 */
static bool net_drs_ldap_connect(struct net_drs_context *drs_ctx)
{
	char *url;
	bool bret = true;

	url = talloc_asprintf(drs_ctx, "ldap://%s/", drs_ctx->dc_name);
	if (!url) {
		d_printf(__location__ ": Have no memory");
		return false;
	}

	drs_ctx->ldap.ldb = ldb_wrap_connect(drs_ctx,
	                                     drs_ctx->net_ctx->event_ctx, drs_ctx->net_ctx->lp_ctx,
	                                     url,
	                                     NULL,
	                                     drs_ctx->net_ctx->credentials,
	                                     0);
	if (drs_ctx->ldap.ldb == NULL) {
		d_printf("Unable to connect to LDAP %s", url);
		bret = false;
	}

	talloc_free(url);

	return bret;
}

/**
 * fetch RootDSE record
 */
static bool net_drs_ldap_rootdse(struct net_drs_context *drs_ctx)
{
	int ret;
	struct ldb_result *r;
	struct ldb_dn *basedn;
	static const char *attrs[] = {
		"*",
		NULL
	};

	SMB_ASSERT(drs_ctx->ldap.ldb != NULL);

	basedn = ldb_dn_new(drs_ctx, drs_ctx->ldap.ldb, NULL);
	if (!basedn) {
		d_printf(__location__ ": No memory");
		return false;
	}

	ret = ldb_search(drs_ctx->ldap.ldb, drs_ctx, &r,
	                 basedn, LDB_SCOPE_BASE, attrs,
			 "(objectClass=*)");
	talloc_free(basedn);
	if (ret != LDB_SUCCESS) {
		d_printf("RootDSE search failed: %s", ldb_errstring(drs_ctx->ldap.ldb));
		talloc_free(r);
		return false;
	} else if (r->count != 1) {
		d_printf("RootDSE search returned more than one record!");
		talloc_free(r);
		return false;
	}

	drs_ctx->ldap.rootdse = r->msgs[0];

	return true;
}

/**
 * parses binding from command line
 * and gets target DC name
 */
static bool net_drs_parse_binding(struct net_drs_context *drs_ctx, const char *dc_binding)
{
	NTSTATUS status;
	struct dcerpc_binding *b;

	status = dcerpc_parse_binding(drs_ctx->drs_conn, dc_binding, &b);
	if (!NT_STATUS_IS_OK(status)) {
		d_printf("Bad binding supplied %s\n", dc_binding);
		return false;
	}

	b->transport = NCACN_IP_TCP;
	b->flags |= DCERPC_SIGN | DCERPC_SEAL;

	/* cache target DC name */
	drs_ctx->dc_name = b->target_hostname;

	drs_ctx->drs_conn->binding = b;

	return true;
}

/**
 * Free DRSUAPI connection upon net_drs_context
 * destruction
 */
static int net_drs_context_destructor(struct net_drs_context *drs_ctx)
{
	if (drs_ctx->drs_conn && drs_ctx->drs_conn->drs_pipe) {
		net_drs_DsUnbind(drs_ctx->drs_conn);
	}
	return 0;
}

/**
 * Create net_drs_context context to be used
 * by 'net drs' sub-commands
 */
bool net_drs_create_context(struct net_context *net_ctx,
			    const char *dc_binding,
			    struct net_drs_context **_drs_ctx)
{
	struct net_drs_context *drs_ctx;

	drs_ctx = talloc_zero(net_ctx, struct net_drs_context);
	if (!drs_ctx) {
		d_printf(__location__ ": No memory");
		return false;
	}

	drs_ctx->drs_conn = talloc_zero(drs_ctx, struct net_drs_connection);
	if (!drs_ctx->drs_conn) {
		d_printf(__location__ ": No memory");
		return false;
	}

	drs_ctx->net_ctx = net_ctx;

	if (!net_drs_parse_binding(drs_ctx, dc_binding)) {
		goto failed;
	}

	/* LDAP connect */
	if (!net_drs_ldap_connect(drs_ctx)) {
		goto failed;
	}
	/* fetch RootDSE */
	if (!net_drs_ldap_rootdse(drs_ctx)) {
		goto failed;
	}

	/* DRSUAPI connection */
	if (!net_drs_DsBind(drs_ctx, drs_ctx->drs_conn)) {
		goto failed;
	}

	/* set destructor to free any open connections */
	talloc_set_destructor(drs_ctx, net_drs_context_destructor);

	*_drs_ctx = drs_ctx;
	return true;

failed:
	talloc_free(drs_ctx);
	return false;
}