Back to index

openldap  2.4.31
add.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 
00024 #include "portable.h"
00025 
00026 #include <stdio.h>
00027 
00028 #include <ac/string.h>
00029 #include <ac/socket.h>
00030 
00031 #include "slap.h"
00032 #include "../back-ldap/back-ldap.h"
00033 #include "back-meta.h"
00034 
00035 int
00036 meta_back_add( Operation *op, SlapReply *rs )
00037 {
00038        metainfo_t    *mi = ( metainfo_t * )op->o_bd->be_private;
00039        metatarget_t  *mt;
00040        metaconn_t    *mc;
00041        int           i, candidate = -1;
00042        int           isupdate;
00043        Attribute     *a;
00044        LDAPMod              **attrs;
00045        struct berval mdn = BER_BVNULL, mapped;
00046        dncookie      dc;
00047        int           msgid;
00048        ldap_back_send_t     retrying = LDAP_BACK_RETRYING;
00049        LDAPControl   **ctrls = NULL;
00050 
00051        Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
00052                      op->o_req_dn.bv_val, 0, 0 );
00053 
00054        /*
00055         * get the current connection
00056         */
00057        mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
00058        if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
00059               return rs->sr_err;
00060        }
00061 
00062        assert( mc->mc_conns[ candidate ].msc_ld != NULL );
00063 
00064        /*
00065         * Rewrite the add dn, if needed
00066         */
00067        mt = mi->mi_targets[ candidate ];
00068        dc.target = mt;
00069        dc.conn = op->o_conn;
00070        dc.rs = rs;
00071        dc.ctx = "addDN";
00072 
00073        if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
00074               send_ldap_result( op, rs );
00075               goto done;
00076        }
00077 
00078        /* Count number of attributes in entry ( +1 ) */
00079        for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
00080        
00081        /* Create array of LDAPMods for ldap_add() */
00082        attrs = ch_malloc( sizeof( LDAPMod * )*i );
00083 
00084        dc.ctx = "addAttrDN";
00085        isupdate = be_shadow_update( op );
00086        for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
00087               int                  j, is_oc = 0;
00088 
00089               if ( !isupdate && !get_relax( op ) && a->a_desc->ad_type->sat_no_user_mod  )
00090               {
00091                      continue;
00092               }
00093 
00094               if ( a->a_desc == slap_schema.si_ad_objectClass 
00095                             || a->a_desc == slap_schema.si_ad_structuralObjectClass )
00096               {
00097                      is_oc = 1;
00098                      mapped = a->a_desc->ad_cname;
00099 
00100               } else {
00101                      ldap_back_map( &mt->mt_rwmap.rwm_at,
00102                                    &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
00103                      if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
00104                             continue;
00105                      }
00106               }
00107 
00108               attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
00109               if ( attrs[ i ] == NULL ) {
00110                      continue;
00111               }
00112               attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
00113               attrs[ i ]->mod_type = mapped.bv_val;
00114 
00115               if ( is_oc ) {
00116                      for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
00117                             ;
00118 
00119                      attrs[ i ]->mod_bvalues =
00120                             (struct berval **)ch_malloc( ( j + 1 ) *
00121                             sizeof( struct berval * ) );
00122 
00123                      for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
00124                             struct ldapmapping   *mapping;
00125 
00126                             ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
00127                                           &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
00128 
00129                             if ( mapping == NULL ) {
00130                                    if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
00131                                           continue;
00132                                    }
00133                                    attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
00134 
00135                             } else {
00136                                    attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
00137                             }
00138                             j++;
00139                      }
00140                      attrs[ i ]->mod_bvalues[ j ] = NULL;
00141 
00142               } else {
00143                      /*
00144                       * FIXME: dn-valued attrs should be rewritten
00145                       * to allow their use in ACLs at the back-ldap
00146                       * level.
00147                       */
00148                      if ( a->a_desc->ad_type->sat_syntax ==
00149                             slap_schema.si_syn_distinguishedName )
00150                      {
00151                             (void)ldap_dnattr_rewrite( &dc, a->a_vals );
00152                             if ( a->a_vals == NULL ) {
00153                                    continue;
00154                             }
00155                      }
00156 
00157                      for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
00158                             ;
00159                      
00160                      attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
00161                      for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
00162                             attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
00163                      }
00164                      attrs[ i ]->mod_bvalues[ j ] = NULL;
00165               }
00166               i++;
00167        }
00168        attrs[ i ] = NULL;
00169 
00170 retry:;
00171        ctrls = op->o_ctrls;
00172        if ( meta_back_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
00173        {
00174               send_ldap_result( op, rs );
00175               goto cleanup;
00176        }
00177 
00178        rs->sr_err = ldap_add_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
00179                            attrs, ctrls, NULL, &msgid );
00180        rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
00181               mt->mt_timeout[ SLAP_OP_ADD ], ( LDAP_BACK_SENDRESULT | retrying ) );
00182        if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
00183               retrying &= ~LDAP_BACK_RETRYING;
00184               if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
00185                      /* if the identity changed, there might be need to re-authz */
00186                      (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00187                      goto retry;
00188               }
00189        }
00190 
00191 cleanup:;
00192        (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00193 
00194        for ( --i; i >= 0; --i ) {
00195               free( attrs[ i ]->mod_bvalues );
00196               free( attrs[ i ] );
00197        }
00198        free( attrs );
00199        if ( mdn.bv_val != op->ora_e->e_dn ) {
00200               free( mdn.bv_val );
00201               BER_BVZERO( &mdn );
00202        }
00203 
00204 done:;
00205        if ( mc ) {
00206               meta_back_release_conn( mi, mc );
00207        }
00208 
00209        return rs->sr_err;
00210 }
00211