Back to index

openldap  2.4.31
passwd.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* ACKNOWLEDGEMENTS:
00016  * This program was orignally developed by Kurt D. Zeilenga for inclusion in
00017  * OpenLDAP Software.
00018  */
00019 
00020 #include "portable.h"
00021 
00022 #include <stdio.h>
00023 #include <ac/stdlib.h>
00024 #include <ac/string.h>
00025 #include <ac/time.h>
00026 
00027 #include "ldap-int.h"
00028 
00029 /*
00030  * LDAP Password Modify (Extended) Operation (RFC 3062)
00031  */
00032 
00033 int ldap_parse_passwd(
00034        LDAP *ld,
00035        LDAPMessage *res,
00036        struct berval *newpasswd )
00037 {
00038        int rc;
00039        struct berval *retdata = NULL;
00040 
00041        assert( ld != NULL );
00042        assert( LDAP_VALID( ld ) );
00043        assert( res != NULL );
00044        assert( newpasswd != NULL );
00045 
00046        newpasswd->bv_val = NULL;
00047        newpasswd->bv_len = 0;
00048 
00049        rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
00050        if ( rc != LDAP_SUCCESS ) {
00051               return rc;
00052        }
00053 
00054        if ( retdata != NULL ) {
00055               ber_tag_t tag;
00056               BerElement *ber = ber_init( retdata );
00057 
00058               if ( ber == NULL ) {
00059                      rc = ld->ld_errno = LDAP_NO_MEMORY;
00060                      goto done;
00061               }
00062 
00063               /* we should check the tag */
00064               tag = ber_scanf( ber, "{o}", newpasswd );
00065               ber_free( ber, 1 );
00066 
00067               if ( tag == LBER_ERROR ) {
00068                      rc = ld->ld_errno = LDAP_DECODING_ERROR;
00069               }
00070        }
00071 
00072 done:;
00073        ber_bvfree( retdata );
00074 
00075        return rc;
00076 }
00077 
00078 int
00079 ldap_passwd( LDAP *ld,
00080        struct berval *user,
00081        struct berval *oldpw,
00082        struct berval *newpw,
00083        LDAPControl          **sctrls,
00084        LDAPControl          **cctrls,
00085        int                         *msgidp )
00086 {
00087        int rc;
00088        struct berval bv = BER_BVNULL;
00089        BerElement *ber = NULL;
00090 
00091        assert( ld != NULL );
00092        assert( LDAP_VALID( ld ) );
00093        assert( msgidp != NULL );
00094 
00095        if( user != NULL || oldpw != NULL || newpw != NULL ) {
00096               /* build change password control */
00097               ber = ber_alloc_t( LBER_USE_DER );
00098 
00099               if( ber == NULL ) {
00100                      ld->ld_errno = LDAP_NO_MEMORY;
00101                      return ld->ld_errno;
00102               }
00103 
00104               ber_printf( ber, "{" /*}*/ );
00105 
00106               if( user != NULL ) {
00107                      ber_printf( ber, "tO",
00108                             LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
00109               }
00110 
00111               if( oldpw != NULL ) {
00112                      ber_printf( ber, "tO",
00113                             LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
00114               }
00115 
00116               if( newpw != NULL ) {
00117                      ber_printf( ber, "tO",
00118                             LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
00119               }
00120 
00121               ber_printf( ber, /*{*/ "N}" );
00122 
00123               rc = ber_flatten2( ber, &bv, 0 );
00124 
00125               if( rc < 0 ) {
00126                      ld->ld_errno = LDAP_ENCODING_ERROR;
00127                      return ld->ld_errno;
00128               }
00129 
00130        }
00131        
00132        rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
00133               bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
00134 
00135        ber_free( ber, 1 );
00136 
00137        return rc;
00138 }
00139 
00140 int
00141 ldap_passwd_s(
00142        LDAP *ld,
00143        struct berval *user,
00144        struct berval *oldpw,
00145        struct berval *newpw,
00146        struct berval *newpasswd,
00147        LDAPControl **sctrls,
00148        LDAPControl **cctrls )
00149 {
00150        int           rc;
00151        int           msgid;
00152        LDAPMessage   *res;
00153 
00154        rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
00155        if ( rc != LDAP_SUCCESS ) {
00156               return rc;
00157        }
00158 
00159        if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
00160               return ld->ld_errno;
00161        }
00162 
00163        rc = ldap_parse_passwd( ld, res, newpasswd );
00164        if( rc != LDAP_SUCCESS ) {
00165               ldap_msgfree( res );
00166               return rc;
00167        }
00168 
00169        return( ldap_result2error( ld, res, 1 ) );
00170 }