]> git.sur5r.net Git - openldap/blob - servers/slapd/syntax.c
c5b36cfdfdeaf18ef4851e8c709d84aaa2f53583
[openldap] / servers / slapd / syntax.c
1 /* syntax.c - routines to manage syntax definitions */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/ctype.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26
27 struct sindexrec {
28         char            *sir_name;
29         Syntax          *sir_syn;
30 };
31
32 static Avlnode  *syn_index = NULL;
33 static LDAP_SLIST_HEAD(SyntaxList, slap_syntax) syn_list
34         = LDAP_SLIST_HEAD_INITIALIZER(&syn_list);
35
36 static int
37 syn_index_cmp(
38         const void *v_sir1,
39         const void *v_sir2
40 )
41 {
42         const struct sindexrec *sir1 = v_sir1, *sir2 = v_sir2;
43         return (strcmp( sir1->sir_name, sir2->sir_name ));
44 }
45
46 static int
47 syn_index_name_cmp(
48         const void *name,
49         const void *sir
50 )
51 {
52         return (strcmp( name, ((const struct sindexrec *)sir)->sir_name ));
53 }
54
55 Syntax *
56 syn_find( const char *synname )
57 {
58         struct sindexrec        *sir = NULL;
59
60         if ( (sir = avl_find( syn_index, synname, syn_index_name_cmp )) != NULL ) {
61                 return( sir->sir_syn );
62         }
63         return( NULL );
64 }
65
66 Syntax *
67 syn_find_desc( const char *syndesc, int *len )
68 {
69         Syntax          *synp;
70
71         LDAP_SLIST_FOREACH(synp, &syn_list, ssyn_next) {
72                 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{' /*'}'*/ ))) {
73                         return synp;
74                 }
75         }
76         return( NULL );
77 }
78
79 void
80 syn_destroy( void )
81 {
82         Syntax *s;
83
84         avl_free(syn_index, ldap_memfree);
85         while( !LDAP_SLIST_EMPTY(&syn_list) ) {
86                 s = LDAP_SLIST_FIRST(&syn_list);
87                 LDAP_SLIST_REMOVE_HEAD(&syn_list, ssyn_next);
88                 ldap_syntax_free((LDAPSyntax *)s);
89         }
90 }
91
92 static int
93 syn_insert(
94     Syntax              *ssyn,
95     const char          **err
96 )
97 {
98         struct sindexrec        *sir;
99
100         LDAP_SLIST_NEXT( ssyn, ssyn_next ) = NULL;
101         LDAP_SLIST_INSERT_HEAD( &syn_list, ssyn, ssyn_next );
102  
103         if ( ssyn->ssyn_oid ) {
104                 sir = (struct sindexrec *)
105                         SLAP_CALLOC( 1, sizeof(struct sindexrec) );
106                 if( sir == NULL ) {
107                         Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
108                         return LDAP_OTHER;
109                 }
110                 sir->sir_name = ssyn->ssyn_oid;
111                 sir->sir_syn = ssyn;
112                 if ( avl_insert( &syn_index, (caddr_t) sir,
113                                  syn_index_cmp, avl_dup_error ) ) {
114                         *err = ssyn->ssyn_oid;
115                         ldap_memfree(sir);
116                         return SLAP_SCHERR_SYN_DUP;
117                 }
118                 /* FIX: temporal consistency check */
119                 syn_find(sir->sir_name);
120         }
121         return 0;
122 }
123
124 int
125 syn_add(
126     LDAPSyntax          *syn,
127     slap_syntax_defs_rec *def,
128     const char          **err
129 )
130 {
131         Syntax          *ssyn;
132         int             code;
133
134         ssyn = (Syntax *) SLAP_CALLOC( 1, sizeof(Syntax) );
135         if( ssyn == NULL ) {
136                 Debug( LDAP_DEBUG_ANY, "SLAP_CALLOC Error\n", 0, 0, 0 );
137                 return LDAP_OTHER;
138         }
139
140         AC_MEMCPY( &ssyn->ssyn_syn, syn, sizeof(LDAPSyntax) );
141
142         LDAP_SLIST_NEXT(ssyn,ssyn_next) = NULL;
143
144         /*
145          * note: ssyn_bvoid uses the same memory of ssyn_syn.syn_oid;
146          * ssyn_oidlen is #defined as ssyn_bvoid.bv_len
147          */
148         ssyn->ssyn_bvoid.bv_val = ssyn->ssyn_syn.syn_oid;
149         ssyn->ssyn_oidlen = strlen(syn->syn_oid);
150         ssyn->ssyn_flags = def->sd_flags;
151         ssyn->ssyn_validate = def->sd_validate;
152         ssyn->ssyn_pretty = def->sd_pretty;
153
154 #ifdef SLAPD_BINARY_CONVERSION
155         ssyn->ssyn_ber2str = def->sd_ber2str;
156         ssyn->ssyn_str2ber = def->sd_str2ber;
157 #endif
158
159         code = syn_insert(ssyn, err);
160         return code;
161 }
162
163 int
164 register_syntax(
165         slap_syntax_defs_rec *def )
166 {
167         LDAPSyntax      *syn;
168         int             code;
169         const char      *err;
170
171         syn = ldap_str2syntax( def->sd_desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
172         if ( !syn ) {
173                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
174                     ldap_scherr2str(code), err, def->sd_desc );
175
176                 return( -1 );
177         }
178
179         code = syn_add( syn, def, &err );
180
181         if ( code ) {
182                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
183                     scherr2str(code), err, def->sd_desc );
184                 ldap_syntax_free( syn );
185
186                 return( -1 );
187         }
188
189         ldap_memfree( syn );
190
191         return( 0 );
192 }
193
194 int
195 syn_schema_info( Entry *e )
196 {
197         AttributeDescription *ad_ldapSyntaxes = slap_schema.si_ad_ldapSyntaxes;
198         Syntax          *syn;
199         struct berval   val;
200         struct berval   nval;
201
202         LDAP_SLIST_FOREACH(syn, &syn_list, ssyn_next ) {
203                 if ( ! syn->ssyn_validate ) {
204                         /* skip syntaxes without validators */
205                         continue;
206                 }
207                 if ( syn->ssyn_flags & SLAP_SYNTAX_HIDE ) {
208                         /* hide syntaxes */
209                         continue;
210                 }
211
212                 if ( ldap_syntax2bv( &syn->ssyn_syn, &val ) == NULL ) {
213                         return -1;
214                 }
215 #if 0
216                 Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
217                (long) val.bv_len, val.bv_val, 0 );
218 #endif
219
220                 nval.bv_val = syn->ssyn_oid;
221                 nval.bv_len = strlen(syn->ssyn_oid);
222
223                 if( attr_merge_one( e, ad_ldapSyntaxes, &val, &nval ) )
224                 {
225                         return -1;
226                 }
227                 ldap_memfree( val.bv_val );
228         }
229         return 0;
230 }
231