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->dn.bv_val != NULL ) {
44 free( id->dn.bv_val );
67 char upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
71 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n",
76 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
77 Debug( LDAP_DEBUG_TRACE,
78 "backsql_dn2id(): DN \"%s\" (%ld bytes) "
79 "exceeds max DN length (%d):\n",
80 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
85 Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
86 assert( bi->id_query );
87 rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
88 if ( rc != SQL_SUCCESS ) {
89 Debug( LDAP_DEBUG_TRACE,
90 "backsql_dn2id(): error preparing SQL:\n%s",
92 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
93 SQLFreeStmt( sth, SQL_DROP );
97 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
99 * Prepare an upper cased, byte reversed version
100 * that can be searched using indexes
103 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
104 upperdn[ i ] = dn->bv_val[ j ];
107 ldap_pvt_str2upper( upperdn );
109 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
113 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
114 AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
115 ldap_pvt_str2upper( upperdn );
116 Debug( LDAP_DEBUG_TRACE,
117 "==>backsql_dn2id(): upperdn='%s'\n",
126 rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
127 if ( rc != SQL_SUCCESS) {
129 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
130 "error binding dn=\"%s\" parameter:\n",
132 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
133 SQLFreeStmt( sth, SQL_DROP );
137 rc = SQLExecute( sth );
138 if ( rc != SQL_SUCCESS ) {
139 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
140 "error executing query (\"%s\", \"%s\"):\n",
141 bi->id_query, toBind, 0 );
142 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
143 SQLFreeStmt( sth, SQL_DROP );
147 backsql_BindRowAsStrings( sth, &row );
148 rc = SQLFetch( sth );
149 if ( BACKSQL_SUCCESS( rc ) ) {
150 id->id = strtol( row.cols[ 0 ], NULL, 0 );
151 id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
152 id->oc_id = strtol( row.cols[ 2 ], NULL, 0 );
153 ber_dupbv( &id->dn, dn );
159 res = LDAP_NO_SUCH_OBJECT;
161 backsql_FreeRow( &row );
163 SQLFreeStmt( sth, SQL_DROP );
164 if ( res == LDAP_SUCCESS ) {
165 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
168 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
175 backsql_count_children(
179 unsigned long *nchildren )
184 int res = LDAP_SUCCESS;
186 Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn='%s'\n",
189 if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
190 Debug( LDAP_DEBUG_TRACE,
191 "backsql_count_children(): DN \"%s\" (%ld bytes) "
192 "exceeds max DN length (%d):\n",
193 dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
198 Debug(LDAP_DEBUG_TRACE, "children id query '%s'\n",
199 bi->has_children_query, 0, 0);
200 assert( bi->has_children_query );
201 rc = backsql_Prepare( dbh, &sth, bi->has_children_query, 0 );
202 if ( rc != SQL_SUCCESS ) {
203 Debug( LDAP_DEBUG_TRACE,
204 "backsql_count_children(): error preparing SQL:\n%s",
205 bi->has_children_query, 0, 0);
206 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
207 SQLFreeStmt( sth, SQL_DROP );
211 rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
212 if ( rc != SQL_SUCCESS) {
214 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
215 "error binding dn=\"%s\" parameter:\n",
217 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
218 SQLFreeStmt( sth, SQL_DROP );
222 rc = SQLExecute( sth );
223 if ( rc != SQL_SUCCESS ) {
224 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
225 "error executing query (\"%s\", \"%s\"):\n",
226 bi->has_children_query, dn->bv_val, 0 );
227 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
228 SQLFreeStmt( sth, SQL_DROP );
232 backsql_BindRowAsStrings( sth, &row );
234 rc = SQLFetch( sth );
235 if ( BACKSQL_SUCCESS( rc ) ) {
238 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
239 if ( end[ 0 ] != '\0' ) {
246 backsql_FreeRow( &row );
248 SQLFreeStmt( sth, SQL_DROP );
250 Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
257 backsql_has_children(
262 unsigned long nchildren;
265 rc = backsql_count_children( bi, dbh, dn, &nchildren );
267 if ( rc == LDAP_SUCCESS ) {
268 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
275 backsql_get_attr_vals( void *v_at, void *v_bsi )
277 backsql_at_map_rec *at = v_at;
278 backsql_srch_info *bsi = v_bsi;
279 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
288 Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
289 "oc='%s' attr='%s' keyval=%ld\n",
290 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val,
291 bsi->bsi_c_eid->keyval );
293 rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
294 if ( rc != SQL_SUCCESS ) {
295 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
296 "error preparing query: %s\n", at->bam_query, 0, 0 );
297 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
301 rc = backsql_BindParamID( sth, 1, &bsi->bsi_c_eid->keyval );
302 if ( rc != SQL_SUCCESS ) {
303 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
304 "error binding key value parameter\n", 0, 0, 0 );
308 rc = SQLExecute( sth );
309 if ( ! BACKSQL_SUCCESS( rc ) ) {
310 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
311 "error executing attribute query '%s'\n",
312 at->bam_query, 0, 0 );
313 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
314 SQLFreeStmt( sth, SQL_DROP );
318 backsql_BindRowAsStrings( sth, &row );
320 rc = SQLFetch( sth );
321 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
322 for ( i = 0; i < row.ncols; i++ ) {
323 if ( row.value_len[ i ] > 0 ) {
326 bv.bv_val = row.cols[ i ];
328 bv.bv_len = row.col_prec[ i ];
331 * FIXME: what if a binary
334 bv.bv_len = strlen( row.cols[ i ] );
336 backsql_entry_addattr( bsi->bsi_e,
337 &row.col_names[ i ], &bv,
338 bsi->bsi_op->o_tmpmemctx );
341 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
342 (int)row.col_prec[ i ], 0, 0 );
344 Debug( LDAP_DEBUG_TRACE, "NULL value "
345 "in this row for attribute '%s'\n",
346 row.col_names[ i ].bv_val, 0, 0 );
347 #endif /* BACKSQL_TRACE */
352 backsql_FreeRow( &row );
353 SQLFreeStmt( sth, SQL_DROP );
354 Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
356 if ( at->bam_next ) {
357 return backsql_get_attr_vals( at->bam_next, v_bsi );
364 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
368 AttributeDescription *ad_oc = slap_schema.si_ad_objectClass;
370 Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
372 rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname,
373 bsi->bsi_op->o_tmpmemctx );
374 if ( rc != LDAP_SUCCESS ) {
378 bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private, eid->oc_id );
380 bsi->bsi_c_eid = eid;
386 if ( bsi->bsi_attrs != NULL ) {
387 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
388 "custom attribute list\n", 0, 0, 0 );
389 for ( i = 0; bsi->bsi_attrs[ i ].an_name.bv_val; i++ ) {
390 backsql_at_map_rec **vat;
391 AttributeName *attr = &bsi->bsi_attrs[ i ];
394 if ( attr->an_desc == ad_oc ) {
398 rc = backsql_supad2at( bsi->bsi_oc, attr->an_desc, &vat );
399 if ( rc != 0 || vat == NULL ) {
400 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
401 "attribute '%s' is not defined "
402 "for objectlass '%s'\n",
403 attr->an_name.bv_val,
404 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
408 for ( j = 0; vat[j]; j++ ) {
409 backsql_get_attr_vals( vat[j], bsi );
416 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
417 "retrieving all attributes\n", 0, 0, 0 );
418 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
419 bsi, 0, AVL_INORDER );
422 if ( attr_merge_normalize_one( bsi->bsi_e, ad_oc,
423 &bsi->bsi_oc->bom_oc->soc_cname,
424 bsi->bsi_op->o_tmpmemctx ) ) {
429 if ( global_schemacheck ) {
430 const char *text = NULL;
431 char textbuf[ 1024 ];
432 size_t textlen = sizeof( textbuf );
433 struct berval bv[ 2 ];
437 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
438 bv[ 1 ].bv_val = NULL;
440 rc = structural_class( bv, &soc, NULL,
441 &text, textbuf, textlen );
442 if ( rc != LDAP_SUCCESS ) {
447 if ( ( bsi->bsi_flags | BSQL_SF_ALL_OPER )
448 || an_find( bsi->bsi_attrs, &AllOper ) ) {
449 rc = attr_merge_normalize_one( bsi->bsi_e,
450 slap_schema.si_ad_structuralObjectClass,
451 &soc, bsi->bsi_op->o_tmpmemctx );
452 if ( rc != LDAP_SUCCESS ) {
459 Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
464 #endif /* SLAPD_SQL */