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