]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
New indexer/filter codes (test suite works) with cheats
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ad.c - routines for dealing with attribute descriptions */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19 #include "slap.h"
20
21 #ifdef SLAPD_SCHEMA_NOT_COMPAT
22 AttributeDescription *ad_dup(
23         AttributeDescription *desc )
24 {
25         AttributeDescription *ad;
26
27         if( desc == NULL ) {
28                 return NULL;
29         }
30
31         ad = (AttributeDescription *) ch_malloc( sizeof(AttributeDescription) );
32
33         *ad = *desc;
34
35         if( ad->ad_cname != NULL ) {
36                 ad->ad_cname = ber_bvdup( ad->ad_cname );
37         }
38
39         if( ad->ad_lang != NULL ) {
40                 ad->ad_lang = ch_strdup( ad->ad_lang );
41         }
42
43         return ad;
44 }
45
46 void
47 ad_free( AttributeDescription *ad, int freeit )
48 {
49         if( ad == NULL ) return;
50
51         if( ad->ad_cname != NULL ) {
52                 ber_bvfree( ad->ad_cname );
53         }
54
55         free( ad->ad_lang );
56
57         if( freeit ) free( ad );
58 }
59
60 static int ad_keystring(
61         struct berval *bv )
62 {
63         ber_len_t i;
64
65         if( !AD_CHAR( bv->bv_val[0] ) ) {
66                 return 1;
67         }
68
69         for( i=1; i<bv->bv_len; i++ ) {
70                 if( !AD_CHAR( bv->bv_val[i] ) ) {
71                         return 1;
72                 }
73         }
74         return 0;
75 }
76
77 int slap_str2ad(
78         const char *str,
79         AttributeDescription **ad,
80         const char **text )
81 {
82         struct berval bv;
83         bv.bv_val = (char *) str;
84         bv.bv_len = strlen( str );
85
86         return slap_bv2ad( &bv, ad, text );
87 }
88
89 int slap_bv2ad(
90         struct berval *bv,
91         AttributeDescription **ad,
92         const char **text )
93 {
94         int rtn = LDAP_UNDEFINED_TYPE;
95         int i;
96         AttributeDescription desc;
97         char **tokens;
98
99         assert( ad != NULL );
100         assert( *ad == NULL ); /* temporary */
101
102         if( bv == NULL || bv->bv_len == 0 ) {
103                 *text = "empty attribute description";
104                 return rtn;
105         }
106
107         /* make sure description is IA5 */
108         if( ad_keystring( bv ) ) {
109                 *text = "attribute description contains inappropriate characters";
110                 return rtn;
111         }
112
113         tokens = str2charray( bv->bv_val, ";");
114
115         if( tokens == NULL || *tokens == NULL ) {
116                 *text = "no attribute type";
117                 goto done;
118         }
119
120         desc.ad_type = at_find( *tokens );
121
122         if( desc.ad_type == NULL ) {
123                 *text = "attribute type undefined";
124                 goto done;
125         }
126
127         desc.ad_flags = SLAP_DESC_NONE;
128         desc.ad_lang = NULL;
129
130         for( i=1; tokens[i] != NULL; i++ ) {
131                 if( strcasecmp( tokens[i], "binary" ) == 0 ) {
132                         if( slap_ad_is_binary( &desc ) ) {
133                                 *text = "option \"binary\" specified multiple times";
134                                 goto done;
135                         }
136
137                         if( !slap_syntax_is_binary( desc.ad_type->sat_syntax )) {
138                                 /* not stored in binary, disallow option */
139                                 *text = "option \"binary\" with type not supported";
140                                 goto done;
141                         }
142
143                         desc.ad_flags |= SLAP_DESC_BINARY;
144
145                 } else if ( strncasecmp( tokens[i], "lang-",
146                         sizeof("lang-")-1 ) == 0 && tokens[i][sizeof("lang-")-1] )
147                 {
148                         if( desc.ad_lang != NULL ) {
149                                 *text = "multiple language tag options specified";
150                                 goto done;
151                         }
152
153                         desc.ad_lang = tokens[i];
154
155                         /* normalize to all lower case, it's easy */
156                         ldap_pvt_str2lower( desc.ad_lang );
157
158                 } else {
159                         *text = "unrecognized option";
160                         goto done;
161                 }
162         }
163
164         desc.ad_cname = ch_malloc( sizeof( struct berval ) );
165
166         desc.ad_cname->bv_len = strlen( desc.ad_type->sat_cname );
167         if( slap_ad_is_binary( &desc ) ) {
168                 desc.ad_cname->bv_len += sizeof("binary");
169         }
170         if( desc.ad_lang != NULL ) {
171                 desc.ad_cname->bv_len += 1 + strlen( desc.ad_lang );
172         }
173
174         desc.ad_cname->bv_val = ch_malloc( desc.ad_cname->bv_len + 1 );
175
176         strcpy( desc.ad_cname->bv_val, desc.ad_type->sat_cname );
177         if( slap_ad_is_binary( &desc ) ) {
178                 strcat( desc.ad_cname->bv_val, ";binary" );
179         }
180
181         if( desc.ad_lang != NULL ) {
182                 strcat( desc.ad_cname->bv_val, ";" );
183                 strcat( desc.ad_cname->bv_val, desc.ad_lang );
184         }
185
186         if( *ad == NULL ) {
187                 *ad = ch_malloc( sizeof( AttributeDescription ) );
188         }
189
190         **ad = desc;
191
192         rtn = LDAP_SUCCESS;
193
194 done:
195         charray_free( tokens );
196         return rtn;
197 }
198
199 int is_ad_subtype(
200         AttributeDescription *sub,
201         AttributeDescription *super
202 )
203 {
204         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
205                 return 0;
206         }
207
208         if( super->ad_flags && ( super->ad_flags == sub->ad_flags )) {
209                 return 0;
210         }
211
212         if( super->ad_lang != NULL && ( sub->ad_lang == NULL
213                 || strcasecmp( super->ad_lang, sub->ad_lang )))
214         {
215                 return 0;
216         }
217
218         return 1;
219 }
220
221
222 int ad_inlist(
223         AttributeDescription *desc,
224         char **attrs )
225 {
226         int i;
227         for( i=0; attrs[i] != NULL; i++ ) {
228                 AttributeDescription *ad = NULL;
229                 const char *text;
230                 int rc;
231                 
232                 rc = slap_str2ad( attrs[i], &ad, &text );
233
234                 if( rc != LDAP_SUCCESS ) continue;
235
236                 rc = is_ad_subtype( desc, ad );
237
238                 ad_free( ad, 1 );
239
240                 if( rc ) return 1;
241         }
242
243         return 0;
244 }
245
246 #endif
247