xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-ldap/modrdn.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: modrdn.c,v 1.3 2021/08/14 16:14:59 christos 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-2021 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 <sys/cdefs.h>
27 __RCSID("$NetBSD: modrdn.c,v 1.3 2021/08/14 16:14:59 christos Exp $");
28 
29 #include "portable.h"
30 
31 #include <stdio.h>
32 
33 #include <ac/socket.h>
34 #include <ac/string.h>
35 
36 #include "slap.h"
37 #include "back-ldap.h"
38 
39 int
ldap_back_modrdn(Operation * op,SlapReply * rs)40 ldap_back_modrdn(
41 		Operation	*op,
42  		SlapReply	*rs )
43 {
44 	ldapinfo_t		*li = (ldapinfo_t *)op->o_bd->be_private;
45 
46 	ldapconn_t		*lc = NULL;
47 	ber_int_t		msgid;
48 	LDAPControl		**ctrls = NULL;
49 	ldap_back_send_t	retrying = LDAP_BACK_RETRYING;
50 	int			rc = LDAP_SUCCESS;
51 	char			*newSup = NULL;
52 	struct berval		newrdn = BER_BVNULL;
53 
54 	if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
55 		return rs->sr_err;
56 	}
57 
58 	if ( op->orr_newSup ) {
59 		/* needs LDAPv3 */
60 		switch ( li->li_version ) {
61 		case LDAP_VERSION3:
62 			break;
63 
64 		case 0:
65 			if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
66 				break;
67 			}
68 			/* fall thru */
69 
70 		default:
71 			/* op->o_protocol cannot be anything but LDAPv3,
72 			 * otherwise wouldn't be here */
73 			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
74 			send_ldap_result( op, rs );
75 			goto cleanup;
76 		}
77 
78 		newSup = op->orr_newSup->bv_val;
79 	}
80 
81 	/* NOTE: we need to copy the newRDN in case it was formed
82 	 * from a DN by simply changing the length (ITS#5397) */
83 	newrdn = op->orr_newrdn;
84 	if ( newrdn.bv_val[ newrdn.bv_len ] != '\0' ) {
85 		ber_dupbv_x( &newrdn, &op->orr_newrdn, op->o_tmpmemctx );
86 	}
87 
88 retry:
89 	ctrls = op->o_ctrls;
90 	rc = ldap_back_controls_add( op, rs, lc, &ctrls );
91 	if ( rc != LDAP_SUCCESS ) {
92 		send_ldap_result( op, rs );
93 		goto cleanup;
94 	}
95 
96 	rs->sr_err = ldap_rename( lc->lc_ld, op->o_req_dn.bv_val,
97 			newrdn.bv_val, newSup,
98 			op->orr_deleteoldrdn, ctrls, NULL, &msgid );
99 	rc = ldap_back_op_result( lc, op, rs, msgid,
100 		li->li_timeout[ SLAP_OP_MODRDN ],
101 		( LDAP_BACK_SENDRESULT | retrying ) );
102 	if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
103 		retrying &= ~LDAP_BACK_RETRYING;
104 		if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
105 			/* if the identity changed, there might be need to re-authz */
106 			(void)ldap_back_controls_free( op, rs, &ctrls );
107 			goto retry;
108 		}
109 	}
110 
111 	ldap_pvt_thread_mutex_lock( &li->li_counter_mutex );
112 	ldap_pvt_mp_add( li->li_ops_completed[ SLAP_OP_MODRDN ], 1 );
113 	ldap_pvt_thread_mutex_unlock( &li->li_counter_mutex );
114 
115 cleanup:
116 	(void)ldap_back_controls_free( op, rs, &ctrls );
117 
118 	if ( newrdn.bv_val != op->orr_newrdn.bv_val ) {
119 		op->o_tmpfree( newrdn.bv_val, op->o_tmpmemctx );
120 	}
121 
122 	if ( lc != NULL ) {
123 		ldap_back_release_conn( li, lc );
124 	}
125 
126 	return rs->sr_err;
127 }
128 
129