summaryrefslogtreecommitdiff
path: root/source4/dsdb/samdb/ldb_modules/acl.c
blob: 2f123145dbae22b4c3e1a97010036c4cf0083deb (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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
/*
   ldb database library

   Copyright (C) Simo Sorce 2006-2008
   Copyright (C) Nadezhda Ivanova 2009
   Copyright (C) Anatoliy Atanasov  2009

    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/>.
*/

/*
 *  Name: ldb
 *
 *  Component: ldb ACL module
 *
 *  Description: Module that performs authorisation access checks based on the
 *               account's security context and the DACL of the object being polled.
 *               Only DACL checks implemented at this point
 *
 *  Authors: Nadezhda Ivanova, Anatoliy Atanasov
 */

#include "includes.h"
#include "ldb_module.h"
#include "auth/auth.h"
#include "libcli/security/security.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "dsdb/samdb/samdb.h"
#include "librpc/gen_ndr/ndr_security.h"
#include "param/param.h"

/* acl_search helper */
struct acl_context {

	struct ldb_module *module;
	struct ldb_request *req;
	struct ldb_request *down_req;

	/*needed if we have to identify if this is SYSTEM_USER*/
	enum security_user_level user_type;

	uint32_t access_needed;
	struct ldb_dn * dn_to_check;

	/* set to true when we need to process the request as a SYSTEM_USER, regardless
	 * of the user's actual rights - for example when we need to retrieve the
	 * ntSecurityDescriptor */
	bool ignore_security;
	struct security_token *token;
	/*needed to identify if we have requested these attributes*/
	bool nTSecurityDescriptor;
	bool objectClass;
	int sec_result;
};

struct extended_access_check_attribute {
	const char *oa_name;
	const uint32_t requires_rights;
};

struct acl_private{
	bool perform_check;
};

static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares);

/*FIXME: Perhaps this should go in the .idl file*/
#define SEC_GENERIC_ACCESS_NEVER_GRANTED ( 0xFFFFFFFF )

/*Contains a part of the attributes - the ones that have predefined required rights*/
static const struct extended_access_check_attribute extended_access_checks_table[] =
{
	{
		.oa_name = "nTSecurityDescriptor",
		.requires_rights = SEC_FLAG_SYSTEM_SECURITY & SEC_STD_READ_CONTROL,
	},
	{
                .oa_name = "pekList",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
        },
	{
                .oa_name = "currentValue",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "dBCSPwd",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "unicodePwd",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "ntPwdHistory",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "priorValue",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "supplementalCredentials",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "trustAuthIncoming",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "trustAuthOutgoing",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "ImPwdHistory",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "initialAuthIncoming",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "initialAuthOutgoing",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
	{
                .oa_name = "msDS-ExecuteScriptPassword",
                .requires_rights = SEC_GENERIC_ACCESS_NEVER_GRANTED,
	},
};

static NTSTATUS extended_access_check(const char *attribute_name, const int access_rights, uint32_t searchFlags)
{
	int i = 0;
	if (access_rights == SEC_GENERIC_ACCESS_NEVER_GRANTED) {
		return NT_STATUS_ACCESS_DENIED;
	}

	/*Check if the attribute is in the table first*/
	for ( i = 0; extended_access_checks_table[i].oa_name; i++ ) {
		if (ldb_attr_cmp(extended_access_checks_table[i].oa_name, attribute_name) == 0) {
			if ((access_rights & extended_access_checks_table[i].requires_rights) == access_rights) {
				return NT_STATUS_OK;
			} else {
				return NT_STATUS_ACCESS_DENIED;
			}
		}
	}

	/*Check for attribute whose attributeSchema has 0x80 set in searchFlags*/
	if ((searchFlags & SEARCH_FLAG_CONFIDENTIAL) == SEARCH_FLAG_CONFIDENTIAL) {
		if (((SEC_ADS_READ_PROP & SEC_ADS_CONTROL_ACCESS) & access_rights) == access_rights) {
			return NT_STATUS_OK;
		} else {
			return NT_STATUS_ACCESS_DENIED;
		}
	}

	/*Check attributes with *special* behaviour*/
	if (ldb_attr_cmp("msDS-QuotaEffective", attribute_name) == 0 || ldb_attr_cmp("msDS-QuotaUsed", attribute_name) == 0){
		/*Rights required:
		 *
		 *(RIGHT_DS_READ_PROPERTY on the Quotas container or
		 *((the client is querying the quota for the security principal it is authenticated as) and
		 *(DS-Query-Self-Quota control access right on the Quotas container))
		 */
	}

        if (ldb_attr_cmp("userPassword", attribute_name) == 0) {
		/*When the dSHeuristics.fUserPwdSupport flag is false, the requester must be granted RIGHT_DS_READ_PROPERTY.
		 *When the dSHeuristics.fUserPwdSupport flag is true, access is never granted.
		 */
	}

	if (ldb_attr_cmp("sDRightsEffective", attribute_name) == 0) {
		/*FIXME:3.1.1.4.5.4 in MS-ADTS*/
	}

	if (ldb_attr_cmp("allowedChildClassesEffective", attribute_name) == 0) {
		/*FIXME:3.1.1.4.5.5 in MS-ADTS*/
        }

	if (ldb_attr_cmp("allowedAttributesEffective", attribute_name) == 0) {
		/*FIXME:3.1.1.4.5.7 in MS-ADTS*/
        }

	if (ldb_attr_cmp("msDS-Approx-Immed-Subordinates", attribute_name) == 0) {
		/*FIXME:3.1.1.4.5.15 in MS-ADTS*/
        }

	if (ldb_attr_cmp("msDS-QuotaEffective", attribute_name) == 0) {
		/*FIXME:3.1.1.4.5.22 in MS-ADTS*/
        }

	if (ldb_attr_cmp("msDS-ReplAttributeMetaData", attribute_name) == 0 || ldb_attr_cmp("msDS-ReplAttributeMetaData", attribute_name) == 0) {
		/*The security context of the requester must be granted the following rights on the replPropertyMetaData attribute:
		 *(RIGHT_DS_READ_PROPERTY)or (DS-Replication-Manage-Topology by ON!nTSecurityDescriptor)
		 */
        }

	if (ldb_attr_cmp("msDS-NCReplInboundNeighbors", attribute_name) == 0) {
		/*The security context of the requester must be granted the following rights on repsFrom:
		 *(RIGHT_DS_READ_PROPERTY) or (DS-Replication-Manage-Topology) or (DS-Replication-Monitor-Topology)
		 */
        }

	if (ldb_attr_cmp("msDS-NCReplOutboundNeighbors", attribute_name) == 0) {
		/*The security context of the requester must be granted the following rights on repsTo:
		 *(RIGHT_DS_READ_PROPERTY) or (DS-Replication-Manage-Topology) or (DS-Replication-Monitor-Topology)
		 */
        }

	if (ldb_attr_cmp("msDS-NCReplCursors", attribute_name) == 0) {
		/*The security context of the requester must be granted the following rights on replUpToDateVector: (RIGHT_DS_READ_PROPERTY)
		 *or (DS-Replication-Manage-Topology) or (DS-Replication-Monitor-Topology)
		 */
        }

	if (ldb_attr_cmp("msDS-IsUserCachableAtRodc", attribute_name) == 0) {
		/*The security context of the requester must be granted
		 *the DS-Replication-Secrets-Synchronize control access right on the root of the default NC.
		 */
        }

	return NT_STATUS_OK;
}

/* Builds an object tree for object specific access checks */
static struct object_tree * build_object_tree_form_attr_list(TALLOC_CTX *mem_ctx,   /* Todo this context or separate? */
							     struct ldb_context *ldb,
							     const char ** attr_names,
							     int num_attrs,
							     const char * object_class,
							     uint32_t init_access)
{
	const struct dsdb_schema *schema = dsdb_get_schema(ldb);
	const struct GUID *oc_guid = class_schemaid_guid_by_lDAPDisplayName(schema, object_class);
	struct object_tree *tree;
	int i;

	if (!oc_guid)
		return NULL;

	tree = insert_in_object_tree(mem_ctx, oc_guid, NULL, init_access, NULL);
	if (attr_names){
		for (i=0; i < num_attrs; i++){
			const struct dsdb_attribute *attribute = dsdb_attribute_by_lDAPDisplayName(schema,attr_names[i]);
			if (attribute)
				insert_in_object_tree(mem_ctx,
						      &attribute->schemaIDGUID,
						      &attribute->attributeSecurityGUID,
						      init_access,
						      tree);
		}
	}
	return tree;
}

bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
{
	int result;
	struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
	result = ldb_dn_compare(root_base_dn,dn_to_check);
	return (result==0);
}

static int acl_op_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	struct acl_context *ac;

	ac = talloc_get_type(req->context, struct acl_context);

	if (!ares) {
		return ldb_module_done(ac->req, NULL, NULL,
					LDB_ERR_OPERATIONS_ERROR);
	}
	if (ares->error != LDB_SUCCESS) {
		return ldb_module_done(ac->req, ares->controls,
					ares->response, ares->error);
	}

	if (ares->type != LDB_REPLY_DONE) {
		talloc_free(ares);
		return ldb_module_done(ac->req, NULL, NULL,
					LDB_ERR_OPERATIONS_ERROR);
	}

	return ldb_module_done(ac->req, ares->controls,
				ares->response, ares->error);
}


static int acl_access_check_add(struct ldb_reply *ares,
				struct acl_context *ac,
				struct security_descriptor *sd)
{
	uint32_t access_granted = 0;
	NTSTATUS status;
	struct ldb_dn *parent;
	struct ldb_dn *grandparent;
	struct object_tree *tree = NULL;

	parent = ldb_dn_get_parent(ac->req, ac->req->op.add.message->dn);
	grandparent = ldb_dn_get_parent(ac->req, parent);
	if (ldb_dn_compare(ares->message->dn, grandparent) == 0)
		status = sec_access_check_ds(sd, ac->token,
					     SEC_ADS_LIST,
					     &access_granted,
					     NULL);
	else if (ldb_dn_compare(ares->message->dn, parent) == 0){
		struct ldb_message_element *oc_el;
		struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
		const struct dsdb_schema *schema = dsdb_get_schema(ldb);
		int i;

		oc_el = ldb_msg_find_element(ares->message, "objectClass");
		if (!oc_el || oc_el->num_values == 0)
			return LDB_SUCCESS;
		for (i = 0; i < oc_el->num_values; i++){
			const struct GUID *guid = class_schemaid_guid_by_lDAPDisplayName(schema,
											  oc_el->values[i].data);
			ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
			tree = insert_in_object_tree(ac->req, guid, NULL, SEC_ADS_CREATE_CHILD,
						     tree);
			status = sec_access_check_ds(sd, ac->token, SEC_ADS_CREATE_CHILD,&access_granted, tree);
			if (NT_STATUS_IS_OK(status))
				ac->sec_result = LDB_SUCCESS;
		}
	}
	else
		return LDB_SUCCESS;

	return ac->sec_result;
}

static int acl_access_check_modify(struct ldb_reply *ares, struct acl_context *ac,
				   struct security_descriptor *sd)
{
	uint32_t access_granted = 0;
	NTSTATUS status;
	struct ldb_dn *parent;
	struct object_tree *tree = NULL;

	parent = ldb_dn_get_parent(ac->req, ac->req->op.add.message->dn);
	if (ldb_dn_compare(ares->message->dn, parent) == 0)
		status = sec_access_check_ds(sd, ac->token, SEC_ADS_LIST,&access_granted, NULL);
	else if (ldb_dn_compare(ares->message->dn, ac->req->op.add.message->dn) == 0){
		struct ldb_message_element *oc_el;
		struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
		const struct dsdb_schema *schema = dsdb_get_schema(ldb);
		int i;
		struct GUID *guid;
		oc_el = ldb_msg_find_element(ares->message, "objectClass");
		if (!oc_el || oc_el->num_values == 0)
			return LDB_SUCCESS;

		guid = class_schemaid_guid_by_lDAPDisplayName(schema,
							      oc_el->values[oc_el->num_values-1].data);
		tree = insert_in_object_tree(ac->req, guid, NULL, SEC_ADS_WRITE_PROP,
						     tree);
		for (i=0; i < ac->req->op.mod.message->num_elements; i++){
			const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
											      ac->req->op.mod.message->elements[i].name);
			if (!attr)
				return LDB_ERR_OPERATIONS_ERROR; /* What should we actually return here? */
			insert_in_object_tree(ac, &attr->schemaIDGUID,
					      &attr->attributeSecurityGUID, ac->access_needed, tree);
		}
		status = sec_access_check_ds(sd, ac->token, SEC_ADS_WRITE_PROP ,&access_granted, tree);
		if (!NT_STATUS_IS_OK(status))
			ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
	}
	else
		return LDB_SUCCESS;
	return ac->sec_result;
}
/*TODO*/
static int acl_access_check_rename(struct ldb_reply *ares, struct acl_context *ac,
				   struct security_descriptor *sd)
{
	return ac->sec_result;
}

static int acl_access_check_delete(struct ldb_reply *ares, struct acl_context *ac,
				   struct security_descriptor *sd)
{
	uint32_t access_granted = 0;
	NTSTATUS status;
	struct ldb_dn *parent;
	struct object_tree *tree = NULL;

	parent = ldb_dn_get_parent(ac->req, ac->req->op.del.dn);
	if (ldb_dn_compare(ares->message->dn, parent) == 0){
		status = sec_access_check_ds(sd, ac->token, SEC_ADS_LIST,&access_granted, NULL);
		if (!NT_STATUS_IS_OK(status)){
			ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
			return ac->sec_result;
		}
		status = sec_access_check_ds(sd, ac->token, SEC_ADS_DELETE_CHILD,&access_granted, NULL);
		if (NT_STATUS_IS_OK(status)){
			ac->sec_result = LDB_SUCCESS;
			return ac->sec_result;
		}
	}
	else if (ldb_dn_compare(ares->message->dn, ac->req->op.del.dn) == 0){
		status = sec_access_check_ds(sd, ac->token, SEC_STD_DELETE, &access_granted, NULL);
		if (!NT_STATUS_IS_OK(status))
			ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
	}
	return ac->sec_result;
}

static int acl_access_check_search(struct ldb_reply *ares, struct acl_context *ac,
				   struct security_descriptor *sd)
{
	uint32_t access_granted;
	NTSTATUS status;
	struct ldb_dn *parent;

	if (ac->user_type == SECURITY_SYSTEM || ac->user_type == SECURITY_ANONYMOUS) {
		return LDB_SUCCESS;/*FIXME: we have anonymous access*/
	}

	parent = ldb_dn_get_parent(ac->req, ac->dn_to_check);
	ac->sec_result = LDB_SUCCESS;
	if (ldb_dn_compare(ares->message->dn, parent) == 0) {
		status = sec_access_check_ds(sd, ac->token, SEC_ADS_LIST,&access_granted, NULL);
		if (!NT_STATUS_IS_OK(status)) {
			ac->sec_result = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
		}
	}

	return ac->sec_result;
}

static int acl_perform_access_check(struct ldb_request *req, struct ldb_reply *ares,
				    struct acl_context *ac)
{
	struct ldb_message_element *oc_el;
	struct security_descriptor *sd;
	enum ndr_err_code ndr_err;

	oc_el = ldb_msg_find_element(ares->message, "ntSecurityDescriptor");
	if (!oc_el || oc_el->num_values == 0)
		return LDB_SUCCESS;

	sd = talloc(ac, struct security_descriptor);
	if(!sd) {
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	}
	ndr_err = ndr_pull_struct_blob(&oc_el->values[0], sd, NULL, sd,
				       (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);

	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err))
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	switch (ac->req->operation) {
	case LDB_SEARCH:
		return acl_access_check_search(ares, ac, sd);
	case LDB_ADD:
		return acl_access_check_add(ares, ac, sd);
	case LDB_MODIFY:
		return acl_access_check_modify(ares, ac, sd);
	case LDB_DELETE:
		return acl_access_check_delete(ares, ac, sd);
	case LDB_RENAME:
		return acl_access_check_rename(ares, ac, sd);
	default:
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	}
	return LDB_SUCCESS;
}

static int acl_forward_add(struct ldb_reply *ares,
			   struct acl_context *ac)
{
  struct ldb_request *newreq;
	struct ldb_context *ldb;
	int ret;

	ldb = ldb_module_get_ctx(ac->module);
	ret = ldb_build_add_req(&newreq,ldb,
				ac,
				ac->req->op.add.message,
				ac->req->controls,
				ac,
				acl_op_callback,
				ac->req);
	if (ret != LDB_SUCCESS)
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	return ldb_next_request(ac->module, newreq);
}

static int acl_forward_modify(struct ldb_reply *ares,
			      struct acl_context *ac)
{
  struct ldb_request *newreq;
	struct ldb_context *ldb;
	int ret;

	ldb = ldb_module_get_ctx(ac->module);
	ret = ldb_build_mod_req(&newreq,ldb,
				    ac,
				    ac->req->op.mod.message,
				    ac->req->controls,
				    ac,
				    acl_op_callback,
				    ac->req);
	if (ret != LDB_SUCCESS)
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	return ldb_next_request(ac->module, newreq);
}

static int acl_forward_delete(struct ldb_reply *ares,
			      struct acl_context *ac)
{
	struct ldb_request *newreq;
	struct ldb_context *ldb;
	int ret;

	ldb = ldb_module_get_ctx(ac->module);
	ret = ldb_build_del_req(&newreq, ldb,
				    ac,
				    ac->req->op.del.dn,
				    ac->req->controls,
				    ac,
				    acl_op_callback,
				    ac->req);
	if (ret != LDB_SUCCESS)
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	return ldb_next_request(ac->module, newreq);
}

static int acl_forward_rename(struct ldb_reply *ares,
			      struct acl_context *ac)
{
	return LDB_SUCCESS;
}

static int acl_forward_search(struct acl_context *ac)
{
	int ret;
	const char * const *attrs;
	struct ldb_control *sd_control;
	struct ldb_control **sd_saved_controls;
	struct ldb_context *ldb;
	struct ldb_request *newreq;

	ldb = ldb_module_get_ctx(ac->module);
	attrs = ac->req->op.search.attrs;
	if (attrs) {
		ac->nTSecurityDescriptor = false;
		ac->objectClass = false;
		if (!ldb_attr_in_list(ac->req->op.search.attrs, "nTSecurityDescriptor")) {
			attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
			ac->nTSecurityDescriptor = true;
		}
		if (!ldb_attr_in_list(ac->req->op.search.attrs, "objectClass")) {
			attrs = ldb_attr_list_copy_add(ac, attrs, "objectClass");
			ac->objectClass = true;
		}
	}
	ret = ldb_build_search_req_ex(&newreq,ldb,
				      ac,
				      ac->req->op.search.base,
				      ac->req->op.search.scope,
				      ac->req->op.search.tree,
				      attrs,
				      ac->req->controls,
				      ac, acl_search_callback,
				      ac->req);
	if (ret != LDB_SUCCESS) {
                return LDB_ERR_OPERATIONS_ERROR;
	}
	/* check if there's an SD_FLAGS control */
	sd_control = ldb_request_get_control(newreq, LDB_CONTROL_SD_FLAGS_OID);
	if (sd_control) {
		/* save it locally and remove it from the list */
		/* we do not need to replace them later as we
		 * are keeping the original req intact */
		if (!save_controls(sd_control, newreq, &sd_saved_controls)) {
			return LDB_ERR_OPERATIONS_ERROR;
		}
	}
	return ldb_next_request(ac->module, newreq);
}

static int acl_forward_request(struct ldb_reply *ares,
			       struct acl_context *ac)
{
	switch (ac->req->operation) {
	case LDB_SEARCH:
		return acl_forward_search(ac);
	case LDB_ADD:
		return acl_forward_add(ares,ac);
	case LDB_MODIFY:
		return acl_forward_modify(ares,ac);
	case LDB_DELETE:
		return acl_forward_delete(ares,ac);
	case LDB_RENAME:
		return acl_forward_rename(ares,ac);
	default:
		return ldb_module_done(ac->req, ares->controls,
				       ares->response, LDB_ERR_OPERATIONS_ERROR);
	}
	return LDB_SUCCESS;
}

static int acl_visible_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	struct acl_context *ac;

	ac = talloc_get_type(req->context, struct acl_context);

	if (!ares) {
		return ldb_module_done(ac->req, NULL, NULL,
					LDB_ERR_OPERATIONS_ERROR);
	}

	if (ares->error != LDB_SUCCESS) {
		return ldb_module_done(ac->req, ares->controls,
					ares->response, ares->error);
	}

	switch (ares->type) {
	case LDB_REPLY_ENTRY:
		return acl_perform_access_check(req, ares, ac);
	case LDB_REPLY_REFERRAL:
		return ldb_module_send_referral(ac->req, ares->referral); /* what to do here actually? */
	case LDB_REPLY_DONE:
		if (ac->sec_result != LDB_SUCCESS) {
			return ldb_module_done(ac->req, ares->controls,
                                               ares->response, ac->sec_result);
		}
		return acl_forward_request(ares,ac);
	default:
		break;
	}
	return LDB_SUCCESS;
}

static enum security_user_level what_is_user(struct ldb_module *module)
{
	struct ldb_context *ldb = ldb_module_get_ctx(module);
	struct auth_session_info *session_info
		= (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
	return security_session_user_level(session_info);
}

static struct security_token * user_token(struct ldb_module *module)
{
	struct ldb_context *ldb = ldb_module_get_ctx(module);
	struct auth_session_info *session_info
		= (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
	if(!session_info) {
		return NULL;
	}
	return session_info->security_token;
}


static int make_req_access_check(struct ldb_module *module, struct ldb_request *req,
				 struct acl_context *ac, const char *filter)
{
	struct ldb_context *ldb;
	int ret;
	const char **attrs = talloc_array(ac, const char *, 3);
	struct ldb_parse_tree *tree = ldb_parse_tree(req, filter);

	attrs[0] = talloc_strdup(attrs, "ntSecurityDescriptor");
	attrs[1] = talloc_strdup(attrs, "objectClass");
	attrs[2] = NULL;

	ldb = ldb_module_get_ctx(module);
	ret = ldb_build_search_req_ex(&ac->down_req,
				      ldb, ac,
				      ac->dn_to_check,
				      LDB_SCOPE_SUBTREE,
				      tree,
				      attrs,
				      NULL,
				      ac, acl_visible_callback,
				      req);
	return ret;
}

static const char *user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
{
	struct ldb_context *ldb = ldb_module_get_ctx(module);
	struct auth_session_info *session_info
		= (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
	if (!session_info) {
		return "UNKNOWN (NULL)";
	}

	return talloc_asprintf(mem_ctx, "%s\\%s",
			       session_info->server_info->domain_name,
			       session_info->server_info->account_name);
}

static int acl_module_init(struct ldb_module *module)
{
	struct ldb_context *ldb;
	struct acl_private *data;
	int ret;

	ldb = ldb_module_get_ctx(module);

	ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
	if (ret != LDB_SUCCESS) {
		ldb_debug(ldb, LDB_DEBUG_ERROR,
			  "acl_module_init: Unable to register control with rootdse!\n");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	data = talloc(module, struct acl_private);
	data->perform_check = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
				  NULL, "acl", "perform", false);
	ldb_module_set_private(module, data);

	return ldb_next_init(module);
}

static int acl_add(struct ldb_module *module, struct ldb_request *req)
{
	int ret;
	struct acl_context *ac;
	struct ldb_dn * parent = ldb_dn_get_parent(req, req->op.add.message->dn);
	char * filter;
	struct ldb_context *ldb;
	struct acl_private *data;

	ldb = ldb_module_get_ctx(module);
	data = talloc_get_type(ldb_module_get_private(module), struct acl_private);

	if (!data->perform_check)
		return ldb_next_request(module, req);

	ac = talloc(req, struct acl_context);
	if (ac == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (what_is_user(module) == SECURITY_SYSTEM)
		return ldb_next_request(module, req);

	ac->module = module;
	ac->req = req;
	ac->ignore_security = true;
	ac->dn_to_check = ldb_dn_get_parent(req, parent);
	ac->token = user_token(module);
	ac->user_type = what_is_user(module);
	ac->sec_result = LDB_SUCCESS;
	if (!is_root_base_dn(ldb, req->op.add.message->dn) && parent && !is_root_base_dn(ldb, parent)){
		filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
					 ldb_dn_get_component_name(parent,0),
					 ldb_dn_get_component_val(parent,0)->data,
					 ldb_dn_get_component_name(ac->dn_to_check,0),
					 ldb_dn_get_component_val(ac->dn_to_check,0)->data);

		ret = make_req_access_check(module, req, ac, filter);
		if (ret != LDB_SUCCESS){
			return ret;
		}
		return ldb_next_request(module, ac->down_req);
	}
	return ldb_next_request(module, req);
}

static int acl_modify(struct ldb_module *module, struct ldb_request *req)
{
	int ret;
	struct acl_context *ac;
	struct ldb_dn * parent = ldb_dn_get_parent(req, req->op.mod.message->dn);
	char * filter;
	struct ldb_context *ldb;
	struct acl_private *data;

	ldb = ldb_module_get_ctx(module);
	data = talloc_get_type(ldb_module_get_private(module), struct acl_private);

	if (!data->perform_check)
		return ldb_next_request(module, req);

	ac = talloc(req, struct acl_context);
	if (ac == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

/* TODO Is this really right? */
/*	if (what_is_user(module) == SECURITY_SYSTEM) */
		return ldb_next_request(module, req);

	ac->module = module;
	ac->req = req;
	ac->ignore_security = true;
	ac->dn_to_check = req->op.mod.message->dn;
	ac->token = user_token(module);
	ac->user_type = what_is_user(module);
	ac->sec_result = LDB_SUCCESS;
	if (!is_root_base_dn(ldb, req->op.mod.message->dn) && parent && !is_root_base_dn(ldb, parent)){
		filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
				   ldb_dn_get_component_name(parent,0),
				   ldb_dn_get_component_val(parent,0)->data,
				   ldb_dn_get_component_name(req->op.mod.message->dn,0),
				   ldb_dn_get_component_val(req->op.mod.message->dn,0)->data);

		ret = make_req_access_check(module, req, ac, filter);
		if (ret != LDB_SUCCESS){
			return ret;
		}
		return ldb_next_request(module, ac->down_req);
	}
	return ldb_next_request(module, req);
}

/* similar to the modify for the time being.
 * We need to concider the special delete tree case, though - TODO */
static int acl_delete(struct ldb_module *module, struct ldb_request *req)
{
	int ret;
	struct acl_context *ac;
	struct ldb_dn * parent = ldb_dn_get_parent(req, req->op.del.dn);
	char * filter;
	struct ldb_context *ldb;
	struct acl_private *data;

	ldb = ldb_module_get_ctx(module);
	data = talloc_get_type(ldb_module_get_private(module), struct acl_private);

	if (!data->perform_check)
		return ldb_next_request(module, req);

	ac = talloc(req, struct acl_context);
	if (ac == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (ac->user_type == SECURITY_SYSTEM)
		return ldb_next_request(module, req);

	ac->module = module;
	ac->req = req;
	ac->ignore_security = true;
	ac->dn_to_check = req->op.del.dn;
	ac->token = user_token(module);
	ac->user_type = what_is_user(module);
	ac->sec_result = LDB_SUCCESS;
	if (parent) {
		filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
					 ldb_dn_get_component_name(parent,0),
					 ldb_dn_get_component_val(parent,0)->data,
					 ldb_dn_get_component_name(req->op.del.dn,0),
					 ldb_dn_get_component_val(req->op.del.dn,0)->data);
		ret = make_req_access_check(module, req, ac, filter);

		if (ret != LDB_SUCCESS){
			return ret;
                }
		return ldb_next_request(module, ac->down_req);
	}

	return ldb_next_request(module, req);
}

static int acl_rename(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_dn *source_parent;
	struct ldb_dn *dest_parent;
	int ret;
	struct acl_context *ac;
	char * filter;
	struct ldb_context *ldb;
	struct acl_private *data;

	ldb = ldb_module_get_ctx(module);
	data = talloc_get_type(ldb_module_get_private(module), struct acl_private);

	if (!data->perform_check)
		return ldb_next_request(module, req);

	ac = talloc(req, struct acl_context);
	if (ac == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (ac->user_type == SECURITY_SYSTEM)
		return ldb_next_request(module, req);

	ac->module = module;
	ac->req = req;
	ac->ignore_security = true;
	ac->token = user_token(module);
	ac->user_type = what_is_user(module);
	ac->sec_result = LDB_SUCCESS;

	/* We need to know if it is a simple rename or a move operation */
	source_parent = ldb_dn_get_parent(req, req->op.rename.olddn);
	dest_parent = ldb_dn_get_parent(req, req->op.rename.newdn);

	if (ldb_dn_compare(source_parent, dest_parent) == 0){
		/*Not a move, just rename*/
		filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
					 ldb_dn_get_component_name(dest_parent,0),
					 ldb_dn_get_component_val(dest_parent,0)->data,
					 ldb_dn_get_component_name(req->op.rename.olddn,0),
					 ldb_dn_get_component_val(req->op.rename.olddn,0)->data);
	}
	else{
		filter = talloc_asprintf(req,"(&(objectClass=*)(|(%s=%s)(%s=%s))))",
					 ldb_dn_get_component_name(dest_parent,0),
					 ldb_dn_get_component_val(dest_parent,0)->data,
					 ldb_dn_get_component_name(source_parent,0),
					 ldb_dn_get_component_val(source_parent,0)->data);
	}

	ret = make_req_access_check(module, req, ac, filter);

	if (ret != LDB_SUCCESS){
		return ret;
		}
	return ldb_next_request(module, ac->down_req);
}

static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	struct ldb_context *ldb;
	struct acl_context *ac;
	struct security_descriptor *sd;
	uint32_t searchFlags;
	uint32_t access_mask;
	struct object_tree *ot;
	int i, ret;
	NTSTATUS status;
	struct ldb_message_element *element_security_descriptor;
	struct ldb_message_element *element_object_class;
	const struct dsdb_attribute *attr;
	const struct dsdb_schema *schema;
	struct GUID *oc_guid;

	ac = talloc_get_type(req->context, struct acl_context);
	ldb = ldb_module_get_ctx(ac->module);
	schema = dsdb_get_schema(ldb);
	if (!ares) {
		return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
	}
	if (ares->error != LDB_SUCCESS) {
		return ldb_module_done(ac->req, ares->controls, ares->response, ares->error);
	}

	switch (ares->type) {
	case LDB_REPLY_ENTRY:
		switch (ac->user_type) {
		case SECURITY_SYSTEM:
		case SECURITY_ANONYMOUS:/*FIXME: should we let anonymous have system access*/
			break;
		default:
			/* Access checks
			 *
			 * 0. If we do not have nTSecurityDescriptor, we do not have an object in the response,
			 *    so check the parent dn.
			 * 1. Call sec_access_check on empty tree
			 * 2. For each attribute call extended_access_check
			 * 3. For each attribute call build_object_tree_form_attr_list and then check with sec_access_check
			 *
			 */
			element_security_descriptor = ldb_msg_find_element(ares->message, "nTSecurityDescriptor");
			element_object_class = ldb_msg_find_element(ares->message, "objectClass");
			if (!element_security_descriptor || !element_object_class)
				break;

			sd = talloc(ldb, struct security_descriptor);
			if(!sd) {
			  return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
			}
			if(!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&element_security_descriptor->values[0],
									 ldb,
									 NULL,
									 sd,
									 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
				DEBUG(0, ("acl_search_callback: Error parsing security descriptor\n"));
				return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
			}

			oc_guid = class_schemaid_guid_by_lDAPDisplayName(schema, element_object_class->values[0].data);
			for (i=0; i<ares->message->num_elements; i++) {
				attr = dsdb_attribute_by_lDAPDisplayName(schema, ares->message->elements[i].name);
				if (attr) {
					searchFlags = attr->searchFlags;
				} else {
					searchFlags = 0x0;
				}

				/*status = extended_access_check(ares->message->elements[i].name, access_mask, searchFlags); */ /* Todo FIXME */
				ac->access_needed = SEC_ADS_READ_PROP;
				if (NT_STATUS_IS_OK(status)) {
					ot = insert_in_object_tree(req, oc_guid, NULL, ac->access_needed, NULL);

					insert_in_object_tree(req,
							      &attr->schemaIDGUID,
							      &attr->attributeSecurityGUID,
							      ac->access_needed,
							      ot);

					status = sec_access_check_ds(sd,
								     ac->token,
								     ac->access_needed,
								     &access_mask,
								     ot);

					if (NT_STATUS_IS_OK(status)) {
						continue;
					}
				}
				ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
			}
			break;
		}
		if (ac->nTSecurityDescriptor) {
			ldb_msg_remove_attr(ares->message, "nTSecurityDescriptor");
		} else if (ac->objectClass) {
			ldb_msg_remove_attr(ares->message, "objectClass");
		}

		return ldb_module_send_entry(ac->req, ares->message, ares->controls);
	case LDB_REPLY_REFERRAL:
		return ldb_module_send_referral(ac->req, ares->referral);

	case LDB_REPLY_DONE:
		return ldb_module_done(ac->req, ares->controls,ares->response, LDB_SUCCESS);
	}

        return LDB_SUCCESS;
}

static int acl_search(struct ldb_module *module, struct ldb_request *req)
{
	int ret;
	struct ldb_context *ldb;
	struct acl_context *ac;
	const char **attrs;
	struct ldb_control *sd_control;
	struct ldb_control **sd_saved_controls;
	struct ldb_dn * parent;
	struct acl_private *data;

	ldb = ldb_module_get_ctx(module);
	data = talloc_get_type(ldb_module_get_private(module), struct acl_private);

	if (!data || !data->perform_check)
		return ldb_next_request(module, req);

	if (what_is_user(module) == SECURITY_SYSTEM)
		return ldb_next_request(module, req);

	ac = talloc_get_type(req->context, struct acl_context);
	if ( ac == NULL ) {
		ac = talloc(req, struct acl_context);
		if (ac == NULL) {
			ldb_oom(ldb);
			return LDB_ERR_OPERATIONS_ERROR;
		}
		ac->module = module;
		ac->req = req;
		ac->ignore_security = false;
		ac->user_type = what_is_user(module);
		ac->token = user_token(module);
		ac->dn_to_check = req->op.search.base;
		ac->sec_result = LDB_SUCCESS;

		attrs = talloc_array(ac, const char*, 2);
		attrs[0] = talloc_strdup(attrs, "nTSecurityDescriptor");
		attrs[1] = NULL;
		parent = ldb_dn_get_parent(req, ac->dn_to_check);
		if (!is_root_base_dn(ldb, req->op.search.base) && parent && !is_root_base_dn(ldb, parent)) {
			/*we have parent so check for visibility*/
			ret = ldb_build_search_req(&ac->down_req,
						   ldb, ac,
						   parent,
						   LDB_SCOPE_BASE,
						   "(objectClass=*)",
						   attrs,
						   req->controls,
						   ac, acl_visible_callback,
						   req);
			if (ret != LDB_SUCCESS) {
				return ret;
			}
			return ldb_next_request(module, ac->down_req);
		} else {
			return acl_forward_search(ac);
		}
	}

	return ldb_next_request(module, req);
}

static int acl_extended(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_context *ldb = ldb_module_get_ctx(module);
	enum security_user_level user_type;
	struct acl_private *data;

	data = talloc_get_type(ldb_module_get_private(module), struct acl_private);

	if (!data->perform_check)
		return ldb_next_request(module, req);

	/* allow everybody to read the sequence number */
	if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
		return ldb_next_request(module, req);
	}

	user_type = what_is_user(module);
	switch (user_type) {
	case SECURITY_SYSTEM:
	case SECURITY_ADMINISTRATOR:
		return ldb_next_request(module, req);
	default:
		ldb_asprintf_errstring(ldb,
				       "acl_extended: attempted database modify not permitted."
				       "User %s is not SYSTEM or an Administrator",
				       user_name(req, module));
		return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
	}
}

_PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
	.name		   = "acl",
	.search            = acl_search,
	.add               = acl_add,
	.modify            = acl_modify,
	.del               = acl_delete,
	.rename            = acl_rename,
	.extended          = acl_extended,
	.init_context	   = acl_module_init
};