]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Prepare for unifdef -DSLAPD_SCHEMA_NOT_COMPAT
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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     const char  *dn,
35     Modifications       *modlist,
36     Entry       *e,
37         const char **text 
38 )
39 {
40         int rc, err;
41         Modification    *mod;
42         Modifications   *ml;
43         Attribute       *save_attrs;
44
45         Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal:\n", 0, 0, 0);
46
47         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
48                 return LDAP_INSUFFICIENT_ACCESS;
49         }
50
51         save_attrs = e->e_attrs;
52         e->e_attrs = attrs_dup( e->e_attrs );
53
54         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
55                 mod = &ml->sml_mod;
56
57                 switch ( mod->sm_op ) {
58                 case LDAP_MOD_ADD:
59                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
60                         err = add_values( e, mod, op->o_ndn );
61
62                         if( err != LDAP_SUCCESS ) {
63                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
64                                         err, text, 0);
65                                 *text = "modify: add values failed";
66                         }
67                         break;
68
69                 case LDAP_MOD_DELETE:
70                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
71                         err = delete_values( e, mod, op->o_ndn );
72                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
73                         if( err != LDAP_SUCCESS ) {
74                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
75                                         err, text, 0);
76                                 *text = "modify: delete values failed";
77                         }
78                         break;
79
80                 case LDAP_MOD_REPLACE:
81                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
82                         err = replace_values( e, mod, op->o_ndn );
83                         assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
84                         if( err != LDAP_SUCCESS ) {
85                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
86                                         err, text, 0);
87                                 *text = "modify: replace values failed";
88                         }
89                         break;
90
91                 case SLAP_MOD_SOFTADD:
92                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
93                         /* Avoid problems in index_add_mods()
94                          * We need to add index if necessary.
95                          */
96                         mod->sm_op = LDAP_MOD_ADD;
97                         err = add_values( e, mod, op->o_ndn );
98
99                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
100                                 err = LDAP_SUCCESS;
101                         }
102
103                         if( err != LDAP_SUCCESS ) {
104                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
105                                         err, text, 0);
106                                 *text = "modify: (soft)add values failed";
107                         }
108                         break;
109
110                 default:
111                         Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
112                                 mod->sm_op, 0, 0);
113                         *text = "Invalid modify operation";
114                         err = LDAP_OTHER;
115                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
116                                 err, text, 0);
117                 }
118
119                 if ( err != LDAP_SUCCESS ) {
120                         attrs_free( e->e_attrs );
121                         e->e_attrs = save_attrs;
122                         /* unlock entry, delete from cache */
123                         return err; 
124                 }
125         }
126
127         /* check for abandon */
128         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
129         if ( op->o_abandon ) {
130                 attrs_free( e->e_attrs );
131                 e->e_attrs = save_attrs;
132                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
133                 return SLAPD_ABANDON;
134         }
135         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
136
137         /* check that the entry still obeys the schema */
138         rc = entry_schema_check( e, save_attrs, text );
139         if ( rc != LDAP_SUCCESS ) {
140                 attrs_free( e->e_attrs );
141                 e->e_attrs = save_attrs;
142                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
143                         *text, 0, 0 );
144                 return rc;
145         }
146
147         /* check for abandon */
148         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
149         if ( op->o_abandon ) {
150                 attrs_free( e->e_attrs );
151                 e->e_attrs = save_attrs;
152                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
153                 return SLAPD_ABANDON;
154         }
155         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
156
157         /* delete indices for old attributes */
158         index_entry_del( be, e, save_attrs);
159
160         /* add indices for new attributes */
161         index_entry_add( be, e, e->e_attrs);
162
163         attrs_free( save_attrs );
164
165         return LDAP_SUCCESS;
166 }
167
168
169 int
170 ldbm_back_modify(
171     Backend     *be,
172     Connection  *conn,
173     Operation   *op,
174     const char  *dn,
175     const char  *ndn,
176     Modifications       *modlist
177 )
178 {
179         int rc;
180         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
181         Entry           *matched;
182         Entry           *e;
183         int             manageDSAit = get_manageDSAit( op );
184         const char *text = NULL;
185
186         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
187
188         /* acquire and lock entry */
189         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
190                 char* matched_dn = NULL;
191                 struct berval **refs = NULL;
192
193                 if ( matched != NULL ) {
194                         matched_dn = ch_strdup( matched->e_dn );
195                         refs = is_entry_referral( matched )
196                                 ? get_entry_referrals( be, conn, op, matched )
197                                 : NULL;
198                         cache_return_entry_r( &li->li_cache, matched );
199                 } else {
200                         refs = default_referral;
201                 }
202
203                 send_ldap_result( conn, op, LDAP_REFERRAL,
204                         matched_dn, NULL, refs, NULL );
205
206                 if ( matched != NULL ) {
207                         ber_bvecfree( refs );
208                         free( matched_dn );
209                 }
210
211                 return( -1 );
212         }
213
214     if ( !manageDSAit && is_entry_referral( e ) ) {
215                 /* parent is a referral, don't allow add */
216                 /* parent is an alias, don't allow add */
217                 struct berval **refs = get_entry_referrals( be,
218                         conn, op, e );
219
220                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
221                     0, 0 );
222
223                 send_ldap_result( conn, op, LDAP_REFERRAL,
224                     e->e_dn, NULL, refs, NULL );
225
226                 ber_bvecfree( refs );
227
228                 goto error_return;
229         }
230         
231         /* Modify the entry */
232         rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e, &text );
233
234         if( rc != LDAP_SUCCESS ) {
235                 if( rc != SLAPD_ABANDON ) {
236                         send_ldap_result( conn, op, rc,
237                                 NULL, text, NULL, NULL );
238                 }
239
240                 goto error_return;
241         }
242
243         /* change the entry itself */
244         if ( id2entry_add( be, e ) != 0 ) {
245                 send_ldap_result( conn, op, LDAP_OTHER,
246                         NULL, "id2entry failure", NULL, NULL );
247                 goto error_return;
248         }
249
250         send_ldap_result( conn, op, LDAP_SUCCESS,
251                 NULL, NULL, NULL, NULL );
252
253         cache_return_entry_w( &li->li_cache, e );
254         return( 0 );
255
256 error_return:;
257         cache_return_entry_w( &li->li_cache, e );
258         return( -1 );
259 }
260
261 static int
262 add_values(
263     Entry       *e,
264     Modification        *mod,
265     char        *dn
266 )
267 {
268         int             i;
269         Attribute       *a;
270
271 #ifdef SLAPD_SCHEMA_NOT_COMPAT
272         /* char *desc = mod->sm_desc->ad_cname->bv_val; */
273         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
274
275         if( mr == NULL ) {
276                 return LDAP_INAPPROPRIATE_MATCHING;
277         }
278
279 #else
280         /* char *desc = mod->mod_type; */
281 #endif
282
283         a = attr_find( e->e_attrs, mod->sm_desc );
284
285         /* check if the values we're adding already exist */
286         if ( a != NULL ) {
287                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
288 #ifdef SLAPD_SCHEMA_NOT_COMPAT
289                         int rc;
290                         int j;
291                         const char *text = NULL;
292                         struct berval *asserted;
293
294                         rc = value_normalize( mod->sm_desc,
295                                 SLAP_MR_EQUALITY,
296                                 mod->sm_bvalues[i],
297                                 &asserted,
298                                 &text );
299
300                         if( rc != LDAP_SUCCESS ) return rc;
301
302                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
303                                 int match;
304                                 int rc = value_match( &match, mod->sm_desc, mr,
305                                         a->a_vals[j], asserted, &text );
306
307                                 if( rc == LDAP_SUCCESS && match == 0 ) {
308                                         ber_bvfree( asserted );
309                                         return LDAP_TYPE_OR_VALUE_EXISTS;
310                                 }
311                         }
312
313                         ber_bvfree( asserted );
314 #else
315                         if ( value_find( a->a_vals, mod->sm_bvalues[i],
316                             a->a_syntax, 3 ) == 0 ) {
317                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
318                         }
319 #endif
320                 }
321         }
322
323         /* no - add them */
324         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
325                 /* this should return result return of attr_merge */
326                 return LDAP_OTHER;
327         }
328
329         return LDAP_SUCCESS;
330 }
331
332 static int
333 delete_values(
334     Entry       *e,
335     Modification        *mod,
336     char        *dn
337 )
338 {
339         int             i, j, k, found;
340         Attribute       *a;
341 #ifdef SLAPD_SCHEMA_NOT_COMPAT
342         char *desc = mod->sm_desc->ad_cname->bv_val;
343         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
344
345         if( mr == NULL || !mr->smr_match ) {
346                 return LDAP_INAPPROPRIATE_MATCHING;
347         }
348 #else
349         char *desc = mod->mod_type;
350 #endif
351
352         /* delete the entire attribute */
353         if ( mod->sm_bvalues == NULL ) {
354                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
355                     desc, 0, 0 );
356                 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
357                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
358         }
359
360         /* delete specific values - find the attribute first */
361         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
362                 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
363                         "could not find attribute %s\n",
364                     desc, 0, 0 );
365                 return( LDAP_NO_SUCH_ATTRIBUTE );
366         }
367
368         /* find each value to delete */
369         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
370 #ifdef SLAPD_SCHEMA_NOT_COMPAT
371                 int rc;
372                 const char *text = NULL;
373
374                 struct berval *asserted;
375
376                 rc = value_normalize( mod->sm_desc,
377                         SLAP_MR_EQUALITY,
378                         mod->sm_bvalues[i],
379                         &asserted,
380                         &text );
381
382                 if( rc != LDAP_SUCCESS ) return rc;
383 #endif
384
385                 found = 0;
386                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
387 #ifdef SLAPD_SCHEMA_NOT_COMPAT
388                         int match;
389                         int rc = value_match( &match, mod->sm_desc, mr,
390                                 a->a_vals[j], asserted, &text );
391
392                         if( rc == LDAP_SUCCESS && match != 0 )
393 #else
394                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
395                             a->a_syntax, 3 ) != 0 )
396 #endif
397                         {
398                                 continue;
399                         }
400                         found = 1;
401
402                         /* found a matching value - delete it */
403                         ber_bvfree( a->a_vals[j] );
404                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
405                                 a->a_vals[k - 1] = a->a_vals[k];
406                         }
407                         a->a_vals[k - 1] = NULL;
408
409                         /* delete the entire attribute, if no values remain */
410                         if ( a->a_vals[0] == NULL) {
411                                 Debug( LDAP_DEBUG_ARGS,
412                                         "removing entire attribute %s\n",
413                                         desc, 0, 0 );
414                                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
415 #ifdef SLAPD_SCHEMA_NOT_COMPAT
416                                         ber_bvfree( asserted );
417 #endif
418                                         return LDAP_NO_SUCH_ATTRIBUTE;
419                                 }
420                         }
421
422                         break;
423                 }
424
425 #ifdef SLAPD_SCHEMA_NOT_COMPAT
426                 ber_bvfree( asserted );
427 #endif
428
429                 /* looked through them all w/o finding it */
430                 if ( ! found ) {
431                         Debug( LDAP_DEBUG_ARGS,
432                             "ldbm_modify_delete: could not find value for attr %s\n",
433                             desc, 0, 0 );
434                         return LDAP_NO_SUCH_ATTRIBUTE;
435                 }
436         }
437
438         return LDAP_SUCCESS;
439 }
440
441 static int
442 replace_values(
443     Entry       *e,
444     Modification        *mod,
445     char        *dn
446 )
447 {
448         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
449
450         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
451                 return rc;
452         }
453
454         if ( mod->sm_bvalues != NULL &&
455                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
456         {
457                 return LDAP_OTHER;
458         }
459
460         return LDAP_SUCCESS;
461 }