]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/modrdn.c
Import ITS#4158 fixes from HEAD
[openldap] / servers / slapd / back-sql / modrdn.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_SQL
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "slap.h"
30 #include "ldap_pvt.h"
31 #include "proto-sql.h"
32
33 int
34 backsql_modrdn( Operation *op, SlapReply *rs )
35 {
36         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
37         SQLHDBC                 dbh;
38         SQLHSTMT                sth;
39         RETCODE                 rc;
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;
49         Entry                   e;
50         Modifications           *mod = NULL;
51         struct berval           *newSuperior = op->oq_modrdn.rs_newSup;
52         char                    *next;
53  
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", 
62                         0, 0, 0 );
63                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
64                         ?  "SQL-backend error" : NULL;
65                 send_ldap_result( op, rs );
66                 return 1;
67         }
68
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",
73                         rs->sr_err, 0, 0 );
74                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
75                         ?  "SQL-backend error" : NULL;
76                 send_ldap_result( op, rs );
77                 return 1;
78         }
79
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",
85                 e_id.eid_id, 0, 0 );
86 #endif /* ! BACKSQL_ARBITRARY_KEY */
87
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 );
95                 return 1;
96         }
97
98         dnParent( &op->o_req_dn, &p_dn );
99         dnParent( &op->o_req_ndn, &p_ndn );
100
101         /*
102          * namingContext "" is not supported
103          */
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 );
110                 goto modrdn_return;
111         }
112
113         /*
114          * Check for children access to parent
115          */
116         e.e_attrs = NULL;
117         e.e_name = p_dn;
118         e.e_nname = p_ndn;
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;
123                 goto modrdn_return;
124         }
125
126         if ( newSuperior ) {
127                 /*
128                  * namingContext "" is not supported
129                  */
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 );
136                         goto modrdn_return;
137                 }
138
139                 new_pdn = newSuperior;
140                 new_npdn = op->oq_modrdn.rs_nnewSup;
141
142                 e.e_name = *new_pdn;
143                 e.e_nname = *new_npdn;
144
145                 /*
146                  * Check for children access to new parent
147                  */
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;
154                         goto modrdn_return;
155                 }
156
157         } else {
158                 new_pdn = &p_dn;
159                 new_npdn = &p_ndn;
160         }
161
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",
165                         0, 0, 0 );
166                 newSuperior = NULL;
167         }
168
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 );
176                 goto modrdn_return;
177         }
178
179         build_new_dn( &new_dn, new_pdn, &op->oq_modrdn.rs_newrdn,
180                         op->o_tmpmemctx );
181         rs->sr_err = dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn,
182                         op->o_tmpmemctx );
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 );
189                 goto modrdn_return;
190         }
191         
192         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): new entry dn is \"%s\"\n",
193                         new_dn.bv_val, 0, 0 );
194
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 );
202                 goto modrdn_return;
203         }
204
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 */
212
213         backsql_free_entryID( &pe_id, 0 );
214
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 );
222                 goto modrdn_return;
223         }
224
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 */
232
233  
234         Debug(  LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
235                 "executing delentry_query\n", 0, 0, 0 );
236
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, 
243                                 sth, rc );
244
245                 rs->sr_text = "SQL-backend error";
246                 rs->sr_err = LDAP_OTHER;
247                 goto done;
248         }
249
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",
261                         0, 0, 0 );
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 );
267                 goto done;
268         }
269
270         SQLFreeStmt( sth, SQL_DROP );
271
272         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
273                 "executing insentry_query\n", 0, 0, 0 );
274
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, 
281                                 sth, rc );
282
283                 rs->sr_text = "SQL-backend error";
284                 rs->sr_err = LDAP_OTHER;
285                 goto done;
286         }
287
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 );
311                 goto done;
312         }
313         SQLFreeStmt( sth, SQL_DROP );
314
315         /*
316          * Get attribute type and attribute value of our new rdn,
317          * we will need to add that to our new entry
318          */
319         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, &next, 
320                                 LDAP_DN_FORMAT_LDAP ) )
321         {
322                 Debug( LDAP_DEBUG_TRACE,
323                         "   backsql_modrdn: can't figure out "
324                         "type(s)/values(s) of newrdn\n", 
325                         0, 0, 0 );
326                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
327                 goto done;
328         }
329
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 );
335
336         if ( op->oq_modrdn.rs_deleteoldrdn ) {
337                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, &next,
338                                         LDAP_DN_FORMAT_LDAP ) )
339                 {
340                         Debug( LDAP_DEBUG_TRACE,
341                                 "   backsql_modrdn: can't figure out "
342                                 "the old_rdn type(s)/value(s)\n", 
343                                 0, 0, 0 );
344                         rs->sr_err = LDAP_OTHER;
345                         goto done;              
346                 }
347         }
348
349         e.e_name = new_dn;
350         e.e_nname = new_ndn;
351         rs->sr_err = slap_modrdn2mods( op, rs, &e, old_rdn, new_rdn, &mod );
352         if ( rs->sr_err != LDAP_SUCCESS ) {
353                 goto modrdn_return;
354         }
355
356         if ( !acl_check_modlist( op, &e, mod )) {
357                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
358                 goto modrdn_return;
359         }
360
361         oc = backsql_id2oc( bi, e_id.eid_oc_id );
362         rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, mod );
363
364 done:;
365         /*
366          * Commit only if all operations succeed
367          */
368         if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
369                 SQLTransact( SQL_NULL_HENV, dbh, SQL_COMMIT );
370
371         } else {
372                 SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK );
373         }
374
375 modrdn_return:;
376         if ( !BER_BVISNULL( &new_dn ) ) {
377                 slap_sl_free( new_dn.bv_val, op->o_tmpmemctx );
378         }
379         
380         if ( !BER_BVISNULL( &new_ndn ) ) {
381                 slap_sl_free( new_ndn.bv_val, op->o_tmpmemctx );
382         }
383         
384         /* LDAP v2 supporting correct attribute handling. */
385         if ( new_rdn != NULL ) {
386                 ldap_rdnfree( new_rdn );
387         }
388         if ( old_rdn != NULL ) {
389                 ldap_rdnfree( old_rdn );
390         }
391         if ( mod != NULL ) {
392                 Modifications *tmp;
393                 for (; mod; mod = tmp ) {
394                         tmp = mod->sml_next;
395                         free( mod );
396                 }
397         }
398
399         if ( new_pe_id.eid_dn.bv_val ) {
400                 backsql_free_entryID( &new_pe_id, 0 );
401         }
402
403         send_ldap_result( op, rs );
404
405         Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n", 0, 0, 0 );
406         return op->o_noop;
407 }
408
409 #endif /* SLAPD_SQL */
410