summaryrefslogtreecommitdiff
path: root/source4/dsdb/kcc/kcc_topology.c
blob: 571bd8482eedb225fe5ee5ab297aca528285a57c (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
   Unix SMB/CIFS implementation.

   KCC service

   Copyright (C) Crístian Deives 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 "dsdb/samdb/samdb.h"
#include "lib/messaging/irpc.h"
#include "librpc/gen_ndr/ndr_misc.h"

#define NTDSTRANSPORT_OPT_IGNORE_SCHEDULES 0x00000001
#define NTDSTRANSPORT_OPT_BRIDGES_REQUIRED 0x00000002

/** replication parameters of a graph edge */
struct kcctpl_repl_info {
	int cost;
	int interval;
	int options;
	uint8_t schedule[84];
};

/** color of a vertex */
enum kcctpl_color { RED, BLACK, WHITE };

/** a GUID array list */
struct GUID_list {
	struct GUID *data;
	unsigned int count;
};

/** a vertex in the site graph */
struct kcctpl_vertex {
	struct GUID id;
	struct GUID_list edge_ids;
	enum kcctpl_color color;
	struct GUID_list accept_red_red;
	struct GUID_list accept_black;
	struct kcctpl_repl_info repl_info;
	int dist_to_red;

	/* Dijkstra data */
	struct GUID root_id;
	bool demoted;

	/* Kruskal data */
	struct GUID component_id;
	int component_index;
};

/** fully connected subgraph of vertices */
struct kcctpl_multi_edge {
	struct GUID id;
	struct GUID_list vertex_ids;
	struct GUID type;
	struct kcctpl_repl_info repl_info;
	bool directed;
};

/** set of transitively connected kcc_multi_edge's. all edges within the set
 * have the same type. */
struct kcctpl_multi_edge_set {
	struct GUID id;
	struct GUID_list edge_ids;
};

/** a vertices array list */
struct kcctpl_vertex_list {
	struct kcctpl_vertex *data;
	unsigned int count;
};

/** an edges linked list */
struct kcctpl_multi_edge_list {
	struct kcctpl_multi_edge *data;
	unsigned int count;
};

/** an edge sets linked list */
struct kcctpl_multi_edge_set_list {
	struct kcctpl_multi_edge_set *data;
	unsigned int count;
};

/** a site graph */
struct kcctpl_graph {
	struct kcctpl_vertex_list vertices;
	struct kcctpl_multi_edge_list edges;
	struct kcctpl_multi_edge_set_list edge_sets;
};

/** path found in the graph between two non-white vertices */
struct kcctpl_internal_edge {
	struct GUID v1id, v2id;
	bool red_red;
	struct kcctpl_repl_info repl_info;
	struct GUID type;
};

/**
 * find a graph edge based on its GUID.
 */
static struct kcctpl_multi_edge *kcctpl_find_edge_by_guid(struct kcctpl_graph *graph,
							  struct GUID guid)
{
	unsigned int i;

	for (i = 0; i < graph->edges.count; i++) {
		if (GUID_compare(&graph->edges.data[i].id, &guid) == 0) {
			return &graph->edges.data[i];
		}
	}
	return NULL;
}
/**
 * create a kcctpl_graph instance.
 */
static NTSTATUS kcctpl_create_graph(TALLOC_CTX *mem_ctx,
				    struct GUID_list *guids,
				    struct kcctpl_graph **_graph)
{
	struct kcctpl_graph *graph;
	unsigned int i;

	graph = talloc_zero(mem_ctx, struct kcctpl_graph);
	NT_STATUS_HAVE_NO_MEMORY(graph);

	graph->vertices.count = guids->count;
	graph->vertices.data = talloc_zero_array(graph, struct kcctpl_vertex,
						 guids->count);
	NT_STATUS_HAVE_NO_MEMORY_AND_FREE(graph->vertices.data, graph);

	TYPESAFE_QSORT(guids->data, guids->count, GUID_compare);

	for (i = 0; i < guids->count; i++) {
		graph->vertices.data[i].id = guids->data[i];
	}

	*_graph = graph;
	return NT_STATUS_OK;
}

/**
 * create a kcctpl_multi_edge instance.
 */
static NTSTATUS kcctpl_create_edge(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
				   struct GUID type,
				   struct ldb_message *site_link,
				   struct kcctpl_multi_edge **_edge)
{
	struct kcctpl_multi_edge *edge;
	TALLOC_CTX *tmp_ctx;
	struct ldb_result *res;
	const char * const attrs[] = { "siteList", NULL };
	int ret;
	struct ldb_message_element *el;
	unsigned int i;
	struct ldb_val val;

	edge = talloc(mem_ctx, struct kcctpl_multi_edge);
	NT_STATUS_HAVE_NO_MEMORY(edge);

	edge->id = samdb_result_guid(site_link, "objectGUID");

	tmp_ctx = talloc_new(mem_ctx);
	ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_BASE, attrs,
			 "objectGUID=%s", GUID_string(tmp_ctx, &edge->id));
	if (ret != LDB_SUCCESS) {
		DEBUG(0, ("failed to find siteLink object %s: %s\n",
			  GUID_string(tmp_ctx, &edge->id), ldb_strerror(ret)));
		talloc_free(edge);
		talloc_free(tmp_ctx);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}
	if (res->count == 0) {
		DEBUG(0, ("failed to find siteLink object %s\n",
			  GUID_string(tmp_ctx, &edge->id)));
		talloc_free(edge);
		talloc_free(tmp_ctx);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	el = ldb_msg_find_element(res->msgs[0], "siteList");
	edge->vertex_ids.count = el->num_values;
	edge->vertex_ids.data = talloc_array(edge, struct GUID, el->num_values);
	if (!edge->vertex_ids.data) {
		talloc_free(edge);
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	for (i = 0; i < el->num_values; i++) {
		struct ldb_dn *dn;
		struct GUID guid;

		val = el->values[i];
		dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &val);
		ret = dsdb_find_guid_by_dn(ldb, dn, &guid);
		if (ret != LDB_SUCCESS) {
			DEBUG(0, ("failed to find objectGUID for object %s: "
				  "%s\n", ldb_dn_get_linearized(dn),
				  ldb_strerror(ret)));
			talloc_free(edge);
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		edge->vertex_ids.data = talloc_realloc(edge,
						       edge->vertex_ids.data,
						       struct GUID,
						       edge->vertex_ids.count + 1);
		if (!edge->vertex_ids.data) {
			talloc_free(edge);
			talloc_free(tmp_ctx);
			return NT_STATUS_NO_MEMORY;
		}
		edge->vertex_ids.data[edge->vertex_ids.count] = guid;
		edge->vertex_ids.count++;
	}

	edge->repl_info.cost = samdb_result_int64(site_link, "cost", 0);
	edge->repl_info.options = samdb_result_int64(site_link, "options", 0);
	edge->repl_info.interval = samdb_result_int64(site_link, "replInterval",
						      0);
	/* val = ldb_msg_find_ldb_val(site_link, "schedule");
	edge->repl_info.schedule = val->data; */
	edge->type = type;
	edge->directed = false;

	*_edge = edge;
	talloc_free(tmp_ctx);
	return NT_STATUS_OK;
}

/**
 * create a kcctpl_multi_edge_set instance containing edges for all siteLink
 * objects.
 */
static NTSTATUS kcctpl_create_auto_edge_set(struct kcctpl_graph *graph,
					    struct GUID type,
					    struct ldb_result *res_site_link,
					    struct kcctpl_multi_edge_set **_set)
{
	struct kcctpl_multi_edge_set *set;
	unsigned int i;

	set = talloc_zero(graph, struct kcctpl_multi_edge_set);
	NT_STATUS_HAVE_NO_MEMORY(set);

	for (i = 0; i < res_site_link->count; i++) {
		struct GUID site_link_guid;
		struct kcctpl_multi_edge *edge;

		site_link_guid = samdb_result_guid(res_site_link->msgs[i],
						   "objectGUID");
		edge = kcctpl_find_edge_by_guid(graph, site_link_guid);
		if (!edge) {
			TALLOC_CTX *tmp_ctx = talloc_new(graph);
			DEBUG(0, ("failed to find a graph edge with ID=%s\n",
				  GUID_string(tmp_ctx, &site_link_guid)));
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		if (GUID_compare(&edge->type, &type) == 0) {
			set->edge_ids.data = talloc_realloc(set,
							    set->edge_ids.data,
							    struct GUID,
							    set->edge_ids.count + 1);
			NT_STATUS_HAVE_NO_MEMORY_AND_FREE(set->edge_ids.data,
							  set);
			set->edge_ids.data[set->edge_ids.count] = site_link_guid;
			set->edge_ids.count++;
		}
	}

	*_set = set;
	return NT_STATUS_OK;
}

/**
 * create a kcctpl_multi_edge_set instance.
 */
static NTSTATUS kcctpl_create_edge_set(struct ldb_context *ldb,
				       struct kcctpl_graph *graph,
				       struct GUID type,
				       struct ldb_message *bridge,
				       struct kcctpl_multi_edge_set **_set)
{
	struct kcctpl_multi_edge_set *set;
	struct ldb_message_element *el;
	unsigned int i;

	set = talloc_zero(graph, struct kcctpl_multi_edge_set);
	NT_STATUS_HAVE_NO_MEMORY(set);

	set->id = samdb_result_guid(bridge, "objectGUID");

	el = ldb_msg_find_element(bridge, "siteLinkList");
	for (i = 0; i < el->num_values; i++) {
		struct ldb_val val;
		TALLOC_CTX *tmp_ctx;
		struct ldb_dn *dn;
		struct GUID site_link_guid;
		int ret;
		struct kcctpl_multi_edge *edge;

		val = el->values[i];
		tmp_ctx = talloc_new(graph);
		dn = ldb_dn_from_ldb_val(tmp_ctx, ldb, &val);
		if (!ldb_dn_validate(dn)) {
			DEBUG(0, ("invalid DN in siteLinkList attr of %s\n",
				  GUID_string(tmp_ctx, &set->id)));
			talloc_free(set);
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		ret = dsdb_find_guid_by_dn(ldb, dn, &site_link_guid);
		if (ret != LDB_SUCCESS) {
			DEBUG(0, ("failed to find objectGUID for object %s: "
				  "%s\n", ldb_dn_get_linearized(dn),
				  ldb_strerror(ret)));
			talloc_free(set);
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		edge = kcctpl_find_edge_by_guid(graph, site_link_guid);
		if (!edge) {
			DEBUG(0, ("failed to find a graph edge with ID=%s\n",
				  GUID_string(tmp_ctx, &site_link_guid)));
			talloc_free(set);
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		talloc_free(tmp_ctx);

		if (GUID_compare(&edge->type, &type) == 0) {
			set->edge_ids.data = talloc_realloc(set,
							    set->edge_ids.data,
							    struct GUID,
							    set->edge_ids.count + 1);
			NT_STATUS_HAVE_NO_MEMORY_AND_FREE(set->edge_ids.data,
							  set);
			set->edge_ids.data[set->edge_ids.count] = site_link_guid;
			set->edge_ids.count++;
		}
	}

	*_set = set;
	return NT_STATUS_OK;
}

/**
 * set up a kcctpl_graph, populated with a kcctpl_vertex for each site object, a
 * kcctpl_multi_edge for each siteLink object, and a kcctpl_multi_edge_set for
 * each siteLinkBridge object (or implied siteLinkBridge).
 */
static NTSTATUS kcctpl_setup_graph(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
				   struct kcctpl_graph **_graph)
{
	struct kcctpl_graph *graph;
	struct ldb_dn *config_dn, *base_dn;
	TALLOC_CTX *tmp_ctx;
	bool ok;
	struct ldb_result *res;
	const char * const site_link_attrs[] = { "objectGUID", NULL };
	const char * const inter_site_transport_attrs[] = { "objectGUID",
							    "distinguishedName",
							    NULL };
	const char * const attrs[] = { "objectGUID", "cost", "options",
				       "replInterval", "schedule", NULL };
	const char * const site_link_bridge_attrs[] = { "objectGUID",
							"siteLinkList",
							NULL };
	int ret;
	struct GUID_list vertex_ids;
	unsigned int i;
	NTSTATUS status;

	config_dn = samdb_config_dn(ldb);
	if (!config_dn) {
		DEBUG(0, ("failed to find our own Config DN\n"));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	tmp_ctx = talloc_new(mem_ctx);
	base_dn = ldb_dn_copy(tmp_ctx, config_dn);
	if (!base_dn) {
		DEBUG(0, ("failed to copy Config DN %s\n",
			  ldb_dn_get_linearized(config_dn)));
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	ok = ldb_dn_add_child_fmt(base_dn, "CN=Sites");
	if (!ok) {
		if (ldb_dn_validate(base_dn)) {
			DEBUG(0, ("failed to format DN %s\n",
				  ldb_dn_get_linearized(base_dn)));
		}
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	ret = ldb_search(ldb, tmp_ctx, &res, base_dn, LDB_SCOPE_SUBTREE,
			 site_link_attrs, "objectClass=siteLink");
	if (ret != LDB_SUCCESS) {
		DEBUG(0, ("failed to find siteLink objects under %s: %s\n",
			  ldb_dn_get_linearized(base_dn), ldb_strerror(ret)));
		talloc_free(tmp_ctx);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	ZERO_STRUCT(vertex_ids);
	for (i = 0; i < res->count; i++) {
		struct GUID guid;

		guid = samdb_result_guid(res->msgs[i], "objectGUID");
		vertex_ids.data = talloc_realloc(tmp_ctx, vertex_ids.data,
						 struct GUID,
						 vertex_ids.count + 1);
		NT_STATUS_HAVE_NO_MEMORY_AND_FREE(vertex_ids.data, tmp_ctx);
		vertex_ids.data[vertex_ids.count] = guid;
		vertex_ids.count++;
	}

	status = kcctpl_create_graph(mem_ctx, &vertex_ids, &graph);
	if (NT_STATUS_IS_ERR(status)) {
		DEBUG(0, ("failed to create graph: %s\n", nt_errstr(status)));
		talloc_free(tmp_ctx);
		return status;
	}

	/* get site of local DC */

	ok = ldb_dn_add_child_fmt(base_dn, "CN=Inter-Site Transports");
	if (!ok) {
		if (ldb_dn_validate(base_dn)) {
			DEBUG(0, ("failed to format DN %s\n",
				  ldb_dn_get_linearized(base_dn)));
		}
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	ret = ldb_search(ldb, tmp_ctx, &res, base_dn, LDB_SCOPE_SUBTREE,
			inter_site_transport_attrs,
			"objectClass=interSiteTransport");
	if (ret != LDB_SUCCESS) {
		DEBUG(0, ("failed to find interSiteTransport objects under %s: "
			  "%s\n", ldb_dn_get_linearized(base_dn),
			  ldb_strerror(ret)));
		talloc_free(tmp_ctx);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	for (i = 0; i < res->count; i++) {
		struct ldb_message *transport;
		struct ldb_result *res_site_link;
		struct GUID transport_guid;
		unsigned int j;
		int options;

		transport = res->msgs[i];

		base_dn = samdb_result_dn(ldb, tmp_ctx, transport,
					  "distinguishedName", NULL);
		if (!base_dn) {
			DEBUG(0, ("failed to find DN for interSiteTransport "
				  "object\n"));
			talloc_free(graph);
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		/* TODO: don't need to ldb_search again; search in res. */
		ret = ldb_search(ldb, tmp_ctx, &res_site_link, base_dn,
				 LDB_SCOPE_SUBTREE, attrs,
				 "objectClass=siteLink");
		if (ret != LDB_SUCCESS) {
			DEBUG(0, ("failed to find siteLink objects under %s: "
				  "%s\n", ldb_dn_get_linearized(base_dn),
				  ldb_strerror(ret)));
			talloc_free(graph);
			talloc_free(tmp_ctx);
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}

		transport_guid = samdb_result_guid(transport, "objectGUID");
		for (j = 0; j < res_site_link->count; j++) {
			struct kcctpl_multi_edge *edge;

			status = kcctpl_create_edge(ldb, graph, transport_guid,
						    res_site_link->msgs[j],
						    &edge);
			if (NT_STATUS_IS_ERR(status)) {
				DEBUG(0, ("failed to create edge: %s\n",
					  nt_errstr(status)));
				talloc_free(graph);
				talloc_free(tmp_ctx);
				return status;
			}

			graph->edges.data = talloc_realloc(graph,
							   graph->edges.data,
							   struct kcctpl_multi_edge,
							   graph->edges.count + 1);
			if (!graph->edges.data) {
				talloc_free(graph);
				talloc_free(tmp_ctx);
				return NT_STATUS_NO_MEMORY;
			}
			graph->edges.data[graph->edges.count] = *edge;
			graph->edges.count++;
		}

		options = samdb_result_int64(transport, "options", 0);
		if ((options & NTDSTRANSPORT_OPT_BRIDGES_REQUIRED) == 0) {
			struct kcctpl_multi_edge_set *edge_set;

			status = kcctpl_create_auto_edge_set(graph,
							     transport_guid,
							     res_site_link,
							     &edge_set);
			if (NT_STATUS_IS_ERR(status)) {
				DEBUG(0, ("failed to create edge set: %s\n",
					  nt_errstr(status)));
				talloc_free(graph);
				talloc_free(tmp_ctx);
				return status;
			}

			graph->edge_sets.data = talloc_realloc(graph,
							       graph->edge_sets.data,
							       struct kcctpl_multi_edge_set,
							       graph->edge_sets.count + 1);
			if (!graph->edge_sets.data) {
				talloc_free(graph);
				talloc_free(tmp_ctx);
				return NT_STATUS_NO_MEMORY;
			}
			graph->edge_sets.data[graph->edge_sets.count] = *edge_set;
			graph->edge_sets.count++;
		} else {
			ret = ldb_search(ldb, tmp_ctx, &res_site_link, base_dn,
					 LDB_SCOPE_SUBTREE,
					 site_link_bridge_attrs,
					 "objectClass=siteLinkBridge");
			if (ret != LDB_SUCCESS) {
				DEBUG(0, ("failed to find siteLinkBridge "
					  "objects under %s: %s\n",
					  ldb_dn_get_linearized(base_dn),
					  ldb_strerror(ret)));
				talloc_free(graph);
				talloc_free(tmp_ctx);
				return NT_STATUS_INTERNAL_DB_CORRUPTION;
			}

			for (j = 0; j < res_site_link->count; j++) {
				struct ldb_message *bridge;
				struct kcctpl_multi_edge_set *edge_set;

				bridge = res_site_link->msgs[j];
				status = kcctpl_create_edge_set(ldb, graph,
								transport_guid,
								bridge,
								&edge_set);
				if (NT_STATUS_IS_ERR(status)) {
					DEBUG(0, ("failed to create edge set: "
						  "%s\n", nt_errstr(status)));
					talloc_free(graph);
					talloc_free(tmp_ctx);
					return status;
				}

				graph->edge_sets.data = talloc_realloc(graph,
								       graph->edge_sets.data,
								       struct kcctpl_multi_edge_set,
								       graph->edge_sets.count + 1);
				if (!graph->edge_sets.data) {
					talloc_free(graph);
					talloc_free(tmp_ctx);
					return NT_STATUS_NO_MEMORY;
				}
				graph->edge_sets.data[graph->edge_sets.count] = *edge_set;
				graph->edge_sets.count++;
			}
		}
	}

	*_graph = graph;
	talloc_free(tmp_ctx);
	return NT_STATUS_OK;
}