]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/modrdn.c
83efbb176c506af13e23776258733b85a7870396
[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             msgid;
44         int             do_retry = 1;
45
46         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
47         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
48                 return rs->sr_err;
49         }
50
51         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
52
53         dc.conn = op->o_conn;
54         dc.rs = rs;
55
56         if ( op->orr_newSup ) {
57                 int     version = LDAP_VERSION3;
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                 /* newSuperior needs LDAPv3; if we got here, we can safely
79                  * enforce it */
80                 ldap_set_option( mc->mc_conns[ candidate ].msc_ld,
81                                 LDAP_OPT_PROTOCOL_VERSION, &version );
82
83                 /*
84                  * Rewrite the new superior, if defined and required
85                  */
86                 dc.target = &mi->mi_targets[ candidate ];
87                 dc.ctx = "newSuperiorDN";
88                 if ( ldap_back_dn_massage( &dc, op->orr_newSup, &mnewSuperior ) ) {
89                         rs->sr_err = LDAP_OTHER;
90                         goto cleanup;
91                 }
92         }
93
94         /*
95          * Rewrite the modrdn dn, if required
96          */
97         dc.target = &mi->mi_targets[ candidate ];
98         dc.ctx = "modrDN";
99         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
100                 rs->sr_err = LDAP_OTHER;
101                 goto cleanup;
102         }
103
104 retry:;
105         rs->sr_err = ldap_rename( mc->mc_conns[ candidate ].msc_ld,
106                         mdn.bv_val, op->orr_newrdn.bv_val,
107                         mnewSuperior.bv_val, op->orr_deleteoldrdn,
108                         op->o_ctrls, NULL, &msgid );
109         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
110                 do_retry = 0;
111                 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
112                         goto retry;
113                 }
114
115         } else if ( rs->sr_err == LDAP_SUCCESS ) {
116                 struct timeval  tv, *tvp = NULL;
117                 LDAPMessage     *res = NULL;
118                 int             rc;
119
120                 if ( mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_MODRDN ] != 0 ) {
121                         tv.tv_sec = mi->mi_targets[ candidate ].mt_timeout[ LDAP_BACK_OP_MODRDN ];
122                         tv.tv_usec = 0;
123                         tvp = &tv;
124                 }
125
126                 rs->sr_err = LDAP_OTHER;
127                 rc = ldap_result( mc->mc_conns[ candidate ].msc_ld,
128                         msgid, LDAP_MSG_ALL, tvp, &res );
129                 switch ( rc ) {
130                 case -1:
131                         break;
132
133                 case 0:
134                         ldap_abandon_ext( mc->mc_conns[ candidate ].msc_ld,
135                                 msgid, NULL, NULL );
136                         rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
137                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
138                         break;
139
140                 case LDAP_RES_RENAME:
141                         rc = ldap_parse_result( mc->mc_conns[ candidate ].msc_ld,
142                                 res, &rs->sr_err, NULL, NULL, NULL, NULL, 1 );
143                         if ( rc != LDAP_SUCCESS ) {
144                                 rs->sr_err = rc;
145                         }
146                         break;
147
148                 default:
149                         ldap_msgfree( res );
150                         break;
151                 }
152         }
153
154 cleanup:;
155         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
156                 free( mdn.bv_val );
157                 BER_BVZERO( &mdn );
158         }
159         
160         if ( !BER_BVISNULL( &mnewSuperior )
161                         && mnewSuperior.bv_val != op->orr_newSup->bv_val )
162         {
163                 free( mnewSuperior.bv_val );
164                 BER_BVZERO( &mnewSuperior );
165         }
166
167         if ( rs->sr_err == LDAP_SUCCESS ) {
168                 meta_back_op_result( mc, op, rs, candidate );
169         } else {
170                 send_ldap_result( op, rs );
171         }
172
173         meta_back_release_conn( op, mc );
174
175         return rs->sr_err;
176 }
177