]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/modrdn.c
2b3067dfee91903ab99d73b2ec9e711b5b86d32b
[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-2003 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 #ifdef LDAP_BACK_PROXY_AUTHZ 
44         LDAPControl **ctrls = NULL;
45         int rc = LDAP_SUCCESS;
46 #endif /* LDAP_BACK_PROXY_AUTHZ */
47
48         struct berval mdn = { 0, NULL }, mnewSuperior = { 0, NULL };
49
50         lc = ldap_back_getconn( op, rs );
51         if ( !lc || !ldap_back_dobind(lc, op, rs) ) {
52                 return( -1 );
53         }
54
55         dc.rwmap = &li->rwmap;
56 #ifdef ENABLE_REWRITE
57         dc.conn = op->o_conn;
58         dc.rs = rs;
59 #else
60         dc.tofrom = 1;
61         dc.normalized = 0;
62 #endif
63         if (op->orr_newSup) {
64                 int version = LDAP_VERSION3;
65                 ldap_set_option( lc->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
66                 
67                 /*
68                  * Rewrite the new superior, if defined and required
69                  */
70 #ifdef ENABLE_REWRITE
71                 dc.ctx = "newSuperiorDn";
72 #endif
73                 if ( ldap_back_dn_massage( &dc, op->orr_newSup,
74                         &mnewSuperior ) ) {
75                         send_ldap_result( op, rs );
76                         return -1;
77                 }
78         }
79
80         /*
81          * Rewrite the modrdn dn, if required
82          */
83 #ifdef ENABLE_REWRITE
84         dc.ctx = "modrDn";
85 #endif
86         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
87                 send_ldap_result( op, rs );
88                 return -1;
89         }
90
91 #ifdef LDAP_BACK_PROXY_AUTHZ
92         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
93         if ( rc != LDAP_SUCCESS ) {
94                 goto cleanup;
95         }
96 #endif /* LDAP_BACK_PROXY_AUTHZ */
97
98         rs->sr_err = ldap_rename( lc->ld, mdn.bv_val,
99                         op->orr_newrdn.bv_val, mnewSuperior.bv_val,
100                         op->orr_deleteoldrdn,
101 #ifdef LDAP_BACK_PROXY_AUTHZ
102                         ctrls,
103 #else /* ! LDAP_BACK_PROXY_AUTHZ */
104                         op->o_ctrls,
105 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
106                         NULL, &msgid );
107
108 #ifdef LDAP_BACK_PROXY_AUTHZ
109 cleanup:
110         if ( ctrls && ctrls != op->o_ctrls ) {
111                 free( ctrls[ 0 ] );
112                 free( ctrls );
113         }
114 #endif /* LDAP_BACK_PROXY_AUTHZ */
115
116         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
117                 free( mdn.bv_val );
118         }
119         if ( mnewSuperior.bv_val != NULL
120                 && mnewSuperior.bv_val != op->oq_modrdn.rs_newSup->bv_val ) {
121                 free( mnewSuperior.bv_val );
122         }
123
124 #ifdef LDAP_BACK_PROXY_AUTHZ
125         if ( rc != LDAP_SUCCESS ) {
126                 send_ldap_result( op, rs );
127                 return -1;
128         }
129 #endif /* LDAP_BACK_PROXY_AUTHZ */
130
131         return( ldap_back_op_result( lc, op, rs, msgid, 1 ) );
132 }
133