]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/tools/centipede.c
Added connection initialisation and destruction notification. Now backends can regist...
[openldap] / servers / slapd / tools / centipede.c
index 9919232b7aa31fd9b5fcc039e5125d8ba13ea7ef..f3b36d72f3697e63890e1cf5e7265b7d22698e39 100644 (file)
@@ -1,12 +1,18 @@
 /* centipede.c - generate and install indexing information (view w/tabstop=4) */
 
+#include "portable.h"
+
 #include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <sys/time.h>
+#include <ac/stdlib.h>
+
+#include <ac/ctype.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>         /* get link(), unlink() */
+
 #include <lber.h>
 #include <ldap.h>
-#include <ldapconfig.h>
+
 #include <ldbm.h>
 
 #define DEFAULT_LDAPFILTER     "(objectclass=*)"
@@ -35,13 +41,13 @@ int         destldapauthmethod;
 int            verbose;
 int            not;
 
-static LDAP            *start_ldap_search();
-static LDAP            *bind_to_destination_ldap();
-static int             create_tmp_files();
-static int             generate_new_centroids();
-static LDAPMod **diff_centroids();
-static LDAPMod **full_centroid();
-static char            **charray_add_dup();
+static LDAP            *start_ldap_search(char *ldapsrcurl, char *ldapfilter, char **attrs);
+static LDAP            *bind_to_destination_ldap(char *ldapsrcurl, char *ldapdesturl);
+static int             create_tmp_files(char **attrs, char ***tmpfile, LDBM **ldbm);
+static int             generate_new_centroids(LDAP *ld, char **attrs, LDBM *ldbm);
+static LDAPMod **diff_centroids(char *attr, LDBM oldbm, LDBM nldbm, int nentries);
+static LDAPMod **full_centroid(char *attr, LDBM ldbm, int nentries);
+static char            **charray_add_dup(char ***a, int *cur, int *max, char *s);
 
 static void usage( char *name )
 {
@@ -66,9 +72,10 @@ static void usage( char *name )
        fprintf( stderr, "\t-c size\t\tldbm cache size\n" );
 }
 
+int
 main( int argc, char **argv )
 {
-       char            *ldapfilter, *ldapref;
+       char            *ldapfilter;
        char            *ldapsrcurl, *ldapdesturl;
        LDAP            *ld;
        LDAPMod         **mods;
@@ -79,13 +86,11 @@ main( int argc, char **argv )
        char            buf[BUFSIZ];
        int                     i, j, k, count;
        char            *s;
-       extern int      optind;
-       extern char     *optarg;
 
        ldapsrcurl = NULL;
        ldapdesturl = NULL;
-       ldaphost = LDAPHOST;
-       ldapbase = DEFAULT_BASE;
+       ldaphost = NULL;
+       ldapbase = NULL;
        srcldapauthmethod = LDAP_AUTH_SIMPLE;
        destldapauthmethod = LDAP_AUTH_SIMPLE;
        srcldapbinddn = NULL;
@@ -161,8 +166,7 @@ main( int argc, char **argv )
                                srcldapauthmethod = LDAP_AUTH_KRBV4;
                        } else {
                                fprintf( stderr, "%s: unknown auth method\n", optarg );
-                               fprintf( stderr, "expecting \"simple\" or \"kerberos\"\n",
-                                   optarg );
+                               fputs( "expecting \"simple\" or \"kerberos\"\n", stderr );
                                exit( 1 );
                        }
                        break;
@@ -174,8 +178,7 @@ main( int argc, char **argv )
                                destldapauthmethod = LDAP_AUTH_KRBV4;
                        } else {
                                fprintf( stderr, "%s: unknown auth method\n", optarg );
-                               fprintf( stderr, "expecting \"simple\" or \"kerberos\"\n",
-                                   optarg );
+                               fputs( "expecting \"simple\" or \"kerberos\"\n", stderr );
                                exit( 1 );
                        }
                        break;
@@ -353,9 +356,11 @@ start_ldap_search(
        char    *s, *s2;
        int             i;
 
-       if ( strncmp( ldapsrcurl, "ldap://", 7 ) == 0 ) {
-               s = ldapsrcurl + 7;
+       if ( strncmp( ldapsrcurl, "ldap://", 7 ) != 0 ) {
+               fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
+               return( NULL );
        }
+       s = ldapsrcurl + 7;
        if ( (s2 = strchr( s, '/' )) == NULL ) {
                ldapbase = strdup( s );
        } else {
@@ -378,8 +383,8 @@ start_ldap_search(
                fflush( stdout );
        }
 
-       if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
-               perror( "ldap_open" );
+       if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
+               perror( "ldap_init" );
                return( NULL );
        }
 
@@ -468,6 +473,8 @@ generate_new_centroids(
        char            **val;
        char            last;
 
+       ldbm_datum_init( data );
+
        if ( verbose ) {
                printf( "Generating new centroids for..." );
                fflush( stdout );
@@ -490,11 +497,12 @@ generate_new_centroids(
 
                        /* for each value */
                        for ( j = 0; val[j] != NULL; j++ ) {
+
+                               ldbm_datum_init( key );
+
                                /* normalize the value */
                                for ( s = val[j]; *s; s++ ) {
-                                       if ( isascii( *s ) ) {
-                                               *s = tolower( *s );
-                                       }
+                                       *s = TOLOWER( (unsigned char) *s );
                                        last = *s;
                                }
                                if ( isascii( last ) && isdigit( last ) ) {
@@ -509,8 +517,10 @@ generate_new_centroids(
 
                                /* generate a word-based centroid */
                                } else {
-                                       for ( w = strtok( val[j], WORD_BREAKS ); w != NULL;
-                                         w = strtok( NULL, WORD_BREAKS ) ) {
+                                       char *lasts;
+                                       for ( w = ldap_pvt_strtok( val[j], WORD_BREAKS, &lasts );
+                                         w != NULL;
+                                         w = ldap_pvt_strtok( NULL, WORD_BREAKS, &lasts ) ) {
                                                key.dptr = w;
                                                key.dsize = strlen( key.dptr ) + 1;
                                                (void) ldbm_store( ldbm[i], key, data, LDBM_INSERT );
@@ -550,17 +560,29 @@ diff_centroids(
        Datum   olast, nlast;
        Datum   lastkey, key;
        Datum   data;
-       int             rc;
        LDAPMod **mods;
        char    **avals, **dvals;
        int             amax, acur, dmax, dcur;
        char    **vals;
 
+#ifdef HAVE_BERKELEY_DB2
+       DBC     *ocursorp;
+       DBC     *ncursorp;
+#endif /* HAVE_BERKELEY_DB2 */
+
        if ( verbose ) {
                printf( "Generating mods for differential %s centroid...", attr );
                fflush( stdout );
        }
 
+       ldbm_datum_init( okey );
+       ldbm_datum_init( nkey );
+       ldbm_datum_init( olast );
+       ldbm_datum_init( nlast );
+       ldbm_datum_init( lastkey );
+       ldbm_datum_init( key );
+       ldbm_datum_init( data );
+
        if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 4 )) == NULL ||
             (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
             (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
@@ -600,10 +622,16 @@ diff_centroids(
 
        olast.dptr = NULL;
        nlast.dptr = NULL;
+#ifdef HAVE_BERKELEY_DB2
+       for ( okey = ldbm_firstkey( oldbm, &ocursorp ),
+                       nkey = ldbm_firstkey( nldbm, &ncursorp );
+             okey.dptr != NULL && nkey.dptr != NULL; )
+#else
        for ( okey = ldbm_firstkey( oldbm ), nkey = ldbm_firstkey( nldbm );
              okey.dptr != NULL && nkey.dptr != NULL; )
+#endif
        {
-               rc = strcmp( okey.dptr, nkey.dptr );
+               int     rc = strcmp( okey.dptr, nkey.dptr );
 
                if ( rc == 0 ) {
                        /* value is in both places - leave it */
@@ -616,8 +644,13 @@ diff_centroids(
                        }
                        nlast = nkey;
 
+#ifdef HAVE_BERKELEY_DB2
+                       okey = ldbm_nextkey( oldbm, olast, ocursorp );
+                       nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
+#else
                        okey = ldbm_nextkey( oldbm, olast );
                        nkey = ldbm_nextkey( nldbm, nlast );
+#endif
                } else if ( rc > 0 ) {
                        /* new value is not in old centroid - add it */
                        if ( charray_add_dup( &avals, &acur, &amax, nkey.dptr ) == NULL ) {
@@ -629,7 +662,12 @@ diff_centroids(
                                ldbm_datum_free( nldbm, nlast );
                        }
                        nlast = nkey;
+
+#ifdef HAVE_BERKELEY_DB2
+                       nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
+#else
                        nkey = ldbm_nextkey( nldbm, nlast );
+#endif
                } else {
                        /* old value is not in new centroid - delete it */
                        if ( charray_add_dup( &dvals, &dcur, &dmax, okey.dptr ) == NULL ) {
@@ -641,7 +679,12 @@ diff_centroids(
                                ldbm_datum_free( oldbm, olast );
                        }
                        olast = okey;
+
+#ifdef HAVE_BERKELEY_DB2
+                       okey = ldbm_nextkey( oldbm, olast, ocursorp );
+#else
                        okey = ldbm_nextkey( oldbm, olast );
+#endif
                }
        }
 
@@ -651,7 +694,11 @@ diff_centroids(
                        return( NULL );
                }
 
+#ifdef HAVE_BERKELEY_DB2
+               okey = ldbm_nextkey( oldbm, olast, ocursorp );
+#else
                okey = ldbm_nextkey( oldbm, olast );
+#endif
                if ( olast.dptr != NULL ) {
                        ldbm_datum_free( oldbm, olast );
                }
@@ -666,7 +713,11 @@ diff_centroids(
                        return( NULL );
                }
 
+#ifdef HAVE_BERKELEY_DB2
+               nkey = ldbm_nextkey( nldbm, nlast, ncursorp );
+#else
                nkey = ldbm_nextkey( nldbm, nlast );
+#endif
                if ( nlast.dptr != NULL ) {
                        ldbm_datum_free( nldbm, nlast );
                }
@@ -687,8 +738,14 @@ diff_centroids(
 
        /* generate list of values to add */
        lastkey.dptr = NULL;
+#ifdef HAVE_BERKELEY_DB2
+       for ( key = ldbm_firstkey( nldbm, &ncursorp ); key.dptr != NULL;
+         key = ldbm_nextkey( nldbm, lastkey, ncursorp ) )
+#else
        for ( key = ldbm_firstkey( nldbm ); key.dptr != NULL;
-         key = ldbm_nextkey( nldbm, lastkey ) ) {
+         key = ldbm_nextkey( nldbm, lastkey ) )
+#endif
+       {
                /* see if it's in the old one */
                data = ldbm_fetch( oldbm, key );
 
@@ -712,8 +769,14 @@ diff_centroids(
 
        /* generate list of values to delete */
        lastkey.dptr = NULL;
+#ifdef HAVE_BERKELEY_DB2
+       for ( key = ldbm_firstkey( oldbm, &ocursorp ); key.dptr != NULL;
+         key = ldbm_nextkey( oldbm, lastkey, ocursorp ) )
+#else
        for ( key = ldbm_firstkey( oldbm ); key.dptr != NULL;
-         key = ldbm_nextkey( oldbm, lastkey ) ) {
+         key = ldbm_nextkey( oldbm, lastkey ) )
+#endif
+       {
                /* see if it's in the new one */
                data = ldbm_fetch( nldbm, key );
 
@@ -773,11 +836,18 @@ full_centroid(
        char    **vals;
        int             vcur, vmax;
 
+#ifdef HAVE_BERKELEY_DB2
+       DBC *cursorp;
+#endif
+
        if ( verbose ) {
                printf( "Generating mods for full %s centroid...", attr );
                fflush( stdout );
        }
 
+       ldbm_datum_init( key );
+       ldbm_datum_init( lastkey );
+
        if ( (mods = (LDAPMod **) malloc( sizeof(LDAPMod *) * 3 )) == NULL ||
             (mods[0] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
             (mods[1] = (LDAPMod *) malloc( sizeof(LDAPMod) )) == NULL ||
@@ -800,8 +870,14 @@ full_centroid(
        lastkey.dptr = NULL;
        vals = NULL;
        vcur = vmax = 0;
+#ifdef HAVE_BERKELEY_DB2
+       for ( key = ldbm_firstkey( ldbm, &cursorp ); key.dptr != NULL;
+         key = ldbm_nextkey( ldbm, lastkey, cursorp ) )
+#else
        for ( key = ldbm_firstkey( ldbm ); key.dptr != NULL;
-         key = ldbm_nextkey( ldbm, lastkey ) ) {
+         key = ldbm_nextkey( ldbm, lastkey ) )
+#endif
+       {
                if ( charray_add_dup( &vals, &vcur, &vmax, key.dptr ) == NULL ) {
                        ldap_mods_free( mods, 1 );
                        return( NULL );
@@ -862,10 +938,13 @@ bind_to_destination_ldap(
        /* first, pick out the destination ldap server info */
        if ( ldapbase != NULL ) {
                free( ldapbase );
+               ldapbase = NULL;
        }
-       if ( strncmp( ldapdesturl, "ldap://", 7 ) == 0 ) {
-               s = ldapdesturl + 7;
+       if ( strncmp( ldapdesturl, "ldap://", 7 ) != 0 ) {
+               fputs( "Not an LDAP URL", stderr ); /* Should be smarter? */
+               return( NULL );
        }
+       s = ldapdesturl + 7;
        if ( (s2 = strchr( s, '/' )) == NULL ) {
                ldapbase = strdup( s );
        } else {
@@ -897,8 +976,8 @@ bind_to_destination_ldap(
        free( ldapbase );
        ldapbase = strdup( buf );
 
-       if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
-               perror( "ldap_open" );
+       if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
+               perror( "ldap_init" );
                return( NULL );
        }
 
@@ -966,8 +1045,6 @@ charray_add_dup(
        char    *s
 )
 {
-       int n;
        if ( *a == NULL ) {
                *a = (char **) malloc( (BUFSIZ + 1) * sizeof(char *) );
                *cur = 0;