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 = { 0, NULL };
48 #ifdef LDAP_BACK_PROXY_AUTHZ
49 LDAPControl **ctrls = NULL;
50 int rc = LDAP_SUCCESS;
51 #endif /* LDAP_BACK_PROXY_AUTHZ */
54 LDAP_LOG( BACK_LDAP, ENTRY, "ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
55 #else /* !NEW_LOGGING */
56 Debug(LDAP_DEBUG_ARGS, "==> ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
57 #endif /* !NEW_LOGGING */
59 lc = ldap_back_getconn(op, rs);
60 if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
65 * Rewrite the add dn, if needed
67 dc.rwmap = &li->rwmap;
76 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
77 send_ldap_result( op, rs );
81 /* Count number of attributes in entry */
82 for (i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next)
85 /* Create array of LDAPMods for ldap_add() */
86 attrs = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
91 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->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
98 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
102 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
103 if (attrs[i] == NULL) {
107 attrs[i]->mod_op = LDAP_MOD_BVALUES;
108 attrs[i]->mod_type = mapped.bv_val;
110 if ( a->a_desc->ad_type->sat_syntax ==
111 slap_schema.si_syn_distinguishedName ) {
113 * FIXME: rewrite could fail; in this case
114 * the operation should give up, right?
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];
123 attrs[i]->mod_vals.modv_bvals[j] = NULL;
128 #ifdef LDAP_BACK_PROXY_AUTHZ
129 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
130 if ( rc != LDAP_SUCCESS ) {
133 #endif /* LDAP_BACK_PROXY_AUTHZ */
135 rs->sr_err = ldap_add_ext(lc->ld, mdn.bv_val, attrs,
136 #ifdef LDAP_BACK_PROXY_AUTHZ
138 #else /* ! LDAP_BACK_PROXY_AUTHZ */
140 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
143 #ifdef LDAP_BACK_PROXY_AUTHZ
145 if ( ctrls && ctrls != op->o_ctrls ) {
149 #endif /* LDAP_BACK_PROXY_AUTHZ */
151 for (--i; i>= 0; --i) {
152 ch_free(attrs[i]->mod_vals.modv_bvals);
156 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
159 #ifdef LDAP_BACK_PROXY_AUTHZ
160 if ( rc != LDAP_SUCCESS ) {
161 send_ldap_result( op, rs );
164 #endif /* LDAP_BACK_PROXY_AUTHZ */
165 return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;