]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/modrdn.c
Merge remote branch 'origin/mdb.master'
[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-2011 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         ldapinfo_t              *li = (ldapinfo_t *)op->o_bd->be_private;
40
41         ldapconn_t              *lc = NULL;
42         ber_int_t               msgid;
43         LDAPControl             **ctrls = NULL;
44         ldap_back_send_t        retrying = LDAP_BACK_RETRYING;
45         int                     rc = LDAP_SUCCESS;
46         char                    *newSup = NULL;
47         struct berval           newrdn = BER_BVNULL;
48
49         if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
50                 return rs->sr_err;
51         }
52
53         if ( op->orr_newSup ) {
54                 /* needs LDAPv3 */
55                 switch ( li->li_version ) {
56                 case LDAP_VERSION3:
57                         break;
58
59                 case 0:
60                         if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
61                                 break;
62                         }
63                         /* fall thru */
64
65                 default:
66                         /* op->o_protocol cannot be anything but LDAPv3,
67                          * otherwise wouldn't be here */
68                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
69                         send_ldap_result( op, rs );
70                         goto cleanup;
71                 }
72                 
73                 newSup = op->orr_newSup->bv_val;
74         }
75
76         /* NOTE: we need to copy the newRDN in case it was formed
77          * from a DN by simply changing the length (ITS#5397) */
78         newrdn = op->orr_newrdn;
79         if ( newrdn.bv_val[ newrdn.bv_len ] != '\0' ) {
80                 ber_dupbv_x( &newrdn, &op->orr_newrdn, op->o_tmpmemctx );
81         }
82
83 retry:
84         ctrls = op->o_ctrls;
85         rc = ldap_back_controls_add( op, rs, lc, &ctrls );
86         if ( rc != LDAP_SUCCESS ) {
87                 send_ldap_result( op, rs );
88                 rc = -1;
89                 goto cleanup;
90         }
91
92         rs->sr_err = ldap_rename( lc->lc_ld, op->o_req_dn.bv_val,
93                         newrdn.bv_val, newSup,
94                         op->orr_deleteoldrdn, ctrls, NULL, &msgid );
95         rc = ldap_back_op_result( lc, op, rs, msgid,
96                 li->li_timeout[ SLAP_OP_MODRDN ],
97                 ( LDAP_BACK_SENDRESULT | retrying ) );
98         if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
99                 retrying &= ~LDAP_BACK_RETRYING;
100                 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
101                         /* if the identity changed, there might be need to re-authz */
102                         (void)ldap_back_controls_free( op, rs, &ctrls );
103                         goto retry;
104                 }
105         }
106
107 cleanup:
108         (void)ldap_back_controls_free( op, rs, &ctrls );
109
110         if ( newrdn.bv_val != op->orr_newrdn.bv_val ) {
111                 op->o_tmpfree( newrdn.bv_val, op->o_tmpmemctx );
112         }
113
114         if ( lc != NULL ) {
115                 ldap_back_release_conn( li, lc );
116         }
117
118         return rc;
119 }
120