1 /* add.c - ldap backend add function */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 /* This is an altered version */
9 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
11 * Permission is granted to anyone to use this software for any purpose
12 * on any computer system, and to alter it and redistribute it, subject
13 * to the following restrictions:
15 * 1. The author is not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
18 * 2. The origin of this software must not be misrepresented, either by
19 * explicit claim or by omission. Since few users ever read sources,
20 * credits should appear in the documentation.
22 * 3. Altered versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software. Since few users
24 * ever read sources, credits should appear in the documentation.
26 * 4. This notice may not be removed or altered.
30 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
32 * This software is being modified by Pierangelo Masarati.
33 * The previously reported conditions apply to the modified code as well.
34 * Changes in the original code are highlighted where required.
35 * Credits for the original code go to the author, Howard Chu.
42 #include <ac/string.h>
43 #include <ac/socket.h>
46 #include "back-ldap.h"
53 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
59 struct berval mdn = { 0, NULL };
64 LDAP_LOG( BACK_LDAP, ENTRY, "ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
65 #else /* !NEW_LOGGING */
66 Debug(LDAP_DEBUG_ARGS, "==> ldap_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
67 #endif /* !NEW_LOGGING */
69 lc = ldap_back_getconn(op, rs);
70 if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
75 * Rewrite the add dn, if needed
77 dc.rwmap = &li->rwmap;
86 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
87 send_ldap_result( op, rs );
91 /* Count number of attributes in entry */
92 for (i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next)
95 /* Create array of LDAPMods for ldap_add() */
96 attrs = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
101 for (i=0, a=op->oq_add.rs_e->e_attrs; a; a=a->a_next) {
102 if ( a->a_desc->ad_type->sat_no_user_mod ) {
106 ldap_back_map(&li->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
108 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
112 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
113 if (attrs[i] == NULL) {
117 attrs[i]->mod_op = LDAP_MOD_BVALUES;
118 attrs[i]->mod_type = mapped.bv_val;
120 if ( a->a_desc->ad_type->sat_syntax ==
121 slap_schema.si_syn_distinguishedName ) {
123 * FIXME: rewrite could fail; in this case
124 * the operation should give up, right?
126 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
129 for (j=0; a->a_vals[j].bv_val; j++);
130 attrs[i]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
131 for (j=0; a->a_vals[j].bv_val; j++)
132 attrs[i]->mod_vals.modv_bvals[j] = &a->a_vals[j];
133 attrs[i]->mod_vals.modv_bvals[j] = NULL;
138 rs->sr_err = ldap_add_ext(lc->ld, mdn.bv_val, attrs,
139 op->o_ctrls, NULL, &msgid);
140 for (--i; i>= 0; --i) {
141 ch_free(attrs[i]->mod_vals.modv_bvals);
145 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
149 return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;
161 for ( last = 0; a_vals[last].bv_val != NULL; last++ );
164 for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
165 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
167 case LDAP_OTHER: /* ? */
169 /* leave attr untouched if massage failed */
170 if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
171 ch_free( a_vals[i].bv_val );
176 case LDAP_UNWILLING_TO_PERFORM:
178 * FIXME: need to check if it may be considered
179 * legal to trim values when adding/modifying;
180 * it should be when searching (see ACLs).
182 ch_free( a_vals[i].bv_val );
184 a_vals[i] = a_vals[last];
186 a_vals[last].bv_len = 0;
187 a_vals[last].bv_val = NULL;