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