1 #pragma ident "%Z%%M% %I% %E% SMI"
4 * The contents of this file are subject to the Netscape Public
5 * License Version 1.1 (the "License"); you may not use this file
6 * except in compliance with the License. You may obtain a copy of
7 * the License at http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS
10 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * rights and limitations under the License.
14 * The Original Code is Mozilla Communicator client code, released
17 * The Initial Developer of the Original Code is Netscape
18 * Communications Corporation. Portions created by Netscape are
19 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25 * Copyright (c) 1990 Regents of the University of Michigan.
26 * All rights reserved.
34 static char copyright
[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
41 * ldap_modify - initiate an ldap modify operation. Parameters:
44 * dn DN of the object to modify
45 * mods List of modifications to make. This is null-terminated
46 * array of struct ldapmod's, specifying the modifications
51 * { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
52 * { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
55 * msgid = ldap_modify( ld, dn, mods );
59 ldap_modify( LDAP
*ld
, const char *dn
, LDAPMod
**mods
)
63 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_modify\n", 0, 0, 0 );
65 if ( ldap_modify_ext( ld
, dn
, mods
, NULL
, NULL
, &msgid
)
69 return( -1 ); /* error is in ld handle */
75 ldap_modify_ext( LDAP
*ld
, const char *dn
, LDAPMod
**mods
,
76 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
, int *msgidp
)
82 * A modify request looks like this:
83 * ModifyRequet ::= SEQUENCE {
84 * object DistinguishedName,
85 * modifications SEQUENCE OF SEQUENCE {
86 * operation ENUMERATED {
91 * modification SEQUENCE {
93 * values SET OF AttributeValue
99 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_modify_ext\n", 0, 0, 0 );
101 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
102 return( LDAP_PARAM_ERROR
);
104 if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp
))
106 LDAP_SET_LDERRNO( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
107 return( LDAP_PARAM_ERROR
);
110 if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( mods
)) {
111 lderr
= LDAP_PARAM_ERROR
;
112 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
119 LDAP_MUTEX_LOCK( ld
, LDAP_MSGID_LOCK
);
120 *msgidp
= ++ld
->ld_msgid
;
121 LDAP_MUTEX_UNLOCK( ld
, LDAP_MSGID_LOCK
);
123 /* see if we should add to the cache */
124 if ( ld
->ld_cache_on
&& ld
->ld_cache_modify
!= NULL
) {
125 LDAP_MUTEX_LOCK( ld
, LDAP_CACHE_LOCK
);
126 if ( (rc
= (ld
->ld_cache_modify
)( ld
, *msgidp
, LDAP_REQ_MODIFY
,
129 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
130 return( LDAP_SUCCESS
);
132 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
135 /* create a message to send */
136 if (( lderr
= nsldapi_alloc_ber_with_options( ld
, &ber
))
141 if ( ber_printf( ber
, "{it{s{", *msgidp
, LDAP_REQ_MODIFY
, dn
)
143 lderr
= LDAP_ENCODING_ERROR
;
144 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
149 /* for each modification to be performed... */
150 for ( i
= 0; mods
[i
] != NULL
; i
++ ) {
151 if (( mods
[i
]->mod_op
& LDAP_MOD_BVALUES
) != 0 ) {
152 rc
= ber_printf( ber
, "{e{s[V]}}",
153 mods
[i
]->mod_op
& ~LDAP_MOD_BVALUES
,
154 mods
[i
]->mod_type
, mods
[i
]->mod_bvalues
);
156 rc
= ber_printf( ber
, "{e{s[v]}}", mods
[i
]->mod_op
,
157 mods
[i
]->mod_type
, mods
[i
]->mod_values
);
161 lderr
= LDAP_ENCODING_ERROR
;
162 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
168 if ( ber_printf( ber
, "}}" ) == -1 ) {
169 lderr
= LDAP_ENCODING_ERROR
;
170 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
175 if (( lderr
= nsldapi_put_controls( ld
, serverctrls
, 1, ber
))
181 /* send the message */
182 rc
= nsldapi_send_initial_request( ld
, *msgidp
, LDAP_REQ_MODIFY
,
185 return( rc
< 0 ? LDAP_GET_LDERRNO( ld
, NULL
, NULL
) : LDAP_SUCCESS
);
190 ldap_modify_s( LDAP
*ld
, const char *dn
, LDAPMod
**mods
)
192 return( ldap_modify_ext_s( ld
, dn
, mods
, NULL
, NULL
));
197 ldap_modify_ext_s( LDAP
*ld
, const char *dn
, LDAPMod
**mods
,
198 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
)
203 if (( err
= ldap_modify_ext( ld
, dn
, mods
, serverctrls
, clientctrls
,
204 &msgid
)) != LDAP_SUCCESS
) {
208 if ( ldap_result( ld
, msgid
, 1, (struct timeval
*)NULL
, &res
) == -1 ) {
209 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
212 return( ldap_result2error( ld
, res
, 1 ) );