2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This program was orignally developed by Kurt D. Zeilenga for inclusion in
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
30 * LDAP Password Modify (Extended) Operation <RFC 3062>
33 int ldap_parse_passwd(
36 struct berval *newpasswd )
39 struct berval *retdata = NULL;
42 assert( LDAP_VALID( ld ) );
43 assert( res != NULL );
44 assert( newpasswd != NULL );
46 newpasswd->bv_val = NULL;
47 newpasswd->bv_len = 0;
49 rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
50 if ( rc != LDAP_SUCCESS ) {
54 if ( retdata != NULL ) {
56 BerElement *ber = ber_init( retdata );
59 rc = ld->ld_errno = LDAP_NO_MEMORY;
63 /* we should check the tag */
64 tag = ber_scanf( ber, "{o}", newpasswd );
67 if ( tag == LBER_ERROR ) {
68 rc = ld->ld_errno = LDAP_DECODING_ERROR;
73 ber_bvfree( retdata );
79 ldap_passwd( LDAP *ld,
88 struct berval bv = BER_BVNULL;
89 BerElement *ber = NULL;
92 assert( LDAP_VALID( ld ) );
93 assert( msgidp != NULL );
95 if( user != NULL || oldpw != NULL || newpw != NULL ) {
96 /* build change password control */
97 ber = ber_alloc_t( LBER_USE_DER );
100 ld->ld_errno = LDAP_NO_MEMORY;
104 ber_printf( ber, "{" /*}*/ );
107 ber_printf( ber, "tO",
108 LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
111 if( oldpw != NULL ) {
112 ber_printf( ber, "tO",
113 LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
116 if( newpw != NULL ) {
117 ber_printf( ber, "tO",
118 LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
121 ber_printf( ber, /*{*/ "N}" );
123 rc = ber_flatten2( ber, &bv, 0 );
126 ld->ld_errno = LDAP_ENCODING_ERROR;
132 rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
133 bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
144 struct berval *oldpw,
145 struct berval *newpw,
146 struct berval *newpasswd,
147 LDAPControl **sctrls,
148 LDAPControl **cctrls )
154 rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
155 if ( rc != LDAP_SUCCESS ) {
159 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 ) {
163 rc = ldap_parse_passwd( ld, res, newpasswd );
164 if( rc != LDAP_SUCCESS ) {
169 return( ldap_result2error( ld, res, 1 ) );