1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
27 /* for the cache of attribute information (which are indexed, etc.) */
28 typedef struct bdb_attrinfo {
29 AttributeDescription *ai_desc; /* attribute description cn;lang-en */
30 slap_mask_t ai_indexmask; /* how the attr is indexed */
31 #ifdef LDAP_COMP_MATCH
32 ComponentReference* ai_cr; /*component indexing*/
42 const AttributeDescription *desc = v_desc;
43 const AttrInfo *a = v_a;
44 return SLAP_PTRCMP(desc, a->ai_desc);
53 const AttrInfo *a = v_a, *b = v_b;
54 return SLAP_PTRCMP(a->ai_desc, b->ai_desc);
57 #ifdef LDAP_COMP_MATCH
61 AttributeDescription *desc,
62 ComponentReference** cr )
66 a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
68 *cr = a != NULL ? a->ai_cr : 0 ;
73 AttributeDescription *desc,
74 slap_mask_t *indexmask,
75 ComponentReference** cr )
79 a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
81 *indexmask = a->ai_indexmask;
93 AttributeDescription *desc,
94 slap_mask_t *indexmask )
98 a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
100 *indexmask = a != NULL ? a->ai_indexmask : 0;
104 bdb_attr_index_config(
105 struct bdb_info *bdb,
115 char **indexes = NULL;
117 attrs = ldap_str2charray( argv[0], "," );
119 if( attrs == NULL ) {
120 fprintf( stderr, "%s: line %d: "
121 "no attributes specified: %s\n",
122 fname, lineno, argv[0] );
123 return LDAP_PARAM_ERROR;
127 indexes = ldap_str2charray( argv[1], "," );
129 if( indexes == NULL ) {
130 fprintf( stderr, "%s: line %d: "
131 "no indexes specified: %s\n",
132 fname, lineno, argv[1] );
133 return LDAP_PARAM_ERROR;
137 if( indexes == NULL ) {
138 mask = bdb->bi_defaultmask;
143 for ( i = 0; indexes[i] != NULL; i++ ) {
145 rc = slap_str2index( indexes[i], &index );
147 if( rc != LDAP_SUCCESS ) {
148 fprintf( stderr, "%s: line %d: "
149 "index type \"%s\" undefined\n",
150 fname, lineno, indexes[i] );
151 return LDAP_PARAM_ERROR;
159 fprintf( stderr, "%s: line %d: "
160 "no indexes selected\n",
162 return LDAP_PARAM_ERROR;
165 for ( i = 0; attrs[i] != NULL; i++ ) {
167 AttributeDescription *ad;
169 #ifdef LDAP_COMP_MATCH
170 ComponentReference* cr = NULL;
171 AttrInfo *a_cr = NULL;
174 if( strcasecmp( attrs[i], "default" ) == 0 ) {
175 bdb->bi_defaultmask |= mask;
179 #ifdef LDAP_COMP_MATCH
180 if ( is_component_reference( attrs[i] ) ) {
181 rc = extract_component_reference( attrs[i], &cr );
182 if ( rc != LDAP_SUCCESS ) {
183 fprintf( stderr, "%s: line %d: "
184 "index component reference\"%s\" undefined\n",
185 fname, lineno, attrs[i] );
188 cr->cr_indexmask = mask;
190 * After extracting a component reference
191 * only the name of a attribute will be remaining
197 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
199 #ifdef LDAP_COMP_MATCH
203 rc = slap_str2ad( attrs[i], &ad, &text );
205 if( rc != LDAP_SUCCESS ) {
206 fprintf( stderr, "%s: line %d: "
207 "index attribute \"%s\" undefined\n",
208 fname, lineno, attrs[i] );
212 if( slap_ad_is_binary( ad ) ) {
213 fprintf( stderr, "%s: line %d: "
214 "index of attribute \"%s\" disallowed\n",
215 fname, lineno, attrs[i] );
216 return LDAP_UNWILLING_TO_PERFORM;
219 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
220 ad->ad_type->sat_approx
221 && ad->ad_type->sat_approx->smr_indexer
222 && ad->ad_type->sat_approx->smr_filter ) )
224 fprintf( stderr, "%s: line %d: "
225 "approx index of attribute \"%s\" disallowed\n",
226 fname, lineno, attrs[i] );
227 return LDAP_INAPPROPRIATE_MATCHING;
230 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
231 ad->ad_type->sat_equality
232 && ad->ad_type->sat_equality->smr_indexer
233 && ad->ad_type->sat_equality->smr_filter ) )
235 fprintf( stderr, "%s: line %d: "
236 "equality index of attribute \"%s\" disallowed\n",
237 fname, lineno, attrs[i] );
238 return LDAP_INAPPROPRIATE_MATCHING;
241 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
242 ad->ad_type->sat_substr
243 && ad->ad_type->sat_substr->smr_indexer
244 && ad->ad_type->sat_substr->smr_filter ) )
246 fprintf( stderr, "%s: line %d: "
247 "substr index of attribute \"%s\" disallowed\n",
248 fname, lineno, attrs[i] );
249 return LDAP_INAPPROPRIATE_MATCHING;
252 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
253 ad->ad_cname.bv_val, mask, 0 );
256 a->ai_indexmask = mask;
257 #ifdef LDAP_COMP_MATCH
259 a_cr = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
262 * AttrInfo is already in AVL
263 * just add the extracted component reference
266 rc = insert_component_reference( cr, &a_cr->ai_cr );
267 if ( rc != LDAP_SUCCESS) {
268 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
269 return LDAP_PARAM_ERROR;
273 rc = insert_component_reference( cr, &a->ai_cr );
274 if ( rc != LDAP_SUCCESS) {
275 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
276 return LDAP_PARAM_ERROR;
281 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
282 ainfo_cmp, avl_dup_error );
285 fprintf( stderr, "%s: line %d: duplicate index definition "
286 "for attr \"%s\" (ignored)\n",
287 fname, lineno, attrs[i] );
289 return LDAP_PARAM_ERROR;
293 ldap_charray_free( attrs );
294 if ( indexes != NULL ) ldap_charray_free( indexes );
300 bdb_attr_index_destroy( Avlnode *tree )
302 avl_free( tree, free );