]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Import alias deref finding bug fix from devel
[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         /* char *desc = mod->sm_desc->ad_cname->bv_val; */
272         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
273
274         a = attr_find( e->e_attrs, mod->sm_desc );
275
276         /* check if the values we're adding already exist */
277         if ( a != NULL ) {
278                 if( mr == NULL || !mr->smr_match ) {
279                         /* do not allow add of additional attribute
280                                 if no equality rule exists */
281                         return LDAP_INAPPROPRIATE_MATCHING;
282                 }
283
284                 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
285                         int rc;
286                         int j;
287                         const char *text = NULL;
288                         struct berval *asserted;
289
290                         rc = value_normalize( mod->sm_desc,
291                                 SLAP_MR_EQUALITY,
292                                 mod->sm_bvalues[i],
293                                 &asserted,
294                                 &text );
295
296                         if( rc != LDAP_SUCCESS ) return rc;
297
298                         for ( j = 0; a->a_vals[j] != NULL; j++ ) {
299                                 int match;
300                                 int rc = value_match( &match, mod->sm_desc, mr,
301                                         SLAP_MR_MODIFY_MATCHING,
302                                         a->a_vals[j], asserted, &text );
303
304                                 if( rc == LDAP_SUCCESS && match == 0 ) {
305                                         ber_bvfree( asserted );
306                                         return LDAP_TYPE_OR_VALUE_EXISTS;
307                                 }
308                         }
309
310                         ber_bvfree( asserted );
311                 }
312         }
313
314         /* no - add them */
315         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
316                 /* this should return result return of attr_merge */
317                 return LDAP_OTHER;
318         }
319
320         return LDAP_SUCCESS;
321 }
322
323 static int
324 delete_values(
325     Entry       *e,
326     Modification        *mod,
327     char        *dn
328 )
329 {
330         int             i, j, k, found;
331         Attribute       *a;
332         char *desc = mod->sm_desc->ad_cname->bv_val;
333         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
334
335         /* delete the entire attribute */
336         if ( mod->sm_bvalues == NULL ) {
337                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
338                     desc, 0, 0 );
339                 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
340                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
341         }
342
343         if( mr == NULL || !mr->smr_match ) {
344                 /* disallow specific attributes from being deleted if
345                         no equality rule */
346                 return LDAP_INAPPROPRIATE_MATCHING;
347         }
348
349         /* delete specific values - find the attribute first */
350         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
351                 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
352                         "could not find attribute %s\n",
353                     desc, 0, 0 );
354                 return( LDAP_NO_SUCH_ATTRIBUTE );
355         }
356
357         /* find each value to delete */
358         for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
359                 int rc;
360                 const char *text = NULL;
361
362                 struct berval *asserted;
363
364                 rc = value_normalize( mod->sm_desc,
365                         SLAP_MR_EQUALITY,
366                         mod->sm_bvalues[i],
367                         &asserted,
368                         &text );
369
370                 if( rc != LDAP_SUCCESS ) return rc;
371
372                 found = 0;
373                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
374                         int match;
375                         int rc = value_match( &match, mod->sm_desc, mr,
376                                 SLAP_MR_MODIFY_MATCHING,
377                                 a->a_vals[j], asserted, &text );
378
379                         if( rc == LDAP_SUCCESS && match != 0 ) {
380                                 continue;
381                         }
382
383                         /* found a matching value */
384                         found = 1;
385
386                         /* delete it */
387                         ber_bvfree( a->a_vals[j] );
388                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
389                                 a->a_vals[k - 1] = a->a_vals[k];
390                         }
391                         a->a_vals[k - 1] = NULL;
392
393                         break;
394                 }
395
396                 ber_bvfree( asserted );
397
398                 /* looked through them all w/o finding it */
399                 if ( ! found ) {
400                         Debug( LDAP_DEBUG_ARGS,
401                             "ldbm_modify_delete: could not find value for attr %s\n",
402                             desc, 0, 0 );
403                         return LDAP_NO_SUCH_ATTRIBUTE;
404                 }
405         }
406
407         /* if no values remain, delete the entire attribute */
408         if ( a->a_vals[0] == NULL ) {
409                 Debug( LDAP_DEBUG_ARGS,
410                         "removing entire attribute %s\n",
411                         desc, 0, 0 );
412                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
413                         return LDAP_NO_SUCH_ATTRIBUTE;
414                 }
415         }
416
417         return LDAP_SUCCESS;
418 }
419
420 static int
421 replace_values(
422     Entry       *e,
423     Modification        *mod,
424     char        *dn
425 )
426 {
427         int rc = attr_delete( &e->e_attrs, mod->sm_desc );
428
429         if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
430                 return rc;
431         }
432
433         if ( mod->sm_bvalues != NULL &&
434                 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
435         {
436                 return LDAP_OTHER;
437         }
438
439         return LDAP_SUCCESS;
440 }