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