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