/* $OpenLDAP$ */1/* This work is part of OpenLDAP Software <http://www.openldap.org/>.2*3* Copyright 1998-2024 The OpenLDAP Foundation.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted only as authorized by the OpenLDAP8* Public License.9*10* A copy of this license is available in the file LICENSE in the11* top-level directory of the distribution or, alternatively, at12* <http://www.OpenLDAP.org/license.html>.13*/14/* Portions Copyright (c) 1990 Regents of the University of Michigan.15* All rights reserved.16*/1718#include "portable.h"1920#include <stdio.h>2122#include <ac/socket.h>23#include <ac/string.h>24#include <ac/time.h>2526#include "ldap-int.h"2728/* A modify request/response looks like this:29* ModifyRequest ::= [APPLICATION 6] SEQUENCE {30* object LDAPDN,31* changes SEQUENCE OF change SEQUENCE {32* operation ENUMERATED {33* add (0),34* delete (1),35* replace (2),36* ... },37* modification PartialAttribute } }38*39* PartialAttribute ::= SEQUENCE {40* type AttributeDescription,41* vals SET OF value AttributeValue }42*43* AttributeDescription ::= LDAPString44* -- Constrained to <attributedescription> [RFC4512]45*46* AttributeValue ::= OCTET STRING47*48* ModifyResponse ::= [APPLICATION 7] LDAPResult49*50* (Source: RFC 4511)51*/5253BerElement *54ldap_build_modify_req(55LDAP *ld,56LDAP_CONST char *dn,57LDAPMod **mods,58LDAPControl **sctrls,59LDAPControl **cctrls,60ber_int_t *msgidp )61{62BerElement *ber;63int i, rc;6465/* create a message to send */66if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {67return( NULL );68}6970LDAP_NEXT_MSGID( ld, *msgidp );71rc = ber_printf( ber, "{it{s{" /*}}}*/, *msgidp, LDAP_REQ_MODIFY, dn );72if ( rc == -1 ) {73ld->ld_errno = LDAP_ENCODING_ERROR;74ber_free( ber, 1 );75return( NULL );76}7778/* allow mods to be NULL ("touch") */79if ( mods ) {80/* for each modification to be performed... */81for ( i = 0; mods[i] != NULL; i++ ) {82if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {83rc = ber_printf( ber, "{e{s[V]N}N}",84(ber_int_t) ( mods[i]->mod_op & ~LDAP_MOD_BVALUES ),85mods[i]->mod_type, mods[i]->mod_bvalues );86} else {87rc = ber_printf( ber, "{e{s[v]N}N}",88(ber_int_t) mods[i]->mod_op,89mods[i]->mod_type, mods[i]->mod_values );90}9192if ( rc == -1 ) {93ld->ld_errno = LDAP_ENCODING_ERROR;94ber_free( ber, 1 );95return( NULL );96}97}98}99100if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {101ld->ld_errno = LDAP_ENCODING_ERROR;102ber_free( ber, 1 );103return( NULL );104}105106/* Put Server Controls */107if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {108ber_free( ber, 1 );109return( NULL );110}111112if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {113ld->ld_errno = LDAP_ENCODING_ERROR;114ber_free( ber, 1 );115return( NULL );116}117118return( ber );119}120121/*122* ldap_modify_ext - initiate an ldap extended modify operation.123*124* Parameters:125*126* ld LDAP descriptor127* dn DN of the object to modify128* mods List of modifications to make. This is null-terminated129* array of struct ldapmod's, specifying the modifications130* to perform.131* sctrls Server Controls132* cctrls Client Controls133* msgidp Message ID pointer134*135* Example:136* LDAPMod *mods[] = {137* { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },138* { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },139* { LDAP_MOD_DELETE, "ou", 0 },140* { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }141* 0142* }143* rc= ldap_modify_ext( ld, dn, mods, sctrls, cctrls, &msgid );144*/145int146ldap_modify_ext( LDAP *ld,147LDAP_CONST char *dn,148LDAPMod **mods,149LDAPControl **sctrls,150LDAPControl **cctrls,151int *msgidp )152{153BerElement *ber;154int rc;155ber_int_t id;156157Debug0( LDAP_DEBUG_TRACE, "ldap_modify_ext\n" );158159/* check client controls */160rc = ldap_int_client_controls( ld, cctrls );161if( rc != LDAP_SUCCESS ) return rc;162163ber = ldap_build_modify_req( ld, dn, mods, sctrls, cctrls, &id );164if( !ber )165return ld->ld_errno;166167/* send the message */168*msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber, id );169return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );170}171172/*173* ldap_modify - initiate an ldap modify operation.174*175* Parameters:176*177* ld LDAP descriptor178* dn DN of the object to modify179* mods List of modifications to make. This is null-terminated180* array of struct ldapmod's, specifying the modifications181* to perform.182*183* Example:184* LDAPMod *mods[] = {185* { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },186* { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },187* { LDAP_MOD_DELETE, "ou", 0 },188* { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }189* 0190* }191* msgid = ldap_modify( ld, dn, mods );192*/193int194ldap_modify( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )195{196int rc, msgid;197198Debug0( LDAP_DEBUG_TRACE, "ldap_modify\n" );199200rc = ldap_modify_ext( ld, dn, mods, NULL, NULL, &msgid );201202if ( rc != LDAP_SUCCESS )203return -1;204205return msgid;206}207208int209ldap_modify_ext_s( LDAP *ld, LDAP_CONST char *dn,210LDAPMod **mods, LDAPControl **sctrl, LDAPControl **cctrl )211{212int rc;213int msgid;214LDAPMessage *res;215216rc = ldap_modify_ext( ld, dn, mods, sctrl, cctrl, &msgid );217218if ( rc != LDAP_SUCCESS )219return( rc );220221if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )222return( ld->ld_errno );223224return( ldap_result2error( ld, res, 1 ) );225}226227int228ldap_modify_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )229{230return ldap_modify_ext_s( ld, dn, mods, NULL, NULL );231}232233234