xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-ldap/modrdn.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: modrdn.c,v 1.1.1.4 2014/05/28 09:58:49 tron Exp $	*/
2 
3 /* modrdn.c - ldap backend modrdn function */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1999-2014 The OpenLDAP Foundation.
8  * Portions Copyright 1999-2003 Howard Chu.
9  * Portions Copyright 2000-2003 Pierangelo Masarati.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted only as authorized by the OpenLDAP
14  * Public License.
15  *
16  * A copy of this license is available in the file LICENSE in the
17  * top-level directory of the distribution or, alternatively, at
18  * <http://www.OpenLDAP.org/license.html>.
19  */
20 /* ACKNOWLEDGEMENTS:
21  * This work was initially developed by the Howard Chu for inclusion
22  * in OpenLDAP Software and subsequently enhanced by Pierangelo
23  * Masarati.
24  */
25 
26 #include "portable.h"
27 
28 #include <stdio.h>
29 
30 #include <ac/socket.h>
31 #include <ac/string.h>
32 
33 #include "slap.h"
34 #include "back-ldap.h"
35 
36 int
37 ldap_back_modrdn(
38 		Operation	*op,
39  		SlapReply	*rs )
40 {
41 	ldapinfo_t		*li = (ldapinfo_t *)op->o_bd->be_private;
42 
43 	ldapconn_t		*lc = NULL;
44 	ber_int_t		msgid;
45 	LDAPControl		**ctrls = NULL;
46 	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
47 	int			rc = LDAP_SUCCESS;
48 	char			*newSup = NULL;
49 	struct berval		newrdn = BER_BVNULL;
50 
51 	if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
52 		return rs->sr_err;
53 	}
54 
55 	if ( op->orr_newSup ) {
56 		/* needs LDAPv3 */
57 		switch ( li->li_version ) {
58 		case LDAP_VERSION3:
59 			break;
60 
61 		case 0:
62 			if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
63 				break;
64 			}
65 			/* fall thru */
66 
67 		default:
68 			/* op->o_protocol cannot be anything but LDAPv3,
69 			 * otherwise wouldn't be here */
70 			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
71 			send_ldap_result( op, rs );
72 			goto cleanup;
73 		}
74 
75 		newSup = op->orr_newSup->bv_val;
76 	}
77 
78 	/* NOTE: we need to copy the newRDN in case it was formed
79 	 * from a DN by simply changing the length (ITS#5397) */
80 	newrdn = op->orr_newrdn;
81 	if ( newrdn.bv_val[ newrdn.bv_len ] != '\0' ) {
82 		ber_dupbv_x( &newrdn, &op->orr_newrdn, op->o_tmpmemctx );
83 	}
84 
85 retry:
86 	ctrls = op->o_ctrls;
87 	rc = ldap_back_controls_add( op, rs, lc, &ctrls );
88 	if ( rc != LDAP_SUCCESS ) {
89 		send_ldap_result( op, rs );
90 		rc = -1;
91 		goto cleanup;
92 	}
93 
94 	rs->sr_err = ldap_rename( lc->lc_ld, op->o_req_dn.bv_val,
95 			newrdn.bv_val, newSup,
96 			op->orr_deleteoldrdn, ctrls, NULL, &msgid );
97 	rc = ldap_back_op_result( lc, op, rs, msgid,
98 		li->li_timeout[ SLAP_OP_MODRDN ],
99 		( LDAP_BACK_SENDRESULT | retrying ) );
100 	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
101 		retrying &= ~LDAP_BACK_RETRYING;
102 		if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
103 			/* if the identity changed, there might be need to re-authz */
104 			(void)ldap_back_controls_free( op, rs, &ctrls );
105 			goto retry;
106 		}
107 	}
108 
109 	ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
110 	ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_MODRDN ], 1 );
111 	ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
112 
113 cleanup:
114 	(void)ldap_back_controls_free( op, rs, &ctrls );
115 
116 	if ( newrdn.bv_val != op->orr_newrdn.bv_val ) {
117 		op->o_tmpfree( newrdn.bv_val, op->o_tmpmemctx );
118 	}
119 
120 	if ( lc != NULL ) {
121 		ldap_back_release_conn( li, lc );
122 	}
123 
124 	return rc;
125 }
126 
127