]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
dd36a7ea0f52d1ea98ab9e06a23f05d081fe13b3
[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     const char          *type,
182     struct berval       **vals )
183 {
184         Attribute       **a;
185
186         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
187 #ifdef SLAPD_SCHEMA_NOT_COMPAT
188                 /* not yet implemented */
189 #else
190                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
191                         break;
192                 }
193 #endif
194         }
195
196         if ( *a == NULL ) {
197                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
198 #ifdef SLAPD_SCHEMA_NOT_COMPAT
199                 /* not yet implemented */
200 #else
201                 (*a)->a_type = attr_normalize( ch_strdup( type ) );
202                 (*a)->a_syntax = attr_syntax( type );
203 #endif
204                 (*a)->a_vals = NULL;
205                 (*a)->a_next = NULL;
206         }
207
208         return( value_add( &(*a)->a_vals, vals ) );
209 }
210
211 #ifdef SLAPD_SCHEMA_NOT_COMPAT
212 /*
213  * attrs_find - find attribute(s) by AttributeDescription
214  * returns next attribute which is subtype of provided description.
215  */
216
217 Attribute *
218 attrs_find(
219     Attribute   *a,
220         AttributeDescription *desc
221 )
222 {
223         for ( ; a != NULL; a = a->a_next ) {
224                 if ( is_ad_subtype( a->a_desc, desc ) == 0 ) {
225                         return( a );
226                 }
227         }
228
229         return( NULL );
230 }
231 #endif
232
233 /*
234  * attr_find - find attribute by type
235  */
236
237 Attribute *
238 attr_find(
239     Attribute   *a,
240 #ifdef SLAPD_SCHEMA_NOT_COMPAT
241         AttributeDescription *desc
242 #else
243         const char      *type
244 #endif
245 )
246 {
247         for ( ; a != NULL; a = a->a_next ) {
248 #ifdef SLAPD_SCHEMA_NOT_COMPAT
249                 if ( ad_cmp( a->a_desc, desc ) == 0 )
250 #else
251                 if ( strcasecmp( a->a_type, type ) == 0 )
252 #endif
253                 {
254                         return( a );
255                 }
256         }
257
258         return( NULL );
259 }
260
261 /*
262  * attr_delete - delete the attribute type in list pointed to by attrs
263  * return       0       deleted ok
264  *              1       not found in list a
265  *              -1      something bad happened
266  */
267
268 int
269 attr_delete(
270     Attribute   **attrs,
271 #ifdef SLAPD_SCHEMA_NOT_COMPAT
272         AttributeDescription *desc
273 #else
274     const char  *type
275 #endif
276 )
277 {
278         Attribute       **a;
279         Attribute       *save;
280
281         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
282 #ifdef SLAPD_SCHEMA_NOT_COMPAT
283                 /* not yet implemented */
284 #else
285                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
286                         break;
287                 }
288 #endif
289         }
290
291         if ( *a == NULL ) {
292                 return( 1 );
293         }
294
295         save = *a;
296         *a = (*a)->a_next;
297         attr_free( save );
298
299         return( 0 );
300 }
301