1 /* syntax.c - routines to manage syntax definitions */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/syntax.c,v 1.43.2.3 2008/02/11 23:26:45 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 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_SLIST_HEAD(SyntaxList
, Syntax
) syn_list
34 = LDAP_SLIST_HEAD_INITIALIZER(&syn_list
);
42 const struct sindexrec
*sir1
= v_sir1
, *sir2
= v_sir2
;
43 return (strcmp( sir1
->sir_name
, sir2
->sir_name
));
52 return (strcmp( name
, ((const struct sindexrec
*)sir
)->sir_name
));
56 syn_find( const char *synname
)
58 struct sindexrec
*sir
= NULL
;
60 if ( (sir
= avl_find( syn_index
, synname
, syn_index_name_cmp
)) != NULL
) {
61 return( sir
->sir_syn
);
67 syn_find_desc( const char *syndesc
, int *len
)
71 LDAP_SLIST_FOREACH(synp
, &syn_list
, ssyn_next
) {
72 if ((*len
= dscompare( synp
->ssyn_syn
.syn_desc
, syndesc
, '{' /*'}'*/ ))) {
80 syn_is_sup( Syntax
*syn
, Syntax
*sup
)
84 assert( syn
!= NULL
);
85 assert( sup
!= NULL
);
91 if ( syn
->ssyn_sups
== NULL
) {
95 for ( i
= 0; syn
->ssyn_sups
[i
]; i
++ ) {
96 if ( syn
->ssyn_sups
[i
] == sup
) {
100 if ( syn_is_sup( syn
->ssyn_sups
[i
], sup
) ) {
113 avl_free( syn_index
, ldap_memfree
);
114 while( !LDAP_SLIST_EMPTY( &syn_list
) ) {
115 s
= LDAP_SLIST_FIRST( &syn_list
);
116 LDAP_SLIST_REMOVE_HEAD( &syn_list
, ssyn_next
);
117 if ( s
->ssyn_sups
) {
118 SLAP_FREE( s
->ssyn_sups
);
120 ldap_syntax_free( (LDAPSyntax
*)s
);
130 struct sindexrec
*sir
;
132 LDAP_SLIST_NEXT( ssyn
, ssyn_next
) = NULL
;
133 LDAP_SLIST_INSERT_HEAD( &syn_list
, ssyn
, ssyn_next
);
135 if ( ssyn
->ssyn_oid
) {
136 sir
= (struct sindexrec
*)
137 SLAP_CALLOC( 1, sizeof(struct sindexrec
) );
139 Debug( LDAP_DEBUG_ANY
, "SLAP_CALLOC Error\n", 0, 0, 0 );
142 sir
->sir_name
= ssyn
->ssyn_oid
;
144 if ( avl_insert( &syn_index
, (caddr_t
) sir
,
145 syn_index_cmp
, avl_dup_error
) ) {
146 *err
= ssyn
->ssyn_oid
;
148 return SLAP_SCHERR_SYN_DUP
;
150 /* FIX: temporal consistency check */
151 syn_find(sir
->sir_name
);
159 slap_syntax_defs_rec
*def
,
166 ssyn
= (Syntax
*) SLAP_CALLOC( 1, sizeof(Syntax
) );
167 if ( ssyn
== NULL
) {
168 Debug( LDAP_DEBUG_ANY
, "SLAP_CALLOC Error\n", 0, 0, 0 );
169 return SLAP_SCHERR_OUTOFMEM
;
172 AC_MEMCPY( &ssyn
->ssyn_syn
, syn
, sizeof(LDAPSyntax
) );
174 LDAP_SLIST_NEXT(ssyn
,ssyn_next
) = NULL
;
177 * note: ssyn_bvoid uses the same memory of ssyn_syn.syn_oid;
178 * ssyn_oidlen is #defined as ssyn_bvoid.bv_len
180 ssyn
->ssyn_bvoid
.bv_val
= ssyn
->ssyn_syn
.syn_oid
;
181 ssyn
->ssyn_oidlen
= strlen(syn
->syn_oid
);
182 ssyn
->ssyn_flags
= def
->sd_flags
;
183 ssyn
->ssyn_validate
= def
->sd_validate
;
184 ssyn
->ssyn_pretty
= def
->sd_pretty
;
186 ssyn
->ssyn_sups
= NULL
;
188 #ifdef SLAPD_BINARY_CONVERSION
189 ssyn
->ssyn_ber2str
= def
->sd_ber2str
;
190 ssyn
->ssyn_str2ber
= def
->sd_str2ber
;
193 if ( def
->sd_sups
!= NULL
) {
196 for ( cnt
= 0; def
->sd_sups
[cnt
] != NULL
; cnt
++ )
199 ssyn
->ssyn_sups
= (Syntax
**)SLAP_CALLOC( cnt
+ 1,
200 sizeof( Syntax
* ) );
201 if ( ssyn
->ssyn_sups
== NULL
) {
202 Debug( LDAP_DEBUG_ANY
, "SLAP_CALLOC Error\n", 0, 0, 0 );
203 code
= SLAP_SCHERR_OUTOFMEM
;
206 for ( cnt
= 0; def
->sd_sups
[cnt
] != NULL
; cnt
++ ) {
207 ssyn
->ssyn_sups
[cnt
] = syn_find( def
->sd_sups
[cnt
] );
208 if ( ssyn
->ssyn_sups
[cnt
] == NULL
) {
209 *err
= def
->sd_sups
[cnt
];
210 code
= SLAP_SCHERR_SYN_SUP_NOT_FOUND
;
217 code
= syn_insert( ssyn
, err
);
221 if ( code
!= 0 && ssyn
!= NULL
) {
222 if ( ssyn
->ssyn_sups
!= NULL
) {
223 SLAP_FREE( ssyn
->ssyn_sups
);
233 slap_syntax_defs_rec
*def
)
239 syn
= ldap_str2syntax( def
->sd_desc
, &code
, &err
, LDAP_SCHEMA_ALLOW_ALL
);
241 Debug( LDAP_DEBUG_ANY
, "Error in register_syntax: %s before %s in %s\n",
242 ldap_scherr2str(code
), err
, def
->sd_desc
);
247 code
= syn_add( syn
, def
, &err
);
250 Debug( LDAP_DEBUG_ANY
, "Error in register_syntax: %s %s in %s\n",
251 scherr2str(code
), err
, def
->sd_desc
);
252 ldap_syntax_free( syn
);
263 syn_schema_info( Entry
*e
)
265 AttributeDescription
*ad_ldapSyntaxes
= slap_schema
.si_ad_ldapSyntaxes
;
270 LDAP_SLIST_FOREACH(syn
, &syn_list
, ssyn_next
) {
271 if ( ! syn
->ssyn_validate
) {
272 /* skip syntaxes without validators */
275 if ( syn
->ssyn_flags
& SLAP_SYNTAX_HIDE
) {
280 if ( ldap_syntax2bv( &syn
->ssyn_syn
, &val
) == NULL
) {
284 Debug( LDAP_DEBUG_TRACE
, "Merging syn [%ld] %s\n",
285 (long) val
.bv_len
, val
.bv_val
, 0 );
288 nval
.bv_val
= syn
->ssyn_oid
;
289 nval
.bv_len
= strlen(syn
->ssyn_oid
);
291 if( attr_merge_one( e
, ad_ldapSyntaxes
, &val
, &nval
) )
295 ldap_memfree( val
.bv_val
);