1 /* add.c - ldap backend add function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2000-2003 Pierangelo Masarati.
7 * Portions Copyright 1999-2003 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "back-ldap.h"
39 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
45 struct berval mdn = BER_BVNULL;
49 #ifdef LDAP_BACK_PROXY_AUTHZ
50 LDAPControl **ctrls = NULL;
51 int rc = LDAP_SUCCESS;
52 #endif /* LDAP_BACK_PROXY_AUTHZ */
55 LDAP_LOG( BACK_LDAP, ENTRY, "ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
56 #else /* !NEW_LOGGING */
57 Debug(LDAP_DEBUG_ARGS, "==> ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
58 #endif /* !NEW_LOGGING */
60 lc = ldap_back_getconn(op, rs);
61 if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
66 * Rewrite the add dn, if needed
68 dc.rwmap = &li->rwmap;
77 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
78 send_ldap_result( op, rs );
82 /* Count number of attributes in entry */
83 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 = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
93 isupdate = be_isupdate( op ) || syncrepl_isupdate( op );
94 for (i=0, a=op->oq_add.rs_e->e_attrs; a; a=a->a_next) {
95 if ( !isupdate && a->a_desc->ad_type->sat_no_user_mod ) {
99 ldap_back_map(&li->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
101 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
105 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
106 if (attrs[i] == NULL) {
110 attrs[i]->mod_op = LDAP_MOD_BVALUES;
111 attrs[i]->mod_type = mapped.bv_val;
113 if ( a->a_desc->ad_type->sat_syntax ==
114 slap_schema.si_syn_distinguishedName ) {
116 * FIXME: rewrite could fail; in this case
117 * the operation should give up, right?
119 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
122 for (j=0; a->a_vals[j].bv_val; j++);
123 attrs[i]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
124 for (j=0; a->a_vals[j].bv_val; j++)
125 attrs[i]->mod_vals.modv_bvals[j] = &a->a_vals[j];
126 attrs[i]->mod_vals.modv_bvals[j] = NULL;
131 #ifdef LDAP_BACK_PROXY_AUTHZ
132 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
133 if ( rc != LDAP_SUCCESS ) {
136 #endif /* LDAP_BACK_PROXY_AUTHZ */
138 rs->sr_err = ldap_add_ext(lc->ld, mdn.bv_val, attrs,
139 #ifdef LDAP_BACK_PROXY_AUTHZ
141 #else /* ! LDAP_BACK_PROXY_AUTHZ */
143 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
146 #ifdef LDAP_BACK_PROXY_AUTHZ
148 if ( ctrls && ctrls != op->o_ctrls ) {
152 #endif /* LDAP_BACK_PROXY_AUTHZ */
154 for (--i; i>= 0; --i) {
155 ch_free(attrs[i]->mod_vals.modv_bvals);
159 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
162 #ifdef LDAP_BACK_PROXY_AUTHZ
163 if ( rc != LDAP_SUCCESS ) {
164 send_ldap_result( op, rs );
167 #endif /* LDAP_BACK_PROXY_AUTHZ */
168 return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;