]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
c98662ee7b070a0b0e34b4ddd9fba35f3f6f7061
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm backend modify routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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 static int add_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
21 static int delete_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
22 static int replace_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
23
24 /* We need this function because of LDAP modrdn. If we do not 
25  * add this there would be a bunch of code replication here 
26  * and there and of course the likelihood of bugs increases.
27  * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
28  */ 
29
30 int ldbm_modify_internal(
31     Backend     *be,
32     Connection  *conn,
33     Operation   *op,
34     char        *dn,
35     Modifications       *modlist,
36     Entry       *e 
37 )
38 {
39         int rc, err;
40         char *text;
41         Modification    *mod;
42         Modifications   *ml;
43         Attribute       *save_attrs;
44
45         if ( !acl_check_modlist( be, conn, op, e, modlist )) {
46                 return LDAP_INSUFFICIENT_ACCESS;
47         }
48
49         save_attrs = e->e_attrs;
50         e->e_attrs = attrs_dup( e->e_attrs );
51
52         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
53                 mod = &ml->sml_mod;
54
55 #ifdef SLAPD_SCHEMA_NOT_COMPAT
56                 switch ( mod->sm_op )
57 #else
58                 switch ( mod->mod_op )
59 #endif
60                 {
61                 case LDAP_MOD_ADD:
62                         err = add_values( e, mod, op->o_ndn );
63                         break;
64
65                 case LDAP_MOD_DELETE:
66                         err = delete_values( e, mod, op->o_ndn );
67                         break;
68
69                 case LDAP_MOD_REPLACE:
70                         err = replace_values( e, mod, op->o_ndn );
71                         break;
72
73                 case SLAP_MOD_SOFTADD:
74                         /* Avoid problems in index_add_mods()
75                          * We need to add index if necessary.
76                          */
77 #ifdef SLAPD_SCHEMA_NOT_COMPAT
78                         mod->sm_op = LDAP_MOD_ADD;
79 #else
80                         mod->mod_op = LDAP_MOD_ADD;
81 #endif
82                         err = add_values( e, mod, op->o_ndn );
83
84                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
85                                 err = LDAP_SUCCESS;
86                         }
87                         break;
88                 }
89
90                 if ( err != LDAP_SUCCESS ) {
91                         attrs_free( e->e_attrs );
92                         e->e_attrs = save_attrs;
93                         /* unlock entry, delete from cache */
94                         return err; 
95                 }
96         }
97
98         /* check for abandon */
99         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
100         if ( op->o_abandon ) {
101                 attrs_free( e->e_attrs );
102                 e->e_attrs = save_attrs;
103                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
104                 return SLAPD_ABANDON;
105         }
106         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
107
108         /* check that the entry still obeys the schema */
109         rc = entry_schema_check( e, save_attrs, &text );
110         if ( rc != LDAP_SUCCESS ) {
111                 attrs_free( e->e_attrs );
112                 e->e_attrs = save_attrs;
113                 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
114                         text, 0, 0 );
115                 return rc;
116         }
117
118         /* check for abandon */
119         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
120         if ( op->o_abandon ) {
121                 attrs_free( e->e_attrs );
122                 e->e_attrs = save_attrs;
123                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
124                 return SLAPD_ABANDON;
125         }
126         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
127
128         /* run through the attributes removing old indices */
129         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
130                 mod = &ml->sml_mod;
131
132 #ifdef SLAPD_SCHEMA_NOT_COMPAT
133                 switch ( mod->sm_op )
134 #else
135                 switch ( mod->mod_op )
136 #endif
137                 {
138                 case LDAP_MOD_REPLACE: {
139 #ifdef SLAPD_SCHEMA_NOT_COMPAT
140                         /* not yet implemented */
141 #else
142                         /* Need to remove all values from indexes */
143                         Attribute *a = save_attrs
144                                 ? attr_find( save_attrs, mod->mod_type )
145                                 : NULL;
146
147                         if( a != NULL ) {
148                                 (void) index_change_values( be,
149                                         mod->mod_type,
150                                         a->a_vals,
151                                         e->e_id,
152                                         SLAP_INDEX_DELETE_OP );
153                         }
154 #endif
155                         } break;
156
157                 case LDAP_MOD_DELETE:
158 #ifdef SLAPD_SCHEMA_NOT_COMPAT
159                         /* not yet implemented */
160 #else
161                         /* remove deleted values */
162                         (void) index_change_values( be,
163                                 mod->mod_type,
164                                 mod->mod_bvalues,
165                                 e->e_id,
166                                 SLAP_INDEX_DELETE_OP );
167 #endif
168                         break;
169                 }
170         }
171
172         attrs_free( save_attrs );
173
174         /* run through the attributes adding new indices */
175         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
176                 mod = &ml->sml_mod;
177
178                 switch ( mod->mod_op ) {
179                 case LDAP_MOD_REPLACE:
180                 case LDAP_MOD_ADD:
181 #ifdef SLAPD_SCHEMA_NOT_COMPAT
182                         /* not yet implemented */
183 #else
184                         (void) index_change_values( be,
185                                 mod->mod_type,
186                                 mod->mod_bvalues,
187                                 e->e_id,
188                                 SLAP_INDEX_ADD_OP );
189 #endif
190                         break;
191
192                 case LDAP_MOD_DELETE: {
193 #ifdef SLAPD_SCHEMA_NOT_COMPAT
194                         /* not yet implemented */
195 #else
196                         /* Need to add all remaining values */
197                         Attribute *a = e->e_attrs
198                                 ? attr_find( e->e_attrs, mod->mod_type )
199                                 : NULL;
200 #endif
201
202                         if( a != NULL ) {
203                                 (void) index_change_values( be,
204                                         mod->mod_type,
205                                         a->a_vals,
206                                         e->e_id,
207                                         SLAP_INDEX_ADD_OP );
208                         }
209                         } break;
210                 }
211         }
212
213         return LDAP_SUCCESS;
214 }
215
216
217 int
218 ldbm_back_modify(
219     Backend     *be,
220     Connection  *conn,
221     Operation   *op,
222     char        *dn,
223     char        *ndn,
224     Modifications       *modlist
225 )
226 {
227         int rc;
228         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
229         Entry           *matched;
230         Entry           *e;
231         int             manageDSAit = get_manageDSAit( op );
232
233         Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
234
235         /* acquire and lock entry */
236         if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
237                 char* matched_dn = NULL;
238                 struct berval **refs = NULL;
239
240                 if ( matched != NULL ) {
241                         matched_dn = ch_strdup( matched->e_dn );
242                         refs = is_entry_referral( matched )
243                                 ? get_entry_referrals( be, conn, op, matched )
244                                 : NULL;
245                         cache_return_entry_r( &li->li_cache, matched );
246                 } else {
247                         refs = default_referral;
248                 }
249
250                 send_ldap_result( conn, op, LDAP_REFERRAL,
251                         matched_dn, NULL, refs, NULL );
252
253                 if ( matched != NULL ) {
254                         ber_bvecfree( refs );
255                         free( matched_dn );
256                 }
257
258                 return( -1 );
259         }
260
261     if ( !manageDSAit && is_entry_referral( e ) ) {
262                 /* parent is a referral, don't allow add */
263                 /* parent is an alias, don't allow add */
264                 struct berval **refs = get_entry_referrals( be,
265                         conn, op, e );
266
267                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
268                     0, 0 );
269
270                 send_ldap_result( conn, op, LDAP_REFERRAL,
271                     e->e_dn, NULL, refs, NULL );
272
273                 ber_bvecfree( refs );
274
275                 goto error_return;
276         }
277         
278         /* Modify the entry */
279         rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e );
280
281         if( rc != LDAP_SUCCESS ) {
282                 if( rc != SLAPD_ABANDON ) {
283                         send_ldap_result( conn, op, rc,
284                                 NULL, NULL, NULL, NULL );
285                 }
286
287                 goto error_return;
288         }
289
290         /* change the entry itself */
291         if ( id2entry_add( be, e ) != 0 ) {
292                 send_ldap_result( conn, op, LDAP_OTHER,
293                         NULL, NULL, NULL, NULL );
294                 goto error_return;
295         }
296
297         send_ldap_result( conn, op, LDAP_SUCCESS,
298                 NULL, NULL, NULL, NULL );
299
300         cache_return_entry_w( &li->li_cache, e );
301         return( 0 );
302
303 error_return:;
304         cache_return_entry_w( &li->li_cache, e );
305         return( -1 );
306 }
307
308 static int
309 add_values(
310     Entry       *e,
311     Modification        *mod,
312     char        *dn
313 )
314 {
315 #ifdef SLAPD_SCHEMA_NOT_COMPAT
316         /* not yet implemented */
317 #else
318         int             i;
319         Attribute       *a;
320
321         /* check if the values we're adding already exist */
322         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
323                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
324                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
325                             a->a_syntax, 3 ) == 0 ) {
326                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
327                         }
328                 }
329         }
330
331         /* no - add them */
332         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
333                 return( LDAP_CONSTRAINT_VIOLATION );
334         }
335 #endif
336
337         return( LDAP_SUCCESS );
338 }
339
340 static int
341 delete_values(
342     Entry       *e,
343     Modification        *mod,
344     char        *dn
345 )
346 {
347 #ifdef SLAPD_SCHEMA_NOT_COMPAT
348         /* not yet implemented */
349 #else
350         int             i, j, k, found;
351         Attribute       *a;
352
353         /* delete the entire attribute */
354         if ( mod->mod_bvalues == NULL ) {
355                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
356                     mod->mod_type, 0, 0 );
357                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
358                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
359         }
360
361         /* delete specific values - find the attribute first */
362         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
363                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
364                     mod->mod_type, 0, 0 );
365                 return( LDAP_NO_SUCH_ATTRIBUTE );
366         }
367
368         /* find each value to delete */
369         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
370                 found = 0;
371                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
372                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
373                             a->a_syntax, 3 ) != 0 ) {
374                                 continue;
375                         }
376                         found = 1;
377
378                         /* found a matching value - delete it */
379                         ber_bvfree( a->a_vals[j] );
380                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
381                                 a->a_vals[k - 1] = a->a_vals[k];
382                         }
383                         a->a_vals[k - 1] = NULL;
384
385                         /* delete the entire attribute, if no values remain */
386                         if ( a->a_vals[0] == NULL) {
387                                 Debug( LDAP_DEBUG_ARGS,
388                                         "removing entire attribute %s\n",
389                                         mod->mod_type, 0, 0 );
390                                 if ( attr_delete( &e->e_attrs, mod->mod_type ) ) {
391                                         return LDAP_NO_SUCH_ATTRIBUTE;
392                                 }
393                         }
394
395                         break;
396                 }
397
398                 /* looked through them all w/o finding it */
399                 if ( ! found ) {
400                         Debug( LDAP_DEBUG_ARGS,
401                             "could not find value for attr %s\n",
402                             mod->mod_type, 0, 0 );
403                         return LDAP_NO_SUCH_ATTRIBUTE;
404                 }
405         }
406 #endif
407
408         return( LDAP_SUCCESS );
409 }
410
411 static int
412 replace_values(
413     Entry       *e,
414     Modification        *mod,
415     char        *dn
416 )
417 {
418 #ifdef SLAPD_SCHEMA_NOT_COMPAT
419         /* not yet implemented */
420 #else
421         (void) attr_delete( &e->e_attrs, mod->mod_type );
422
423         if ( mod->mod_bvalues != NULL &&
424                 attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 )
425         {
426                 return( LDAP_CONSTRAINT_VIOLATION );
427         }
428 #endif
429
430         return( LDAP_SUCCESS );
431 }