2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
7 * Portions Copyright 2004 Mark Adamson.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
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>.
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.
27 #include <sys/types.h>
28 #include "ac/string.h"
31 #include "proto-sql.h"
33 #ifdef BACKSQL_ARBITRARY_KEY
34 struct berval backsql_baseObject_bv = BER_BVC( BACKSQL_BASEOBJECT_IDSTR );
35 #endif /* BACKSQL_ARBITRARY_KEY */
38 backsql_free_entryID( Operation *op, backsql_entryID *id, int freeit )
40 backsql_entryID *next;
46 if ( !BER_BVISNULL( &id->eid_ndn ) ) {
47 if ( !BER_BVISNULL( &id->eid_dn )
48 && id->eid_dn.bv_val != id->eid_ndn.bv_val )
50 op->o_tmpfree( id->eid_dn.bv_val, op->o_tmpmemctx );
51 BER_BVZERO( &id->eid_dn );
54 op->o_tmpfree( id->eid_ndn.bv_val, op->o_tmpmemctx );
55 BER_BVZERO( &id->eid_ndn );
58 #ifdef BACKSQL_ARBITRARY_KEY
59 if ( !BER_BVISNULL( &id->eid_id ) ) {
60 op->o_tmpfree( id->eid_id.bv_val, op->o_tmpmemctx );
61 BER_BVZERO( &id->eid_id );
64 if ( !BER_BVISNULL( &id->eid_keyval ) ) {
65 op->o_tmpfree( id->eid_keyval.bv_val, op->o_tmpmemctx );
66 BER_BVZERO( &id->eid_keyval );
68 #endif /* BACKSQL_ARBITRARY_KEY */
71 op->o_tmpfree( id, op->o_tmpmemctx );
78 * NOTE: the dn must be normalized
90 backsql_info *bi = op->o_bd->be_private;
91 SQLHSTMT sth = SQL_NULL_HSTMT;
95 struct berval realndn = BER_BVNULL;
98 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
103 * NOTE: id can be NULL; in this case, the function
104 * simply checks whether the DN can be successfully
105 * turned into an ID, returning LDAP_SUCCESS for
106 * positive cases, or the most appropriate error
109 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(\"%s\")%s%s\n",
110 ndn->bv_val, id == NULL ? " (no ID expected)" : "",
111 matched ? " matched expected" : "" );
114 /* NOTE: trap inconsistencies */
115 assert( BER_BVISNULL( &id->eid_ndn ) );
118 if ( ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
119 Debug( LDAP_DEBUG_TRACE,
120 " backsql_dn2id(\"%s\"): DN length=%ld "
121 "exceeds max DN length %d:\n",
122 ndn->bv_val, ndn->bv_len, BACKSQL_MAX_DN_LEN );
126 /* return baseObject if available and matches */
127 /* FIXME: if ndn is already mucked, we cannot check this */
128 if ( bi->sql_baseObject != NULL &&
129 dn_match( ndn, &bi->sql_baseObject->e_nname ) )
132 #ifdef BACKSQL_ARBITRARY_KEY
133 ber_dupbv_x( &id->eid_id, &backsql_baseObject_bv,
135 ber_dupbv_x( &id->eid_keyval, &backsql_baseObject_bv,
137 #else /* ! BACKSQL_ARBITRARY_KEY */
138 id->eid_id = BACKSQL_BASEOBJECT_ID;
139 id->eid_keyval = BACKSQL_BASEOBJECT_KEYVAL;
140 #endif /* ! BACKSQL_ARBITRARY_KEY */
141 id->eid_oc_id = BACKSQL_BASEOBJECT_OC;
143 ber_dupbv_x( &id->eid_ndn, &bi->sql_baseObject->e_nname,
145 ber_dupbv_x( &id->eid_dn, &bi->sql_baseObject->e_name,
155 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): id_query \"%s\"\n",
156 ndn->bv_val, bi->sql_id_query, 0 );
157 assert( bi->sql_id_query );
158 rc = backsql_Prepare( dbh, &sth, bi->sql_id_query, 0 );
159 if ( rc != SQL_SUCCESS ) {
160 Debug( LDAP_DEBUG_TRACE,
161 " backsql_dn2id(\"%s\"): "
162 "error preparing SQL:\n %s",
163 ndn->bv_val, bi->sql_id_query, 0 );
164 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
171 if ( backsql_api_dn2odbc( op, rs, &realndn ) ) {
172 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
173 "backsql_api_dn2odbc(\"%s\") failed\n",
174 ndn->bv_val, realndn.bv_val, 0 );
180 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
182 * Prepare an upper cased, byte reversed version
183 * that can be searched using indexes
186 for ( i = 0, j = realndn.bv_len - 1; realndn.bv_val[ i ]; i++, j--)
188 upperdn[ i ] = realndn.bv_val[ j ];
191 ldap_pvt_str2upper( upperdn );
193 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
195 ndn->bv_val, upperdn, 0 );
196 ber_str2bv( upperdn, 0, 0, &tbbDN );
199 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
200 AC_MEMCPY( upperdn, realndn.bv_val, realndn.bv_len + 1 );
201 ldap_pvt_str2upper( upperdn );
202 Debug( LDAP_DEBUG_TRACE,
203 " backsql_dn2id(\"%s\"): "
205 ndn->bv_val, upperdn, 0 );
206 ber_str2bv( upperdn, 0, 0, &tbbDN );
213 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &tbbDN );
214 if ( rc != SQL_SUCCESS) {
216 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
217 "error binding dn=\"%s\" parameter:\n",
218 ndn->bv_val, tbbDN.bv_val, 0 );
219 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
224 rc = SQLExecute( sth );
225 if ( rc != SQL_SUCCESS ) {
226 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
227 "error executing query (\"%s\", \"%s\"):\n",
228 ndn->bv_val, bi->sql_id_query, tbbDN.bv_val );
229 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
234 backsql_BindRowAsStrings( sth, &row );
235 rc = SQLFetch( sth );
236 if ( BACKSQL_SUCCESS( rc ) ) {
237 char buf[ SLAP_TEXT_BUFLEN ];
240 snprintf( buf, sizeof(buf),
241 "id=%s keyval=%s oc_id=%s dn=%s",
242 row.cols[ 0 ], row.cols[ 1 ],
243 row.cols[ 2 ], row.cols[ 3 ] );
244 Debug( LDAP_DEBUG_TRACE,
245 " backsql_dn2id(\"%s\"): %s\n",
246 ndn->bv_val, buf, 0 );
247 #endif /* LDAP_DEBUG */
253 #ifdef BACKSQL_ARBITRARY_KEY
254 ber_str2bv_x( row.cols[ 0 ], 0, 1, &id->eid_id,
256 ber_str2bv_x( row.cols[ 1 ], 0, 1, &id->eid_keyval,
258 #else /* ! BACKSQL_ARBITRARY_KEY */
259 id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
260 id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
261 #endif /* ! BACKSQL_ARBITRARY_KEY */
262 id->eid_oc_id = strtol( row.cols[ 2 ], NULL, 0 );
264 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
266 if ( backsql_api_odbc2dn( op, rs, &dn ) ) {
270 res = dnPrettyNormal( NULL, &dn,
271 &id->eid_dn, &id->eid_ndn,
273 if ( res != LDAP_SUCCESS ) {
274 Debug( LDAP_DEBUG_TRACE,
275 " backsql_dn2id(\"%s\"): "
276 "dnPrettyNormal failed (%d: %s)\n",
278 ldap_err2string( res ) );
281 (void)backsql_free_entryID( op, id, 0 );
284 if ( dn.bv_val != row.cols[ 3 ] ) {
293 res = LDAP_NO_SUCH_OBJECT;
295 struct berval pdn = *ndn;
300 rs->sr_matched = NULL;
301 while ( !be_issuffix( op->o_bd, &pdn ) ) {
302 char *matchedDN = NULL;
304 dnParent( &pdn, &pdn );
307 * Empty DN ("") defaults to LDAP_SUCCESS
309 rs->sr_err = backsql_dn2id( op, rs, dbh, &pdn, id, 0, 1 );
310 switch ( rs->sr_err ) {
311 case LDAP_NO_SUCH_OBJECT:
312 /* try another one */
316 matchedDN = pdn.bv_val;
317 /* fail over to next case */
320 rs->sr_err = LDAP_NO_SUCH_OBJECT;
321 rs->sr_matched = matchedDN;
327 backsql_FreeRow( &row );
330 Debug( LDAP_DEBUG_TRACE,
331 "<==backsql_dn2id(\"%s\"): err=%d\n",
332 ndn->bv_val, res, 0 );
333 if ( sth != SQL_NULL_HSTMT ) {
334 SQLFreeStmt( sth, SQL_DROP );
337 if ( !BER_BVISNULL( &realndn ) && realndn.bv_val != ndn->bv_val ) {
338 ch_free( realndn.bv_val );
345 backsql_count_children(
349 unsigned long *nchildren )
351 SQLHSTMT sth = SQL_NULL_HSTMT;
354 int res = LDAP_SUCCESS;
356 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n",
359 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
360 Debug( LDAP_DEBUG_TRACE,
361 "backsql_count_children(): DN \"%s\" (%ld bytes) "
362 "exceeds max DN length (%d):\n",
363 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
368 Debug(LDAP_DEBUG_TRACE, "children id query \"%s\"\n",
369 bi->sql_has_children_query, 0, 0);
370 assert( bi->sql_has_children_query );
371 rc = backsql_Prepare( dbh, &sth, bi->sql_has_children_query, 0 );
372 if ( rc != SQL_SUCCESS ) {
373 Debug( LDAP_DEBUG_TRACE,
374 "backsql_count_children(): error preparing SQL:\n%s",
375 bi->sql_has_children_query, 0, 0);
376 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
377 SQLFreeStmt( sth, SQL_DROP );
381 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
382 if ( rc != SQL_SUCCESS) {
384 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
385 "error binding dn=\"%s\" parameter:\n",
387 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
388 SQLFreeStmt( sth, SQL_DROP );
392 rc = SQLExecute( sth );
393 if ( rc != SQL_SUCCESS ) {
394 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
395 "error executing query (\"%s\", \"%s\"):\n",
396 bi->sql_has_children_query, dn->bv_val, 0 );
397 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
398 SQLFreeStmt( sth, SQL_DROP );
402 backsql_BindRowAsStrings( sth, &row );
404 rc = SQLFetch( sth );
405 if ( BACKSQL_SUCCESS( rc ) ) {
408 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
409 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
410 /* FIXME: braindead RDBMSes return
411 * a fractional number from COUNT!
419 backsql_FreeRow( &row );
421 SQLFreeStmt( sth, SQL_DROP );
423 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
430 backsql_has_children(
435 unsigned long nchildren;
438 rc = backsql_count_children( bi, dbh, dn, &nchildren );
440 if ( rc == LDAP_SUCCESS ) {
441 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
448 backsql_get_attr_vals( void *v_at, void *v_bsi )
450 backsql_at_map_rec *at = v_at;
451 backsql_srch_info *bsi = v_bsi;
452 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
454 SQLHSTMT sth = SQL_NULL_HSTMT;
459 #ifdef BACKSQL_COUNTQUERY
461 countsize = sizeof( count ),
463 Attribute *attr = NULL;
465 slap_mr_normalize_func *normfunc = NULL;
466 #endif /* BACKSQL_COUNTQUERY */
467 #ifdef BACKSQL_PRETTY_VALIDATE
468 slap_syntax_validate_func *validate = NULL;
469 slap_syntax_transform_func *pretty = NULL;
470 #endif /* BACKSQL_PRETTY_VALIDATE */
475 #ifdef BACKSQL_ARBITRARY_KEY
476 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
477 "oc=\"%s\" attr=\"%s\" keyval=%s\n",
478 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
479 bsi->bsi_c_eid->eid_keyval.bv_val );
480 #else /* ! BACKSQL_ARBITRARY_KEY */
481 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
482 "oc=\"%s\" attr=\"%s\" keyval=%ld\n",
483 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
484 bsi->bsi_c_eid->eid_keyval );
485 #endif /* ! BACKSQL_ARBITRARY_KEY */
487 #ifdef BACKSQL_PRETTY_VALIDATE
488 validate = at->bam_ad->ad_type->sat_syntax->ssyn_validate;
489 pretty = at->bam_ad->ad_type->sat_syntax->ssyn_pretty;
491 if ( validate == NULL && pretty == NULL ) {
494 #endif /* BACKSQL_PRETTY_VALIDATE */
496 #ifdef BACKSQL_COUNTQUERY
497 if ( at->bam_ad->ad_type->sat_equality ) {
498 normfunc = at->bam_ad->ad_type->sat_equality->smr_normalize;
501 /* Count how many rows will be returned. This avoids memory
502 * fragmentation that can result from loading the values in
503 * one by one and using realloc()
505 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_countquery, 0 );
506 if ( rc != SQL_SUCCESS ) {
507 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
508 "error preparing count query: %s\n",
509 at->bam_countquery, 0, 0 );
510 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
514 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
515 &bsi->bsi_c_eid->eid_keyval );
516 if ( rc != SQL_SUCCESS ) {
517 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
518 "error binding key value parameter\n", 0, 0, 0 );
519 SQLFreeStmt( sth, SQL_DROP );
523 rc = SQLExecute( sth );
524 if ( ! BACKSQL_SUCCESS( rc ) ) {
525 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
526 "error executing attribute count query '%s'\n",
527 at->bam_countquery, 0, 0 );
528 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
529 SQLFreeStmt( sth, SQL_DROP );
533 SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_LONG,
535 (SQLINTEGER)sizeof( count ),
538 rc = SQLFetch( sth );
539 if ( rc != SQL_SUCCESS ) {
540 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
541 "error fetch results of count query: %s\n",
542 at->bam_countquery, 0, 0 );
543 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
544 SQLFreeStmt( sth, SQL_DROP );
548 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
549 "number of values in query: %d\n", count, 0, 0 );
550 SQLFreeStmt( sth, SQL_DROP );
555 attr = attr_find( bsi->bsi_e->e_attrs, at->bam_ad );
556 if ( attr != NULL ) {
559 if ( attr->a_vals != NULL ) {
560 for ( ; !BER_BVISNULL( &attr->a_vals[ oldcount ] ); oldcount++ )
564 tmp = ch_realloc( attr->a_vals, ( oldcount + count + 1 ) * sizeof( struct berval ) );
569 memset( &attr->a_vals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) );
572 tmp = ch_realloc( attr->a_nvals, ( oldcount + count + 1 ) * sizeof( struct berval ) );
577 memset( &attr->a_nvals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) );
580 attr->a_nvals = attr->a_vals;
586 /* Make space for the array of values */
587 attr = (Attribute *) ch_malloc( sizeof( Attribute ) );
588 attr->a_desc = at->bam_ad;
591 attr->a_vals = ch_calloc( count + 1, sizeof( struct berval ) );
592 if ( attr->a_vals == NULL ) {
593 Debug( LDAP_DEBUG_TRACE, "Out of memory!\n", 0,0,0 );
597 memset( attr->a_vals, 0, ( count + 1 ) * sizeof( struct berval ) );
599 attr->a_nvals = ch_calloc( count + 1, sizeof( struct berval ) );
600 if ( attr->a_nvals == NULL ) {
601 ch_free( attr->a_vals );
606 memset( attr->a_nvals, 0, ( count + 1 ) * sizeof( struct berval ) );
610 attr->a_nvals = attr->a_vals;
613 for ( ap = &bsi->bsi_e->e_attrs; (*ap) != NULL; ap = &(*ap)->a_next )
617 #endif /* BACKSQL_COUNTQUERY */
619 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
620 if ( rc != SQL_SUCCESS ) {
621 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
622 "error preparing query: %s\n", at->bam_query, 0, 0 );
623 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
627 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
628 &bsi->bsi_c_eid->eid_keyval );
629 if ( rc != SQL_SUCCESS ) {
630 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
631 "error binding key value parameter\n", 0, 0, 0 );
636 #ifdef BACKSQL_ARBITRARY_KEY
637 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
638 "query=\"%s\" keyval=%s\n", at->bam_query,
639 bsi->bsi_c_eid->eid_keyval.bv_val, 0 );
640 #else /* !BACKSQL_ARBITRARY_KEY */
641 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
642 "query=\"%s\" keyval=%d\n", at->bam_query,
643 bsi->bsi_c_eid->eid_keyval, 0 );
644 #endif /* ! BACKSQL_ARBITRARY_KEY */
645 #endif /* BACKSQL_TRACE */
647 rc = SQLExecute( sth );
648 if ( ! BACKSQL_SUCCESS( rc ) ) {
649 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
650 "error executing attribute query \"%s\"\n",
651 at->bam_query, 0, 0 );
652 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
653 SQLFreeStmt( sth, SQL_DROP );
657 backsql_BindRowAsStrings( sth, &row );
658 #ifdef BACKSQL_COUNTQUERY
660 #endif /* BACKSQL_COUNTQUERY */
661 for ( rc = SQLFetch( sth ), k = 0;
662 BACKSQL_SUCCESS( rc );
663 rc = SQLFetch( sth ), k++ )
665 for ( i = 0; i < row.ncols; i++ ) {
667 if ( row.value_len[ i ] > 0 ) {
671 AttributeDescription *ad = NULL;
674 retval = slap_bv2ad( &row.col_names[ i ], &ad, &text );
675 if ( retval != LDAP_SUCCESS ) {
676 Debug( LDAP_DEBUG_ANY,
677 "==>backsql_get_attr_vals(\"%s\"): "
678 "unable to find AttributeDescription %s "
680 bsi->bsi_e->e_name.bv_val,
681 row.col_names[ i ].bv_val, retval );
685 if ( ad != at->bam_ad ) {
686 Debug( LDAP_DEBUG_ANY,
687 "==>backsql_get_attr_vals(\"%s\"): "
688 "column name %s differs from "
689 "AttributeDescription %s\n",
690 bsi->bsi_e->e_name.bv_val,
692 at->bam_ad->ad_cname.bv_val );
695 #endif /* BACKSQL_TRACE */
698 * FIXME: what if a binary
701 ber_str2bv( row.cols[ i ], 0, 0, &bv );
703 #ifdef BACKSQL_PRETTY_VALIDATE
707 retval = pretty( at->bam_ad->ad_type->sat_syntax,
708 &bv, &pbv, bsi->bsi_op->o_tmpmemctx );
712 retval = validate( at->bam_ad->ad_type->sat_syntax,
716 if ( retval != LDAP_SUCCESS ) {
717 char buf[ SLAP_TEXT_BUFLEN ];
719 /* FIXME: we're ignoring invalid values,
720 * but we're accepting the attributes;
721 * should we fail at all? */
722 snprintf( buf, sizeof( buf ),
723 "unable to %s value #%d "
724 "of AttributeDescription %s",
725 pretty ? "prettify" : "validate",
726 at->bam_ad->ad_cname.bv_val,
728 Debug( LDAP_DEBUG_TRACE,
729 "==>backsql_get_attr_vals(\"%s\"): "
731 bsi->bsi_e->e_name.bv_val, buf, retval );
734 #endif /* BACKSQL_PRETTY_VALIDATE */
736 #ifndef BACKSQL_COUNTQUERY
737 (void)backsql_entry_addattr( bsi->bsi_e,
739 bsi->bsi_op->o_tmpmemctx );
741 #else /* BACKSQL_COUNTQUERY */
745 retval = (*normfunc)( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
746 at->bam_ad->ad_type->sat_syntax,
747 at->bam_ad->ad_type->sat_equality,
749 bsi->bsi_op->o_tmpmemctx );
751 if ( retval != LDAP_SUCCESS ) {
752 char buf[ SLAP_TEXT_BUFLEN ];
754 /* FIXME: we're ignoring invalid values,
755 * but we're accepting the attributes;
756 * should we fail at all? */
757 snprintf( buf, sizeof( buf ),
758 "unable to normalize value #%d "
759 "of AttributeDescription %s",
760 at->bam_ad->ad_cname.bv_val,
762 Debug( LDAP_DEBUG_TRACE,
763 "==>backsql_get_attr_vals(\"%s\"): "
765 bsi->bsi_e->e_name.bv_val, buf, retval );
767 #ifdef BACKSQL_PRETTY_VALIDATE
769 bsi->bsi_op->o_tmpfree( bv.bv_val,
770 bsi->bsi_op->o_tmpmemctx );
772 #endif /* BACKSQL_PRETTY_VALIDATE */
776 ber_dupbv( &attr->a_nvals[ j ], &nbv );
777 bsi->bsi_op->o_tmpfree( nbv.bv_val,
778 bsi->bsi_op->o_tmpmemctx );
781 ber_dupbv( &attr->a_vals[ j ], &bv );
783 assert( j < oldcount + count );
785 #endif /* BACKSQL_COUNTQUERY */
787 #ifdef BACKSQL_PRETTY_VALIDATE
789 bsi->bsi_op->o_tmpfree( bv.bv_val,
790 bsi->bsi_op->o_tmpmemctx );
792 #endif /* BACKSQL_PRETTY_VALIDATE */
795 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
796 (int)row.col_prec[ i ], 0, 0 );
799 Debug( LDAP_DEBUG_TRACE, "NULL value "
800 "in this row for attribute \"%s\"\n",
801 row.col_names[ i ].bv_val, 0, 0 );
802 #endif /* BACKSQL_TRACE */
807 backsql_FreeRow( &row );
808 SQLFreeStmt( sth, SQL_DROP );
809 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
811 if ( at->bam_next ) {
812 return backsql_get_attr_vals( at->bam_next, v_bsi );
819 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
821 Operation *op = bsi->bsi_op;
822 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
826 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
828 assert( bsi->bsi_e );
830 memset( bsi->bsi_e, 0, sizeof( Entry ) );
832 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
835 e = entry_dup( bi->sql_baseObject );
837 return LDAP_NO_MEMORY;
845 ber_dupbv_x( &bsi->bsi_e->e_name, &eid->eid_dn, op->o_tmpmemctx );
846 ber_dupbv_x( &bsi->bsi_e->e_nname, &eid->eid_ndn, op->o_tmpmemctx );
848 bsi->bsi_e->e_attrs = NULL;
849 bsi->bsi_e->e_private = NULL;
851 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
853 bsi->bsi_c_eid = eid;
855 #ifndef BACKSQL_ARBITRARY_KEY
857 bsi->bsi_e->e_id = eid->eid_id;
858 #endif /* ! BACKSQL_ARBITRARY_KEY */
860 rc = attr_merge_normalize_one( bsi->bsi_e,
861 slap_schema.si_ad_objectClass,
862 &bsi->bsi_oc->bom_oc->soc_cname,
863 bsi->bsi_op->o_tmpmemctx );
864 if ( rc != LDAP_SUCCESS ) {
865 entry_clean( bsi->bsi_e );
869 if ( bsi->bsi_attrs == NULL || ( bsi->bsi_flags & BSQL_SF_ALL_USER ) )
871 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
872 "retrieving all attributes\n", 0, 0, 0 );
873 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
874 bsi, 0, AVL_INORDER );
877 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
878 "custom attribute list\n", 0, 0, 0 );
879 for ( i = 0; !BER_BVISNULL( &bsi->bsi_attrs[ i ].an_name ); i++ ) {
880 backsql_at_map_rec **vat;
881 AttributeName *an = &bsi->bsi_attrs[ i ];
884 /* if one of the attributes listed here is
885 * a subtype of another, it must be ignored,
886 * because subtypes are already dealt with
887 * by backsql_supad2at()
889 for ( j = 0; !BER_BVISNULL( &bsi->bsi_attrs[ j ].an_name ); j++ ) {
896 if ( is_at_subtype( an->an_desc->ad_type,
897 bsi->bsi_attrs[ j ].an_desc->ad_type ) )
903 rc = backsql_supad2at( bsi->bsi_oc, an->an_desc, &vat );
904 if ( rc != 0 || vat == NULL ) {
905 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
906 "attribute \"%s\" is not defined "
907 "for objectlass \"%s\"\n",
909 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
913 for ( j = 0; vat[j]; j++ ) {
914 backsql_get_attr_vals( vat[j], bsi );
923 if ( bsi->bsi_flags & BSQL_SF_RETURN_ENTRYUUID ) {
924 Attribute *a_entryUUID,
927 a_entryUUID = backsql_operational_entryUUID( bi, eid );
928 if ( a_entryUUID != NULL ) {
929 for ( ap = &bsi->bsi_e->e_attrs;
931 ap = &(*ap)->a_next );
937 if ( ( bsi->bsi_flags & BSQL_SF_ALL_OPER )
938 || an_find( bsi->bsi_attrs, &AllOper )
939 || an_find( bsi->bsi_attrs, &slap_schema.si_ad_structuralObjectClass->ad_cname ) )
941 if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
943 const char *text = NULL;
944 char textbuf[ 1024 ];
945 size_t textlen = sizeof( textbuf );
949 int rc = LDAP_SUCCESS;
951 a = attr_find( bsi->bsi_e->e_attrs,
952 slap_schema.si_ad_objectClass );
957 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
958 BER_BVZERO( &bv[ 1 ] );
962 rc = structural_class( nvals, &soc, NULL,
963 &text, textbuf, textlen );
964 if ( rc != LDAP_SUCCESS ) {
965 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
966 "structural_class() failed %d (%s)\n",
967 bsi->bsi_e->e_name.bv_val,
968 rc, text ? text : "" );
969 entry_clean( bsi->bsi_e );
973 if ( !bvmatch( &soc, &bsi->bsi_oc->bom_oc->soc_cname ) ) {
974 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
975 "computed structuralObjectClass %s "
976 "does not match objectClass %s associated "
978 bsi->bsi_e->e_name.bv_val, soc.bv_val,
979 bsi->bsi_oc->bom_oc->soc_cname.bv_val );
980 entry_clean( bsi->bsi_e );
985 rc = attr_merge_normalize_one( bsi->bsi_e,
986 slap_schema.si_ad_structuralObjectClass,
987 &bsi->bsi_oc->bom_oc->soc_cname,
988 bsi->bsi_op->o_tmpmemctx );
989 if ( rc != LDAP_SUCCESS ) {
990 entry_clean( bsi->bsi_e );
996 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );