]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
Mem context tweaks for bdb_dn2idl
[openldap] / servers / slapd / dn.c
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/time.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20
21 #include "lutil.h"
22
23 /*
24  * The DN syntax-related functions take advantage of the dn representation
25  * handling functions ldap_str2dn/ldap_dn2str.  The latter are not schema-
26  * aware, so the attributes and their values need be validated (and possibly
27  * normalized).  In the current implementation the required validation/nor-
28  * malization/"pretty"ing are done on newly created DN structural represen-
29  * tations; however the idea is to move towards DN handling in structural
30  * representation instead of the current string representation.  To this
31  * purpose, we need to do only the required operations and keep track of
32  * what has been done to minimize their impact on performances.
33  *
34  * Developers are strongly encouraged to use this feature, to speed-up
35  * its stabilization.
36  */
37
38 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
39
40 /*
41  * In-place, schema-aware validation of the
42  * structural representation of a distinguished name.
43  */
44 static int
45 LDAPDN_validate( LDAPDN dn )
46 {
47         int             iRDN;
48         int             rc;
49
50         assert( dn );
51
52         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
53                 LDAPRDN         rdn = dn[ iRDN ];
54                 int             iAVA;
55
56                 assert( rdn );
57
58                 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
59                         LDAPAVA                 *ava = rdn[ iAVA ];
60                         AttributeDescription    *ad;
61                         slap_syntax_validate_func *validate = NULL;
62
63                         assert( ava );
64                         
65                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
66                                 const char      *text = NULL;
67
68                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
69                                 if ( rc != LDAP_SUCCESS ) {
70                                         return LDAP_INVALID_SYNTAX;
71                                 }
72
73                                 ava->la_private = ( void * )ad;
74                         }
75
76                         /* 
77                          * Replace attr oid/name with the canonical name
78                          */
79                         ava->la_attr = ad->ad_cname;
80
81                         validate = ad->ad_type->sat_syntax->ssyn_validate;
82
83                         if ( validate ) {
84                                 /*
85                                  * validate value by validate function
86                                  */
87                                 rc = ( *validate )( ad->ad_type->sat_syntax,
88                                         &ava->la_value );
89                         
90                                 if ( rc != LDAP_SUCCESS ) {
91                                         return LDAP_INVALID_SYNTAX;
92                                 }
93                         }
94                 }
95         }
96
97         return LDAP_SUCCESS;
98 }
99
100 /*
101  * dn validate routine
102  */
103 int
104 dnValidate(
105         Syntax *syntax,
106         struct berval *in )
107 {
108         int             rc;
109         LDAPDN          dn = NULL;
110
111         assert( in );
112
113         if ( in->bv_len == 0 ) {
114                 return LDAP_SUCCESS;
115
116         } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
117                 return LDAP_INVALID_SYNTAX;
118         }
119
120         rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
121         if ( rc != LDAP_SUCCESS ) {
122                 return LDAP_INVALID_SYNTAX;
123         }
124
125         assert( strlen( in->bv_val ) == in->bv_len );
126
127         /*
128          * Schema-aware validate
129          */
130         rc = LDAPDN_validate( dn );
131         ldap_dnfree( dn );
132
133         if ( rc != LDAP_SUCCESS ) {
134                 return LDAP_INVALID_SYNTAX;
135         }
136
137         return LDAP_SUCCESS;
138 }
139
140 /*
141  * AVA sorting inside a RDN
142  *
143  * rule: sort attributeTypes in alphabetical order; in case of multiple
144  * occurrences of the same attributeType, sort values in byte order
145  * (use memcmp, which implies alphabetical order in case of IA5 value;
146  * this should guarantee the repeatability of the operation).
147  *
148  * Note: the sorting can be slightly improved by sorting first
149  * by attribute type length, then by alphabetical order.
150  *
151  * uses a linear search; should be fine since the number of AVAs in
152  * a RDN should be limited.
153  */
154 static void
155 AVA_Sort( LDAPRDN rdn, int iAVA )
156 {
157         int             i;
158         LDAPAVA         *ava_in = rdn[ iAVA ];
159
160         assert( rdn );
161         assert( ava_in );
162         
163         for ( i = 0; i < iAVA; i++ ) {
164                 LDAPAVA         *ava = rdn[ i ];
165                 int             a, j;
166
167                 assert( ava );
168
169                 a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
170
171                 if ( a > 0 ) {
172                         break;
173                 }
174
175                 while ( a == 0 ) {
176                         int             v, d;
177
178                         d = ava_in->la_value.bv_len - ava->la_value.bv_len;
179
180                         v = memcmp( ava_in->la_value.bv_val, 
181                                         ava->la_value.bv_val,
182                                         d <= 0 ? ava_in->la_value.bv_len 
183                                                 : ava->la_value.bv_len );
184
185                         if ( v == 0 && d != 0 ) {
186                                 v = d;
187                         }
188
189                         if ( v <= 0 ) {
190                                 /* 
191                                  * got it!
192                                  */
193                                 break;
194                         }
195
196                         if ( ++i == iAVA ) {
197                                 /*
198                                  * already sorted
199                                  */
200                                 return;
201                         }
202
203                         ava = rdn[ i ];
204                         a = strcmp( ava_in->la_attr.bv_val, 
205                                         ava->la_attr.bv_val );
206                 }
207
208                 /*
209                  * move ahead
210                  */
211                 for ( j = iAVA; j > i; j-- ) {
212                         rdn[ j ] = rdn[ j - 1 ];
213                 }
214                 rdn[ i ] = ava_in;
215
216                 return;
217         }
218 }
219
220 /*
221  * In-place, schema-aware normalization / "pretty"ing of the
222  * structural representation of a distinguished name.
223  */
224 static int
225 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
226 {
227         int             iRDN;
228         int             rc;
229
230         assert( dn );
231
232         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
233                 LDAPRDN         rdn = dn[ iRDN ];
234                 int             iAVA;
235
236                 assert( rdn );
237
238                 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
239                         LDAPAVA                 *ava = rdn[ iAVA ];
240                         AttributeDescription    *ad;
241                         slap_syntax_validate_func *validf = NULL;
242                         slap_mr_normalize_func *normf = NULL;
243                         slap_syntax_transform_func *transf = NULL;
244                         MatchingRule *mr = NULL;
245                         struct berval           bv = { 0, NULL };
246                         int                     do_sort = 0;
247
248                         assert( ava );
249
250                         if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
251                                 const char      *text = NULL;
252
253                                 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
254                                 if ( rc != LDAP_SUCCESS ) {
255                                         return LDAP_INVALID_SYNTAX;
256                                 }
257                                 
258                                 ava->la_private = ( void * )ad;
259                                 do_sort = 1;
260                         }
261
262                         /* 
263                          * Replace attr oid/name with the canonical name
264                          */
265                         ava->la_attr = ad->ad_cname;
266
267                         if( ava->la_flags & LDAP_AVA_BINARY ) {
268                                 if( ava->la_value.bv_len == 0 ) {
269                                         /* BER encoding is empty */
270                                         return LDAP_INVALID_SYNTAX;
271                                 }
272
273                                 /* AVA is binary encoded, don't muck with it */
274                         } else if( flags & SLAP_LDAPDN_PRETTY ) {
275                                 transf = ad->ad_type->sat_syntax->ssyn_pretty;
276                                 if( !transf ) {
277                                         validf = ad->ad_type->sat_syntax->ssyn_validate;
278                                 }
279                         } else { /* normalization */
280                                 validf = ad->ad_type->sat_syntax->ssyn_validate;
281                                 mr = ad->ad_type->sat_equality;
282                                 if( mr ) normf = mr->smr_normalize;
283                         }
284
285                         if ( validf ) {
286                                 /* validate value before normalization */
287                                 rc = ( *validf )( ad->ad_type->sat_syntax,
288                                         ava->la_value.bv_len
289                                                 ? &ava->la_value
290                                                 : (struct berval *) &slap_empty_bv );
291
292                                 if ( rc != LDAP_SUCCESS ) {
293                                         return LDAP_INVALID_SYNTAX;
294                                 }
295                         }
296
297                         if ( transf ) {
298                                 /*
299                                  * transform value by pretty function
300                                  *      if value is empty, use empty_bv
301                                  */
302                                 rc = ( *transf )( ad->ad_type->sat_syntax,
303                                         ava->la_value.bv_len
304                                                 ? &ava->la_value
305                                                 : (struct berval *) &slap_empty_bv,
306                                         &bv, ctx );
307                         
308                                 if ( rc != LDAP_SUCCESS ) {
309                                         return LDAP_INVALID_SYNTAX;
310                                 }
311                         }
312
313                         if ( normf ) {
314                                 /*
315                                  * normalize value
316                                  *      if value is empty, use empty_bv
317                                  */
318                                 rc = ( *normf )(
319                                         0,
320                                         ad->ad_type->sat_syntax,
321                                         mr,
322                                         ava->la_value.bv_len
323                                                 ? &ava->la_value
324                                                 : (struct berval *) &slap_empty_bv,
325                                         &bv, ctx );
326                         
327                                 if ( rc != LDAP_SUCCESS ) {
328                                         return LDAP_INVALID_SYNTAX;
329                                 }
330                         }
331
332
333                         if( bv.bv_val ) {
334                                 ber_memfree_x( ava->la_value.bv_val, ctx );
335                                 ava->la_value = bv;
336                         }
337
338                         if( do_sort ) AVA_Sort( rdn, iAVA );
339                 }
340         }
341
342         return LDAP_SUCCESS;
343 }
344
345 int
346 dnNormalize(
347     slap_mask_t use,
348     Syntax *syntax,
349     MatchingRule *mr,
350     struct berval *val,
351     struct berval *out,
352     void *ctx)
353 {
354         assert( val );
355         assert( out );
356
357         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
358
359         if ( val->bv_len != 0 ) {
360                 LDAPDN          dn = NULL;
361                 int             rc;
362
363                 /*
364                  * Go to structural representation
365                  */
366                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
367                 if ( rc != LDAP_SUCCESS ) {
368                         return LDAP_INVALID_SYNTAX;
369                 }
370
371                 assert( strlen( val->bv_val ) == val->bv_len );
372
373                 /*
374                  * Schema-aware rewrite
375                  */
376                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
377                         ldap_dnfree( dn );
378                         return LDAP_INVALID_SYNTAX;
379                 }
380
381                 /*
382                  * Back to string representation
383                  */
384                 rc = ldap_dn2bv_x( dn, out,
385                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
386
387                 ldap_dnfree_x( dn, ctx );
388
389                 if ( rc != LDAP_SUCCESS ) {
390                         return LDAP_INVALID_SYNTAX;
391                 }
392         } else {
393                 ber_dupbv_x( out, val, ctx );
394         }
395
396         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
397
398         return LDAP_SUCCESS;
399 }
400
401 #if 0
402 /*
403  * dn "pretty"ing routine
404  */
405 int
406 dnPretty(
407         Syntax *syntax,
408         struct berval *val,
409         struct berval **pretty)
410 {
411         struct berval *out;
412         int rc;
413
414         assert( pretty && *pretty == NULL );
415
416         out = ch_malloc( sizeof( struct berval ) );
417         rc = dnPretty2( syntax, val, out );
418         if ( rc != LDAP_SUCCESS )
419                 free( out );
420         else
421                 *pretty = out;
422         return rc;
423 }
424 #endif
425
426 int
427 dnPretty2(
428         Syntax *syntax,
429         struct berval *val,
430         struct berval *out,
431         void *ctx)
432 {
433         assert( val );
434         assert( out );
435
436 #ifdef NEW_LOGGING
437         LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
438 #else
439         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
440 #endif
441
442         if ( val->bv_len == 0 ) {
443                 ber_dupbv_x( out, val, ctx );
444
445         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
446                 return LDAP_INVALID_SYNTAX;
447
448         } else {
449                 LDAPDN          dn = NULL;
450                 int             rc;
451
452                 /* FIXME: should be liberal in what we accept */
453                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
454                 if ( rc != LDAP_SUCCESS ) {
455                         return LDAP_INVALID_SYNTAX;
456                 }
457
458                 assert( strlen( val->bv_val ) == val->bv_len );
459
460                 /*
461                  * Schema-aware rewrite
462                  */
463                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
464                         ldap_dnfree_x( dn, ctx );
465                         return LDAP_INVALID_SYNTAX;
466                 }
467
468                 /* FIXME: not sure why the default isn't pretty */
469                 /* RE: the default is the form that is used as
470                  * an internal representation; the pretty form
471                  * is a variant */
472                 rc = ldap_dn2bv_x( dn, out,
473                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
474
475                 ldap_dnfree_x( dn, ctx );
476
477                 if ( rc != LDAP_SUCCESS ) {
478                         return LDAP_INVALID_SYNTAX;
479                 }
480         }
481
482         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
483
484         return LDAP_SUCCESS;
485 }
486
487 int
488 dnPrettyNormalDN(
489         Syntax *syntax,
490         struct berval *val,
491         LDAPDN *dn,
492         int flags,
493         void *ctx )
494 {
495         assert( val );
496         assert( dn );
497
498 #ifdef NEW_LOGGING
499         LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n", 
500                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
501                         val->bv_val, 0 );
502 #else
503         Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
504                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
505                         val->bv_val, 0 );
506 #endif
507
508         if ( val->bv_len == 0 ) {
509                 return LDAP_SUCCESS;
510
511         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
512                 return LDAP_INVALID_SYNTAX;
513
514         } else {
515                 int             rc;
516
517                 /* FIXME: should be liberal in what we accept */
518                 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
519                 if ( rc != LDAP_SUCCESS ) {
520                         return LDAP_INVALID_SYNTAX;
521                 }
522
523                 assert( strlen( val->bv_val ) == val->bv_len );
524
525                 /*
526                  * Schema-aware rewrite
527                  */
528                 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
529                         ldap_dnfree_x( *dn, ctx );
530                         *dn = NULL;
531                         return LDAP_INVALID_SYNTAX;
532                 }
533         }
534
535         Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n", 
536                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
537                         0, 0 );
538
539         return LDAP_SUCCESS;
540 }
541
542 /*
543  * Combination of both dnPretty and dnNormalize
544  */
545 int
546 dnPrettyNormal(
547         Syntax *syntax,
548         struct berval *val,
549         struct berval *pretty,
550         struct berval *normal,
551         void *ctx)
552 {
553 #ifdef NEW_LOGGING
554         LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
555 #else
556         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
557 #endif
558
559         assert( val );
560         assert( pretty );
561         assert( normal );
562
563         if ( val->bv_len == 0 ) {
564                 ber_dupbv_x( pretty, val, ctx );
565                 ber_dupbv_x( normal, val, ctx );
566
567         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
568                 /* too big */
569                 return LDAP_INVALID_SYNTAX;
570
571         } else {
572                 LDAPDN          dn = NULL;
573                 int             rc;
574
575                 pretty->bv_val = NULL;
576                 normal->bv_val = NULL;
577                 pretty->bv_len = 0;
578                 normal->bv_len = 0;
579
580                 /* FIXME: should be liberal in what we accept */
581                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
582                 if ( rc != LDAP_SUCCESS ) {
583                         return LDAP_INVALID_SYNTAX;
584                 }
585
586                 assert( strlen( val->bv_val ) == val->bv_len );
587
588                 /*
589                  * Schema-aware rewrite
590                  */
591                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
592                         ldap_dnfree_x( dn, ctx );
593                         return LDAP_INVALID_SYNTAX;
594                 }
595
596                 rc = ldap_dn2bv_x( dn, pretty,
597                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
598
599                 if ( rc != LDAP_SUCCESS ) {
600                         ldap_dnfree_x( dn, ctx );
601                         return LDAP_INVALID_SYNTAX;
602                 }
603
604                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
605                         ldap_dnfree_x( dn, ctx );
606                         ber_memfree_x( pretty->bv_val, ctx );
607                         pretty->bv_val = NULL;
608                         pretty->bv_len = 0;
609                         return LDAP_INVALID_SYNTAX;
610                 }
611
612                 rc = ldap_dn2bv_x( dn, normal,
613                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
614
615                 ldap_dnfree_x( dn, ctx );
616                 if ( rc != LDAP_SUCCESS ) {
617                         ber_memfree_x( pretty->bv_val, ctx );
618                         pretty->bv_val = NULL;
619                         pretty->bv_len = 0;
620                         return LDAP_INVALID_SYNTAX;
621                 }
622         }
623
624 #ifdef NEW_LOGGING
625         LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
626                 pretty->bv_val, normal->bv_val, 0  );
627 #else
628         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
629                 pretty->bv_val, normal->bv_val, 0 );
630 #endif
631
632         return LDAP_SUCCESS;
633 }
634
635 /*
636  * dnMatch routine
637  */
638 int
639 dnMatch(
640         int *matchp,
641         slap_mask_t flags,
642         Syntax *syntax,
643         MatchingRule *mr,
644         struct berval *value,
645         void *assertedValue )
646 {
647         int match;
648         struct berval *asserted = (struct berval *) assertedValue;
649
650         assert( matchp );
651         assert( value );
652         assert( assertedValue );
653         
654         match = value->bv_len - asserted->bv_len;
655
656         if ( match == 0 ) {
657                 match = memcmp( value->bv_val, asserted->bv_val, 
658                                 value->bv_len );
659         }
660
661 #ifdef NEW_LOGGING
662         LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n    %s\n    %s\n", 
663                 match, value->bv_val, asserted->bv_val  );
664 #else
665         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
666                 match, value->bv_val, asserted->bv_val );
667 #endif
668
669         *matchp = match;
670         return( LDAP_SUCCESS );
671 }
672
673 /*
674  * dnParent - dn's parent, in-place
675  *
676  * note: the incoming dn is assumed to be normalized/prettyfied,
677  * so that escaped rdn/ava separators are in '\'+hexpair form
678  */
679 void
680 dnParent( 
681         struct berval   *dn, 
682         struct berval   *pdn )
683 {
684         char    *p;
685
686         p = strchr( dn->bv_val, ',' );
687
688         /* one-level dn */
689         if ( p == NULL ) {
690                 pdn->bv_len = 0;
691                 pdn->bv_val = dn->bv_val + dn->bv_len;
692                 return;
693         }
694
695         assert( DN_SEPARATOR( p[ 0 ] ) );
696         p++;
697
698         assert( ATTR_LEADCHAR( p[ 0 ] ) );
699         pdn->bv_val = p;
700         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
701
702         return;
703 }
704
705 int
706 dnExtractRdn( 
707         struct berval   *dn, 
708         struct berval   *rdn,
709         void *ctx )
710 {
711         LDAPRDN         tmpRDN;
712         const char      *p;
713         int             rc;
714
715         assert( dn );
716         assert( rdn );
717
718         if( dn->bv_len == 0 ) {
719                 return LDAP_OTHER;
720         }
721
722         rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
723         if ( rc != LDAP_SUCCESS ) {
724                 return rc;
725         }
726
727         rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
728
729         ldap_rdnfree_x( tmpRDN, ctx );
730         if ( rc != LDAP_SUCCESS ) {
731                 return rc;
732         }
733
734         return LDAP_SUCCESS;
735 }
736
737 /*
738  * We can assume the input is a prettied or normalized DN
739  */
740 int 
741 dn_rdnlen(
742         Backend         *be,
743         struct berval   *dn_in )
744 {
745         const char      *p;
746
747         assert( dn_in );
748
749         if ( dn_in == NULL ) {
750                 return 0;
751         }
752
753         if ( !dn_in->bv_len ) {
754                 return 0;
755         }
756
757         if ( be != NULL && be_issuffix( be, dn_in ) ) {
758                 return 0;
759         }
760
761         p = strchr( dn_in->bv_val, ',' );
762
763         return p ? p - dn_in->bv_val : dn_in->bv_len;
764 }
765
766
767 /* rdnValidate:
768  *
769  * LDAP_SUCCESS if rdn is a legal rdn;
770  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
771  */
772 int
773 rdnValidate( struct berval *rdn )
774 {
775 #if 1
776         /* Major cheat!
777          * input is a pretty or normalized DN
778          * hence, we can just search for ','
779          */
780         if( rdn == NULL || rdn->bv_len == 0 ||
781                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
782         {
783                 return LDAP_INVALID_SYNTAX;
784         }
785
786         return strchr( rdn->bv_val, ',' ) == NULL
787                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
788
789 #else
790         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
791         const char      *p;
792         int             rc;
793
794         /*
795          * must be non-empty
796          */
797         if ( rdn == NULL || rdn == '\0' ) {
798                 return 0;
799         }
800
801         /*
802          * must be parsable
803          */
804         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
805         if ( rc != LDAP_SUCCESS ) {
806                 return 0;
807         }
808
809         /*
810          * Must be one-level
811          */
812         if ( p[ 0 ] != '\0' ) {
813                 return 0;
814         }
815
816         /*
817          * Schema-aware validate
818          */
819         if ( rc == LDAP_SUCCESS ) {
820                 rc = LDAPDN_validate( DN );
821         }
822         ldap_rdnfree( RDN );
823
824         /*
825          * Must validate (there's a repeated parsing ...)
826          */
827         return ( rc == LDAP_SUCCESS );
828 #endif
829 }
830
831
832 /* build_new_dn:
833  *
834  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
835  * renamed.
836  *
837  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
838  */
839
840 void
841 build_new_dn( struct berval * new_dn,
842         struct berval * parent_dn,
843         struct berval * newrdn )
844 {
845         char *ptr;
846
847         if ( parent_dn == NULL ) {
848                 ber_dupbv( new_dn, newrdn );
849                 return;
850         }
851
852         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
853         new_dn->bv_val = (char *) ch_malloc( new_dn->bv_len + 1 );
854
855         ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
856         *ptr++ = ',';
857         strcpy( ptr, parent_dn->bv_val );
858 }
859
860
861 /*
862  * dnIsSuffix - tells whether suffix is a suffix of dn.
863  * Both dn and suffix must be normalized.
864  */
865 int
866 dnIsSuffix(
867         const struct berval *dn,
868         const struct berval *suffix )
869 {
870         int     d = dn->bv_len - suffix->bv_len;
871
872         assert( dn );
873         assert( suffix );
874
875         /* empty suffix matches any dn */
876         if ( suffix->bv_len == 0 ) {
877                 return 1;
878         }
879
880         /* suffix longer than dn */
881         if ( d < 0 ) {
882                 return 0;
883         }
884
885         /* no rdn separator or escaped rdn separator */
886         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
887                 return 0;
888         }
889
890         /* no possible match or malformed dn */
891         if ( d == 1 ) {
892                 return 0;
893         }
894
895         /* compare */
896         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
897 }
898
899 #ifdef HAVE_TLS
900 /*
901  * Convert an X.509 DN into a normalized LDAP DN
902  */
903 int
904 dnX509normalize( void *x509_name, struct berval *out )
905 {
906         /* Invoke the LDAP library's converter with our schema-rewriter */
907         return ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
908 }
909
910 /*
911  * Get the TLS session's peer's DN into a normalized LDAP DN
912  */
913 int
914 dnX509peerNormalize( void *ssl, struct berval *dn )
915 {
916
917         return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
918 }
919 #endif