]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
4da7301032ce91cdbfe6aa23b1708903b8582a1c
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14 #include <ac/time.h>
15
16 #include "slap.h"
17 #include "back-ldbm.h"
18 #include "proto-back-ldbm.h"
19
20 static int add_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
21 static int delete_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
22 static int replace_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
23
24 /* We need this function because of LDAP modrdn. If we do not 
25  * add this there would be a bunch of code replication here 
26  * and there and of course the likelihood of bugs increases.
27  * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
28  */ 
29
30 int ldbm_modify_internal(
31     Backend     *be,
32     Connection  *conn,
33     Operation   *op,
34     char        *dn,
35     Modifications       *modlist,
36     Entry       *e 
37 )
38 {
39         int err;
40         Modification    *mod;
41         Modifications   *ml;
42         Attribute       *save_attrs;
43
44         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
45                 return LDAP_INSUFFICIENT_ACCESS;
46         }
47
48         save_attrs = e->e_attrs;
49         e->e_attrs = attrs_dup( e->e_attrs );
50
51         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
52                 mod = &ml->sml_mod;
53
54 #ifdef SLAPD_SCHEMA_NOT_COMPAT
55                 switch ( mod->sm_op )
56 #else
57                 switch ( mod->mod_op )
58 #endif
59                 {
60                 case LDAP_MOD_ADD:
61                         err = add_values( e, mod, op->o_ndn );
62                         break;
63
64                 case LDAP_MOD_DELETE:
65                         err = delete_values( e, mod, op->o_ndn );
66                         break;
67
68                 case LDAP_MOD_REPLACE:
69                         err = replace_values( e, mod, op->o_ndn );
70                         break;
71
72                 case SLAP_MOD_SOFTADD:
73                         /* Avoid problems in index_add_mods()
74                          * We need to add index if necessary.
75                          */
76 #ifdef SLAPD_SCHEMA_NOT_COMPAT
77                         mod->sm_op = LDAP_MOD_ADD;
78 #else
79                         mod->mod_op = LDAP_MOD_ADD;
80 #endif
81                         err = add_values( e, mod, op->o_ndn );
82
83                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
84                                 err = LDAP_SUCCESS;
85                         }
86                         break;
87                 }
88
89                 if ( err != LDAP_SUCCESS ) {
90                         attrs_free( e->e_attrs );
91                         e->e_attrs = save_attrs;
92                         /* unlock entry, delete from cache */
93                         return err; 
94                 }
95         }
96
97         /* check for abandon */
98         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
99         if ( op->o_abandon ) {
100                 attrs_free( e->e_attrs );
101                 e->e_attrs = save_attrs;
102                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
103                 return SLAPD_ABANDON;
104         }
105         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
106
107         /* check that the entry still obeys the schema */
108         if ( schema_check_entry( e ) != 0 ) {
109                 attrs_free( e->e_attrs );
110                 e->e_attrs = save_attrs;
111                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
112                 return LDAP_OBJECT_CLASS_VIOLATION;
113         }
114
115         /* check for abandon */
116         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
117         if ( op->o_abandon ) {
118                 attrs_free( e->e_attrs );
119                 e->e_attrs = save_attrs;
120                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
121                 return SLAPD_ABANDON;
122         }
123         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
124
125         /* run through the attributes removing old indices */
126         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
127                 mod = &ml->sml_mod;
128
129 #ifdef SLAPD_SCHEMA_NOT_COMPAT
130                 switch ( mod->sm_op )
131 #else
132                 switch ( mod->mod_op )
133 #endif
134                 {
135                 case LDAP_MOD_REPLACE: {
136 #ifdef SLAPD_SCHEMA_NOT_COMPAT
137                         /* not yet implemented */
138 #else
139                         /* Need to remove all values from indexes */
140                         Attribute *a = save_attrs
141                                 ? attr_find( save_attrs, mod->mod_type )
142                                 : NULL;
143
144                         if( a != NULL ) {
145                                 (void) index_change_values( be,
146                                         mod->mod_type,
147                                         a->a_vals,
148                                         e->e_id,
149                                         SLAP_INDEX_DELETE_OP );
150                         }
151 #endif
152                         } break;
153
154                 case LDAP_MOD_DELETE:
155 #ifdef SLAPD_SCHEMA_NOT_COMPAT
156                         /* not yet implemented */
157 #else
158                         /* remove deleted values */
159                         (void) index_change_values( be,
160                                 mod->mod_type,
161                                 mod->mod_bvalues,
162                                 e->e_id,
163                                 SLAP_INDEX_DELETE_OP );
164 #endif
165                         break;
166                 }
167         }
168
169         attrs_free( save_attrs );
170
171         /* run through the attributes adding new indices */
172         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
173                 mod = &ml->sml_mod;
174
175                 switch ( mod->mod_op ) {
176                 case LDAP_MOD_REPLACE:
177                 case LDAP_MOD_ADD:
178 #ifdef SLAPD_SCHEMA_NOT_COMPAT
179                 /* not yet implemented */
180 #else
181                         (void) index_change_values( be,
182                                 mod->mod_type,
183                                 mod->mod_bvalues,
184                                 e->e_id,
185                                 SLAP_INDEX_ADD_OP );
186 #endif
187                         break;
188
189                 case LDAP_MOD_DELETE: {
190 #ifdef SLAPD_SCHEMA_NOT_COMPAT
191                 /* not yet implemented */
192 #else
193                         /* Need to add all remaining values */
194                         Attribute *a = e->e_attrs
195                                 ? attr_find( e->e_attrs, mod->mod_type )
196                                 : NULL;
197 #endif
198
199                         if( a != NULL ) {
200                                 (void) index_change_values( be,
201                                         mod->mod_type,
202                                         a->a_vals,
203                                         e->e_id,
204                                         SLAP_INDEX_ADD_OP );
205                         }
206                         } break;
207                 }
208         }
209
210         return LDAP_SUCCESS;
211 }
212
213
214 int
215 ldbm_back_modify(
216     Backend     *be,
217     Connection  *conn,
218     Operation   *op,
219     char        *dn,
220     char        *ndn,
221     Modifications       *modlist
222 )
223 {
224         int rc;
225         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
226         Entry           *matched;
227         Entry           *e;
228         int             manageDSAit = get_manageDSAit( op );
229
230         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
231
232         /* acquire and lock entry */
233         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
234                 char* matched_dn = NULL;
235                 struct berval **refs = NULL;
236
237                 if ( matched != NULL ) {
238                         matched_dn = ch_strdup( matched->e_dn );
239                         refs = is_entry_referral( matched )
240                                 ? get_entry_referrals( be, conn, op, matched )
241                                 : NULL;
242                         cache_return_entry_r( &li->li_cache, matched );
243                 } else {
244                         refs = default_referral;
245                 }
246
247                 send_ldap_result( conn, op, LDAP_REFERRAL,
248                         matched_dn, NULL, refs, NULL );
249
250                 if ( matched != NULL ) {
251                         ber_bvecfree( refs );
252                         free( matched_dn );
253                 }
254
255                 return( -1 );
256         }
257
258     if ( !manageDSAit && is_entry_referral( e ) ) {
259                 /* parent is a referral, don't allow add */
260                 /* parent is an alias, don't allow add */
261                 struct berval **refs = get_entry_referrals( be,
262                         conn, op, e );
263
264                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
265                     0, 0 );
266
267                 send_ldap_result( conn, op, LDAP_REFERRAL,
268                     e->e_dn, NULL, refs, NULL );
269
270                 ber_bvecfree( refs );
271
272                 goto error_return;
273         }
274         
275         /* Modify the entry */
276         rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e );
277
278         if( rc != LDAP_SUCCESS ) {
279                 if( rc != SLAPD_ABANDON ) {
280                         send_ldap_result( conn, op, rc,
281                                 NULL, NULL, NULL, NULL );
282                 }
283
284                 goto error_return;
285         }
286
287         /* change the entry itself */
288         if ( id2entry_add( be, e ) != 0 ) {
289                 send_ldap_result( conn, op, LDAP_OTHER,
290                         NULL, NULL, NULL, NULL );
291                 goto error_return;
292         }
293
294         send_ldap_result( conn, op, LDAP_SUCCESS,
295                 NULL, NULL, NULL, NULL );
296
297         cache_return_entry_w( &li->li_cache, e );
298         return( 0 );
299
300 error_return:;
301         cache_return_entry_w( &li->li_cache, e );
302         return( -1 );
303 }
304
305 static int
306 add_values(
307     Entry       *e,
308     Modification        *mod,
309     char        *dn
310 )
311 {
312 #ifdef SLAPD_SCHEMA_NOT_COMPAT
313         /* not yet implemented */
314 #else
315         int             i;
316         Attribute       *a;
317
318         /* check if the values we're adding already exist */
319         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
320                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
321                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
322                             a->a_syntax, 3 ) == 0 ) {
323                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
324                         }
325                 }
326         }
327
328         /* no - add them */
329         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
330                 return( LDAP_CONSTRAINT_VIOLATION );
331         }
332 #endif
333
334         return( LDAP_SUCCESS );
335 }
336
337 static int
338 delete_values(
339     Entry       *e,
340     Modification        *mod,
341     char        *dn
342 )
343 {
344 #ifdef SLAPD_SCHEMA_NOT_COMPAT
345         /* not yet implemented */
346 #else
347         int             i, j, k, found;
348         Attribute       *a;
349
350         /* delete the entire attribute */
351         if ( mod->mod_bvalues == NULL ) {
352                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
353                     mod->mod_type, 0, 0 );
354                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
355                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
356         }
357
358         /* delete specific values - find the attribute first */
359         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
360                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
361                     mod->mod_type, 0, 0 );
362                 return( LDAP_NO_SUCH_ATTRIBUTE );
363         }
364
365         /* find each value to delete */
366         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
367                 found = 0;
368                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
369                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
370                             a->a_syntax, 3 ) != 0 ) {
371                                 continue;
372                         }
373                         found = 1;
374
375                         /* found a matching value - delete it */
376                         ber_bvfree( a->a_vals[j] );
377                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
378                                 a->a_vals[k - 1] = a->a_vals[k];
379                         }
380                         a->a_vals[k - 1] = NULL;
381
382                         /* delete the entire attribute, if no values remain */
383                         if ( a->a_vals[0] == NULL) {
384                                 Debug( LDAP_DEBUG_ARGS,
385                                         "removing entire attribute %s\n",
386                                         mod->mod_type, 0, 0 );
387                                 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
388                                         return LDAP_NO_SUCH_ATTRIBUTE;
389                                 }
390                         }
391
392                         break;
393                 }
394
395                 /* looked through them all w/o finding it */
396                 if ( ! found ) {
397                         Debug( LDAP_DEBUG_ARGS,
398                             "could not find value for attr %s\n",
399                             mod->mod_type, 0, 0 );
400                         return( LDAP_NO_SUCH_ATTRIBUTE );
401                 }
402         }
403 #endif
404
405         return( LDAP_SUCCESS );
406 }
407
408 static int
409 replace_values(
410     Entry       *e,
411     Modification        *mod,
412     char        *dn
413 )
414 {
415 #ifdef SLAPD_SCHEMA_NOT_COMPAT
416         /* not yet implemented */
417 #else
418         (void) attr_delete( &e->e_attrs, mod->mod_type );
419
420         if ( mod->mod_bvalues != NULL &&
421                 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
422         {
423                 return( LDAP_CONSTRAINT_VIOLATION );
424         }
425 #endif
426
427         return( LDAP_SUCCESS );
428 }