]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/tools/slapcommon.c
Implement slapcat -s <dn>: Only dump a subtree of the database.
[openldap] / servers / slapd / tools / slapcommon.c
index 61b6d6e08e7454f48409617bbb22f32b242d06a7..e8fe3dcbb8e527aba7a7db758693b4b82e646a81 100644 (file)
@@ -26,6 +26,7 @@ int           verbose         = 0;
 int            continuemode = 0;
 int            nosubordinates = 0;
 int            dryrun = 0;
+struct berval  sub_ndn = { 0, NULL };
 
 char   *ldiffile       = NULL;
 FILE   *ldiffp         = NULL;
@@ -82,6 +83,7 @@ slap_tool_init(
 {
        char *options;
        struct berval base = { 0, NULL };
+       char *subtree = NULL;
        int rc, i, dbnum;
        int mode = SLAP_TOOL_MODE;
 
@@ -106,7 +108,7 @@ slap_tool_init(
                break;
 
        case SLAPCAT:
-               options = "b:cd:f:l:n:v";
+               options = "b:cd:f:l:n:s:v";
                break;
 
        default:
@@ -145,6 +147,10 @@ slap_tool_init(
                        dbnum = atoi( optarg ) - 1;
                        break;
 
+               case 's':       /* dump subtree */
+                       subtree = strdup( optarg );
+                       break;
+
                case 't':       /* turn on truncate */
                        truncatemode++;
                        mode |= SLAP_TRUNCATE_MODE;
@@ -229,6 +235,22 @@ slap_tool_init(
                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;