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