1 /* centipede.c - generate and install indexing information (view w/tabstop=4) */
15 #include <ldapconfig.h>
18 #define DEFAULT_LDAPFILTER "(objectclass=*)"
20 #define CENTROID_VALUE 1
21 #define CENTROID_WORD 2
23 #define CENTROID_RELATIVE 1
24 #define CENTROID_FULL 2
26 #define WORD_BREAKS " -',.()!;:&$%*\"/\\+_<>=?[]|^~"
39 int srcldapauthmethod;
40 int destldapauthmethod;
44 static LDAP *start_ldap_search();
45 static LDAP *bind_to_destination_ldap();
46 static int create_tmp_files();
47 static int generate_new_centroids();
48 static LDAPMod **diff_centroids();
49 static LDAPMod **full_centroid();
50 static char **charray_add_dup();
52 static void usage( char *name )
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" );
75 main( int argc, char **argv )
78 char *ldapsrcurl, *ldapdesturl;
94 ldapbase = DEFAULT_BASE;
95 srcldapauthmethod = LDAP_AUTH_SIMPLE;
96 destldapauthmethod = LDAP_AUTH_SIMPLE;
99 destldapbinddn = NULL;
100 destldappasswd = NULL;
101 ldapfilter = DEFAULT_LDAPFILTER;
102 centroidvalues = CENTROID_VALUE;
103 centroidtype = CENTROID_RELATIVE;
108 while ( (i = getopt( argc, argv, "s:d:c:b:B:f:FRWp:P:m:M:t:vwn" ))
111 case 's': /* source url [[ldap://][host[:port]]/]basedn */
112 ldapsrcurl = strdup( optarg );
115 case 'd': /* destination url [[ldap://][host[:port]]/]entrydn */
116 ldapdesturl = strdup( optarg );
119 case 'f': /* specify a filter */
120 ldapfilter = strdup( optarg );
123 case 'F': /* generate full centroid */
124 centroidtype = CENTROID_FULL;
127 case 'R': /* generate relative centroid */
128 centroidtype = CENTROID_RELATIVE;
131 case 'w': /* generate word centroid */
132 centroidvalues = CENTROID_WORD;
135 case 'W': /* generate weights */
139 case 't': /* temp file directory */
140 centdir = strdup( optarg );
143 case 'b': /* src bind dn */
144 srcldapbinddn = strdup( optarg );
147 case 'p': /* src bind password */
148 srcldappasswd = strdup( optarg );
153 case 'B': /* dest bind dn */
154 destldapbinddn = strdup( optarg );
157 case 'P': /* dest bind password */
158 destldappasswd = strdup( optarg );
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;
169 fprintf( stderr, "%s: unknown auth method\n", optarg );
170 fputs( "expecting \"simple\" or \"kerberos\"\n", stderr );
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 fputs( "expecting \"simple\" or \"kerberos\"\n", stderr );
187 case 'c': /* ldbm cache size */
188 ldbmcachesize = atoi( optarg );
191 case 'v': /* turn on verbose mode */
195 case 'n': /* don't actually install index info */
204 if ( optind == argc || ldapsrcurl == NULL || ldapdesturl == NULL ) {
208 attrs = &argv[optind];
211 * open the ldap connection and start searching for the entries
212 * we will use to generate the centroids.
215 if ( (ld = start_ldap_search( ldapsrcurl, ldapfilter, attrs )) == NULL ) {
216 fprintf( stderr, "could not initiate ldap search\n" );
220 if ( create_tmp_files( attrs, &tmpfile, &ldbm ) != 0 ) {
221 fprintf( stderr, "could not create temp files\n" );
226 * go through the entries returned, building a centroid for each
227 * attribute as we go.
230 if ( (count = generate_new_centroids( ld, attrs, ldbm )) < 1 ) {
232 fprintf( stderr, "no entries matched\n" );
235 fprintf( stderr, "could not generate new centroid\n" );
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.
247 if ( (ld = bind_to_destination_ldap( ldapsrcurl, ldapdesturl )) == NULL ) {
249 "could not bind to index server, or could not create index entry\n" );
253 for ( i = 0; ldbm[i] != NULL; i++ ) {
254 /* generate the name of the existing centroid, if any */
255 s = strrchr( tmpfile[i], '/' );
257 sprintf( buf, "%s/cent.%s", tmpfile[i], attrs[i] );
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",
269 /* generate the differential centroid changes */
271 if ( (mods = diff_centroids( attrs[i], oldbm, ldbm[i], count ))
273 fprintf( stderr, "could not diff centroids\n" );
281 printf("changes:\n");
282 for ( j = 0; mods[j] != NULL; j++ ) {
283 switch( mods[j]->mod_op ) {
285 printf( "\tadd: %s\n",mods[j]->mod_type );
287 case LDAP_MOD_DELETE:
288 printf( "\tdelete: %s\n",mods[j]->mod_type );
290 case LDAP_MOD_REPLACE:
291 printf( "\treplace: %s\n",mods[j]->mod_type );
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] );
300 printf("end changes:\n");
304 printf( "%sModifying centroid...", not ? "Not " : "" );
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",
312 ldap_perror( ld, ldapbase );
314 ldap_mods_free( mods, 1 );
321 /* move the new centroid into the old one's place */
323 (void) unlink( buf );
324 if ( link( tmpfile[i], buf ) != 0 ) {
326 fprintf( stderr, "could not rename %s to %s\n", buf,
331 (void) unlink( tmpfile[i] );
335 for ( i = 0; attrs[i] != NULL; i++ ) {
336 ldbm_close( ldbm[i] );
346 * open an ldap connection, bind, and initiate the search
360 if ( strncmp( ldapsrcurl, "ldap://", 7 ) != 0 ) {
361 fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
365 if ( (s2 = strchr( s, '/' )) == NULL ) {
366 ldapbase = strdup( s );
370 ldaphost = strdup( s );
373 ldapbase = strdup( s2 + 1 );
377 printf( "Base: %s\n", ldapbase );
378 printf( "Attributes:" );
379 for ( i = 0; attrs[i] != NULL; i++ ) {
380 printf( " %s", attrs[i] );
383 printf( "Binding to source LDAP server..." );
387 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
388 perror( "ldap_open" );
392 if ( ldap_bind_s( ld, srcldapbinddn, srcldappasswd, srcldapauthmethod )
394 ldap_perror( ld, "ldap_bind_s" );
399 printf( "\nInitiating search..." );
400 if ( ldap_search( ld, ldapbase, LDAP_SCOPE_SUBTREE, ldapfilter, attrs, 0 )
402 ldap_perror( ld, "ldap_search" );
415 * create the temporary ldbm files we will use to hold the new centroids
427 for ( i = 0; attrs[i] != NULL; i++ )
431 if ( (*tmpfile = (char **) malloc( i * sizeof(char *) )) == NULL ) {
435 if ( (*ldbm = (LDBM *) malloc( i * sizeof(LDBM) )) == NULL ) {
439 for ( i = 0; attrs[i] != NULL; i++ ) {
440 if ( ((*tmpfile)[i] = tempnam( centdir, NULL )) == NULL ) {
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" );
452 (*tmpfile)[i] = NULL;
459 * step through each entry returned from the search and generate
460 * the appropriate centroid values.
464 generate_new_centroids(
472 LDAPMessage *res, *e;
478 printf( "Generating new centroids for..." );
485 while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ))
486 == LDAP_RES_SEARCH_ENTRY ) {
488 e = ldap_first_entry( ld, res );
489 dn = ldap_get_dn( ld, e );
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 ) {
498 for ( j = 0; val[j] != NULL; j++ ) {
499 /* normalize the value */
500 for ( s = val[j]; *s; s++ ) {
501 if ( isascii( *s ) ) {
506 if ( isascii( last ) && isdigit( last ) ) {
510 /* generate a value-based centroid */
511 if ( centroidvalues == CENTROID_VALUE ) {
513 key.dsize = strlen( key.dptr ) + 1;
514 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
516 /* generate a word-based centroid */
518 for ( w = strtok( val[j], WORD_BREAKS ); w != NULL;
519 w = strtok( NULL, WORD_BREAKS ) ) {
521 key.dsize = strlen( key.dptr ) + 1;
522 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
526 ldap_value_free( val );
535 printf( "%d entries\n", count );
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.
561 char **avals, **dvals;
562 int amax, acur, dmax, dcur;
565 #ifdef HAVE_BERKELEY_DB2
568 #endif /* HAVE_BERKELEY_DB2 */
571 printf( "Generating mods for differential %s centroid...", attr );
575 if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 4 )) == NULL ||
576 (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
577 (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
578 (mods[2] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
579 (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
580 (vals[0] = (char *) malloc( 20 )) == NULL )
585 /* add values in mods[0] */
586 mods[0]->mod_op = LDAP_MOD_ADD;
587 mods[0]->mod_type = attr;
588 mods[0]->mod_values = NULL;
591 /* delete values in mods[1] */
592 mods[1]->mod_op = LDAP_MOD_DELETE;
593 mods[1]->mod_type = attr;
594 mods[1]->mod_values = NULL;
597 /* number of entries in mods[2] */
598 sprintf( vals[0], "%d", nentries );
600 mods[2]->mod_op = LDAP_MOD_REPLACE;
601 mods[2]->mod_type = "nentries";
602 mods[2]->mod_values = vals;
603 /* null terminate list of mods */
608 * if the underlying database is ordered, we can do a more efficient
609 * dual traversal, yielding O(N) performance.
614 #ifdef HAVE_BERKELEY_DB2
615 for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
616 nkey = ldbm_firstkey( nldbm, &ncursorp );
617 okey.dptr != NULL && nkey.dptr != NULL; )
619 for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
620 okey.dptr != NULL && nkey.dptr != NULL; )
623 rc = strcmp( okey.dptr, nkey.dptr );
626 /* value is in both places - leave it */
627 if ( olast.dptr != NULL ) {
628 ldbm_datum_free( oldbm, olast );
631 if ( nlast.dptr != NULL ) {
632 ldbm_datum_free( nldbm, nlast );
636 #ifdef HAVE_BERKELEY_DB2
637 okey = ldbm_nextkey( oldbm, olast, ocursorp );
638 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
640 okey = ldbm_nextkey( oldbm, olast );
641 nkey = ldbm_nextkey( nldbm, nlast );
643 } else if ( rc > 0 ) {
644 /* new value is not in old centroid - add it */
645 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
646 ldap_mods_free( mods, 1 );
650 if ( nlast.dptr != NULL ) {
651 ldbm_datum_free( nldbm, nlast );
655 #ifdef HAVE_BERKELEY_DB2
656 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
658 nkey = ldbm_nextkey( nldbm, nlast );
661 /* old value is not in new centroid - delete it */
662 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
663 ldap_mods_free( mods, 1 );
667 if ( olast.dptr != NULL ) {
668 ldbm_datum_free( oldbm, olast );
672 #ifdef HAVE_BERKELEY_DB2
673 okey = ldbm_nextkey( oldbm, olast, ocursorp );
675 okey = ldbm_nextkey( oldbm, olast );
680 while ( okey.dptr != NULL ) {
681 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
682 ldap_mods_free( mods, 1 );
686 #ifdef HAVE_BERKELEY_DB2
687 okey = ldbm_nextkey( oldbm, olast, ocursorp );
689 okey = ldbm_nextkey( oldbm, olast );
691 if ( olast.dptr != NULL ) {
692 ldbm_datum_free( oldbm, olast );
696 if ( olast.dptr != NULL ) {
697 ldbm_datum_free( oldbm, olast );
699 while ( nkey.dptr != NULL ) {
700 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
701 ldap_mods_free( mods, 1 );
705 #ifdef HAVE_BERKELEY_DB2
706 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
708 nkey = ldbm_nextkey( nldbm, nlast );
710 if ( nlast.dptr != NULL ) {
711 ldbm_datum_free( nldbm, nlast );
715 if ( nlast.dptr != NULL ) {
716 ldbm_datum_free( nldbm, nlast );
720 * if the underlying database is not ordered, we have to
721 * generate list of values to add by stepping through all new
722 * values and looking them up in the old centroid (not there => add),
723 * then stepping through all old values and looking them up in the
724 * new centroid (not there => delete). this yields O(Nf(N)) performance,
725 * where f(N) is the order to retrieve a single item.
728 /* generate list of values to add */
730 #ifdef HAVE_BERKELEY_DB2
731 for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
732 key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
734 for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
735 key = ldbm_nextkey( nldbm, lastkey ) )
738 /* see if it's in the old one */
739 data = ldbm_fetch( oldbm, key );
741 /* not there - add it */
742 if ( data.dptr == NULL ) {
743 if ( charray_add_dup( &avals, &acur, &amax, key.dptr ) == NULL ) {
744 ldap_mods_free( mods, 1 );
748 ldbm_datum_free( oldbm, data );
750 if ( lastkey.dptr != NULL ) {
751 ldbm_datum_free( nldbm, lastkey );
755 if ( lastkey.dptr != NULL ) {
756 ldbm_datum_free( nldbm, lastkey );
759 /* generate list of values to delete */
761 #ifdef HAVE_BERKELEY_DB2
762 for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
763 key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
765 for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
766 key = ldbm_nextkey( oldbm, lastkey ) )
769 /* see if it's in the new one */
770 data = ldbm_fetch( nldbm, key );
772 /* not there - delete it */
773 if ( data.dptr == NULL ) {
774 if ( charray_add_dup( &dvals, &dcur, &dmax, key.dptr ) == NULL ) {
775 ldap_mods_free( mods, 1 );
779 ldbm_datum_free( nldbm, data );
781 if ( lastkey.dptr != NULL ) {
782 ldbm_datum_free( oldbm, lastkey );
786 if ( lastkey.dptr != NULL ) {
787 ldbm_datum_free( oldbm, lastkey );
791 mods[0]->mod_values = avals;
792 mods[1]->mod_values = dvals;
799 if ( mods[1]->mod_values == NULL ) {
800 free( (char *) mods[1] );
803 if ( mods[0]->mod_values == NULL ) {
804 free( (char *) mods[0] );
808 if ( mods[0] == NULL ) {
809 free( (char *) mods );
828 #ifdef HAVE_BERKELEY_DB2
833 printf( "Generating mods for full %s centroid...", attr );
837 if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 3 )) == NULL ||
838 (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
839 (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
840 (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
841 (vals[0] = (char *) malloc( 20 )) == NULL )
846 mods[0]->mod_op = LDAP_MOD_REPLACE;
847 mods[0]->mod_type = attr;
848 mods[0]->mod_values = NULL;
849 sprintf( vals[0], "%d", nentries );
851 mods[1]->mod_op = LDAP_MOD_REPLACE;
852 mods[1]->mod_type = "nentries";
853 mods[1]->mod_values = vals;
859 #ifdef HAVE_BERKELEY_DB2
860 for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
861 key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
863 for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
864 key = ldbm_nextkey( ldbm, lastkey ) )
867 if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
868 ldap_mods_free( mods, 1 );
872 if ( lastkey.dptr != NULL ) {
873 ldbm_datum_free( ldbm, lastkey );
877 if ( lastkey.dptr != NULL ) {
878 ldbm_datum_free( ldbm, lastkey );
880 mods[0]->mod_values = vals;
887 if ( mods[0]->mod_values == NULL ) {
888 free( (char *) mods[0] );
889 free( (char *) mods );
897 * extract the destination ldap host, port, and base object for the
898 * server to receive the index information. then, open a connection,
899 * bind, and see if the entry exists. if not, create it and set things
900 * up so the centroid full and diff routines can modify it to contain
901 * the new centroid information.
905 bind_to_destination_ldap(
914 char *attrs[2], *refvalues[2], *ocvalues[2];
920 printf( "Binding to destination LDAP server..." );
924 /* first, pick out the destination ldap server info */
925 if ( ldapbase != NULL ) {
929 if ( strncmp( ldapdesturl, "ldap://", 7 ) != 0 ) {
930 fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
934 if ( (s2 = strchr( s, '/' )) == NULL ) {
935 ldapbase = strdup( s );
939 if ( ldaphost != NULL )
941 ldaphost = strdup( s );
944 ldapbase = strdup( s2 + 1 );
946 strcpy( buf, "ref=" );
947 if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
950 for ( s = d = ldapsrcurl; *s; s++ ) {
956 strcat( buf, ldapsrcurl );
957 if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
961 strcat( buf, ldapbase );
963 ldapbase = strdup( buf );
965 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
966 perror( "ldap_open" );
970 if ( ldap_bind_s( ld, destldapbinddn, destldappasswd, destldapauthmethod )
972 ldap_perror( ld, "ldap_bind_s" );
982 rc = ldap_search_s( ld, ldapbase, LDAP_SCOPE_BASE, "(objectclass=*)",
986 if ( rc == LDAP_NO_SUCH_OBJECT ) {
988 printf( "%sCreating centroid entry...", not ? "Not " : "" );
992 /* create the centroid index entry */
994 m[0].mod_type = "ref";
995 refvalues[0] = ldapsrcurl;
997 m[0].mod_values = refvalues;
999 m[1].mod_type = "objectclass";
1000 ocvalues[0] = "indexentry";
1002 m[1].mod_values = ocvalues;
1007 if ( !not && ldap_add_s( ld, ldapbase, mp ) != LDAP_SUCCESS ) {
1008 ldap_perror( ld, ldapbase );
1017 } else if ( rc != LDAP_SUCCESS ) {
1018 ldap_perror( ld, "ldap_search_s" );
1037 *a = (char **) malloc( (BUFSIZ + 1) * sizeof(char *) );
1040 } else if ( *cur >= *max ) {
1042 *a = (char **) realloc( *a, (*max + 1) * sizeof(char *) );
1048 (*a)[(*cur)++] = strdup( s );