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