syntax.c 9.29 KB
Newer Older
1
2
/* syntax.c - routines to manage syntax definitions */
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 1998-2019 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * 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>.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 */

#include "portable.h"

#include <stdio.h>

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

#include "slap.h"

struct sindexrec {
	char		*sir_name;
	Syntax		*sir_syn;
};

static Avlnode	*syn_index = NULL;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
33
34
35
36
37
static LDAP_STAILQ_HEAD(SyntaxList, Syntax) syn_list
	= LDAP_STAILQ_HEAD_INITIALIZER(syn_list);

/* Last hardcoded attribute registered */
Syntax *syn_sys_tail;
38
39
40

static int
syn_index_cmp(
41
42
	const void *v_sir1,
	const void *v_sir2
43
44
)
{
45
	const struct sindexrec *sir1 = v_sir1, *sir2 = v_sir2;
46
47
48
49
50
	return (strcmp( sir1->sir_name, sir2->sir_name ));
}

static int
syn_index_name_cmp(
51
52
	const void *name,
	const void *sir
53
54
)
{
55
	return (strcmp( name, ((const struct sindexrec *)sir)->sir_name ));
56
57
58
59
60
61
62
}

Syntax *
syn_find( const char *synname )
{
	struct sindexrec	*sir = NULL;

63
	if ( (sir = avl_find( syn_index, synname, syn_index_name_cmp )) != NULL ) {
64
65
66
67
68
69
70
71
72
73
		return( sir->sir_syn );
	}
	return( NULL );
}

Syntax *
syn_find_desc( const char *syndesc, int *len )
{
	Syntax		*synp;

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
74
	LDAP_STAILQ_FOREACH(synp, &syn_list, ssyn_next) {
75
		if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{' /*'}'*/ ))) {
76
			return synp;
77
78
		}
	}
79
80
81
	return( NULL );
}

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
int
syn_is_sup( Syntax *syn, Syntax *sup )
{
	int	i;

	assert( syn != NULL );
	assert( sup != NULL );

	if ( syn == sup ) {
		return 1;
	}

	if ( syn->ssyn_sups == NULL ) {
		return 0;
	}

	for ( i = 0; syn->ssyn_sups[i]; i++ ) {
		if ( syn->ssyn_sups[i] == sup ) {
			return 1;
		}

		if ( syn_is_sup( syn->ssyn_sups[i], sup ) ) {
			return 1;
		}
	}

	return 0;
}

111
112
113
void
syn_destroy( void )
{
114
115
116
	Syntax	*s;

	avl_free( syn_index, ldap_memfree );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
117
118
119
	while( !LDAP_STAILQ_EMPTY( &syn_list ) ) {
		s = LDAP_STAILQ_FIRST( &syn_list );
		LDAP_STAILQ_REMOVE_HEAD( &syn_list, ssyn_next );
120
121
122
123
		if ( s->ssyn_sups ) {
			SLAP_FREE( s->ssyn_sups );
		}
		ldap_syntax_free( (LDAPSyntax *)s );
124
125
126
	}
}

127
128
static int
syn_insert(
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
129
130
131
	Syntax		*ssyn,
	Syntax		*prev,
	const char	**err )
132
133
134
{
	struct sindexrec	*sir;

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
135
	LDAP_STAILQ_NEXT( ssyn, ssyn_next ) = NULL;
136
 
137
138
	if ( ssyn->ssyn_oid ) {
		sir = (struct sindexrec *)
Julius Enarusai's avatar
   
Julius Enarusai committed
139
140
141
142
143
			SLAP_CALLOC( 1, sizeof(struct sindexrec) );
		if( sir == NULL ) {
			Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
			return LDAP_OTHER;
		}
144
145
146
		sir->sir_name = ssyn->ssyn_oid;
		sir->sir_syn = ssyn;
		if ( avl_insert( &syn_index, (caddr_t) sir,
147
		                 syn_index_cmp, avl_dup_error ) ) {
148
149
			*err = ssyn->ssyn_oid;
			ldap_memfree(sir);
150
			return SLAP_SCHERR_SYN_DUP;
151
152
153
154
		}
		/* FIX: temporal consistency check */
		syn_find(sir->sir_name);
	}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
155
156
157
158
159
160
161
162
163
164
165

	if ( ssyn->ssyn_flags & SLAP_AT_HARDCODE ) {
		prev = syn_sys_tail;
		syn_sys_tail = ssyn;
	}

	if ( prev ) {
		LDAP_STAILQ_INSERT_AFTER( &syn_list, prev, ssyn, ssyn_next );
	} else {
		LDAP_STAILQ_INSERT_TAIL( &syn_list, ssyn, ssyn_next );
	}
166
167
168
169
170
	return 0;
}

int
syn_add(
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
171
172
173
174
175
176
	LDAPSyntax		*syn,
	int			user,
	slap_syntax_defs_rec	*def,
	Syntax			**ssynp,
	Syntax			*prev,
	const char		**err )
177
178
{
	Syntax		*ssyn;
179
	int		code = 0;
180

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
181
182
183
184
	if ( ssynp != NULL ) {
		*ssynp = NULL;
	}

Julius Enarusai's avatar
   
Julius Enarusai committed
185
	ssyn = (Syntax *) SLAP_CALLOC( 1, sizeof(Syntax) );
186
	if ( ssyn == NULL ) {
Julius Enarusai's avatar
   
Julius Enarusai committed
187
		Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
188
		return SLAP_SCHERR_OUTOFMEM;
Julius Enarusai's avatar
   
Julius Enarusai committed
189
	}
190

Kurt Zeilenga's avatar
Kurt Zeilenga committed
191
	AC_MEMCPY( &ssyn->ssyn_syn, syn, sizeof(LDAPSyntax) );
192

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
193
	LDAP_STAILQ_NEXT(ssyn,ssyn_next) = NULL;
194

195
196
197
198
199
	/*
	 * note: ssyn_bvoid uses the same memory of ssyn_syn.syn_oid;
	 * ssyn_oidlen is #defined as ssyn_bvoid.bv_len
	 */
	ssyn->ssyn_bvoid.bv_val = ssyn->ssyn_syn.syn_oid;
200
	ssyn->ssyn_oidlen = strlen(syn->syn_oid);
201
202
203
	ssyn->ssyn_flags = def->sd_flags;
	ssyn->ssyn_validate = def->sd_validate;
	ssyn->ssyn_pretty = def->sd_pretty;
204

205
206
	ssyn->ssyn_sups = NULL;

207
#ifdef SLAPD_BINARY_CONVERSION
208
209
	ssyn->ssyn_ber2str = def->sd_ber2str;
	ssyn->ssyn_str2ber = def->sd_str2ber;
210
#endif
211

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
	if ( def->sd_validate == NULL && def->sd_pretty == NULL && syn->syn_extensions != NULL ) {
		LDAPSchemaExtensionItem **lsei;
		Syntax *subst = NULL;

		for ( lsei = syn->syn_extensions; *lsei != NULL; lsei++) {
			if ( strcmp( (*lsei)->lsei_name, "X-SUBST" ) != 0 ) {
				continue;
			}

			assert( (*lsei)->lsei_values != NULL );
			if ( (*lsei)->lsei_values[0] == '\0'
				|| (*lsei)->lsei_values[1] != '\0' )
			{
				Debug( LDAP_DEBUG_ANY, "syn_add(%s): exactly one substitute syntax must be present\n",
					ssyn->ssyn_syn.syn_oid, 0, 0 );
Howard Chu's avatar
Howard Chu committed
227
				SLAP_FREE( ssyn );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
228
229
230
231
232
233
234
				return SLAP_SCHERR_SYN_SUBST_NOT_SPECIFIED;
			}

			subst = syn_find( (*lsei)->lsei_values[0] );
			if ( subst == NULL ) {
				Debug( LDAP_DEBUG_ANY, "syn_add(%s): substitute syntax %s not found\n",
					ssyn->ssyn_syn.syn_oid, (*lsei)->lsei_values[0], 0 );
Howard Chu's avatar
Howard Chu committed
235
				SLAP_FREE( ssyn );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
				return SLAP_SCHERR_SYN_SUBST_NOT_FOUND;
			}
			break;
		}

		if ( subst != NULL ) {
			ssyn->ssyn_flags = subst->ssyn_flags;
			ssyn->ssyn_validate = subst->ssyn_validate;
			ssyn->ssyn_pretty = subst->ssyn_pretty;

			ssyn->ssyn_sups = NULL;

#ifdef SLAPD_BINARY_CONVERSION
			ssyn->ssyn_ber2str = subst->ssyn_ber2str;
			ssyn->ssyn_str2ber = subst->ssyn_str2ber;
#endif
		}
	}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
	if ( def->sd_sups != NULL ) {
		int	cnt;

		for ( cnt = 0; def->sd_sups[cnt] != NULL; cnt++ )
			;
		
		ssyn->ssyn_sups = (Syntax **)SLAP_CALLOC( cnt + 1,
			sizeof( Syntax * ) );
		if ( ssyn->ssyn_sups == NULL ) {
			Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
			code = SLAP_SCHERR_OUTOFMEM;

		} else {
			for ( cnt = 0; def->sd_sups[cnt] != NULL; cnt++ ) {
				ssyn->ssyn_sups[cnt] = syn_find( def->sd_sups[cnt] );
				if ( ssyn->ssyn_sups[cnt] == NULL ) {
					*err = def->sd_sups[cnt];
					code = SLAP_SCHERR_SYN_SUP_NOT_FOUND;
				}
			}
		}
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
278
279
	if ( !user )
		ssyn->ssyn_flags |= SLAP_SYNTAX_HARDCODE;
280

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
281
282
	if ( code == 0 ) {
		code = syn_insert( ssyn, prev, err );
283
284
285
286
287
288
289
	}

	if ( code != 0 && ssyn != NULL ) {
		if ( ssyn->ssyn_sups != NULL ) {
			SLAP_FREE( ssyn->ssyn_sups );
		}
		SLAP_FREE( ssyn );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
290
291
292
293
294
		ssyn = NULL;
	}

	if (ssynp ) {
		*ssynp = ssyn;
295
296
	}

297
298
299
300
301
	return code;
}

int
register_syntax(
302
	slap_syntax_defs_rec *def )
303
{
304
	LDAPSyntax	*syn;
305
306
307
	int		code;
	const char	*err;

308
	syn = ldap_str2syntax( def->sd_desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
309
310
	if ( !syn ) {
		Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
311
		    ldap_scherr2str(code), err, def->sd_desc );
312

313
314
315
		return( -1 );
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
316
	code = syn_add( syn, 0, def, NULL, NULL, &err );
317

318
319
	if ( code ) {
		Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
320
		    scherr2str(code), err, def->sd_desc );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
321
		ldap_syntax_free( syn );
322

323
324
325
		return( -1 );
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
326
327
	ldap_memfree( syn );

328
329
330
331
332
333
	return( 0 );
}

int
syn_schema_info( Entry *e )
{
334
	AttributeDescription *ad_ldapSyntaxes = slap_schema.si_ad_ldapSyntaxes;
335
336
337
	Syntax		*syn;
	struct berval	val;
	struct berval	nval;
338

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
339
	LDAP_STAILQ_FOREACH(syn, &syn_list, ssyn_next ) {
340
341
342
343
344
345
346
347
348
		if ( ! syn->ssyn_validate ) {
			/* skip syntaxes without validators */
			continue;
		}
		if ( syn->ssyn_flags & SLAP_SYNTAX_HIDE ) {
			/* hide syntaxes */
			continue;
		}

349
		if ( ldap_syntax2bv( &syn->ssyn_syn, &val ) == NULL ) {
350
351
			return -1;
		}
352
#if 0
353
		Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
354
	       (long) val.bv_len, val.bv_val, 0 );
355
#endif
356
357
358
359
360

		nval.bv_val = syn->ssyn_oid;
		nval.bv_len = strlen(syn->ssyn_oid);

		if( attr_merge_one( e, ad_ldapSyntaxes, &val, &nval ) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
361
		{
Julius Enarusai's avatar
   
Julius Enarusai committed
362
			return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
363
		}
364
		ldap_memfree( val.bv_val );
365
366
367
368
	}
	return 0;
}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
void
syn_delete( Syntax *syn )
{
	LDAP_STAILQ_REMOVE(&syn_list, syn, Syntax, ssyn_next);
}

int
syn_start( Syntax **syn )
{
	assert( syn != NULL );

	*syn = LDAP_STAILQ_FIRST(&syn_list);

	return (*syn != NULL);
}

int
syn_next( Syntax **syn )
{
	assert( syn != NULL );

#if 0	/* pedantic check: don't use this */
	{
		Syntax *tmp = NULL;

		LDAP_STAILQ_FOREACH(tmp,&syn_list,ssyn_next) {
			if ( tmp == *syn ) {
				break;
			}
		}

		assert( tmp != NULL );
	}
#endif

	*syn = LDAP_STAILQ_NEXT(*syn,ssyn_next);

	return (*syn != NULL);
}

void
syn_unparse( BerVarray *res, Syntax *start, Syntax *end, int sys )
{
	Syntax *syn;
	int i, num;
	struct berval bv, *bva = NULL, idx;
	char ibuf[32];

	if ( !start )
		start = LDAP_STAILQ_FIRST( &syn_list );

	/* count the result size */
	i = 0;
	for ( syn = start; syn; syn = LDAP_STAILQ_NEXT( syn, ssyn_next ) ) {
		if ( sys && !( syn->ssyn_flags & SLAP_SYNTAX_HARDCODE ) ) break;
		i++;
		if ( syn == end ) break;
	}
	if ( !i ) return;

	num = i;
	bva = ch_malloc( (num+1) * sizeof(struct berval) );
	BER_BVZERO( bva );
	idx.bv_val = ibuf;
	if ( sys ) {
		idx.bv_len = 0;
		ibuf[0] = '\0';
	}
	i = 0;
	for ( syn = start; syn; syn = LDAP_STAILQ_NEXT( syn, ssyn_next ) ) {
		if ( sys && !( syn->ssyn_flags & SLAP_SYNTAX_HARDCODE ) ) break;
		if ( ldap_syntax2bv( &syn->ssyn_syn, &bv ) == NULL ) {
			ber_bvarray_free( bva );
		}
		if ( !sys ) {
			idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
		}
		bva[i].bv_len = idx.bv_len + bv.bv_len;
		bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
		strcpy( bva[i].bv_val, ibuf );
		strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
		i++;
		bva[i].bv_val = NULL;
		ldap_memfree( bv.bv_val );
		if ( syn == end ) break;
	}
	*res = bva;
}