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