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