1 /* modify.c - bdb2 backend modify routine */
12 #include "back-bdb2.h"
13 #include "proto-back-bdb2.h"
16 bdb2i_back_modify_internal(
25 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
29 Debug(LDAP_DEBUG_ARGS, "bdb2i_back_modify:\n", 0, 0, 0);
31 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
32 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
33 NULL, NULL, NULL, NULL );
37 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
38 LDAPMod *mod = &ml->ml_mod;
40 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
42 err = bdb2i_add_values( e, mod, op->o_ndn );
46 err = bdb2i_delete_values( e, mod, op->o_ndn );
49 case LDAP_MOD_REPLACE:
50 err = bdb2i_replace_values( e, mod, op->o_ndn );
53 case LDAP_MOD_SOFTADD:
54 /* Avoid problems in index_add_mods()
55 * We need to add index if necessary.
57 mod->mod_op = LDAP_MOD_ADD;
58 if ( (err = bdb2i_add_values( e, mod, op->o_ndn ))
59 == LDAP_TYPE_OR_VALUE_EXISTS ) {
62 mod->mod_op = LDAP_MOD_SOFTADD;
68 if ( err != LDAP_SUCCESS ) {
69 /* unlock entry, delete from cache */
70 send_ldap_result( conn, op, err,
71 NULL, NULL, NULL, NULL );
76 /* check that the entry still obeys the schema */
77 if ( schema_check_entry( e ) != 0 ) {
78 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
79 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
80 NULL, NULL, NULL, NULL );
84 /* check for abandon */
85 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
86 if ( op->o_abandon ) {
87 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
90 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
93 if ( bdb2i_index_add_mods( be, modlist, e->e_id ) != 0 ) {
94 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
95 NULL, NULL, NULL, NULL );
99 /* check for abandon */
100 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
101 if ( op->o_abandon ) {
102 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
105 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
107 /* change the entry itself */
108 if ( bdb2i_id2entry_add( be, e ) != 0 ) {
109 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
110 NULL, NULL, NULL, NULL );
114 send_ldap_result( conn, op, LDAP_SUCCESS,
115 NULL, NULL, NULL, NULL );
134 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
135 struct timeval time1;
136 int ret, manageDSAit;
140 bdb2i_start_timing( be->bd_info, &time1 );
142 if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
143 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
144 NULL, NULL, NULL, NULL );
148 /* check, if a new default attribute index will be created,
149 in which case we have to open the index file BEFORE TP */
150 switch ( slapMode & SLAP_MODE ) {
151 case SLAP_SERVER_MODE:
153 bdb2i_check_default_attr_index_mod( li, modlist );
157 if ( (e = bdb2i_dn2entry_w( be, ndn, &matched )) == NULL ) {
158 char *matched_dn = NULL;
159 struct berval **refs = NULL;
161 if ( matched != NULL ) {
162 matched_dn = ch_strdup( matched->e_dn );
163 refs = is_entry_referral( matched )
164 ? get_entry_referrals( be, conn, op, matched )
166 bdb2i_cache_return_entry_r( &li->li_cache, matched );
168 refs = default_referral;
171 send_ldap_result( conn, op, LDAP_REFERRAL,
172 matched_dn, NULL, refs, NULL );
174 if( matched != NULL ) {
175 ber_bvecfree( refs );
183 if (!manageDSAit && is_entry_referral( e ) ) {
184 /* entry is a referral, don't allow add */
185 struct berval **refs = get_entry_referrals( be,
188 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
191 send_ldap_result( conn, op, LDAP_REFERRAL,
192 e->e_dn, NULL, refs, NULL );
194 bdb2i_cache_return_entry_w( &li->li_cache, e );
196 ber_bvecfree( refs );
202 ret = bdb2i_back_modify_internal( be, conn, op, ndn, modlist, e );
203 bdb2i_cache_return_entry_w( &li->li_cache, e );
206 (void) bdb2i_leave_backend_w( lock );
207 bdb2i_stop_timing( be->bd_info, time1, "MOD", conn, op );
223 /* check if the values we're adding already exist */
224 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
225 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
226 if ( value_find( a->a_vals, mod->mod_bvalues[i],
227 a->a_syntax, 3 ) == 0 ) {
228 return( LDAP_TYPE_OR_VALUE_EXISTS );
234 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
235 return( LDAP_CONSTRAINT_VIOLATION );
238 return( LDAP_SUCCESS );
251 /* delete the entire attribute */
252 if ( mod->mod_bvalues == NULL ) {
253 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
254 mod->mod_type, 0, 0 );
255 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
256 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
259 /* delete specific values - find the attribute first */
260 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
261 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
262 mod->mod_type, 0, 0 );
263 return( LDAP_NO_SUCH_ATTRIBUTE );
266 /* find each value to delete */
267 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
269 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
270 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
271 a->a_syntax, 3 ) != 0 ) {
276 /* found a matching value - delete it */
277 ber_bvfree( a->a_vals[j] );
278 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
279 a->a_vals[k - 1] = a->a_vals[k];
281 a->a_vals[k - 1] = NULL;
283 /* delete the entire attribute, if no values remain */
284 if ( a->a_vals[0] == NULL) {
285 Debug( LDAP_DEBUG_ARGS,
286 "removing entire attribute %s\n",
287 mod->mod_type, 0, 0 );
288 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
289 return LDAP_NO_SUCH_ATTRIBUTE;
296 /* looked through them all w/o finding it */
298 Debug( LDAP_DEBUG_ARGS,
299 "could not find value for attr %s\n",
300 mod->mod_type, 0, 0 );
301 return( LDAP_NO_SUCH_ATTRIBUTE );
305 return( LDAP_SUCCESS );
309 bdb2i_replace_values(
315 (void) attr_delete( &e->e_attrs, mod->mod_type );
317 if ( mod->mod_bvalues != NULL &&
318 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
320 return( LDAP_CONSTRAINT_VIOLATION );
323 return( LDAP_SUCCESS );