-
Notifications
You must be signed in to change notification settings - Fork 80
/
dn.c
3174 lines (2940 loc) · 91.8 KB
/
dn.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
/* dn.c - routines for dealing with distinguished names */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/time.h>
#include <sys/socket.h>
#endif
#include "slap.h"
#include <plhash.h>
#undef SDN_DEBUG
static void add_rdn_av( char *avstart, char *avend, int *rdn_av_countp,
struct berval **rdn_avsp, struct berval *avstack );
static void reset_rdn_avs( struct berval **rdn_avsp, int *rdn_av_countp );
static void sort_rdn_avs( struct berval *avs, int count, int escape );
static int rdn_av_cmp( struct berval *av1, struct berval *av2 );
static void rdn_av_swap( struct berval *av1, struct berval *av2, int escape );
/* normalized dn cache related definitions*/
struct
ndn_cache_lru
{
struct ndn_cache_lru *prev;
struct ndn_cache_lru *next;
char *key;
};
struct
ndn_cache_ctx
{
struct ndn_cache_lru *head;
struct ndn_cache_lru *tail;
Slapi_Counter *cache_hits;
Slapi_Counter *cache_tries;
Slapi_Counter *cache_misses;
size_t cache_size;
size_t cache_max_size;
long cache_count;
};
struct
ndn_hash_val
{
char *ndn;
size_t len;
int size;
struct ndn_cache_lru *lru_node; /* used to speed up lru shuffling */
};
#define NDN_FLUSH_COUNT 10000 /* number of DN's to remove when cache fills up */
#define NDN_MIN_COUNT 1000 /* the minimum number of DN's to keep in the cache */
#define NDN_CACHE_BUCKETS 2053 /* prime number */
static PLHashNumber ndn_hash_string(const void *key);
static int ndn_cache_lookup(char *dn, size_t dn_len, char **result, char **udn, int *rc);
static void ndn_cache_update_lru(struct ndn_cache_lru **node);
static void ndn_cache_add(char *dn, size_t dn_len, char *ndn, size_t ndn_len);
static void ndn_cache_delete(char *dn);
static void ndn_cache_flush();
static void ndn_cache_free();
static int ndn_started = 0;
static PRLock *lru_lock = NULL;
static Slapi_RWLock *ndn_cache_lock = NULL;
static struct ndn_cache_ctx *ndn_cache = NULL;
static PLHashTable *ndn_cache_hashtable = NULL;
#define ISBLANK(c) ((c) == ' ')
#define ISBLANKSTR(s) (((*(s)) == '2') && (*((s)+1) == '0'))
#define ISSPACE(c) (ISBLANK(c) || ((c) == '\n') || ((c) == '\r')) /* XXX 518524 */
#define ISEQUAL(c) ((c) == '=')
#define ISEQUALSTR(s) \
((*(s) == '3') && ((*((s)+1) == 'd') || (*((s)+1) == 'D')))
#define ISPLUS(c) ((c) == '+')
#define ISPLUSSTR(s) \
((*(s) == '2') && ((*((s)+1) == 'b') || (*((s)+1) == 'B')))
#define ISESCAPE(c) ((c) == '\\')
#define ISQUOTE(c) ((c) == '"')
#define DNSEPARATOR(c) (((c) == ',') || ((c) == ';'))
#define DNSEPARATORSTR(s) \
(((*(s) == '2') && ((*((s)+1) == 'c') || (*((s)+1) == 'C'))) || \
((*(s) == '3') && ((*((s)+1) == 'b') || (*((s)+1) == 'B'))))
#define SEPARATOR(c) (DNSEPARATOR(c) || ISPLUS(c))
#define SEPARATORSTR(s) (DNSEPARATORSTR(s) || ISPLUSSTR(s))
#define NEEDSESCAPE(c) (ISESCAPE(c) || ISQUOTE(c) || SEPARATOR(c) || \
((c) == '<') || ((c) == '>') || ISEQUAL(c))
#define NEEDSESCAPESTR(s) \
(((*(s) == '2') && ((*((s)+1) == '2') || \
(*((s)+1) == 'b') || (*((s)+1) == 'B') || \
(*((s)+1) == 'c') || (*((s)+1) == 'C'))) || \
((*(s) == '3') && (((*((s)+1) >= 'b') && (*((s)+1) < 'f')) || \
((*((s)+1) >= 'B') && (*((s)+1) < 'F')))) || \
((*(s) == '5') && ((*((s)+1) == 'c') || (*((s)+1) == 'C'))))
#define LEADNEEDSESCAPE(c) (ISBLANK(c) || ((c) == '#') || NEEDSESCAPE(c))
#define LEADNEEDSESCAPESTR(s) (NEEDSESCAPESTR(s) || \
((*(s) == '2') && (*((s)+1) == '3')))
#define ISCLOSEBRACKET(c) (((c) == ')') || ((c) == ']'))
#define MAYBEDN(eq) ( \
(eq) && ((eq) != subtypestart) && \
((eq) != subtypestart + strlen(subtypestart) - 3) \
)
#define B4TYPE 0
#define INTYPE 1
#define B4EQUAL 2
#define B4VALUE 3
#define INVALUE 4
#define INQUOTEDVALUE 5
#define B4SEPARATOR 6
#define INVALUE1ST 7
#define INQUOTEDVALUE1ST 8
#define SLAPI_DNNORM_INITIAL_RDN_AVS 10
#define SLAPI_DNNORM_SMALL_RDN_AV 512
/*
* substr_dn_normalize - map a DN to a canonical form.
* The DN is read from *dn through *(end-1) and normalized in place.
* The new end is returned; that is, the canonical form is in
* *dn through *(the_return_value-1).
*/
/* The goals of this function are:
* 1. be compatible with previous implementations. Especially, enable
* a server running this code to find database index keys that were
* computed by Directory Server 3.0 with a prior version of this code.
* 2. Normalize in place; that is, avoid allocating memory to contain
* the canonical form.
* 3. eliminate insignificant differences; that is, any two DNs are
* not significantly different if and only if their canonical forms
* are identical (ignoring upper/lower case).
* 4. handle a DN in the syntax defined by RFC 2253.
* 5. handle a DN in the syntax defined by RFC 1779.
*
* Goals 3 through 5 are not entirely achieved by this implementation,
* because it can't be done without violating goal 1. Specifically,
* DNs like cn="a,b" and cn=a\,b are not mapped to the same canonical form,
* although they're not significantly different. Likewise for any pair
* of DNs that differ only in their choice of quoting convention.
* A previous version of this code changed all DNs to the most compact
* quoting convention, but that violated goal 1, since Directory Server
* 3.0 did not.
*
* Also, this implementation handles the \xx convention of RFC 2253 and
* consequently violates RFC 1779, according to which this type of quoting
* would be interpreted as a sequence of 2 numerals (not a single byte).
*
* Finally, if the DN contains any RDNs that are multivalued, we sort
* the values in the RDN(s) to help meet goal 3. Ordering is based on a
* case-insensitive comparison of the "attribute=value" pairs.
*
* This function does not support UTF-8 multi-byte encoding for attribute
* values, in particular it does not support UTF-8 whitespace. First the
* ISSPACE macro above is limited, but also its frequent use of '-1' indexing
* into a char[] may hit the middle of a multi-byte UTF-8 whitespace character
* encoding (518524).
*/
char *
substr_dn_normalize_orig( char *dn, char *end )
{
/* \xx is changed to \c.
* \c is changed to c, unless this would change its meaning.
* All values that contain 2 or more separators are "enquoted";
* all other values are not enquoted.
*/
char *value = NULL;
char *value_separator = NULL;
char *d = NULL;
char *s = NULL;
char *typestart = NULL;
char *lastesc = NULL;
int gotesc = 0;
int state = B4TYPE;
int rdn_av_count = 0;
struct berval *rdn_avs = NULL;
struct berval initial_rdn_av_stack[ SLAPI_DNNORM_INITIAL_RDN_AVS ];
for ( d = s = dn; s != end; s++ ) {
switch ( state ) {
case B4TYPE:
if ( ! ISSPACE( *s ) ) {
state = INTYPE;
typestart = d;
*d++ = *s;
}
break;
case INTYPE:
if ( *s == '=' ) {
state = B4VALUE;
*d++ = *s;
} else if ( ISSPACE( *s ) ) {
state = B4EQUAL;
} else {
*d++ = *s;
}
break;
case B4EQUAL:
if ( *s == '=' ) {
state = B4VALUE;
*d++ = *s;
} else if ( ! ISSPACE( *s ) ) {
/* not a valid dn - but what can we do here? */
*d++ = *s;
}
break;
case B4VALUE:
if ( *s == '"' || ! ISSPACE( *s ) ) {
value_separator = NULL;
value = d;
state = ( *s == '"' ) ? INQUOTEDVALUE : INVALUE1ST;
lastesc = NULL;
*d++ = *s;
}
break;
case INVALUE1ST:
case INVALUE:
if ( gotesc ) {
if ( SEPARATOR( *s ) ) {
value_separator = d;
}
if ( INVALUE1ST == state ) {
if ( !LEADNEEDSESCAPE( *s )) {
/* checking the leading char + special chars */
--d; /* eliminate the \ */
}
} else if ( !NEEDSESCAPE( *s ) ) {
--d; /* eliminate the \ */
lastesc = d;
}
} else if ( SEPARATOR( *s ) ) {
/* handling a trailing escaped space */
/* assuming a space is the only an extra character which
* is not escaped if it appears in the middle, but should
* be if it does at the end of the RDN value */
/* e.g., ou=ABC \ ,o=XYZ --> ou=ABC \ ,o=XYZ */
if ( lastesc ) {
while ( ISSPACE( *(d - 1) ) && d > lastesc ) {
d--;
}
if ( d == lastesc ) {
*d++ = '\\';
*d++ = ' '; /* escaped trailing space */
}
} else {
while ( ISSPACE( *(d - 1) ) ) {
d--;
}
}
if ( value_separator == dn ) { /* 2 or more separators */
/* convert to quoted value: */
char *L = NULL; /* char after last seperator */
char *R; /* value character iterator */
int escape_skips = 0; /* number of escapes we have seen after the first */
for ( R = value; (R = strchr( R, '\\' )) && (R < d); L = ++R ) {
if ( SEPARATOR( R[1] )) {
if ( L == NULL ) {
/* executes once, at first escape, adds opening quote */
const size_t len = R - value;
/* make room for quote by covering escape */
if ( len > 0 ) {
memmove( value+1, value, len );
}
*value = '"'; /* opening quote */
value = R + 1; /* move passed what has been parsed */
} else {
const size_t len = R - L;
if ( len > 0 ) {
/* remove the seperator */
memmove( value, L, len );
value += len; /* move passed what has been parsed */
}
--d;
++escape_skips;
}
} /* if ( SEPARATOR( R[1] )) */
} /* for */
memmove( value, L, d - L + escape_skips );
*d++ = '"'; /* closing quote */
} /* if (value_separator == dn) */
state = B4TYPE;
/*
* Track and sort attribute values within
* multivalued RDNs.
*/
if ( *s == '+' || rdn_av_count > 0 ) {
add_rdn_av( typestart, d, &rdn_av_count,
&rdn_avs, initial_rdn_av_stack );
}
if ( *s != '+' ) { /* at end of this RDN */
if ( rdn_av_count > 1 ) {
sort_rdn_avs( rdn_avs, rdn_av_count, 0 );
}
if ( rdn_av_count > 0 ) {
reset_rdn_avs( &rdn_avs, &rdn_av_count );
}
}
*d++ = (*s == '+') ? '+' : ',';
break;
} /* else if ( SEPARATOR( *s ) ) */
if ( INVALUE1ST == state ) {
state = INVALUE;
}
*d++ = *s;
break;
case INQUOTEDVALUE:
if ( gotesc ) {
if ( ! NEEDSESCAPE( *s ) ) {
--d; /* eliminate the \ */
}
} else if ( *s == '"' ) {
state = B4SEPARATOR;
if (!value) {
LDAPDebug( LDAP_DEBUG_ANY,
"slapi_dn_normalize - missing value\n", 0, 0, 0 );
break;
}
if ( value_separator == dn /* 2 or more separators */
|| ISSPACE( value[1] ) || ISSPACE( d[-1] ) ) {
*d++ = *s;
} else {
/* convert to non-quoted value: */
if ( value_separator == NULL ) { /* no separators */
memmove ( value, value+1, (d-value)-1 );
--d;
} else { /* 1 separator */
memmove ( value, value+1, (value_separator-value)-1 );
*(value_separator - 1) = '\\';
}
}
break;
}
if ( SEPARATOR( *s )) {
if ( value_separator ) value_separator = dn;
else value_separator = d;
}
*d++ = *s;
break;
case B4SEPARATOR:
if ( SEPARATOR( *s ) ) {
state = B4TYPE;
/*
* Track and sort attribute values within
* multivalued RDNs.
*/
if ( *s == '+' || rdn_av_count > 0 ) {
add_rdn_av( typestart, d, &rdn_av_count,
&rdn_avs, initial_rdn_av_stack );
}
if ( *s != '+' ) { /* at end of this RDN */
if ( rdn_av_count > 1 ) {
sort_rdn_avs( rdn_avs, rdn_av_count, 0 );
}
if ( rdn_av_count > 0 ) {
reset_rdn_avs( &rdn_avs, &rdn_av_count );
}
}
*d++ = (*s == '+') ? '+' : ',';
}
break;
default:
LDAPDebug( LDAP_DEBUG_ANY,
"slapi_dn_normalize - unknown state %d\n", state, 0, 0 );
break;
}
if ( *s == '\\' ) {
if ( gotesc ) { /* '\\', again */
/* <type>=XXX\\\\7AYYY; we should keep \\\\. */
gotesc = 0;
} else {
gotesc = 1;
if ( s+2 < end ) {
int n = slapi_hexchar2int( s[1] );
if ( n >= 0 && n < 16 ) {
int n2 = slapi_hexchar2int( s[2] );
if ( n2 >= 0 ) {
n = (n << 4) + n2;
if (n == 0) { /* don't change \00 */
*d++ = *++s;
*d++ = *++s;
gotesc = 0;
} else { /* change \xx to a single char */
++s;
*(unsigned char*)(s+1) = n;
}
}
}
}
}
} else {
gotesc = 0;
}
}
/*
* Track and sort attribute values within multivalued RDNs.
*/
/* We may still be in an unexpected state, such as B4TYPE if
* we encountered something odd like a '+' at the end of the
* rdn. If this is the case, we don't want to add this bogus
* rdn to our list to sort. We should only be in the INVALUE
* or B4SEPARATOR state if we have a valid rdn component to
* be added. */
if ((rdn_av_count > 0) && ((state == INVALUE1ST) ||
(state == INVALUE) || (state == B4SEPARATOR))) {
add_rdn_av( typestart, d, &rdn_av_count,
&rdn_avs, initial_rdn_av_stack );
}
if ( rdn_av_count > 1 ) {
sort_rdn_avs( rdn_avs, rdn_av_count, 0 );
}
if ( rdn_av_count > 0 ) {
reset_rdn_avs( &rdn_avs, &rdn_av_count );
}
/* Trim trailing spaces */
while ( d != dn && *(d - 1) == ' ' ) d--; /* XXX 518524 */
return( d );
}
char *
substr_dn_normalize( char *dn, char *end )
{
/* no op */
return end;
}
static int
ISEOV(char *s, char *ends)
{
char *p;
for (p = s; p && *p && p < ends; p++) {
if (SEPARATOR(*p)) {
return 1;
} else if (!ISBLANK(*p)) {
return 0; /* not the end of the value */
}
}
return 1;
}
/*
* 1) Escaped NEEDSESCAPE chars (e.g., ',', '<', '=', etc.) are converted to
* ESC HEX HEX (e.g., \2C, \3C, \3D, etc.)
* Input could be a string in double quotes
* (= old DN format: dn: cn="x=x,y=y",... --> dn: cn=x\3Dx\2Cy\3Dy,...) or
* an escaped string
* (= new DN format dn: cn=x\=x\,y\=y,... -> dn: cn=x\3Dx\2Cy\3Dy,...)
*
* 2) All the other ESC HEX HEX are converted to the real characters.
*
* 3) Spaces around separator ',', ';', and '+' are removed.
*
* Input:
* src: src DN
* src_len: length of src; 0 is acceptable if src is NULL terminated.
* Output:
* dest: address of the converted string; NULL terminated
* (could store src address if no need to convert)
* dest_len: length of dest
*
* Return values:
* 0: nothing was done; dest is identical to src (src is passed in).
* 1: successfully escaped; dest is different from src. src needs to be freed.
* -1: failed; dest is NULL; invalid DN
*/
int
slapi_dn_normalize_ext(char *src, size_t src_len, char **dest, size_t *dest_len)
{
int rc = -1;
int state = B4TYPE;
char *s = NULL; /* work pointer for src */
char *d = NULL; /* work pointer for dest */
char *ends = NULL;
char *endd = NULL;
char *lastesc = NULL;
char *udn = NULL;
/* rdn avs for the main DN */
char *typestart = NULL;
int rdn_av_count = 0;
struct berval *rdn_avs = NULL;
struct berval initial_rdn_av_stack[ SLAPI_DNNORM_INITIAL_RDN_AVS ];
/* rdn avs for the nested DN */
char *subtypestart = NULL; /* used for nested rdn avs */
int subrdn_av_count = 0;
struct berval *subrdn_avs = NULL;
struct berval subinitial_rdn_av_stack[ SLAPI_DNNORM_INITIAL_RDN_AVS ];
int chkblank = 0;
int is_dn_syntax = 0;
if ((NULL == dest) || (NULL == dest_len)) {
goto bail;
}
if (NULL == src) {
*dest = NULL;
*dest_len = 0;
goto bail;
}
if (0 == src_len) {
src_len = strlen(src);
}
/*
* Check the normalized dn cache
*/
if(ndn_cache_lookup(src, src_len, dest, &udn, &rc)){
*dest_len = strlen(*dest);
return rc;
}
s = PL_strnchr(src, '\\', src_len);
if (s) {
*dest_len = src_len * 3;
*dest = slapi_ch_malloc(*dest_len); /* max length */
rc = 1;
} else {
s = PL_strnchr(src, '"', src_len);
if (s) {
*dest_len = src_len * 3;
*dest = slapi_ch_malloc(*dest_len); /* max length */
rc = 1;
} else {
*dest_len = src_len;
*dest = src; /* just removing spaces around separators */
rc = 0;
}
}
if (0 == src_len) { /* src == "" */
goto bail; /* need to bail after setting up *dest and rc */
}
ends = src + src_len;
endd = *dest + *dest_len;
for (s = src, d = *dest; s < ends && d < endd; ) {
switch (state) {
case B4TYPE: /* before type; cn=... */
/* ^ */
if (ISSPACE(*s)) {
s++; /* skip leading spaces */
} else {
state = INTYPE;
typestart = d;
*d++ = *s++;
}
break;
case INTYPE: /* in type; cn=... */
/* ^ */
if (ISEQUAL(*s)) {
/* See if the type is defined to use
* the Distinguished Name syntax. */
char savechar;
/* We need typestart to be a string containing only
* the type. We terminate the type and then reset
* the string after we check the syntax. */
savechar = *d;
*d = '\0';
is_dn_syntax = slapi_attr_is_dn_syntax_type(typestart);
/* Reset the character we modified. */
*d = savechar;
state = B4VALUE;
*d++ = *s++;
} else if (ISCLOSEBRACKET(*s)) { /* special care for ACL macro */
/* See if the type is defined to use
* the Distinguished Name syntax. */
char savechar;
/* We need typestart to be a string containing only
* the type. We terminate the type and then reset
* the string after we check the syntax. */
savechar = *d;
*d = '\0';
is_dn_syntax = slapi_attr_is_dn_syntax_type(typestart);
/* Reset the character we modified. */
*d = savechar;
state = INVALUE; /* skip a trailing space */
*d++ = *s++;
} else if (ISSPACE(*s)) {
/* See if the type is defined to use
* the Distinguished Name syntax. */
char savechar;
/* We need typestart to be a string containing only
* the type. We terminate the type and then reset
* the string after we check the syntax. */
savechar = *d;
*d = '\0';
is_dn_syntax = slapi_attr_is_dn_syntax_type(typestart);
/* Reset the character we modified. */
*d = savechar;
state = B4EQUAL; /* skip a trailing space */
} else if (ISQUOTE(*s) || SEPARATOR(*s)) {
/* type includes quote / separator; not a valid dn */
rc = -1;
goto bail;
} else {
*d++ = *s++;
}
break;
case B4EQUAL: /* before equal; cn =... */
/* ^ */
if (ISEQUAL(*s)) {
state = B4VALUE;
*d++ = *s++;
} else if (ISSPACE(*s)) {
s++; /* skip trailing spaces */
} else {
/* type includes spaces; not a valid dn */
rc = -1;
goto bail;
}
break;
case B4VALUE: /* before value; cn= ABC */
/* ^ */
if (ISSPACE(*s)) {
s++;
} else {
if (ISQUOTE(*s)) {
s++; /* start with the first char in quotes */
state = INQUOTEDVALUE1ST;
} else {
state = INVALUE1ST;
}
lastesc = NULL;
/* process *s in INVALUE or INQUOTEDVALUE */
}
break;
case INVALUE1ST: /* 1st char in value; cn=ABC */
/* ^ */
if (ISSPACE(*s)) { /* skip leading spaces */
s++;
continue;
} else if (SEPARATOR(*s)) {
/* 1st char in value is separator; invalid dn */
rc = -1;
goto bail;
} /* otherwise, go through */
if (!is_dn_syntax || ISESCAPE(*s)) {
subtypestart = NULL; /* if escaped, can't be multivalued dn */
} else {
subtypestart = d; /* prepare for '+' in the nested DN, if any */
}
subrdn_av_count = 0;
case INVALUE: /* in value; cn=ABC */
/* ^ */
if (ISESCAPE(*s)) {
if (s + 1 >= ends) {
/* DN ends with '\'; invalid dn */
rc = -1;
goto bail;
}
if (((state == INVALUE1ST) && LEADNEEDSESCAPE(*(s+1))) ||
((state == INVALUE) && NEEDSESCAPE(*(s+1)))) {
if (d + 2 >= endd) {
/* Not enough space for dest; this never happens! */
rc = -1;
goto bail;
} else {
if (ISEQUAL(*(s+1)) && is_dn_syntax) {
while (ISSPACE(*(d-1))) {
/* remove trailing spaces */
d--;
}
} else if (SEPARATOR(*(s+1)) && is_dn_syntax) {
/* separator is a subset of needsescape */
while (ISSPACE(*(d-1))) {
/* remove trailing spaces */
d--;
chkblank = 1;
}
if (chkblank && ISESCAPE(*(d-1)) && ISBLANK(*d)) {
/* last space is escaped "cn=A\ ,ou=..." */
/* ^ */
PR_snprintf(d, 3, "%X", *d); /* hexpair */
d += 2;
chkblank = 0;
}
/*
* Track and sort attribute values within
* multivalued RDNs.
*/
if (subtypestart &&
(ISPLUS(*(s+1)) || subrdn_av_count > 0)) {
/* if subtypestart is not valid DN,
* we do not do sorting.*/
char *p = PL_strcasestr(subtypestart, "\\3d");
if (MAYBEDN(p)) {
add_rdn_av(subtypestart, d,
&subrdn_av_count,
&subrdn_avs,
subinitial_rdn_av_stack);
} else {
reset_rdn_avs(&subrdn_avs,
&subrdn_av_count);
subtypestart = NULL;
}
}
if (!ISPLUS(*(s+1))) { /* at end of this RDN */
if (subrdn_av_count > 1) {
sort_rdn_avs( subrdn_avs,
subrdn_av_count, 1 );
}
if (subrdn_av_count > 0) {
reset_rdn_avs( &subrdn_avs,
&subrdn_av_count );
subtypestart = NULL;
}
}
}
/* dn: cn=x\=x\,... -> dn: cn=x\3Dx\2C,... */
*d++ = *s++; /* '\\' */
PR_snprintf(d, 3, "%X", *s); /* hexpair */
d += 2;
if (ISPLUS(*s) && is_dn_syntax) {
/* next type start of multi values */
/* should not be a escape char AND should be
* followed by \\= or \\3D */
if ((PL_strnstr(s, "\\=", ends - s) ||
PL_strncaserstr(s, "\\3D", ends - s))) {
subtypestart = d;
} else {
subtypestart = NULL;
}
}
if ((SEPARATOR(*s) || ISEQUAL(*s)) && is_dn_syntax) {
while (ISSPACE(*(s+1)))
s++; /* remove leading spaces */
s++;
} else {
s++;
}
}
} else if (((state == INVALUE1ST) &&
(s+2 < ends) && LEADNEEDSESCAPESTR(s+1)) ||
((state == INVALUE) &&
(((s+2 < ends) && NEEDSESCAPESTR(s+1)) ||
(ISEOV(s+3, ends) && ISBLANKSTR(s+1))))) {
/* e.g., cn=abc\20 ,... */
/* ^ */
if (ISEQUALSTR(s+1) && is_dn_syntax) {
while (ISSPACE(*(d-1))) {
/* remove trailing spaces */
d--;
}
} else if (SEPARATORSTR(s+1) && is_dn_syntax) {
/* separator is a subset of needsescape */
while (ISSPACE(*(d-1))) {
/* remove trailing spaces */
d--;
chkblank = 1;
}
if (chkblank && ISESCAPE(*(d-1)) && ISBLANK(*d)) {
/* last space is escaped "cn=A\ ,ou=..." */
/* ^ */
PR_snprintf(d, 3, "%X", *d); /* hexpair */
d += 2;
chkblank = 0;
}
/*
* Track and sort attribute values within
* multivalued RDNs.
*/
if (subtypestart &&
(ISPLUSSTR(s+1) || subrdn_av_count > 0)) {
/* if subtypestart is not valid DN,
* we do not do sorting.*/
char *p = PL_strcasestr(subtypestart, "\\3d");
if (MAYBEDN(p)) {
add_rdn_av(subtypestart, d, &subrdn_av_count,
&subrdn_avs, subinitial_rdn_av_stack);
} else {
reset_rdn_avs( &subrdn_avs, &subrdn_av_count );
subtypestart = NULL;
}
}
if (!ISPLUSSTR(s+1)) { /* at end of this RDN */
if (subrdn_av_count > 1) {
sort_rdn_avs( subrdn_avs, subrdn_av_count, 1 );
}
if (subrdn_av_count > 0) {
reset_rdn_avs( &subrdn_avs, &subrdn_av_count );
subtypestart = NULL;
}
}
}
*d++ = *s++; /* '\\' */
*d++ = *s++; /* HEX */
*d++ = *s++; /* HEX */
if (ISPLUSSTR(s-2) && is_dn_syntax) {
/* next type start of multi values */
/* should not be a escape char AND should be followed
* by \\= or \\3D */
if (!ISESCAPE(*s) && (PL_strnstr(s, "\\=", ends - s) ||
PL_strncaserstr(s, "\\3D", ends - s))) {
subtypestart = d;
} else {
subtypestart = NULL;
}
}
if ((SEPARATORSTR(s-2) || ISEQUALSTR(s-2)) && is_dn_syntax) {
while (ISSPACE(*s)) {/* remove leading spaces */
s++;
}
}
} else if (s + 2 < ends &&
isxdigit(*(s+1)) && isxdigit(*(s+2))) {
/* esc hexpair ==> real character */
int n = slapi_hexchar2int(*(s+1));
int n2 = slapi_hexchar2int(*(s+2));
n = (n << 4) + n2;
if (n == 0) { /* don't change \00 */
*d++ = *++s;
*d++ = *++s;
} else {
*d++ = n;
s += 3;
}
} else {
/* ignore an escape for now */
lastesc = d; /* position of the previous escape */
s++;
}
} else if (SEPARATOR(*s)) { /* cn=ABC , ... */
/* ^ */
/* handling a trailing escaped space */
/* assuming a space is the only an extra character which
* is not escaped if it appears in the middle, but should
* be if it does at the end of the RDN value */
/* e.g., ou=ABC \ ,o=XYZ --> ou=ABC \ ,o=XYZ */
if (lastesc) {
while (ISSPACE(*(d-1)) && d > lastesc ) {
d--;
}
if (d == lastesc) {
/* esc hexpair of space: \20 */
*d++ = '\\';
*d++ = '2';
*d++ = '0';
}
} else {
while (ISSPACE(*(d-1))) {
d--;
}
}
state = B4SEPARATOR;
break;
} else if (ISSPACE(*s)) {
/* remove extra spaces, e.g., "cn=ABC DEF" --> "cn=ABC DEF" */
*d++ = *s++;
while (ISSPACE(*s))
s++;
} else {
*d++ = *s++;
}
if (state == INVALUE1ST) {
state = INVALUE;
}
break;
case INQUOTEDVALUE1ST:
if (ISSPACE(*s) && (s+1 < ends && ISSPACE(*(s+1)))) {
/* skip leading spaces but need to leave one */
s++;
continue;
}
if (is_dn_syntax) {
subtypestart = d; /* prepare for '+' in the quoted value, if any */
}
subrdn_av_count = 0;
case INQUOTEDVALUE:
if (ISQUOTE(*s)) {
if (ISESCAPE(*(d-1))) { /* the quote is escaped */
PR_snprintf(d, 3, "%X", *(s++)); /* hexpair */
} else { /* end of INQUOTEVALUE */
if (is_dn_syntax) {
while (ISSPACE(*(d-1))) { /* eliminate trailing spaces */
d--;
chkblank = 1;
}
/* We have to keep the last ' ' of a value in quotes.
* The same idea as the escaped last space:
* "cn=A,ou=B " */
/* ^ */
if (chkblank && ISBLANK(*d)) {
PR_snprintf(d, 4, "\\%X", *d); /* hexpair */
d += 3;
chkblank = 0;
}
} else if (ISSPACE(*(d-1))) {
/* Convert last trailing space to hex code */
d--;
PR_snprintf(d, 4, "\\%X", *d); /* hexpair */
d += 3;
}
state = B4SEPARATOR;
s++;
}
} else if (((state == INQUOTEDVALUE1ST) && LEADNEEDSESCAPE(*s)) ||
(state == INQUOTEDVALUE && NEEDSESCAPE(*s))) {
if (d + 2 >= endd) {
/* Not enough space for dest; this never happens! */
rc = -1;
goto bail;
} else {
if (ISEQUAL(*s) && is_dn_syntax) {
while (ISSPACE(*(d-1))) { /* remove trailing spaces */
d--;
}
} else if (SEPARATOR(*s) && is_dn_syntax) {
/* separator is a subset of needsescape */
while (ISSPACE(*(d-1))) { /* remove trailing spaces */
d--;
chkblank = 1;
}
/* We have to keep the last ' ' of a value in quotes.
* The same idea as the escaped last space:
* "cn=A\ ,ou=..." */
/* ^ */
if (chkblank && ISBLANK(*d)) {
PR_snprintf(d, 4, "\\%X", *d); /* hexpair */
d += 3;
chkblank = 0;
}
/*
* Track and sort attribute values within
* multivalued RDNs.
*/
if (subtypestart &&
(ISPLUS(*s) || subrdn_av_count > 0)) {
/* if subtypestart is not valid DN,
* we do not do sorting.*/