]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modrdn.c
d4ea67f895d2c9c950488f354fbfb1666df4cd2c
[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         struct metainfo         *li = ( struct metainfo * )op->o_bd->be_private;
38         struct metaconn         *lc;
39         int                     rc = 0;
40         int                     candidate = -1;
41         struct berval           mdn = BER_BVNULL,
42                                 mnewSuperior = BER_BVNULL;
43         dncookie                dc;
44
45         lc = meta_back_getconn( op, rs, META_OP_REQUIRE_SINGLE,
46                         &op->o_req_ndn, &candidate );
47         if ( !lc ) {
48                 rc = -1;
49                 goto cleanup;
50         }
51
52         assert( candidate != META_TARGET_NONE );
53
54         if ( !meta_back_dobind( lc, op ) ) {
55                 rs->sr_err = LDAP_UNAVAILABLE;
56
57         } else if ( !meta_back_is_valid( lc, candidate ) ) {
58                 rs->sr_err = LDAP_OTHER;
59         }
60
61         if ( rs->sr_err != LDAP_SUCCESS ) {
62                 rc = -1;
63                 goto cleanup;
64         }
65
66         dc.conn = op->o_conn;
67         dc.rs = rs;
68
69         if ( op->orr_newSup ) {
70                 int nsCandidate, version = LDAP_VERSION3;
71
72                 nsCandidate = meta_back_select_unique_candidate( li,
73                                 op->orr_nnewSup );
74
75                 if ( nsCandidate != candidate ) {
76                         /*
77                          * FIXME: one possibility is to delete the entry
78                          * from one target and add it to the other;
79                          * unfortunately we'd need write access to both,
80                          * which is nearly impossible; for administration
81                          * needs, the rootdn of the metadirectory could
82                          * be mapped to an administrative account on each
83                          * target (the binddn?); we'll see.
84                          */
85                         /*
86                          * FIXME: is this the correct return code?
87                          */
88                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
89                         rs->sr_text = "cross-target rename not supported";
90                         rc = -1;
91                         goto cleanup;
92                 }
93
94                 ldap_set_option( lc->mc_conns[ nsCandidate ].msc_ld,
95                                 LDAP_OPT_PROTOCOL_VERSION, &version );
96
97                 /*
98                  * Rewrite the new superior, if defined and required
99                  */
100                 dc.rwmap = &li->targets[ nsCandidate ]->mt_rwmap;
101                 dc.ctx = "newSuperiorDN";
102                 if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
103                         rc = -1;
104                         goto cleanup;
105                 }
106         }
107
108         /*
109          * Rewrite the modrdn dn, if required
110          */
111         dc.rwmap = &li->targets[ candidate ]->mt_rwmap;
112         dc.ctx = "modrDN";
113         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
114                 rc = -1;
115                 goto cleanup;
116         }
117
118         rc = ldap_rename_s( lc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
119                         op->orr_newrdn.bv_val,
120                         mnewSuperior.bv_val,
121                         op->orr_deleteoldrdn,
122                         NULL, NULL ) != LDAP_SUCCESS;
123
124 cleanup:;
125         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
126                 free( mdn.bv_val );
127                 BER_BVZERO( &mdn );
128         }
129         
130         if ( !BER_BVISNULL( &mnewSuperior )
131                         && mnewSuperior.bv_val != op->orr_newSup->bv_val )
132         {
133                 free( mnewSuperior.bv_val );
134                 BER_BVZERO( &mnewSuperior );
135         }
136
137         if ( rc == 0 ) {
138                 return meta_back_op_result( lc, op, rs ) == LDAP_SUCCESS
139                         ? 0 : 1;
140         } /* else */
141
142         send_ldap_result( op, rs );
143
144         return rc;
145 }
146