]> git.sur5r.net Git - openldap/blobdiff - clients/tools/ldappasswd.c
ITS#7477 check for invalid LDIF
[openldap] / clients / tools / ldappasswd.c
index 4390caaba264cd729d56af36bd0a27910885b775..e4285f46d14f95ab698a0194e7fe31d0ba7c5cef 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2006 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
  * Portions Copyright 1998-2001 Net Boolean Incorporated.
  * Portions Copyright 2001-2003 IBM Corporation.
@@ -81,7 +81,7 @@ usage( void )
 
 
 const char options[] = "a:As:St:T:"
-       "d:D:e:h:H:InO:p:QR:U:vVw:WxX:y:Y:Z";
+       "d:D:e:h:H:InNO:o:p:QR:U:vVw:WxX:y:Y:Z";
 
 int
 handle_private_option( int i )
@@ -177,6 +177,7 @@ main( int argc, char *argv[] )
        char *matcheddn = NULL, *text = NULL, **refs = NULL;
        char    *retoid = NULL;
        struct berval *retdata = NULL;
+       LDAPControl **ctrls = NULL;
 
     tool_init( TOOL_PASSWD );
        prog = lutil_progname( "ldappasswd", argc, argv );
@@ -244,28 +245,12 @@ main( int argc, char *argv[] )
                newpw.bv_len = strlen( newpw.bv_val );
        }
 
-       if ( pw_file ) {
-               rc = lutil_get_filed_password( pw_file, &passwd );
-               if( rc ) {
-                       rc = EXIT_FAILURE;
-                       goto done;
-               }
-
-       } else if ( want_bindpw ) {
-               passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
-               passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
-       }
-
        ld = tool_conn_setup( 0, 0 );
 
        tool_bind( ld );
 
-       if ( assertion || authzid || manageDSAit || noop ) {
-               tool_server_controls( ld, NULL, 0 );
-       }
-
        if( user != NULL || oldpw.bv_val != NULL || newpw.bv_val != NULL ) {
-               /* build change password control */
+               /* build the password modify request data */
                ber = ber_alloc_t( LBER_USE_DER );
 
                if( ber == NULL ) {
@@ -310,6 +295,8 @@ main( int argc, char *argv[] )
                goto done;
        }
 
+       tool_server_controls( ld, NULL, 0);
+
        rc = ldap_extended_operation( ld,
                LDAP_EXOP_MODIFY_PASSWD, bv.bv_val ? &bv : NULL, 
                NULL, NULL, &id );
@@ -326,7 +313,7 @@ main( int argc, char *argv[] )
                struct timeval  tv;
 
                if ( tool_check_abandon( ld, id ) ) {
-                       return LDAP_CANCELLED;
+                       tool_exit( ld, LDAP_CANCELLED );
                }
 
                tv.tv_sec = 0;
@@ -335,7 +322,7 @@ main( int argc, char *argv[] )
                rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
                if ( rc < 0 ) {
                        tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
-                       return rc;
+                       tool_exit( ld, rc );
                }
 
                if ( rc != 0 ) {
@@ -344,7 +331,7 @@ main( int argc, char *argv[] )
        }
 
        rc = ldap_parse_result( ld, res,
-               &code, &matcheddn, &text, &refs, NULL, 0 );
+               &code, &matcheddn, &text, &refs, &ctrls, 0 );
        if( rc != LDAP_SUCCESS ) {
                tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
                rc = EXIT_FAILURE;
@@ -376,17 +363,19 @@ main( int argc, char *argv[] )
                        perror( "ber_scanf" );
                } else {
                        printf(_("New password: %s\n"), s);
-                       free( s );
+                       ber_memfree( s );
                }
 
                ber_free( ber, 1 );
 
-       } else if ( newpw.bv_val == NULL ) {
+       } else if ( code == LDAP_SUCCESS && newpw.bv_val == NULL ) {
                tool_perror( "ldap_parse_extended_result", LDAP_DECODING_ERROR,
                        " new password expected", NULL, NULL, NULL );
        }
 
-       if( verbose || code != LDAP_SUCCESS || matcheddn || text || refs ) {
+       if( verbose || code != LDAP_SUCCESS ||
+               ( matcheddn && *matcheddn ) || ( text && *text ) || refs || ctrls )
+       {
                printf( _("Result: %s (%d)\n"), ldap_err2string( code ), code );
 
                if( text && *text ) {
@@ -403,6 +392,11 @@ main( int argc, char *argv[] )
                                printf(_("Referral: %s\n"), refs[i] );
                        }
                }
+
+               if( ctrls ) {
+                       tool_print_ctrls( ld, ctrls );
+                       ldap_controls_free( ctrls );
+               }
        }
 
        ber_memfree( text );
@@ -415,8 +409,5 @@ main( int argc, char *argv[] )
 
 done:
        /* disconnect from server */
-       if ( ld )
-               tool_unbind( ld ); 
-       tool_destroy();
-       return rc;
+       tool_exit( ld, rc ); 
 }