]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Add a default case with assert() just in case.
[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 int ldbm_modify_internal(
30     Backend     *be,
31     Connection  *conn,
32     Operation   *op,
33     const char  *dn,
34     Modifications       *modlist,
35     Entry       *e,
36         const char **text,
37         char *textbuf,
38         size_t textlen
39 )
40 {
41         int rc = LDAP_SUCCESS;
42         Modification    *mod;
43         Modifications   *ml;
44         Attribute       *save_attrs;
45         Attribute       *ap;
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                         rc = add_values( e, mod, op->o_ndn.bv_val );
75
76                         if( rc != 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                                         rc, *text ));
82 #else
83                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
84                                         rc, *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                         rc = delete_values( e, mod, op->o_ndn.bv_val );
98                         assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
99                         if( rc != 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", rc, *text ));
104 #else
105                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
106                                         rc, *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                         rc = replace_values( e, mod, op->o_ndn.bv_val );
120                         assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
121                         if( rc != 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", rc, *text ));
126 #else
127                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
128                                         rc, *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                         rc = add_values( e, mod, op->o_ndn.bv_val );
147
148                         if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
149                                 rc = LDAP_SUCCESS;
150                         }
151
152                         if( rc != 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", rc, *text ));
157 #else
158                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
159                                         rc, *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                         rc = 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", rc, *text ));
179 #else
180                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
181                                 rc, *text, 0);
182 #endif
183
184                 }
185
186                 if ( rc != LDAP_SUCCESS ) {
187                         goto exit;
188                 }
189
190                 /* check if modified attribute was indexed */
191                 rc = index_is_indexed( be, mod->sm_desc );
192                 if ( rc == LDAP_SUCCESS ) {
193                         ap = attr_find( save_attrs, mod->sm_desc );
194                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
195
196                         ap = attr_find( e->e_attrs, mod->sm_desc );
197                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
198                 }
199         }
200
201         /* check that the entry still obeys the schema */
202         rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
203         if ( rc != LDAP_SUCCESS ) {
204 #ifdef NEW_LOGGING
205                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
206                         "ldbm_modify_internal: entry failed schema check: %s\n",
207                         *text ));
208 #else
209                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
210                         *text, 0, 0 );
211 #endif
212
213                 goto exit;
214         }
215
216         /* check for abandon */
217         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
218         rc = op->o_abandon;
219         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
220         if ( rc ) {
221                 rc = SLAPD_ABANDON;
222                 goto exit;
223         }
224
225         /* update the indices of the modified attributes */
226
227         /* start with deleting the old index entries */
228         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
229                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
230                         rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
231                                            SLAP_INDEX_DELETE_OP );
232                         if ( rc != LDAP_SUCCESS ) {
233 #ifdef NEW_LOGGING
234                                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
235                                            "ldbm_modify_internal: Attribute index delete failure\n" ));
236 #else
237                                 Debug( LDAP_DEBUG_ANY,
238                                        "Attribute index delete failure",
239                                        0, 0, 0 );
240 #endif
241                                 goto exit;
242                         }
243                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
244                 }
245         }
246
247         /* add the new index entries */
248         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
249                 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
250                         rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
251                                            SLAP_INDEX_ADD_OP );
252                         if ( rc != LDAP_SUCCESS ) {
253 #ifdef NEW_LOGGING
254                                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
255                                            "ldbm_modify_internal: Attribute index add failure\n" ));
256 #else
257                                 Debug( LDAP_DEBUG_ANY,
258                                        "Attribute index add failure",
259                                        0, 0, 0 );
260 #endif
261                                 goto exit;
262                         }
263                         ap->a_flags &= ~SLAP_ATTR_IXADD;
264                 }
265         }
266
267 exit:
268         if ( rc == LDAP_SUCCESS ) {
269                 attrs_free( save_attrs );
270         } else {
271                 for ( ap = save_attrs; ap; ap = ap->a_next ) {
272                         ap->a_flags = 0;
273                 }
274                 attrs_free( e->e_attrs );
275                 e->e_attrs = save_attrs;
276         }
277
278         return rc;
279 }
280
281 int
282 ldbm_back_modify(
283     Backend     *be,
284     Connection  *conn,
285     Operation   *op,
286     struct berval       *dn,
287     struct berval       *ndn,
288     Modifications       *modlist
289 )
290 {
291         int rc;
292         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
293         Entry           *matched;
294         Entry           *e;
295         int             manageDSAit = get_manageDSAit( op );
296         const char *text = NULL;
297         char textbuf[SLAP_TEXT_BUFLEN];
298         size_t textlen = sizeof textbuf;
299
300 #ifdef NEW_LOGGING
301         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
302                 "ldbm_back_modify: enter\n" ));
303 #else
304         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
305 #endif
306
307
308         /* acquire and lock entry */
309         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
310                 char* matched_dn = NULL;
311                 BerVarray refs;
312
313                 if ( matched != NULL ) {
314                         matched_dn = ch_strdup( matched->e_dn );
315                         refs = is_entry_referral( matched )
316                                 ? get_entry_referrals( be, conn, op, matched )
317                                 : NULL;
318                         cache_return_entry_r( &li->li_cache, matched );
319                 } else {
320                         refs = referral_rewrite( default_referral,
321                                 NULL, dn, LDAP_SCOPE_DEFAULT );
322                 }
323
324                 send_ldap_result( conn, op, LDAP_REFERRAL,
325                         matched_dn, NULL, refs, NULL );
326
327                 if ( refs ) ber_bvarray_free( refs );
328                 free( matched_dn );
329
330                 return( -1 );
331         }
332
333     if ( !manageDSAit && is_entry_referral( e ) ) {
334                 /* parent is a referral, don't allow add */
335                 /* parent is an alias, don't allow add */
336                 BerVarray refs = get_entry_referrals( be,
337                         conn, op, e );
338
339 #ifdef NEW_LOGGING
340                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
341                            "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val ));
342 #else
343                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
344                     0, 0 );
345 #endif
346
347
348                 send_ldap_result( conn, op, LDAP_REFERRAL,
349                     e->e_dn, NULL, refs, NULL );
350
351                 if ( refs ) ber_bvarray_free( refs );
352
353                 goto error_return;
354         }
355         
356         /* Modify the entry */
357         rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
358                 &text, textbuf, textlen );
359
360         if( rc != LDAP_SUCCESS ) {
361                 if( rc != SLAPD_ABANDON ) {
362                         send_ldap_result( conn, op, rc,
363                                 NULL, text, NULL, NULL );
364                 }
365
366                 goto error_return;
367         }
368
369         /* change the entry itself */
370         if ( id2entry_add( be, e ) != 0 ) {
371                 send_ldap_result( conn, op, LDAP_OTHER,
372                         NULL, "id2entry failure", NULL, NULL );
373                 goto error_return;
374         }
375
376         send_ldap_result( conn, op, LDAP_SUCCESS,
377                 NULL, NULL, NULL, NULL );
378
379         cache_return_entry_w( &li->li_cache, e );
380         return( 0 );
381
382 error_return:;
383         cache_return_entry_w( &li->li_cache, e );
384         return( -1 );
385 }
386
387 static int
388 add_values(
389     Entry       *e,
390     Modification        *mod,
391     char        *dn
392 )
393 {
394         int             i;
395         Attribute       *a;
396
397         /* char *desc = mod->sm_desc->ad_cname.bv_val; */
398         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
399
400         a = attr_find( e->e_attrs, mod->sm_desc );
401
402         /* check if the values we're adding already exist */
403         if ( a != NULL ) {
404                 if( mr == NULL || !mr->smr_match ) {
405                         /* do not allow add of additional attribute
406                                 if no equality rule exists */
407                         return LDAP_INAPPROPRIATE_MATCHING;
408                 }
409
410                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
411                         int rc;
412                         int j;
413                         const char *text = NULL;
414                         struct berval asserted;
415
416                         rc = value_normalize( mod->sm_desc,
417                                 SLAP_MR_EQUALITY,
418                                 &mod->sm_bvalues[i],
419                                 &asserted,
420                                 &text );
421
422                         if( rc != LDAP_SUCCESS ) return rc;
423
424                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
425                                 int match;
426                                 int rc = value_match( &match, mod->sm_desc, mr,
427                                         SLAP_MR_VALUE_SYNTAX_MATCH,
428                                         &a->a_vals[j], &asserted, &text );
429
430                                 if( rc == LDAP_SUCCESS && match == 0 ) {
431                                         free( asserted.bv_val );
432                                         return LDAP_TYPE_OR_VALUE_EXISTS;
433                                 }
434                         }
435
436                         free( asserted.bv_val );
437                 }
438         }
439
440         /* no - add them */
441         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
442                 /* this should return result return of attr_merge */
443                 return LDAP_OTHER;
444         }
445
446         return LDAP_SUCCESS;
447 }
448
449 static int
450 delete_values(
451     Entry       *e,
452     Modification        *mod,
453     char        *dn
454 )
455 {
456         int             i, j, k, found;
457         Attribute       *a;
458         char *desc = mod->sm_desc->ad_cname.bv_val;
459         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
460
461         /* delete the entire attribute */
462         if ( mod->sm_bvalues == NULL ) {
463 #ifdef NEW_LOGGING
464                 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
465                            "delete_values: removing entire attribute %s\n", desc ));
466 #else
467                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
468                     desc, 0, 0 );
469 #endif
470
471                 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
472                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
473         }
474
475         if( mr == NULL || !mr->smr_match ) {
476                 /* disallow specific attributes from being deleted if
477                         no equality rule */
478                 return LDAP_INAPPROPRIATE_MATCHING;
479         }
480
481         /* delete specific values - find the attribute first */
482         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
483 #ifdef NEW_LOGGING
484                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
485                            "ldap_modify_delete: Could not find attribute %s\n", desc ));
486 #else
487                 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
488                         "could not find attribute %s\n",
489                     desc, 0, 0 );
490 #endif
491
492                 return( LDAP_NO_SUCH_ATTRIBUTE );
493         }
494
495         /* find each value to delete */
496         for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
497                 int rc;
498                 const char *text = NULL;
499
500                 struct berval asserted;
501
502                 rc = value_normalize( mod->sm_desc,
503                         SLAP_MR_EQUALITY,
504                         &mod->sm_bvalues[i],
505                         &asserted,
506                         &text );
507
508                 if( rc != LDAP_SUCCESS ) return rc;
509
510                 found = 0;
511                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
512                         int match;
513                         int rc = value_match( &match, mod->sm_desc, mr,
514                                 SLAP_MR_VALUE_SYNTAX_MATCH,
515                                 &a->a_vals[j], &asserted, &text );
516
517                         if( rc == LDAP_SUCCESS && match != 0 ) {
518                                 continue;
519                         }
520
521                         /* found a matching value */
522                         found = 1;
523
524                         /* delete it */
525                         free( a->a_vals[j].bv_val );
526                         for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
527                                 a->a_vals[k - 1] = a->a_vals[k];
528                         }
529                         a->a_vals[k - 1].bv_val = NULL;
530
531                         break;
532                 }
533
534                 free( asserted.bv_val );
535
536                 /* looked through them all w/o finding it */
537                 if ( ! found ) {
538 #ifdef NEW_LOGGING
539                         LDAP_LOG(( "backend", LDAP_LEVEL_ARGS,
540                                    "delete_values: could not find value for attr %s\n", desc )); 
541 #else
542                         Debug( LDAP_DEBUG_ARGS,
543                             "ldbm_modify_delete: could not find value for attr %s\n",
544                             desc, 0, 0 );
545 #endif
546
547                         return LDAP_NO_SUCH_ATTRIBUTE;
548                 }
549         }
550
551         /* if no values remain, delete the entire attribute */
552         if ( a->a_vals[0].bv_val == NULL ) {
553 #ifdef NEW_LOGGING
554                 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
555                            "delete_values: removing entire attribute %s\n", desc ));
556 #else
557                 Debug( LDAP_DEBUG_ARGS,
558                         "removing entire attribute %s\n",
559                         desc, 0, 0 );
560 #endif
561
562                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
563                         return LDAP_NO_SUCH_ATTRIBUTE;
564                 }
565         }
566
567         return LDAP_SUCCESS;
568 }
569
570 static int
571 replace_values(
572     Entry       *e,
573     Modification        *mod,
574     char        *dn
575 )
576 {
577         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
578
579         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
580                 return rc;
581         }
582
583         if ( mod->sm_bvalues != NULL &&
584                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
585         {
586                 return LDAP_OTHER;
587         }
588
589         return LDAP_SUCCESS;
590 }