]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
Fix ITS#3424
[openldap] / servers / slapd / back-sql / entry-id.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include "ac/string.h"
26
27 #include "slap.h"
28 #include "proto-sql.h"
29
30 #ifdef BACKSQL_ARBITRARY_KEY
31 struct berval backsql_baseObject_bv = BER_BVC( BACKSQL_BASEOBJECT_IDSTR );
32 #endif /* BACKSQL_ARBITRARY_KEY */
33
34 backsql_entryID *
35 backsql_free_entryID( backsql_entryID *id, int freeit )
36 {
37         backsql_entryID         *next;
38
39         assert( id );
40
41         next = id->eid_next;
42
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 )
46                 {
47                         free( id->eid_dn.bv_val );
48                         BER_BVZERO( &id->eid_dn );
49                 }
50
51                 free( id->eid_ndn.bv_val );
52                 BER_BVZERO( &id->eid_ndn );
53         }
54
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 );
59         }
60
61         if ( id->eid_keyval.bv_val ) {
62                 free( id->eid_keyval.bv_val );
63                 BER_BVZERO( &id->eid_keyval );
64         }
65 #endif /* BACKSQL_ARBITRARY_KEY */
66
67         if ( freeit ) {
68                 free( id );
69         }
70
71         return next;
72 }
73
74 /*
75  * NOTE: the dn must be normalized
76  */
77 int
78 backsql_dn2id(
79         Operation               *op,
80         SlapReply               *rs,
81         backsql_entryID         *id,
82         SQLHDBC                 dbh,
83         struct berval           *ndn,
84         int                     muck )
85 {
86         backsql_info            *bi = op->o_bd->be_private;
87         SQLHSTMT                sth = SQL_NULL_HSTMT; 
88         BACKSQL_ROW_NTS         row;
89         RETCODE                 rc;
90         int                     res;
91         struct berval           realndn = BER_BVNULL;
92
93         /* TimesTen */
94         char                    upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
95         struct berval           tbbDN;
96         int                     i, j;
97
98         /*
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
103          */
104
105         Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn=\"%s\"%s\n", 
106                         ndn->bv_val, id == NULL ? " (no ID)" : "", 0 );
107
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 );
113                 return LDAP_OTHER;
114         }
115
116         /* return baseObject if available and matches */
117         if ( bi->sql_baseObject != NULL && dn_match( ndn, &bi->sql_baseObject->e_nname ) ) {
118                 if ( id != NULL ) {
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;
127
128                         ber_dupbv( &id->eid_ndn, &bi->sql_baseObject->e_nname );
129                         ber_dupbv( &id->eid_dn, &bi->sql_baseObject->e_name );
130
131                         id->eid_next = NULL;
132                 }
133
134                 return LDAP_SUCCESS;
135         }
136         
137         /* begin TimesTen */
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 );
146                 res = LDAP_OTHER;
147                 goto done;
148         }
149
150         realndn = *ndn;
151         if ( muck ) {
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 );
156                         res = LDAP_OTHER;
157                         goto done;
158                 }
159         }
160
161         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
162                 /*
163                  * Prepare an upper cased, byte reversed version 
164                  * that can be searched using indexes
165                  */
166
167                 for ( i = 0, j = realndn.bv_len - 1; realndn.bv_val[ i ]; i++, j--) {
168                         upperdn[ i ] = realndn.bv_val[ j ];
169                 }
170                 upperdn[ i ] = '\0';
171                 ldap_pvt_str2upper( upperdn );
172
173                 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn=\"%s\"\n",
174                                 upperdn, 0, 0 );
175                 ber_str2bv( upperdn, 0, 0, &tbbDN );
176
177         } else {
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",
183                                 upperdn, 0, 0 );
184                         ber_str2bv( upperdn, 0, 0, &tbbDN );
185
186                 } else {
187                         tbbDN = realndn;
188                 }
189         }
190
191         rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &tbbDN );
192         if ( rc != SQL_SUCCESS) {
193                 /* end TimesTen */ 
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 );
198                 res = LDAP_OTHER;
199                 goto done;
200         }
201
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 );
208                 res = LDAP_OTHER;
209                 goto done;
210         }
211
212         backsql_BindRowAsStrings( sth, &row );
213         rc = SQLFetch( sth );
214         if ( BACKSQL_SUCCESS( rc ) ) {
215                 char    buf[BUFSIZ];
216
217 #ifdef LDAP_DEBUG
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 */
225
226                 res = LDAP_SUCCESS;
227                 if ( id != NULL ) {
228                         struct berval   dn;
229
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 );
238
239                         ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
240
241                         if ( backsql_api_odbc2dn( op, rs, &dn ) ) {
242                                 res = LDAP_OTHER;
243
244                         } else {
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",
250                                                 realndn.bv_val, res,
251                                                 ldap_err2string( res ) );
252
253                                         /* cleanup... */
254                                         (void)backsql_free_entryID( id, 0 );
255                                 }
256
257                                 if ( dn.bv_val != row.cols[ 3 ] ) {
258                                         free( dn.bv_val );
259                                 }
260                         }
261
262                         id->eid_next = NULL;
263                 }
264
265         } else {
266                 res = LDAP_NO_SUCH_OBJECT;
267                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
268                                 0, 0, 0 );
269         }
270         backsql_FreeRow( &row );
271
272 done:;
273         if ( sth != SQL_NULL_HSTMT ) {
274                 SQLFreeStmt( sth, SQL_DROP );
275         }
276
277         if ( !BER_BVISNULL( &realndn ) && realndn.bv_val != ndn->bv_val ) {
278                 ch_free( realndn.bv_val );
279         }
280
281         return res;
282 }
283
284 int
285 backsql_count_children(
286         backsql_info            *bi,
287         SQLHDBC                 dbh,
288         struct berval           *dn,
289         unsigned long           *nchildren )
290 {
291         SQLHSTMT                sth; 
292         BACKSQL_ROW_NTS         row;
293         RETCODE                 rc;
294         int                     res = LDAP_SUCCESS;
295
296         Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn=\"%s\"\n", 
297                         dn->bv_val, 0, 0 );
298
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 );
304                 return LDAP_OTHER;
305         }
306         
307         /* begin TimesTen */
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 );
318                 return LDAP_OTHER;
319         }
320
321         rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, dn );
322         if ( rc != SQL_SUCCESS) {
323                 /* end TimesTen */ 
324                 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
325                         "error binding dn=\"%s\" parameter:\n", 
326                         dn->bv_val, 0, 0 );
327                 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
328                 SQLFreeStmt( sth, SQL_DROP );
329                 return LDAP_OTHER;
330         }
331
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 );
339                 return LDAP_OTHER;
340         }
341
342         backsql_BindRowAsStrings( sth, &row );
343         
344         rc = SQLFetch( sth );
345         if ( BACKSQL_SUCCESS( rc ) ) {
346                 char *end;
347
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!
352                          */
353                         res = LDAP_OTHER;
354                 }
355
356         } else {
357                 res = LDAP_OTHER;
358         }
359         backsql_FreeRow( &row );
360
361         SQLFreeStmt( sth, SQL_DROP );
362
363         Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
364                         *nchildren, 0, 0 );
365
366         return res;
367 }
368
369 int
370 backsql_has_children(
371         backsql_info            *bi,
372         SQLHDBC                 dbh,
373         struct berval           *dn )
374 {
375         unsigned long   nchildren;
376         int             rc;
377
378         rc = backsql_count_children( bi, dbh, dn, &nchildren );
379
380         if ( rc == LDAP_SUCCESS ) {
381                 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
382         }
383
384         return rc;
385 }
386
387 static int
388 backsql_get_attr_vals( void *v_at, void *v_bsi )
389 {
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;
393         RETCODE                 rc;
394         SQLHSTMT                sth;
395         BACKSQL_ROW_NTS         row;
396         int                     i;
397
398         assert( at );
399         assert( bsi );
400
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 */
412
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 );
418                 return 1;
419         }
420
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 );
426                 return 1;
427         }
428
429 #ifdef BACKSQL_TRACE
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 */
440
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 );
448                 return 1;
449         }
450
451         backsql_BindRowAsStrings( sth, &row );
452
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 ) {
457                                 struct berval   bv;
458
459                                 bv.bv_val = row.cols[ i ];
460 #if 0
461                                 bv.bv_len = row.col_prec[ i ];
462 #else
463                                 /*
464                                  * FIXME: what if a binary 
465                                  * is fetched?
466                                  */
467                                 bv.bv_len = strlen( row.cols[ i ] );
468 #endif
469                                 backsql_entry_addattr( bsi->bsi_e, 
470                                                 &row.col_names[ i ], &bv,
471                                                 bsi->bsi_op->o_tmpmemctx );
472
473 #ifdef BACKSQL_TRACE
474                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
475                                         (int)row.col_prec[ i ], 0, 0 );
476                         } else {
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 */
481                         }
482                 }
483         }
484
485         backsql_FreeRow( &row );
486         SQLFreeStmt( sth, SQL_DROP );
487         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
488
489         if ( at->bam_next ) {
490                 return backsql_get_attr_vals( at->bam_next, v_bsi );
491         }
492
493         return 1;
494 }
495
496 int
497 backsql_id2entry( backsql_srch_info *bsi, backsql_entryID *eid )
498 {
499         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
500         int                     i;
501         int                     rc;
502         AttributeDescription    *ad_oc = slap_schema.si_ad_objectClass;
503
504         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
505
506         assert( bsi->bsi_e );
507
508         memset( bsi->bsi_e, 0, sizeof( Entry ) );
509
510         if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
511                 Entry   *e;
512
513                 e = entry_dup( bi->sql_baseObject );
514                 if ( e == NULL ) {
515                         return LDAP_NO_MEMORY;
516                 }
517                         
518                 *bsi->bsi_e = *e;
519                 free( e );
520                 goto done;
521         }
522
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 );
525
526         bsi->bsi_e->e_attrs = NULL;
527         bsi->bsi_e->e_private = NULL;
528
529         bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
530                         eid->eid_oc_id );
531         bsi->bsi_c_eid = eid;
532
533 #ifndef BACKSQL_ARBITRARY_KEY   
534         bsi->bsi_e->e_id = eid->eid_id;
535 #endif /* ! BACKSQL_ARBITRARY_KEY */
536  
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 );
542                 return rc;
543         }
544
545         if ( bsi->bsi_attrs == NULL || ( bsi->bsi_flags & BSQL_SF_ALL_USER ) )
546         {
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 );
551
552         } else {
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 ];
558                         int                     j;
559
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()
564                          */
565                         for ( j = 0; bsi->bsi_attrs[ j ].an_name.bv_val; j++ ) {
566                                 /* skip self */
567                                 if ( j == i ) {
568                                         continue;
569                                 }
570
571                                 /* skip subtypes */
572                                 if ( is_at_subtype( an->an_desc->ad_type,
573                                                         bsi->bsi_attrs[ j ].an_desc->ad_type ) )
574                                 {
575                                         goto next;
576                                 }
577                         }
578
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",
584                                                 an->an_name.bv_val, 
585                                                 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
586                                 continue;
587                         }
588
589                         for ( j = 0; vat[j]; j++ ) {
590                                 backsql_get_attr_vals( vat[j], bsi );
591                         }
592
593                         ch_free( vat );
594
595 next:;
596                 }
597         }
598
599         if ( bsi->bsi_flags & BSQL_SF_RETURN_ENTRYUUID ) {
600                 Attribute       *a_entryUUID,
601                                 **ap;
602
603                 a_entryUUID = backsql_operational_entryUUID( bi, eid );
604                 if ( a_entryUUID != NULL ) {
605                         for ( ap = &bsi->bsi_e->e_attrs; 
606                                         *ap; 
607                                         ap = &(*ap)->a_next );
608
609                         *ap = a_entryUUID;
610                 }
611         }
612
613         if ( global_schemacheck ) {
614                 const char      *text = NULL;
615                 char            textbuf[ 1024 ];
616                 size_t          textlen = sizeof( textbuf );
617                 struct berval   bv[ 2 ];
618                 struct berval   soc;
619                 int rc;
620
621                 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
622                 bv[ 1 ].bv_val = NULL;
623
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 );
632                         return rc;
633                 }
634
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 ) )
638                 {
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 );
644                                 return rc;
645                         }
646                 }
647         }
648
649 done:;
650         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
651
652         return LDAP_SUCCESS;
653 }
654