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