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.
26 #include <sys/types.h>
27 #include "ac/string.h"
32 #include "proto-sql.h"
35 backsql_free_entryID( backsql_entryID *id, int freeit )
37 backsql_entryID *next;
43 if ( id->eid_dn.bv_val != NULL ) {
44 free( id->eid_dn.bv_val );
47 #ifdef BACKSQL_ARBITRARY_KEY
48 if ( id->eid_id.bv_val ) {
49 free( id->eid_id.bv_val );
52 if ( id->eid_keyval.bv_val ) {
53 free( id->eid_keyval.bv_val );
55 #endif /* BACKSQL_ARBITRARY_KEY */
77 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
81 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n",
86 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
87 Debug( LDAP_DEBUG_TRACE,
88 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
89 "exceeds max DN length (%d):\n",
90 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
95 Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
96 assert( bi->id_query );
97 rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
98 if ( rc != SQL_SUCCESS ) {
99 Debug( LDAP_DEBUG_TRACE,
100 "backsql_dn2id(): error preparing SQL:\n%s",
102 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
103 SQLFreeStmt( sth, SQL_DROP );
107 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
109 * Prepare an upper cased, byte reversed version
110 * that can be searched using indexes
113 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
114 upperdn[ i ] = dn->bv_val[ j ];
117 ldap_pvt_str2upper( upperdn );
119 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
123 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
124 AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
125 ldap_pvt_str2upper( upperdn );
126 Debug( LDAP_DEBUG_TRACE,
127 "==>backsql_dn2id(): upperdn='%s'\n",
136 rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
137 if ( rc != SQL_SUCCESS) {
139 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
140 "error binding dn=\"%s\" parameter:\n",
142 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
143 SQLFreeStmt( sth, SQL_DROP );
147 rc = SQLExecute( sth );
148 if ( rc != SQL_SUCCESS ) {
149 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
150 "error executing query (\"%s\", \"%s\"):\n",
151 bi->id_query, toBind, 0 );
152 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
153 SQLFreeStmt( sth, SQL_DROP );
157 backsql_BindRowAsStrings( sth, &row );
158 rc = SQLFetch( sth );
159 if ( BACKSQL_SUCCESS( rc ) ) {
160 #ifdef BACKSQL_ARBITRARY_KEY
161 ber_str2bv( row.cols[ 0 ], 0, 1, &id->eid_id );
162 ber_str2bv( row.cols[ 1 ], 0, 1, &id->eid_keyval );
163 #else /* ! BACKSQL_ARBITRARY_KEY */
164 id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
165 id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
166 #endif /* ! BACKSQL_ARBITRARY_KEY */
167 id->eid_oc_id = strtol( row.cols[ 2 ], NULL, 0 );
168 ber_dupbv( &id->eid_dn, dn );
174 res = LDAP_NO_SUCH_OBJECT;
176 backsql_FreeRow( &row );
178 SQLFreeStmt( sth, SQL_DROP );
179 if ( res == LDAP_SUCCESS ) {
180 #ifdef BACKSQL_ARBITRARY_KEY
181 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%s\n",
182 id->eid_id.bv_val, 0, 0 );
183 #else /* ! BACKSQL_ARBITRARY_KEY */
184 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
186 #endif /* !BACKSQL_ARBITRARY_KEY */
188 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
195 backsql_count_children(
199 unsigned long *nchildren )
204 int res = LDAP_SUCCESS;
206 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn='%s'\n",
209 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
210 Debug( LDAP_DEBUG_TRACE,
211 "backsql_count_children(): DN \"%s\" (%ld bytes) "
212 "exceeds max DN length (%d):\n",
213 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
218 Debug(LDAP_DEBUG_TRACE, "children id query '%s'\n",
219 bi->has_children_query, 0, 0);
220 assert( bi->has_children_query );
221 rc = backsql_Prepare( dbh, &sth, bi->has_children_query, 0 );
222 if ( rc != SQL_SUCCESS ) {
223 Debug( LDAP_DEBUG_TRACE,
224 "backsql_count_children(): error preparing SQL:\n%s",
225 bi->has_children_query, 0, 0);
226 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
227 SQLFreeStmt( sth, SQL_DROP );
231 rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
232 if ( rc != SQL_SUCCESS) {
234 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
235 "error binding dn=\"%s\" parameter:\n",
237 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
238 SQLFreeStmt( sth, SQL_DROP );
242 rc = SQLExecute( sth );
243 if ( rc != SQL_SUCCESS ) {
244 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
245 "error executing query (\"%s\", \"%s\"):\n",
246 bi->has_children_query, dn->bv_val, 0 );
247 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
248 SQLFreeStmt( sth, SQL_DROP );
252 backsql_BindRowAsStrings( sth, &row );
254 rc = SQLFetch( sth );
255 if ( BACKSQL_SUCCESS( rc ) ) {
258 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
259 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
260 /* FIXME: braindead RDBMSes return
261 * a fractional number from COUNT!
269 backsql_FreeRow( &row );
271 SQLFreeStmt( sth, SQL_DROP );
273 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
280 backsql_has_children(
285 unsigned long nchildren;
288 rc = backsql_count_children( bi, dbh, dn, &nchildren );
290 if ( rc == LDAP_SUCCESS ) {
291 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
298 backsql_get_attr_vals( void *v_at, void *v_bsi )
300 backsql_at_map_rec *at = v_at;
301 backsql_srch_info *bsi = v_bsi;
302 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
311 #ifdef BACKSQL_ARBITRARY_KEY
312 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
313 "oc='%s' attr='%s' keyval=%s\n",
314 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
315 bsi->bsi_c_eid->eid_keyval.bv_val );
316 #else /* ! BACKSQL_ARBITRARY_KEY */
317 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
318 "oc='%s' attr='%s' keyval=%ld\n",
319 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
320 bsi->bsi_c_eid->eid_keyval );
321 #endif /* ! BACKSQL_ARBITRARY_KEY */
323 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
324 if ( rc != SQL_SUCCESS ) {
325 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
326 "error preparing query: %s\n", at->bam_query, 0, 0 );
327 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
331 #ifdef BACKSQL_ARBITRARY_KEY
332 rc = backsql_BindParamStr( sth, 1, bsi->bsi_c_eid->eid_keyval.bv_val,
333 BACKSQL_MAX_KEY_LEN );
334 #else /* ! BACKSQL_ARBITRARY_KEY */
335 rc = backsql_BindParamID( sth, 1, &bsi->bsi_c_eid->eid_keyval );
336 #endif /* ! BACKSQL_ARBITRARY_KEY */
337 if ( rc != SQL_SUCCESS ) {
338 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
339 "error binding key value parameter\n", 0, 0, 0 );
343 rc = SQLExecute( sth );
344 if ( ! BACKSQL_SUCCESS( rc ) ) {
345 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
346 "error executing attribute query '%s'\n",
347 at->bam_query, 0, 0 );
348 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
349 SQLFreeStmt( sth, SQL_DROP );
353 backsql_BindRowAsStrings( sth, &row );
355 rc = SQLFetch( sth );
356 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
357 for ( i = 0; i < row.ncols; i++ ) {
358 if ( row.value_len[ i ] > 0 ) {
361 bv.bv_val = row.cols[ i ];
363 bv.bv_len = row.col_prec[ i ];
366 * FIXME: what if a binary
369 bv.bv_len = strlen( row.cols[ i ] );
371 backsql_entry_addattr( bsi->bsi_e,
372 &row.col_names[ i ], &bv,
373 bsi->bsi_op->o_tmpmemctx );
376 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
377 (int)row.col_prec[ i ], 0, 0 );
379 Debug( LDAP_DEBUG_TRACE, "NULL value "
380 "in this row for attribute '%s'\n",
381 row.col_names[ i ].bv_val, 0, 0 );
382 #endif /* BACKSQL_TRACE */
387 backsql_FreeRow( &row );
388 SQLFreeStmt( sth, SQL_DROP );
389 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
391 if ( at->bam_next ) {
392 return backsql_get_attr_vals( at->bam_next, v_bsi );
399 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
403 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
405 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
407 rc = dnPrettyNormal( NULL, &eid->eid_dn, &e->e_name, &e->e_nname,
408 bsi->bsi_op->o_tmpmemctx );
409 if ( rc != LDAP_SUCCESS ) {
413 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
416 bsi->bsi_c_eid = eid;
420 #ifndef BACKSQL_ARBITRARY_KEY
421 e->e_id = eid->eid_id;
422 #endif /* ! BACKSQL_ARBITRARY_KEY */
424 if ( bsi->bsi_attrs != NULL ) {
425 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
426 "custom attribute list\n", 0, 0, 0 );
427 for ( i = 0; bsi->bsi_attrs[ i ].an_name.bv_val; i++ ) {
428 backsql_at_map_rec **vat;
429 AttributeName *attr = &bsi->bsi_attrs[ i ];
432 if ( attr->an_desc == ad_oc ) {
436 /* if one of the attributes listed here is
437 * a subtype of another, it must be ignored,
438 * because subtypes are already dealt with
439 * by backsql_supad2at()
441 for ( j = 0; bsi->bsi_attrs[ j ].an_name.bv_val; j++ ) {
448 if ( is_at_subtype( attr->an_desc->ad_type, bsi->bsi_attrs[ j ].an_desc->ad_type ) )
454 rc = backsql_supad2at( bsi->bsi_oc, attr->an_desc, &vat );
455 if ( rc != 0 || vat == NULL ) {
456 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
457 "attribute '%s' is not defined "
458 "for objectlass '%s'\n",
459 attr->an_name.bv_val,
460 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
464 for ( j = 0; vat[j]; j++ ) {
465 backsql_get_attr_vals( vat[j], bsi );
474 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
475 "retrieving all attributes\n", 0, 0, 0 );
476 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
477 bsi, 0, AVL_INORDER );
480 if ( attr_merge_normalize_one( bsi->bsi_e, ad_oc,
481 &bsi->bsi_oc->bom_oc->soc_cname,
482 bsi->bsi_op->o_tmpmemctx ) ) {
487 if ( global_schemacheck ) {
488 const char *text = NULL;
489 char textbuf[ 1024 ];
490 size_t textlen = sizeof( textbuf );
491 struct berval bv[ 2 ];
495 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
496 bv[ 1 ].bv_val = NULL;
498 rc = structural_class( bv, &soc, NULL,
499 &text, textbuf, textlen );
500 if ( rc != LDAP_SUCCESS ) {
505 if ( ( bsi->bsi_flags | BSQL_SF_ALL_OPER )
506 || an_find( bsi->bsi_attrs, &AllOper ) ) {
507 rc = attr_merge_normalize_one( bsi->bsi_e,
508 slap_schema.si_ad_structuralObjectClass,
509 &soc, bsi->bsi_op->o_tmpmemctx );
510 if ( rc != LDAP_SUCCESS ) {
517 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
522 #endif /* SLAPD_SQL */