]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
First round of imports from HEAD
[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     Backend     *be,
27     Connection  *conn,
28     Operation   *op,
29     const char  *dn,
30     Modifications       *modlist,
31     Entry       *e,
32         const char **text,
33         char *textbuf,
34         size_t textlen
35 )
36 {
37         int rc = LDAP_SUCCESS;
38         Modification    *mod;
39         Modifications   *ml;
40         Attribute       *save_attrs;
41         Attribute       *ap;
42
43 #ifdef NEW_LOGGING
44         LDAP_LOG( BACK_LDBM, ENTRY,  "ldbm_modify_internal: %s\n", dn, 0, 0 );
45 #else
46         Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal: %s\n", dn, 0, 0);
47 #endif
48
49
50         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
51                 return LDAP_INSUFFICIENT_ACCESS;
52         }
53
54         save_attrs = e->e_attrs;
55         e->e_attrs = attrs_dup( e->e_attrs );
56
57         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
58                 mod = &ml->sml_mod;
59
60                 switch ( mod->sm_op ) {
61                 case LDAP_MOD_ADD:
62 #ifdef NEW_LOGGING
63                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: add\n", 0, 0, 0);
64 #else
65                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
66 #endif
67
68                         rc = modify_add_values( e, mod, op->o_permitmodify, text, textbuf, textlen );
69                         if( rc != LDAP_SUCCESS ) {
70 #ifdef NEW_LOGGING
71                                 LDAP_LOG( BACK_LDBM, INFO, 
72                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
73 #else
74                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
75                                         rc, *text, 0);
76 #endif
77                         }
78                         break;
79
80                 case LDAP_MOD_DELETE:
81 #ifdef NEW_LOGGING
82                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal: delete\n", 0,0,0);
83 #else
84                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
85 #endif
86
87                         rc = modify_delete_values( e, mod, op->o_permitmodify, text, textbuf, textlen );
88                         assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
89                         if( rc != LDAP_SUCCESS ) {
90 #ifdef NEW_LOGGING
91                                 LDAP_LOG( BACK_LDBM, INFO, 
92                                         "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
93 #else
94                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
95                                         rc, *text, 0);
96 #endif
97                         }
98                         break;
99
100                 case LDAP_MOD_REPLACE:
101 #ifdef NEW_LOGGING
102                         LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_modify_internal:  replace\n",0,0,0);
103 #else
104                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
105 #endif
106
107                         rc = modify_replace_values( e, mod, op->o_permitmodify, 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, op->o_permitmodify, text, textbuf, textlen );
133                         if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
134                                 rc = LDAP_SUCCESS;
135                         }
136
137                         if( rc != LDAP_SUCCESS ) {
138 #ifdef NEW_LOGGING
139                                 LDAP_LOG( BACK_LDBM, INFO, 
140                                            "ldbm_modify_internal: failed %d (%s)\n", rc, *text, 0 );
141 #else
142                                 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
143                                         rc, *text, 0);
144 #endif
145                         }
146                         break;
147
148                 default:
149 #ifdef NEW_LOGGING
150                         LDAP_LOG( BACK_LDBM, ERR, 
151                                 "ldbm_modify_internal: invalid op %d\n", mod->sm_op, 0, 0 );
152 #else
153                         Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
154                                 mod->sm_op, 0, 0);
155 #endif
156
157                         rc = LDAP_OTHER;
158                         *text = "Invalid modify operation";
159 #ifdef NEW_LOGGING
160                         LDAP_LOG( BACK_LDBM, INFO, 
161                                 "ldbm_modify_internal: %d (%s)\n", rc, *text, 0 );
162 #else
163                         Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
164                                 rc, *text, 0);
165 #endif
166                 }
167
168                 if ( rc != LDAP_SUCCESS ) {
169                         goto exit;
170                 }
171
172                 /* If objectClass was modified, reset the flags */
173                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
174                         e->e_ocflags = 0;
175                 }
176
177                 /* check if modified attribute was indexed */
178                 rc = index_is_indexed( be, mod->sm_desc );
179                 if ( rc == LDAP_SUCCESS ) {
180                         ap = attr_find( save_attrs, mod->sm_desc );
181                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
182
183                         ap = attr_find( e->e_attrs, mod->sm_desc );
184                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
185                 }
186         }
187
188         /* check that the entry still obeys the schema */
189         rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
190         if ( rc != LDAP_SUCCESS ) {
191 #ifdef NEW_LOGGING
192                 LDAP_LOG( BACK_LDBM, ERR, 
193                         "ldbm_modify_internal: entry failed schema check: %s\n", 
194                         *text, 0, 0 );
195 #else
196                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
197                         *text, 0, 0 );
198 #endif
199
200                 goto exit;
201         }
202
203         /* check for abandon */
204         if ( op->o_abandon ) {
205                 rc = SLAPD_ABANDON;
206                 goto exit;
207         }
208
209         /* update the indices of the modified attributes */
210
211         /* start with deleting the old index entries */
212         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
213                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
214                         rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
215                                            SLAP_INDEX_DELETE_OP );
216                         if ( rc != LDAP_SUCCESS ) {
217 #ifdef NEW_LOGGING
218                                 LDAP_LOG( BACK_LDBM, ERR,
219                                         "ldbm_modify_internal: Attribute index delete failure\n",
220                                         0, 0, 0 );
221 #else
222                                 Debug( LDAP_DEBUG_ANY,
223                                        "Attribute index delete failure",
224                                        0, 0, 0 );
225 #endif
226                                 goto exit;
227                         }
228                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
229                 }
230         }
231
232         /* add the new index entries */
233         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
234                 if ( ap->a_flags & SLAP_ATTR_IXADD ) {
235                         rc = index_values( be, ap->a_desc, ap->a_vals, e->e_id,
236                                            SLAP_INDEX_ADD_OP );
237                         if ( rc != LDAP_SUCCESS ) {
238 #ifdef NEW_LOGGING
239                                 LDAP_LOG( BACK_LDBM, ERR,
240                                         "ldbm_modify_internal: Attribute index add failure\n",
241                                         0, 0, 0 );
242 #else
243                                 Debug( LDAP_DEBUG_ANY,
244                                        "Attribute index add failure",
245                                        0, 0, 0 );
246 #endif
247                                 goto exit;
248                         }
249                         ap->a_flags &= ~SLAP_ATTR_IXADD;
250                 }
251         }
252
253 exit:
254         if ( rc == LDAP_SUCCESS ) {
255                 attrs_free( save_attrs );
256         } else {
257                 for ( ap = save_attrs; ap; ap = ap->a_next ) {
258                         ap->a_flags = 0;
259                 }
260                 attrs_free( e->e_attrs );
261                 e->e_attrs = save_attrs;
262         }
263
264         return rc;
265 }
266
267 int
268 ldbm_back_modify(
269     Backend     *be,
270     Connection  *conn,
271     Operation   *op,
272     struct berval       *dn,
273     struct berval       *ndn,
274     Modifications       *modlist
275 )
276 {
277         int rc;
278         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
279         Entry           *matched;
280         Entry           *e;
281         int             manageDSAit = get_manageDSAit( op );
282         const char *text = NULL;
283         char textbuf[SLAP_TEXT_BUFLEN];
284         size_t textlen = sizeof textbuf;
285
286 #ifdef NEW_LOGGING
287         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_modify: enter\n", 0, 0, 0);
288 #else
289         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
290 #endif
291
292         /* grab giant lock for writing */
293         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
294
295         /* acquire and lock entry */
296         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
297                 char* matched_dn = NULL;
298                 BerVarray refs;
299
300                 if ( matched != NULL ) {
301                         matched_dn = ch_strdup( matched->e_dn );
302                         refs = is_entry_referral( matched )
303                                 ? get_entry_referrals( be, conn, op, matched )
304                                 : NULL;
305                         cache_return_entry_r( &li->li_cache, matched );
306                 } else {
307                         refs = referral_rewrite( default_referral,
308                                 NULL, dn, LDAP_SCOPE_DEFAULT );
309                 }
310
311                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
312                 send_ldap_result( conn, op, LDAP_REFERRAL,
313                         matched_dn, NULL, refs, NULL );
314
315                 if ( refs ) ber_bvarray_free( refs );
316                 free( matched_dn );
317
318                 return( -1 );
319         }
320
321     if ( !manageDSAit && is_entry_referral( e ) ) {
322                 /* parent is a referral, don't allow add */
323                 /* parent is an alias, don't allow add */
324                 BerVarray refs = get_entry_referrals( be,
325                         conn, op, e );
326
327 #ifdef NEW_LOGGING
328                 LDAP_LOG( BACK_LDBM, INFO, 
329                            "ldbm_back_modify: entry (%s) is referral\n", ndn->bv_val, 0, 0 );
330 #else
331                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
332                     0, 0 );
333 #endif
334
335
336                 send_ldap_result( conn, op, LDAP_REFERRAL,
337                     e->e_dn, NULL, refs, NULL );
338
339                 if ( refs ) ber_bvarray_free( refs );
340
341                 goto error_return;
342         }
343         
344         /* Modify the entry */
345         rc = ldbm_modify_internal( be, conn, op, ndn->bv_val, modlist, e,
346                 &text, textbuf, textlen );
347
348         if( rc != LDAP_SUCCESS ) {
349                 if( rc != SLAPD_ABANDON ) {
350                         send_ldap_result( conn, op, rc,
351                                 NULL, text, NULL, NULL );
352                 }
353
354                 goto error_return;
355         }
356
357         /* change the entry itself */
358         if ( id2entry_add( be, e ) != 0 ) {
359                 send_ldap_result( conn, op, LDAP_OTHER,
360                         NULL, "id2entry failure", NULL, NULL );
361                 goto error_return;
362         }
363
364         send_ldap_result( conn, op, LDAP_SUCCESS,
365                 NULL, NULL, NULL, NULL );
366
367         cache_return_entry_w( &li->li_cache, e );
368         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
369         return( 0 );
370
371 error_return:;
372         cache_return_entry_w( &li->li_cache, e );
373         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
374         return( -1 );
375 }