]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/idl.c
Initial implementation of Kerberos password verification for
[openldap] / servers / slapd / back-ldbm / idl.c
1 /* idl.c - ldap id list handling routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18 static ID_BLOCK* idl_dup( ID_BLOCK *idl );
19
20 /* Allocate an ID_BLOCK with room for nids ids */
21 ID_BLOCK *
22 idl_alloc( unsigned int nids )
23 {
24         ID_BLOCK        *new;
25
26         /* nmax + nids + space for the ids */
27         new = (ID_BLOCK *) ch_calloc( (ID_BLOCK_IDS_OFFSET + nids), sizeof(ID) );
28         ID_BLOCK_NMAX(new) = nids;
29         ID_BLOCK_NIDS(new) = 0;
30
31         return( new );
32 }
33
34
35 /* Allocate an empty ALLIDS ID_BLOCK */
36 ID_BLOCK        *
37 idl_allids( Backend *be )
38 {
39         ID_BLOCK        *idl;
40
41         idl = idl_alloc( 0 );
42         ID_BLOCK_NMAX(idl) = ID_BLOCK_ALLIDS_VALUE;
43         ID_BLOCK_NIDS(idl) = next_id_get( be );
44
45         return( idl );
46 }
47
48 /* Free an ID_BLOCK */
49 void
50 idl_free( ID_BLOCK *idl )
51 {
52         if ( idl == NULL ) {
53                 Debug( LDAP_DEBUG_TRACE,
54                         "idl_free: called with NULL pointer\n",
55                         0, 0, 0 );
56                 return;
57         }
58
59         free( (char *) idl );
60 }
61
62
63 /* Fetch an single ID_BLOCK from the cache */
64 static ID_BLOCK *
65 idl_fetch_one(
66     Backend             *be,
67     DBCache     *db,
68     Datum               key
69 )
70 {
71         Datum   data;
72         ID_BLOCK        *idl;
73
74         /* Debug( LDAP_DEBUG_TRACE, "=> idl_fetch_one\n", 0, 0, 0 ); */
75
76         data = ldbm_cache_fetch( db, key );
77
78         if( data.dptr == NULL ) {
79                 return NULL;
80         }
81
82         idl = idl_dup((ID_BLOCK *) data.dptr);
83
84         ldbm_datum_free( db->dbc_db, data );
85
86         return idl;
87 }
88
89
90 /* Fetch a set of ID_BLOCKs from the cache
91  *      if not INDIRECT
92  *              if block return is an ALLIDS block,
93  *                      return an new ALLIDS block
94  *              otherwise
95  *                      return block
96  *      construct super block from all blocks referenced by INDIRECT block
97  *      return super block
98  */
99 ID_BLOCK *
100 idl_fetch(
101     Backend             *be,
102     DBCache     *db,
103     Datum               key
104 )
105 {
106         Datum   data;
107         ID_BLOCK        *idl;
108         ID_BLOCK        **tmp;
109         char    *kstr;
110         int     i, nids;
111
112         idl = idl_fetch_one( be, db, key );
113
114         if ( idl == NULL ) {
115                 return NULL;
116         }
117
118         if ( ID_BLOCK_ALLIDS(idl) ) {
119                 /* all ids block */
120                 /* make sure we have the current value of highest id */
121                 idl_free( idl );
122                 idl = idl_allids( be );
123
124                 return( idl );
125         }
126
127         if ( ! ID_BLOCK_INDIRECT( idl ) ) {
128                 /* regular block */
129                 return( idl );
130         }
131
132         /*
133          * this is an indirect block which points to other blocks.
134          * we need to read in all the blocks it points to and construct
135          * a big id list containing all the ids, which we will return.
136          */
137
138         /* count the number of blocks & allocate space for pointers to them */
139         for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ )
140                 ;       /* NULL */
141         tmp = (ID_BLOCK **) ch_malloc( (i + 1) * sizeof(ID_BLOCK *) );
142
143         /* read in all the blocks */
144         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
145         nids = 0;
146         for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
147                 ldbm_datum_init( data );
148
149                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
150                         ID_BLOCK_ID(idl, i), key.dptr );
151
152                 data.dptr = kstr;
153                 data.dsize = strlen( kstr ) + 1;
154
155                 if ( (tmp[i] = idl_fetch_one( be, db, data )) == NULL ) {
156                         Debug( LDAP_DEBUG_ANY,
157                             "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
158                         continue;
159                 }
160
161                 nids += ID_BLOCK_NIDS(tmp[i]);
162         }
163         tmp[i] = NULL;
164         free( kstr );
165         idl_free( idl );
166
167         /* allocate space for the big block */
168         idl = idl_alloc( nids );
169         ID_BLOCK_NIDS(idl) = nids;
170         nids = 0;
171
172         /* copy in all the ids from the component blocks */
173         for ( i = 0; tmp[i] != NULL; i++ ) {
174                 if ( tmp[i] == NULL ) {
175                         continue;
176                 }
177
178                 SAFEMEMCPY(
179                         (char *) &ID_BLOCK_ID(idl, nids),
180                         (char *) &ID_BLOCK_ID(tmp[i], 0),
181                         ID_BLOCK_NIDS(tmp[i]) * sizeof(ID) );
182                 nids += ID_BLOCK_NIDS(tmp[i]);
183
184                 idl_free( tmp[i] );
185         }
186         free( (char *) tmp );
187
188         Debug( LDAP_DEBUG_TRACE, "<= idl_fetch %ld ids (%ld max)\n",
189                ID_BLOCK_NIDS(idl), ID_BLOCK_NMAX(idl), 0 );
190         return( idl );
191 }
192
193
194 /* store a single block */
195 static int
196 idl_store(
197     Backend             *be,
198     DBCache     *db,
199     Datum               key, 
200     ID_BLOCK            *idl
201 )
202 {
203         int     rc, flags;
204         Datum   data;
205         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
206
207         ldbm_datum_init( data );
208
209         /* Debug( LDAP_DEBUG_TRACE, "=> idl_store\n", 0, 0, 0 ); */
210
211         data.dptr = (char *) idl;
212         data.dsize = (ID_BLOCK_IDS_OFFSET + ID_BLOCK_NMAX(idl)) * sizeof(ID);
213         
214 #ifdef LDBM_DEBUG
215         Statslog( LDAP_DEBUG_STATS, "<= idl_store(): rc=%d\n",
216                 rc, 0, 0, 0, 0 );
217 #endif
218
219         flags = LDBM_REPLACE;
220         rc = ldbm_cache_store( db, key, data, flags );
221
222         /* Debug( LDAP_DEBUG_TRACE, "<= idl_store %d\n", rc, 0, 0 ); */
223         return( rc );
224 }
225
226 /* split the block at id 
227  *      locate ID greater than or equal to id.
228  */
229 static void
230 idl_split_block(
231     ID_BLOCK    *b,
232     ID          id,
233     ID_BLOCK    **right,
234     ID_BLOCK    **left
235 )
236 {
237         unsigned int    nr, nl;
238
239         /* find where to split the block *//* XXX linear search XXX */
240         for ( nr = 0; nr < ID_BLOCK_NIDS(b) && id > ID_BLOCK_ID(b, nr); nr++ )
241                 ;       /* NULL */
242
243         nl = ID_BLOCK_NIDS(b) - nr;
244
245         *right = idl_alloc( nr == 0 ? 1 : nr );
246         *left = idl_alloc( nl + (nr == 0 ? 0 : 1));
247
248         /*
249          * everything before the id being inserted in the first block
250          * unless there is nothing, in which case the id being inserted
251          * goes there.
252          */
253         if ( nr == 0 ) {
254                 ID_BLOCK_NIDS(*right) = 1;
255                 ID_BLOCK_ID(*right, 0) = id;
256         } else {
257                 SAFEMEMCPY(
258                         (char *) &ID_BLOCK_ID(*right, 0),
259                         (char *) &ID_BLOCK_ID(b, 0),
260                         nr * sizeof(ID) );
261                 ID_BLOCK_NIDS(*right) = nr;
262                 ID_BLOCK_ID(*left, 0) = id;
263         }
264
265         /* the id being inserted & everything after in the second block */
266         SAFEMEMCPY(
267                 (char *) &ID_BLOCK_ID(*left, (nr == 0 ? 0 : 1)),
268             (char *) &ID_BLOCK_ID(b, nr),
269                 nl * sizeof(ID) );
270         ID_BLOCK_NIDS(*left) = nl + (nr == 0 ? 0 : 1);
271 }
272
273
274 /*
275  * idl_change_first - called when an indirect block's first key has
276  * changed, meaning it needs to be stored under a new key, and the
277  * header block pointing to it needs updating.
278  */
279 static int
280 idl_change_first(
281     Backend             *be,
282     DBCache     *db,
283     Datum               hkey,           /* header block key     */
284     ID_BLOCK            *h,             /* header block         */
285     int                 pos,            /* pos in h to update   */
286     Datum               bkey,           /* data block key       */
287     ID_BLOCK            *b              /* data block           */
288 )
289 {
290         int     rc;
291
292         /* Debug( LDAP_DEBUG_TRACE, "=> idl_change_first\n", 0, 0, 0 ); */
293
294         /* delete old key block */
295         if ( (rc = ldbm_cache_delete( db, bkey )) != 0 ) {
296                 Debug( LDAP_DEBUG_ANY,
297                     "ldbm_delete of (%s) returns %d\n", bkey.dptr, rc,
298                     0 );
299                 return( rc );
300         }
301
302         /* write block with new key */
303         sprintf( bkey.dptr, "%c%ld%s", CONT_PREFIX,
304                 ID_BLOCK_ID(b, 0), hkey.dptr );
305
306         bkey.dsize = strlen( bkey.dptr ) + 1;
307         if ( (rc = idl_store( be, db, bkey, b )) != 0 ) {
308                 Debug( LDAP_DEBUG_ANY,
309                     "idl_store of (%s) returns %d\n", bkey.dptr, rc, 0 );
310                 return( rc );
311         }
312
313         /* update + write indirect header block */
314         ID_BLOCK_ID(h, pos) = ID_BLOCK_ID(b, 0);
315         if ( (rc = idl_store( be, db, hkey, h )) != 0 ) {
316                 Debug( LDAP_DEBUG_ANY,
317                     "idl_store of (%s) returns %d\n", hkey.dptr, rc, 0 );
318                 return( rc );
319         }
320
321         return( 0 );
322 }
323
324
325 int
326 idl_insert_key(
327     Backend             *be,
328     DBCache     *db,
329     Datum               key,
330     ID                  id
331 )
332 {
333         int     i, j, first, rc;
334         ID_BLOCK        *idl, *tmp, *tmp2, *tmp3;
335         char    *kstr;
336         Datum   k2;
337
338         ldbm_datum_init( k2 );
339
340         if ( (idl = idl_fetch_one( be, db, key )) == NULL ) {
341 #ifdef LDBM_DEBUG
342                 Statslog( LDAP_DEBUG_STATS, "=> idl_insert_key(): no key yet\n",
343                         0, 0, 0, 0, 0 );
344 #endif
345
346                 idl = idl_alloc( 1 );
347                 ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)++) = id;
348                 rc = idl_store( be, db, key, idl );
349
350                 idl_free( idl );
351                 return( rc );
352         }
353
354         if ( ID_BLOCK_ALLIDS( idl ) ) {
355                 /* ALLIDS */
356                 idl_free( idl );
357                 return 0;
358         }
359
360         if ( ! ID_BLOCK_INDIRECT( idl ) ) {
361                 /* regular block */
362                 switch ( idl_insert( &idl, id, db->dbc_maxids ) ) {
363                 case 0:         /* id inserted - store the updated block */
364                 case 1:
365                         rc = idl_store( be, db, key, idl );
366                         break;
367
368                 case 2:         /* id already there - nothing to do */
369                         rc = 0;
370                         break;
371
372                 case 3:         /* id not inserted - block must be split */
373                         /* check threshold for marking this an all-id block */
374                         if ( db->dbc_maxindirect < 2 ) {
375                                 idl_free( idl );
376                                 idl = idl_allids( be );
377                                 rc = idl_store( be, db, key, idl );
378                                 break;
379                         }
380
381                         idl_split_block( idl, id, &tmp, &tmp2 );
382                         idl_free( idl );
383
384                         /* create the header indirect block */
385                         idl = idl_alloc( 3 );
386                         ID_BLOCK_NMAX(idl) = 3;
387                         ID_BLOCK_NIDS(idl) = ID_BLOCK_INDIRECT_VALUE;
388                         ID_BLOCK_ID(idl, 0) = ID_BLOCK_ID(tmp, 0);
389                         ID_BLOCK_ID(idl, 1) = ID_BLOCK_ID(tmp2, 0);
390                         ID_BLOCK_ID(idl, 2) = NOID;
391
392                         /* store it */
393                         rc = idl_store( be, db, key, idl );
394
395                         /* store the first id block */
396                         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
397                         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
398                                 ID_BLOCK_ID(tmp, 0), key.dptr );
399
400                         k2.dptr = kstr;
401                         k2.dsize = strlen( kstr ) + 1;
402                         rc = idl_store( be, db, k2, tmp );
403
404                         /* store the second id block */
405                         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
406                                 ID_BLOCK_ID(tmp2, 0), key.dptr );
407                         k2.dptr = kstr;
408                         k2.dsize = strlen( kstr ) + 1;
409                         rc = idl_store( be, db, k2, tmp2 );
410
411                         free( kstr );
412                         idl_free( tmp );
413                         idl_free( tmp2 );
414                         break;
415                 }
416
417                 idl_free( idl );
418                 return( rc );
419         }
420
421         /*
422          * this is an indirect block which points to other blocks.
423          * we need to read in the block into which the id should be
424          * inserted, then insert the id and store the block.  we might
425          * have to split the block if it is full, which means we also
426          * need to write a new "header" block.
427          */
428
429         /* select the block to try inserting into *//* XXX linear search XXX */
430         for ( i = 0; !ID_BLOCK_NOID(idl, i) && id > ID_BLOCK_ID(idl, i); i++ )
431                 ;       /* NULL */
432         if ( i != 0 ) {
433                 i--;
434                 first = 0;
435         } else {
436                 first = 1;
437         }
438
439         /* get the block */
440         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
441         sprintf( kstr, "%c%ld%s", CONT_PREFIX,
442                 ID_BLOCK_ID(idl, i), key.dptr );
443         k2.dptr = kstr;
444         k2.dsize = strlen( kstr ) + 1;
445         if ( (tmp = idl_fetch_one( be, db, k2 )) == NULL ) {
446                 Debug( LDAP_DEBUG_ANY, "nonexistent continuation block (%s)\n",
447                     k2.dptr, 0, 0 );
448                 free( kstr );
449                 idl_free( idl );
450                 return( -1 );
451         }
452
453         /* insert the id */
454         switch ( idl_insert( &tmp, id, db->dbc_maxids ) ) {
455         case 0:         /* id inserted ok */
456                 if ( (rc = idl_store( be, db, k2, tmp )) != 0 ) {
457                         Debug( LDAP_DEBUG_ANY,
458                             "idl_store of (%s) returns %d\n", k2.dptr, rc, 0 );
459                 }
460                 break;
461
462         case 1:         /* id inserted - first id in block has changed */
463                 /*
464                  * key for this block has changed, so we have to
465                  * write the block under the new key, delete the
466                  * old key block + update and write the indirect
467                  * header block.
468                  */
469
470                 rc = idl_change_first( be, db, key, idl, i, k2, tmp );
471                 break;
472
473         case 2:         /* id not inserted - already there, do nothing */
474                 rc = 0;
475                 break;
476
477         case 3:         /* id not inserted - block is full */
478                 /*
479                  * first, see if it will fit in the next block,
480                  * without splitting, unless we're trying to insert
481                  * into the beginning of the first block.
482                  */
483
484                 /* is there a next block? */
485                 if ( !first && !ID_BLOCK_NOID(idl, i + 1) ) {
486                         /* read it in */
487                         k2.dptr = (char *) ch_malloc( key.dsize + CONT_SIZE );
488                         sprintf( k2.dptr, "%c%ld%s", CONT_PREFIX,
489                                 ID_BLOCK_ID(idl, i + 1), key.dptr );
490                         k2.dsize = strlen( k2.dptr ) + 1;
491                         if ( (tmp2 = idl_fetch_one( be, db, k2 )) == NULL ) {
492                                 Debug( LDAP_DEBUG_ANY,
493                                     "idl_fetch_one (%s) returns NULL\n",
494                                     k2.dptr, 0, 0 );
495                                 /* split the original block */
496                                 free( k2.dptr );
497                                 goto split;
498                         }
499
500                         /* If the new id is less than the last id in the
501                          * current block, it must not be put into the next
502                          * block. Push the last id of the current block
503                          * into the next block instead.
504                          */
505                         if (id < ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1)) {
506                             ID id2 = ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp) - 1);
507                             Datum k3;
508
509                             ldbm_datum_init( k3 );
510
511                             --ID_BLOCK_NIDS(tmp);
512                             /* This must succeed since we just popped one
513                              * ID off the end of it.
514                              */
515                             rc = idl_insert( &tmp, id, db->dbc_maxids );
516                             assert( rc == 0 );
517                             k3.dptr = kstr;
518                             k3.dsize = strlen( kstr ) + 1;
519                             if ( (rc = idl_store( be, db, k3, tmp )) != 0 ) {
520                                 Debug( LDAP_DEBUG_ANY,
521                             "idl_store of (%s) returns %d\n", k3.dptr, rc, 0 );
522                             }
523                             free( kstr );
524                             kstr = k2.dptr;
525
526                             id = id2;
527                             /* This new id will necessarily be inserted
528                              * as the first id of the next block by the
529                              * following switch() statement.
530                              */
531                         }
532
533                         switch ( (rc = idl_insert( &tmp2, id,
534                             db->dbc_maxids )) ) {
535                         case 1:         /* id inserted first in block */
536                                 rc = idl_change_first( be, db, key, idl,
537                                     i + 1, k2, tmp2 );
538                                 /* FALL */
539
540                         case 2:         /* id already there - how? */
541                         case 0:         /* id inserted: this can never be
542                                          * the result of idl_insert, because
543                                          * we guaranteed that idl_change_first
544                                          * will always be called.
545                                          */
546                                 if ( rc == 2 ) {
547                                         Debug( LDAP_DEBUG_ANY,
548                                             "id %ld already in next block\n",
549                                             id, 0, 0 );
550                                 }
551                                 free( kstr );
552                                 idl_free( tmp );
553                                 idl_free( tmp2 );
554                                 idl_free( idl );
555                                 return( 0 );
556
557                         case 3:         /* split the original block */
558                                 break;
559                         }
560
561                         idl_free( tmp2 );
562                 }
563
564 split:
565                 /*
566                  * must split the block, write both new blocks + update
567                  * and write the indirect header block.
568                  */
569
570                 rc = 0; /* optimistic */
571
572
573                 /* count how many indirect blocks *//* XXX linear count XXX */
574                 for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ )
575                         ;       /* NULL */
576
577                 /* check it against all-id thresholed */
578                 if ( j + 1 > db->dbc_maxindirect ) {
579                         /*
580                          * we've passed the all-id threshold, meaning
581                          * that this set of blocks should be replaced
582                          * by a single "all-id" block.  our job: delete
583                          * all the indirect blocks, and replace the header
584                          * block by an all-id block.
585                          */
586
587                         /* delete all indirect blocks */
588                         for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) {
589                                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
590                                         ID_BLOCK_ID(idl, j), key.dptr );
591                                 k2.dptr = kstr;
592                                 k2.dsize = strlen( kstr ) + 1;
593
594                                 rc = ldbm_cache_delete( db, k2 );
595                         }
596
597                         /* store allid block in place of header block */
598                         idl_free( idl );
599                         idl = idl_allids( be );
600                         rc = idl_store( be, db, key, idl );
601
602                         free( kstr );
603                         idl_free( idl );
604                         idl_free( tmp );
605                         return( rc );
606                 }
607
608                 idl_split_block( tmp, id, &tmp2, &tmp3 );
609                 idl_free( tmp );
610
611                 /* create a new updated indirect header block */
612                 tmp = idl_alloc( ID_BLOCK_NMAX(idl) + 1 );
613                 ID_BLOCK_NIDS(tmp) = ID_BLOCK_INDIRECT_VALUE;
614                 /* everything up to the split block */
615                 SAFEMEMCPY(
616                         (char *) &ID_BLOCK_ID(tmp, 0),
617                         (char *) &ID_BLOCK_ID(idl, 0),
618                     i * sizeof(ID) );
619                 /* the two new blocks */
620                 ID_BLOCK_ID(tmp, i) = ID_BLOCK_ID(tmp2, 0);
621                 ID_BLOCK_ID(tmp, i + 1) = ID_BLOCK_ID(tmp3, 0);
622                 /* everything after the split block */
623                 SAFEMEMCPY(
624                         (char *) &ID_BLOCK_ID(tmp, i + 2),
625                         (char *) &ID_BLOCK_ID(idl, i + 1),
626                         (ID_BLOCK_NMAX(idl) - i - 1) * sizeof(ID) );
627
628                 /* store the header block */
629                 rc = idl_store( be, db, key, tmp );
630
631                 /* store the first id block */
632                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
633                         ID_BLOCK_ID(tmp2, 0), key.dptr );
634                 k2.dptr = kstr;
635                 k2.dsize = strlen( kstr ) + 1;
636                 rc = idl_store( be, db, k2, tmp2 );
637
638                 /* store the second id block */
639                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
640                         ID_BLOCK_ID(tmp3, 0), key.dptr );
641                 k2.dptr = kstr;
642                 k2.dsize = strlen( kstr ) + 1;
643                 rc = idl_store( be, db, k2, tmp3 );
644
645                 idl_free( tmp2 );
646                 idl_free( tmp3 );
647                 break;
648         }
649
650         free( kstr );
651         idl_free( tmp );
652         idl_free( idl );
653         return( rc );
654 }
655
656
657 /*
658  * idl_insert - insert an id into an id list.
659  *
660  *      returns
661  *              0       id inserted
662  *              1       id inserted, first id in block has changed
663  *              2       id not inserted, already there
664  *              3       id not inserted, block must be split
665  */
666 int
667 idl_insert( ID_BLOCK **idl, ID id, unsigned int maxids )
668 {
669         unsigned int    i;
670
671         if ( ID_BLOCK_ALLIDS( *idl ) ) {
672                 return( 2 );    /* already there */
673         }
674
675         /* is it already there? *//* XXX linear search XXX */
676         for ( i = 0; i < ID_BLOCK_NIDS(*idl) && id > ID_BLOCK_ID(*idl, i); i++ ) {
677                 ;       /* NULL */
678         }
679         if ( i < ID_BLOCK_NIDS(*idl) && ID_BLOCK_ID(*idl, i) == id ) {
680                 return( 2 );    /* already there */
681         }
682
683         /* do we need to make room for it? */
684         if ( ID_BLOCK_NIDS(*idl) == ID_BLOCK_NMAX(*idl) ) {
685                 /* make room or indicate block needs splitting */
686                 if ( ID_BLOCK_NMAX(*idl) >= maxids ) {
687                         return( 3 );    /* block needs splitting */
688                 }
689
690                 ID_BLOCK_NMAX(*idl) *= 2;
691                 if ( ID_BLOCK_NMAX(*idl) > maxids ) {
692                         ID_BLOCK_NMAX(*idl) = maxids;
693                 }
694                 *idl = (ID_BLOCK *) ch_realloc( (char *) *idl,
695                     (ID_BLOCK_NMAX(*idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
696         }
697
698         /* make a slot for the new id */
699         SAFEMEMCPY( &ID_BLOCK_ID(*idl, i+1), &ID_BLOCK_ID(*idl, i),
700                     (ID_BLOCK_NIDS(*idl) - i) * sizeof(ID) );
701
702         ID_BLOCK_ID(*idl, i) = id;
703         ID_BLOCK_NIDS(*idl)++;
704         (void) memset(
705                 (char *) &ID_BLOCK_ID((*idl), ID_BLOCK_NIDS(*idl)),
706                 '\0',
707             (ID_BLOCK_NMAX(*idl) - ID_BLOCK_NIDS(*idl)) * sizeof(ID) );
708
709         return( i == 0 ? 1 : 0 );       /* inserted - first id changed or not */
710 }
711
712
713 int
714 idl_delete_key (
715         Backend         *be,
716         DBCache  *db,
717         Datum           key,
718         ID              id
719 )
720 {
721         Datum  data;
722         ID_BLOCK *idl;
723         unsigned i;
724         int j, nids;
725         char    *kstr;
726
727         if ( (idl = idl_fetch_one( be, db, key ) ) == NULL )
728         {
729                 /* It wasn't found.  Hmm... */
730                 return -1;
731         }
732
733         if ( ID_BLOCK_ALLIDS( idl ) ) {
734                 idl_free( idl );
735                 return 0;
736         }
737
738         if ( ! ID_BLOCK_INDIRECT( idl ) ) {
739                 for ( i=0; i < ID_BLOCK_NIDS(idl); i++ ) {
740                         if ( ID_BLOCK_ID(idl, i) == id ) {
741                                 if( --ID_BLOCK_NIDS(idl) == 0 ) {
742                                         ldbm_cache_delete( db, key );
743
744                                 } else {
745                                         SAFEMEMCPY (
746                                                 &ID_BLOCK_ID(idl, i),
747                                                 &ID_BLOCK_ID(idl, i+1),
748                                                 (ID_BLOCK_NIDS(idl)-i) * sizeof(ID) );
749
750                                         ID_BLOCK_ID(idl, ID_BLOCK_NIDS(idl)) = NOID;
751
752                                         idl_store( be, db, key, idl );
753                                 }
754
755                                 idl_free( idl );
756                                 return 0;
757                         }
758                         /*  We didn't find the ID.  Hmmm... */
759                 }
760                 idl_free( idl );
761                 return -1;
762         }
763         
764         /* We have to go through an indirect block and find the ID
765            in the list of IDL's
766            */
767         for ( nids = 0; !ID_BLOCK_NOID(idl, nids); nids++ )
768                 ;       /* NULL */
769         kstr = (char *) ch_malloc( key.dsize + CONT_SIZE );
770
771         for ( j = 0; !ID_BLOCK_NOID(idl, j); j++ ) 
772         {
773                 ID_BLOCK *tmp;
774                 ldbm_datum_init( data );
775                 sprintf( kstr, "%c%ld%s", CONT_PREFIX,
776                         ID_BLOCK_ID(idl, j), key.dptr );
777                 data.dptr = kstr;
778                 data.dsize = strlen( kstr ) + 1;
779
780                 if ( (tmp = idl_fetch_one( be, db, data )) == NULL ) {
781                         Debug( LDAP_DEBUG_ANY,
782                             "idl_fetch of (%s) returns NULL\n", data.dptr, 0, 0 );
783                         continue;
784                 }
785                 /*
786                    Now try to find the ID in tmp
787                 */
788                 for ( i=0; i < ID_BLOCK_NIDS(tmp); i++ )
789                 {
790                         if ( ID_BLOCK_ID(tmp, i) == id )
791                         {
792                                 SAFEMEMCPY(
793                                         &ID_BLOCK_ID(tmp, i),
794                                         &ID_BLOCK_ID(tmp, i+1),
795                                         (ID_BLOCK_NIDS(tmp)-(i+1)) * sizeof(ID));
796                                 ID_BLOCK_ID(tmp, ID_BLOCK_NIDS(tmp)-1 ) = NOID;
797                                 ID_BLOCK_NIDS(tmp)--;
798
799                                 if ( ID_BLOCK_NIDS(tmp) ) {
800                                         idl_store ( be, db, data, tmp );
801
802                                 } else {
803                                         ldbm_cache_delete( db, data );
804                                         SAFEMEMCPY(
805                                                 &ID_BLOCK_ID(idl, j),
806                                                 &ID_BLOCK_ID(idl, j+1),
807                                                 (nids-(j+1)) * sizeof(ID));
808                                         ID_BLOCK_ID(idl, nids-1) = NOID;
809                                         nids--;
810                                         if ( ! nids )
811                                                 ldbm_cache_delete( db, key );
812                                         else
813                                                 idl_store( be, db, key, idl );
814                                 }
815                                 idl_free( tmp );
816                                 free( kstr );
817                                 idl_free( idl );
818                                 return 0;
819                         }
820                 }
821                 idl_free( tmp );
822         }
823         free( kstr );
824         idl_free( idl );
825         return -1;
826 }
827
828
829 /* return a duplicate of a single ID_BLOCK */
830 static ID_BLOCK *
831 idl_dup( ID_BLOCK *idl )
832 {
833         ID_BLOCK        *new;
834
835         if ( idl == NULL ) {
836                 return( NULL );
837         }
838
839         new = idl_alloc( ID_BLOCK_NMAX(idl) );
840
841         SAFEMEMCPY(
842                 (char *) new,
843                 (char *) idl,
844                 (ID_BLOCK_NMAX(idl) + ID_BLOCK_IDS_OFFSET) * sizeof(ID) );
845
846         return( new );
847 }
848
849
850 /* return the smaller ID_BLOCK */
851 static ID_BLOCK *
852 idl_min( ID_BLOCK *a, ID_BLOCK *b )
853 {
854         return( ID_BLOCK_NIDS(a) > ID_BLOCK_NIDS(b) ? b : a );
855 }
856
857
858 /*
859  * idl_intersection - return a intersection b
860  */
861 ID_BLOCK *
862 idl_intersection(
863     Backend     *be,
864     ID_BLOCK    *a,
865     ID_BLOCK    *b
866 )
867 {
868         unsigned int    ai, bi, ni;
869         ID_BLOCK                *n;
870
871         if ( a == NULL || b == NULL ) {
872                 return( NULL );
873         }
874         if ( ID_BLOCK_ALLIDS( a ) ) {
875                 return( idl_dup( b ) );
876         }
877         if ( ID_BLOCK_ALLIDS( b ) ) {
878                 return( idl_dup( a ) );
879         }
880
881         n = idl_dup( idl_min( a, b ) );
882
883         for ( ni = 0, ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
884                 for ( ;
885                         bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
886                         bi++ )
887                 {
888                         ;       /* NULL */
889                 }
890
891                 if ( bi == ID_BLOCK_NIDS(b) ) {
892                         break;
893                 }
894
895                 if ( ID_BLOCK_ID(b, bi) == ID_BLOCK_ID(a, ai) ) {
896                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
897                 }
898         }
899
900         if ( ni == 0 ) {
901                 idl_free( n );
902                 return( NULL );
903         }
904         ID_BLOCK_NIDS(n) = ni;
905
906         return( n );
907 }
908
909
910 /*
911  * idl_union - return a union b
912  */
913 ID_BLOCK *
914 idl_union(
915     Backend     *be,
916     ID_BLOCK    *a,
917     ID_BLOCK    *b
918 )
919 {
920         unsigned int    ai, bi, ni;
921         ID_BLOCK                *n;
922
923         if ( a == NULL ) {
924                 return( idl_dup( b ) );
925         }
926         if ( b == NULL ) {
927                 return( idl_dup( a ) );
928         }
929         if ( ID_BLOCK_ALLIDS( a ) || ID_BLOCK_ALLIDS( b ) ) {
930                 return( idl_allids( be ) );
931         }
932
933         if ( ID_BLOCK_NIDS(b) < ID_BLOCK_NIDS(a) ) {
934                 n = a;
935                 a = b;
936                 b = n;
937         }
938
939         n = idl_alloc( ID_BLOCK_NIDS(a) + ID_BLOCK_NIDS(b) );
940
941         for ( ni = 0, ai = 0, bi = 0;
942                 ai < ID_BLOCK_NIDS(a) && bi < ID_BLOCK_NIDS(b);
943                 )
944         {
945                 if ( ID_BLOCK_ID(a, ai) < ID_BLOCK_ID(b, bi) ) {
946                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai++);
947
948                 } else if ( ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai) ) {
949                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi++);
950
951                 } else {
952                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
953                         ai++, bi++;
954                 }
955         }
956
957         for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
958                 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
959         }
960         for ( ; bi < ID_BLOCK_NIDS(b); bi++ ) {
961                 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(b, bi);
962         }
963         ID_BLOCK_NIDS(n) = ni;
964
965         return( n );
966 }
967
968
969 /*
970  * idl_notin - return a intersection ~b (or a minus b)
971  */
972 ID_BLOCK *
973 idl_notin(
974     Backend     *be,
975     ID_BLOCK    *a,
976     ID_BLOCK    *b
977 )
978 {
979         unsigned int    ni, ai, bi;
980         ID_BLOCK                *n;
981
982         if ( a == NULL ) {
983                 return( NULL );
984         }
985         if ( b == NULL || ID_BLOCK_ALLIDS( b )) {
986                 return( idl_dup( a ) );
987         }
988
989         if ( ID_BLOCK_ALLIDS( a ) ) {
990                 n = idl_alloc( SLAPD_LDBM_MIN_MAXIDS );
991                 ni = 0;
992
993                 for ( ai = 1, bi = 0;
994                         ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n) && bi < ID_BLOCK_NMAX(b);
995                         ai++ )
996                 {
997                         if ( ID_BLOCK_ID(b, bi) == ai ) {
998                                 bi++;
999                         } else {
1000                                 ID_BLOCK_ID(n, ni++) = ai;
1001                         }
1002                 }
1003
1004                 for ( ; ai < ID_BLOCK_NIDS(a) && ni < ID_BLOCK_NMAX(n); ai++ ) {
1005                         ID_BLOCK_ID(n, ni++) = ai;
1006                 }
1007
1008                 if ( ni == ID_BLOCK_NMAX(n) ) {
1009                         idl_free( n );
1010                         return( idl_allids( be ) );
1011                 } else {
1012                         ID_BLOCK_NIDS(n) = ni;
1013                         return( n );
1014                 }
1015         }
1016
1017         n = idl_dup( a );
1018
1019         ni = 0;
1020         for ( ai = 0, bi = 0; ai < ID_BLOCK_NIDS(a); ai++ ) {
1021                 for ( ;
1022                         bi < ID_BLOCK_NIDS(b) && ID_BLOCK_ID(b, bi) < ID_BLOCK_ID(a, ai);
1023                     bi++ )
1024                 {
1025                         ;       /* NULL */
1026                 }
1027
1028                 if ( bi == ID_BLOCK_NIDS(b) ) {
1029                         break;
1030                 }
1031
1032                 if ( ID_BLOCK_ID(b, bi) != ID_BLOCK_ID(a, ai) ) {
1033                         ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1034                 }
1035         }
1036
1037         for ( ; ai < ID_BLOCK_NIDS(a); ai++ ) {
1038                 ID_BLOCK_ID(n, ni++) = ID_BLOCK_ID(a, ai);
1039         }
1040         ID_BLOCK_NIDS(n) = ni;
1041
1042         return( n );
1043 }
1044
1045 /*      return the first ID in the block
1046  *      if ALLIDS block
1047  *              NIDS > 1 return 1
1048  *              otherwise return NOID 
1049  *      otherwise return first ID
1050  *
1051  *      cursor is set to 1
1052  */         
1053 ID
1054 idl_firstid( ID_BLOCK *idl, ID *cursor )
1055 {
1056         *cursor = 1;
1057
1058         if ( idl == NULL || ID_BLOCK_NIDS(idl) == 0 ) {
1059                 return( NOID );
1060         }
1061
1062         if ( ID_BLOCK_ALLIDS( idl ) ) {
1063                 return( ID_BLOCK_NIDS(idl) > 1 ? 1 : NOID );
1064         }
1065
1066         return( ID_BLOCK_ID(idl, 0) );
1067 }
1068
1069 /*      return next ID
1070  *      if ALLIDS block, cursor is id.
1071  *              increment id
1072  *              if id < NIDS return id
1073  *              otherwise NOID.
1074  *      otherwise cursor is index into block
1075  *              if index < nids
1076  *                      return id at index then increment
1077  */ 
1078 ID
1079 idl_nextid( ID_BLOCK *idl, ID *cursor )
1080 {
1081         if ( ID_BLOCK_ALLIDS( idl ) ) {
1082                 if( ++(*cursor) < ID_BLOCK_NIDS(idl) ) {
1083                         return *cursor;
1084                 } else {
1085                         return NOID;
1086                 }
1087         }
1088
1089         if ( *cursor < ID_BLOCK_NIDS(idl) ) {
1090                 return( ID_BLOCK_ID(idl, (*cursor)++) );
1091         }
1092
1093         return( NOID );
1094 }