]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/add.c
Free IDL_CACHE locks
[openldap] / servers / slapd / back-ldap / add.c
1 /* add.c - ldap backend add function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
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:
14  * 
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.
17  * 
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.
21  * 
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.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  *
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.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/string.h>
43 #include <ac/socket.h>
44
45 #include "slap.h"
46 #include "back-ldap.h"
47
48 int
49 ldap_back_add(
50     Operation   *op,
51     SlapReply   *rs )
52 {
53         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
54         struct ldapconn *lc;
55         int i, j;
56         Attribute *a;
57         LDAPMod **attrs;
58         struct berval mapped;
59         struct berval mdn = { 0, NULL };
60         ber_int_t msgid;
61         dncookie dc;
62
63 #ifdef NEW_LOGGING
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 */
68         
69         lc = ldap_back_getconn(op, rs);
70         if ( !lc || !ldap_back_dobind( lc, op, rs ) ) {
71                 return( -1 );
72         }
73
74         /*
75          * Rewrite the add dn, if needed
76          */
77         dc.rwmap = &li->rwmap;
78 #ifdef ENABLE_REWRITE
79         dc.conn = op->o_conn;
80         dc.rs = rs;
81         dc.ctx = "addDn";
82 #else
83         dc.tofrom = 1;
84         dc.normalized = 0;
85 #endif
86         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
87                 send_ldap_result( op, rs );
88                 return -1;
89         }
90
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)
93                 ;
94         
95         /* Create array of LDAPMods for ldap_add() */
96         attrs = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
97
98 #ifdef ENABLE_REWRITE
99         dc.ctx = "addDnAttr";
100 #endif
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  ) {
103                         continue;
104                 }
105
106                 ldap_back_map(&li->rwmap.rwm_at, &a->a_desc->ad_cname, &mapped,
107                                 BACKLDAP_MAP);
108                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
109                         continue;
110                 }
111
112                 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
113                 if (attrs[i] == NULL) {
114                         continue;
115                 }
116
117                 attrs[i]->mod_op = LDAP_MOD_BVALUES;
118                 attrs[i]->mod_type = mapped.bv_val;
119
120                 if ( a->a_desc->ad_type->sat_syntax ==
121                         slap_schema.si_syn_distinguishedName ) {
122                         /*
123                          * FIXME: rewrite could fail; in this case
124                          * the operation should give up, right?
125                          */
126                         (void)ldap_dnattr_rewrite( &dc, a->a_vals );
127                 }
128
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;
134                 i++;
135         }
136         attrs[i] = NULL;
137
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);
142                 ch_free(attrs[i]);
143         }
144         ch_free(attrs);
145         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
146                 free( mdn.bv_val );
147         }
148         
149         return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;
150 }
151