]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
552d23946e09706e28cff8d02d2cfc48b029e6e8
[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 "ldap_pvt.h"
18 #include "slap.h"
19 #include "back-sql.h"
20 #include "sql-wrap.h"
21 #include "schema-map.h"
22 #include "entry-id.h"
23 #include "util.h"
24
25 backsql_entryID *
26 backsql_free_entryID( backsql_entryID *id, int freeit )
27 {
28         backsql_entryID         *next;
29
30         assert( id );
31
32         next = id->next;
33
34         if ( id->dn.bv_val != NULL ) {
35                 free( id->dn.bv_val );
36         }
37
38         if ( freeit ) {
39                 free( id );
40         }
41
42         return next;
43 }
44
45 /*
46  * FIXME: need to change API to pass backsql_entryID **id 
47  * and return an error code, to distinguish LDAP_OTHER from
48  * LDAP_NO_SUCH_OBJECT
49  */
50 int
51 backsql_dn2id(
52         backsql_info            *bi,
53         backsql_entryID         *id,
54         SQLHDBC                 dbh,
55         struct berval           *dn )
56 {
57         SQLHSTMT                sth; 
58         BACKSQL_ROW_NTS         row;
59 #if 0
60         SQLINTEGER              nrows = 0;
61 #endif
62         RETCODE                 rc;
63         int                     res;
64
65         /* TimesTen */
66         char                    upperdn[ BACKSQL_MAX_DN_LEN + 1 ];
67         char                    *toBind;
68         int                     i, j;
69
70         Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): dn='%s'\n", 
71                         dn->bv_val, 0, 0 );
72
73         assert( id );
74
75         if ( dn->bv_len > BACKSQL_MAX_DN_LEN ) {
76                 Debug( LDAP_DEBUG_TRACE, 
77                         "backsql_dn2id(): DN \"%s\" (%ld bytes) "
78                         "exceeds max DN length (%d):\n",
79                         dn->bv_val, dn->bv_len, BACKSQL_MAX_DN_LEN );
80                 return LDAP_OTHER;
81         }
82         
83         /* begin TimesTen */
84         Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
85         assert( bi->id_query );
86         rc = backsql_Prepare( dbh, &sth, bi->id_query, 0 );
87         if ( rc != SQL_SUCCESS ) {
88                 Debug( LDAP_DEBUG_TRACE, 
89                         "backsql_dn2id(): error preparing SQL:\n%s", 
90                         bi->id_query, 0, 0);
91                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
92                 SQLFreeStmt( sth, SQL_DROP );
93                 return LDAP_OTHER;
94         }
95
96         if ( bi->has_ldapinfo_dn_ru ) {
97                 /*
98                  * Prepare an upper cased, byte reversed version 
99                  * that can be searched using indexes
100                  */
101
102                 for ( i = 0, j = dn->bv_len - 1; dn->bv_val[ i ]; i++, j--) {
103                         upperdn[ i ] = dn->bv_val[ j ];
104                 }
105                 upperdn[ i ] = '\0';
106                 ldap_pvt_str2upper( upperdn );
107
108                 Debug( LDAP_DEBUG_TRACE, "==>backsql_dn2id(): upperdn='%s'\n",
109                                 upperdn, 0, 0 );
110                 toBind = upperdn;
111         } else {
112                 if ( bi->isTimesTen ) {
113                         AC_MEMCPY( upperdn, dn->bv_val, dn->bv_len + 1 );
114                         ldap_pvt_str2upper( upperdn );
115                         Debug( LDAP_DEBUG_TRACE,
116                                 "==>backsql_dn2id(): upperdn='%s'\n",
117                                 upperdn, 0, 0 );
118                         toBind = upperdn;
119
120                 } else {
121                         toBind = dn->bv_val;
122                 }
123         }
124
125         rc = backsql_BindParamStr( sth, 1, toBind, BACKSQL_MAX_DN_LEN );
126         if ( rc != SQL_SUCCESS) {
127                 /* end TimesTen */ 
128                 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
129                         "error binding dn=\"%s\" parameter:\n", toBind, 0, 0 );
130                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
131                 SQLFreeStmt( sth, SQL_DROP );
132                 return LDAP_OTHER;
133         }
134
135         rc = SQLExecute( sth );
136         if ( rc != SQL_SUCCESS ) {
137                 Debug( LDAP_DEBUG_TRACE, "backsql_dn2id(): "
138                         "error executing query (\"%s\", \"%s\"):\n", 
139                         bi->id_query, toBind, 0 );
140                 backsql_PrintErrors( SQL_NULL_HENV, dbh, sth, rc );
141                 SQLFreeStmt( sth, SQL_DROP );
142                 return LDAP_OTHER;
143         }
144
145         backsql_BindRowAsStrings( sth, &row );
146         rc = SQLFetch( sth );
147         if ( BACKSQL_SUCCESS( rc ) ) {
148                 id->id = atoi( row.cols[ 0 ] );
149                 id->keyval = atoi( row.cols[ 1 ] );
150                 id->oc_id = atoi( row.cols[ 2 ] );
151                 ber_dupbv( &id->dn, dn );
152                 id->next = NULL;
153
154                 res = LDAP_SUCCESS;
155
156         } else {
157                 res = LDAP_NO_SUCH_OBJECT;
158         }
159         backsql_FreeRow( &row );
160
161         SQLFreeStmt( sth, SQL_DROP );
162         if ( res == LDAP_SUCCESS ) {
163                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): id=%ld\n",
164                                 id->id, 0, 0 );
165         } else {
166                 Debug( LDAP_DEBUG_TRACE, "<==backsql_dn2id(): no match\n",
167                                 0, 0, 0 );
168         }
169         return res;
170 }
171
172 int
173 backsql_get_attr_vals( backsql_at_map_rec *at, backsql_srch_info *bsi )
174 {
175         RETCODE         rc;
176         SQLHSTMT        sth;
177         BACKSQL_ROW_NTS row;
178         int             i;
179
180         assert( at );
181         assert( bsi );
182  
183         Debug( LDAP_DEBUG_TRACE, "==>backsql_get_attr_vals(): "
184                 "oc='%s' attr='%s' keyval=%ld\n",
185                 bsi->oc->name.bv_val, at->name.bv_val, bsi->c_eid->keyval );
186
187         rc = backsql_Prepare( bsi->dbh, &sth, at->query, 0 );
188         if ( rc != SQL_SUCCESS ) {
189                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
190                         "error preparing query: %s\n", at->query, 0, 0 );
191                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
192                 return 1;
193         }
194
195         rc = backsql_BindParamID( sth, 1, &bsi->c_eid->keyval );
196         if ( rc != SQL_SUCCESS ) {
197                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
198                         "error binding key value parameter\n", 0, 0, 0 );
199                 return 1;
200         }
201
202         rc = SQLExecute( sth );
203         if ( ! BACKSQL_SUCCESS( rc ) ) {
204                 Debug( LDAP_DEBUG_TRACE, "backsql_get_attr_values(): "
205                         "error executing attribute query '%s'\n",
206                         at->query, 0, 0 );
207                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
208                 SQLFreeStmt( sth, SQL_DROP );
209                 return 1;
210         }
211
212         backsql_BindRowAsStrings( sth, &row );
213
214         rc = SQLFetch( sth );
215         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
216                 for ( i = 0; i < row.ncols; i++ ) {
217                         if ( row.is_null[ i ] > 0 ) {
218                                 struct berval   bv;
219
220                                 bv.bv_val = row.cols[ i ];
221 #if 0
222                                 bv.bv_len = row.col_prec[ i ];
223 #else
224                                 /*
225                                  * FIXME: what if a binary 
226                                  * is fetched?
227                                  */
228                                 bv.bv_len = strlen( row.cols[ i ] );
229 #endif
230                                 backsql_entry_addattr( bsi->e, 
231                                                 &row.col_names[ i ], &bv );
232
233 #if 0
234                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
235                                         (int)row.col_prec[ i ], 0, 0 );
236                         } else {
237                                 Debug( LDAP_DEBUG_TRACE, "NULL value "
238                                         "in this row for attribute '%s'\n",
239                                         row.col_names[ i ].bv_val, 0, 0 );
240 #endif
241                         }
242                 }
243         }
244
245         backsql_FreeRow( &row );
246         SQLFreeStmt( sth, SQL_DROP );
247         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
248
249         return 1;
250 }
251
252 Entry *
253 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
254 {
255         int                     i;
256         backsql_at_map_rec      *at;
257         int                     rc;
258
259         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
260
261         rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
262         if ( rc != LDAP_SUCCESS ) {
263                 return NULL;
264         }
265
266         bsi->oc = backsql_id2oc( bsi->bi, eid->oc_id );
267         bsi->e = e;
268         bsi->c_eid = eid;
269         e->e_attrs = NULL;
270         e->e_private = NULL;
271  
272         /* if ( bsi->base_dn != NULL)??? */
273         
274         e->e_id = eid->id;
275  
276         if ( bsi->attrs != NULL ) {
277                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
278                         "custom attribute list\n", 0, 0, 0 );
279                 for ( i = 0; bsi->attrs[ i ].an_name.bv_val; i++ ) {
280                         AttributeName *attr = &bsi->attrs[ i ];
281
282                         if ( attr->an_desc == slap_schema.si_ad_objectClass
283 #if 0   /* FIXME: what is 0.10 ? */
284                                         || !BACKSQL_NCMP( &attr->an_name, &bv_n_0_10 ) 
285 #endif
286                                         ) {
287 #if 0
288                                 backsql_entry_addattr( bsi->e, 
289                                                 &bv_n_objectclass,
290                                                 &bsi->oc->name );
291 #endif
292                                 continue;
293                         }
294
295                         at = backsql_ad2at( bsi->oc, attr->an_desc );
296                         if ( at != NULL ) {
297                                 backsql_get_attr_vals( at, bsi );
298                         } else {
299                                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
300                                         "attribute '%s' is not defined "
301                                         "for objectlass '%s'\n",
302                                         attr->an_name.bv_val, 
303                                         bsi->oc->name.bv_val, 0 );
304                         }
305                 }
306         } else {
307                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
308                         "retrieving all attributes\n", 0, 0, 0 );
309                 avl_apply( bsi->oc->attrs, (AVL_APPLY)backsql_get_attr_vals,
310                                 bsi, 0, AVL_INORDER );
311         }
312
313         backsql_entry_addattr( bsi->e, &bv_n_objectclass, &bsi->oc->name );
314
315         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
316
317         return e;
318 }
319
320 #endif /* SLAPD_SQL */
321