]> git.sur5r.net Git - openldap/blob - servers/slapd/attr.c
Eliminate second session protocol version field.
[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         ad_free( &a->a_desc, 0 );
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 = a->a_desc;
83         tmp->a_desc.ad_cname = ber_bvdup( a->a_desc.ad_cname );
84         tmp->a_desc.ad_lang = ch_strdup( a->a_desc.ad_lang );
85 #else
86         tmp->a_type = ch_strdup( a->a_type );
87         tmp->a_syntax = a->a_syntax;
88 #endif
89         tmp->a_next = NULL;
90
91         return tmp;
92 }
93
94 Attribute *attrs_dup( Attribute *a )
95 {
96         Attribute *tmp, **next;
97
98         if( a == NULL ) return NULL;
99
100         tmp = NULL;
101         next = &tmp;
102
103         for( ; a != NULL ; a = a->a_next ) {
104                 *next = attr_dup( a );
105                 next = &((*next)->a_next);
106         }
107         *next = NULL;
108
109         return tmp;
110 }
111
112 #ifndef SLAPD_SCHEMA_NOT_COMPAT
113 /*
114  * attr_normalize - normalize an attribute name (make it all lowercase)
115  */
116
117 char *
118 attr_normalize( char *s )
119 {
120         assert( s != NULL );
121
122         return( ldap_pvt_str2lower( s ) );
123 }
124 #endif
125
126 #ifndef SLAPD_SCHEMA_NOT_COMPAT
127 /*
128  * attr_merge_fast - merge the given type and value with the list of
129  * attributes in attrs. called from str2entry(), where we can make some
130  * assumptions to make things faster.
131  * returns      0       everything went ok
132  *              -1      trouble
133  */
134
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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
149                         /* not yet implemented */
150 #else
151                         if ( strcasecmp( (**a)->a_type, type ) == 0 ) {
152                                 break;
153                         }
154 #endif
155                 }
156         }
157
158         if ( **a == NULL ) {
159                 **a = (Attribute *) ch_malloc( sizeof(Attribute) );
160                 (**a)->a_vals = NULL;
161 #ifndef SLAPD_SCHEMA_NOT_COMPAT
162                 (**a)->a_type = attr_normalize( ch_strdup( type ) );
163                 (**a)->a_syntax = attr_syntax( type );
164 #endif
165                 (**a)->a_next = NULL;
166         }
167
168         return( value_add_fast( &(**a)->a_vals, vals, nvals, naddvals,
169             maxvals ) );
170 }
171 #endif
172
173 /*
174  * attr_merge - merge the given type and value with the list of
175  * attributes in attrs.
176  * returns      0       everything went ok
177  *              -1      trouble
178  */
179
180 int
181 attr_merge(
182     Entry               *e,
183     const char          *type,
184     struct berval       **vals )
185 {
186         Attribute       **a;
187
188         for ( a = &e->e_attrs; *a != NULL; a = &(*a)->a_next ) {
189 #ifdef SLAPD_SCHEMA_NOT_COMPAT
190                 /* not yet implemented */
191 #else
192                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
193                         break;
194                 }
195 #endif
196         }
197
198         if ( *a == NULL ) {
199                 *a = (Attribute *) ch_malloc( sizeof(Attribute) );
200 #ifdef SLAPD_SCHEMA_NOT_COMPAT
201                 /* not yet implemented */
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 /*
214  * attr_find - find attribute by type
215  */
216
217 Attribute *
218 attr_find(
219     Attribute   *a,
220 #ifdef SLAPD_SCHEMA_NOT_COMPAT
221         AttributeDescription *desc
222 #else
223         const char      *type
224 #endif
225 )
226 {
227         for ( ; a != NULL; a = a->a_next ) {
228 #ifdef SLAPD_SCHEMA_NOT_COMPAT
229                 /* not yet implemented */
230 #else
231                 if ( strcasecmp( a->a_type, type ) == 0 ) {
232                         return( a );
233                 }
234 #endif
235         }
236
237         return( NULL );
238 }
239
240 /*
241  * attr_delete - delete the attribute type in list pointed to by attrs
242  * return       0       deleted ok
243  *              1       not found in list a
244  *              -1      something bad happened
245  */
246
247 int
248 attr_delete(
249     Attribute   **attrs,
250 #ifdef SLAPD_SCHEMA_NOT_COMPAT
251         AttributeDescription *desc
252 #else
253     const char  *type
254 #endif
255 )
256 {
257         Attribute       **a;
258         Attribute       *save;
259
260         for ( a = attrs; *a != NULL; a = &(*a)->a_next ) {
261 #ifdef SLAPD_SCHEMA_NOT_COMPAT
262                 /* not yet implemented */
263 #else
264                 if ( strcasecmp( (*a)->a_type, type ) == 0 ) {
265                         break;
266                 }
267 #endif
268         }
269
270         if ( *a == NULL ) {
271                 return( 1 );
272         }
273
274         save = *a;
275         *a = (*a)->a_next;
276         attr_free( save );
277
278         return( 0 );
279 }
280