]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/attr.c
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / servers / slapd / back-mdb / 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 2000-2013 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-mdb.h"
26 #include "config.h"
27 #include "lutil.h"
28
29 /* Find the ad, return -1 if not found,
30  * set point for insertion if ins is non-NULL
31  */
32 int
33 mdb_attr_slot( struct mdb_info *mdb, AttributeDescription *ad, int *ins )
34 {
35         unsigned base = 0, cursor = 0;
36         unsigned n = mdb->mi_nattrs;
37         int val = 0;
38         
39         while ( 0 < n ) {
40                 unsigned pivot = n >> 1;
41                 cursor = base + pivot;
42
43                 val = SLAP_PTRCMP( ad, mdb->mi_attrs[cursor]->ai_desc );
44                 if ( val < 0 ) {
45                         n = pivot;
46                 } else if ( val > 0 ) {
47                         base = cursor + 1;
48                         n -= pivot + 1;
49                 } else {
50                         return cursor;
51                 }
52         }
53         if ( ins ) {
54                 if ( val > 0 )
55                         ++cursor;
56                 *ins = cursor;
57         }
58         return -1;
59 }
60
61 static int
62 ainfo_insert( struct mdb_info *mdb, AttrInfo *a )
63 {
64         int x;
65         int i = mdb_attr_slot( mdb, a->ai_desc, &x );
66
67         /* Is it a dup? */
68         if ( i >= 0 )
69                 return -1;
70
71         mdb->mi_attrs = ch_realloc( mdb->mi_attrs, ( mdb->mi_nattrs+1 ) * 
72                 sizeof( AttrInfo * ));
73         if ( x < mdb->mi_nattrs )
74                 AC_MEMCPY( &mdb->mi_attrs[x+1], &mdb->mi_attrs[x],
75                         ( mdb->mi_nattrs - x ) * sizeof( AttrInfo *));
76         mdb->mi_attrs[x] = a;
77         mdb->mi_nattrs++;
78         return 0;
79 }
80
81 AttrInfo *
82 mdb_attr_mask(
83         struct mdb_info *mdb,
84         AttributeDescription *desc )
85 {
86         int i = mdb_attr_slot( mdb, desc, NULL );
87         return i < 0 ? NULL : mdb->mi_attrs[i];
88 }
89
90 /* Open all un-opened index DB handles */
91 int
92 mdb_attr_dbs_open(
93         BackendDB *be, MDB_txn *tx0, ConfigReply *cr )
94 {
95         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
96         MDB_txn *txn;
97         MDB_dbi *dbis = NULL;
98         int i, flags;
99         int rc;
100
101         txn = tx0;
102         if ( txn == NULL ) {
103                 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
104                 if ( rc ) {
105                         snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
106                                 "txn_begin failed: %s (%d).",
107                                 be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
108                         Debug( LDAP_DEBUG_ANY,
109                                 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
110                                 cr->msg, 0, 0 );
111                         return rc;
112                 }
113                 dbis = ch_calloc( 1, mdb->mi_nattrs * sizeof(MDB_dbi) );
114         } else {
115                 rc = 0;
116         }
117
118         flags = MDB_DUPSORT|MDB_DUPFIXED|MDB_INTEGERDUP;
119         if ( !(slapMode & SLAP_TOOL_READONLY) )
120                 flags |= MDB_CREATE;
121
122         for ( i=0; i<mdb->mi_nattrs; i++ ) {
123                 if ( mdb->mi_attrs[i]->ai_dbi ) /* already open */
124                         continue;
125                 rc = mdb_dbi_open( txn, mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
126                         flags, &mdb->mi_attrs[i]->ai_dbi );
127                 if ( rc ) {
128                         snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
129                                 "mdb_dbi_open(%s) failed: %s (%d).",
130                                 be->be_suffix[0].bv_val,
131                                 mdb->mi_attrs[i]->ai_desc->ad_type->sat_cname.bv_val,
132                                 mdb_strerror(rc), rc );
133                         Debug( LDAP_DEBUG_ANY,
134                                 LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
135                                 cr->msg, 0, 0 );
136                         break;
137                 }
138                 /* Remember newly opened DBI handles */
139                 if ( dbis )
140                         dbis[i] = mdb->mi_attrs[i]->ai_dbi;
141         }
142
143         /* Only commit if this is our txn */
144         if ( tx0 == NULL ) {
145                 if ( !rc ) {
146                         rc = mdb_txn_commit( txn );
147                         if ( rc ) {
148                                 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
149                                         "txn_commit failed: %s (%d).",
150                                         be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
151                                 Debug( LDAP_DEBUG_ANY,
152                                         LDAP_XSTRING(mdb_attr_dbs) ": %s\n",
153                                         cr->msg, 0, 0 );
154                         }
155                 } else {
156                         mdb_txn_abort( txn );
157                 }
158                 /* Something failed, forget anything we just opened */
159                 if ( rc ) {
160                         for ( i=0; i<mdb->mi_nattrs; i++ ) {
161                                 if ( dbis[i] ) {
162                                         mdb->mi_attrs[i]->ai_dbi = 0;
163                                         mdb->mi_attrs[i]->ai_indexmask |= MDB_INDEX_DELETING;
164                                 }
165                         }
166                         mdb_attr_flush( mdb );
167                 }
168                 ch_free( dbis );
169         }
170
171         return rc;
172 }
173
174 void
175 mdb_attr_dbs_close(
176         struct mdb_info *mdb
177 )
178 {
179         int i;
180         for ( i=0; i<mdb->mi_nattrs; i++ )
181                 if ( mdb->mi_attrs[i]->ai_dbi ) {
182                         mdb_dbi_close( mdb->mi_dbenv, mdb->mi_attrs[i]->ai_dbi );
183                         mdb->mi_attrs[i]->ai_dbi = 0;
184                 }
185 }
186
187 int
188 mdb_attr_index_config(
189         struct mdb_info *mdb,
190         const char              *fname,
191         int                     lineno,
192         int                     argc,
193         char            **argv,
194         struct          config_reply_s *c_reply)
195 {
196         int rc = 0;
197         int     i;
198         slap_mask_t mask;
199         char **attrs;
200         char **indexes = NULL;
201
202         attrs = ldap_str2charray( argv[0], "," );
203
204         if( attrs == NULL ) {
205                 fprintf( stderr, "%s: line %d: "
206                         "no attributes specified: %s\n",
207                         fname, lineno, argv[0] );
208                 return LDAP_PARAM_ERROR;
209         }
210
211         if ( argc > 1 ) {
212                 indexes = ldap_str2charray( argv[1], "," );
213
214                 if( indexes == NULL ) {
215                         fprintf( stderr, "%s: line %d: "
216                                 "no indexes specified: %s\n",
217                                 fname, lineno, argv[1] );
218                         rc = LDAP_PARAM_ERROR;
219                         goto done;
220                 }
221         }
222
223         if( indexes == NULL ) {
224                 mask = mdb->mi_defaultmask;
225
226         } else {
227                 mask = 0;
228
229                 for ( i = 0; indexes[i] != NULL; i++ ) {
230                         slap_mask_t index;
231                         rc = slap_str2index( indexes[i], &index );
232
233                         if( rc != LDAP_SUCCESS ) {
234                                 if ( c_reply )
235                                 {
236                                         snprintf(c_reply->msg, sizeof(c_reply->msg),
237                                                 "index type \"%s\" undefined", indexes[i] );
238
239                                         fprintf( stderr, "%s: line %d: %s\n",
240                                                 fname, lineno, c_reply->msg );
241                                 }
242                                 rc = LDAP_PARAM_ERROR;
243                                 goto done;
244                         }
245
246                         mask |= index;
247                 }
248         }
249
250         if( !mask ) {
251                 if ( c_reply )
252                 {
253                         snprintf(c_reply->msg, sizeof(c_reply->msg),
254                                 "no indexes selected" );
255                         fprintf( stderr, "%s: line %d: %s\n",
256                                 fname, lineno, c_reply->msg );
257                 }
258                 rc = LDAP_PARAM_ERROR;
259                 goto done;
260         }
261
262         for ( i = 0; attrs[i] != NULL; i++ ) {
263                 AttrInfo        *a;
264                 AttributeDescription *ad;
265                 const char *text;
266 #ifdef LDAP_COMP_MATCH
267                 ComponentReference* cr = NULL;
268                 AttrInfo *a_cr = NULL;
269 #endif
270
271                 if( strcasecmp( attrs[i], "default" ) == 0 ) {
272                         mdb->mi_defaultmask |= mask;
273                         continue;
274                 }
275
276 #ifdef LDAP_COMP_MATCH
277                 if ( is_component_reference( attrs[i] ) ) {
278                         rc = extract_component_reference( attrs[i], &cr );
279                         if ( rc != LDAP_SUCCESS ) {
280                                 if ( c_reply )
281                                 {
282                                         snprintf(c_reply->msg, sizeof(c_reply->msg),
283                                                 "index component reference\"%s\" undefined",
284                                                 attrs[i] );
285                                         fprintf( stderr, "%s: line %d: %s\n",
286                                                 fname, lineno, c_reply->msg );
287                                 }
288                                 goto done;
289                         }
290                         cr->cr_indexmask = mask;
291                         /*
292                          * After extracting a component reference
293                          * only the name of a attribute will be remaining
294                          */
295                 } else {
296                         cr = NULL;
297                 }
298 #endif
299                 ad = NULL;
300                 rc = slap_str2ad( attrs[i], &ad, &text );
301
302                 if( rc != LDAP_SUCCESS ) {
303                         if ( c_reply )
304                         {
305                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
306                                         "index attribute \"%s\" undefined",
307                                         attrs[i] );
308
309                                 fprintf( stderr, "%s: line %d: %s\n",
310                                         fname, lineno, c_reply->msg );
311                         }
312                         goto done;
313                 }
314
315                 if( ad == slap_schema.si_ad_entryDN || slap_ad_is_binary( ad ) ) {
316                         if (c_reply) {
317                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
318                                         "index of attribute \"%s\" disallowed", attrs[i] );
319                                 fprintf( stderr, "%s: line %d: %s\n",
320                                         fname, lineno, c_reply->msg );
321                         }
322                         rc = LDAP_UNWILLING_TO_PERFORM;
323                         goto done;
324                 }
325
326                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_APPROX ) && !(
327                         ad->ad_type->sat_approx
328                                 && ad->ad_type->sat_approx->smr_indexer
329                                 && ad->ad_type->sat_approx->smr_filter ) )
330                 {
331                         if (c_reply) {
332                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
333                                         "approx index of attribute \"%s\" disallowed", attrs[i] );
334                                 fprintf( stderr, "%s: line %d: %s\n",
335                                         fname, lineno, c_reply->msg );
336                         }
337                         rc = LDAP_INAPPROPRIATE_MATCHING;
338                         goto done;
339                 }
340
341                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_EQUALITY ) && !(
342                         ad->ad_type->sat_equality
343                                 && ad->ad_type->sat_equality->smr_indexer
344                                 && ad->ad_type->sat_equality->smr_filter ) )
345                 {
346                         if (c_reply) {
347                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
348                                         "equality index of attribute \"%s\" disallowed", attrs[i] );
349                                 fprintf( stderr, "%s: line %d: %s\n",
350                                         fname, lineno, c_reply->msg );
351                         }
352                         rc = LDAP_INAPPROPRIATE_MATCHING;
353                         goto done;
354                 }
355
356                 if( IS_SLAP_INDEX( mask, SLAP_INDEX_SUBSTR ) && !(
357                         ad->ad_type->sat_substr
358                                 && ad->ad_type->sat_substr->smr_indexer
359                                 && ad->ad_type->sat_substr->smr_filter ) )
360                 {
361                         if (c_reply) {
362                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
363                                         "substr index of attribute \"%s\" disallowed", attrs[i] );
364                                 fprintf( stderr, "%s: line %d: %s\n",
365                                         fname, lineno, c_reply->msg );
366                         }
367                         rc = LDAP_INAPPROPRIATE_MATCHING;
368                         goto done;
369                 }
370
371                 Debug( LDAP_DEBUG_CONFIG, "index %s 0x%04lx\n",
372                         ad->ad_cname.bv_val, mask, 0 ); 
373
374                 a = (AttrInfo *) ch_malloc( sizeof(AttrInfo) );
375
376 #ifdef LDAP_COMP_MATCH
377                 a->ai_cr = NULL;
378 #endif
379                 a->ai_cursor = NULL;
380                 a->ai_flist = NULL;
381                 a->ai_clist = NULL;
382                 a->ai_root = NULL;
383                 a->ai_desc = ad;
384                 a->ai_dbi = 0;
385
386                 if ( mdb->mi_flags & MDB_IS_OPEN ) {
387                         a->ai_indexmask = 0;
388                         a->ai_newmask = mask;
389                 } else {
390                         a->ai_indexmask = mask;
391                         a->ai_newmask = 0;
392                 }
393
394 #ifdef LDAP_COMP_MATCH
395                 if ( cr ) {
396                         a_cr = mdb_attr_mask( mdb, ad );
397                         if ( a_cr ) {
398                                 /*
399                                  * AttrInfo is already in AVL
400                                  * just add the extracted component reference
401                                  * in the AttrInfo
402                                  */
403                                 rc = insert_component_reference( cr, &a_cr->ai_cr );
404                                 if ( rc != LDAP_SUCCESS) {
405                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
406                                         rc = LDAP_PARAM_ERROR;
407                                         goto done;
408                                 }
409                                 continue;
410                         } else {
411                                 rc = insert_component_reference( cr, &a->ai_cr );
412                                 if ( rc != LDAP_SUCCESS) {
413                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
414                                         rc = LDAP_PARAM_ERROR;
415                                         goto done;
416                                 }
417                         }
418                 }
419 #endif
420                 rc = ainfo_insert( mdb, a );
421                 if( rc ) {
422                         if ( mdb->mi_flags & MDB_IS_OPEN ) {
423                                 AttrInfo *b = mdb_attr_mask( mdb, ad );
424                                 /* If there is already an index defined for this attribute
425                                  * it must be replaced. Otherwise we end up with multiple 
426                                  * olcIndex values for the same attribute */
427                                 if ( b->ai_indexmask & MDB_INDEX_DELETING ) {
428                                         /* If we were editing this attr, reset it */
429                                         b->ai_indexmask &= ~MDB_INDEX_DELETING;
430                                         /* If this is leftover from a previous add, commit it */
431                                         if ( b->ai_newmask )
432                                                 b->ai_indexmask = b->ai_newmask;
433                                         b->ai_newmask = a->ai_newmask;
434                                         ch_free( a );
435                                         rc = 0;
436                                         continue;
437                                 }
438                         }
439                         if (c_reply) {
440                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
441                                         "duplicate index definition for attr \"%s\"",
442                                         attrs[i] );
443                                 fprintf( stderr, "%s: line %d: %s\n",
444                                         fname, lineno, c_reply->msg );
445                         }
446
447                         rc = LDAP_PARAM_ERROR;
448                         goto done;
449                 }
450         }
451
452 done:
453         ldap_charray_free( attrs );
454         if ( indexes != NULL ) ldap_charray_free( indexes );
455
456         return rc;
457 }
458
459 static int
460 mdb_attr_index_unparser( void *v1, void *v2 )
461 {
462         AttrInfo *ai = v1;
463         BerVarray *bva = v2;
464         struct berval bv;
465         char *ptr;
466
467         slap_index2bvlen( ai->ai_indexmask, &bv );
468         if ( bv.bv_len ) {
469                 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
470                 ptr = ch_malloc( bv.bv_len+1 );
471                 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
472                 *bv.bv_val++ = ' ';
473                 slap_index2bv( ai->ai_indexmask, &bv );
474                 bv.bv_val = ptr;
475                 ber_bvarray_add( bva, &bv );
476         }
477         return 0;
478 }
479
480 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
481 static AttrInfo aidef = { &addef };
482
483 void
484 mdb_attr_index_unparse( struct mdb_info *mdb, BerVarray *bva )
485 {
486         int i;
487
488         if ( mdb->mi_defaultmask ) {
489                 aidef.ai_indexmask = mdb->mi_defaultmask;
490                 mdb_attr_index_unparser( &aidef, bva );
491         }
492         for ( i=0; i<mdb->mi_nattrs; i++ )
493                 mdb_attr_index_unparser( mdb->mi_attrs[i], bva );
494 }
495
496 void
497 mdb_attr_info_free( AttrInfo *ai )
498 {
499 #ifdef LDAP_COMP_MATCH
500         free( ai->ai_cr );
501 #endif
502         free( ai );
503 }
504
505 void
506 mdb_attr_index_destroy( struct mdb_info *mdb )
507 {
508         int i;
509
510         for ( i=0; i<mdb->mi_nattrs; i++ ) 
511                 mdb_attr_info_free( mdb->mi_attrs[i] );
512
513         free( mdb->mi_attrs );
514 }
515
516 void mdb_attr_index_free( struct mdb_info *mdb, AttributeDescription *ad )
517 {
518         int i;
519
520         i = mdb_attr_slot( mdb, ad, NULL );
521         if ( i >= 0 ) {
522                 mdb_attr_info_free( mdb->mi_attrs[i] );
523                 mdb->mi_nattrs--;
524                 for (; i<mdb->mi_nattrs; i++)
525                         mdb->mi_attrs[i] = mdb->mi_attrs[i+1];
526         }
527 }
528
529 void mdb_attr_flush( struct mdb_info *mdb )
530 {
531         int i;
532
533         for ( i=0; i<mdb->mi_nattrs; i++ ) {
534                 if ( mdb->mi_attrs[i]->ai_indexmask & MDB_INDEX_DELETING ) {
535                         int j;
536                         mdb_attr_info_free( mdb->mi_attrs[i] );
537                         mdb->mi_nattrs--;
538                         for (j=i; j<mdb->mi_nattrs; j++)
539                                 mdb->mi_attrs[j] = mdb->mi_attrs[j+1];
540                         i--;
541                 }
542         }
543 }
544
545 int mdb_ad_read( struct mdb_info *mdb, MDB_txn *txn )
546 {
547         int i, rc;
548         MDB_cursor *mc;
549         MDB_val key, data;
550         struct berval bdata;
551         const char *text;
552         AttributeDescription *ad;
553
554         rc = mdb_cursor_open( txn, mdb->mi_ad2id, &mc );
555         if ( rc ) {
556                 Debug( LDAP_DEBUG_ANY,
557                         "mdb_ad_read: cursor_open failed %s(%d)\n",
558                         mdb_strerror(rc), rc, 0);
559                 return rc;
560         }
561
562         /* our array is 1-based, an index of 0 means no data */
563         i = mdb->mi_numads+1;
564         key.mv_size = sizeof(int);
565         key.mv_data = &i;
566
567         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
568
569         while ( rc == MDB_SUCCESS ) {
570                 bdata.bv_len = data.mv_size;
571                 bdata.bv_val = data.mv_data;
572                 ad = NULL;
573                 rc = slap_bv2ad( &bdata, &ad, &text );
574                 if ( rc ) {
575                         rc = slap_bv2undef_ad( &bdata, &mdb->mi_ads[i], &text, 0 );
576                 } else {
577                         if ( ad->ad_index >= MDB_MAXADS ) {
578                                 Debug( LDAP_DEBUG_ANY,
579                                         "mdb_adb_read: too many AttributeDescriptions in use\n",
580                                         0, 0, 0 );
581                                 return LDAP_OTHER;
582                         }
583                         mdb->mi_adxs[ad->ad_index] = i;
584                         mdb->mi_ads[i] = ad;
585                 }
586                 i++;
587                 rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT );
588         }
589         mdb->mi_numads = i-1;
590
591 done:
592         if ( rc == MDB_NOTFOUND )
593                 rc = 0;
594
595         mdb_cursor_close( mc );
596
597         return rc;
598 }
599
600 int mdb_ad_get( struct mdb_info *mdb, MDB_txn *txn, AttributeDescription *ad )
601 {
602         int i, rc;
603         MDB_val key, val;
604
605         rc = mdb_ad_read( mdb, txn );
606         if (rc)
607                 return rc;
608
609         if ( mdb->mi_adxs[ad->ad_index] )
610                 return 0;
611
612         i = mdb->mi_numads+1;
613         key.mv_size = sizeof(int);
614         key.mv_data = &i;
615         val.mv_size = ad->ad_cname.bv_len;
616         val.mv_data = ad->ad_cname.bv_val;
617
618         rc = mdb_put( txn, mdb->mi_ad2id, &key, &val, 0 );
619         if ( rc == MDB_SUCCESS ) {
620                 mdb->mi_adxs[ad->ad_index] = i;
621                 mdb->mi_ads[i] = ad;
622                 mdb->mi_numads++;
623         } else {
624                 Debug( LDAP_DEBUG_ANY,
625                         "mdb_ad_get: mdb_put failed %s(%d)\n",
626                         mdb_strerror(rc), rc, 0);
627         }
628
629         return rc;
630 }