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.
33 static char copyright
[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
39 * ldap_modify - initiate an ldap modify operation. Parameters:
42 * dn DN of the object to modify
43 * mods List of modifications to make. This is null-terminated
44 * array of struct ldapmod's, specifying the modifications
49 * { LDAP_MOD_ADD, "cn", { "babs jensen", "babs", 0 } },
50 * { LDAP_MOD_REPLACE, "sn", { "jensen", 0 } },
53 * msgid = ldap_modify( ld, dn, mods );
57 ldap_modify( LDAP
*ld
, const char *dn
, LDAPMod
**mods
)
61 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_modify\n", 0, 0, 0 );
63 if ( ldap_modify_ext( ld
, dn
, mods
, NULL
, NULL
, &msgid
)
67 return( -1 ); /* error is in ld handle */
73 ldap_modify_ext( LDAP
*ld
, const char *dn
, LDAPMod
**mods
,
74 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
, int *msgidp
)
80 * A modify request looks like this:
81 * ModifyRequet ::= SEQUENCE {
82 * object DistinguishedName,
83 * modifications SEQUENCE OF SEQUENCE {
84 * operation ENUMERATED {
89 * modification SEQUENCE {
91 * values SET OF AttributeValue
97 LDAPDebug( LDAP_DEBUG_TRACE
, "ldap_modify_ext\n", 0, 0, 0 );
99 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
100 return( LDAP_PARAM_ERROR
);
102 if ( !NSLDAPI_VALID_LDAPMESSAGE_POINTER( msgidp
))
104 LDAP_SET_LDERRNO( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
105 return( LDAP_PARAM_ERROR
);
108 if ( !NSLDAPI_VALID_NONEMPTY_LDAPMOD_ARRAY( mods
)) {
109 lderr
= LDAP_PARAM_ERROR
;
110 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
117 LDAP_MUTEX_LOCK( ld
, LDAP_MSGID_LOCK
);
118 *msgidp
= ++ld
->ld_msgid
;
119 LDAP_MUTEX_UNLOCK( ld
, LDAP_MSGID_LOCK
);
121 /* see if we should add to the cache */
122 if ( ld
->ld_cache_on
&& ld
->ld_cache_modify
!= NULL
) {
123 LDAP_MUTEX_LOCK( ld
, LDAP_CACHE_LOCK
);
124 if ( (rc
= (ld
->ld_cache_modify
)( ld
, *msgidp
, LDAP_REQ_MODIFY
,
127 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
128 return( LDAP_SUCCESS
);
130 LDAP_MUTEX_UNLOCK( ld
, LDAP_CACHE_LOCK
);
133 /* create a message to send */
134 if (( lderr
= nsldapi_alloc_ber_with_options( ld
, &ber
))
139 if ( ber_printf( ber
, "{it{s{", *msgidp
, LDAP_REQ_MODIFY
, dn
)
141 lderr
= LDAP_ENCODING_ERROR
;
142 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
147 /* for each modification to be performed... */
148 for ( i
= 0; mods
[i
] != NULL
; i
++ ) {
149 if (( mods
[i
]->mod_op
& LDAP_MOD_BVALUES
) != 0 ) {
150 rc
= ber_printf( ber
, "{e{s[V]}}",
151 mods
[i
]->mod_op
& ~LDAP_MOD_BVALUES
,
152 mods
[i
]->mod_type
, mods
[i
]->mod_bvalues
);
154 rc
= ber_printf( ber
, "{e{s[v]}}", mods
[i
]->mod_op
,
155 mods
[i
]->mod_type
, mods
[i
]->mod_values
);
159 lderr
= LDAP_ENCODING_ERROR
;
160 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
166 if ( ber_printf( ber
, "}}" ) == -1 ) {
167 lderr
= LDAP_ENCODING_ERROR
;
168 LDAP_SET_LDERRNO( ld
, lderr
, NULL
, NULL
);
173 if (( lderr
= nsldapi_put_controls( ld
, serverctrls
, 1, ber
))
179 /* send the message */
180 rc
= nsldapi_send_initial_request( ld
, *msgidp
, LDAP_REQ_MODIFY
,
183 return( rc
< 0 ? LDAP_GET_LDERRNO( ld
, NULL
, NULL
) : LDAP_SUCCESS
);
188 ldap_modify_s( LDAP
*ld
, const char *dn
, LDAPMod
**mods
)
190 return( ldap_modify_ext_s( ld
, dn
, mods
, NULL
, NULL
));
195 ldap_modify_ext_s( LDAP
*ld
, const char *dn
, LDAPMod
**mods
,
196 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
)
201 if (( err
= ldap_modify_ext( ld
, dn
, mods
, serverctrls
, clientctrls
,
202 &msgid
)) != LDAP_SUCCESS
) {
206 if ( ldap_result( ld
, msgid
, 1, NULL
, &res
) == -1 ) {
207 return( LDAP_GET_LDERRNO( ld
, NULL
, NULL
) );
210 return( ldap_result2error( ld
, res
, 1 ) );