]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
74bd594e7602634d68bad0ee7440a0307e9e18d6
[openldap] / servers / slapd / dn.c
1 /* dn.c - routines for dealing with distinguished names */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/ctype.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35
36 #include "slap.h"
37 #include "lutil.h"
38
39 /*
40  * The DN syntax-related functions take advantage of the dn representation
41  * handling functions ldap_str2dn/ldap_dn2str.  The latter are not schema-
42  * aware, so the attributes and their values need be validated (and possibly
43  * normalized).  In the current implementation the required validation/nor-
44  * malization/"pretty"ing are done on newly created DN structural represen-
45  * tations; however the idea is to move towards DN handling in structural
46  * representation instead of the current string representation.  To this
47  * purpose, we need to do only the required operations and keep track of
48  * what has been done to minimize their impact on performances.
49  *
50  * Developers are strongly encouraged to use this feature, to speed-up
51  * its stabilization.
52  */
53
54 #define AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )
55
56 int slap_DN_strict = SLAP_AD_NOINSERT;
57
58 static int
59 LDAPRDN_validate( LDAPRDN rdn )
60 {
61         int             iAVA;
62         int             rc;
63
64         assert( rdn != NULL );
65
66         for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
67                 LDAPAVA                 *ava = rdn[ iAVA ];
68                 AttributeDescription    *ad;
69                 slap_syntax_validate_func *validate = NULL;
70
71                 assert( ava != NULL );
72                 
73                 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
74                         const char      *text = NULL;
75
76                         rc = slap_bv2ad( &ava->la_attr, &ad, &text );
77                         if ( rc != LDAP_SUCCESS ) {
78                                 rc = slap_bv2undef_ad( &ava->la_attr,
79                                         &ad, &text,
80                                         SLAP_AD_PROXIED|slap_DN_strict );
81                                 if ( rc != LDAP_SUCCESS ) {
82                                         return LDAP_INVALID_SYNTAX;
83                                 }
84                         }
85
86                         ava->la_private = ( void * )ad;
87                 }
88
89                 /*
90                  * Do not allow X-ORDERED 'VALUES' naming attributes
91                  */
92                 if ( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
93                         return LDAP_INVALID_SYNTAX;
94                 }
95
96                 /* 
97                  * Replace attr oid/name with the canonical name
98                  */
99                 ava->la_attr = ad->ad_cname;
100
101                 validate = ad->ad_type->sat_syntax->ssyn_validate;
102
103                 if ( validate ) {
104                         /*
105                          * validate value by validate function
106                          */
107                         rc = ( *validate )( ad->ad_type->sat_syntax,
108                                 &ava->la_value );
109                         
110                         if ( rc != LDAP_SUCCESS ) {
111                                 return LDAP_INVALID_SYNTAX;
112                         }
113                 }
114         }
115
116         return LDAP_SUCCESS;
117 }
118
119 /*
120  * In-place, schema-aware validation of the
121  * structural representation of a distinguished name.
122  */
123 static int
124 LDAPDN_validate( LDAPDN dn )
125 {
126         int             iRDN;
127         int             rc;
128
129         assert( dn != NULL );
130
131         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
132                 rc = LDAPRDN_validate( dn[ iRDN ] );
133                 if ( rc != LDAP_SUCCESS ) {
134                         return rc;
135                 }
136         }
137
138         return LDAP_SUCCESS;
139 }
140
141 /*
142  * dn validate routine
143  */
144 int
145 dnValidate(
146         Syntax *syntax,
147         struct berval *in )
148 {
149         int             rc;
150         LDAPDN          dn = NULL;
151
152         assert( in != NULL );
153
154         if ( in->bv_len == 0 ) {
155                 return LDAP_SUCCESS;
156
157         } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
158                 return LDAP_INVALID_SYNTAX;
159         }
160
161         rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
162         if ( rc != LDAP_SUCCESS ) {
163                 return LDAP_INVALID_SYNTAX;
164         }
165
166         assert( strlen( in->bv_val ) == in->bv_len );
167
168         /*
169          * Schema-aware validate
170          */
171         rc = LDAPDN_validate( dn );
172         ldap_dnfree( dn );
173
174         if ( rc != LDAP_SUCCESS ) {
175                 return LDAP_INVALID_SYNTAX;
176         }
177
178         return LDAP_SUCCESS;
179 }
180
181 int
182 rdnValidate(
183         Syntax *syntax,
184         struct berval *in )
185 {
186         int             rc;
187         LDAPRDN         rdn;
188         char*           p;
189
190         assert( in != NULL );
191         if ( in->bv_len == 0 ) {
192                 return LDAP_SUCCESS;
193
194         } else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
195                 return LDAP_INVALID_SYNTAX;
196         }
197
198         rc = ldap_bv2rdn_x( in , &rdn, (char **) &p,
199                                 LDAP_DN_FORMAT_LDAP, NULL);
200         if ( rc != LDAP_SUCCESS ) {
201                 return LDAP_INVALID_SYNTAX;
202         }
203
204         assert( strlen( in->bv_val ) == in->bv_len );
205
206         /*
207          * Schema-aware validate
208          */
209         rc = LDAPRDN_validate( rdn );
210         ldap_rdnfree( rdn );
211
212         if ( rc != LDAP_SUCCESS ) {
213                 return LDAP_INVALID_SYNTAX;
214         }
215
216         return LDAP_SUCCESS;
217 }
218
219
220 /*
221  * AVA sorting inside a RDN
222  *
223  * Rule: sort attributeTypes in alphabetical order.
224  *
225  * Note: the sorting can be slightly improved by sorting first
226  * by attribute type length, then by alphabetical order.
227  *
228  * uses an insertion sort; should be fine since the number of AVAs in
229  * a RDN should be limited.
230  */
231 static int
232 AVA_Sort( LDAPRDN rdn, int nAVAs )
233 {
234         LDAPAVA *ava_i;
235         int             i;
236
237         assert( rdn != NULL );
238
239         for ( i = 1; i < nAVAs; i++ ) {
240                 LDAPAVA *ava_j;
241                 int j;
242
243                 ava_i = rdn[ i ];
244                 for ( j = i-1; j >=0; j-- ) {
245                         int a;
246
247                         ava_j = rdn[ j ];
248                         a = strcmp( ava_i->la_attr.bv_val, ava_j->la_attr.bv_val );
249
250                         /* RFC4512 does not allow multiple AVAs
251                          * with the same attribute type in RDN (ITS#5968) */
252                         if ( a == 0 )
253                                 return LDAP_INVALID_DN_SYNTAX;
254
255                         if ( a > 0 )
256                                 break;
257
258                         rdn[ j+1 ] = rdn[ j ];
259                 }
260                 rdn[ j+1 ] = ava_i;
261         }
262         return LDAP_SUCCESS;
263 }
264
265 static int
266 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
267 {
268
269         int rc, iAVA, do_sort = 0;
270
271         for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
272                 LDAPAVA                 *ava = rdn[ iAVA ];
273                 AttributeDescription    *ad;
274                 slap_syntax_validate_func *validf = NULL;
275                 slap_mr_normalize_func *normf = NULL;
276                 slap_syntax_transform_func *transf = NULL;
277                 MatchingRule *mr = NULL;
278                 struct berval           bv = BER_BVNULL;
279
280                 assert( ava != NULL );
281
282                 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
283                         const char      *text = NULL;
284
285                         rc = slap_bv2ad( &ava->la_attr, &ad, &text );
286                         if ( rc != LDAP_SUCCESS ) {
287                                 rc = slap_bv2undef_ad( &ava->la_attr,
288                                         &ad, &text,
289                                         SLAP_AD_PROXIED|slap_DN_strict );
290                                 if ( rc != LDAP_SUCCESS ) {
291                                         return LDAP_INVALID_SYNTAX;
292                                 }
293                         }
294                         
295                         ava->la_private = ( void * )ad;
296                         do_sort = 1;
297                 }
298
299                 /* 
300                  * Replace attr oid/name with the canonical name
301                  */
302                 ava->la_attr = ad->ad_cname;
303
304                 if( ava->la_flags & LDAP_AVA_BINARY ) {
305                         /* AVA is binary encoded, not supported */
306                         return LDAP_INVALID_SYNTAX;
307
308                         /* Do not allow X-ORDERED 'VALUES' naming attributes */
309                 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
310                         return LDAP_INVALID_SYNTAX;
311
312                 } else if( flags & SLAP_LDAPDN_PRETTY ) {
313                         transf = ad->ad_type->sat_syntax->ssyn_pretty;
314                         if( !transf ) {
315                                 validf = ad->ad_type->sat_syntax->ssyn_validate;
316                         }
317                 } else { /* normalization */
318                         validf = ad->ad_type->sat_syntax->ssyn_validate;
319                         mr = ad->ad_type->sat_equality;
320                         if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
321                                 normf = mr->smr_normalize;
322                         }
323                 }
324
325                 if ( validf ) {
326                         /* validate value before normalization */
327                         rc = ( *validf )( ad->ad_type->sat_syntax,
328                                 ava->la_value.bv_len
329                                         ? &ava->la_value
330                                         : (struct berval *) &slap_empty_bv );
331
332                         if ( rc != LDAP_SUCCESS ) {
333                                 return LDAP_INVALID_SYNTAX;
334                         }
335                 }
336
337                 if ( transf ) {
338                         /*
339                          * transform value by pretty function
340                          *      if value is empty, use empty_bv
341                          */
342                         rc = ( *transf )( ad->ad_type->sat_syntax,
343                                 ava->la_value.bv_len
344                                         ? &ava->la_value
345                                         : (struct berval *) &slap_empty_bv,
346                                 &bv, ctx );
347                 
348                         if ( rc != LDAP_SUCCESS ) {
349                                 return LDAP_INVALID_SYNTAX;
350                         }
351                 }
352
353                 if ( normf ) {
354                         /*
355                          * normalize value
356                          *      if value is empty, use empty_bv
357                          */
358                         rc = ( *normf )(
359                                 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
360                                 ad->ad_type->sat_syntax,
361                                 mr,
362                                 ava->la_value.bv_len
363                                         ? &ava->la_value
364                                         : (struct berval *) &slap_empty_bv,
365                                 &bv, ctx );
366                 
367                         if ( rc != LDAP_SUCCESS ) {
368                                 return LDAP_INVALID_SYNTAX;
369                         }
370                 }
371
372
373                 if( bv.bv_val ) {
374                         if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
375                                 ber_memfree_x( ava->la_value.bv_val, ctx );
376                         ava->la_value = bv;
377                         ava->la_flags |= LDAP_AVA_FREE_VALUE;
378                 }
379                 /* reject empty values */
380                 if (!ava->la_value.bv_len) {
381                         return LDAP_INVALID_SYNTAX;
382                 }
383         }
384         rc = LDAP_SUCCESS;
385
386         if ( do_sort ) {
387                 rc = AVA_Sort( rdn, iAVA );
388         }
389
390         return rc;
391 }
392
393 /*
394  * In-place, schema-aware normalization / "pretty"ing of the
395  * structural representation of a distinguished name.
396  */
397 static int
398 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
399 {
400         int             iRDN;
401         int             rc;
402
403         assert( dn != NULL );
404
405         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
406                 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
407                 if ( rc != LDAP_SUCCESS ) {
408                         return rc;
409                 }
410         }
411
412         return LDAP_SUCCESS;
413 }
414
415 int
416 dnNormalize(
417     slap_mask_t use,
418     Syntax *syntax,
419     MatchingRule *mr,
420     struct berval *val,
421     struct berval *out,
422     void *ctx)
423 {
424         assert( val != NULL );
425         assert( out != NULL );
426
427         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
428
429         if ( val->bv_len != 0 ) {
430                 LDAPDN          dn = NULL;
431                 int             rc;
432
433                 /*
434                  * Go to structural representation
435                  */
436                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
437                 if ( rc != LDAP_SUCCESS ) {
438                         return LDAP_INVALID_SYNTAX;
439                 }
440
441                 assert( strlen( val->bv_val ) == val->bv_len );
442
443                 /*
444                  * Schema-aware rewrite
445                  */
446                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
447                         ldap_dnfree_x( dn, ctx );
448                         return LDAP_INVALID_SYNTAX;
449                 }
450
451                 /*
452                  * Back to string representation
453                  */
454                 rc = ldap_dn2bv_x( dn, out,
455                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
456
457                 ldap_dnfree_x( dn, ctx );
458
459                 if ( rc != LDAP_SUCCESS ) {
460                         return LDAP_INVALID_SYNTAX;
461                 }
462         } else {
463                 ber_dupbv_x( out, val, ctx );
464         }
465
466         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
467
468         return LDAP_SUCCESS;
469 }
470
471 int
472 rdnNormalize(
473     slap_mask_t use,
474     Syntax *syntax,
475     MatchingRule *mr,
476     struct berval *val,
477     struct berval *out,
478     void *ctx)
479 {
480         assert( val != NULL );
481         assert( out != NULL );
482
483         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
484         if ( val->bv_len != 0 ) {
485                 LDAPRDN         rdn = NULL;
486                 int             rc;
487                 char*           p;
488
489                 /*
490                  * Go to structural representation
491                  */
492                 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
493                                         LDAP_DN_FORMAT_LDAP, ctx);
494
495                 if ( rc != LDAP_SUCCESS ) {
496                         return LDAP_INVALID_SYNTAX;
497                 }
498
499                 assert( strlen( val->bv_val ) == val->bv_len );
500
501                 /*
502                  * Schema-aware rewrite
503                  */
504                 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
505                         ldap_rdnfree_x( rdn, ctx );
506                         return LDAP_INVALID_SYNTAX;
507                 }
508
509                 /*
510                  * Back to string representation
511                  */
512                 rc = ldap_rdn2bv_x( rdn, out,
513                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
514
515                 ldap_rdnfree_x( rdn, ctx );
516
517                 if ( rc != LDAP_SUCCESS ) {
518                         return LDAP_INVALID_SYNTAX;
519                 }
520         } else {
521                 ber_dupbv_x( out, val, ctx );
522         }
523
524         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
525
526         return LDAP_SUCCESS;
527 }
528
529 int
530 dnPretty(
531         Syntax *syntax,
532         struct berval *val,
533         struct berval *out,
534         void *ctx)
535 {
536         assert( val != NULL );
537         assert( out != NULL );
538
539         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
540
541         if ( val->bv_len == 0 ) {
542                 ber_dupbv_x( out, val, ctx );
543
544         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
545                 return LDAP_INVALID_SYNTAX;
546
547         } else {
548                 LDAPDN          dn = NULL;
549                 int             rc;
550
551                 /* FIXME: should be liberal in what we accept */
552                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
553                 if ( rc != LDAP_SUCCESS ) {
554                         return LDAP_INVALID_SYNTAX;
555                 }
556
557                 assert( strlen( val->bv_val ) == val->bv_len );
558
559                 /*
560                  * Schema-aware rewrite
561                  */
562                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
563                         ldap_dnfree_x( dn, ctx );
564                         return LDAP_INVALID_SYNTAX;
565                 }
566
567                 /* FIXME: not sure why the default isn't pretty */
568                 /* RE: the default is the form that is used as
569                  * an internal representation; the pretty form
570                  * is a variant */
571                 rc = ldap_dn2bv_x( dn, out,
572                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
573
574                 ldap_dnfree_x( dn, ctx );
575
576                 if ( rc != LDAP_SUCCESS ) {
577                         return LDAP_INVALID_SYNTAX;
578                 }
579         }
580
581         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
582
583         return LDAP_SUCCESS;
584 }
585
586 int
587 rdnPretty(
588         Syntax *syntax,
589         struct berval *val,
590         struct berval *out,
591         void *ctx)
592 {
593         assert( val != NULL );
594         assert( out != NULL );
595
596         Debug( LDAP_DEBUG_TRACE, ">>> rdnPretty: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
597
598         if ( val->bv_len == 0 ) {
599                 ber_dupbv_x( out, val, ctx );
600
601         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
602                 return LDAP_INVALID_SYNTAX;
603
604         } else {
605                 LDAPRDN         rdn = NULL;
606                 int             rc;
607                 char*           p;
608
609                 /* FIXME: should be liberal in what we accept */
610                 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
611                                         LDAP_DN_FORMAT_LDAP, ctx);
612                 if ( rc != LDAP_SUCCESS ) {
613                         return LDAP_INVALID_SYNTAX;
614                 }
615
616                 assert( strlen( val->bv_val ) == val->bv_len );
617
618                 /*
619                  * Schema-aware rewrite
620                  */
621                 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
622                         ldap_rdnfree_x( rdn, ctx );
623                         return LDAP_INVALID_SYNTAX;
624                 }
625
626                 /* FIXME: not sure why the default isn't pretty */
627                 /* RE: the default is the form that is used as
628                  * an internal representation; the pretty form
629                  * is a variant */
630                 rc = ldap_rdn2bv_x( rdn, out,
631                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
632
633                 ldap_rdnfree_x( rdn, ctx );
634
635                 if ( rc != LDAP_SUCCESS ) {
636                         return LDAP_INVALID_SYNTAX;
637                 }
638         }
639
640         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val ? out->bv_val : "", 0, 0 );
641
642         return LDAP_SUCCESS;
643 }
644
645
646 int
647 dnPrettyNormalDN(
648         Syntax *syntax,
649         struct berval *val,
650         LDAPDN *dn,
651         int flags,
652         void *ctx )
653 {
654         assert( val != NULL );
655         assert( dn != NULL );
656
657         Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
658                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
659                         val->bv_val ? val->bv_val : "", 0 );
660
661         if ( val->bv_len == 0 ) {
662                 return LDAP_SUCCESS;
663
664         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
665                 return LDAP_INVALID_SYNTAX;
666
667         } else {
668                 int             rc;
669
670                 /* FIXME: should be liberal in what we accept */
671                 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
672                 if ( rc != LDAP_SUCCESS ) {
673                         return LDAP_INVALID_SYNTAX;
674                 }
675
676                 assert( strlen( val->bv_val ) == val->bv_len );
677
678                 /*
679                  * Schema-aware rewrite
680                  */
681                 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
682                         ldap_dnfree_x( *dn, ctx );
683                         *dn = NULL;
684                         return LDAP_INVALID_SYNTAX;
685                 }
686         }
687
688         Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n", 
689                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
690                         0, 0 );
691
692         return LDAP_SUCCESS;
693 }
694
695 /*
696  * Combination of both dnPretty and dnNormalize
697  */
698 int
699 dnPrettyNormal(
700         Syntax *syntax,
701         struct berval *val,
702         struct berval *pretty,
703         struct berval *normal,
704         void *ctx)
705 {
706         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val ? val->bv_val : "", 0, 0 );
707
708         assert( val != NULL );
709         assert( pretty != NULL );
710         assert( normal != NULL );
711
712         if ( val->bv_len == 0 ) {
713                 ber_dupbv_x( pretty, val, ctx );
714                 ber_dupbv_x( normal, val, ctx );
715
716         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
717                 /* too big */
718                 return LDAP_INVALID_SYNTAX;
719
720         } else {
721                 LDAPDN          dn = NULL;
722                 int             rc;
723
724                 pretty->bv_val = NULL;
725                 normal->bv_val = NULL;
726                 pretty->bv_len = 0;
727                 normal->bv_len = 0;
728
729                 /* FIXME: should be liberal in what we accept */
730                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
731                 if ( rc != LDAP_SUCCESS ) {
732                         return LDAP_INVALID_SYNTAX;
733                 }
734
735                 assert( strlen( val->bv_val ) == val->bv_len );
736
737                 /*
738                  * Schema-aware rewrite
739                  */
740                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
741                         ldap_dnfree_x( dn, ctx );
742                         return LDAP_INVALID_SYNTAX;
743                 }
744
745                 rc = ldap_dn2bv_x( dn, pretty,
746                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
747
748                 if ( rc != LDAP_SUCCESS ) {
749                         ldap_dnfree_x( dn, ctx );
750                         return LDAP_INVALID_SYNTAX;
751                 }
752
753                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
754                         ldap_dnfree_x( dn, ctx );
755                         ber_memfree_x( pretty->bv_val, ctx );
756                         pretty->bv_val = NULL;
757                         pretty->bv_len = 0;
758                         return LDAP_INVALID_SYNTAX;
759                 }
760
761                 rc = ldap_dn2bv_x( dn, normal,
762                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
763
764                 ldap_dnfree_x( dn, ctx );
765                 if ( rc != LDAP_SUCCESS ) {
766                         ber_memfree_x( pretty->bv_val, ctx );
767                         pretty->bv_val = NULL;
768                         pretty->bv_len = 0;
769                         return LDAP_INVALID_SYNTAX;
770                 }
771         }
772
773         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
774                 pretty->bv_val ? pretty->bv_val : "",
775                 normal->bv_val ? normal->bv_val : "", 0 );
776
777         return LDAP_SUCCESS;
778 }
779
780 /*
781  * dnMatch routine
782  */
783 int
784 dnMatch(
785         int *matchp,
786         slap_mask_t flags,
787         Syntax *syntax,
788         MatchingRule *mr,
789         struct berval *value,
790         void *assertedValue )
791 {
792         int match;
793         struct berval *asserted = (struct berval *) assertedValue;
794
795         assert( matchp != NULL );
796         assert( value != NULL );
797         assert( assertedValue != NULL );
798         assert( !BER_BVISNULL( value ) );
799         assert( !BER_BVISNULL( asserted ) );
800         
801         match = value->bv_len - asserted->bv_len;
802
803         if ( match == 0 ) {
804                 match = memcmp( value->bv_val, asserted->bv_val, 
805                                 value->bv_len );
806         }
807
808         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
809                 match, value->bv_val, asserted->bv_val );
810
811         *matchp = match;
812         return LDAP_SUCCESS;
813 }
814
815 /*
816  * dnRelativeMatch routine
817  */
818 int
819 dnRelativeMatch(
820         int *matchp,
821         slap_mask_t flags,
822         Syntax *syntax,
823         MatchingRule *mr,
824         struct berval *value,
825         void *assertedValue )
826 {
827         int match;
828         struct berval *asserted = (struct berval *) assertedValue;
829
830         assert( matchp != NULL );
831         assert( value != NULL );
832         assert( assertedValue != NULL );
833         assert( !BER_BVISNULL( value ) );
834         assert( !BER_BVISNULL( asserted ) );
835
836         if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
837                 if( asserted->bv_len > value->bv_len ) {
838                         match = -1;
839                 } else if ( asserted->bv_len == value->bv_len ) {
840                         match = memcmp( value->bv_val, asserted->bv_val, 
841                                 value->bv_len );
842                 } else {
843                         if( DN_SEPARATOR(
844                                 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
845                         {
846                                 match = memcmp(
847                                         &value->bv_val[value->bv_len - asserted->bv_len],
848                                         asserted->bv_val, 
849                                         asserted->bv_len );
850                         } else {
851                                 match = 1;
852                         }
853                 }
854
855                 *matchp = match;
856                 return LDAP_SUCCESS;
857         }
858
859         if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
860                 asserted = value;
861                 value = (struct berval *) assertedValue;
862                 mr = slap_schema.si_mr_dnSubordinateMatch;
863         }
864
865         if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
866                 if( asserted->bv_len >= value->bv_len ) {
867                         match = -1;
868                 } else {
869                         if( DN_SEPARATOR(
870                                 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
871                         {
872                                 match = memcmp(
873                                         &value->bv_val[value->bv_len - asserted->bv_len],
874                                         asserted->bv_val, 
875                                         asserted->bv_len );
876                         } else {
877                                 match = 1;
878                         }
879                 }
880
881                 *matchp = match;
882                 return LDAP_SUCCESS;
883         }
884
885         if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
886                 if( asserted->bv_len >= value->bv_len ) {
887                         match = -1;
888                 } else {
889                         if( DN_SEPARATOR(
890                                 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
891                         {
892                                 match = memcmp(
893                                         &value->bv_val[value->bv_len - asserted->bv_len],
894                                         asserted->bv_val, 
895                                         asserted->bv_len );
896
897                                 if( !match ) {
898                                         struct berval rdn;
899                                         rdn.bv_val = value->bv_val;
900                                         rdn.bv_len = value->bv_len - asserted->bv_len - 1;
901                                         match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
902                                 }
903                         } else {
904                                 match = 1;
905                         }
906                 }
907
908                 *matchp = match;
909                 return LDAP_SUCCESS;
910         }
911
912         /* should not be reachable */
913         assert( 0 );
914         return LDAP_OTHER;
915 }
916
917 int
918 rdnMatch(
919         int *matchp,
920         slap_mask_t flags,
921         Syntax *syntax,
922         MatchingRule *mr,
923         struct berval *value,
924         void *assertedValue )
925 {
926         int match;
927         struct berval *asserted = (struct berval *) assertedValue;
928
929         assert( matchp != NULL );
930         assert( value != NULL );
931         assert( assertedValue != NULL );
932         
933         match = value->bv_len - asserted->bv_len;
934
935         if ( match == 0 ) {
936                 match = memcmp( value->bv_val, asserted->bv_val, 
937                                 value->bv_len );
938         }
939
940         Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
941                 match, value->bv_val, asserted->bv_val );
942
943         *matchp = match;
944         return LDAP_SUCCESS;
945 }
946
947
948 /*
949  * dnParent - dn's parent, in-place
950  * note: the incoming dn is assumed to be normalized/prettyfied,
951  * so that escaped rdn/ava separators are in '\'+hexpair form
952  *
953  * note: "dn" and "pdn" can point to the same berval;
954  * beware that, in this case, the pointer to the original buffer
955  * will get lost.
956  */
957 void
958 dnParent( 
959         struct berval   *dn, 
960         struct berval   *pdn )
961 {
962         char    *p;
963
964         p = ber_bvchr( dn, ',' );
965
966         /* one-level dn */
967         if ( p == NULL ) {
968                 pdn->bv_val = dn->bv_val + dn->bv_len;
969                 pdn->bv_len = 0;
970                 return;
971         }
972
973         assert( DN_SEPARATOR( p[ 0 ] ) );
974         p++;
975
976         assert( ATTR_LEADCHAR( p[ 0 ] ) );
977         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
978         pdn->bv_val = p;
979
980         return;
981 }
982
983 /*
984  * dnRdn - dn's rdn, in-place
985  * note: the incoming dn is assumed to be normalized/prettyfied,
986  * so that escaped rdn/ava separators are in '\'+hexpair form
987  */
988 void
989 dnRdn( 
990         struct berval   *dn, 
991         struct berval   *rdn )
992 {
993         char    *p;
994
995         *rdn = *dn;
996         p = ber_bvchr( dn, ',' );
997
998         /* one-level dn */
999         if ( p == NULL ) {
1000                 return;
1001         }
1002
1003         assert( DN_SEPARATOR( p[ 0 ] ) );
1004         assert( ATTR_LEADCHAR( p[ 1 ] ) );
1005         rdn->bv_len = p - dn->bv_val;
1006
1007         return;
1008 }
1009
1010 int
1011 dnExtractRdn( 
1012         struct berval   *dn, 
1013         struct berval   *rdn,
1014         void *ctx )
1015 {
1016         LDAPRDN         tmpRDN;
1017         const char      *p;
1018         int             rc;
1019
1020         assert( dn != NULL );
1021         assert( rdn != NULL );
1022
1023         if( dn->bv_len == 0 ) {
1024                 return LDAP_OTHER;
1025         }
1026
1027         rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1028         if ( rc != LDAP_SUCCESS ) {
1029                 return rc;
1030         }
1031
1032         rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1033                 ctx );
1034
1035         ldap_rdnfree_x( tmpRDN, ctx );
1036         return rc;
1037 }
1038
1039 /*
1040  * We can assume the input is a prettied or normalized DN
1041  */
1042 ber_len_t
1043 dn_rdnlen(
1044         Backend         *be,
1045         struct berval   *dn_in )
1046 {
1047         const char      *p;
1048
1049         assert( dn_in != NULL );
1050
1051         if ( dn_in == NULL ) {
1052                 return 0;
1053         }
1054
1055         if ( !dn_in->bv_len ) {
1056                 return 0;
1057         }
1058
1059         if ( be != NULL && be_issuffix( be, dn_in ) ) {
1060                 return 0;
1061         }
1062
1063         p = ber_bvchr( dn_in, ',' );
1064
1065         return p ? (ber_len_t) (p - dn_in->bv_val) : dn_in->bv_len;
1066 }
1067
1068
1069 /* rdnValidate:
1070  *
1071  * LDAP_SUCCESS if rdn is a legal rdn;
1072  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1073  */
1074 int
1075 rdn_validate( struct berval *rdn )
1076 {
1077 #if 1
1078         /* Major cheat!
1079          * input is a pretty or normalized DN
1080          * hence, we can just search for ','
1081          */
1082         if( rdn == NULL || rdn->bv_len == 0 ||
1083                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1084         {
1085                 return LDAP_INVALID_SYNTAX;
1086         }
1087         return ber_bvchr( rdn, ',' ) == NULL
1088                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1089
1090 #else
1091         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
1092         const char      *p;
1093         int             rc;
1094
1095         /*
1096          * must be non-empty
1097          */
1098         if ( rdn == NULL || rdn == '\0' ) {
1099                 return 0;
1100         }
1101
1102         /*
1103          * must be parsable
1104          */
1105         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1106         if ( rc != LDAP_SUCCESS ) {
1107                 return 0;
1108         }
1109
1110         /*
1111          * Must be one-level
1112          */
1113         if ( p[ 0 ] != '\0' ) {
1114                 return 0;
1115         }
1116
1117         /*
1118          * Schema-aware validate
1119          */
1120         if ( rc == LDAP_SUCCESS ) {
1121                 rc = LDAPDN_validate( DN );
1122         }
1123         ldap_rdnfree( RDN );
1124
1125         /*
1126          * Must validate (there's a repeated parsing ...)
1127          */
1128         return ( rc == LDAP_SUCCESS );
1129 #endif
1130 }
1131
1132
1133 /* build_new_dn:
1134  *
1135  * Used by back-bdb back_modrdn to create the new dn of entries being
1136  * renamed.
1137  *
1138  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1139  */
1140
1141 void
1142 build_new_dn( struct berval * new_dn,
1143         struct berval * parent_dn,
1144         struct berval * newrdn,
1145         void *memctx )
1146 {
1147         char *ptr;
1148
1149         if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1150                 ber_dupbv_x( new_dn, newrdn, memctx );
1151                 return;
1152         }
1153
1154         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1155         new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1156
1157         ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1158         *ptr++ = ',';
1159         strcpy( ptr, parent_dn->bv_val );
1160 }
1161
1162
1163 /*
1164  * dnIsSuffix - tells whether suffix is a suffix of dn.
1165  * Both dn and suffix must be normalized.
1166  */
1167 int
1168 dnIsSuffix(
1169         const struct berval *dn,
1170         const struct berval *suffix )
1171 {
1172         int     d = dn->bv_len - suffix->bv_len;
1173
1174         assert( dn != NULL );
1175         assert( suffix != NULL );
1176
1177         /* empty suffix matches any dn */
1178         if ( suffix->bv_len == 0 ) {
1179                 return 1;
1180         }
1181
1182         /* suffix longer than dn */
1183         if ( d < 0 ) {
1184                 return 0;
1185         }
1186
1187         /* no rdn separator or escaped rdn separator */
1188         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1189                 return 0;
1190         }
1191
1192         /* no possible match or malformed dn */
1193         if ( d == 1 ) {
1194                 return 0;
1195         }
1196
1197         /* compare */
1198         return( strncmp( dn->bv_val + d, suffix->bv_val, suffix->bv_len ) == 0 );
1199 }
1200
1201 /*
1202  * In place; assumes:
1203  * - ndn is normalized
1204  * - nbase is normalized
1205  * - dnIsSuffix( ndn, nbase ) == TRUE
1206  * - LDAP_SCOPE_DEFAULT == LDAP_SCOPE_SUBTREE
1207  */
1208 int
1209 dnIsWithinScope( struct berval *ndn, struct berval *nbase, int scope )
1210 {
1211         assert( ndn != NULL );
1212         assert( nbase != NULL );
1213         assert( !BER_BVISNULL( ndn ) );
1214         assert( !BER_BVISNULL( nbase ) );
1215
1216         switch ( scope ) {
1217         case LDAP_SCOPE_DEFAULT:
1218         case LDAP_SCOPE_SUBTREE:
1219                 break;
1220
1221         case LDAP_SCOPE_BASE:
1222                 if ( ndn->bv_len != nbase->bv_len ) {
1223                         return 0;
1224                 }
1225                 break;
1226
1227         case LDAP_SCOPE_ONELEVEL: {
1228                 struct berval pndn;
1229                 dnParent( ndn, &pndn );
1230                 if ( pndn.bv_len != nbase->bv_len ) {
1231                         return 0;
1232                 }
1233                 } break;
1234
1235         case LDAP_SCOPE_SUBORDINATE:
1236                 if ( ndn->bv_len == nbase->bv_len ) {
1237                         return 0;
1238                 }
1239                 break;
1240
1241         /* unknown scope */
1242         default:
1243                 return -1;
1244         }
1245
1246         return 1;
1247 }
1248
1249 /*
1250  * In place; assumes:
1251  * - ndn is normalized
1252  * - nbase is normalized
1253  * - LDAP_SCOPE_DEFAULT == LDAP_SCOPE_SUBTREE
1254  */
1255 int
1256 dnIsSuffixScope( struct berval *ndn, struct berval *nbase, int scope )
1257 {
1258         if ( !dnIsSuffix( ndn, nbase ) ) {
1259                 return 0;
1260         }
1261
1262         return dnIsWithinScope( ndn, nbase, scope );
1263 }
1264
1265 int
1266 dnIsOneLevelRDN( struct berval *rdn )
1267 {
1268         ber_len_t       len = rdn->bv_len;
1269         for ( ; len--; ) {
1270                 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1271                         return 0;
1272                 }
1273         }
1274
1275         return 1;
1276 }
1277
1278 #ifdef HAVE_TLS
1279 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1280 #endif
1281
1282 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1283 {
1284 #ifdef HAVE_TLS
1285         if ( DNX509PeerNormalizeCertMap == NULL ) {
1286                 DNX509PeerNormalizeCertMap = fn;
1287                 return 0;
1288         }
1289 #endif
1290
1291         return -1;
1292 }
1293
1294 /*
1295  * Convert an X.509 DN into a normalized LDAP DN
1296  */
1297 int
1298 dnX509normalize( void *x509_name, struct berval *out )
1299 {
1300         /* Invoke the LDAP library's converter with our schema-rewriter */
1301         int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1302
1303         Debug( LDAP_DEBUG_TRACE,
1304                 "dnX509Normalize: <%s> (%d)\n",
1305                 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1306
1307         return rc;
1308 }
1309
1310 #ifdef HAVE_TLS
1311 /*
1312  * Get the TLS session's peer's DN into a normalized LDAP DN
1313  */
1314 int
1315 dnX509peerNormalize( void *ssl, struct berval *dn )
1316 {
1317         int rc = LDAP_INVALID_CREDENTIALS;
1318
1319         if ( DNX509PeerNormalizeCertMap != NULL )
1320                 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1321
1322         if ( rc != LDAP_SUCCESS ) {
1323                 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1324                         (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
1325         }
1326
1327         return rc;
1328 }
1329 #endif