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