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