]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modify.c
+ Fixed slapd connectionless LDAP support
[openldap] / servers / slapd / back-meta / modify.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 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33
34 int
35 meta_back_modify( Operation *op, SlapReply *rs )
36 {
37         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
38         metaconn_t      *mc;
39         int             rc = 0;
40         LDAPMod         **modv = NULL;
41         LDAPMod         *mods = NULL;
42         Modifications   *ml;
43         int             candidate = -1, i;
44         int             isupdate;
45         struct berval   mdn = BER_BVNULL;
46         struct berval   mapped;
47         dncookie        dc;
48         int             msgid;
49         int             do_retry = 1;
50
51         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
52         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
53                 return rs->sr_err;
54         }
55
56         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
57
58         /*
59          * Rewrite the modify dn, if needed
60          */
61         dc.target = &mi->mi_targets[ candidate ];
62         dc.conn = op->o_conn;
63         dc.rs = rs;
64         dc.ctx = "modifyDN";
65
66         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
67                 rc = -1;
68                 goto cleanup;
69         }
70
71         for ( i = 0, ml = op->orm_modlist; ml; i++ ,ml = ml->sml_next )
72                 ;
73
74         mods = ch_malloc( sizeof( LDAPMod )*i );
75         if ( mods == NULL ) {
76                 rs->sr_err = LDAP_NO_MEMORY;
77                 rc = -1;
78                 goto cleanup;
79         }
80         modv = ( LDAPMod ** )ch_malloc( ( i + 1 )*sizeof( LDAPMod * ) );
81         if ( modv == NULL ) {
82                 rs->sr_err = LDAP_NO_MEMORY;
83                 rc = -1;
84                 goto cleanup;
85         }
86
87         dc.ctx = "modifyAttrDN";
88         isupdate = be_shadow_update( op );
89         for ( i = 0, ml = op->orm_modlist; ml; ml = ml->sml_next ) {
90                 int     j, is_oc = 0;
91
92                 if ( !isupdate && !get_manageDIT( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
93                 {
94                         continue;
95                 }
96
97                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
98                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
99                 {
100                         is_oc = 1;
101                         mapped = ml->sml_desc->ad_cname;
102
103                 } else {
104                         ldap_back_map( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
105                                         &ml->sml_desc->ad_cname, &mapped,
106                                         BACKLDAP_MAP );
107                         if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
108                                 continue;
109                         }
110                 }
111
112                 modv[ i ] = &mods[ i ];
113                 mods[ i ].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
114                 mods[ i ].mod_type = mapped.bv_val;
115
116                 /*
117                  * FIXME: dn-valued attrs should be rewritten
118                  * to allow their use in ACLs at the back-ldap
119                  * level.
120                  */
121                 if ( ml->sml_values != NULL ) {
122                         if ( is_oc ) {
123                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
124                                         ;
125                                 mods[ i ].mod_bvalues =
126                                         (struct berval **)ch_malloc( ( j + 1 ) *
127                                         sizeof( struct berval * ) );
128                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); ) {
129                                         struct ldapmapping      *mapping;
130
131                                         ldap_back_mapping( &mi->mi_targets[ candidate ].mt_rwmap.rwm_oc,
132                                                         &ml->sml_values[ j ], &mapping, BACKLDAP_MAP );
133
134                                         if ( mapping == NULL ) {
135                                                 if ( mi->mi_targets[ candidate ].mt_rwmap.rwm_oc.drop_missing ) {
136                                                         continue;
137                                                 }
138                                                 mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
139
140                                         } else {
141                                                 mods[ i ].mod_bvalues[ j ] = &mapping->dst;
142                                         }
143                                         j++;
144                                 }
145                                 mods[ i ].mod_bvalues[ j ] = NULL;
146
147                         } else {
148                                 if ( ml->sml_desc->ad_type->sat_syntax ==
149                                                 slap_schema.si_syn_distinguishedName )
150                                 {
151                                         ( void )ldap_dnattr_rewrite( &dc, ml->sml_values );
152                                         if ( ml->sml_values == NULL ) {
153                                                 continue;
154                                         }
155                                 }
156
157                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ )
158                                         ;
159                                 mods[ i ].mod_bvalues =
160                                         (struct berval **)ch_malloc( ( j + 1 ) *
161                                         sizeof( struct berval * ) );
162                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
163                                         mods[ i ].mod_bvalues[ j ] = &ml->sml_values[ j ];
164                                 }
165                                 mods[ i ].mod_bvalues[ j ] = NULL;
166                         }
167
168                 } else {
169                         mods[ i ].mod_bvalues = NULL;
170                 }
171
172                 i++;
173         }
174         modv[ i ] = 0;
175
176 retry:;
177         rs->sr_err = ldap_modify_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
178                         modv, op->o_ctrls, NULL, &msgid );
179         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
180                 do_retry = 0;
181                 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
182                         goto retry;
183                 }
184
185         } else if ( rs->sr_err == LDAP_SUCCESS ) {
186                 struct timeval  tv, *tvp = NULL;
187                 LDAPMessage     *res = NULL;
188
189                 if ( mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_MODIFY ] != 0 ) {
190                         tv.tv_sec = mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_MODIFY ];
191                         tv.tv_usec = 0;
192                         tvp = &tv;
193                 }
194
195                 rs->sr_err = LDAP_OTHER;
196                 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
197                         msgid, LDAP_MSG_ALL, tvp, &res );
198                 switch ( rc ) {
199                 case -1:
200                         rc = -1;
201                         break;
202
203                 case 0:
204                         ldap_abandon_ext( mc->mc_conns[ candidate ].msc_ld,
205                                 msgid, NULL, NULL );
206                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
207                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
208                         rc = -1;
209                         break;
210
211                 case LDAP_RES_MODIFY:
212                         rc = ldap_parse_result( mc->mc_conns[ candidate ].msc_ld,
213                                 res, &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
214                         if ( rc != LDAP_SUCCESS ) {
215                                 rs->sr_err = rc;
216                         }
217                         rc = 0;
218                         break;
219
220                 default:
221                         rc = -1;
222                         ldap_msgfree( res );
223                         break;
224                 }
225         }
226
227 cleanup:;
228         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
229                 free( mdn.bv_val );
230                 BER_BVZERO( &mdn );
231         }
232         if ( modv != NULL ) {
233                 for ( i = 0; modv[ i ]; i++ ) {
234                         free( modv[ i ]->mod_bvalues );
235                 }
236         }
237         free( mods );
238         free( modv );
239
240         if ( rc != -1 ) {
241                 rc = meta_back_op_result( mc, op, rs, candidate );
242
243         } else {
244                 send_ldap_result( op, rs );
245                 rc = 0;
246         }
247
248         meta_back_release_conn( op, mc );
249
250         return rc;
251 }
252