]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
18c83efc7c8883a71b232c1a6a050e0fbc94443e
[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 #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 );
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 );
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 );
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 );
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( 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     const char  *dn,
249     const char  *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                 struct berval **refs = NULL;
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 = default_referral;
283                 }
284
285                 send_ldap_result( conn, op, LDAP_REFERRAL,
286                         matched_dn, NULL, refs, NULL );
287
288                 if ( matched != NULL ) {
289                         ber_bvecfree( refs );
290                         free( matched_dn );
291                 }
292
293                 return( -1 );
294         }
295
296     if ( !manageDSAit && is_entry_referral( e ) ) {
297                 /* parent is a referral, don't allow add */
298                 /* parent is an alias, don't allow add */
299                 struct berval **refs = get_entry_referrals( be,
300                         conn, op, e );
301
302 #ifdef NEW_LOGGING
303                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
304                            "ldbm_back_modify: entry (%s) is referral\n", ndn ));
305 #else
306                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
307                     0, 0 );
308 #endif
309
310
311                 send_ldap_result( conn, op, LDAP_REFERRAL,
312                     e->e_dn, NULL, refs, NULL );
313
314                 ber_bvecfree( refs );
315
316                 goto error_return;
317         }
318         
319         /* Modify the entry */
320         rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e,
321                 &text, textbuf, textlen );
322
323         if( rc != LDAP_SUCCESS ) {
324                 if( rc != SLAPD_ABANDON ) {
325                         send_ldap_result( conn, op, rc,
326                                 NULL, text, NULL, NULL );
327                 }
328
329                 goto error_return;
330         }
331
332         /* change the entry itself */
333         if ( id2entry_add( be, e ) != 0 ) {
334                 send_ldap_result( conn, op, LDAP_OTHER,
335                         NULL, "id2entry failure", NULL, NULL );
336                 goto error_return;
337         }
338
339         send_ldap_result( conn, op, LDAP_SUCCESS,
340                 NULL, NULL, NULL, NULL );
341
342         cache_return_entry_w( &li->li_cache, e );
343         return( 0 );
344
345 error_return:;
346         cache_return_entry_w( &li->li_cache, e );
347         return( -1 );
348 }
349
350 static int
351 add_values(
352     Entry       *e,
353     Modification        *mod,
354     char        *dn
355 )
356 {
357         int             i;
358         Attribute       *a;
359
360         /* char *desc = mod->sm_desc->ad_cname.bv_val; */
361         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
362
363         a = attr_find( e->e_attrs, mod->sm_desc );
364
365         /* check if the values we're adding already exist */
366         if ( a != NULL ) {
367                 if( mr == NULL || !mr->smr_match ) {
368                         /* do not allow add of additional attribute
369                                 if no equality rule exists */
370                         return LDAP_INAPPROPRIATE_MATCHING;
371                 }
372
373                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
374                         int rc;
375                         int j;
376                         const char *text = NULL;
377                         struct berval *asserted;
378
379                         rc = value_normalize( mod->sm_desc,
380                                 SLAP_MR_EQUALITY,
381                                 mod->sm_bvalues[i],
382                                 &asserted,
383                                 &text );
384
385                         if( rc != LDAP_SUCCESS ) return rc;
386
387                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
388                                 int match;
389                                 int rc = value_match( &match, mod->sm_desc, mr,
390                                         SLAP_MR_MODIFY_MATCHING,
391                                         a->a_vals[j], asserted, &text );
392
393                                 if( rc == LDAP_SUCCESS && match == 0 ) {
394                                         ber_bvfree( asserted );
395                                         return LDAP_TYPE_OR_VALUE_EXISTS;
396                                 }
397                         }
398
399                         ber_bvfree( asserted );
400                 }
401         }
402
403         /* no - add them */
404         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
405                 /* this should return result return of attr_merge */
406                 return LDAP_OTHER;
407         }
408
409         return LDAP_SUCCESS;
410 }
411
412 static int
413 delete_values(
414     Entry       *e,
415     Modification        *mod,
416     char        *dn
417 )
418 {
419         int             i, j, k, found;
420         Attribute       *a;
421         char *desc = mod->sm_desc->ad_cname.bv_val;
422         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
423
424         /* delete the entire attribute */
425         if ( mod->sm_bvalues == NULL ) {
426 #ifdef NEW_LOGGING
427                 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
428                            "delete_values: removing entire attribute %s\n", desc ));
429 #else
430                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
431                     desc, 0, 0 );
432 #endif
433
434                 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
435                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
436         }
437
438         if( mr == NULL || !mr->smr_match ) {
439                 /* disallow specific attributes from being deleted if
440                         no equality rule */
441                 return LDAP_INAPPROPRIATE_MATCHING;
442         }
443
444         /* delete specific values - find the attribute first */
445         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
446 #ifdef NEW_LOGGING
447                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
448                            "ldap_modify_delete: Could not find attribute %s\n", desc ));
449 #else
450                 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
451                         "could not find attribute %s\n",
452                     desc, 0, 0 );
453 #endif
454
455                 return( LDAP_NO_SUCH_ATTRIBUTE );
456         }
457
458         /* find each value to delete */
459         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
460                 int rc;
461                 const char *text = NULL;
462
463                 struct berval *asserted;
464
465                 rc = value_normalize( mod->sm_desc,
466                         SLAP_MR_EQUALITY,
467                         mod->sm_bvalues[i],
468                         &asserted,
469                         &text );
470
471                 if( rc != LDAP_SUCCESS ) return rc;
472
473                 found = 0;
474                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
475                         int match;
476                         int rc = value_match( &match, mod->sm_desc, mr,
477                                 SLAP_MR_MODIFY_MATCHING,
478                                 a->a_vals[j], asserted, &text );
479
480                         if( rc == LDAP_SUCCESS && match != 0 ) {
481                                 continue;
482                         }
483
484                         /* found a matching value */
485                         found = 1;
486
487                         /* delete it */
488                         ber_bvfree( a->a_vals[j] );
489                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
490                                 a->a_vals[k - 1] = a->a_vals[k];
491                         }
492                         a->a_vals[k - 1] = NULL;
493
494                         break;
495                 }
496
497                 ber_bvfree( asserted );
498
499                 /* looked through them all w/o finding it */
500                 if ( ! found ) {
501 #ifdef NEW_LOGGING
502                         LDAP_LOG(( "backend", LDAP_LEVEL_ARGS,
503                                    "delete_values: could not find value for attr %s\n", desc )); 
504 #else
505                         Debug( LDAP_DEBUG_ARGS,
506                             "ldbm_modify_delete: could not find value for attr %s\n",
507                             desc, 0, 0 );
508 #endif
509
510                         return LDAP_NO_SUCH_ATTRIBUTE;
511                 }
512         }
513
514         /* if no values remain, delete the entire attribute */
515         if ( a->a_vals[0] == NULL ) {
516 #ifdef NEW_LOGGING
517                 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
518                            "delete_values: removing entire attribute %s\n", desc ));
519 #else
520                 Debug( LDAP_DEBUG_ARGS,
521                         "removing entire attribute %s\n",
522                         desc, 0, 0 );
523 #endif
524
525                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
526                         return LDAP_NO_SUCH_ATTRIBUTE;
527                 }
528         }
529
530         return LDAP_SUCCESS;
531 }
532
533 static int
534 replace_values(
535     Entry       *e,
536     Modification        *mod,
537     char        *dn
538 )
539 {
540         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
541
542         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
543                 return rc;
544         }
545
546         if ( mod->sm_bvalues != NULL &&
547                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
548         {
549                 return LDAP_OTHER;
550         }
551
552         return LDAP_SUCCESS;
553 }