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
90 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
95 * NOTE: id can be NULL; in this case, the function
96 * simply checks whether the DN can be successfully
97 * turned into an ID, returning LDAP_SUCCESS for
98 * positive cases, or the most appropriate error
101 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn=\"%s\"%s\n",
102 ndn->bv_val, id == NULL ? " (no ID)" : "", 0 );
104 if ( ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
105 Debug( LDAP_DEBUG_TRACE,
106 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
107 "exceeds max DN length (%d):\n",
108 ndn->bv_val, ndn->bv_len, BACKSQL_MAX_DN_LEN );
112 /* return baseObject if available and matches */
113 if ( bi->sql_baseObject != NULL && dn_match( ndn, &bi->sql_baseObject->e_nname ) ) {
115 #ifdef BACKSQL_ARBITRARY_KEY
116 ber_dupbv( &id->eid_id, &backsql_baseObject_bv );
117 ber_dupbv( &id->eid_keyval, &backsql_baseObject_bv );
118 #else /* ! BACKSQL_ARBITRARY_KEY */
119 id->eid_id = BACKSQL_BASEOBJECT_ID;
120 id->eid_keyval = BACKSQL_BASEOBJECT_KEYVAL;
121 #endif /* ! BACKSQL_ARBITRARY_KEY */
122 id->eid_oc_id = BACKSQL_BASEOBJECT_OC;
124 ber_dupbv( &id->eid_ndn, &bi->sql_baseObject->e_nname );
125 ber_dupbv( &id->eid_dn, &bi->sql_baseObject->e_name );
134 Debug( LDAP_DEBUG_TRACE, "id_query \"%s\"\n", bi->sql_id_query, 0, 0 );
135 assert( bi->sql_id_query );
136 rc = backsql_Prepare( dbh, &sth, bi->sql_id_query, 0 );
137 if ( rc != SQL_SUCCESS ) {
138 Debug( LDAP_DEBUG_TRACE,
139 "backsql_dn2id(): error preparing SQL:\n%s",
140 bi->sql_id_query, 0, 0);
141 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
142 SQLFreeStmt( sth, SQL_DROP );
146 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
148 * Prepare an upper cased, byte reversed version
149 * that can be searched using indexes
152 for ( i = 0, j = ndn->bv_len - 1; ndn->bv_val[ i ]; i++, j--) {
153 upperdn[ i ] = ndn->bv_val[ j ];
156 ldap_pvt_str2upper( upperdn );
158 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn=\"%s\"\n",
160 ber_str2bv( upperdn, 0, 0, &tbbDN );
163 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
164 AC_MEMCPY( upperdn, ndn->bv_val, ndn->bv_len + 1 );
165 ldap_pvt_str2upper( upperdn );
166 Debug( LDAP_DEBUG_TRACE,
167 "==>backsql_dn2id(): upperdn=\"%s\"\n",
169 ber_str2bv( upperdn, 0, 0, &tbbDN );
176 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &tbbDN );
177 if ( rc != SQL_SUCCESS) {
179 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
180 "error binding dn=\"%s\" parameter:\n",
181 tbbDN.bv_val, 0, 0 );
182 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
183 SQLFreeStmt( sth, SQL_DROP );
187 rc = SQLExecute( sth );
188 if ( rc != SQL_SUCCESS ) {
189 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
190 "error executing query (\"%s\", \"%s\"):\n",
191 bi->sql_id_query, tbbDN.bv_val, 0 );
192 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
193 SQLFreeStmt( sth, SQL_DROP );
197 backsql_BindRowAsStrings( sth, &row );
198 rc = SQLFetch( sth );
199 if ( BACKSQL_SUCCESS( rc ) ) {
203 snprintf( buf, sizeof(buf),
204 "id=%s keyval=%s oc_id=%s dn=%s",
205 row.cols[ 0 ], row.cols[ 1 ],
206 row.cols[ 2 ], row.cols[ 3 ] );
207 Debug( LDAP_DEBUG_TRACE,
208 "<==backsql_dn2id(): %s\n", buf, 0, 0 );
209 #endif /* LDAP_DEBUG */
215 #ifdef BACKSQL_ARBITRARY_KEY
216 ber_str2bv( row.cols[ 0 ], 0, 1, &id->eid_id );
217 ber_str2bv( row.cols[ 1 ], 0, 1, &id->eid_keyval );
218 #else /* ! BACKSQL_ARBITRARY_KEY */
219 id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
220 id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
221 #endif /* ! BACKSQL_ARBITRARY_KEY */
222 id->eid_oc_id = strtol( row.cols[ 2 ], NULL, 0 );
224 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
226 res = dnPrettyNormal( NULL, &dn, &id->eid_dn, &id->eid_ndn, NULL );
227 if ( res != LDAP_SUCCESS ) {
228 Debug( LDAP_DEBUG_TRACE,
229 "<==backsql_dn2id(\"%s\"): "
230 "dnPrettyNormal failed (%d: %s)\n",
232 ldap_err2string( res ) );
235 (void)backsql_free_entryID( id, 0 );
242 res = LDAP_NO_SUCH_OBJECT;
243 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
246 backsql_FreeRow( &row );
248 SQLFreeStmt( sth, SQL_DROP );
253 backsql_count_children(
257 unsigned long *nchildren )
262 int res = LDAP_SUCCESS;
264 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n",
267 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
268 Debug( LDAP_DEBUG_TRACE,
269 "backsql_count_children(): DN \"%s\" (%ld bytes) "
270 "exceeds max DN length (%d):\n",
271 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
276 Debug(LDAP_DEBUG_TRACE, "children id query \"%s\"\n",
277 bi->sql_has_children_query, 0, 0);
278 assert( bi->sql_has_children_query );
279 rc = backsql_Prepare( dbh, &sth, bi->sql_has_children_query, 0 );
280 if ( rc != SQL_SUCCESS ) {
281 Debug( LDAP_DEBUG_TRACE,
282 "backsql_count_children(): error preparing SQL:\n%s",
283 bi->sql_has_children_query, 0, 0);
284 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
285 SQLFreeStmt( sth, SQL_DROP );
289 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
290 if ( rc != SQL_SUCCESS) {
292 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
293 "error binding dn=\"%s\" parameter:\n",
295 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
296 SQLFreeStmt( sth, SQL_DROP );
300 rc = SQLExecute( sth );
301 if ( rc != SQL_SUCCESS ) {
302 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
303 "error executing query (\"%s\", \"%s\"):\n",
304 bi->sql_has_children_query, dn->bv_val, 0 );
305 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
306 SQLFreeStmt( sth, SQL_DROP );
310 backsql_BindRowAsStrings( sth, &row );
312 rc = SQLFetch( sth );
313 if ( BACKSQL_SUCCESS( rc ) ) {
316 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
317 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
318 /* FIXME: braindead RDBMSes return
319 * a fractional number from COUNT!
327 backsql_FreeRow( &row );
329 SQLFreeStmt( sth, SQL_DROP );
331 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
338 backsql_has_children(
343 unsigned long nchildren;
346 rc = backsql_count_children( bi, dbh, dn, &nchildren );
348 if ( rc == LDAP_SUCCESS ) {
349 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
356 backsql_get_attr_vals( void *v_at, void *v_bsi )
358 backsql_at_map_rec *at = v_at;
359 backsql_srch_info *bsi = v_bsi;
360 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
369 #ifdef BACKSQL_ARBITRARY_KEY
370 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
371 "oc=\"%s\" attr=\"%s\" keyval=%s\n",
372 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
373 bsi->bsi_c_eid->eid_keyval.bv_val );
374 #else /* ! BACKSQL_ARBITRARY_KEY */
375 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
376 "oc=\"%s\" attr=\"%s\" keyval=%ld\n",
377 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
378 bsi->bsi_c_eid->eid_keyval );
379 #endif /* ! BACKSQL_ARBITRARY_KEY */
381 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
382 if ( rc != SQL_SUCCESS ) {
383 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
384 "error preparing query: %s\n", at->bam_query, 0, 0 );
385 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
389 rc = backsql_BindParamID( sth, 1, SQL_PARAM_INPUT,
390 &bsi->bsi_c_eid->eid_keyval );
391 if ( rc != SQL_SUCCESS ) {
392 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
393 "error binding key value parameter\n", 0, 0, 0 );
398 #ifdef BACKSQL_ARBITRARY_KEY
399 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
400 "query=\"%s\" keyval=%s\n", at->bam_query,
401 bsi->bsi_c_eid->eid_keyval.bv_val, 0 );
402 #else /* !BACKSQL_ARBITRARY_KEY */
403 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
404 "query=\"%s\" keyval=%d\n", at->bam_query,
405 bsi->bsi_c_eid->eid_keyval, 0 );
406 #endif /* ! BACKSQL_ARBITRARY_KEY */
407 #endif /* BACKSQL_TRACE */
409 rc = SQLExecute( sth );
410 if ( ! BACKSQL_SUCCESS( rc ) ) {
411 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
412 "error executing attribute query \"%s\"\n",
413 at->bam_query, 0, 0 );
414 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
415 SQLFreeStmt( sth, SQL_DROP );
419 backsql_BindRowAsStrings( sth, &row );
421 rc = SQLFetch( sth );
422 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
423 for ( i = 0; i < row.ncols; i++ ) {
424 if ( row.value_len[ i ] > 0 ) {
427 bv.bv_val = row.cols[ i ];
429 bv.bv_len = row.col_prec[ i ];
432 * FIXME: what if a binary
435 bv.bv_len = strlen( row.cols[ i ] );
437 backsql_entry_addattr( bsi->bsi_e,
438 &row.col_names[ i ], &bv,
439 bsi->bsi_op->o_tmpmemctx );
442 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
443 (int)row.col_prec[ i ], 0, 0 );
445 Debug( LDAP_DEBUG_TRACE, "NULL value "
446 "in this row for attribute \"%s\"\n",
447 row.col_names[ i ].bv_val, 0, 0 );
448 #endif /* BACKSQL_TRACE */
453 backsql_FreeRow( &row );
454 SQLFreeStmt( sth, SQL_DROP );
455 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
457 if ( at->bam_next ) {
458 return backsql_get_attr_vals( at->bam_next, v_bsi );
465 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
467 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
470 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
472 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
474 assert( bsi->bsi_e );
476 memset( bsi->bsi_e, 0, sizeof( Entry ) );
478 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
481 e = entry_dup( bi->sql_baseObject );
483 return LDAP_NO_MEMORY;
491 ber_dupbv_x( &bsi->bsi_e->e_name, &eid->eid_dn, bsi->bsi_op->o_tmpmemctx );
492 ber_dupbv_x( &bsi->bsi_e->e_nname, &eid->eid_ndn, bsi->bsi_op->o_tmpmemctx );
494 bsi->bsi_e->e_attrs = NULL;
495 bsi->bsi_e->e_private = NULL;
497 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
499 bsi->bsi_c_eid = eid;
501 #ifndef BACKSQL_ARBITRARY_KEY
502 bsi->bsi_e->e_id = eid->eid_id;
503 #endif /* ! BACKSQL_ARBITRARY_KEY */
505 rc = attr_merge_normalize_one( bsi->bsi_e, ad_oc,
506 &bsi->bsi_oc->bom_oc->soc_cname,
507 bsi->bsi_op->o_tmpmemctx );
508 if ( rc != LDAP_SUCCESS ) {
509 entry_clean( bsi->bsi_e );
513 if ( bsi->bsi_attrs != NULL ) {
514 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
515 "custom attribute list\n", 0, 0, 0 );
516 for ( i = 0; bsi->bsi_attrs[ i ].an_name.bv_val; i++ ) {
517 backsql_at_map_rec **vat;
518 AttributeName *an = &bsi->bsi_attrs[ i ];
521 /* if one of the attributes listed here is
522 * a subtype of another, it must be ignored,
523 * because subtypes are already dealt with
524 * by backsql_supad2at()
526 for ( j = 0; bsi->bsi_attrs[ j ].an_name.bv_val; j++ ) {
533 if ( is_at_subtype( an->an_desc->ad_type,
534 bsi->bsi_attrs[ j ].an_desc->ad_type ) )
540 rc = backsql_supad2at( bsi->bsi_oc, an->an_desc, &vat );
541 if ( rc != 0 || vat == NULL ) {
542 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
543 "attribute \"%s\" is not defined "
544 "for objectlass \"%s\"\n",
546 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
550 for ( j = 0; vat[j]; j++ ) {
551 backsql_get_attr_vals( vat[j], bsi );
560 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
561 "retrieving all attributes\n", 0, 0, 0 );
562 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
563 bsi, 0, AVL_INORDER );
566 if ( global_schemacheck ) {
567 const char *text = NULL;
568 char textbuf[ 1024 ];
569 size_t textlen = sizeof( textbuf );
570 struct berval bv[ 2 ];
574 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
575 bv[ 1 ].bv_val = NULL;
577 rc = structural_class( bv, &soc, NULL,
578 &text, textbuf, textlen );
579 if ( rc != LDAP_SUCCESS ) {
580 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(%s): "
581 "structural_class() failed %d (%s)\n",
582 bsi->bsi_e->e_name.bv_val,
583 rc, text ? text : "" );
584 entry_clean( bsi->bsi_e );
588 if ( ( bsi->bsi_flags | BSQL_SF_ALL_OPER )
589 || an_find( bsi->bsi_attrs, &AllOper ) ) {
590 rc = attr_merge_normalize_one( bsi->bsi_e,
591 slap_schema.si_ad_structuralObjectClass,
592 &soc, bsi->bsi_op->o_tmpmemctx );
593 if ( rc != LDAP_SUCCESS ) {
594 entry_clean( bsi->bsi_e );
601 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );