Back to index

openldap  2.4.31
modify.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1999-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2001-2003 Pierangelo Masarati.
00006  * Portions Copyright 1999-2003 Howard Chu.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by the Howard Chu for inclusion
00019  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00020  * Masarati.
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #include <stdio.h>
00026 
00027 #include <ac/string.h>
00028 #include <ac/socket.h>
00029 
00030 #include "slap.h"
00031 #include "../back-ldap/back-ldap.h"
00032 #include "back-meta.h"
00033 
00034 int
00035 meta_back_modify( Operation *op, SlapReply *rs )
00036 {
00037        metainfo_t    *mi = ( metainfo_t * )op->o_bd->be_private;
00038        metatarget_t  *mt;
00039        metaconn_t    *mc;
00040        int           rc = 0;
00041        LDAPMod              **modv = NULL;
00042        LDAPMod              *mods = NULL;
00043        Modifications *ml;
00044        int           candidate = -1, i;
00045        int           isupdate;
00046        struct berval mdn = BER_BVNULL;
00047        struct berval mapped;
00048        dncookie      dc;
00049        int           msgid;
00050        ldap_back_send_t     retrying = LDAP_BACK_RETRYING;
00051        LDAPControl   **ctrls = NULL;
00052 
00053        mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
00054        if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
00055               return rs->sr_err;
00056        }
00057 
00058        assert( mc->mc_conns[ candidate ].msc_ld != NULL );
00059 
00060        /*
00061         * Rewrite the modify dn, if needed
00062         */
00063        mt = mi->mi_targets[ candidate ];
00064        dc.target = mt;
00065        dc.conn = op->o_conn;
00066        dc.rs = rs;
00067        dc.ctx = "modifyDN";
00068 
00069        if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
00070               send_ldap_result( op, rs );
00071               goto cleanup;
00072        }
00073 
00074        for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
00075               ;
00076 
00077        mods = ch_malloc( sizeof( LDAPMod )*i );
00078        if ( mods == NULL ) {
00079               rs->sr_err = LDAP_OTHER;
00080               send_ldap_result( op, rs );
00081               goto cleanup;
00082        }
00083        modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
00084        if ( modv == NULL ) {
00085               rs->sr_err = LDAP_OTHER;
00086               send_ldap_result( op, rs );
00087               goto cleanup;
00088        }
00089 
00090        dc.ctx = "modifyAttrDN";
00091        isupdate = be_shadow_update( op );
00092        for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
00093               int    j, is_oc = 0;
00094 
00095               if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
00096               {
00097                      continue;
00098               }
00099 
00100               if ( ml->sml_desc == slap_schema.si_ad_objectClass 
00101                             || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
00102               {
00103                      is_oc = 1;
00104                      mapped = ml->sml_desc->ad_cname;
00105 
00106               } else {
00107                      ldap_back_map( &mt->mt_rwmap.rwm_at,
00108                                    &ml->sml_desc->ad_cname, &mapped,
00109                                    BACKLDAP_MAP );
00110                      if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
00111                             continue;
00112                      }
00113               }
00114 
00115               modv[ i ] = &mods[ i ];
00116               mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
00117               mods[ i ].mod_type = mapped.bv_val;
00118 
00119               /*
00120                * FIXME: dn-valued attrs should be rewritten
00121                * to allow their use in ACLs at the back-ldap
00122                * level.
00123                */
00124               if ( ml->sml_values != NULL ) {
00125                      if ( is_oc ) {
00126                             for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
00127                                    ;
00128                             mods[ i ].mod_bvalues =
00129                                    (struct berval **)ch_malloc( ( j + 1 ) *
00130                                    sizeof( struct berval * ) );
00131                             for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
00132                                    struct ldapmapping   *mapping;
00133 
00134                                    ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
00135                                                  &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
00136 
00137                                    if ( mapping == NULL ) {
00138                                           if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
00139                                                  continue;
00140                                           }
00141                                           mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
00142 
00143                                    } else {
00144                                           mods[ i ].mod_bvalues[ j ] = &mapping->dst;
00145                                    }
00146                                    j++;
00147                             }
00148                             mods[ i ].mod_bvalues[ j ] = NULL;
00149 
00150                      } else {
00151                             if ( ml->sml_desc->ad_type->sat_syntax ==
00152                                           slap_schema.si_syn_distinguishedName )
00153                             {
00154                                    ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
00155                                    if ( ml->sml_values == NULL ) {
00156                                           continue;
00157                                    }
00158                             }
00159 
00160                             for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
00161                                    ;
00162                             mods[ i ].mod_bvalues =
00163                                    (struct berval **)ch_malloc( ( j + 1 ) *
00164                                    sizeof( struct berval * ) );
00165                             for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
00166                                    mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
00167                             }
00168                             mods[ i ].mod_bvalues[ j ] = NULL;
00169                      }
00170 
00171               } else {
00172                      mods[ i ].mod_bvalues = NULL;
00173               }
00174 
00175               i++;
00176        }
00177        modv[ i ] = 0;
00178 
00179 retry:;
00180        ctrls = op->o_ctrls;
00181        rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
00182        if ( rc != LDAP_SUCCESS ) {
00183               send_ldap_result( op, rs );
00184               goto cleanup;
00185        }
00186 
00187        rs->sr_err = ldap_modify_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
00188                      modv, ctrls, NULL, &msgid );
00189        rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
00190               mt->mt_timeout[ SLAP_OP_MODIFY ], ( LDAP_BACK_SENDRESULT | retrying ) );
00191        if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
00192               retrying &= ~LDAP_BACK_RETRYING;
00193               if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
00194                      /* if the identity changed, there might be need to re-authz */
00195                      (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00196                      goto retry;
00197               }
00198        }
00199 
00200 cleanup:;
00201        (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00202 
00203        if ( mdn.bv_val != op->o_req_dn.bv_val ) {
00204               free( mdn.bv_val );
00205               BER_BVZERO( &mdn );
00206        }
00207        if ( modv != NULL ) {
00208               for ( i = 0; modv[ i ]; i++ ) {
00209                      free( modv[ i ]->mod_bvalues );
00210               }
00211        }
00212        free( mods );
00213        free( modv );
00214 
00215        if ( mc ) {
00216               meta_back_release_conn( mi, mc );
00217        }
00218 
00219        return rs->sr_err;
00220 }
00221