]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
Notices and acknowledgements
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 /* We need this function because of LDAP modrdn. If we do not 
21  * add this there would be a bunch of code replication here 
22  * and there and of course the likelihood of bugs increases.
23  * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
24  */ 
25 int ldbm_modify_internal(
26     Operation   *op,
27     Modifications       *modlist,
28     Entry       *e,
29         const char **text,
30         char *textbuf,
31         size_t textlen
32 )
33 {
34         int rc = LDAP_SUCCESS;
35         Modification    *mod;
36         Modifications   *ml;
37         Attribute       *save_attrs;
38         Attribute       *ap;
39
40 #ifdef NEW_LOGGING
41         LDAP_LOG( BACK_LDBM, ENTRY,  "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0 );
42 #else
43         Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", e->e_name.bv_val, 0, 0);
44 #endif
45
46
47         if ( !acl_check_modlist( 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 #ifdef NEW_LOGGING
60                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
61 #else
62                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
63 #endif
64
65                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
66                                                 text, textbuf, textlen );
67                         if( rc != LDAP_SUCCESS ) {
68 #ifdef NEW_LOGGING
69                                 LDAP_LOG( BACK_LDBM, INFO, 
70                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
71 #else
72                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
73                                         rc, *text, 0);
74 #endif
75                         }
76                         break;
77
78                 case LDAP_MOD_DELETE:
79 #ifdef NEW_LOGGING
80                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
81 #else
82                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
83 #endif
84
85                         rc = modify_delete_values( e, mod, get_permissiveModify( op ),
86                                                         text, textbuf, textlen );
87                         assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
88                         if( rc != LDAP_SUCCESS ) {
89 #ifdef NEW_LOGGING
90                                 LDAP_LOG( BACK_LDBM, INFO, 
91                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
92 #else
93                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
94                                         rc, *text, 0);
95 #endif
96                         }
97                         break;
98
99                 case LDAP_MOD_REPLACE:
100 #ifdef NEW_LOGGING
101                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal:  replace\n",0,0,0);
102 #else
103                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
104 #endif
105
106                         rc = modify_replace_values( e, mod, get_permissiveModify( op ),
107                                                         text, textbuf, textlen );
108                         if( rc != LDAP_SUCCESS ) {
109 #ifdef NEW_LOGGING
110                                 LDAP_LOG( BACK_LDBM, INFO, 
111                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
112 #else
113                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
114                                         rc, *text, 0);
115 #endif
116                         }
117                         break;
118
119                 case LDAP_MOD_INCREMENT:
120 #ifdef NEW_LOGGING
121                         LDAP_LOG( BACK_LDBM, DETAIL1,
122                                 "ldbm_modify_internal:  increment\n",0,0,0);
123 #else
124                         Debug(LDAP_DEBUG_ARGS,
125                                 "ldbm_modify_internal:  increment\n",0,0,0);
126 #endif
127
128                         rc = modify_increment_values( e, mod, get_permissiveModify( op ),
129                                 text, textbuf, textlen );
130                         if( rc != LDAP_SUCCESS ) {
131 #ifdef NEW_LOGGING
132                                 LDAP_LOG( BACK_LDBM, INFO, 
133                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
134 #else
135                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
136                                         rc, *text, 0);
137 #endif
138                         }
139                         break;
140
141                 case SLAP_MOD_SOFTADD:
142 #ifdef NEW_LOGGING
143                         LDAP_LOG( BACK_LDBM, DETAIL1, 
144                                 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
145 #else
146                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
147 #endif
148
149                         /* Avoid problems in index_add_mods()
150                          * We need to add index if necessary.
151                          */
152                         mod->sm_op = LDAP_MOD_ADD;
153
154                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
155                                                 text, textbuf, textlen );
156                         mod->sm_op = SLAP_MOD_SOFTADD;
157                         if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
158                                 rc = LDAP_SUCCESS;
159                         }
160
161                         if( rc != LDAP_SUCCESS ) {
162 #ifdef NEW_LOGGING
163                                 LDAP_LOG( BACK_LDBM, INFO, 
164                                            "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
165 #else
166                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
167                                         rc, *text, 0);
168 #endif
169                         }
170                         break;
171
172                 default:
173 #ifdef NEW_LOGGING
174                         LDAP_LOG( BACK_LDBM, ERR, 
175                                 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
176 #else
177                         Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
178                                 mod->sm_op, 0, 0);
179 #endif
180
181                         rc = LDAP_OTHER;
182                         *text = "Invalid modify operation";
183 #ifdef NEW_LOGGING
184                         LDAP_LOG( BACK_LDBM, INFO, 
185                                 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
186 #else
187                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
188                                 rc, *text, 0);
189 #endif
190                 }
191
192                 if ( rc != LDAP_SUCCESS ) {
193                         goto exit;
194                 }
195
196                 /* If objectClass was modified, reset the flags */
197                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
198                         e->e_ocflags = 0;
199                 }
200
201                 /* check if modified attribute was indexed */
202                 rc = index_is_indexed( op->o_bd, mod->sm_desc );
203                 if ( rc == LDAP_SUCCESS ) {
204                         ap = attr_find( save_attrs, mod->sm_desc );
205                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
206
207                         ap = attr_find( e->e_attrs, mod->sm_desc );
208                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
209                 }
210         }
211
212         /* check that the entry still obeys the schema */
213         rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
214
215         if ( rc != LDAP_SUCCESS ) {
216 #ifdef NEW_LOGGING
217                 LDAP_LOG( BACK_LDBM, ERR, 
218                         "ldbm_modify_internal: entry failed schema check: %s\n", 
219                         *text, 0, 0 );
220 #else
221                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
222                         *text, 0, 0 );
223 #endif
224
225                 goto exit;
226         }
227
228         /* check for abandon */
229         if ( op->o_abandon ) {
230                 rc = SLAPD_ABANDON;
231                 goto exit;
232         }
233
234         /* update the indices of the modified attributes */
235
236         /* start with deleting the old index entries */
237         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
238                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
239                         rc = index_values( op, ap->a_desc,
240                                 ap->a_nvals,
241                                 e->e_id, SLAP_INDEX_DELETE_OP );
242                         if ( rc != LDAP_SUCCESS ) {
243 #ifdef NEW_LOGGING
244                                 LDAP_LOG( BACK_LDBM, ERR,
245                                         "ldbm_modify_internal: Attribute index delete failure\n",
246                                         0, 0, 0 );
247 #else
248                                 Debug( LDAP_DEBUG_ANY,
249                                        "Attribute index delete failure",
250                                        0, 0, 0 );
251 #endif
252                                 goto exit;
253                         }
254                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
255                 }
256         }
257
258         /* add the new index entries */
259         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
260                 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
261                         rc = index_values( op, ap->a_desc,
262                                 ap->a_nvals,
263                                 e->e_id, SLAP_INDEX_ADD_OP );
264                         if ( rc != LDAP_SUCCESS ) {
265 #ifdef NEW_LOGGING
266                                 LDAP_LOG( BACK_LDBM, ERR,
267                                         "ldbm_modify_internal: Attribute index add failure\n",
268                                         0, 0, 0 );
269 #else
270                                 Debug( LDAP_DEBUG_ANY,
271                                        "Attribute index add failure",
272                                        0, 0, 0 );
273 #endif
274                                 goto exit;
275                         }
276                         ap->a_flags &= ~SLAP_ATTR_IXADD;
277                 }
278         }
279
280 exit:
281         if ( rc == LDAP_SUCCESS ) {
282                 attrs_free( save_attrs );
283         } else {
284                 for ( ap = save_attrs; ap; ap = ap->a_next ) {
285                         ap->a_flags = 0;
286                 }
287                 attrs_free( e->e_attrs );
288                 e->e_attrs = save_attrs;
289         }
290
291         return rc;
292 }
293
294 int
295 ldbm_back_modify(
296     Operation   *op,
297     SlapReply   *rs )
298 {
299         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
300         Entry           *matched;
301         Entry           *e;
302         int             manageDSAit = get_manageDSAit( op );
303         char textbuf[SLAP_TEXT_BUFLEN];
304         size_t textlen = sizeof textbuf;
305
306 #ifdef NEW_LOGGING
307         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
308 #else
309         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
310 #endif
311
312         /* grab giant lock for writing */
313         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
314
315         /* acquire and lock entry */
316         e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
317
318         /* FIXME: dn2entry() should return non-glue entry */
319         if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
320                 if ( matched != NULL ) {
321                         rs->sr_matched = ch_strdup( matched->e_dn );
322                         rs->sr_ref = is_entry_referral( matched )
323                                 ? get_entry_referrals( op, matched )
324                                 : NULL;
325                         cache_return_entry_r( &li->li_cache, matched );
326                 } else {
327                         BerVarray deref = NULL;
328                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
329                                 syncinfo_t *si;
330                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
331                                         struct berval tmpbv;
332                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
333                                         ber_bvarray_add( &deref, &tmpbv );
334                                 }
335                         } else {
336                                 deref = default_referral;
337                         }
338                         rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn,
339                                                         LDAP_SCOPE_DEFAULT );
340                 }
341
342                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
343                 rs->sr_err = LDAP_REFERRAL;
344                 send_ldap_result( op, rs );
345
346                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
347                 free( (char *)rs->sr_matched );
348
349                 rs->sr_ref = NULL;
350                 rs->sr_matched = NULL;
351                 return rs->sr_err;
352         }
353
354         if ( !manageDSAit && is_entry_referral( e ) )
355         {
356                 /* parent is a referral, don't allow add */
357                 /* parent is an alias, don't allow add */
358                 rs->sr_ref = get_entry_referrals( op, e );
359
360 #ifdef NEW_LOGGING
361                 LDAP_LOG( BACK_LDBM, INFO, 
362                            "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
363 #else
364                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
365                     0, 0 );
366 #endif
367
368                 rs->sr_err = LDAP_REFERRAL;
369                 rs->sr_matched = e->e_name.bv_val;
370                 send_ldap_result( op, rs );
371
372                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
373                 rs->sr_ref = NULL;
374                 rs->sr_matched = NULL;
375                 goto error_return;
376         }
377         
378         /* Modify the entry */
379         rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
380                 &rs->sr_text, textbuf, textlen );
381
382         if( rs->sr_err != LDAP_SUCCESS ) {
383                 if( rs->sr_err != SLAPD_ABANDON ) {
384                         send_ldap_result( op, rs );
385                 }
386
387                 goto error_return;
388         }
389
390         /* change the entry itself */
391         if ( id2entry_add( op->o_bd, e ) != 0 ) {
392                 send_ldap_error( op, rs, LDAP_OTHER,
393                         "id2entry failure" );
394                 rs->sr_err = LDAP_OTHER;
395                 goto error_return;
396         }
397
398         rs->sr_text = NULL;
399         send_ldap_error( op, rs, LDAP_SUCCESS,
400                 NULL );
401
402         cache_return_entry_w( &li->li_cache, e );
403         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
404
405         return LDAP_SUCCESS;
406
407 error_return:;
408         cache_return_entry_w( &li->li_cache, e );
409         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
410         rs->sr_text = NULL;
411         return rs->sr_err;
412 }