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