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
85 backsql_info *bi = op->o_bd->be_private;
92 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
97 * NOTE: id can be NULL; in this case, the function
98 * simply checks whether the DN can be successfully
99 * turned into an ID, returning LDAP_SUCCESS for
100 * positive cases, or the most appropriate error
103 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn=\"%s\"%s\n",
104 ndn->bv_val, id == NULL ? " (no ID)" : "", 0 );
106 if ( ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
107 Debug( LDAP_DEBUG_TRACE,
108 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
109 "exceeds max DN length (%d):\n",
110 ndn->bv_val, ndn->bv_len, BACKSQL_MAX_DN_LEN );
114 /* return baseObject if available and matches */
115 if ( bi->sql_baseObject != NULL && dn_match( ndn, &bi->sql_baseObject->e_nname ) ) {
117 #ifdef BACKSQL_ARBITRARY_KEY
118 ber_dupbv( &id->eid_id, &backsql_baseObject_bv );
119 ber_dupbv( &id->eid_keyval, &backsql_baseObject_bv );
120 #else /* ! BACKSQL_ARBITRARY_KEY */
121 id->eid_id = BACKSQL_BASEOBJECT_ID;
122 id->eid_keyval = BACKSQL_BASEOBJECT_KEYVAL;
123 #endif /* ! BACKSQL_ARBITRARY_KEY */
124 id->eid_oc_id = BACKSQL_BASEOBJECT_OC;
126 ber_dupbv( &id->eid_ndn, &bi->sql_baseObject->e_nname );
127 ber_dupbv( &id->eid_dn, &bi->sql_baseObject->e_name );
136 Debug( LDAP_DEBUG_TRACE, "id_query \"%s\"\n", bi->sql_id_query, 0, 0 );
137 assert( bi->sql_id_query );
138 rc = backsql_Prepare( dbh, &sth, bi->sql_id_query, 0 );
139 if ( rc != SQL_SUCCESS ) {
140 Debug( LDAP_DEBUG_TRACE,
141 "backsql_dn2id(): error preparing SQL:\n%s",
142 bi->sql_id_query, 0, 0);
143 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
144 SQLFreeStmt( sth, SQL_DROP );
148 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
150 * Prepare an upper cased, byte reversed version
151 * that can be searched using indexes
154 for ( i = 0, j = ndn->bv_len - 1; ndn->bv_val[ i ]; i++, j--) {
155 upperdn[ i ] = ndn->bv_val[ j ];
158 ldap_pvt_str2upper( upperdn );
160 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn=\"%s\"\n",
162 ber_str2bv( upperdn, 0, 0, &tbbDN );
165 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
166 AC_MEMCPY( upperdn, ndn->bv_val, ndn->bv_len + 1 );
167 ldap_pvt_str2upper( upperdn );
168 Debug( LDAP_DEBUG_TRACE,
169 "==>backsql_dn2id(): upperdn=\"%s\"\n",
171 ber_str2bv( upperdn, 0, 0, &tbbDN );
178 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &tbbDN );
179 if ( rc != SQL_SUCCESS) {
181 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
182 "error binding dn=\"%s\" parameter:\n",
183 tbbDN.bv_val, 0, 0 );
184 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
185 SQLFreeStmt( sth, SQL_DROP );
189 rc = SQLExecute( sth );
190 if ( rc != SQL_SUCCESS ) {
191 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
192 "error executing query (\"%s\", \"%s\"):\n",
193 bi->sql_id_query, tbbDN.bv_val, 0 );
194 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
195 SQLFreeStmt( sth, SQL_DROP );
199 backsql_BindRowAsStrings( sth, &row );
200 rc = SQLFetch( sth );
201 if ( BACKSQL_SUCCESS( rc ) ) {
205 snprintf( buf, sizeof(buf),
206 "id=%s keyval=%s oc_id=%s dn=%s",
207 row.cols[ 0 ], row.cols[ 1 ],
208 row.cols[ 2 ], row.cols[ 3 ] );
209 Debug( LDAP_DEBUG_TRACE,
210 "<==backsql_dn2id(): %s\n", buf, 0, 0 );
211 #endif /* LDAP_DEBUG */
217 #ifdef BACKSQL_ARBITRARY_KEY
218 ber_str2bv( row.cols[ 0 ], 0, 1, &id->eid_id );
219 ber_str2bv( row.cols[ 1 ], 0, 1, &id->eid_keyval );
220 #else /* ! BACKSQL_ARBITRARY_KEY */
221 id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
222 id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
223 #endif /* ! BACKSQL_ARBITRARY_KEY */
224 id->eid_oc_id = strtol( row.cols[ 2 ], NULL, 0 );
226 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
228 if ( backsql_api_odbc2dn( op, rs, &dn ) ) {
232 res = dnPrettyNormal( NULL, &dn, &id->eid_dn, &id->eid_ndn, NULL );
233 if ( res != LDAP_SUCCESS ) {
234 Debug( LDAP_DEBUG_TRACE,
235 "<==backsql_dn2id(\"%s\"): "
236 "dnPrettyNormal failed (%d: %s)\n",
238 ldap_err2string( res ) );
241 (void)backsql_free_entryID( id, 0 );
244 if ( dn.bv_val != row.cols[ 3 ] ) {
253 res = LDAP_NO_SUCH_OBJECT;
254 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
257 backsql_FreeRow( &row );
259 SQLFreeStmt( sth, SQL_DROP );
264 backsql_count_children(
268 unsigned long *nchildren )
273 int res = LDAP_SUCCESS;
275 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n",
278 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
279 Debug( LDAP_DEBUG_TRACE,
280 "backsql_count_children(): DN \"%s\" (%ld bytes) "
281 "exceeds max DN length (%d):\n",
282 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
287 Debug(LDAP_DEBUG_TRACE, "children id query \"%s\"\n",
288 bi->sql_has_children_query, 0, 0);
289 assert( bi->sql_has_children_query );
290 rc = backsql_Prepare( dbh, &sth, bi->sql_has_children_query, 0 );
291 if ( rc != SQL_SUCCESS ) {
292 Debug( LDAP_DEBUG_TRACE,
293 "backsql_count_children(): error preparing SQL:\n%s",
294 bi->sql_has_children_query, 0, 0);
295 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
296 SQLFreeStmt( sth, SQL_DROP );
300 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
301 if ( rc != SQL_SUCCESS) {
303 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
304 "error binding dn=\"%s\" parameter:\n",
306 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
307 SQLFreeStmt( sth, SQL_DROP );
311 rc = SQLExecute( sth );
312 if ( rc != SQL_SUCCESS ) {
313 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
314 "error executing query (\"%s\", \"%s\"):\n",
315 bi->sql_has_children_query, dn->bv_val, 0 );
316 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
317 SQLFreeStmt( sth, SQL_DROP );
321 backsql_BindRowAsStrings( sth, &row );
323 rc = SQLFetch( sth );
324 if ( BACKSQL_SUCCESS( rc ) ) {
327 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
328 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
329 /* FIXME: braindead RDBMSes return
330 * a fractional number from COUNT!
338 backsql_FreeRow( &row );
340 SQLFreeStmt( sth, SQL_DROP );
342 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
349 backsql_has_children(
354 unsigned long nchildren;
357 rc = backsql_count_children( bi, dbh, dn, &nchildren );
359 if ( rc == LDAP_SUCCESS ) {
360 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
367 backsql_get_attr_vals( void *v_at, void *v_bsi )
369 backsql_at_map_rec *at = v_at;
370 backsql_srch_info *bsi = v_bsi;
371 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
380 #ifdef BACKSQL_ARBITRARY_KEY
381 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
382 "oc=\"%s\" attr=\"%s\" keyval=%s\n",
383 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
384 bsi->bsi_c_eid->eid_keyval.bv_val );
385 #else /* ! BACKSQL_ARBITRARY_KEY */
386 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
387 "oc=\"%s\" attr=\"%s\" keyval=%ld\n",
388 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
389 bsi->bsi_c_eid->eid_keyval );
390 #endif /* ! BACKSQL_ARBITRARY_KEY */
392 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
393 if ( rc != SQL_SUCCESS ) {
394 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
395 "error preparing query: %s\n", at->bam_query, 0, 0 );
396 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
400 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
401 &bsi->bsi_c_eid->eid_keyval );
402 if ( rc != SQL_SUCCESS ) {
403 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
404 "error binding key value parameter\n", 0, 0, 0 );
409 #ifdef BACKSQL_ARBITRARY_KEY
410 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
411 "query=\"%s\" keyval=%s\n", at->bam_query,
412 bsi->bsi_c_eid->eid_keyval.bv_val, 0 );
413 #else /* !BACKSQL_ARBITRARY_KEY */
414 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
415 "query=\"%s\" keyval=%d\n", at->bam_query,
416 bsi->bsi_c_eid->eid_keyval, 0 );
417 #endif /* ! BACKSQL_ARBITRARY_KEY */
418 #endif /* BACKSQL_TRACE */
420 rc = SQLExecute( sth );
421 if ( ! BACKSQL_SUCCESS( rc ) ) {
422 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
423 "error executing attribute query \"%s\"\n",
424 at->bam_query, 0, 0 );
425 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
426 SQLFreeStmt( sth, SQL_DROP );
430 backsql_BindRowAsStrings( sth, &row );
432 rc = SQLFetch( sth );
433 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
434 for ( i = 0; i < row.ncols; i++ ) {
435 if ( row.value_len[ i ] > 0 ) {
438 bv.bv_val = row.cols[ i ];
440 bv.bv_len = row.col_prec[ i ];
443 * FIXME: what if a binary
446 bv.bv_len = strlen( row.cols[ i ] );
448 backsql_entry_addattr( bsi->bsi_e,
449 &row.col_names[ i ], &bv,
450 bsi->bsi_op->o_tmpmemctx );
453 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
454 (int)row.col_prec[ i ], 0, 0 );
456 Debug( LDAP_DEBUG_TRACE, "NULL value "
457 "in this row for attribute \"%s\"\n",
458 row.col_names[ i ].bv_val, 0, 0 );
459 #endif /* BACKSQL_TRACE */
464 backsql_FreeRow( &row );
465 SQLFreeStmt( sth, SQL_DROP );
466 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
468 if ( at->bam_next ) {
469 return backsql_get_attr_vals( at->bam_next, v_bsi );
476 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
478 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
481 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
483 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
485 assert( bsi->bsi_e );
487 memset( bsi->bsi_e, 0, sizeof( Entry ) );
489 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
492 e = entry_dup( bi->sql_baseObject );
494 return LDAP_NO_MEMORY;
502 ber_dupbv_x( &bsi->bsi_e->e_name, &eid->eid_dn, bsi->bsi_op->o_tmpmemctx );
503 ber_dupbv_x( &bsi->bsi_e->e_nname, &eid->eid_ndn, bsi->bsi_op->o_tmpmemctx );
505 bsi->bsi_e->e_attrs = NULL;
506 bsi->bsi_e->e_private = NULL;
508 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
510 bsi->bsi_c_eid = eid;
512 #ifndef BACKSQL_ARBITRARY_KEY
513 bsi->bsi_e->e_id = eid->eid_id;
514 #endif /* ! BACKSQL_ARBITRARY_KEY */
516 rc = attr_merge_normalize_one( bsi->bsi_e, ad_oc,
517 &bsi->bsi_oc->bom_oc->soc_cname,
518 bsi->bsi_op->o_tmpmemctx );
519 if ( rc != LDAP_SUCCESS ) {
520 entry_clean( bsi->bsi_e );
524 if ( bsi->bsi_attrs == NULL || ( bsi->bsi_flags & BSQL_SF_ALL_USER ) )
526 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
527 "retrieving all attributes\n", 0, 0, 0 );
528 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
529 bsi, 0, AVL_INORDER );
532 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
533 "custom attribute list\n", 0, 0, 0 );
534 for ( i = 0; bsi->bsi_attrs[ i ].an_name.bv_val; i++ ) {
535 backsql_at_map_rec **vat;
536 AttributeName *an = &bsi->bsi_attrs[ i ];
539 /* if one of the attributes listed here is
540 * a subtype of another, it must be ignored,
541 * because subtypes are already dealt with
542 * by backsql_supad2at()
544 for ( j = 0; bsi->bsi_attrs[ j ].an_name.bv_val; j++ ) {
551 if ( is_at_subtype( an->an_desc->ad_type,
552 bsi->bsi_attrs[ j ].an_desc->ad_type ) )
558 rc = backsql_supad2at( bsi->bsi_oc, an->an_desc, &vat );
559 if ( rc != 0 || vat == NULL ) {
560 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
561 "attribute \"%s\" is not defined "
562 "for objectlass \"%s\"\n",
564 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
568 for ( j = 0; vat[j]; j++ ) {
569 backsql_get_attr_vals( vat[j], bsi );
578 if ( bsi->bsi_flags & BSQL_SF_RETURN_ENTRYUUID ) {
579 Attribute *a_entryUUID,
582 a_entryUUID = backsql_operational_entryUUID( bi, eid );
583 if ( a_entryUUID != NULL ) {
584 for ( ap = &bsi->bsi_e->e_attrs;
586 ap = &(*ap)->a_next );
592 if ( global_schemacheck ) {
593 const char *text = NULL;
594 char textbuf[ 1024 ];
595 size_t textlen = sizeof( textbuf );
596 struct berval bv[ 2 ];
600 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
601 bv[ 1 ].bv_val = NULL;
603 rc = structural_class( bv, &soc, NULL,
604 &text, textbuf, textlen );
605 if ( rc != LDAP_SUCCESS ) {
606 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
607 "structural_class() failed %d (%s)\n",
608 bsi->bsi_e->e_name.bv_val,
609 rc, text ? text : "" );
610 entry_clean( bsi->bsi_e );
614 if ( ( bsi->bsi_flags & BSQL_SF_ALL_OPER )
615 || an_find( bsi->bsi_attrs, &AllOper )
616 || an_find( bsi->bsi_attrs, &slap_schema.si_ad_structuralObjectClass->ad_cname ) )
618 rc = attr_merge_normalize_one( bsi->bsi_e,
619 slap_schema.si_ad_structuralObjectClass,
620 &soc, bsi->bsi_op->o_tmpmemctx );
621 if ( rc != LDAP_SUCCESS ) {
622 entry_clean( bsi->bsi_e );
629 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );