Home | History | Annotate | Line # | Download | only in libldap
passwd.c revision 1.1
      1  1.1  lukem /* $OpenLDAP: pkg/ldap/libraries/libldap/passwd.c,v 1.18.2.3 2008/02/11 23:26:41 kurt Exp $ */
      2  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1  lukem  *
      4  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      8  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
      9  1.1  lukem  * Public License.
     10  1.1  lukem  *
     11  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     12  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     13  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     14  1.1  lukem  */
     15  1.1  lukem /* ACKNOWLEDGEMENTS:
     16  1.1  lukem  * This program was orignally developed by Kurt D. Zeilenga for inclusion in
     17  1.1  lukem  * OpenLDAP Software.
     18  1.1  lukem  */
     19  1.1  lukem 
     20  1.1  lukem #include "portable.h"
     21  1.1  lukem 
     22  1.1  lukem #include <stdio.h>
     23  1.1  lukem #include <ac/stdlib.h>
     24  1.1  lukem #include <ac/string.h>
     25  1.1  lukem #include <ac/time.h>
     26  1.1  lukem 
     27  1.1  lukem #include "ldap-int.h"
     28  1.1  lukem 
     29  1.1  lukem /*
     30  1.1  lukem  * LDAP Password Modify (Extended) Operation (RFC 3062)
     31  1.1  lukem  */
     32  1.1  lukem 
     33  1.1  lukem int ldap_parse_passwd(
     34  1.1  lukem 	LDAP *ld,
     35  1.1  lukem 	LDAPMessage *res,
     36  1.1  lukem 	struct berval *newpasswd )
     37  1.1  lukem {
     38  1.1  lukem 	int rc;
     39  1.1  lukem 	struct berval *retdata = NULL;
     40  1.1  lukem 
     41  1.1  lukem 	assert( ld != NULL );
     42  1.1  lukem 	assert( LDAP_VALID( ld ) );
     43  1.1  lukem 	assert( res != NULL );
     44  1.1  lukem 	assert( newpasswd != NULL );
     45  1.1  lukem 
     46  1.1  lukem 	newpasswd->bv_val = NULL;
     47  1.1  lukem 	newpasswd->bv_len = 0;
     48  1.1  lukem 
     49  1.1  lukem 	rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
     50  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
     51  1.1  lukem 		return rc;
     52  1.1  lukem 	}
     53  1.1  lukem 
     54  1.1  lukem 	if ( retdata != NULL ) {
     55  1.1  lukem 		ber_tag_t tag;
     56  1.1  lukem 		BerElement *ber = ber_init( retdata );
     57  1.1  lukem 
     58  1.1  lukem 		if ( ber == NULL ) {
     59  1.1  lukem 			rc = ld->ld_errno = LDAP_NO_MEMORY;
     60  1.1  lukem 			goto done;
     61  1.1  lukem 		}
     62  1.1  lukem 
     63  1.1  lukem 		/* we should check the tag */
     64  1.1  lukem 		tag = ber_scanf( ber, "{o}", newpasswd );
     65  1.1  lukem 		ber_free( ber, 1 );
     66  1.1  lukem 
     67  1.1  lukem 		if ( tag == LBER_ERROR ) {
     68  1.1  lukem 			rc = ld->ld_errno = LDAP_DECODING_ERROR;
     69  1.1  lukem 		}
     70  1.1  lukem 	}
     71  1.1  lukem 
     72  1.1  lukem done:;
     73  1.1  lukem 	ber_bvfree( retdata );
     74  1.1  lukem 
     75  1.1  lukem 	return rc;
     76  1.1  lukem }
     77  1.1  lukem 
     78  1.1  lukem int
     79  1.1  lukem ldap_passwd( LDAP *ld,
     80  1.1  lukem 	struct berval	*user,
     81  1.1  lukem 	struct berval	*oldpw,
     82  1.1  lukem 	struct berval	*newpw,
     83  1.1  lukem 	LDAPControl		**sctrls,
     84  1.1  lukem 	LDAPControl		**cctrls,
     85  1.1  lukem 	int				*msgidp )
     86  1.1  lukem {
     87  1.1  lukem 	int rc;
     88  1.1  lukem 	struct berval bv = BER_BVNULL;
     89  1.1  lukem 	BerElement *ber = NULL;
     90  1.1  lukem 
     91  1.1  lukem 	assert( ld != NULL );
     92  1.1  lukem 	assert( LDAP_VALID( ld ) );
     93  1.1  lukem 	assert( msgidp != NULL );
     94  1.1  lukem 
     95  1.1  lukem 	if( user != NULL || oldpw != NULL || newpw != NULL ) {
     96  1.1  lukem 		/* build change password control */
     97  1.1  lukem 		ber = ber_alloc_t( LBER_USE_DER );
     98  1.1  lukem 
     99  1.1  lukem 		if( ber == NULL ) {
    100  1.1  lukem 			ld->ld_errno = LDAP_NO_MEMORY;
    101  1.1  lukem 			return ld->ld_errno;
    102  1.1  lukem 		}
    103  1.1  lukem 
    104  1.1  lukem 		ber_printf( ber, "{" /*}*/ );
    105  1.1  lukem 
    106  1.1  lukem 		if( user != NULL ) {
    107  1.1  lukem 			ber_printf( ber, "tO",
    108  1.1  lukem 				LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
    109  1.1  lukem 		}
    110  1.1  lukem 
    111  1.1  lukem 		if( oldpw != NULL ) {
    112  1.1  lukem 			ber_printf( ber, "tO",
    113  1.1  lukem 				LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
    114  1.1  lukem 		}
    115  1.1  lukem 
    116  1.1  lukem 		if( newpw != NULL ) {
    117  1.1  lukem 			ber_printf( ber, "tO",
    118  1.1  lukem 				LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
    119  1.1  lukem 		}
    120  1.1  lukem 
    121  1.1  lukem 		ber_printf( ber, /*{*/ "N}" );
    122  1.1  lukem 
    123  1.1  lukem 		rc = ber_flatten2( ber, &bv, 0 );
    124  1.1  lukem 
    125  1.1  lukem 		if( rc < 0 ) {
    126  1.1  lukem 			ld->ld_errno = LDAP_ENCODING_ERROR;
    127  1.1  lukem 			return ld->ld_errno;
    128  1.1  lukem 		}
    129  1.1  lukem 
    130  1.1  lukem 	}
    131  1.1  lukem 
    132  1.1  lukem 	rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
    133  1.1  lukem 		bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
    134  1.1  lukem 
    135  1.1  lukem 	ber_free( ber, 1 );
    136  1.1  lukem 
    137  1.1  lukem 	return rc;
    138  1.1  lukem }
    139  1.1  lukem 
    140  1.1  lukem int
    141  1.1  lukem ldap_passwd_s(
    142  1.1  lukem 	LDAP *ld,
    143  1.1  lukem 	struct berval	*user,
    144  1.1  lukem 	struct berval	*oldpw,
    145  1.1  lukem 	struct berval	*newpw,
    146  1.1  lukem 	struct berval *newpasswd,
    147  1.1  lukem 	LDAPControl **sctrls,
    148  1.1  lukem 	LDAPControl **cctrls )
    149  1.1  lukem {
    150  1.1  lukem 	int		rc;
    151  1.1  lukem 	int		msgid;
    152  1.1  lukem 	LDAPMessage	*res;
    153  1.1  lukem 
    154  1.1  lukem 	rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
    155  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
    156  1.1  lukem 		return rc;
    157  1.1  lukem 	}
    158  1.1  lukem 
    159  1.1  lukem 	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
    160  1.1  lukem 		return ld->ld_errno;
    161  1.1  lukem 	}
    162  1.1  lukem 
    163  1.1  lukem 	rc = ldap_parse_passwd( ld, res, newpasswd );
    164  1.1  lukem 	if( rc != LDAP_SUCCESS ) {
    165  1.1  lukem 		ldap_msgfree( res );
    166  1.1  lukem 		return rc;
    167  1.1  lukem 	}
    168  1.1  lukem 
    169  1.1  lukem 	return( ldap_result2error( ld, res, 1 ) );
    170  1.1  lukem }
    171