2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
16 #include "ac/string.h"
21 #include "schema-map.h"
26 * PostgreSQL 7.0 doesn't work without :(
28 #define BACKSQL_REALLOC_STMT
32 * - the first occurrence of objectClass, which is used
33 * to determine how to build the SQL entry (FIXME ?!?)
34 * - operational attributes
35 * empty attributes (FIXME ?!?)
37 #define backsql_attr_skip(ad,vals) \
39 ( (ad) == slap_schema.si_ad_objectClass \
40 && (vals)[ 1 ].bv_val == NULL ) \
41 || is_at_operational( (ad)->ad_type ) \
42 || ( (vals)[ 0 ].bv_val == NULL ) \
46 backsql_modify_internal(
50 backsql_oc_map_rec *oc,
51 backsql_entryID *e_id,
52 Modifications *modlist )
54 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
59 Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
60 "traversing modifications list\n", 0, 0, 0 );
62 #ifndef BACKSQL_REALLOC_STMT
63 SQLAllocStmt( dbh, &sth );
64 #endif /* BACKSQL_REALLOC_STMT */
66 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
67 AttributeDescription *ad;
68 backsql_at_map_rec *at = NULL;
69 struct berval *at_val;
72 /* first parameter no, parameter order */
74 /* procedure return code */
77 #ifdef BACKSQL_REALLOC_STMT
78 SQLAllocStmt( dbh, &sth );
79 #endif /* BACKSQL_REALLOC_STMT */
84 Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
85 "modifying attribute '%s'\n",
86 ad->ad_cname.bv_val, 0, 0 );
88 if ( backsql_attr_skip( ad, c_mod->sm_bvalues ) ) {
92 at = backsql_ad2at( oc, ad );
94 Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
95 "attribute provided is not registered "
96 "in objectClass '%s'\n",
97 ad->ad_cname.bv_val, 0, 0 );
99 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
100 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
101 rs->sr_text = "operation not permitted "
102 "within namingContext";
109 switch( c_mod->sm_op ) {
110 case LDAP_MOD_REPLACE: {
114 Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
115 "replacing values for attribute '%s'\n",
116 at->ad->ad_cname.bv_val, 0, 0 );
118 if ( at->add_proc == NULL ) {
119 Debug( LDAP_DEBUG_TRACE,
120 "backsql_modify_internal(): "
121 "add procedure is not defined "
122 "for attribute '%s' "
123 "- unable to perform replacements\n",
124 at->ad->ad_cname.bv_val, 0, 0 );
126 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
127 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
128 rs->sr_text = "operation not permitted "
129 "within namingContext";
136 if ( at->delete_proc == NULL ) {
137 Debug( LDAP_DEBUG_TRACE,
138 "backsql_modify_internal(): "
139 "delete procedure is not defined "
140 "for attribute '%s' "
142 at->ad->ad_cname.bv_val, 0, 0 );
144 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
145 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
146 rs->sr_text = "operation not permitted "
147 "within namingContext";
155 rc = backsql_Prepare( dbh, &asth, at->query, 0 );
156 if ( rc != SQL_SUCCESS ) {
157 Debug( LDAP_DEBUG_TRACE,
158 "backsql_modify_internal(): "
159 "error preparing query\n", 0, 0, 0 );
160 backsql_PrintErrors( bi->db_env, dbh,
163 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
164 rs->sr_err = LDAP_OTHER;
165 rs->sr_text = "SQL-backend error";
172 rc = backsql_BindParamID( asth, 1, &e_id->keyval );
173 if ( rc != SQL_SUCCESS ) {
174 Debug( LDAP_DEBUG_TRACE,
175 "backsql_modify_internal(): "
176 "error binding key value parameter\n",
178 backsql_PrintErrors( bi->db_env, dbh,
180 SQLFreeStmt( asth, SQL_DROP );
182 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
183 rs->sr_err = LDAP_OTHER;
184 rs->sr_text = "SQL-backend error";
191 rc = SQLExecute( asth );
192 if ( !BACKSQL_SUCCESS( rc ) ) {
193 Debug( LDAP_DEBUG_TRACE,
194 "backsql_modify_internal(): "
195 "error executing attribute query\n",
197 backsql_PrintErrors( bi->db_env, dbh,
199 SQLFreeStmt( asth, SQL_DROP );
201 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
202 rs->sr_err = LDAP_OTHER;
203 rs->sr_text = "SQL-backend error";
210 backsql_BindRowAsStrings( asth, &row );
211 rc = SQLFetch( asth );
212 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( asth ) ) {
213 for ( i = 0; i < row.ncols; i++ ) {
214 if ( BACKSQL_IS_DEL( at->expect_return ) ) {
216 SQLBindParameter(sth, 1,
224 po = ( BACKSQL_IS_DEL( at->param_order ) ) > 0;
225 SQLBindParameter( sth, pno + 1 + po,
227 SQL_C_ULONG, SQL_INTEGER,
228 0, 0, &e_id->keyval, 0, 0 );
231 * check for syntax needed here
232 * maybe need binary bind?
234 SQLBindParameter(sth, pno + 2 - po,
236 SQL_C_CHAR, SQL_CHAR,
238 strlen( row.cols[ i ] ), 0 );
240 Debug( LDAP_DEBUG_TRACE,
241 "backsql_modify_internal(): "
243 at->delete_proc, 0, 0 );
244 rc = SQLExecDirect( sth,
245 at->delete_proc, SQL_NTS );
246 if ( rc != SQL_SUCCESS ) {
247 Debug( LDAP_DEBUG_TRACE,
248 "backsql_modify_internal(): "
250 "execution failed\n",
252 backsql_PrintErrors( bi->db_env,
255 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
256 rs->sr_err = LDAP_OTHER;
257 rs->sr_text = "SQL-backend error";
261 #ifdef BACKSQL_REALLOC_STMT
262 SQLFreeStmt( sth, SQL_DROP );
263 SQLAllocStmt( dbh, &sth );
264 #endif /* BACKSQL_REALLOC_STMT */
267 backsql_FreeRow( &row );
268 SQLFreeStmt( asth, SQL_DROP );
272 * PASSTHROUGH - to add new attributes -- do NOT add break
275 case SLAP_MOD_SOFTADD:
277 if ( at->add_proc == NULL ) {
278 Debug( LDAP_DEBUG_TRACE,
279 "backsql_modify_internal(): "
280 "add procedure is not defined "
281 "for attribute '%s'\n",
282 at->ad->ad_cname.bv_val, 0, 0 );
284 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
285 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
286 rs->sr_text = "operation not permitted "
287 "within namingContext";
294 Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
295 "adding new values for attribute '%s'\n",
296 at->ad->ad_cname.bv_val, 0, 0 );
297 for ( i = 0, at_val = c_mod->sm_bvalues;
298 at_val->bv_val != NULL;
300 if ( BACKSQL_IS_ADD( at->expect_return ) ) {
302 SQLBindParameter( sth, 1,
304 SQL_C_ULONG, SQL_INTEGER,
309 po = ( BACKSQL_IS_ADD( at->param_order ) ) > 0;
310 SQLBindParameter( sth, pno + 1 + po,
312 SQL_C_ULONG, SQL_INTEGER,
313 0, 0, &e_id->keyval, 0, 0 );
316 * check for syntax needed here
317 * maybe need binary bind?
319 SQLBindParameter( sth, pno + 2 - po,
321 SQL_C_CHAR, SQL_CHAR,
322 0, 0, at_val->bv_val,
325 Debug( LDAP_DEBUG_TRACE,
326 "backsql_modify_internal(): "
328 at->add_proc, 0, 0 );
329 rc = SQLExecDirect( sth, at->add_proc,
331 if ( rc != SQL_SUCCESS ) {
332 Debug( LDAP_DEBUG_TRACE,
333 "backsql_modify_internal(): "
334 "add_proc execution failed\n",
336 backsql_PrintErrors( bi->db_env,
339 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
340 rs->sr_err = LDAP_OTHER;
341 rs->sr_text = "SQL-backend error";
345 #ifdef BACKSQL_REALLOC_STMT
346 SQLFreeStmt( sth, SQL_DROP );
347 SQLAllocStmt( dbh, &sth );
348 #endif /* BACKSQL_REALLOC_STMT */
352 case LDAP_MOD_DELETE:
353 if ( at->delete_proc == NULL ) {
354 Debug( LDAP_DEBUG_TRACE,
355 "backsql_modify_internal(): "
356 "delete procedure is not defined "
357 "for attribute '%s'\n",
358 at->ad->ad_cname.bv_val, 0, 0 );
360 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
361 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
362 rs->sr_text = "operation not permitted "
363 "within namingContext";
370 if ( c_mod->sm_bvalues == NULL ) {
371 Debug( LDAP_DEBUG_TRACE,
372 "backsql_modify_internal(): "
373 "no values given to delete "
374 "for attribute '%s' "
375 "-- deleting all values\n",
376 at->ad->ad_cname.bv_val, 0, 0 );
380 Debug( LDAP_DEBUG_TRACE, "backsql_modify_internal(): "
381 "deleting values for attribute '%s'\n",
382 at->ad->ad_cname.bv_val, 0, 0 );
384 for ( i = 0, at_val = c_mod->sm_bvalues;
385 at_val->bv_val != NULL;
387 if ( BACKSQL_IS_DEL( at->expect_return ) ) {
389 SQLBindParameter( sth, 1,
391 SQL_C_ULONG, SQL_INTEGER,
396 po = ( BACKSQL_IS_DEL( at->param_order ) ) > 0;
397 SQLBindParameter( sth, pno + 1 + po,
399 SQL_C_ULONG, SQL_INTEGER,
400 0, 0, &e_id->keyval, 0, 0 );
403 * check for syntax needed here
404 * maybe need binary bind?
406 SQLBindParameter( sth, pno + 2 - po,
407 SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
408 0, 0, at_val->bv_val,
411 Debug( LDAP_DEBUG_TRACE,
412 "backsql_modify_internal(): "
414 at->delete_proc, 0, 0 );
415 rc = SQLExecDirect( sth, at->delete_proc,
417 if ( rc != SQL_SUCCESS ) {
418 Debug( LDAP_DEBUG_TRACE,
419 "backsql_modify_internal(): "
420 "delete_proc execution "
421 "failed\n", 0, 0, 0 );
422 backsql_PrintErrors( bi->db_env,
425 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
426 rs->sr_err = LDAP_OTHER;
427 rs->sr_text = "SQL-backend error";
431 #ifdef BACKSQL_REALLOC_STMT
432 SQLFreeStmt( sth, SQL_DROP );
433 SQLAllocStmt( dbh, &sth );
434 #endif /* BACKSQL_REALLOC_STMT */
438 #ifndef BACKSQL_REALLOC_STMT
439 SQLFreeStmt( sth, SQL_RESET_PARAMS );
440 #else /* BACKSQL_REALLOC_STMT */
441 SQLFreeStmt( sth, SQL_DROP );
442 #endif /* BACKSQL_REALLOC_STMT */
447 #ifndef BACKSQL_REALLOC_STMT
448 SQLFreeStmt( sth, SQL_DROP );
449 #endif /* BACKSQL_REALLOC_STMT */
452 * FIXME: should fail in case one change fails?
458 backsql_modify( Operation *op, SlapReply *rs )
460 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
462 backsql_oc_map_rec *oc = NULL;
463 backsql_entryID e_id;
467 * FIXME: in case part of the operation cannot be performed
468 * (missing mapping, SQL write fails or so) the entire operation
469 * should be rolled-back
471 Debug( LDAP_DEBUG_TRACE, "==>backsql_modify(): changing entry '%s'\n",
472 op->o_req_ndn.bv_val, 0, 0 );
474 rs->sr_err = backsql_get_db_conn( op, &dbh );
475 if ( rs->sr_err != LDAP_SUCCESS ) {
476 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
477 "could not get connection handle - exiting\n",
480 * FIXME: we don't want to send back
481 * excessively detailed messages
483 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
484 ? "SQL-backend error" : NULL;
485 send_ldap_result( op, rs );
489 rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
490 if ( rs->sr_err != LDAP_SUCCESS ) {
491 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
492 "could not lookup entry id\n", 0, 0, 0 );
493 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
494 ? "SQL-backend error" : NULL;
495 send_ldap_result( op, rs );
499 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
500 "modifying entry '%s' (id=%ld)\n",
501 e_id.dn.bv_val, e_id.id, 0 );
503 oc = backsql_id2oc( bi, e_id.oc_id );
505 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
506 "cannot determine objectclass of entry -- aborting\n",
509 * FIXME: should never occur, since the entry was built!!!
513 * FIXME: we don't want to send back
514 * excessively detailed messages
516 rs->sr_err = LDAP_OTHER;
517 rs->sr_text = "SQL-backend error";
518 send_ldap_result( op, rs );
523 e.e_name = op->o_req_dn;
524 e.e_nname = op->o_req_ndn;
525 if ( !acl_check_modlist( op, &e, op->oq_modify.rs_modlist ) ) {
526 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
529 rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id,
530 op->oq_modify.rs_modlist );
533 if ( rs->sr_err == LDAP_SUCCESS ) {
535 * Commit only if all operations succeed
537 SQLTransact( SQL_NULL_HENV, dbh,
538 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
540 send_ldap_result( op, rs );
541 Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n", 0, 0, 0 );
547 backsql_modrdn( Operation *op, SlapReply *rs )
549 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
553 backsql_entryID e_id, pe_id, new_pid;
554 backsql_oc_map_rec *oc = NULL;
555 struct berval p_dn, p_ndn,
556 *new_pdn = NULL, *new_npdn = NULL,
558 LDAPRDN new_rdn = NULL;
559 LDAPRDN old_rdn = NULL;
562 struct berval *newSuperior = op->oq_modrdn.rs_newSup;
564 Debug( LDAP_DEBUG_TRACE, "==>backsql_modrdn() renaming entry '%s', "
565 "newrdn='%s', newSuperior='%s'\n",
566 op->o_req_dn.bv_val, op->oq_modrdn.rs_newrdn.bv_val,
567 newSuperior ? newSuperior->bv_val : "(NULL)" );
568 rs->sr_err = backsql_get_db_conn( op, &dbh );
569 if ( rs->sr_err != LDAP_SUCCESS ) {
570 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
571 "could not get connection handle - exiting\n",
573 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
574 ? "SQL-backend error" : NULL;
575 send_ldap_result( op, rs );
579 rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
580 if ( rs->sr_err != LDAP_SUCCESS ) {
581 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
582 "could not lookup entry id\n", 0, 0, 0 );
583 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
584 ? "SQL-backend error" : NULL;
585 send_ldap_result( op, rs );
589 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): entry id is %ld\n",
592 if ( backsql_has_children( bi, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
593 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
594 "entry \"%s\" has children\n",
595 op->o_req_dn.bv_val, 0, 0 );
596 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
597 rs->sr_text = "subtree rename not supported";
598 send_ldap_result( op, rs );
602 dnParent( &op->o_req_dn, &p_dn );
603 dnParent( &op->o_req_ndn, &p_ndn );
606 * namingContext "" is not supported
608 if ( p_dn.bv_len == 0 ) {
609 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
610 "parent is \"\" - aborting\n", 0, 0, 0 );
611 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
612 rs->sr_text = "not allowed within namingContext";
613 send_ldap_result( op, rs );
618 * Check for children access to parent
623 if ( !access_allowed( op, &e, slap_schema.si_ad_children,
624 NULL, ACL_WRITE, NULL ) ) {
625 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0, 0, 0 );
626 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
632 * namingContext "" is not supported
634 if ( newSuperior->bv_len == 0 ) {
635 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
636 "newSuperior is \"\" - aborting\n", 0, 0, 0 );
637 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
638 rs->sr_text = "not allowed within namingContext";
639 send_ldap_result( op, rs );
643 new_pdn = newSuperior;
644 new_npdn = op->oq_modrdn.rs_nnewSup;
647 e.e_nname = *new_npdn;
650 * Check for children access to new parent
652 if ( !access_allowed( op, &e, slap_schema.si_ad_children,
653 NULL, ACL_WRITE, NULL ) ) {
654 Debug( LDAP_DEBUG_TRACE, "no access to new parent\n",
656 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
665 if ( newSuperior && dn_match( &p_ndn, new_npdn ) ) {
666 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
667 "newSuperior is equal to old parent - ignored\n",
672 if ( newSuperior && dn_match( &op->o_req_ndn, new_npdn ) ) {
673 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
674 "newSuperior is equal to entry being moved "
675 "- aborting\n", 0, 0, 0 );
676 rs->sr_err = LDAP_OTHER;
677 rs->sr_text = "newSuperior is equal to old DN";
678 send_ldap_result( op, rs );
682 build_new_dn( &new_dn, new_pdn, &op->oq_modrdn.rs_newrdn, NULL );
683 rs->sr_err = dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn,
685 if ( rs->sr_err != LDAP_SUCCESS ) {
686 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
687 "new dn is invalid ('%s') - aborting\n",
688 new_dn.bv_val, 0, 0 );
689 rs->sr_text = "unable to build new DN";
690 send_ldap_result( op, rs );
694 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): new entry dn is '%s'\n",
695 new_dn.bv_val, 0, 0 );
697 rs->sr_err = backsql_dn2id( bi, &pe_id, dbh, &p_ndn );
698 if ( rs->sr_err != LDAP_SUCCESS ) {
699 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
700 "could not lookup old parent entry id\n", 0, 0, 0 );
701 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
702 ? "SQL-backend error" : NULL;
703 send_ldap_result( op, rs );
707 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
708 "old parent entry id is %ld\n", pe_id.id, 0, 0 );
710 rs->sr_err = backsql_dn2id( bi, &new_pid, dbh, new_npdn );
711 if ( rs->sr_err != LDAP_SUCCESS ) {
712 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
713 "could not lookup new parent entry id\n", 0, 0, 0 );
714 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
715 ? "SQL-backend error" : NULL;
716 send_ldap_result( op, rs );
720 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
721 "new parent entry id is %ld\n", new_pid.id, 0, 0 );
724 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
725 "executing delentry_query\n", 0, 0, 0 );
726 SQLAllocStmt( dbh, &sth );
727 SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
728 0, 0, &e_id.id, 0, 0 );
729 rc = SQLExecDirect( sth, bi->delentry_query, SQL_NTS );
730 if ( rc != SQL_SUCCESS ) {
731 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
732 "failed to delete record from ldap_entries\n",
734 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
735 rs->sr_err = LDAP_OTHER;
736 rs->sr_text = "SQL-backend error";
737 send_ldap_result( op, rs );
741 SQLFreeStmt( sth, SQL_RESET_PARAMS );
743 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
744 "executing insentry_query\n", 0, 0, 0 );
745 backsql_BindParamStr( sth, 1, new_dn.bv_val, BACKSQL_MAX_DN_LEN );
746 SQLBindParameter( sth, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
747 0, 0, &e_id.oc_id, 0, 0 );
748 SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
749 0, 0, &new_pid.id, 0, 0 );
750 SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
751 0, 0, &e_id.keyval, 0, 0 );
752 rc = SQLExecDirect( sth, bi->insentry_query, SQL_NTS );
753 if ( rc != SQL_SUCCESS ) {
754 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
755 "could not insert ldap_entries record\n", 0, 0, 0 );
756 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
757 rs->sr_err = LDAP_OTHER;
758 rs->sr_text = "SQL-backend error";
759 send_ldap_result( op, rs );
764 * Get attribute type and attribute value of our new rdn,
765 * we will need to add that to our new entry
767 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn,
768 (char **)&rs->sr_text,
769 LDAP_DN_FORMAT_LDAP ) ) {
771 LDAP_LOG ( OPERATION, ERR,
772 "backsql_modrdn: can't figure out "
773 "type(s)/values(s) of newrdn\n",
776 Debug( LDAP_DEBUG_TRACE,
777 "backsql_modrdn: can't figure out "
778 "type(s)/values(s) of newrdn\n",
781 rs->sr_err = LDAP_INVALID_DN_SYNTAX;
786 LDAP_LOG ( OPERATION, RESULTS,
787 "backsql_modrdn: new_rdn_type=\"%s\", "
788 "new_rdn_val=\"%s\"\n",
789 new_rdn[ 0 ]->la_attr.bv_val,
790 new_rdn[ 0 ]->la_value.bv_val, 0 );
792 Debug( LDAP_DEBUG_TRACE,
793 "backsql_modrdn: new_rdn_type=\"%s\", "
794 "new_rdn_val=\"%s\"\n",
795 new_rdn[ 0 ]->la_attr.bv_val,
796 new_rdn[ 0 ]->la_value.bv_val, 0 );
799 if ( op->oq_modrdn.rs_deleteoldrdn ) {
800 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn,
801 (char **)&rs->sr_text,
802 LDAP_DN_FORMAT_LDAP ) ) {
804 LDAP_LOG ( OPERATION, ERR,
805 "backsql_modrdn: can't figure out "
806 "type(s)/values(s) of old_rdn\n",
809 Debug( LDAP_DEBUG_TRACE,
810 "backsql_modrdn: can't figure out "
811 "the old_rdn type(s)/value(s)\n",
814 rs->sr_err = LDAP_OTHER;
821 rs->sr_err = slap_modrdn2mods( op, rs, &e, old_rdn, new_rdn, &mod );
822 if ( rs->sr_err != LDAP_SUCCESS ) {
826 if ( !acl_check_modlist( op, &e, mod )) {
827 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
831 oc = backsql_id2oc( bi, e_id.oc_id );
832 rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, mod );
834 if ( rs->sr_err == LDAP_SUCCESS ) {
837 * Commit only if all operations succeed
839 SQLTransact( SQL_NULL_HENV, dbh,
840 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
844 SQLFreeStmt( sth, SQL_DROP );
846 if ( new_dn.bv_val ) {
847 ch_free( new_dn.bv_val );
850 if ( new_ndn.bv_val ) {
851 ch_free( new_ndn.bv_val );
854 /* LDAP v2 supporting correct attribute handling. */
855 if ( new_rdn != NULL ) {
856 ldap_rdnfree( new_rdn );
858 if ( old_rdn != NULL ) {
859 ldap_rdnfree( old_rdn );
863 for (; mod; mod=tmp ) {
869 send_ldap_result( op, rs );
871 Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n", 0, 0, 0 );
876 backsql_add( Operation *op, SlapReply *rs )
878 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
881 unsigned long new_keyval = 0;
884 backsql_oc_map_rec *oc = NULL;
885 backsql_at_map_rec *at_rec = NULL;
886 backsql_entryID e_id, parent_id;
889 struct berval *at_val;
891 /* first parameter #, parameter order */
892 SQLUSMALLINT pno, po;
893 /* procedure return code */
896 Debug( LDAP_DEBUG_TRACE, "==>backsql_add(): adding entry '%s'\n",
897 op->oq_add.rs_e->e_name.bv_val, 0, 0 );
900 if ( global_schemacheck ) {
901 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
903 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
905 &rs->sr_text, textbuf, sizeof( textbuf ) );
906 if ( rs->sr_err != LDAP_SUCCESS ) {
907 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
908 "entry failed schema check -- aborting\n",
910 send_ldap_result( op, rs );
915 /* search structural objectClass */
916 for ( at = op->oq_add.rs_e->e_attrs; at != NULL; at = at->a_next ) {
917 if ( at->a_desc == slap_schema.si_ad_structuralObjectClass ) {
922 /* there must exist */
923 assert( at != NULL );
925 /* I guess we should play with sub/supertypes to find a suitable oc */
926 oc = backsql_name2oc( bi, &at->a_vals[0] );
929 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
930 "cannot determine objectclass of entry -- aborting\n",
932 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
933 rs->sr_text = "operation not permitted within namingContext";
934 send_ldap_result( op, rs );
938 if ( oc->create_proc == NULL ) {
939 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
940 "create procedure is not defined for this objectclass "
941 "- aborting\n", 0, 0, 0 );
942 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
943 rs->sr_text = "operation not permitted within namingContext";
944 send_ldap_result( op, rs );
947 } else if ( BACKSQL_CREATE_NEEDS_SELECT( bi )
948 && oc->create_keyval == NULL ) {
949 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
950 "create procedure needs select procedure, "
951 "but none is defined - aborting\n", 0, 0, 0 );
952 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
953 rs->sr_text = "operation not permitted within namingContext";
954 send_ldap_result( op, rs );
958 rs->sr_err = backsql_get_db_conn( op, &dbh );
959 if ( rs->sr_err != LDAP_SUCCESS ) {
960 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
961 "could not get connection handle - exiting\n",
963 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
964 ? "SQL-backend error" : NULL,
965 send_ldap_result( op, rs );
970 * Check if entry exists
972 rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->oq_add.rs_e->e_name );
973 if ( rs->sr_err == LDAP_SUCCESS ) {
974 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
975 "entry '%s' exists\n",
976 op->oq_add.rs_e->e_name.bv_val, 0, 0 );
977 rs->sr_err = LDAP_ALREADY_EXISTS;
978 send_ldap_result( op, rs );
983 * Check if parent exists
985 dnParent( &op->oq_add.rs_e->e_name, &pdn );
986 rs->sr_err = backsql_dn2id( bi, &parent_id, dbh, &pdn );
987 if ( rs->sr_err != LDAP_SUCCESS ) {
988 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
989 "could not lookup parent entry for new record '%s'\n",
992 if ( rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
993 send_ldap_result( op, rs );
1002 char *matched = NULL;
1005 dnParent( &dn, &pdn );
1008 * Empty DN ("") defaults to LDAP_SUCCESS
1010 rs->sr_err = backsql_dn2id( bi, &parent_id, dbh, &pdn );
1011 switch ( rs->sr_err ) {
1012 case LDAP_NO_SUCH_OBJECT:
1013 if ( pdn.bv_len > 0 ) {
1016 /* fail over to next case */
1019 matched = pdn.bv_val;
1020 /* fail over to next case */
1023 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1024 rs->sr_matched = matched;
1025 send_ldap_result( op, rs );
1032 * create_proc is executed; if expect_return is set, then
1033 * an output parameter is bound, which should contain
1034 * the id of the added row; otherwise the procedure
1035 * is expected to return the id as the first column of a select
1040 dnParent( &op->oq_add.rs_e->e_nname, &p.e_nname );
1041 if ( !access_allowed( op, &p, slap_schema.si_ad_children,
1042 NULL, ACL_WRITE, NULL ) ) {
1043 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1044 send_ldap_result( op, rs );
1048 rc = SQLAllocStmt( dbh, &sth );
1049 if ( rc != SQL_SUCCESS ) {
1050 rs->sr_err = LDAP_OTHER;
1051 rs->sr_text = "SQL-backend error";
1052 send_ldap_result( op, rs );
1056 if ( BACKSQL_IS_ADD( oc->expect_return ) ) {
1057 SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG,
1058 SQL_INTEGER, 0, 0, &new_keyval, 0, 0 );
1061 Debug( LDAP_DEBUG_TRACE, "backsql_add(): executing '%s'\n",
1062 oc->create_proc, 0, 0 );
1063 rc = SQLExecDirect( sth, oc->create_proc, SQL_NTS );
1064 if ( rc != SQL_SUCCESS ) {
1065 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1066 "create_proc execution failed\n", 0, 0, 0 );
1067 backsql_PrintErrors( bi->db_env, dbh, sth, rc);
1068 SQLFreeStmt( sth, SQL_DROP );
1069 rs->sr_err = LDAP_OTHER;
1070 rs->sr_text = "SQL-backend error";
1071 send_ldap_result( op, rs );
1075 SQLTransact( SQL_NULL_HENV, dbh, SQL_ROLLBACK );
1078 if ( !BACKSQL_IS_ADD( oc->expect_return ) ) {
1080 SQLINTEGER value_len;
1082 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
1083 #ifndef BACKSQL_REALLOC_STMT
1084 SQLFreeStmt( sth, SQL_RESET_PARAMS );
1085 #else /* BACKSQL_REALLOC_STMT */
1086 SQLFreeStmt( sth, SQL_DROP );
1087 rc = SQLAllocStmt( dbh, &sth );
1088 if ( rc != SQL_SUCCESS ) {
1089 rs->sr_err = LDAP_OTHER;
1090 rs->sr_text = "SQL-backend error";
1091 send_ldap_result( op, rs );
1094 #endif /* BACKSQL_REALLOC_STMT */
1096 rc = SQLExecDirect( sth, oc->create_keyval, SQL_NTS );
1097 if ( rc != SQL_SUCCESS ) {
1098 rs->sr_err = LDAP_OTHER;
1099 rs->sr_text = "SQL-backend error";
1100 send_ldap_result( op, rs );
1106 * the query to know the id of the inserted entry
1107 * must be embedded in the create procedure
1109 rc = SQLNumResultCols( sth, &ncols );
1110 if ( rc != SQL_SUCCESS ) {
1111 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1112 "create_proc result evaluation failed\n",
1114 backsql_PrintErrors( bi->db_env, dbh, sth, rc);
1115 SQLFreeStmt( sth, SQL_DROP );
1116 rs->sr_err = LDAP_OTHER;
1117 rs->sr_text = "SQL-backend error";
1118 send_ldap_result( op, rs );
1121 } else if ( ncols != 1 ) {
1122 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1123 "create_proc result is bogus (ncols=%d)\n",
1125 backsql_PrintErrors( bi->db_env, dbh, sth, rc);
1126 SQLFreeStmt( sth, SQL_DROP );
1127 rs->sr_err = LDAP_OTHER;
1128 rs->sr_text = "SQL-backend error";
1129 send_ldap_result( op, rs );
1135 SQLCHAR colname[ 64 ];
1136 SQLSMALLINT name_len, col_type, col_scale, col_null;
1140 * FIXME: check whether col_type is compatible,
1141 * if it can be null and so on ...
1143 rc = SQLDescribeCol( sth, (SQLUSMALLINT)1,
1145 (SQLUINTEGER)( sizeof( colname ) - 1 ),
1146 &name_len, &col_type,
1147 &col_prec, &col_scale, &col_null );
1151 rc = SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_ULONG,
1152 (SQLPOINTER)&new_keyval,
1153 (SQLINTEGER)sizeof( new_keyval ),
1156 rc = SQLFetch( sth );
1158 if ( value_len <= 0 ) {
1159 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1160 "create_proc result is empty?\n",
1162 backsql_PrintErrors( bi->db_env, dbh, sth, rc);
1163 SQLFreeStmt( sth, SQL_DROP );
1164 rs->sr_err = LDAP_OTHER;
1165 rs->sr_text = "SQL-backend error";
1166 send_ldap_result( op, rs );
1171 #ifndef BACKSQL_REALLOC_STMT
1172 SQLFreeStmt( sth, SQL_RESET_PARAMS );
1173 #else /* BACKSQL_REALLOC_STMT */
1174 SQLFreeStmt( sth, SQL_DROP );
1175 #endif /* BACKSQL_REALLOC_STMT */
1177 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1178 "create_proc returned keyval=%ld\n", new_keyval, 0, 0 );
1180 for ( at = op->oq_add.rs_e->e_attrs; at != NULL; at = at->a_next ) {
1181 SQLUSMALLINT currpos;
1183 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1184 "adding attribute '%s'\n",
1185 at->a_desc->ad_cname.bv_val, 0, 0 );
1189 * - the first occurrence of objectClass, which is used
1190 * to determine how to bulid the SQL entry (FIXME ?!?)
1191 * - operational attributes
1192 * empty attributes (FIXME ?!?)
1194 if ( backsql_attr_skip( at->a_desc, at->a_vals ) ) {
1198 at_rec = backsql_ad2at( oc, at->a_desc );
1200 if ( at_rec == NULL ) {
1201 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1202 "attribute '%s' is not registered "
1203 "in objectclass '%s'\n",
1204 at->a_desc->ad_cname.bv_val,
1205 BACKSQL_OC_NAME( oc ), 0 );
1207 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
1208 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1209 rs->sr_text = "operation not permitted "
1210 "within namingContext";
1211 send_ldap_result( op, rs );
1218 if ( at_rec->add_proc == NULL ) {
1219 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1220 "add procedure is not defined "
1221 "for attribute '%s'\n",
1222 at->a_desc->ad_cname.bv_val, 0, 0 );
1224 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
1225 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1226 rs->sr_text = "operation not permitted "
1227 "within namingContext";
1228 send_ldap_result( op, rs );
1235 #ifdef BACKSQL_REALLOC_STMT
1236 rc = backsql_Prepare( dbh, &sth, at_rec->add_proc, 0 );
1237 if ( rc != SQL_SUCCESS ) {
1239 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
1240 rs->sr_err = LDAP_OTHER;
1241 rs->sr_text = "SQL-backend error";
1242 send_ldap_result( op, rs );
1248 #endif /* BACKSQL_REALLOC_STMT */
1250 if ( BACKSQL_IS_ADD( at_rec->expect_return ) ) {
1252 SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT,
1253 SQL_C_ULONG, SQL_INTEGER,
1259 po = ( BACKSQL_IS_ADD( at_rec->param_order ) ) > 0;
1260 currpos = pno + 1 + po;
1261 SQLBindParameter( sth, currpos,
1262 SQL_PARAM_INPUT, SQL_C_ULONG,
1263 SQL_INTEGER, 0, 0, &new_keyval, 0, 0 );
1264 currpos = pno + 2 - po;
1266 for ( i = 0, at_val = &at->a_vals[ i ];
1267 at_val->bv_val != NULL;
1268 i++, at_val = &at->a_vals[ i ] ) {
1271 * Do not deal with the objectClass that is used
1272 * to build the entry
1274 if ( at->a_desc == slap_schema.si_ad_objectClass ) {
1275 if ( bvmatch( at_val, &oc->oc->soc_cname ) ) {
1281 * check for syntax needed here
1282 * maybe need binary bind?
1285 backsql_BindParamStr( sth, currpos,
1286 at_val->bv_val, at_val->bv_len + 1 );
1287 #ifdef SECURITY_PARANOID
1288 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1289 "executing '%s', id=%ld\n",
1290 at_rec->add_proc, new_keyval, 0 );
1292 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1293 "executing '%s' with val='%s', id=%ld\n",
1294 at_rec->add_proc, at_val->bv_val, new_keyval );
1296 #ifndef BACKSQL_REALLOC_STMT
1297 rc = SQLExecDirect( sth, at_rec->add_proc, SQL_NTS );
1298 #else /* BACKSQL_REALLOC_STMT */
1299 rc = SQLExecute( sth );
1300 #endif /* BACKSQL_REALLOC_STMT */
1301 if ( rc != SQL_SUCCESS ) {
1302 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1303 "add_proc execution failed\n",
1305 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
1307 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
1308 rs->sr_err = LDAP_OTHER;
1309 rs->sr_text = "SQL-backend error";
1310 send_ldap_result( op, rs );
1315 #ifndef BACKSQL_REALLOC_STMT
1316 SQLFreeStmt( sth, SQL_RESET_PARAMS );
1317 #else /* BACKSQL_REALLOC_STMT */
1318 SQLFreeStmt( sth, SQL_DROP );
1319 #endif /* BACKSQL_REALLOC_STMT */
1322 #ifdef BACKSQL_REALLOC_STMT
1323 rc = backsql_Prepare( dbh, &sth, bi->insentry_query, 0 );
1324 if ( rc != SQL_SUCCESS ) {
1325 rs->sr_err = LDAP_OTHER;
1326 rs->sr_text = "SQL-backend error";
1327 send_ldap_result( op, rs );
1330 #endif /* BACKSQL_REALLOC_STMT */
1332 backsql_BindParamStr( sth, 1, op->oq_add.rs_e->e_name.bv_val,
1333 BACKSQL_MAX_DN_LEN );
1334 SQLBindParameter( sth, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
1335 0, 0, &oc->id, 0, 0 );
1336 SQLBindParameter( sth, 3, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
1337 0, 0, &parent_id.id, 0, 0 );
1338 SQLBindParameter( sth, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
1339 0, 0, &new_keyval, 0, 0 );
1341 Debug( LDAP_DEBUG_TRACE, "backsql_add(): executing '%s' for dn '%s'\n",
1342 bi->insentry_query, op->oq_add.rs_e->e_name.bv_val, 0 );
1343 Debug( LDAP_DEBUG_TRACE, " for oc_map_id=%ld, parent_id=%ld, "
1344 "keyval=%ld\n", oc->id, parent_id.id, new_keyval );
1345 #ifndef BACKSQL_REALLOC_STMT
1346 rc = SQLExecDirect( sth, bi->insentry_query, SQL_NTS );
1347 #else /* BACKSQL_REALLOC_STMT */
1348 rc = SQLExecute( sth );
1349 #endif /* BACKSQL_REALLOC_STMT */
1350 if ( rc != SQL_SUCCESS ) {
1351 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1352 "could not insert ldap_entries record\n", 0, 0, 0 );
1353 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
1356 * execute delete_proc to delete data added !!!
1358 SQLFreeStmt( sth, SQL_DROP );
1359 rs->sr_err = LDAP_OTHER;
1360 rs->sr_text = "SQL-backend error";
1361 send_ldap_result( op, rs );
1365 SQLFreeStmt( sth, SQL_DROP );
1368 * Commit only if all operations succeed
1370 SQLTransact( SQL_NULL_HENV, dbh,
1371 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
1374 * FIXME: NOOP does not work for add -- it works for all
1375 * the other operations, and I don't get the reason :(
1378 send_ldap_result( op, rs );
1384 backsql_delete( Operation *op, SlapReply *rs )
1386 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
1390 backsql_oc_map_rec *oc = NULL;
1391 backsql_entryID e_id;
1393 /* first parameter no */
1396 Debug( LDAP_DEBUG_TRACE, "==>backsql_delete(): deleting entry '%s'\n",
1397 op->o_req_ndn.bv_val, 0, 0 );
1399 dnParent( &op->o_req_dn, &e.e_name );
1400 dnParent( &op->o_req_ndn, &e.e_nname );
1403 /* check parent for "children" acl */
1404 if ( !access_allowed( op, &e, slap_schema.si_ad_children,
1405 NULL, ACL_WRITE, NULL ) ) {
1406 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1407 "no write access to parent\n",
1409 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1410 send_ldap_result( op, rs );
1415 rs->sr_err = backsql_get_db_conn( op, &dbh );
1416 if ( rs->sr_err != LDAP_SUCCESS ) {
1417 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1418 "could not get connection handle - exiting\n",
1420 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
1421 ? "SQL-backend error" : NULL,
1422 send_ldap_result( op, rs );
1426 rs->sr_err = backsql_dn2id( bi, &e_id, dbh, &op->o_req_ndn );
1427 if ( rs->sr_err != LDAP_SUCCESS ) {
1428 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1429 "could not lookup entry id\n", 0, 0, 0 );
1430 send_ldap_result( op, rs );
1434 rs->sr_err = backsql_has_children( bi, dbh, &op->o_req_ndn );
1435 switch ( rs->sr_err ) {
1436 case LDAP_COMPARE_TRUE:
1437 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1438 "entry \"%s\" has children\n",
1439 op->o_req_dn.bv_val, 0, 0 );
1440 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
1441 rs->sr_text = "subtree delete not supported";
1442 send_ldap_result( op, rs );
1445 case LDAP_COMPARE_FALSE:
1449 send_ldap_result( op, rs );
1453 oc = backsql_id2oc( bi, e_id.oc_id );
1455 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1456 "cannot determine objectclass of entry -- aborting\n",
1458 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1459 rs->sr_text = "operation not permitted within namingContext";
1460 send_ldap_result( op, rs );
1464 if ( oc->delete_proc == NULL ) {
1465 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1466 "delete procedure is not defined "
1467 "for this objectclass - aborting\n", 0, 0, 0 );
1468 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1469 rs->sr_text = "operation not permitted within namingContext";
1470 send_ldap_result( op, rs );
1474 SQLAllocStmt( dbh, &sth );
1475 if ( BACKSQL_IS_DEL( oc->expect_return ) ) {
1477 SQLBindParameter( sth, 1, SQL_PARAM_OUTPUT, SQL_C_ULONG,
1478 SQL_INTEGER, 0, 0, &rc, 0, 0 );
1483 SQLBindParameter( sth, pno + 1, SQL_PARAM_INPUT,
1484 SQL_C_ULONG, SQL_INTEGER, 0, 0, &e_id.keyval, 0, 0 );
1486 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): executing '%s'\n",
1487 oc->delete_proc, 0, 0 );
1488 rc = SQLExecDirect( sth, oc->delete_proc, SQL_NTS );
1489 if ( rc != SQL_SUCCESS ) {
1490 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1491 "delete_proc execution failed\n", 0, 0, 0 );
1492 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
1493 SQLFreeStmt( sth, SQL_DROP );
1494 rs->sr_err = LDAP_OTHER;
1495 rs->sr_text = "SQL-backend error";
1496 send_ldap_result( op, rs );
1499 #ifndef BACKSQL_REALLOC_STMT
1500 SQLFreeStmt( sth, SQL_RESET_PARAMS );
1501 #else /* BACKSQL_REALLOC_STMT */
1502 SQLFreeStmt( sth, SQL_DROP );
1503 SQLAllocStmt( dbh, &sth );
1504 #endif /* BACKSQL_REALLOC_STMT */
1506 SQLBindParameter( sth, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER,
1507 0, 0, &e_id.id, 0, 0 );
1508 rc = SQLExecDirect( sth, bi->delentry_query, SQL_NTS );
1509 if ( rc != SQL_SUCCESS ) {
1510 Debug( LDAP_DEBUG_TRACE, "backsql_delete(): "
1511 "failed to delete record from ldap_entries\n",
1513 backsql_PrintErrors( bi->db_env, dbh, sth, rc );
1514 SQLFreeStmt( sth, SQL_DROP );
1515 rs->sr_err = LDAP_OTHER;
1516 rs->sr_text = "SQL-backend error";
1517 send_ldap_result( op, rs );
1521 SQLFreeStmt( sth, SQL_DROP );
1524 * Commit only if all operations succeed
1526 * FIXME: backsql_add() does not fail if add operations
1527 * are not available for some attributes, or if
1528 * a multiple value add actually results in a replace,
1529 * or if a single operation on an attribute fails
1532 SQLTransact( SQL_NULL_HENV, dbh,
1533 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
1535 rs->sr_err = LDAP_SUCCESS;
1536 send_ldap_result( op, rs );
1537 Debug( LDAP_DEBUG_TRACE, "<==backsql_delete()\n", 0, 0, 0 );
1542 #endif /* SLAPD_SQL */