]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/add.c
a9be70d466d6f08e405ab218903670a3f08bdaa6
[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 #ifdef ENABLE_REWRITE
78         dc.rw = li->rwinfo;
79         dc.conn = op->o_conn;
80         dc.rs = rs;
81         dc.ctx = "addDn";
82 #else
83         dc.li = li;
84         dc.tofrom = 1;
85         dc.normalized = 0;
86 #endif
87         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
88                 send_ldap_result( op, rs );
89                 return -1;
90         }
91
92         /* Count number of attributes in entry */
93         for (i = 1, a = op->oq_add.rs_e->e_attrs; a; i++, a = a->a_next)
94                 ;
95         
96         /* Create array of LDAPMods for ldap_add() */
97         attrs = (LDAPMod **)ch_malloc(sizeof(LDAPMod *)*i);
98
99 #ifdef ENABLE_REWRITE
100         dc.ctx = "addDnAttr";
101 #endif
102         for (i=0, a=op->oq_add.rs_e->e_attrs; a; a=a->a_next) {
103                 if ( a->a_desc->ad_type->sat_no_user_mod  ) {
104                         continue;
105                 }
106
107                 ldap_back_map(&li->at_map, &a->a_desc->ad_cname, &mapped,
108                                 BACKLDAP_MAP);
109                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
110                         continue;
111                 }
112
113                 attrs[i] = (LDAPMod *)ch_malloc(sizeof(LDAPMod));
114                 if (attrs[i] == NULL) {
115                         continue;
116                 }
117
118                 attrs[i]->mod_op = LDAP_MOD_BVALUES;
119                 attrs[i]->mod_type = mapped.bv_val;
120
121                 if ( a->a_desc->ad_type->sat_syntax ==
122                         slap_schema.si_syn_distinguishedName ) {
123                         /*
124                          * FIXME: rewrite could fail; in this case
125                          * the operation should give up, right?
126                          */
127                         (void)ldap_dnattr_rewrite( &dc, a->a_vals );
128                 }
129
130                 for (j=0; a->a_vals[j].bv_val; j++);
131                 attrs[i]->mod_vals.modv_bvals = ch_malloc((j+1)*sizeof(struct berval *));
132                 for (j=0; a->a_vals[j].bv_val; j++)
133                         attrs[i]->mod_vals.modv_bvals[j] = &a->a_vals[j];
134                 attrs[i]->mod_vals.modv_bvals[j] = NULL;
135                 i++;
136         }
137         attrs[i] = NULL;
138
139         rs->sr_err = ldap_add_ext(lc->ld, mdn.bv_val, attrs,
140                         op->o_ctrls, NULL, &msgid);
141         for (--i; i>= 0; --i) {
142                 ch_free(attrs[i]->mod_vals.modv_bvals);
143                 ch_free(attrs[i]);
144         }
145         ch_free(attrs);
146         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
147                 free( mdn.bv_val );
148         }
149         
150         return ldap_back_op_result( lc, op, rs, msgid, 1 ) != LDAP_SUCCESS;
151 }
152
153 int
154 ldap_dnattr_rewrite(
155         dncookie                *dc,
156         BerVarray               a_vals
157 )
158 {
159         struct berval   bv;
160         int             i, last;
161
162         for ( last = 0; a_vals[last].bv_val != NULL; last++ );
163         last--;
164
165         for ( i = 0; a_vals[i].bv_val != NULL; i++ ) {
166                 switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) {
167                 case LDAP_SUCCESS:
168                 case LDAP_OTHER:        /* ? */
169                 default:                /* ??? */
170                         /* leave attr untouched if massage failed */
171                         if ( bv.bv_val && bv.bv_val != a_vals[i].bv_val ) {
172                                 ch_free( a_vals[i].bv_val );
173                                 a_vals[i] = bv;
174                         }
175                         break;
176
177                 case LDAP_UNWILLING_TO_PERFORM:
178                         /*
179                          * FIXME: need to check if it may be considered 
180                          * legal to trim values when adding/modifying;
181                          * it should be when searching (see ACLs).
182                          */
183                         ch_free( a_vals[i].bv_val );
184                         if (last > i ) {
185                                 a_vals[i] = a_vals[last];
186                         }
187                         a_vals[last].bv_len = 0;
188                         a_vals[last].bv_val = NULL;
189                         last--;
190                         break;
191                 }
192         }
193         
194         return 0;
195 }