]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/centipede.c
Mongo patch: changes from -devel from 981105 snap to present
[openldap] / servers / slapd / tools / centipede.c
1 /* centipede.c - generate and install indexing information (view w/tabstop=4) */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/ctype.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14
15 #include <ldapconfig.h>
16 #include <ldbm.h>
17
18 #define DEFAULT_LDAPFILTER      "(objectclass=*)"
19
20 #define CENTROID_VALUE  1
21 #define CENTROID_WORD   2
22
23 #define CENTROID_RELATIVE       1
24 #define CENTROID_FULL           2
25
26 #define WORD_BREAKS     " -',.()!;:&$%*\"/\\+_<>=?[]|^~"
27
28 char    *centdir;
29 int             ldbmcachesize;
30 int             centroidvalues;
31 int             centroidtype;
32 int             doweights;
33 char    *ldaphost;
34 char    *srcldapbinddn;
35 char    *srcldappasswd;
36 char    *destldapbinddn;
37 char    *destldappasswd;
38 char    *ldapbase;
39 int             srcldapauthmethod;
40 int             destldapauthmethod;
41 int             verbose;
42 int             not;
43
44 static LDAP             *start_ldap_search();
45 static LDAP             *bind_to_destination_ldap();
46 static int              create_tmp_files();
47 static int              generate_new_centroids();
48 static LDAPMod  **diff_centroids();
49 static LDAPMod  **full_centroid();
50 static char             **charray_add_dup();
51
52 static void usage( char *name )
53 {
54         fprintf( stderr, "usage: %s [options] -s url -d url attributes\n", name );
55         fprintf( stderr, "where:\n" );
56         fprintf( stderr, "\t-s url\t\t[[ldap://][host[:port]]/]searchbasedn\n");
57         fprintf( stderr, "\t-d url\t\t[[ldap://][host[:port]]/]centroidentrydn\n");
58         fprintf( stderr, "options:\n" );
59         fprintf( stderr, "\t-v \t\tturn on verbose mode\n" );
60         fprintf( stderr, "\t-n \t\tgenerate, but do not install index info\n" );
61         fprintf( stderr, "\t-f filter\tentry selection filter\n" );
62         fprintf( stderr, "\t-F \t\tgenerate a full centroid\n" );
63         fprintf( stderr, "\t-R \t\tgenerate a relative centroid\n" );
64         fprintf( stderr, "\t-w \t\tgenerate a word-based centroid\n" );
65         fprintf( stderr, "\t-t directory\tcentroid directory\n" );
66         fprintf( stderr, "\t-b binddn\tsource bind dn\n" );
67         fprintf( stderr, "\t-p passwd\tsource bind passwd (for simple auth)\n" );
68         fprintf( stderr, "\t-m authmethod\tsource authmethod \"simple\" or \"kerberos\"\n" );
69         fprintf( stderr, "\t-B binddn\tdestination bind dn\n" );
70         fprintf( stderr, "\t-P passwd\tdestination bind passwd (for simple auth)\n" );
71         fprintf( stderr, "\t-M authmethod\tdestination authmethod \"simple\" or \"kerberos\"\n" );
72         fprintf( stderr, "\t-c size\t\tldbm cache size\n" );
73 }
74
75 main( int argc, char **argv )
76 {
77         char            *ldapfilter;
78         char            *ldapsrcurl, *ldapdesturl;
79         LDAP            *ld;
80         LDAPMod         **mods;
81         char            **attrs;
82         char            **tmpfile;
83         LDBM            *ldbm;
84         LDBM            oldbm;
85         char            buf[BUFSIZ];
86         int                     i, j, k, count;
87         char            *s;
88         extern int      optind;
89         extern char     *optarg;
90
91         ldapsrcurl = NULL;
92         ldapdesturl = NULL;
93         ldaphost = LDAPHOST;
94         ldapbase = DEFAULT_BASE;
95         srcldapauthmethod = LDAP_AUTH_SIMPLE;
96         destldapauthmethod = LDAP_AUTH_SIMPLE;
97         srcldapbinddn = NULL;
98         srcldappasswd = NULL;
99         destldapbinddn = NULL;
100         destldappasswd = NULL;
101         ldapfilter = DEFAULT_LDAPFILTER;
102         centroidvalues = CENTROID_VALUE;
103         centroidtype = CENTROID_RELATIVE;
104         centdir = NULL;
105         tmpfile = NULL;
106         ldbmcachesize = 0;
107
108         while ( (i = getopt( argc, argv, "s:d:c:b:B:f:FRWp:P:m:M:t:vwn" ))
109             != EOF ) {
110                 switch ( i ) {
111                 case 's':       /* source url [[ldap://][host[:port]]/]basedn */
112                         ldapsrcurl = strdup( optarg );
113                         break;
114
115                 case 'd':       /* destination url [[ldap://][host[:port]]/]entrydn */
116                         ldapdesturl = strdup( optarg );
117                         break;
118
119                 case 'f':       /* specify a filter */
120                         ldapfilter = strdup( optarg );
121                         break;
122
123                 case 'F':       /* generate full centroid */
124                         centroidtype = CENTROID_FULL;
125                         break;
126
127                 case 'R':       /* generate relative centroid */
128                         centroidtype = CENTROID_RELATIVE;
129                         break;
130
131                 case 'w':       /* generate word centroid */
132                         centroidvalues = CENTROID_WORD;
133                         break;
134
135                 case 'W':       /* generate weights */
136                         doweights = 1;
137                         break;
138
139                 case 't':       /* temp file directory */
140                         centdir = strdup( optarg );
141                         break;
142
143                 case 'b':       /* src bind dn */
144                         srcldapbinddn = strdup( optarg );
145                         break;
146
147                 case 'p':       /* src bind password */
148                         srcldappasswd = strdup( optarg );
149                         while ( *optarg )
150                                 *optarg++ = 'x';
151                         break;
152
153                 case 'B':       /* dest bind dn */
154                         destldapbinddn = strdup( optarg );
155                         break;
156
157                 case 'P':       /* dest bind password */
158                         destldappasswd = strdup( optarg );
159                         while ( *optarg )
160                                 *optarg++ = 'x';
161                         break;
162
163                 case 'm':       /* src bind method */
164                         if ( strcasecmp( optarg, "simple" ) == 0 ) {
165                                 srcldapauthmethod = LDAP_AUTH_SIMPLE;
166                         } else if ( strcasecmp( optarg, "kerberos" ) == 0 ) {
167                                 srcldapauthmethod = LDAP_AUTH_KRBV4;
168                         } else {
169                                 fprintf( stderr, "%s: unknown auth method\n", optarg );
170                                 fputs( "expecting \"simple\" or \"kerberos\"\n", stderr );
171                                 exit( 1 );
172                         }
173                         break;
174
175                 case 'M':       /* dest bind method */
176                         if ( strcasecmp( optarg, "simple" ) == 0 ) {
177                                 destldapauthmethod = LDAP_AUTH_SIMPLE;
178                         } else if ( strcasecmp( optarg, "kerberos" ) == 0 ) {
179                                 destldapauthmethod = LDAP_AUTH_KRBV4;
180                         } else {
181                                 fprintf( stderr, "%s: unknown auth method\n", optarg );
182                                 fputs( "expecting \"simple\" or \"kerberos\"\n", stderr );
183                                 exit( 1 );
184                         }
185                         break;
186
187                 case 'c':       /* ldbm cache size */
188                         ldbmcachesize = atoi( optarg );
189                         break;
190
191                 case 'v':       /* turn on verbose mode */
192                         verbose++;
193                         break;
194
195                 case 'n':       /* don't actually install index info */
196                         not++;
197                         break;
198
199                 default:
200                         usage( argv[0] );
201                         exit( 1 );
202                 }
203         }
204         if ( optind == argc || ldapsrcurl == NULL || ldapdesturl == NULL ) {
205                 usage( argv[0] );
206                 exit( 1 );
207         }
208         attrs = &argv[optind];
209
210         /*
211          * open the ldap connection and start searching for the entries
212          * we will use to generate the centroids.
213          */
214
215         if ( (ld = start_ldap_search( ldapsrcurl, ldapfilter, attrs )) == NULL ) {
216                 fprintf( stderr, "could not initiate ldap search\n" );
217                 exit( 1 );
218         }
219
220         if ( create_tmp_files( attrs, &tmpfile, &ldbm ) != 0 ) {
221                 fprintf( stderr, "could not create temp files\n" );
222                 exit( 1 );
223         }
224
225         /*
226          * go through the entries returned, building a centroid for each
227          * attribute as we go.
228          */
229
230         if ( (count = generate_new_centroids( ld, attrs, ldbm )) < 1 ) {
231                 if ( count == 0 ) {
232                     fprintf( stderr, "no entries matched\n" );
233                     exit( 0 );
234                 } else {
235                     fprintf( stderr, "could not generate new centroid\n" );
236                     exit( 1 );
237                 }
238         }
239
240         /*
241          * for each centroid we generated above, compare to the existing
242          * centroid, if any, and produce adds and deletes, or produce
243          * an entirely new centroid. in either case, update the "current"
244          * centroid version with the new one we just generated.
245          */
246
247         if ( (ld = bind_to_destination_ldap( ldapsrcurl, ldapdesturl )) == NULL ) {
248                 fprintf( stderr,
249                   "could not bind to index server, or could not create index entry\n" );
250                 exit( 1 );
251         }
252
253         for ( i = 0; ldbm[i] != NULL; i++ ) {
254                 /* generate the name of the existing centroid, if any */
255                 s = strrchr( tmpfile[i], '/' );
256                 *s = '\0';
257                 sprintf( buf, "%s/cent.%s", tmpfile[i], attrs[i] );
258                 *s = '/';
259
260                 /* generate the full centroid changes */
261                 if ( centroidtype == CENTROID_FULL || (oldbm = ldbm_open( buf,
262                   LDBM_WRITER, 0, ldbmcachesize )) == NULL ) {
263                         if ( (mods = full_centroid( attrs[i], ldbm[i], count )) == NULL ) {
264                                 fprintf( stderr, "could not produce full centroid for %s\n",
265                                   attrs[i] );
266                                 continue;
267                         }
268
269                 /* generate the differential centroid changes */
270                 } else {
271                         if ( (mods = diff_centroids( attrs[i], oldbm, ldbm[i], count ))
272                           == NULL ) {
273                                 fprintf( stderr, "could not diff centroids\n" );
274                                 ldbm_close( oldbm );
275                                 continue;
276                         }
277                         ldbm_close( oldbm );
278                 }
279
280                 if ( verbose > 1 ) {
281                         printf("changes:\n");
282                         for ( j = 0; mods[j] != NULL; j++ ) {
283                                 switch( mods[j]->mod_op ) {
284                                 case LDAP_MOD_ADD:
285                                         printf( "\tadd: %s\n",mods[j]->mod_type );
286                                         break;
287                                 case LDAP_MOD_DELETE:
288                                         printf( "\tdelete: %s\n",mods[j]->mod_type );
289                                         break;
290                                 case LDAP_MOD_REPLACE:
291                                         printf( "\treplace: %s\n",mods[j]->mod_type );
292                                         break;
293                                 }
294                                 if ( mods[j]->mod_values != NULL ) {
295                                         for ( k = 0; mods[j]->mod_values[k] != NULL; k++ ) {
296                                                 printf( "\t\t%s\n", mods[j]->mod_values[k] );
297                                         }
298                                 }
299                         }
300                         printf("end changes:\n");
301                 }
302
303                 if ( verbose ) {
304                         printf( "%sModifying centroid...", not ? "Not " : "" );
305                         fflush( stdout );
306                 }
307
308                 /* attempt to make the changes to the index server entry */
309                 if ( !not && ldap_modify_s( ld, ldapbase, mods ) != LDAP_SUCCESS ) {
310                         fprintf( stderr, "could not apply centroid modification for %s\n",
311                           attrs[i] );
312                         ldap_perror( ld, ldapbase );
313                 }
314                 ldap_mods_free( mods, 1 );
315
316                 if ( verbose ) {
317                         printf( "\n" );
318                         fflush( stdout );
319                 }
320
321                 /* move the new centroid into the old one's place */
322                 if ( ! not ) {
323                         (void) unlink( buf );
324                         if ( link( tmpfile[i], buf ) != 0 ) {
325                                 perror( "link" );
326                                 fprintf( stderr, "could not rename %s to %s\n", buf,
327                                   tmpfile[i] );
328                                 continue;
329                         }
330                 }
331                 (void) unlink( tmpfile[i] );
332         }
333
334         /* clean up */
335         for ( i = 0; attrs[i] != NULL; i++ ) {
336                 ldbm_close( ldbm[i] );
337                 free( tmpfile[i] );
338         }
339         free( ldbm );
340         free( tmpfile );
341
342         exit( 0 );
343 }
344
345 /*
346  * open an ldap connection, bind, and initiate the search
347  */
348
349 static LDAP *
350 start_ldap_search(
351         char    *ldapsrcurl,
352         char    *ldapfilter,
353         char    **attrs
354 )
355 {
356         LDAP    *ld;
357         char    *s, *s2;
358         int             i;
359
360         if ( strncmp( ldapsrcurl, "ldap://", 7 ) != 0 ) {
361                 fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
362                 return( NULL );
363         }
364         s = ldapsrcurl + 7;
365         if ( (s2 = strchr( s, '/' )) == NULL ) {
366                 ldapbase = strdup( s );
367         } else {
368                 if ( *s != '/' ) {
369                         *s2 = '\0';
370                         ldaphost = strdup( s );
371                         *s2 = '/';
372                 }
373                 ldapbase = strdup( s2 + 1 );
374         }
375
376         if ( verbose ) {
377                 printf( "Base: %s\n", ldapbase );
378                 printf( "Attributes:" );
379                 for ( i = 0; attrs[i] != NULL; i++ ) {
380                         printf( " %s", attrs[i] );
381                 }
382                 printf( "\n" );
383                 printf( "Binding to source LDAP server..." );
384                 fflush( stdout );
385         }
386
387         if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
388                 perror( "ldap_open" );
389                 return( NULL );
390         }
391
392         if ( ldap_bind_s( ld, srcldapbinddn, srcldappasswd, srcldapauthmethod )
393           != LDAP_SUCCESS) {
394                 ldap_perror( ld, "ldap_bind_s" );
395                 ldap_unbind( ld );
396                 return( NULL );
397         }
398
399         printf( "\nInitiating search..." );
400         if ( ldap_search( ld, ldapbase, LDAP_SCOPE_SUBTREE, ldapfilter, attrs, 0 )
401           == -1 ) {
402                 ldap_perror( ld, "ldap_search" );
403                 ldap_unbind( ld );
404                 return( NULL );
405         }
406
407         if ( verbose ) {
408                 printf( "\n" );
409         }
410
411         return( ld );
412 }
413
414 /*
415  * create the temporary ldbm files we will use to hold the new centroids
416  */
417
418 static int
419 create_tmp_files(
420         char    **attrs,
421         char    ***tmpfile,
422         LDBM    **ldbm
423 )
424 {
425         int     i;
426
427         for ( i = 0; attrs[i] != NULL; i++ )
428                 ;       /* NULL */
429         i++;
430
431         if ( (*tmpfile = (char **) malloc( i * sizeof(char *) )) == NULL ) {
432                 perror( "malloc" );
433                 return( -1 );
434         }
435         if ( (*ldbm = (LDBM *) malloc( i * sizeof(LDBM) )) == NULL ) {
436                 perror( "malloc" );
437                 return( -1 );
438         }
439         for ( i = 0; attrs[i] != NULL; i++ ) {
440                 if ( ((*tmpfile)[i] = tempnam( centdir, NULL )) == NULL ) {
441                         perror( "tmpnam" );
442                         return( -1 );
443                 }
444
445                 if ( ((*ldbm)[i] = ldbm_open( (*tmpfile)[i], LDBM_WRCREAT, 0600,
446                   ldbmcachesize )) == NULL ) {
447                         fprintf( stderr, "ldbm_open of \"%s\" failed\n", (*tmpfile)[i] );
448                         perror( "ldbm_open" );
449                         return( -1 );
450                 }
451         }
452         (*tmpfile)[i] = NULL;
453         (*ldbm)[i] = NULL;
454
455         return( 0 );
456 }
457
458 /*
459  * step through each entry returned from the search and generate
460  * the appropriate centroid values.
461  */
462
463 static int
464 generate_new_centroids(
465         LDAP    *ld,
466         char    **attrs,
467         LDBM    *ldbm
468 )
469 {
470         Datum           key, data;
471         int                     rc, i, j, count;
472         LDAPMessage     *res, *e;
473         char            *dn, *s, *w;
474         char            **val;
475         char            last;
476
477         if ( verbose ) {
478                 printf( "Generating new centroids for..." );
479                 fflush( stdout );
480         }
481
482         data.dptr = "";
483         data.dsize = 1;
484         count = 0;
485         while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ))
486           == LDAP_RES_SEARCH_ENTRY ) {
487                 count++;
488                 e = ldap_first_entry( ld, res );
489                 dn = ldap_get_dn( ld, e );
490
491                 /* for each attr we want to generate a centroid for */
492                 for ( i = 0; attrs[i] != NULL; i++ ) {
493                         if ( (val = ldap_get_values( ld, e, attrs[i] )) == NULL ) {
494                                 continue;
495                         }
496
497                         /* for each value */
498                         for ( j = 0; val[j] != NULL; j++ ) {
499                                 /* normalize the value */
500                                 for ( s = val[j]; *s; s++ ) {
501                                         if ( isascii( *s ) ) {
502                                                 *s = TOLOWER( *s );
503                                         }
504                                         last = *s;
505                                 }
506                                 if ( isascii( last ) && isdigit( last ) ) {
507                                         continue;
508                                 }
509
510                                 /* generate a value-based centroid */
511                                 if ( centroidvalues == CENTROID_VALUE ) {
512                                         key.dptr = val[j];
513                                         key.dsize = strlen( key.dptr ) + 1;
514                                         (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
515
516                                 /* generate a word-based centroid */
517                                 } else {
518                                         for ( w = strtok( val[j], WORD_BREAKS ); w != NULL;
519                                           w = strtok( NULL, WORD_BREAKS ) ) {
520                                                 key.dptr = w;
521                                                 key.dsize = strlen( key.dptr ) + 1;
522                                                 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
523                                         }
524                                 }
525                         }
526                         ldap_value_free( val );
527                 }
528                 free( dn );
529                 ldap_msgfree( res );
530         }
531         ldap_msgfree( res );
532         ldap_unbind( ld );
533
534         if ( verbose ) {
535                 printf( "%d entries\n", count );
536         }
537
538         return( count );
539 }
540
541 /*
542  * compare the old and new centroids, generating the appropriate add
543  * and delete operations. if the underlying database is ordered, we
544  * can do this more efficiently.
545  */
546
547 static LDAPMod **
548 diff_centroids(
549         char    *attr,
550         LDBM    oldbm,
551         LDBM    nldbm,
552     int         nentries
553 )
554 {
555         Datum   okey, nkey;
556         Datum   olast, nlast;
557         Datum   lastkey, key;
558         Datum   data;
559         int             rc;
560         LDAPMod **mods;
561         char    **avals, **dvals;
562         int             amax, acur, dmax, dcur;
563         char    **vals;
564
565 #ifdef HAVE_BERKELEY_DB2
566         DBC     *ocursorp;
567         DBC     *ncursorp;
568 #endif /* HAVE_BERKELEY_DB2 */
569
570         if ( verbose ) {
571                 printf( "Generating mods for differential %s centroid...", attr );
572                 fflush( stdout );
573         }
574
575         if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 4 )) == NULL ||
576              (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
577              (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
578              (mods[2] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
579              (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
580                  (vals[0] = (char *) malloc( 20 )) == NULL )
581         {
582                 perror( "malloc" );
583                 exit( -1 );
584         }
585         /* add values in mods[0] */
586         mods[0]->mod_op = LDAP_MOD_ADD;
587         mods[0]->mod_type = attr;
588         mods[0]->mod_values = NULL;
589         avals = NULL;
590         acur = amax = 0;
591         /* delete values in mods[1] */
592         mods[1]->mod_op = LDAP_MOD_DELETE;
593         mods[1]->mod_type = attr;
594         mods[1]->mod_values = NULL;
595         dvals = NULL;
596         dcur = dmax = 0;
597         /* number of entries in mods[2] */
598         sprintf( vals[0], "%d", nentries );
599         vals[1] = NULL;
600         mods[2]->mod_op = LDAP_MOD_REPLACE;
601         mods[2]->mod_type = "nentries";
602         mods[2]->mod_values = vals;
603         /* null terminate list of mods */
604         mods[3] = NULL;
605
606 #ifdef LDBM_ORDERED
607         /*
608          * if the underlying database is ordered, we can do a more efficient
609          * dual traversal, yielding O(N) performance.
610          */
611
612         olast.dptr = NULL;
613         nlast.dptr = NULL;
614 #ifdef HAVE_BERKELEY_DB2
615         for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
616                         nkey = ldbm_firstkey( nldbm, &ncursorp );
617               okey.dptr != NULL && nkey.dptr != NULL; )
618 #else
619         for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
620               okey.dptr != NULL && nkey.dptr != NULL; )
621 #endif
622         {
623                 rc = strcmp( okey.dptr, nkey.dptr );
624
625                 if ( rc == 0 ) {
626                         /* value is in both places - leave it */
627                         if ( olast.dptr != NULL ) {
628                                 ldbm_datum_free( oldbm, olast );
629                         }
630                         olast = okey;
631                         if ( nlast.dptr != NULL ) {
632                                 ldbm_datum_free( nldbm, nlast );
633                         }
634                         nlast = nkey;
635
636 #ifdef HAVE_BERKELEY_DB2
637                         okey = ldbm_nextkey( oldbm, olast, ocursorp );
638                         nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
639 #else
640                         okey = ldbm_nextkey( oldbm, olast );
641                         nkey = ldbm_nextkey( nldbm, nlast );
642 #endif
643                 } else if ( rc > 0 ) {
644                         /* new value is not in old centroid - add it */
645                         if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
646                                 ldap_mods_free( mods, 1 );
647                                 return( NULL );
648                         }
649
650                         if ( nlast.dptr != NULL ) {
651                                 ldbm_datum_free( nldbm, nlast );
652                         }
653                         nlast = nkey;
654
655 #ifdef HAVE_BERKELEY_DB2
656                         nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
657 #else
658                         nkey = ldbm_nextkey( nldbm, nlast );
659 #endif
660                 } else {
661                         /* old value is not in new centroid - delete it */
662                         if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
663                                 ldap_mods_free( mods, 1 );
664                                 return( NULL );
665                         }
666
667                         if ( olast.dptr != NULL ) {
668                                 ldbm_datum_free( oldbm, olast );
669                         }
670                         olast = okey;
671
672 #ifdef HAVE_BERKELEY_DB2
673                         okey = ldbm_nextkey( oldbm, olast, ocursorp );
674 #else
675                         okey = ldbm_nextkey( oldbm, olast );
676 #endif
677                 }
678         }
679
680         while ( okey.dptr != NULL ) {
681                 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
682                         ldap_mods_free( mods, 1 );
683                         return( NULL );
684                 }
685
686 #ifdef HAVE_BERKELEY_DB2
687                 okey = ldbm_nextkey( oldbm, olast, ocursorp );
688 #else
689                 okey = ldbm_nextkey( oldbm, olast );
690 #endif
691                 if ( olast.dptr != NULL ) {
692                         ldbm_datum_free( oldbm, olast );
693                 }
694                 olast = okey;
695         }
696         if ( olast.dptr != NULL ) {
697                 ldbm_datum_free( oldbm, olast );
698         }
699         while ( nkey.dptr != NULL ) {
700                 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
701                         ldap_mods_free( mods, 1 );
702                         return( NULL );
703                 }
704
705 #ifdef HAVE_BERKELEY_DB2
706                 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
707 #else
708                 nkey = ldbm_nextkey( nldbm, nlast );
709 #endif
710                 if ( nlast.dptr != NULL ) {
711                         ldbm_datum_free( nldbm, nlast );
712                 }
713                 nlast = nkey;
714         }
715         if ( nlast.dptr != NULL ) {
716                 ldbm_datum_free( nldbm, nlast );
717         }
718 #else
719         /*
720          * if the underlying database is not ordered, we have to
721          * generate list of values to add by stepping through all new
722          * values and looking them up in the old centroid (not there => add),
723          * then stepping through all old values and looking them up in the
724          * new centroid (not there => delete). this yields O(Nf(N)) performance,
725          * where f(N) is the order to retrieve a single item.
726          */
727
728         /* generate list of values to add */
729         lastkey.dptr = NULL;
730 #ifdef HAVE_BERKELEY_DB2
731         for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
732           key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
733 #else
734         for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
735           key = ldbm_nextkey( nldbm, lastkey ) )
736 #endif
737         {
738                 /* see if it's in the old one */
739                 data = ldbm_fetch( oldbm, key );
740
741                 /* not there - add it */
742                 if ( data.dptr == NULL ) {
743                         if ( charray_add_dup( &avals, &acur, &amax, key.dptr ) == NULL ) {
744                                 ldap_mods_free( mods, 1 );
745                                 return( NULL );
746                         }
747                 } else {
748                         ldbm_datum_free( oldbm, data );
749                 }
750                 if ( lastkey.dptr != NULL ) {
751                         ldbm_datum_free( nldbm, lastkey );
752                 }
753                 lastkey = key;
754         }
755         if ( lastkey.dptr != NULL ) {
756                 ldbm_datum_free( nldbm, lastkey );
757         }
758
759         /* generate list of values to delete */
760         lastkey.dptr = NULL;
761 #ifdef HAVE_BERKELEY_DB2
762         for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
763           key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
764 #else
765         for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
766           key = ldbm_nextkey( oldbm, lastkey ) )
767 #endif
768         {
769                 /* see if it's in the new one */
770                 data = ldbm_fetch( nldbm, key );
771
772                 /* not there - delete it */
773                 if ( data.dptr == NULL ) {
774                         if ( charray_add_dup( &dvals, &dcur, &dmax, key.dptr ) == NULL ) {
775                                 ldap_mods_free( mods, 1 );
776                                 return( NULL );
777                         }
778                 } else {
779                         ldbm_datum_free( nldbm, data );
780                 }
781                 if ( lastkey.dptr != NULL ) {
782                         ldbm_datum_free( oldbm, lastkey );
783                 }
784                 lastkey = key;
785         }
786         if ( lastkey.dptr != NULL ) {
787                 ldbm_datum_free( oldbm, lastkey );
788         }
789 #endif
790
791         mods[0]->mod_values = avals;
792         mods[1]->mod_values = dvals;
793
794         if ( verbose ) {
795                 printf( "\n" );
796                 fflush( stdout );
797         }
798
799         if ( mods[1]->mod_values == NULL ) {
800                 free( (char *) mods[1] );
801                 mods[1] = NULL;
802         }
803         if ( mods[0]->mod_values == NULL ) {
804                 free( (char *) mods[0] );
805                 mods[0] = mods[1];
806                 mods[1] = NULL;
807         }
808         if ( mods[0] == NULL ) {
809                 free( (char *) mods );
810                 return( NULL );
811         } else {
812                 return( mods );
813         }
814 }
815
816 static LDAPMod **
817 full_centroid(
818         char    *attr,
819         LDBM    ldbm,
820     int         nentries
821 )
822 {
823         Datum   key, lastkey;
824         LDAPMod **mods;
825         char    **vals;
826         int             vcur, vmax;
827
828 #ifdef HAVE_BERKELEY_DB2
829         DBC *cursorp;
830 #endif
831
832         if ( verbose ) {
833                 printf( "Generating mods for full %s centroid...", attr );
834                 fflush( stdout );
835         }
836
837         if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 3 )) == NULL ||
838              (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
839              (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
840              (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
841              (vals[0] = (char *) malloc( 20 )) == NULL )
842         {
843                 perror( "malloc" );
844                 exit( -1 );
845         }
846         mods[0]->mod_op = LDAP_MOD_REPLACE;
847         mods[0]->mod_type = attr;
848         mods[0]->mod_values = NULL;
849         sprintf( vals[0], "%d", nentries );
850         vals[1] = NULL;
851         mods[1]->mod_op = LDAP_MOD_REPLACE;
852         mods[1]->mod_type = "nentries";
853         mods[1]->mod_values = vals;
854         mods[2] = NULL;
855
856         lastkey.dptr = NULL;
857         vals = NULL;
858         vcur = vmax = 0;
859 #ifdef HAVE_BERKELEY_DB2
860         for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
861           key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
862 #else
863         for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
864           key = ldbm_nextkey( ldbm, lastkey ) )
865 #endif
866         {
867                 if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
868                         ldap_mods_free( mods, 1 );
869                         return( NULL );
870                 }
871
872                 if ( lastkey.dptr != NULL ) {
873                         ldbm_datum_free( ldbm, lastkey );
874                 }
875                 lastkey = key;
876         }
877         if ( lastkey.dptr != NULL ) {
878                 ldbm_datum_free( ldbm, lastkey );
879         }
880         mods[0]->mod_values = vals;
881
882         if ( verbose ) {
883                 printf( "\n" );
884                 fflush( stdout );
885         }
886
887         if ( mods[0]->mod_values == NULL ) {
888                 free( (char *) mods[0] );
889                 free( (char *) mods );
890                 return( NULL );
891         } else {
892                 return( mods );
893         }
894 }
895
896 /*
897  * extract the destination ldap host, port, and base object for the
898  * server to receive the index information. then, open a connection,
899  * bind, and see if the entry exists. if not, create it and set things
900  * up so the centroid full and diff routines can modify it to contain
901  * the new centroid information.
902  */
903
904 static LDAP *
905 bind_to_destination_ldap(
906         char    *ldapsrcurl,
907         char    *ldapdesturl
908 )
909 {
910         LDAP            *ld;
911         LDAPMessage     *res;
912         int                     rc;
913         char            *s, *s2, *d;
914         char            *attrs[2], *refvalues[2], *ocvalues[2];
915         LDAPMod         *mp[3];
916         LDAPMod         m[2];
917         char            buf[BUFSIZ];
918
919         if ( verbose ) {
920                 printf( "Binding to destination LDAP server..." );
921                 fflush( stdout );
922         }
923
924         /* first, pick out the destination ldap server info */
925         if ( ldapbase != NULL ) {
926                 free( ldapbase );
927                 ldapbase = NULL;
928         }
929         if ( strncmp( ldapdesturl, "ldap://", 7 ) != 0 ) {
930                 fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
931                 return( NULL );
932         }
933         s = ldapdesturl + 7;
934         if ( (s2 = strchr( s, '/' )) == NULL ) {
935                 ldapbase = strdup( s );
936         } else {
937                 if ( *s != '/' ) {
938                         *s2 = '\0';
939                         if ( ldaphost != NULL )
940                                 free( ldaphost );
941                         ldaphost = strdup( s );
942                         *s2 = '/';
943                 }
944                 ldapbase = strdup( s2 + 1 );
945         }
946         strcpy( buf, "ref=" );
947         if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
948                 strcat( buf, "\"" );
949         }
950         for ( s = d = ldapsrcurl; *s; s++ ) {
951                 if ( *s != '"' ) {
952                         *d++ = *s;
953                 }
954         }
955         *d = '\0';
956         strcat( buf, ldapsrcurl );
957         if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
958                 strcat( buf, "\"" );
959         }
960         strcat( buf, ", " );
961         strcat( buf, ldapbase );
962         free( ldapbase );
963         ldapbase = strdup( buf );
964
965         if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
966                 perror( "ldap_open" );
967                 return( NULL );
968         }
969
970         if ( ldap_bind_s( ld, destldapbinddn, destldappasswd, destldapauthmethod )
971           != LDAP_SUCCESS) {
972                 ldap_perror( ld, "ldap_bind_s" );
973                 ldap_unbind( ld );
974                 return( NULL );
975         }
976         if ( verbose ) {
977                 printf( "\n" );
978         }
979
980         attrs[0] = "c";
981         attrs[1] = NULL;
982         rc = ldap_search_s( ld, ldapbase, LDAP_SCOPE_BASE, "(objectclass=*)",
983           attrs, 0, &res );
984         ldap_msgfree( res );
985
986         if ( rc == LDAP_NO_SUCH_OBJECT ) {
987                 if ( verbose ) {
988                         printf( "%sCreating centroid entry...", not ? "Not " : "" );
989                         fflush( stdout );
990                 }
991
992                 /* create the centroid index entry */
993                 m[0].mod_op = 0;
994                 m[0].mod_type = "ref";
995                 refvalues[0] = ldapsrcurl;
996                 refvalues[1] = NULL;
997                 m[0].mod_values = refvalues;
998                 m[1].mod_op = 0;
999                 m[1].mod_type = "objectclass";
1000                 ocvalues[0] = "indexentry";
1001                 ocvalues[1] = NULL;
1002                 m[1].mod_values = ocvalues;
1003                 mp[0] = &m[0];
1004                 mp[1] = &m[1];
1005                 mp[2] = NULL;
1006
1007                 if ( !not && ldap_add_s( ld, ldapbase, mp ) != LDAP_SUCCESS ) {
1008                         ldap_perror( ld, ldapbase );
1009                         ldap_unbind( ld );
1010                         return( NULL );
1011                 }
1012
1013                 if ( verbose ) {
1014                         printf( "\n" );
1015                         fflush( stdout );
1016                 }
1017         } else if ( rc != LDAP_SUCCESS ) {
1018                 ldap_perror( ld, "ldap_search_s" );
1019                 ldap_unbind( ld );
1020                 return( NULL );
1021         }
1022
1023         return( ld );
1024 }
1025
1026 static char **
1027 charray_add_dup(
1028         char    ***a,
1029     int         *cur,
1030     int         *max,
1031         char    *s
1032 )
1033 {
1034         int n;
1035  
1036         if ( *a == NULL ) {
1037                 *a = (char **) malloc( (BUFSIZ + 1) * sizeof(char *) );
1038                 *cur = 0;
1039                 *max = BUFSIZ;
1040         } else if ( *cur >= *max ) {
1041                 *max += BUFSIZ;
1042                 *a = (char **) realloc( *a, (*max + 1) * sizeof(char *) );
1043         }
1044         if ( *a == NULL ) {
1045                 return( NULL );
1046         }
1047
1048         (*a)[(*cur)++] = strdup( s );
1049         (*a)[*cur] = NULL;
1050         return( *a );
1051 }