1 /* syntax.c - routines to manage syntax definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
32 static Avlnode *syn_index = NULL;
33 static LDAP_STAILQ_HEAD(SyntaxList, Syntax) syn_list
34 = LDAP_STAILQ_HEAD_INITIALIZER(syn_list);
36 /* Last hardcoded attribute registered */
45 const struct sindexrec *sir1 = v_sir1, *sir2 = v_sir2;
46 return (strcmp( sir1->sir_name, sir2->sir_name ));
55 return (strcmp( name, ((const struct sindexrec *)sir)->sir_name ));
59 syn_find( const char *synname )
61 struct sindexrec *sir = NULL;
63 if ( (sir = avl_find( syn_index, synname, syn_index_name_cmp )) != NULL ) {
64 return( sir->sir_syn );
70 syn_find_desc( const char *syndesc, int *len )
74 LDAP_STAILQ_FOREACH(synp, &syn_list, ssyn_next) {
75 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{' /*'}'*/ ))) {
83 syn_is_sup( Syntax *syn, Syntax *sup )
87 assert( syn != NULL );
88 assert( sup != NULL );
94 if ( syn->ssyn_sups == NULL ) {
98 for ( i = 0; syn->ssyn_sups[i]; i++ ) {
99 if ( syn->ssyn_sups[i] == sup ) {
103 if ( syn_is_sup( syn->ssyn_sups[i], sup ) ) {
116 avl_free( syn_index, ldap_memfree );
117 while( !LDAP_STAILQ_EMPTY( &syn_list ) ) {
118 s = LDAP_STAILQ_FIRST( &syn_list );
119 LDAP_STAILQ_REMOVE_HEAD( &syn_list, ssyn_next );
120 if ( s->ssyn_sups ) {
121 SLAP_FREE( s->ssyn_sups );
123 ldap_syntax_free( (LDAPSyntax *)s );
133 struct sindexrec *sir;
135 LDAP_STAILQ_NEXT( ssyn, ssyn_next ) = NULL;
137 if ( ssyn->ssyn_oid ) {
138 sir = (struct sindexrec *)
139 SLAP_CALLOC( 1, sizeof(struct sindexrec) );
141 Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
144 sir->sir_name = ssyn->ssyn_oid;
146 if ( avl_insert( &syn_index, (caddr_t) sir,
147 syn_index_cmp, avl_dup_error ) ) {
148 *err = ssyn->ssyn_oid;
150 return SLAP_SCHERR_SYN_DUP;
152 /* FIX: temporal consistency check */
153 syn_find(sir->sir_name);
156 if ( ssyn->ssyn_flags & SLAP_AT_HARDCODE ) {
162 LDAP_STAILQ_INSERT_AFTER( &syn_list, prev, ssyn, ssyn_next );
164 LDAP_STAILQ_INSERT_TAIL( &syn_list, ssyn, ssyn_next );
173 slap_syntax_defs_rec *def,
181 if ( ssynp != NULL ) {
185 ssyn = (Syntax *) SLAP_CALLOC( 1, sizeof(Syntax) );
186 if ( ssyn == NULL ) {
187 Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
188 return SLAP_SCHERR_OUTOFMEM;
191 AC_MEMCPY( &ssyn->ssyn_syn, syn, sizeof(LDAPSyntax) );
193 LDAP_STAILQ_NEXT(ssyn,ssyn_next) = NULL;
196 * note: ssyn_bvoid uses the same memory of ssyn_syn.syn_oid;
197 * ssyn_oidlen is #defined as ssyn_bvoid.bv_len
199 ssyn->ssyn_bvoid.bv_val = ssyn->ssyn_syn.syn_oid;
200 ssyn->ssyn_oidlen = strlen(syn->syn_oid);
201 ssyn->ssyn_flags = def->sd_flags;
202 ssyn->ssyn_validate = def->sd_validate;
203 ssyn->ssyn_pretty = def->sd_pretty;
205 ssyn->ssyn_sups = NULL;
207 #ifdef SLAPD_BINARY_CONVERSION
208 ssyn->ssyn_ber2str = def->sd_ber2str;
209 ssyn->ssyn_str2ber = def->sd_str2ber;
212 if ( def->sd_validate == NULL && def->sd_pretty == NULL && syn->syn_extensions != NULL ) {
213 LDAPSchemaExtensionItem **lsei;
214 Syntax *subst = NULL;
216 for ( lsei = syn->syn_extensions; *lsei != NULL; lsei++) {
217 if ( strcmp( (*lsei)->lsei_name, "X-SUBST" ) != 0 ) {
221 assert( (*lsei)->lsei_values != NULL );
222 if ( (*lsei)->lsei_values[0] == '\0'
223 || (*lsei)->lsei_values[1] != '\0' )
225 Debug( LDAP_DEBUG_ANY, "syn_add(%s): exactly one substitute syntax must be present\n",
226 ssyn->ssyn_syn.syn_oid, 0, 0 );
227 return SLAP_SCHERR_SYN_SUBST_NOT_SPECIFIED;
230 subst = syn_find( (*lsei)->lsei_values[0] );
231 if ( subst == NULL ) {
232 Debug( LDAP_DEBUG_ANY, "syn_add(%s): substitute syntax %s not found\n",
233 ssyn->ssyn_syn.syn_oid, (*lsei)->lsei_values[0], 0 );
234 return SLAP_SCHERR_SYN_SUBST_NOT_FOUND;
239 if ( subst != NULL ) {
240 ssyn->ssyn_flags = subst->ssyn_flags;
241 ssyn->ssyn_validate = subst->ssyn_validate;
242 ssyn->ssyn_pretty = subst->ssyn_pretty;
244 ssyn->ssyn_sups = NULL;
246 #ifdef SLAPD_BINARY_CONVERSION
247 ssyn->ssyn_ber2str = subst->ssyn_ber2str;
248 ssyn->ssyn_str2ber = subst->ssyn_str2ber;
253 if ( def->sd_sups != NULL ) {
256 for ( cnt = 0; def->sd_sups[cnt] != NULL; cnt++ )
259 ssyn->ssyn_sups = (Syntax **)SLAP_CALLOC( cnt + 1,
260 sizeof( Syntax * ) );
261 if ( ssyn->ssyn_sups == NULL ) {
262 Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
263 code = SLAP_SCHERR_OUTOFMEM;
266 for ( cnt = 0; def->sd_sups[cnt] != NULL; cnt++ ) {
267 ssyn->ssyn_sups[cnt] = syn_find( def->sd_sups[cnt] );
268 if ( ssyn->ssyn_sups[cnt] == NULL ) {
269 *err = def->sd_sups[cnt];
270 code = SLAP_SCHERR_SYN_SUP_NOT_FOUND;
277 ssyn->ssyn_flags |= SLAP_SYNTAX_HARDCODE;
280 code = syn_insert( ssyn, prev, err );
283 if ( code != 0 && ssyn != NULL ) {
284 if ( ssyn->ssyn_sups != NULL ) {
285 SLAP_FREE( ssyn->ssyn_sups );
300 slap_syntax_defs_rec *def )
306 syn = ldap_str2syntax( def->sd_desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
308 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
309 ldap_scherr2str(code), err, def->sd_desc );
314 code = syn_add( syn, 0, def, NULL, NULL, &err );
317 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
318 scherr2str(code), err, def->sd_desc );
319 ldap_syntax_free( syn );
330 syn_schema_info( Entry *e )
332 AttributeDescription *ad_ldapSyntaxes = slap_schema.si_ad_ldapSyntaxes;
337 LDAP_STAILQ_FOREACH(syn, &syn_list, ssyn_next ) {
338 if ( ! syn->ssyn_validate ) {
339 /* skip syntaxes without validators */
342 if ( syn->ssyn_flags & SLAP_SYNTAX_HIDE ) {
347 if ( ldap_syntax2bv( &syn->ssyn_syn, &val ) == NULL ) {
351 Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
352 (long) val.bv_len, val.bv_val, 0 );
355 nval.bv_val = syn->ssyn_oid;
356 nval.bv_len = strlen(syn->ssyn_oid);
358 if( attr_merge_one( e, ad_ldapSyntaxes, &val, &nval ) )
362 ldap_memfree( val.bv_val );
368 syn_delete( Syntax *syn )
370 LDAP_STAILQ_REMOVE(&syn_list, syn, Syntax, ssyn_next);
374 syn_start( Syntax **syn )
376 assert( syn != NULL );
378 *syn = LDAP_STAILQ_FIRST(&syn_list);
380 return (*syn != NULL);
384 syn_next( Syntax **syn )
386 assert( syn != NULL );
388 #if 0 /* pedantic check: don't use this */
392 LDAP_STAILQ_FOREACH(tmp,&syn_list,ssyn_next) {
398 assert( tmp != NULL );
402 *syn = LDAP_STAILQ_NEXT(*syn,ssyn_next);
404 return (*syn != NULL);
408 syn_unparse( BerVarray *res, Syntax *start, Syntax *end, int sys )
412 struct berval bv, *bva = NULL, idx;
416 start = LDAP_STAILQ_FIRST( &syn_list );
418 /* count the result size */
420 for ( syn = start; syn; syn = LDAP_STAILQ_NEXT( syn, ssyn_next ) ) {
421 if ( sys && !( syn->ssyn_flags & SLAP_SYNTAX_HARDCODE ) ) break;
423 if ( syn == end ) break;
428 bva = ch_malloc( (num+1) * sizeof(struct berval) );
436 for ( syn = start; syn; syn = LDAP_STAILQ_NEXT( syn, ssyn_next ) ) {
437 if ( sys && !( syn->ssyn_flags & SLAP_SYNTAX_HARDCODE ) ) break;
438 if ( ldap_syntax2bv( &syn->ssyn_syn, &bv ) == NULL ) {
439 ber_bvarray_free( bva );
442 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
444 bva[i].bv_len = idx.bv_len + bv.bv_len;
445 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
446 strcpy( bva[i].bv_val, ibuf );
447 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
449 bva[i].bv_val = NULL;
450 ldap_memfree( bv.bv_val );
451 if ( syn == end ) break;