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