]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/index.c
Add reference to slapd.conf(5) and recommendation to avoid cleartext passwords.
[openldap] / servers / slapd / back-ldbm / index.c
1 /* index.c - routines for dealing with attribute indexes */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12
13 static int      add_value(Backend *be, struct dbcache *db, char *type, int indextype, char *val, ID id);
14 static int      index2prefix(int indextype);
15
16 int
17 index_add_entry(
18     Backend     *be,
19     Entry       *e
20 )
21 {
22         Attribute       *ap;
23         char            *dnval;
24         struct berval   bv;
25         struct berval   *bvals[2];
26
27         Debug( LDAP_DEBUG_TRACE, "=> index_add( %ld, \"%s\" )\n", e->e_id,
28             e->e_dn, 0 );
29
30         /*
31          * dn index entry - make it look like an attribute so it works
32          * with index_add_values() call
33          */
34
35         bv.bv_val = ch_strdup( e->e_dn );
36         bv.bv_len = strlen( bv.bv_val );
37         (void) dn_normalize_case( bv.bv_val );
38         bvals[0] = &bv;
39         bvals[1] = NULL;
40
41         /* add the dn to the indexes */
42         index_add_values( be, "dn", bvals, e->e_id );
43
44         free( bv.bv_val );
45
46         /* add each attribute to the indexes */
47         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
48                 index_add_values( be, ap->a_type, ap->a_vals, e->e_id );
49         }
50
51         Debug( LDAP_DEBUG_TRACE, "<= index_add( %ld, \"%s\" ) 0\n", e->e_id,
52             e->e_dn, 0 );
53         return( 0 );
54 }
55
56 int
57 index_add_mods(
58     Backend     *be,
59     LDAPModList *ml,
60     ID          id
61 )
62 {
63         int     rc;
64
65         for ( ; ml != NULL; ml = ml->ml_next ) {
66                 LDAPMod *mod = &ml->ml_mod;
67
68                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
69                 case LDAP_MOD_ADD:
70                 case LDAP_MOD_REPLACE:
71                         rc = index_add_values( be, mod->mod_type,
72                             mod->mod_bvalues, id );
73                         break;
74
75                 case LDAP_MOD_DELETE:
76                         rc = 0;
77                         break;
78                 }
79
80                 if ( rc != 0 ) {
81                         return( rc );
82                 }
83         }
84
85         return( 0 );
86 }
87
88 IDList *
89 index_read(
90     Backend     *be,
91     char        *type,
92     int         indextype,
93     char        *val
94 )
95 {
96         struct dbcache  *db;
97         Datum           key;
98         IDList          *idl;
99         int             indexmask, syntax;
100         char            prefix;
101         char            *realval, *tmpval;
102         char            buf[BUFSIZ];
103
104         ldbm_datum_init( key );
105
106         prefix = index2prefix( indextype );
107         Debug( LDAP_DEBUG_TRACE, "=> index_read( \"%s\" \"%c\" \"%s\" )\n",
108             type, prefix, val );
109
110         attr_masks( be->be_private, type, &indexmask, &syntax );
111         if ( ! (indextype & indexmask) ) {
112                 idl =  idl_allids( be );
113                 Debug( LDAP_DEBUG_TRACE,
114                     "<= index_read %lu candidates (allids - not indexed)\n",
115                     idl ? idl->b_nids : 0, 0, 0 );
116                 return( idl );
117         }
118
119         attr_normalize( type );
120         if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
121             == NULL ) {
122                 Debug( LDAP_DEBUG_ANY,
123                     "<= index_read NULL (could not open %s%s)\n", type,
124                     LDBM_SUFFIX, 0 );
125                 return( NULL );
126         }
127
128         realval = val;
129         tmpval = NULL;
130         if ( prefix != UNKNOWN_PREFIX ) {
131               unsigned int      len = strlen( val );
132
133               if ( (len + 2) < sizeof(buf) ) {
134                         realval = buf;
135                 } else {
136                         /* value + prefix + null */
137                         tmpval = (char *) ch_malloc( len + 2 );
138                         realval = tmpval;
139                 }
140               realval[0] = prefix;
141               strcpy( &realval[1], val );
142         }
143
144         key.dptr = realval;
145         key.dsize = strlen( realval ) + 1;
146
147         idl = idl_fetch( be, db, key );
148       if ( tmpval != NULL ) {
149               free( tmpval );
150       }
151
152         ldbm_cache_close( be, db );
153
154         Debug( LDAP_DEBUG_TRACE, "<= index_read %lu candidates\n",
155             idl ? idl->b_nids : 0, 0, 0 );
156         return( idl );
157 }
158
159 static int
160 add_value(
161     Backend             *be,
162     struct dbcache      *db,
163     char                *type,
164     int                 indextype,
165     char                *val,
166     ID                  id
167 )
168 {
169         int     rc;
170         Datum   key;
171         IDList  *idl;
172         char    prefix;
173         char    *realval, *tmpval, *s;
174         char    buf[BUFSIZ];
175
176         ldbm_datum_init( key );
177
178         prefix = index2prefix( indextype );
179         Debug( LDAP_DEBUG_TRACE, "=> add_value( \"%c%s\" )\n", prefix, val, 0 );
180
181         realval = val;
182         tmpval = NULL;
183         idl = NULL;
184         if ( prefix != UNKNOWN_PREFIX ) {
185               unsigned int     len = strlen( val );
186
187               if ( (len + 2) < sizeof(buf) ) {
188                         realval = buf;
189                 } else {
190                         /* value + prefix + null */
191                         tmpval = (char *) ch_malloc( len + 2 );
192                         realval = tmpval;
193                 }
194               realval[0] = prefix;
195               strcpy( &realval[1], val );
196         }
197
198         key.dptr = realval;
199         key.dsize = strlen( realval ) + 1;
200
201         rc = idl_insert_key( be, db, key, id );
202
203         if ( tmpval != NULL ) {
204                 free( tmpval );
205         }
206         idl_free( idl );
207
208         pthread_yield();
209
210         /* Debug( LDAP_DEBUG_TRACE, "<= add_value %d\n", rc, 0, 0 ); */
211         return( rc );
212 }
213
214 int
215 index_add_values(
216     Backend             *be,
217     char                *type,
218     struct berval       **vals,
219     ID                  id
220 )
221 {
222         char            *val, *p, *code, *w;
223         unsigned        i, j, len;
224         int             indexmask, syntax;
225         char            buf[SUBLEN + 1];
226         char            vbuf[BUFSIZ];
227         char            *bigbuf;
228         struct dbcache  *db;
229
230         Debug( LDAP_DEBUG_TRACE, "=> index_add_values( \"%s\", %ld )\n", type,
231             id, 0 );
232
233         attr_masks( be->be_private, type, &indexmask, &syntax );
234         if ( indexmask == 0 ) {
235                 return( 0 );
236         }
237
238         if ( (db = ldbm_cache_open( be, type, LDBM_SUFFIX, LDBM_WRCREAT ))
239             == NULL ) {
240                 Debug( LDAP_DEBUG_ANY,
241                     "<= index_add_values -1 (could not open/create %s%s)\n",
242                     type, LDBM_SUFFIX, 0 );
243                 return( -1 );
244         }
245
246
247         for ( i = 0; vals[i] != NULL; i++ ) {
248                 /*
249                  * presence index entry
250                  */
251                 if ( indexmask & INDEX_PRESENCE ) {
252                         add_value( be, db, type, INDEX_PRESENCE, "*", id );
253                 }
254
255                 Debug( LDAP_DEBUG_TRACE, "*** index_add_values syntax 0x%x syntax bin 0x%x\n",
256                     syntax, SYNTAX_BIN, 0 );
257                 if ( syntax & SYNTAX_BIN ) {
258                         ldbm_cache_close( be, db );
259                         return( 0 );
260                 }
261
262                 bigbuf = NULL;
263                 len = vals[i]->bv_len;
264
265                 /* value + null */
266                 if ( len + 2 > sizeof(vbuf) ) {
267                         bigbuf = (char *) ch_malloc( len + 1 );
268                         val = bigbuf;
269                 } else {
270                         val = vbuf;
271                 }
272                 (void) memcpy( val, vals[i]->bv_val, len );
273                 val[len] = '\0';
274
275                 value_normalize( val, syntax );
276
277                 /*
278                  * equality index entry
279                  */
280                 if ( indexmask & INDEX_EQUALITY ) {
281                         add_value( be, db, type, INDEX_EQUALITY, val, id );
282                 }
283
284                 /*
285                  * approximate index entry
286                  */
287                 if ( indexmask & INDEX_APPROX ) {
288                         for ( w = first_word( val ); w != NULL;
289                             w = next_word( w ) ) {
290                                 if ( (code = phonetic( w )) != NULL ) {
291                                         add_value( be, db, type, INDEX_APPROX,
292                                             code, id );
293                                         free( code );
294                                 }
295                         }
296                 }
297
298                 /*
299                  * substrings index entry
300                  */
301                 if ( indexmask & INDEX_SUB ) {
302                         /* leading and trailing */
303                         if ( len > SUBLEN - 2 ) {
304                                 buf[0] = '^';
305                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
306                                         buf[j + 1] = val[j];
307                                 }
308                                 buf[SUBLEN] = '\0';
309
310                                 add_value( be, db, type, INDEX_SUB, buf, id );
311
312                                 p = val + len - SUBLEN + 1;
313                                 for ( j = 0; j < SUBLEN - 1; j++ ) {
314                                         buf[j] = p[j];
315                                 }
316                                 buf[SUBLEN - 1] = '$';
317                                 buf[SUBLEN] = '\0';
318
319                                 add_value( be, db, type, INDEX_SUB, buf, id );
320                         }
321
322                         /* any */
323                         for ( p = val; p < (val + len - SUBLEN + 1); p++ ) {
324                                 for ( j = 0; j < SUBLEN; j++ ) {
325                                         buf[j] = p[j];
326                                 }
327                                 buf[SUBLEN] = '\0';
328
329                                 add_value( be, db, type, INDEX_SUB, buf, id );
330                         }
331                 }
332
333                 if ( bigbuf != NULL ) {
334                         free( bigbuf );
335                 }
336         }
337         ldbm_cache_close( be, db );
338
339         return( 0 );
340 }
341
342 static int
343 index2prefix( int indextype )
344 {
345         int     prefix;
346
347         switch ( indextype ) {
348         case INDEX_EQUALITY:
349                 prefix = EQ_PREFIX;
350                 break;
351         case INDEX_APPROX:
352                 prefix = APPROX_PREFIX;
353                 break;
354         case INDEX_SUB:
355                 prefix = SUB_PREFIX;
356                 break;
357         default:
358                 prefix = UNKNOWN_PREFIX;
359                 break;
360         }
361
362         return( prefix );
363 }