1 /* attr.c - backend routines for dealing with attributes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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
59 bdb_attr_comp_ref( struct bdb_info *bdb, AttributeDescription *desc, ComponentReference** cr )
63 a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
65 *cr = a != NULL ? a->ai_cr : 0 ;
72 AttributeDescription *desc,
73 slap_mask_t *indexmask )
77 a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
79 *indexmask = a != NULL ? a->ai_indexmask : 0;
83 bdb_attr_index_config(
94 char **indexes = NULL;
96 attrs = ldap_str2charray( argv[0], "," );
99 fprintf( stderr, "%s: line %d: "
100 "no attributes specified: %s\n",
101 fname, lineno, argv[0] );
102 return LDAP_PARAM_ERROR;
106 indexes = ldap_str2charray( argv[1], "," );
108 if( indexes == NULL ) {
109 fprintf( stderr, "%s: line %d: "
110 "no indexes specified: %s\n",
111 fname, lineno, argv[1] );
112 return LDAP_PARAM_ERROR;
116 if( indexes == NULL ) {
117 mask = bdb->bi_defaultmask;
122 for ( i = 0; indexes[i] != NULL; i++ ) {
124 rc = slap_str2index( indexes[i], &index );
126 if( rc != LDAP_SUCCESS ) {
127 fprintf( stderr, "%s: line %d: "
128 "index type \"%s\" undefined\n",
129 fname, lineno, indexes[i] );
130 return LDAP_PARAM_ERROR;
138 fprintf( stderr, "%s: line %d: "
139 "no indexes selected\n",
141 return LDAP_PARAM_ERROR;
144 for ( i = 0; attrs[i] != NULL; i++ ) {
146 AttributeDescription *ad;
148 #ifdef LDAP_COMP_MATCH
149 ComponentReference* cr = NULL;
150 AttrInfo *a_cr = NULL;
153 if( strcasecmp( attrs[i], "default" ) == 0 ) {
154 bdb->bi_defaultmask |= mask;
158 #ifdef LDAP_COMP_MATCH
159 if ( is_component_reference( attrs[i] ) ) {
160 rc = extract_component_reference( attrs[i], &cr );
161 if ( rc != LDAP_SUCCESS ) {
162 fprintf( stderr, "%s: line %d: "
163 "index component reference\"%s\" undefined\n",
164 fname, lineno, attrs[i] );
167 cr->cr_indexmask = mask;
169 * After extracting a component reference
170 * only the name of a attribute will be remaining
176 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
178 #ifdef LDAP_COMP_MATCH
182 rc = slap_str2ad( attrs[i], &ad, &text );
184 if( rc != LDAP_SUCCESS ) {
185 fprintf( stderr, "%s: line %d: "
186 "index attribute \"%s\" undefined\n",
187 fname, lineno, attrs[i] );
191 if( slap_ad_is_binary( ad ) ) {
192 fprintf( stderr, "%s: line %d: "
193 "index of attribute \"%s\" disallowed\n",
194 fname, lineno, attrs[i] );
195 return LDAP_UNWILLING_TO_PERFORM;
198 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
199 ad->ad_type->sat_approx
200 && ad->ad_type->sat_approx->smr_indexer
201 && ad->ad_type->sat_approx->smr_filter ) )
203 fprintf( stderr, "%s: line %d: "
204 "approx index of attribute \"%s\" disallowed\n",
205 fname, lineno, attrs[i] );
206 return LDAP_INAPPROPRIATE_MATCHING;
209 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
210 ad->ad_type->sat_equality
211 && ad->ad_type->sat_equality->smr_indexer
212 && ad->ad_type->sat_equality->smr_filter ) )
214 fprintf( stderr, "%s: line %d: "
215 "equality index of attribute \"%s\" disallowed\n",
216 fname, lineno, attrs[i] );
217 return LDAP_INAPPROPRIATE_MATCHING;
220 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
221 ad->ad_type->sat_substr
222 && ad->ad_type->sat_substr->smr_indexer
223 && ad->ad_type->sat_substr->smr_filter ) )
225 fprintf( stderr, "%s: line %d: "
226 "substr index of attribute \"%s\" disallowed\n",
227 fname, lineno, attrs[i] );
228 return LDAP_INAPPROPRIATE_MATCHING;
231 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
232 ad->ad_cname.bv_val, mask, 0 );
235 a->ai_indexmask = mask;
236 #ifdef LDAP_COMP_MATCH
238 a_cr = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
241 * AttrInfo is already in AVL
242 * just add the extracted component reference
245 rc = insert_component_reference( cr, &a_cr->ai_cr );
246 if ( rc != LDAP_SUCCESS) {
247 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
248 return LDAP_PARAM_ERROR;
252 rc = insert_component_reference( cr, &a->ai_cr );
253 if ( rc != LDAP_SUCCESS) {
254 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
255 return LDAP_PARAM_ERROR;
260 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
261 ainfo_cmp, avl_dup_error );
264 fprintf( stderr, "%s: line %d: duplicate index definition "
265 "for attr \"%s\" (ignored)\n",
266 fname, lineno, attrs[i] );
268 return LDAP_PARAM_ERROR;
272 ldap_charray_free( attrs );
273 if ( indexes != NULL ) ldap_charray_free( indexes );
279 bdb_attr_index_destroy( Avlnode *tree )
281 avl_free( tree, free );