]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
Additional fix for ITS#4522. The "dn=" ist not optional.
[openldap] / servers / slapd / back-meta / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "../back-ldap/back-ldap.h"
33 #include "back-meta.h"
34
35 int
36 meta_back_add( Operation *op, SlapReply *rs )
37 {
38         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
39         metaconn_t      *mc;
40         int             i, candidate = -1;
41         int             isupdate;
42         Attribute       *a;
43         LDAPMod         **attrs;
44         struct berval   mdn = BER_BVNULL, mapped;
45         dncookie        dc;
46         int             msgid;
47         int             do_retry = 1;
48         int             maperr = 1;
49
50         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
51                         op->o_req_dn.bv_val, 0, 0 );
52
53         /*
54          * get the current connection
55          */
56         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
57         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
58                 return rs->sr_err;
59         }
60
61         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
62
63         /*
64          * Rewrite the add dn, if needed
65          */
66         dc.target = mi->mi_targets[ candidate ];
67         dc.conn = op->o_conn;
68         dc.rs = rs;
69         dc.ctx = "addDN";
70
71         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
72                 send_ldap_result( op, rs );
73                 goto done;
74         }
75
76         /* Count number of attributes in entry ( +1 ) */
77         for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
78         
79         /* Create array of LDAPMods for ldap_add() */
80         attrs = ch_malloc( sizeof( LDAPMod * )*i );
81
82         dc.ctx = "addAttrDN";
83         isupdate = be_shadow_update( op );
84         for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
85                 int                     j, is_oc = 0;
86
87                 if ( !isupdate && !get_manageDIT( op ) && a->a_desc->ad_type->sat_no_user_mod  )
88                 {
89                         continue;
90                 }
91
92                 if ( a->a_desc == slap_schema.si_ad_objectClass 
93                                 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
94                 {
95                         is_oc = 1;
96                         mapped = a->a_desc->ad_cname;
97
98                 } else {
99                         ldap_back_map( &mi->mi_targets[ candidate ]->mt_rwmap.rwm_at,
100                                         &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
101                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
102                                 continue;
103                         }
104                 }
105
106                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
107                 if ( attrs[ i ] == NULL ) {
108                         continue;
109                 }
110                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
111                 attrs[ i ]->mod_type = mapped.bv_val;
112
113                 if ( is_oc ) {
114                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
115                                 ;
116
117                         attrs[ i ]->mod_bvalues =
118                                 (struct berval **)ch_malloc( ( j + 1 ) *
119                                 sizeof( struct berval * ) );
120
121                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
122                                 struct ldapmapping      *mapping;
123
124                                 ldap_back_mapping( &mi->mi_targets[ candidate ]->mt_rwmap.rwm_oc,
125                                                 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
126
127                                 if ( mapping == NULL ) {
128                                         if ( mi->mi_targets[ candidate ]->mt_rwmap.rwm_oc.drop_missing ) {
129                                                 continue;
130                                         }
131                                         attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
132
133                                 } else {
134                                         attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
135                                 }
136                                 j++;
137                         }
138                         attrs[ i ]->mod_bvalues[ j ] = NULL;
139
140                 } else {
141                         /*
142                          * FIXME: dn-valued attrs should be rewritten
143                          * to allow their use in ACLs at the back-ldap
144                          * level.
145                          */
146                         if ( a->a_desc->ad_type->sat_syntax ==
147                                 slap_schema.si_syn_distinguishedName )
148                         {
149                                 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
150                                 if ( a->a_vals == NULL ) {
151                                         continue;
152                                 }
153                         }
154
155                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
156                                 ;
157                         
158                         attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
159                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
160                                 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
161                         }
162                         attrs[ i ]->mod_bvalues[ j ] = NULL;
163                 }
164                 i++;
165         }
166         attrs[ i ] = NULL;
167
168 retry:;
169         rs->sr_err = ldap_add_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
170                               attrs, op->o_ctrls, NULL, &msgid );
171         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
172                 do_retry = 0;
173                 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
174                         goto retry;
175                 }
176                 goto cleanup;
177
178         } else if ( rs->sr_err == LDAP_SUCCESS ) {
179                 struct timeval  tv, *tvp = NULL;
180                 LDAPMessage     *res = NULL;
181                 int             rc;
182
183                 if ( mi->mi_targets[ candidate ]->mt_timeout[ LDAP_BACK_OP_ADD ] != 0 ) {
184                         tv.tv_sec = mi->mi_targets[ candidate ]->mt_timeout[ LDAP_BACK_OP_ADD ];
185                         tv.tv_usec = 0;
186                         tvp = &tv;
187                 }
188
189                 rs->sr_err = LDAP_OTHER;
190                 maperr = 0;
191                 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
192                         msgid, LDAP_MSG_ALL, tvp, &res );
193                 switch ( rc ) {
194                 case -1:
195                         break;
196
197                 case 0:
198                         ldap_abandon_ext( mc->mc_conns[ candidate ].msc_ld,
199                                 msgid, NULL, NULL );
200                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
201                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
202                         break;
203
204                 case LDAP_RES_ADD:
205                         rc = ldap_parse_result( mc->mc_conns[ candidate ].msc_ld,
206                                 res, &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
207                         if ( rc != LDAP_SUCCESS ) {
208                                 rs->sr_err = rc;
209                         }
210                         maperr = 1;
211                         break;
212
213                 default:
214                         ldap_msgfree( res );
215                         break;
216                 }
217         }
218
219         if ( maperr ) {
220                 rs->sr_err = meta_back_op_result( mc, op, rs, candidate );
221
222         } else {
223                 send_ldap_result( op, rs );
224         }
225
226 cleanup:;
227         for ( --i; i >= 0; --i ) {
228                 free( attrs[ i ]->mod_bvalues );
229                 free( attrs[ i ] );
230         }
231         free( attrs );
232         if ( mdn.bv_val != op->ora_e->e_dn ) {
233                 free( mdn.bv_val );
234                 BER_BVZERO( &mdn );
235         }
236
237 done:;
238         if ( mc ) {
239                 meta_back_release_conn( op, mc );
240         }
241
242         return rs->sr_err;
243 }
244