]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/attr.c
f642aedec9d834a0c7e84fb260331efc792a7958
[openldap] / servers / slapd / back-ldbm / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18
19 /* for the cache of attribute information (which are indexed, etc.) */
20 typedef struct ldbm_attrinfo {
21 #ifdef SLAPD_USE_AD
22         AttributeDescription *ai_desc; /* attribute description cn;lang-en      */
23 #else
24         char *ai_desc;
25 #endif
26         slap_mask_t ai_indexmask;       /* how the attr is indexed      */
27 } AttrInfo;
28
29 static int
30 ainfo_type_cmp(
31 #ifdef SLAPD_USE_AD
32         AttributeDescription *desc,
33 #else
34     char                *desc,
35 #endif
36     AttrInfo    *a
37 )
38 {
39 #ifdef SLAPD_USE_AD
40         return ad_cmp( desc, a->ai_desc );
41 #else
42         return( strcasecmp( desc, a->ai_desc ) );
43 #endif
44 }
45
46 static int
47 ainfo_cmp(
48     AttrInfo    *a,
49     AttrInfo    *b
50 )
51 {
52 #ifdef SLAPD_USE_AD
53         return ad_cmp( a->ai_desc, b->ai_desc );
54 #else
55         return( strcasecmp( a->ai_desc, b->ai_desc ) );
56 #endif
57 }
58
59 void
60 attr_mask(
61     struct ldbminfo     *li,
62 #ifdef SLAPD_USE_AD
63         AttributeDescription *desc,
64 #else
65     const char *desc,
66 #endif
67     slap_mask_t *indexmask )
68 {
69         AttrInfo        *a;
70
71         a = (AttrInfo *) avl_find( li->li_attrs, desc,
72             (AVL_CMP) ainfo_type_cmp );
73         
74         *indexmask = a != NULL ? a->ai_indexmask : 0;
75 }
76
77 int
78 attr_index_config(
79     struct ldbminfo     *li,
80     const char          *fname,
81     int                 lineno,
82     int                 argc,
83     char                **argv )
84 {
85         int rc;
86         int     i;
87         slap_mask_t mask;
88         char **attrs;
89         char **indexes = NULL;
90
91         attrs = str2charray( argv[0], "," );
92
93         if( attrs == NULL ) {
94                 fprintf( stderr, "%s: line %d: "
95                         "no attributes specified: %s\n",
96                         fname, lineno, argv[0] );
97                 return LDAP_PARAM_ERROR;
98         }
99
100         if ( argc > 1 ) {
101                 indexes = str2charray( argv[1], "," );
102
103                 if( indexes == NULL ) {
104                         fprintf( stderr, "%s: line %d: "
105                                 "no indexes specified: %s\n",
106                                 fname, lineno, argv[1] );
107                         return LDAP_PARAM_ERROR;
108                 }
109         }
110
111         if( indexes == NULL ) {
112                 mask = li->li_defaultmask;
113
114         } else {
115                 mask = 0;
116
117                 for ( i = 0; indexes[i] != NULL; i++ ) {
118                         slap_mask_t index;
119                         rc = slap_str2index( indexes[i], &index );
120
121                         if( rc != LDAP_SUCCESS ) {
122                                 fprintf( stderr, "%s: line %d: "
123                                         "index type \"%s\" undefined\n",
124                                         fname, lineno, indexes[i] );
125                                 return LDAP_PARAM_ERROR;
126                         }
127
128                         mask |= index;
129                 }
130         }
131
132     if( !mask ) {
133                 fprintf( stderr, "%s: line %d: "
134                         "no indexes selected\n",
135                         fname, lineno );
136                 return LDAP_PARAM_ERROR;
137         }
138
139         for ( i = 0; attrs[i] != NULL; i++ ) {
140                 AttrInfo        *a;
141                 AttributeDescription *ad;
142                 const char *text;
143
144                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
145                         li->li_defaultmask = mask;
146                         continue;
147                 }
148
149                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
150
151                 ad = NULL;
152                 rc = slap_str2ad( attrs[i], &ad, &text );
153
154                 if( rc != LDAP_SUCCESS ) {
155                         fprintf( stderr, "%s: line %d: "
156                                 "index attribute \"%s\" undefined\n",
157                                 fname, lineno, attrs[i] );
158                         return rc;
159                 }
160
161                 if( slap_ad_is_binary( ad ) ) {
162                         fprintf( stderr, "%s: line %d: "
163                                 "index of attribute \"%s\" disallowed\n",
164                                 fname, lineno, attrs[i] );
165                         return LDAP_UNWILLING_TO_PERFORM;
166                 }
167
168                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
169                         ( ad->ad_type->sat_approx
170                                 && ad->ad_type->sat_approx->smr_indexer
171                                 && ad->ad_type->sat_approx->smr_filter )
172                         && ( ad->ad_type->sat_equality
173                                 && ad->ad_type->sat_equality->smr_indexer
174                                 && ad->ad_type->sat_equality->smr_filter ) ) )
175                 {
176                         fprintf( stderr, "%s: line %d: "
177                                 "approx index of attribute \"%s\" disallowed\n",
178                                 fname, lineno, attrs[i] );
179                         return LDAP_INAPPROPRIATE_MATCHING;
180                 }
181
182                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
183                         ad->ad_type->sat_equality
184                                 && ad->ad_type->sat_equality->smr_indexer
185                                 && ad->ad_type->sat_equality->smr_filter ) )
186                 {
187                         fprintf( stderr, "%s: line %d: "
188                                 "equality index of attribute \"%s\" disallowed\n",
189                                 fname, lineno, attrs[i] );
190                         return LDAP_INAPPROPRIATE_MATCHING;
191                 }
192
193                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
194                         ad->ad_type->sat_substr
195                                 && ad->ad_type->sat_substr->smr_indexer
196                                 && ad->ad_type->sat_substr->smr_filter ) )
197                 {
198                         fprintf( stderr, "%s: line %d: "
199                                 "substr index of attribute \"%s\" disallowed\n",
200                                 fname, lineno, attrs[i] );
201                         return LDAP_INAPPROPRIATE_MATCHING;
202                 }
203
204 #ifdef NEW_LOGGING
205                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
206                            "attr_index_config: index %s 0x%04x\n",
207                            ad->ad_cname->bv_val, mask ));
208 #else
209                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04x\n",
210                         ad->ad_cname->bv_val, mask, 0 ); 
211 #endif
212
213
214 #ifdef SLAPD_USE_AD
215                 a->ai_desc = ad;
216 #else
217                 a->ai_desc = ch_strdup( ad->ad_cname->bv_val );
218                 ad_free( ad, 1 );
219 #endif
220
221                 a->ai_indexmask = mask;
222
223                 rc = avl_insert( &li->li_attrs, (caddr_t) a,
224                         (AVL_CMP) ainfo_cmp, (AVL_DUP) avl_dup_error );
225
226                 if( rc ) {
227                         fprintf( stderr, "%s: line %d: duplicate index definition "
228                                 "for attr \"%s\" (ignored)\n",
229                             fname, lineno, attrs[i] );
230
231                         return LDAP_PARAM_ERROR;
232                 }
233         }
234
235         charray_free( attrs );
236         if ( indexes != NULL ) charray_free( indexes );
237
238         return LDAP_SUCCESS;
239 }
240
241
242 static void
243 ainfo_free( void *attr )
244 {
245         AttrInfo *ai = attr;
246 #ifdef SLAPD_USE_AD
247         ad_free( ai->ai_desc, 1 );
248 #else
249         free( ai->ai_desc );
250 #endif
251         free( ai );
252 }
253
254 void
255 attr_index_destroy( Avlnode *tree )
256 {
257         avl_free( tree, ainfo_free );
258 }
259