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