]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
More for #5057 - reject duplicate AVAs
[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-2007 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; in case of multiple
224  * occurrences of the same attributeType, sort values in byte order
225  * (use memcmp, which implies alphabetical order in case of IA5 value;
226  * this should guarantee the repeatability of the operation).
227  *
228  * Note: the sorting can be slightly improved by sorting first
229  * by attribute type length, then by alphabetical order.
230  *
231  * uses an insertion sort; should be fine since the number of AVAs in
232  * a RDN should be limited.
233  */
234 static int
235 AVA_Sort( LDAPRDN rdn, int nAVAs )
236 {
237         LDAPAVA *ava_i;
238         int             i;
239
240         assert( rdn != NULL );
241
242         for ( i = 1; i < nAVAs; i++ ) {
243                 LDAPAVA *ava_j;
244                 int j;
245
246                 ava_i = rdn[ i ];
247                 for ( j = i-1; j >=0; j-- ) {
248                         int a;
249
250                         ava_j = rdn[ j ];
251                         a = strcmp( ava_i->la_attr.bv_val, ava_j->la_attr.bv_val );
252
253                         if ( a == 0 ) {
254                                 int             d;
255
256                                 d = ava_i->la_value.bv_len - ava_j->la_value.bv_len;
257
258                                 a = memcmp( ava_i->la_value.bv_val, 
259                                                 ava_j->la_value.bv_val,
260                                                 d <= 0 ? ava_i->la_value.bv_len 
261                                                         : ava_j->la_value.bv_len );
262
263                                 if ( a == 0 ) {
264                                         a = d;
265                                 }
266                         }
267                         /* Duplicates are not allowed */
268                         if ( a == 0 )
269                                 return LDAP_INVALID_DN_SYNTAX;
270
271                         if ( a > 0 )
272                                 break;
273
274                         rdn[ j+1 ] = rdn[ j ];
275                 }
276                 rdn[ j+1 ] = ava_i;
277         }
278         return LDAP_SUCCESS;
279 }
280
281 static int
282 LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
283 {
284
285         int rc, iAVA, do_sort = 0;
286
287         for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
288                 LDAPAVA                 *ava = rdn[ iAVA ];
289                 AttributeDescription    *ad;
290                 slap_syntax_validate_func *validf = NULL;
291                 slap_mr_normalize_func *normf = NULL;
292                 slap_syntax_transform_func *transf = NULL;
293                 MatchingRule *mr = NULL;
294                 struct berval           bv = BER_BVNULL;
295
296                 assert( ava != NULL );
297
298                 if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
299                         const char      *text = NULL;
300
301                         rc = slap_bv2ad( &ava->la_attr, &ad, &text );
302                         if ( rc != LDAP_SUCCESS ) {
303                                 rc = slap_bv2undef_ad( &ava->la_attr,
304                                         &ad, &text,
305                                         SLAP_AD_PROXIED|slap_DN_strict );
306                                 if ( rc != LDAP_SUCCESS ) {
307                                         return LDAP_INVALID_SYNTAX;
308                                 }
309                         }
310                         
311                         ava->la_private = ( void * )ad;
312                         do_sort = 1;
313                 }
314
315                 /* 
316                  * Replace attr oid/name with the canonical name
317                  */
318                 ava->la_attr = ad->ad_cname;
319
320                 if( ava->la_flags & LDAP_AVA_BINARY ) {
321                         if( ava->la_value.bv_len == 0 ) {
322                                 /* BER encoding is empty */
323                                 return LDAP_INVALID_SYNTAX;
324                         }
325
326                         /* Do not allow X-ORDERED 'VALUES' naming attributes */
327                 } else if( ad->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
328                         return LDAP_INVALID_SYNTAX;
329
330                         /* AVA is binary encoded, don't muck with it */
331                 } else if( flags & SLAP_LDAPDN_PRETTY ) {
332                         transf = ad->ad_type->sat_syntax->ssyn_pretty;
333                         if( !transf ) {
334                                 validf = ad->ad_type->sat_syntax->ssyn_validate;
335                         }
336                 } else { /* normalization */
337                         validf = ad->ad_type->sat_syntax->ssyn_validate;
338                         mr = ad->ad_type->sat_equality;
339                         if( mr && (!( mr->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))) {
340                                 normf = mr->smr_normalize;
341                         }
342                 }
343
344                 if ( validf ) {
345                         /* validate value before normalization */
346                         rc = ( *validf )( ad->ad_type->sat_syntax,
347                                 ava->la_value.bv_len
348                                         ? &ava->la_value
349                                         : (struct berval *) &slap_empty_bv );
350
351                         if ( rc != LDAP_SUCCESS ) {
352                                 return LDAP_INVALID_SYNTAX;
353                         }
354                 }
355
356                 if ( transf ) {
357                         /*
358                          * transform value by pretty function
359                          *      if value is empty, use empty_bv
360                          */
361                         rc = ( *transf )( ad->ad_type->sat_syntax,
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                 if ( normf ) {
373                         /*
374                          * normalize value
375                          *      if value is empty, use empty_bv
376                          */
377                         rc = ( *normf )(
378                                 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
379                                 ad->ad_type->sat_syntax,
380                                 mr,
381                                 ava->la_value.bv_len
382                                         ? &ava->la_value
383                                         : (struct berval *) &slap_empty_bv,
384                                 &bv, ctx );
385                 
386                         if ( rc != LDAP_SUCCESS ) {
387                                 return LDAP_INVALID_SYNTAX;
388                         }
389                 }
390
391
392                 if( bv.bv_val ) {
393                         if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
394                                 ber_memfree_x( ava->la_value.bv_val, ctx );
395                         ava->la_value = bv;
396                         ava->la_flags |= LDAP_AVA_FREE_VALUE;
397                 }
398         }
399         rc = LDAP_SUCCESS;
400
401         if ( do_sort ) {
402                 rc = AVA_Sort( rdn, iAVA );
403         }
404
405         return rc;
406 }
407
408 /*
409  * In-place, schema-aware normalization / "pretty"ing of the
410  * structural representation of a distinguished name.
411  */
412 static int
413 LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
414 {
415         int             iRDN;
416         int             rc;
417
418         assert( dn != NULL );
419
420         for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
421                 rc = LDAPRDN_rewrite( dn[ iRDN ], flags, ctx );
422                 if ( rc != LDAP_SUCCESS ) {
423                         return rc;
424                 }
425         }
426
427         return LDAP_SUCCESS;
428 }
429
430 int
431 dnNormalize(
432     slap_mask_t use,
433     Syntax *syntax,
434     MatchingRule *mr,
435     struct berval *val,
436     struct berval *out,
437     void *ctx)
438 {
439         assert( val != NULL );
440         assert( out != NULL );
441
442         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
443
444         if ( val->bv_len != 0 ) {
445                 LDAPDN          dn = NULL;
446                 int             rc;
447
448                 /*
449                  * Go to structural representation
450                  */
451                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
452                 if ( rc != LDAP_SUCCESS ) {
453                         return LDAP_INVALID_SYNTAX;
454                 }
455
456                 assert( strlen( val->bv_val ) == val->bv_len );
457
458                 /*
459                  * Schema-aware rewrite
460                  */
461                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
462                         ldap_dnfree_x( dn, ctx );
463                         return LDAP_INVALID_SYNTAX;
464                 }
465
466                 /*
467                  * Back to string representation
468                  */
469                 rc = ldap_dn2bv_x( dn, out,
470                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
471
472                 ldap_dnfree_x( dn, ctx );
473
474                 if ( rc != LDAP_SUCCESS ) {
475                         return LDAP_INVALID_SYNTAX;
476                 }
477         } else {
478                 ber_dupbv_x( out, val, ctx );
479         }
480
481         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
482
483         return LDAP_SUCCESS;
484 }
485
486 int
487 rdnNormalize(
488     slap_mask_t use,
489     Syntax *syntax,
490     MatchingRule *mr,
491     struct berval *val,
492     struct berval *out,
493     void *ctx)
494 {
495         assert( val != NULL );
496         assert( out != NULL );
497
498         Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
499         if ( val->bv_len != 0 ) {
500                 LDAPRDN         rdn = NULL;
501                 int             rc;
502                 char*           p;
503
504                 /*
505                  * Go to structural representation
506                  */
507                 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
508                                         LDAP_DN_FORMAT_LDAP, ctx);
509
510                 if ( rc != LDAP_SUCCESS ) {
511                         return LDAP_INVALID_SYNTAX;
512                 }
513
514                 assert( strlen( val->bv_val ) == val->bv_len );
515
516                 /*
517                  * Schema-aware rewrite
518                  */
519                 if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
520                         ldap_rdnfree_x( rdn, ctx );
521                         return LDAP_INVALID_SYNTAX;
522                 }
523
524                 /*
525                  * Back to string representation
526                  */
527                 rc = ldap_rdn2bv_x( rdn, out,
528                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
529
530                 ldap_rdnfree_x( rdn, ctx );
531
532                 if ( rc != LDAP_SUCCESS ) {
533                         return LDAP_INVALID_SYNTAX;
534                 }
535         } else {
536                 ber_dupbv_x( out, val, ctx );
537         }
538
539         Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );
540
541         return LDAP_SUCCESS;
542 }
543
544 int
545 dnPretty(
546         Syntax *syntax,
547         struct berval *val,
548         struct berval *out,
549         void *ctx)
550 {
551         assert( val != NULL );
552         assert( out != NULL );
553
554         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
555
556         if ( val->bv_len == 0 ) {
557                 ber_dupbv_x( out, val, ctx );
558
559         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
560                 return LDAP_INVALID_SYNTAX;
561
562         } else {
563                 LDAPDN          dn = NULL;
564                 int             rc;
565
566                 /* FIXME: should be liberal in what we accept */
567                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
568                 if ( rc != LDAP_SUCCESS ) {
569                         return LDAP_INVALID_SYNTAX;
570                 }
571
572                 assert( strlen( val->bv_val ) == val->bv_len );
573
574                 /*
575                  * Schema-aware rewrite
576                  */
577                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
578                         ldap_dnfree_x( dn, ctx );
579                         return LDAP_INVALID_SYNTAX;
580                 }
581
582                 /* FIXME: not sure why the default isn't pretty */
583                 /* RE: the default is the form that is used as
584                  * an internal representation; the pretty form
585                  * is a variant */
586                 rc = ldap_dn2bv_x( dn, out,
587                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
588
589                 ldap_dnfree_x( dn, ctx );
590
591                 if ( rc != LDAP_SUCCESS ) {
592                         return LDAP_INVALID_SYNTAX;
593                 }
594         }
595
596         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
597
598         return LDAP_SUCCESS;
599 }
600
601 int
602 rdnPretty(
603         Syntax *syntax,
604         struct berval *val,
605         struct berval *out,
606         void *ctx)
607 {
608         assert( val != NULL );
609         assert( out != NULL );
610
611         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
612
613         if ( val->bv_len == 0 ) {
614                 ber_dupbv_x( out, val, ctx );
615
616         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
617                 return LDAP_INVALID_SYNTAX;
618
619         } else {
620                 LDAPRDN         rdn = NULL;
621                 int             rc;
622                 char*           p;
623
624                 /* FIXME: should be liberal in what we accept */
625                 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
626                                         LDAP_DN_FORMAT_LDAP, ctx);
627                 if ( rc != LDAP_SUCCESS ) {
628                         return LDAP_INVALID_SYNTAX;
629                 }
630
631                 assert( strlen( val->bv_val ) == val->bv_len );
632
633                 /*
634                  * Schema-aware rewrite
635                  */
636                 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
637                         ldap_rdnfree_x( rdn, ctx );
638                         return LDAP_INVALID_SYNTAX;
639                 }
640
641                 /* FIXME: not sure why the default isn't pretty */
642                 /* RE: the default is the form that is used as
643                  * an internal representation; the pretty form
644                  * is a variant */
645                 rc = ldap_rdn2bv_x( rdn, out,
646                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
647
648                 ldap_rdnfree_x( rdn, ctx );
649
650                 if ( rc != LDAP_SUCCESS ) {
651                         return LDAP_INVALID_SYNTAX;
652                 }
653         }
654
655         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
656
657         return LDAP_SUCCESS;
658 }
659
660
661 int
662 dnPrettyNormalDN(
663         Syntax *syntax,
664         struct berval *val,
665         LDAPDN *dn,
666         int flags,
667         void *ctx )
668 {
669         assert( val != NULL );
670         assert( dn != NULL );
671
672         Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
673                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
674                         val->bv_val, 0 );
675
676         if ( val->bv_len == 0 ) {
677                 return LDAP_SUCCESS;
678
679         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
680                 return LDAP_INVALID_SYNTAX;
681
682         } else {
683                 int             rc;
684
685                 /* FIXME: should be liberal in what we accept */
686                 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
687                 if ( rc != LDAP_SUCCESS ) {
688                         return LDAP_INVALID_SYNTAX;
689                 }
690
691                 assert( strlen( val->bv_val ) == val->bv_len );
692
693                 /*
694                  * Schema-aware rewrite
695                  */
696                 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
697                         ldap_dnfree_x( *dn, ctx );
698                         *dn = NULL;
699                         return LDAP_INVALID_SYNTAX;
700                 }
701         }
702
703         Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n", 
704                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
705                         0, 0 );
706
707         return LDAP_SUCCESS;
708 }
709
710 /*
711  * Combination of both dnPretty and dnNormalize
712  */
713 int
714 dnPrettyNormal(
715         Syntax *syntax,
716         struct berval *val,
717         struct berval *pretty,
718         struct berval *normal,
719         void *ctx)
720 {
721         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
722
723         assert( val != NULL );
724         assert( pretty != NULL );
725         assert( normal != NULL );
726
727         if ( val->bv_len == 0 ) {
728                 ber_dupbv_x( pretty, val, ctx );
729                 ber_dupbv_x( normal, val, ctx );
730
731         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
732                 /* too big */
733                 return LDAP_INVALID_SYNTAX;
734
735         } else {
736                 LDAPDN          dn = NULL;
737                 int             rc;
738
739                 pretty->bv_val = NULL;
740                 normal->bv_val = NULL;
741                 pretty->bv_len = 0;
742                 normal->bv_len = 0;
743
744                 /* FIXME: should be liberal in what we accept */
745                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
746                 if ( rc != LDAP_SUCCESS ) {
747                         return LDAP_INVALID_SYNTAX;
748                 }
749
750                 assert( strlen( val->bv_val ) == val->bv_len );
751
752                 /*
753                  * Schema-aware rewrite
754                  */
755                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
756                         ldap_dnfree_x( dn, ctx );
757                         return LDAP_INVALID_SYNTAX;
758                 }
759
760                 rc = ldap_dn2bv_x( dn, pretty,
761                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
762
763                 if ( rc != LDAP_SUCCESS ) {
764                         ldap_dnfree_x( dn, ctx );
765                         return LDAP_INVALID_SYNTAX;
766                 }
767
768                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
769                         ldap_dnfree_x( dn, ctx );
770                         ber_memfree_x( pretty->bv_val, ctx );
771                         pretty->bv_val = NULL;
772                         pretty->bv_len = 0;
773                         return LDAP_INVALID_SYNTAX;
774                 }
775
776                 rc = ldap_dn2bv_x( dn, normal,
777                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
778
779                 ldap_dnfree_x( dn, ctx );
780                 if ( rc != LDAP_SUCCESS ) {
781                         ber_memfree_x( pretty->bv_val, ctx );
782                         pretty->bv_val = NULL;
783                         pretty->bv_len = 0;
784                         return LDAP_INVALID_SYNTAX;
785                 }
786         }
787
788         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
789                 pretty->bv_val, normal->bv_val, 0 );
790
791         return LDAP_SUCCESS;
792 }
793
794 /*
795  * dnMatch routine
796  */
797 int
798 dnMatch(
799         int *matchp,
800         slap_mask_t flags,
801         Syntax *syntax,
802         MatchingRule *mr,
803         struct berval *value,
804         void *assertedValue )
805 {
806         int match;
807         struct berval *asserted = (struct berval *) assertedValue;
808
809         assert( matchp != NULL );
810         assert( value != NULL );
811         assert( assertedValue != NULL );
812         assert( !BER_BVISNULL( value ) );
813         assert( !BER_BVISNULL( asserted ) );
814         
815         match = value->bv_len - asserted->bv_len;
816
817         if ( match == 0 ) {
818                 match = memcmp( value->bv_val, asserted->bv_val, 
819                                 value->bv_len );
820         }
821
822         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
823                 match, value->bv_val, asserted->bv_val );
824
825         *matchp = match;
826         return LDAP_SUCCESS;
827 }
828
829 /*
830  * dnRelativeMatch routine
831  */
832 int
833 dnRelativeMatch(
834         int *matchp,
835         slap_mask_t flags,
836         Syntax *syntax,
837         MatchingRule *mr,
838         struct berval *value,
839         void *assertedValue )
840 {
841         int match;
842         struct berval *asserted = (struct berval *) assertedValue;
843
844         assert( matchp != NULL );
845         assert( value != NULL );
846         assert( assertedValue != NULL );
847         assert( !BER_BVISNULL( value ) );
848         assert( !BER_BVISNULL( asserted ) );
849
850         if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
851                 if( asserted->bv_len > value->bv_len ) {
852                         match = -1;
853                 } else if ( asserted->bv_len == value->bv_len ) {
854                         match = memcmp( value->bv_val, asserted->bv_val, 
855                                 value->bv_len );
856                 } else {
857                         if( DN_SEPARATOR(
858                                 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
859                         {
860                                 match = memcmp(
861                                         &value->bv_val[value->bv_len - asserted->bv_len],
862                                         asserted->bv_val, 
863                                         asserted->bv_len );
864                         } else {
865                                 match = 1;
866                         }
867                 }
868
869                 *matchp = match;
870                 return LDAP_SUCCESS;
871         }
872
873         if( mr == slap_schema.si_mr_dnSuperiorMatch ) {
874                 asserted = value;
875                 value = (struct berval *) assertedValue;
876                 mr = slap_schema.si_mr_dnSubordinateMatch;
877         }
878
879         if( mr == slap_schema.si_mr_dnSubordinateMatch ) {
880                 if( asserted->bv_len >= value->bv_len ) {
881                         match = -1;
882                 } else {
883                         if( DN_SEPARATOR(
884                                 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
885                         {
886                                 match = memcmp(
887                                         &value->bv_val[value->bv_len - asserted->bv_len],
888                                         asserted->bv_val, 
889                                         asserted->bv_len );
890                         } else {
891                                 match = 1;
892                         }
893                 }
894
895                 *matchp = match;
896                 return LDAP_SUCCESS;
897         }
898
899         if( mr == slap_schema.si_mr_dnOneLevelMatch ) {
900                 if( asserted->bv_len >= value->bv_len ) {
901                         match = -1;
902                 } else {
903                         if( DN_SEPARATOR(
904                                 value->bv_val[value->bv_len - asserted->bv_len - 1] ))
905                         {
906                                 match = memcmp(
907                                         &value->bv_val[value->bv_len - asserted->bv_len],
908                                         asserted->bv_val, 
909                                         asserted->bv_len );
910
911                                 if( !match ) {
912                                         struct berval rdn;
913                                         rdn.bv_val = value->bv_val;
914                                         rdn.bv_len = value->bv_len - asserted->bv_len - 1;
915                                         match = dnIsOneLevelRDN( &rdn ) ? 0 : 1;
916                                 }
917                         } else {
918                                 match = 1;
919                         }
920                 }
921
922                 *matchp = match;
923                 return LDAP_SUCCESS;
924         }
925
926         /* should not be reachable */
927         assert( 0 );
928         return LDAP_OTHER;
929 }
930
931 int
932 rdnMatch(
933         int *matchp,
934         slap_mask_t flags,
935         Syntax *syntax,
936         MatchingRule *mr,
937         struct berval *value,
938         void *assertedValue )
939 {
940         int match;
941         struct berval *asserted = (struct berval *) assertedValue;
942
943         assert( matchp != NULL );
944         assert( value != NULL );
945         assert( assertedValue != NULL );
946         
947         match = value->bv_len - asserted->bv_len;
948
949         if ( match == 0 ) {
950                 match = memcmp( value->bv_val, asserted->bv_val, 
951                                 value->bv_len );
952         }
953
954         Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
955                 match, value->bv_val, asserted->bv_val );
956
957         *matchp = match;
958         return LDAP_SUCCESS;
959 }
960
961
962 /*
963  * dnParent - dn's parent, in-place
964  * note: the incoming dn is assumed to be normalized/prettyfied,
965  * so that escaped rdn/ava separators are in '\'+hexpair form
966  *
967  * note: "dn" and "pdn" can point to the same berval;
968  * beware that, in this case, the pointer to the original buffer
969  * will get lost.
970  */
971 void
972 dnParent( 
973         struct berval   *dn, 
974         struct berval   *pdn )
975 {
976         char    *p;
977
978         p = ber_bvchr( dn, ',' );
979
980         /* one-level dn */
981         if ( p == NULL ) {
982                 pdn->bv_len = 0;
983                 pdn->bv_val = dn->bv_val + dn->bv_len;
984                 return;
985         }
986
987         assert( DN_SEPARATOR( p[ 0 ] ) );
988         p++;
989
990         assert( ATTR_LEADCHAR( p[ 0 ] ) );
991         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
992         pdn->bv_val = p;
993
994         return;
995 }
996
997 /*
998  * dnRdn - dn's rdn, in-place
999  * note: the incoming dn is assumed to be normalized/prettyfied,
1000  * so that escaped rdn/ava separators are in '\'+hexpair form
1001  */
1002 void
1003 dnRdn( 
1004         struct berval   *dn, 
1005         struct berval   *rdn )
1006 {
1007         char    *p;
1008
1009         *rdn = *dn;
1010         p = ber_bvchr( dn, ',' );
1011
1012         /* one-level dn */
1013         if ( p == NULL ) {
1014                 return;
1015         }
1016
1017         assert( DN_SEPARATOR( p[ 0 ] ) );
1018         assert( ATTR_LEADCHAR( p[ 1 ] ) );
1019         rdn->bv_len = p - dn->bv_val;
1020
1021         return;
1022 }
1023
1024 int
1025 dnExtractRdn( 
1026         struct berval   *dn, 
1027         struct berval   *rdn,
1028         void *ctx )
1029 {
1030         LDAPRDN         tmpRDN;
1031         const char      *p;
1032         int             rc;
1033
1034         assert( dn != NULL );
1035         assert( rdn != NULL );
1036
1037         if( dn->bv_len == 0 ) {
1038                 return LDAP_OTHER;
1039         }
1040
1041         rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1042         if ( rc != LDAP_SUCCESS ) {
1043                 return rc;
1044         }
1045
1046         rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY,
1047                 ctx );
1048
1049         ldap_rdnfree_x( tmpRDN, ctx );
1050         return rc;
1051 }
1052
1053 /*
1054  * We can assume the input is a prettied or normalized DN
1055  */
1056 ber_len_t
1057 dn_rdnlen(
1058         Backend         *be,
1059         struct berval   *dn_in )
1060 {
1061         const char      *p;
1062
1063         assert( dn_in != NULL );
1064
1065         if ( dn_in == NULL ) {
1066                 return 0;
1067         }
1068
1069         if ( !dn_in->bv_len ) {
1070                 return 0;
1071         }
1072
1073         if ( be != NULL && be_issuffix( be, dn_in ) ) {
1074                 return 0;
1075         }
1076
1077         p = ber_bvchr( dn_in, ',' );
1078
1079         return p ? p - dn_in->bv_val : dn_in->bv_len;
1080 }
1081
1082
1083 /* rdnValidate:
1084  *
1085  * LDAP_SUCCESS if rdn is a legal rdn;
1086  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1087  */
1088 int
1089 rdn_validate( struct berval *rdn )
1090 {
1091 #if 1
1092         /* Major cheat!
1093          * input is a pretty or normalized DN
1094          * hence, we can just search for ','
1095          */
1096         if( rdn == NULL || rdn->bv_len == 0 ||
1097                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1098         {
1099                 return LDAP_INVALID_SYNTAX;
1100         }
1101         return ber_bvchr( rdn, ',' ) == NULL
1102                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1103
1104 #else
1105         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
1106         const char      *p;
1107         int             rc;
1108
1109         /*
1110          * must be non-empty
1111          */
1112         if ( rdn == NULL || rdn == '\0' ) {
1113                 return 0;
1114         }
1115
1116         /*
1117          * must be parsable
1118          */
1119         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1120         if ( rc != LDAP_SUCCESS ) {
1121                 return 0;
1122         }
1123
1124         /*
1125          * Must be one-level
1126          */
1127         if ( p[ 0 ] != '\0' ) {
1128                 return 0;
1129         }
1130
1131         /*
1132          * Schema-aware validate
1133          */
1134         if ( rc == LDAP_SUCCESS ) {
1135                 rc = LDAPDN_validate( DN );
1136         }
1137         ldap_rdnfree( RDN );
1138
1139         /*
1140          * Must validate (there's a repeated parsing ...)
1141          */
1142         return ( rc == LDAP_SUCCESS );
1143 #endif
1144 }
1145
1146
1147 /* build_new_dn:
1148  *
1149  * Used by back-bdb back_modrdn to create the new dn of entries being
1150  * renamed.
1151  *
1152  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1153  */
1154
1155 void
1156 build_new_dn( struct berval * new_dn,
1157         struct berval * parent_dn,
1158         struct berval * newrdn,
1159         void *memctx )
1160 {
1161         char *ptr;
1162
1163         if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1164                 ber_dupbv_x( new_dn, newrdn, memctx );
1165                 return;
1166         }
1167
1168         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1169         new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1170
1171         ptr = lutil_strncopy( new_dn->bv_val, newrdn->bv_val, newrdn->bv_len );
1172         *ptr++ = ',';
1173         strcpy( ptr, parent_dn->bv_val );
1174 }
1175
1176
1177 /*
1178  * dnIsSuffix - tells whether suffix is a suffix of dn.
1179  * Both dn and suffix must be normalized.
1180  */
1181 int
1182 dnIsSuffix(
1183         const struct berval *dn,
1184         const struct berval *suffix )
1185 {
1186         int     d = dn->bv_len - suffix->bv_len;
1187
1188         assert( dn != NULL );
1189         assert( suffix != NULL );
1190
1191         /* empty suffix matches any dn */
1192         if ( suffix->bv_len == 0 ) {
1193                 return 1;
1194         }
1195
1196         /* suffix longer than dn */
1197         if ( d < 0 ) {
1198                 return 0;
1199         }
1200
1201         /* no rdn separator or escaped rdn separator */
1202         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1203                 return 0;
1204         }
1205
1206         /* no possible match or malformed dn */
1207         if ( d == 1 ) {
1208                 return 0;
1209         }
1210
1211         /* compare */
1212         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1213 }
1214
1215 int
1216 dnIsOneLevelRDN( struct berval *rdn )
1217 {
1218         ber_len_t       len = rdn->bv_len;
1219         for ( ; len--; ) {
1220                 if ( DN_SEPARATOR( rdn->bv_val[ len ] ) ) {
1221                         return 0;
1222                 }
1223         }
1224
1225         return 1;
1226 }
1227
1228 #ifdef HAVE_TLS
1229 static SLAP_CERT_MAP_FN *DNX509PeerNormalizeCertMap = NULL;
1230 #endif
1231
1232 int register_certificate_map_function(SLAP_CERT_MAP_FN *fn)
1233 {
1234 #ifdef HAVE_TLS
1235         if ( DNX509PeerNormalizeCertMap == NULL ) {
1236                 DNX509PeerNormalizeCertMap = fn;
1237                 return 0;
1238         }
1239 #endif
1240
1241         return -1;
1242 }
1243
1244 /*
1245  * Convert an X.509 DN into a normalized LDAP DN
1246  */
1247 int
1248 dnX509normalize( void *x509_name, struct berval *out )
1249 {
1250         /* Invoke the LDAP library's converter with our schema-rewriter */
1251         int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1252
1253         Debug( LDAP_DEBUG_TRACE,
1254                 "dnX509Normalize: <%s> (%d)\n",
1255                 BER_BVISNULL( out ) ? "(null)" : out->bv_val, rc, 0 );
1256
1257         return rc;
1258 }
1259
1260 #ifdef HAVE_TLS
1261 /*
1262  * Get the TLS session's peer's DN into a normalized LDAP DN
1263  */
1264 int
1265 dnX509peerNormalize( void *ssl, struct berval *dn )
1266 {
1267         int rc = LDAP_INVALID_CREDENTIALS;
1268
1269         if ( DNX509PeerNormalizeCertMap != NULL )
1270                 rc = (*DNX509PeerNormalizeCertMap)( ssl, dn );
1271
1272         if ( rc != LDAP_SUCCESS ) {
1273                 rc = ldap_pvt_tls_get_peer_dn( ssl, dn,
1274                         (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
1275         }
1276
1277         return rc;
1278 }
1279 #endif