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