]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
ff82d744d468c43b73d9445640e6e97744110773
[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 #ifndef SLAPD_SCHEMA_NOT_COMPAT
125 /*
126  * attr_merge_fast - merge the given type and value with the list of
127  * attributes in attrs. called from str2entry(), where we can make some
128  * assumptions to make things faster.
129  * returns      0       everything went ok
130  *              -1      trouble
131  */
132
133 int
134 attr_merge_fast(
135     Entry               *e,
136     const char          *type,
137     struct berval       **vals,
138     int                 nvals,
139     int                 naddvals,
140     int                 *maxvals,
141     Attribute           ***a
142 )
143 {
144         if ( *a == NULL ) {
145                 for ( *a = &e->e_attrs; **a != NULL; *a = &(**a)->a_next ) {
146 #ifdef SLAPD_SCHEMA_NOT_COMPAT
147                         /* not yet implemented */
148 #else
149                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
150                                 break;
151                         }
152 #endif
153                 }
154         }
155
156         if ( **a == NULL ) {
157                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
158                 (**a)->a_vals = NULL;
159 #ifndef SLAPD_SCHEMA_NOT_COMPAT
160                 (**a)->a_type = attr_normalize( ch_strdup( type ) );
161                 (**a)->a_syntax = attr_syntax( type );
162 #endif
163                 (**a)->a_next = NULL;
164         }
165
166         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
167             maxvals ) );
168 }
169 #endif
170
171 /*
172  * attr_merge - merge the given type and value with the list of
173  * attributes in attrs.
174  * returns      0       everything went ok
175  *              -1      trouble
176  */
177
178 int
179 attr_merge(
180     Entry               *e,
181 #ifdef SLAPD_SCHEMA_NOT_COMPAT
182         AttributeDescription *desc,
183 #else
184     const char          *type,
185 #endif
186     struct berval       **vals )
187 {
188         Attribute       **a;
189
190         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
191 #ifdef SLAPD_SCHEMA_NOT_COMPAT
192                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
193 #else
194                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
195 #endif
196                 {
197                         break;
198                 }
199         }
200
201         if ( *a == NULL ) {
202                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
203 #ifdef SLAPD_SCHEMA_NOT_COMPAT
204                 (*a)->a_desc = ad_dup( desc );
205 #else
206                 (*a)->a_type = attr_normalize( ch_strdup( type ) );
207                 (*a)->a_syntax = attr_syntax( type );
208 #endif
209                 (*a)->a_vals = NULL;
210                 (*a)->a_next = NULL;
211         }
212
213         return( value_add( &(*a)->a_vals, vals ) );
214 }
215
216 #ifdef SLAPD_SCHEMA_NOT_COMPAT
217 /*
218  * attrs_find - find attribute(s) by AttributeDescription
219  * returns next attribute which is subtype of provided description.
220  */
221
222 Attribute *
223 attrs_find(
224     Attribute   *a,
225         AttributeDescription *desc
226 )
227 {
228         for ( ; a != NULL; a = a->a_next ) {
229                 if ( is_ad_subtype( a->a_desc, desc ) == 0 ) {
230                         return( a );
231                 }
232         }
233
234         return( NULL );
235 }
236 #endif
237
238 /*
239  * attr_find - find attribute by type
240  */
241
242 Attribute *
243 attr_find(
244     Attribute   *a,
245 #ifdef SLAPD_SCHEMA_NOT_COMPAT
246         AttributeDescription *desc
247 #else
248         const char      *type
249 #endif
250 )
251 {
252         for ( ; a != NULL; a = a->a_next ) {
253 #ifdef SLAPD_SCHEMA_NOT_COMPAT
254                 if ( ad_cmp( a->a_desc, desc ) == 0 )
255 #else
256                 if ( strcasecmp( a->a_type, type ) == 0 )
257 #endif
258                 {
259                         return( a );
260                 }
261         }
262
263         return( NULL );
264 }
265
266 /*
267  * attr_delete - delete the attribute type in list pointed to by attrs
268  * return       0       deleted ok
269  *              1       not found in list a
270  *              -1      something bad happened
271  */
272
273 int
274 attr_delete(
275     Attribute   **attrs,
276 #ifdef SLAPD_SCHEMA_NOT_COMPAT
277         AttributeDescription *desc
278 #else
279     const char  *type
280 #endif
281 )
282 {
283         Attribute       **a;
284         Attribute       *save;
285
286         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
287 #ifdef SLAPD_SCHEMA_NOT_COMPAT
288                 if ( ad_cmp( (*a)->a_desc, desc ) == 0 )
289 #else
290                 if ( strcasecmp( (*a)->a_type, type ) == 0 )
291 #endif
292                 {
293                         break;
294                 }
295         }
296
297         if ( *a == NULL ) {
298                 return( 1 );
299         }
300
301         save = *a;
302         *a = (*a)->a_next;
303         attr_free( save );
304
305         return( 0 );
306 }
307