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