17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
37c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
47c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
57c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
87c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
97c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
107c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
137c478bd9Sstevel@tonic-gate  * March 31, 1998.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
167c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
177c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
187c478bd9Sstevel@tonic-gate  * Rights Reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Contributor(s):
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
247c478bd9Sstevel@tonic-gate  *  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *  modify.c
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #if 0
31*1da57d55SToomas Soome #ifndef lint
327c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "ldap-int.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * ldap_modify - initiate an ldap modify operation.  Parameters:
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	ld		LDAP descriptor
427c478bd9Sstevel@tonic-gate  *	dn		DN of the object to modify
437c478bd9Sstevel@tonic-gate  *	mods		List of modifications to make.  This is null-terminated
447c478bd9Sstevel@tonic-gate  *			array of struct ldapmod's, specifying the modifications
457c478bd9Sstevel@tonic-gate  *			to perform.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * Example:
48*1da57d55SToomas Soome  *	LDAPMod	*mods[] = {
497c478bd9Sstevel@tonic-gate  *			{ LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
507c478bd9Sstevel@tonic-gate  *			{ LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
517c478bd9Sstevel@tonic-gate  *			0
527c478bd9Sstevel@tonic-gate  *		}
537c478bd9Sstevel@tonic-gate  *	msgid = ldap_modify( ld, dn, mods );
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate int
567c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modify(LDAP * ld,const char * dn,LDAPMod ** mods)577c478bd9Sstevel@tonic-gate ldap_modify( LDAP *ld, const char *dn, LDAPMod **mods )
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	int		msgid;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_modify\n", 0, 0, 0 );
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if ( ldap_modify_ext( ld, dn, mods, NULL, NULL, &msgid )
647c478bd9Sstevel@tonic-gate 	    == LDAP_SUCCESS ) {
657c478bd9Sstevel@tonic-gate 		return( msgid );
667c478bd9Sstevel@tonic-gate 	} else {
677c478bd9Sstevel@tonic-gate 		return( -1 );	/* error is in ld handle */
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate int
727c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modify_ext(LDAP * ld,const char * dn,LDAPMod ** mods,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)737c478bd9Sstevel@tonic-gate ldap_modify_ext( LDAP *ld, const char *dn, LDAPMod **mods,
747c478bd9Sstevel@tonic-gate     LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp )
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	BerElement	*ber;
777c478bd9Sstevel@tonic-gate 	int		i, rc, lderr;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * A modify request looks like this:
817c478bd9Sstevel@tonic-gate 	 *	ModifyRequet ::= SEQUENCE {
827c478bd9Sstevel@tonic-gate 	 *		object		DistinguishedName,
837c478bd9Sstevel@tonic-gate 	 *		modifications	SEQUENCE OF SEQUENCE {
847c478bd9Sstevel@tonic-gate 	 *			operation	ENUMERATED {
857c478bd9Sstevel@tonic-gate 	 *				add	(0),
867c478bd9Sstevel@tonic-gate 	 *				delete	(1),
877c478bd9Sstevel@tonic-gate 	 *				replace	(2)
887c478bd9Sstevel@tonic-gate 	 *			},
897c478bd9Sstevel@tonic-gate 	 *			modification	SEQUENCE {
907c478bd9Sstevel@tonic-gate 	 *				type	AttributeType,
917c478bd9Sstevel@tonic-gate 	 *				values	SET OF AttributeValue
927c478bd9Sstevel@tonic-gate 	 *			}
937c478bd9Sstevel@tonic-gate 	 *		}
947c478bd9Sstevel@tonic-gate 	 *	}
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_modify_ext\n", 0, 0, 0 );
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1007c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
1017c478bd9Sstevel@tonic-gate 	}
102*1da57d55SToomas Soome 	if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp ))
1037c478bd9Sstevel@tonic-gate         {
1047c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1057c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
1067c478bd9Sstevel@tonic-gate 	}
107*1da57d55SToomas Soome 
1087c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( mods )) {
1097c478bd9Sstevel@tonic-gate 		lderr = LDAP_PARAM_ERROR;
1107c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1117c478bd9Sstevel@tonic-gate 		return( lderr );
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
1147c478bd9Sstevel@tonic-gate 		dn = "";
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1187c478bd9Sstevel@tonic-gate 	*msgidp = ++ld->ld_msgid;
1197c478bd9Sstevel@tonic-gate 	LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/* see if we should add to the cache */
1227c478bd9Sstevel@tonic-gate 	if ( ld->ld_cache_on && ld->ld_cache_modify != NULL ) {
1237c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1247c478bd9Sstevel@tonic-gate 		if ( (rc = (ld->ld_cache_modify)( ld, *msgidp, LDAP_REQ_MODIFY,
1257c478bd9Sstevel@tonic-gate 		    dn, mods )) != 0 ) {
1267c478bd9Sstevel@tonic-gate 			*msgidp = rc;
1277c478bd9Sstevel@tonic-gate 			LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1287c478bd9Sstevel@tonic-gate 			return( LDAP_SUCCESS );
1297c478bd9Sstevel@tonic-gate 		}
1307c478bd9Sstevel@tonic-gate 		LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/* create a message to send */
1347c478bd9Sstevel@tonic-gate 	if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber ))
1357c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
1367c478bd9Sstevel@tonic-gate 		return( lderr );
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{s{", *msgidp, LDAP_REQ_MODIFY, dn )
1407c478bd9Sstevel@tonic-gate 	    == -1 ) {
1417c478bd9Sstevel@tonic-gate 		lderr = LDAP_ENCODING_ERROR;
1427c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1437c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1447c478bd9Sstevel@tonic-gate 		return( lderr );
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* for each modification to be performed... */
1487c478bd9Sstevel@tonic-gate 	for ( i = 0; mods[i] != NULL; i++ ) {
1497c478bd9Sstevel@tonic-gate 		if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
1507c478bd9Sstevel@tonic-gate 			rc = ber_printf( ber, "{e{s[V]}}",
1517c478bd9Sstevel@tonic-gate 			    mods[i]->mod_op & ~LDAP_MOD_BVALUES,
1527c478bd9Sstevel@tonic-gate 			    mods[i]->mod_type, mods[i]->mod_bvalues );
1537c478bd9Sstevel@tonic-gate 		} else {
1547c478bd9Sstevel@tonic-gate 			rc = ber_printf( ber, "{e{s[v]}}", mods[i]->mod_op,
1557c478bd9Sstevel@tonic-gate 			    mods[i]->mod_type, mods[i]->mod_values );
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		if ( rc == -1 ) {
1597c478bd9Sstevel@tonic-gate 			lderr = LDAP_ENCODING_ERROR;
1607c478bd9Sstevel@tonic-gate 			LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1617c478bd9Sstevel@tonic-gate 			ber_free( ber, 1 );
1627c478bd9Sstevel@tonic-gate 			return( lderr );
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if ( ber_printf( ber, "}}" ) == -1 ) {
1677c478bd9Sstevel@tonic-gate 		lderr = LDAP_ENCODING_ERROR;
1687c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
1697c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1707c478bd9Sstevel@tonic-gate 		return( lderr );
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (( lderr = nsldapi_put_controls( ld, serverctrls, 1, ber ))
1747c478bd9Sstevel@tonic-gate 	    != LDAP_SUCCESS ) {
1757c478bd9Sstevel@tonic-gate 		ber_free( ber, 1 );
1767c478bd9Sstevel@tonic-gate 		return( lderr );
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* send the message */
1807c478bd9Sstevel@tonic-gate 	rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_MODIFY,
1817c478bd9Sstevel@tonic-gate 		(char *)dn, ber );
1827c478bd9Sstevel@tonic-gate 	*msgidp = rc;
1837c478bd9Sstevel@tonic-gate 	return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate int
1877c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modify_s(LDAP * ld,const char * dn,LDAPMod ** mods)1887c478bd9Sstevel@tonic-gate ldap_modify_s( LDAP *ld, const char *dn, LDAPMod **mods )
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	return( ldap_modify_ext_s( ld, dn, mods, NULL, NULL ));
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate int
1947c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_modify_ext_s(LDAP * ld,const char * dn,LDAPMod ** mods,LDAPControl ** serverctrls,LDAPControl ** clientctrls)1957c478bd9Sstevel@tonic-gate ldap_modify_ext_s( LDAP *ld, const char *dn, LDAPMod **mods,
1967c478bd9Sstevel@tonic-gate     LDAPControl **serverctrls, LDAPControl **clientctrls )
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	int		msgid, err;
1997c478bd9Sstevel@tonic-gate 	LDAPMessage	*res;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (( err = ldap_modify_ext( ld, dn, mods, serverctrls, clientctrls,
2027c478bd9Sstevel@tonic-gate 	    &msgid )) != LDAP_SUCCESS ) {
2037c478bd9Sstevel@tonic-gate 		return( err );
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res ) == -1 ) {
2077c478bd9Sstevel@tonic-gate 		return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
2117c478bd9Sstevel@tonic-gate }
212