]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
46ca1025a9471f132ae9b6db966aba937af53af5
[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 SLAP_MOD_SOFTADD:
120 #ifdef NEW_LOGGING
121                         LDAP_LOG( BACK_LDBM, DETAIL1, 
122                                 "ldbm_modify_internal: softadd\n", 0, 0, 0 );
123 #else
124                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
125 #endif
126
127                         /* Avoid problems in index_add_mods()
128                          * We need to add index if necessary.
129                          */
130                         mod->sm_op = LDAP_MOD_ADD;
131
132                         rc = modify_add_values( e, mod, get_permissiveModify( op ),
133                                                 text, textbuf, textlen );
134                         if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
135                                 rc = LDAP_SUCCESS;
136                         }
137
138                         if( rc != LDAP_SUCCESS ) {
139 #ifdef NEW_LOGGING
140                                 LDAP_LOG( BACK_LDBM, INFO, 
141                                            "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
142 #else
143                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
144                                         rc, *text, 0);
145 #endif
146                         }
147                         break;
148
149                 default:
150 #ifdef NEW_LOGGING
151                         LDAP_LOG( BACK_LDBM, ERR, 
152                                 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
153 #else
154                         Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
155                                 mod->sm_op, 0, 0);
156 #endif
157
158                         rc = LDAP_OTHER;
159                         *text = "Invalid modify operation";
160 #ifdef NEW_LOGGING
161                         LDAP_LOG( BACK_LDBM, INFO, 
162                                 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
163 #else
164                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
165                                 rc, *text, 0);
166 #endif
167                 }
168
169                 if ( rc != LDAP_SUCCESS ) {
170                         goto exit;
171                 }
172
173                 /* If objectClass was modified, reset the flags */
174                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
175                         e->e_ocflags = 0;
176                 }
177
178                 /* check if modified attribute was indexed */
179                 rc = index_is_indexed( op->o_bd, mod->sm_desc );
180                 if ( rc == LDAP_SUCCESS ) {
181                         ap = attr_find( save_attrs, mod->sm_desc );
182                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
183
184                         ap = attr_find( e->e_attrs, mod->sm_desc );
185                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
186                 }
187         }
188
189         /* check that the entry still obeys the schema */
190 #ifndef LDAP_CACHING
191         rc = entry_schema_check( op->o_bd, e, save_attrs, text, textbuf, textlen );
192 #else /* LDAP_CACHING */
193         if ( !op->o_caching_on ) {
194                 rc = entry_schema_check( op->o_bd, e, save_attrs,
195                                 text, textbuf, textlen );
196         } else {
197                 rc = LDAP_SUCCESS; 
198         }
199 #endif /* LDAP_CACHING */
200
201         if ( rc != LDAP_SUCCESS ) {
202 #ifdef NEW_LOGGING
203                 LDAP_LOG( BACK_LDBM, ERR, 
204                         "ldbm_modify_internal: entry failed schema check: %s\n", 
205                         *text, 0, 0 );
206 #else
207                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
208                         *text, 0, 0 );
209 #endif
210
211                 goto exit;
212         }
213
214         /* check for abandon */
215         if ( op->o_abandon ) {
216                 rc = SLAPD_ABANDON;
217                 goto exit;
218         }
219
220         /* update the indices of the modified attributes */
221
222         /* start with deleting the old index entries */
223         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
224                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
225                         rc = index_values( op->o_bd, ap->a_desc,
226                                 ap->a_nvals,
227                                 e->e_id, SLAP_INDEX_DELETE_OP );
228                         if ( rc != LDAP_SUCCESS ) {
229 #ifdef NEW_LOGGING
230                                 LDAP_LOG( BACK_LDBM, ERR,
231                                         "ldbm_modify_internal: Attribute index delete failure\n",
232                                         0, 0, 0 );
233 #else
234                                 Debug( LDAP_DEBUG_ANY,
235                                        "Attribute index delete failure",
236                                        0, 0, 0 );
237 #endif
238                                 goto exit;
239                         }
240                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
241                 }
242         }
243
244         /* add the new index entries */
245         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
246                 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
247                         rc = index_values( op->o_bd, ap->a_desc,
248                                 ap->a_nvals,
249                                 e->e_id, SLAP_INDEX_ADD_OP );
250                         if ( rc != LDAP_SUCCESS ) {
251 #ifdef NEW_LOGGING
252                                 LDAP_LOG( BACK_LDBM, ERR,
253                                         "ldbm_modify_internal: Attribute index add failure\n",
254                                         0, 0, 0 );
255 #else
256                                 Debug( LDAP_DEBUG_ANY,
257                                        "Attribute index add failure",
258                                        0, 0, 0 );
259 #endif
260                                 goto exit;
261                         }
262                         ap->a_flags &= ~SLAP_ATTR_IXADD;
263                 }
264         }
265
266 exit:
267         if ( rc == LDAP_SUCCESS ) {
268                 attrs_free( save_attrs );
269         } else {
270                 for ( ap = save_attrs; ap; ap = ap->a_next ) {
271                         ap->a_flags = 0;
272                 }
273                 attrs_free( e->e_attrs );
274                 e->e_attrs = save_attrs;
275         }
276
277         return rc;
278 }
279
280 int
281 ldbm_back_modify(
282     Operation   *op,
283     SlapReply   *rs )
284 {
285         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
286         Entry           *matched;
287         Entry           *e;
288         int             manageDSAit = get_manageDSAit( op );
289         char textbuf[SLAP_TEXT_BUFLEN];
290         size_t textlen = sizeof textbuf;
291
292 #ifdef NEW_LOGGING
293         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
294 #else
295         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
296 #endif
297
298         /* grab giant lock for writing */
299         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
300
301         /* acquire and lock entry */
302         if ( (e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
303                 if ( matched != NULL ) {
304                         rs->sr_matched = ch_strdup( matched->e_dn );
305                         rs->sr_ref = is_entry_referral( matched )
306                                 ? get_entry_referrals( op, matched )
307                                 : NULL;
308                         cache_return_entry_r( &li->li_cache, matched );
309                 } else {
310                         rs->sr_ref = referral_rewrite( default_referral,
311                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
312                 }
313
314                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
315                 rs->sr_err = LDAP_REFERRAL;
316                 send_ldap_result( op, rs );
317
318                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
319                 free( (char *)rs->sr_matched );
320
321                 return( -1 );
322         }
323
324 #ifndef LDAP_CACHING
325         if ( !manageDSAit && is_entry_referral( e ) )
326 #else /* LDAP_CACHING */
327         if ( !op->o_caching_on && !manageDSAit && is_entry_referral( e ) )
328 #endif /* LDAP_CACHING */
329         {
330                 /* parent is a referral, don't allow add */
331                 /* parent is an alias, don't allow add */
332                 rs->sr_ref = get_entry_referrals( op, e );
333
334 #ifdef NEW_LOGGING
335                 LDAP_LOG( BACK_LDBM, INFO, 
336                            "ldbm_back_modify: entry (%s) is referral\n", op->o_req_ndn.bv_val, 0, 0 );
337 #else
338                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
339                     0, 0 );
340 #endif
341
342                 rs->sr_err = LDAP_REFERRAL;
343                 rs->sr_matched = e->e_name.bv_val;
344                 send_ldap_result( op, rs );
345
346                 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
347
348                 goto error_return;
349         }
350         
351         /* Modify the entry */
352         rs->sr_err = ldbm_modify_internal( op, op->oq_modify.rs_modlist, e,
353                 &rs->sr_text, textbuf, textlen );
354
355         if( rs->sr_err != LDAP_SUCCESS ) {
356                 if( rs->sr_err != SLAPD_ABANDON ) {
357                         send_ldap_result( op, rs );
358                 }
359
360                 goto error_return;
361         }
362
363         /* change the entry itself */
364         if ( id2entry_add( op->o_bd, e ) != 0 ) {
365                 send_ldap_error( op, rs, LDAP_OTHER,
366                         "id2entry failure" );
367                 goto error_return;
368         }
369
370         send_ldap_error( op, rs, LDAP_SUCCESS,
371                 NULL );
372
373         cache_return_entry_w( &li->li_cache, e );
374         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
375         return( 0 );
376
377 error_return:;
378         cache_return_entry_w( &li->li_cache, e );
379         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
380         return( -1 );
381 }