]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/modrdn.c
ITS#3647 additional hdb dn2idl fixes
[openldap] / servers / slapd / back-ldap / modrdn.c
1 /* modrdn.c - ldap backend modrdn function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 int
35 ldap_back_modrdn(
36     Operation   *op,
37     SlapReply   *rs )
38 {
39         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
40         struct ldapconn *lc;
41         ber_int_t msgid;
42         dncookie dc;
43         int do_retry = 1;
44         int rc = LDAP_SUCCESS;
45 #ifdef LDAP_BACK_PROXY_AUTHZ 
46         LDAPControl **ctrls = NULL;
47 #endif /* LDAP_BACK_PROXY_AUTHZ */
48
49         struct berval mdn = BER_BVNULL, mnewSuperior = BER_BVNULL;
50
51         lc = ldap_back_getconn( op, rs );
52         if ( !lc || !ldap_back_dobind(lc, op, rs) ) {
53                 return( -1 );
54         }
55
56         dc.rwmap = &li->rwmap;
57 #ifdef ENABLE_REWRITE
58         dc.conn = op->o_conn;
59         dc.rs = rs;
60 #else
61         dc.tofrom = 1;
62         dc.normalized = 0;
63 #endif
64         if (op->orr_newSup) {
65                 int version = LDAP_VERSION3;
66                 ldap_set_option( lc->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
67                 
68                 /*
69                  * Rewrite the new superior, if defined and required
70                  */
71 #ifdef ENABLE_REWRITE
72                 dc.ctx = "newSuperiorDN";
73 #endif
74                 if ( ldap_back_dn_massage( &dc, op->orr_newSup,
75                         &mnewSuperior ) ) {
76                         send_ldap_result( op, rs );
77                         return -1;
78                 }
79         }
80
81         /*
82          * Rewrite the modrdn dn, if required
83          */
84 #ifdef ENABLE_REWRITE
85         dc.ctx = "modrDN";
86 #endif
87         if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mdn ) ) {
88                 send_ldap_result( op, rs );
89                 return -1;
90         }
91
92 #ifdef LDAP_BACK_PROXY_AUTHZ
93         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
94         if ( rc != LDAP_SUCCESS ) {
95                 send_ldap_result( op, rs );
96                 rc = -1;
97                 goto cleanup;
98         }
99 #endif /* LDAP_BACK_PROXY_AUTHZ */
100
101 retry:
102         rs->sr_err = ldap_rename( lc->ld, mdn.bv_val,
103                         op->orr_newrdn.bv_val, mnewSuperior.bv_val,
104                         op->orr_deleteoldrdn,
105 #ifdef LDAP_BACK_PROXY_AUTHZ
106                         ctrls,
107 #else /* ! LDAP_BACK_PROXY_AUTHZ */
108                         op->o_ctrls,
109 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
110                         NULL, &msgid );
111         rc = ldap_back_op_result( lc, op, rs, msgid, 1 );
112         if ( rs->sr_err == LDAP_SERVER_DOWN && do_retry ) {
113                 do_retry = 0;
114                 if ( ldap_back_retry (lc, op, rs )) goto retry;
115         }
116
117 #ifdef LDAP_BACK_PROXY_AUTHZ
118 cleanup:
119         if ( ctrls && ctrls != op->o_ctrls ) {
120                 free( ctrls[ 0 ] );
121                 free( ctrls );
122         }
123 #endif /* LDAP_BACK_PROXY_AUTHZ */
124
125         if ( mdn.bv_val != op->o_req_ndn.bv_val ) {
126                 free( mdn.bv_val );
127         }
128         if ( mnewSuperior.bv_val != NULL
129                 && mnewSuperior.bv_val != op->oq_modrdn.rs_newSup->bv_val ) {
130                 free( mnewSuperior.bv_val );
131         }
132
133         return rc;
134 }
135