1 /* modify.c - ldbm backend modify routine */
6 #include <sys/socket.h>
10 extern int global_schemacheck;
11 extern Entry *dn2entry();
12 extern Attribute *attr_find();
14 static int add_values();
15 static int delete_values();
16 static int replace_values();
27 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
33 if ( (e = dn2entry( be, dn, &matched )) == NULL ) {
34 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT, matched,
36 if ( matched != NULL ) {
43 if ( (err = acl_check_mods( be, conn, op, e, mods )) != LDAP_SUCCESS ) {
44 send_ldap_result( conn, op, err, NULL, NULL );
45 cache_return_entry( &li->li_cache, e );
49 for ( mod = mods; mod != NULL; mod = mod->mod_next ) {
50 switch ( mod->mod_op & ~LDAP_MOD_BVALUES ) {
52 err = add_values( e, mod, op->o_dn );
56 err = delete_values( e, mod, op->o_dn );
59 case LDAP_MOD_REPLACE:
60 err = replace_values( e, mod, op->o_dn );
64 if ( err != LDAP_SUCCESS ) {
65 /* unlock entry, delete from cache */
66 send_ldap_result( conn, op, err, NULL, NULL );
67 cache_return_entry( &li->li_cache, e );
72 /* check for abandon */
73 pthread_mutex_lock( &op->o_abandonmutex );
74 if ( op->o_abandon ) {
75 pthread_mutex_unlock( &op->o_abandonmutex );
76 cache_return_entry( &li->li_cache, e );
79 pthread_mutex_unlock( &op->o_abandonmutex );
81 /* check that the entry still obeys the schema */
82 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
83 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, NULL,
85 cache_return_entry( &li->li_cache, e );
90 if ( index_add_mods( be, mods, e->e_id ) != 0 ) {
91 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
92 cache_return_entry( &li->li_cache, e );
96 /* check for abandon */
97 pthread_mutex_lock( &op->o_abandonmutex );
98 if ( op->o_abandon ) {
99 pthread_mutex_unlock( &op->o_abandonmutex );
100 cache_return_entry( &li->li_cache, e );
103 pthread_mutex_unlock( &op->o_abandonmutex );
105 /* change the entry itself */
106 if ( id2entry_add( be, e ) != 0 ) {
107 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, NULL, NULL );
108 cache_return_entry( &li->li_cache, e );
112 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
113 cache_return_entry( &li->li_cache, e );
128 /* check if the values we're adding already exist */
129 if ( (a = attr_find( e->e_attrs, mod->mod_type )) != NULL ) {
130 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
131 if ( value_find( a->a_vals, mod->mod_bvalues[i],
132 a->a_syntax, 3 ) == 0 ) {
133 return( LDAP_TYPE_OR_VALUE_EXISTS );
139 if( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
140 return( LDAP_CONSTRAINT_VIOLATION );
143 return( LDAP_SUCCESS );
156 /* delete the entire attribute */
157 if ( mod->mod_bvalues == NULL ) {
158 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
159 mod->mod_type, 0, 0 );
160 return( attr_delete( &e->e_attrs, mod->mod_type ) ?
161 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
164 /* delete specific values - find the attribute first */
165 if ( (a = attr_find( e->e_attrs, mod->mod_type )) == NULL ) {
166 Debug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
167 mod->mod_type, 0, 0 );
168 return( LDAP_NO_SUCH_ATTRIBUTE );
171 /* find each value to delete */
172 for ( i = 0; mod->mod_bvalues[i] != NULL; i++ ) {
174 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
175 if ( value_cmp( mod->mod_bvalues[i], a->a_vals[j],
176 a->a_syntax, 3 ) != 0 ) {
181 /* found a matching value - delete it */
182 ber_bvfree( a->a_vals[j] );
183 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
184 a->a_vals[k - 1] = a->a_vals[k];
186 a->a_vals[k - 1] = NULL;
190 /* looked through them all w/o finding it */
192 Debug( LDAP_DEBUG_ARGS,
193 "could not find value for attr %s\n",
194 mod->mod_type, 0, 0 );
195 return( LDAP_NO_SUCH_ATTRIBUTE );
199 return( LDAP_SUCCESS );
209 (void) attr_delete( &e->e_attrs, mod->mod_type );
211 if ( attr_merge( e, mod->mod_type, mod->mod_bvalues ) != 0 ) {
212 return( LDAP_CONSTRAINT_VIOLATION );
215 return( LDAP_SUCCESS );