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