]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
cleanup error messages
[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->next;
42
43         if ( id->dn.bv_val != NULL ) {
44                 free( id->dn.bv_val );
45         }
46
47         if ( freeit ) {
48                 free( id );
49         }
50
51         return next;
52 }
53
54 int
55 backsql_dn2id(
56         backsql_info            *bi,
57         backsql_entryID         *id,
58         SQLHDBC                 dbh,
59         struct berval           *dn )
60 {
61         SQLHSTMT                sth; 
62         BACKSQL_ROW_NTS         row;
63         RETCODE                 rc;
64         int                     res;
65
66         /* TimesTen */
67         char                    upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
68         char                    *toBind;
69         int                     i, j;
70
71         Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n", 
72                         dn->bv_val, 0, 0 );
73
74         assert( id );
75
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 );
81                 return LDAP_OTHER;
82         }
83         
84         /* begin TimesTen */
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", 
91                         bi->id_query, 0, 0);
92                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
93                 SQLFreeStmt( sth, SQL_DROP );
94                 return LDAP_OTHER;
95         }
96
97         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
98                 /*
99                  * Prepare an upper cased, byte reversed version 
100                  * that can be searched using indexes
101                  */
102
103                 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
104                         upperdn[ i ] = dn->bv_val[ j ];
105                 }
106                 upperdn[ i ] = '\0';
107                 ldap_pvt_str2upper( upperdn );
108
109                 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
110                                 upperdn, 0, 0 );
111                 toBind = upperdn;
112         } else {
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",
118                                 upperdn, 0, 0 );
119                         toBind = upperdn;
120
121                 } else {
122                         toBind = dn->bv_val;
123                 }
124         }
125
126         rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
127         if ( rc != SQL_SUCCESS) {
128                 /* end TimesTen */ 
129                 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
130                         "error binding dn=\"%s\" parameter:\n", 
131                         toBind, 0, 0 );
132                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
133                 SQLFreeStmt( sth, SQL_DROP );
134                 return LDAP_OTHER;
135         }
136
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 );
144                 return LDAP_OTHER;
145         }
146
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 );
154                 id->next = NULL;
155
156                 res = LDAP_SUCCESS;
157
158         } else {
159                 res = LDAP_NO_SUCH_OBJECT;
160         }
161         backsql_FreeRow( &row );
162
163         SQLFreeStmt( sth, SQL_DROP );
164         if ( res == LDAP_SUCCESS ) {
165                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
166                                 id->id, 0, 0 );
167         } else {
168                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
169                                 0, 0, 0 );
170         }
171         return res;
172 }
173
174 int
175 backsql_count_children(
176         backsql_info            *bi,
177         SQLHDBC                 dbh,
178         struct berval           *dn,
179         unsigned long           *nchildren )
180 {
181         SQLHSTMT                sth; 
182         BACKSQL_ROW_NTS         row;
183         RETCODE                 rc;
184         int                     res = LDAP_SUCCESS;
185
186         Debug( LDAP_DEBUG_TRACE, "==>backsql_count_children(): dn='%s'\n", 
187                         dn->bv_val, 0, 0 );
188
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 );
194                 return LDAP_OTHER;
195         }
196         
197         /* begin TimesTen */
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 );
208                 return LDAP_OTHER;
209         }
210
211         rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
212         if ( rc != SQL_SUCCESS) {
213                 /* end TimesTen */ 
214                 Debug( LDAP_DEBUG_TRACE, "backsql_count_children(): "
215                         "error binding dn=\"%s\" parameter:\n", 
216                         dn->bv_val, 0, 0 );
217                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
218                 SQLFreeStmt( sth, SQL_DROP );
219                 return LDAP_OTHER;
220         }
221
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 );
229                 return LDAP_OTHER;
230         }
231
232         backsql_BindRowAsStrings( sth, &row );
233         
234         rc = SQLFetch( sth );
235         if ( BACKSQL_SUCCESS( rc ) ) {
236                 char *end;
237
238                 *nchildren = strtol( row.cols[ 0 ], &end, 0 );
239                 if ( end[ 0 ] != '\0' && end[0] != '.' ) {
240                         /* FIXME: braindead RDBMSes return
241                          * a fractional number from COUNT!
242                          */
243                         res = LDAP_OTHER;
244                 }
245
246         } else {
247                 res = LDAP_OTHER;
248         }
249         backsql_FreeRow( &row );
250
251         SQLFreeStmt( sth, SQL_DROP );
252
253         Debug( LDAP_DEBUG_TRACE, "<==backsql_count_children(): %lu\n",
254                         *nchildren, 0, 0 );
255
256         return res;
257 }
258
259 int
260 backsql_has_children(
261         backsql_info            *bi,
262         SQLHDBC                 dbh,
263         struct berval           *dn )
264 {
265         unsigned long   nchildren;
266         int             rc;
267
268         rc = backsql_count_children( bi, dbh, dn, &nchildren );
269
270         if ( rc == LDAP_SUCCESS ) {
271                 return nchildren > 0 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
272         }
273
274         return rc;
275 }
276
277 static int
278 backsql_get_attr_vals( void *v_at, void *v_bsi )
279 {
280         backsql_at_map_rec      *at = v_at;
281         backsql_srch_info       *bsi = v_bsi;
282         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
283         RETCODE                 rc;
284         SQLHSTMT                sth;
285         BACKSQL_ROW_NTS         row;
286         int                     i;
287
288         assert( at );
289         assert( bsi );
290  
291         Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
292                 "oc='%s' attr='%s' keyval=%ld\n",
293                 BACKSQL_OC_NAME( bsi->bsi_oc ), at->bam_ad->ad_cname.bv_val, 
294                 bsi->bsi_c_eid->keyval );
295
296         rc = backsql_Prepare( bsi->bsi_dbh, &sth, at->bam_query, 0 );
297         if ( rc != SQL_SUCCESS ) {
298                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
299                         "error preparing query: %s\n", at->bam_query, 0, 0 );
300                 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
301                 return 1;
302         }
303
304         rc = backsql_BindParamID( sth, 1, &bsi->bsi_c_eid->keyval );
305         if ( rc != SQL_SUCCESS ) {
306                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
307                         "error binding key value parameter\n", 0, 0, 0 );
308                 return 1;
309         }
310
311         rc = SQLExecute( sth );
312         if ( ! BACKSQL_SUCCESS( rc ) ) {
313                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
314                         "error executing attribute query '%s'\n",
315                         at->bam_query, 0, 0 );
316                 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
317                 SQLFreeStmt( sth, SQL_DROP );
318                 return 1;
319         }
320
321         backsql_BindRowAsStrings( sth, &row );
322
323         rc = SQLFetch( sth );
324         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
325                 for ( i = 0; i < row.ncols; i++ ) {
326                         if ( row.value_len[ i ] > 0 ) {
327                                 struct berval   bv;
328
329                                 bv.bv_val = row.cols[ i ];
330 #if 0
331                                 bv.bv_len = row.col_prec[ i ];
332 #else
333                                 /*
334                                  * FIXME: what if a binary 
335                                  * is fetched?
336                                  */
337                                 bv.bv_len = strlen( row.cols[ i ] );
338 #endif
339                                 backsql_entry_addattr( bsi->bsi_e, 
340                                                 &row.col_names[ i ], &bv,
341                                                 bsi->bsi_op->o_tmpmemctx );
342
343 #ifdef BACKSQL_TRACE
344                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
345                                         (int)row.col_prec[ i ], 0, 0 );
346                         } else {
347                                 Debug( LDAP_DEBUG_TRACE, "NULL value "
348                                         "in this row for attribute '%s'\n",
349                                         row.col_names[ i ].bv_val, 0, 0 );
350 #endif /* BACKSQL_TRACE */
351                         }
352                 }
353         }
354
355         backsql_FreeRow( &row );
356         SQLFreeStmt( sth, SQL_DROP );
357         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
358
359         if ( at->bam_next ) {
360                 return backsql_get_attr_vals( at->bam_next, v_bsi );
361         }
362
363         return 1;
364 }
365
366 Entry *
367 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
368 {
369         int                     i;
370         int                     rc;
371         AttributeDescription    *ad_oc = slap_schema.si_ad_objectClass;
372
373         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
374
375         rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname,
376                         bsi->bsi_op->o_tmpmemctx );
377         if ( rc != LDAP_SUCCESS ) {
378                 return NULL;
379         }
380
381         bsi->bsi_oc = backsql_id2oc( bsi->bsi_op->o_bd->be_private, eid->oc_id );
382         bsi->bsi_e = e;
383         bsi->bsi_c_eid = eid;
384         e->e_attrs = NULL;
385         e->e_private = NULL;
386  
387         e->e_id = eid->id;
388  
389         if ( bsi->bsi_attrs != NULL ) {
390                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
391                         "custom attribute list\n", 0, 0, 0 );
392                 for ( i = 0; bsi->bsi_attrs[ i ].an_name.bv_val; i++ ) {
393                         backsql_at_map_rec      **vat;
394                         AttributeName           *attr = &bsi->bsi_attrs[ i ];
395                         int                     j;
396
397                         if ( attr->an_desc == ad_oc ) {
398                                 continue;
399                         }
400
401                         rc = backsql_supad2at( bsi->bsi_oc, attr->an_desc, &vat );
402                         if ( rc != 0 || vat == NULL ) {
403                                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
404                                                 "attribute '%s' is not defined "
405                                                 "for objectlass '%s'\n",
406                                                 attr->an_name.bv_val, 
407                                                 BACKSQL_OC_NAME( bsi->bsi_oc ), 0 );
408                                 continue;
409                         }
410
411                         for ( j = 0; vat[j]; j++ ) {
412                                 backsql_get_attr_vals( vat[j], bsi );
413                         }
414
415                         ch_free( vat );
416                 }
417
418         } else {
419                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
420                         "retrieving all attributes\n", 0, 0, 0 );
421                 avl_apply( bsi->bsi_oc->bom_attrs, backsql_get_attr_vals,
422                                 bsi, 0, AVL_INORDER );
423         }
424
425         if ( attr_merge_normalize_one( bsi->bsi_e, ad_oc,
426                                 &bsi->bsi_oc->bom_oc->soc_cname,
427                                 bsi->bsi_op->o_tmpmemctx ) ) {
428                 entry_free( e );
429                 return NULL;
430         }
431
432         if ( global_schemacheck ) {
433                 const char      *text = NULL;
434                 char            textbuf[ 1024 ];
435                 size_t          textlen = sizeof( textbuf );
436                 struct berval   bv[ 2 ];
437                 struct berval   soc;
438                 int rc;
439
440                 bv[ 0 ] = bsi->bsi_oc->bom_oc->soc_cname;
441                 bv[ 1 ].bv_val = NULL;
442
443                 rc = structural_class( bv, &soc, NULL, 
444                                 &text, textbuf, textlen );
445                 if ( rc != LDAP_SUCCESS ) {
446                         entry_free( e );
447                         return NULL;
448                 }
449
450                 if ( ( bsi->bsi_flags | BSQL_SF_ALL_OPER )
451                                 || an_find( bsi->bsi_attrs, &AllOper ) ) {
452                         rc = attr_merge_normalize_one( bsi->bsi_e,
453                                         slap_schema.si_ad_structuralObjectClass,
454                                         &soc, bsi->bsi_op->o_tmpmemctx );
455                         if ( rc != LDAP_SUCCESS ) {
456                                 entry_free( e );
457                                 return NULL;
458                         }
459                 }
460         }
461
462         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
463
464         return e;
465 }
466
467 #endif /* SLAPD_SQL */
468