2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
24 #include <sys/types.h>
25 #include "ac/string.h"
28 #include "proto-sql.h"
30 #ifdef BACKSQL_ARBITRARY_KEY
31 struct berval backsql_baseObject_bv = BER_BVC( BACKSQL_BASEOBJECT_IDSTR );
32 #endif /* BACKSQL_ARBITRARY_KEY */
35 backsql_free_entryID( backsql_entryID *id, int freeit )
37 backsql_entryID *next;
43 if ( !BER_BVISNULL( &id->eid_ndn ) ) {
44 if ( !BER_BVISNULL( &id->eid_dn )
45 && id->eid_dn.bv_val != id->eid_ndn.bv_val )
47 free( id->eid_dn.bv_val );
48 BER_BVZERO( &id->eid_dn );
51 free( id->eid_ndn.bv_val );
52 BER_BVZERO( &id->eid_ndn );
55 #ifdef BACKSQL_ARBITRARY_KEY
56 if ( id->eid_id.bv_val ) {
57 free( id->eid_id.bv_val );
58 BER_BVZERO( &id->eid_id );
61 if ( id->eid_keyval.bv_val ) {
62 free( id->eid_keyval.bv_val );
63 BER_BVZERO( &id->eid_keyval );
65 #endif /* BACKSQL_ARBITRARY_KEY */
75 * NOTE: the dn must be normalized
86 backsql_info *bi = op->o_bd->be_private;
87 SQLHSTMT sth = SQL_NULL_HSTMT;
91 struct berval realndn = BER_BVNULL;
94 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
99 * NOTE: id can be NULL; in this case, the function
100 * simply checks whether the DN can be successfully
101 * turned into an ID, returning LDAP_SUCCESS for
102 * positive cases, or the most appropriate error
105 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn=\"%s\"%s\n",
106 ndn->bv_val, id == NULL ? " (no ID)" : "", 0 );
108 if ( ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
109 Debug( LDAP_DEBUG_TRACE,
110 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
111 "exceeds max DN length (%d):\n",
112 ndn->bv_val, ndn->bv_len, BACKSQL_MAX_DN_LEN );
116 /* return baseObject if available and matches */
117 if ( bi->sql_baseObject != NULL && dn_match( ndn, &bi->sql_baseObject->e_nname ) ) {
119 #ifdef BACKSQL_ARBITRARY_KEY
120 ber_dupbv( &id->eid_id, &backsql_baseObject_bv );
121 ber_dupbv( &id->eid_keyval, &backsql_baseObject_bv );
122 #else /* ! BACKSQL_ARBITRARY_KEY */
123 id->eid_id = BACKSQL_BASEOBJECT_ID;
124 id->eid_keyval = BACKSQL_BASEOBJECT_KEYVAL;
125 #endif /* ! BACKSQL_ARBITRARY_KEY */
126 id->eid_oc_id = BACKSQL_BASEOBJECT_OC;
128 ber_dupbv( &id->eid_ndn, &bi->sql_baseObject->e_nname );
129 ber_dupbv( &id->eid_dn, &bi->sql_baseObject->e_name );
138 Debug( LDAP_DEBUG_TRACE, "id_query \"%s\"\n", bi->sql_id_query, 0, 0 );
139 assert( bi->sql_id_query );
140 rc = backsql_Prepare( dbh, &sth, bi->sql_id_query, 0 );
141 if ( rc != SQL_SUCCESS ) {
142 Debug( LDAP_DEBUG_TRACE,
143 "backsql_dn2id(): error preparing SQL:\n%s",
144 bi->sql_id_query, 0, 0);
145 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
152 if ( backsql_api_dn2odbc( op, rs, &realndn ) ) {
153 Debug( LDAP_DEBUG_TRACE, " backsql_dn2id(\"%s\"): "
154 "backsql_api_dn2odbc(\"%s\") failed\n",
155 ndn->bv_val, realndn.bv_val, 0 );
161 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
163 * Prepare an upper cased, byte reversed version
164 * that can be searched using indexes
167 for ( i = 0, j = realndn.bv_len - 1; realndn.bv_val[ i ]; i++, j--) {
168 upperdn[ i ] = realndn.bv_val[ j ];
171 ldap_pvt_str2upper( upperdn );
173 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn=\"%s\"\n",
175 ber_str2bv( upperdn, 0, 0, &tbbDN );
178 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
179 AC_MEMCPY( upperdn, realndn.bv_val, realndn.bv_len + 1 );
180 ldap_pvt_str2upper( upperdn );
181 Debug( LDAP_DEBUG_TRACE,
182 "==>backsql_dn2id(): upperdn=\"%s\"\n",
184 ber_str2bv( upperdn, 0, 0, &tbbDN );
191 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &tbbDN );
192 if ( rc != SQL_SUCCESS) {
194 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
195 "error binding dn=\"%s\" parameter:\n",
196 tbbDN.bv_val, 0, 0 );
197 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
202 rc = SQLExecute( sth );
203 if ( rc != SQL_SUCCESS ) {
204 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
205 "error executing query (\"%s\", \"%s\"):\n",
206 bi->sql_id_query, tbbDN.bv_val, 0 );
207 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
212 backsql_BindRowAsStrings( sth, &row );
213 rc = SQLFetch( sth );
214 if ( BACKSQL_SUCCESS( rc ) ) {
218 snprintf( buf, sizeof(buf),
219 "id=%s keyval=%s oc_id=%s dn=%s",
220 row.cols[ 0 ], row.cols[ 1 ],
221 row.cols[ 2 ], row.cols[ 3 ] );
222 Debug( LDAP_DEBUG_TRACE,
223 "<==backsql_dn2id(): %s\n", buf, 0, 0 );
224 #endif /* LDAP_DEBUG */
230 #ifdef BACKSQL_ARBITRARY_KEY
231 ber_str2bv( row.cols[ 0 ], 0, 1, &id->eid_id );
232 ber_str2bv( row.cols[ 1 ], 0, 1, &id->eid_keyval );
233 #else /* ! BACKSQL_ARBITRARY_KEY */
234 id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
235 id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
236 #endif /* ! BACKSQL_ARBITRARY_KEY */
237 id->eid_oc_id = strtol( row.cols[ 2 ], NULL, 0 );
239 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
241 if ( backsql_api_odbc2dn( op, rs, &dn ) ) {
245 res = dnPrettyNormal( NULL, &dn, &id->eid_dn, &id->eid_ndn, NULL );
246 if ( res != LDAP_SUCCESS ) {
247 Debug( LDAP_DEBUG_TRACE,
248 "<==backsql_dn2id(\"%s\"): "
249 "dnPrettyNormal failed (%d: %s)\n",
251 ldap_err2string( res ) );
254 (void)backsql_free_entryID( id, 0 );
257 if ( dn.bv_val != row.cols[ 3 ] ) {
266 res = LDAP_NO_SUCH_OBJECT;
267 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
270 backsql_FreeRow( &row );
273 if ( sth != SQL_NULL_HSTMT ) {
274 SQLFreeStmt( sth, SQL_DROP );
277 if ( !BER_BVISNULL( &realndn ) && realndn.bv_val != ndn->bv_val ) {
278 ch_free( realndn.bv_val );
285 backsql_count_children(
289 unsigned long *nchildren )
294 int res = LDAP_SUCCESS;
296 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n",
299 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
300 Debug( LDAP_DEBUG_TRACE,
301 "backsql_count_children(): DN \"%s\" (%ld bytes) "
302 "exceeds max DN length (%d):\n",
303 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
308 Debug(LDAP_DEBUG_TRACE, "children id query \"%s\"\n",
309 bi->sql_has_children_query, 0, 0);
310 assert( bi->sql_has_children_query );
311 rc = backsql_Prepare( dbh, &sth, bi->sql_has_children_query, 0 );
312 if ( rc != SQL_SUCCESS ) {
313 Debug( LDAP_DEBUG_TRACE,
314 "backsql_count_children(): error preparing SQL:\n%s",
315 bi->sql_has_children_query, 0, 0);
316 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
317 SQLFreeStmt( sth, SQL_DROP );
321 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
322 if ( rc != SQL_SUCCESS) {
324 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
325 "error binding dn=\"%s\" parameter:\n",
327 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
328 SQLFreeStmt( sth, SQL_DROP );
332 rc = SQLExecute( sth );
333 if ( rc != SQL_SUCCESS ) {
334 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
335 "error executing query (\"%s\", \"%s\"):\n",
336 bi->sql_has_children_query, dn->bv_val, 0 );
337 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
338 SQLFreeStmt( sth, SQL_DROP );
342 backsql_BindRowAsStrings( sth, &row );
344 rc = SQLFetch( sth );
345 if ( BACKSQL_SUCCESS( rc ) ) {
348 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
349 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
350 /* FIXME: braindead RDBMSes return
351 * a fractional number from COUNT!
359 backsql_FreeRow( &row );
361 SQLFreeStmt( sth, SQL_DROP );
363 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
370 backsql_has_children(
375 unsigned long nchildren;
378 rc = backsql_count_children( bi, dbh, dn, &nchildren );
380 if ( rc == LDAP_SUCCESS ) {
381 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
388 backsql_get_attr_vals( void *v_at, void *v_bsi )
390 backsql_at_map_rec *at = v_at;
391 backsql_srch_info *bsi = v_bsi;
392 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
401 #ifdef BACKSQL_ARBITRARY_KEY
402 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
403 "oc=\"%s\" attr=\"%s\" keyval=%s\n",
404 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
405 bsi->bsi_c_eid->eid_keyval.bv_val );
406 #else /* ! BACKSQL_ARBITRARY_KEY */
407 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
408 "oc=\"%s\" attr=\"%s\" keyval=%ld\n",
409 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
410 bsi->bsi_c_eid->eid_keyval );
411 #endif /* ! BACKSQL_ARBITRARY_KEY */
413 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
414 if ( rc != SQL_SUCCESS ) {
415 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
416 "error preparing query: %s\n", at->bam_query, 0, 0 );
417 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
421 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
422 &bsi->bsi_c_eid->eid_keyval );
423 if ( rc != SQL_SUCCESS ) {
424 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
425 "error binding key value parameter\n", 0, 0, 0 );
430 #ifdef BACKSQL_ARBITRARY_KEY
431 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
432 "query=\"%s\" keyval=%s\n", at->bam_query,
433 bsi->bsi_c_eid->eid_keyval.bv_val, 0 );
434 #else /* !BACKSQL_ARBITRARY_KEY */
435 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
436 "query=\"%s\" keyval=%d\n", at->bam_query,
437 bsi->bsi_c_eid->eid_keyval, 0 );
438 #endif /* ! BACKSQL_ARBITRARY_KEY */
439 #endif /* BACKSQL_TRACE */
441 rc = SQLExecute( sth );
442 if ( ! BACKSQL_SUCCESS( rc ) ) {
443 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
444 "error executing attribute query \"%s\"\n",
445 at->bam_query, 0, 0 );
446 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
447 SQLFreeStmt( sth, SQL_DROP );
451 backsql_BindRowAsStrings( sth, &row );
453 rc = SQLFetch( sth );
454 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
455 for ( i = 0; i < row.ncols; i++ ) {
456 if ( row.value_len[ i ] > 0 ) {
459 bv.bv_val = row.cols[ i ];
461 bv.bv_len = row.col_prec[ i ];
464 * FIXME: what if a binary
467 bv.bv_len = strlen( row.cols[ i ] );
469 backsql_entry_addattr( bsi->bsi_e,
470 &row.col_names[ i ], &bv,
471 bsi->bsi_op->o_tmpmemctx );
474 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
475 (int)row.col_prec[ i ], 0, 0 );
477 Debug( LDAP_DEBUG_TRACE, "NULL value "
478 "in this row for attribute \"%s\"\n",
479 row.col_names[ i ].bv_val, 0, 0 );
480 #endif /* BACKSQL_TRACE */
485 backsql_FreeRow( &row );
486 SQLFreeStmt( sth, SQL_DROP );
487 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
489 if ( at->bam_next ) {
490 return backsql_get_attr_vals( at->bam_next, v_bsi );
497 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
499 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
502 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
504 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
506 assert( bsi->bsi_e );
508 memset( bsi->bsi_e, 0, sizeof( Entry ) );
510 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
513 e = entry_dup( bi->sql_baseObject );
515 return LDAP_NO_MEMORY;
523 ber_dupbv_x( &bsi->bsi_e->e_name, &eid->eid_dn, bsi->bsi_op->o_tmpmemctx );
524 ber_dupbv_x( &bsi->bsi_e->e_nname, &eid->eid_ndn, bsi->bsi_op->o_tmpmemctx );
526 bsi->bsi_e->e_attrs = NULL;
527 bsi->bsi_e->e_private = NULL;
529 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
531 bsi->bsi_c_eid = eid;
533 #ifndef BACKSQL_ARBITRARY_KEY
534 bsi->bsi_e->e_id = eid->eid_id;
535 #endif /* ! BACKSQL_ARBITRARY_KEY */
537 rc = attr_merge_normalize_one( bsi->bsi_e, ad_oc,
538 &bsi->bsi_oc->bom_oc->soc_cname,
539 bsi->bsi_op->o_tmpmemctx );
540 if ( rc != LDAP_SUCCESS ) {
541 entry_clean( bsi->bsi_e );
545 if ( bsi->bsi_attrs == NULL || ( bsi->bsi_flags & BSQL_SF_ALL_USER ) )
547 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
548 "retrieving all attributes\n", 0, 0, 0 );
549 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
550 bsi, 0, AVL_INORDER );
553 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
554 "custom attribute list\n", 0, 0, 0 );
555 for ( i = 0; bsi->bsi_attrs[ i ].an_name.bv_val; i++ ) {
556 backsql_at_map_rec **vat;
557 AttributeName *an = &bsi->bsi_attrs[ i ];
560 /* if one of the attributes listed here is
561 * a subtype of another, it must be ignored,
562 * because subtypes are already dealt with
563 * by backsql_supad2at()
565 for ( j = 0; bsi->bsi_attrs[ j ].an_name.bv_val; j++ ) {
572 if ( is_at_subtype( an->an_desc->ad_type,
573 bsi->bsi_attrs[ j ].an_desc->ad_type ) )
579 rc = backsql_supad2at( bsi->bsi_oc, an->an_desc, &vat );
580 if ( rc != 0 || vat == NULL ) {
581 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
582 "attribute \"%s\" is not defined "
583 "for objectlass \"%s\"\n",
585 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
589 for ( j = 0; vat[j]; j++ ) {
590 backsql_get_attr_vals( vat[j], bsi );
599 if ( bsi->bsi_flags & BSQL_SF_RETURN_ENTRYUUID ) {
600 Attribute *a_entryUUID,
603 a_entryUUID = backsql_operational_entryUUID( bi, eid );
604 if ( a_entryUUID != NULL ) {
605 for ( ap = &bsi->bsi_e->e_attrs;
607 ap = &(*ap)->a_next );
613 if ( global_schemacheck ) {
614 const char *text = NULL;
615 char textbuf[ 1024 ];
616 size_t textlen = sizeof( textbuf );
617 struct berval bv[ 2 ];
621 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
622 bv[ 1 ].bv_val = NULL;
624 rc = structural_class( bv, &soc, NULL,
625 &text, textbuf, textlen );
626 if ( rc != LDAP_SUCCESS ) {
627 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
628 "structural_class() failed %d (%s)\n",
629 bsi->bsi_e->e_name.bv_val,
630 rc, text ? text : "" );
631 entry_clean( bsi->bsi_e );
635 if ( ( bsi->bsi_flags & BSQL_SF_ALL_OPER )
636 || an_find( bsi->bsi_attrs, &AllOper )
637 || an_find( bsi->bsi_attrs, &slap_schema.si_ad_structuralObjectClass->ad_cname ) )
639 rc = attr_merge_normalize_one( bsi->bsi_e,
640 slap_schema.si_ad_structuralObjectClass,
641 &soc, bsi->bsi_op->o_tmpmemctx );
642 if ( rc != LDAP_SUCCESS ) {
643 entry_clean( bsi->bsi_e );
650 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );