dn.c 17.7 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
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8

#include "portable.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
10
11
12
13
14
15
16

#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
17
#include "slap.h"
18
#include "ldap_pvt.h" /* must be after slap.h, to get ldap_bv2dn_x() & co */
19
20
#include "lutil.h"

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * 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 )

/*
 * In-place, schema-aware validation of the
 * structural representation of a distinguished name.
 */
static int
Howard Chu's avatar
Howard Chu committed
43
LDAPDN_validate( LDAPDN dn )
44
45
46
47
48
49
{
	int 		iRDN;
	int 		rc;

	assert( dn );

Howard Chu's avatar
Howard Chu committed
50
51
	for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
		LDAPRDN		rdn = dn[ iRDN ];
52
53
54
55
		int		iAVA;

		assert( rdn );

Howard Chu's avatar
Howard Chu committed
56
57
		for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
			LDAPAVA			*ava = rdn[ iAVA ];
58
59
60
61
62
63
64
65
			AttributeDescription	*ad;
			slap_syntax_validate_func *validate = NULL;

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

66
				rc = slap_bv2ad( &ava->la_attr, &ad, &text );
67
68
69
70
71
72
73
74
75
76
				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
77
			ava->la_attr = ad->ad_cname;
78
79
80
81
82
83
84
85

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

			if ( validate ) {
				/*
			 	 * validate value by validate function
				 */
				rc = ( *validate )( ad->ad_type->sat_syntax,
86
					&ava->la_value );
87
88
89
90
91
92
93
94
95
96
97
			
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}
		}
	}

	return LDAP_SUCCESS;
}

98
/*
99
 * dn validate routine
100
 */
101
102
103
104
int
dnValidate(
	Syntax *syntax,
	struct berval *in )
105
{
106
	int		rc;
Howard Chu's avatar
Howard Chu committed
107
	LDAPDN		dn = NULL;
108
109
110
111

	assert( in );

	if ( in->bv_len == 0 ) {
112
113
114
115
		return LDAP_SUCCESS;

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

Pierangelo Masarati's avatar
Pierangelo Masarati committed
118
	rc = ldap_bv2dn( in, &dn, LDAP_DN_FORMAT_LDAP );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
119
120
121
122
123
	if ( rc != LDAP_SUCCESS ) {
		return LDAP_INVALID_SYNTAX;
	}

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

125
	/*
126
	 * Schema-aware validate
127
	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
128
	rc = LDAPDN_validate( dn );
129
130
131
132
	ldap_dnfree( dn );

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

	return LDAP_SUCCESS;
136
137
138
139
140
141
142
143
144
145
}

/*
 * 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).
 *
146
147
148
 * Note: the sorting can be slightly improved by sorting first
 * by attribute type length, then by alphabetical order.
 *
149
150
151
152
 * 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
153
AVA_Sort( LDAPRDN rdn, int iAVA )
154
155
{
	int		i;
Howard Chu's avatar
Howard Chu committed
156
	LDAPAVA		*ava_in = rdn[ iAVA ];
157
158
159
160
161

	assert( rdn );
	assert( ava_in );
	
	for ( i = 0; i < iAVA; i++ ) {
Howard Chu's avatar
Howard Chu committed
162
		LDAPAVA		*ava = rdn[ i ];
163
164
165
166
		int		a, j;

		assert( ava );

167
		a = strcmp( ava_in->la_attr.bv_val, ava->la_attr.bv_val );
168
169
170
171
172
173
174
175

		if ( a > 0 ) {
			break;
		}

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

176
			d = ava_in->la_value.bv_len - ava->la_value.bv_len;
177

178
179
180
181
			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 );
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

			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
201
			ava = rdn[ i ];
202
203
			a = strcmp( ava_in->la_attr.bv_val, 
					ava->la_attr.bv_val );
204
205
206
207
208
209
		}

		/*
		 * move ahead
		 */
		for ( j = iAVA; j > i; j-- ) {
Howard Chu's avatar
Howard Chu committed
210
			rdn[ j ] = rdn[ j - 1 ];
211
		}
Howard Chu's avatar
Howard Chu committed
212
		rdn[ i ] = ava_in;
213
214
215
216
217
218
219
220
221
222

		return;
	}
}

/*
 * In-place, schema-aware normalization / "pretty"ing of the
 * structural representation of a distinguished name.
 */
static int
Howard Chu's avatar
Howard Chu committed
223
LDAPDN_rewrite( LDAPDN dn, unsigned flags, void *ctx )
224
225
226
227
228
229
{
	int 		iRDN;
	int 		rc;

	assert( dn );

Howard Chu's avatar
Howard Chu committed
230
231
	for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
		LDAPRDN		rdn = dn[ iRDN ];
232
233
234
235
		int		iAVA;

		assert( rdn );

Howard Chu's avatar
Howard Chu committed
236
237
		for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
			LDAPAVA			*ava = rdn[ iAVA ];
238
			AttributeDescription	*ad;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
			slap_syntax_validate_func *validf = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
240
			slap_mr_normalize_func *normf = NULL;
241
			slap_syntax_transform_func *transf = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242
			MatchingRule *mr = NULL;
Howard Chu's avatar
Howard Chu committed
243
			struct berval		bv = { 0, NULL };
244
			int			do_sort = 0;
245

246
247
248
249
250
			assert( ava );

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

251
				rc = slap_bv2ad( &ava->la_attr, &ad, &text );
252
253
254
255
256
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
				
				ava->la_private = ( void * )ad;
257
				do_sort = 1;
258
259
260
261
262
			}

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

265
			if( ava->la_flags & LDAP_AVA_BINARY ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
266
267
268
269
270
				if( ava->la_value.bv_len == 0 ) {
					/* BER encoding is empty */
					return LDAP_INVALID_SYNTAX;
				}

271
272
				/* AVA is binary encoded, don't muck with it */
			} else if( flags & SLAP_LDAPDN_PRETTY ) {
273
				transf = ad->ad_type->sat_syntax->ssyn_pretty;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
274
275
276
277
				if( !transf ) {
					validf = ad->ad_type->sat_syntax->ssyn_validate;
				}
			} else { /* normalization */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
278
				validf = ad->ad_type->sat_syntax->ssyn_validate;
279
				mr = ad->ad_type->sat_equality;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
280
				if( mr ) normf = mr->smr_normalize;
281
282
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
283
284
285
286
287
288
289
290
291
292
293
294
			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;
				}
			}

295
296
			if ( transf ) {
				/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
297
			 	 * transform value by pretty function
298
				 *	if value is empty, use empty_bv
299
300
				 */
				rc = ( *transf )( ad->ad_type->sat_syntax,
301
302
303
					ava->la_value.bv_len
						? &ava->la_value
						: (struct berval *) &slap_empty_bv,
Howard Chu's avatar
Howard Chu committed
304
					&bv, ctx );
305
306
307
308
309
310
			
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
311
312
313
314
315
316
			if ( normf ) {
				/*
			 	 * normalize value
				 *	if value is empty, use empty_bv
				 */
				rc = ( *normf )(
317
					SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
318
319
320
321
322
					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
323
					&bv, ctx );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
324
325
326
327
328
329
			
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
			}

330

331
			if( bv.bv_val ) {
332
333
				if ( ava->la_flags & LDAP_AVA_FREE_VALUE )
					ber_memfree_x( ava->la_value.bv_val, ctx );
334
				ava->la_value = bv;
335
				ava->la_flags |= LDAP_AVA_FREE_VALUE;
336
337
			}

338
			if( do_sort ) AVA_Sort( rdn, iAVA );
339
340
341
342
343
344
345
346
		}
	}

	return LDAP_SUCCESS;
}

int
dnNormalize(
347
348
349
350
    slap_mask_t use,
    Syntax *syntax,
    MatchingRule *mr,
    struct berval *val,
Howard Chu's avatar
Howard Chu committed
351
352
    struct berval *out,
    void *ctx)
353
{
354
	assert( val );
355
	assert( out );
356

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

359
	if ( val->bv_len != 0 ) {
Howard Chu's avatar
Howard Chu committed
360
		LDAPDN		dn = NULL;
361
362
363
364
365
		int		rc;

		/*
		 * Go to structural representation
		 */
Howard Chu's avatar
Howard Chu committed
366
		rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
367
368
369
370
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

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

373
374
375
		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
376
		if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
377
			ldap_dnfree_x( dn, ctx );
378
379
380
381
382
383
			return LDAP_INVALID_SYNTAX;
		}

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

Howard Chu's avatar
Howard Chu committed
387
		ldap_dnfree_x( dn, ctx );
388
389
390
391
392

		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}
	} else {
Howard Chu's avatar
Howard Chu committed
393
		ber_dupbv_x( out, val, ctx );
394
395
396
397
398
399
400
401
402
	}

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

	return LDAP_SUCCESS;
}

int
dnPretty(
403
404
	Syntax *syntax,
	struct berval *val,
Howard Chu's avatar
Howard Chu committed
405
406
	struct berval *out,
	void *ctx)
407
{
408
	assert( val );
409
410
	assert( out );

Julius Enarusai's avatar
   
Julius Enarusai committed
411
412
413
#ifdef NEW_LOGGING
	LDAP_LOG( OPERATION, ARGS, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
#else
414
	Debug( LDAP_DEBUG_TRACE, ">>> dnPretty: <%s>\n", val->bv_val, 0, 0 );
Julius Enarusai's avatar
   
Julius Enarusai committed
415
#endif
416

417
	if ( val->bv_len == 0 ) {
Howard Chu's avatar
Howard Chu committed
418
		ber_dupbv_x( out, val, ctx );
419
420
421
422
423

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

	} else {
Howard Chu's avatar
Howard Chu committed
424
		LDAPDN		dn = NULL;
425
426
427
		int		rc;

		/* FIXME: should be liberal in what we accept */
Howard Chu's avatar
Howard Chu committed
428
		rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
429
430
431
432
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

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

435
436
437
		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
438
439
		if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( dn, ctx );
440
441
442
443
444
445
446
			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
447
448
		rc = ldap_dn2bv_x( dn, out,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
449

Howard Chu's avatar
Howard Chu committed
450
		ldap_dnfree_x( dn, ctx );
451
452
453
454
455
456

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

457
458
459
#ifdef NEW_LOGGING
	LDAP_LOG( OPERATION, ARGS, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
#else
460
	Debug( LDAP_DEBUG_TRACE, "<<< dnPretty: <%s>\n", out->bv_val, 0, 0 );
461
#endif
462
463

	return LDAP_SUCCESS;
464
}
465

466
467
468
469
int
dnPrettyNormalDN(
	Syntax *syntax,
	struct berval *val,
Howard Chu's avatar
Howard Chu committed
470
471
472
	LDAPDN *dn,
	int flags,
	void *ctx )
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
{
	assert( val );
	assert( dn );

#ifdef NEW_LOGGING
	LDAP_LOG( OPERATION, ARGS, ">>> dn%sDN: <%s>\n", 
			flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
			val->bv_val, 0 );
#else
	Debug( LDAP_DEBUG_TRACE, ">>> dn%sDN: <%s>\n", 
			flags == SLAP_LDAPDN_PRETTY ? "Pretty" : "Normal", 
			val->bv_val, 0 );
#endif

	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
497
		rc = ldap_bv2dn_x( val, dn, LDAP_DN_FORMAT_LDAP, ctx );
498
499
500
501
502
503
504
505
506
		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
507
508
		if ( LDAPDN_rewrite( *dn, flags, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( *dn, ctx );
509
510
511
512
513
514
515
516
517
518
519
520
			*dn = NULL;
			return LDAP_INVALID_SYNTAX;
		}
	}

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

	return LDAP_SUCCESS;
}

521
522
523
524
525
526
527
528
/*
 * Combination of both dnPretty and dnNormalize
 */
int
dnPrettyNormal(
	Syntax *syntax,
	struct berval *val,
	struct berval *pretty,
Howard Chu's avatar
Howard Chu committed
529
530
	struct berval *normal,
	void *ctx)
531
{
Julius Enarusai's avatar
   
Julius Enarusai committed
532
533
534
#ifdef NEW_LOGGING
	LDAP_LOG ( OPERATION, ENTRY, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
#else
535
	Debug( LDAP_DEBUG_TRACE, ">>> dnPrettyNormal: <%s>\n", val->bv_val, 0, 0 );
Julius Enarusai's avatar
   
Julius Enarusai committed
536
#endif
537
538
539
540
541

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

542
	if ( val->bv_len == 0 ) {
Howard Chu's avatar
Howard Chu committed
543
544
		ber_dupbv_x( pretty, val, ctx );
		ber_dupbv_x( normal, val, ctx );
545
546
547
548
549
550

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

	} else {
Howard Chu's avatar
Howard Chu committed
551
		LDAPDN		dn = NULL;
552
553
554
555
556
557
558
559
		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
560
		rc = ldap_bv2dn_x( val, &dn, LDAP_DN_FORMAT_LDAP, ctx );
561
562
563
564
		if ( rc != LDAP_SUCCESS ) {
			return LDAP_INVALID_SYNTAX;
		}

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

567
568
569
		/*
		 * Schema-aware rewrite
		 */
Howard Chu's avatar
Howard Chu committed
570
571
		if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( dn, ctx );
572
573
574
			return LDAP_INVALID_SYNTAX;
		}

Howard Chu's avatar
Howard Chu committed
575
576
		rc = ldap_dn2bv_x( dn, pretty,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
577
578

		if ( rc != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
579
			ldap_dnfree_x( dn, ctx );
580
581
582
			return LDAP_INVALID_SYNTAX;
		}

Howard Chu's avatar
Howard Chu committed
583
584
585
		if ( LDAPDN_rewrite( dn, 0, ctx ) != LDAP_SUCCESS ) {
			ldap_dnfree_x( dn, ctx );
			ber_memfree_x( pretty->bv_val, ctx );
586
587
588
589
590
			pretty->bv_val = NULL;
			pretty->bv_len = 0;
			return LDAP_INVALID_SYNTAX;
		}

Howard Chu's avatar
Howard Chu committed
591
592
		rc = ldap_dn2bv_x( dn, normal,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
593

Howard Chu's avatar
Howard Chu committed
594
		ldap_dnfree_x( dn, ctx );
595
		if ( rc != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
596
			ber_memfree_x( pretty->bv_val, ctx );
597
598
599
600
601
602
			pretty->bv_val = NULL;
			pretty->bv_len = 0;
			return LDAP_INVALID_SYNTAX;
		}
	}

Julius Enarusai's avatar
   
Julius Enarusai committed
603
604
605
606
#ifdef NEW_LOGGING
	LDAP_LOG (OPERATION, RESULTS, "<<< dnPrettyNormal: <%s>, <%s>\n",
		pretty->bv_val, normal->bv_val, 0  );
#else
607
608
	Debug( LDAP_DEBUG_TRACE, "<<< dnPrettyNormal: <%s>, <%s>\n",
		pretty->bv_val, normal->bv_val, 0 );
Julius Enarusai's avatar
   
Julius Enarusai committed
609
#endif
610
611
612
613

	return LDAP_SUCCESS;
}

614
/*
615
 * dnMatch routine
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
 */
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 );
	
	match = value->bv_len - asserted->bv_len;

	if ( match == 0 ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
636
637
		match = memcmp( value->bv_val, asserted->bv_val, 
				value->bv_len );
638
639
640
	}

#ifdef NEW_LOGGING
Julius Enarusai's avatar
   
Julius Enarusai committed
641
642
	LDAP_LOG( CONFIG, ENTRY, "dnMatch: %d\n    %s\n    %s\n", 
		match, value->bv_val, asserted->bv_val  );
643
644
645
#else
	Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
		match, value->bv_val, asserted->bv_val );
646
#endif
647

648
649
650
651
	*matchp = match;
	return( LDAP_SUCCESS );
}

652
653
654
655
656
657
/*
 * dnParent - dn's parent, in-place
 *
 * note: the incoming dn is assumed to be normalized/prettyfied,
 * so that escaped rdn/ava separators are in '\'+hexpair form
 */
658
void
659
dnParent( 
660
661
	struct berval	*dn, 
	struct berval	*pdn )
662
{
Howard Chu's avatar
Howard Chu committed
663
	char	*p;
664

665
	p = strchr( dn->bv_val, ',' );
666
667
668

	/* one-level dn */
	if ( p == NULL ) {
669
670
		pdn->bv_len = 0;
		pdn->bv_val = dn->bv_val + dn->bv_len;
671
		return;
672
673
674
675
676
677
	}

	assert( DN_SEPARATOR( p[ 0 ] ) );
	p++;

	assert( ATTR_LEADCHAR( p[ 0 ] ) );
678
679
	pdn->bv_val = p;
	pdn->bv_len = dn->bv_len - (p - dn->bv_val);
680

681
	return;
682
683
}

684
685
int
dnExtractRdn( 
686
	struct berval	*dn, 
Howard Chu's avatar
Howard Chu committed
687
688
	struct berval 	*rdn,
	void *ctx )
689
{
Howard Chu's avatar
Howard Chu committed
690
	LDAPRDN		tmpRDN;
691
692
693
694
695
696
	const char	*p;
	int		rc;

	assert( dn );
	assert( rdn );

697
698
699
700
	if( dn->bv_len == 0 ) {
		return LDAP_OTHER;
	}

Howard Chu's avatar
Howard Chu committed
701
	rc = ldap_bv2rdn_x( dn, &tmpRDN, (char **)&p, LDAP_DN_FORMAT_LDAP, ctx );
702
703
704
705
	if ( rc != LDAP_SUCCESS ) {
		return rc;
	}

Howard Chu's avatar
Howard Chu committed
706
	rc = ldap_rdn2bv_x( tmpRDN, rdn, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY, ctx );
707

Howard Chu's avatar
Howard Chu committed
708
	ldap_rdnfree_x( tmpRDN, ctx );
709
710
711
712
713
714
715
716
	if ( rc != LDAP_SUCCESS ) {
		return rc;
	}

	return LDAP_SUCCESS;
}

/*
717
 * We can assume the input is a prettied or normalized DN
718
719
720
721
 */
int 
dn_rdnlen(
	Backend		*be,
722
	struct berval	*dn_in )
723
{
724
	const char	*p;
725
726
727
728
729
730
731

	assert( dn_in );

	if ( dn_in == NULL ) {
		return 0;
	}

732
	if ( !dn_in->bv_len ) {
733
734
735
		return 0;
	}

736
	if ( be != NULL && be_issuffix( be, dn_in ) ) {
737
738
739
		return 0;
	}

740
	p = strchr( dn_in->bv_val, ',' );
741

742
	return p ? p - dn_in->bv_val : dn_in->bv_len;
743
744
}

745

Kurt Zeilenga's avatar
Kurt Zeilenga committed
746
/* rdnValidate:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
747
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
748
749
 * LDAP_SUCCESS if rdn is a legal rdn;
 * LDAP_INVALID_SYNTAX otherwise (including a sequence of rdns)
750
751
 */
int
Kurt Zeilenga's avatar
Kurt Zeilenga committed
752
rdnValidate( struct berval *rdn )
753
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
754
755
756
757
758
#if 1
	/* Major cheat!
	 * input is a pretty or normalized DN
	 * hence, we can just search for ','
	 */
759
760
761
	if( rdn == NULL || rdn->bv_len == 0 ||
		rdn->bv_len > SLAP_LDAPDN_MAXLEN )
	{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
762
763
764
765
766
767
768
		return LDAP_INVALID_SYNTAX;
	}

	return strchr( rdn->bv_val, ',' ) == NULL
		? LDAP_SUCCESS : LDAP_INVALID_SYNTAX;

#else
769
770
771
	LDAPRDN		*RDN, **DN[ 2 ] = { &RDN, NULL };
	const char	*p;
	int		rc;
772

773
774
775
776
777
	/*
	 * must be non-empty
	 */
	if ( rdn == NULL || rdn == '\0' ) {
		return 0;
778
779
	}

780
781
782
	/*
	 * must be parsable
	 */
783
	rc = ldap_bv2rdn( rdn, &RDN, (char **)&p, LDAP_DN_FORMAT_LDAP );
784
785
	if ( rc != LDAP_SUCCESS ) {
		return 0;
786
787
	}

788
789
790
791
792
	/*
	 * Must be one-level
	 */
	if ( p[ 0 ] != '\0' ) {
		return 0;
793
794
	}

795
796
797
798
799
	/*
	 * Schema-aware validate
	 */
	if ( rc == LDAP_SUCCESS ) {
		rc = LDAPDN_validate( DN );
800
	}
801
	ldap_rdnfree( RDN );
802

803
804
805
806
	/*
	 * Must validate (there's a repeated parsing ...)
	 */
	return ( rc == LDAP_SUCCESS );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
807
#endif
808
}
809
810
811
812


/* build_new_dn:
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
813
 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
814
815
 * renamed.
 *
816
 * new_dn = parent (p_dn) + separator + rdn (newrdn) + null.
817
818
819
 */

void
820
821
build_new_dn( struct berval * new_dn,
	struct berval * parent_dn,
822
823
	struct berval * newrdn,
	void *memctx )
824
{
Howard Chu's avatar
Howard Chu committed
825
	char *ptr;
Juan Gomez's avatar
Juan Gomez committed
826

827
	if ( parent_dn == NULL ) {
828
		ber_dupbv( new_dn, newrdn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
829
830
831
		return;
	}

832
	new_dn->bv_len = parent_dn->bv_len + newrdn->bv_len + 1;
833
	new_dn->bv_val = (char *) sl_malloc( new_dn->bv_len + 1, memctx );
834

835
	ptr = lutil_strcopy( new_dn->bv_val, newrdn->bv_val );
Howard Chu's avatar
Howard Chu committed
836
	*ptr++ = ',';
837
	strcpy( ptr, parent_dn->bv_val );
838
}
839

840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865

/*
 * dnIsSuffix - tells whether suffix is a suffix of dn.
 * Both dn and suffix must be normalized.
 */
int
dnIsSuffix(
	const struct berval *dn,
	const struct berval *suffix )
{
	int	d = dn->bv_len - suffix->bv_len;

	assert( dn );
	assert( suffix );

	/* empty suffix matches any dn */
	if ( suffix->bv_len == 0 ) {
		return 1;
	}

	/* suffix longer than dn */
	if ( d < 0 ) {
		return 0;
	}

	/* no rdn separator or escaped rdn separator */
866
	if ( d > 1 && !DN_SEPARATOR( dn->bv_val[ d - 1 ] ) ) {
867
868
869
870
871
872
873
874
875
876
877
		return 0;
	}

	/* no possible match or malformed dn */
	if ( d == 1 ) {
		return 0;
	}

	/* compare */
	return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
}
878

879
#ifdef HAVE_TLS
880
/*
881
 * Convert an X.509 DN into a normalized LDAP DN
882
883
 */
int
884
dnX509normalize( void *x509_name, struct berval *out )
885
{
886
	/* Invoke the LDAP library's converter with our schema-rewriter */
887
888
889
890
891
892
	int rc = ldap_X509dn2bv( x509_name, out, LDAPDN_rewrite, 0 );

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

	return rc;
893
}
894

895
896
897
/*
 * Get the TLS session's peer's DN into a normalized LDAP DN
 */
898
899
int
dnX509peerNormalize( void *ssl, struct berval *dn )
900
{
901
902

	return ldap_pvt_tls_get_peer_dn( ssl, dn, (LDAPDN_rewrite_dummy *)LDAPDN_rewrite, 0 );
903
}
904
#endif