-
Notifications
You must be signed in to change notification settings - Fork 81
/
entry.c
4301 lines (3873 loc) · 127 KB
/
entry.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
* 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; version 2 of the License.
*
* 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, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* In addition, as a special exception, Red Hat, Inc. gives You the additional
* right to link the code of this Program with code not covered under the GNU
* General Public License ("Non-GPL Code") and to distribute linked combinations
* including the two, subject to the limitations in this paragraph. Non-GPL Code
* permitted under this exception must only link to the code of this Program
* through those well defined interfaces identified in the file named EXCEPTION
* found in the source code files (the "Approved Interfaces"). The files of
* Non-GPL Code may instantiate templates or use macros or inline functions from
* the Approved Interfaces without causing the resulting work to be covered by
* the GNU General Public License. Only Red Hat, Inc. may make changes or
* additions to the list of Approved Interfaces. You must obey the GNU General
* Public License in all respects for all of the Program code and other code used
* in conjunction with the Program except the Non-GPL Code covered by this
* exception. If you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you do not wish to
* provide this exception without modification, you must delete this exception
* statement from your version and license this file solely under the GPL without
* exception.
*
*
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* entry.c - routines for dealing with entries */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#undef DEBUG /* disable counters */
#include <prcountr.h>
#include "slap.h"
#undef ENTRY_DEBUG
#define DELETED_ATTR_STRING ";deletedattribute"
#define DELETED_ATTR_STRSIZE 17 /* sizeof(";deletedattribute") */
#define DELETED_VALUE_STRING ";deleted"
#define DELETED_VALUE_STRSIZE 8 /* sizeof(";deleted") */
/* a helper function to set special rdn to a tombstone entry */
static int _entry_set_tombstone_rdn(Slapi_Entry *e, const char *normdn);
/* computation of the size of the vattr in the entry */
#define VATTR_READ_LOCK(e) slapi_rwlock_rdlock(e->e_virtual_lock)
#define VATTR_READ_UNLOCK(e) slapi_rwlock_unlock(e->e_virtual_lock)
#define VATTR_WRITE_LOCK(e) slapi_rwlock_wrlock(e->e_virtual_lock)
#define VATTR_WRITE_UNLOCK(e) slapi_rwlock_unlock(e->e_virtual_lock)
static size_t entry_vattr_size(Slapi_Entry *e);
static struct _entry_vattr *entry_vattr_lookup_nolock(const Slapi_Entry *e, const char *attr_name);
static void entry_vattr_add_nolock(Slapi_Entry *e, const char *type, Slapi_Attr *attr);
static void entry_vattr_free_nolock(Slapi_Entry *e);
/* protected attributes which are not included in the flattened entry,
* which will be stored in the db. */
static char **protected_attrs_all = NULL;
/*
* add or delete attr to or from protected_attr_all list depending on the flag.
* flag: 0 -- add
* 1 -- delete
*/
void
set_attr_to_protected_list(char *attr, int flag)
{
if (charray_inlist(protected_attrs_all, attr)) { /* attr is in the list */
if (flag) { /* delete */
charray_remove(protected_attrs_all, attr, 1);
}
} else { /* attr is not in the list */
if (!flag) { /* add */
charray_add(&protected_attrs_all, slapi_ch_strdup(attr));
}
}
}
#if defined(USE_OLD_UNHASHED)
static char *forbidden_attrs [] = {PSEUDO_ATTR_UNHASHEDUSERPASSWORD,
NULL};
#endif
/* Attributes which are put into the entry extension */
struct attrs_in_extension attrs_in_extension[] =
{
{PSEUDO_ATTR_UNHASHEDUSERPASSWORD,
slapi_pw_get_entry_ext,
slapi_pw_set_entry_ext,
pw_copy_entry_ext,
pw_get_ext_size},
{NULL, NULL, NULL, NULL, NULL}
};
/* Structure used to store the virtual attribute cache in each entry
* If 'attr' is not NULL, the name of the attribute is taken from attr->a_type and so
* attrname is set to NULL.
* If 'attr' is NULL, the name of the attribute is stored in attrname
*/
struct _entry_vattr {
char *attrname; /* if NULL, the attribute name is the one in attr->a_type */
Slapi_Attr *attr; /* attribute computed by a SP */
struct _entry_vattr *next;
};
/*
* An attribute name is of the form 'basename[;option]'.
* The state informaion is encoded in options. For example:
*
* telephonenumber;vucsn-011111111222233334444: 1 650 937 5739
*
* This function strips out the csn options, leaving behind a
* type with any non-csn options left intact.
*/
/*
* WARNING: s gets butchered... the base type remains.
*/
void
str2entry_state_information_from_type(char *s,CSNSet **csnset,CSN **attributedeletioncsn,CSN **maxcsn,int *value_state,int *attr_state)
{
char *p= strchr(s, ';');
*value_state= VALUE_PRESENT;
*attr_state= ATTRIBUTE_PRESENT;
while(p!=NULL)
{
if(p[3]=='c' && p[4]=='s' && p[5]=='n' && p[6]=='-')
{
CSNType t= CSN_TYPE_UNKNOWN;
if(p[1]=='x' && p[2]=='1')
{
t= CSN_TYPE_UNKNOWN;
}
if(p[1]=='x' && p[2]=='2')
{
t= CSN_TYPE_NONE;
}
if(p[1]=='a' && p[2]=='d')
{
t= CSN_TYPE_ATTRIBUTE_DELETED;
}
if(p[1]=='v' && p[2]=='u')
{
t= CSN_TYPE_VALUE_UPDATED;
}
if(p[1]=='v' && p[2]=='d')
{
t= CSN_TYPE_VALUE_DELETED;
}
if(p[1]=='m' && p[2]=='d')
{
t= CSN_TYPE_VALUE_DISTINGUISHED;
}
p[0]='\0';
if(t!=CSN_TYPE_ATTRIBUTE_DELETED)
{
CSN csn;
csn_init_by_string(&csn,p+7);
csnset_add_csn(csnset,t,&csn);
if ( *maxcsn == NULL )
{
*maxcsn = csn_dup ( &csn );
}
else if ( csn_compare (*maxcsn, &csn) < 0 )
{
csn_init_by_csn ( *maxcsn, &csn );
}
}
else
{
*attributedeletioncsn= csn_new_by_string(p+7);
if ( *maxcsn == NULL )
{
*maxcsn = csn_dup ( *attributedeletioncsn );
}
else if ( csn_compare (*maxcsn, *attributedeletioncsn) < 0 )
{
csn_init_by_csn ( *maxcsn, *attributedeletioncsn );
}
}
}
else if(strncmp(p+1,"deletedattribute", 16)==0)
{
p[0]='\0';
*attr_state= ATTRIBUTE_DELETED;
}
else if(strncmp(p+1,"deleted", 7)==0)
{
p[0]='\0';
*value_state= VALUE_DELETED;
}
p= strchr(p+1, ';');
}
}
/* rawdn is not consumed. Caller needs to free it. */
static Slapi_Entry *
str2entry_fast( const char *rawdn, const Slapi_RDN *srdn, char *s, int flags, int read_stateinfo )
{
Slapi_Entry *e;
char *next, *ptype=NULL;
int nvals= 0;
int del_nvals= 0;
unsigned long attr_val_cnt = 0;
CSN *attributedeletioncsn= NULL; /* Moved to this level so that the JCM csn_free call below gets useful */
CSNSet *valuecsnset= NULL; /* Moved to this level so that the JCM csn_free call below gets useful */
CSN *maxcsn = NULL;
char *normdn = NULL;
Slapi_Attr **a = NULL;
#ifdef OBSOLETE_DN_SYNTAX_CHECK
int strict = 0;
/* Check if we should be performing strict validation. */
strict = config_get_dn_validate_strict();
#endif
/*
* In string format, an entry looks like either of these:
*
* dn: <dn>\n
* [<attr>:[:] <value>\n]
* [<tab><continuedvalue>\n]*
* ...
*
* rdn: <rdn>\n
* [<attr>:[:] <value>\n]
* [<tab><continuedvalue>\n]*
* ...
*
* If a double colon is used after a type, it means the
* following value is encoded as a base 64 string. This
* happens if the value contains a non-printing character
* or newline.
*
* In case an entry starts with rdn:, dn must be provided.
*/
LDAPDebug( LDAP_DEBUG_TRACE, "=> str2entry_fast\n", 0, 0, 0 );
e = slapi_entry_alloc();
slapi_entry_init(e,NULL,NULL);
/* dn|rdn + attributes */
next = s;
/* get the read lock of name2asi for performance purpose.
It reduces read locking by per-entry lock, instead of per-attribute.
*/
/* attr_syntax_read_lock();
* no longer needed since attr syntax is not initialized
*/
while ( (s = ldif_getline( &next )) != NULL &&
attr_val_cnt < ENTRY_MAX_ATTRIBUTE_VALUE_COUNT )
{
struct berval type = {0, NULL};
struct berval value = {0, NULL};
int freeval = 0;
int value_state= VALUE_NOTFOUND;
int attr_state= ATTRIBUTE_NOTFOUND;
if ( *s == '\n' || *s == '\0' ) {
break;
}
if ( slapi_ldif_parse_line( s, &type, &value, &freeval ) < 0 ) {
LDAPDebug( LDAP_DEBUG_TRACE,
"<= str2entry_fast NULL (parse_line)\n", 0, 0, 0 );
continue;
}
/*
* Extract the attribute and value CSNs from the attribute type.
*/
csn_free(&attributedeletioncsn); /* JCM - Do this more efficiently */
csnset_free(&valuecsnset);
value_state= VALUE_NOTFOUND;
attr_state= ATTRIBUTE_NOTFOUND;
str2entry_state_information_from_type(type.bv_val,&valuecsnset,&attributedeletioncsn,&maxcsn,&value_state,&attr_state);
if(!read_stateinfo)
{
/* We are not maintaining state information */
if(value_state==VALUE_DELETED || attr_state==ATTRIBUTE_DELETED)
{
/* ignore deleted values and attributes */
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&value.bv_val);
continue;
}
/* Ignore CSNs */
csn_free(&attributedeletioncsn);
csnset_free(&valuecsnset);
}
/*
* We cache some stuff as we go around the loop.
*/
if((ptype==NULL)||(PL_strcasecmp(type.bv_val,ptype) != 0))
{
slapi_ch_free_string(&ptype);
ptype=PL_strndup(type.bv_val, type.bv_len);
nvals = 0;
del_nvals = 0;
a = NULL;
}
if ( rawdn ) {
if ( NULL == slapi_entry_get_dn_const( e )) {
if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
normdn =
slapi_dn_normalize_original(slapi_ch_strdup(rawdn));
} else {
if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
normdn = slapi_ch_strdup(rawdn);
} else {
normdn = slapi_create_dn_string("%s", rawdn);
if (NULL == normdn) {
LDAPDebug1Arg(LDAP_DEBUG_TRACE,
"str2entry_fast: Invalid DN: %s\n", rawdn);
slapi_entry_free( e );
if (freeval) slapi_ch_free_string(&value.bv_val);
e = NULL;
goto done;
}
}
}
/* normdn is consumed in e */
slapi_entry_set_normdn(e, normdn);
}
if ( NULL == slapi_entry_get_rdn_const( e )) {
if (srdn) {
/* we can use the rdn generated in entryrdn_lookup_dn */
slapi_entry_set_srdn ( e, srdn );
}else if (normdn) {
/* normdn is just referred in slapi_entry_set_rdn. */
slapi_entry_set_rdn(e, normdn);
} else {
if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
normdn =
slapi_dn_normalize_original(slapi_ch_strdup(rawdn));
} else {
if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
normdn = slapi_ch_strdup(rawdn);
} else {
normdn = slapi_create_dn_string("%s", rawdn);
if (NULL == normdn) {
LDAPDebug1Arg(LDAP_DEBUG_TRACE,
"str2entry_fast: Invalid DN: %s\n", rawdn);
slapi_entry_free( e );
if (freeval)
slapi_ch_free_string(&value.bv_val);
e = NULL;
goto done;
}
}
}
/* normdn is just referred in slapi_entry_set_rdn. */
slapi_entry_set_rdn(e, normdn);
slapi_ch_free_string(&normdn);
}
}
rawdn = NULL; /* Set once in the loop.
This won't affect the caller's passed address. */
}
if ( type.bv_len == SLAPI_ATTR_DN_LENGTH && PL_strncasecmp( type.bv_val, SLAPI_ATTR_DN, type.bv_len ) == 0 ) {
if ( slapi_entry_get_dn_const(e)!=NULL ) {
char ebuf[ BUFSIZ ];
LDAPDebug( LDAP_DEBUG_TRACE,
"str2entry_fast: entry has multiple dns \"%s\" and "
"\"%s\" (second ignored)\n",
slapi_entry_get_dn_const(e),
escape_string( value.bv_val, ebuf ), 0 );
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&value.bv_val);
continue;
}
if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
normdn =
slapi_ch_strdup(slapi_dn_normalize_original(value.bv_val));
} else {
normdn = slapi_create_dn_string("%s", value.bv_val);
}
if (NULL == normdn) {
char ebuf[ BUFSIZ ];
LDAPDebug1Arg(LDAP_DEBUG_TRACE,
"str2entry_fast: Invalid DN: %s\n",
escape_string( value.bv_val, ebuf ));
slapi_entry_free( e );
if (freeval) slapi_ch_free_string(&value.bv_val);
e = NULL;
goto done;
}
/* normdn is consumed in e */
slapi_entry_set_normdn(e, normdn);
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&value.bv_val);
continue;
}
if ( type.bv_len == SLAPI_ATTR_RDN_LENGTH && PL_strncasecmp( type.bv_val, SLAPI_ATTR_RDN, type.bv_len ) == 0 ) {
if ( NULL == slapi_entry_get_rdn_const( e )) {
slapi_entry_set_rdn( e, value.bv_val );
}
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&value.bv_val);
continue;
}
/* If SLAPI_STR2ENTRY_NO_ENTRYDN is set, skip entrydn */
if ( (flags & SLAPI_STR2ENTRY_NO_ENTRYDN) &&
type.bv_len == SLAPI_ATTR_ENTRYDN_LENGTH && PL_strncasecmp( type.bv_val, SLAPI_ATTR_ENTRYDN, type.bv_len ) == 0 ) {
if (freeval) slapi_ch_free_string(&value.bv_val);
continue;
}
/* retrieve uniqueid */
if ( type.bv_len == SLAPI_ATTR_UNIQUEID_LENGTH && PL_strncasecmp (type.bv_val, SLAPI_ATTR_UNIQUEID, type.bv_len) == 0 ){
if (e->e_uniqueid != NULL){
LDAPDebug (LDAP_DEBUG_TRACE,
"str2entry_fast: entry has multiple uniqueids %s "
"and %s (second ignored)\n",
e->e_uniqueid, value.bv_val, 0);
}else{
/* name2asi will be locked in slapi_entry_set_uniqueid */
/* attr_syntax_unlock_read(); */
slapi_entry_set_uniqueid (e, PL_strndup(value.bv_val, value.bv_len));
/* attr_syntax_read_lock();*/
}
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&value.bv_val);
continue;
}
if (value_state == VALUE_PRESENT && type.bv_len >= SLAPI_ATTR_OBJECTCLASS_LENGTH
&& PL_strncasecmp(type.bv_val, SLAPI_ATTR_OBJECTCLASS, type.bv_len) == 0) {
if (value.bv_len >= SLAPI_ATTR_VALUE_SUBENTRY_LENGTH && PL_strncasecmp(value.bv_val,SLAPI_ATTR_VALUE_SUBENTRY,value.bv_len) == 0)
e->e_flags |= SLAPI_ENTRY_LDAPSUBENTRY;
if (value.bv_len >= SLAPI_ATTR_VALUE_TOMBSTONE_LENGTH && PL_strncasecmp(value.bv_val, SLAPI_ATTR_VALUE_TOMBSTONE,value.bv_len) == 0)
e->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
}
{
Slapi_Value *svalue = NULL;
if(a==NULL)
{
switch(attr_state)
{
case ATTRIBUTE_PRESENT:
if(attrlist_append_nosyntax_init(&e->e_attrs, type.bv_val, &a)==0 /* Found */)
{
LDAPDebug (LDAP_DEBUG_ANY, "str2entry_fast: Error. Non-contiguous attribute values for %s\n", type.bv_val, 0, 0);
PR_ASSERT(0);
continue;
}
break;
case ATTRIBUTE_DELETED:
if(attrlist_append_nosyntax_init(&e->e_deleted_attrs, type.bv_val, &a)==0 /* Found */)
{
LDAPDebug (LDAP_DEBUG_ANY, "str2entry_fast: Error. Non-contiguous deleted attribute values for %s\n", type.bv_val, 0, 0);
PR_ASSERT(0);
continue;
}
break;
case ATTRIBUTE_NOTFOUND:
LDAPDebug (LDAP_DEBUG_ANY, "str2entry_fast: Error. Non-contiguous deleted attribute values for %s\n", type.bv_val, 0, 0);
PR_ASSERT(0);
continue;
/* break; ??? */
}
}
/* moved the value setting code here to check Slapi_Attr 'a'
* to retrieve the attribute syntax info */
svalue = value_new(NULL, CSN_TYPE_NONE, NULL);
#ifdef OBSOLETE_DN_SYNTAX_CHECK
if (slapi_attr_is_dn_syntax_attr(*a)) {
int rc = 0;
char *dn_aval = NULL;
if (strict) {
/* check that the dn is formatted correctly */
rc = slapi_dn_syntax_check(NULL, value.bv_val, 1);
if (rc) { /* syntax check failed */
LDAPDebug2Args(LDAP_DEBUG_TRACE,
"str2entry_fast: strict: Invalid DN value: %s: %s\n",
type.bv_val, value.bv_val);
slapi_entry_free( e );
if (freeval) slapi_ch_free_string(&value.bv_val);
e = NULL;
goto done;
}
}
if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
dn_aval = slapi_dn_normalize_original(value.bv_val);
slapi_value_set(svalue, dn_aval, strlen(dn_aval));
} else {
Slapi_DN *sdn = slapi_sdn_new_dn_byref(value.bv_val);
/* Note: slapi_sdn_get_dn returns normalized DN with
* case-intact. Thus, the length of dn_aval is
* slapi_sdn_get_ndn_len(sdn). */
dn_aval = (char *)slapi_sdn_get_dn(sdn);
slapi_value_set(svalue, (void *)dn_aval,
slapi_sdn_get_ndn_len(sdn));
slapi_sdn_free(&sdn);
}
} else {
slapi_value_set_berval(svalue, &value);
}
#endif
slapi_value_set_berval(svalue, &value);
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&value.bv_val);
svalue->v_csnset = valuecsnset;
valuecsnset = NULL;
{
const CSN *distinguishedcsn= csnset_get_csn_of_type(svalue->v_csnset,CSN_TYPE_VALUE_DISTINGUISHED);
if(distinguishedcsn!=NULL)
{
entry_add_dncsn_ext(e,distinguishedcsn, ENTRY_DNCSN_INCREASING);
}
}
if(value_state==VALUE_DELETED)
{
/* consumes the value */
slapi_valueset_add_attr_value_ext(
*a,
&(*a)->a_deleted_values,
svalue,
SLAPI_VALUE_FLAG_PASSIN );
del_nvals++;
}
else
{
/* consumes the value */
slapi_valueset_add_attr_value_ext(
*a,
&(*a)->a_present_values,
svalue,
SLAPI_VALUE_FLAG_PASSIN);
nvals++;
}
if(attributedeletioncsn!=NULL)
{
attr_set_deletion_csn(*a,attributedeletioncsn);
}
}
csn_free(&attributedeletioncsn);
csnset_free(&valuecsnset);
attr_val_cnt++;
}
slapi_ch_free_string(&ptype);
if ( attr_val_cnt >= ENTRY_MAX_ATTRIBUTE_VALUE_COUNT )
{
LDAPDebug( LDAP_DEBUG_ANY,
"str2entry_fast: entry %s exceeded max attribute value cound %ld\n",
slapi_entry_get_dn_const(e)?slapi_entry_get_dn_const(e):"unknown",
attr_val_cnt, 0 );
}
if (read_stateinfo && maxcsn)
{
e->e_maxcsn = maxcsn;
maxcsn = NULL;
}
/* release read lock of name2asi, per-entry lock */
/* attr_syntax_unlock_read();
* no longer locked since attr syntax is not initialized
*/
/* If this is a tombstone, it requires a special treatment for rdn. */
if (e->e_flags & SLAPI_ENTRY_FLAG_TOMBSTONE) {
/* tombstone */
if (_entry_set_tombstone_rdn(e, slapi_entry_get_dn_const(e))) {
LDAPDebug1Arg( LDAP_DEBUG_TRACE, "str2entry_fast: "
"tombstone entry has badly formatted dn: %s\n",
slapi_entry_get_dn_const(e) );
slapi_entry_free( e ); e = NULL;
goto done;
}
}
/* check to make sure there was a dn: line */
if ( slapi_entry_get_dn_const(e)==NULL ) {
if (!(SLAPI_STR2ENTRY_INCLUDE_VERSION_STR & flags))
LDAPDebug( LDAP_DEBUG_ANY, "str2entry_fast: entry has no dn\n",
0, 0, 0 );
slapi_entry_free( e );
e = NULL;
}
done:
csnset_free(&valuecsnset);
csn_free(&attributedeletioncsn);
csn_free(&maxcsn);
LDAPDebug( LDAP_DEBUG_TRACE, "<= str2entry_fast 0x%x\n",
e, 0, 0 );
return( e );
}
#define STR2ENTRY_SMALL_BUFFER_SIZE 64
#define STR2ENTRY_INITIAL_BERVAL_ARRAY_SIZE 8
#define STR2ENTRY_VALUE_DUPCHECK_THRESHOLD 5
typedef struct _entry_attr_data {
int ead_attrarrayindex;
const char *ead_attrtypename;
char ead_allocated; /* non-zero if this struct needs to be freed */
} entry_attr_data;
/* Structure which stores a tree for the attributes on the entry rather than the linked list on a regular entry struture */
typedef struct _entry_attrs {
Avlnode *ea_attrlist;
int ea_attrdatacount;
entry_attr_data ea_attrdata[ STR2ENTRY_SMALL_BUFFER_SIZE ];
} entry_attrs;
typedef struct _str2entry_attr {
char *sa_type;
int sa_state;
struct slapi_value_set sa_present_values;
struct slapi_value_set sa_deleted_values;
int sa_numdups;
value_compare_fn_type sa_comparefn;
Avlnode *sa_vtree;
CSN *sa_attributedeletioncsn;
Slapi_Attr sa_attr;
} str2entry_attr;
static void
entry_attr_init(str2entry_attr *sa, const char *type, int state)
{
sa->sa_type= slapi_ch_strdup(type);
sa->sa_state= state;
slapi_valueset_init(&sa->sa_present_values);
slapi_valueset_init(&sa->sa_deleted_values);
sa->sa_numdups= 0;
sa->sa_comparefn = NULL;
sa->sa_vtree= NULL;
sa->sa_attributedeletioncsn= NULL;
slapi_attr_init(&sa->sa_attr, type);
}
/*
* Create a tree of attributes.
*/
static int
entry_attrs_new(entry_attrs **pea)
{
entry_attrs *tmp = (entry_attrs *)slapi_ch_calloc(1, sizeof(entry_attrs));
if (NULL == tmp) {
return -1;
} else {
*pea = tmp;
return 0;
}
}
/*
* Delete an attribute type tree node.
*/
static void
attr_type_node_free( caddr_t data )
{
entry_attr_data *ea = (entry_attr_data *)data;
if ( NULL != ea && ea->ead_allocated ) {
slapi_ch_free( (void **)&ea );
}
}
/*
* Delete a tree of attributes.
*/
static void
entry_attrs_delete(entry_attrs **pea)
{
if (NULL != *pea) {
/* Delete the AVL tree */
avl_free((*pea)->ea_attrlist, attr_type_node_free);
slapi_ch_free((void**)pea);
}
}
static int
attr_type_node_cmp( caddr_t d1, caddr_t d2 )
{
/*
* A simple strcasecmp() will do here because we do not care
* about subtypes, etc. The slapi_str2entry() function treats
* subtypes as distinct attribute types, because that is how
* they are stored within the Slapi_Entry structure.
*/
entry_attr_data *ea1= (entry_attr_data *)d1;
entry_attr_data *ea2= (entry_attr_data *)d2;
PR_ASSERT( ea1 != NULL );
PR_ASSERT( ea1->ead_attrtypename != NULL );
PR_ASSERT( ea2 != NULL );
PR_ASSERT( ea2->ead_attrtypename != NULL );
return strcasecmp(ea1->ead_attrtypename,ea2->ead_attrtypename);
}
/*
* Adds a new attribute to the attribute tree.
*/
static void
entry_attrs_add(entry_attrs *ea, const char *atname, int atarrayindex)
{
entry_attr_data *ead;
if ( ea->ea_attrdatacount < STR2ENTRY_SMALL_BUFFER_SIZE ) {
ead = &(ea->ea_attrdata[ ea->ea_attrdatacount ]);
ead->ead_allocated = 0;
} else {
ead = (entry_attr_data *)slapi_ch_malloc( sizeof( entry_attr_data ));
ead->ead_allocated = 1;
}
++ea->ea_attrdatacount;
ead->ead_attrarrayindex = atarrayindex;
ead->ead_attrtypename = atname; /* a reference, not a strdup! */
avl_insert( &(ea->ea_attrlist), ead, attr_type_node_cmp, avl_dup_error );
}
/*
* Checks for an attribute in the tree. Returns the attr array index or -1
* if not found;
*/
static int
entry_attrs_find(entry_attrs *ea,char *type)
{
entry_attr_data tmpead = {0};
entry_attr_data *foundead;
tmpead.ead_attrtypename = type;
foundead = (entry_attr_data *)avl_find( ea->ea_attrlist, &tmpead,
attr_type_node_cmp );
return ( NULL != foundead ) ? foundead->ead_attrarrayindex : -1;
}
/* What's going on here then ?
Well, originally duplicate value checking was done by taking each
new value and comparing in turn against all the previous values.
Needless to say this was costly when there were many values.
So, new code was written which built a binary tree of index keys
for the values, and the test was done against the tree.
Nothing wrong with this, it speeded up the case where there were
many values nicely.
Unfortunately, when there are few values, it proved to be a significent
performance sink.
So, now we check the old way up 'till there's 5 attribute values, then
switch to the tree-based scheme.
Note that duplicate values are only checked for and ignored
if flags contains SLAPI_STR2ENTRY_REMOVEDUPVALS.
*/
/* dn is not consumed. Caller needs to free it. */
static Slapi_Entry *
str2entry_dupcheck( const char *rawdn, char *s, int flags, int read_stateinfo )
{
Slapi_Entry *e;
str2entry_attr stack_attrs[STR2ENTRY_SMALL_BUFFER_SIZE];
str2entry_attr *dyn_attrs = NULL;
str2entry_attr *attrs = stack_attrs;
str2entry_attr *prev_attr= NULL;
int nattrs;
int maxattrs = STR2ENTRY_SMALL_BUFFER_SIZE;
char *type;
struct berval bvtype;
str2entry_attr *sa;
int i;
char *next=NULL;
char *valuecharptr=NULL;
struct berval bvvalue;
int rc;
entry_attrs *ea = NULL;
int tree_attr_checking = 0;
int big_entry_attr_presence_check = 0;
int check_for_duplicate_values =
( 0 != ( flags & SLAPI_STR2ENTRY_REMOVEDUPVALS ));
Slapi_Value *value = 0;
CSN *attributedeletioncsn= NULL;
CSNSet *valuecsnset= NULL;
CSN *maxcsn= NULL;
char *normdn = NULL;
int strict = 0;
/* Check if we should be performing strict validation. */
strict = config_get_dn_validate_strict();
LDAPDebug( LDAP_DEBUG_TRACE, "=> str2entry_dupcheck\n", 0, 0, 0 );
e = slapi_entry_alloc();
slapi_entry_init(e,NULL,NULL);
next = s;
nattrs = 0;
if (flags & SLAPI_STR2ENTRY_BIGENTRY)
{
big_entry_attr_presence_check = 1;
}
while ( (s = ldif_getline( &next )) != NULL )
{
int value_state= VALUE_NOTFOUND;
int attr_state= VALUE_NOTFOUND;
int freeval = 0;
struct berval bv_null = {0, NULL};
csn_free(&attributedeletioncsn);
if ( *s == '\n' || *s == '\0' ) {
break;
}
bvtype = bv_null;
bvvalue = bv_null;
if ( slapi_ldif_parse_line( s, &bvtype, &bvvalue, &freeval ) < 0 ) {
LDAPDebug(LDAP_DEBUG_ANY,
"Warning: Entry (%s), ignoring invalid line \"%s\"...\n",
rawdn ? rawdn : "", s, 0);
continue;
}
type = bvtype.bv_val;
valuecharptr = bvvalue.bv_val;
/*
* Extract the attribute and value CSNs from the attribute type.
*/
csnset_free(&valuecsnset);
value_state= VALUE_NOTFOUND;
attr_state= VALUE_NOTFOUND;
str2entry_state_information_from_type(type,&valuecsnset,&attributedeletioncsn,&maxcsn,&value_state,&attr_state);
if(!read_stateinfo)
{
/* We are not maintaining state information */
if(value_state==VALUE_DELETED || attr_state==ATTRIBUTE_DELETED)
{
/* ignore deleted values and attributes */
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
continue;
}
/* Ignore CSNs */
csn_free(&attributedeletioncsn);
csnset_free(&valuecsnset);
}
if ( rawdn ) {
if ( NULL == slapi_entry_get_dn_const(e) ) {
if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
normdn = slapi_ch_strdup(rawdn);
} else {
normdn = slapi_create_dn_string("%s", rawdn);
if (NULL == normdn) {
LDAPDebug1Arg(LDAP_DEBUG_TRACE,
"str2entry_dupcheck: Invalid DN: %s\n", rawdn);
slapi_entry_free( e );
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
csnset_free(&valuecsnset);
csn_free(&attributedeletioncsn);
csn_free(&maxcsn);
return NULL;
}
}
/* normdn is consumed in e */
slapi_entry_set_normdn(e, normdn);
}
if ( NULL == slapi_entry_get_rdn_const(e) ) {
if (normdn) {
/* normdn is just referred in slapi_entry_set_rdn. */
slapi_entry_set_rdn(e, normdn);
} else {
if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
normdn = slapi_ch_strdup(rawdn);
} else {
normdn = slapi_create_dn_string("%s", rawdn);
if (NULL == normdn) {
LDAPDebug1Arg(LDAP_DEBUG_TRACE,
"str2entry_dupcheck: Invalid DN: %s\n", rawdn);
slapi_entry_free( e );
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
csnset_free(&valuecsnset);
csn_free(&attributedeletioncsn);
csn_free(&maxcsn);
return NULL;
}
}
/* normdn is just referred in slapi_entry_set_rdn. */
slapi_entry_set_rdn(e, normdn);
slapi_ch_free_string(&normdn);
}
}
rawdn = NULL; /* Set once in the loop.
This won't affect the caller's passed address. */
}
if ( strcasecmp( type, "dn" ) == 0 ) {
if ( slapi_entry_get_dn_const(e)!=NULL ) {
char ebuf[ BUFSIZ ];
LDAPDebug( LDAP_DEBUG_TRACE,
"str2entry_dupcheck: entry has multiple dns \"%s\" "
"and \"%s\" (second ignored)\n",
slapi_entry_get_dn_const(e),
escape_string( valuecharptr, ebuf ), 0 );
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
continue;
}
normdn = slapi_create_dn_string("%s", valuecharptr);
if (NULL == normdn) {
LDAPDebug1Arg(LDAP_DEBUG_TRACE,
"str2entry_dupcheck: Invalid DN: %s\n", valuecharptr);
slapi_entry_free( e ); e = NULL;
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
goto free_and_return;
}
/* normdn is consumed in e */
slapi_entry_set_normdn(e, normdn);
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
continue;
}
if ( strcasecmp( type, "rdn" ) == 0 ) {
if ( NULL == slapi_entry_get_rdn_const( e )) {
slapi_entry_set_rdn( e, valuecharptr );
}
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
continue;
}
/* If SLAPI_STR2ENTRY_NO_ENTRYDN is set, skip entrydn */
if ( (flags & SLAPI_STR2ENTRY_NO_ENTRYDN) &&
strcasecmp( type, "entrydn" ) == 0 ) {
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
continue;
}
/* retrieve uniqueid */
if ( strcasecmp (type, SLAPI_ATTR_UNIQUEID) == 0 ){
if (e->e_uniqueid != NULL){
LDAPDebug (LDAP_DEBUG_TRACE,
"str2entry_dupcheck: entry has multiple uniqueids %s "
"and %s (second ignored)\n",
e->e_uniqueid, valuecharptr, 0);
}else{
slapi_entry_set_uniqueid (e, slapi_ch_strdup(valuecharptr));
}
/* the memory below was not allocated by the slapi_ch_ functions */
if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
continue;
}
if (strcasecmp(type,"objectclass") == 0) {
if (strcasecmp(valuecharptr,"ldapsubentry") == 0)
e->e_flags |= SLAPI_ENTRY_LDAPSUBENTRY;
if (strcasecmp(valuecharptr, SLAPI_ATTR_VALUE_TOMBSTONE) == 0)
e->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
}
/* Here we have a quick look to see if this attribute is a new
value for the type we last processed or a new type.
If not, we look to see if we've seen this attribute type before.
*/
if ( prev_attr!=NULL && strcasecmp( type, prev_attr->sa_type ) != 0 )
{
/* Different attribute type - find it, or alloc new */
prev_attr = NULL;
/* The linear check below can take a while, so we change to use a tree if there are many attrs */
if (!big_entry_attr_presence_check)
{
for ( i = 0; i < nattrs; i++ )
{
if (strcasecmp( type, attrs[i].sa_type ) == 0 )
{
prev_attr = &attrs[i];
break;
}
}
}