]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
Fix cursor initialization, scope IDs
[openldap] / servers / slapd / back-sql / entry-id.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
4  *       Redistribution and use in source and binary forms are permitted only
5  *       as authorized by the OpenLDAP Public License.  A copy of this
6  *       license is available at http://www.OpenLDAP.org/license.html or
7  *       in file LICENSE in the top-level directory of the distribution.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include "ac/string.h"
17 #include "lber_pvt.h"
18 #include "ldap_pvt.h"
19 #include "slap.h"
20 #include "back-sql.h"
21 #include "sql-wrap.h"
22 #include "schema-map.h"
23 #include "entry-id.h"
24 #include "util.h"
25
26 backsql_entryID *
27 backsql_free_entryID( backsql_entryID *id, int freeit )
28 {
29         backsql_entryID         *next;
30
31         assert( id );
32
33         next = id->next;
34
35         if ( id->dn.bv_val != NULL ) {
36                 free( id->dn.bv_val );
37         }
38
39         if ( freeit ) {
40                 free( id );
41         }
42
43         return next;
44 }
45
46 int
47 backsql_dn2id(
48         backsql_info            *bi,
49         backsql_entryID         *id,
50         SQLHDBC                 dbh,
51         struct berval           *dn )
52 {
53         SQLHSTMT                sth; 
54         BACKSQL_ROW_NTS         row;
55         RETCODE                 rc;
56         int                     res;
57
58         /* TimesTen */
59         char                    upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
60         char                    *toBind;
61         int                     i, j;
62
63         Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n", 
64                         dn->bv_val, 0, 0 );
65
66         assert( id );
67
68         if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
69                 Debug( LDAP_DEBUG_TRACE, 
70                         "backsql_dn2id(): DN \"%s\" (%ld bytes) "
71                         "exceeds max DN length (%d):\n",
72                         dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
73                 return LDAP_OTHER;
74         }
75         
76         /* begin TimesTen */
77         Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
78         assert( bi->id_query );
79         rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
80         if ( rc != SQL_SUCCESS ) {
81                 Debug( LDAP_DEBUG_TRACE, 
82                         "backsql_dn2id(): error preparing SQL:\n%s", 
83                         bi->id_query, 0, 0);
84                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
85                 SQLFreeStmt( sth, SQL_DROP );
86                 return LDAP_OTHER;
87         }
88
89         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
90                 /*
91                  * Prepare an upper cased, byte reversed version 
92                  * that can be searched using indexes
93                  */
94
95                 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
96                         upperdn[ i ] = dn->bv_val[ j ];
97                 }
98                 upperdn[ i ] = '\0';
99                 ldap_pvt_str2upper( upperdn );
100
101                 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
102                                 upperdn, 0, 0 );
103                 toBind = upperdn;
104         } else {
105                 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
106                         AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
107                         ldap_pvt_str2upper( upperdn );
108                         Debug( LDAP_DEBUG_TRACE,
109                                 "==>backsql_dn2id(): upperdn='%s'\n",
110                                 upperdn, 0, 0 );
111                         toBind = upperdn;
112
113                 } else {
114                         toBind = dn->bv_val;
115                 }
116         }
117
118         rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
119         if ( rc != SQL_SUCCESS) {
120                 /* end TimesTen */ 
121                 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
122                         "error binding dn=\"%s\" parameter:\n", 
123                         toBind, 0, 0 );
124                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
125                 SQLFreeStmt( sth, SQL_DROP );
126                 return LDAP_OTHER;
127         }
128
129         rc = SQLExecute( sth );
130         if ( rc != SQL_SUCCESS ) {
131                 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
132                         "error executing query (\"%s\", \"%s\"):\n", 
133                         bi->id_query, toBind, 0 );
134                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
135                 SQLFreeStmt( sth, SQL_DROP );
136                 return LDAP_OTHER;
137         }
138
139         backsql_BindRowAsStrings( sth, &row );
140         rc = SQLFetch( sth );
141         if ( BACKSQL_SUCCESS( rc ) ) {
142                 id->id = strtol( row.cols[ 0 ], NULL, 0 );
143                 id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
144                 id->oc_id = strtol( row.cols[ 2 ], NULL, 0 );
145                 ber_dupbv( &id->dn, dn );
146                 id->next = NULL;
147
148                 res = LDAP_SUCCESS;
149
150         } else {
151                 res = LDAP_NO_SUCH_OBJECT;
152         }
153         backsql_FreeRow( &row );
154
155         SQLFreeStmt( sth, SQL_DROP );
156         if ( res == LDAP_SUCCESS ) {
157                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
158                                 id->id, 0, 0 );
159         } else {
160                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
161                                 0, 0, 0 );
162         }
163         return res;
164 }
165
166 int
167 backsql_count_children(
168         backsql_info            *bi,
169         SQLHDBC                 dbh,
170         struct berval           *dn,
171         unsigned long           *nchildren )
172 {
173         SQLHSTMT                sth; 
174         BACKSQL_ROW_NTS         row;
175         RETCODE                 rc;
176         int                     res = LDAP_SUCCESS;
177
178         Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn='%s'\n", 
179                         dn->bv_val, 0, 0 );
180
181         if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
182                 Debug( LDAP_DEBUG_TRACE, 
183                         "backsql_count_children(): DN \"%s\" (%ld bytes) "
184                         "exceeds max DN length (%d):\n",
185                         dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
186                 return LDAP_OTHER;
187         }
188         
189         /* begin TimesTen */
190         Debug(LDAP_DEBUG_TRACE, "children id query '%s'\n", 
191                         bi->has_children_query, 0, 0);
192         assert( bi->has_children_query );
193         rc = backsql_Prepare( dbh, &sth, bi->has_children_query, 0 );
194         if ( rc != SQL_SUCCESS ) {
195                 Debug( LDAP_DEBUG_TRACE, 
196                         "backsql_count_children(): error preparing SQL:\n%s", 
197                         bi->has_children_query, 0, 0);
198                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
199                 SQLFreeStmt( sth, SQL_DROP );
200                 return LDAP_OTHER;
201         }
202
203         rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
204         if ( rc != SQL_SUCCESS) {
205                 /* end TimesTen */ 
206                 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
207                         "error binding dn=\"%s\" parameter:\n", 
208                         dn->bv_val, 0, 0 );
209                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
210                 SQLFreeStmt( sth, SQL_DROP );
211                 return LDAP_OTHER;
212         }
213
214         rc = SQLExecute( sth );
215         if ( rc != SQL_SUCCESS ) {
216                 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
217                         "error executing query (\"%s\", \"%s\"):\n", 
218                         bi->has_children_query, dn->bv_val, 0 );
219                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
220                 SQLFreeStmt( sth, SQL_DROP );
221                 return LDAP_OTHER;
222         }
223
224         backsql_BindRowAsStrings( sth, &row );
225         
226         rc = SQLFetch( sth );
227         if ( BACKSQL_SUCCESS( rc ) ) {
228                 char *end;
229
230                 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
231                 if ( end[ 0 ] != '\0' ) {
232                         res = LDAP_OTHER;
233                 }
234
235         } else {
236                 res = LDAP_OTHER;
237         }
238         backsql_FreeRow( &row );
239
240         SQLFreeStmt( sth, SQL_DROP );
241
242         Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
243                         *nchildren, 0, 0 );
244
245         return res;
246 }
247
248 int
249 backsql_has_children(
250         backsql_info            *bi,
251         SQLHDBC                 dbh,
252         struct berval           *dn )
253 {
254         unsigned long   nchildren;
255         int             rc;
256
257         rc = backsql_count_children( bi, dbh, dn, &nchildren );
258
259         if ( rc == LDAP_SUCCESS ) {
260                 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
261         }
262
263         return rc;
264 }
265
266 static int
267 backsql_get_attr_vals( void *v_at, void *v_bsi )
268 {
269         backsql_at_map_rec *at  = v_at;
270         backsql_srch_info  *bsi = v_bsi;
271         RETCODE         rc;
272         SQLHSTMT        sth;
273         BACKSQL_ROW_NTS row;
274         int             i;
275
276         assert( at );
277         assert( bsi );
278  
279         Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
280                 "oc='%s' attr='%s' keyval=%ld\n",
281                 BACKSQL_OC_NAME( bsi->oc ), at->ad->ad_cname.bv_val, 
282                 bsi->c_eid->keyval );
283
284         rc = backsql_Prepare( bsi->dbh, &sth, at->query, 0 );
285         if ( rc != SQL_SUCCESS ) {
286                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
287                         "error preparing query: %s\n", at->query, 0, 0 );
288                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
289                 return 1;
290         }
291
292         rc = backsql_BindParamID( sth, 1, &bsi->c_eid->keyval );
293         if ( rc != SQL_SUCCESS ) {
294                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
295                         "error binding key value parameter\n", 0, 0, 0 );
296                 return 1;
297         }
298
299         rc = SQLExecute( sth );
300         if ( ! BACKSQL_SUCCESS( rc ) ) {
301                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
302                         "error executing attribute query '%s'\n",
303                         at->query, 0, 0 );
304                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
305                 SQLFreeStmt( sth, SQL_DROP );
306                 return 1;
307         }
308
309         backsql_BindRowAsStrings( sth, &row );
310
311         rc = SQLFetch( sth );
312         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
313                 for ( i = 0; i < row.ncols; i++ ) {
314                         if ( row.value_len[ i ] > 0 ) {
315                                 struct berval   bv;
316
317                                 bv.bv_val = row.cols[ i ];
318 #if 0
319                                 bv.bv_len = row.col_prec[ i ];
320 #else
321                                 /*
322                                  * FIXME: what if a binary 
323                                  * is fetched?
324                                  */
325                                 bv.bv_len = strlen( row.cols[ i ] );
326 #endif
327                                 backsql_entry_addattr( bsi->e, 
328                                                 &row.col_names[ i ], &bv );
329
330 #ifdef BACKSQL_TRACE
331                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
332                                         (int)row.col_prec[ i ], 0, 0 );
333                         } else {
334                                 Debug( LDAP_DEBUG_TRACE, "NULL value "
335                                         "in this row for attribute '%s'\n",
336                                         row.col_names[ i ].bv_val, 0, 0 );
337 #endif /* BACKSQL_TRACE */
338                         }
339                 }
340         }
341
342         backsql_FreeRow( &row );
343         SQLFreeStmt( sth, SQL_DROP );
344         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
345
346         return 1;
347 }
348
349 Entry *
350 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
351 {
352         int                     i;
353         backsql_at_map_rec      *at;
354         int                     rc;
355         AttributeDescription    *ad_oc = slap_schema.si_ad_objectClass;
356
357         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
358
359         rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
360         if ( rc != LDAP_SUCCESS ) {
361                 return NULL;
362         }
363
364         bsi->oc = backsql_id2oc( bsi->bi, eid->oc_id );
365         bsi->e = e;
366         bsi->c_eid = eid;
367         e->e_attrs = NULL;
368         e->e_private = NULL;
369  
370         /* if ( bsi->base_dn != NULL)??? */
371         
372         e->e_id = eid->id;
373  
374         if ( bsi->attrs != NULL ) {
375                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
376                         "custom attribute list\n", 0, 0, 0 );
377                 for ( i = 0; bsi->attrs[ i ].an_name.bv_val; i++ ) {
378                         AttributeName *attr = &bsi->attrs[ i ];
379
380                         if ( attr->an_desc == ad_oc
381 #if 0   /* FIXME: what is 0.10 ? */
382                                         || !BACKSQL_NCMP( &attr->an_name, &bv_n_0_10 ) 
383 #endif
384                                         ) {
385 #if 0
386                                 backsql_entry_addattr( bsi->e, 
387                                                 &bv_n_objectclass,
388                                                 BACKSQL_OC_NAME( bsi->oc ) );
389 #endif
390                                 continue;
391                         }
392
393                         at = backsql_ad2at( bsi->oc, attr->an_desc );
394                         if ( at != NULL ) {
395                                 backsql_get_attr_vals( at, bsi );
396                         } else {
397                                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
398                                         "attribute '%s' is not defined "
399                                         "for objectlass '%s'\n",
400                                         attr->an_name.bv_val, 
401                                         BACKSQL_OC_NAME( bsi->oc ), 0 );
402                         }
403                 }
404
405         } else {
406                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
407                         "retrieving all attributes\n", 0, 0, 0 );
408                 avl_apply( bsi->oc->attrs, backsql_get_attr_vals,
409                                 bsi, 0, AVL_INORDER );
410         }
411
412         if ( attr_mergeit_one( bsi->e, ad_oc, &bsi->oc->oc->soc_cname ) ) {
413                 entry_free( e );
414                 return NULL;
415         }
416
417         if ( global_schemacheck ) {
418                 const char      *text = NULL;
419                 char            textbuf[ 1024 ];
420                 size_t          textlen = sizeof( textbuf );
421                 struct berval   bv[ 2 ];
422                 struct berval   soc;
423                 AttributeDescription    *ad_soc
424                         = slap_schema.si_ad_structuralObjectClass;
425                 int rc;
426
427                 bv[ 0 ] = bsi->oc->oc->soc_cname;
428                 bv[ 1 ].bv_val = NULL;
429
430                 rc = structural_class( bv, &soc, NULL, 
431                                 &text, textbuf, textlen );
432                 if ( rc != LDAP_SUCCESS ) {
433                         entry_free( e );
434                         return NULL;
435                 }
436
437                 if ( bsi->bsi_flags | BSQL_SF_ALL_OPER 
438                                 || an_find( bsi->attrs, &AllOper ) ) {
439                         if ( attr_mergeit_one( bsi->e, ad_soc, &soc ) ) {
440                                 entry_free( e );
441                                 return NULL;
442                         }
443                 }
444         }
445
446         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
447
448         return e;
449 }
450
451 #endif /* SLAPD_SQL */
452