]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/modify.c
Make dbEnv a private toy of back-bdb2.
[openldap] / servers / slapd / back-bdb2 / modify.c
1 /* modify.c - bdb2 backend modify routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-bdb2.h"
12 #include "proto-back-bdb2.h"
13
14
15 static int
16 bdb2i_back_modify_internal(
17     BackendDB   *be,
18     Connection  *conn,
19     Operation   *op,
20     char        *dn,
21     LDAPModList *modlist
22 )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25         char            *matched;
26         LDAPModList     *ml;
27         Entry           *e;
28         int             i, err;
29
30         Debug(LDAP_DEBUG_ARGS, "bdb2i_back_modify:\n", 0, 0, 0);
31
32         if ( (e = bdb2i_dn2entry_w( be, dn, &matched )) == NULL ) {
33                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
34                     NULL );
35                 if ( matched != NULL ) {
36                         free( matched );
37                 }
38                 return( -1 );
39         }
40
41         if ( (err = acl_check_modlist( be, conn, op, e, modlist )) != LDAP_SUCCESS ) {
42                 send_ldap_result( conn, op, err, NULL, NULL );
43                 goto error_return;
44         }
45
46         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
47                 LDAPMod *mod = &ml->ml_mod;
48
49                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
50                 case LDAP_MOD_ADD:
51                         err = bdb2i_add_values( e, mod, op->o_ndn );
52                         break;
53
54                 case LDAP_MOD_DELETE:
55                         err = bdb2i_delete_values( e, mod, op->o_ndn );
56                         break;
57
58                 case LDAP_MOD_REPLACE:
59                         err = bdb2i_replace_values( e, mod, op->o_ndn );
60                         break;
61                 }
62
63                 if ( err != LDAP_SUCCESS ) {
64                         /* unlock entry, delete from cache */
65                         send_ldap_result( conn, op, err, NULL, NULL );
66                         goto error_return;
67                 }
68         }
69
70         /* check that the entry still obeys the schema */
71         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
72                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
73                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
74                 goto error_return;
75         }
76
77         /* check for abandon */
78         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
79         if ( op->o_abandon ) {
80                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
81                 goto error_return;
82         }
83         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
84
85         /* modify indexes */
86         if ( bdb2i_index_add_mods( be, modlist, e->e_id ) != 0 ) {
87                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
88                 goto error_return;
89         }
90
91         /* check for abandon */
92         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
93         if ( op->o_abandon ) {
94                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
95                 goto error_return;
96         }
97         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
98
99         /* change the entry itself */
100         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
101                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
102                 goto error_return;
103         }
104
105         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
106         bdb2i_cache_return_entry_w( &li->li_cache, e );
107         return( 0 );
108
109 error_return:;
110         bdb2i_cache_return_entry_w( &li->li_cache, e );
111         return( -1 );
112 }
113
114
115 int
116 bdb2_back_modify(
117     BackendDB   *be,
118     Connection  *conn,
119     Operation   *op,
120     char        *dn,
121     LDAPModList *modlist
122 )
123 {
124         DB_LOCK         lock;
125         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
126         struct timeval  time1;
127         int             ret;
128
129         bdb2i_start_timing( be->bd_info, &time1 );
130
131         if ( bdb2i_enter_backend_w( &lock ) != 0 ) {
132
133                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
134                 return( -1 );
135
136         }
137
138         /*  check, if a new default attribute index will be created,
139                 in which case we have to open the index file BEFORE TP  */
140         switch ( slapMode ) {
141                 case SLAP_SERVER_MODE:
142                 case SLAP_TIMEDSERVER_MODE:
143                 case SLAP_TOOL_MODE:
144                 case SLAP_TOOLID_MODE:
145                         bdb2i_check_default_attr_index_mod( li, modlist );
146                         break;
147         }
148
149          ret = bdb2i_back_modify_internal( be, conn, op, dn, modlist );
150         (void) bdb2i_leave_backend_w( lock );
151         bdb2i_stop_timing( be->bd_info, time1, "MOD", conn, op );
152
153         return( ret );
154 }
155
156
157 int
158 bdb2i_add_values(
159     Entry       *e,
160     LDAPMod     *mod,
161     char        *dn
162 )
163 {
164         int             i;
165         Attribute       *a;
166
167         /* check if the values we're adding already exist */
168         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
169                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
170                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
171                             a->a_syntax, 3 ) == 0 ) {
172                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
173                         }
174                 }
175         }
176
177         /* no - add them */
178         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
179                 return( LDAP_CONSTRAINT_VIOLATION );
180         }
181
182         return( LDAP_SUCCESS );
183 }
184
185 int
186 bdb2i_delete_values(
187     Entry       *e,
188     LDAPMod     *mod,
189     char        *dn
190 )
191 {
192         int             i, j, k, found;
193         Attribute       *a;
194
195         /* delete the entire attribute */
196         if ( mod->mod_bvalues == NULL ) {
197                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
198                     mod->mod_type, 0, 0 );
199                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
200                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
201         }
202
203         /* delete specific values - find the attribute first */
204         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
205                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
206                     mod->mod_type, 0, 0 );
207                 return( LDAP_NO_SUCH_ATTRIBUTE );
208         }
209
210         /* find each value to delete */
211         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
212                 found = 0;
213                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
214                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
215                             a->a_syntax, 3 ) != 0 ) {
216                                 continue;
217                         }
218                         found = 1;
219
220                         /* found a matching value - delete it */
221                         ber_bvfree( a->a_vals[j] );
222                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
223                                 a->a_vals[k - 1] = a->a_vals[k];
224                         }
225                         a->a_vals[k - 1] = NULL;
226                         break;
227                 }
228
229                 /* looked through them all w/o finding it */
230                 if ( ! found ) {
231                         Debug( LDAP_DEBUG_ARGS,
232                             "could not find value for attr %s\n",
233                             mod->mod_type, 0, 0 );
234                         return( LDAP_NO_SUCH_ATTRIBUTE );
235                 }
236         }
237
238         return( LDAP_SUCCESS );
239 }
240
241 int
242 bdb2i_replace_values(
243     Entry       *e,
244     LDAPMod     *mod,
245     char        *dn
246 )
247 {
248         (void) attr_delete( &e->e_attrs, mod->mod_type );
249
250         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
251                 return( LDAP_CONSTRAINT_VIOLATION );
252         }
253
254         return( LDAP_SUCCESS );
255 }