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