]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
more new API ...
[openldap] / servers / slapd / back-sql / init.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 "slap.h"
17 #include "ldap_pvt.h"
18 #include "back-sql.h"
19 #include "sql-wrap.h"
20 #include "schema-map.h"
21 #include "util.h"
22
23 #ifdef SLAPD_SQL_DYNAMIC
24
25 int
26 backsql_LTX_init_module(
27         int             argc, 
28         char            *argv[] )
29 {
30         BackendInfo bi;
31
32         memset( &bi, '\0', sizeof( bi ) );
33         bi.bi_type = "sql";
34         bi.bi_init = sql_back_initialize;
35
36         backend_add( &bi );
37         return 0;
38 }
39
40 #endif /* SLAPD_SHELL_DYNAMIC */
41
42 int
43 sql_back_initialize(
44         BackendInfo     *bi )
45
46         static char *controls[] = {
47 #ifdef LDAP_CONTROL_NOOP
48                 LDAP_CONTROL_NOOP,
49 #endif
50 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
51                 LDAP_CONTROL_VALUESRETURNFILTER,
52 #endif
53                 NULL
54         };
55
56         bi->bi_controls = controls;
57
58         Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
59         
60         bi->bi_open = 0;
61         bi->bi_config = 0;
62         bi->bi_close = 0;
63         bi->bi_destroy = 0;
64
65         bi->bi_db_init = backsql_db_init;
66         bi->bi_db_config = backsql_db_config;
67         bi->bi_db_open = backsql_db_open;
68         bi->bi_db_close = backsql_db_close;
69         bi->bi_db_destroy = backsql_db_destroy;
70
71         bi->bi_op_abandon = 0;
72         bi->bi_op_compare = backsql_compare;
73         bi->bi_op_bind = backsql_bind;
74         bi->bi_op_unbind = 0;
75         bi->bi_op_search = backsql_search;
76         bi->bi_op_modify = backsql_modify;
77         bi->bi_op_modrdn = backsql_modrdn;
78         bi->bi_op_add = backsql_add;
79         bi->bi_op_delete = backsql_delete;
80         
81         bi->bi_chk_referrals = 0;
82         bi->bi_operational = backsql_operational;
83  
84         bi->bi_connection_init = 0;
85         bi->bi_connection_destroy = backsql_connection_destroy;
86         
87         Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
88         return 0;
89 }
90
91
92 int
93 backsql_destroy( 
94         BackendInfo     *bi )
95 {
96         Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
97         Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
98         return 0;
99 }
100
101 int
102 backsql_db_init(
103         BackendDB       *bd )
104 {
105         backsql_info *si;
106  
107         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
108         si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
109         memset( si, '\0', sizeof( backsql_info ) );
110         ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
111         ldap_pvt_thread_mutex_init( &si->schema_mutex );
112         backsql_init_db_env( si );
113
114         bd->be_private = si;
115         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
116         return 0;
117 }
118
119 int
120 backsql_db_destroy(
121         BackendDB       *bd )
122 {
123         backsql_info *si = (backsql_info*)bd->be_private;
124  
125         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
126         ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
127         backsql_free_db_env( si );
128         ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
129         ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
130         ldap_pvt_thread_mutex_lock( &si->schema_mutex );
131         backsql_destroy_schema_map( si );
132         ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
133         ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
134         free( si->dbname );
135         free( si->dbuser );
136         if ( si->dbpasswd ) {
137                 free( si->dbpasswd );
138         }
139         if ( si->dbhost ) {
140                 free( si->dbhost );
141         }
142         if ( si->upper_func.bv_val ) {
143                 free( si->upper_func.bv_val );
144                 free( si->upper_func_open.bv_val );
145                 free( si->upper_func_close.bv_val );
146         }
147         
148         free( si->subtree_cond.bv_val );
149         free( si->oc_query );
150         free( si->at_query );
151         free( si->insentry_query );
152         free( si->delentry_query );
153         free( si );
154         
155         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
156         return 0;
157 }
158
159 int
160 backsql_db_open(
161         BackendDB       *bd )
162 {
163         backsql_info    *si = (backsql_info*)bd->be_private;
164         Connection      tmp;
165         SQLHDBC         dbh;
166         ber_len_t       idq_len;
167         struct berval   bv;
168
169         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
170                 "testing RDBMS connection\n", 0, 0, 0 );
171         if ( si->dbname == NULL ) {
172                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
173                         "datasource name not specified "
174                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
175                 return 1;
176         }
177
178         if ( si->concat_func == NULL ) {
179                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
180                         "concat func not specified (use \"concat_pattern\" "
181                         "directive in slapd.conf)\n", 0, 0, 0 );
182
183                 if ( backsql_split_pattern( backsql_def_concat_func, 
184                                 &si->concat_func, 2 ) ) {
185                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
186                                 "unable to parse pattern '%s'",
187                                 backsql_def_concat_func, 0, 0 );
188                         return 1;
189                 }
190         }
191
192         /*
193          * Prepare cast string as required
194          */
195         if ( si->upper_func.bv_val ) {
196                 char buf[1024];
197
198                 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
199                         snprintf( buf, sizeof( buf ), 
200                                 "%s(cast (" /* ? as varchar(%d))) */ , 
201                                 si->upper_func.bv_val );
202                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
203
204                         snprintf( buf, sizeof( buf ),
205                                 /* (cast(? */ " as varchar(%d)))",
206                                 BACKSQL_MAX_DN_LEN );
207                         ber_str2bv( buf, 0, 1, &si->upper_func_close );
208
209                 } else {
210                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
211                                         si->upper_func.bv_val );
212                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
213
214                         ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
215                 }
216         }
217         
218         if ( si->dbuser == NULL ) {
219                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
220                         "user name not specified "
221                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
222                 return 1;
223         }
224         
225         if ( si->subtree_cond.bv_val == NULL ) {
226                 /*
227                  * Prepare concat function for subtree search condition
228                  */
229                 struct berval   concat;
230                 ber_len_t       len = 0;
231                 struct berval   values[] = {
232                         { sizeof( "'%'" ) - 1,  "'%'" },
233                         { sizeof( "?" ) - 1,    "?" },
234                         { 0,                    NULL }
235                 };
236
237                 if ( backsql_prepare_pattern( si->concat_func, values, 
238                                 &concat ) ) {
239                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
240                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
241                         return 1;
242                 }
243                         
244                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
245                         "subtree search SQL condition not specified "
246                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
247                         0, 0, 0);
248
249                 si->subtree_cond.bv_val = NULL;
250                 si->subtree_cond.bv_len = 0;
251
252                 if ( si->upper_func.bv_val ) {
253
254                         /*
255                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
256                          */
257
258                         backsql_strfcat( &si->subtree_cond, &len, "blbbb",
259                                         &si->upper_func,
260                                         (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
261                                                 "(ldap_entries.dn) LIKE ",
262                                         &si->upper_func_open,
263                                         &concat,
264                                         &si->upper_func_close );
265
266                 } else {
267
268                         /*
269                          * ldap_entries.dn LIKE CONCAT('%',?)
270                          */
271
272                         backsql_strfcat( &si->subtree_cond, &len, "lb",
273                                         (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
274                                                 "ldap_entries.dn LIKE ",
275                                         &concat );
276                 }
277                         
278                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
279                         "setting '%s' as default\n",
280                         si->subtree_cond.bv_val, 0, 0 );
281         }
282
283         if ( si->children_cond.bv_val == NULL ) {
284                 ber_len_t       len = 0;
285
286                 if ( si->upper_func.bv_val ) {
287
288                         /*
289                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
290                          */
291
292                         backsql_strfcat( &si->children_cond, &len, "blbl",
293                                         &si->upper_func,
294                                         (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
295                                                 "(ldap_entries.dn)=",
296                                         &si->upper_func,
297                                         (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
298
299                 } else {
300
301                         /*
302                          * ldap_entries.dn LIKE CONCAT('%,',?)
303                          */
304
305                         backsql_strfcat( &si->children_cond, &len, "l",
306                                         (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
307                                                 "ldap_entries.dn=?");
308                 }
309                         
310                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
311                         "setting '%s' as default\n",
312                         si->children_cond.bv_val, 0, 0 );
313         }
314
315         if ( si->oc_query == NULL ) {
316                 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
317                         si->oc_query =
318                                 ch_strdup( backsql_def_needs_select_oc_query );
319
320                 } else {
321                         si->oc_query = ch_strdup( backsql_def_oc_query );
322                 }
323
324                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
325                         "objectclass mapping SQL statement not specified "
326                         "(use \"oc_query\" directive in slapd.conf)\n", 
327                         0, 0, 0 );
328                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
329                         "setting '%s' by default\n", si->oc_query, 0, 0 );
330         }
331         
332         if ( si->at_query == NULL ) {
333                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334                         "attribute mapping SQL statement not specified "
335                         "(use \"at_query\" directive in slapd.conf)\n",
336                         0, 0, 0 );
337                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
338                         "setting '%s' by default\n",
339                         backsql_def_at_query, 0, 0 );
340                 si->at_query = ch_strdup( backsql_def_at_query );
341         }
342         
343         if ( si->insentry_query == NULL ) {
344                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
345                         "entry insertion SQL statement not specified "
346                         "(use \"insentry_query\" directive in slapd.conf)\n",
347                         0, 0, 0 );
348                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
349                         "setting '%s' by default\n",
350                         backsql_def_insentry_query, 0, 0 );
351                 si->insentry_query = ch_strdup( backsql_def_insentry_query );
352         }
353         
354         if ( si->delentry_query == NULL ) {
355                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
356                         "entry deletion SQL statement not specified "
357                         "(use \"delentry_query\" directive in slapd.conf)\n",
358                         0, 0, 0 );
359                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
360                         "setting '%s' by default\n",
361                         backsql_def_delentry_query, 0, 0 );
362                 si->delentry_query = ch_strdup( backsql_def_delentry_query );
363         }
364
365
366         tmp.c_connid =- 1;
367         if ( backsql_get_db_conn( bd, &tmp, &dbh ) != LDAP_SUCCESS ) {
368                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
369                         "connection failed, exiting\n", 0, 0, 0 );
370                 return 1;
371         }
372
373         /*
374          * Prepare ID selection query
375          */
376         si->id_query = NULL;
377         idq_len = 0;
378
379         bv.bv_val = NULL;
380         bv.bv_len = 0;
381         if ( si->upper_func.bv_val == NULL ) {
382                 backsql_strcat( &bv, &idq_len, backsql_id_query, 
383                                 "dn=?", NULL );
384         } else {
385                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
386                         backsql_strcat( &bv, &idq_len, backsql_id_query,
387                                         "dn_ru=?", NULL );
388                 } else {
389                         if ( BACKSQL_USE_REVERSE_DN( si ) ) {
390                                 backsql_strfcat( &bv, &idq_len, "sbl",
391                                                 backsql_id_query,
392                                                 &si->upper_func, 
393                                                 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
394                         } else {
395                                 backsql_strfcat( &bv, &idq_len, "sblbcb",
396                                                 backsql_id_query,
397                                                 &si->upper_func, 
398                                                 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
399                                                 &si->upper_func_open, 
400                                                 '?', 
401                                                 &si->upper_func_close );
402                         }
403                 }
404         }
405         si->id_query = bv.bv_val;
406
407         /*
408          * Prepare children ID selection query
409          */
410         si->has_children_query = NULL;
411         idq_len = 0;
412
413         bv.bv_val = NULL;
414         bv.bv_len = 0;
415         backsql_strfcat( &bv, &idq_len, "sb",
416                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
417
418                         &si->children_cond );
419         si->has_children_query = bv.bv_val;
420  
421         backsql_free_db_conn( bd, &tmp );
422         if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
423                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
424                         "test failed, schema map not loaded - exiting\n",
425                         0, 0, 0 );
426                 return 1;
427         }
428         
429         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
430                 "test succeeded, schema map loaded\n", 0, 0, 0 );
431         return 0;
432 }
433
434 int
435 backsql_db_close(
436         BackendDB       *bd )
437 {
438         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
439         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
440         return 0;
441 }
442
443 int
444 backsql_connection_destroy(
445         BackendDB       *be,
446         Connection      *conn )
447 {
448         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
449         backsql_free_db_conn( be, conn );
450         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
451         return 0;
452 }
453
454 #endif /* SLAPD_SQL */
455