2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include <sys/types.h>
27 #include "ac/string.h"
31 #include "proto-sql.h"
34 backsql_modrdn( Operation *op, SlapReply *rs )
36 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
40 backsql_entryID e_id = BACKSQL_ENTRYID_INIT,
41 pe_id = BACKSQL_ENTRYID_INIT,
42 new_pe_id = BACKSQL_ENTRYID_INIT;
43 backsql_oc_map_rec *oc = NULL;
44 struct berval p_dn = BER_BVNULL, p_ndn = BER_BVNULL,
45 *new_pdn = NULL, *new_npdn = NULL,
46 new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
47 LDAPRDN new_rdn = NULL;
48 LDAPRDN old_rdn = NULL;
50 Modifications *mod = NULL;
51 struct berval *newSuperior = op->oq_modrdn.rs_newSup;
54 Debug( LDAP_DEBUG_TRACE, "==>backsql_modrdn() renaming entry \"%s\", "
55 "newrdn=\"%s\", newSuperior=\"%s\"\n",
56 op->o_req_dn.bv_val, op->oq_modrdn.rs_newrdn.bv_val,
57 newSuperior ? newSuperior->bv_val : "(NULL)" );
58 rs->sr_err = backsql_get_db_conn( op, &dbh );
59 if ( rs->sr_err != LDAP_SUCCESS ) {
60 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
61 "could not get connection handle - exiting\n",
63 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
64 ? "SQL-backend error" : NULL;
65 send_ldap_result( op, rs );
69 rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
70 if ( rs->sr_err != LDAP_SUCCESS ) {
71 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
72 "could not lookup entry id (%d)\n",
74 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
75 ? "SQL-backend error" : NULL;
76 send_ldap_result( op, rs );
80 #ifdef BACKSQL_ARBITRARY_KEY
81 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): entry id=%s\n",
82 e_id.eid_id.bv_val, 0, 0 );
83 #else /* ! BACKSQL_ARBITRARY_KEY */
84 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): entry id=%ld\n",
86 #endif /* ! BACKSQL_ARBITRARY_KEY */
88 if ( backsql_has_children( bi, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
89 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
90 "entry \"%s\" has children\n",
91 op->o_req_dn.bv_val, 0, 0 );
92 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
93 rs->sr_text = "subtree rename not supported";
94 send_ldap_result( op, rs );
98 dnParent( &op->o_req_dn, &p_dn );
99 dnParent( &op->o_req_ndn, &p_ndn );
102 * namingContext "" is not supported
104 if ( p_dn.bv_len == 0 ) {
105 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
106 "parent is \"\" - aborting\n", 0, 0, 0 );
107 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
108 rs->sr_text = "not allowed within namingContext";
109 send_ldap_result( op, rs );
114 * Check for children access to parent
119 if ( !access_allowed( op, &e, slap_schema.si_ad_children,
120 NULL, ACL_WRITE, NULL ) ) {
121 Debug( LDAP_DEBUG_TRACE, " no access to parent\n", 0, 0, 0 );
122 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
128 * namingContext "" is not supported
130 if ( newSuperior->bv_len == 0 ) {
131 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
132 "newSuperior is \"\" - aborting\n", 0, 0, 0 );
133 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
134 rs->sr_text = "not allowed within namingContext";
135 send_ldap_result( op, rs );
139 new_pdn = newSuperior;
140 new_npdn = op->oq_modrdn.rs_nnewSup;
143 e.e_nname = *new_npdn;
146 * Check for children access to new parent
148 if ( !access_allowed( op, &e, slap_schema.si_ad_children,
149 NULL, ACL_WRITE, NULL ) ) {
150 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
151 "no access to new parent \"%s\"\n",
152 new_pdn->bv_val, 0, 0 );
153 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
162 if ( newSuperior && dn_match( &p_ndn, new_npdn ) ) {
163 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
164 "newSuperior is equal to old parent - ignored\n",
169 if ( newSuperior && dn_match( &op->o_req_ndn, new_npdn ) ) {
170 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
171 "newSuperior is equal to entry being moved "
172 "- aborting\n", 0, 0, 0 );
173 rs->sr_err = LDAP_OTHER;
174 rs->sr_text = "newSuperior is equal to old DN";
175 send_ldap_result( op, rs );
179 build_new_dn( &new_dn, new_pdn, &op->oq_modrdn.rs_newrdn,
181 rs->sr_err = dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn,
183 if ( rs->sr_err != LDAP_SUCCESS ) {
184 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
185 "new dn is invalid (\"%s\") - aborting\n",
186 new_dn.bv_val, 0, 0 );
187 rs->sr_text = "unable to build new DN";
188 send_ldap_result( op, rs );
192 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): new entry dn is \"%s\"\n",
193 new_dn.bv_val, 0, 0 );
195 rs->sr_err = backsql_dn2id( bi, &pe_id, dbh, &p_ndn );
196 if ( rs->sr_err != LDAP_SUCCESS ) {
197 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
198 "could not lookup old parent entry id\n", 0, 0, 0 );
199 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
200 ? "SQL-backend error" : NULL;
201 send_ldap_result( op, rs );
205 #ifdef BACKSQL_ARBITRARY_KEY
206 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
207 "old parent entry id is %s\n", pe_id.eid_id.bv_val, 0, 0 );
208 #else /* ! BACKSQL_ARBITRARY_KEY */
209 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
210 "old parent entry id is %ld\n", pe_id.eid_id, 0, 0 );
211 #endif /* ! BACKSQL_ARBITRARY_KEY */
213 backsql_free_entryID( &pe_id, 0 );
215 rs->sr_err = backsql_dn2id( bi, &new_pe_id, dbh, new_npdn );
216 if ( rs->sr_err != LDAP_SUCCESS ) {
217 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
218 "could not lookup new parent entry id\n", 0, 0, 0 );
219 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
220 ? "SQL-backend error" : NULL;
221 send_ldap_result( op, rs );
225 #ifdef BACKSQL_ARBITRARY_KEY
226 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
227 "new parent entry id=%s\n", new_pe_id.eid_id.bv_val, 0, 0 );
228 #else /* ! BACKSQL_ARBITRARY_KEY */
229 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
230 "new parent entry id=%ld\n", new_pe_id.eid_id, 0, 0 );
231 #endif /* ! BACKSQL_ARBITRARY_KEY */
234 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
235 "executing delentry_query\n", 0, 0, 0 );
237 rc = backsql_Prepare( dbh, &sth, bi->delentry_query, 0 );
238 if ( rc != SQL_SUCCESS ) {
239 Debug( LDAP_DEBUG_TRACE,
240 " backsql_modrdn(): "
241 "error preparing delentry_query\n", 0, 0, 0 );
242 backsql_PrintErrors( bi->db_env, dbh,
245 rs->sr_text = "SQL-backend error";
246 rs->sr_err = LDAP_OTHER;
250 #ifdef BACKSQL_ARBITRARY_KEY
251 SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
252 0, 0, e_id.eid_id.bv_val, 0, 0 );
253 #else /* ! BACKSQL_ARBITRARY_KEY */
254 SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
255 0, 0, &e_id.eid_id, 0, 0 );
256 #endif /* ! BACKSQL_ARBITRARY_KEY */
257 rc = SQLExecute( sth );
258 if ( rc != SQL_SUCCESS ) {
259 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
260 "failed to delete record from ldap_entries\n",
262 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
263 SQLFreeStmt( sth, SQL_DROP );
264 rs->sr_err = LDAP_OTHER;
265 rs->sr_text = "SQL-backend error";
266 send_ldap_result( op, rs );
270 SQLFreeStmt( sth, SQL_DROP );
272 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
273 "executing insentry_query\n", 0, 0, 0 );
275 rc = backsql_Prepare( dbh, &sth, bi->insentry_query, 0 );
276 if ( rc != SQL_SUCCESS ) {
277 Debug( LDAP_DEBUG_TRACE,
278 " backsql_modrdn(): "
279 "error preparing insentry_query\n", 0, 0, 0 );
280 backsql_PrintErrors( bi->db_env, dbh,
283 rs->sr_text = "SQL-backend error";
284 rs->sr_err = LDAP_OTHER;
288 backsql_BindParamStr( sth, 1, new_dn.bv_val, BACKSQL_MAX_DN_LEN );
289 SQLBindParameter( sth, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
290 0, 0, &e_id.eid_oc_id, 0, 0 );
291 #ifdef BACKSQL_ARBITRARY_KEY
292 SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
293 0, 0, new_pe_id.eid_id.bv_val, 0, 0 );
294 SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
295 0, 0, e_id.eid_keyval.bv_val, 0, 0 );
296 #else /* ! BACKSQL_ARBITRARY_KEY */
297 SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
298 0, 0, &new_pe_id.eid_id, 0, 0 );
299 SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
300 0, 0, &e_id.eid_keyval, 0, 0 );
301 #endif /* ! BACKSQL_ARBITRARY_KEY */
302 rc = SQLExecute( sth );
303 if ( rc != SQL_SUCCESS ) {
304 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
305 "could not insert ldap_entries record\n", 0, 0, 0 );
306 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
307 SQLFreeStmt( sth, SQL_DROP );
308 rs->sr_err = LDAP_OTHER;
309 rs->sr_text = "SQL-backend error";
310 send_ldap_result( op, rs );
313 SQLFreeStmt( sth, SQL_DROP );
316 * Get attribute type and attribute value of our new rdn,
317 * we will need to add that to our new entry
319 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, &next,
320 LDAP_DN_FORMAT_LDAP ) )
322 Debug( LDAP_DEBUG_TRACE,
323 " backsql_modrdn: can't figure out "
324 "type(s)/values(s) of newrdn\n",
326 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
330 Debug( LDAP_DEBUG_TRACE,
331 " backsql_modrdn: new_rdn_type=\"%s\", "
332 "new_rdn_val=\"%s\"\n",
333 new_rdn[ 0 ]->la_attr.bv_val,
334 new_rdn[ 0 ]->la_value.bv_val, 0 );
336 if ( op->oq_modrdn.rs_deleteoldrdn ) {
337 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, &next,
338 LDAP_DN_FORMAT_LDAP ) )
340 Debug( LDAP_DEBUG_TRACE,
341 " backsql_modrdn: can't figure out "
342 "the old_rdn type(s)/value(s)\n",
344 rs->sr_err = LDAP_OTHER;
351 rs->sr_err = slap_modrdn2mods( op, rs, &e, old_rdn, new_rdn, &mod );
352 if ( rs->sr_err != LDAP_SUCCESS ) {
356 if ( !acl_check_modlist( op, &e, mod )) {
357 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
361 oc = backsql_id2oc( bi, e_id.eid_oc_id );
362 rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, mod );
366 * Commit only if all operations succeed
368 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
369 SQLTransact( SQL_NULL_HENV, dbh, SQL_COMMIT );
372 SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK );
376 if ( !BER_BVISNULL( &new_dn ) ) {
377 slap_sl_free( new_dn.bv_val, op->o_tmpmemctx );
380 if ( !BER_BVISNULL( &new_ndn ) ) {
381 slap_sl_free( new_ndn.bv_val, op->o_tmpmemctx );
384 /* LDAP v2 supporting correct attribute handling. */
385 if ( new_rdn != NULL ) {
386 ldap_rdnfree( new_rdn );
388 if ( old_rdn != NULL ) {
389 ldap_rdnfree( old_rdn );
393 for (; mod; mod = tmp ) {
399 if ( new_pe_id.eid_dn.bv_val ) {
400 backsql_free_entryID( &new_pe_id, 0 );
403 send_ldap_result( op, rs );
405 Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n", 0, 0, 0 );
409 #endif /* SLAPD_SQL */