1 /* centipede.c - generate and install indexing information (view w/tabstop=4) */
14 #include <ldapconfig.h>
17 #define DEFAULT_LDAPFILTER "(objectclass=*)"
19 #define CENTROID_VALUE 1
20 #define CENTROID_WORD 2
22 #define CENTROID_RELATIVE 1
23 #define CENTROID_FULL 2
25 #define WORD_BREAKS " -',.()!;:&$%*\"/\\+_<>=?[]|^~"
38 int srcldapauthmethod;
39 int destldapauthmethod;
43 static LDAP *start_ldap_search();
44 static LDAP *bind_to_destination_ldap();
45 static int create_tmp_files();
46 static int generate_new_centroids();
47 static LDAPMod **diff_centroids();
48 static LDAPMod **full_centroid();
49 static char **charray_add_dup();
51 static void usage( char *name )
53 fprintf( stderr, "usage: %s [options] -s url -d url attributes\n", name );
54 fprintf( stderr, "where:\n" );
55 fprintf( stderr, "\t-s url\t\t[[ldap://][host[:port]]/]searchbasedn\n");
56 fprintf( stderr, "\t-d url\t\t[[ldap://][host[:port]]/]centroidentrydn\n");
57 fprintf( stderr, "options:\n" );
58 fprintf( stderr, "\t-v \t\tturn on verbose mode\n" );
59 fprintf( stderr, "\t-n \t\tgenerate, but do not install index info\n" );
60 fprintf( stderr, "\t-f filter\tentry selection filter\n" );
61 fprintf( stderr, "\t-F \t\tgenerate a full centroid\n" );
62 fprintf( stderr, "\t-R \t\tgenerate a relative centroid\n" );
63 fprintf( stderr, "\t-w \t\tgenerate a word-based centroid\n" );
64 fprintf( stderr, "\t-t directory\tcentroid directory\n" );
65 fprintf( stderr, "\t-b binddn\tsource bind dn\n" );
66 fprintf( stderr, "\t-p passwd\tsource bind passwd (for simple auth)\n" );
67 fprintf( stderr, "\t-m authmethod\tsource authmethod \"simple\" or \"kerberos\"\n" );
68 fprintf( stderr, "\t-B binddn\tdestination bind dn\n" );
69 fprintf( stderr, "\t-P passwd\tdestination bind passwd (for simple auth)\n" );
70 fprintf( stderr, "\t-M authmethod\tdestination authmethod \"simple\" or \"kerberos\"\n" );
71 fprintf( stderr, "\t-c size\t\tldbm cache size\n" );
74 main( int argc, char **argv )
76 char *ldapfilter, *ldapref;
77 char *ldapsrcurl, *ldapdesturl;
93 ldapbase = DEFAULT_BASE;
94 srcldapauthmethod = LDAP_AUTH_SIMPLE;
95 destldapauthmethod = LDAP_AUTH_SIMPLE;
98 destldapbinddn = NULL;
99 destldappasswd = NULL;
100 ldapfilter = DEFAULT_LDAPFILTER;
101 centroidvalues = CENTROID_VALUE;
102 centroidtype = CENTROID_RELATIVE;
107 while ( (i = getopt( argc, argv, "s:d:c:b:B:f:FRWp:P:m:M:t:vwn" ))
110 case 's': /* source url [[ldap://][host[:port]]/]basedn */
111 ldapsrcurl = strdup( optarg );
114 case 'd': /* destination url [[ldap://][host[:port]]/]entrydn */
115 ldapdesturl = strdup( optarg );
118 case 'f': /* specify a filter */
119 ldapfilter = strdup( optarg );
122 case 'F': /* generate full centroid */
123 centroidtype = CENTROID_FULL;
126 case 'R': /* generate relative centroid */
127 centroidtype = CENTROID_RELATIVE;
130 case 'w': /* generate word centroid */
131 centroidvalues = CENTROID_WORD;
134 case 'W': /* generate weights */
138 case 't': /* temp file directory */
139 centdir = strdup( optarg );
142 case 'b': /* src bind dn */
143 srcldapbinddn = strdup( optarg );
146 case 'p': /* src bind password */
147 srcldappasswd = strdup( optarg );
152 case 'B': /* dest bind dn */
153 destldapbinddn = strdup( optarg );
156 case 'P': /* dest bind password */
157 destldappasswd = strdup( optarg );
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;
168 fprintf( stderr, "%s: unknown auth method\n", optarg );
169 fprintf( stderr, "expecting \"simple\" or \"kerberos\"\n",
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;
181 fprintf( stderr, "%s: unknown auth method\n", optarg );
182 fprintf( stderr, "expecting \"simple\" or \"kerberos\"\n",
188 case 'c': /* ldbm cache size */
189 ldbmcachesize = atoi( optarg );
192 case 'v': /* turn on verbose mode */
196 case 'n': /* don't actually install index info */
205 if ( optind == argc || ldapsrcurl == NULL || ldapdesturl == NULL ) {
209 attrs = &argv[optind];
212 * open the ldap connection and start searching for the entries
213 * we will use to generate the centroids.
216 if ( (ld = start_ldap_search( ldapsrcurl, ldapfilter, attrs )) == NULL ) {
217 fprintf( stderr, "could not initiate ldap search\n" );
221 if ( create_tmp_files( attrs, &tmpfile, &ldbm ) != 0 ) {
222 fprintf( stderr, "could not create temp files\n" );
227 * go through the entries returned, building a centroid for each
228 * attribute as we go.
231 if ( (count = generate_new_centroids( ld, attrs, ldbm )) < 1 ) {
233 fprintf( stderr, "no entries matched\n" );
236 fprintf( stderr, "could not generate new centroid\n" );
242 * for each centroid we generated above, compare to the existing
243 * centroid, if any, and produce adds and deletes, or produce
244 * an entirely new centroid. in either case, update the "current"
245 * centroid version with the new one we just generated.
248 if ( (ld = bind_to_destination_ldap( ldapsrcurl, ldapdesturl )) == NULL ) {
250 "could not bind to index server, or could not create index entry\n" );
254 for ( i = 0; ldbm[i] != NULL; i++ ) {
255 /* generate the name of the existing centroid, if any */
256 s = strrchr( tmpfile[i], '/' );
258 sprintf( buf, "%s/cent.%s", tmpfile[i], attrs[i] );
261 /* generate the full centroid changes */
262 if ( centroidtype == CENTROID_FULL || (oldbm = ldbm_open( buf,
263 LDBM_WRITER, 0, ldbmcachesize )) == NULL ) {
264 if ( (mods = full_centroid( attrs[i], ldbm[i], count )) == NULL ) {
265 fprintf( stderr, "could not produce full centroid for %s\n",
270 /* generate the differential centroid changes */
272 if ( (mods = diff_centroids( attrs[i], oldbm, ldbm[i], count ))
274 fprintf( stderr, "could not diff centroids\n" );
282 printf("changes:\n");
283 for ( j = 0; mods[j] != NULL; j++ ) {
284 switch( mods[j]->mod_op ) {
286 printf( "\tadd: %s\n",mods[j]->mod_type );
288 case LDAP_MOD_DELETE:
289 printf( "\tdelete: %s\n",mods[j]->mod_type );
291 case LDAP_MOD_REPLACE:
292 printf( "\treplace: %s\n",mods[j]->mod_type );
295 if ( mods[j]->mod_values != NULL ) {
296 for ( k = 0; mods[j]->mod_values[k] != NULL; k++ ) {
297 printf( "\t\t%s\n", mods[j]->mod_values[k] );
301 printf("end changes:\n");
305 printf( "%sModifying centroid...", not ? "Not " : "" );
309 /* attempt to make the changes to the index server entry */
310 if ( !not && ldap_modify_s( ld, ldapbase, mods ) != LDAP_SUCCESS ) {
311 fprintf( stderr, "could not apply centroid modification for %s\n",
313 ldap_perror( ld, ldapbase );
315 ldap_mods_free( mods, 1 );
322 /* move the new centroid into the old one's place */
324 (void) unlink( buf );
325 if ( link( tmpfile[i], buf ) != 0 ) {
327 fprintf( stderr, "could not rename %s to %s\n", buf,
332 (void) unlink( tmpfile[i] );
336 for ( i = 0; attrs[i] != NULL; i++ ) {
337 ldbm_close( ldbm[i] );
347 * open an ldap connection, bind, and initiate the search
361 if ( strncmp( ldapsrcurl, "ldap://", 7 ) == 0 ) {
364 if ( (s2 = strchr( s, '/' )) == NULL ) {
365 ldapbase = strdup( s );
369 ldaphost = strdup( s );
372 ldapbase = strdup( s2 + 1 );
376 printf( "Base: %s\n", ldapbase );
377 printf( "Attributes:" );
378 for ( i = 0; attrs[i] != NULL; i++ ) {
379 printf( " %s", attrs[i] );
382 printf( "Binding to source LDAP server..." );
386 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
387 perror( "ldap_open" );
391 if ( ldap_bind_s( ld, srcldapbinddn, srcldappasswd, srcldapauthmethod )
393 ldap_perror( ld, "ldap_bind_s" );
398 printf( "\nInitiating search..." );
399 if ( ldap_search( ld, ldapbase, LDAP_SCOPE_SUBTREE, ldapfilter, attrs, 0 )
401 ldap_perror( ld, "ldap_search" );
414 * create the temporary ldbm files we will use to hold the new centroids
426 for ( i = 0; attrs[i] != NULL; i++ )
430 if ( (*tmpfile = (char **) malloc( i * sizeof(char *) )) == NULL ) {
434 if ( (*ldbm = (LDBM *) malloc( i * sizeof(LDBM) )) == NULL ) {
438 for ( i = 0; attrs[i] != NULL; i++ ) {
439 if ( ((*tmpfile)[i] = tempnam( centdir, NULL )) == NULL ) {
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" );
451 (*tmpfile)[i] = NULL;
458 * step through each entry returned from the search and generate
459 * the appropriate centroid values.
463 generate_new_centroids(
471 LDAPMessage *res, *e;
477 printf( "Generating new centroids for..." );
484 while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ))
485 == LDAP_RES_SEARCH_ENTRY ) {
487 e = ldap_first_entry( ld, res );
488 dn = ldap_get_dn( ld, e );
490 /* for each attr we want to generate a centroid for */
491 for ( i = 0; attrs[i] != NULL; i++ ) {
492 if ( (val = ldap_get_values( ld, e, attrs[i] )) == NULL ) {
497 for ( j = 0; val[j] != NULL; j++ ) {
498 /* normalize the value */
499 for ( s = val[j]; *s; s++ ) {
500 if ( isascii( *s ) ) {
505 if ( isascii( last ) && isdigit( last ) ) {
509 /* generate a value-based centroid */
510 if ( centroidvalues == CENTROID_VALUE ) {
512 key.dsize = strlen( key.dptr ) + 1;
513 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
515 /* generate a word-based centroid */
517 for ( w = strtok( val[j], WORD_BREAKS ); w != NULL;
518 w = strtok( NULL, WORD_BREAKS ) ) {
520 key.dsize = strlen( key.dptr ) + 1;
521 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
525 ldap_value_free( val );
534 printf( "%d entries\n", count );
541 * compare the old and new centroids, generating the appropriate add
542 * and delete operations. if the underlying database is ordered, we
543 * can do this more efficiently.
560 char **avals, **dvals;
561 int amax, acur, dmax, dcur;
564 #ifdef HAVE_BERKELEY_DB2
567 #endif /* HAVE_BERKELEY_DB2 */
570 printf( "Generating mods for differential %s centroid...", attr );
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 )
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;
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;
596 /* number of entries in mods[2] */
597 sprintf( vals[0], "%d", nentries );
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 */
607 * if the underlying database is ordered, we can do a more efficient
608 * dual traversal, yielding O(N) performance.
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; )
618 for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
619 okey.dptr != NULL && nkey.dptr != NULL; )
622 rc = strcmp( okey.dptr, nkey.dptr );
625 /* value is in both places - leave it */
626 if ( olast.dptr != NULL ) {
627 ldbm_datum_free( oldbm, olast );
630 if ( nlast.dptr != NULL ) {
631 ldbm_datum_free( nldbm, nlast );
635 #ifdef HAVE_BERKELEY_DB2
636 okey = ldbm_nextkey( oldbm, olast, ocursorp );
637 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
639 okey = ldbm_nextkey( oldbm, olast );
640 nkey = ldbm_nextkey( nldbm, nlast );
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 );
649 if ( nlast.dptr != NULL ) {
650 ldbm_datum_free( nldbm, nlast );
654 #ifdef HAVE_BERKELEY_DB2
655 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
657 nkey = ldbm_nextkey( nldbm, nlast );
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 );
666 if ( olast.dptr != NULL ) {
667 ldbm_datum_free( oldbm, olast );
671 #ifdef HAVE_BERKELEY_DB2
672 okey = ldbm_nextkey( oldbm, olast, ocursorp );
674 okey = ldbm_nextkey( oldbm, olast );
679 while ( okey.dptr != NULL ) {
680 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
681 ldap_mods_free( mods, 1 );
685 #ifdef HAVE_BERKELEY_DB2
686 okey = ldbm_nextkey( oldbm, olast, ocursorp );
688 okey = ldbm_nextkey( oldbm, olast );
690 if ( olast.dptr != NULL ) {
691 ldbm_datum_free( oldbm, olast );
695 if ( olast.dptr != NULL ) {
696 ldbm_datum_free( oldbm, olast );
698 while ( nkey.dptr != NULL ) {
699 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
700 ldap_mods_free( mods, 1 );
704 #ifdef HAVE_BERKELEY_DB2
705 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
707 nkey = ldbm_nextkey( nldbm, nlast );
709 if ( nlast.dptr != NULL ) {
710 ldbm_datum_free( nldbm, nlast );
714 if ( nlast.dptr != NULL ) {
715 ldbm_datum_free( nldbm, nlast );
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.
727 /* generate list of values to add */
729 #ifdef HAVE_BERKELEY_DB2
730 for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
731 key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
733 for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
734 key = ldbm_nextkey( nldbm, lastkey ) )
737 /* see if it's in the old one */
738 data = ldbm_fetch( oldbm, key );
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 );
747 ldbm_datum_free( oldbm, data );
749 if ( lastkey.dptr != NULL ) {
750 ldbm_datum_free( nldbm, lastkey );
754 if ( lastkey.dptr != NULL ) {
755 ldbm_datum_free( nldbm, lastkey );
758 /* generate list of values to delete */
760 #ifdef HAVE_BERKELEY_DB2
761 for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
762 key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
764 for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
765 key = ldbm_nextkey( oldbm, lastkey ) )
768 /* see if it's in the new one */
769 data = ldbm_fetch( nldbm, key );
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 );
778 ldbm_datum_free( nldbm, data );
780 if ( lastkey.dptr != NULL ) {
781 ldbm_datum_free( oldbm, lastkey );
785 if ( lastkey.dptr != NULL ) {
786 ldbm_datum_free( oldbm, lastkey );
790 mods[0]->mod_values = avals;
791 mods[1]->mod_values = dvals;
798 if ( mods[1]->mod_values == NULL ) {
799 free( (char *) mods[1] );
802 if ( mods[0]->mod_values == NULL ) {
803 free( (char *) mods[0] );
807 if ( mods[0] == NULL ) {
808 free( (char *) mods );
827 #ifdef HAVE_BERKELEY_DB2
832 printf( "Generating mods for full %s centroid...", attr );
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 )
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 );
850 mods[1]->mod_op = LDAP_MOD_REPLACE;
851 mods[1]->mod_type = "nentries";
852 mods[1]->mod_values = vals;
858 #ifdef HAVE_BERKELEY_DB2
859 for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
860 key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
862 for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
863 key = ldbm_nextkey( ldbm, lastkey ) )
866 if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
867 ldap_mods_free( mods, 1 );
871 if ( lastkey.dptr != NULL ) {
872 ldbm_datum_free( ldbm, lastkey );
876 if ( lastkey.dptr != NULL ) {
877 ldbm_datum_free( ldbm, lastkey );
879 mods[0]->mod_values = vals;
886 if ( mods[0]->mod_values == NULL ) {
887 free( (char *) mods[0] );
888 free( (char *) mods );
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.
904 bind_to_destination_ldap(
913 char *attrs[2], *refvalues[2], *ocvalues[2];
919 printf( "Binding to destination LDAP server..." );
923 /* first, pick out the destination ldap server info */
924 if ( ldapbase != NULL ) {
927 if ( strncmp( ldapdesturl, "ldap://", 7 ) == 0 ) {
930 if ( (s2 = strchr( s, '/' )) == NULL ) {
931 ldapbase = strdup( s );
935 if ( ldaphost != NULL )
937 ldaphost = strdup( s );
940 ldapbase = strdup( s2 + 1 );
942 strcpy( buf, "ref=" );
943 if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
946 for ( s = d = ldapsrcurl; *s; s++ ) {
952 strcat( buf, ldapsrcurl );
953 if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
957 strcat( buf, ldapbase );
959 ldapbase = strdup( buf );
961 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
962 perror( "ldap_open" );
966 if ( ldap_bind_s( ld, destldapbinddn, destldappasswd, destldapauthmethod )
968 ldap_perror( ld, "ldap_bind_s" );
978 rc = ldap_search_s( ld, ldapbase, LDAP_SCOPE_BASE, "(objectclass=*)",
982 if ( rc == LDAP_NO_SUCH_OBJECT ) {
984 printf( "%sCreating centroid entry...", not ? "Not " : "" );
988 /* create the centroid index entry */
990 m[0].mod_type = "ref";
991 refvalues[0] = ldapsrcurl;
993 m[0].mod_values = refvalues;
995 m[1].mod_type = "objectclass";
996 ocvalues[0] = "indexentry";
998 m[1].mod_values = ocvalues;
1003 if ( !not && ldap_add_s( ld, ldapbase, mp ) != LDAP_SUCCESS ) {
1004 ldap_perror( ld, ldapbase );
1013 } else if ( rc != LDAP_SUCCESS ) {
1014 ldap_perror( ld, "ldap_search_s" );
1033 *a = (char **) malloc( (BUFSIZ + 1) * sizeof(char *) );
1036 } else if ( *cur >= *max ) {
1038 *a = (char **) realloc( *a, (*max + 1) * sizeof(char *) );
1044 (*a)[(*cur)++] = strdup( s );