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