]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
eab33bbc140462d3fe4b2635781479331353603f
[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, at->name, 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                                 backsql_entry_addattr( bsi->e, 
219                                                 row.col_names[ i ],
220                                                 row.cols[ i ],
221 #if 0
222                                                 row.col_prec[ i ]
223 #else
224                                                 /*
225                                                  * FIXME: what if a binary 
226                                                  * is fetched?
227                                                  */
228                                                 strlen( row.cols[ i ] )
229 #endif
230                                                 );
231 #if 0
232                                 Debug( LDAP_DEBUG_TRACE, "prec=%d\n",
233                                         (int)row.col_prec[ i ], 0, 0 );
234                         } else {
235                                 Debug( LDAP_DEBUG_TRACE, "NULL value "
236                                         "in this row for attribute '%s'\n",
237                                         row.col_names[ i ], 0, 0 );
238 #endif
239                         }
240                 }
241         }
242
243         backsql_FreeRow( &row );
244         SQLFreeStmt( sth, SQL_DROP );
245         Debug( LDAP_DEBUG_TRACE, "<==backsql_get_attr_vals()\n", 0, 0, 0 );
246
247         return 1;
248 }
249
250 Entry *
251 backsql_id2entry( backsql_srch_info *bsi, Entry *e, backsql_entryID *eid )
252 {
253         char                    **c_at_name;
254         backsql_at_map_rec      *at;
255         int                     rc;
256
257         Debug( LDAP_DEBUG_TRACE, "==>backsql_id2entry()\n", 0, 0, 0 );
258
259         rc = dnPrettyNormal( NULL, &eid->dn, &e->e_name, &e->e_nname );
260         if ( rc != LDAP_SUCCESS ) {
261                 return NULL;
262         }
263
264         bsi->oc = backsql_oc_with_id( bsi->bi, eid->oc_id );
265         bsi->e = e;
266         bsi->c_eid = eid;
267         e->e_attrs = NULL;
268         e->e_private = NULL;
269  
270         /* if ( bsi->base_dn != NULL)??? */
271         
272         e->e_id = eid->id;
273  
274         if ( bsi->attrs != NULL ) {
275                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
276                         "custom attribute list\n", 0, 0, 0 );
277                 for ( c_at_name = bsi->attrs; *c_at_name != NULL; c_at_name++ ) {
278                         if ( !strcasecmp( *c_at_name, "objectclass" ) 
279                                         || !strcasecmp( *c_at_name, "0.10" ) ) {
280 #if 0
281                                 backsql_entry_addattr( bsi->e, "objectclass",
282                                                 bsi->oc->name,
283                                                 strlen( bsi->oc->name ) );
284 #endif
285                                 continue;
286                         }
287                         at = backsql_at_with_name( bsi->oc, *c_at_name );
288                         if ( at != NULL ) {
289                                 backsql_get_attr_vals( at, bsi );
290                         } else {
291                                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
292                                         "attribute '%s' is not defined "
293                                         "for objectlass '%s'\n",
294                                         *c_at_name, bsi->oc->name, 0 );
295                         }
296                 }
297         } else {
298                 Debug( LDAP_DEBUG_TRACE, "backsql_id2entry(): "
299                         "retrieving all attributes\n", 0, 0, 0 );
300                 avl_apply( bsi->oc->attrs, (AVL_APPLY)backsql_get_attr_vals,
301                                 bsi, 0, AVL_INORDER );
302         }
303
304         backsql_entry_addattr( bsi->e, "objectclass", bsi->oc->name,
305                         strlen( bsi->oc->name ) );
306
307         Debug( LDAP_DEBUG_TRACE, "<==backsql_id2entry()\n", 0, 0, 0 );
308
309         return e;
310 }
311
312 #endif /* SLAPD_SQL */
313