]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Fix matched values bug
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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         free( a );
36 }
37
38 void
39 attrs_free( Attribute *a )
40 {
41         Attribute *next;
42
43         for( ; a != NULL ; a = next ) {
44                 next = a->a_next;
45                 attr_free( a );
46         }
47 }
48
49 Attribute *attr_dup( Attribute *a )
50 {
51         Attribute *tmp;
52
53         if( a == NULL) return NULL;
54
55         tmp = ch_malloc( sizeof(Attribute) );
56
57         if( a->a_vals != NULL ) {
58                 int i;
59
60                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
61                         /* EMPTY */ ;
62                 }
63
64                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval));
65
66                 for( i=0; a->a_vals[i].bv_val != NULL; i++ ) {
67                         ber_dupbv( &tmp->a_vals[i], &a->a_vals[i] );
68                         if( tmp->a_vals[i].bv_val == NULL ) break;
69                 }
70
71                 tmp->a_vals[i].bv_val = NULL;
72
73         } else {
74                 tmp->a_vals = NULL;
75         }
76
77         tmp->a_desc = a->a_desc;
78         tmp->a_next = NULL;
79         tmp->a_flags = 0;
80
81         return tmp;
82 }
83
84 Attribute *attrs_dup( Attribute *a )
85 {
86         Attribute *tmp, **next;
87
88         if( a == NULL ) return NULL;
89
90         tmp = NULL;
91         next = &tmp;
92
93         for( ; a != NULL ; a = a->a_next ) {
94                 *next = attr_dup( a );
95                 next = &((*next)->a_next);
96         }
97         *next = NULL;
98
99         return tmp;
100 }
101
102
103
104 /*
105  * attr_merge - merge the given type and value with the list of
106  * attributes in attrs.
107  * returns      0       everything went ok
108  *              -1      trouble
109  */
110
111 int
112 attr_merge(
113         Entry           *e,
114         AttributeDescription *desc,
115         BerVarray       vals )
116 {
117         Attribute       **a;
118
119         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
120                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
121                         break;
122                 }
123         }
124
125         if ( *a == NULL ) {
126                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
127                 (*a)->a_desc = desc;
128                 (*a)->a_vals = NULL;
129                 (*a)->a_next = NULL;
130                 (*a)->a_flags = 0;
131         }
132
133         return( value_add( &(*a)->a_vals, vals ) );
134 }
135
136 /*
137  * attrs_find - find attribute(s) by AttributeDescription
138  * returns next attribute which is subtype of provided description.
139  */
140
141 Attribute *
142 attrs_find(
143     Attribute   *a,
144         AttributeDescription *desc
145 )
146 {
147         for ( ; a != NULL; a = a->a_next ) {
148                 if ( is_ad_subtype( a->a_desc, desc ) ) {
149                         return( a );
150                 }
151         }
152
153         return( NULL );
154 }
155
156 /*
157  * attr_find - find attribute by type
158  */
159
160 Attribute *
161 attr_find(
162     Attribute   *a,
163         AttributeDescription *desc
164 )
165 {
166         for ( ; a != NULL; a = a->a_next ) {
167                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
168                         return( a );
169                 }
170         }
171
172         return( NULL );
173 }
174
175 /*
176  * attr_delete - delete the attribute type in list pointed to by attrs
177  * return       0       deleted ok
178  *              1       not found in list a
179  *              -1      something bad happened
180  */
181
182 int
183 attr_delete(
184     Attribute   **attrs,
185         AttributeDescription *desc
186 )
187 {
188         Attribute       **a;
189
190         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
191                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
192                         Attribute       *save = *a;
193                         *a = (*a)->a_next;
194                         attr_free( save );
195
196                         return LDAP_SUCCESS;
197                 }
198         }
199
200         return LDAP_NO_SUCH_ATTRIBUTE;
201 }
202