]> git.sur5r.net Git - openldap/blob - servers/slapd/dn.c
44528fa2f63271998e13026268bcd5bc3dc4fb24
[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 #ifdef NEW_LOGGING
690         LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
691 #else
692         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
693 #endif
694
695         if ( val->bv_len == 0 ) {
696                 ber_dupbv_x( out, val, ctx );
697
698         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
699                 return LDAP_INVALID_SYNTAX;
700
701         } else {
702                 LDAPDN          dn = NULL;
703                 int             rc;
704
705                 /* FIXME: should be liberal in what we accept */
706                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
707                 if ( rc != LDAP_SUCCESS ) {
708                         return LDAP_INVALID_SYNTAX;
709                 }
710
711                 assert( strlen( val->bv_val ) == val->bv_len );
712
713                 /*
714                  * Schema-aware rewrite
715                  */
716                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
717                         ldap_dnfree_x( dn, ctx );
718                         return LDAP_INVALID_SYNTAX;
719                 }
720
721                 /* FIXME: not sure why the default isn't pretty */
722                 /* RE: the default is the form that is used as
723                  * an internal representation; the pretty form
724                  * is a variant */
725                 rc = ldap_dn2bv_x( dn, out,
726                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
727
728                 ldap_dnfree_x( dn, ctx );
729
730                 if ( rc != LDAP_SUCCESS ) {
731                         return LDAP_INVALID_SYNTAX;
732                 }
733         }
734
735 #ifdef NEW_LOGGING
736         LDAP_LOG( OPERATION, ARGS, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
737 #else
738         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
739 #endif
740
741         return LDAP_SUCCESS;
742 }
743
744 int
745 rdnPretty(
746         Syntax *syntax,
747         struct berval *val,
748         struct berval *out,
749         void *ctx)
750 {
751         assert( val );
752         assert( out );
753
754 #ifdef NEW_LOGGING
755         LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
756 #else
757         Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
758 #endif
759
760         if ( val->bv_len == 0 ) {
761                 ber_dupbv_x( out, val, ctx );
762
763         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
764                 return LDAP_INVALID_SYNTAX;
765
766         } else {
767                 LDAPRDN         rdn = NULL;
768                 int             rc;
769                 char*           p;
770
771                 /* FIXME: should be liberal in what we accept */
772                 rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
773                                         LDAP_DN_FORMAT_LDAP, ctx);
774                 if ( rc != LDAP_SUCCESS ) {
775                         return LDAP_INVALID_SYNTAX;
776                 }
777
778                 assert( strlen( val->bv_val ) == val->bv_len );
779
780                 /*
781                  * Schema-aware rewrite
782                  */
783                 if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
784                         ldap_rdnfree_x( rdn, ctx );
785                         return LDAP_INVALID_SYNTAX;
786                 }
787
788                 /* FIXME: not sure why the default isn't pretty */
789                 /* RE: the default is the form that is used as
790                  * an internal representation; the pretty form
791                  * is a variant */
792                 rc = ldap_rdn2bv_x( rdn, out,
793                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
794
795                 ldap_rdnfree_x( rdn, ctx );
796
797                 if ( rc != LDAP_SUCCESS ) {
798                         return LDAP_INVALID_SYNTAX;
799                 }
800         }
801
802 #ifdef NEW_LOGGING
803         LDAP_LOG( OPERATION, ARGS, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
804 #else
805         Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
806 #endif
807
808         return LDAP_SUCCESS;
809 }
810
811
812 int
813 dnPrettyNormalDN(
814         Syntax *syntax,
815         struct berval *val,
816         LDAPDN *dn,
817         int flags,
818         void *ctx )
819 {
820         assert( val );
821         assert( dn );
822
823 #ifdef NEW_LOGGING
824         LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n", 
825                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
826                         val->bv_val, 0 );
827 #else
828         Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
829                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
830                         val->bv_val, 0 );
831 #endif
832
833         if ( val->bv_len == 0 ) {
834                 return LDAP_SUCCESS;
835
836         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
837                 return LDAP_INVALID_SYNTAX;
838
839         } else {
840                 int             rc;
841
842                 /* FIXME: should be liberal in what we accept */
843                 rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
844                 if ( rc != LDAP_SUCCESS ) {
845                         return LDAP_INVALID_SYNTAX;
846                 }
847
848                 assert( strlen( val->bv_val ) == val->bv_len );
849
850                 /*
851                  * Schema-aware rewrite
852                  */
853                 if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
854                         ldap_dnfree_x( *dn, ctx );
855                         *dn = NULL;
856                         return LDAP_INVALID_SYNTAX;
857                 }
858         }
859
860         Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n", 
861                         flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
862                         0, 0 );
863
864         return LDAP_SUCCESS;
865 }
866
867 /*
868  * Combination of both dnPretty and dnNormalize
869  */
870 int
871 dnPrettyNormal(
872         Syntax *syntax,
873         struct berval *val,
874         struct berval *pretty,
875         struct berval *normal,
876         void *ctx)
877 {
878 #ifdef NEW_LOGGING
879         LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
880 #else
881         Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
882 #endif
883
884         assert( val );
885         assert( pretty );
886         assert( normal );
887
888         if ( val->bv_len == 0 ) {
889                 ber_dupbv_x( pretty, val, ctx );
890                 ber_dupbv_x( normal, val, ctx );
891
892         } else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
893                 /* too big */
894                 return LDAP_INVALID_SYNTAX;
895
896         } else {
897                 LDAPDN          dn = NULL;
898                 int             rc;
899
900                 pretty->bv_val = NULL;
901                 normal->bv_val = NULL;
902                 pretty->bv_len = 0;
903                 normal->bv_len = 0;
904
905                 /* FIXME: should be liberal in what we accept */
906                 rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
907                 if ( rc != LDAP_SUCCESS ) {
908                         return LDAP_INVALID_SYNTAX;
909                 }
910
911                 assert( strlen( val->bv_val ) == val->bv_len );
912
913                 /*
914                  * Schema-aware rewrite
915                  */
916                 if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
917                         ldap_dnfree_x( dn, ctx );
918                         return LDAP_INVALID_SYNTAX;
919                 }
920
921                 rc = ldap_dn2bv_x( dn, pretty,
922                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
923
924                 if ( rc != LDAP_SUCCESS ) {
925                         ldap_dnfree_x( dn, ctx );
926                         return LDAP_INVALID_SYNTAX;
927                 }
928
929                 if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
930                         ldap_dnfree_x( dn, ctx );
931                         ber_memfree_x( pretty->bv_val, ctx );
932                         pretty->bv_val = NULL;
933                         pretty->bv_len = 0;
934                         return LDAP_INVALID_SYNTAX;
935                 }
936
937                 rc = ldap_dn2bv_x( dn, normal,
938                         LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
939
940                 ldap_dnfree_x( dn, ctx );
941                 if ( rc != LDAP_SUCCESS ) {
942                         ber_memfree_x( pretty->bv_val, ctx );
943                         pretty->bv_val = NULL;
944                         pretty->bv_len = 0;
945                         return LDAP_INVALID_SYNTAX;
946                 }
947         }
948
949 #ifdef NEW_LOGGING
950         LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
951                 pretty->bv_val, normal->bv_val, 0  );
952 #else
953         Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
954                 pretty->bv_val, normal->bv_val, 0 );
955 #endif
956
957         return LDAP_SUCCESS;
958 }
959
960 /*
961  * dnMatch routine
962  */
963 int
964 dnMatch(
965         int *matchp,
966         slap_mask_t flags,
967         Syntax *syntax,
968         MatchingRule *mr,
969         struct berval *value,
970         void *assertedValue )
971 {
972         int match;
973         struct berval *asserted = (struct berval *) assertedValue;
974
975         assert( matchp );
976         assert( value );
977         assert( assertedValue );
978         assert( !BER_BVISNULL( value ) );
979         assert( !BER_BVISNULL( asserted ) );
980         
981         match = value->bv_len - asserted->bv_len;
982
983         if ( match == 0 ) {
984                 match = memcmp( value->bv_val, asserted->bv_val, 
985                                 value->bv_len );
986         }
987
988 #ifdef NEW_LOGGING
989         LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n    %s\n    %s\n", 
990                 match, value->bv_val, asserted->bv_val  );
991 #else
992         Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
993                 match, value->bv_val, asserted->bv_val );
994 #endif
995
996         *matchp = match;
997         return( LDAP_SUCCESS );
998 }
999
1000 int
1001 rdnMatch(
1002         int *matchp,
1003         slap_mask_t flags,
1004         Syntax *syntax,
1005         MatchingRule *mr,
1006         struct berval *value,
1007         void *assertedValue )
1008 {
1009         int match;
1010         struct berval *asserted = (struct berval *) assertedValue;
1011
1012         assert( matchp );
1013         assert( value );
1014         assert( assertedValue );
1015         
1016         match = value->bv_len - asserted->bv_len;
1017
1018         if ( match == 0 ) {
1019                 match = memcmp( value->bv_val, asserted->bv_val, 
1020                                 value->bv_len );
1021         }
1022
1023 #ifdef NEW_LOGGING
1024         LDAP_LOG( CONFIG, ENTRY, "rdnMatch: %d\n    %s\n    %s\n", 
1025                 match, value->bv_val, asserted->bv_val  );
1026 #else
1027         Debug( LDAP_DEBUG_ARGS, "rdnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
1028                 match, value->bv_val, asserted->bv_val );
1029 #endif
1030
1031         *matchp = match;
1032
1033         return( LDAP_SUCCESS );
1034 }
1035
1036
1037 /*
1038  * dnParent - dn's parent, in-place
1039  * note: the incoming dn is assumed to be normalized/prettyfied,
1040  * so that escaped rdn/ava separators are in '\'+hexpair form
1041  */
1042 void
1043 dnParent( 
1044         struct berval   *dn, 
1045         struct berval   *pdn )
1046 {
1047         char    *p;
1048
1049         p = strchr( dn->bv_val, ',' );
1050
1051         /* one-level dn */
1052         if ( p == NULL ) {
1053                 pdn->bv_len = 0;
1054                 pdn->bv_val = dn->bv_val + dn->bv_len;
1055                 return;
1056         }
1057
1058         assert( DN_SEPARATOR( p[ 0 ] ) );
1059         p++;
1060
1061         assert( ATTR_LEADCHAR( p[ 0 ] ) );
1062         pdn->bv_val = p;
1063         pdn->bv_len = dn->bv_len - (p - dn->bv_val);
1064
1065         return;
1066 }
1067
1068 int
1069 dnExtractRdn( 
1070         struct berval   *dn, 
1071         struct berval   *rdn,
1072         void *ctx )
1073 {
1074         LDAPRDN         tmpRDN;
1075         const char      *p;
1076         int             rc;
1077
1078         assert( dn );
1079         assert( rdn );
1080
1081         if( dn->bv_len == 0 ) {
1082                 return LDAP_OTHER;
1083         }
1084
1085         rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
1086         if ( rc != LDAP_SUCCESS ) {
1087                 return rc;
1088         }
1089
1090         rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
1091
1092         ldap_rdnfree_x( tmpRDN, ctx );
1093         if ( rc != LDAP_SUCCESS ) {
1094                 return rc;
1095         }
1096
1097         return LDAP_SUCCESS;
1098 }
1099
1100 /*
1101  * We can assume the input is a prettied or normalized DN
1102  */
1103 int 
1104 dn_rdnlen(
1105         Backend         *be,
1106         struct berval   *dn_in )
1107 {
1108         const char      *p;
1109
1110         assert( dn_in );
1111
1112         if ( dn_in == NULL ) {
1113                 return 0;
1114         }
1115
1116         if ( !dn_in->bv_len ) {
1117                 return 0;
1118         }
1119
1120         if ( be != NULL && be_issuffix( be, dn_in ) ) {
1121                 return 0;
1122         }
1123
1124         p = strchr( dn_in->bv_val, ',' );
1125
1126         return p ? p - dn_in->bv_val : dn_in->bv_len;
1127 }
1128
1129
1130 /* rdnValidate:
1131  *
1132  * LDAP_SUCCESS if rdn is a legal rdn;
1133  * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
1134  */
1135 int
1136 rdn_validate( struct berval *rdn )
1137 {
1138 #if 1
1139         /* Major cheat!
1140          * input is a pretty or normalized DN
1141          * hence, we can just search for ','
1142          */
1143         if( rdn == NULL || rdn->bv_len == 0 ||
1144                 rdn->bv_len > SLAP_LDAPDN_MAXLEN )
1145         {
1146                 return LDAP_INVALID_SYNTAX;
1147         }
1148         return strchr( rdn->bv_val, ',' ) == NULL
1149                 ? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;
1150
1151 #else
1152         LDAPRDN         *RDN, **DN[ 2 ] = { &RDN, NULL };
1153         const char      *p;
1154         int             rc;
1155
1156         /*
1157          * must be non-empty
1158          */
1159         if ( rdn == NULL || rdn == '\0' ) {
1160                 return 0;
1161         }
1162
1163         /*
1164          * must be parsable
1165          */
1166         rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
1167         if ( rc != LDAP_SUCCESS ) {
1168                 return 0;
1169         }
1170
1171         /*
1172          * Must be one-level
1173          */
1174         if ( p[ 0 ] != '\0' ) {
1175                 return 0;
1176         }
1177
1178         /*
1179          * Schema-aware validate
1180          */
1181         if ( rc == LDAP_SUCCESS ) {
1182                 rc = LDAPDN_validate( DN );
1183         }
1184         ldap_rdnfree( RDN );
1185
1186         /*
1187          * Must validate (there's a repeated parsing ...)
1188          */
1189         return ( rc == LDAP_SUCCESS );
1190 #endif
1191 }
1192
1193
1194 /* build_new_dn:
1195  *
1196  * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
1197  * renamed.
1198  *
1199  * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
1200  */
1201
1202 void
1203 build_new_dn( struct berval * new_dn,
1204         struct berval * parent_dn,
1205         struct berval * newrdn,
1206         void *memctx )
1207 {
1208         char *ptr;
1209
1210         if ( parent_dn == NULL || parent_dn->bv_len == 0 ) {
1211                 ber_dupbv( new_dn, newrdn );
1212                 return;
1213         }
1214
1215         new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
1216         new_dn->bv_val = (char *) slap_sl_malloc( new_dn->bv_len + 1, memctx );
1217
1218         ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
1219         *ptr++ = ',';
1220         strcpy( ptr, parent_dn->bv_val );
1221 }
1222
1223
1224 /*
1225  * dnIsSuffix - tells whether suffix is a suffix of dn.
1226  * Both dn and suffix must be normalized.
1227  */
1228 int
1229 dnIsSuffix(
1230         const struct berval *dn,
1231         const struct berval *suffix )
1232 {
1233         int     d = dn->bv_len - suffix->bv_len;
1234
1235         assert( dn );
1236         assert( suffix );
1237
1238         /* empty suffix matches any dn */
1239         if ( suffix->bv_len == 0 ) {
1240                 return 1;
1241         }
1242
1243         /* suffix longer than dn */
1244         if ( d < 0 ) {
1245                 return 0;
1246         }
1247
1248         /* no rdn separator or escaped rdn separator */
1249         if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
1250                 return 0;
1251         }
1252
1253         /* no possible match or malformed dn */
1254         if ( d == 1 ) {
1255                 return 0;
1256         }
1257
1258         /* compare */
1259         return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
1260 }
1261
1262 #ifdef HAVE_TLS
1263 /*
1264  * Convert an X.509 DN into a normalized LDAP DN
1265  */
1266 int
1267 dnX509normalize( void *x509_name, struct berval *out )
1268 {
1269         /* Invoke the LDAP library's converter with our schema-rewriter */
1270         int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );
1271
1272         Debug( LDAP_DEBUG_TRACE,
1273                 "dnX509Normalize: <%s>\n", out->bv_val, 0, 0 );
1274
1275         return rc;
1276 }
1277
1278 /*
1279  * Get the TLS session's peer's DN into a normalized LDAP DN
1280  */
1281 int
1282 dnX509peerNormalize( void *ssl, struct berval *dn )
1283 {
1284
1285         return ldap_pvt_tls_get_peer_dn( ssl, dn,
1286                 (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
1287 }
1288 #endif