dn.c 27.8 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* dn.c - routines for dealing with distinguished names */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 1998-2004 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27

#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
28
29
30
31
32
33
34
35

#include <stdio.h>

#include <ac/ctype.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
36
#include "slap.h"
37
38
#include "lutil.h"

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * The DN syntax-related functions take advantage of the dn representation
 * handling functions ldap_str2dn/ldap_dn2str.  The latter are not schema-
 * aware, so the attributes and their values need be validated (and possibly
 * normalized).  In the current implementation the required validation/nor-
 * malization/"pretty"ing are done on newly created DN structural represen-
 * tations; however the idea is to move towards DN handling in structural
 * representation instead of the current string representation.  To this
 * purpose, we need to do only the required operations and keep track of
 * what has been done to minimize their impact on performances.
 *
 * Developers are strongly encouraged to use this feature, to speed-up
 * its stabilization.
 */

#define	AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
static int
LDAPRDN_validate( LDAPRDN rdn )
{
	int		iAVA;
	int 		rc;

	assert( rdn );

	for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
		LDAPAVA			*ava = rdn[ iAVA ];
		AttributeDescription	*ad;
		slap_syntax_validate_func *validate = NULL;

		assert( ava );
		
		if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
			const char	*text = NULL;

			rc = slap_bv2ad( &ava->la_attr, &ad, &text );
			if ( rc != LDAP_SUCCESS ) {
				return LDAP_INVALID_SYNTAX;
			}

			ava->la_private = ( void * )ad;
		}

		/* 
		 * Replace attr oid/name with the canonical name
		 */
		ava->la_attr = ad->ad_cname;

		validate = ad->ad_type->sat_syntax->ssyn_validate;

		if ( validate ) {
			/*
		 	 * validate value by validate function
			 */
			rc = ( *validate )( ad->ad_type->sat_syntax,
				&ava->la_value );
			
			if ( rc != LDAP_SUCCESS ) {
				return LDAP_INVALID_SYNTAX;
			}
		}
	}
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
101
102

	return LDAP_SUCCESS;
103
104
}

105
106
107
108
109
/*
 * In-place, schema-aware validation of the
 * structural representation of a distinguished name.
 */
static int
Howard Chu's avatar
Howard Chu committed
110
LDAPDN_validate( LDAPDN dn )
111
112
113
114
115
116
{
	int 		iRDN;
	int 		rc;

	assert( dn );

Howard Chu's avatar
Howard Chu committed
117
118
	for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
		LDAPRDN		rdn = dn[ iRDN ];
119
120
121
122
		int		iAVA;

		assert( rdn );

Howard Chu's avatar
Howard Chu committed
123
124
		for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
			LDAPAVA			*ava = rdn[ iAVA ];
125
126
127
128
129
130
131
132
			AttributeDescription	*ad;
			slap_syntax_validate_func *validate = NULL;

			assert( ava );
			
			if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
				const char	*text = NULL;

133
				rc = slap_bv2ad( &ava->la_attr, &ad, &text );
134
135
136
137
138
139
140
141
142
143
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}

				ava->la_private = ( void * )ad;
			}

			/* 
			 * Replace attr oid/name with the canonical name
			 */
Howard Chu's avatar
Howard Chu committed
144
			ava->la_attr = ad->ad_cname;
145
146
147
148
149
150
151
152

			validate = ad->ad_type->sat_syntax->ssyn_validate;

			if ( validate ) {
				/*
			 	 * validate value by validate function
				 */
				rc = ( *validate )( ad->ad_type->sat_syntax,
153
					&ava->la_value );
154
155
156
157
158
159
160
161
162
163
164
			
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}
		}
	}

	return LDAP_SUCCESS;
}

165
/*
166
 * dn validate routine
167
 */
168
169
170
171
int
dnValidate(
	Syntax *syntax,
	struct berval *in )
172
{
173
	int		rc;
Howard Chu's avatar
Howard Chu committed
174
	LDAPDN		dn = NULL;
175
176
177
178

	assert( in );

	if ( in->bv_len == 0 ) {
179
180
181
182
		return LDAP_SUCCESS;

	} else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
		return LDAP_INVALID_SYNTAX;
183
184
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
185
	rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
186
187
188
189
190
	if ( rc != LDAP_SUCCESS ) {
		return LDAP_INVALID_SYNTAX;
	}

	assert( strlen( in->bv_val ) == in->bv_len );
191

192
	/*
193
	 * Schema-aware validate
194
	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
195
	rc = LDAPDN_validate( dn );
196
197
198
199
	ldap_dnfree( dn );

	if ( rc != LDAP_SUCCESS ) {
		return LDAP_INVALID_SYNTAX;
200
	}
201
202

	return LDAP_SUCCESS;
203
204
}

205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
int
rdnValidate(
	Syntax *syntax,
	struct berval *in )
{
	int		rc;
	LDAPRDN		rdn;
	char*		p;

	assert( in );
	if ( in->bv_len == 0 ) {
		return LDAP_SUCCESS;

	} else if ( in->bv_len > SLAP_LDAPDN_MAXLEN ) {
		return LDAP_INVALID_SYNTAX;
	}

	rc = ldap_bv2rdn_x( in , &rdn, (char **) &p,
				LDAP_DN_FORMAT_LDAP, NULL);
	if ( rc != LDAP_SUCCESS ) {
		return LDAP_INVALID_SYNTAX;
	}

	assert( strlen( in->bv_val ) == in->bv_len );

	/*
	 * Schema-aware validate
	 */
	rc = LDAPRDN_validate( rdn );
	ldap_rdnfree( rdn );

	if ( rc != LDAP_SUCCESS ) {
		return LDAP_INVALID_SYNTAX;
	}

	return LDAP_SUCCESS;
}


244
245
246
247
248
249
250
251
/*
 * AVA sorting inside a RDN
 *
 * rule: sort attributeTypes in alphabetical order; in case of multiple
 * occurrences of the same attributeType, sort values in byte order
 * (use memcmp, which implies alphabetical order in case of IA5 value;
 * this should guarantee the repeatability of the operation).
 *
252
253
254
 * Note: the sorting can be slightly improved by sorting first
 * by attribute type length, then by alphabetical order.
 *
255
256
257
258
 * uses a linear search; should be fine since the number of AVAs in
 * a RDN should be limited.
 */
static void
Howard Chu's avatar
Howard Chu committed
259
AVA_Sort( LDAPRDN rdn, int iAVA )
260
261
{
	int		i;
Howard Chu's avatar
Howard Chu committed
262
	LDAPAVA		*ava_in = rdn[ iAVA ];
263
264
265
266
267

	assert( rdn );
	assert( ava_in );
	
	for ( i = 0; i < iAVA; i++ ) {
Howard Chu's avatar
Howard Chu committed
268
		LDAPAVA		*ava = rdn[ i ];
269
270
271
272
		int		a, j;

		assert( ava );

273
		a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
274
275
276
277
278
279
280
281

		if ( a > 0 ) {
			break;
		}

		while ( a == 0 ) {
			int		v, d;

282
			d = ava_in->la_value.bv_len - ava->la_value.bv_len;
283

284
285
286
287
			v = memcmp( ava_in->la_value.bv_val, 
					ava->la_value.bv_val,
					d <= 0 ? ava_in->la_value.bv_len 
						: ava->la_value.bv_len );
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

			if ( v == 0 && d != 0 ) {
				v = d;
			}

			if ( v <= 0 ) {
				/* 
				 * got it!
				 */
				break;
			}

			if ( ++i == iAVA ) {
				/*
				 * already sorted
				 */
				return;
			}

Howard Chu's avatar
Howard Chu committed
307
			ava = rdn[ i ];
308
309
			a = strcmp( ava_in->la_attr.bv_val, 
					ava->la_attr.bv_val );
310
311
312
313
314
315
		}

		/*
		 * move ahead
		 */
		for ( j = iAVA; j > i; j-- ) {
Howard Chu's avatar
Howard Chu committed
316
			rdn[ j ] = rdn[ j - 1 ];
317
		}
Howard Chu's avatar
Howard Chu committed
318
		rdn[ i ] = ava_in;
319
320
321
322
323

		return;
	}
}

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
static int
LDAPRDN_rewrite( LDAPRDN rdn, unsigned flags, void *ctx )
{

	int rc;
	int		iAVA;
	for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
		LDAPAVA			*ava = rdn[ iAVA ];
		AttributeDescription	*ad;
		slap_syntax_validate_func *validf = NULL;
		slap_mr_normalize_func *normf = NULL;
		slap_syntax_transform_func *transf = NULL;
		MatchingRule *mr = NULL;
		struct berval		bv = BER_BVNULL;
		int			do_sort = 0;

		assert( ava );

		if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
			const char	*text = NULL;

			rc = slap_bv2ad( &ava->la_attr, &ad, &text );
			if ( rc != LDAP_SUCCESS ) {
				return LDAP_INVALID_SYNTAX;
			}
			
			ava->la_private = ( void * )ad;
			do_sort = 1;
		}

		/* 
		 * Replace attr oid/name with the canonical name
		 */
		ava->la_attr = ad->ad_cname;

		if( ava->la_flags & LDAP_AVA_BINARY ) {
			if( ava->la_value.bv_len == 0 ) {
				/* BER encoding is empty */
				return LDAP_INVALID_SYNTAX;
			}

			/* AVA is binary encoded, don't muck with it */
		} else if( flags & SLAP_LDAPDN_PRETTY ) {
			transf = ad->ad_type->sat_syntax->ssyn_pretty;
			if( !transf ) {
				validf = ad->ad_type->sat_syntax->ssyn_validate;
			}
		} else { /* normalization */
			validf = ad->ad_type->sat_syntax->ssyn_validate;
			mr = ad->ad_type->sat_equality;
			if( mr ) normf = mr->smr_normalize;
		}

		if ( validf ) {
			/* validate value before normalization */
			rc = ( *validf )( ad->ad_type->sat_syntax,
				ava->la_value.bv_len
					? &ava->la_value
					: (struct berval *) &slap_empty_bv );

			if ( rc != LDAP_SUCCESS ) {
				return LDAP_INVALID_SYNTAX;
			}
		}

		if ( transf ) {
			/*
		 	 * transform value by pretty function
			 *	if value is empty, use empty_bv
			 */
			rc = ( *transf )( ad->ad_type->sat_syntax,
				ava->la_value.bv_len
					? &ava->la_value
					: (struct berval *) &slap_empty_bv,
				&bv, ctx );
		
			if ( rc != LDAP_SUCCESS ) {
				return LDAP_INVALID_SYNTAX;
			}
		}

		if ( normf ) {
			/*
		 	 * normalize value
			 *	if value is empty, use empty_bv
			 */
			rc = ( *normf )(
				SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
				ad->ad_type->sat_syntax,
				mr,
				ava->la_value.bv_len
					? &ava->la_value
					: (struct berval *) &slap_empty_bv,
				&bv, ctx );
		
			if ( rc != LDAP_SUCCESS ) {
				return LDAP_INVALID_SYNTAX;
			}
		}


		if( bv.bv_val ) {
			if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
				ber_memfree_x( ava->la_value.bv_val, ctx );
			ava->la_value = bv;
			ava->la_flags |= LDAP_AVA_FREE_VALUE;
		}

		if( do_sort ) AVA_Sort( rdn, iAVA );
	}
	return LDAP_SUCCESS;
}

437
438
439
440
441
/*
 * In-place, schema-aware normalization / "pretty"ing of the
 * structural representation of a distinguished name.
 */
static int
Howard Chu's avatar
Howard Chu committed
442
LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
443
444
445
446
447
448
{
	int 		iRDN;
	int 		rc;

	assert( dn );

Howard Chu's avatar
Howard Chu committed
449
450
	for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
		LDAPRDN		rdn = dn[ iRDN ];
451
452
453
454
		int		iAVA;

		assert( rdn );

Howard Chu's avatar
Howard Chu committed
455
456
		for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
			LDAPAVA			*ava = rdn[ iAVA ];
457
			AttributeDescription	*ad;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
458
			slap_syntax_validate_func *validf = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
459
			slap_mr_normalize_func *normf = NULL;
460
			slap_syntax_transform_func *transf = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
461
			MatchingRule *mr = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
462
			struct berval		bv = BER_BVNULL;
463
			int			do_sort = 0;
464

465
466
467
468
469
			assert( ava );

			if ( ( ad = AVA_PRIVATE( ava ) ) == NULL ) {
				const char	*text = NULL;

470
				rc = slap_bv2ad( &ava->la_attr, &ad, &text );
471
472
473
474
475
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
				
				ava->la_private = ( void * )ad;
476
				do_sort = 1;
477
478
479
480
481
			}

			/* 
			 * Replace attr oid/name with the canonical name
			 */
Howard Chu's avatar
Howard Chu committed
482
			ava->la_attr = ad->ad_cname;
483

484
			if( ava->la_flags & LDAP_AVA_BINARY ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
485
486
487
488
489
				if( ava->la_value.bv_len == 0 ) {
					/* BER encoding is empty */
					return LDAP_INVALID_SYNTAX;
				}

490
491
				/* AVA is binary encoded, don't muck with it */
			} else if( flags & SLAP_LDAPDN_PRETTY ) {
492
				transf = ad->ad_type->sat_syntax->ssyn_pretty;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
493
494
495
496
				if( !transf ) {
					validf = ad->ad_type->sat_syntax->ssyn_validate;
				}
			} else { /* normalization */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
497
				validf = ad->ad_type->sat_syntax->ssyn_validate;
498
				mr = ad->ad_type->sat_equality;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
499
				if( mr ) normf = mr->smr_normalize;
500
501
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
502
503
504
505
506
507
508
509
510
511
512
513
			if ( validf ) {
				/* validate value before normalization */
				rc = ( *validf )( ad->ad_type->sat_syntax,
					ava->la_value.bv_len
						? &ava->la_value
						: (struct berval *) &slap_empty_bv );

				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}

514
515
			if ( transf ) {
				/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516
			 	 * transform value by pretty function
517
				 *	if value is empty, use empty_bv
518
519
				 */
				rc = ( *transf )( ad->ad_type->sat_syntax,
520
521
522
					ava->la_value.bv_len
						? &ava->la_value
						: (struct berval *) &slap_empty_bv,
Howard Chu's avatar
Howard Chu committed
523
					&bv, ctx );
524
525
526
527
528
529
			
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
530
531
532
533
534
535
			if ( normf ) {
				/*
			 	 * normalize value
				 *	if value is empty, use empty_bv
				 */
				rc = ( *normf )(
536
					SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
537
538
539
540
541
					ad->ad_type->sat_syntax,
					mr,
					ava->la_value.bv_len
						? &ava->la_value
						: (struct berval *) &slap_empty_bv,
Howard Chu's avatar
Howard Chu committed
542
					&bv, ctx );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
543
544
545
546
547
548
			
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}

549

550
			if( bv.bv_val ) {
551
552
				if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
					ber_memfree_x( ava->la_value.bv_val, ctx );
553
				ava->la_value = bv;
554
				ava->la_flags |= LDAP_AVA_FREE_VALUE;
555
556
			}

557
			if( do_sort ) AVA_Sort( rdn, iAVA );
558
559
560
561
562
563
564
565
		}
	}

	return LDAP_SUCCESS;
}

int
dnNormalize(
566
567
568
569
    slap_mask_t use,
    Syntax *syntax,
    MatchingRule *mr,
    struct berval *val,
Howard Chu's avatar
Howard Chu committed
570
571
    struct berval *out,
    void *ctx)
572
{
573
	assert( val );
574
	assert( out );
575

576
	Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
577

578
	if ( val->bv_len != 0 ) {
Howard Chu's avatar
Howard Chu committed
579
		LDAPDN		dn = NULL;
580
581
582
583
584
		int		rc;

		/*
		 * Go to structural representation
		 */
Howard Chu's avatar
Howard Chu committed
585
		rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
586
587
588
589
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
590
591
		assert( strlen( val->bv_val ) == val->bv_len );

592
593
594
		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
595
		if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
596
			ldap_dnfree_x( dn, ctx );
597
598
599
600
601
602
			return LDAP_INVALID_SYNTAX;
		}

		/*
		 * Back to string representation
		 */
Howard Chu's avatar
Howard Chu committed
603
604
		rc = ldap_dn2bv_x( dn, out,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
605

Howard Chu's avatar
Howard Chu committed
606
		ldap_dnfree_x( dn, ctx );
607
608
609
610
611

		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}
	} else {
Howard Chu's avatar
Howard Chu committed
612
		ber_dupbv_x( out, val, ctx );
613
614
615
616
617
618
619
	}

	Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );

	return LDAP_SUCCESS;
}

620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
int
rdnNormalize(
    slap_mask_t use,
    Syntax *syntax,
    MatchingRule *mr,
    struct berval *val,
    struct berval *out,
    void *ctx)
{
	assert( val );
	assert( out );

	Debug( LDAP_DEBUG_TRACE, ">>> dnNormalize: <%s>\n", val->bv_val, 0, 0 );
	if ( val->bv_len != 0 ) {
		LDAPRDN		rdn = NULL;
		int		rc;
		char*		p;

		/*
		 * Go to structural representation
		 */
		rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
					LDAP_DN_FORMAT_LDAP, ctx);

		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

		assert( strlen( val->bv_val ) == val->bv_len );

		/*
		 * Schema-aware rewrite
		 */
		if ( LDAPRDN_rewrite( rdn, 0, ctx ) != LDAP_SUCCESS ) {
			ldap_rdnfree_x( rdn, ctx );
			return LDAP_INVALID_SYNTAX;
		}

		/*
		 * Back to string representation
		 */
		rc = ldap_rdn2bv_x( rdn, out,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );

		ldap_rdnfree_x( rdn, ctx );

		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}
	} else {
		ber_dupbv_x( out, val, ctx );
	}

	Debug( LDAP_DEBUG_TRACE, "<<< dnNormalize: <%s>\n", out->bv_val, 0, 0 );

	return LDAP_SUCCESS;
}

678
679
int
dnPretty(
680
681
	Syntax *syntax,
	struct berval *val,
Howard Chu's avatar
Howard Chu committed
682
683
	struct berval *out,
	void *ctx)
684
{
685
	assert( val );
686
687
688
	assert( out );

	Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
689

690
	if ( val->bv_len == 0 ) {
Howard Chu's avatar
Howard Chu committed
691
		ber_dupbv_x( out, val, ctx );
692
693
694
695
696

	} else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
		return LDAP_INVALID_SYNTAX;

	} else {
Howard Chu's avatar
Howard Chu committed
697
		LDAPDN		dn = NULL;
698
699
700
		int		rc;

		/* FIXME: should be liberal in what we accept */
Howard Chu's avatar
Howard Chu committed
701
		rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
702
703
704
705
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
706
707
		assert( strlen( val->bv_val ) == val->bv_len );

708
709
710
		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
711
712
		if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( dn, ctx );
713
714
715
716
717
718
719
			return LDAP_INVALID_SYNTAX;
		}

		/* FIXME: not sure why the default isn't pretty */
		/* RE: the default is the form that is used as
		 * an internal representation; the pretty form
		 * is a variant */
Howard Chu's avatar
Howard Chu committed
720
721
		rc = ldap_dn2bv_x( dn, out,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
722

Howard Chu's avatar
Howard Chu committed
723
		ldap_dnfree_x( dn, ctx );
724
725
726
727
728
729
730
731
732

		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}
	}

	Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );

	return LDAP_SUCCESS;
733
}
734

735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
int
rdnPretty(
	Syntax *syntax,
	struct berval *val,
	struct berval *out,
	void *ctx)
{
	assert( val );
	assert( out );

	Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );

	if ( val->bv_len == 0 ) {
		ber_dupbv_x( out, val, ctx );

	} else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
		return LDAP_INVALID_SYNTAX;

	} else {
		LDAPRDN		rdn = NULL;
		int		rc;
		char*		p;

		/* FIXME: should be liberal in what we accept */
		rc = ldap_bv2rdn_x( val , &rdn, (char **) &p,
					LDAP_DN_FORMAT_LDAP, ctx);
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

		assert( strlen( val->bv_val ) == val->bv_len );

		/*
		 * Schema-aware rewrite
		 */
		if ( LDAPRDN_rewrite( rdn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
			ldap_rdnfree_x( rdn, ctx );
			return LDAP_INVALID_SYNTAX;
		}

		/* FIXME: not sure why the default isn't pretty */
		/* RE: the default is the form that is used as
		 * an internal representation; the pretty form
		 * is a variant */
		rc = ldap_rdn2bv_x( rdn, out,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );

		ldap_rdnfree_x( rdn, ctx );

		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}
	}

	Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );

	return LDAP_SUCCESS;
}


795
796
797
798
int
dnPrettyNormalDN(
	Syntax *syntax,
	struct berval *val,
Howard Chu's avatar
Howard Chu committed
799
800
801
	LDAPDN *dn,
	int flags,
	void *ctx )
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
{
	assert( val );
	assert( dn );

	Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
			flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
			val->bv_val, 0 );

	if ( val->bv_len == 0 ) {
		return LDAP_SUCCESS;

	} else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
		return LDAP_INVALID_SYNTAX;

	} else {
		int		rc;

		/* FIXME: should be liberal in what we accept */
Howard Chu's avatar
Howard Chu committed
820
		rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
821
822
823
824
825
826
827
828
829
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

		assert( strlen( val->bv_val ) == val->bv_len );

		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
830
831
		if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( *dn, ctx );
832
833
834
835
836
837
838
839
840
841
842
843
			*dn = NULL;
			return LDAP_INVALID_SYNTAX;
		}
	}

	Debug( LDAP_DEBUG_TRACE, "<<< dn%sDN\n", 
			flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal",
			0, 0 );

	return LDAP_SUCCESS;
}

844
845
846
847
848
849
850
851
/*
 * Combination of both dnPretty and dnNormalize
 */
int
dnPrettyNormal(
	Syntax *syntax,
	struct berval *val,
	struct berval *pretty,
Howard Chu's avatar
Howard Chu committed
852
853
	struct berval *normal,
	void *ctx)
854
855
856
857
858
859
860
{
	Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );

	assert( val );
	assert( pretty );
	assert( normal );

861
	if ( val->bv_len == 0 ) {
Howard Chu's avatar
Howard Chu committed
862
863
		ber_dupbv_x( pretty, val, ctx );
		ber_dupbv_x( normal, val, ctx );
864
865
866
867
868
869

	} else if ( val->bv_len > SLAP_LDAPDN_MAXLEN ) {
		/* too big */
		return LDAP_INVALID_SYNTAX;

	} else {
Howard Chu's avatar
Howard Chu committed
870
		LDAPDN		dn = NULL;
871
872
873
874
875
876
877
878
		int		rc;

		pretty->bv_val = NULL;
		normal->bv_val = NULL;
		pretty->bv_len = 0;
		normal->bv_len = 0;

		/* FIXME: should be liberal in what we accept */
Howard Chu's avatar
Howard Chu committed
879
		rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
880
881
882
883
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
884
885
		assert( strlen( val->bv_val ) == val->bv_len );

886
887
888
		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
889
890
		if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( dn, ctx );
891
892
893
			return LDAP_INVALID_SYNTAX;
		}

Howard Chu's avatar
Howard Chu committed
894
895
		rc = ldap_dn2bv_x( dn, pretty,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
896
897

		if ( rc != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
898
			ldap_dnfree_x( dn, ctx );
899
900
901
			return LDAP_INVALID_SYNTAX;
		}

Howard Chu's avatar
Howard Chu committed
902
903
904
		if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( dn, ctx );
			ber_memfree_x( pretty->bv_val, ctx );
905
906
907
908
909
			pretty->bv_val = NULL;
			pretty->bv_len = 0;
			return LDAP_INVALID_SYNTAX;
		}

Howard Chu's avatar
Howard Chu committed
910
911
		rc = ldap_dn2bv_x( dn, normal,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
912

Howard Chu's avatar
Howard Chu committed
913
		ldap_dnfree_x( dn, ctx );
914
		if ( rc != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
915
			ber_memfree_x( pretty->bv_val, ctx );
916
917
918
919
920
921
922
923
924
925
926
927
			pretty->bv_val = NULL;
			pretty->bv_len = 0;
			return LDAP_INVALID_SYNTAX;
		}
	}

	Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
		pretty->bv_val, normal->bv_val, 0 );

	return LDAP_SUCCESS;
}

928
/*
929
 * dnMatch routine
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
 */
int
dnMatch(
	int *matchp,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	int match;
	struct berval *asserted = (struct berval *) assertedValue;

	assert( matchp );
	assert( value );
	assert( assertedValue );
946
947
	assert( !BER_BVISNULL( value ) );
	assert( !BER_BVISNULL( asserted ) );
948
949
950
951
	
	match = value->bv_len - asserted->bv_len;

	if ( match == 0 ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
952
953
		match = memcmp( value->bv_val, asserted->bv_val, 
				value->bv_len );
954
955
956
957
	}

	Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
		match, value->bv_val, asserted->bv_val );
958

959
	*matchp = match;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
	return LDAP_SUCCESS;
}

/*
 * dnRelativeMatch routine
 */
int
dnRelativeMatch(
	int *matchp,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	int match;
	struct berval *asserted = (struct berval *) assertedValue;

	assert( matchp );
	assert( value );
	assert( assertedValue );
	assert( !BER_BVISNULL( value ) );
	assert( !BER_BVISNULL( asserted ) );

	if( mr == slap_schema.si_mr_dnSubtreeMatch ) {
		if( asserted->bv_len > value->bv_len ) {
			match = -1;
		} else if ( asserted->bv_len == value->bv_len ) {
			match = memcmp( value->bv_val, asserted->bv_val, 
				value->bv_len );
		} else {
			if( DN_SEPARATOR(
				value->bv_val[value->bv_len - asserted->bv_len - 1] ))
			{
				match = memcmp(
					&value->bv_val[value->bv_len - asserted->bv_len],
					asserted->bv_val, 
					asserted->bv_len );
			} else {
				return 1;
			}
For faster browsing, not all history is shown. View entire blame