]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/attr.c
850c4650cf9576ff76ad69679bc24071dd9acc17
[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_index 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_index *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         int init )
85 {
86         int rc;
87         int     i;
88         slap_index mask;
89         char **attrs;
90         char **indexes = NULL;
91
92         attrs = str2charray( argv[0], "," );
93
94         if( attrs == NULL ) {
95                 fprintf( stderr, "%s: line %d: "
96                         "no attributes specified: %s\n",
97                         fname, lineno, argv[0] );
98                 return LDAP_PARAM_ERROR;
99         }
100
101         if ( argc > 1 ) {
102                 indexes = str2charray( argv[1], "," );
103
104                 if( indexes == NULL ) {
105                         fprintf( stderr, "%s: line %d: "
106                                 "no indexes specified: %s\n",
107                                 fname, lineno, argv[1] );
108                         return LDAP_PARAM_ERROR;
109                 }
110         }
111
112         if( indexes == NULL ) {
113                 mask = li->li_defaultmask;
114
115         } else {
116                 mask = 0;
117
118                 for ( i = 0; indexes[i] != NULL; i++ ) {
119                         slap_index index;
120                         rc = slap_str2index( indexes[i], &index );
121
122                         if( rc != LDAP_SUCCESS ) {
123                                 fprintf( stderr, "%s: line %d: "
124                                         "index type \"%s\" undefined\n",
125                                         fname, lineno, indexes[i] );
126                                 return LDAP_PARAM_ERROR;
127                         }
128
129                         mask |= index;
130                 }
131         }
132
133     if( !mask ) {
134                 fprintf( stderr, "%s: line %d: "
135                         "no indexes selected\n",
136                         fname, lineno );
137                 return LDAP_PARAM_ERROR;
138         }
139
140         for ( i = 0; attrs[i] != NULL; i++ ) {
141                 AttrInfo        *a;
142 #ifdef SLAPD_SCHEMA_NOT_COMPAT
143                 AttributeDescription *ad;
144                 const char *text;
145 #endif
146
147                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
148                         li->li_defaultmask = mask;
149                         continue;
150                 }
151
152                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
153
154 #ifdef SLAPD_SCHEMA_NOT_COMPAT
155                 ad = NULL;
156                 rc = slap_str2ad( attrs[i], &ad, &text );
157
158                 if( rc != LDAP_SUCCESS ) {
159                         fprintf( stderr, "%s: line %d: "
160                                 "index attribute \"%s\" undefined\n",
161                                 fname, lineno, attrs[i] );
162                         return rc;
163                 }
164
165                 if( slap_ad_is_binary( ad ) ) {
166                         fprintf( stderr, "%s: line %d: "
167                                 "index of attribute \"%s\" disallowed\n",
168                                 fname, lineno, attrs[i] );
169                         return LDAP_UNWILLING_TO_PERFORM;
170                 }
171
172                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
173                         ( ad->ad_type->sat_approx
174                                 && ad->ad_type->sat_approx->smr_indexer
175                                 && ad->ad_type->sat_approx->smr_filter )
176                         && ( ad->ad_type->sat_equality
177                                 && ad->ad_type->sat_equality->smr_indexer
178                                 && ad->ad_type->sat_equality->smr_filter ) ) )
179                 {
180                         fprintf( stderr, "%s: line %d: "
181                                 "approx index of attribute \"%s\" disallowed\n",
182                                 fname, lineno, attrs[i] );
183                         return LDAP_INAPPROPRIATE_MATCHING;
184                 }
185
186                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
187                         ad->ad_type->sat_equality
188                                 && ad->ad_type->sat_equality->smr_indexer
189                                 && ad->ad_type->sat_equality->smr_filter ) )
190                 {
191                         fprintf( stderr, "%s: line %d: "
192                                 "equality index of attribute \"%s\" disallowed\n",
193                                 fname, lineno, attrs[i] );
194                         return LDAP_INAPPROPRIATE_MATCHING;
195                 }
196
197                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
198                         ad->ad_type->sat_substr
199                                 && ad->ad_type->sat_substr->smr_indexer
200                                 && ad->ad_type->sat_substr->smr_filter ) )
201                 {
202                         fprintf( stderr, "%s: line %d: "
203                                 "substr index of attribute \"%s\" disallowed\n",
204                                 fname, lineno, attrs[i] );
205                         return LDAP_INAPPROPRIATE_MATCHING;
206                 }
207
208                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04x\n",
209                         ad->ad_cname->bv_val, mask, 0 ); 
210
211 #ifdef SLAPD_USE_AD
212                 a->ai_desc = ad;
213 #else
214                 a->ai_desc = ch_strdup( ad->ad_cname->bv_val );
215                 ad_free( ad, 1 );
216 #endif
217 #else
218                 a->ai_desc = ch_strdup( attrs[i] );
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 && !init ) {
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