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 ) ) {
303 char *matchedDN = NULL;
306 dnParent( &dn, &pdn );
309 * Empty DN ("") defaults to LDAP_SUCCESS
311 rs->sr_err = backsql_dn2id( op, rs, dbh, &pdn, id, 0, 1 );
312 switch ( rs->sr_err ) {
313 case LDAP_NO_SUCH_OBJECT:
314 /* try another one */
318 matchedDN = pdn.bv_val;
319 /* fail over to next case */
322 rs->sr_err = LDAP_NO_SUCH_OBJECT;
323 rs->sr_matched = matchedDN;
329 backsql_FreeRow( &row );
332 Debug( LDAP_DEBUG_TRACE,
333 "<==backsql_dn2id(\"%s\"): err=%d\n",
334 ndn->bv_val, res, 0 );
335 if ( sth != SQL_NULL_HSTMT ) {
336 SQLFreeStmt( sth, SQL_DROP );
339 if ( !BER_BVISNULL( &realndn ) && realndn.bv_val != ndn->bv_val ) {
340 ch_free( realndn.bv_val );
347 backsql_count_children(
351 unsigned long *nchildren )
353 SQLHSTMT sth = SQL_NULL_HSTMT;
356 int res = LDAP_SUCCESS;
358 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n",
361 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
362 Debug( LDAP_DEBUG_TRACE,
363 "backsql_count_children(): DN \"%s\" (%ld bytes) "
364 "exceeds max DN length (%d):\n",
365 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
370 Debug(LDAP_DEBUG_TRACE, "children id query \"%s\"\n",
371 bi->sql_has_children_query, 0, 0);
372 assert( bi->sql_has_children_query );
373 rc = backsql_Prepare( dbh, &sth, bi->sql_has_children_query, 0 );
374 if ( rc != SQL_SUCCESS ) {
375 Debug( LDAP_DEBUG_TRACE,
376 "backsql_count_children(): error preparing SQL:\n%s",
377 bi->sql_has_children_query, 0, 0);
378 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
379 SQLFreeStmt( sth, SQL_DROP );
383 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
384 if ( rc != SQL_SUCCESS) {
386 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
387 "error binding dn=\"%s\" parameter:\n",
389 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
390 SQLFreeStmt( sth, SQL_DROP );
394 rc = SQLExecute( sth );
395 if ( rc != SQL_SUCCESS ) {
396 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
397 "error executing query (\"%s\", \"%s\"):\n",
398 bi->sql_has_children_query, dn->bv_val, 0 );
399 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
400 SQLFreeStmt( sth, SQL_DROP );
404 backsql_BindRowAsStrings( sth, &row );
406 rc = SQLFetch( sth );
407 if ( BACKSQL_SUCCESS( rc ) ) {
410 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
411 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
412 /* FIXME: braindead RDBMSes return
413 * a fractional number from COUNT!
421 backsql_FreeRow( &row );
423 SQLFreeStmt( sth, SQL_DROP );
425 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
432 backsql_has_children(
437 unsigned long nchildren;
440 rc = backsql_count_children( bi, dbh, dn, &nchildren );
442 if ( rc == LDAP_SUCCESS ) {
443 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
450 backsql_get_attr_vals( void *v_at, void *v_bsi )
452 backsql_at_map_rec *at = v_at;
453 backsql_srch_info *bsi = v_bsi;
454 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
456 SQLHSTMT sth = SQL_NULL_HSTMT;
461 #ifdef BACKSQL_COUNTQUERY
463 countsize = sizeof( count ),
465 Attribute *attr = NULL;
467 slap_mr_normalize_func *normfunc = NULL;
468 #endif /* BACKSQL_COUNTQUERY */
469 #ifdef BACKSQL_PRETTY_VALIDATE
470 slap_syntax_validate_func *validate = NULL;
471 slap_syntax_transform_func *pretty = NULL;
472 #endif /* BACKSQL_PRETTY_VALIDATE */
477 #ifdef BACKSQL_ARBITRARY_KEY
478 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
479 "oc=\"%s\" attr=\"%s\" keyval=%s\n",
480 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
481 bsi->bsi_c_eid->eid_keyval.bv_val );
482 #else /* ! BACKSQL_ARBITRARY_KEY */
483 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
484 "oc=\"%s\" attr=\"%s\" keyval=%ld\n",
485 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
486 bsi->bsi_c_eid->eid_keyval );
487 #endif /* ! BACKSQL_ARBITRARY_KEY */
489 #ifdef BACKSQL_PRETTY_VALIDATE
490 validate = at->bam_ad->ad_type->sat_syntax->ssyn_validate;
491 pretty = at->bam_ad->ad_type->sat_syntax->ssyn_pretty;
493 if ( validate == NULL && pretty == NULL ) {
496 #endif /* BACKSQL_PRETTY_VALIDATE */
498 #ifdef BACKSQL_COUNTQUERY
499 if ( at->bam_ad->ad_type->sat_equality ) {
500 normfunc = at->bam_ad->ad_type->sat_equality->smr_normalize;
503 /* Count how many rows will be returned. This avoids memory
504 * fragmentation that can result from loading the values in
505 * one by one and using realloc()
507 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_countquery, 0 );
508 if ( rc != SQL_SUCCESS ) {
509 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
510 "error preparing count query: %s\n",
511 at->bam_countquery, 0, 0 );
512 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
516 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
517 &bsi->bsi_c_eid->eid_keyval );
518 if ( rc != SQL_SUCCESS ) {
519 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
520 "error binding key value parameter\n", 0, 0, 0 );
521 SQLFreeStmt( sth, SQL_DROP );
525 rc = SQLExecute( sth );
526 if ( ! BACKSQL_SUCCESS( rc ) ) {
527 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
528 "error executing attribute count query '%s'\n",
529 at->bam_countquery, 0, 0 );
530 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
531 SQLFreeStmt( sth, SQL_DROP );
535 SQLBindCol( sth, (SQLUSMALLINT)1, SQL_C_LONG,
537 (SQLINTEGER)sizeof( count ),
540 rc = SQLFetch( sth );
541 if ( rc != SQL_SUCCESS ) {
542 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
543 "error fetch results of count query: %s\n",
544 at->bam_countquery, 0, 0 );
545 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
546 SQLFreeStmt( sth, SQL_DROP );
550 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
551 "number of values in query: %d\n", count, 0, 0 );
552 SQLFreeStmt( sth, SQL_DROP );
557 attr = attr_find( bsi->bsi_e->e_attrs, at->bam_ad );
558 if ( attr != NULL ) {
561 if ( attr->a_vals != NULL ) {
562 for ( ; !BER_BVISNULL( &attr->a_vals[ oldcount ] ); oldcount++ )
566 tmp = ch_realloc( attr->a_vals, ( oldcount + count + 1 ) * sizeof( struct berval ) );
571 memset( &attr->a_vals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) );
574 tmp = ch_realloc( attr->a_nvals, ( oldcount + count + 1 ) * sizeof( struct berval ) );
579 memset( &attr->a_nvals[ oldcount ], 0, ( count + 1 ) * sizeof( struct berval ) );
582 attr->a_nvals = attr->a_vals;
588 /* Make space for the array of values */
589 attr = (Attribute *) ch_malloc( sizeof( Attribute ) );
590 attr->a_desc = at->bam_ad;
593 attr->a_vals = ch_calloc( count + 1, sizeof( struct berval ) );
594 if ( attr->a_vals == NULL ) {
595 Debug( LDAP_DEBUG_TRACE, "Out of memory!\n", 0,0,0 );
599 memset( attr->a_vals, 0, ( count + 1 ) * sizeof( struct berval ) );
601 attr->a_nvals = ch_calloc( count + 1, sizeof( struct berval ) );
602 if ( attr->a_nvals == NULL ) {
603 ch_free( attr->a_vals );
608 memset( attr->a_nvals, 0, ( count + 1 ) * sizeof( struct berval ) );
612 attr->a_nvals = attr->a_vals;
615 for ( ap = &bsi->bsi_e->e_attrs; (*ap) != NULL; ap = &(*ap)->a_next )
619 #endif /* BACKSQL_COUNTQUERY */
621 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
622 if ( rc != SQL_SUCCESS ) {
623 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
624 "error preparing query: %s\n", at->bam_query, 0, 0 );
625 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
629 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
630 &bsi->bsi_c_eid->eid_keyval );
631 if ( rc != SQL_SUCCESS ) {
632 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
633 "error binding key value parameter\n", 0, 0, 0 );
638 #ifdef BACKSQL_ARBITRARY_KEY
639 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
640 "query=\"%s\" keyval=%s\n", at->bam_query,
641 bsi->bsi_c_eid->eid_keyval.bv_val, 0 );
642 #else /* !BACKSQL_ARBITRARY_KEY */
643 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
644 "query=\"%s\" keyval=%d\n", at->bam_query,
645 bsi->bsi_c_eid->eid_keyval, 0 );
646 #endif /* ! BACKSQL_ARBITRARY_KEY */
647 #endif /* BACKSQL_TRACE */
649 rc = SQLExecute( sth );
650 if ( ! BACKSQL_SUCCESS( rc ) ) {
651 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_vals(): "
652 "error executing attribute query \"%s\"\n",
653 at->bam_query, 0, 0 );
654 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
655 SQLFreeStmt( sth, SQL_DROP );
659 backsql_BindRowAsStrings( sth, &row );
660 #ifdef BACKSQL_COUNTQUERY
662 #endif /* BACKSQL_COUNTQUERY */
663 for ( rc = SQLFetch( sth ), k = 0;
664 BACKSQL_SUCCESS( rc );
665 rc = SQLFetch( sth ), k++ )
667 for ( i = 0; i < row.ncols; i++ ) {
669 if ( row.value_len[ i ] > 0 ) {
673 AttributeDescription *ad = NULL;
676 retval = slap_bv2ad( &row.col_names[ i ], &ad, &text );
677 if ( retval != LDAP_SUCCESS ) {
678 Debug( LDAP_DEBUG_ANY,
679 "==>backsql_get_attr_vals(\"%s\"): "
680 "unable to find AttributeDescription %s "
682 bsi->bsi_e->e_name.bv_val,
683 row.col_names[ i ].bv_val, retval );
687 if ( ad != at->bam_ad ) {
688 Debug( LDAP_DEBUG_ANY,
689 "==>backsql_get_attr_vals(\"%s\"): "
690 "column name %s differs from "
691 "AttributeDescription %s\n",
692 bsi->bsi_e->e_name.bv_val,
694 at->bam_ad->ad_cname.bv_val );
697 #endif /* BACKSQL_TRACE */
700 * FIXME: what if a binary
703 ber_str2bv( row.cols[ i ], 0, 0, &bv );
705 #ifdef BACKSQL_PRETTY_VALIDATE
709 retval = pretty( at->bam_ad->ad_type->sat_syntax,
710 &bv, &pbv, bsi->bsi_op->o_tmpmemctx );
714 retval = validate( at->bam_ad->ad_type->sat_syntax,
718 if ( retval != LDAP_SUCCESS ) {
719 char buf[ SLAP_TEXT_BUFLEN ];
721 /* FIXME: we're ignoring invalid values,
722 * but we're accepting the attributes;
723 * should we fail at all? */
724 snprintf( buf, sizeof( buf ),
725 "unable to %s value #%d "
726 "of AttributeDescription %s",
727 pretty ? "prettify" : "validate",
728 at->bam_ad->ad_cname.bv_val,
730 Debug( LDAP_DEBUG_TRACE,
731 "==>backsql_get_attr_vals(\"%s\"): "
733 bsi->bsi_e->e_name.bv_val, buf, retval );
736 #endif /* BACKSQL_PRETTY_VALIDATE */
738 #ifndef BACKSQL_COUNTQUERY
739 (void)backsql_entry_addattr( bsi->bsi_e,
741 bsi->bsi_op->o_tmpmemctx );
743 #else /* BACKSQL_COUNTQUERY */
747 retval = (*normfunc)( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
748 at->bam_ad->ad_type->sat_syntax,
749 at->bam_ad->ad_type->sat_equality,
751 bsi->bsi_op->o_tmpmemctx );
753 if ( retval != LDAP_SUCCESS ) {
754 char buf[ SLAP_TEXT_BUFLEN ];
756 /* FIXME: we're ignoring invalid values,
757 * but we're accepting the attributes;
758 * should we fail at all? */
759 snprintf( buf, sizeof( buf ),
760 "unable to normalize value #%d "
761 "of AttributeDescription %s",
762 at->bam_ad->ad_cname.bv_val,
764 Debug( LDAP_DEBUG_TRACE,
765 "==>backsql_get_attr_vals(\"%s\"): "
767 bsi->bsi_e->e_name.bv_val, buf, retval );
769 #ifdef BACKSQL_PRETTY_VALIDATE
771 bsi->bsi_op->o_tmpfree( bv.bv_val,
772 bsi->bsi_op->o_tmpmemctx );
774 #endif /* BACKSQL_PRETTY_VALIDATE */
778 ber_dupbv( &attr->a_nvals[ j ], &nbv );
779 bsi->bsi_op->o_tmpfree( nbv.bv_val,
780 bsi->bsi_op->o_tmpmemctx );
783 ber_dupbv( &attr->a_vals[ j ], &bv );
785 assert( j < oldcount + count );
787 #endif /* BACKSQL_COUNTQUERY */
789 #ifdef BACKSQL_PRETTY_VALIDATE
791 bsi->bsi_op->o_tmpfree( bv.bv_val,
792 bsi->bsi_op->o_tmpmemctx );
794 #endif /* BACKSQL_PRETTY_VALIDATE */
797 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
798 (int)row.col_prec[ i ], 0, 0 );
801 Debug( LDAP_DEBUG_TRACE, "NULL value "
802 "in this row for attribute \"%s\"\n",
803 row.col_names[ i ].bv_val, 0, 0 );
804 #endif /* BACKSQL_TRACE */
809 backsql_FreeRow( &row );
810 SQLFreeStmt( sth, SQL_DROP );
811 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
813 if ( at->bam_next ) {
814 return backsql_get_attr_vals( at->bam_next, v_bsi );
821 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
823 Operation *op = bsi->bsi_op;
824 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
828 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
830 assert( bsi->bsi_e );
832 memset( bsi->bsi_e, 0, sizeof( Entry ) );
834 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
837 e = entry_dup( bi->sql_baseObject );
839 return LDAP_NO_MEMORY;
847 ber_dupbv_x( &bsi->bsi_e->e_name, &eid->eid_dn, op->o_tmpmemctx );
848 ber_dupbv_x( &bsi->bsi_e->e_nname, &eid->eid_ndn, op->o_tmpmemctx );
850 bsi->bsi_e->e_attrs = NULL;
851 bsi->bsi_e->e_private = NULL;
853 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
855 bsi->bsi_c_eid = eid;
857 #ifndef BACKSQL_ARBITRARY_KEY
859 bsi->bsi_e->e_id = eid->eid_id;
860 #endif /* ! BACKSQL_ARBITRARY_KEY */
862 rc = attr_merge_normalize_one( bsi->bsi_e,
863 slap_schema.si_ad_objectClass,
864 &bsi->bsi_oc->bom_oc->soc_cname,
865 bsi->bsi_op->o_tmpmemctx );
866 if ( rc != LDAP_SUCCESS ) {
867 entry_clean( bsi->bsi_e );
871 if ( bsi->bsi_attrs == NULL || ( bsi->bsi_flags & BSQL_SF_ALL_USER ) )
873 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
874 "retrieving all attributes\n", 0, 0, 0 );
875 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
876 bsi, 0, AVL_INORDER );
879 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
880 "custom attribute list\n", 0, 0, 0 );
881 for ( i = 0; !BER_BVISNULL( &bsi->bsi_attrs[ i ].an_name ); i++ ) {
882 backsql_at_map_rec **vat;
883 AttributeName *an = &bsi->bsi_attrs[ i ];
886 /* if one of the attributes listed here is
887 * a subtype of another, it must be ignored,
888 * because subtypes are already dealt with
889 * by backsql_supad2at()
891 for ( j = 0; !BER_BVISNULL( &bsi->bsi_attrs[ j ].an_name ); j++ ) {
898 if ( is_at_subtype( an->an_desc->ad_type,
899 bsi->bsi_attrs[ j ].an_desc->ad_type ) )
905 rc = backsql_supad2at( bsi->bsi_oc, an->an_desc, &vat );
906 if ( rc != 0 || vat == NULL ) {
907 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
908 "attribute \"%s\" is not defined "
909 "for objectlass \"%s\"\n",
911 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
915 for ( j = 0; vat[j]; j++ ) {
916 backsql_get_attr_vals( vat[j], bsi );
925 if ( bsi->bsi_flags & BSQL_SF_RETURN_ENTRYUUID ) {
926 Attribute *a_entryUUID,
929 a_entryUUID = backsql_operational_entryUUID( bi, eid );
930 if ( a_entryUUID != NULL ) {
931 for ( ap = &bsi->bsi_e->e_attrs;
933 ap = &(*ap)->a_next );
939 if ( global_schemacheck ) {
940 const char *text = NULL;
941 char textbuf[ 1024 ];
942 size_t textlen = sizeof( textbuf );
943 struct berval bv[ 2 ];
947 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
948 BER_BVZERO( &bv[ 1 ] );
950 rc = structural_class( bv, &soc, NULL,
951 &text, textbuf, textlen );
952 if ( rc != LDAP_SUCCESS ) {
953 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
954 "structural_class() failed %d (%s)\n",
955 bsi->bsi_e->e_name.bv_val,
956 rc, text ? text : "" );
957 entry_clean( bsi->bsi_e );
961 if ( ( bsi->bsi_flags & BSQL_SF_ALL_OPER )
962 || an_find( bsi->bsi_attrs, &AllOper )
963 || an_find( bsi->bsi_attrs, &slap_schema.si_ad_structuralObjectClass->ad_cname ) )
965 rc = attr_merge_normalize_one( bsi->bsi_e,
966 slap_schema.si_ad_structuralObjectClass,
967 &soc, bsi->bsi_op->o_tmpmemctx );
968 if ( rc != LDAP_SUCCESS ) {
969 entry_clean( bsi->bsi_e );
976 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );