]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/tools/slapcommon.c
cast isdigit argument to unsigned char.
[openldap] / servers / slapd / tools / slapcommon.c
index abe5b7abeb754ee50f234627758e243640aa7887..cea438712da9aab77266541e59001388c8df12ec 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* slapcommon.c - common routine for the slap tools */
@@ -23,9 +23,18 @@ char *progname       = NULL;
 char   *conffile       = SLAPD_DEFAULT_CONFIGFILE;
 int            truncatemode = 0;
 int            verbose         = 0;
+int            update_ctxcsn = SLAP_TOOL_CTXCSN_KEEP;
+int            retrieve_ctxcsn = 0;
+int            retrieve_synccookie = 0;
+int            replica_promotion = 0;
+int            replica_demotion = 0;
+char   *replica_id_string = NULL;
+char   **replica_id_strlist = NULL;
+int            *replica_id_list = NULL;
 int            continuemode = 0;
 int            nosubordinates = 0;
 int            dryrun = 0;
+struct berval  sub_ndn = { 0, NULL };
 
 char   *ldiffile       = NULL;
 FILE   *ldiffp         = NULL;
@@ -47,11 +56,11 @@ usage( int tool )
 
        switch( tool ) {
        case SLAPADD:
-               options = "\t[-l ldiffile]\n";
+               options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
                break;
 
        case SLAPCAT:
-               options = "\t[-l ldiffile]\n";
+               options = "\t[-l ldiffile] [-m] [-k]\n";
                break;
 
        case SLAPINDEX:
@@ -82,6 +91,7 @@ slap_tool_init(
 {
        char *options;
        struct berval base = { 0, NULL };
+       char *subtree = NULL;
        int rc, i, dbnum;
        int mode = SLAP_TOOL_MODE;
 
@@ -98,15 +108,17 @@ slap_tool_init(
 
        switch( tool ) {
        case SLAPADD:
-               options = "b:cd:f:l:n:tuv";
+               options = "b:cd:f:i:l:n:prtuvWw";
                break;
 
        case SLAPINDEX:
                options = "b:cd:f:n:v";
+               mode |= SLAP_TOOL_READMAIN;
                break;
 
        case SLAPCAT:
-               options = "b:cd:f:l:n:v";
+               options = "b:cd:f:kl:mn:s:v";
+               mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
                break;
 
        default:
@@ -123,6 +135,7 @@ slap_tool_init(
                case 'b':
                        base.bv_val = strdup( optarg );
                        base.bv_len = strlen( base.bv_val );
+                       break;
 
                case 'c':       /* enable continue mode */
                        continuemode++;
@@ -136,14 +149,55 @@ slap_tool_init(
                        conffile = strdup( optarg );
                        break;
 
+               case 'i': /* specify syncrepl id list */
+                       replica_id_string = strdup( optarg );
+                       if ( !isdigit( (unsigned char) *replica_id_string )) {
+                               usage( tool );
+                               exit( EXIT_FAILURE );
+                       }
+                       str2clist( &replica_id_strlist, replica_id_string, "," );
+                       for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
+                       replica_id_list = ch_calloc( i + 1, sizeof( int ) );
+                       for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
+                               replica_id_list[i] = atoi( replica_id_strlist[i] );
+                               if ( replica_id_list[i] >= 1000 ) {
+                                       fprintf(stderr,
+                                               "%s: syncrepl id %d is out of range [0..999]\n",
+                                               progname, replica_id_list[i] );
+                                       exit( EXIT_FAILURE );
+                               }
+                       }
+                       replica_id_list[i] = -1;
+                       break;
+
+               case 'k':       /* Retrieve sync cookie entry */
+                       retrieve_synccookie = 1;
+                       break;
+
                case 'l':       /* LDIF file */
                        ldiffile = strdup( optarg );
                        break;
 
+               case 'm':       /* Retrieve ldapsync entry */
+                       retrieve_ctxcsn = 1;
+                       break;
+
                case 'n':       /* which config file db to index */
                        dbnum = atoi( optarg ) - 1;
                        break;
 
+               case 'p':       /* replica promotion */
+                       replica_promotion = 1;          
+                       break;
+
+               case 'r':       /* replica demotion */
+                       replica_demotion = 1;           
+                       break;
+
+               case 's':       /* dump subtree */
+                       subtree = strdup( optarg );
+                       break;
+
                case 't':       /* turn on truncate */
                        truncatemode++;
                        mode |= SLAP_TRUNCATE_MODE;
@@ -157,6 +211,15 @@ slap_tool_init(
                        verbose++;
                        break;
 
+               case 'W':       /* write context csn on every entry add */
+                       update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
+                       /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
+                       break;
+
+               case 'w':       /* write context csn on at the end */
+                       update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
+                       break;
+
                default:
                        usage( tool );
                        break;
@@ -167,6 +230,14 @@ slap_tool_init(
                usage( tool );
        }
 
+       if ( replica_promotion && replica_demotion ) {
+               usage( tool );
+       } else if ( !replica_promotion && !replica_demotion ) {
+               if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
+                       usage( tool );
+               }
+       }
+
        if ( ldiffile == NULL ) {
                ldiffp = tool == SLAPCAT ? stdout : stdin;
 
@@ -181,21 +252,28 @@ slap_tool_init(
         * initialize stuff and figure out which backend we're dealing with
         */
 
+#ifdef SLAPD_MODULES
+       if ( module_init() != 0 ) {
+               fprintf( stderr, "%s: module_init failed!\n", progname );
+               exit( EXIT_FAILURE );
+       }
+#endif
+               
        rc = slap_init( mode, progname );
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "%s: slap_init failed!\n", progname );
                exit( EXIT_FAILURE );
        }
 
        rc = slap_schema_init();
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
                exit( EXIT_FAILURE );
        }
 
-       rc = read_config( conffile );
+       rc = read_config( conffile, 0 );
 
        if ( rc != 0 ) {
                fprintf( stderr, "%s: bad configuration file!\n", progname );
@@ -209,30 +287,46 @@ slap_tool_init(
 
        rc = glue_sub_init();
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "Subordinate configuration error\n" );
                exit( EXIT_FAILURE );
        }
 
        rc = slap_schema_check();
 
-       if (rc != 0 ) {
+       if ( rc != 0 ) {
                fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
                exit( EXIT_FAILURE );
        }
 
+       if( subtree ) {
+               struct berval val;
+               val.bv_val = subtree;
+               val.bv_len = strlen( subtree );
+               rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
+               if( rc != LDAP_SUCCESS ) {
+                       fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
+                       exit( EXIT_FAILURE );
+               }
+
+               if( base.bv_val == NULL && dbnum == -1 )
+                       base = val;
+               else
+                       free( subtree );
+       }
+
        if( base.bv_val != NULL ) {
-               struct berval *nbase = NULL;
+               struct berval nbase;
 
-               rc = dnNormalize( NULL, &base, &nbase );
+               rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
                if( rc != LDAP_SUCCESS ) {
                        fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
                                progname, base.bv_val );
                        exit( EXIT_FAILURE );
                }
 
-               be = select_backend( nbase, 0, 0 );
-               ber_bvfree( nbase );
+               be = select_backend( &nbase, 0, 0 );
+               ber_memfree( nbase.bv_val );
 
                if( be == NULL ) {
                        fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
@@ -242,17 +336,45 @@ slap_tool_init(
                /* If the named base is a glue master, operate on the
                 * entire context
                 */
-               if (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE)
+               if (SLAP_GLUE_INSTANCE(be)) {
                        nosubordinates = 1;
+               }
 
        } else if ( dbnum == -1 ) {
+               if ( nbackends <= 0 ) {
+                       fprintf( stderr, "No available databases\n" );
+                       exit( EXIT_FAILURE );
+               }
+               
                be = &backends[dbnum=0];
                /* If just doing the first by default and it is a
                 * glue subordinate, find the master.
                 */
-               while (be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
-                       nosubordinates = 1;
+               while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
+                       if (SLAP_GLUE_SUBORDINATE(be)) {
+                               nosubordinates = 1;
+                       }
                        be++;
+                       dbnum++;
+               }
+
+
+               if ( dbnum >= nbackends ) {
+                       fprintf( stderr, "Available database(s) "
+                                       "do not allow %s\n", name );
+                       exit( EXIT_FAILURE );
+               }
+               
+               if ( nosubordinates == 0 && dbnum > 0 ) {
+#ifdef NEW_LOGGING
+                       LDAP_LOG( BACKEND, ERR, 
+"The first database does not allow %s; using the first available one (%d)\n",
+                               name, dbnum + 1, 0 );
+#else
+                       Debug( LDAP_DEBUG_ANY,
+"The first database does not allow %s; using the first available one (%d)\n",
+                               name, dbnum + 1, 0 );
+#endif
                }
 
        } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
@@ -278,6 +400,18 @@ void slap_tool_destroy( void )
 {
        slap_shutdown( be );
        slap_destroy();
+#ifdef SLAPD_MODULES
+       if ( slapMode == SLAP_SERVER_MODE ) {
+       /* always false. just pulls in necessary symbol references. */
+               lutil_uuidstr(NULL, 0);
+       }
+       module_kill();
+#endif
+       schema_destroy();
+#ifdef HAVE_TLS
+       ldap_pvt_tls_destroy();
+#endif
+       config_destroy();
 
 #ifdef CSRIMALLOC
        mal_dumpleaktrace( leakfile );