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 ;
75 AttributeDescription *desc,
76 slap_mask_t *indexmask )
80 a = (AttrInfo *) avl_find( bdb->bi_attrs, desc, ainfo_type_cmp );
82 *indexmask = a != NULL ? a->ai_indexmask : 0;
86 bdb_attr_index_config(
97 char **indexes = NULL;
99 attrs = ldap_str2charray( argv[0], "," );
101 if( attrs == NULL ) {
102 fprintf( stderr, "%s: line %d: "
103 "no attributes specified: %s\n",
104 fname, lineno, argv[0] );
105 return LDAP_PARAM_ERROR;
109 indexes = ldap_str2charray( argv[1], "," );
111 if( indexes == NULL ) {
112 fprintf( stderr, "%s: line %d: "
113 "no indexes specified: %s\n",
114 fname, lineno, argv[1] );
115 return LDAP_PARAM_ERROR;
119 if( indexes == NULL ) {
120 mask = bdb->bi_defaultmask;
125 for ( i = 0; indexes[i] != NULL; i++ ) {
127 rc = slap_str2index( indexes[i], &index );
129 if( rc != LDAP_SUCCESS ) {
130 fprintf( stderr, "%s: line %d: "
131 "index type \"%s\" undefined\n",
132 fname, lineno, indexes[i] );
133 return LDAP_PARAM_ERROR;
141 fprintf( stderr, "%s: line %d: "
142 "no indexes selected\n",
144 return LDAP_PARAM_ERROR;
147 for ( i = 0; attrs[i] != NULL; i++ ) {
149 AttributeDescription *ad;
151 #ifdef LDAP_COMP_MATCH
152 ComponentReference* cr = NULL;
153 AttrInfo *a_cr = NULL;
156 if( strcasecmp( attrs[i], "default" ) == 0 ) {
157 bdb->bi_defaultmask |= mask;
161 #ifdef LDAP_COMP_MATCH
162 if ( is_component_reference( attrs[i] ) ) {
163 rc = extract_component_reference( attrs[i], &cr );
164 if ( rc != LDAP_SUCCESS ) {
165 fprintf( stderr, "%s: line %d: "
166 "index component reference\"%s\" undefined\n",
167 fname, lineno, attrs[i] );
170 cr->cr_indexmask = mask;
172 * After extracting a component reference
173 * only the name of a attribute will be remaining
179 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
181 #ifdef LDAP_COMP_MATCH
185 rc = slap_str2ad( attrs[i], &ad, &text );
187 if( rc != LDAP_SUCCESS ) {
188 fprintf( stderr, "%s: line %d: "
189 "index attribute \"%s\" undefined\n",
190 fname, lineno, attrs[i] );
194 if( slap_ad_is_binary( ad ) ) {
195 fprintf( stderr, "%s: line %d: "
196 "index of attribute \"%s\" disallowed\n",
197 fname, lineno, attrs[i] );
198 return LDAP_UNWILLING_TO_PERFORM;
201 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
202 ad->ad_type->sat_approx
203 && ad->ad_type->sat_approx->smr_indexer
204 && ad->ad_type->sat_approx->smr_filter ) )
206 fprintf( stderr, "%s: line %d: "
207 "approx index of attribute \"%s\" disallowed\n",
208 fname, lineno, attrs[i] );
209 return LDAP_INAPPROPRIATE_MATCHING;
212 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
213 ad->ad_type->sat_equality
214 && ad->ad_type->sat_equality->smr_indexer
215 && ad->ad_type->sat_equality->smr_filter ) )
217 fprintf( stderr, "%s: line %d: "
218 "equality index of attribute \"%s\" disallowed\n",
219 fname, lineno, attrs[i] );
220 return LDAP_INAPPROPRIATE_MATCHING;
223 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
224 ad->ad_type->sat_substr
225 && ad->ad_type->sat_substr->smr_indexer
226 && ad->ad_type->sat_substr->smr_filter ) )
228 fprintf( stderr, "%s: line %d: "
229 "substr index of attribute \"%s\" disallowed\n",
230 fname, lineno, attrs[i] );
231 return LDAP_INAPPROPRIATE_MATCHING;
234 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
235 ad->ad_cname.bv_val, mask, 0 );
238 a->ai_indexmask = mask;
239 #ifdef LDAP_COMP_MATCH
241 a_cr = avl_find( bdb->bi_attrs, ad, ainfo_type_cmp );
244 * AttrInfo is already in AVL
245 * just add the extracted component reference
248 rc = insert_component_reference( cr, &a_cr->ai_cr );
249 if ( rc != LDAP_SUCCESS) {
250 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
251 return LDAP_PARAM_ERROR;
255 rc = insert_component_reference( cr, &a->ai_cr );
256 if ( rc != LDAP_SUCCESS) {
257 fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
258 return LDAP_PARAM_ERROR;
263 rc = avl_insert( &bdb->bi_attrs, (caddr_t) a,
264 ainfo_cmp, avl_dup_error );
267 fprintf( stderr, "%s: line %d: duplicate index definition "
268 "for attr \"%s\" (ignored)\n",
269 fname, lineno, attrs[i] );
271 return LDAP_PARAM_ERROR;
275 ldap_charray_free( attrs );
276 if ( indexes != NULL ) ldap_charray_free( indexes );
282 bdb_attr_index_destroy( Avlnode *tree )
284 avl_free( tree, free );