]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Fix ITS#1991 - referrals with sarch base == target (wasn't sure at first,
[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 int
137 attr_merge_one(
138         Entry           *e,
139         AttributeDescription *desc,
140         struct berval   *val )
141 {
142         Attribute       **a;
143
144         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
145                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
146                         break;
147                 }
148         }
149
150         if ( *a == NULL ) {
151                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
152                 (*a)->a_desc = desc;
153                 (*a)->a_vals = NULL;
154                 (*a)->a_next = NULL;
155                 (*a)->a_flags = 0;
156         }
157
158         return( value_add_one( &(*a)->a_vals, val ) );
159 }
160
161 /*
162  * attrs_find - find attribute(s) by AttributeDescription
163  * returns next attribute which is subtype of provided description.
164  */
165
166 Attribute *
167 attrs_find(
168     Attribute   *a,
169         AttributeDescription *desc
170 )
171 {
172         for ( ; a != NULL; a = a->a_next ) {
173                 if ( is_ad_subtype( a->a_desc, desc ) ) {
174                         return( a );
175                 }
176         }
177
178         return( NULL );
179 }
180
181 /*
182  * attr_find - find attribute by type
183  */
184
185 Attribute *
186 attr_find(
187     Attribute   *a,
188         AttributeDescription *desc
189 )
190 {
191         for ( ; a != NULL; a = a->a_next ) {
192                 if ( ad_cmp( a->a_desc, desc ) == 0 ) {
193                         return( a );
194                 }
195         }
196
197         return( NULL );
198 }
199
200 /*
201  * attr_delete - delete the attribute type in list pointed to by attrs
202  * return       0       deleted ok
203  *              1       not found in list a
204  *              -1      something bad happened
205  */
206
207 int
208 attr_delete(
209     Attribute   **attrs,
210         AttributeDescription *desc
211 )
212 {
213         Attribute       **a;
214
215         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
216                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 ) {
217                         Attribute       *save = *a;
218                         *a = (*a)->a_next;
219                         attr_free( save );
220
221                         return LDAP_SUCCESS;
222                 }
223         }
224
225         return LDAP_NO_SUCH_ATTRIBUTE;
226 }
227