1 /* centipede.c - generate and install indexing information (view w/tabstop=4) */
11 #include <ldapconfig.h>
14 #define DEFAULT_LDAPFILTER "(objectclass=*)"
16 #define CENTROID_VALUE 1
17 #define CENTROID_WORD 2
19 #define CENTROID_RELATIVE 1
20 #define CENTROID_FULL 2
22 #define WORD_BREAKS " -',.()!;:&$%*\"/\\+_<>=?[]|^~"
35 int srcldapauthmethod;
36 int destldapauthmethod;
40 static LDAP *start_ldap_search();
41 static LDAP *bind_to_destination_ldap();
42 static int create_tmp_files();
43 static int generate_new_centroids();
44 static LDAPMod **diff_centroids();
45 static LDAPMod **full_centroid();
46 static char **charray_add_dup();
48 static void usage( char *name )
50 fprintf( stderr, "usage: %s [options] -s url -d url attributes\n", name );
51 fprintf( stderr, "where:\n" );
52 fprintf( stderr, "\t-s url\t\t[[ldap://][host[:port]]/]searchbasedn\n");
53 fprintf( stderr, "\t-d url\t\t[[ldap://][host[:port]]/]centroidentrydn\n");
54 fprintf( stderr, "options:\n" );
55 fprintf( stderr, "\t-v \t\tturn on verbose mode\n" );
56 fprintf( stderr, "\t-n \t\tgenerate, but do not install index info\n" );
57 fprintf( stderr, "\t-f filter\tentry selection filter\n" );
58 fprintf( stderr, "\t-F \t\tgenerate a full centroid\n" );
59 fprintf( stderr, "\t-R \t\tgenerate a relative centroid\n" );
60 fprintf( stderr, "\t-w \t\tgenerate a word-based centroid\n" );
61 fprintf( stderr, "\t-t directory\tcentroid directory\n" );
62 fprintf( stderr, "\t-b binddn\tsource bind dn\n" );
63 fprintf( stderr, "\t-p passwd\tsource bind passwd (for simple auth)\n" );
64 fprintf( stderr, "\t-m authmethod\tsource authmethod \"simple\" or \"kerberos\"\n" );
65 fprintf( stderr, "\t-B binddn\tdestination bind dn\n" );
66 fprintf( stderr, "\t-P passwd\tdestination bind passwd (for simple auth)\n" );
67 fprintf( stderr, "\t-M authmethod\tdestination authmethod \"simple\" or \"kerberos\"\n" );
68 fprintf( stderr, "\t-c size\t\tldbm cache size\n" );
71 main( int argc, char **argv )
73 char *ldapfilter, *ldapref;
74 char *ldapsrcurl, *ldapdesturl;
90 ldapbase = DEFAULT_BASE;
91 srcldapauthmethod = LDAP_AUTH_SIMPLE;
92 destldapauthmethod = LDAP_AUTH_SIMPLE;
95 destldapbinddn = NULL;
96 destldappasswd = NULL;
97 ldapfilter = DEFAULT_LDAPFILTER;
98 centroidvalues = CENTROID_VALUE;
99 centroidtype = CENTROID_RELATIVE;
104 while ( (i = getopt( argc, argv, "s:d:c:b:B:f:FRWp:P:m:M:t:vwn" ))
107 case 's': /* source url [[ldap://][host[:port]]/]basedn */
108 ldapsrcurl = strdup( optarg );
111 case 'd': /* destination url [[ldap://][host[:port]]/]entrydn */
112 ldapdesturl = strdup( optarg );
115 case 'f': /* specify a filter */
116 ldapfilter = strdup( optarg );
119 case 'F': /* generate full centroid */
120 centroidtype = CENTROID_FULL;
123 case 'R': /* generate relative centroid */
124 centroidtype = CENTROID_RELATIVE;
127 case 'w': /* generate word centroid */
128 centroidvalues = CENTROID_WORD;
131 case 'W': /* generate weights */
135 case 't': /* temp file directory */
136 centdir = strdup( optarg );
139 case 'b': /* src bind dn */
140 srcldapbinddn = strdup( optarg );
143 case 'p': /* src bind password */
144 srcldappasswd = strdup( optarg );
149 case 'B': /* dest bind dn */
150 destldapbinddn = strdup( optarg );
153 case 'P': /* dest bind password */
154 destldappasswd = strdup( optarg );
159 case 'm': /* src bind method */
160 if ( strcasecmp( optarg, "simple" ) == 0 ) {
161 srcldapauthmethod = LDAP_AUTH_SIMPLE;
162 } else if ( strcasecmp( optarg, "kerberos" ) == 0 ) {
163 srcldapauthmethod = LDAP_AUTH_KRBV4;
165 fprintf( stderr, "%s: unknown auth method\n", optarg );
166 fprintf( stderr, "expecting \"simple\" or \"kerberos\"\n",
172 case 'M': /* dest bind method */
173 if ( strcasecmp( optarg, "simple" ) == 0 ) {
174 destldapauthmethod = LDAP_AUTH_SIMPLE;
175 } else if ( strcasecmp( optarg, "kerberos" ) == 0 ) {
176 destldapauthmethod = LDAP_AUTH_KRBV4;
178 fprintf( stderr, "%s: unknown auth method\n", optarg );
179 fprintf( stderr, "expecting \"simple\" or \"kerberos\"\n",
185 case 'c': /* ldbm cache size */
186 ldbmcachesize = atoi( optarg );
189 case 'v': /* turn on verbose mode */
193 case 'n': /* don't actually install index info */
202 if ( optind == argc || ldapsrcurl == NULL || ldapdesturl == NULL ) {
206 attrs = &argv[optind];
209 * open the ldap connection and start searching for the entries
210 * we will use to generate the centroids.
213 if ( (ld = start_ldap_search( ldapsrcurl, ldapfilter, attrs )) == NULL ) {
214 fprintf( stderr, "could not initiate ldap search\n" );
218 if ( create_tmp_files( attrs, &tmpfile, &ldbm ) != 0 ) {
219 fprintf( stderr, "could not create temp files\n" );
224 * go through the entries returned, building a centroid for each
225 * attribute as we go.
228 if ( (count = generate_new_centroids( ld, attrs, ldbm )) < 1 ) {
230 fprintf( stderr, "no entries matched\n" );
233 fprintf( stderr, "could not generate new centroid\n" );
239 * for each centroid we generated above, compare to the existing
240 * centroid, if any, and produce adds and deletes, or produce
241 * an entirely new centroid. in either case, update the "current"
242 * centroid version with the new one we just generated.
245 if ( (ld = bind_to_destination_ldap( ldapsrcurl, ldapdesturl )) == NULL ) {
247 "could not bind to index server, or could not create index entry\n" );
251 for ( i = 0; ldbm[i] != NULL; i++ ) {
252 /* generate the name of the existing centroid, if any */
253 s = strrchr( tmpfile[i], '/' );
255 sprintf( buf, "%s/cent.%s", tmpfile[i], attrs[i] );
258 /* generate the full centroid changes */
259 if ( centroidtype == CENTROID_FULL || (oldbm = ldbm_open( buf,
260 LDBM_WRITER, 0, ldbmcachesize )) == NULL ) {
261 if ( (mods = full_centroid( attrs[i], ldbm[i], count )) == NULL ) {
262 fprintf( stderr, "could not produce full centroid for %s\n",
267 /* generate the differential centroid changes */
269 if ( (mods = diff_centroids( attrs[i], oldbm, ldbm[i], count ))
271 fprintf( stderr, "could not diff centroids\n" );
279 printf("changes:\n");
280 for ( j = 0; mods[j] != NULL; j++ ) {
281 switch( mods[j]->mod_op ) {
283 printf( "\tadd: %s\n",mods[j]->mod_type );
285 case LDAP_MOD_DELETE:
286 printf( "\tdelete: %s\n",mods[j]->mod_type );
288 case LDAP_MOD_REPLACE:
289 printf( "\treplace: %s\n",mods[j]->mod_type );
292 if ( mods[j]->mod_values != NULL ) {
293 for ( k = 0; mods[j]->mod_values[k] != NULL; k++ ) {
294 printf( "\t\t%s\n", mods[j]->mod_values[k] );
298 printf("end changes:\n");
302 printf( "%sModifying centroid...", not ? "Not " : "" );
306 /* attempt to make the changes to the index server entry */
307 if ( !not && ldap_modify_s( ld, ldapbase, mods ) != LDAP_SUCCESS ) {
308 fprintf( stderr, "could not apply centroid modification for %s\n",
310 ldap_perror( ld, ldapbase );
312 ldap_mods_free( mods, 1 );
319 /* move the new centroid into the old one's place */
321 (void) unlink( buf );
322 if ( link( tmpfile[i], buf ) != 0 ) {
324 fprintf( stderr, "could not rename %s to %s\n", buf,
329 (void) unlink( tmpfile[i] );
333 for ( i = 0; attrs[i] != NULL; i++ ) {
334 ldbm_close( ldbm[i] );
344 * open an ldap connection, bind, and initiate the search
358 if ( strncmp( ldapsrcurl, "ldap://", 7 ) == 0 ) {
361 if ( (s2 = strchr( s, '/' )) == NULL ) {
362 ldapbase = strdup( s );
366 ldaphost = strdup( s );
369 ldapbase = strdup( s2 + 1 );
373 printf( "Base: %s\n", ldapbase );
374 printf( "Attributes:" );
375 for ( i = 0; attrs[i] != NULL; i++ ) {
376 printf( " %s", attrs[i] );
379 printf( "Binding to source LDAP server..." );
383 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
384 perror( "ldap_open" );
388 if ( ldap_bind_s( ld, srcldapbinddn, srcldappasswd, srcldapauthmethod )
390 ldap_perror( ld, "ldap_bind_s" );
395 printf( "\nInitiating search..." );
396 if ( ldap_search( ld, ldapbase, LDAP_SCOPE_SUBTREE, ldapfilter, attrs, 0 )
398 ldap_perror( ld, "ldap_search" );
411 * create the temporary ldbm files we will use to hold the new centroids
423 for ( i = 0; attrs[i] != NULL; i++ )
427 if ( (*tmpfile = (char **) malloc( i * sizeof(char *) )) == NULL ) {
431 if ( (*ldbm = (LDBM *) malloc( i * sizeof(LDBM) )) == NULL ) {
435 for ( i = 0; attrs[i] != NULL; i++ ) {
436 if ( ((*tmpfile)[i] = tempnam( centdir, NULL )) == NULL ) {
441 if ( ((*ldbm)[i] = ldbm_open( (*tmpfile)[i], LDBM_WRCREAT, 0600,
442 ldbmcachesize )) == NULL ) {
443 fprintf( stderr, "ldbm_open of \"%s\" failed\n", (*tmpfile)[i] );
444 perror( "ldbm_open" );
448 (*tmpfile)[i] = NULL;
455 * step through each entry returned from the search and generate
456 * the appropriate centroid values.
460 generate_new_centroids(
468 LDAPMessage *res, *e;
474 printf( "Generating new centroids for..." );
481 while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ))
482 == LDAP_RES_SEARCH_ENTRY ) {
484 e = ldap_first_entry( ld, res );
485 dn = ldap_get_dn( ld, e );
487 /* for each attr we want to generate a centroid for */
488 for ( i = 0; attrs[i] != NULL; i++ ) {
489 if ( (val = ldap_get_values( ld, e, attrs[i] )) == NULL ) {
494 for ( j = 0; val[j] != NULL; j++ ) {
495 /* normalize the value */
496 for ( s = val[j]; *s; s++ ) {
497 if ( isascii( *s ) ) {
502 if ( isascii( last ) && isdigit( last ) ) {
506 /* generate a value-based centroid */
507 if ( centroidvalues == CENTROID_VALUE ) {
509 key.dsize = strlen( key.dptr ) + 1;
510 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
512 /* generate a word-based centroid */
514 for ( w = strtok( val[j], WORD_BREAKS ); w != NULL;
515 w = strtok( NULL, WORD_BREAKS ) ) {
517 key.dsize = strlen( key.dptr ) + 1;
518 (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
522 ldap_value_free( val );
531 printf( "%d entries\n", count );
538 * compare the old and new centroids, generating the appropriate add
539 * and delete operations. if the underlying database is ordered, we
540 * can do this more efficiently.
557 char **avals, **dvals;
558 int amax, acur, dmax, dcur;
564 #endif /* LDBM_USE_DB2 */
567 printf( "Generating mods for differential %s centroid...", attr );
571 if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 4 )) == NULL ||
572 (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
573 (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
574 (mods[2] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
575 (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
576 (vals[0] = (char *) malloc( 20 )) == NULL )
581 /* add values in mods[0] */
582 mods[0]->mod_op = LDAP_MOD_ADD;
583 mods[0]->mod_type = attr;
584 mods[0]->mod_values = NULL;
587 /* delete values in mods[1] */
588 mods[1]->mod_op = LDAP_MOD_DELETE;
589 mods[1]->mod_type = attr;
590 mods[1]->mod_values = NULL;
593 /* number of entries in mods[2] */
594 sprintf( vals[0], "%d", nentries );
596 mods[2]->mod_op = LDAP_MOD_REPLACE;
597 mods[2]->mod_type = "nentries";
598 mods[2]->mod_values = vals;
599 /* null terminate list of mods */
604 * if the underlying database is ordered, we can do a more efficient
605 * dual traversal, yielding O(N) performance.
611 for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
612 nkey = ldbm_firstkey( nldbm, &ncursorp );
613 okey.dptr != NULL && nkey.dptr != NULL; )
615 for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
616 okey.dptr != NULL && nkey.dptr != NULL; )
619 rc = strcmp( okey.dptr, nkey.dptr );
622 /* value is in both places - leave it */
623 if ( olast.dptr != NULL ) {
624 ldbm_datum_free( oldbm, olast );
627 if ( nlast.dptr != NULL ) {
628 ldbm_datum_free( nldbm, nlast );
633 okey = ldbm_nextkey( oldbm, olast, ocursorp );
634 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
636 okey = ldbm_nextkey( oldbm, olast );
637 nkey = ldbm_nextkey( nldbm, nlast );
639 } else if ( rc > 0 ) {
640 /* new value is not in old centroid - add it */
641 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
642 ldap_mods_free( mods, 1 );
646 if ( nlast.dptr != NULL ) {
647 ldbm_datum_free( nldbm, nlast );
652 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
654 nkey = ldbm_nextkey( nldbm, nlast );
657 /* old value is not in new centroid - delete it */
658 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
659 ldap_mods_free( mods, 1 );
663 if ( olast.dptr != NULL ) {
664 ldbm_datum_free( oldbm, olast );
669 okey = ldbm_nextkey( oldbm, olast, ocursorp );
671 okey = ldbm_nextkey( oldbm, olast );
676 while ( okey.dptr != NULL ) {
677 if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
678 ldap_mods_free( mods, 1 );
683 okey = ldbm_nextkey( oldbm, olast, ocursorp );
685 okey = ldbm_nextkey( oldbm, olast );
687 if ( olast.dptr != NULL ) {
688 ldbm_datum_free( oldbm, olast );
692 if ( olast.dptr != NULL ) {
693 ldbm_datum_free( oldbm, olast );
695 while ( nkey.dptr != NULL ) {
696 if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
697 ldap_mods_free( mods, 1 );
702 nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
704 nkey = ldbm_nextkey( nldbm, nlast );
706 if ( nlast.dptr != NULL ) {
707 ldbm_datum_free( nldbm, nlast );
711 if ( nlast.dptr != NULL ) {
712 ldbm_datum_free( nldbm, nlast );
716 * if the underlying database is not ordered, we have to
717 * generate list of values to add by stepping through all new
718 * values and looking them up in the old centroid (not there => add),
719 * then stepping through all old values and looking them up in the
720 * new centroid (not there => delete). this yields O(Nf(N)) performance,
721 * where f(N) is the order to retrieve a single item.
724 /* generate list of values to add */
727 for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
728 key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
730 for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
731 key = ldbm_nextkey( nldbm, lastkey ) )
734 /* see if it's in the old one */
735 data = ldbm_fetch( oldbm, key );
737 /* not there - add it */
738 if ( data.dptr == NULL ) {
739 if ( charray_add_dup( &avals, &acur, &amax, key.dptr ) == NULL ) {
740 ldap_mods_free( mods, 1 );
744 ldbm_datum_free( oldbm, data );
746 if ( lastkey.dptr != NULL ) {
747 ldbm_datum_free( nldbm, lastkey );
751 if ( lastkey.dptr != NULL ) {
752 ldbm_datum_free( nldbm, lastkey );
755 /* generate list of values to delete */
758 for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
759 key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
761 for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
762 key = ldbm_nextkey( oldbm, lastkey ) )
765 /* see if it's in the new one */
766 data = ldbm_fetch( nldbm, key );
768 /* not there - delete it */
769 if ( data.dptr == NULL ) {
770 if ( charray_add_dup( &dvals, &dcur, &dmax, key.dptr ) == NULL ) {
771 ldap_mods_free( mods, 1 );
775 ldbm_datum_free( nldbm, data );
777 if ( lastkey.dptr != NULL ) {
778 ldbm_datum_free( oldbm, lastkey );
782 if ( lastkey.dptr != NULL ) {
783 ldbm_datum_free( oldbm, lastkey );
787 mods[0]->mod_values = avals;
788 mods[1]->mod_values = dvals;
795 if ( mods[1]->mod_values == NULL ) {
796 free( (char *) mods[1] );
799 if ( mods[0]->mod_values == NULL ) {
800 free( (char *) mods[0] );
804 if ( mods[0] == NULL ) {
805 free( (char *) mods );
829 printf( "Generating mods for full %s centroid...", attr );
833 if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 3 )) == NULL ||
834 (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
835 (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
836 (vals = (char **) malloc( 2 * sizeof(char *) )) == NULL ||
837 (vals[0] = (char *) malloc( 20 )) == NULL )
842 mods[0]->mod_op = LDAP_MOD_REPLACE;
843 mods[0]->mod_type = attr;
844 mods[0]->mod_values = NULL;
845 sprintf( vals[0], "%d", nentries );
847 mods[1]->mod_op = LDAP_MOD_REPLACE;
848 mods[1]->mod_type = "nentries";
849 mods[1]->mod_values = vals;
856 for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
857 key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
859 for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
860 key = ldbm_nextkey( ldbm, lastkey ) )
863 if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
864 ldap_mods_free( mods, 1 );
868 if ( lastkey.dptr != NULL ) {
869 ldbm_datum_free( ldbm, lastkey );
873 if ( lastkey.dptr != NULL ) {
874 ldbm_datum_free( ldbm, lastkey );
876 mods[0]->mod_values = vals;
883 if ( mods[0]->mod_values == NULL ) {
884 free( (char *) mods[0] );
885 free( (char *) mods );
893 * extract the destination ldap host, port, and base object for the
894 * server to receive the index information. then, open a connection,
895 * bind, and see if the entry exists. if not, create it and set things
896 * up so the centroid full and diff routines can modify it to contain
897 * the new centroid information.
901 bind_to_destination_ldap(
910 char *attrs[2], *refvalues[2], *ocvalues[2];
916 printf( "Binding to destination LDAP server..." );
920 /* first, pick out the destination ldap server info */
921 if ( ldapbase != NULL ) {
924 if ( strncmp( ldapdesturl, "ldap://", 7 ) == 0 ) {
927 if ( (s2 = strchr( s, '/' )) == NULL ) {
928 ldapbase = strdup( s );
932 if ( ldaphost != NULL )
934 ldaphost = strdup( s );
937 ldapbase = strdup( s2 + 1 );
939 strcpy( buf, "ref=" );
940 if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
943 for ( s = d = ldapsrcurl; *s; s++ ) {
949 strcat( buf, ldapsrcurl );
950 if ( strpbrk( ldapsrcurl, " ,;" ) != NULL ) {
954 strcat( buf, ldapbase );
956 ldapbase = strdup( buf );
958 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
959 perror( "ldap_open" );
963 if ( ldap_bind_s( ld, destldapbinddn, destldappasswd, destldapauthmethod )
965 ldap_perror( ld, "ldap_bind_s" );
975 rc = ldap_search_s( ld, ldapbase, LDAP_SCOPE_BASE, "(objectclass=*)",
979 if ( rc == LDAP_NO_SUCH_OBJECT ) {
981 printf( "%sCreating centroid entry...", not ? "Not " : "" );
985 /* create the centroid index entry */
987 m[0].mod_type = "ref";
988 refvalues[0] = ldapsrcurl;
990 m[0].mod_values = refvalues;
992 m[1].mod_type = "objectclass";
993 ocvalues[0] = "indexentry";
995 m[1].mod_values = ocvalues;
1000 if ( !not && ldap_add_s( ld, ldapbase, mp ) != LDAP_SUCCESS ) {
1001 ldap_perror( ld, ldapbase );
1010 } else if ( rc != LDAP_SUCCESS ) {
1011 ldap_perror( ld, "ldap_search_s" );
1030 *a = (char **) malloc( (BUFSIZ + 1) * sizeof(char *) );
1033 } else if ( *cur >= *max ) {
1035 *a = (char **) realloc( *a, (*max + 1) * sizeof(char *) );
1041 (*a)[(*cur)++] = strdup( s );