]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/add.c
Changes suggested by Ando.
[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
49         Debug(LDAP_DEBUG_ARGS, "==> meta_back_add: %s\n",
50                         op->o_req_dn.bv_val, 0, 0 );
51
52         /*
53          * get the current connection
54          */
55         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
56         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
57                 return rs->sr_err;
58         }
59
60         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
61
62         /*
63          * Rewrite the add dn, if needed
64          */
65         dc.target = &mi->mi_targets[ candidate ];
66         dc.conn = op->o_conn;
67         dc.rs = rs;
68         dc.ctx = "addDN";
69
70         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
71                 send_ldap_result( op, rs );
72                 goto done;
73         }
74
75         /* Count number of attributes in entry ( +1 ) */
76         for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
77         
78         /* Create array of LDAPMods for ldap_add() */
79         attrs = ch_malloc( sizeof( LDAPMod * )*i );
80
81         dc.ctx = "addAttrDN";
82         isupdate = be_shadow_update( op );
83         for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
84                 int                     j, is_oc = 0;
85
86                 if ( !isupdate && !get_manageDIT( op ) && a->a_desc->ad_type->sat_no_user_mod  )
87                 {
88                         continue;
89                 }
90
91                 if ( a->a_desc == slap_schema.si_ad_objectClass 
92                                 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
93                 {
94                         is_oc = 1;
95                         mapped = a->a_desc->ad_cname;
96
97                 } else {
98                         ldap_back_map( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
99                                         &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
100                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
101                                 continue;
102                         }
103                 }
104
105                 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
106                 if ( attrs[ i ] == NULL ) {
107                         continue;
108                 }
109                 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
110                 attrs[ i ]->mod_type = mapped.bv_val;
111
112                 if ( is_oc ) {
113                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
114                                 ;
115
116                         attrs[ i ]->mod_bvalues =
117                                 (struct berval **)ch_malloc( ( j + 1 ) *
118                                 sizeof( struct berval * ) );
119
120                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
121                                 struct ldapmapping      *mapping;
122
123                                 ldap_back_mapping( &mi->mi_targets[ candidate ].mt_rwmap.rwm_oc,
124                                                 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
125
126                                 if ( mapping == NULL ) {
127                                         if ( mi->mi_targets[ candidate ].mt_rwmap.rwm_oc.drop_missing ) {
128                                                 continue;
129                                         }
130                                         attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
131
132                                 } else {
133                                         attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
134                                 }
135                                 j++;
136                         }
137                         attrs[ i ]->mod_bvalues[ j ] = NULL;
138
139                 } else {
140                         /*
141                          * FIXME: dn-valued attrs should be rewritten
142                          * to allow their use in ACLs at the back-ldap
143                          * level.
144                          */
145                         if ( a->a_desc->ad_type->sat_syntax ==
146                                 slap_schema.si_syn_distinguishedName )
147                         {
148                                 (void)ldap_dnattr_rewrite( &dc, a->a_vals );
149                                 if ( a->a_vals == NULL ) {
150                                         continue;
151                                 }
152                         }
153
154                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
155                                 ;
156                         
157                         attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
158                         for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
159                                 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
160                         }
161                         attrs[ i ]->mod_bvalues[ j ] = NULL;
162                 }
163                 i++;
164         }
165         attrs[ i ] = NULL;
166
167 retry:;
168         rs->sr_err = ldap_add_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
169                               attrs, op->o_ctrls, NULL, &msgid );
170         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
171                 do_retry = 0;
172                 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
173                         goto retry;
174                 }
175                 goto cleanup;
176
177         } else if ( rs->sr_err == LDAP_SUCCESS ) {
178                 struct timeval  tv, *tvp = NULL;
179                 LDAPMessage     *res = NULL;
180                 int             rc;
181
182                 if ( mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_ADD ] != 0 ) {
183                         tv.tv_sec = mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_ADD ];
184                         tv.tv_usec = 0;
185                         tvp = &tv;
186                 }
187
188                 rs->sr_err = LDAP_OTHER;
189                 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
190                         msgid, LDAP_MSG_ALL, tvp, &res );
191                 switch ( rc ) {
192                 case -1:
193                         send_ldap_result( op, rs );
194                         goto cleanup;
195
196                 case 0:
197                         ldap_abandon_ext( mc->mc_conns[ candidate ].msc_ld,
198                                 msgid, NULL, NULL );
199                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
200                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
201                         send_ldap_result( op, rs );
202                         goto cleanup;
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                         break;
211
212                 default:
213                         ldap_msgfree( res );
214                         break;
215                 }
216         }
217
218         (void)meta_back_op_result( mc, op, rs, candidate );
219
220 cleanup:;
221         for ( --i; i >= 0; --i ) {
222                 free( attrs[ i ]->mod_bvalues );
223                 free( attrs[ i ] );
224         }
225         free( attrs );
226         if ( mdn.bv_val != op->ora_e->e_dn ) {
227                 free( mdn.bv_val );
228                 BER_BVZERO( &mdn );
229         }
230
231 done:;
232         if ( mc ) {
233                 meta_back_release_conn( op, mc );
234         }
235
236         return rs->sr_err;
237 }
238