-
Notifications
You must be signed in to change notification settings - Fork 80
/
uid.c
1651 lines (1447 loc) · 51.6 KB
/
uid.c
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
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*
* uid.c
*
* Implements a directory server pre-operation plugin to test
* attributes for uniqueness within a defined subtree in the
* directory.
*
* Called uid.c since the original purpose of the plugin was to
* check the uid attribute in user entries.
*/
#include <slapi-plugin.h>
#include <portable.h>
#include <slapi-private.h>
#include <string.h>
#include "plugin-utils.h"
#include "nspr.h"
#if defined(LDAP_ERROR_LOGGING) && !defined(DEBUG)
#define DEBUG
#endif
#define UNTAGGED_PARAMETER 12
/* Quoting routine - this should be in a library somewhere (slapi?) */
int ldap_quote_filter_value(
char *value,
int len,
char *out,
int maxLen,
int *outLen);
static int search_one_berval(Slapi_DN *baseDN, const char **attrNames, const struct berval *value, const char *requiredObjectClass, Slapi_DN *target, Slapi_DN **excludes);
/*
* ISSUES:
* How should this plugin handle ACL issues? It seems wrong to reject
* adds and modifies because there is already a conflicting UID, when
* the request would have failed because of an ACL check anyway.
*
* This code currently defines a maximum filter string size of 512. Is
* this large enough?
*
* This code currently does not quote the value portion of the filter as
* it is created. This is a bug.
*/
/* */
#define BEGIN do {
#define END \
} \
while (0) \
;
/*
* Slapi plugin descriptor
*/
static char *plugin_name = "NSUniqueAttr";
static Slapi_PluginDesc
pluginDesc = {
"NSUniqueAttr", VENDOR, DS_PACKAGE_VERSION,
"Enforce unique attribute values"};
static void *plugin_identity = NULL;
typedef struct attr_uniqueness_config
{
const char **attrs;
char *attr_friendly;
Slapi_DN **subtrees;
Slapi_DN **exclude_subtrees;
PRBool unique_in_all_subtrees;
char *top_entry_oc;
char *subtree_entries_oc;
struct attr_uniqueness_config *next;
} attr_uniqueness_config_t;
#define ATTR_UNIQUENESS_ATTRIBUTE_NAME "uniqueness-attribute-name"
#define ATTR_UNIQUENESS_SUBTREES "uniqueness-subtrees"
#define ATTR_UNIQUENESS_EXCLUDE_SUBTREES "uniqueness-exclude-subtrees"
#define ATTR_UNIQUENESS_ACROSS_ALL_SUBTREES "uniqueness-across-all-subtrees"
#define ATTR_UNIQUENESS_TOP_ENTRY_OC "uniqueness-top-entry-oc"
#define ATTR_UNIQUENESS_SUBTREE_ENTRIES_OC "uniqueness-subtree-entries-oc"
static int getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass, char **requiredObjectClass);
static struct attr_uniqueness_config *uniqueness_entry_to_config(Slapi_PBlock *pb, Slapi_Entry *config_entry);
/*
* More information about constraint failure
*/
static char *moreInfo =
"Another entry with the same attribute value already exists (attribute: \"%s\")";
static void
free_uniqueness_config(struct attr_uniqueness_config *config)
{
int i;
for (i = 0; config->attrs && config->attrs[i]; i++) {
slapi_ch_free_string((char **)&(config->attrs[i]));
}
for (i = 0; config->subtrees && config->subtrees[i]; i++) {
slapi_sdn_free(&config->subtrees[i]);
}
for (i = 0; config->exclude_subtrees && config->exclude_subtrees[i]; i++) {
slapi_sdn_free(&config->exclude_subtrees[i]);
}
slapi_ch_free((void **)&config->attrs);
slapi_ch_free((void **)&config->subtrees);
slapi_ch_free((void **)&config->exclude_subtrees);
slapi_ch_free_string(&config->attr_friendly);
slapi_ch_free_string((char **)&config->top_entry_oc);
slapi_ch_free_string((char **)&config->subtree_entries_oc);
}
/*
* New styles:
* ----------
*
* uniqueness-attribute-name: uid
* uniqueness-subtrees: dc=people,dc=example,dc=com
* uniqueness-subtrees: dc=sales, dc=example,dc=com
* uniqueness-exclude-subtrees: dc=machines, dc=examples, dc=com
* uniqueness-across-all-subtrees: on
*
* or
*
* uniqueness-attribute-name: uid
* uniqueness-top-entry-oc: organizationalUnit
* uniqueness-subtree-entries-oc: person
*
* If both are present:
* - uniqueness-subtrees
* - uniqueness-top-entry-oc/uniqueness-subtree-entries-oc
* Then uniqueness-subtrees has the priority
*
* Old styles:
* ----------
*
* nsslapd-pluginarg0: uid
* nsslapd-pluginarg1: dc=people,dc=example,dc=com
* nsslapd-pluginarg2: dc=sales, dc=example,dc=com
*
* or
*
* nsslapd-pluginarg0: attribute=uid
* nsslapd-pluginarg1: markerobjectclass=organizationalUnit
* nsslapd-pluginarg2: requiredobjectclass=person
*
* From a Slapi_Entry of the config entry, it creates a attr_uniqueness_config.
* It returns a (attr_uniqueness_config *) if the configuration is valid
* Else it returns NULL
*/
static struct attr_uniqueness_config *
uniqueness_entry_to_config(Slapi_PBlock *pb, Slapi_Entry *config_entry)
{
attr_uniqueness_config_t *tmp_config = NULL;
char **values = NULL;
int argc;
char **argv = NULL;
int rc = SLAPI_PLUGIN_SUCCESS;
int i;
int attrLen = 0;
char *fp;
int nb_subtrees = 0;
if (config_entry == NULL) {
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
/* We are going to fill tmp_config in a first phase */
if ((tmp_config = (attr_uniqueness_config_t *)slapi_ch_calloc(1, sizeof(attr_uniqueness_config_t))) == NULL) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - "
"Failed to allocate configuration\n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
} else {
/* set these to -1 for config validation */
}
/* Check if this is new/old config style */
slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc);
if (argc == 0) {
/* This is new config style
* uniqueness-attribute-name: uid
* uniqueness-subtrees: dc=people,dc=example,dc=com
* uniqueness-subtrees: dc=sales, dc=example,dc=com
* uniqueness-across-all-subtrees: on
*
* or
*
* uniqueness-attribute-name: uid
* uniqueness-top-entry-oc: organizationalUnit
* uniqueness-subtree-entries-oc: person
*/
/* Attribute name of the attribute we are going to check value uniqueness */
values = slapi_entry_attr_get_charray(config_entry, ATTR_UNIQUENESS_ATTRIBUTE_NAME);
if (values) {
for (i = 0; values && values[i]; i++)
;
tmp_config->attrs = (const char **)slapi_ch_calloc(i + 1, sizeof(char *));
for (i = 0; values && values[i]; i++) {
tmp_config->attrs[i] = slapi_ch_strdup(values[i]);
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, "uniqueness_entry_to_config - "
"Adding attribute %s to uniqueness set\n",
tmp_config->attrs[i]);
}
slapi_ch_array_free(values);
values = NULL;
}
/* Subtrees where uniqueness is tested */
values = slapi_entry_attr_get_charray(config_entry, ATTR_UNIQUENESS_SUBTREES);
if (values) {
for (i = 0; values && values[i]; i++)
;
/* slapi_ch_calloc never returns NULL unless the 2 args are 0 or negative. */
tmp_config->subtrees = (Slapi_DN **)slapi_ch_calloc(i + 1, sizeof(Slapi_DN *));
/* copy the valid subtree DN into the config */
for (i = 0, nb_subtrees = 0; values && values[i]; i++) {
if (slapi_dn_syntax_check(pb, values[i], 1)) { /* syntax check failed */
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - "
"Invalid DN (skipped): %s\n",
values[i]);
continue;
}
tmp_config->subtrees[nb_subtrees] = slapi_sdn_new_dn_byval(values[i]);
nb_subtrees++;
}
slapi_ch_array_free(values);
values = NULL;
}
/* Subtrees where uniqueness is explicitly ignored */
values = slapi_entry_attr_get_charray(config_entry, ATTR_UNIQUENESS_EXCLUDE_SUBTREES);
if (values) {
for (i = 0; values && values[i]; i++)
;
/* slapi_ch_calloc never returns NULL unless the 2 args are 0 or negative. */
tmp_config->exclude_subtrees = (Slapi_DN **)slapi_ch_calloc(i + 1, sizeof(Slapi_DN *));
/* copy the valid subtree DN into the config */
for (i = 0, nb_subtrees = 0; values && values[i]; i++) {
if (slapi_dn_syntax_check(pb, values[i], 1)) { /* syntax check failed */
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - "
"Invalid DN (skipped): %s\n",
values[i]);
continue;
}
tmp_config->exclude_subtrees[nb_subtrees] = slapi_sdn_new_dn_byval(values[i]);
nb_subtrees++;
}
slapi_ch_array_free(values);
values = NULL;
}
/* Uniqueness may be enforced accross all the subtrees, by default it is not */
tmp_config->unique_in_all_subtrees = slapi_entry_attr_get_bool(config_entry, ATTR_UNIQUENESS_ACROSS_ALL_SUBTREES);
/* enforce uniqueness only if the modified entry has this objectclass */
tmp_config->top_entry_oc = slapi_entry_attr_get_charptr(config_entry, ATTR_UNIQUENESS_TOP_ENTRY_OC);
/* enforce uniqueness, in the modified entry subtree, only to entries having this objectclass */
tmp_config->subtree_entries_oc = slapi_entry_attr_get_charptr(config_entry, ATTR_UNIQUENESS_SUBTREE_ENTRIES_OC);
} else {
int result;
char *attrName = NULL;
char *markerObjectClass = NULL;
char *requiredObjectClass = NULL;
/* using the old style of configuration */
result = getArguments(pb, &attrName, &markerObjectClass, &requiredObjectClass);
if (LDAP_OPERATIONS_ERROR == result) {
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, "uniqueness_entry_to_config - "
"Unable to parse old style\n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
if (UNTAGGED_PARAMETER == result) {
/* This is
* nsslapd-pluginarg0: uid
* nsslapd-pluginarg1: dc=people,dc=example,dc=com
* nsslapd-pluginarg2: dc=sales, dc=example,dc=com
*
* config attribute are in argc/argv
*
* attrName is set
* markerObjectClass/requiredObjectClass are NOT set
*/
if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc) || slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv)) {
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, "uniqueness_entry_to_config - "
"Only attribute name is valid\n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
/* Store attrName in the config */
tmp_config->attrs = (const char **)slapi_ch_calloc(2, sizeof(char *));
tmp_config->attrs[0] = slapi_ch_strdup(attrName);
argc--;
argv++; /* First argument was attribute name and remaining are subtrees */
/* Store the subtrees */
nb_subtrees = 0;
if ((tmp_config->subtrees = (Slapi_DN **)slapi_ch_calloc(argc + 1, sizeof(Slapi_DN *))) == NULL) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - "
"Fail to allocate subtree array\n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
for (; argc > 0; argc--, argv++) {
if (slapi_dn_syntax_check(pb, *argv, 1)) { /* syntax check failed */
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - "
"Invalid DN (skipped): %s\n",
*argv);
continue;
}
tmp_config->subtrees[nb_subtrees] = slapi_sdn_new_dn_byval(*argv);
nb_subtrees++;
}
/* this interface does not configure accross subtree uniqueness*/
tmp_config->unique_in_all_subtrees = PR_FALSE;
/* Not really usefull, but it clarifies the config */
tmp_config->subtree_entries_oc = NULL;
tmp_config->top_entry_oc = NULL;
} else {
/* This is
* nsslapd-pluginarg0: attribute=uid
* nsslapd-pluginarg1: markerobjectclass=organizationalUnit
* nsslapd-pluginarg2: requiredobjectclass=person
*
* config attributes are in
* - attrName
* - markerObjectClass
* - requiredObjectClass
*/
/* Store attrName in the config */
tmp_config->attrs = (const char **)slapi_ch_calloc(2, sizeof(char *));
tmp_config->attrs[0] = slapi_ch_strdup(attrName);
/* There is no subtrees */
tmp_config->subtrees = NULL;
/* this interface does not configure accross subtree uniqueness*/
tmp_config->unique_in_all_subtrees = PR_FALSE;
/* set the objectclasses retrieved by getArgument */
tmp_config->subtree_entries_oc = slapi_ch_strdup(requiredObjectClass);
tmp_config->top_entry_oc = slapi_ch_strdup(markerObjectClass);
}
}
/* Time to check that the new configuration is valid */
/* Check that we have 1 or more value */
if (tmp_config->attrs == NULL) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - Attribute name not defined \n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
/* If the config is valid, prepare the friendly string for error messages */
for (i = 0; tmp_config->attrs && (tmp_config->attrs)[i]; i++) {
attrLen += strlen((tmp_config->attrs)[i]) + 1;
}
tmp_config->attr_friendly = (char *)slapi_ch_calloc(attrLen + 1, sizeof(char));
fp = tmp_config->attr_friendly;
for (i = 0; tmp_config->attrs && (tmp_config->attrs)[i]; i++) {
strcpy(fp, (tmp_config->attrs)[i]);
fp += strlen((tmp_config->attrs)[i]);
strcpy(fp, " ");
fp++;
}
/* Only ensure subtrees are set, no need to check excluded subtrees as setting exclusion without actual subtrees make little sense */
if (tmp_config->subtrees == NULL) {
/* Uniqueness is enforced on entries matching objectclass */
if (tmp_config->subtree_entries_oc == NULL) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - Objectclass for subtree entries is not defined\n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
} else if (tmp_config->subtrees[0] == NULL) {
/* Uniqueness is enforced on subtrees but none are defined */
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "uniqueness_entry_to_config - No valid subtree is defined \n");
rc = SLAPI_PLUGIN_FAILURE;
goto done;
}
done:
if (rc != SLAPI_PLUGIN_SUCCESS) {
if (tmp_config) {
free_uniqueness_config(tmp_config);
slapi_ch_free((void **)&tmp_config);
}
return NULL;
} else {
return tmp_config;
}
}
static void
freePblock(Slapi_PBlock *spb)
{
if (spb) {
slapi_free_search_results_internal(spb);
slapi_pblock_destroy(spb);
}
}
/* ------------------------------------------------------------ */
/*
* op_error - Record (and report) an operational error.
* name changed to uid_op_error so as not to conflict with the external function
* of the same name thereby preventing compiler warnings.
*/
static int
uid_op_error(int internal_error)
{
slapi_log_err(
SLAPI_LOG_PLUGIN,
plugin_name,
"Internal error: %d\n",
internal_error);
return LDAP_OPERATIONS_ERROR;
}
/* ------------------------------------------------------------ */
/*
* Create an LDAP search filter from the attribute
* name and value supplied.
*/
static char *
create_filter(const char **attributes, const struct berval *value, const char *requiredObjectClass)
{
char *filter = NULL;
char *fp;
char *max;
int *attrLen = NULL;
int totalAttrLen = 0;
int attrCount = 0;
int valueLen;
int classLen = 0;
int filterLen;
int i = 0;
PR_ASSERT(attributes);
/* Compute the length of the required buffer */
for (attrCount = 0; attributes && attributes[attrCount]; attrCount++)
;
attrCount++;
attrLen = (int *)slapi_ch_calloc(attrCount, sizeof(int));
for (i = 0; attributes && attributes[i]; i++) {
attrLen[i] += strlen(attributes[i]);
totalAttrLen += attrLen[i];
}
/* if attrCount is 1, attrLen is already corect for usage.*/
if (attrCount > 1) {
/* Filter will be (|(attr=value)(attr=value)) */
/* 3 for the (| ) */
/* 3 for each attr for (=) not in attr or value */
totalAttrLen += 3 + (attrCount * 3);
} else {
totalAttrLen += 3;
}
if (ldap_quote_filter_value(value->bv_val, value->bv_len, 0, 0, &valueLen)) {
slapi_ch_free((void **)&attrLen);
return filter;
}
if (requiredObjectClass) {
classLen = strlen(requiredObjectClass);
/* "(&(objectClass=)<Filter here>)" == 17 */
filterLen = totalAttrLen + 1 + (valueLen * attrCount) + classLen + 17 + 1;
} else {
filterLen = totalAttrLen + 1 + (valueLen * attrCount) + 1;
}
/* Allocate the buffer */
filter = (char *)slapi_ch_calloc(1, filterLen + 1);
fp = filter;
max = &filter[filterLen];
/* Place AND expression and objectClass in filter */
if (requiredObjectClass) {
strcpy(fp, "(&(objectClass=");
fp += 15;
strcpy(fp, requiredObjectClass);
fp += classLen;
*fp++ = ')';
}
if (attrCount == 1) {
*fp++ = '(';
/* Place attribute name in filter */
strcpy(fp, attributes[0]);
fp += attrLen[0];
/* Place comparison operator */
*fp++ = '=';
/* Place value in filter */
if (ldap_quote_filter_value(value->bv_val, value->bv_len, fp, max - fp, &valueLen)) {
slapi_ch_free_string(&filter);
slapi_ch_free((void **)&attrLen);
return 0;
}
fp += valueLen;
*fp++ = ')';
} else {
strcpy(fp, "(|");
fp += 2;
for (i = 0; attributes && attributes[i]; i++) {
strcpy(fp, "(");
fp += 1;
/* Place attribute name in filter */
strcpy(fp, attributes[i]);
fp += attrLen[i];
/* Place comparison operator */
*fp++ = '=';
/* Place value in filter */
if (ldap_quote_filter_value(value->bv_val, value->bv_len, fp, max - fp, &valueLen)) {
slapi_ch_free_string(&filter);
slapi_ch_free((void **)&attrLen);
return 0;
}
fp += valueLen;
strcpy(fp, ")");
fp += 1;
}
strcpy(fp, ")");
fp += 1;
}
/* Close AND expression if a requiredObjectClass was set */
if (requiredObjectClass) {
*fp++ = ')';
}
/* Terminate */
*fp = 0;
slapi_ch_free((void **)&attrLen);
return filter;
}
/* ------------------------------------------------------------ */
/*
* search - search a subtree for entries with a named attribute matching
* the list of values. An entry matching the 'target' DN is
* not considered in the search.
*
* If 'attr' is NULL, the values are taken from 'values'.
* If 'attr' is non-NULL, the values are taken from 'attr'.
*
* Return:
* LDAP_SUCCESS - no matches, or the attribute matches the
* target dn.
* LDAP_CONSTRAINT_VIOLATION - an entry was found that already
* contains the attribute value.
* LDAP_OPERATIONS_ERROR - a server failure.
*/
static int
search(Slapi_DN *baseDN, const char **attrNames, Slapi_Attr *attr, struct berval **values, const char *requiredObjectClass, Slapi_DN *target, Slapi_DN **excludes)
{
int result;
#ifdef DEBUG
/* Fix this later to print all the attr names */
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name,
"search - SEARCH baseDN=%s attr=%s target=%s\n", slapi_sdn_get_dn(baseDN), attrNames[0],
target ? slapi_sdn_get_dn(target) : "None");
#endif
result = LDAP_SUCCESS;
/* If no values, can't possibly be a conflict */
if ((Slapi_Attr *)NULL == attr && (struct berval **)NULL == values)
return result;
/*
* Perform the search for each value provided
*
* Another possibility would be to search for all the values at once.
* However, this is more complex (for filter creation) and unique
* attributes values are probably only changed one at a time anyway.
*/
if ((Slapi_Attr *)NULL != attr) {
Slapi_Value *v = NULL;
int vhint = -1;
for (vhint = slapi_attr_first_value(attr, &v);
vhint != -1 && LDAP_SUCCESS == result;
vhint = slapi_attr_next_value(attr, vhint, &v)) {
result = search_one_berval(baseDN, attrNames,
slapi_value_get_berval(v),
requiredObjectClass, target, excludes);
}
} else {
for (; *values != NULL && LDAP_SUCCESS == result; values++) {
result = search_one_berval(baseDN, attrNames, *values, requiredObjectClass,
target, excludes);
}
}
#ifdef DEBUG
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name,
"search - SEARCH result = %d\n", result);
#endif
return (result);
}
static int
search_one_berval(Slapi_DN *baseDN, const char **attrNames, const struct berval *value, const char *requiredObjectClass, Slapi_DN *target, Slapi_DN **excludes)
{
int result;
char *filter;
Slapi_PBlock *spb;
result = LDAP_SUCCESS;
/* If no value, can't possibly be a conflict */
if ((struct berval *)NULL == value)
return result;
filter = 0;
spb = 0;
BEGIN
int err;
int sres;
Slapi_Entry **entries;
static char *attrs[] = {"1.1", 0};
/* Create the filter - this needs to be freed */
filter = create_filter(attrNames, value, requiredObjectClass);
#ifdef DEBUG
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name,
"search_one_berval - SEARCH filter=%s\n", filter);
#endif
/* Perform the search using the new internal API */
spb = slapi_pblock_new();
if (!spb) {
result = uid_op_error(2);
break;
}
slapi_search_internal_set_pb_ext(spb, baseDN, LDAP_SCOPE_SUBTREE,
filter, attrs, 0 /* attrs only */, NULL, NULL, plugin_identity, 0 /* actions */);
slapi_search_internal_pb(spb);
err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_RESULT, &sres);
if (err) {
result = uid_op_error(3);
break;
}
/* Allow search to report that there is nothing in the subtree */
if (sres == LDAP_NO_SUCH_OBJECT)
break;
/* Other errors are bad */
if (sres) {
result = uid_op_error(3);
break;
}
err = slapi_pblock_get(spb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
&entries);
if (err) {
result = uid_op_error(4);
break;
}
/*
* Look at entries returned. Any entry found must be the
* target entry or the constraint fails.
*/
for (; *entries; entries++) {
#ifdef DEBUG
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name,
"search_one_berval - SEARCH entry dn=%s\n", slapi_entry_get_dn(*entries));
#endif
/*
* It is a Constraint Violation if any entry is found, unless
* the entry is the target entry (if any).
*/
if (!target || slapi_sdn_compare(slapi_entry_get_sdn(*entries), target) != 0) {
int i;
result = LDAP_CONSTRAINT_VIOLATION;
if (excludes == NULL || *excludes == NULL) {
break;
}
/* Do the same check for excluded subtrees as resulted entries may have matched them */
for (i = 0; excludes && excludes[i]; i++) {
Slapi_DN *entry_dn = slapi_entry_get_sdn(*entries);
if (slapi_sdn_issuffix(entry_dn, excludes[i])) {
result = LDAP_SUCCESS;
break;
}
}
if (result == LDAP_CONSTRAINT_VIOLATION) {
break;
}
}
}
#ifdef DEBUG
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name,
"search_one_berval - SEARCH complete result=%d\n", result);
#endif
END
/* Clean-up */
if (spb)
{
slapi_free_search_results_internal(spb);
slapi_pblock_destroy(spb);
}
slapi_ch_free((void **)&filter);
return result;
}
/* ------------------------------------------------------------ */
/*
* searchAllSubtrees - search all subtrees in argv for entries
* with a named attribute matching the list of values, by
* calling search for each one.
*
* If 'attr' is NULL, the values are taken from 'values'.
* If 'attr' is non-NULL, the values are taken from 'attr'.
*
* Return:
* LDAP_SUCCESS - no matches, or the attribute matches the
* source (target) dn.
* LDAP_CONSTRAINT_VIOLATION - an entry was found that already
* contains the attribute value.
* LDAP_OPERATIONS_ERROR - a server failure.
*/
static int
searchAllSubtrees(Slapi_DN **subtrees, Slapi_DN **exclude_subtrees, const char **attrNames, Slapi_Attr *attr, struct berval **values, const char *requiredObjectClass, Slapi_DN *destinationSDN, Slapi_DN *sourceSDN, PRBool unique_in_all_subtrees)
{
int result = LDAP_SUCCESS;
int i;
if (unique_in_all_subtrees) {
PRBool in_a_subtree = PR_FALSE;
/* we need to check that the added values of this attribute
* are unique in all the monitored subtrees
*/
/* First check the destination entry is in one of
* the monitored subtree, so adding 'values' would
* violate constraint
*/
for (i = 0; subtrees && subtrees[i]; i++) {
if (slapi_sdn_issuffix(destinationSDN, subtrees[i])) {
in_a_subtree = PR_TRUE;
break;
}
}
if (!in_a_subtree) {
return result;
}
}
/* If DN is in the excluded subtrees, we should ignore it in any case, not only
* in the case of uniqueness in all subtrees. */
if (exclude_subtrees != NULL) {
PRBool in_a_subtree = PR_FALSE;
for (i = 0; exclude_subtrees && exclude_subtrees[i]; i++) {
if (slapi_sdn_issuffix(destinationSDN, exclude_subtrees[i])) {
in_a_subtree = PR_TRUE;
break;
}
}
if (in_a_subtree) {
return result;
}
}
/*
* For each DN in the managed list, do uniqueness checking if
* the destination (target) DN is a subnode in the tree.
*/
for (i = 0; subtrees && subtrees[i]; i++) {
Slapi_DN *sufdn = subtrees[i];
/*
* The DN should already be normalized, so we don't have to
* worry about that here.
*/
if (unique_in_all_subtrees || slapi_sdn_issuffix(destinationSDN, sufdn)) {
result = search(sufdn, attrNames, attr, values, requiredObjectClass, sourceSDN, exclude_subtrees);
if (result)
break;
}
}
return result;
}
/* ------------------------------------------------------------ */
/*
* getArguments - parse invocation parameters
* Return:
* 0 - success
* >0 - error parsing parameters
*/
static int
getArguments(Slapi_PBlock *pb, char **attrName, char **markerObjectClass, char **requiredObjectClass)
{
int argc;
char **argv;
/*
* Get the arguments
*/
if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, &argc)) {
return uid_op_error(10);
}
if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, &argv)) {
return uid_op_error(11);
}
/*
* Required arguments: attribute and markerObjectClass
* Optional argument: requiredObjectClass
*/
for (; argc > 0; argc--, argv++) {
char *param = *argv;
char *delimiter = strchr(param, '=');
if (NULL == delimiter) {
/* Old style untagged parameter */
*attrName = *argv;
return UNTAGGED_PARAMETER;
}
if (strncasecmp(param, "attribute", delimiter - param) == 0) {
/* It's OK to set a pointer here, because ultimately it points
* inside the argv array of the pblock, which will be staying
* arround.
*/
*attrName = delimiter + 1;
} else if (strncasecmp(param, "markerobjectclass", delimiter - param) == 0) {
*markerObjectClass = delimiter + 1;
} else if (strncasecmp(param, "requiredobjectclass", delimiter - param) == 0) {
*requiredObjectClass = delimiter + 1;
}
}
if (!*attrName || !*markerObjectClass) {
return uid_op_error(13);
}
return 0;
}
/* ------------------------------------------------------------ */
/*
* findSubtreeAndSearch - walk up the tree to find an entry with
* the marker object class; if found, call search from there and
* return the result it returns
*
* If 'attr' is NULL, the values are taken from 'values'.
* If 'attr' is non-NULL, the values are taken from 'attr'.
*
* Return:
* LDAP_SUCCESS - no matches, or the attribute matches the
* source (target) dn.
* LDAP_CONSTRAINT_VIOLATION - an entry was found that already
* contains the attribute value.
* LDAP_OPERATIONS_ERROR - a server failure.
*/
static int
findSubtreeAndSearch(Slapi_DN *destinationSDN, const char **attrNames, Slapi_Attr *attr, struct berval **values, const char *requiredObjectClass, Slapi_DN *sourceSDN, const char *markerObjectClass, Slapi_DN **excludes)
{
int result = LDAP_SUCCESS;
Slapi_PBlock *spb = NULL;
Slapi_DN *curpar = slapi_sdn_new();
Slapi_DN *newpar = NULL;
slapi_sdn_get_parent(destinationSDN, curpar);
while (slapi_sdn_get_dn(curpar) != NULL) {
if ((spb = dnHasObjectClass(curpar, markerObjectClass))) {
freePblock(spb);
/*
* Do the search. There is no entry that is allowed
* to have the attribute already.
*/
result = search(curpar, attrNames, attr, values, requiredObjectClass,
sourceSDN, excludes);
break;
}
newpar = slapi_sdn_new();
slapi_sdn_copy(curpar, newpar);
slapi_sdn_get_parent(newpar, curpar);
slapi_sdn_free(&newpar);
}
slapi_sdn_free(&curpar);
return result;
}
/* ------------------------------------------------------------ */
/*
* preop_add - pre-operation plug-in for add
*/
static int
preop_add(Slapi_PBlock *pb)
{
int result;
char *errtext = NULL;
const char **attrNames = NULL;
char *attr_friendly = NULL;
#ifdef DEBUG
slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, "ADD begin\n");
#endif
result = LDAP_SUCCESS;
/*
* Do constraint check on the added entry. Set result.
*/
BEGIN
int err;
char *markerObjectClass = NULL;
char *requiredObjectClass = NULL;
Slapi_DN *targetSDN = NULL;
int isupdatedn;
Slapi_Entry *e;
Slapi_Attr *attr;
struct attr_uniqueness_config *config = NULL;
int i = 0;
/*
* If this is a replication update, just be a noop.
*/
err = slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &isupdatedn);
if (err) {
result = uid_op_error(50);
break;
}
if (isupdatedn) {
break;
}
slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &config);
if (config == NULL) {
slapi_log_err(SLAPI_LOG_ERR, plugin_name, "preop_add - Failed to retrieve the config\n");
result = LDAP_OPERATIONS_ERROR;
break;
}
/*
* Get the arguments
*/
attrNames = config->attrs;
markerObjectClass = config->top_entry_oc;
requiredObjectClass = config->subtree_entries_oc;
attr_friendly = config->attr_friendly;
/*