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