2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2009 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/string.h>
28 #include <ac/socket.h>
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
35 meta_back_modify( Operation *op, SlapReply *rs )
37 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
41 LDAPMod **modv = NULL;
44 int candidate = -1, i;
46 struct berval mdn = BER_BVNULL;
50 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
51 LDAPControl **ctrls = NULL;
53 mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
54 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
58 assert( mc->mc_conns[ candidate ].msc_ld != NULL );
61 * Rewrite the modify dn, if needed
63 mt = mi->mi_targets[ candidate ];
69 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
70 send_ldap_result( op, rs );
74 for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
77 mods = ch_malloc( sizeof( LDAPMod )*i );
79 rs->sr_err = LDAP_OTHER;
80 send_ldap_result( op, rs );
83 modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
85 rs->sr_err = LDAP_OTHER;
86 send_ldap_result( op, rs );
90 dc.ctx = "modifyAttrDN";
91 isupdate = be_shadow_update( op );
92 for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
95 if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
100 if ( ml->sml_desc == slap_schema.si_ad_objectClass
101 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
104 mapped = ml->sml_desc->ad_cname;
107 ldap_back_map( &mt->mt_rwmap.rwm_at,
108 &ml->sml_desc->ad_cname, &mapped,
110 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
115 modv[ i ] = &mods[ i ];
116 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
117 mods[ i ].mod_type = mapped.bv_val;
120 * FIXME: dn-valued attrs should be rewritten
121 * to allow their use in ACLs at the back-ldap
124 if ( ml->sml_values != NULL ) {
126 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
128 mods[ i ].mod_bvalues =
129 (struct berval **)ch_malloc( ( j + 1 ) *
130 sizeof( struct berval * ) );
131 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
132 struct ldapmapping *mapping;
134 ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
135 &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
137 if ( mapping == NULL ) {
138 if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
141 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
144 mods[ i ].mod_bvalues[ j ] = &mapping->dst;
148 mods[ i ].mod_bvalues[ j ] = NULL;
151 if ( ml->sml_desc->ad_type->sat_syntax ==
152 slap_schema.si_syn_distinguishedName )
154 ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
155 if ( ml->sml_values == NULL ) {
160 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
162 mods[ i ].mod_bvalues =
163 (struct berval **)ch_malloc( ( j + 1 ) *
164 sizeof( struct berval * ) );
165 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
166 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
168 mods[ i ].mod_bvalues[ j ] = NULL;
172 mods[ i ].mod_bvalues = NULL;
181 rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
182 if ( rc != LDAP_SUCCESS ) {
183 send_ldap_result( op, rs );
187 rs->sr_err = ldap_modify_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
188 modv, ctrls, NULL, &msgid );
189 rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
190 mt->mt_timeout[ SLAP_OP_MODIFY ], ( LDAP_BACK_SENDRESULT | retrying ) );
191 if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
192 retrying &= ~LDAP_BACK_RETRYING;
193 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
194 /* if the identity changed, there might be need to re-authz */
195 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
201 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
203 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
207 if ( modv != NULL ) {
208 for ( i = 0; modv[ i ]; i++ ) {
209 free( modv[ i ]->mod_bvalues );
216 meta_back_release_conn( mi, mc );