schema_init.c 114 KB
Newer Older
1
2
3
/* schema_init.c - init builtin schema */
/* $OpenLDAP$ */
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
9
10
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
11
#include <limits.h>
12
13

#include <ac/ctype.h>
14
#include <ac/errno.h>
15
16
17
18
19
#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "ldap_pvt.h"
20

21
22
#include "ldap_utf8.h"

23
24
25
26
27
28
#include "lutil_hash.h"
#define HASH_BYTES				LUTIL_HASH_BYTES
#define HASH_CONTEXT			lutil_HASH_CTX
#define HASH_Init(c)			lutil_HASHInit(c)
#define HASH_Update(c,buf,len)	lutil_HASHUpdate(c,buf,len)
#define HASH_Final(d,c)			lutil_HASHFinal(d,c)
29

30
/* recycled validatation routines */
31
#define berValidate						blobValidate
32
33

/* unimplemented pretters */
34
#define integerPretty					NULL
35
#ifndef USE_LDAP_DN_PARSING
36
37
38
#	define dnPretty						NULL
#else
#	define SLAP_LDAPDN_PRETTY 0x1
39
#endif /* !USE_LDAP_DN_PARSING */
40
41

/* recycled matching routines */
42
#define bitStringMatch					octetStringMatch
43
44
45
#define numericStringMatch				caseIgnoreIA5Match
#define objectIdentifierMatch			caseIgnoreIA5Match
#define telephoneNumberMatch			caseIgnoreIA5Match
46
#define telephoneNumberSubstringsMatch	caseIgnoreIA5SubstringsMatch
47
48
#define generalizedTimeMatch			caseIgnoreIA5Match
#define generalizedTimeOrderingMatch	caseIgnoreIA5Match
49
#define uniqueMemberMatch				dnMatch
50

51
52
/* approx matching rules */
#define directoryStringApproxMatchOID	"1.3.6.1.4.1.4203.666.4.4"
Gary Williams's avatar
Gary Williams committed
53
54
55
#define directoryStringApproxMatch	approxMatch
#define directoryStringApproxIndexer	approxIndexer
#define directoryStringApproxFilter	approxFilter
56
#define IA5StringApproxMatchOID			"1.3.6.1.4.1.4203.666.4.5"
Gary Williams's avatar
Gary Williams committed
57
#define IA5StringApproxMatch			approxMatch
58
#define IA5StringApproxIndexer			approxIndexer
Gary Williams's avatar
Gary Williams committed
59
#define IA5StringApproxFilter			approxFilter
60

61
62
63
64
/* orderring matching rules */
#define caseIgnoreOrderingMatch			caseIgnoreMatch
#define caseExactOrderingMatch			caseExactMatch

65
/* unimplemented matching routines */
66
67
68
69
70
71
72
#define caseIgnoreListMatch				NULL
#define caseIgnoreListSubstringsMatch	NULL
#define protocolInformationMatch		NULL
#define integerFirstComponentMatch		NULL

#define OpenLDAPaciMatch				NULL
#define authPasswordMatch				NULL
73
74

/* recycled indexing/filtering routines */
75
76
#define dnIndexer				caseExactIgnoreIndexer
#define dnFilter				caseExactIgnoreFilter
77
78
#define bitStringFilter			octetStringFilter
#define bitStringIndexer		octetStringIndexer
79

80
81
82
83
84
#define telephoneNumberIndexer			caseIgnoreIA5Indexer
#define telephoneNumberFilter			caseIgnoreIA5Filter
#define telephoneNumberSubstringsIndexer	caseIgnoreIA5SubstringsIndexer
#define telephoneNumberSubstringsFilter		caseIgnoreIA5SubstringsFilter

85
86
87
88
/* must match OIDs below */
#define caseExactMatchOID			"2.5.13.5"
#define caseExactSubstringsMatchOID		"2.5.13.7"

89
90
91
92
static char *strcasechr( const char *str, int c )
{
	char *lower = strchr( str, TOLOWER(c) );
	char *upper = strchr( str, TOUPPER(c) );
93

94
95
96
97
98
99
100
101
	if( lower && upper ) {
		return lower < upper ? lower : upper;
	} else if ( lower ) {
		return lower;
	} else {
		return upper;
	}
}
102

103
104
105
static int
octetStringMatch(
	int *matchp,
106
	slap_mask_t flags,
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	int match = value->bv_len - ((struct berval *) assertedValue)->bv_len;

	if( match == 0 ) {
		match = memcmp( value->bv_val,
			((struct berval *) assertedValue)->bv_val,
			value->bv_len );
	}

	*matchp = match;
	return LDAP_SUCCESS;
}

/* Index generation function */
int octetStringIndexer(
126
127
	slap_mask_t use,
	slap_mask_t flags,
128
129
130
131
132
133
134
135
136
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	int i;
	size_t slen, mlen;
	struct berval **keys;
137
	HASH_CONTEXT   HASHcontext;
Gary Williams's avatar
Gary Williams committed
138
	unsigned char	HASHdigest[HASH_BYTES];
139
	struct berval digest;
140
141
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
142
143
144
145
146

	for( i=0; values[i] != NULL; i++ ) {
		/* just count them */
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
147
148
149
	/* we should have at least one value at this point */
	assert( i > 0 );

150
151
152
153
154
155
	keys = ch_malloc( sizeof( struct berval * ) * (i+1) );

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	for( i=0; values[i] != NULL; i++ ) {
156
		HASH_Init( &HASHcontext );
157
		if( prefix != NULL && prefix->bv_len > 0 ) {
158
			HASH_Update( &HASHcontext,
159
160
				prefix->bv_val, prefix->bv_len );
		}
161
		HASH_Update( &HASHcontext,
162
			syntax->ssyn_oid, slen );
163
		HASH_Update( &HASHcontext,
164
			mr->smr_oid, mlen );
165
		HASH_Update( &HASHcontext,
166
			values[i]->bv_val, values[i]->bv_len );
167
		HASH_Final( HASHdigest, &HASHcontext );
168
169
170
171
172
173
174
175
176
177
178
179
180

		keys[i] = ber_bvdup( &digest );
	}

	keys[i] = NULL;

	*keysp = keys;

	return LDAP_SUCCESS;
}

/* Index generation function */
int octetStringFilter(
181
182
	slap_mask_t use,
	slap_mask_t flags,
183
184
185
186
187
188
189
190
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	void * assertValue,
	struct berval ***keysp )
{
	size_t slen, mlen;
	struct berval **keys;
191
	HASH_CONTEXT   HASHcontext;
Gary Williams's avatar
Gary Williams committed
192
	unsigned char	HASHdigest[HASH_BYTES];
193
194
	struct berval *value = (struct berval *) assertValue;
	struct berval digest;
195
196
	digest.bv_val = HASHdigest;
	digest.bv_len = sizeof(HASHdigest);
197
198
199
200
201
202

	slen = strlen( syntax->ssyn_oid );
	mlen = strlen( mr->smr_oid );

	keys = ch_malloc( sizeof( struct berval * ) * 2 );

203
	HASH_Init( &HASHcontext );
204
	if( prefix != NULL && prefix->bv_len > 0 ) {
205
		HASH_Update( &HASHcontext,
206
207
			prefix->bv_val, prefix->bv_len );
	}
208
	HASH_Update( &HASHcontext,
209
		syntax->ssyn_oid, slen );
210
	HASH_Update( &HASHcontext,
211
		mr->smr_oid, mlen );
212
	HASH_Update( &HASHcontext,
213
		value->bv_val, value->bv_len );
214
	HASH_Final( HASHdigest, &HASHcontext );
215
216
217
218
219
220
221
222

	keys[0] = ber_bvdup( &digest );
	keys[1] = NULL;

	*keysp = keys;

	return LDAP_SUCCESS;
}
223

224
#ifdef USE_LDAP_DN_PARSING
225

Pierangelo Masarati's avatar
Pierangelo Masarati committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * 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.
 */

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#define	AVA_PRIVATE( ava ) ( ( AttributeDescription * )(ava)->la_private )

/*
 * In-place, schema-aware validation of the
 * structural representation of a distinguished name.
 */
static int
LDAPDN_validate( LDAPDN *dn )
{
	int 		iRDN;
	int 		rc;

	assert( dn );

	for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
		LDAPRDN		*rdn = dn[ iRDN ][ 0 ];
		int		iAVA;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
259
260
		assert( rdn );

261
262
263
264
		for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
			LDAPAVA			*ava = rdn[ iAVA ][ 0 ];
			AttributeDescription	*ad;
			slap_syntax_validate_func *validate = NULL;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
265
266

			assert( ava );
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
			
			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
			 */
			ber_bvfree( ava->la_attr );
			ava->la_attr = ber_bvdup( &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;
				}
			}
		}
	}

	return LDAP_SUCCESS;
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
304
305
306
/*
 * dn validate routine
 */
307
308
309
310
311
312
313
314
static int
dnValidate(
	Syntax *syntax,
	struct berval *in )
{
	int		rc;
	LDAPDN		*dn = NULL;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
315
316
	assert( in );

317
318
319
320
	if ( in->bv_len == 0 ) {
		return( LDAP_SUCCESS );
	}

321
322
323
	rc = ldap_str2dn( in->bv_val, &dn, LDAP_DN_FORMAT_LDAP );

	/*
324
	 * Schema-aware validate
325
	 */
326
327
328
329
	if ( rc == LDAP_SUCCESS ) {
		rc = LDAPDN_validate( dn );
	}
	
330
331
332
333
334
335
336
337
338
	ldapava_free_dn( dn );
	
	if ( rc != LDAP_SUCCESS ) {
		return( LDAP_INVALID_SYNTAX );
	}

	return( LDAP_SUCCESS );
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
339
340
341
342
343
344
345
346
347
348
349
/*
 * 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).
 *
 * uses a linear search; should be fine since the number of AVAs in
 * a RDN should be limited.
 */
350
351
352
353
354
static void
AVA_Sort( LDAPRDN *rdn, int iAVA )
{
	int		i;
	LDAPAVA		*ava_in = rdn[ iAVA ][ 0 ];
Pierangelo Masarati's avatar
Pierangelo Masarati committed
355
356
357

	assert( rdn );
	assert( ava_in );
358
359
360
361
362
	
	for ( i = 0; i < iAVA; i++ ) {
		LDAPAVA		*ava = rdn[ i ][ 0 ];
		int		a, j;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
363
364
		assert( ava );

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
		a = strcmp( ava_in->la_attr->bv_val, ava->la_attr->bv_val );

		if ( a > 0 ) {
			break;
		}

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

			d = ava_in->la_value->bv_len - ava->la_value->bv_len;

			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 );

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

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

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

			ava = rdn[ i ][ 0 ];
			a = strcmp( ava_in->la_value->bv_val, 
					ava->la_value->bv_val );
		}

		/*
		 * move ahead
		 */
		for ( j = iAVA; j > i; j-- ) {
			rdn[ j ][ 0 ] = rdn[ j - 1 ][ 0 ];
		}
		rdn[ i ][ 0 ] = ava_in;

		return;
	}
}

/*
417
418
 * In-place, schema-aware normalization / "pretty"ing of the
 * structural representation of a distinguished name.
419
420
 */
static int
421
LDAPDN_rewrite( LDAPDN *dn, unsigned flags )
422
423
424
425
426
427
428
429
430
431
{
	int 		iRDN;
	int 		rc;

	assert( dn );

	for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
		LDAPRDN		*rdn = dn[ iRDN ][ 0 ];
		int		iAVA;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
432
433
		assert( rdn );

434
435
		for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
			LDAPAVA			*ava = rdn[ iAVA ][ 0 ];
436
			AttributeDescription	*ad;
437
438
			slap_syntax_transform_func *transf = NULL;
			MatchingRule *mr;
439
440
			struct berval		*bv = NULL;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
441
442
			assert( ava );

443
444
445
446
447
448
449
450
451
			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;
452
453
454
			}

			/* 
455
			 * Replace attr oid/name with the canonical name
456
457
458
			 */
			ber_bvfree( ava->la_attr );
			ava->la_attr = ber_bvdup( &ad->ad_cname );
459
460
461
462
463
464
465

			if( flags & SLAP_LDAPDN_PRETTY ) {
				transf = ad->ad_type->sat_syntax->ssyn_pretty;
				mr = NULL;
			} else {
				transf = ad->ad_type->sat_syntax->ssyn_normalize;
				mr = ad->ad_type->sat_equality;
466
			}
467
468
469
470
471
472
473

			if ( transf ) {
				/*
			 	 * transform value by normalize/pretty function
				 */
				rc = ( *transf )( ad->ad_type->sat_syntax,
					ava->la_value, &bv );
474
			
475
476
477
				if ( rc != LDAP_SUCCESS ) {
					return LDAP_INVALID_SYNTAX;
				}
478
479
			}

480
			if( mr && ( mr->smr_usage & SLAP_MR_DN_FOLD ) ) {
481
				struct berval *s = bv;
482
483
484
485

				bv = ber_bvstr( UTF8normalize( bv ? bv : ava->la_value, 
					UTF8_CASEFOLD ) );

486
487
488
				ber_bvfree( s );
			}

489
490
491
492
			if( bv ) {
				ber_bvfree( ava->la_value );
				ava->la_value = bv;
			}
493
494
495
496
497

			AVA_Sort( rdn, iAVA );
		}
	}

498
	return LDAP_SUCCESS;
499
500
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
501
502
503
/*
 * dn normalize routine
 */
504
505
506
507
508
509
510
511
int
dnNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	struct berval *out = NULL;

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

Pierangelo Masarati's avatar
Pierangelo Masarati committed
514
515
516
	assert( val );
	assert( normalized );

517
518
519
	if ( val->bv_len != 0 ) {
		LDAPDN		*dn = NULL;
		char		*dn_out = NULL;
520
		int		rc;
521

522
523
524
		/*
		 * Go to structural representation
		 */
525
		rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
526
		if ( rc != LDAP_SUCCESS ) {
527
			return LDAP_INVALID_SYNTAX;
528
529
		}

530
		/*
531
		 * Schema-aware rewrite
532
		 */
533
534
535
		if ( LDAPDN_rewrite( dn, 0 ) != LDAP_SUCCESS ) {
			ldapava_free_dn( dn );
			return LDAP_INVALID_SYNTAX;
536
537
		}

538
539
540
		/*
		 * Back to string representation
		 */
541
542
		rc = ldap_dn2str( dn, &dn_out, LDAP_DN_FORMAT_LDAPV3 );

543
544
		ldapava_free_dn( dn );

545
		if ( rc != LDAP_SUCCESS ) {
546
			return LDAP_INVALID_SYNTAX;
547
548
549
550
551
552
553
554
		}

		out = ber_bvstr( dn_out );

	} else {
		out = ber_bvdup( val );
	}

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

557
558
	*normalized = out;

559
	return LDAP_SUCCESS;
560
561
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
562
563
564
/*
 * dn "pretty"ing routine
 */
565
566
567
568
int
dnPretty(
	Syntax *syntax,
	struct berval *val,
569
	struct berval **pretty)
570
571
572
{
	struct berval *out = NULL;

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

Pierangelo Masarati's avatar
Pierangelo Masarati committed
575
576
577
	assert( val );
	assert( pretty );

578
579
580
581
582
	if ( val->bv_len != 0 ) {
		LDAPDN		*dn = NULL;
		char		*dn_out = NULL;
		int		rc;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
583
		/* FIXME: should be liberal in what we accept */
584
		rc = ldap_str2dn( val->bv_val, &dn, LDAP_DN_FORMAT_LDAP );
585
		if ( rc != LDAP_SUCCESS ) {
586
			return LDAP_INVALID_SYNTAX;
587
588
		}

589
590
		/*
		 * Schema-aware rewrite
Kurt Zeilenga's avatar
Kurt Zeilenga committed
591
		 */
592
593
594
595
		if ( LDAPDN_rewrite( dn, SLAP_LDAPDN_PRETTY ) != LDAP_SUCCESS ) {
			ldapava_free_dn( dn );
			return LDAP_INVALID_SYNTAX;
		}
596

Kurt Zeilenga's avatar
Kurt Zeilenga committed
597
		/* FIXME: not sure why the default isn't pretty */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
598
599
600
		/* RE: the default is the form that is used as
		 * an internal representation; the pretty form
		 * is a variant */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
601
602
		rc = ldap_dn2str( dn, &dn_out,
			LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
603

604
605
606
		ldapava_free_dn( dn );

		if ( rc != LDAP_SUCCESS ) {
607
			return LDAP_INVALID_SYNTAX;
608
609
610
611
612
613
614
615
		}

		out = ber_bvstr( dn_out );

	} else {
		out = ber_bvdup( val );
	}

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

618
	*pretty = out;
619

620
	return LDAP_SUCCESS;
621
622
}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
623
624
625
626
627
628
/*
 * dn match routine
 *
 * note: uses exact string match (strcmp) because it is supposed to work
 * on normalized DNs.
 */
629
630
631
632
633
634
635
636
637
638
639
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;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
640
641
642
643

	assert( matchp );
	assert( value );
	assert( assertedValue );
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
	
	match = value->bv_len - asserted->bv_len;

	if ( match == 0 ) {
		match = strcmp( value->bv_val, asserted->bv_val );
	}

#ifdef NEW_LOGGING
	LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
		"dnMatch: %d\n    %s\n    %s\n", match,
		value->bv_val, asserted->bv_val ));
#else
	Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
		match, value->bv_val, asserted->bv_val );
#endif

	*matchp = match;
	return( LDAP_SUCCESS );
}

#else /* !USE_LDAP_DN_PARSING */

666
667
668
669
670
671
672
673
674
675
static int
dnValidate(
	Syntax *syntax,
	struct berval *in )
{
	int rc;
	char *dn;

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

676
	dn = ch_strdup( in->bv_val );
677

678
	if( dn == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
679
		return LDAP_INVALID_SYNTAX;
680

681
	} else if ( strlen( in->bv_val ) != in->bv_len ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
682
		rc = LDAP_INVALID_SYNTAX;
683

684
	} else if ( dn_validate( dn ) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
685
		rc = LDAP_INVALID_SYNTAX;
686
687
688
689
690
691

	} else {
		rc = LDAP_SUCCESS;
	}

	ch_free( dn );
692
693
694
	return rc;
}

695
int
696
697
698
699
700
dnNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
701
	struct berval *out;
702

703
704
	if ( val->bv_len != 0 ) {
		char *dn;
705
		out = ber_bvstr( UTF8normalize( val, UTF8_CASEFOLD ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
706

707
708
		dn = dn_validate( out->bv_val );

709
		if( dn == NULL ) {
710
711
712
			ber_bvfree( out );
			return LDAP_INVALID_SYNTAX;
		}
713

714
715
716
717
		out->bv_val = dn;
		out->bv_len = strlen( dn );
	} else {
		out = ber_bvdup( val );
718
719
720
721
722
723
	}

	*normalized = out;
	return LDAP_SUCCESS;
}

724
int
725
dnMatch(
726
	int *matchp,
727
	slap_mask_t flags,
728
729
730
731
732
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
733
	int match;
734
735
	struct berval *asserted = (struct berval *) assertedValue;
	
736
737
738
	match = value->bv_len - asserted->bv_len;

	if( match == 0 ) {
739
#ifdef USE_DN_NORMALIZE
740
		match = strcmp( value->bv_val, asserted->bv_val );
741
#else
742
		match = strcasecmp( value->bv_val, asserted->bv_val );
743
#endif
744
745
	}

746
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
747
	LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
748
749
		"dnMatch: %d\n    %s\n    %s\n", match,
		value->bv_val, asserted->bv_val ));
750
#else
751
	Debug( LDAP_DEBUG_ARGS, "dnMatch %d\n\t\"%s\"\n\t\"%s\"\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
752
		match, value->bv_val, asserted->bv_val );
753
754
#endif

755
756
757

	*matchp = match;
	return LDAP_SUCCESS;
758
}
759

760
761
#endif /* !USE_LDAP_DN_PARSING */

762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
static int
nameUIDValidate(
	Syntax *syntax,
	struct berval *in )
{
	int rc;
	struct berval *dn;

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

	dn = ber_bvdup( in );

	if( dn->bv_val[dn->bv_len-1] == '\'' ) {
		/* assume presence of optional UID */
		ber_len_t i;

		for(i=dn->bv_len-2; i>2; i--) {
			if( dn->bv_val[i] != '0' &&	dn->bv_val[i] != '1' ) {
				break;
			}
		}
Stig Venaas's avatar
Stig Venaas committed
783
784
785
786
		if( dn->bv_val[i] != '\'' ||
		    dn->bv_val[i-1] != 'B' ||
		    dn->bv_val[i-2] != '#' ) {
			ber_bvfree( dn );
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
			return LDAP_INVALID_SYNTAX;
		}

		/* trim the UID to allow use of dn_validate */
		dn->bv_val[i-2] = '\0';
	}

	rc = dn_validate( dn->bv_val ) == NULL
		? LDAP_INVALID_SYNTAX : LDAP_SUCCESS;

	ber_bvfree( dn );
	return rc;
}

static int
nameUIDNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	struct berval *out = ber_bvdup( val );

	if( out->bv_len != 0 ) {
		char *dn;
		ber_len_t dnlen;
		char *uid = NULL;
		ber_len_t uidlen = 0;

		if( out->bv_val[out->bv_len-1] == '\'' ) {
			/* assume presence of optional UID */
			uid = strrchr( out->bv_val, '#' );

			if( uid == NULL ) {
				ber_bvfree( out );
				return LDAP_INVALID_SYNTAX;
			}

			uidlen = out->bv_len - (out->bv_val - uid);
			/* temporarily trim the UID */
			*uid = '\0';
		}

#ifdef USE_DN_NORMALIZE
		dn = dn_normalize( out->bv_val );
#else
		dn = dn_validate( out->bv_val );
#endif

		if( dn == NULL ) {
			ber_bvfree( out );
			return LDAP_INVALID_SYNTAX;
		}

		dnlen = strlen(dn);

		if( uidlen ) {
			/* restore the separator */
			*uid = '#';
			/* shift the UID */
			SAFEMEMCPY( &dn[dnlen], uid, uidlen );
		}

		out->bv_val = dn;
		out->bv_len = dnlen + uidlen;
	}

	*normalized = out;
	return LDAP_SUCCESS;
}

857
858
859
860
861
862
863
864
865
static int
inValidate(
	Syntax *syntax,
	struct berval *in )
{
	/* any value allowed */
	return LDAP_OTHER;
}

866
static int
867
blobValidate(
868
869
870
871
	Syntax *syntax,
	struct berval *in )
{
	/* any value allowed */
872
	return LDAP_SUCCESS;
873
874
}

875
876
877
878
879
880
881
882
883
884
885
886
887
static int
bitStringValidate(
	Syntax *syntax,
	struct berval *in )
{
	ber_len_t i;

	/* very unforgiving validation, requires no normalization
	 * before simplistic matching
	 */
	if( in->bv_len < 3 ) {
		return LDAP_INVALID_SYNTAX;
	}
888

889
890
891
892
893
894
895
896
897
898
	/*
	 * rfc 2252 section 6.3 Bit String
	 * bitstring = "'" *binary-digit "'"
	 * binary-digit = "0" / "1"
	 * example: '0101111101'B
	 */
	
	if( in->bv_val[0] != '\'' ||
		in->bv_val[in->bv_len-2] != '\'' ||
		in->bv_val[in->bv_len-1] != 'B' )
899
900
901
902
	{
		return LDAP_INVALID_SYNTAX;
	}

903
	for( i=in->bv_len-3; i>0; i-- ) {
904
905
906
907
908
909
910
911
		if( in->bv_val[i] != '0' && in->bv_val[i] != '1' ) {
			return LDAP_INVALID_SYNTAX;
		}
	}

	return LDAP_SUCCESS;
}

912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
static int
bitStringNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	/*
     * A normalized bitString is has no extaneous (leading) zero bits.
	 * That is, '00010'B is normalized to '10'B
	 * However, as a special case, '0'B requires no normalization.
     */
	struct berval *newval;
	char *p;

	/* start at the first bit */
	p = &val->bv_val[1];

	/* Find the first non-zero bit */
	while ( *p == '0' ) p++;

	newval = (struct berval *) ch_malloc( sizeof(struct berval) );

	if( *p == '\'' ) {
		/* no non-zero bits */
		newval->bv_val = ch_strdup("\'0\'B");
		newval->bv_len = sizeof("\'0\'B") - 1;
		goto done;
	}

	newval->bv_val = ch_malloc( val->bv_len + 1 );

	newval->bv_val[0] = '\'';
	newval->bv_len = 1;

	for( ; *p != '\0'; p++ ) {
		newval->bv_val[newval->bv_len++] = *p;
	}

	newval->bv_val[newval->bv_len] = '\0';

done:
	*normalized = newval;
	return LDAP_SUCCESS;
}

957
958
959
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
/*
 * Handling boolean syntax and matching is quite rigid.
 * A more flexible approach would be to allow a variety
 * of strings to be normalized and prettied into TRUE
 * and FALSE.
 */
static int
booleanValidate(
	Syntax *syntax,
	struct berval *in )
{
	/* very unforgiving validation, requires no normalization
	 * before simplistic matching
	 */

	if( in->bv_len == 4 ) {
		if( !memcmp( in->bv_val, "TRUE", 4 ) ) {
			return LDAP_SUCCESS;
		}
	} else if( in->bv_len == 5 ) {
		if( !memcmp( in->bv_val, "FALSE", 5 ) ) {
			return LDAP_SUCCESS;
		}
	}

	return LDAP_INVALID_SYNTAX;
}

static int
booleanMatch(
	int *matchp,
988
	slap_mask_t flags,
989
990
991
992
993
994
995
996
997
998
999
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
	/* simplistic matching allowed by rigid validation */
	struct berval *asserted = (struct berval *) assertedValue;
	*matchp = value->bv_len != asserted->bv_len;
	return LDAP_SUCCESS;
}

1000
1001
1002
1003
1004
1005
1006
1007
1008
static int
UTF8StringValidate(
	Syntax *syntax,
	struct berval *in )
{
	ber_len_t count;
	int len;
	unsigned char *u = in->bv_val;

1009
1010
	if( !in->bv_len ) return LDAP_INVALID_SYNTAX;

1011
	for( count = in->bv_len; count > 0; count-=len, u+=len ) {
1012
1013
1014
1015
		/* get the length indicated by the first byte */
		len = LDAP_UTF8_CHARLEN( u );

		/* should not be zero */
1016
		if( len == 0 ) return LDAP_INVALID_SYNTAX;
1017
1018
1019

		/* make sure len corresponds with the offset
			to the next character */
1020
		if( LDAP_UTF8_OFFSET( u ) != len ) return LDAP_INVALID_SYNTAX;
1021
1022
	}

1023
	if( count != 0 ) return LDAP_INVALID_SYNTAX;
1024

1025
	return LDAP_SUCCESS;
1026
1027
1028
1029
1030
1031
1032
1033
1034
}

static int
UTF8StringNormalize(
	Syntax *syntax,
	struct berval *val,
	struct berval **normalized )
{
	struct berval *newval;
1035
	char *p, *q, *s;
1036

1037
	newval = ch_malloc( sizeof( struct berval ) );
1038

1039
	p = val->bv_val;
1040

1041
1042
1043
	/* Ignore initial whitespace */
	while ( ldap_utf8_isspace( p ) ) {
		LDAP_UTF8_INCR( p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1044
	}
1045

1046
1047
	if( *p == '\0' ) {
		ch_free( newval );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1048
1049
		return LDAP_INVALID_SYNTAX;
	}
1050

1051
1052
1053
	newval->bv_val = ch_strdup( p );
	p = q = newval->bv_val;
	s = NULL;
1054

1055
1056
	while ( *p ) {
		int len;
1057

1058
1059
1060
1061
1062
		if ( ldap_utf8_isspace( p ) ) {
			len = LDAP_UTF8_COPY(q,p);
			s=q;
			p+=len;
			q+=len;
1063

1064
1065
1066
1067
			/* Ignore the extra whitespace */
			while ( ldap_utf8_isspace( p ) ) {
				LDAP_UTF8_INCR( p );
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1068
		} else {
1069
1070
1071
1072
			len = LDAP_UTF8_COPY(q,p);
			s=NULL;
			p+=len;
			q+=len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1073
		}
1074
1075
1076
1077
	}

	assert( *newval->bv_val );
	assert( newval->bv_val < p );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1078
	assert( q <= p );
1079

1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
	/* cannot start with a space */
	assert( !ldap_utf8_isspace(newval->bv_val) );

	/*
	 * If the string ended in space, backup the pointer one
	 * position.  One is enough because the above loop collapsed
	 * all whitespace to a single space.
	 */

	if ( s != NULL ) {
		q = s;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1091
	}
1092

1093
1094
1095
1096
1097
1098
1099
1100
	/* cannot end with a space */
	assert( !ldap_utf8_isspace( LDAP_UTF8_PREV(q) ) );

	/* null terminate */
	*q = '\0';

	newval->bv_len = q - newval->bv_val;
	*normalized = newval;
1101

1102
	return LDAP_SUCCESS;
1103
1104
}

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
/* Returns Unicode cannonically normalized copy of a substring assertion
 * Skipping attribute description */
SubstringsAssertion *
UTF8SubstringsassertionNormalize(
	SubstringsAssertion *sa,
	char casefold )
{
	SubstringsAssertion *nsa;
	int i;

	nsa = (SubstringsAssertion *)ch_calloc( 1, sizeof(SubstringsAssertion) );
	if( nsa == NULL ) {
		return NULL;
	}

	if( sa->sa_initial != NULL ) {
1121
		nsa->sa_initial = ber_bvstr( UTF8normalize( sa->sa_initial, casefold ) );
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
		if( nsa->sa_initial == NULL ) {
			goto err;
		}
	}

	if( sa->sa_any != NULL ) {
		for( i=0; sa->sa_any[i] != NULL; i++ ) {
			/* empty */
		}
		nsa->sa_any = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
		for( i=0; sa->sa_any[i] != NULL; i++ ) {
1133
			nsa->sa_any[i] = ber_bvstr( UTF8normalize( sa->sa_any[i], casefold ) );
1134
1135
1136
1137
1138
1139
1140
1141
			if( nsa->sa_any[i] == NULL ) {
				goto err;
			}
		}
		nsa->sa_any[i] = NULL;
	}

	if( sa->sa_final != NULL ) {
1142
		nsa->sa_final = ber_bvstr( UTF8normalize( sa->sa_final, casefold ) );
1143
1144
1145
1146
1147
1148
1149
1150
		if( nsa->sa_final == NULL ) {
			goto err;
		}
	}

	return nsa;

err:
1151
	ber_bvfree( nsa->sa_final );
1152
	ber_bvecfree( nsa->sa_any );
1153
	ber_bvfree( nsa->sa_initial );
1154
1155
1156
1157
	ch_free( nsa );
	return NULL;
}

1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
/* Strip characters with the 8th bit set */
char *
strip8bitChars(
	char *in )      
{
	char *p = in, *q;
  
	if( in == NULL ) {
		return NULL;
	}
	while( *p ) {
		if( *p & 0x80 ) {
			q = p;
			while( *++q & 0x80 ) {
				/* empty */
			}
			p = memmove(p, q, strlen(q) + 1);
		} else {
			p++;
		}
	}
	return in;
}

1182
#ifndef SLAPD_APPROX_OLDSINGLESTRING
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200

#if defined(SLAPD_APPROX_INITIALS)
#define SLAPD_APPROX_DELIMITER "._ "
#define SLAPD_APPROX_WORDLEN 2
#else
#define SLAPD_APPROX_DELIMITER " "
#define SLAPD_APPROX_WORDLEN 1
#endif

static int
approxMatch(
	int *matchp,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *value,
	void *assertedValue )
{
1201
	char *val, *nval, *assertv, **values, **words, *c;
1202
	int i, count, len, nextchunk=0, nextavail=0;
1203
	size_t avlen;
1204

1205
	/* Yes, this is necessary */
1206
	nval = UTF8normalize( value, UTF8_NOCASEFOLD );
1207
	if( nval == NULL ) {
1208
1209
1210
		*matchp = 1;
		return LDAP_SUCCESS;
	}
1211
	strip8bitChars( nval );
1212
1213

	/* Yes, this is necessary */
1214
	assertv = UTF8normalize( ((struct berval *)assertedValue),
1215
1216
				 UTF8_NOCASEFOLD );
	if( assertv == NULL ) {
1217
		ch_free( nval );
1218
1219
1220
1221
1222
		*matchp = 1;
		return LDAP_SUCCESS;
	}
	strip8bitChars( assertv );
	avlen = strlen( assertv );
1223
1224

	/* Isolate how many words there are */
1225
	for( c=nval,count=1; *c; c++ ) {
1226
1227
1228
1229
1230
1231
1232
1233
1234
		c = strpbrk( c, SLAPD_APPROX_DELIMITER );
		if ( c == NULL ) break;
		*c = '\0';
		count++;
	}

	/* Get a phonetic copy of each word */
	words = (char **)ch_malloc( count * sizeof(char *) );
	values = (char **)ch_malloc( count * sizeof(char *) );
1235
	for( c=nval,i=0;  i<count;  i++,c+=strlen(c)+1 ) {
1236
1237
1238
1239
		words[i] = c;
		values[i] = phonetic(c);
	}

1240
	/* Work through the asserted value's words, to see if at least some
1241
1242
	   of the words are there, in the same order. */
	len = 0;
1243
	while ( nextchunk < avlen ) {
1244
		len = strcspn( assertv + nextchunk, SLAPD_APPROX_DELIMITER);
1245
1246
1247
1248
		if( len == 0 ) {
			nextchunk++;
			continue;
		}
1249
#if defined(SLAPD_APPROX_INITIALS)
1250
		else if( len == 1 ) {
1251
1252
			/* Single letter words need to at least match one word's initial */
			for( i=nextavail; i<count; i++ )
1253
1254
				if( !strncasecmp( assertv+nextchunk, words[i], 1 )) {
					nextavail=i+1;
1255
					break;
1256
				}
1257
1258
		}
#endif
1259
		else {
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
			/* Isolate the next word in the asserted value and phonetic it */
			assertv[nextchunk+len] = '\0';
			val = phonetic( assertv + nextchunk );

			/* See if this phonetic chunk is in the remaining words of *value */
			for( i=nextavail; i<count; i++ ){
				if( !strcmp( val, values[i] ) ){
					nextavail = i+1;
					break;
				}
			}
1271
			ch_free( val );
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
		}

		/* This chunk in the asserted value was NOT within the *value. */
		if( i >= count ) {
			nextavail=-1;
			break;
		}

		/* Go on to the next word in the asserted value */
		nextchunk += len+1;
	}

	/* If some of the words were seen, call it a match */
	if( nextavail > 0 ) {
		*matchp = 0;
	}
	else {
		*matchp = 1;
	}

	/* Cleanup allocs */
1293
	free( assertv );
1294
1295
1296
1297
1298
	for( i=0; i<count; i++ ) {
		ch_free( values[i] );
	}
	ch_free( values );
	ch_free( words );
1299
	ch_free( nval );
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318

	return LDAP_SUCCESS;
}

int 
approxIndexer(
	slap_mask_t use,
	slap_mask_t flags,
	Syntax *syntax,
	MatchingRule *mr,
	struct berval *prefix,
	struct berval **values,
	struct berval ***keysp )
{
	char *val, *c;
	int i,j, len, wordcount, keycount=0;
	struct berval **newkeys, **keys=NULL;

	for( j=0; values[j] != NULL; j++ ) {
1319
		/* Yes, this is necessary */
1320
		val = UTF8normalize( values[j], UTF8_NOCASEFOLD );
1321
1322
		strip8bitChars( val );

1323
		/* Isolate how many words there are. There will be a key for each */
Gary Williams's avatar
Gary Williams committed
1324
		for( wordcount=0,c=val;	 *c;  c++) {
1325
1326
1327
1328
1329
1330
1331
1332
1333
			len = strcspn(c, SLAPD_APPROX_DELIMITER);
			if( len >= SLAPD_APPROX_WORDLEN ) wordcount++;
			c+= len;
			if (*c == '\0') break;
			*c = '\0';
		}

		/* Allocate/increase storage to account for new keys */
		newkeys = (struct berval **)ch_malloc( (keycount + wordcount + 1)