]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/passwd.c
Connect callbacks need error recovery checks
[openldap] / libraries / libldap / passwd.c
index d2d32b58a51284382ed095edda82abbc195ba7c6..0f2e2f13ee9fecb8415b16d3e16265c2a40268f4 100644 (file)
@@ -1,7 +1,20 @@
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2008 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This program was orignally developed by Kurt D. Zeilenga for inclusion in
+ * OpenLDAP Software.
  */
 
 #include "portable.h"
 #include "ldap-int.h"
 
 /*
- * LDAP Password Modify (Extended) Operation <RFC 3???>
+ * LDAP Password Modify (Extended) Operation (RFC 3062)
  */
 
 int ldap_parse_passwd(
        LDAP *ld,
        LDAPMessage *res,
-       struct berval **newpasswd )
+       struct berval *newpasswd )
 {
        int rc;
-       char *retoid = NULL;
-       struct berval *retdata;
+       struct berval *retdata = NULL;
 
        assert( ld != NULL );
        assert( LDAP_VALID( ld ) );
        assert( res != NULL );
        assert( newpasswd != NULL );
 
-       *newpasswd = NULL;
-
-       rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 0 );
+       newpasswd->bv_val = NULL;
+       newpasswd->bv_len = 0;
 
-       if( rc != LDAP_SUCCESS ) {
+       rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
+       if ( rc != LDAP_SUCCESS ) {
                return rc;
        }
 
-       if( retdata != NULL ) {
+       if ( retdata != NULL ) {
                ber_tag_t tag;
                BerElement *ber = ber_init( retdata );
 
-               if( ber == NULL ) {
-                       ld->ld_errno = LDAP_NO_MEMORY;
-                       return ld->ld_errno;
+               if ( ber == NULL ) {
+                       rc = ld->ld_errno = LDAP_NO_MEMORY;
+                       goto done;
                }
 
                /* we should check the tag */
                tag = ber_scanf( ber, "{o}", newpasswd );
                ber_free( ber, 1 );
 
-               if( tag == -1 ) {
+               if ( tag == LBER_ERROR ) {
                        rc = ld->ld_errno = LDAP_DECODING_ERROR;
                }
        }
 
-       ber_memfree( retoid );
+done:;
+       ber_bvfree( retdata );
+
        return rc;
 }
 
@@ -71,7 +85,8 @@ ldap_passwd( LDAP *ld,
        int                             *msgidp )
 {
        int rc;
-       struct berval *bv = NULL;
+       struct berval bv = BER_BVNULL;
+       BerElement *ber = NULL;
 
        assert( ld != NULL );
        assert( LDAP_VALID( ld ) );
@@ -79,7 +94,7 @@ ldap_passwd( LDAP *ld,
 
        if( user != NULL || oldpw != NULL || newpw != NULL ) {
                /* build change password control */
-               BerElement *ber = ber_alloc_t( LBER_USE_DER );
+               ber = ber_alloc_t( LBER_USE_DER );
 
                if( ber == NULL ) {
                        ld->ld_errno = LDAP_NO_MEMORY;
@@ -89,25 +104,23 @@ ldap_passwd( LDAP *ld,
                ber_printf( ber, "{" /*}*/ );
 
                if( user != NULL ) {
-                       ber_printf( ber, "ts",
+                       ber_printf( ber, "tO",
                                LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
                }
 
                if( oldpw != NULL ) {
-                       ber_printf( ber, "ts",
+                       ber_printf( ber, "tO",
                                LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
                }
 
                if( newpw != NULL ) {
-                       ber_printf( ber, "ts",
+                       ber_printf( ber, "tO",
                                LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
                }
 
                ber_printf( ber, /*{*/ "N}" );
 
-               rc = ber_flatten( ber, &bv );
-
-               ber_free( ber, 1 );
+               rc = ber_flatten2( ber, &bv, 0 );
 
                if( rc < 0 ) {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
@@ -117,7 +130,9 @@ ldap_passwd( LDAP *ld,
        }
        
        rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
-               bv, sctrls, cctrls, msgidp );
+               bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
+
+       ber_free( ber, 1 );
 
        return rc;
 }
@@ -128,7 +143,7 @@ ldap_passwd_s(
        struct berval   *user,
        struct berval   *oldpw,
        struct berval   *newpw,
-       struct berval **newpasswd,
+       struct berval *newpasswd,
        LDAPControl **sctrls,
        LDAPControl **cctrls )
 {
@@ -141,11 +156,15 @@ ldap_passwd_s(
                return rc;
        }
 
-       if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
+       if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
                return ld->ld_errno;
        }
 
        rc = ldap_parse_passwd( ld, res, newpasswd );
-       ldap_msgfree( res );
-       return rc;
+       if( rc != LDAP_SUCCESS ) {
+               ldap_msgfree( res );
+               return rc;
+       }
+
+       return( ldap_result2error( ld, res, 1 ) );
 }