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