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