]> git.sur5r.net Git - openldap/blobdiff - clients/tools/common.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / clients / tools / common.c
index c1c489a4758f0cb86758377c99040ba33c7813fe..05430415fa8c3fd638abd3107d8f5153a6247ecc 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2009 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
  * Portions Copyright 2003 Kurt D. Zeilenga.
  * Portions Copyright 2003 IBM Corporation.
  * All rights reserved.
@@ -66,6 +66,7 @@ int           nocanon = 0;
 int            referrals = 0;
 int            verbose = 0;
 int            ldif = 0;
+ber_len_t      ldif_wrap = LDIF_LINE_WIDTH;
 char           *prog = NULL;
 
 /* connection */
@@ -238,6 +239,10 @@ tool_init( tool_type_t type )
 void
 tool_destroy( void )
 {
+       static int destroyed;
+       if ( destroyed++ )
+               return;
+
 #ifdef HAVE_CYRUS_SASL
        sasl_done();
 #endif
@@ -252,19 +257,48 @@ tool_destroy( void )
 
        if ( pr_cookie.bv_val != NULL ) {
                ber_memfree( pr_cookie.bv_val );
-               pr_cookie.bv_val = NULL;
-               pr_cookie.bv_len = 0;
+               BER_BVZERO( &pr_cookie );
        }
 
        if ( binddn != NULL ) {
                ber_memfree( binddn );
+               binddn = NULL;
        }
 
-#if 0  /* not yet */
        if ( passwd.bv_val != NULL ) {
                ber_memfree( passwd.bv_val );
+               BER_BVZERO( &passwd );
+       }
+
+       if ( infile != NULL ) {
+               ber_memfree( infile );
+               infile = NULL;
+       }
+
+       if ( assertion ) {
+               ber_memfree( assertion );
+               assertion = NULL;
+       }
+
+       if ( authzid ) {
+               ber_memfree( authzid );
+               authzid = NULL;
+       }
+
+       if ( proxydn ) {
+               ber_memfree( proxydn );
+               proxydn = NULL;
+       }
+
+       if ( preread_attrs ) {
+               ber_memfree( preread_attrs );
+               preread_attrs = NULL;
+       }
+
+       if ( postread_attrs ) {
+               ber_memfree( postread_attrs );
+               postread_attrs = NULL;
        }
-#endif
 }
 
 void
@@ -307,8 +341,9 @@ N_("  -I         use SASL Interactive mode\n"),
 N_("  -n         show what would be done but don't actually do it\n"),
 N_("  -N         do not use reverse DNS to canonicalize SASL host name\n"),
 N_("  -O props   SASL security properties\n"),
-N_("  -o <opt>[=<optparam] general options\n"),
+N_("  -o <opt>[=<optparam>] general options\n"),
 N_("             nettimeout=<timeout> (in seconds, or \"none\" or \"max\")\n"),
+N_("             ldif-wrap=<width> (in columns, or \"no\" for no wrapping)\n"),
 N_("  -p port    port on LDAP server\n"),
 N_("  -Q         use SASL Quiet mode\n"),
 N_("  -R realm   SASL realm\n"),
@@ -332,6 +367,8 @@ NULL
                        fputs( _(*cpp), stderr );
                }
        }
+
+       tool_destroy();
 }
 
 void tool_perror(
@@ -375,7 +412,7 @@ tool_args( int argc, char **argv )
                case 'c':       /* continuous operation mode */
                        contoper++;
                        break;
-               case 'C':
+               case 'C':       /* referrals: obsolete */
                        referrals++;
                        break;
                case 'd':
@@ -423,7 +460,7 @@ tool_args( int argc, char **argv )
                                assertctl = 1 + crit;
 
                                assert( assertion == NULL );
-                               assertion = cvalue;
+                               assertion = ber_strdup( cvalue );
 
                        } else if ( strcasecmp( control, "authzid" ) == 0 ) {
                                if( authzid != NULL ) {
@@ -446,7 +483,7 @@ tool_args( int argc, char **argv )
                                }
 
                                assert( authzid == NULL );
-                               authzid = cvalue;
+                               authzid = ber_strdup( cvalue );
 
 #ifdef LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ
                        } else if ( strcasecmp( control, "proxydn" ) == 0 ) {
@@ -468,7 +505,7 @@ tool_args( int argc, char **argv )
                                }
 
                                assert( proxydn == NULL );
-                               proxydn = cvalue;
+                               proxydn = ber_strdup( cvalue );
 #endif /* LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ */
 
                        } else if ( ( strcasecmp( control, "relax" ) == 0 ) ||
@@ -538,7 +575,7 @@ tool_args( int argc, char **argv )
                                }
 
                                preread = 1 + crit;
-                               preread_attrs = cvalue;
+                               preread_attrs = ber_strdup( cvalue );
 
                        } else if ( strcasecmp( control, "postread" ) == 0 ) {
                                if( postread ) {
@@ -547,7 +584,7 @@ tool_args( int argc, char **argv )
                                }
 
                                postread = 1 + crit;
-                               postread_attrs = cvalue;
+                               postread_attrs = ber_strdup( cvalue );
 
 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
                        } else if ( strcasecmp( control, "chaining" ) == 0 ) {
@@ -595,6 +632,19 @@ tool_args( int argc, char **argv )
                                }
 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
 
+#ifdef LDAP_CONTROL_X_SESSION_TRACKING
+                       } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
+                               if ( sessionTracking ) {
+                                       fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
+                                       exit( EXIT_FAILURE );
+                               }
+                               sessionTracking = 1;
+                               if( crit ) {
+                                       fprintf( stderr, "sessiontracking: critical flag not allowed\n" );
+                                       usage();
+                               }
+#endif /* LDAP_CONTROL_X_SESSION_TRACKING */
+
                        /* this shouldn't go here, really; but it's a feature... */
                        } else if ( strcasecmp( control, "abandon" ) == 0 ) {
                                abcan = Intr_Abandon;
@@ -614,15 +664,6 @@ tool_args( int argc, char **argv )
                                        gotintr = abcan;
                                }
 
-#ifdef LDAP_CONTROL_X_SESSION_TRACKING
-                       } else if ( strcasecmp( control, "sessiontracking" ) == 0 ) {
-                               if ( sessionTracking ) {
-                                       fprintf( stderr, "%s: session tracking can be only specified once\n", prog );
-                                       exit( EXIT_FAILURE );
-                               }
-                               sessionTracking = 1;
-#endif /* LDAP_CONTROL_X_SESSION_TRACKING */
-
                        } else if ( tool_is_oid( control ) ) {
                                LDAPControl     *tmpctrls, ctrl;
 
@@ -634,6 +675,8 @@ tool_args( int argc, char **argv )
                                }
                                unknown_ctrls = tmpctrls;
                                ctrl.ldctl_oid = control;
+                               /* don't free it */
+                               control = NULL;
                                ctrl.ldctl_value.bv_val = NULL;
                                ctrl.ldctl_value.bv_len = 0;
                                ctrl.ldctl_iscritical = crit;
@@ -668,6 +711,10 @@ tool_args( int argc, char **argv )
                                        control );
                                usage();
                        }
+                       if ( control ) {
+                               ber_memfree( control );  
+                               control = NULL;
+                       }
                        break;
                case 'f':       /* read from file */
                        if( infile != NULL ) {
@@ -749,11 +796,30 @@ tool_args( int argc, char **argv )
                                                prog, (long)nettimeout.tv_sec );
                                        exit( EXIT_FAILURE );
                                }
+
+                       } else if ( strcasecmp( control, "ldif-wrap" ) == 0 ) {
+                               if ( cvalue == 0 ) {
+                                       ldif_wrap = LDIF_LINE_WIDTH;
+
+                               } else if ( strcasecmp( cvalue, "no" ) == 0 ) {
+                                       ldif_wrap = LDIF_LINE_WIDTH_MAX;
+
+                               } else {
+                                       unsigned int u;
+                                       if ( lutil_atou( &u, cvalue ) ) {
+                                               fprintf( stderr,
+                                                       _("Unable to parse ldif-wrap=\"%s\"\n"), cvalue );
+                                               exit( EXIT_FAILURE );
+                                       }
+                                       ldif_wrap = (ber_len_t)u;
+                               }
+
                        } else {
                                fprintf( stderr, "Invalid general option name: %s\n",
                                        control );
                                usage();
                        }
+                       ber_memfree(control);
                        break;
                case 'O':
 #ifdef HAVE_CYRUS_SASL
@@ -1065,6 +1131,12 @@ tool_args( int argc, char **argv )
                }
 #endif
        }
+
+       if ( ( pw_file || want_bindpw ) && !BER_BVISNULL( &passwd ) ) {
+               fprintf( stderr, "%s: -%c incompatible with -w\n",
+                       prog, ( pw_file ? 'y' : 'W' ) );
+               exit( EXIT_FAILURE );
+       }
 }
 
 
@@ -1257,23 +1329,24 @@ dnssrv_free:;
 
                if( private_setup ) private_setup( ld );
 
-               /* referrals */
+               /* referrals: obsolete */
                if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
                        referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
                {
                        fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
                                referrals ? "on" : "off" );
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
 #ifdef HAVE_CYRUS_SASL
                /* canon */
-               if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
-                       nocanon ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
-               {
-                       fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON %s\n",
-                               nocanon ? "on" : "off" );
-                       exit( EXIT_FAILURE );
+               if( nocanon ) {
+                       if( ldap_set_option( ld, LDAP_OPT_X_SASL_NOCANON,
+                               LDAP_OPT_ON ) != LDAP_OPT_SUCCESS )
+                       {
+                               fprintf( stderr, "Could not set LDAP_OPT_X_SASL_NOCANON on\n" );
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
                }
 #endif
                if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &protocol )
@@ -1281,15 +1354,18 @@ dnssrv_free:;
                {
                        fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
                                protocol );
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                if ( use_tls ) {
                        rc = ldap_start_tls_s( ld, NULL, NULL );
                        if ( rc != LDAP_SUCCESS ) {
-                               tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
+                               char *msg=NULL;
+                               ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
+                               tool_perror( "ldap_start_tls", rc, NULL, NULL, msg, NULL );
+                               ldap_memfree(msg);
                                if ( use_tls > 1 ) {
-                                       exit( EXIT_FAILURE );
+                                       tool_exit( ld, EXIT_FAILURE );
                                }
                        }
                }
@@ -1300,7 +1376,7 @@ dnssrv_free:;
                        {
                                fprintf( stderr, "Could not set LDAP_OPT_NETWORK_TIMEOUT %ld\n",
                                        (long)nettimeout.tv_sec );
-                               exit( EXIT_FAILURE );
+                               tool_exit( ld, EXIT_FAILURE );
                        }
                }
        }
@@ -1334,8 +1410,8 @@ tool_bind( LDAP *ld )
        if ( sessionTracking ) {
                LDAPControl c;
 
-               if (stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
-                       exit( EXIT_FAILURE );
+               if ( BER_BVISNULL( &stValue) && st_value( ld, &stValue ) ) {
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                c.ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
@@ -1354,6 +1430,23 @@ tool_bind( LDAP *ld )
 
        assert( nsctrls < (int) (sizeof(sctrls)/sizeof(sctrls[0])) );
 
+       if ( pw_file || want_bindpw ) {
+               assert( passwd.bv_val == NULL && passwd.bv_len == 0 );
+
+               if ( pw_file ) {
+                       if ( lutil_get_filed_password( pw_file, &passwd ) ) {
+                               tool_exit( ld, EXIT_FAILURE );
+                       }
+
+               } else {
+                       char *pw = getpassphrase( _("Enter LDAP Password: ") );
+                       if ( pw ) {
+                               passwd.bv_val = ber_strdup( pw );
+                               passwd.bv_len = strlen( passwd.bv_val );
+                       }
+               }
+       }
+
        if ( authmethod == LDAP_AUTH_SASL ) {
 #ifdef HAVE_CYRUS_SASL
                void *defaults;
@@ -1367,7 +1460,7 @@ tool_bind( LDAP *ld )
                                fprintf( stderr,
                                        "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
                                        sasl_secprops );
-                               exit( LDAP_LOCAL_ERROR );
+                               tool_exit( ld, LDAP_LOCAL_ERROR );
                        }
                }
 
@@ -1384,13 +1477,16 @@ tool_bind( LDAP *ld )
 
                lutil_sasl_freedefs( defaults );
                if( rc != LDAP_SUCCESS ) {
+                       char *msg=NULL;
+                       ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
                        tool_perror( "ldap_sasl_interactive_bind_s",
-                               rc, NULL, NULL, NULL, NULL );
-                       exit( rc );
+                               rc, NULL, NULL, msg, NULL );
+                       ldap_memfree(msg);
+                       tool_exit( ld, rc );
                }
 #else
                fprintf( stderr, "%s: not compiled with SASL support\n", prog );
-               exit( LDAP_NOT_SUPPORTED );
+               tool_exit( ld, LDAP_NOT_SUPPORTED );
 #endif
        } else {
                int msgid, err, rc;
@@ -1410,20 +1506,26 @@ tool_bind( LDAP *ld )
                        if ( msgid == -1 ) {
                                tool_perror( "ldap_sasl_bind(SIMPLE)", rc,
                                        NULL, NULL, NULL, NULL );
-                               exit( rc );
+                               tool_exit( ld, rc );
                        }
                }
 
-               if ( ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result ) == -1 ) {
+               rc = ldap_result( ld, msgid, LDAP_MSG_ALL, NULL, &result );
+               if ( rc == -1 ) {
                        tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
-                       exit( LDAP_LOCAL_ERROR );
+                       tool_exit( ld, LDAP_LOCAL_ERROR );
+               }
+
+               if ( rc == 0 ) {
+                       tool_perror( "ldap_result", LDAP_TIMEOUT, NULL, NULL, NULL, NULL );
+                       tool_exit( ld, LDAP_LOCAL_ERROR );
                }
 
                rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
                        &ctrls, 1 );
                if ( rc != LDAP_SUCCESS ) {
                        tool_perror( "ldap_bind parse result", rc, NULL, matched, info, refs );
-                       exit( LDAP_LOCAL_ERROR );
+                       tool_exit( ld, LDAP_LOCAL_ERROR );
                }
 
 #ifdef LDAP_CONTROL_PASSWORDPOLICYREQUEST
@@ -1473,7 +1575,7 @@ tool_bind( LDAP *ld )
                        if( info ) ber_memfree( info );
                        if( refs ) ber_memvfree( (void **)refs );
 
-                       if ( err != LDAP_SUCCESS ) exit( err );
+                       if ( err != LDAP_SUCCESS ) tool_exit( ld, err );
                }
        }
 }
@@ -1490,6 +1592,16 @@ tool_unbind( LDAP *ld )
        (void) ldap_unbind_ext( ld, NULL, NULL );
 }
 
+void
+tool_exit( LDAP *ld, int status )
+{
+       if ( ld != NULL ) {
+               tool_unbind( ld );
+       }
+       tool_destroy();
+       exit( status );
+}
+
 
 /* Set server controls.  Add controls extra_c[0..count-1], if set. */
 void
@@ -1526,7 +1638,7 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
        ctrls = (LDAPControl**) malloc(sizeof(c) + (count + unknown_ctrls_num + 1)*sizeof(LDAPControl*));
        if ( ctrls == NULL ) {
                fprintf( stderr, "No memory\n" );
-               exit( EXIT_FAILURE );
+               tool_exit( ld, EXIT_FAILURE );
        }
 
        if ( assertctl ) {
@@ -1566,11 +1678,11 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
                ber_init2( ber, NULL, LBER_USE_DER );
 
                if ( ber_printf( ber, "s", proxydn ) == -1 ) {
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                c[i].ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
@@ -1627,13 +1739,13 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
 
                if( ber_printf( ber, "{v}", attrs ) == -1 ) {
                        fprintf( stderr, "preread attrs encode failed.\n" );
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
                if( err < 0 ) {
                        fprintf( stderr, "preread flatten failed (%d)\n", err );
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                c[i].ldctl_oid = LDAP_CONTROL_PRE_READ;
@@ -1657,13 +1769,13 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
 
                if( ber_printf( ber, "{v}", attrs ) == -1 ) {
                        fprintf( stderr, "postread attrs encode failed.\n" );
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                err = ber_flatten2( ber, &c[i].ldctl_value, 0 );
                if( err < 0 ) {
                        fprintf( stderr, "postread flatten failed (%d)\n", err );
-                       exit( EXIT_FAILURE );
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                c[i].ldctl_oid = LDAP_CONTROL_POST_READ;
@@ -1686,7 +1798,7 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
                        if ( err == -1 ) {
                                ber_free( ber, 1 );
                                fprintf( stderr, _("Chaining behavior control encoding error!\n") );
-                               exit( EXIT_FAILURE );
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        if ( chainingContinuation > -1 ) {
@@ -1694,7 +1806,7 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
                                if ( err == -1 ) {
                                        ber_free( ber, 1 );
                                        fprintf( stderr, _("Chaining behavior control encoding error!\n") );
-                                       exit( EXIT_FAILURE );
+                                       tool_exit( ld, EXIT_FAILURE );
                                }
                        }
 
@@ -1702,11 +1814,11 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
                        if ( err == -1 ) {
                                ber_free( ber, 1 );
                                fprintf( stderr, _("Chaining behavior control encoding error!\n") );
-                               exit( EXIT_FAILURE );
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                        if ( ber_flatten2( ber, &c[i].ldctl_value, 0 ) == -1 ) {
-                               exit( EXIT_FAILURE );
+                               tool_exit( ld, EXIT_FAILURE );
                        }
 
                } else {
@@ -1722,8 +1834,8 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
 
 #ifdef LDAP_CONTROL_X_SESSION_TRACKING
        if ( sessionTracking ) {
-               if ( stValue.bv_val == NULL && st_value( ld, &stValue ) ) {
-                       exit( EXIT_FAILURE );
+               if ( BER_BVISNULL( &stValue ) && st_value( ld, &stValue ) ) {
+                       tool_exit( ld, EXIT_FAILURE );
                }
 
                c[i].ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
@@ -1755,7 +1867,7 @@ tool_server_controls( LDAP *ld, LDAPControl *extra_c, int count )
 
        free( ctrls );
        if ( crit ) {
-               exit( EXIT_FAILURE );
+               tool_exit( ld, EXIT_FAILURE );
        }
 }
 
@@ -1808,6 +1920,7 @@ print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
                while ( ber_scanf( ber, "{m" /*}*/, &bv ) != LBER_ERROR ) {
                        int             i;
                        BerVarray       vals = NULL;
+                       char            *str = NULL;
 
                        if ( ber_scanf( ber, "[W]", &vals ) == LBER_ERROR ||
                                vals == NULL )
@@ -1815,14 +1928,25 @@ print_prepostread( LDAP *ld, LDAPControl *ctrl, struct berval *what)
                                /* error? */
                                return 1;
                        }
+
+                       if ( ldif ) {
+                               char *ptr;
+
+                               str = malloc( bv.bv_len + STRLENOF(": ") + 1 );
+
+                               ptr = str;
+                               ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
+                               ptr = lutil_strcopy( ptr, ": " );
+                       }
                
                        for ( i = 0; vals[ i ].bv_val != NULL; i++ ) {
                                tool_write_ldif(
                                        ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                                       bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
+                                       ldif ? str : bv.bv_val, vals[ i ].bv_val, vals[ i ].bv_len );
                        }
 
                        ber_bvarray_free( vals );
+                       if ( str ) free( str );
                }
        }
 
@@ -1902,7 +2026,8 @@ print_paged_results( LDAP *ld, LDAPControl *ctrl )
                }
 
                tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                       "pagedresults", buf, ptr - buf );
+                       ldif ? "pagedresults: " : "pagedresults",
+                       buf, ptr - buf );
        }
 
        return 0;
@@ -1922,7 +2047,7 @@ print_sss( LDAP *ld, LDAPControl *ctrl )
                        err, ldap_err2string(err), attr ? " " : "", attr ? attr : "" );
 
                tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                       "sortResult", buf, rc );
+                       ldif ? "sortResult: " : "sortResult", buf, rc );
        }
 
        return rc;
@@ -1962,7 +2087,7 @@ print_vlv( LDAP *ld, LDAPControl *ctrl )
                        ber_memfree( bv.bv_val );
 
                tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                       "vlvResult", buf, rc );
+                       ldif ? "vlvResult" : "vlvResult", buf, rc );
        }
 
        return rc;
@@ -2012,22 +2137,15 @@ print_deref( LDAP *ld, LDAPControl *ctrl )
                        if ( dv->vals != NULL ) {
                                int j;
                                for ( j = 0; dv->vals[ j ].bv_val != NULL; j++ ) {
-                                       int k;
-
-                                       for ( k = 0; k < dv->vals[ j ].bv_len; k++ ) {
-                                               if ( !isprint( dv->vals[ j ].bv_val[k] ) ) {
-                                                       k = -1;
-                                                       break;
-                                               }
-                                       }
+                                       int k = ldif_is_not_printable( dv->vals[ j ].bv_val, dv->vals[ j ].bv_len );
 
                                        *ptr++ = '<';
                                        ptr = lutil_strcopy( ptr, dv->type );
-                                       if ( k == -1 ) {
+                                       if ( k ) {
                                                *ptr++ = ':';
                                        }
                                        *ptr++ = '=';
-                                       if ( k == -1 ) {
+                                       if ( k ) {
                                                k = lutil_b64_ntop(
                                                        (unsigned char *) dv->vals[ j ].bv_val,
                                                        dv->vals[ j ].bv_len,
@@ -2129,7 +2247,7 @@ print_ppolicy( LDAP *ld, LDAPControl *ctrl )
                }
 
                tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
-                       "ppolicy", buf, ptr - buf );
+                       ldif ? "ppolicy: " : "ppolicy", buf, ptr - buf );
        }
 
        return rc;
@@ -2225,7 +2343,7 @@ tool_write_ldif( int type, char *name, char *value, ber_len_t vallen )
 {
        char    *ldif;
 
-       if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
+       if (( ldif = ldif_put_wrap( type, name, value, vallen, ldif_wrap )) == NULL ) {
                return( -1 );
        }
 
@@ -2265,4 +2383,3 @@ tool_is_oid( const char *s )
 
        return 1;
 }
-