]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/modrdn.c
more on DN munging; now works for all operations
[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 #include <stdio.h>
24 #include <sys/types.h>
25 #include "ac/string.h"
26
27 #include "slap.h"
28 #include "proto-sql.h"
29
30 int
31 backsql_modrdn( Operation *op, SlapReply *rs )
32 {
33         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
34         SQLHDBC                 dbh;
35         SQLHSTMT                sth;
36         RETCODE                 rc;
37         backsql_entryID         e_id = BACKSQL_ENTRYID_INIT,
38                                 pe_id = BACKSQL_ENTRYID_INIT,
39                                 new_pe_id = BACKSQL_ENTRYID_INIT;
40         backsql_oc_map_rec      *oc = NULL;
41         struct berval           p_dn = BER_BVNULL, p_ndn = BER_BVNULL,
42                                 *new_pdn = NULL, *new_npdn = NULL,
43                                 new_dn = BER_BVNULL, new_ndn = BER_BVNULL,
44                                 realnew_dn = BER_BVNULL;
45         LDAPRDN                 new_rdn = NULL;
46         LDAPRDN                 old_rdn = NULL;
47         Entry                   e;
48         Modifications           *mod = NULL;
49         struct berval           *newSuperior = op->oq_modrdn.rs_newSup;
50         char                    *next;
51  
52         Debug( LDAP_DEBUG_TRACE, "==>backsql_modrdn() renaming entry \"%s\", "
53                         "newrdn=\"%s\", newSuperior=\"%s\"\n",
54                         op->o_req_dn.bv_val, op->oq_modrdn.rs_newrdn.bv_val, 
55                         newSuperior ? newSuperior->bv_val : "(NULL)" );
56         rs->sr_err = backsql_get_db_conn( op, &dbh );
57         if ( rs->sr_err != LDAP_SUCCESS ) {
58                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
59                         "could not get connection handle - exiting\n", 
60                         0, 0, 0 );
61                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
62                         ?  "SQL-backend error" : NULL;
63                 send_ldap_result( op, rs );
64                 return 1;
65         }
66
67         rs->sr_err = backsql_dn2id( op, rs, &e_id, dbh, &op->o_req_ndn, 1 );
68         if ( rs->sr_err != LDAP_SUCCESS ) {
69                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
70                         "could not lookup entry id (%d)\n",
71                         rs->sr_err, 0, 0 );
72                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
73                         ?  "SQL-backend error" : NULL;
74                 send_ldap_result( op, rs );
75                 return 1;
76         }
77
78 #ifdef BACKSQL_ARBITRARY_KEY
79         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): entry id=%s\n",
80                 e_id.eid_id.bv_val, 0, 0 );
81 #else /* ! BACKSQL_ARBITRARY_KEY */
82         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): entry id=%ld\n",
83                 e_id.eid_id, 0, 0 );
84 #endif /* ! BACKSQL_ARBITRARY_KEY */
85
86         if ( backsql_has_children( bi, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
87                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
88                         "entry \"%s\" has children\n",
89                         op->o_req_dn.bv_val, 0, 0 );
90                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
91                 rs->sr_text = "subtree rename not supported";
92                 send_ldap_result( op, rs );
93                 return 1;
94         }
95
96         dnParent( &op->o_req_dn, &p_dn );
97         dnParent( &op->o_req_ndn, &p_ndn );
98
99         /*
100          * namingContext "" is not supported
101          */
102         if ( p_dn.bv_len == 0 ) {
103                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
104                         "parent is \"\" - aborting\n", 0, 0, 0 );
105                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
106                 rs->sr_text = "not allowed within namingContext";
107                 send_ldap_result( op, rs );
108                 goto modrdn_return;
109         }
110
111         /*
112          * Check for children access to parent
113          */
114         e.e_attrs = NULL;
115         e.e_name = p_dn;
116         e.e_nname = p_ndn;
117         if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
118                                 NULL, ACL_WRITE, NULL ) ) {
119                 Debug( LDAP_DEBUG_TRACE, "   no access to parent\n", 0, 0, 0 );
120                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
121                 goto modrdn_return;
122         }
123
124         if ( newSuperior ) {
125                 /*
126                  * namingContext "" is not supported
127                  */
128                 if ( newSuperior->bv_len == 0 ) {
129                         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
130                                 "newSuperior is \"\" - aborting\n", 0, 0, 0 );
131                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
132                         rs->sr_text = "not allowed within namingContext";
133                         send_ldap_result( op, rs );
134                         goto modrdn_return;
135                 }
136
137                 new_pdn = newSuperior;
138                 new_npdn = op->oq_modrdn.rs_nnewSup;
139
140                 e.e_name = *new_pdn;
141                 e.e_nname = *new_npdn;
142
143                 /*
144                  * Check for children access to new parent
145                  */
146                 if ( !access_allowed( op, &e, slap_schema.si_ad_children, 
147                                         NULL, ACL_WRITE, NULL ) ) {
148                         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
149                                         "no access to new parent \"%s\"\n", 
150                                         new_pdn->bv_val, 0, 0 );
151                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
152                         goto modrdn_return;
153                 }
154
155         } else {
156                 new_pdn = &p_dn;
157                 new_npdn = &p_ndn;
158         }
159
160         if ( newSuperior && dn_match( &p_ndn, new_npdn ) ) {
161                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
162                         "newSuperior is equal to old parent - ignored\n",
163                         0, 0, 0 );
164                 newSuperior = NULL;
165         }
166
167         if ( newSuperior && dn_match( &op->o_req_ndn, new_npdn ) ) {
168                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
169                         "newSuperior is equal to entry being moved "
170                         "- aborting\n", 0, 0, 0 );
171                 rs->sr_err = LDAP_OTHER;
172                 rs->sr_text = "newSuperior is equal to old DN";
173                 send_ldap_result( op, rs );
174                 goto modrdn_return;
175         }
176
177         build_new_dn( &new_dn, new_pdn, &op->oq_modrdn.rs_newrdn,
178                         op->o_tmpmemctx );
179         rs->sr_err = dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn,
180                         op->o_tmpmemctx );
181         if ( rs->sr_err != LDAP_SUCCESS ) {
182                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
183                         "new dn is invalid (\"%s\") - aborting\n",
184                         new_dn.bv_val, 0, 0 );
185                 rs->sr_text = "unable to build new DN";
186                 send_ldap_result( op, rs );
187                 goto modrdn_return;
188         }
189         
190         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): new entry dn is \"%s\"\n",
191                         new_dn.bv_val, 0, 0 );
192
193         rs->sr_err = backsql_dn2id( op, rs, &pe_id, dbh, &p_ndn, 1 );
194         if ( rs->sr_err != LDAP_SUCCESS ) {
195                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
196                         "could not lookup old parent entry id\n", 0, 0, 0 );
197                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
198                         ? "SQL-backend error" : NULL;
199                 send_ldap_result( op, rs );
200                 goto modrdn_return;
201         }
202
203 #ifdef BACKSQL_ARBITRARY_KEY
204         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
205                 "old parent entry id is %s\n", pe_id.eid_id.bv_val, 0, 0 );
206 #else /* ! BACKSQL_ARBITRARY_KEY */
207         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
208                 "old parent entry id is %ld\n", pe_id.eid_id, 0, 0 );
209 #endif /* ! BACKSQL_ARBITRARY_KEY */
210
211         (void)backsql_free_entryID( &pe_id, 0 );
212
213         rs->sr_err = backsql_dn2id( op, rs, &new_pe_id, dbh, new_npdn, 1 );
214         if ( rs->sr_err != LDAP_SUCCESS ) {
215                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
216                         "could not lookup new parent entry id\n", 0, 0, 0 );
217                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
218                         ? "SQL-backend error" : NULL;
219                 send_ldap_result( op, rs );
220                 goto modrdn_return;
221         }
222
223 #ifdef BACKSQL_ARBITRARY_KEY
224         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
225                 "new parent entry id=%s\n", new_pe_id.eid_id.bv_val, 0, 0 );
226 #else /* ! BACKSQL_ARBITRARY_KEY */
227         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
228                 "new parent entry id=%ld\n", new_pe_id.eid_id, 0, 0 );
229 #endif /* ! BACKSQL_ARBITRARY_KEY */
230
231  
232         Debug(  LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
233                 "executing delentry_query\n", 0, 0, 0 );
234
235         rc = backsql_Prepare( dbh, &sth, bi->sql_delentry_query, 0 );
236         if ( rc != SQL_SUCCESS ) {
237                 Debug( LDAP_DEBUG_TRACE,
238                         "   backsql_modrdn(): "
239                         "error preparing delentry_query\n", 0, 0, 0 );
240                 backsql_PrintErrors( bi->sql_db_env, dbh, 
241                                 sth, rc );
242
243                 rs->sr_text = "SQL-backend error";
244                 rs->sr_err = LDAP_OTHER;
245                 goto done;
246         }
247
248         rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT, &e_id.eid_id );
249         if ( rc != SQL_SUCCESS ) {
250                 Debug( LDAP_DEBUG_TRACE,
251                         "   backsql_delete(): "
252                         "error binding entry ID parameter "
253                         "for objectClass %s\n",
254                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
255                 backsql_PrintErrors( bi->sql_db_env, dbh, 
256                         sth, rc );
257                 SQLFreeStmt( sth, SQL_DROP );
258
259                 rs->sr_text = "SQL-backend error";
260                 rs->sr_err = LDAP_OTHER;
261                 goto done;
262         }
263
264         rc = SQLExecute( sth );
265         if ( rc != SQL_SUCCESS ) {
266                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
267                         "failed to delete record from ldap_entries\n",
268                         0, 0, 0 );
269                 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
270                 SQLFreeStmt( sth, SQL_DROP );
271                 rs->sr_err = LDAP_OTHER;
272                 rs->sr_text = "SQL-backend error";
273                 send_ldap_result( op, rs );
274                 goto done;
275         }
276
277         SQLFreeStmt( sth, SQL_DROP );
278
279         Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
280                 "executing insentry_query\n", 0, 0, 0 );
281
282         rc = backsql_Prepare( dbh, &sth, bi->sql_insentry_query, 0 );
283         if ( rc != SQL_SUCCESS ) {
284                 Debug( LDAP_DEBUG_TRACE,
285                         "   backsql_modrdn(): "
286                         "error preparing insentry_query\n", 0, 0, 0 );
287                 backsql_PrintErrors( bi->sql_db_env, dbh, 
288                                 sth, rc );
289
290                 rs->sr_text = "SQL-backend error";
291                 rs->sr_err = LDAP_OTHER;
292                 goto done;
293         }
294
295         realnew_dn = new_dn;
296         if ( backsql_api_dn2odbc( op, rs, &realnew_dn ) ) {
297                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(\"%s\"): "
298                         "backsql_api_dn2odbc(\"%s\") failed\n", 
299                         op->o_req_dn.bv_val, realnew_dn.bv_val, 0 );
300                 SQLFreeStmt( sth, SQL_DROP );
301
302                 rs->sr_text = "SQL-backend error";
303                 rs->sr_err = LDAP_OTHER;
304                 goto done;
305         }
306
307         rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &realnew_dn );
308         if ( rc != SQL_SUCCESS ) {
309                 Debug( LDAP_DEBUG_TRACE,
310                         "   backsql_add_attr(): "
311                         "error binding DN parameter for objectClass %s\n",
312                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
313                 backsql_PrintErrors( bi->sql_db_env, dbh, 
314                         sth, rc );
315                 SQLFreeStmt( sth, SQL_DROP );
316
317                 rs->sr_text = "SQL-backend error";
318                 rs->sr_err = LDAP_OTHER;
319                 goto done;
320         }
321
322         rc = backsql_BindParamInt( sth, 2, SQL_PARAM_INPUT, &e_id.eid_oc_id );
323         if ( rc != SQL_SUCCESS ) {
324                 Debug( LDAP_DEBUG_TRACE,
325                         "   backsql_add_attr(): "
326                         "error binding objectClass ID parameter for objectClass %s\n",
327                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
328                 backsql_PrintErrors( bi->sql_db_env, dbh, 
329                         sth, rc );
330                 SQLFreeStmt( sth, SQL_DROP );
331
332                 rs->sr_text = "SQL-backend error";
333                 rs->sr_err = LDAP_OTHER;
334                 goto done;
335         }
336
337         rc = backsql_BindParamID( sth, 3, SQL_PARAM_INPUT, &new_pe_id.eid_id );
338         if ( rc != SQL_SUCCESS ) {
339                 Debug( LDAP_DEBUG_TRACE,
340                         "   backsql_add_attr(): "
341                         "error binding parent ID parameter for objectClass %s\n",
342                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
343                 backsql_PrintErrors( bi->sql_db_env, dbh, 
344                         sth, rc );
345                 SQLFreeStmt( sth, SQL_DROP );
346
347                 rs->sr_text = "SQL-backend error";
348                 rs->sr_err = LDAP_OTHER;
349                 goto done;
350         }
351
352         rc = backsql_BindParamID( sth, 4, SQL_PARAM_INPUT, &e_id.eid_keyval );
353         if ( rc != SQL_SUCCESS ) {
354                 Debug( LDAP_DEBUG_TRACE,
355                         "   backsql_add_attr(): "
356                         "error binding entry ID parameter for objectClass %s\n",
357                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
358                 backsql_PrintErrors( bi->sql_db_env, dbh, 
359                         sth, rc );
360                 SQLFreeStmt( sth, SQL_DROP );
361
362                 rs->sr_text = "SQL-backend error";
363                 rs->sr_err = LDAP_OTHER;
364                 goto done;
365         }
366
367         rc = SQLExecute( sth );
368         if ( rc != SQL_SUCCESS ) {
369                 Debug( LDAP_DEBUG_TRACE, "   backsql_modrdn(): "
370                         "could not insert ldap_entries record\n", 0, 0, 0 );
371                 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
372                 SQLFreeStmt( sth, SQL_DROP );
373                 rs->sr_err = LDAP_OTHER;
374                 rs->sr_text = "SQL-backend error";
375                 send_ldap_result( op, rs );
376                 goto done;
377         }
378         SQLFreeStmt( sth, SQL_DROP );
379
380         /*
381          * Get attribute type and attribute value of our new rdn,
382          * we will need to add that to our new entry
383          */
384         if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, &next, 
385                                 LDAP_DN_FORMAT_LDAP ) )
386         {
387                 Debug( LDAP_DEBUG_TRACE,
388                         "   backsql_modrdn: can't figure out "
389                         "type(s)/values(s) of newrdn\n", 
390                         0, 0, 0 );
391                 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
392                 goto done;
393         }
394
395         Debug( LDAP_DEBUG_TRACE,
396                 "   backsql_modrdn: new_rdn_type=\"%s\", "
397                 "new_rdn_val=\"%s\"\n",
398                 new_rdn[ 0 ]->la_attr.bv_val,
399                 new_rdn[ 0 ]->la_value.bv_val, 0 );
400
401         if ( op->oq_modrdn.rs_deleteoldrdn ) {
402                 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, &next,
403                                         LDAP_DN_FORMAT_LDAP ) )
404                 {
405                         Debug( LDAP_DEBUG_TRACE,
406                                 "   backsql_modrdn: can't figure out "
407                                 "the old_rdn type(s)/value(s)\n", 
408                                 0, 0, 0 );
409                         rs->sr_err = LDAP_OTHER;
410                         goto done;              
411                 }
412         }
413
414         e.e_name = new_dn;
415         e.e_nname = new_ndn;
416         rs->sr_err = slap_modrdn2mods( op, rs, &e, old_rdn, new_rdn, &mod );
417         if ( rs->sr_err != LDAP_SUCCESS ) {
418                 goto modrdn_return;
419         }
420
421         if ( !acl_check_modlist( op, &e, mod )) {
422                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
423                 goto modrdn_return;
424         }
425
426         oc = backsql_id2oc( bi, e_id.eid_oc_id );
427         rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, mod );
428
429 done:;
430         /*
431          * Commit only if all operations succeed
432          */
433         if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
434                 SQLTransact( SQL_NULL_HENV, dbh, SQL_COMMIT );
435
436         } else {
437                 SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK );
438         }
439
440 modrdn_return:;
441         if ( !BER_BVISNULL( &realnew_dn ) && realnew_dn.bv_val != new_dn.bv_val ) {
442                 ch_free( realnew_dn.bv_val );
443         }
444
445         if ( !BER_BVISNULL( &new_dn ) ) {
446                 slap_sl_free( new_dn.bv_val, op->o_tmpmemctx );
447         }
448         
449         if ( !BER_BVISNULL( &new_ndn ) ) {
450                 slap_sl_free( new_ndn.bv_val, op->o_tmpmemctx );
451         }
452         
453         /* LDAP v2 supporting correct attribute handling. */
454         if ( new_rdn != NULL ) {
455                 ldap_rdnfree( new_rdn );
456         }
457         if ( old_rdn != NULL ) {
458                 ldap_rdnfree( old_rdn );
459         }
460         if ( mod != NULL ) {
461                 Modifications *tmp;
462                 for (; mod; mod = tmp ) {
463                         tmp = mod->sml_next;
464                         free( mod );
465                 }
466         }
467
468         if ( !BER_BVISNULL( &new_pe_id.eid_ndn ) ) {
469                 (void)backsql_free_entryID( &new_pe_id, 0 );
470         }
471
472         send_ldap_result( op, rs );
473
474         Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n", 0, 0, 0 );
475         return op->o_noop;
476 }
477