]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Prepare for unifdef -DSLAPD_SCHEMA_NOT_COMPAT
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* attr.c - routines for dealing with attributes */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #ifdef HAVE_FCNTL_H
13 #include <fcntl.h>
14 #endif
15
16 #include <ac/ctype.h>
17 #include <ac/errno.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap_pvt.h"
23 #include "slap.h"
24
25 #ifdef LDAP_DEBUG
26 static void at_index_print( void );
27 #endif
28
29 void
30 attr_free( Attribute *a )
31 {
32 #ifdef SLAPD_SCHEMA_NOT_COMPAT
33         ad_free( a->a_desc, 1 );
34 #else
35         free( a->a_type );
36 #endif
37         ber_bvecfree( a->a_vals );
38         free( a );
39 }
40
41 void
42 attrs_free( Attribute *a )
43 {
44         Attribute *next;
45
46         for( ; a != NULL ; a = next ) {
47                 next = a->a_next;
48                 attr_free( a );
49         }
50 }
51
52 Attribute *attr_dup( Attribute *a )
53 {
54         Attribute *tmp;
55
56         if( a == NULL) return NULL;
57
58         tmp = ch_malloc( sizeof(Attribute) );
59
60         if( a->a_vals != NULL ) {
61                 int i;
62
63                 for( i=0; a->a_vals[i] != NULL; i++ ) {
64                         /* EMPTY */ ;
65                 }
66
67                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
68
69                 for( i=0; a->a_vals[i] != NULL; i++ ) {
70                         tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
71
72                         if( tmp->a_vals[i] == NULL ) break;
73                 }
74
75                 tmp->a_vals[i] = NULL;
76
77         } else {
78                 tmp->a_vals = NULL;
79         }
80
81 #ifdef SLAPD_SCHEMA_NOT_COMPAT
82         tmp->a_desc = ad_dup( a->a_desc );
83 #else
84         tmp->a_type = ch_strdup( a->a_type );
85         tmp->a_syntax = a->a_syntax;
86 #endif
87         tmp->a_next = NULL;
88
89         return tmp;
90 }
91
92 Attribute *attrs_dup( Attribute *a )
93 {
94         Attribute *tmp, **next;
95
96         if( a == NULL ) return NULL;
97
98         tmp = NULL;
99         next = &tmp;
100
101         for( ; a != NULL ; a = a->a_next ) {
102                 *next = attr_dup( a );
103                 next = &((*next)->a_next);
104         }
105         *next = NULL;
106
107         return tmp;
108 }
109
110 #ifndef SLAPD_SCHEMA_NOT_COMPAT
111 /*
112  * attr_normalize - normalize an attribute name (make it all lowercase)
113  */
114
115 char *
116 attr_normalize( char *s )
117 {
118         assert( s != NULL );
119
120         return( ldap_pvt_str2lower( s ) );
121 }
122
123 /*
124  * attr_merge_fast - merge the given type and value with the list of
125  * attributes in attrs. called from str2entry(), where we can make some
126  * assumptions to make things faster.
127  * returns      0       everything went ok
128  *              -1      trouble
129  */
130
131 int
132 attr_merge_fast(
133     Entry               *e,
134     const char          *type,
135     struct berval       **vals,
136     int                 nvals,
137     int                 naddvals,
138     int                 *maxvals,
139     Attribute           ***a
140 )
141 {
142         if ( *a == NULL ) {
143                 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
144                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
145                                 break;
146                         }
147                 }
148         }
149
150         if ( **a == NULL ) {
151                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
152                 (**a)->a_vals = NULL;
153                 (**a)->a_type = attr_normalize( ch_strdup( type ) );
154                 (**a)->a_syntax = attr_syntax( type );
155                 (**a)->a_next = NULL;
156         }
157
158         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
159             maxvals ) );
160 }
161 #endif
162
163
164 /*
165  * attr_merge - merge the given type and value with the list of
166  * attributes in attrs.
167  * returns      0       everything went ok
168  *              -1      trouble
169  */
170
171 int
172 attr_merge(
173     Entry               *e,
174 #ifdef SLAPD_SCHEMA_NOT_COMPAT
175         AttributeDescription *desc,
176 #else
177     const char          *type,
178 #endif
179     struct berval       **vals )
180 {
181         Attribute       **a;
182
183         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
184 #ifdef SLAPD_SCHEMA_NOT_COMPAT
185                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
186 #else
187                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
188 #endif
189                 {
190                         break;
191                 }
192         }
193
194         if ( *a == NULL ) {
195                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
196 #ifdef SLAPD_SCHEMA_NOT_COMPAT
197                 (*a)->a_desc = ad_dup( desc );
198 #else
199                 (*a)->a_type = attr_normalize( ch_strdup( type ) );
200                 (*a)->a_syntax = attr_syntax( type );
201 #endif
202                 (*a)->a_vals = NULL;
203                 (*a)->a_next = NULL;
204         }
205
206         return( value_add( &(*a)->a_vals, vals ) );
207 }
208
209 #ifdef SLAPD_SCHEMA_NOT_COMPAT
210 /*
211  * attrs_find - find attribute(s) by AttributeDescription
212  * returns next attribute which is subtype of provided description.
213  */
214
215 Attribute *
216 attrs_find(
217     Attribute   *a,
218         AttributeDescription *desc
219 )
220 {
221         for ( ; a != NULL; a = a->a_next ) {
222                 if ( is_ad_subtype( a->a_desc, desc ) ) {
223                         return( a );
224                 }
225         }
226
227         return( NULL );
228 }
229 #endif
230
231 /*
232  * attr_find - find attribute by type
233  */
234
235 Attribute *
236 attr_find(
237     Attribute   *a,
238 #ifdef SLAPD_SCHEMA_NOT_COMPAT
239         AttributeDescription *desc
240 #else
241         const char      *type
242 #endif
243 )
244 {
245         for ( ; a != NULL; a = a->a_next ) {
246 #ifdef SLAPD_SCHEMA_NOT_COMPAT
247                 if ( ad_cmp( a->a_desc, desc ) == 0 )
248 #else
249                 if ( strcasecmp( a->a_type, type ) == 0 )
250 #endif
251                 {
252                         return( a );
253                 }
254         }
255
256         return( NULL );
257 }
258
259 /*
260  * attr_delete - delete the attribute type in list pointed to by attrs
261  * return       0       deleted ok
262  *              1       not found in list a
263  *              -1      something bad happened
264  */
265
266 int
267 attr_delete(
268     Attribute   **attrs,
269 #ifdef SLAPD_SCHEMA_NOT_COMPAT
270         AttributeDescription *desc
271 #else
272     const char  *type
273 #endif
274 )
275 {
276         Attribute       **a;
277
278         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
279 #ifdef SLAPD_SCHEMA_NOT_COMPAT
280                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
281 #else
282                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
283 #endif
284                 {
285                         Attribute       *save = *a;
286                         *a = (*a)->a_next;
287                         attr_free( save );
288
289                         return LDAP_SUCCESS;
290                 }
291         }
292
293         return LDAP_NO_SUCH_ATTRIBUTE;
294 }
295