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
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "../back-ldap/back-ldap.h"
33 #include "back-meta.h"
36 meta_back_add( Operation *op, SlapReply *rs )
38 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
41 int i, candidate = -1;
45 struct berval mdn = BER_BVNULL, mapped;
48 ldap_back_send_t retrying = LDAP_BACK_RETRYING;
49 LDAPControl **ctrls = NULL;
51 Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
52 op->o_req_dn.bv_val, 0, 0 );
55 * get the current connection
57 mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
58 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
62 assert( mc->mc_conns[ candidate ].msc_ld != NULL );
65 * Rewrite the add dn, if needed
67 mt = mi->mi_targets[ candidate ];
73 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
74 send_ldap_result( op, rs );
78 /* Count number of attributes in entry ( +1 ) */
79 for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
81 /* Create array of LDAPMods for ldap_add() */
82 attrs = ch_malloc( sizeof( LDAPMod * )*i );
85 isupdate = be_shadow_update( op );
86 for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
89 if ( !isupdate && !get_relax( op ) && a->a_desc->ad_type->sat_no_user_mod )
94 if ( a->a_desc == slap_schema.si_ad_objectClass
95 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
98 mapped = a->a_desc->ad_cname;
101 ldap_back_map( &mt->mt_rwmap.rwm_at,
102 &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
103 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
108 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
109 if ( attrs[ i ] == NULL ) {
112 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
113 attrs[ i ]->mod_type = mapped.bv_val;
116 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
119 attrs[ i ]->mod_bvalues =
120 (struct berval **)ch_malloc( ( j + 1 ) *
121 sizeof( struct berval * ) );
123 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
124 struct ldapmapping *mapping;
126 ldap_back_mapping( &mt->mt_rwmap.rwm_oc,
127 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
129 if ( mapping == NULL ) {
130 if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
133 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
136 attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
140 attrs[ i ]->mod_bvalues[ j ] = NULL;
144 * FIXME: dn-valued attrs should be rewritten
145 * to allow their use in ACLs at the back-ldap
148 if ( a->a_desc->ad_type->sat_syntax ==
149 slap_schema.si_syn_distinguishedName )
151 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
152 if ( a->a_vals == NULL ) {
157 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
160 attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
161 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
162 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
164 attrs[ i ]->mod_bvalues[ j ] = NULL;
172 if ( meta_back_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
174 send_ldap_result( op, rs );
178 rs->sr_err = ldap_add_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
179 attrs, ctrls, NULL, &msgid );
180 rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
181 mt->mt_timeout[ SLAP_OP_ADD ], ( LDAP_BACK_SENDRESULT | retrying ) );
182 if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
183 retrying &= ~LDAP_BACK_RETRYING;
184 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
185 /* if the identity changed, there might be need to re-authz */
186 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
192 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
194 for ( --i; i >= 0; --i ) {
195 free( attrs[ i ]->mod_bvalues );
199 if ( mdn.bv_val != op->ora_e->e_dn ) {
206 meta_back_release_conn( mi, mc );