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