Debian Codegraph @

servers/slapd/back-sql/add.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2024 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
7 * Portions Copyright 2004 Mark Adamson.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18 /* ACKNOWLEDGEMENTS:
19 * This work was initially developed by Dmitry Kovalev for inclusion
20 * by OpenLDAP Software. Additional significant contributors include
21 * Pierangelo Masarati and Mark Adamson.
22
23 */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include "ac/string.h"
30
31 #include "slap.h"
32 #include "proto-sql.h"
33
34 #ifdef BACKSQL_SYNCPROV
35 #include <lutil.h>
36 #endif /* BACKSQL_SYNCPROV */
37
38 const char * processable_op_attrs[] = {
39 "pwdAccountLockedTime",
40 "pwdChangedTime",
41 "pwdFailureTime",
42 "pwdGraceUseTime",
43 "pwdHistory",
44 "pwdPolicySubentry",
45 "pwdReset",
46 "entryUUID"
47 };
48
49 #define processable_op_attrs_length (sizeof (processable_op_attrs) / sizeof (const char *))
50
51 static int indexOf(const char *array[], int array_size, const char * value) {
52 for (int i = 0; i < array_size; ++i) {
53 if(strcmp(array[i], value) == 0) {
54 return i;
55 }
56 }
57 return -1;
58 }
59
60 static int is_processable_opattr(const char * attr) {
61 return indexOf(processable_op_attrs, processable_op_attrs_length, attr) >= 0;
62 }
63
64 #define backsql_opattr_skip(ad) \
65 (is_at_operational( (ad)->ad_type ) && (ad) != slap_schema.si_ad_ref )
66
67 /*
68 * Skip:
69 * - null values (e.g. delete modification)
70 * - single occurrence of objectClass, because it is already used
71 * to determine how to build the SQL entry
72 * - operational attributes (except those in processable_op_attrs)
73 * - empty attributes
74 */
75 #define backsql_attr_skip(ad, vals) \
76 ( \
77 ( ( (ad) == slap_schema.si_ad_objectClass \
78 && (vals) && BER_BVISNULL( &((vals)[ 1 ]) ) ) \
79 || backsql_opattr_skip( (ad) ) \
80 || ( (vals) && BER_BVISNULL( &((vals)[ 0 ]) ) ) \
81 ) && !is_processable_opattr( ad->ad_cname.bv_val ) )
82
83 int
84 backsql_modify_delete_all_values(
85 Operation *op,
86 SlapReply *rs,
87 SQLHDBC dbh,
88 backsql_entryID *e_id,
89 backsql_at_map_rec *at )
90 {
91 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
92 RETCODE rc;
93 SQLHSTMT asth = SQL_NULL_HSTMT;
94 BACKSQL_ROW_NTS row;
95
96 assert( at != NULL );
97 if ( at->bam_delete_proc == NULL ) {
98 Debug( LDAP_DEBUG_TRACE,
99 " backsql_modify_delete_all_values(): "
100 "missing attribute value delete procedure "
101 "for attr \"%s\"\n",
102 at->bam_ad->ad_cname.bv_val );
103 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
104 rs->sr_text = "SQL-backend error";
105 return rs->sr_err = LDAP_OTHER;
106 }
107
108 return LDAP_SUCCESS;
109 }
110
111 rc = backsql_Prepare( dbh, &asth, at->bam_query, 0 );
112 if ( rc != SQL_SUCCESS ) {
113 Debug( LDAP_DEBUG_TRACE,
114 " backsql_modify_delete_all_values(): "
115 "error preparing attribute value select query "
116 "\"%s\"\n",
117 at->bam_query );
118 backsql_PrintErrors( bi->sql_db_env, dbh,
119 asth, rc );
120
121 rs->sr_text = "SQL-backend error";
122 return rs->sr_err = LDAP_OTHER;
123 }
124
125 rc = backsql_BindParamID( asth, 1, SQL_PARAM_INPUT, &e_id->eid_keyval );
126 if ( rc != SQL_SUCCESS ) {
127 Debug( LDAP_DEBUG_TRACE,
128 " backsql_modify_delete_all_values(): "
129 "error binding key value parameter "
130 "to attribute value select query\n" );
131 backsql_PrintErrors( bi->sql_db_env, dbh,
132 asth, rc );
133 SQLFreeStmt( asth, SQL_DROP );
134
135 rs->sr_text = "SQL-backend error";
136 return rs->sr_err = LDAP_OTHER;
137 }
138
139 rc = SQLExecute( asth );
140 if ( !BACKSQL_SUCCESS( rc ) ) {
141 Debug( LDAP_DEBUG_TRACE,
142 " backsql_modify_delete_all_values(): "
143 "error executing attribute value select query\n" );
144 backsql_PrintErrors( bi->sql_db_env, dbh,
145 asth, rc );
146 SQLFreeStmt( asth, SQL_DROP );
147
148 rs->sr_text = "SQL-backend error";
149 return rs->sr_err = LDAP_OTHER;
150 }
151
152 backsql_BindRowAsStrings_x( asth, &row, op->o_tmpmemctx );
153 for ( rc = SQLFetch( asth );
154 BACKSQL_SUCCESS( rc );
155 rc = SQLFetch( asth ) )
156 {
157 int i;
158 /* first parameter no, parameter order */
159 SQLUSMALLINT pno = 0,
160 po = 0;
161 /* procedure return code */
162 int prc = LDAP_SUCCESS;
163
164 for ( i = 0; i < row.ncols; i++ ) {
165 SQLHSTMT sth = SQL_NULL_HSTMT;
166 ber_len_t col_len;
167
168 rc = backsql_Prepare( dbh, &sth, at->bam_delete_proc, 0 );
169 if ( rc != SQL_SUCCESS ) {
170 Debug( LDAP_DEBUG_TRACE,
171 " backsql_modify_delete_all_values(): "
172 "error preparing attribute value "
173 "delete procedure "
174 "\"%s\"\n",
175 at->bam_delete_proc );
176 backsql_PrintErrors( bi->sql_db_env, dbh,
177 sth, rc );
178
179 rs->sr_text = "SQL-backend error";
180 rs->sr_err = LDAP_OTHER;
181 goto done;
182 }
183
184 if ( BACKSQL_IS_DEL( at->bam_expect_return ) ) {
185 pno = 1;
186 rc = backsql_BindParamInt( sth, 1,
187 SQL_PARAM_OUTPUT, &prc );
188 if ( rc != SQL_SUCCESS ) {
189 Debug( LDAP_DEBUG_TRACE,
190 " backsql_modify_delete_all_values(): "
191 "error binding output parameter for %s[%d]\n",
192 at->bam_ad->ad_cname.bv_val, i );
193 backsql_PrintErrors( bi->sql_db_env, dbh,
194 sth, rc );
195 SQLFreeStmt( sth, SQL_DROP );
196
197 rs->sr_text = "SQL-backend error";
198 rs->sr_err = LDAP_OTHER;
199 goto done;
200 }
201 }
202 po = ( BACKSQL_IS_DEL( at->bam_param_order ) ) > 0;
203 rc = backsql_BindParamID( sth, pno + 1 + po,
204 SQL_PARAM_INPUT, &e_id->eid_keyval );
205 if ( rc != SQL_SUCCESS ) {
206 Debug( LDAP_DEBUG_TRACE,
207 " backsql_modify_delete_all_values(): "
208 "error binding keyval parameter for %s[%d]\n",
209 at->bam_ad->ad_cname.bv_val, i );
210 backsql_PrintErrors( bi->sql_db_env, dbh,
211 sth, rc );
212 SQLFreeStmt( sth, SQL_DROP );
213
214 rs->sr_text = "SQL-backend error";
215 rs->sr_err = LDAP_OTHER;
216 goto done;
217 }
218
219 Debug( LDAP_DEBUG_TRACE,
220 " backsql_modify_delete_all_values() "
221 "arg(%d)=" BACKSQL_IDFMT "\n",
222 pno + 1 + po,
223 BACKSQL_IDARG(e_id->eid_keyval) );
224
225 /*
226 * check for syntax needed here
227 * maybe need binary bind?
228 */
229 col_len = strlen( row.cols[ i ] );
230 rc = backsql_BindParamStr( sth, pno + 2 - po,
231 SQL_PARAM_INPUT, row.cols[ i ], col_len );
232 if ( rc != SQL_SUCCESS ) {
233 Debug( LDAP_DEBUG_TRACE,
234 " backsql_modify_delete_all_values(): "
235 "error binding value parameter for %s[%d]\n",
236 at->bam_ad->ad_cname.bv_val, i );
237 backsql_PrintErrors( bi->sql_db_env, dbh,
238 sth, rc );
239 SQLFreeStmt( sth, SQL_DROP );
240
241 rs->sr_text = "SQL-backend error";
242 rs->sr_err = LDAP_OTHER;
243 goto done;
244 }
245
246 Debug( LDAP_DEBUG_TRACE,
247 " backsql_modify_delete_all_values(): "
248 "arg(%d)=%s; executing \"%s\"\n",
249 pno + 2 - po, row.cols[ i ],
250 at->bam_delete_proc );
251 rc = SQLExecute( sth );
252 if ( rc == SQL_SUCCESS && prc == LDAP_SUCCESS ) {
253 rs->sr_err = LDAP_SUCCESS;
254
255 } else {
256 Debug( LDAP_DEBUG_TRACE,
257 " backsql_modify_delete_all_values(): "
258 "delete_proc "
259 "execution failed (rc=%d, prc=%d)\n",
260 rc, prc );
261 if ( prc != LDAP_SUCCESS ) {
262 /* SQL procedure executed fine
263 * but returned an error */
264 rs->sr_err = BACKSQL_SANITIZE_ERROR( prc );
265
266 } else {
267 backsql_PrintErrors( bi->sql_db_env, dbh,
268 sth, rc );
269 rs->sr_err = LDAP_OTHER;
270 }
271 rs->sr_text = op->o_req_dn.bv_val;
272 SQLFreeStmt( sth, SQL_DROP );
273 goto done;
274 }
275 SQLFreeStmt( sth, SQL_DROP );
276 }
277 }
278
279 rs->sr_err = LDAP_SUCCESS;
280
281 done:;
282 backsql_FreeRow_x( &row, op->o_tmpmemctx );
283 SQLFreeStmt( asth, SQL_DROP );
284
285 return rs->sr_err;
286 }
287
288 int
289 backsql_modify_internal(
290 Operation *op,
291 SlapReply *rs,
292 SQLHDBC dbh,
293 backsql_oc_map_rec *oc,
294 backsql_entryID *e_id,
295 Modifications *modlist )
296 {
297 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
298 RETCODE rc;
299 Modifications *ml;
300
301 Debug( LDAP_DEBUG_TRACE, "==>backsql_modify_internal(): "
302 "traversing modifications list\n" );
303
304 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
305 AttributeDescription *ad;
306 int sm_op;
307 static char *sm_ops[] = { "add", "delete", "replace", "increment", NULL };
308
309 BerVarray sm_values;
310 #if 0
311 /* NOTE: some day we'll have to pass
312 * the normalized values as well */
313 BerVarray sm_nvalues;
314 #endif
315 backsql_at_map_rec *at = NULL;
316 struct berval *at_val;
317 int i;
318
319 ad = ml->sml_mod.sm_desc;
320 sm_op = ( ml->sml_mod.sm_op & LDAP_MOD_OP );
321 sm_values = ml->sml_mod.sm_values;
322 #if 0
323 sm_nvalues = ml->sml_mod.sm_nvalues;
324 #endif
325
326 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
327 "modifying attribute \"%s\" (%s) according to "
328 "mappings for objectClass \"%s\"\n",
329 ad->ad_cname.bv_val, sm_ops[ sm_op ], BACKSQL_OC_NAME( oc ) );
330
331 if ( backsql_attr_skip( ad, sm_values ) ) {
332 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
333 "skipping attribute \"%s\"\n",
334 ad->ad_cname.bv_val, 0, 0 );
335
336 continue;
337 }
338
339 at = backsql_ad2at( oc, ad );
340 if ( at == NULL ) {
341 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
342 "attribute \"%s\" is not registered "
343 "in objectClass \"%s\"\n",
344 ad->ad_cname.bv_val, BACKSQL_OC_NAME( oc ) );
345
346 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
347 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
348 rs->sr_text = "operation not permitted "
349 "within namingContext";
350 goto done;
351 }
352
353 continue;
354 }
355
356 switch ( sm_op ) {
357 case LDAP_MOD_REPLACE: {
358 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
359 "replacing values for attribute \"%s\"\n",
360 at->bam_ad->ad_cname.bv_val );
361
362 if ( at->bam_add_proc == NULL ) {
363 Debug( LDAP_DEBUG_TRACE,
364 " backsql_modify_internal(): "
365 "add procedure is not defined "
366 "for attribute \"%s\" "
367 "- unable to perform replacements\n",
368 at->bam_ad->ad_cname.bv_val );
369
370 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
371 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
372 rs->sr_text = "operation not permitted "
373 "within namingContext";
374 goto done;
375 }
376
377 break;
378 }
379
380 if ( at->bam_delete_proc == NULL ) {
381 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
382 Debug( LDAP_DEBUG_TRACE,
383 " backsql_modify_internal(): "
384 "delete procedure is not defined "
385 "for attribute \"%s\"\n",
386 at->bam_ad->ad_cname.bv_val );
387
388 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
389 rs->sr_text = "operation not permitted "
390 "within namingContext";
391 goto done;
392 }
393
394 Debug( LDAP_DEBUG_TRACE,
395 " backsql_modify_internal(): "
396 "delete procedure is not defined "
397 "for attribute \"%s\" "
398 "- adding only\n",
399 at->bam_ad->ad_cname.bv_val );
400
401 goto add_only;
402 }
403
404 del_all:
405 rs->sr_err = backsql_modify_delete_all_values( op, rs, dbh, e_id, at );
406 if ( rs->sr_err != LDAP_SUCCESS ) {
407 goto done;
408 }
409
410 /* LDAP_MOD_DELETE gets here if all values must be deleted */
411 if ( sm_op == LDAP_MOD_DELETE ) {
412 break;
413 }
414 }
415
416 /*
417 * PASSTHROUGH - to add new attributes -- do NOT add break
418 */
419 case LDAP_MOD_ADD:
420 /* case SLAP_MOD_SOFTADD: */
421 /* case SLAP_MOD_ADD_IF_NOT_PRESENT: */
422 add_only:;
423 if ( at->bam_add_proc == NULL ) {
424 Debug( LDAP_DEBUG_TRACE,
425 " backsql_modify_internal(): "
426 "add procedure is not defined "
427 "for attribute \"%s\"\n",
428 at->bam_ad->ad_cname.bv_val );
429
430 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
431 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
432 rs->sr_text = "operation not permitted "
433 "within namingContext";
434 goto done;
435 }
436
437 break;
438 }
439
440 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
441 "adding new values for attribute \"%s\"\n",
442 at->bam_ad->ad_cname.bv_val );
443
444 /* can't add a NULL val array */
445 assert( sm_values != NULL );
446
447 for ( i = 0, at_val = sm_values;
448 !BER_BVISNULL( at_val );
449 i++, at_val++ )
450 {
451 SQLHSTMT sth = SQL_NULL_HSTMT;
452 /* first parameter position, parameter order */
453 SQLUSMALLINT pno = 0,
454 po;
455 /* procedure return code */
456 int prc = LDAP_SUCCESS;
457
458 rc = backsql_Prepare( dbh, &sth, at->bam_add_proc, 0 );
459 if ( rc != SQL_SUCCESS ) {
460 Debug( LDAP_DEBUG_TRACE,
461 " backsql_modify_internal(): "
462 "error preparing add query\n" );
463 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
464
465 rs->sr_err = LDAP_OTHER;
466 rs->sr_text = "SQL-backend error";
467 goto done;
468 }
469
470 if ( BACKSQL_IS_ADD( at->bam_expect_return ) ) {
471 pno = 1;
472 rc = backsql_BindParamInt( sth, 1,
473 SQL_PARAM_OUTPUT, &prc );
474 if ( rc != SQL_SUCCESS ) {
475 Debug( LDAP_DEBUG_TRACE,
476 " backsql_modify_internal(): "
477 "error binding output parameter for %s[%d]\n",
478 at->bam_ad->ad_cname.bv_val, i );
479 backsql_PrintErrors( bi->sql_db_env, dbh,
480 sth, rc );
481 SQLFreeStmt( sth, SQL_DROP );
482
483 rs->sr_text = "SQL-backend error";
484 rs->sr_err = LDAP_OTHER;
485 goto done;
486 }
487 }
488 po = ( BACKSQL_IS_ADD( at->bam_param_order ) ) > 0;
489 rc = backsql_BindParamID( sth, pno + 1 + po,
490 SQL_PARAM_INPUT, &e_id->eid_keyval );
491 if ( rc != SQL_SUCCESS ) {
492 Debug( LDAP_DEBUG_TRACE,
493 " backsql_modify_internal(): "
494 "error binding keyval parameter for %s[%d]\n",
495 at->bam_ad->ad_cname.bv_val, i );
496 backsql_PrintErrors( bi->sql_db_env, dbh,
497 sth, rc );
498 SQLFreeStmt( sth, SQL_DROP );
499
500 rs->sr_text = "SQL-backend error";
501 rs->sr_err = LDAP_OTHER;
502 goto done;
503 }
504
505 Debug( LDAP_DEBUG_TRACE,
506 " backsql_modify_internal(): "
507 "arg(%d)=" BACKSQL_IDFMT "\n",
508 pno + 1 + po,
509 BACKSQL_IDARG(e_id->eid_keyval) );
510
511 /*
512 * check for syntax needed here
513 * maybe need binary bind?
514 */
515 rc = backsql_BindParamBerVal( sth, pno + 2 - po,
516 SQL_PARAM_INPUT, at_val );
517 if ( rc != SQL_SUCCESS ) {
518 Debug( LDAP_DEBUG_TRACE,
519 " backsql_modify_internal(): "
520 "error binding value parameter for %s[%d]\n",
521 at->bam_ad->ad_cname.bv_val, i );
522 backsql_PrintErrors( bi->sql_db_env, dbh,
523 sth, rc );
524 SQLFreeStmt( sth, SQL_DROP );
525
526 rs->sr_text = "SQL-backend error";
527 rs->sr_err = LDAP_OTHER;
528 goto done;
529 }
530 Debug( LDAP_DEBUG_TRACE,
531 " backsql_modify_internal(): "
532 "arg(%d)=\"%s\"; executing \"%s\"\n",
533 pno + 2 - po, at_val->bv_val,
534 at->bam_add_proc );
535
536 rc = SQLExecute( sth );
537 if ( rc == SQL_SUCCESS && prc == LDAP_SUCCESS ) {
538 rs->sr_err = LDAP_SUCCESS;
539
540 } else {
541 Debug( LDAP_DEBUG_TRACE,
542 " backsql_modify_internal(): "
543 "add_proc execution failed "
544 "(rc=%d, prc=%d)\n",
545 rc, prc );
546 if ( prc != LDAP_SUCCESS ) {
547 /* SQL procedure executed fine
548 * but returned an error */
549 SQLFreeStmt( sth, SQL_DROP );
550
551 rs->sr_err = BACKSQL_SANITIZE_ERROR( prc );
552 rs->sr_text = at->bam_ad->ad_cname.bv_val;
553 return rs->sr_err;
554
555 } else {
556 backsql_PrintErrors( bi->sql_db_env, dbh,
557 sth, rc );
558 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) )
559 {
560 SQLFreeStmt( sth, SQL_DROP );
561
562 rs->sr_err = LDAP_OTHER;
563 rs->sr_text = "SQL-backend error";
564 goto done;
565 }
566 }
567 }
568 SQLFreeStmt( sth, SQL_DROP );
569 }
570 break;
571
572 case LDAP_MOD_DELETE:
573 /* case SLAP_MOD_SOFTDEL: */
574 if ( at->bam_delete_proc == NULL ) {
575 Debug( LDAP_DEBUG_TRACE,
576 " backsql_modify_internal(): "
577 "delete procedure is not defined "
578 "for attribute \"%s\"\n",
579 at->bam_ad->ad_cname.bv_val );
580
581 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
582 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
583 rs->sr_text = "operation not permitted "
584 "within namingContext";
585 goto done;
586 }
587
588 break;
589 }
590
591 if ( sm_values == NULL ) {
592 Debug( LDAP_DEBUG_TRACE,
593 " backsql_modify_internal(): "
594 "no values given to delete "
595 "for attribute \"%s\" "
596 "-- deleting all values\n",
597 at->bam_ad->ad_cname.bv_val );
598 goto del_all;
599 }
600
601 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
602 "deleting values for attribute \"%s\"\n",
603 at->bam_ad->ad_cname.bv_val );
604
605 for ( i = 0, at_val = sm_values;
606 !BER_BVISNULL( at_val );
607 i++, at_val++ )
608 {
609 SQLHSTMT sth = SQL_NULL_HSTMT;
610 /* first parameter position, parameter order */
611 SQLUSMALLINT pno = 0,
612 po;
613 /* procedure return code */
614 int prc = LDAP_SUCCESS;
615
616 rc = backsql_Prepare( dbh, &sth, at->bam_delete_proc, 0 );
617 if ( rc != SQL_SUCCESS ) {
618 Debug( LDAP_DEBUG_TRACE,
619 " backsql_modify_internal(): "
620 "error preparing delete query\n" );
621 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
622
623 rs->sr_err = LDAP_OTHER;
624 rs->sr_text = "SQL-backend error";
625 goto done;
626 }
627
628 if ( BACKSQL_IS_DEL( at->bam_expect_return ) ) {
629 pno = 1;
630 rc = backsql_BindParamInt( sth, 1,
631 SQL_PARAM_OUTPUT, &prc );
632 if ( rc != SQL_SUCCESS ) {
633 Debug( LDAP_DEBUG_TRACE,
634 " backsql_modify_internal(): "
635 "error binding output parameter for %s[%d]\n",
636 at->bam_ad->ad_cname.bv_val, i );
637 backsql_PrintErrors( bi->sql_db_env, dbh,
638 sth, rc );
639 SQLFreeStmt( sth, SQL_DROP );
640
641 rs->sr_text = "SQL-backend error";
642 rs->sr_err = LDAP_OTHER;
643 goto done;
644 }
645 }
646 po = ( BACKSQL_IS_DEL( at->bam_param_order ) ) > 0;
647 rc = backsql_BindParamID( sth, pno + 1 + po,
648 SQL_PARAM_INPUT, &e_id->eid_keyval );
649 if ( rc != SQL_SUCCESS ) {
650 Debug( LDAP_DEBUG_TRACE,
651 " backsql_modify_internal(): "
652 "error binding keyval parameter for %s[%d]\n",
653 at->bam_ad->ad_cname.bv_val, i );
654 backsql_PrintErrors( bi->sql_db_env, dbh,
655 sth, rc );
656 SQLFreeStmt( sth, SQL_DROP );
657
658 rs->sr_text = "SQL-backend error";
659 rs->sr_err = LDAP_OTHER;
660 goto done;
661 }
662
663 Debug( LDAP_DEBUG_TRACE,
664 " backsql_modify_internal(): "
665 "arg(%d)=" BACKSQL_IDFMT "\n",
666 pno + 1 + po,
667 BACKSQL_IDARG(e_id->eid_keyval) );
668
669 /*
670 * check for syntax needed here
671 * maybe need binary bind?
672 */
673 rc = backsql_BindParamBerVal( sth, pno + 2 - po,
674 SQL_PARAM_INPUT, at_val );
675 if ( rc != SQL_SUCCESS ) {
676 Debug( LDAP_DEBUG_TRACE,
677 " backsql_modify_internal(): "
678 "error binding value parameter for %s[%d]\n",
679 at->bam_ad->ad_cname.bv_val, i );
680 backsql_PrintErrors( bi->sql_db_env, dbh,
681 sth, rc );
682 SQLFreeStmt( sth, SQL_DROP );
683
684 rs->sr_text = "SQL-backend error";
685 rs->sr_err = LDAP_OTHER;
686 goto done;
687 }
688
689 Debug( LDAP_DEBUG_TRACE,
690 " backsql_modify_internal(): "
691 "executing \"%s\"\n",
692 at->bam_delete_proc );
693 rc = SQLExecute( sth );
694 if ( rc == SQL_SUCCESS && prc == LDAP_SUCCESS )
695 {
696 rs->sr_err = LDAP_SUCCESS;
697
698 } else {
699 Debug( LDAP_DEBUG_TRACE,
700 " backsql_modify_internal(): "
701 "delete_proc execution "
702 "failed (rc=%d, prc=%d)\n",
703 rc, prc );
704
705 if ( prc != LDAP_SUCCESS ) {
706 /* SQL procedure executed fine
707 * but returned an error */
708 rs->sr_err = BACKSQL_SANITIZE_ERROR( prc );
709 rs->sr_text = at->bam_ad->ad_cname.bv_val;
710 goto done;
711
712 } else {
713 backsql_PrintErrors( bi->sql_db_env,
714 dbh, sth, rc );
715 SQLFreeStmt( sth, SQL_DROP );
716 rs->sr_err = LDAP_OTHER;
717 rs->sr_text = at->bam_ad->ad_cname.bv_val;
718 goto done;
719 }
720 }
721 SQLFreeStmt( sth, SQL_DROP );
722 }
723 break;
724
725 case LDAP_MOD_INCREMENT:
726 Debug( LDAP_DEBUG_TRACE, " backsql_modify_internal(): "
727 "increment not supported yet\n" );
728 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
729 rs->sr_err = LDAP_OTHER;
730 rs->sr_text = "SQL-backend error";
731 goto done;
732 }
733 break;
734 }
735 }
736
737 done:;
738 Debug( LDAP_DEBUG_TRACE, "<==backsql_modify_internal(): %d%s%s\n",
739 rs->sr_err,
740 rs->sr_text ? ": " : "",
741 rs->sr_text ? rs->sr_text : "" );
742
743 /*
744 * FIXME: should fail in case one change fails?
745 */
746 return rs->sr_err;
747 }
748
749 static int
750 backsql_add_attr(
751 Operation *op,
752 SlapReply *rs,
753 SQLHDBC dbh,
754 backsql_oc_map_rec *oc,
755 Attribute *at,
756 backsql_key_t new_keyval )
757 {
758 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
759 backsql_at_map_rec *at_rec = NULL;
760 struct berval *at_val;
761 unsigned long i;
762 RETCODE rc;
763 SQLUSMALLINT currpos;
764 SQLHSTMT sth = SQL_NULL_HSTMT;
765
766 at_rec = backsql_ad2at( oc, at->a_desc );
767
768 if ( at_rec == NULL ) {
769 Debug( LDAP_DEBUG_TRACE, " backsql_add_attr(\"%s\"): "
770 "attribute \"%s\" is not registered "
771 "in objectclass \"%s\"\n",
772 op->ora_e->e_name.bv_val,
773 at->a_desc->ad_cname.bv_val,
774 BACKSQL_OC_NAME( oc ) );
775
776 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
777 rs->sr_text = "operation not permitted "
778 "within namingContext";
779 return rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
780 }
781
782 return LDAP_SUCCESS;
783 }
784
785 if ( at_rec->bam_add_proc == NULL ) {
786 Debug( LDAP_DEBUG_TRACE, " backsql_add_attr(\"%s\"): "
787 "add procedure is not defined "
788 "for attribute \"%s\" "
789 "of structuralObjectClass \"%s\"\n",
790 op->ora_e->e_name.bv_val,
791 at->a_desc->ad_cname.bv_val,
792 BACKSQL_OC_NAME( oc ) );
793
794 if ( BACKSQL_FAIL_IF_NO_MAPPING( bi ) ) {
795 rs->sr_text = "operation not permitted "
796 "within namingContext";
797 return rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
798 }
799
800 return LDAP_SUCCESS;
801 }
802
803 for ( i = 0, at_val = &at->a_vals[ i ];
804 !BER_BVISNULL( at_val );
805 i++, at_val = &at->a_vals[ i ] )
806 {
807 /* procedure return code */
808 int prc = LDAP_SUCCESS;
809 /* first parameter #, parameter order */
810 SQLUSMALLINT pno, po;
811
812 /*
813 * Do not deal with the objectClass that is used
814 * to build the entry
815 */
816 if ( at->a_desc == slap_schema.si_ad_objectClass ) {
817 if ( dn_match( at_val, &oc->bom_oc->soc_cname ) )
818 {
819 continue;
820 }
821 }
822
823 rc = backsql_Prepare( dbh, &sth, at_rec->bam_add_proc, 0 );
824 if ( rc != SQL_SUCCESS ) {
825 rs->sr_text = "SQL-backend error";
826 return rs->sr_err = LDAP_OTHER;
827 }
828
829 if ( BACKSQL_IS_ADD( at_rec->bam_expect_return ) ) {
830 pno = 1;
831 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_OUTPUT, &prc );
832 if ( rc != SQL_SUCCESS ) {
833 Debug( LDAP_DEBUG_TRACE,
834 " backsql_add_attr(): "
835 "error binding output parameter for %s[%lu]\n",
836 at_rec->bam_ad->ad_cname.bv_val, i );
837 backsql_PrintErrors( bi->sql_db_env, dbh,
838 sth, rc );
839 SQLFreeStmt( sth, SQL_DROP );
840
841 rs->sr_text = "SQL-backend error";
842 return rs->sr_err = LDAP_OTHER;
843 }
844
845 } else {
846 pno = 0;
847 }
848
849 po = ( BACKSQL_IS_ADD( at_rec->bam_param_order ) ) > 0;
850 currpos = pno + 1 + po;
851 rc = backsql_BindParamNumID( sth, currpos,
852 SQL_PARAM_INPUT, &new_keyval );
853 if ( rc != SQL_SUCCESS ) {
854 Debug( LDAP_DEBUG_TRACE,
855 " backsql_add_attr(): "
856 "error binding keyval parameter for %s[%lu]\n",
857 at_rec->bam_ad->ad_cname.bv_val, i );
858 backsql_PrintErrors( bi->sql_db_env, dbh,
859 sth, rc );
860 SQLFreeStmt( sth, SQL_DROP );
861
862 rs->sr_text = "SQL-backend error";
863 return rs->sr_err = LDAP_OTHER;
864 }
865
866 currpos = pno + 2 - po;
867
868 /*
869 * check for syntax needed here
870 * maybe need binary bind?
871 */
872
873 rc = backsql_BindParamBerVal( sth, currpos, SQL_PARAM_INPUT, at_val );
874 if ( rc != SQL_SUCCESS ) {
875 Debug( LDAP_DEBUG_TRACE,
876 " backsql_add_attr(): "
877 "error binding value parameter for %s[%lu]\n",
878 at_rec->bam_ad->ad_cname.bv_val, i );
879 backsql_PrintErrors( bi->sql_db_env, dbh,
880 sth, rc );
881 SQLFreeStmt( sth, SQL_DROP );
882
883 rs->sr_text = "SQL-backend error";
884 return rs->sr_err = LDAP_OTHER;
885 }
886
887 #ifdef LDAP_DEBUG
888 Debug(LDAP_DEBUG_TRACE,
889 " backsql_add_attr(\"%s\"): " "executing \"%s\" val[%lu], id=" BACKSQL_IDNUMFMT "\n",
890 op->ora_e->e_name.bv_val, at_rec->bam_add_proc,
891 i, new_keyval );
892 #endif
893 rc = SQLExecute( sth );
894 if ( rc == SQL_SUCCESS && prc == LDAP_SUCCESS ) {
895 rs->sr_err = LDAP_SUCCESS;
896
897 } else {
898 Debug( LDAP_DEBUG_TRACE,
899 " backsql_add_attr(\"%s\"): "
900 "add_proc execution failed (rc=%d, prc=%d)\n",
901 op->ora_e->e_name.bv_val, rc, prc );
902 if ( prc != LDAP_SUCCESS ) {
903 /* SQL procedure executed fine
904 * but returned an error */
905 rs->sr_err = BACKSQL_SANITIZE_ERROR( prc );
906 rs->sr_text = op->ora_e->e_name.bv_val;
907 SQLFreeStmt( sth, SQL_DROP );
908 return rs->sr_err;
909
910 } else {
911 backsql_PrintErrors( bi->sql_db_env, dbh,
912 sth, rc );
913 rs->sr_err = LDAP_OTHER;
914 rs->sr_text = op->ora_e->e_name.bv_val;
915 SQLFreeStmt( sth, SQL_DROP );
916 return rs->sr_err;
917 }
918 }
919 SQLFreeStmt( sth, SQL_DROP );
920 }
921
922 return LDAP_SUCCESS;
923 }
924
925 int
926 backsql_add( Operation *op, SlapReply *rs )
927 {
928 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
929 SQLHDBC dbh = SQL_NULL_HDBC;
930 SQLHSTMT sth = SQL_NULL_HSTMT;
931 backsql_key_t new_keyval = 0;
932 RETCODE rc;
933 backsql_oc_map_rec *oc = NULL;
934 backsql_srch_info bsi = { 0 };
935 Entry p = { 0 }, *e = NULL;
936 Attribute *at,
937 *at_objectClass = NULL;
938 ObjectClass *soc = NULL;
939 struct berval scname = BER_BVNULL;
940 struct berval pdn;
941 struct berval realdn = BER_BVNULL;
942 int colnum;
943 slap_mask_t mask;
944
945 char textbuf[ SLAP_TEXT_BUFLEN ];
946 size_t textlen = sizeof( textbuf );
947
948 #ifdef BACKSQL_SYNCPROV
949 /*
950 * NOTE: fake successful result to force contextCSN to be bumped up
951 */
952 if ( op->o_sync ) {
953 char buf[ LDAP_PVT_CSNSTR_BUFSIZE ];
954 struct berval csn;
955
956 csn.bv_val = buf;
957 csn.bv_len = sizeof( buf );
958 slap_get_csn( op, &csn, 1 );
959
960 rs->sr_err = LDAP_SUCCESS;
961 send_ldap_result( op, rs );
962
963 slap_graduate_commit_csn( op );
964
965 return 0;
966 }
967 #endif /* BACKSQL_SYNCPROV */
968
969 Debug( LDAP_DEBUG_TRACE, "==>backsql_add(\"%s\")\n",
970 op->ora_e->e_name.bv_val );
971
972 /* check schema */
973 if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
974 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
975
976 rs->sr_err = entry_schema_check( op, op->ora_e, NULL, 0, 1, NULL,
977 &rs->sr_text, textbuf, sizeof( textbuf ) );
978 if ( rs->sr_err != LDAP_SUCCESS ) {
979 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
980 "entry failed schema check -- aborting\n",
981 op->ora_e->e_name.bv_val );
982 e = NULL;
983 goto done;
984 }
985 }
986
987 slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
988
989 if ( get_assert( op ) &&
990 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
991 {
992 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
993 "assertion control failed -- aborting\n",
994 op->ora_e->e_name.bv_val );
995 e = NULL;
996 rs->sr_err = LDAP_ASSERTION_FAILED;
997 goto done;
998 }
999
1000 /* search structuralObjectClass */
1001 for ( at = op->ora_e->e_attrs; at != NULL; at = at->a_next ) {
1002 if ( at->a_desc == slap_schema.si_ad_structuralObjectClass ) {
1003 break;
1004 }
1005 }
1006
1007 /* there must exist */
1008 if ( at == NULL ) {
1009 char buf[ SLAP_TEXT_BUFLEN ];
1010 const char *text;
1011
1012 /* search structuralObjectClass */
1013 for ( at = op->ora_e->e_attrs; at != NULL; at = at->a_next ) {
1014 if ( at->a_desc == slap_schema.si_ad_objectClass ) {
1015 break;
1016 }
1017 }
1018
1019 if ( at == NULL ) {
1020 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1021 "no objectClass\n",
1022 op->ora_e->e_name.bv_val );
1023 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
1024 e = NULL;
1025 goto done;
1026 }
1027
1028 rs->sr_err = structural_class( at->a_vals, &soc, NULL,
1029 &text, buf, sizeof( buf ), op->o_tmpmemctx );
1030 if ( rs->sr_err != LDAP_SUCCESS ) {
1031 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1032 "%s (%d)\n",
1033 op->ora_e->e_name.bv_val, text, rs->sr_err );
1034 e = NULL;
1035 goto done;
1036 }
1037 scname = soc->soc_cname;
1038
1039 } else {
1040 scname = at->a_vals[0];
1041 }
1042
1043 /* I guess we should play with sub/supertypes to find a suitable oc */
1044 oc = backsql_name2oc( bi, &scname );
1045
1046 if ( oc == NULL ) {
1047 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1048 "cannot map structuralObjectClass \"%s\" -- aborting\n",
1049 op->ora_e->e_name.bv_val,
1050 scname.bv_val );
1051 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1052 rs->sr_text = "operation not permitted within namingContext";
1053 e = NULL;
1054 goto done;
1055 }
1056
1057 if ( oc->bom_create_proc == NULL ) {
1058 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1059 "create procedure is not defined "
1060 "for structuralObjectClass \"%s\" - aborting\n",
1061 op->ora_e->e_name.bv_val,
1062 scname.bv_val );
1063 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1064 rs->sr_text = "operation not permitted within namingContext";
1065 e = NULL;
1066 goto done;
1067
1068 } else if ( BACKSQL_CREATE_NEEDS_SELECT( bi )
1069 && oc->bom_create_keyval == NULL ) {
1070 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1071 "create procedure needs select procedure, "
1072 "but none is defined for structuralObjectClass \"%s\" "
1073 "- aborting\n",
1074 op->ora_e->e_name.bv_val,
1075 scname.bv_val );
1076 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1077 rs->sr_text = "operation not permitted within namingContext";
1078 e = NULL;
1079 goto done;
1080 }
1081
1082 /* check write access */
1083 if ( !access_allowed_mask( op, op->ora_e,
1084 slap_schema.si_ad_entry,
1085 NULL, ACL_WADD, NULL, &mask ) )
1086 {
1087 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1088 e = op->ora_e;
1089 goto done;
1090 }
1091
1092 rs->sr_err = backsql_get_db_conn( op, &dbh );
1093 if ( rs->sr_err != LDAP_SUCCESS ) {
1094 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1095 "could not get connection handle - exiting\n",
1096 op->ora_e->e_name.bv_val );
1097 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
1098 ? "SQL-backend error" : NULL;
1099 e = NULL;
1100 goto done;
1101 }
1102
1103 /*
1104 * Check if entry exists
1105 *
1106 * NOTE: backsql_api_dn2odbc() is called explicitly because
1107 * we need the mucked DN to pass it to the create procedure.
1108 */
1109 realdn = op->ora_e->e_name;
1110 if ( backsql_api_dn2odbc( op, rs, &realdn ) ) {
1111 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1112 "backsql_api_dn2odbc(\"%s\") failed\n",
1113 op->ora_e->e_name.bv_val, realdn.bv_val );
1114 rs->sr_err = LDAP_OTHER;
1115 rs->sr_text = "SQL-backend error";
1116 e = NULL;
1117 goto done;
1118 }
1119
1120 rs->sr_err = backsql_dn2id( op, rs, dbh, &realdn, NULL, 0, 0 );
1121 if ( rs->sr_err == LDAP_SUCCESS ) {
1122 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1123 "entry exists\n",
1124 op->ora_e->e_name.bv_val );
1125 rs->sr_err = LDAP_ALREADY_EXISTS;
1126 e = op->ora_e;
1127 goto done;
1128 }
1129
1130 /*
1131 * Get the parent dn and see if the corresponding entry exists.
1132 */
1133 if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
1134 pdn = slap_empty_bv;
1135
1136 } else {
1137 dnParent( &op->ora_e->e_nname, &pdn );
1138
1139 /*
1140 * Get the parent
1141 */
1142 bsi.bsi_e = &p;
1143 rs->sr_err = backsql_init_search( &bsi, &pdn,
1144 LDAP_SCOPE_BASE,
1145 (time_t)(-1), NULL, dbh, op, rs, slap_anlist_no_attrs,
1146 ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
1147 if ( rs->sr_err != LDAP_SUCCESS ) {
1148 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1149 "could not retrieve addDN parent "
1150 "\"%s\" ID - %s matched=\"%s\"\n",
1151 pdn.bv_val,
1152 rs->sr_err == LDAP_REFERRAL ? "referral" : "no such entry",
1153 rs->sr_matched ? rs->sr_matched : "(null)" );
1154 e = &p;
1155 goto done;
1156 }
1157
1158 /* check "children" pseudo-attribute access to parent */
1159 if ( !access_allowed( op, &p, slap_schema.si_ad_children,
1160 NULL, ACL_WADD, NULL ) )
1161 {
1162 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1163 e = &p;
1164 goto done;
1165 }
1166 }
1167
1168 /*
1169 * create_proc is executed; if expect_return is set, then
1170 * an output parameter is bound, which should contain
1171 * the id of the added row; otherwise the procedure
1172 * is expected to return the id as the first column of a select
1173 */
1174 rc = backsql_Prepare( dbh, &sth, oc->bom_create_proc, 0 );
1175 if ( rc != SQL_SUCCESS ) {
1176 rs->sr_err = LDAP_OTHER;
1177 rs->sr_text = "SQL-backend error";
1178 e = NULL;
1179 goto done;
1180 }
1181
1182 colnum = 1;
1183 if ( BACKSQL_IS_ADD( oc->bom_expect_return ) ) {
1184 rc = backsql_BindParamNumID( sth, 1, SQL_PARAM_OUTPUT, &new_keyval );
1185 if ( rc != SQL_SUCCESS ) {
1186 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1187 "error binding keyval parameter "
1188 "for objectClass %s\n",
1189 op->ora_e->e_name.bv_val,
1190 oc->bom_oc->soc_cname.bv_val );
1191 backsql_PrintErrors( bi->sql_db_env, dbh,
1192 sth, rc );
1193 SQLFreeStmt( sth, SQL_DROP );
1194
1195 rs->sr_text = "SQL-backend error";
1196 rs->sr_err = LDAP_OTHER;
1197 e = NULL;
1198 goto done;
1199 }
1200 colnum++;
1201 }
1202
1203 if ( oc->bom_create_hint ) {
1204 at = attr_find( op->ora_e->e_attrs, oc->bom_create_hint );
1205 if ( at && at->a_vals ) {
1206 backsql_BindParamStr( sth, colnum, SQL_PARAM_INPUT,
1207 at->a_vals[0].bv_val,
1208 at->a_vals[0].bv_len );
1209 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1210 "create_proc hint: param = '%s'\n",
1211 at->a_vals[0].bv_val );
1212
1213 } else {
1214 backsql_BindParamStr( sth, colnum, SQL_PARAM_INPUT,
1215 "", 0 );
1216 Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
1217 "create_proc hint (%s) not available\n",
1218 oc->bom_create_hint->ad_cname.bv_val );
1219 }
1220 colnum++;
1221 }
1222
1223 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): executing \"%s\"\n",
1224 op->ora_e->e_name.bv_val, oc->bom_create_proc );
1225 rc = SQLExecute( sth );
1226 if ( rc != SQL_SUCCESS ) {
1227 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1228 "create_proc execution failed\n",
1229 op->ora_e->e_name.bv_val );
1230 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc);
1231 SQLFreeStmt( sth, SQL_DROP );
1232 rs->sr_err = LDAP_OTHER;
1233 rs->sr_text = "SQL-backend error";
1234 e = NULL;
1235 goto done;
1236 }
1237
1238 /* FIXME: after SQLExecute(), the row is already inserted
1239 * (at least with PostgreSQL and unixODBC); needs investigation */
1240
1241 if ( !BACKSQL_IS_ADD( oc->bom_expect_return ) ) {
1242 SWORD ncols;
1243 SQLLEN value_len;
1244
1245 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
1246 SQLFreeStmt( sth, SQL_DROP );
1247
1248 rc = backsql_Prepare( dbh, &sth, oc->bom_create_keyval, 0 );
1249 if ( rc != SQL_SUCCESS ) {
1250 rs->sr_err = LDAP_OTHER;
1251 rs->sr_text = "SQL-backend error";
1252 e = NULL;
1253 goto done;
1254 }
1255
1256 rc = SQLExecute( sth );
1257 if ( rc != SQL_SUCCESS ) {
1258 rs->sr_err = LDAP_OTHER;
1259 rs->sr_text = "SQL-backend error";
1260 e = NULL;
1261 goto done;
1262 }
1263 }
1264
1265 /*
1266 * the query to know the id of the inserted entry
1267 * must be embedded in the create procedure
1268 */
1269 rc = SQLNumResultCols( sth, &ncols );
1270 if ( rc != SQL_SUCCESS ) {
1271 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1272 "create_proc result evaluation failed\n",
1273 op->ora_e->e_name.bv_val );
1274 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc);
1275 SQLFreeStmt( sth, SQL_DROP );
1276 rs->sr_err = LDAP_OTHER;
1277 rs->sr_text = "SQL-backend error";
1278 e = NULL;
1279 goto done;
1280
1281 } else if ( ncols != 1 ) {
1282 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1283 "create_proc result is bogus (ncols=%d)\n",
1284 op->ora_e->e_name.bv_val, ncols );
1285 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc);
1286 SQLFreeStmt( sth, SQL_DROP );
1287 rs->sr_err = LDAP_OTHER;
1288 rs->sr_text = "SQL-backend error";
1289 e = NULL;
1290 goto done;
1291 }
1292
1293 #if 0
1294 {
1295 SQLCHAR colname[ 64 ];
1296 SQLSMALLINT name_len, col_type, col_scale, col_null;
1297 UDWORD col_prec;
1298
1299 /*
1300 * FIXME: check whether col_type is compatible,
1301 * if it can be null and so on ...
1302 */
1303 rc = SQLDescribeCol( sth, (SQLUSMALLINT)1,
1304 &colname[ 0 ],
1305 (SQLUINTEGER)( sizeof( colname ) - 1 ),
1306 &name_len, &col_type,
1307 &col_prec, &col_scale, &col_null );
1308 }
1309 #endif
1310
1311 rc = SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_ULONG,
1312 (SQLPOINTER)&new_keyval,
1313 (SQLINTEGER)sizeof( new_keyval ),
1314 &value_len );
1315
1316 rc = SQLFetch( sth );
1317
1318 if ( value_len <= 0 ) {
1319 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1320 "create_proc result is empty?\n",
1321 op->ora_e->e_name.bv_val );
1322 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc);
1323 SQLFreeStmt( sth, SQL_DROP );
1324 rs->sr_err = LDAP_OTHER;
1325 rs->sr_text = "SQL-backend error";
1326 e = NULL;
1327 goto done;
1328 }
1329 }
1330
1331 SQLFreeStmt( sth, SQL_DROP );
1332
1333 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1334 "create_proc returned keyval=" BACKSQL_IDNUMFMT "\n",
1335 op->ora_e->e_name.bv_val, new_keyval );
1336
1337 rc = backsql_Prepare( dbh, &sth, bi->sql_insentry_stmt, 0 );
1338 if ( rc != SQL_SUCCESS ) {
1339 rs->sr_err = LDAP_OTHER;
1340 rs->sr_text = "SQL-backend error";
1341 e = NULL;
1342 goto done;
1343 }
1344
1345 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &realdn );
1346 if ( rc != SQL_SUCCESS ) {
1347 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1348 "error binding DN parameter for objectClass %s\n",
1349 op->ora_e->e_name.bv_val,
1350 oc->bom_oc->soc_cname.bv_val );
1351 backsql_PrintErrors( bi->sql_db_env, dbh,
1352 sth, rc );
1353 SQLFreeStmt( sth, SQL_DROP );
1354
1355 rs->sr_text = "SQL-backend error";
1356 rs->sr_err = LDAP_OTHER;
1357 e = NULL;
1358 goto done;
1359 }
1360
1361 rc = backsql_BindParamNumID( sth, 2, SQL_PARAM_INPUT, &oc->bom_id );
1362 if ( rc != SQL_SUCCESS ) {
1363 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1364 "error binding objectClass ID parameter "
1365 "for objectClass %s\n",
1366 op->ora_e->e_name.bv_val,
1367 oc->bom_oc->soc_cname.bv_val );
1368 backsql_PrintErrors( bi->sql_db_env, dbh,
1369 sth, rc );
1370 SQLFreeStmt( sth, SQL_DROP );
1371
1372 rs->sr_text = "SQL-backend error";
1373 rs->sr_err = LDAP_OTHER;
1374 e = NULL;
1375 goto done;
1376 }
1377
1378 rc = backsql_BindParamID( sth, 3, SQL_PARAM_INPUT, &bsi.bsi_base_id.eid_id );
1379 if ( rc != SQL_SUCCESS ) {
1380 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1381 "error binding parent ID parameter "
1382 "for objectClass %s\n",
1383 op->ora_e->e_name.bv_val,
1384 oc->bom_oc->soc_cname.bv_val );
1385 backsql_PrintErrors( bi->sql_db_env, dbh,
1386 sth, rc );
1387 SQLFreeStmt( sth, SQL_DROP );
1388
1389 rs->sr_text = "SQL-backend error";
1390 rs->sr_err = LDAP_OTHER;
1391 e = NULL;
1392 goto done;
1393 }
1394
1395 rc = backsql_BindParamNumID( sth, 4, SQL_PARAM_INPUT, &new_keyval );
1396 if ( rc != SQL_SUCCESS ) {
1397 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1398 "error binding entry ID parameter "
1399 "for objectClass %s\n",
1400 op->ora_e->e_name.bv_val,
1401 oc->bom_oc->soc_cname.bv_val );
1402 backsql_PrintErrors( bi->sql_db_env, dbh,
1403 sth, rc );
1404 SQLFreeStmt( sth, SQL_DROP );
1405
1406 rs->sr_text = "SQL-backend error";
1407 rs->sr_err = LDAP_OTHER;
1408 e = NULL;
1409 goto done;
1410 }
1411
1412 Debug(LDAP_DEBUG_TRACE,
1413 " backsql_add(): executing \"%s\" for dn=\"%s\" oc_map_id=" BACKSQL_IDNUMFMT " p_id=" BACKSQL_IDFMT " keyval=" BACKSQL_IDNUMFMT "\n",
1414 bi->sql_insentry_stmt, op->ora_e->e_name.bv_val,
1415 oc->bom_id, BACKSQL_IDARG(bsi.bsi_base_id.eid_id),
1416 new_keyval );
1417
1418 rc = SQLExecute( sth );
1419 if ( rc != SQL_SUCCESS ) {
1420 Debug( LDAP_DEBUG_TRACE, " backsql_add(\"%s\"): "
1421 "could not insert ldap_entries record\n",
1422 op->ora_e->e_name.bv_val );
1423 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
1424
1425 /*
1426 * execute delete_proc to delete data added !!!
1427 */
1428 SQLFreeStmt( sth, SQL_DROP );
1429 rs->sr_err = LDAP_OTHER;
1430 rs->sr_text = "SQL-backend error";
1431 e = NULL;
1432 goto done;
1433 }
1434
1435 SQLFreeStmt( sth, SQL_DROP );
1436
1437 for ( at = op->ora_e->e_attrs; at != NULL; at = at->a_next ) {
1438 Debug( LDAP_DEBUG_TRACE, " backsql_add(): "
1439 "adding attribute \"%s\"\n",
1440 at->a_desc->ad_cname.bv_val );
1441
1442 /*
1443 * Skip:
1444 * - the first occurrence of objectClass, which is used
1445 * to determine how to build the SQL entry (FIXME ?!?)
1446 * - operational attributes
1447 * - empty attributes (FIXME ?!?)
1448 */
1449 if ( backsql_attr_skip( at->a_desc, at->a_vals ) ) {
1450 continue;
1451 }
1452
1453 if ( at->a_desc == slap_schema.si_ad_objectClass ) {
1454 at_objectClass = at;
1455 continue;
1456 }
1457
1458 rs->sr_err = backsql_add_attr( op, rs, dbh, oc, at, new_keyval );
1459 if ( rs->sr_err != LDAP_SUCCESS ) {
1460 e = op->ora_e;
1461 goto done;
1462 }
1463 }
1464
1465 if ( at_objectClass ) {
1466 rs->sr_err = backsql_add_attr( op, rs, dbh, oc,
1467 at_objectClass, new_keyval );
1468 if ( rs->sr_err != LDAP_SUCCESS ) {
1469 e = op->ora_e;
1470 goto done;
1471 }
1472 }
1473
1474 done:;
1475 /*
1476 * Commit only if all operations succeed
1477 */
1478 if ( sth != SQL_NULL_HSTMT ) {
1479 SQLUSMALLINT CompletionType = SQL_ROLLBACK;
1480
1481 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
1482 assert( e == NULL );
1483 CompletionType = SQL_COMMIT;
1484 }
1485
1486 SQLTransact( SQL_NULL_HENV, dbh, CompletionType );
1487 }
1488
1489 /*
1490 * FIXME: NOOP does not work for add -- it works for all
1491 * the other operations, and I don't get the reason :(
1492 *
1493 * hint: there might be some autocommit in Postgres
1494 * so that when the unique id of the key table is
1495 * automatically increased, there's no rollback.
1496 * We might implement a "rollback" procedure consisting
1497 * in deleting that row.
1498 */
1499
1500 if ( e != NULL ) {
1501 int disclose = 1;
1502
1503 if ( e == op->ora_e && !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
1504 /* mask already collected */
1505 disclose = 0;
1506
1507 } else if ( e == &p && !access_allowed( op, &p,
1508 slap_schema.si_ad_entry, NULL,
1509 ACL_DISCLOSE, NULL ) )
1510 {
1511 disclose = 0;
1512 }
1513
1514 if ( disclose == 0 ) {
1515 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1516 rs->sr_text = NULL;
1517 rs->sr_matched = NULL;
1518 if ( rs->sr_ref ) {
1519 ber_bvarray_free( rs->sr_ref );
1520 rs->sr_ref = NULL;
1521 }
1522 }
1523 }
1524
1525 if ( op->o_noop && rs->sr_err == LDAP_SUCCESS ) {
1526 rs->sr_err = LDAP_X_NO_OPERATION;
1527 }
1528
1529 send_ldap_result( op, rs );
1530 slap_graduate_commit_csn( op );
1531
1532 if ( !BER_BVISNULL( &realdn )
1533 && realdn.bv_val != op->ora_e->e_name.bv_val )
1534 {
1535 ch_free( realdn.bv_val );
1536 }
1537
1538 if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
1539 (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
1540 }
1541
1542 if ( !BER_BVISNULL( &p.e_nname ) ) {
1543 backsql_entry_clean( op, &p );
1544 }
1545
1546 Debug( LDAP_DEBUG_TRACE, "<==backsql_add(\"%s\"): %d \"%s\"\n",
1547 op->ora_e->e_name.bv_val,
1548 rs->sr_err,
1549 rs->sr_text ? rs->sr_text : "" );
1550
1551 rs->sr_text = NULL;
1552 rs->sr_matched = NULL;
1553 if ( rs->sr_ref ) {
1554 ber_bvarray_free( rs->sr_ref );
1555 rs->sr_ref = NULL;
1556 }
1557
1558 return rs->sr_err;
1559 }
1560