]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/modify.c
During cleanup always unlock root_mutex if rootlock is true
[openldap] / servers / slapd / back-ldbm / modify.c
1 /* modify.c - ldbm 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-ldbm.h"
12 #include "proto-back-ldbm.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 int
19 ldbm_back_modify(
20     Backend     *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, "ldbm_back_modify:\n", 0, 0, 0);
34
35         /* acquire and lock entry */
36         if ( (e = dn2entry_w( be, dn, &matched )) == NULL ) {
37                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
38                     NULL );
39                 if ( matched != NULL ) {
40                         free( matched );
41                 }
42                 return( -1 );
43         }
44
45         if ( (err = acl_check_modlist( be, conn, op, e, modlist )) != LDAP_SUCCESS ) {
46                 send_ldap_result( conn, op, err, NULL, NULL );
47                 goto error_return;
48         }
49
50         for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
51                 LDAPMod *mod = &ml->ml_mod;
52
53                 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
54                 case LDAP_MOD_ADD:
55                         err = add_values( e, mod, op->o_ndn );
56                         break;
57
58                 case LDAP_MOD_DELETE:
59                         err = delete_values( e, mod, op->o_ndn );
60                         break;
61
62                 case LDAP_MOD_REPLACE:
63                         err = replace_values( e, mod, op->o_ndn );
64                         break;
65                 }
66
67                 if ( err != LDAP_SUCCESS ) {
68                         /* unlock entry, delete from cache */
69                         send_ldap_result( conn, op, err, NULL, NULL );
70                         goto error_return;
71                 }
72         }
73
74         /* check that the entry still obeys the schema */
75         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
76                 Debug( LDAP_DEBUG_ANY, "entry failed schema check\n", 0, 0, 0 );
77                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL, NULL );
78                 goto error_return;
79         }
80
81         /* check for abandon */
82         pthread_mutex_lock( &op->o_abandonmutex );
83         if ( op->o_abandon ) {
84                 pthread_mutex_unlock( &op->o_abandonmutex );
85                 goto error_return;
86         }
87         pthread_mutex_unlock( &op->o_abandonmutex );
88
89         /* modify indexes */
90         if ( index_add_mods( be, modlist, e->e_id ) != 0 ) {
91                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
92                 goto error_return;
93         }
94
95         /* check for abandon */
96         pthread_mutex_lock( &op->o_abandonmutex );
97         if ( op->o_abandon ) {
98                 pthread_mutex_unlock( &op->o_abandonmutex );
99                 goto error_return;
100         }
101         pthread_mutex_unlock( &op->o_abandonmutex );
102
103         /* change the entry itself */
104         if ( id2entry_add( be, e ) != 0 ) {
105                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
106                 goto error_return;
107         }
108
109         send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
110         cache_return_entry_w( &li->li_cache, e );
111         return( 0 );
112
113 error_return:;
114         cache_return_entry_w( &li->li_cache, e );
115         return( -1 );
116 }
117
118 static int
119 add_values(
120     Entry       *e,
121     LDAPMod     *mod,
122     char        *dn
123 )
124 {
125         int             i;
126         Attribute       *a;
127
128         /* check if the values we're adding already exist */
129         if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
130                 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
131                         if ( value_find( a->a_vals, mod->mod_bvalues[i],
132                             a->a_syntax, 3 ) == 0 ) {
133                                 return( LDAP_TYPE_OR_VALUE_EXISTS );
134                         }
135                 }
136         }
137
138         /* no - add them */
139         if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
140                 return( LDAP_CONSTRAINT_VIOLATION );
141         }
142
143         return( LDAP_SUCCESS );
144 }
145
146 static int
147 delete_values(
148     Entry       *e,
149     LDAPMod     *mod,
150     char        *dn
151 )
152 {
153         int             i, j, k, found;
154         Attribute       *a;
155
156         /* delete the entire attribute */
157         if ( mod->mod_bvalues == NULL ) {
158                 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
159                     mod->mod_type, 0, 0 );
160                 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
161                     LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
162         }
163
164         /* delete specific values - find the attribute first */
165         if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
166                 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
167                     mod->mod_type, 0, 0 );
168                 return( LDAP_NO_SUCH_ATTRIBUTE );
169         }
170
171         /* find each value to delete */
172         for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
173                 found = 0;
174                 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
175                         if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
176                             a->a_syntax, 3 ) != 0 ) {
177                                 continue;
178                         }
179                         found = 1;
180
181                         /* found a matching value - delete it */
182                         ber_bvfree( a->a_vals[j] );
183                         for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
184                                 a->a_vals[k - 1] = a->a_vals[k];
185                         }
186                         a->a_vals[k - 1] = NULL;
187                         break;
188                 }
189
190                 /* looked through them all w/o finding it */
191                 if ( ! found ) {
192                         Debug( LDAP_DEBUG_ARGS,
193                             "could not find value for attr %s\n",
194                             mod->mod_type, 0, 0 );
195                         return( LDAP_NO_SUCH_ATTRIBUTE );
196                 }
197         }
198
199         return( LDAP_SUCCESS );
200 }
201
202 static int
203 replace_values(
204     Entry       *e,
205     LDAPMod     *mod,
206     char        *dn
207 )
208 {
209         (void) attr_delete( &e->e_attrs, mod->mod_type );
210
211         if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
212                 return( LDAP_CONSTRAINT_VIOLATION );
213         }
214
215         return( LDAP_SUCCESS );
216 }