]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
e1d273c552690d0ecafd1d4cfeb3830050b5f70b
[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 #endif
123
124 /*
125  * attr_merge_fast - merge the given type and value with the list of
126  * attributes in attrs. called from str2entry(), where we can make some
127  * assumptions to make things faster.
128  * returns      0       everything went ok
129  *              -1      trouble
130  */
131
132 #ifdef SLAPD_SCHEMA_NOT_COMPAT
133         /* not yet implemented */
134 #else
135 int
136 attr_merge_fast(
137     Entry               *e,
138     const char          *type,
139     struct berval       **vals,
140     int                 nvals,
141     int                 naddvals,
142     int                 *maxvals,
143     Attribute           ***a
144 )
145 {
146         if ( *a == NULL ) {
147                 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
148 #ifdef SLAPD_SCHEMA_NOT_COMPAT
149                         /* not yet implemented */
150 #else
151                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
152                                 break;
153                         }
154 #endif
155                 }
156         }
157
158         if ( **a == NULL ) {
159                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
160                 (**a)->a_vals = NULL;
161 #ifndef SLAPD_SCHEMA_NOT_COMPAT
162                 (**a)->a_type = attr_normalize( ch_strdup( type ) );
163                 (**a)->a_syntax = attr_syntax( type );
164 #endif
165                 (**a)->a_next = NULL;
166         }
167
168         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
169             maxvals ) );
170 }
171 #endif
172
173
174 /*
175  * attr_merge - merge the given type and value with the list of
176  * attributes in attrs.
177  * returns      0       everything went ok
178  *              -1      trouble
179  */
180
181 int
182 attr_merge(
183     Entry               *e,
184 #ifdef SLAPD_SCHEMA_NOT_COMPAT
185         AttributeDescription *desc,
186 #else
187     const char          *type,
188 #endif
189     struct berval       **vals )
190 {
191         Attribute       **a;
192
193         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
194 #ifdef SLAPD_SCHEMA_NOT_COMPAT
195                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
196 #else
197                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
198 #endif
199                 {
200                         break;
201                 }
202         }
203
204         if ( *a == NULL ) {
205                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
206 #ifdef SLAPD_SCHEMA_NOT_COMPAT
207                 (*a)->a_desc = ad_dup( desc );
208 #else
209                 (*a)->a_type = attr_normalize( ch_strdup( type ) );
210                 (*a)->a_syntax = attr_syntax( type );
211 #endif
212                 (*a)->a_vals = NULL;
213                 (*a)->a_next = NULL;
214         }
215
216         return( value_add( &(*a)->a_vals, vals ) );
217 }
218
219 #ifdef SLAPD_SCHEMA_NOT_COMPAT
220 /*
221  * attrs_find - find attribute(s) by AttributeDescription
222  * returns next attribute which is subtype of provided description.
223  */
224
225 Attribute *
226 attrs_find(
227     Attribute   *a,
228         AttributeDescription *desc
229 )
230 {
231         for ( ; a != NULL; a = a->a_next ) {
232                 if ( is_ad_subtype( a->a_desc, desc ) == 0 ) {
233                         return( a );
234                 }
235         }
236
237         return( NULL );
238 }
239 #endif
240
241 /*
242  * attr_find - find attribute by type
243  */
244
245 Attribute *
246 attr_find(
247     Attribute   *a,
248 #ifdef SLAPD_SCHEMA_NOT_COMPAT
249         AttributeDescription *desc
250 #else
251         const char      *type
252 #endif
253 )
254 {
255         for ( ; a != NULL; a = a->a_next ) {
256 #ifdef SLAPD_SCHEMA_NOT_COMPAT
257                 if ( ad_cmp( a->a_desc, desc ) == 0 )
258 #else
259                 if ( strcasecmp( a->a_type, type ) == 0 )
260 #endif
261                 {
262                         return( a );
263                 }
264         }
265
266         return( NULL );
267 }
268
269 /*
270  * attr_delete - delete the attribute type in list pointed to by attrs
271  * return       0       deleted ok
272  *              1       not found in list a
273  *              -1      something bad happened
274  */
275
276 int
277 attr_delete(
278     Attribute   **attrs,
279 #ifdef SLAPD_SCHEMA_NOT_COMPAT
280         AttributeDescription *desc
281 #else
282     const char  *type
283 #endif
284 )
285 {
286         Attribute       **a;
287         Attribute       *save;
288
289         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
290 #ifdef SLAPD_SCHEMA_NOT_COMPAT
291                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
292 #else
293                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
294 #endif
295                 {
296                         break;
297                 }
298         }
299
300         if ( *a == NULL ) {
301                 return( 1 );
302         }
303
304         save = *a;
305         *a = (*a)->a_next;
306         attr_free( save );
307
308         return( 0 );
309 }
310