]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
b946f24c10c8bf43835329dffd568211e55af7c7
[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         SQLHDBC         dbh;
165         ber_len_t       idq_len;
166         struct berval   bv;
167
168         Operation       otmp;
169                 
170         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
171                 "testing RDBMS connection\n", 0, 0, 0 );
172         if ( si->dbname == NULL ) {
173                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
174                         "datasource name not specified "
175                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
176                 return 1;
177         }
178
179         if ( si->concat_func == NULL ) {
180                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
181                         "concat func not specified (use \"concat_pattern\" "
182                         "directive in slapd.conf)\n", 0, 0, 0 );
183
184                 if ( backsql_split_pattern( backsql_def_concat_func, 
185                                 &si->concat_func, 2 ) ) {
186                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
187                                 "unable to parse pattern '%s'",
188                                 backsql_def_concat_func, 0, 0 );
189                         return 1;
190                 }
191         }
192
193         /*
194          * Prepare cast string as required
195          */
196         if ( si->upper_func.bv_val ) {
197                 char buf[1024];
198
199                 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
200                         snprintf( buf, sizeof( buf ), 
201                                 "%s(cast (" /* ? as varchar(%d))) */ , 
202                                 si->upper_func.bv_val );
203                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
204
205                         snprintf( buf, sizeof( buf ),
206                                 /* (cast(? */ " as varchar(%d)))",
207                                 BACKSQL_MAX_DN_LEN );
208                         ber_str2bv( buf, 0, 1, &si->upper_func_close );
209
210                 } else {
211                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
212                                         si->upper_func.bv_val );
213                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
214
215                         ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
216                 }
217         }
218         
219         if ( si->dbuser == NULL ) {
220                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
221                         "user name not specified "
222                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
223                 return 1;
224         }
225         
226         if ( si->subtree_cond.bv_val == NULL ) {
227                 /*
228                  * Prepare concat function for subtree search condition
229                  */
230                 struct berval   concat;
231                 ber_len_t       len = 0;
232                 struct berval   values[] = {
233                         { sizeof( "'%'" ) - 1,  "'%'" },
234                         { sizeof( "?" ) - 1,    "?" },
235                         { 0,                    NULL }
236                 };
237
238                 if ( backsql_prepare_pattern( si->concat_func, values, 
239                                 &concat ) ) {
240                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
242                         return 1;
243                 }
244                         
245                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
246                         "subtree search SQL condition not specified "
247                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
248                         0, 0, 0);
249
250                 si->subtree_cond.bv_val = NULL;
251                 si->subtree_cond.bv_len = 0;
252
253                 if ( si->upper_func.bv_val ) {
254
255                         /*
256                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
257                          */
258
259                         backsql_strfcat( &si->subtree_cond, &len, "blbbb",
260                                         &si->upper_func,
261                                         (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
262                                                 "(ldap_entries.dn) LIKE ",
263                                         &si->upper_func_open,
264                                         &concat,
265                                         &si->upper_func_close );
266
267                 } else {
268
269                         /*
270                          * ldap_entries.dn LIKE CONCAT('%',?)
271                          */
272
273                         backsql_strfcat( &si->subtree_cond, &len, "lb",
274                                         (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
275                                                 "ldap_entries.dn LIKE ",
276                                         &concat );
277                 }
278                         
279                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
280                         "setting '%s' as default\n",
281                         si->subtree_cond.bv_val, 0, 0 );
282         }
283
284         if ( si->children_cond.bv_val == NULL ) {
285                 ber_len_t       len = 0;
286
287                 if ( si->upper_func.bv_val ) {
288
289                         /*
290                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
291                          */
292
293                         backsql_strfcat( &si->children_cond, &len, "blbl",
294                                         &si->upper_func,
295                                         (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
296                                                 "(ldap_entries.dn)=",
297                                         &si->upper_func,
298                                         (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
299
300                 } else {
301
302                         /*
303                          * ldap_entries.dn LIKE CONCAT('%,',?)
304                          */
305
306                         backsql_strfcat( &si->children_cond, &len, "l",
307                                         (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
308                                                 "ldap_entries.dn=?");
309                 }
310                         
311                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
312                         "setting '%s' as default\n",
313                         si->children_cond.bv_val, 0, 0 );
314         }
315
316         if ( si->oc_query == NULL ) {
317                 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
318                         si->oc_query =
319                                 ch_strdup( backsql_def_needs_select_oc_query );
320
321                 } else {
322                         si->oc_query = ch_strdup( backsql_def_oc_query );
323                 }
324
325                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
326                         "objectclass mapping SQL statement not specified "
327                         "(use \"oc_query\" directive in slapd.conf)\n", 
328                         0, 0, 0 );
329                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
330                         "setting '%s' by default\n", si->oc_query, 0, 0 );
331         }
332         
333         if ( si->at_query == NULL ) {
334                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
335                         "attribute mapping SQL statement not specified "
336                         "(use \"at_query\" directive in slapd.conf)\n",
337                         0, 0, 0 );
338                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
339                         "setting '%s' by default\n",
340                         backsql_def_at_query, 0, 0 );
341                 si->at_query = ch_strdup( backsql_def_at_query );
342         }
343         
344         if ( si->insentry_query == NULL ) {
345                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
346                         "entry insertion SQL statement not specified "
347                         "(use \"insentry_query\" directive in slapd.conf)\n",
348                         0, 0, 0 );
349                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
350                         "setting '%s' by default\n",
351                         backsql_def_insentry_query, 0, 0 );
352                 si->insentry_query = ch_strdup( backsql_def_insentry_query );
353         }
354         
355         if ( si->delentry_query == NULL ) {
356                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
357                         "entry deletion SQL statement not specified "
358                         "(use \"delentry_query\" directive in slapd.conf)\n",
359                         0, 0, 0 );
360                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
361                         "setting '%s' by default\n",
362                         backsql_def_delentry_query, 0, 0 );
363                 si->delentry_query = ch_strdup( backsql_def_delentry_query );
364         }
365
366         otmp.o_connid = -1;
367         otmp.o_bd = bd;
368         if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
369                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
370                         "connection failed, exiting\n", 0, 0, 0 );
371                 return 1;
372         }
373
374         /*
375          * Prepare ID selection query
376          */
377         si->id_query = NULL;
378         idq_len = 0;
379
380         bv.bv_val = NULL;
381         bv.bv_len = 0;
382         if ( si->upper_func.bv_val == NULL ) {
383                 backsql_strcat( &bv, &idq_len, backsql_id_query, 
384                                 "dn=?", NULL );
385         } else {
386                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
387                         backsql_strcat( &bv, &idq_len, backsql_id_query,
388                                         "dn_ru=?", NULL );
389                 } else {
390                         if ( BACKSQL_USE_REVERSE_DN( si ) ) {
391                                 backsql_strfcat( &bv, &idq_len, "sbl",
392                                                 backsql_id_query,
393                                                 &si->upper_func, 
394                                                 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
395                         } else {
396                                 backsql_strfcat( &bv, &idq_len, "sblbcb",
397                                                 backsql_id_query,
398                                                 &si->upper_func, 
399                                                 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
400                                                 &si->upper_func_open, 
401                                                 '?', 
402                                                 &si->upper_func_close );
403                         }
404                 }
405         }
406         si->id_query = bv.bv_val;
407
408         /*
409          * Prepare children ID selection query
410          */
411         si->has_children_query = NULL;
412         idq_len = 0;
413
414         bv.bv_val = NULL;
415         bv.bv_len = 0;
416         backsql_strfcat( &bv, &idq_len, "sb",
417                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
418
419                         &si->children_cond );
420         si->has_children_query = bv.bv_val;
421  
422         backsql_free_db_conn( &otmp );
423         if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
424                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
425                         "test failed, schema map not loaded - exiting\n",
426                         0, 0, 0 );
427                 return 1;
428         }
429         
430         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
431                 "test succeeded, schema map loaded\n", 0, 0, 0 );
432         return 0;
433 }
434
435 int
436 backsql_db_close(
437         BackendDB       *bd )
438 {
439         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
440         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
441         return 0;
442 }
443
444 int
445 backsql_connection_destroy( Backend *bd, Connection *c )
446 {
447         Operation o;
448         o.o_bd = bd;
449         o.o_connid = c->c_connid;
450
451         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
452         backsql_free_db_conn( &o );
453         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
454         return 0;
455 }
456
457 #endif /* SLAPD_SQL */
458