]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
dff429ded9a358e068cdca3db1ba3814cb09020b
[openldap] / servers / slapd / attr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 #endif
28
29 void
30 attr_free( Attribute *a )
31 {
32 #ifdef SLAPD_SCHEMA_NOT_COMPAT
33         ad_free( a->a_desc, 1 );
34 #else
35         free( a->a_type );
36 #endif
37         ber_bvecfree( a->a_vals );
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] != NULL; i++ ) {
64                         /* EMPTY */ ;
65                 }
66
67                 tmp->a_vals = ch_malloc((i+1) * sizeof(struct berval*));
68
69                 for( i=0; a->a_vals[i] != NULL; i++ ) {
70                         tmp->a_vals[i] = ber_bvdup( a->a_vals[i] );
71
72                         if( tmp->a_vals[i] == NULL ) break;
73                 }
74
75                 tmp->a_vals[i] = NULL;
76
77         } else {
78                 tmp->a_vals = NULL;
79         }
80
81 #ifdef SLAPD_SCHEMA_NOT_COMPAT
82         tmp->a_desc = ad_dup( a->a_desc );
83 #else
84         tmp->a_type = ch_strdup( a->a_type );
85         tmp->a_syntax = a->a_syntax;
86 #endif
87         tmp->a_next = NULL;
88
89         return tmp;
90 }
91
92 Attribute *attrs_dup( Attribute *a )
93 {
94         Attribute *tmp, **next;
95
96         if( a == NULL ) return NULL;
97
98         tmp = NULL;
99         next = &tmp;
100
101         for( ; a != NULL ; a = a->a_next ) {
102                 *next = attr_dup( a );
103                 next = &((*next)->a_next);
104         }
105         *next = NULL;
106
107         return tmp;
108 }
109
110 #ifndef SLAPD_SCHEMA_NOT_COMPAT
111 /*
112  * attr_normalize - normalize an attribute name (make it all lowercase)
113  */
114
115 char *
116 attr_normalize( char *s )
117 {
118         assert( s != NULL );
119
120         return( ldap_pvt_str2lower( s ) );
121 }
122 #endif
123
124 /*
125  * attr_merge_fast - merge the given type and value with the list of
126  * attributes in attrs. called from str2entry(), where we can make some
127  * assumptions to make things faster.
128  * returns      0       everything went ok
129  *              -1      trouble
130  */
131
132 #ifdef SLAPD_SCHEMA_NOT_COMPAT
133         /* not used */
134 #else
135 int
136 attr_merge_fast(
137     Entry               *e,
138     const char          *type,
139     struct berval       **vals,
140     int                 nvals,
141     int                 naddvals,
142     int                 *maxvals,
143     Attribute           ***a
144 )
145 {
146         if ( *a == NULL ) {
147                 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
148                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
149                                 break;
150                         }
151                 }
152         }
153
154         if ( **a == NULL ) {
155                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
156                 (**a)->a_vals = NULL;
157                 (**a)->a_type = attr_normalize( ch_strdup( type ) );
158                 (**a)->a_syntax = attr_syntax( type );
159                 (**a)->a_next = NULL;
160         }
161
162         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
163             maxvals ) );
164 }
165 #endif
166
167
168 /*
169  * attr_merge - merge the given type and value with the list of
170  * attributes in attrs.
171  * returns      0       everything went ok
172  *              -1      trouble
173  */
174
175 int
176 attr_merge(
177     Entry               *e,
178 #ifdef SLAPD_SCHEMA_NOT_COMPAT
179         AttributeDescription *desc,
180 #else
181     const char          *type,
182 #endif
183     struct berval       **vals )
184 {
185         Attribute       **a;
186
187         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
188 #ifdef SLAPD_SCHEMA_NOT_COMPAT
189                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
190 #else
191                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
192 #endif
193                 {
194                         break;
195                 }
196         }
197
198         if ( *a == NULL ) {
199                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
200 #ifdef SLAPD_SCHEMA_NOT_COMPAT
201                 (*a)->a_desc = ad_dup( desc );
202 #else
203                 (*a)->a_type = attr_normalize( ch_strdup( type ) );
204                 (*a)->a_syntax = attr_syntax( type );
205 #endif
206                 (*a)->a_vals = NULL;
207                 (*a)->a_next = NULL;
208         }
209
210         return( value_add( &(*a)->a_vals, vals ) );
211 }
212
213 #ifdef SLAPD_SCHEMA_NOT_COMPAT
214 /*
215  * attrs_find - find attribute(s) by AttributeDescription
216  * returns next attribute which is subtype of provided description.
217  */
218
219 Attribute *
220 attrs_find(
221     Attribute   *a,
222         AttributeDescription *desc
223 )
224 {
225         for ( ; a != NULL; a = a->a_next ) {
226                 if ( is_ad_subtype( a->a_desc, desc ) ) {
227                         return( a );
228                 }
229         }
230
231         return( NULL );
232 }
233 #endif
234
235 /*
236  * attr_find - find attribute by type
237  */
238
239 Attribute *
240 attr_find(
241     Attribute   *a,
242 #ifdef SLAPD_SCHEMA_NOT_COMPAT
243         AttributeDescription *desc
244 #else
245         const char      *type
246 #endif
247 )
248 {
249         for ( ; a != NULL; a = a->a_next ) {
250 #ifdef SLAPD_SCHEMA_NOT_COMPAT
251                 if ( ad_cmp( a->a_desc, desc ) == 0 )
252 #else
253                 if ( strcasecmp( a->a_type, type ) == 0 )
254 #endif
255                 {
256                         return( a );
257                 }
258         }
259
260         return( NULL );
261 }
262
263 /*
264  * attr_delete - delete the attribute type in list pointed to by attrs
265  * return       0       deleted ok
266  *              1       not found in list a
267  *              -1      something bad happened
268  */
269
270 int
271 attr_delete(
272     Attribute   **attrs,
273 #ifdef SLAPD_SCHEMA_NOT_COMPAT
274         AttributeDescription *desc
275 #else
276     const char  *type
277 #endif
278 )
279 {
280         Attribute       **a;
281
282         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
283 #ifdef SLAPD_SCHEMA_NOT_COMPAT
284                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
285 #else
286                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
287 #endif
288                 {
289                         Attribute       *save = *a;
290                         *a = (*a)->a_next;
291                         attr_free( save );
292
293                         return LDAP_SUCCESS;
294                 }
295         }
296
297         return LDAP_NO_SUCH_ATTRIBUTE;
298 }
299