]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/slapcommon.c
fix ITS#3406 wrt/ dnattr; silence warning
[openldap] / servers / slapd / slapcommon.c
index 9db0214ef047bf8b7274032f6de7e07d78cc1675..56c2b613c1bba25eb24bc9a94b3fa2f687d276d5 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2004 The OpenLDAP Foundation.
+ * Copyright 1998-2005 The OpenLDAP Foundation.
  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
  * Portions Copyright 2003 IBM Corporation.
  * All rights reserved.
@@ -49,24 +49,43 @@ 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]",
+               progname );
 
        switch( tool ) {
+       case SLAPACL:
+               options = "\n\t[-U authcID | -D authcDN]"
+                       " -b DN [attr[/access][:value]] [...]\n";
+               break;
+
        case SLAPADD:
-               options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
+               options = "\n\t[-n databasenumber | -b suffix]\n"
+                       "\t[-l ldiffile] [-q] [-u] [-w]\n";
+               break;
+
+       case SLAPAUTH:
+               options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
                break;
 
        case SLAPCAT:
-               options = "\t[-l ldiffile] [-m] [-k]\n";
+               options = "\n\t[-n databasenumber | -b suffix]"
+                       " [-l ldiffile] [-a filter]\n";
+               break;
+
+       case SLAPDN:
+               options = " DN [...]\n";
                break;
 
        case SLAPINDEX:
-               options = "\n";
+               options = "\n\t[-n databasenumber | -b suffix] [-q]\n";
+               break;
+
+       case SLAPTEST:
+               options = " [-u]\n";
                break;
        }
 
-       if( options != NULL ) {
+       if ( options != NULL ) {
                fputs( options, stderr );
        }
        exit( EXIT_FAILURE );
@@ -89,7 +108,8 @@ slap_tool_init(
 {
        char *options;
        char *conffile = SLAPD_DEFAULT_CONFIGFILE;
-       struct berval base = { 0, NULL };
+       struct berval base = BER_BVNULL;
+       char *filterstr = NULL;
        char *subtree = NULL;
        char *ldiffile  = NULL;
        int rc, i, dbnum;
@@ -97,7 +117,7 @@ slap_tool_init(
        int truncatemode = 0;
 
 #ifdef CSRIMALLOC
-       leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
+       leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
        sprintf( leakfilename, "%s.leak", progname );
        if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
                leakfile = stderr;
@@ -107,31 +127,53 @@ slap_tool_init(
 
        switch( tool ) {
        case SLAPADD:
-               options = "b:cd:f:i:l:n:prtuvWw";
+               options = "b:cd:f:l:n:qtuvw";
+               break;
+
+       case SLAPCAT:
+               options = "a:b:cd:f:l:n:s:v";
+               mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
+               break;
+
+       case SLAPDN:
+               options = "d:f:v";
+               mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
+               break;
+
+       case SLAPTEST:
+               options = "d:f:uv";
+               mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
+               break;
+
+       case SLAPAUTH:
+               options = "d:f:M:R:U:vX:";
+               mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
                break;
 
        case SLAPINDEX:
-               options = "b:cd:f:n:v";
+               options = "b:cd:f:n:qv";
                mode |= SLAP_TOOL_READMAIN;
                break;
 
-       case SLAPCAT:
-               options = "b:cd:f:kl:mn:s:v";
+       case SLAPACL:
+               options = "b:D:d:f:U:v";
                mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
                break;
 
        default:
-               fprintf( stderr, "%s: unknown tool mode (%d)\n",
-                        progname, tool );
+               fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
                exit( EXIT_FAILURE );
        }
 
        dbnum = -1;
        while ( (i = getopt( argc, argv, options )) != EOF ) {
                switch ( i ) {
+               case 'a':
+                       filterstr = strdup( optarg );
+                       break;
+
                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 */
@@ -142,53 +184,32 @@ slap_tool_init(
                        ldap_debug += atoi( optarg );
                        break;
 
-               case 'f':       /* specify a conf file */
-                       conffile = strdup( optarg );
-                       break;
-
-               case 'i': /* specify syncrepl id list */
-                       replica_id_string = strdup( optarg );
-                       if ( !isdigit( (unsigned char) *replica_id_string )) {
-                               usage( tool, progname );
-                               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;
+               case 'D':
+                       ber_str2bv( optarg, 0, 1, &authcDN );
                        break;
 
-               case 'k':       /* Retrieve sync cookie entry */
-                       retrieve_synccookie = 1;
+               case 'f':       /* specify a conf file */
+                       conffile = strdup( optarg );
                        break;
 
                case 'l':       /* LDIF file */
                        ldiffile = strdup( optarg );
                        break;
 
-               case 'm':       /* Retrieve ldapsync entry */
-                       retrieve_ctxcsn = 1;
+               case 'M':
+                       ber_str2bv( optarg, 0, 0, &mech );
                        break;
 
                case 'n':       /* which config file db to index */
-                       dbnum = atoi( optarg ) - 1;
+                       dbnum = atoi( optarg );
                        break;
 
-               case 'p':       /* replica promotion */
-                       replica_promotion = 1;          
+               case 'q':       /* turn on quick */
+                       mode |= SLAP_TOOL_QUICK;
                        break;
 
-               case 'r':       /* replica demotion */
-                       replica_demotion = 1;           
+               case 'R':
+                       realm = optarg;
                        break;
 
                case 's':       /* dump subtree */
@@ -200,6 +221,10 @@ slap_tool_init(
                        mode |= SLAP_TRUNCATE_MODE;
                        break;
 
+               case 'U':
+                       ber_str2bv( optarg, 0, 0, &authcID );
+                       break;
+
                case 'u':       /* dry run */
                        dryrun++;
                        break;
@@ -208,13 +233,12 @@ 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; */
+               case 'w':       /* write context csn at the end */
+                       update_ctxcsn++;
                        break;
 
-               case 'w':       /* write context csn on at the end */
-                       update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
+               case 'X':
+                       ber_str2bv( optarg, 0, 0, &authzID );
                        break;
 
                default:
@@ -223,22 +247,52 @@ slap_tool_init(
                }
        }
 
-       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 );
+               }
+
+               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;
 
-       if ( replica_promotion && replica_demotion ) {
-               usage( tool, progname );
-       } else if ( !replica_promotion && !replica_demotion ) {
-               if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
+       case SLAPACL:
+               if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
                        usage( tool, progname );
                }
+               if ( BER_BVISNULL( &base ) ) {
+                       usage( tool, progname );
+               }
+               ber_dupbv( &baseDN, &base );
+               break;
+
+       default:
+               break;
        }
 
        if ( ldiffile == NULL ) {
                ldiffp = tool == SLAPCAT ? stdout : stdin;
 
-       } else if(ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
+       } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
                == NULL )
        {
                perror( ldiffile );
@@ -270,6 +324,16 @@ slap_tool_init(
                exit( EXIT_FAILURE );
        }
 
+       if ( frontend_init() ) {
+               fprintf( stderr, "%s: frontend_init failed!\n", progname );
+               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 ) {
@@ -277,18 +341,22 @@ slap_tool_init(
                exit( EXIT_FAILURE );
        }
 
+       at_oc_cache = 1;
        ldap_syslog = 0;
 
-       if ( !nbackends ) {
-               fprintf( stderr, "No databases found in config file\n" );
-               exit( EXIT_FAILURE );
-       }
-
-       rc = glue_sub_init();
+       switch ( tool ) {
+       case SLAPADD:
+       case SLAPCAT:
+       case SLAPINDEX:
+               if ( !nbackends ) {
+                       fprintf( stderr, "No databases found "
+                                       "in config file\n" );
+                       exit( EXIT_FAILURE );
+               }
+               break;
 
-       if ( rc != 0 ) {
-               fprintf( stderr, "Subordinate configuration error\n" );
-               exit( EXIT_FAILURE );
+       default:
+               break;
        }
 
        rc = slap_schema_check();
@@ -298,20 +366,40 @@ slap_tool_init(
                exit( EXIT_FAILURE );
        }
 
+       switch ( tool ) {
+       case SLAPDN:
+       case SLAPTEST:
+       case SLAPAUTH:
+               be = NULL;
+               goto startup;
+
+       default:
+               break;
+       }
+
+       if( filterstr ) {
+               filter = str2filter( filterstr );
+
+               if( filter == NULL ) {
+                       fprintf( stderr, "Invalid filter '%s'\n", filterstr );
+                       exit( EXIT_FAILURE );
+               }
+       }
+
        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 );
+                       fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
                        exit( EXIT_FAILURE );
                }
 
-               if( base.bv_val == NULL && dbnum == -1 )
+               if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
                        base = val;
-               else
+               } else {
                        free( subtree );
+               }
        }
 
        if( base.bv_val != NULL ) {
@@ -327,6 +415,14 @@ slap_tool_init(
                be = select_backend( &nbase, 0, 0 );
                ber_memfree( nbase.bv_val );
 
+               switch ( tool ) {
+               case SLAPACL:
+                       goto startup;
+
+               default:
+                       break;
+               }
+
                if( be == NULL ) {
                        fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
                                progname, base.bv_val );
@@ -345,7 +441,7 @@ slap_tool_init(
                        exit( EXIT_FAILURE );
                }
                
-               be = &backends[dbnum=0];
+               be = &backends[dbnum=1];
                /* If just doing the first by default and it is a
                 * glue subordinate, find the master.
                 */
@@ -365,22 +461,17 @@ slap_tool_init(
                }
                
                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",
-                               progname, dbnum + 1, 0 );
-#else
                        Debug( LDAP_DEBUG_ANY,
-"The first database does not allow %s; using the first available one (%d)\n",
+                               "The first database does not allow %s;"
+                               " using the first available one (%d)\n",
                                progname, dbnum + 1, 0 );
-#endif
                }
 
        } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
                fprintf( stderr,
                        "Database number selected via -n is out of range\n"
                        "Must be in the range 1 to %d"
-                               " (number of databases in the config file)\n",
+                       " (number of databases in the config file)\n",
                        nbackends );
                exit( EXIT_FAILURE );
 
@@ -388,19 +479,35 @@ slap_tool_init(
                be = &backends[dbnum];
        }
 
+startup:;
+
 #ifdef CSRIMALLOC
        mal_leaktrace(1);
 #endif
 
-       if ( slap_startup( be ) ) {
-               fprintf( stderr, "slap_startup failed\n" );
+       if ( !dryrun && slap_startup( be ) ) {
+
+               switch ( tool ) {
+               case SLAPTEST:
+                       fprintf( stderr, "slap_startup failed "
+                                       "(test would succeed using "
+                                       "the -u switch)\n" );
+                       break;
+
+               default:
+                       fprintf( stderr, "slap_startup failed\n" );
+                       break;
+               }
+               
                exit( EXIT_FAILURE );
        }
 }
 
 void slap_tool_destroy( void )
 {
-       slap_shutdown( be );
+       if ( !dryrun && be != NULL ) {
+               slap_shutdown( be );
+       }
        slap_destroy();
 #ifdef SLAPD_MODULES
        if ( slapMode == SLAP_SERVER_MODE ) {
@@ -418,4 +525,8 @@ void slap_tool_destroy( void )
 #ifdef CSRIMALLOC
        mal_dumpleaktrace( leakfile );
 #endif
+
+       if ( !BER_BVISNULL( &authcDN ) ) {
+               ch_free( authcDN.bv_val );
+       }
 }