]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modrdn.c
honor "chase-referrals no" (ITS#4447)
[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         metaconn_t      *mc;
39         int             candidate = -1;
40         struct berval   mdn = BER_BVNULL,
41                         mnewSuperior = BER_BVNULL;
42         dncookie        dc;
43         int             msgid;
44         int             do_retry = 1;
45         int             maperr = 1;
46
47         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
48         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
49                 return rs->sr_err;
50         }
51
52         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
53
54         dc.conn = op->o_conn;
55         dc.rs = rs;
56
57         if ( op->orr_newSup ) {
58                 int     version = LDAP_VERSION3;
59
60                 /*
61                  * NOTE: the newParent, if defined, must be on the 
62                  * same target as the entry to be renamed.  This check
63                  * has been anticipated in meta_back_getconn()
64                  */
65                 /*
66                  * FIXME: one possibility is to delete the entry
67                  * from one target and add it to the other;
68                  * unfortunately we'd need write access to both,
69                  * which is nearly impossible; for administration
70                  * needs, the rootdn of the metadirectory could
71                  * be mapped to an administrative account on each
72                  * target (the binddn?); we'll see.
73                  */
74                 /*
75                  * NOTE: we need to port the identity assertion
76                  * feature from back-ldap
77                  */
78
79                 /* newSuperior needs LDAPv3; if we got here, we can safely
80                  * enforce it */
81                 ldap_set_option( mc->mc_conns[ candidate ].msc_ld,
82                                 LDAP_OPT_PROTOCOL_VERSION, &version );
83
84                 /*
85                  * Rewrite the new superior, if defined and required
86                  */
87                 dc.target = &mi->mi_targets[ candidate ];
88                 dc.ctx = "newSuperiorDN";
89                 if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
90                         rs->sr_err = LDAP_OTHER;
91                         maperr = 0;
92                         goto cleanup;
93                 }
94         }
95
96         /*
97          * Rewrite the modrdn dn, if required
98          */
99         dc.target = &mi->mi_targets[ candidate ];
100         dc.ctx = "modrDN";
101         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
102                 rs->sr_err = LDAP_OTHER;
103                 maperr = 0;
104                 goto cleanup;
105         }
106
107 retry:;
108         rs->sr_err = ldap_rename( mc->mc_conns[ candidate ].msc_ld,
109                         mdn.bv_val, op->orr_newrdn.bv_val,
110                         mnewSuperior.bv_val, op->orr_deleteoldrdn,
111                         op->o_ctrls, NULL, &msgid );
112         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
113                 do_retry = 0;
114                 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
115                         goto retry;
116                 }
117
118         } else if ( rs->sr_err == LDAP_SUCCESS ) {
119                 struct timeval  tv, *tvp = NULL;
120                 LDAPMessage     *res = NULL;
121                 int             rc;
122
123                 if ( mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_MODRDN ] != 0 ) {
124                         tv.tv_sec = mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_MODRDN ];
125                         tv.tv_usec = 0;
126                         tvp = &tv;
127                 }
128
129                 rs->sr_err = LDAP_OTHER;
130                 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
131                         msgid, LDAP_MSG_ALL, tvp, &res );
132                 maperr = 0;
133                 switch ( rc ) {
134                 case -1:
135                         break;
136
137                 case 0:
138                         ldap_abandon_ext( mc->mc_conns[ candidate ].msc_ld,
139                                 msgid, NULL, NULL );
140                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
141                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
142                         break;
143
144                 case LDAP_RES_RENAME:
145                         rc = ldap_parse_result( mc->mc_conns[ candidate ].msc_ld,
146                                 res, &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
147                         if ( rc != LDAP_SUCCESS ) {
148                                 rs->sr_err = rc;
149                         }
150                         maperr = 1;
151                         break;
152
153                 default:
154                         ldap_msgfree( res );
155                         break;
156                 }
157         }
158
159 cleanup:;
160         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
161                 free( mdn.bv_val );
162                 BER_BVZERO( &mdn );
163         }
164         
165         if ( !BER_BVISNULL( &mnewSuperior )
166                         && mnewSuperior.bv_val != op->orr_newSup->bv_val )
167         {
168                 free( mnewSuperior.bv_val );
169                 BER_BVZERO( &mnewSuperior );
170         }
171
172         if ( maperr ) {
173                 meta_back_op_result( mc, op, rs, candidate );
174
175         } else {
176                 send_ldap_result( op, rs );
177         }
178
179         meta_back_release_conn( op, mc );
180
181         return rs->sr_err;
182 }
183