summaryrefslogtreecommitdiff
path: root/source4/lib/policy/gp_ldap.c
blob: 2d8d4f921c03ba44566ce04dbc0f12f137cb9d1a (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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
 *  Unix SMB/CIFS implementation.
 *  Group Policy Object Support
 *  Copyright (C) Jelmer Vernooij 2008
 *  Copyright (C) Wilco Baan Hofman 2008-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 "param/param.h"
#include "lib/ldb/include/ldb.h"
#include "lib/ldb_wrap.h"
#include "auth/credentials/credentials.h"
#include "../librpc/gen_ndr/nbt.h"
#include "libcli/libcli.h"
#include "libnet/libnet.h"
#include "../librpc/gen_ndr/ndr_security.h"
#include "../libcli/security/dom_sid.h"
#include "libcli/security/security.h"
#include "../lib/talloc/talloc.h"
#include "policy.h"

struct gpo_stringmap {
	const char *str;
	uint32_t flags;
};
static const struct gpo_stringmap gplink_options [] = {
	{ "GPLINK_OPT_DISABLE", GPLINK_OPT_DISABLE },
	{ "GPLINK_OPT_ENFORCE", GPLINK_OPT_ENFORCE },
	{ NULL, 0 }
};
static const struct gpo_stringmap gpo_flags [] = {
	{ "GPO_FLAG_USER_DISABLE", GPO_FLAG_USER_DISABLE },
	{ "GPO_FLAG_MACHINE_DISABLE", GPO_FLAG_MACHINE_DISABLE },
	{ NULL, 0 }
};
static const struct gpo_stringmap gpo_inheritance [] = {
	{ "GPO_INHERIT", GPO_INHERIT },
	{ "GPO_BLOCK_INHERITANCE", GPO_BLOCK_INHERITANCE },
	{ NULL, 0 }
};


static NTSTATUS parse_gpo(TALLOC_CTX *mem_ctx, struct ldb_message *msg, struct gp_object **ret)
{
	struct gp_object *gpo = talloc(mem_ctx, struct gp_object);
	enum ndr_err_code ndr_err;
	const DATA_BLOB *data;

	gpo->dn = talloc_steal(mem_ctx, ldb_dn_get_linearized(msg->dn));

	DEBUG(9, ("Parsing GPO LDAP data for %s\n", gpo->dn));

	gpo->display_name = talloc_steal(mem_ctx, ldb_msg_find_attr_as_string(msg, "displayName", ""));
	gpo->name = talloc_steal(mem_ctx, ldb_msg_find_attr_as_string(msg, "name", ""));
	gpo->flags = ldb_msg_find_attr_as_uint(msg, "name", 0);
	gpo->version = ldb_msg_find_attr_as_uint(msg, "version", 0);
	gpo->file_sys_path = talloc_steal(mem_ctx, ldb_msg_find_attr_as_string(msg, "gPCFileSysPath", ""));

	/* Pull the security descriptor through the NDR library */
	data = ldb_msg_find_ldb_val(msg, "nTSecurityDescriptor");
	gpo->security_descriptor = talloc(mem_ctx, struct security_descriptor);
	ndr_err = ndr_pull_struct_blob(data,
			mem_ctx,
			NULL,
			gpo->security_descriptor,
			(ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		return ndr_map_error2ntstatus(ndr_err);
	}

	*ret = gpo;
	return NT_STATUS_OK;
}

NTSTATUS gp_get_gpo_flags(TALLOC_CTX *mem_ctx, uint32_t flags, const char ***ret)
{
	unsigned int i, count=0;
	const char **flag_strs = talloc_array(mem_ctx, const char *, 1);

	flag_strs[0] = NULL;

	for (i = 0; gpo_flags[i].str != NULL; i++) {
		if (flags & gpo_flags[i].flags) {
			flag_strs = talloc_realloc(mem_ctx, flag_strs, const char *, count+2);
			flag_strs[count] = gpo_flags[i].str;
			flag_strs[count+1] = NULL;
			count++;
		}
	}
	*ret = flag_strs;
	return NT_STATUS_OK;
}

NTSTATUS gp_get_gplink_options(TALLOC_CTX *mem_ctx, uint32_t options, const char ***ret)
{
	unsigned int i, count=0;
	const char **flag_strs = talloc_array(mem_ctx, const char *, 1);

	flag_strs[0] = NULL;

	for (i = 0; gplink_options[i].str != NULL; i++) {
		if (options & gplink_options[i].flags) {
			flag_strs = talloc_realloc(mem_ctx, flag_strs, const char *, count+2);
			flag_strs[count] = gplink_options[i].str;
			flag_strs[count+1] = NULL;
			count++;
		}
	}
	*ret = flag_strs;
	return NT_STATUS_OK;
}

NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
		struct loadparm_context *lp_ctx,
		struct cli_credentials *credentials,
		struct tevent_context *ev_ctx,
		struct gp_context **gp_ctx)
{

	struct libnet_LookupDCs *io;
	char *url;
	struct libnet_context *net_ctx;
	struct ldb_context *ldb_ctx;
	NTSTATUS rv;

	/* Initialise the libnet context */
	net_ctx = libnet_context_init(ev_ctx, lp_ctx);
	net_ctx->cred = credentials;

	/* Prepare libnet lookup structure for looking a DC (PDC is correct). */
	io = talloc_zero(mem_ctx, struct libnet_LookupDCs);
	io->in.name_type = NBT_NAME_PDC;
	io->in.domain_name = lp_workgroup(lp_ctx);

	/* Find Active DC's */
	rv = libnet_LookupDCs(net_ctx, mem_ctx, io);
	if (!NT_STATUS_IS_OK(rv)) {
		DEBUG(0, ("Failed to lookup DCs in domain\n"));
		return rv;
	}

	/* Connect to ldap://DC_NAME with all relevant contexts*/
	url = talloc_asprintf(mem_ctx, "ldap://%s", io->out.dcs[0].name);
	ldb_ctx = ldb_wrap_connect(mem_ctx, net_ctx->event_ctx, lp_ctx,
			url, NULL, net_ctx->cred, 0);
	if (ldb_ctx == NULL) {
		DEBUG(0, ("Can't connect to DC's LDAP with url %s\n", url));
		return NT_STATUS_UNSUCCESSFUL;
	}

	/* We don't need to keep the libnet context */
	talloc_free(net_ctx);

	*gp_ctx = talloc_zero(mem_ctx, struct gp_context);
	(*gp_ctx)->lp_ctx = lp_ctx;
	(*gp_ctx)->credentials = credentials;
	(*gp_ctx)->ev_ctx = ev_ctx;
	(*gp_ctx)->ldb_ctx = ldb_ctx;
	return NT_STATUS_OK;
}

NTSTATUS gp_list_all_gpos(struct gp_context *gp_ctx, struct gp_object ***ret)
{
	struct ldb_result *result;
	int rv;
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	struct ldb_dn *dn;
	struct gp_object **gpo;
	unsigned int i; /* same as in struct ldb_result */
	const char **attrs;

	/* Create a forked memory context, as a base for everything here */
	mem_ctx = talloc_new(gp_ctx);

	/* Create full ldb dn of the policies base object */
	dn = ldb_get_default_basedn(gp_ctx->ldb_ctx);
	rv = ldb_dn_add_child(dn, ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, "CN=Policies,CN=System"));
	if (!rv) {
		DEBUG(0, ("Can't append subtree to DN\n"));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	DEBUG(10, ("Searching for policies in DN: %s\n", ldb_dn_get_linearized(dn)));

	attrs = talloc_array(mem_ctx, const char *, 7);
	attrs[0] = "nTSecurityDescriptor";
	attrs[1] = "versionNumber";
	attrs[2] = "flags";
	attrs[3] = "name";
	attrs[4] = "displayName";
	attrs[5] = "gPCFileSysPath";
	attrs[6] = NULL;

	rv = ldb_search(gp_ctx->ldb_ctx, mem_ctx, &result, dn, LDB_SCOPE_ONELEVEL, attrs, "(objectClass=groupPolicyContainer)");
	if (rv != LDB_SUCCESS) {
		DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),ldb_errstring(gp_ctx->ldb_ctx)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	gpo = talloc_array(gp_ctx, struct gp_object *, result->count+1);
	gpo[result->count] = NULL;

	for (i = 0; i < result->count; i++) {
		status = parse_gpo(gp_ctx, result->msgs[i], &gpo[i]);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("Failed to parse GPO.\n"));
			talloc_free(mem_ctx);
			return status;
		}
	}

	talloc_free(mem_ctx);

	*ret = gpo;
	return NT_STATUS_OK;
}

NTSTATUS gp_get_gpo_info(struct gp_context *gp_ctx, const char *dn_str, struct gp_object **ret)
{
	struct ldb_result *result;
	struct ldb_dn *dn;
	struct gp_object *gpo;
	int rv;
	NTSTATUS status;
	TALLOC_CTX *mem_ctx;
	const char **attrs;

	/* Create a forked memory context, as a base for everything here */
	mem_ctx = talloc_new(gp_ctx);

	/* Create an ldb dn struct for the dn string */
	dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);

	attrs = talloc_array(mem_ctx, const char *, 7);
	attrs[0] = "nTSecurityDescriptor";
	attrs[1] = "versionNumber";
	attrs[2] = "flags";
	attrs[3] = "name";
	attrs[4] = "displayName";
	attrs[5] = "gPCFileSysPath";
	attrs[6] = NULL;

	rv = ldb_search(gp_ctx->ldb_ctx,
			mem_ctx,
			&result,
			dn,
			LDB_SCOPE_BASE,
			attrs,
			"objectClass=groupPolicyContainer");
	if (rv != LDB_SUCCESS) {
		DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),ldb_errstring(gp_ctx->ldb_ctx)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	/* We expect exactly one record */
	if (result->count != 1) {
		DEBUG(0, ("Could not find GPC with dn %s\n", dn_str));
		talloc_free(mem_ctx);
		return NT_STATUS_NOT_FOUND;
	}

	status = parse_gpo(gp_ctx, result->msgs[0], &gpo);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to parse GPO.\n"));
		talloc_free(mem_ctx);
		return status;
	}

	talloc_free(mem_ctx);

	*ret = gpo;
	return NT_STATUS_OK;
}

static NTSTATUS parse_gplink (TALLOC_CTX *mem_ctx, const char *gplink_str, struct gp_link ***ret)
{
	int start, idx=0;
	int pos;
	struct gp_link **gplinks;
	char *buf, *end;

	gplinks = talloc_array(mem_ctx, struct gp_link *, 1);
	gplinks[0] = NULL;

	/* Assuming every gPLink starts with "[LDAP://" */
	start = 8;

	for (pos = start; pos < strlen(gplink_str); pos++) {
		if (gplink_str[pos] == ';') {
			gplinks = talloc_realloc(mem_ctx, gplinks, struct gp_link *, idx+2);
			gplinks[idx] = talloc(mem_ctx, struct gp_link);
			gplinks[idx]->dn = talloc_strndup(mem_ctx,
			                                  gplink_str + start,
			                                  pos - start);

			for (start = pos + 1; gplink_str[pos] != ']'; pos++);

			buf = talloc_strndup(gplinks, gplink_str + start, pos - start);
			gplinks[idx]->options = (uint32_t) strtoll(buf, &end, 0);
			talloc_free(buf);

			/* Set the last entry in the array to be NULL */
			gplinks[idx + 1] = NULL;

			/* Increment the array index, the string position past
			   the next "[LDAP://", and set the start reference */
			idx++;
			pos += 9;
			start = pos;
		}
	}

	*ret = gplinks;
	return NT_STATUS_OK;
}


NTSTATUS gp_get_gplinks(struct gp_context *gp_ctx, const char *dn_str, struct gp_link ***ret)
{
	TALLOC_CTX *mem_ctx;
	struct ldb_dn *dn;
	struct ldb_result *result;
	struct gp_link **gplinks;
	char *gplink_str;
	int rv;
	unsigned int i, j;
	NTSTATUS status;

	/* Create a forked memory context, as a base for everything here */
	mem_ctx = talloc_new(gp_ctx);

	dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);

	rv = ldb_search(gp_ctx->ldb_ctx, mem_ctx, &result, dn, LDB_SCOPE_BASE, NULL, "(objectclass=*)");
	if (rv != LDB_SUCCESS) {
		DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv), ldb_errstring(gp_ctx->ldb_ctx)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	for (i = 0; i < result->count; i++) {
		for (j = 0; j < result->msgs[i]->num_elements; j++) {
			struct ldb_message_element *element = &result->msgs[i]->elements[j];

			if (strcmp(element->name, "gPLink") == 0) {
				SMB_ASSERT(element->num_values > 0);
				gplink_str = talloc_strdup(mem_ctx, (char *) element->values[0].data);
				goto found;
			}
		}
	}
	DEBUG(0, ("Object or gPLink attribute not found.\n"));
	return NT_STATUS_NOT_FOUND;

	found:

	status = parse_gplink(gp_ctx, gplink_str, &gplinks);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to parse gPLink\n"));
		return status;
	}

	talloc_free(mem_ctx);

	*ret = gplinks;
	return NT_STATUS_OK;
}

NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, const char ***ret)
{
	TALLOC_CTX *mem_ctx;
	const char **gpos;
	struct ldb_result *result;
	const char *sid;
	struct ldb_dn *dn;
	struct ldb_message_element *element;
	int inherit;
	const char *attrs[] = { "objectClass", NULL };
	int rv;
	NTSTATUS status;
	unsigned int count = 0;
	unsigned int i;
	enum {
		ACCOUNT_TYPE_USER = 0,
		ACCOUNT_TYPE_MACHINE = 1
	} account_type;

	/* Create a forked memory context, as a base for everything here */
	mem_ctx = talloc_new(gp_ctx);

	sid = dom_sid_string(mem_ctx, token->user_sid);

	/* Find the user DN and objectclass via the sid from the security token */
	rv = ldb_search(gp_ctx->ldb_ctx,
			mem_ctx,
			&result,
			ldb_get_default_basedn(gp_ctx->ldb_ctx),
			LDB_SCOPE_SUBTREE,
			attrs,
			"(&(objectclass=user)(objectSid=%s))", sid);
	if (rv != LDB_SUCCESS) {
		DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),
				ldb_errstring(gp_ctx->ldb_ctx)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}
	if (result->count != 1) {
		DEBUG(0, ("Could not find user with sid %s.\n", sid));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}
	DEBUG(10,("Found DN for this user: %s\n", ldb_dn_get_linearized(result->msgs[0]->dn)));

	element = ldb_msg_find_element(result->msgs[0], "objectClass");

	/* We need to know if this account is a user or machine. */
	account_type = ACCOUNT_TYPE_USER;
	for (i = 0; i < element->num_values; i++) {
		if (strcmp((char *)element->values[i].data, "computer") == 0) {
			account_type = ACCOUNT_TYPE_MACHINE;
			DEBUG(10, ("This user is a machine\n"));
		}
	}

	gpos = talloc_array(gp_ctx, const char *, 1);
	gpos[0] = NULL;

	/* Walk through the containers until we hit the root */
	inherit = 1;
	dn = ldb_dn_get_parent(mem_ctx, result->msgs[0]->dn);
	while (ldb_dn_compare_base(ldb_get_default_basedn(gp_ctx->ldb_ctx), dn) == 0) {
		const char *gpo_attrs[] = { "gPLink", "gPOptions", NULL };
		struct gp_link **gplinks;
		enum gpo_inheritance gpoptions;

		DEBUG(10, ("Getting gPLinks for DN: %s\n", ldb_dn_get_linearized(dn)));

		/* Get the gPLink and gPOptions attributes from the container */
		rv = ldb_search(gp_ctx->ldb_ctx,
				mem_ctx,
				&result,
				dn,
				LDB_SCOPE_BASE,
				gpo_attrs,
				"objectclass=*");
		if (rv != LDB_SUCCESS) {
			DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),
					ldb_errstring(gp_ctx->ldb_ctx)));
			talloc_free(mem_ctx);
			return NT_STATUS_UNSUCCESSFUL;
		}

		/* Parse the gPLink attribute, put it into a nice struct array */
		status = parse_gplink(mem_ctx, ldb_msg_find_attr_as_string(result->msgs[0], "gPLink", ""), &gplinks);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("Failed to parse gPLink\n"));
			talloc_free(mem_ctx);
			return status;
		}

		/* Check all group policy links on this container */
		for (i = 0; gplinks[i] != NULL; i++) {
			struct gp_object *gpo;
			uint32_t access_granted;

			/* If inheritance was blocked at a higher level and this
			 * gplink is not enforced, it should not be applied */
			if (!inherit && !(gplinks[i]->options & GPLINK_OPT_ENFORCE))
				continue;

			/* Don't apply disabled links */
			if (gplinks[i]->options & GPLINK_OPT_DISABLE)
				continue;

			/* Get GPO information */
			status = gp_get_gpo_info(gp_ctx, gplinks[i]->dn, &gpo);
			if (!NT_STATUS_IS_OK(status)) {
				DEBUG(0, ("Failed to get gpo information for %s\n", gplinks[i]->dn));
				talloc_free(mem_ctx);
				return status;
			}

			/* If the account does not have read access, this GPO does not apply
			 * to this account */
			status = sec_access_check(gpo->security_descriptor,
					token,
					(SEC_STD_READ_CONTROL | SEC_ADS_LIST | SEC_ADS_READ_PROP),
					&access_granted);
			if (!NT_STATUS_IS_OK(status)) {
				continue;
			}

			/* If the account is a user and the GPO has user disabled flag, or
			 * a machine and the GPO has machine disabled flag, this GPO does
			 * not apply to this account */
			if ((account_type == ACCOUNT_TYPE_USER &&
					(gpo->flags & GPO_FLAG_USER_DISABLE)) ||
					(account_type == ACCOUNT_TYPE_MACHINE &&
					(gpo->flags & GPO_FLAG_MACHINE_DISABLE))) {
				continue;
			}

			/* Add the GPO to the list */
			gpos = talloc_realloc(gp_ctx, gpos, const char *, count+2);
			gpos[count] = talloc_strdup(gp_ctx, gplinks[i]->dn);
			gpos[count+1] = NULL;
			count++;

			/* Clean up */
			talloc_free(gpo);
		}

		/* If inheritance is blocked, then we should only add enforced gPLinks
		 * higher up */
		gpoptions = ldb_msg_find_attr_as_uint(result->msgs[0], "gPOptions", 0);
		if (gpoptions == GPO_BLOCK_INHERITANCE) {
			inherit = 0;
		}
		dn = ldb_dn_get_parent(mem_ctx, dn);
	}

	talloc_free(mem_ctx);

	*ret = gpos;
	return NT_STATUS_OK;
}

NTSTATUS gp_set_gplink(struct gp_context *gp_ctx, const char *dn_str, struct gp_link *gplink)
{
	TALLOC_CTX *mem_ctx;
	struct ldb_result *result;
	struct ldb_dn *dn;
	struct ldb_message *msg;
	const char *attrs[] = { "gPLink", NULL };
	const char *gplink_str;
	int rv;
	char *start;

	/* Create a forked memory context, as a base for everything here */
	mem_ctx = talloc_new(gp_ctx);

	dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);

	rv = ldb_search(gp_ctx->ldb_ctx, mem_ctx, &result, dn, LDB_SCOPE_BASE, attrs, "(objectclass=*)");
	if (rv != LDB_SUCCESS) {
		DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv), ldb_errstring(gp_ctx->ldb_ctx)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	if (result->count != 1) {
		talloc_free(mem_ctx);
		return NT_STATUS_NOT_FOUND;
	}

	gplink_str = ldb_msg_find_attr_as_string(result->msgs[0], "gPLink", "");

	/* If this GPO link already exists, alter the options, else add it */
	if ((start = strcasestr(gplink_str, gplink->dn)) != NULL) {
		start += strlen(gplink->dn);
		*start = '\0';
		start++;
		while (*start != ']' && *start != '\0') {
			start++;
		}
		gplink_str = talloc_asprintf(mem_ctx, "%s;%d%s", gplink_str, gplink->options, start);

	} else {
		/* Prepend the new GPO link to the string. This list is backwards in priority. */
		gplink_str = talloc_asprintf(mem_ctx, "[LDAP://%s;%d]%s", gplink->dn, gplink->options, gplink_str);
	}



	msg = ldb_msg_new(mem_ctx);
	msg->dn = dn;

	rv = ldb_msg_add_string(msg, "gPLink", gplink_str);
	if (rv != 0) {
		DEBUG(0, ("LDB message add string failed: %s\n", ldb_strerror(rv)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}
	msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;

	rv = ldb_modify(gp_ctx->ldb_ctx, msg);
	if (rv != 0) {
		DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	talloc_free(mem_ctx);
	return NT_STATUS_OK;
}

NTSTATUS gp_del_gplink(struct gp_context *gp_ctx, const char *dn_str, const char *gplink_dn)
{
	TALLOC_CTX *mem_ctx;
	struct ldb_result *result;
	struct ldb_dn *dn;
	struct ldb_message *msg;
	const char *attrs[] = { "gPLink", NULL };
	const char *gplink_str;
	int rv;
	char *p;

	/* Create a forked memory context, as a base for everything here */
	mem_ctx = talloc_new(gp_ctx);

	dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);

	rv = ldb_search(gp_ctx->ldb_ctx, mem_ctx, &result, dn, LDB_SCOPE_BASE, attrs, "(objectclass=*)");
	if (rv != LDB_SUCCESS) {
		DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv), ldb_errstring(gp_ctx->ldb_ctx)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	if (result->count != 1) {
		talloc_free(mem_ctx);
		return NT_STATUS_NOT_FOUND;
	}

	gplink_str = ldb_msg_find_attr_as_string(result->msgs[0], "gPLink", "");

	/* If this GPO link already exists, alter the options, else add it */
	p = strcasestr(gplink_str, talloc_asprintf(mem_ctx, "[LDAP://%s", gplink_dn));
	if (p == NULL) {
		talloc_free(mem_ctx);
		return NT_STATUS_NOT_FOUND;
	}

	*p = '\0';
	p++;
	while (*p != ']' && *p != '\0') {
		p++;
	}
	p++;
	gplink_str = talloc_asprintf(mem_ctx, "%s%s", gplink_str, p);


	msg = ldb_msg_new(mem_ctx);
	msg->dn = dn;

	rv = ldb_msg_add_string(msg, "gPLink", gplink_str);
	if (rv != 0) {
		DEBUG(0, ("LDB message add string failed: %s\n", ldb_strerror(rv)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}
	msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;

	rv = ldb_modify(gp_ctx->ldb_ctx, msg);
	if (rv != 0) {
		DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
		talloc_free(mem_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	talloc_free(mem_ctx);
	return NT_STATUS_OK;
}