1 /* modify.c - ldbm backend modify routine */
12 #include "back-ldbm.h"
13 #include "proto-back-ldbm.h"
15 /* We need this function because of LDAP modrdn. If we do not
16 * add this there would be a bunch of code replication here
17 * and there and of course the likelihood of bugs increases.
18 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
21 int ldbm_modify_internal(
35 if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
38 send_ldap_result( conn, op, err,
39 NULL, NULL, NULL, NULL );
43 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
47 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
49 err = add_values( e, mod, op->o_ndn );
53 err = delete_values( e, mod, op->o_ndn );
56 case LDAP_MOD_REPLACE:
57 /* Need to remove all values from indexes before they
61 && ((a = attr_find( e->e_attrs, mod->mod_type ))
64 (void) index_change_values( be,
71 err = replace_values( e, mod, op->o_ndn );
74 case LDAP_MOD_SOFTADD:
75 /* Avoid problems in index_add_mods()
76 * We need to add index if necessary.
78 mod->mod_op = LDAP_MOD_ADD;
79 if ( (err = add_values( e, mod, op->o_ndn ))
80 == LDAP_TYPE_OR_VALUE_EXISTS ) {
83 mod->mod_op = LDAP_MOD_SOFTADD;
89 if ( err != LDAP_SUCCESS ) {
90 /* unlock entry, delete from cache */
91 send_ldap_result( conn, op, err,
92 NULL, NULL, NULL, NULL );
97 /* check that the entry still obeys the schema */
98 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
99 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
100 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
101 NULL, NULL, NULL, NULL );
105 /* check for abandon */
106 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
107 if ( op->o_abandon ) {
108 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
111 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
114 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
115 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
116 NULL, NULL, NULL, NULL );
120 /* check for abandon */
121 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
122 if ( op->o_abandon ) {
123 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
126 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
130 }/* int ldbm_modify_internal() */
142 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
145 int manageDSAit = get_manageDSAit( op );
147 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
149 /* acquire and lock entry */
150 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
151 char* matched_dn = NULL;
152 struct berval **refs = NULL;
154 if ( matched != NULL ) {
155 matched_dn = ch_strdup( matched->e_dn );
156 refs = is_entry_referral( matched )
157 ? get_entry_referrals( be, conn, op, matched )
159 cache_return_entry_r( &li->li_cache, matched );
161 refs = default_referral;
164 send_ldap_result( conn, op, LDAP_REFERRAL,
165 matched_dn, NULL, refs, NULL );
167 if ( matched != NULL ) {
168 ber_bvecfree( refs );
175 if ( !manageDSAit && is_entry_referral( e ) ) {
176 /* parent is a referral, don't allow add */
177 /* parent is an alias, don't allow add */
178 struct berval **refs = get_entry_referrals( be,
181 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
184 send_ldap_result( conn, op, LDAP_REFERRAL,
185 e->e_dn, NULL, refs, NULL );
187 ber_bvecfree( refs );
192 /* Modify the entry */
193 if ( ldbm_modify_internal( be, conn, op, dn, modlist, e ) != 0 ) {
197 /* change the entry itself */
198 if ( id2entry_add( be, e ) != 0 ) {
199 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
200 NULL, NULL, NULL, NULL );
204 send_ldap_result( conn, op, LDAP_SUCCESS,
205 NULL, NULL, NULL, NULL );
206 cache_return_entry_w( &li->li_cache, e );
210 cache_return_entry_w( &li->li_cache, e );
224 /* check if the values we're adding already exist */
225 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
226 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
227 if ( value_find( a->a_vals, mod->mod_bvalues[i],
228 a->a_syntax, 3 ) == 0 ) {
229 return( LDAP_TYPE_OR_VALUE_EXISTS );
235 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
236 return( LDAP_CONSTRAINT_VIOLATION );
239 return( LDAP_SUCCESS );
252 /* delete the entire attribute */
253 if ( mod->mod_bvalues == NULL ) {
254 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
255 mod->mod_type, 0, 0 );
256 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
257 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
260 /* delete specific values - find the attribute first */
261 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
262 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
263 mod->mod_type, 0, 0 );
264 return( LDAP_NO_SUCH_ATTRIBUTE );
267 /* find each value to delete */
268 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
270 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
271 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
272 a->a_syntax, 3 ) != 0 ) {
277 /* found a matching value - delete it */
278 ber_bvfree( a->a_vals[j] );
279 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
280 a->a_vals[k - 1] = a->a_vals[k];
282 a->a_vals[k - 1] = NULL;
284 /* delete the entire attribute, if no values remain */
285 if ( a->a_vals[0] == NULL) {
286 Debug( LDAP_DEBUG_ARGS,
287 "removing entire attribute %s\n",
288 mod->mod_type, 0, 0 );
289 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
290 return LDAP_NO_SUCH_ATTRIBUTE;
297 /* looked through them all w/o finding it */
299 Debug( LDAP_DEBUG_ARGS,
300 "could not find value for attr %s\n",
301 mod->mod_type, 0, 0 );
302 return( LDAP_NO_SUCH_ATTRIBUTE );
306 return( LDAP_SUCCESS );
317 /* XXX: BEFORE YOU GET RID OF PREVIOUS VALUES REMOVE FROM INDEX
321 (void) attr_delete( &e->e_attrs, mod->mod_type );
323 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
324 return( LDAP_CONSTRAINT_VIOLATION );
327 return( LDAP_SUCCESS );