]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/modrdn.c
c17f90415b3d617bfc5066dc131f567a0408275f
[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-2004 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\n", 0, 0, 0 );
73                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
74                         ?  "SQL-backend error" : NULL;
75                 send_ldap_result( op, rs );
76                 return 1;
77         }
78
79 #ifdef BACKSQL_ARBITRARY_KEY
80         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): entry id=%s\n",
81                 e_id.eid_id.bv_val, 0, 0 );
82 #else /* ! BACKSQL_ARBITRARY_KEY */
83         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): entry id=%ld\n",
84                 e_id.eid_id, 0, 0 );
85 #endif /* ! BACKSQL_ARBITRARY_KEY */
86
87         if ( backsql_has_children( bi, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
88                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
89                         "entry \"%s\" has children\n",
90                         op->o_req_dn.bv_val, 0, 0 );
91                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
92                 rs->sr_text = "subtree rename not supported";
93                 send_ldap_result( op, rs );
94                 return 1;
95         }
96
97         dnParent( &op->o_req_dn, &p_dn );
98         dnParent( &op->o_req_ndn, &p_ndn );
99
100         /*
101          * namingContext "" is not supported
102          */
103         if ( p_dn.bv_len == 0 ) {
104                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
105                         "parent is \"\" - aborting\n", 0, 0, 0 );
106                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
107                 rs->sr_text = "not allowed within namingContext";
108                 send_ldap_result( op, rs );
109                 goto modrdn_return;
110         }
111
112         /*
113          * Check for children access to parent
114          */
115         e.e_attrs = NULL;
116         e.e_name = p_dn;
117         e.e_nname = p_ndn;
118         if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
119                                 NULL, ACL_WRITE, NULL ) ) {
120                 Debug( LDAP_DEBUG_TRACE, "   no access to parent\n", 0, 0, 0 );
121                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
122                 goto modrdn_return;
123         }
124
125         if ( newSuperior ) {
126                 /*
127                  * namingContext "" is not supported
128                  */
129                 if ( newSuperior->bv_len == 0 ) {
130                         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
131                                 "newSuperior is \"\" - aborting\n", 0, 0, 0 );
132                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
133                         rs->sr_text = "not allowed within namingContext";
134                         send_ldap_result( op, rs );
135                         goto modrdn_return;
136                 }
137
138                 new_pdn = newSuperior;
139                 new_npdn = op->oq_modrdn.rs_nnewSup;
140
141                 e.e_name = *new_pdn;
142                 e.e_nname = *new_npdn;
143
144                 /*
145                  * Check for children access to new parent
146                  */
147                 if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
148                                         NULL, ACL_WRITE, NULL ) ) {
149                         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
150                                         "no access to new parent \"%s\"\n", 
151                                         new_pdn->bv_val, 0, 0 );
152                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
153                         goto modrdn_return;
154                 }
155
156         } else {
157                 new_pdn = &p_dn;
158                 new_npdn = &p_ndn;
159         }
160
161         if ( newSuperior && dn_match( &p_ndn, new_npdn ) ) {
162                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
163                         "newSuperior is equal to old parent - ignored\n",
164                         0, 0, 0 );
165                 newSuperior = NULL;
166         }
167
168         if ( newSuperior && dn_match( &op->o_req_ndn, new_npdn ) ) {
169                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
170                         "newSuperior is equal to entry being moved "
171                         "- aborting\n", 0, 0, 0 );
172                 rs->sr_err = LDAP_OTHER;
173                 rs->sr_text = "newSuperior is equal to old DN";
174                 send_ldap_result( op, rs );
175                 goto modrdn_return;
176         }
177
178         build_new_dn( &new_dn, new_pdn, &op->oq_modrdn.rs_newrdn,
179                         op->o_tmpmemctx );
180         rs->sr_err = dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn,
181                         op->o_tmpmemctx );
182         if ( rs->sr_err != LDAP_SUCCESS ) {
183                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
184                         "new dn is invalid (\"%s\") - aborting\n",
185                         new_dn.bv_val, 0, 0 );
186                 rs->sr_text = "unable to build new DN";
187                 send_ldap_result( op, rs );
188                 goto modrdn_return;
189         }
190         
191         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): new entry dn is \"%s\"\n",
192                         new_dn.bv_val, 0, 0 );
193
194         rs->sr_err = backsql_dn2id( bi, &pe_id, dbh, &p_ndn );
195         if ( rs->sr_err != LDAP_SUCCESS ) {
196                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
197                         "could not lookup old parent entry id\n", 0, 0, 0 );
198                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
199                         ? "SQL-backend error" : NULL;
200                 send_ldap_result( op, rs );
201                 goto modrdn_return;
202         }
203
204 #ifdef BACKSQL_ARBITRARY_KEY
205         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
206                 "old parent entry id is %s\n", pe_id.eid_id.bv_val, 0, 0 );
207 #else /* ! BACKSQL_ARBITRARY_KEY */
208         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
209                 "old parent entry id is %ld\n", pe_id.eid_id, 0, 0 );
210 #endif /* ! BACKSQL_ARBITRARY_KEY */
211
212         backsql_free_entryID( &pe_id, 0 );
213
214         rs->sr_err = backsql_dn2id( bi, &new_pe_id, dbh, new_npdn );
215         if ( rs->sr_err != LDAP_SUCCESS ) {
216                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
217                         "could not lookup new parent entry id\n", 0, 0, 0 );
218                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
219                         ? "SQL-backend error" : NULL;
220                 send_ldap_result( op, rs );
221                 goto modrdn_return;
222         }
223
224 #ifdef BACKSQL_ARBITRARY_KEY
225         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
226                 "new parent entry id=%s\n", new_pe_id.eid_id.bv_val, 0, 0 );
227 #else /* ! BACKSQL_ARBITRARY_KEY */
228         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
229                 "new parent entry id=%ld\n", new_pe_id.eid_id, 0, 0 );
230 #endif /* ! BACKSQL_ARBITRARY_KEY */
231
232  
233         Debug(  LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
234                 "executing delentry_query\n", 0, 0, 0 );
235
236         rc = backsql_Prepare( dbh, &sth, bi->delentry_query, 0 );
237         if ( rc != SQL_SUCCESS ) {
238                 Debug( LDAP_DEBUG_TRACE,
239                         "   backsql_modrdn(): "
240                         "error preparing delentry_query\n", 0, 0, 0 );
241                 backsql_PrintErrors( bi->db_env, dbh, 
242                                 sth, rc );
243
244                 rs->sr_text = "SQL-backend error";
245                 rs->sr_err = LDAP_OTHER;
246                 goto done;
247         }
248
249 #ifdef BACKSQL_ARBITRARY_KEY
250         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
251                         0, 0, e_id.eid_id.bv_val, 0, 0 );
252 #else /* ! BACKSQL_ARBITRARY_KEY */
253         SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
254                         0, 0, &e_id.eid_id, 0, 0 );
255 #endif /* ! BACKSQL_ARBITRARY_KEY */
256         rc = SQLExecute( sth );
257         if ( rc != SQL_SUCCESS ) {
258                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
259                         "failed to delete record from ldap_entries\n",
260                         0, 0, 0 );
261                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
262                 SQLFreeStmt( sth, SQL_DROP );
263                 rs->sr_err = LDAP_OTHER;
264                 rs->sr_text = "SQL-backend error";
265                 send_ldap_result( op, rs );
266                 goto done;
267         }
268
269         SQLFreeStmt( sth, SQL_DROP );
270
271         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
272                 "executing insentry_query\n", 0, 0, 0 );
273
274         rc = backsql_Prepare( dbh, &sth, bi->insentry_query, 0 );
275         if ( rc != SQL_SUCCESS ) {
276                 Debug( LDAP_DEBUG_TRACE,
277                         "   backsql_modrdn(): "
278                         "error preparing insentry_query\n", 0, 0, 0 );
279                 backsql_PrintErrors( bi->db_env, dbh, 
280                                 sth, rc );
281
282                 rs->sr_text = "SQL-backend error";
283                 rs->sr_err = LDAP_OTHER;
284                 goto done;
285         }
286
287         backsql_BindParamStr( sth, 1, new_dn.bv_val, BACKSQL_MAX_DN_LEN );
288         SQLBindParameter( sth, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
289                         0, 0, &e_id.eid_oc_id, 0, 0 );
290 #ifdef BACKSQL_ARBITRARY_KEY
291         SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
292                         0, 0, new_pe_id.eid_id.bv_val, 0, 0 );
293         SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR,
294                         0, 0, e_id.eid_keyval.bv_val, 0, 0 );
295 #else /* ! BACKSQL_ARBITRARY_KEY */
296         SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
297                         0, 0, &new_pe_id.eid_id, 0, 0 );
298         SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
299                         0, 0, &e_id.eid_keyval, 0, 0 );
300 #endif /* ! BACKSQL_ARBITRARY_KEY */
301         rc = SQLExecute( sth );
302         if ( rc != SQL_SUCCESS ) {
303                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
304                         "could not insert ldap_entries record\n", 0, 0, 0 );
305                 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
306                 SQLFreeStmt( sth, SQL_DROP );
307                 rs->sr_err = LDAP_OTHER;
308                 rs->sr_text = "SQL-backend error";
309                 send_ldap_result( op, rs );
310                 goto done;
311         }
312         SQLFreeStmt( sth, SQL_DROP );
313
314         /*
315          * Get attribute type and attribute value of our new rdn,
316          * we will need to add that to our new entry
317          */
318         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, &next, 
319                                 LDAP_DN_FORMAT_LDAP ) )
320         {
321                 Debug( LDAP_DEBUG_TRACE,
322                         "   backsql_modrdn: can't figure out "
323                         "type(s)/values(s) of newrdn\n", 
324                         0, 0, 0 );
325                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
326                 goto done;
327         }
328
329         Debug( LDAP_DEBUG_TRACE,
330                 "   backsql_modrdn: new_rdn_type=\"%s\", "
331                 "new_rdn_val=\"%s\"\n",
332                 new_rdn[ 0 ]->la_attr.bv_val,
333                 new_rdn[ 0 ]->la_value.bv_val, 0 );
334
335         if ( op->oq_modrdn.rs_deleteoldrdn ) {
336                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, &next,
337                                         LDAP_DN_FORMAT_LDAP ) )
338                 {
339                         Debug( LDAP_DEBUG_TRACE,
340                                 "   backsql_modrdn: can't figure out "
341                                 "the old_rdn type(s)/value(s)\n", 
342                                 0, 0, 0 );
343                         rs->sr_err = LDAP_OTHER;
344                         goto done;              
345                 }
346         }
347
348         e.e_name = new_dn;
349         e.e_nname = new_ndn;
350         rs->sr_err = slap_modrdn2mods( op, rs, &e, old_rdn, new_rdn, &mod );
351         if ( rs->sr_err != LDAP_SUCCESS ) {
352                 goto modrdn_return;
353         }
354
355         if ( !acl_check_modlist( op, &e, mod )) {
356                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
357                 goto modrdn_return;
358         }
359
360         oc = backsql_id2oc( bi, e_id.eid_oc_id );
361         rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, mod );
362
363 done:;
364         /*
365          * Commit only if all operations succeed
366          */
367         if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
368                 SQLTransact( SQL_NULL_HENV, dbh, SQL_COMMIT );
369
370         } else {
371                 SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK );
372         }
373
374 modrdn_return:;
375         if ( !BER_BVISNULL( &new_dn ) ) {
376                 slap_sl_free( new_dn.bv_val, op->o_tmpmemctx );
377         }
378         
379         if ( !BER_BVISNULL( &new_ndn ) ) {
380                 slap_sl_free( new_ndn.bv_val, op->o_tmpmemctx );
381         }
382         
383         /* LDAP v2 supporting correct attribute handling. */
384         if ( new_rdn != NULL ) {
385                 ldap_rdnfree( new_rdn );
386         }
387         if ( old_rdn != NULL ) {
388                 ldap_rdnfree( old_rdn );
389         }
390         if ( mod != NULL ) {
391                 Modifications *tmp;
392                 for (; mod; mod = tmp ) {
393                         tmp = mod->sml_next;
394                         free( mod );
395                 }
396         }
397
398         if ( new_pe_id.eid_dn.bv_val ) {
399                 backsql_free_entryID( &new_pe_id, 0 );
400         }
401
402         send_ldap_result( op, rs );
403
404         Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n", 0, 0, 0 );
405         return op->o_noop;
406 }
407
408 #endif /* SLAPD_SQL */
409