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>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
18 /* Portions Copyright (C) The Internet Society (1997)
19 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
26 #include <ac/socket.h>
27 #include <ac/string.h>
33 * ldap_modify_ext - initiate an ldap extended modify operation.
38 * dn DN of the object to modify
39 * mods List of modifications to make. This is null-terminated
40 * array of struct ldapmod's, specifying the modifications
42 * sctrls Server Controls
43 * cctrls Client Controls
44 * msgidp Message ID pointer
48 * { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
49 * { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
50 * { LDAP_MOD_DELETE, "ou", 0 },
51 * { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }
54 * rc= ldap_modify_ext( ld, dn, mods, sctrls, cctrls, &msgid );
57 ldap_modify_ext( LDAP *ld,
69 * A modify request looks like this:
70 * ModifyRequet ::= SEQUENCE {
71 * object DistinguishedName,
72 * modifications SEQUENCE OF SEQUENCE {
73 * operation ENUMERATED {
77 * increment (3) -- extension
79 * modification SEQUENCE {
81 * values SET OF AttributeValue
87 Debug( LDAP_DEBUG_TRACE, "ldap_modify_ext\n", 0, 0, 0 );
89 /* check client controls */
90 rc = ldap_int_client_controls( ld, cctrls );
91 if( rc != LDAP_SUCCESS ) return rc;
93 /* create a message to send */
94 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
95 return( LDAP_NO_MEMORY );
98 LDAP_NEXT_MSGID( ld, id );
99 rc = ber_printf( ber, "{it{s{" /*}}}*/, id, LDAP_REQ_MODIFY, dn );
101 ld->ld_errno = LDAP_ENCODING_ERROR;
103 return( ld->ld_errno );
106 /* for each modification to be performed... */
107 for ( i = 0; mods[i] != NULL; i++ ) {
108 if (( mods[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
109 rc = ber_printf( ber, "{e{s[V]N}N}",
110 (ber_int_t) ( mods[i]->mod_op & ~LDAP_MOD_BVALUES ),
111 mods[i]->mod_type, mods[i]->mod_bvalues );
113 rc = ber_printf( ber, "{e{s[v]N}N}",
114 (ber_int_t) mods[i]->mod_op,
115 mods[i]->mod_type, mods[i]->mod_values );
119 ld->ld_errno = LDAP_ENCODING_ERROR;
121 return( ld->ld_errno );
125 if ( ber_printf( ber, /*{{*/ "N}N}" ) == -1 ) {
126 ld->ld_errno = LDAP_ENCODING_ERROR;
128 return( ld->ld_errno );
131 /* Put Server Controls */
132 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
137 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
138 ld->ld_errno = LDAP_ENCODING_ERROR;
140 return( ld->ld_errno );
143 /* send the message */
144 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_MODIFY, dn, ber, id );
145 return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
149 * ldap_modify - initiate an ldap modify operation.
154 * dn DN of the object to modify
155 * mods List of modifications to make. This is null-terminated
156 * array of struct ldapmod's, specifying the modifications
160 * LDAPMod *mods[] = {
161 * { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
162 * { LDAP_MOD_REPLACE, "sn", { "babs jensen", "babs", 0 } },
163 * { LDAP_MOD_DELETE, "ou", 0 },
164 * { LDAP_MOD_INCREMENT, "uidNumber, { "1", 0 } }
167 * msgid = ldap_modify( ld, dn, mods );
170 ldap_modify( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
174 Debug( LDAP_DEBUG_TRACE, "ldap_modify\n", 0, 0, 0 );
176 rc = ldap_modify_ext( ld, dn, mods, NULL, NULL, &msgid );
178 if ( rc != LDAP_SUCCESS )
185 ldap_modify_ext_s( LDAP *ld, LDAP_CONST char *dn,
186 LDAPMod **mods, LDAPControl **sctrl, LDAPControl **cctrl )
192 rc = ldap_modify_ext( ld, dn, mods, sctrl, cctrl, &msgid );
194 if ( rc != LDAP_SUCCESS )
197 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
198 return( ld->ld_errno );
200 return( ldap_result2error( ld, res, 1 ) );
204 ldap_modify_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **mods )
206 return ldap_modify_ext_s( ld, dn, mods, NULL, NULL );