]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
d72c5ae092643218744f7d71b60ae25536ee9c6f
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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 {
28 }
29 #endif
30
31 void
32 attr_free( Attribute *a )
33 {
34         ber_bvarray_free( a->a_vals );
35 #ifdef SLAP_NVALUES
36         ber_bvarray_free( a->a_nvals );
37 #endif
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].bv_val != NULL; i++ ) {
64                         /* EMPTY */ ;
65                 }
66
67                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
68 #ifdef SLAP_NVALUES
69                 if( a->a_nvals != NULL ) {
70                         tmp->a_nvals = ch_malloc((i+1) * sizeof(struct berval));
71                 }
72 #endif
73
74                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
75                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
76                         if( tmp->a_vals[i].bv_val == NULL ) break;
77
78 #ifdef SLAP_NVALUES
79                         if( a->a_nvals ) {
80                                 ber_dupbv( &tmp->a_nvals[i], &a->a_nvals[i] );
81                                 if( tmp->a_nvals[i].bv_val == NULL ) break;
82                         } else {
83                                 tmp->a_nvals = NULL;
84                         }
85 #endif
86                 }
87
88                 tmp->a_vals[i].bv_val = NULL;
89 #ifdef SLAP_NVALUES
90                 if( tmp->a_nvals != NULL ) {
91                         tmp->a_nvals[i].bv_val = NULL;
92                 }
93 #endif
94
95         } else {
96                 tmp->a_vals = NULL;
97 #ifdef SLAP_NVALUES
98                 tmp->a_nvals = NULL;
99 #endif
100         }
101
102         tmp->a_desc = a->a_desc;
103         tmp->a_next = NULL;
104         tmp->a_flags = 0;
105
106         return tmp;
107 }
108
109 Attribute *attrs_dup( Attribute *a )
110 {
111         Attribute *tmp, **next;
112
113         if( a == NULL ) return NULL;
114
115         tmp = NULL;
116         next = &tmp;
117
118         for( ; a != NULL ; a = a->a_next ) {
119                 *next = attr_dup( a );
120                 next = &((*next)->a_next);
121         }
122         *next = NULL;
123
124         return tmp;
125 }
126
127
128
129 /*
130  * attr_merge - merge the given type and value with the list of
131  * attributes in attrs.
132  * returns      0       everything went ok
133  *              -1      trouble
134  */
135
136 int
137 attr_merge(
138         Entry           *e,
139         AttributeDescription *desc,
140         BerVarray       vals
141 #ifdef SLAP_NVALUES
142         , BerVarray     nvals
143 #endif
144 ) {
145         int rc;
146
147         Attribute       **a;
148
149         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
150                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
151                         break;
152                 }
153         }
154
155         if ( *a == NULL ) {
156                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
157                 (*a)->a_desc = desc;
158                 (*a)->a_vals = NULL;
159 #ifdef SLAP_NVALUES
160                 (*a)->a_nvals = NULL;
161 #endif
162                 (*a)->a_next = NULL;
163                 (*a)->a_flags = 0;
164         }
165
166         rc = value_add( &(*a)->a_vals, vals );
167
168 #ifdef SLAP_NVALUES
169         if( !rc && nvals ) rc = value_add( &(*a)->a_nvals, nvals );
170 #endif
171
172         return rc;
173 }
174
175 int
176 attr_merge_one(
177         Entry           *e,
178         AttributeDescription *desc,
179         struct berval   *val
180 #ifdef SLAP_NVALUES
181         , BerVarray     nval
182 #endif
183 ) {
184         int rc;
185         Attribute       **a;
186
187         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
188                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
189                         break;
190                 }
191         }
192
193         if ( *a == NULL ) {
194                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
195                 (*a)->a_desc = desc;
196                 (*a)->a_vals = NULL;
197 #ifdef SLAP_NVALUES
198                 (*a)->a_nvals = NULL;
199 #endif
200                 (*a)->a_next = NULL;
201                 (*a)->a_flags = 0;
202         }
203
204         rc = value_add_one( &(*a)->a_vals, val );
205
206 #ifdef SLAP_NVALUES
207         if( !rc && nval ) rc = value_add_one( &(*a)->a_nvals, nval );
208 #endif
209         return rc;
210 }
211
212 /*
213  * attrs_find - find attribute(s) by AttributeDescription
214  * returns next attribute which is subtype of provided description.
215  */
216
217 Attribute *
218 attrs_find(
219     Attribute   *a,
220         AttributeDescription *desc
221 )
222 {
223         for ( ; a != NULL; a = a->a_next ) {
224                 if ( is_ad_subtype( a->a_desc, desc ) ) {
225                         return( a );
226                 }
227         }
228
229         return( NULL );
230 }
231
232 /*
233  * attr_find - find attribute by type
234  */
235
236 Attribute *
237 attr_find(
238     Attribute   *a,
239         AttributeDescription *desc
240 )
241 {
242         for ( ; a != NULL; a = a->a_next ) {
243                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
244                         return( a );
245                 }
246         }
247
248         return( NULL );
249 }
250
251 /*
252  * attr_delete - delete the attribute type in list pointed to by attrs
253  * return       0       deleted ok
254  *              1       not found in list a
255  *              -1      something bad happened
256  */
257
258 int
259 attr_delete(
260     Attribute   **attrs,
261         AttributeDescription *desc
262 )
263 {
264         Attribute       **a;
265
266         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
267                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
268                         Attribute       *save = *a;
269                         *a = (*a)->a_next;
270                         attr_free( save );
271
272                         return LDAP_SUCCESS;
273                 }
274         }
275
276         return LDAP_NO_SUCH_ATTRIBUTE;
277 }
278