]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
931b4f571cd1e1909a9fc8922feaa4ecb64cabf2
[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 #ifdef SLAPD_SQL
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "lber_pvt.h"
30 #include "ldap_pvt.h"
31 #include "slap.h"
32 #include "proto-sql.h"
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 ( id->eid_dn.bv_val != NULL ) {
44                 free( id->eid_dn.bv_val );
45         }
46
47 #ifdef BACKSQL_ARBITRARY_KEY
48         if ( id->eid_id.bv_val ) {
49                 free( id->eid_id.bv_val );
50         }
51
52         if ( id->eid_keyval.bv_val ) {
53                 free( id->eid_keyval.bv_val );
54         }
55 #endif /* BACKSQL_ARBITRARY_KEY */
56
57         if ( freeit ) {
58                 free( id );
59         }
60
61         return next;
62 }
63
64 int
65 backsql_dn2id(
66         backsql_info            *bi,
67         backsql_entryID         *id,
68         SQLHDBC                 dbh,
69         struct berval           *dn )
70 {
71         SQLHSTMT                sth; 
72         BACKSQL_ROW_NTS         row;
73         RETCODE                 rc;
74         int                     res;
75
76         /* TimesTen */
77         char                    upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
78         char                    *toBind;
79         int                     i, j;
80
81         Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n", 
82                         dn->bv_val, 0, 0 );
83
84         assert( id );
85
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 );
91                 return LDAP_OTHER;
92         }
93         
94         /* begin TimesTen */
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", 
101                         bi->id_query, 0, 0);
102                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
103                 SQLFreeStmt( sth, SQL_DROP );
104                 return LDAP_OTHER;
105         }
106
107         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
108                 /*
109                  * Prepare an upper cased, byte reversed version 
110                  * that can be searched using indexes
111                  */
112
113                 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
114                         upperdn[ i ] = dn->bv_val[ j ];
115                 }
116                 upperdn[ i ] = '\0';
117                 ldap_pvt_str2upper( upperdn );
118
119                 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
120                                 upperdn, 0, 0 );
121                 toBind = upperdn;
122         } else {
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",
128                                 upperdn, 0, 0 );
129                         toBind = upperdn;
130
131                 } else {
132                         toBind = dn->bv_val;
133                 }
134         }
135
136         rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
137         if ( rc != SQL_SUCCESS) {
138                 /* end TimesTen */ 
139                 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
140                         "error binding dn=\"%s\" parameter:\n", 
141                         toBind, 0, 0 );
142                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
143                 SQLFreeStmt( sth, SQL_DROP );
144                 return LDAP_OTHER;
145         }
146
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 );
154                 return LDAP_OTHER;
155         }
156
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 );
169                 id->eid_next = NULL;
170
171                 res = LDAP_SUCCESS;
172
173         } else {
174                 res = LDAP_NO_SUCH_OBJECT;
175         }
176         backsql_FreeRow( &row );
177
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",
185                                 id->eid_id, 0, 0 );
186 #endif /* !BACKSQL_ARBITRARY_KEY */
187         } else {
188                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
189                                 0, 0, 0 );
190         }
191         return res;
192 }
193
194 int
195 backsql_count_children(
196         backsql_info            *bi,
197         SQLHDBC                 dbh,
198         struct berval           *dn,
199         unsigned long           *nchildren )
200 {
201         SQLHSTMT                sth; 
202         BACKSQL_ROW_NTS         row;
203         RETCODE                 rc;
204         int                     res = LDAP_SUCCESS;
205
206         Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn='%s'\n", 
207                         dn->bv_val, 0, 0 );
208
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 );
214                 return LDAP_OTHER;
215         }
216         
217         /* begin TimesTen */
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 );
228                 return LDAP_OTHER;
229         }
230
231         rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
232         if ( rc != SQL_SUCCESS) {
233                 /* end TimesTen */ 
234                 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
235                         "error binding dn=\"%s\" parameter:\n", 
236                         dn->bv_val, 0, 0 );
237                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
238                 SQLFreeStmt( sth, SQL_DROP );
239                 return LDAP_OTHER;
240         }
241
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 );
249                 return LDAP_OTHER;
250         }
251
252         backsql_BindRowAsStrings( sth, &row );
253         
254         rc = SQLFetch( sth );
255         if ( BACKSQL_SUCCESS( rc ) ) {
256                 char *end;
257
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!
262                          */
263                         res = LDAP_OTHER;
264                 }
265
266         } else {
267                 res = LDAP_OTHER;
268         }
269         backsql_FreeRow( &row );
270
271         SQLFreeStmt( sth, SQL_DROP );
272
273         Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
274                         *nchildren, 0, 0 );
275
276         return res;
277 }
278
279 int
280 backsql_has_children(
281         backsql_info            *bi,
282         SQLHDBC                 dbh,
283         struct berval           *dn )
284 {
285         unsigned long   nchildren;
286         int             rc;
287
288         rc = backsql_count_children( bi, dbh, dn, &nchildren );
289
290         if ( rc == LDAP_SUCCESS ) {
291                 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
292         }
293
294         return rc;
295 }
296
297 static int
298 backsql_get_attr_vals( void *v_at, void *v_bsi )
299 {
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;
303         RETCODE                 rc;
304         SQLHSTMT                sth;
305         BACKSQL_ROW_NTS         row;
306         int                     i;
307
308         assert( at );
309         assert( bsi );
310
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 */
322
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 );
328                 return 1;
329         }
330
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 );
340                 return 1;
341         }
342
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 );
350                 return 1;
351         }
352
353         backsql_BindRowAsStrings( sth, &row );
354
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 ) {
359                                 struct berval   bv;
360
361                                 bv.bv_val = row.cols[ i ];
362 #if 0
363                                 bv.bv_len = row.col_prec[ i ];
364 #else
365                                 /*
366                                  * FIXME: what if a binary 
367                                  * is fetched?
368                                  */
369                                 bv.bv_len = strlen( row.cols[ i ] );
370 #endif
371                                 backsql_entry_addattr( bsi->bsi_e, 
372                                                 &row.col_names[ i ], &bv,
373                                                 bsi->bsi_op->o_tmpmemctx );
374
375 #ifdef BACKSQL_TRACE
376                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
377                                         (int)row.col_prec[ i ], 0, 0 );
378                         } else {
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 */
383                         }
384                 }
385         }
386
387         backsql_FreeRow( &row );
388         SQLFreeStmt( sth, SQL_DROP );
389         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
390
391         if ( at->bam_next ) {
392                 return backsql_get_attr_vals( at->bam_next, v_bsi );
393         }
394
395         return 1;
396 }
397
398 Entry *
399 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
400 {
401         int                     i;
402         int                     rc;
403         AttributeDescription    *ad_oc = slap_schema.si_ad_objectClass;
404
405         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
406
407         rc = dnPrettyNormal( NULL, &eid->eid_dn, &e->e_name, &e->e_nname,
408                         bsi->bsi_op->o_tmpmemctx );
409         if ( rc != LDAP_SUCCESS ) {
410                 return NULL;
411         }
412
413         bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private,
414                         eid->eid_oc_id );
415         bsi->bsi_e = e;
416         bsi->bsi_c_eid = eid;
417         e->e_attrs = NULL;
418         e->e_private = NULL;
419
420 #ifndef BACKSQL_ARBITRARY_KEY   
421         e->e_id = eid->eid_id;
422 #endif /* ! BACKSQL_ARBITRARY_KEY */
423  
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 ];
430                         int                     j;
431
432                         if ( attr->an_desc == ad_oc ) {
433                                 continue;
434                         }
435
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()
440                          */
441                         for ( j = 0; bsi->bsi_attrs[ j ].an_name.bv_val; j++ ) {
442                                 /* skip self */
443                                 if ( j == i ) {
444                                         continue;
445                                 }
446
447                                 /* skip subtypes */
448                                 if ( is_at_subtype( attr->an_desc->ad_type, bsi->bsi_attrs[ j ].an_desc->ad_type ) )
449                                 {
450                                         goto next;
451                                 }
452                         }
453
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 );
461                                 continue;
462                         }
463
464                         for ( j = 0; vat[j]; j++ ) {
465                                 backsql_get_attr_vals( vat[j], bsi );
466                         }
467
468                         ch_free( vat );
469
470 next:;
471                 }
472
473         } else {
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 );
478         }
479
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 ) ) {
483                 entry_free( e );
484                 return NULL;
485         }
486
487         if ( global_schemacheck ) {
488                 const char      *text = NULL;
489                 char            textbuf[ 1024 ];
490                 size_t          textlen = sizeof( textbuf );
491                 struct berval   bv[ 2 ];
492                 struct berval   soc;
493                 int rc;
494
495                 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
496                 bv[ 1 ].bv_val = NULL;
497
498                 rc = structural_class( bv, &soc, NULL, 
499                                 &text, textbuf, textlen );
500                 if ( rc != LDAP_SUCCESS ) {
501                         entry_free( e );
502                         return NULL;
503                 }
504
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 ) {
511                                 entry_free( e );
512                                 return NULL;
513                         }
514                 }
515         }
516
517         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
518
519         return e;
520 }
521
522 #endif /* SLAPD_SQL */
523