]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modrdn.c
fix previous commit
[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-2005 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                     do_retry = 1;
44
45         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
46         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
47                 return rs->sr_err;
48         }
49
50         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
51
52         dc.conn = op->o_conn;
53         dc.rs = rs;
54
55         if ( op->orr_newSup ) {
56                 int     version = LDAP_VERSION3;
57
58                 /*
59                  * NOTE: the newParent, if defined, must be on the 
60                  * same target as the entry to be renamed.  This check
61                  * has been anticipated in meta_back_getconn()
62                  */
63                 /*
64                  * FIXME: one possibility is to delete the entry
65                  * from one target and add it to the other;
66                  * unfortunately we'd need write access to both,
67                  * which is nearly impossible; for administration
68                  * needs, the rootdn of the metadirectory could
69                  * be mapped to an administrative account on each
70                  * target (the binddn?); we'll see.
71                  */
72                 /*
73                  * NOTE: we need to port the identity assertion
74                  * feature from back-ldap
75                  */
76
77                 /* newSuperior needs LDAPv3; if we got here, we can safely
78                  * enforce it */
79                 ldap_set_option( mc->mc_conns[ candidate ].msc_ld,
80                                 LDAP_OPT_PROTOCOL_VERSION, &version );
81
82                 /*
83                  * Rewrite the new superior, if defined and required
84                  */
85                 dc.target = &mi->mi_targets[ candidate ];
86                 dc.ctx = "newSuperiorDN";
87                 if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
88                         rs->sr_err = LDAP_OTHER;
89                         goto cleanup;
90                 }
91         }
92
93         /*
94          * Rewrite the modrdn dn, if required
95          */
96         dc.target = &mi->mi_targets[ candidate ];
97         dc.ctx = "modrDN";
98         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
99                 rs->sr_err = LDAP_OTHER;
100                 goto cleanup;
101         }
102
103 retry:;
104         rs->sr_err = ldap_rename_s( mc->mc_conns[ candidate ].msc_ld,
105                         mdn.bv_val, op->orr_newrdn.bv_val,
106                         mnewSuperior.bv_val, op->orr_deleteoldrdn,
107                         op->o_ctrls, NULL );
108         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
109                 do_retry = 0;
110                 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
111                         goto retry;
112                 }
113         }
114
115 cleanup:;
116         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
117                 free( mdn.bv_val );
118                 BER_BVZERO( &mdn );
119         }
120         
121         if ( !BER_BVISNULL( &mnewSuperior )
122                         && mnewSuperior.bv_val != op->orr_newSup->bv_val )
123         {
124                 free( mnewSuperior.bv_val );
125                 BER_BVZERO( &mnewSuperior );
126         }
127
128         if ( rs->sr_err == LDAP_SUCCESS ) {
129                 meta_back_op_result( mc, op, rs, candidate );
130         }
131
132         send_ldap_result( op, rs );
133
134         return rs->sr_err;
135 }
136