Debian Codegraph @

servers/slapd/back-sql/entry-id.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 #include "portable.h"
25
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include "ac/string.h"
29
30 #include "lutil.h"
31 #include "slap.h"
32 #include "proto-sql.h"
33
34 #ifdef BACKSQL_ARBITRARY_KEY
35 struct berval backsql_baseObject_bv = BER_BVC( BACKSQL_BASEOBJECT_IDSTR );
36 #endif /* BACKSQL_ARBITRARY_KEY */
37
38 backsql_entryID *
39 backsql_entryID_dup( backsql_entryID *src, void *ctx )
40 {
41 backsql_entryID *dst;
42
43 if ( src == NULL ) return NULL;
44
45 dst = slap_sl_calloc( 1, sizeof( backsql_entryID ), ctx );
46 ber_dupbv_x( &dst->eid_ndn, &src->eid_ndn, ctx );
47 if ( src->eid_dn.bv_val == src->eid_ndn.bv_val ) {
48 dst->eid_dn = dst->eid_ndn;
49 } else {
50 ber_dupbv_x( &dst->eid_dn, &src->eid_dn, ctx );
51 }
52
53 #ifdef BACKSQL_ARBITRARY_KEY
54 ber_dupbv_x( &dst->eid_id, &src->eid_id, ctx );
55 ber_dupbv_x( &dst->eid_keyval, &src->eid_keyval, ctx );
56 #else /* ! BACKSQL_ARBITRARY_KEY */
57 dst->eid_id = src->eid_id;
58 dst->eid_keyval = src->eid_keyval;
59 #endif /* ! BACKSQL_ARBITRARY_KEY */
60
61 dst->eid_oc = src->eid_oc;
62 dst->eid_oc_id = src->eid_oc_id;
63
64 return dst;
65 }
66
67 backsql_entryID *
68 backsql_free_entryID( backsql_entryID *id, int freeit, void *ctx )
69 {
70 backsql_entryID *next;
71
72 assert( id != NULL );
73
74 next = id->eid_next;
75
76 if ( !BER_BVISNULL( &id->eid_ndn ) ) {
77 if ( !BER_BVISNULL( &id->eid_dn )
78 && id->eid_dn.bv_val != id->eid_ndn.bv_val )
79 {
80 slap_sl_free( id->eid_dn.bv_val, ctx );
81 BER_BVZERO( &id->eid_dn );
82 }
83
84 slap_sl_free( id->eid_ndn.bv_val, ctx );
85 BER_BVZERO( &id->eid_ndn );
86 }
87
88 #ifdef BACKSQL_ARBITRARY_KEY
89 if ( !BER_BVISNULL( &id->eid_id ) ) {
90 slap_sl_free( id->eid_id.bv_val, ctx );
91 BER_BVZERO( &id->eid_id );
92 }
93
94 if ( !BER_BVISNULL( &id->eid_keyval ) ) {
95 slap_sl_free( id->eid_keyval.bv_val, ctx );
96 BER_BVZERO( &id->eid_keyval );
97 }
98 #endif /* BACKSQL_ARBITRARY_KEY */
99
100 if ( freeit ) {
101 slap_sl_free( id, ctx );
102 }
103
104 return next;
105 }
106
107 /*
108 * NOTE: the dn must be normalized
109 */
110 int
111 backsql_dn2id(
112 Operation *op,
113 SlapReply *rs,
114 SQLHDBC dbh,
115 struct berval *ndn,
116 backsql_entryID *id,
117 int matched,
118 int muck )
119 {
120 backsql_info *bi = op->o_bd->be_private;
121 SQLHSTMT sth = SQL_NULL_HSTMT;
122 BACKSQL_ROW_NTS row = { 0 };
123 RETCODE rc;
124 int res;
125 struct berval realndn = BER_BVNULL;
126
127 /* TimesTen */
128 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
129 struct berval tbbDN;
130 int i, j;
131
132 /*
133 * NOTE: id can be NULL; in this case, the function
134 * simply checks whether the DN can be successfully
135 * turned into an ID, returning LDAP_SUCCESS for
136 * positive cases, or the most appropriate error
137 */
138
139 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(\"%s\")%s%s\n",
140 ndn->bv_val, id == NULL ? " (no ID expected)" : "",
141 matched ? " matched expected" : "" );
142
143 if ( id ) {
144 /* NOTE: trap inconsistencies */
145 assert( BER_BVISNULL( &id->eid_ndn ) );
146 }
147
148 if ( ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
149 Debug( LDAP_DEBUG_TRACE,
150 " backsql_dn2id(\"%s\"): DN length=%ld "
151 "exceeds max DN length %d:\n",
152 ndn->bv_val, ndn->bv_len, BACKSQL_MAX_DN_LEN );
153 return LDAP_OTHER;
154 }
155
156 /* return baseObject if available and matches */
157 /* FIXME: if ndn is already mucked, we cannot check this */
158 if ( bi->sql_baseObject != NULL &&
159 dn_match( ndn, &bi->sql_baseObject->e_nname ) )
160 {
161 if ( id != NULL ) {
162 #ifdef BACKSQL_ARBITRARY_KEY
163 ber_dupbv_x( &id->eid_id, &backsql_baseObject_bv,
164 op->o_tmpmemctx );
165 ber_dupbv_x( &id->eid_keyval, &backsql_baseObject_bv,
166 op->o_tmpmemctx );
167 #else /* ! BACKSQL_ARBITRARY_KEY */
168 id->eid_id = BACKSQL_BASEOBJECT_ID;
169 id->eid_keyval = BACKSQL_BASEOBJECT_KEYVAL;
170 #endif /* ! BACKSQL_ARBITRARY_KEY */
171 id->eid_oc_id = BACKSQL_BASEOBJECT_OC;
172
173 ber_dupbv_x( &id->eid_ndn, &bi->sql_baseObject->e_nname,
174 op->o_tmpmemctx );
175 ber_dupbv_x( &id->eid_dn, &bi->sql_baseObject->e_name,
176 op->o_tmpmemctx );
177
178 id->eid_next = NULL;
179 }
180
181 return LDAP_SUCCESS;
182 }
183
184 /* begin TimesTen */
185 assert( bi->sql_id_query != NULL );
186 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): id_query \"%s\"\n",
187 ndn->bv_val, bi->sql_id_query );
188 rc = backsql_Prepare( dbh, &sth, bi->sql_id_query, 0 );
189 if ( rc != SQL_SUCCESS ) {
190 Debug( LDAP_DEBUG_TRACE,
191 " backsql_dn2id(\"%s\"): "
192 "error preparing SQL:\n %s",
193 ndn->bv_val, bi->sql_id_query );
194 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
195 res = LDAP_OTHER;
196 goto done;
197 }
198
199 realndn = *ndn;
200 if ( muck ) {
201 if ( backsql_api_dn2odbc( op, rs, &realndn ) ) {
202 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
203 "backsql_api_dn2odbc(\"%s\") failed\n",
204 ndn->bv_val, realndn.bv_val );
205 res = LDAP_OTHER;
206 goto done;
207 }
208 }
209
210 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
211 /*
212 * Prepare an upper cased, byte reversed version
213 * that can be searched using indexes
214 */
215
216 for ( i = 0, j = realndn.bv_len - 1; realndn.bv_val[ i ]; i++, j--)
217 {
218 upperdn[ i ] = realndn.bv_val[ j ];
219 }
220 upperdn[ i ] = '\0';
221 ldap_pvt_str2upper( upperdn );
222
223 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
224 "upperdn=\"%s\"\n",
225 ndn->bv_val, upperdn );
226 ber_str2bv( upperdn, 0, 0, &tbbDN );
227
228 } else {
229 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
230 AC_MEMCPY( upperdn, realndn.bv_val, realndn.bv_len + 1 );
231 ldap_pvt_str2upper( upperdn );
232 Debug( LDAP_DEBUG_TRACE,
233 " backsql_dn2id(\"%s\"): "
234 "upperdn=\"%s\"\n",
235 ndn->bv_val, upperdn );
236 ber_str2bv( upperdn, 0, 0, &tbbDN );
237
238 } else {
239 tbbDN = realndn;
240 }
241 }
242
243 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &tbbDN );
244 if ( rc != SQL_SUCCESS) {
245 /* end TimesTen */
246 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
247 "error binding dn=\"%s\" parameter:\n",
248 ndn->bv_val, tbbDN.bv_val );
249 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
250 res = LDAP_OTHER;
251 goto done;
252 }
253
254 rc = SQLExecute( sth );
255 if ( rc != SQL_SUCCESS ) {
256 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
257 "error executing query (\"%s\", \"%s\"):\n",
258 ndn->bv_val, bi->sql_id_query, tbbDN.bv_val );
259 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
260 res = LDAP_OTHER;
261 goto done;
262 }
263
264 backsql_BindRowAsStrings_x( sth, &row, op->o_tmpmemctx );
265 rc = SQLFetch( sth );
266 if ( BACKSQL_SUCCESS( rc ) ) {
267 #ifdef LDAP_DEBUG
268 Debug(LDAP_DEBUG_TRACE,
269 " backsql_dn2id(\"%s\"): id=%s keyval=%s oc_id=%s dn=%s\n",
270 ndn->bv_val, row.cols[0], row.cols[1], row.cols[2],
271 row.cols[3] );
272 #endif /* LDAP_DEBUG */
273
274 res = LDAP_SUCCESS;
275 if ( id != NULL ) {
276 struct berval dn;
277
278 id->eid_next = NULL;
279
280 #ifdef BACKSQL_ARBITRARY_KEY
281 ber_str2bv_x( row.cols[ 0 ], 0, 1, &id->eid_id,
282 op->o_tmpmemctx );
283 ber_str2bv_x( row.cols[ 1 ], 0, 1, &id->eid_keyval,
284 op->o_tmpmemctx );
285 #else /* ! BACKSQL_ARBITRARY_KEY */
286 if ( BACKSQL_STR2ID( &id->eid_id, row.cols[ 0 ], 0 ) != 0 ) {
287 res = LDAP_OTHER;
288 goto done;
289 }
290 if ( BACKSQL_STR2ID( &id->eid_keyval, row.cols[ 1 ], 0 ) != 0 ) {
291 res = LDAP_OTHER;
292 goto done;
293 }
294 #endif /* ! BACKSQL_ARBITRARY_KEY */
295 if ( BACKSQL_STR2ID( &id->eid_oc_id, row.cols[ 2 ], 0 ) != 0 ) {
296 res = LDAP_OTHER;
297 goto done;
298 }
299
300 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
301
302 if ( backsql_api_odbc2dn( op, rs, &dn ) ) {
303 res = LDAP_OTHER;
304 goto done;
305 }
306
307 res = dnPrettyNormal( NULL, &dn,
308 &id->eid_dn, &id->eid_ndn,
309 op->o_tmpmemctx );
310 if ( res != LDAP_SUCCESS ) {
311 Debug( LDAP_DEBUG_TRACE,
312 " backsql_dn2id(\"%s\"): "
313 "dnPrettyNormal failed (%d: %s)\n",
314 realndn.bv_val, res,
315 ldap_err2string( res ) );
316
317 /* cleanup... */
318 (void)backsql_free_entryID( id, 0, op->o_tmpmemctx );
319 }
320
321 if ( dn.bv_val != row.cols[ 3 ] ) {
322 free( dn.bv_val );
323 }
324 }
325
326 } else {
327 res = LDAP_NO_SUCH_OBJECT;
328 if ( matched ) {
329 struct berval pdn = *ndn;
330
331 /*
332 * Look for matched
333 */
334 rs->sr_matched = NULL;
335 while ( !be_issuffix( op->o_bd, &pdn ) ) {
336 char *matchedDN = NULL;
337
338 dnParent( &pdn, &pdn );
339
340 /*
341 * Empty DN ("") defaults to LDAP_SUCCESS
342 */
343 rs->sr_err = backsql_dn2id( op, rs, dbh, &pdn, id, 0, 1 );
344 switch ( rs->sr_err ) {
345 case LDAP_NO_SUCH_OBJECT:
346 /* try another one */
347 break;
348
349 case LDAP_SUCCESS:
350 matchedDN = pdn.bv_val;
351 /* fail over to next case */
352
353 default:
354 rs->sr_err = LDAP_NO_SUCH_OBJECT;
355 rs->sr_matched = matchedDN;
356 goto done;
357 }
358 }
359 }
360 }
361
362 done:;
363 backsql_FreeRow_x( &row, op->o_tmpmemctx );
364
365 Debug( LDAP_DEBUG_TRACE,
366 "<==backsql_dn2id(\"%s\"): err=%d\n",
367 ndn->bv_val, res );
368 if ( sth != SQL_NULL_HSTMT ) {
369 SQLFreeStmt( sth, SQL_DROP );
370 }
371
372 if ( !BER_BVISNULL( &realndn ) && realndn.bv_val != ndn->bv_val ) {
373 ch_free( realndn.bv_val );
374 }
375
376 return res;
377 }
378
379 int
380 backsql_count_children(
381 Operation *op,
382 SQLHDBC dbh,
383 struct berval *dn,
384 unsigned long *nchildren )
385 {
386 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
387 SQLHSTMT sth = SQL_NULL_HSTMT;
388 BACKSQL_ROW_NTS row;
389 RETCODE rc;
390 int res = LDAP_SUCCESS;
391
392 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n",
393 dn->bv_val );
394
395 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
396 Debug( LDAP_DEBUG_TRACE,
397 "backsql_count_children(): DN \"%s\" (%ld bytes) "
398 "exceeds max DN length (%d):\n",
399 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
400 return LDAP_OTHER;
401 }
402
403 /* begin TimesTen */
404 assert( bi->sql_has_children_query != NULL );
405 Debug(LDAP_DEBUG_TRACE, "children id query \"%s\"\n",
406 bi->sql_has_children_query );
407 rc = backsql_Prepare( dbh, &sth, bi->sql_has_children_query, 0 );
408 if ( rc != SQL_SUCCESS ) {
409 Debug( LDAP_DEBUG_TRACE,
410 "backsql_count_children(): error preparing SQL:\n%s",
411 bi->sql_has_children_query );
412 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
413 SQLFreeStmt( sth, SQL_DROP );
414 return LDAP_OTHER;
415 }
416
417 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
418 if ( rc != SQL_SUCCESS) {
419 /* end TimesTen */
420 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
421 "error binding dn=\"%s\" parameter:\n",
422 dn->bv_val );
423 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
424 SQLFreeStmt( sth, SQL_DROP );
425 return LDAP_OTHER;
426 }
427
428 rc = SQLExecute( sth );
429 if ( rc != SQL_SUCCESS ) {
430 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
431 "error executing query (\"%s\", \"%s\"):\n",
432 bi->sql_has_children_query, dn->bv_val );
433 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
434 SQLFreeStmt( sth, SQL_DROP );
435 return LDAP_OTHER;
436 }
437
438 backsql_BindRowAsStrings_x( sth, &row, op->o_tmpmemctx );
439
440 rc = SQLFetch( sth );
441 if ( BACKSQL_SUCCESS( rc ) ) {
442 char *end;
443
444 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
445 if ( end == row.cols[ 0 ] ) {
446 res = LDAP_OTHER;
447
448 } else {
449 switch ( end[ 0 ] ) {
450 case '\0':
451 break;
452
453 case '.': {
454 unsigned long ul;
455
456 /* FIXME: braindead RDBMSes return
457 * a fractional number from COUNT!
458 */
459 if ( lutil_atoul( &ul, end + 1 ) != 0 || ul != 0 ) {
460 res = LDAP_OTHER;
461 }
462 } break;
463
464 default:
465 res = LDAP_OTHER;
466 }
467 }
468
469 } else {
470 res = LDAP_OTHER;
471 }
472 backsql_FreeRow_x( &row, op->o_tmpmemctx );
473
474 SQLFreeStmt( sth, SQL_DROP );
475
476 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
477 *nchildren );
478
479 return res;
480 }
481
482 int
483 backsql_has_children(
484 Operation *op,
485 SQLHDBC dbh,
486 struct berval *dn )
487 {
488 unsigned long nchildren;
489 int rc;
490
491 rc = backsql_count_children( op, dbh, dn, &nchildren );
492
493 if ( rc == LDAP_SUCCESS ) {
494 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
495 }
496
497 return rc;
498 }
499
500 static int
501 backsql_get_attr_vals( void *v_at, void *v_bsi )
502 {
503 backsql_at_map_rec *at = v_at;
504 backsql_srch_info *bsi = v_bsi;
505 backsql_info *bi;
506 RETCODE rc;
507 SQLHSTMT sth = SQL_NULL_HSTMT;
508 BACKSQL_ROW_NTS row;
509 unsigned long i,
510 k = 0,
511 oldcount = 0,
512 res = 0;
513 #ifdef BACKSQL_COUNTQUERY
514 unsigned count,
515 j,
516 append = 0;
517 SQLLEN countsize = sizeof( count );
518 Attribute *attr = NULL;
519
520 slap_mr_normalize_func *normfunc = NULL;
521 #endif /* BACKSQL_COUNTQUERY */
522 #ifdef BACKSQL_PRETTY_VALIDATE
523 slap_syntax_validate_func *validate = NULL;
524 slap_syntax_transform_func *pretty = NULL;
525 #endif /* BACKSQL_PRETTY_VALIDATE */
526
527 assert( at != NULL );
528 assert( bsi != NULL );
529 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
530 "oc=\"%s\" attr=\"%s\" keyval=" BACKSQL_IDFMT "\n",
531 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
532 BACKSQL_IDARG(bsi->bsi_c_eid->eid_keyval) );
533
534 bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
535
536 #ifdef BACKSQL_PRETTY_VALIDATE
537 validate = at->bam_true_ad->ad_type->sat_syntax->ssyn_validate;
538 pretty = at->bam_true_ad->ad_type->sat_syntax->ssyn_pretty;
539
540 if ( validate == NULL && pretty == NULL ) {
541 return 1;
542 }
543 #endif /* BACKSQL_PRETTY_VALIDATE */
544
545 #ifdef BACKSQL_COUNTQUERY
546 if ( at->bam_true_ad->ad_type->sat_equality ) {
547 normfunc = at->bam_true_ad->ad_type->sat_equality->smr_normalize;
548 }
549
550 /* Count how many rows will be returned. This avoids memory
551 * fragmentation that can result from loading the values in
552 * one by one and using realloc()
553 */
554 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_countquery, 0 );
555 if ( rc != SQL_SUCCESS ) {
556 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
557 "error preparing count query: %s\n",
558 at->bam_countquery );
559 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
560 return 1;
561 }
562
563 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
564 &bsi->bsi_c_eid->eid_keyval );
565 if ( rc != SQL_SUCCESS ) {
566 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
567 "error binding key value parameter\n" );
568 SQLFreeStmt( sth, SQL_DROP );
569 return 1;
570 }
571
572 rc = SQLExecute( sth );
573 if ( ! BACKSQL_SUCCESS( rc ) ) {
574 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
575 "error executing attribute count query '%s'\n",
576 at->bam_countquery );
577 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
578 SQLFreeStmt( sth, SQL_DROP );
579 return 1;
580 }
581
582 SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_LONG,
583 (SQLPOINTER)&count,
584 (SQLINTEGER)sizeof( count ),
585 &countsize );
586
587 rc = SQLFetch( sth );
588 if ( rc != SQL_SUCCESS ) {
589 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
590 "error fetch results of count query: %s\n",
591 at->bam_countquery );
592 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
593 SQLFreeStmt( sth, SQL_DROP );
594 return 1;
595 }
596
597 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
598 "number of values in query: %u\n", count );
599 SQLFreeStmt( sth, SQL_DROP );
600 if ( count == 0 ) {
601 return 1;
602 }
603
604 attr = attr_find( bsi->bsi_e->e_attrs, at->bam_true_ad );
605 if ( attr != NULL ) {
606 BerVarray tmp;
607
608 if ( attr->a_vals != NULL ) {
609 oldcount = attr->a_numvals;
610 }
611
612 tmp = ch_realloc( attr->a_vals, ( oldcount + count + 1 ) * sizeof( struct berval ) );
613 if ( tmp == NULL ) {
614 return 1;
615 }
616 attr->a_vals = tmp;
617 memset( &attr->a_vals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) );
618
619 if ( normfunc ) {
620 tmp = ch_realloc( attr->a_nvals, ( oldcount + count + 1 ) * sizeof( struct berval ) );
621 if ( tmp == NULL ) {
622 return 1;
623 }
624 attr->a_nvals = tmp;
625 memset( &attr->a_nvals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) );
626
627 } else {
628 attr->a_nvals = attr->a_vals;
629 }
630 attr->a_numvals += count;
631
632 } else {
633 append = 1;
634
635 /* Make space for the array of values */
636 attr = attr_alloc( at->bam_true_ad );
637 attr->a_numvals = count;
638 attr->a_vals = ch_calloc( count + 1, sizeof( struct berval ) );
639 if ( attr->a_vals == NULL ) {
640 Debug( LDAP_DEBUG_TRACE, "Out of memory!\n" );
641 ch_free( attr );
642 return 1;
643 }
644 if ( normfunc ) {
645 attr->a_nvals = ch_calloc( count + 1, sizeof( struct berval ) );
646 if ( attr->a_nvals == NULL ) {
647 ch_free( attr->a_vals );
648 ch_free( attr );
649 return 1;
650
651 }
652
653 } else {
654 attr->a_nvals = attr->a_vals;
655 }
656 }
657 #endif /* BACKSQL_COUNTQUERY */
658
659 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
660 if ( rc != SQL_SUCCESS ) {
661 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
662 "error preparing query: %s\n", at->bam_query );
663 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
664 #ifdef BACKSQL_COUNTQUERY
665 if ( append ) {
666 attr_free( attr );
667 }
668 #endif /* BACKSQL_COUNTQUERY */
669 return 1;
670 }
671
672 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
673 &bsi->bsi_c_eid->eid_keyval );
674 if ( rc != SQL_SUCCESS ) {
675 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
676 "error binding key value parameter\n" );
677 #ifdef BACKSQL_COUNTQUERY
678 if ( append ) {
679 attr_free( attr );
680 }
681 #endif /* BACKSQL_COUNTQUERY */
682 return 1;
683 }
684
685 #ifdef BACKSQL_TRACE
686 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
687 "query=\"%s\" keyval=" BACKSQL_IDFMT "\n", at->bam_query,
688 BACKSQL_IDARG(bsi->bsi_c_eid->eid_keyval) );
689 #endif /* BACKSQL_TRACE */
690
691 rc = SQLExecute( sth );
692 if ( ! BACKSQL_SUCCESS( rc ) ) {
693 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
694 "error executing attribute query \"%s\"\n",
695 at->bam_query );
696 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
697 SQLFreeStmt( sth, SQL_DROP );
698 #ifdef BACKSQL_COUNTQUERY
699 if ( append ) {
700 attr_free( attr );
701 }
702 #endif /* BACKSQL_COUNTQUERY */
703 return 1;
704 }
705
706 backsql_BindRowAsStrings_x( sth, &row, bsi->bsi_op->o_tmpmemctx );
707 #ifdef BACKSQL_COUNTQUERY
708 j = oldcount;
709 #endif /* BACKSQL_COUNTQUERY */
710 for ( rc = SQLFetch( sth ), k = 0;
711 BACKSQL_SUCCESS( rc );
712 rc = SQLFetch( sth ), k++ )
713 {
714 for ( i = 0; i < (unsigned long)row.ncols; i++ ) {
715
716 if ( row.value_len[ i ] > 0 ) {
717 struct berval bv;
718 int retval;
719 #ifdef BACKSQL_TRACE
720 AttributeDescription *ad = NULL;
721 const char *text;
722
723 retval = slap_bv2ad( &row.col_names[ i ], &ad, &text );
724 if ( retval != LDAP_SUCCESS ) {
725 Debug( LDAP_DEBUG_ANY,
726 "==>backsql_get_attr_vals(\"%s\"): "
727 "unable to find AttributeDescription %s "
728 "in schema (%d)\n",
729 bsi->bsi_e->e_name.bv_val,
730 row.col_names[ i ].bv_val, retval );
731 res = 1;
732 goto done;
733 }
734
735 if ( ad != at->bam_ad ) {
736 Debug( LDAP_DEBUG_ANY,
737 "==>backsql_get_attr_vals(\"%s\"): "
738 "column name %s differs from "
739 "AttributeDescription %s\n",
740 bsi->bsi_e->e_name.bv_val,
741 ad->ad_cname.bv_val,
742 at->bam_ad->ad_cname.bv_val );
743 res = 1;
744 goto done;
745 }
746 #endif /* BACKSQL_TRACE */
747
748 /* ITS#3386, ITS#3113 - 20070308
749 * If a binary is fetched?
750 * must use the actual size read
751 * from the database.
752 */
753 if ( BACKSQL_IS_BINARY( row.col_type[ i ] ) ) {
754 #ifdef BACKSQL_TRACE
755 Debug( LDAP_DEBUG_ANY,
756 "==>backsql_get_attr_vals(\"%s\"): "
757 "column name %s: data is binary; "
758 "using database size %ld\n",
759 bsi->bsi_e->e_name.bv_val,
760 ad->ad_cname.bv_val,
761 row.value_len[ i ] );
762 #endif /* BACKSQL_TRACE */
763 bv.bv_val = row.cols[ i ];
764 bv.bv_len = row.value_len[ i ];
765
766 } else {
767 ber_str2bv( row.cols[ i ], 0, 0, &bv );
768 }
769
770 #ifdef BACKSQL_PRETTY_VALIDATE
771 if ( pretty ) {
772 struct berval pbv;
773
774 retval = pretty( at->bam_true_ad->ad_type->sat_syntax,
775 &bv, &pbv, bsi->bsi_op->o_tmpmemctx );
776 bv = pbv;
777
778 } else {
779 retval = validate( at->bam_true_ad->ad_type->sat_syntax,
780 &bv );
781 }
782
783 if ( retval != LDAP_SUCCESS ) {
784 /* FIXME: we're ignoring invalid values,
785 * but we're accepting the attributes;
786 * should we fail at all? */
787 Debug(LDAP_DEBUG_TRACE,
788 "==>backsql_get_attr_vals(\"%s\"): " "unable to %s value #%lu " "of AttributeDescription %s (%d)\n",
789 bsi->bsi_e->e_name.bv_val,
790 pretty ? "prettify" : "validate",
791 k - oldcount,
792 at->bam_ad->ad_cname.bv_val,
793 retval );
794 continue;
795 }
796 #endif /* BACKSQL_PRETTY_VALIDATE */
797
798 #ifndef BACKSQL_COUNTQUERY
799 (void)backsql_entry_addattr( bsi->bsi_e,
800 at->bam_true_ad, &bv,
801 bsi->bsi_op->o_tmpmemctx );
802
803 #else /* BACKSQL_COUNTQUERY */
804 if ( normfunc ) {
805 struct berval nbv;
806
807 retval = (*normfunc)( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
808 at->bam_true_ad->ad_type->sat_syntax,
809 at->bam_true_ad->ad_type->sat_equality,
810 &bv, &nbv,
811 bsi->bsi_op->o_tmpmemctx );
812
813 if ( retval != LDAP_SUCCESS ) {
814 /* FIXME: we're ignoring invalid values,
815 * but we're accepting the attributes;
816 * should we fail at all? */
817 Debug(LDAP_DEBUG_TRACE,
818 "==>backsql_get_attr_vals(\"%s\"): " "unable to normalize value #%lu " "of AttributeDescription %s (%d)\n",
819 bsi->bsi_e->e_name.bv_val,
820 k - oldcount,
821 at->bam_ad->ad_cname.bv_val,
822 retval );
823
824 #ifdef BACKSQL_PRETTY_VALIDATE
825 if ( pretty ) {
826 bsi->bsi_op->o_tmpfree( bv.bv_val,
827 bsi->bsi_op->o_tmpmemctx );
828 }
829 #endif /* BACKSQL_PRETTY_VALIDATE */
830
831 continue;
832 }
833 ber_dupbv( &attr->a_nvals[ j ], &nbv );
834 bsi->bsi_op->o_tmpfree( nbv.bv_val,
835 bsi->bsi_op->o_tmpmemctx );
836 }
837
838 ber_dupbv( &attr->a_vals[ j ], &bv );
839
840 assert( j < oldcount + count );
841 j++;
842 #endif /* BACKSQL_COUNTQUERY */
843
844 #ifdef BACKSQL_PRETTY_VALIDATE
845 if ( pretty ) {
846 bsi->bsi_op->o_tmpfree( bv.bv_val,
847 bsi->bsi_op->o_tmpmemctx );
848 }
849 #endif /* BACKSQL_PRETTY_VALIDATE */
850
851 #ifdef BACKSQL_TRACE
852 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
853 (int)row.col_prec[ i ] );
854
855 } else {
856 Debug( LDAP_DEBUG_TRACE, "NULL value "
857 "in this row for attribute \"%s\"\n",
858 row.col_names[ i ].bv_val );
859 #endif /* BACKSQL_TRACE */
860 }
861 }
862 }
863
864 #ifdef BACKSQL_COUNTQUERY
865 if ( BER_BVISNULL( &attr->a_vals[ 0 ] ) ) {
866 /* don't leave around attributes with no values */
867 attr_free( attr );
868
869 } else if ( append ) {
870 Attribute **ap;
871
872 for ( ap = &bsi->bsi_e->e_attrs; (*ap) != NULL; ap = &(*ap)->a_next )
873 /* goto last */ ;
874 *ap = attr;
875 }
876 #endif /* BACKSQL_COUNTQUERY */
877
878 SQLFreeStmt( sth, SQL_DROP );
879 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n" );
880
881 if ( at->bam_next ) {
882 res = backsql_get_attr_vals( at->bam_next, v_bsi );
883 } else {
884 res = 1;
885 }
886
887 #ifdef BACKSQL_TRACE
888 done:;
889 #endif /* BACKSQL_TRACE */
890 backsql_FreeRow_x( &row, bsi->bsi_op->o_tmpmemctx );
891
892 return res;
893 }
894
895 int
896 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
897 {
898 Operation *op = bsi->bsi_op;
899 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
900 int i;
901 int rc;
902
903 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n" );
904
905 assert( bsi->bsi_e != NULL );
906
907 memset( bsi->bsi_e, 0, sizeof( Entry ) );
908
909 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
910 (void)entry_dup2( bsi->bsi_e, bi->sql_baseObject );
911 goto done;
912 }
913
914 bsi->bsi_e->e_attrs = NULL;
915 bsi->bsi_e->e_private = NULL;
916
917 if ( eid->eid_oc == NULL ) {
918 eid->eid_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
919 eid->eid_oc_id );
920 if ( eid->eid_oc == NULL ) {
921 Debug( LDAP_DEBUG_TRACE,
922 "backsql_id2entry(): unable to fetch objectClass with id=" BACKSQL_IDNUMFMT " for entry id=" BACKSQL_IDFMT " dn=\"%s\"\n",
923 eid->eid_oc_id, BACKSQL_IDARG(eid->eid_id),
924 eid->eid_dn.bv_val );
925 return LDAP_OTHER;
926 }
927 }
928 bsi->bsi_oc = eid->eid_oc;
929 bsi->bsi_c_eid = eid;
930
931 ber_dupbv_x( &bsi->bsi_e->e_name, &eid->eid_dn, op->o_tmpmemctx );
932 ber_dupbv_x( &bsi->bsi_e->e_nname, &eid->eid_ndn, op->o_tmpmemctx );
933
934 #ifndef BACKSQL_ARBITRARY_KEY
935 /* FIXME: unused */
936 bsi->bsi_e->e_id = eid->eid_id;
937 #endif /* ! BACKSQL_ARBITRARY_KEY */
938
939 rc = attr_merge_normalize_one( bsi->bsi_e,
940 slap_schema.si_ad_objectClass,
941 &bsi->bsi_oc->bom_oc->soc_cname,
942 bsi->bsi_op->o_tmpmemctx );
943 if ( rc != LDAP_SUCCESS ) {
944 backsql_entry_clean( op, bsi->bsi_e );
945 return rc;
946 }
947
948 if ( bsi->bsi_attrs == NULL || ( bsi->bsi_flags & BSQL_SF_ALL_USER ) )
949 {
950 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
951 "retrieving all attributes\n" );
952 ldap_avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
953 bsi, 0, AVL_INORDER );
954
955 } else {
956 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
957 "custom attribute list\n" );
958 for ( i = 0; !BER_BVISNULL( &bsi->bsi_attrs[ i ].an_name ); i++ ) {
959 backsql_at_map_rec **vat;
960 AttributeName *an = &bsi->bsi_attrs[ i ];
961 int j;
962
963 /* if one of the attributes listed here is
964 * a subtype of another, it must be ignored,
965 * because subtypes are already dealt with
966 * by backsql_supad2at()
967 */
968 for ( j = 0; !BER_BVISNULL( &bsi->bsi_attrs[ j ].an_name ); j++ ) {
969 /* skip self */
970 if ( j == i ) {
971 continue;
972 }
973
974 /* skip subtypes */
975 if ( is_at_subtype( an->an_desc->ad_type,
976 bsi->bsi_attrs[ j ].an_desc->ad_type ) )
977 {
978 goto next;
979 }
980 }
981
982 rc = backsql_supad2at( bsi->bsi_oc, an->an_desc, &vat );
983 if ( rc != 0 || vat == NULL ) {
984 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
985 "attribute \"%s\" is not defined "
986 "for objectclass \"%s\"\n",
987 an->an_name.bv_val,
988 BACKSQL_OC_NAME( bsi->bsi_oc ) );
989 continue;
990 }
991
992 for ( j = 0; vat[j]; j++ ) {
993 backsql_get_attr_vals( vat[j], bsi );
994 }
995
996 ch_free( vat );
997
998 next:;
999 }
1000 }
1001
1002 if ( bsi->bsi_flags & BSQL_SF_RETURN_ENTRYUUID ) {
1003 Attribute *a_entryUUID,
1004 **ap;
1005
1006 a_entryUUID = backsql_operational_entryUUID( bi, eid );
1007 if ( a_entryUUID != NULL ) {
1008 for ( ap = &bsi->bsi_e->e_attrs;
1009 *ap;
1010 ap = &(*ap)->a_next );
1011
1012 *ap = a_entryUUID;
1013 }
1014 }
1015
1016 if ( ( bsi->bsi_flags & BSQL_SF_ALL_OPER )
1017 || an_find( bsi->bsi_attrs, slap_bv_all_operational_attrs )
1018 || an_find( bsi->bsi_attrs, &slap_schema.si_ad_structuralObjectClass->ad_cname ) )
1019 {
1020 ObjectClass *soc = NULL;
1021
1022 if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
1023 Attribute *a;
1024 const char *text = NULL;
1025 char textbuf[ 1024 ];
1026 size_t textlen = sizeof( textbuf );
1027 struct berval bv[ 2 ],
1028 *nvals;
1029 int rc = LDAP_SUCCESS;
1030
1031 a = attr_find( bsi->bsi_e->e_attrs,
1032 slap_schema.si_ad_objectClass );
1033 if ( a != NULL ) {
1034 nvals = a->a_nvals;
1035
1036 } else {
1037 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
1038 BER_BVZERO( &bv[ 1 ] );
1039 nvals = bv;
1040 }
1041
1042 rc = structural_class( nvals, &soc, NULL,
1043 &text, textbuf, textlen, op->o_tmpmemctx );
1044 if ( rc != LDAP_SUCCESS ) {
1045 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
1046 "structural_class() failed %d (%s)\n",
1047 bsi->bsi_e->e_name.bv_val,
1048 rc, text ? text : "" );
1049 backsql_entry_clean( op, bsi->bsi_e );
1050 return rc;
1051 }
1052
1053 if ( !bvmatch( &soc->soc_cname, &bsi->bsi_oc->bom_oc->soc_cname ) ) {
1054 if ( !is_object_subclass( bsi->bsi_oc->bom_oc, soc ) ) {
1055 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
1056 "computed structuralObjectClass %s "
1057 "does not match objectClass %s associated "
1058 "to entry\n",
1059 bsi->bsi_e->e_name.bv_val, soc->soc_cname.bv_val,
1060 bsi->bsi_oc->bom_oc->soc_cname.bv_val );
1061 backsql_entry_clean( op, bsi->bsi_e );
1062 return rc;
1063 }
1064
1065 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
1066 "computed structuralObjectClass %s "
1067 "is subclass of objectClass %s associated "
1068 "to entry\n",
1069 bsi->bsi_e->e_name.bv_val, soc->soc_cname.bv_val,
1070 bsi->bsi_oc->bom_oc->soc_cname.bv_val );
1071 }
1072
1073 } else {
1074 soc = bsi->bsi_oc->bom_oc;
1075 }
1076
1077 rc = attr_merge_normalize_one( bsi->bsi_e,
1078 slap_schema.si_ad_structuralObjectClass,
1079 &soc->soc_cname,
1080 bsi->bsi_op->o_tmpmemctx );
1081 if ( rc != LDAP_SUCCESS ) {
1082 backsql_entry_clean( op, bsi->bsi_e );
1083 return rc;
1084 }
1085 }
1086
1087 done:;
1088 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n" );
1089
1090 return LDAP_SUCCESS;
1091 }
1092