]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modrdn.c
port identity assertion to back-meta; share as much code as possible with back-ldap...
[openldap] / servers / slapd / back-meta / modrdn.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/socket.h>
28 #include <ac/string.h>
29
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33
34 int
35 meta_back_modrdn( Operation *op, SlapReply *rs )
36 {
37         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
38         metatarget_t    *mt;
39         metaconn_t      *mc;
40         int             candidate = -1;
41         struct berval   mdn = BER_BVNULL,
42                         mnewSuperior = BER_BVNULL;
43         dncookie        dc;
44         int             msgid;
45         int             do_retry = 1;
46         int             maperr = 1;
47         LDAPControl     **ctrls = NULL;
48
49         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
50         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
51                 return rs->sr_err;
52         }
53
54         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
55
56         mt = mi->mi_targets[ candidate ];
57         dc.target = mt;
58         dc.conn = op->o_conn;
59         dc.rs = rs;
60
61         if ( op->orr_newSup ) {
62
63                 /*
64                  * NOTE: the newParent, if defined, must be on the 
65                  * same target as the entry to be renamed.  This check
66                  * has been anticipated in meta_back_getconn()
67                  */
68                 /*
69                  * FIXME: one possibility is to delete the entry
70                  * from one target and add it to the other;
71                  * unfortunately we'd need write access to both,
72                  * which is nearly impossible; for administration
73                  * needs, the rootdn of the metadirectory could
74                  * be mapped to an administrative account on each
75                  * target (the binddn?); we'll see.
76                  */
77                 /*
78                  * NOTE: we need to port the identity assertion
79                  * feature from back-ldap
80                  */
81
82                 /* needs LDAPv3 */
83                 switch ( mt->mt_version ) {
84                 case LDAP_VERSION3:
85                         break;
86
87                 case 0:
88                         if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
89                                 break;
90                         }
91                         /* fall thru */
92
93                 default:
94                         /* op->o_protocol cannot be anything but LDAPv3,
95                          * otherwise wouldn't be here */
96                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
97                         maperr = 0;
98                         goto cleanup;
99                 }
100                 
101                 /*
102                  * Rewrite the new superior, if defined and required
103                  */
104                 dc.ctx = "newSuperiorDN";
105                 if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
106                         rs->sr_err = LDAP_OTHER;
107                         maperr = 0;
108                         goto cleanup;
109                 }
110         }
111
112         /*
113          * Rewrite the modrdn dn, if required
114          */
115         dc.ctx = "modrDN";
116         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
117                 rs->sr_err = LDAP_OTHER;
118                 maperr = 0;
119                 goto cleanup;
120         }
121
122         ctrls = op->o_ctrls;
123         if ( ldap_back_proxy_authz_ctrl( &mc->mc_conns[ candidate ].msc_bound_ndn,
124                 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls ) != LDAP_SUCCESS )
125         {
126                 maperr = 0;
127                 goto cleanup;
128         }
129
130 retry:;
131         rs->sr_err = ldap_rename( mc->mc_conns[ candidate ].msc_ld,
132                         mdn.bv_val, op->orr_newrdn.bv_val,
133                         mnewSuperior.bv_val, op->orr_deleteoldrdn,
134                         ctrls, NULL, &msgid );
135         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
136                 do_retry = 0;
137                 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
138                         goto retry;
139                 }
140                 goto done;
141
142         } else if ( rs->sr_err == LDAP_SUCCESS ) {
143                 struct timeval  tv, *tvp = NULL;
144                 LDAPMessage     *res = NULL;
145                 int             rc;
146
147                 if ( mt->mt_timeout[ LDAP_BACK_OP_MODRDN ] != 0 ) {
148                         tv.tv_sec = mt->mt_timeout[ LDAP_BACK_OP_MODRDN ];
149                         tv.tv_usec = 0;
150                         tvp = &tv;
151                 }
152
153                 rs->sr_err = LDAP_OTHER;
154                 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
155                         msgid, LDAP_MSG_ALL, tvp, &res );
156                 maperr = 0;
157                 switch ( rc ) {
158                 case -1:
159                         break;
160
161                 case 0:
162                         (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
163                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
164                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
165                         break;
166
167                 case LDAP_RES_RENAME:
168                         /* FIXME: matched? referrals? response controls? */
169                         rc = ldap_parse_result( mc->mc_conns[ candidate ].msc_ld,
170                                 res, &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
171                         if ( rc != LDAP_SUCCESS ) {
172                                 rs->sr_err = rc;
173                         }
174                         maperr = 1;
175                         break;
176
177                 default:
178                         ldap_msgfree( res );
179                         break;
180                 }
181         }
182
183 cleanup:;
184         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
185
186         if ( maperr ) {
187                 meta_back_op_result( mc, op, rs, candidate );
188
189         } else {
190                 send_ldap_result( op, rs );
191
192                 if ( META_BACK_TGT_QUARANTINE( mt ) ) {
193                         meta_back_quarantine( op, rs, candidate );
194                 }
195         }
196
197 done:;
198         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
199                 free( mdn.bv_val );
200                 BER_BVZERO( &mdn );
201         }
202         
203         if ( !BER_BVISNULL( &mnewSuperior )
204                         && mnewSuperior.bv_val != op->orr_newSup->bv_val )
205         {
206                 free( mnewSuperior.bv_val );
207                 BER_BVZERO( &mnewSuperior );
208         }
209
210         if ( mc ) {
211                 meta_back_release_conn( op, mc );
212         }
213
214         return rs->sr_err;
215 }
216