]> git.sur5r.net Git - openldap/blob - servers/slapd/syntax.c
Set peeraddr also for IPv6, fixes ITS#1918
[openldap] / servers / slapd / syntax.c
1 /* syntax.c - routines to manage syntax definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_pvt.h"
18
19
20 struct sindexrec {
21         char            *sir_name;
22         Syntax          *sir_syn;
23 };
24
25 static Avlnode  *syn_index = NULL;
26 static Syntax *syn_list = NULL;
27
28 static int
29 syn_index_cmp(
30     struct sindexrec    *sir1,
31     struct sindexrec    *sir2
32 )
33 {
34         return (strcmp( sir1->sir_name, sir2->sir_name ));
35 }
36
37 static int
38 syn_index_name_cmp(
39     const char          *name,
40     struct sindexrec    *sir
41 )
42 {
43         return (strcmp( name, sir->sir_name ));
44 }
45
46 Syntax *
47 syn_find( const char *synname )
48 {
49         struct sindexrec        *sir = NULL;
50
51         if ( (sir = (struct sindexrec *) avl_find( syn_index, synname,
52             (AVL_CMP) syn_index_name_cmp )) != NULL ) {
53                 return( sir->sir_syn );
54         }
55         return( NULL );
56 }
57
58 Syntax *
59 syn_find_desc( const char *syndesc, int *len )
60 {
61         Syntax          *synp;
62
63         for (synp = syn_list; synp; synp = synp->ssyn_next)
64                 if ((*len = dscompare( synp->ssyn_syn.syn_desc, syndesc, '{')))
65                         return synp;
66         return( NULL );
67 }
68
69 void
70 syn_destroy( void )
71 {
72         Syntax *s, *n;
73
74         avl_free(syn_index, ldap_memfree);
75         for (s=syn_list; s; s=n) {
76                 n = s->ssyn_next;
77                 ldap_syntax_free((LDAPSyntax *)s);
78         }
79 }
80
81 static int
82 syn_insert(
83     Syntax              *ssyn,
84     const char          **err
85 )
86 {
87         Syntax          **synp;
88         struct sindexrec        *sir;
89
90         synp = &syn_list;
91         while ( *synp != NULL ) {
92                 synp = &(*synp)->ssyn_next;
93         }
94         *synp = ssyn;
95
96         if ( ssyn->ssyn_oid ) {
97                 sir = (struct sindexrec *)
98                         ch_calloc( 1, sizeof(struct sindexrec) );
99                 sir->sir_name = ssyn->ssyn_oid;
100                 sir->sir_syn = ssyn;
101                 if ( avl_insert( &syn_index, (caddr_t) sir,
102                                  (AVL_CMP) syn_index_cmp,
103                                  (AVL_DUP) avl_dup_error ) ) {
104                         *err = ssyn->ssyn_oid;
105                         ldap_memfree(sir);
106                         return SLAP_SCHERR_SYN_DUP;
107                 }
108                 /* FIX: temporal consistency check */
109                 syn_find(sir->sir_name);
110         }
111         return 0;
112 }
113
114 int
115 syn_add(
116     LDAPSyntax          *syn,
117         unsigned flags,
118     slap_syntax_validate_func   *validate,
119     slap_syntax_transform_func  *normalize,
120     slap_syntax_transform_func  *pretty,
121 #ifdef SLAPD_BINARY_CONVERSION
122     slap_syntax_transform_func  *ber2str,
123     slap_syntax_transform_func  *str2ber,
124 #endif
125     const char          **err
126 )
127 {
128         Syntax          *ssyn;
129         int             code;
130
131         ssyn = (Syntax *) ch_calloc( 1, sizeof(Syntax) );
132
133         AC_MEMCPY( &ssyn->ssyn_syn, syn, sizeof(LDAPSyntax) );
134
135         ssyn->ssyn_next = NULL;
136
137         ssyn->ssyn_oidlen = strlen(syn->syn_oid);
138         ssyn->ssyn_flags = flags;
139         ssyn->ssyn_validate = validate;
140         ssyn->ssyn_normalize = normalize;
141         ssyn->ssyn_pretty = pretty;
142
143 #ifdef SLAPD_BINARY_CONVERSION
144         ssyn->ssyn_ber2str = ber2str;
145         ssyn->ssyn_str2ber = str2ber;
146 #endif
147
148         code = syn_insert(ssyn, err);
149         return code;
150 }
151
152 int
153 register_syntax(
154         const char * desc,
155         unsigned flags,
156         slap_syntax_validate_func *validate,
157         slap_syntax_transform_func *normalize,
158         slap_syntax_transform_func *pretty )
159 {
160         LDAPSyntax      *syn;
161         int             code;
162         const char      *err;
163
164         syn = ldap_str2syntax( desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
165         if ( !syn ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG(( "schema", LDAP_LEVEL_ERR,
168                            "register_syntax: Error - %s before %s in %s.\n",
169                            ldap_scherr2str(code), err, desc ));
170 #else
171                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s before %s in %s\n",
172                     ldap_scherr2str(code), err, desc );
173 #endif
174
175                 return( -1 );
176         }
177
178         code = syn_add( syn, flags, validate, normalize, pretty, &err );
179
180         ldap_memfree( syn );
181
182         if ( code ) {
183 #ifdef NEW_LOGGING
184                 LDAP_LOG(( "schema", LDAP_LEVEL_ERR,
185                            "register_syntax: Error - %s %s in %s\n",
186                            scherr2str(code), err, desc ));
187 #else
188                 Debug( LDAP_DEBUG_ANY, "Error in register_syntax: %s %s in %s\n",
189                     scherr2str(code), err, desc );
190 #endif
191
192                 return( -1 );
193         }
194
195         return( 0 );
196 }
197
198 #if defined( SLAPD_SCHEMA_DN )
199
200 int
201 syn_schema_info( Entry *e )
202 {
203         struct berval   vals[2];
204         Syntax          *syn;
205
206         AttributeDescription *ad_ldapSyntaxes = slap_schema.si_ad_ldapSyntaxes;
207
208         vals[1].bv_val = NULL;
209
210         for ( syn = syn_list; syn; syn = syn->ssyn_next ) {
211                 if ( ! syn->ssyn_validate ) {
212                         /* skip syntaxes without validators */
213                         continue;
214                 }
215                 if ( syn->ssyn_flags & SLAP_SYNTAX_HIDE ) {
216                         /* hide syntaxes */
217                         continue;
218                 }
219
220                 if ( ldap_syntax2bv( &syn->ssyn_syn, vals ) == NULL ) {
221                         return -1;
222                 }
223 #if 0
224 #ifdef NEW_LOGGING
225                 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
226                            "syn_schema_info: Merging syn [%ld] %s\n",
227                            (long)vals[0].bv_len, vals[0].bv_val ));
228 #else
229                 Debug( LDAP_DEBUG_TRACE, "Merging syn [%ld] %s\n",
230                (long) vals[0].bv_len, vals[0].bv_val, 0 );
231 #endif
232
233 #endif
234                 attr_merge( e, ad_ldapSyntaxes, vals );
235                 ldap_memfree( vals[0].bv_val );
236         }
237         return 0;
238 }
239
240 #endif