2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
40 int i, candidate = -1;
43 struct berval mdn = { 0, NULL }, mapped;
47 LDAP_LOG( BACK_META, ENTRY, "meta_back_add: %s\n",
48 op->o_req_dn.bv_val, 0, 0 );
49 #else /* !NEW_LOGGING */
50 Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
51 op->o_req_dn.bv_val, 0, 0 );
52 #endif /* !NEW_LOGGING */
55 * get the current connection
57 lc = meta_back_getconn( op, rs, META_OP_REQUIRE_SINGLE,
58 &op->o_req_ndn, &candidate );
60 send_ldap_result( op, rs );
63 if ( !meta_back_dobind( lc, op )
64 || !meta_back_is_valid( lc, candidate ) ) {
65 rs->sr_err = LDAP_OTHER;
66 send_ldap_result( op, rs );
71 * Rewrite the add dn, if needed
73 dc.rwmap = &li->targets[ candidate ]->rwmap;
78 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
79 send_ldap_result( op, rs );
83 /* Count number of attributes in entry */
84 for ( i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next );
86 /* Create array of LDAPMods for ldap_add() */
87 attrs = ch_malloc( sizeof( LDAPMod * )*i );
89 for ( i = 0, a = op->oq_add.rs_e->e_attrs; a; a = a->a_next ) {
92 if ( a->a_desc->ad_type->sat_no_user_mod ) {
96 ldap_back_map( &li->targets[ candidate ]->rwmap.rwm_at,
97 &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
98 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
102 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
103 if ( attrs[ i ] == NULL ) {
106 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
107 attrs[ i ]->mod_type = mapped.bv_val;
110 * FIXME: dn-valued attrs should be rewritten
111 * to allow their use in ACLs at the back-ldap
114 if ( strcmp( a->a_desc->ad_type->sat_syntax->ssyn_oid,
115 SLAPD_DN_SYNTAX ) == 0 ) {
116 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
119 for ( j = 0; a->a_vals[ j ].bv_val; j++ );
120 attrs[ i ]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
121 for ( j = 0; a->a_vals[ j ].bv_val; j++ ) {
122 attrs[ i ]->mod_vals.modv_bvals[ j ] = &a->a_vals[ j ];
124 attrs[ i ]->mod_vals.modv_bvals[ j ] = NULL;
129 ldap_add_s( lc->conns[ candidate ].ld, mdn.bv_val, attrs );
130 for ( --i; i >= 0; --i ) {
131 free( attrs[ i ]->mod_vals.modv_bvals );
135 if ( mdn.bv_val != op->oq_add.rs_e->e_dn ) {
138 return meta_back_op_result( lc, op, rs );