]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/attr.c
Happy new year (belated)
[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-2014 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_root = NULL;
381                 a->ai_desc = ad;
382                 a->ai_dbi = 0;
383
384                 if ( mdb->mi_flags & MDB_IS_OPEN ) {
385                         a->ai_indexmask = 0;
386                         a->ai_newmask = mask;
387                 } else {
388                         a->ai_indexmask = mask;
389                         a->ai_newmask = 0;
390                 }
391
392 #ifdef LDAP_COMP_MATCH
393                 if ( cr ) {
394                         a_cr = mdb_attr_mask( mdb, ad );
395                         if ( a_cr ) {
396                                 /*
397                                  * AttrInfo is already in AVL
398                                  * just add the extracted component reference
399                                  * in the AttrInfo
400                                  */
401                                 rc = insert_component_reference( cr, &a_cr->ai_cr );
402                                 if ( rc != LDAP_SUCCESS) {
403                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
404                                         rc = LDAP_PARAM_ERROR;
405                                         goto done;
406                                 }
407                                 continue;
408                         } else {
409                                 rc = insert_component_reference( cr, &a->ai_cr );
410                                 if ( rc != LDAP_SUCCESS) {
411                                         fprintf( stderr, " error during inserting component reference in %s ", attrs[i]);
412                                         rc = LDAP_PARAM_ERROR;
413                                         goto done;
414                                 }
415                         }
416                 }
417 #endif
418                 rc = ainfo_insert( mdb, a );
419                 if( rc ) {
420                         if ( mdb->mi_flags & MDB_IS_OPEN ) {
421                                 AttrInfo *b = mdb_attr_mask( mdb, ad );
422                                 /* If there is already an index defined for this attribute
423                                  * it must be replaced. Otherwise we end up with multiple 
424                                  * olcIndex values for the same attribute */
425                                 if ( b->ai_indexmask & MDB_INDEX_DELETING ) {
426                                         /* If we were editing this attr, reset it */
427                                         b->ai_indexmask &= ~MDB_INDEX_DELETING;
428                                         /* If this is leftover from a previous add, commit it */
429                                         if ( b->ai_newmask )
430                                                 b->ai_indexmask = b->ai_newmask;
431                                         b->ai_newmask = a->ai_newmask;
432                                         ch_free( a );
433                                         rc = 0;
434                                         continue;
435                                 }
436                         }
437                         if (c_reply) {
438                                 snprintf(c_reply->msg, sizeof(c_reply->msg),
439                                         "duplicate index definition for attr \"%s\"",
440                                         attrs[i] );
441                                 fprintf( stderr, "%s: line %d: %s\n",
442                                         fname, lineno, c_reply->msg );
443                         }
444
445                         rc = LDAP_PARAM_ERROR;
446                         goto done;
447                 }
448         }
449
450 done:
451         ldap_charray_free( attrs );
452         if ( indexes != NULL ) ldap_charray_free( indexes );
453
454         return rc;
455 }
456
457 static int
458 mdb_attr_index_unparser( void *v1, void *v2 )
459 {
460         AttrInfo *ai = v1;
461         BerVarray *bva = v2;
462         struct berval bv;
463         char *ptr;
464
465         slap_index2bvlen( ai->ai_indexmask, &bv );
466         if ( bv.bv_len ) {
467                 bv.bv_len += ai->ai_desc->ad_cname.bv_len + 1;
468                 ptr = ch_malloc( bv.bv_len+1 );
469                 bv.bv_val = lutil_strcopy( ptr, ai->ai_desc->ad_cname.bv_val );
470                 *bv.bv_val++ = ' ';
471                 slap_index2bv( ai->ai_indexmask, &bv );
472                 bv.bv_val = ptr;
473                 ber_bvarray_add( bva, &bv );
474         }
475         return 0;
476 }
477
478 static AttributeDescription addef = { NULL, NULL, BER_BVC("default") };
479 static AttrInfo aidef = { &addef };
480
481 void
482 mdb_attr_index_unparse( struct mdb_info *mdb, BerVarray *bva )
483 {
484         int i;
485
486         if ( mdb->mi_defaultmask ) {
487                 aidef.ai_indexmask = mdb->mi_defaultmask;
488                 mdb_attr_index_unparser( &aidef, bva );
489         }
490         for ( i=0; i<mdb->mi_nattrs; i++ )
491                 mdb_attr_index_unparser( mdb->mi_attrs[i], bva );
492 }
493
494 void
495 mdb_attr_info_free( AttrInfo *ai )
496 {
497 #ifdef LDAP_COMP_MATCH
498         free( ai->ai_cr );
499 #endif
500         free( ai );
501 }
502
503 void
504 mdb_attr_index_destroy( struct mdb_info *mdb )
505 {
506         int i;
507
508         for ( i=0; i<mdb->mi_nattrs; i++ ) 
509                 mdb_attr_info_free( mdb->mi_attrs[i] );
510
511         free( mdb->mi_attrs );
512 }
513
514 void mdb_attr_index_free( struct mdb_info *mdb, AttributeDescription *ad )
515 {
516         int i;
517
518         i = mdb_attr_slot( mdb, ad, NULL );
519         if ( i >= 0 ) {
520                 mdb_attr_info_free( mdb->mi_attrs[i] );
521                 mdb->mi_nattrs--;
522                 for (; i<mdb->mi_nattrs; i++)
523                         mdb->mi_attrs[i] = mdb->mi_attrs[i+1];
524         }
525 }
526
527 void mdb_attr_flush( struct mdb_info *mdb )
528 {
529         int i;
530
531         for ( i=0; i<mdb->mi_nattrs; i++ ) {
532                 if ( mdb->mi_attrs[i]->ai_indexmask & MDB_INDEX_DELETING ) {
533                         int j;
534                         mdb_attr_info_free( mdb->mi_attrs[i] );
535                         mdb->mi_nattrs--;
536                         for (j=i; j<mdb->mi_nattrs; j++)
537                                 mdb->mi_attrs[j] = mdb->mi_attrs[j+1];
538                         i--;
539                 }
540         }
541 }
542
543 int mdb_ad_read( struct mdb_info *mdb, MDB_txn *txn )
544 {
545         int i, rc;
546         MDB_cursor *mc;
547         MDB_val key, data;
548         struct berval bdata;
549         const char *text;
550         AttributeDescription *ad;
551
552         rc = mdb_cursor_open( txn, mdb->mi_ad2id, &mc );
553         if ( rc ) {
554                 Debug( LDAP_DEBUG_ANY,
555                         "mdb_ad_read: cursor_open failed %s(%d)\n",
556                         mdb_strerror(rc), rc, 0);
557                 return rc;
558         }
559
560         /* our array is 1-based, an index of 0 means no data */
561         i = mdb->mi_numads+1;
562         key.mv_size = sizeof(int);
563         key.mv_data = &i;
564
565         rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
566
567         while ( rc == MDB_SUCCESS ) {
568                 bdata.bv_len = data.mv_size;
569                 bdata.bv_val = data.mv_data;
570                 ad = NULL;
571                 rc = slap_bv2ad( &bdata, &ad, &text );
572                 if ( rc ) {
573                         rc = slap_bv2undef_ad( &bdata, &mdb->mi_ads[i], &text, 0 );
574                 } else {
575                         if ( ad->ad_index >= MDB_MAXADS ) {
576                                 Debug( LDAP_DEBUG_ANY,
577                                         "mdb_adb_read: too many AttributeDescriptions in use\n",
578                                         0, 0, 0 );
579                                 return LDAP_OTHER;
580                         }
581                         mdb->mi_adxs[ad->ad_index] = i;
582                         mdb->mi_ads[i] = ad;
583                 }
584                 i++;
585                 rc = mdb_cursor_get( mc, &key, &data, MDB_NEXT );
586         }
587         mdb->mi_numads = i-1;
588
589 done:
590         if ( rc == MDB_NOTFOUND )
591                 rc = 0;
592
593         mdb_cursor_close( mc );
594
595         return rc;
596 }
597
598 int mdb_ad_get( struct mdb_info *mdb, MDB_txn *txn, AttributeDescription *ad )
599 {
600         int i, rc;
601         MDB_val key, val;
602
603         rc = mdb_ad_read( mdb, txn );
604         if (rc)
605                 return rc;
606
607         if ( mdb->mi_adxs[ad->ad_index] )
608                 return 0;
609
610         i = mdb->mi_numads+1;
611         key.mv_size = sizeof(int);
612         key.mv_data = &i;
613         val.mv_size = ad->ad_cname.bv_len;
614         val.mv_data = ad->ad_cname.bv_val;
615
616         rc = mdb_put( txn, mdb->mi_ad2id, &key, &val, 0 );
617         if ( rc == MDB_SUCCESS ) {
618                 mdb->mi_adxs[ad->ad_index] = i;
619                 mdb->mi_ads[i] = ad;
620                 mdb->mi_numads++;
621         } else {
622                 Debug( LDAP_DEBUG_ANY,
623                         "mdb_ad_get: mdb_put failed %s(%d)\n",
624                         mdb_strerror(rc), rc, 0);
625         }
626
627         return rc;
628 }