]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/attr.c
Happy new year!
[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-2006 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                 {
163                         fprintf( stderr, "%s: line %d: "
164                                 "approx index of attribute \"%s\" disallowed\n",
165                                 fname, lineno, attrs[i] );
166                         return LDAP_INAPPROPRIATE_MATCHING;
167                 }
168
169                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
170                         ad->ad_type->sat_equality
171                                 && ad->ad_type->sat_equality->smr_indexer
172                                 && ad->ad_type->sat_equality->smr_filter ) )
173                 {
174                         fprintf( stderr, "%s: line %d: "
175                                 "equality index of attribute \"%s\" disallowed\n",
176                                 fname, lineno, attrs[i] );
177                         return LDAP_INAPPROPRIATE_MATCHING;
178                 }
179
180                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
181                         ad->ad_type->sat_substr
182                                 && ad->ad_type->sat_substr->smr_indexer
183                                 && ad->ad_type->sat_substr->smr_filter ) )
184                 {
185                         fprintf( stderr, "%s: line %d: "
186                                 "substr index of attribute \"%s\" disallowed\n",
187                                 fname, lineno, attrs[i] );
188                         return LDAP_INAPPROPRIATE_MATCHING;
189                 }
190
191                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
192                         ad->ad_cname.bv_val, mask, 0 ); 
193
194
195                 a->ai_desc = ad;
196
197                 a->ai_indexmask = mask;
198
199                 rc = avl_insert( &li->li_attrs, (caddr_t) a,
200                                  ainfo_cmp, avl_dup_error );
201
202                 if( rc ) {
203                         fprintf( stderr, "%s: line %d: duplicate index definition "
204                                 "for attr \"%s\"" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
205                             fname, lineno, attrs[i] );
206
207                         return LDAP_PARAM_ERROR;
208                 }
209         }
210
211         ldap_charray_free( attrs );
212         if ( indexes != NULL ) ldap_charray_free( indexes );
213
214         return LDAP_SUCCESS;
215 }
216
217 void
218 attr_index_destroy( Avlnode *tree )
219 {
220         avl_free( tree, free );
221 }