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