2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 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;
40 int i, candidate = -1;
44 struct berval mdn = BER_BVNULL, mapped;
50 Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
51 op->o_req_dn.bv_val, 0, 0 );
54 * get the current connection
56 mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
57 if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
61 assert( mc->mc_conns[ candidate ].msc_ld != NULL );
64 * Rewrite the add dn, if needed
66 dc.target = mi->mi_targets[ candidate ];
71 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
72 send_ldap_result( op, rs );
76 /* Count number of attributes in entry ( +1 ) */
77 for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
79 /* Create array of LDAPMods for ldap_add() */
80 attrs = ch_malloc( sizeof( LDAPMod * )*i );
83 isupdate = be_shadow_update( op );
84 for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
87 if ( !isupdate && !get_manageDIT( op ) && a->a_desc->ad_type->sat_no_user_mod )
92 if ( a->a_desc == slap_schema.si_ad_objectClass
93 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
96 mapped = a->a_desc->ad_cname;
99 ldap_back_map( &mi->mi_targets[ candidate ]->mt_rwmap.rwm_at,
100 &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
101 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
106 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
107 if ( attrs[ i ] == NULL ) {
110 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
111 attrs[ i ]->mod_type = mapped.bv_val;
114 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
117 attrs[ i ]->mod_bvalues =
118 (struct berval **)ch_malloc( ( j + 1 ) *
119 sizeof( struct berval * ) );
121 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
122 struct ldapmapping *mapping;
124 ldap_back_mapping( &mi->mi_targets[ candidate ]->mt_rwmap.rwm_oc,
125 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
127 if ( mapping == NULL ) {
128 if ( mi->mi_targets[ candidate ]->mt_rwmap.rwm_oc.drop_missing ) {
131 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
134 attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
138 attrs[ i ]->mod_bvalues[ j ] = NULL;
142 * FIXME: dn-valued attrs should be rewritten
143 * to allow their use in ACLs at the back-ldap
146 if ( a->a_desc->ad_type->sat_syntax ==
147 slap_schema.si_syn_distinguishedName )
149 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
150 if ( a->a_vals == NULL ) {
155 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
158 attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
159 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
160 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
162 attrs[ i ]->mod_bvalues[ j ] = NULL;
169 rs->sr_err = ldap_add_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
170 attrs, op->o_ctrls, NULL, &msgid );
171 if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
173 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
178 } else if ( rs->sr_err == LDAP_SUCCESS ) {
179 struct timeval tv, *tvp = NULL;
180 LDAPMessage *res = NULL;
183 if ( mi->mi_targets[ candidate ]->mt_timeout[ LDAP_BACK_OP_ADD ] != 0 ) {
184 tv.tv_sec = mi->mi_targets[ candidate ]->mt_timeout[ LDAP_BACK_OP_ADD ];
189 rs->sr_err = LDAP_OTHER;
191 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
192 msgid, LDAP_MSG_ALL, tvp, &res );
198 ldap_abandon_ext( mc->mc_conns[ candidate ].msc_ld,
200 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
201 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
205 rc = ldap_parse_result( mc->mc_conns[ candidate ].msc_ld,
206 res, &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
207 if ( rc != LDAP_SUCCESS ) {
220 rs->sr_err = meta_back_op_result( mc, op, rs, candidate );
223 send_ldap_result( op, rs );
227 for ( --i; i >= 0; --i ) {
228 free( attrs[ i ]->mod_bvalues );
232 if ( mdn.bv_val != op->ora_e->e_dn ) {
239 meta_back_release_conn( op, mc );