]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
e1a45c7f1e9b6b36f8d0a89d4162bb168a508027
[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_has_children(
168         backsql_info            *bi,
169         SQLHDBC                 dbh,
170         struct berval           *dn )
171 {
172         SQLHSTMT                sth; 
173         BACKSQL_ROW_NTS         row;
174         RETCODE                 rc;
175         int                     res;
176
177         Debug( LDAP_DEBUG_TRACE, "==>backsql_has_children(): dn='%s'\n", 
178                         dn->bv_val, 0, 0 );
179
180         if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
181                 Debug( LDAP_DEBUG_TRACE, 
182                         "backsql_has_children(): DN \"%s\" (%ld bytes) "
183                         "exceeds max DN length (%d):\n",
184                         dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
185                 return LDAP_OTHER;
186         }
187         
188         /* begin TimesTen */
189         Debug(LDAP_DEBUG_TRACE, "children id query '%s'\n", 
190                         bi->has_children_query, 0, 0);
191         assert( bi->has_children_query );
192         rc = backsql_Prepare( dbh, &sth, bi->has_children_query, 0 );
193         if ( rc != SQL_SUCCESS ) {
194                 Debug( LDAP_DEBUG_TRACE, 
195                         "backsql_has_children(): error preparing SQL:\n%s", 
196                         bi->has_children_query, 0, 0);
197                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
198                 SQLFreeStmt( sth, SQL_DROP );
199                 return LDAP_OTHER;
200         }
201
202         rc = backsql_BindParamStr( sth, 1, dn->bv_val, BACKSQL_MAX_DN_LEN );
203         if ( rc != SQL_SUCCESS) {
204                 /* end TimesTen */ 
205                 Debug( LDAP_DEBUG_TRACE, "backsql_has_children(): "
206                         "error binding dn=\"%s\" parameter:\n", 
207                         dn->bv_val, 0, 0 );
208                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
209                 SQLFreeStmt( sth, SQL_DROP );
210                 return LDAP_OTHER;
211         }
212
213         rc = SQLExecute( sth );
214         if ( rc != SQL_SUCCESS ) {
215                 Debug( LDAP_DEBUG_TRACE, "backsql_has_children(): "
216                         "error executing query (\"%s\", \"%s\"):\n", 
217                         bi->has_children_query, dn->bv_val, 0 );
218                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
219                 SQLFreeStmt( sth, SQL_DROP );
220                 return LDAP_OTHER;
221         }
222
223         backsql_BindRowAsStrings( sth, &row );
224         
225         rc = SQLFetch( sth );
226         if ( BACKSQL_SUCCESS( rc ) ) {
227                 if ( strtol( row.cols[ 0 ], NULL, 0 ) > 0 ) {
228                         res = LDAP_COMPARE_TRUE;
229                 } else {
230                         res = LDAP_COMPARE_FALSE;
231                 }
232
233         } else {
234                 res = LDAP_OTHER;
235         }
236         backsql_FreeRow( &row );
237
238         SQLFreeStmt( sth, SQL_DROP );
239
240         Debug( LDAP_DEBUG_TRACE, "<==backsql_has_children(): %s\n",
241                         res == LDAP_COMPARE_TRUE ? "yes" : "no", 0, 0 );
242
243         return res;
244 }
245
246 int
247 backsql_get_attr_vals( backsql_at_map_rec *at, backsql_srch_info *bsi )
248 {
249         RETCODE         rc;
250         SQLHSTMT        sth;
251         BACKSQL_ROW_NTS row;
252         int             i;
253
254         assert( at );
255         assert( bsi );
256  
257         Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
258                 "oc='%s' attr='%s' keyval=%ld\n",
259                 BACKSQL_OC_NAME( bsi->oc ), at->ad->ad_cname.bv_val, 
260                 bsi->c_eid->keyval );
261
262         rc = backsql_Prepare( bsi->dbh, &sth, at->query, 0 );
263         if ( rc != SQL_SUCCESS ) {
264                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
265                         "error preparing query: %s\n", at->query, 0, 0 );
266                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
267                 return 1;
268         }
269
270         rc = backsql_BindParamID( sth, 1, &bsi->c_eid->keyval );
271         if ( rc != SQL_SUCCESS ) {
272                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
273                         "error binding key value parameter\n", 0, 0, 0 );
274                 return 1;
275         }
276
277         rc = SQLExecute( sth );
278         if ( ! BACKSQL_SUCCESS( rc ) ) {
279                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
280                         "error executing attribute query '%s'\n",
281                         at->query, 0, 0 );
282                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
283                 SQLFreeStmt( sth, SQL_DROP );
284                 return 1;
285         }
286
287         backsql_BindRowAsStrings( sth, &row );
288
289         rc = SQLFetch( sth );
290         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
291                 for ( i = 0; i < row.ncols; i++ ) {
292                         if ( row.is_null[ i ] > 0 ) {
293                                 struct berval   bv;
294
295                                 bv.bv_val = row.cols[ i ];
296 #if 0
297                                 bv.bv_len = row.col_prec[ i ];
298 #else
299                                 /*
300                                  * FIXME: what if a binary 
301                                  * is fetched?
302                                  */
303                                 bv.bv_len = strlen( row.cols[ i ] );
304 #endif
305                                 backsql_entry_addattr( bsi->e, 
306                                                 &row.col_names[ i ], &bv );
307
308 #ifdef BACKSQL_TRACE
309                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
310                                         (int)row.col_prec[ i ], 0, 0 );
311                         } else {
312                                 Debug( LDAP_DEBUG_TRACE, "NULL value "
313                                         "in this row for attribute '%s'\n",
314                                         row.col_names[ i ].bv_val, 0, 0 );
315 #endif /* BACKSQL_TRACE */
316                         }
317                 }
318         }
319
320         backsql_FreeRow( &row );
321         SQLFreeStmt( sth, SQL_DROP );
322         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
323
324         return 1;
325 }
326
327 Entry *
328 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
329 {
330         int                     i;
331         backsql_at_map_rec      *at;
332         int                     rc;
333         AttributeDescription    *ad_oc = slap_schema.si_ad_objectClass;
334
335         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
336
337         rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
338         if ( rc != LDAP_SUCCESS ) {
339                 return NULL;
340         }
341
342         bsi->oc = backsql_id2oc( bsi->bi, eid->oc_id );
343         bsi->e = e;
344         bsi->c_eid = eid;
345         e->e_attrs = NULL;
346         e->e_private = NULL;
347  
348         /* if ( bsi->base_dn != NULL)??? */
349         
350         e->e_id = eid->id;
351  
352         if ( bsi->attrs != NULL ) {
353                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
354                         "custom attribute list\n", 0, 0, 0 );
355                 for ( i = 0; bsi->attrs[ i ].an_name.bv_val; i++ ) {
356                         AttributeName *attr = &bsi->attrs[ i ];
357
358                         if ( attr->an_desc == ad_oc
359 #if 0   /* FIXME: what is 0.10 ? */
360                                         || !BACKSQL_NCMP( &attr->an_name, &bv_n_0_10 ) 
361 #endif
362                                         ) {
363 #if 0
364                                 backsql_entry_addattr( bsi->e, 
365                                                 &bv_n_objectclass,
366                                                 BACKSQL_OC_NAME( bsi->oc ) );
367 #endif
368                                 continue;
369                         }
370
371                         at = backsql_ad2at( bsi->oc, attr->an_desc );
372                         if ( at != NULL ) {
373                                 backsql_get_attr_vals( at, bsi );
374                         } else {
375                                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
376                                         "attribute '%s' is not defined "
377                                         "for objectlass '%s'\n",
378                                         attr->an_name.bv_val, 
379                                         BACKSQL_OC_NAME( bsi->oc ), 0 );
380                         }
381                 }
382
383         } else {
384                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
385                         "retrieving all attributes\n", 0, 0, 0 );
386                 avl_apply( bsi->oc->attrs, (AVL_APPLY)backsql_get_attr_vals,
387                                 bsi, 0, AVL_INORDER );
388         }
389
390         if ( attr_merge_one( bsi->e, ad_oc, &bsi->oc->oc->soc_cname ) ) {
391                 entry_free( e );
392                 return NULL;
393         }
394
395         if ( global_schemacheck ) {
396                 const char      *text = NULL;
397                 char            textbuf[ 1024 ];
398                 size_t          textlen = sizeof( textbuf );
399                 struct berval   bv[ 2 ] = { bsi->oc->oc->soc_cname, BER_BVNULL };
400                 struct berval   soc;
401                 AttributeDescription    *ad_soc
402                         = slap_schema.si_ad_structuralObjectClass;
403
404                 int rc = structural_class( bv, &soc, NULL, 
405                                 &text, textbuf, textlen );
406                 if ( rc != LDAP_SUCCESS ) {
407                         entry_free( e );
408                         return NULL;
409                 }
410
411                 if ( bsi->bsi_flags | BSQL_SF_ALL_OPER 
412                                 || an_find( bsi->attrs, &AllOper ) ) {
413                         if ( attr_merge_one( bsi->e, ad_soc, &soc ) ) {
414                                 entry_free( e );
415                                 return NULL;
416                         }
417                 }
418         }
419
420         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
421
422         return e;
423 }
424
425 #endif /* SLAPD_SQL */
426