]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/centipede.c
Add PORT of ldap.conf support from -devel.
[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 #include <ac/unistd.h>          /* get link(), unlink() */
12
13 #include <lber.h>
14 #include <ldap.h>
15
16 #include <ldapconfig.h>
17 #include <ldbm.h>
18
19 #define DEFAULT_LDAPFILTER      "(objectclass=*)"
20
21 #define CENTROID_VALUE  1
22 #define CENTROID_WORD   2
23
24 #define CENTROID_RELATIVE       1
25 #define CENTROID_FULL           2
26
27 #define WORD_BREAKS     " -',.()!;:&$%*\"/\\+_<>=?[]|^~"
28
29 char    *centdir;
30 int             ldbmcachesize;
31 int             centroidvalues;
32 int             centroidtype;
33 int             doweights;
34 char    *ldaphost;
35 char    *srcldapbinddn;
36 char    *srcldappasswd;
37 char    *destldapbinddn;
38 char    *destldappasswd;
39 char    *ldapbase;
40 int             srcldapauthmethod;
41 int             destldapauthmethod;
42 int             verbose;
43 int             not;
44
45 static LDAP             *start_ldap_search(char *ldapsrcurl, char *ldapfilter, char **attrs);
46 static LDAP             *bind_to_destination_ldap(char *ldapsrcurl, char *ldapdesturl);
47 static int              create_tmp_files(char **attrs, char ***tmpfile, LDBM **ldbm);
48 static int              generate_new_centroids(LDAP *ld, char **attrs, LDBM *ldbm);
49 static LDAPMod  **diff_centroids(char *attr, LDBM oldbm, LDBM nldbm, int nentries);
50 static LDAPMod  **full_centroid(char *attr, LDBM ldbm, int nentries);
51 static char             **charray_add_dup(char ***a, int *cur, int *max, char *s);
52
53 static void usage( char *name )
54 {
55         fprintf( stderr, "usage: %s [options] -s url -d url attributes\n", name );
56         fprintf( stderr, "where:\n" );
57         fprintf( stderr, "\t-s url\t\t[[ldap://][host[:port]]/]searchbasedn\n");
58         fprintf( stderr, "\t-d url\t\t[[ldap://][host[:port]]/]centroidentrydn\n");
59         fprintf( stderr, "options:\n" );
60         fprintf( stderr, "\t-v \t\tturn on verbose mode\n" );
61         fprintf( stderr, "\t-n \t\tgenerate, but do not install index info\n" );
62         fprintf( stderr, "\t-f filter\tentry selection filter\n" );
63         fprintf( stderr, "\t-F \t\tgenerate a full centroid\n" );
64         fprintf( stderr, "\t-R \t\tgenerate a relative centroid\n" );
65         fprintf( stderr, "\t-w \t\tgenerate a word-based centroid\n" );
66         fprintf( stderr, "\t-t directory\tcentroid directory\n" );
67         fprintf( stderr, "\t-b binddn\tsource bind dn\n" );
68         fprintf( stderr, "\t-p passwd\tsource bind passwd (for simple auth)\n" );
69         fprintf( stderr, "\t-m authmethod\tsource authmethod \"simple\" or \"kerberos\"\n" );
70         fprintf( stderr, "\t-B binddn\tdestination bind dn\n" );
71         fprintf( stderr, "\t-P passwd\tdestination bind passwd (for simple auth)\n" );
72         fprintf( stderr, "\t-M authmethod\tdestination authmethod \"simple\" or \"kerberos\"\n" );
73         fprintf( stderr, "\t-c size\t\tldbm cache size\n" );
74 }
75
76 int
77 main( int argc, char **argv )
78 {
79         char            *ldapfilter;
80         char            *ldapsrcurl, *ldapdesturl;
81         LDAP            *ld;
82         LDAPMod         **mods;
83         char            **attrs;
84         char            **tmpfile;
85         LDBM            *ldbm;
86         LDBM            oldbm;
87         char            buf[BUFSIZ];
88         int                     i, j, k, count;
89         char            *s;
90
91         ldapsrcurl = NULL;
92         ldapdesturl = NULL;
93         ldaphost = NULL;
94         ldapbase = NULL;
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, 0 )) == 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         LDAPMod **mods;
560         char    **avals, **dvals;
561         int             amax, acur, dmax, dcur;
562         char    **vals;
563
564 #ifdef HAVE_BERKELEY_DB2
565         DBC     *ocursorp;
566         DBC     *ncursorp;
567 #endif /* HAVE_BERKELEY_DB2 */
568
569         if ( verbose ) {
570                 printf( "Generating mods for differential %s centroid...", attr );
571                 fflush( stdout );
572         }
573
574         if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 4 )) == NULL ||
575              (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
576              (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
577              (mods[2] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
578              (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
579                  (vals[0] = (char *) malloc( 20 )) == NULL )
580         {
581                 perror( "malloc" );
582                 exit( -1 );
583         }
584         /* add values in mods[0] */
585         mods[0]->mod_op = LDAP_MOD_ADD;
586         mods[0]->mod_type = attr;
587         mods[0]->mod_values = NULL;
588         avals = NULL;
589         acur = amax = 0;
590         /* delete values in mods[1] */
591         mods[1]->mod_op = LDAP_MOD_DELETE;
592         mods[1]->mod_type = attr;
593         mods[1]->mod_values = NULL;
594         dvals = NULL;
595         dcur = dmax = 0;
596         /* number of entries in mods[2] */
597         sprintf( vals[0], "%d", nentries );
598         vals[1] = NULL;
599         mods[2]->mod_op = LDAP_MOD_REPLACE;
600         mods[2]->mod_type = "nentries";
601         mods[2]->mod_values = vals;
602         /* null terminate list of mods */
603         mods[3] = NULL;
604
605 #ifdef LDBM_ORDERED
606         /*
607          * if the underlying database is ordered, we can do a more efficient
608          * dual traversal, yielding O(N) performance.
609          */
610
611         olast.dptr = NULL;
612         nlast.dptr = NULL;
613 #ifdef HAVE_BERKELEY_DB2
614         for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
615                         nkey = ldbm_firstkey( nldbm, &ncursorp );
616               okey.dptr != NULL && nkey.dptr != NULL; )
617 #else
618         for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
619               okey.dptr != NULL && nkey.dptr != NULL; )
620 #endif
621         {
622                 int     rc = strcmp( okey.dptr, nkey.dptr );
623
624                 if ( rc == 0 ) {
625                         /* value is in both places - leave it */
626                         if ( olast.dptr != NULL ) {
627                                 ldbm_datum_free( oldbm, olast );
628                         }
629                         olast = okey;
630                         if ( nlast.dptr != NULL ) {
631                                 ldbm_datum_free( nldbm, nlast );
632                         }
633                         nlast = nkey;
634
635 #ifdef HAVE_BERKELEY_DB2
636                         okey = ldbm_nextkey( oldbm, olast, ocursorp );
637                         nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
638 #else
639                         okey = ldbm_nextkey( oldbm, olast );
640                         nkey = ldbm_nextkey( nldbm, nlast );
641 #endif
642                 } else if ( rc > 0 ) {
643                         /* new value is not in old centroid - add it */
644                         if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
645                                 ldap_mods_free( mods, 1 );
646                                 return( NULL );
647                         }
648
649                         if ( nlast.dptr != NULL ) {
650                                 ldbm_datum_free( nldbm, nlast );
651                         }
652                         nlast = nkey;
653
654 #ifdef HAVE_BERKELEY_DB2
655                         nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
656 #else
657                         nkey = ldbm_nextkey( nldbm, nlast );
658 #endif
659                 } else {
660                         /* old value is not in new centroid - delete it */
661                         if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
662                                 ldap_mods_free( mods, 1 );
663                                 return( NULL );
664                         }
665
666                         if ( olast.dptr != NULL ) {
667                                 ldbm_datum_free( oldbm, olast );
668                         }
669                         olast = okey;
670
671 #ifdef HAVE_BERKELEY_DB2
672                         okey = ldbm_nextkey( oldbm, olast, ocursorp );
673 #else
674                         okey = ldbm_nextkey( oldbm, olast );
675 #endif
676                 }
677         }
678
679         while ( okey.dptr != NULL ) {
680                 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
681                         ldap_mods_free( mods, 1 );
682                         return( NULL );
683                 }
684
685 #ifdef HAVE_BERKELEY_DB2
686                 okey = ldbm_nextkey( oldbm, olast, ocursorp );
687 #else
688                 okey = ldbm_nextkey( oldbm, olast );
689 #endif
690                 if ( olast.dptr != NULL ) {
691                         ldbm_datum_free( oldbm, olast );
692                 }
693                 olast = okey;
694         }
695         if ( olast.dptr != NULL ) {
696                 ldbm_datum_free( oldbm, olast );
697         }
698         while ( nkey.dptr != NULL ) {
699                 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
700                         ldap_mods_free( mods, 1 );
701                         return( NULL );
702                 }
703
704 #ifdef HAVE_BERKELEY_DB2
705                 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
706 #else
707                 nkey = ldbm_nextkey( nldbm, nlast );
708 #endif
709                 if ( nlast.dptr != NULL ) {
710                         ldbm_datum_free( nldbm, nlast );
711                 }
712                 nlast = nkey;
713         }
714         if ( nlast.dptr != NULL ) {
715                 ldbm_datum_free( nldbm, nlast );
716         }
717 #else
718         /*
719          * if the underlying database is not ordered, we have to
720          * generate list of values to add by stepping through all new
721          * values and looking them up in the old centroid (not there => add),
722          * then stepping through all old values and looking them up in the
723          * new centroid (not there => delete). this yields O(Nf(N)) performance,
724          * where f(N) is the order to retrieve a single item.
725          */
726
727         /* generate list of values to add */
728         lastkey.dptr = NULL;
729 #ifdef HAVE_BERKELEY_DB2
730         for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
731           key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
732 #else
733         for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
734           key = ldbm_nextkey( nldbm, lastkey ) )
735 #endif
736         {
737                 /* see if it's in the old one */
738                 data = ldbm_fetch( oldbm, key );
739
740                 /* not there - add it */
741                 if ( data.dptr == NULL ) {
742                         if ( charray_add_dup( &avals, &acur, &amax, key.dptr ) == NULL ) {
743                                 ldap_mods_free( mods, 1 );
744                                 return( NULL );
745                         }
746                 } else {
747                         ldbm_datum_free( oldbm, data );
748                 }
749                 if ( lastkey.dptr != NULL ) {
750                         ldbm_datum_free( nldbm, lastkey );
751                 }
752                 lastkey = key;
753         }
754         if ( lastkey.dptr != NULL ) {
755                 ldbm_datum_free( nldbm, lastkey );
756         }
757
758         /* generate list of values to delete */
759         lastkey.dptr = NULL;
760 #ifdef HAVE_BERKELEY_DB2
761         for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
762           key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
763 #else
764         for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
765           key = ldbm_nextkey( oldbm, lastkey ) )
766 #endif
767         {
768                 /* see if it's in the new one */
769                 data = ldbm_fetch( nldbm, key );
770
771                 /* not there - delete it */
772                 if ( data.dptr == NULL ) {
773                         if ( charray_add_dup( &dvals, &dcur, &dmax, key.dptr ) == NULL ) {
774                                 ldap_mods_free( mods, 1 );
775                                 return( NULL );
776                         }
777                 } else {
778                         ldbm_datum_free( nldbm, data );
779                 }
780                 if ( lastkey.dptr != NULL ) {
781                         ldbm_datum_free( oldbm, lastkey );
782                 }
783                 lastkey = key;
784         }
785         if ( lastkey.dptr != NULL ) {
786                 ldbm_datum_free( oldbm, lastkey );
787         }
788 #endif
789
790         mods[0]->mod_values = avals;
791         mods[1]->mod_values = dvals;
792
793         if ( verbose ) {
794                 printf( "\n" );
795                 fflush( stdout );
796         }
797
798         if ( mods[1]->mod_values == NULL ) {
799                 free( (char *) mods[1] );
800                 mods[1] = NULL;
801         }
802         if ( mods[0]->mod_values == NULL ) {
803                 free( (char *) mods[0] );
804                 mods[0] = mods[1];
805                 mods[1] = NULL;
806         }
807         if ( mods[0] == NULL ) {
808                 free( (char *) mods );
809                 return( NULL );
810         } else {
811                 return( mods );
812         }
813 }
814
815 static LDAPMod **
816 full_centroid(
817         char    *attr,
818         LDBM    ldbm,
819     int         nentries
820 )
821 {
822         Datum   key, lastkey;
823         LDAPMod **mods;
824         char    **vals;
825         int             vcur, vmax;
826
827 #ifdef HAVE_BERKELEY_DB2
828         DBC *cursorp;
829 #endif
830
831         if ( verbose ) {
832                 printf( "Generating mods for full %s centroid...", attr );
833                 fflush( stdout );
834         }
835
836         if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 3 )) == NULL ||
837              (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
838              (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
839              (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
840              (vals[0] = (char *) malloc( 20 )) == NULL )
841         {
842                 perror( "malloc" );
843                 exit( -1 );
844         }
845         mods[0]->mod_op = LDAP_MOD_REPLACE;
846         mods[0]->mod_type = attr;
847         mods[0]->mod_values = NULL;
848         sprintf( vals[0], "%d", nentries );
849         vals[1] = NULL;
850         mods[1]->mod_op = LDAP_MOD_REPLACE;
851         mods[1]->mod_type = "nentries";
852         mods[1]->mod_values = vals;
853         mods[2] = NULL;
854
855         lastkey.dptr = NULL;
856         vals = NULL;
857         vcur = vmax = 0;
858 #ifdef HAVE_BERKELEY_DB2
859         for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
860           key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
861 #else
862         for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
863           key = ldbm_nextkey( ldbm, lastkey ) )
864 #endif
865         {
866                 if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
867                         ldap_mods_free( mods, 1 );
868                         return( NULL );
869                 }
870
871                 if ( lastkey.dptr != NULL ) {
872                         ldbm_datum_free( ldbm, lastkey );
873                 }
874                 lastkey = key;
875         }
876         if ( lastkey.dptr != NULL ) {
877                 ldbm_datum_free( ldbm, lastkey );
878         }
879         mods[0]->mod_values = vals;
880
881         if ( verbose ) {
882                 printf( "\n" );
883                 fflush( stdout );
884         }
885
886         if ( mods[0]->mod_values == NULL ) {
887                 free( (char *) mods[0] );
888                 free( (char *) mods );
889                 return( NULL );
890         } else {
891                 return( mods );
892         }
893 }
894
895 /*
896  * extract the destination ldap host, port, and base object for the
897  * server to receive the index information. then, open a connection,
898  * bind, and see if the entry exists. if not, create it and set things
899  * up so the centroid full and diff routines can modify it to contain
900  * the new centroid information.
901  */
902
903 static LDAP *
904 bind_to_destination_ldap(
905         char    *ldapsrcurl,
906         char    *ldapdesturl
907 )
908 {
909         LDAP            *ld;
910         LDAPMessage     *res;
911         int                     rc;
912         char            *s, *s2, *d;
913         char            *attrs[2], *refvalues[2], *ocvalues[2];
914         LDAPMod         *mp[3];
915         LDAPMod         m[2];
916         char            buf[BUFSIZ];
917
918         if ( verbose ) {
919                 printf( "Binding to destination LDAP server..." );
920                 fflush( stdout );
921         }
922
923         /* first, pick out the destination ldap server info */
924         if ( ldapbase != NULL ) {
925                 free( ldapbase );
926                 ldapbase = NULL;
927         }
928         if ( strncmp( ldapdesturl, "ldap://", 7 ) != 0 ) {
929                 fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
930                 return( NULL );
931         }
932         s = ldapdesturl + 7;
933         if ( (s2 = strchr( s, '/' )) == NULL ) {
934                 ldapbase = strdup( s );
935         } else {
936                 if ( *s != '/' ) {
937                         *s2 = '\0';
938                         if ( ldaphost != NULL )
939                                 free( ldaphost );
940                         ldaphost = strdup( s );
941                         *s2 = '/';
942                 }
943                 ldapbase = strdup( s2 + 1 );
944         }
945         strcpy( buf, "ref=" );
946         if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
947                 strcat( buf, "\"" );
948         }
949         for ( s = d = ldapsrcurl; *s; s++ ) {
950                 if ( *s != '"' ) {
951                         *d++ = *s;
952                 }
953         }
954         *d = '\0';
955         strcat( buf, ldapsrcurl );
956         if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
957                 strcat( buf, "\"" );
958         }
959         strcat( buf, ", " );
960         strcat( buf, ldapbase );
961         free( ldapbase );
962         ldapbase = strdup( buf );
963
964         if ( (ld = ldap_open( ldaphost, 0 )) == NULL ) {
965                 perror( "ldap_open" );
966                 return( NULL );
967         }
968
969         if ( ldap_bind_s( ld, destldapbinddn, destldappasswd, destldapauthmethod )
970           != LDAP_SUCCESS) {
971                 ldap_perror( ld, "ldap_bind_s" );
972                 ldap_unbind( ld );
973                 return( NULL );
974         }
975         if ( verbose ) {
976                 printf( "\n" );
977         }
978
979         attrs[0] = "c";
980         attrs[1] = NULL;
981         rc = ldap_search_s( ld, ldapbase, LDAP_SCOPE_BASE, "(objectclass=*)",
982           attrs, 0, &res );
983         ldap_msgfree( res );
984
985         if ( rc == LDAP_NO_SUCH_OBJECT ) {
986                 if ( verbose ) {
987                         printf( "%sCreating centroid entry...", not ? "Not " : "" );
988                         fflush( stdout );
989                 }
990
991                 /* create the centroid index entry */
992                 m[0].mod_op = 0;
993                 m[0].mod_type = "ref";
994                 refvalues[0] = ldapsrcurl;
995                 refvalues[1] = NULL;
996                 m[0].mod_values = refvalues;
997                 m[1].mod_op = 0;
998                 m[1].mod_type = "objectclass";
999                 ocvalues[0] = "indexentry";
1000                 ocvalues[1] = NULL;
1001                 m[1].mod_values = ocvalues;
1002                 mp[0] = &m[0];
1003                 mp[1] = &m[1];
1004                 mp[2] = NULL;
1005
1006                 if ( !not && ldap_add_s( ld, ldapbase, mp ) != LDAP_SUCCESS ) {
1007                         ldap_perror( ld, ldapbase );
1008                         ldap_unbind( ld );
1009                         return( NULL );
1010                 }
1011
1012                 if ( verbose ) {
1013                         printf( "\n" );
1014                         fflush( stdout );
1015                 }
1016         } else if ( rc != LDAP_SUCCESS ) {
1017                 ldap_perror( ld, "ldap_search_s" );
1018                 ldap_unbind( ld );
1019                 return( NULL );
1020         }
1021
1022         return( ld );
1023 }
1024
1025 static char **
1026 charray_add_dup(
1027         char    ***a,
1028     int         *cur,
1029     int         *max,
1030         char    *s
1031 )
1032 {
1033         int n;
1034  
1035         if ( *a == NULL ) {
1036                 *a = (char **) malloc( (BUFSIZ + 1) * sizeof(char *) );
1037                 *cur = 0;
1038                 *max = BUFSIZ;
1039         } else if ( *cur >= *max ) {
1040                 *max += BUFSIZ;
1041                 *a = (char **) realloc( *a, (*max + 1) * sizeof(char *) );
1042         }
1043         if ( *a == NULL ) {
1044                 return( NULL );
1045         }
1046
1047         (*a)[(*cur)++] = strdup( s );
1048         (*a)[*cur] = NULL;
1049         return( *a );
1050 }