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