xref: /onnv-gate/usr/src/lib/libldap4/common/modify.c (revision 3857:21b9b714e4ab)
10Sstevel@tonic-gate /*
2*3857Sstevel  * Portions Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
3*3857Sstevel  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
100Sstevel@tonic-gate  *  All rights reserved.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  *  modify.c
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #ifndef lint
160Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
170Sstevel@tonic-gate #endif
180Sstevel@tonic-gate 
190Sstevel@tonic-gate #include <stdio.h>
200Sstevel@tonic-gate #include <string.h>
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #ifdef MACOS
230Sstevel@tonic-gate #include "macos.h"
240Sstevel@tonic-gate #endif /* MACOS */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #if !defined( MACOS ) && !defined( DOS )
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/socket.h>
290Sstevel@tonic-gate #endif
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include "lber.h"
320Sstevel@tonic-gate #include "ldap.h"
330Sstevel@tonic-gate #include "ldap-private.h"
340Sstevel@tonic-gate #include "ldap-int.h"
350Sstevel@tonic-gate 
ldap_build_modify_req(LDAP * ld,char * dn,LDAPMod ** mods,LDAPControl ** serverctrls)360Sstevel@tonic-gate BerElement * ldap_build_modify_req(LDAP *ld, char *dn, LDAPMod ** mods, LDAPControl **serverctrls)
370Sstevel@tonic-gate {
380Sstevel@tonic-gate 	BerElement	*ber;
390Sstevel@tonic-gate 	int		i, rc;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	/*
420Sstevel@tonic-gate 	 * A modify request looks like this:
430Sstevel@tonic-gate 	 *	ModifyRequest ::= SEQUENCE {
440Sstevel@tonic-gate 	 *		object		DistinguishedName,
450Sstevel@tonic-gate 	 *		modifications	SEQUENCE OF SEQUENCE {
460Sstevel@tonic-gate 	 *			operation	ENUMERATED {
470Sstevel@tonic-gate 	 *				add	(0),
480Sstevel@tonic-gate 	 *				delete	(1),
490Sstevel@tonic-gate 	 *				replace	(2)
500Sstevel@tonic-gate 	 *			},
510Sstevel@tonic-gate 	 *			modification	SEQUENCE {
520Sstevel@tonic-gate 	 *				type	AttributeType,
530Sstevel@tonic-gate 	 *				values	SET OF AttributeValue
540Sstevel@tonic-gate 	 *			}
550Sstevel@tonic-gate 	 *		}
560Sstevel@tonic-gate 	 *	}
570Sstevel@tonic-gate 	 */
580Sstevel@tonic-gate 	/* create a message to send */
590Sstevel@tonic-gate 	if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
600Sstevel@tonic-gate 		return( NULLBER );
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 	if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_MODIFY, dn )
630Sstevel@tonic-gate 	    == -1 ) {
640Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
650Sstevel@tonic-gate 		ber_free( ber, 1 );
660Sstevel@tonic-gate 		return( NULLBER );
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	/* for each modification to be performed... */
700Sstevel@tonic-gate 	for ( i = 0; mods[i] != NULL; i++ ) {
710Sstevel@tonic-gate 		if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
720Sstevel@tonic-gate 			rc = ber_printf( ber, "{e{s[V]}}",
730Sstevel@tonic-gate 			    mods[i]->mod_op & ~LDAP_MOD_BVALUES,
740Sstevel@tonic-gate 			    mods[i]->mod_type, mods[i]->mod_bvalues );
750Sstevel@tonic-gate 		} else {
760Sstevel@tonic-gate 			rc = ber_printf( ber, "{e{s[v]}}", mods[i]->mod_op,
770Sstevel@tonic-gate 			    mods[i]->mod_type, mods[i]->mod_values );
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		if ( rc == -1 ) {
810Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
820Sstevel@tonic-gate 			ber_free( ber, 1 );
830Sstevel@tonic-gate 			return( NULLBER );
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if ( ber_printf( ber, "}}" ) == -1 ) {
880Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
890Sstevel@tonic-gate 		ber_free( ber, 1 );
900Sstevel@tonic-gate 		return( NULLBER );
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	/* LDAPv3 */
940Sstevel@tonic-gate 	/* Code controls if any */
950Sstevel@tonic-gate 	if (serverctrls && serverctrls[0]) {
960Sstevel@tonic-gate 		if (ldap_controls_code(ber, serverctrls) != LDAP_SUCCESS){
970Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
980Sstevel@tonic-gate 			ber_free( ber, 1 );
990Sstevel@tonic-gate 			return( NULLBER );
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 	} else if (ld->ld_srvctrls && ld->ld_srvctrls[0]) {
1020Sstevel@tonic-gate 		/* Otherwise, is there any global server ctrls ? */
1030Sstevel@tonic-gate 		if (ldap_controls_code(ber, ld->ld_srvctrls) != LDAP_SUCCESS){
1040Sstevel@tonic-gate 			ld->ld_errno = LDAP_ENCODING_ERROR;
1050Sstevel@tonic-gate 			ber_free( ber, 1 );
1060Sstevel@tonic-gate 			return( NULLBER );
1070Sstevel@tonic-gate 		}
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if ( ber_printf( ber, "}" ) == -1 ) {
1110Sstevel@tonic-gate 		ld->ld_errno = LDAP_ENCODING_ERROR;
1120Sstevel@tonic-gate 		ber_free( ber, 1 );
1130Sstevel@tonic-gate 		return( NULLBER );
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 	return (ber);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * ldap_modify - initiate an ldap (and X.500) modify operation.  Parameters:
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  *	ld		LDAP descriptor
1220Sstevel@tonic-gate  *	dn		DN of the object to modify
1230Sstevel@tonic-gate  *	mods		List of modifications to make.  This is null-terminated
1240Sstevel@tonic-gate  *			array of struct ldapmod's, specifying the modifications
1250Sstevel@tonic-gate  *			to perform.
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * Example:
1280Sstevel@tonic-gate  *	LDAPMod	*mods[] = {
1290Sstevel@tonic-gate  *			{ LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
1300Sstevel@tonic-gate  *			{ LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
1310Sstevel@tonic-gate  *			0
1320Sstevel@tonic-gate  *		}
1330Sstevel@tonic-gate  *	msgid = ldap_modify( ld, dn, mods );
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate int
ldap_modify(LDAP * ld,char * dn,LDAPMod ** mods)1360Sstevel@tonic-gate ldap_modify( LDAP *ld, char *dn, LDAPMod **mods )
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	BerElement	*ber;
1390Sstevel@tonic-gate 	int rv;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #ifdef _REENTRANT
1420Sstevel@tonic-gate 	LOCK_LDAP(ld);
1430Sstevel@tonic-gate #endif
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 192, "ldap_modify\n"), 0, 0, 0 );
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((ber = ldap_build_modify_req(ld, dn, mods, NULL)) == NULLBER){
1480Sstevel@tonic-gate #ifdef _REENTRANT
1490Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate 		return (-1);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* send the message */
1550Sstevel@tonic-gate 	rv =  send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber );
1560Sstevel@tonic-gate #ifdef _REENTRANT
1570Sstevel@tonic-gate         UNLOCK_LDAP(ld);
1580Sstevel@tonic-gate #endif
1590Sstevel@tonic-gate 	return ( rv );
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate int
ldap_modify_s(LDAP * ld,char * dn,LDAPMod ** mods)1630Sstevel@tonic-gate ldap_modify_s( LDAP *ld, char *dn, LDAPMod **mods )
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	int		msgid;
1660Sstevel@tonic-gate 	LDAPMessage	*res;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if ( (msgid = ldap_modify( ld, dn, mods )) == -1 )
1690Sstevel@tonic-gate 		return( ld->ld_errno );
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
1720Sstevel@tonic-gate 		return( ld->ld_errno );
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	return( ldap_result2error( ld, res, 1 ) );
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /* ldapv3 API extensions */
1780Sstevel@tonic-gate 
ldap_modify_ext(LDAP * ld,char * dn,LDAPMod ** mods,LDAPControl ** serverctrls,LDAPControl ** clientctrls,int * msgidp)1790Sstevel@tonic-gate int ldap_modify_ext(LDAP *ld, char *dn, LDAPMod **mods,
1800Sstevel@tonic-gate 					LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	BerElement	*ber;
1830Sstevel@tonic-gate 	int		i, rc;
1840Sstevel@tonic-gate 	int rv;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #ifdef _REENTRANT
1870Sstevel@tonic-gate 	LOCK_LDAP(ld);
1880Sstevel@tonic-gate #endif
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, catgets(slapdcat, 1, 192, "ldap_modify\n"), 0, 0, 0 );
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if ((ber = ldap_build_modify_req(ld, dn, mods, serverctrls)) == NULLBER){
1930Sstevel@tonic-gate 		rv = ld->ld_errno;
1940Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS)
1950Sstevel@tonic-gate 			rv = LDAP_OTHER;
1960Sstevel@tonic-gate #ifdef _REENTRANT
1970Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
1980Sstevel@tonic-gate #endif
1990Sstevel@tonic-gate 		return (rv);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* send the message */
2030Sstevel@tonic-gate 	rv =  send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber );
2040Sstevel@tonic-gate 	if (rv == -1){
2050Sstevel@tonic-gate 		rv = ld->ld_errno;
2060Sstevel@tonic-gate 		if (rv == LDAP_SUCCESS){
2070Sstevel@tonic-gate 			rv = LDAP_OTHER;
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate #ifdef _REENTRANT
2100Sstevel@tonic-gate 		UNLOCK_LDAP(ld);
2110Sstevel@tonic-gate #endif
2120Sstevel@tonic-gate 		return (rv);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	*msgidp = rv;
2150Sstevel@tonic-gate #ifdef _REENTRANT
2160Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
2170Sstevel@tonic-gate #endif
2180Sstevel@tonic-gate 	return ( LDAP_SUCCESS );
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 
ldap_modify_ext_s(LDAP * ld,char * dn,LDAPMod ** mods,LDAPControl ** serverctrls,LDAPControl ** clientctrls)2220Sstevel@tonic-gate int ldap_modify_ext_s(LDAP *ld, char *dn, LDAPMod **mods,
2230Sstevel@tonic-gate 					  LDAPControl **serverctrls, LDAPControl **clientctrls)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	int msgid;
2260Sstevel@tonic-gate 	int retcode = LDAP_SUCCESS;
2270Sstevel@tonic-gate 	LDAPMessage *res;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if ((retcode = ldap_modify_ext(ld, dn, mods, serverctrls, clientctrls, &msgid)) != LDAP_SUCCESS)
2300Sstevel@tonic-gate 		return (retcode);
2310Sstevel@tonic-gate 	if (ldap_result(ld, msgid, 1, (struct timeval *)NULL, &res ) == -1)
2320Sstevel@tonic-gate 		return (ld->ld_errno );
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #ifdef  _REENTRANT
2350Sstevel@tonic-gate 	LOCK_LDAP(ld);
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate 	retcode = ldap_parse_result(ld, res, &ld->ld_errno, &ld->ld_matched, &ld->ld_error,
2380Sstevel@tonic-gate 								&ld->ld_referrals, &ld->ld_ret_ctrls, 1);
2390Sstevel@tonic-gate 	if (retcode == LDAP_SUCCESS)
2400Sstevel@tonic-gate 		retcode = ld->ld_errno;
2410Sstevel@tonic-gate #ifdef  _REENTRANT
2420Sstevel@tonic-gate 	UNLOCK_LDAP(ld);
2430Sstevel@tonic-gate #endif
2440Sstevel@tonic-gate 	return (retcode);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
247