1 /* modify.c - ldbm backend modify routine */
12 #include "back-ldbm.h"
13 #include "proto-back-ldbm.h"
15 static void add_lastmods(Operation *op, LDAPModList **ml);
19 add_lastmods( Operation *op, LDAPModList **modlist )
23 struct berval *bvals[2];
29 Debug( LDAP_DEBUG_TRACE, "add_lastmods\n", 0, 0, 0 );
34 /* remove any attempts by the user to modify these attrs */
35 for ( m = modlist; *m != NULL; m = &(*m)->ml_next ) {
36 if ( oc_check_no_usermod_attr( (*m)->ml_type ) ) {
37 Debug( LDAP_DEBUG_TRACE,
38 "add_lastmods: found no user mod attr: %s\n",
39 (*m)->ml_type, 0, 0 );
43 if ( tmp->ml_bvalues != NULL ) {
44 ber_bvecfree( tmp->ml_bvalues );
52 if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
54 bv.bv_len = strlen( bv.bv_val );
57 bv.bv_len = strlen( bv.bv_val );
59 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
60 tmp->ml_type = ch_strdup( "modifiersname" );
61 tmp->ml_op = LDAP_MOD_REPLACE;
62 tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
63 tmp->ml_bvalues[0] = ber_bvdup( &bv );
64 tmp->ml_next = *modlist;
67 currenttime = slap_get_time();
68 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
69 #ifndef LDAP_LOCALTIME
70 ltm = gmtime( ¤ttime );
71 strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
73 ltm = localtime( ¤ttime );
74 strftime( buf, sizeof(buf), "%y%m%d%H%M%SZ", ltm );
76 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
79 bv.bv_len = strlen( bv.bv_val );
80 tmp = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
81 tmp->ml_type = ch_strdup( "modifytimestamp" );
82 tmp->ml_op = LDAP_MOD_REPLACE;
83 tmp->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
84 tmp->ml_bvalues[0] = ber_bvdup( &bv );
85 tmp->ml_next = *modlist;
90 /* We need this function because of LDAP modrdn. If we do not
91 * add this there would be a bunch of code replication here
92 * and there and of course the likelihood of bugs increases.
93 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
96 int ldbm_modify_internal(
101 LDAPModList *modlist,
110 if ( ((be->be_lastmod == ON)
111 || ((be->be_lastmod == UNDEFINED)&&(global_lastmod == ON)))
112 && (be->be_update_ndn == NULL)) {
114 /* XXX: It may be wrong, it changes mod time even if
117 add_lastmods( op, &modlist );
122 if ( (err = acl_check_modlist( be, conn, op, e, modlist ))
125 send_ldap_result( conn, op, err,
126 NULL, NULL, NULL, NULL );
130 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
134 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
136 err = add_values( e, mod, op->o_ndn );
139 case LDAP_MOD_DELETE:
140 err = delete_values( e, mod, op->o_ndn );
143 case LDAP_MOD_REPLACE:
144 /* Need to remove all values from indexes before they
148 && ((a = attr_find( e->e_attrs, mod->mod_type ))
151 (void) index_change_values( be,
158 err = replace_values( e, mod, op->o_ndn );
161 case LDAP_MOD_SOFTADD:
162 /* Avoid problems in index_add_mods()
163 * We need to add index if necessary.
165 mod->mod_op = LDAP_MOD_ADD;
166 if ( (err = add_values( e, mod, op->o_ndn ))
167 == LDAP_TYPE_OR_VALUE_EXISTS ) {
170 mod->mod_op = LDAP_MOD_SOFTADD;
176 if ( err != LDAP_SUCCESS ) {
177 /* unlock entry, delete from cache */
178 send_ldap_result( conn, op, err,
179 NULL, NULL, NULL, NULL );
184 /* check that the entry still obeys the schema */
185 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
186 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
187 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION,
188 NULL, NULL, NULL, NULL );
192 /* check for abandon */
193 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
194 if ( op->o_abandon ) {
195 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
198 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
201 if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
202 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
203 NULL, NULL, NULL, NULL );
207 /* check for abandon */
208 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
209 if ( op->o_abandon ) {
210 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
213 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
217 }/* int ldbm_modify_internal() */
229 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
232 int manageDSAit = get_manageDSAit( op );
234 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
236 /* acquire and lock entry */
237 if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
238 char* matched_dn = NULL;
239 struct berval **refs = NULL;
241 if ( matched != NULL ) {
242 matched_dn = ch_strdup( matched->e_dn );
243 refs = is_entry_referral( matched )
244 ? get_entry_referrals( be, conn, op, matched )
246 cache_return_entry_r( &li->li_cache, matched );
248 refs = default_referral;
251 send_ldap_result( conn, op, LDAP_REFERRAL,
252 matched_dn, NULL, refs, NULL );
254 if ( matched != NULL ) {
255 ber_bvecfree( refs );
262 if ( !manageDSAit && is_entry_referral( e ) ) {
263 /* parent is a referral, don't allow add */
264 /* parent is an alias, don't allow add */
265 struct berval **refs = get_entry_referrals( be,
268 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
271 send_ldap_result( conn, op, LDAP_REFERRAL,
272 e->e_dn, NULL, refs, NULL );
274 ber_bvecfree( refs );
279 /* Modify the entry */
280 if ( ldbm_modify_internal( be, conn, op, dn, modlist, e ) != 0 ) {
284 /* change the entry itself */
285 if ( id2entry_add( be, e ) != 0 ) {
286 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
287 NULL, NULL, NULL, NULL );
291 send_ldap_result( conn, op, LDAP_SUCCESS,
292 NULL, NULL, NULL, NULL );
293 cache_return_entry_w( &li->li_cache, e );
297 cache_return_entry_w( &li->li_cache, e );
311 /* check if the values we're adding already exist */
312 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
313 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
314 if ( value_find( a->a_vals, mod->mod_bvalues[i],
315 a->a_syntax, 3 ) == 0 ) {
316 return( LDAP_TYPE_OR_VALUE_EXISTS );
322 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
323 return( LDAP_CONSTRAINT_VIOLATION );
326 return( LDAP_SUCCESS );
339 /* delete the entire attribute */
340 if ( mod->mod_bvalues == NULL ) {
341 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
342 mod->mod_type, 0, 0 );
343 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
344 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
347 /* delete specific values - find the attribute first */
348 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
349 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
350 mod->mod_type, 0, 0 );
351 return( LDAP_NO_SUCH_ATTRIBUTE );
354 /* find each value to delete */
355 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
357 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
358 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
359 a->a_syntax, 3 ) != 0 ) {
364 /* found a matching value - delete it */
365 ber_bvfree( a->a_vals[j] );
366 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
367 a->a_vals[k - 1] = a->a_vals[k];
369 a->a_vals[k - 1] = NULL;
371 /* delete the entire attribute, if no values remain */
372 if ( a->a_vals[0] == NULL) {
373 Debug( LDAP_DEBUG_ARGS,
374 "removing entire attribute %s\n",
375 mod->mod_type, 0, 0 );
376 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
377 return LDAP_NO_SUCH_ATTRIBUTE;
384 /* looked through them all w/o finding it */
386 Debug( LDAP_DEBUG_ARGS,
387 "could not find value for attr %s\n",
388 mod->mod_type, 0, 0 );
389 return( LDAP_NO_SUCH_ATTRIBUTE );
393 return( LDAP_SUCCESS );
404 /* XXX: BEFORE YOU GET RID OF PREVIOUS VALUES REMOVE FROM INDEX
408 (void) attr_delete( &e->e_attrs, mod->mod_type );
410 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
411 return( LDAP_CONSTRAINT_VIOLATION );
414 return( LDAP_SUCCESS );