]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapcommon.c
add logs; fix bug in group/dn selection logic
[openldap] / servers / slapd / slapcommon.c
index 9db0214ef047bf8b7274032f6de7e07d78cc1675..857d4228486e6f55b6fc5a44dbf527c9bc1da705 100644 (file)
@@ -49,24 +49,33 @@ usage( int tool, const char *progname )
 {
        char *options = NULL;
        fprintf( stderr,
-               "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
-                       "\t[-n databasenumber | -b suffix]", progname );
+               "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n",
+               progname );
 
        switch( tool ) {
        case SLAPADD:
-               options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
+               options = "\t[-n databasenumber | -b suffix]\n"
+                       "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
                break;
 
        case SLAPCAT:
-               options = "\t[-l ldiffile] [-m] [-k]\n";
+               options = "\t[-n databasenumber | -b suffix] [-l ldiffile] [-m] [-k]\n";
+               break;
+
+       case SLAPDN:
+               options = "\tDN [...]\n";
                break;
 
        case SLAPINDEX:
-               options = "\n";
+               options = "\t[-n databasenumber | -b suffix]\n";
+               break;
+
+       case SLAPAUTH:
+               options = "\t[-U authcID] [-X authzID] ID [...]\n";
                break;
        }
 
-       if( options != NULL ) {
+       if ( options != NULL ) {
                fputs( options, stderr );
        }
        exit( EXIT_FAILURE );
@@ -89,7 +98,7 @@ slap_tool_init(
 {
        char *options;
        char *conffile = SLAPD_DEFAULT_CONFIGFILE;
-       struct berval base = { 0, NULL };
+       struct berval base = BER_BVNULL;
        char *subtree = NULL;
        char *ldiffile  = NULL;
        int rc, i, dbnum;
@@ -97,7 +106,7 @@ slap_tool_init(
        int truncatemode = 0;
 
 #ifdef CSRIMALLOC
-       leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
+       leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 );
        sprintf( leakfilename, "%s.leak", progname );
        if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
                leakfile = stderr;
@@ -110,16 +119,25 @@ slap_tool_init(
                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:kl:mn:s:v";
                mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
                break;
 
+       case SLAPDN:
+       case SLAPTEST:
+               options = "d:f:v";
+               break;
+
+       case SLAPAUTH:
+               options = "d:f:U:vX:";
+               break;
+
+       case SLAPINDEX:
+               options = "b:cd:f:n:v";
+               mode |= SLAP_TOOL_READMAIN;
+               break;
+
        default:
                fprintf( stderr, "%s: unknown tool mode (%d)\n",
                         progname, tool );
@@ -130,8 +148,7 @@ slap_tool_init(
        while ( (i = getopt( argc, argv, options )) != EOF ) {
                switch ( i ) {
                case 'b':
-                       base.bv_val = strdup( optarg );
-                       base.bv_len = strlen( base.bv_val );
+                       ber_str2bv( optarg, 0, 1, &base );
                        break;
 
                case 'c':       /* enable continue mode */
@@ -200,6 +217,10 @@ slap_tool_init(
                        mode |= SLAP_TRUNCATE_MODE;
                        break;
 
+               case 'U':
+                       ber_str2bv( optarg, 0, 0, &authcID );
+                       break;
+
                case 'u':       /* dry run */
                        dryrun++;
                        break;
@@ -217,22 +238,54 @@ slap_tool_init(
                        update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
                        break;
 
+               case 'X':
+                       ber_str2bv( optarg, 0, 0, &authzID );
+                       break;
+
                default:
                        usage( tool, progname );
                        break;
                }
        }
 
-       if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
-               usage( tool, progname );
-       }
+       switch ( tool ) {
+       case SLAPADD:
+       case SLAPCAT:
+       case SLAPINDEX:
+               if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
+                       usage( tool, progname );
+               }
 
-       if ( replica_promotion && replica_demotion ) {
-               usage( tool, progname );
-       } else if ( !replica_promotion && !replica_demotion ) {
-               if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
+               if ( replica_promotion && replica_demotion ) {
                        usage( tool, progname );
+
+               } else if ( !replica_promotion && !replica_demotion ) {
+                       if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
+                               usage( tool, progname );
+                       }
                }
+               break;
+
+       case SLAPDN:
+               if ( argc == optind ) {
+                       usage( tool, progname );
+               }
+               break;
+
+       case SLAPAUTH:
+               if ( argc == optind && BER_BVISNULL( &authcID ) ) {
+                       usage( tool, progname );
+               }
+               break;
+
+       case SLAPTEST:
+               if ( argc != optind ) {
+                       usage( tool, progname );
+               }
+               break;
+
+       default:
+               break;
        }
 
        if ( ldiffile == NULL ) {
@@ -270,6 +323,11 @@ slap_tool_init(
                exit( EXIT_FAILURE );
        }
 
+       if ( overlay_init() ) {
+               fprintf( stderr, "%s: overlay_init failed!\n", progname );
+               exit( EXIT_FAILURE );
+       }
+
        rc = read_config( conffile, 0 );
 
        if ( rc != 0 ) {
@@ -279,9 +337,19 @@ slap_tool_init(
 
        ldap_syslog = 0;
 
-       if ( !nbackends ) {
-               fprintf( stderr, "No databases found in config file\n" );
-               exit( EXIT_FAILURE );
+       switch ( tool ) {
+       case SLAPADD:
+       case SLAPCAT:
+       case SLAPINDEX:
+               if ( !nbackends ) {
+                       fprintf( stderr, "No databases found "
+                                       "in config file\n" );
+                       exit( EXIT_FAILURE );
+               }
+               break;
+
+       default:
+               break;
        }
 
        rc = glue_sub_init();
@@ -298,17 +366,29 @@ slap_tool_init(
                exit( EXIT_FAILURE );
        }
 
+       switch ( tool ) {
+       case SLAPDN:
+       case SLAPTEST:
+               return;
+
+       case SLAPAUTH:
+               be = NULL;
+               goto startup;
+
+       default:
+               break;
+       }
+
        if( subtree ) {
                struct berval val;
-               val.bv_val = subtree;
-               val.bv_len = strlen( subtree );
+               ber_str2bv( subtree, 0, 0, &val );
                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 )
+               if ( BER_BVISNULL( &base ) && dbnum == -1 )
                        base = val;
                else
                        free( subtree );
@@ -388,6 +468,8 @@ slap_tool_init(
                be = &backends[dbnum];
        }
 
+startup:;
+
 #ifdef CSRIMALLOC
        mal_leaktrace(1);
 #endif