]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
Happy new year!
[openldap] / servers / slapd / back-sql / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_SQL
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "slap.h"
30 #include "ldap_pvt.h"
31 #include "proto-sql.h"
32
33 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
34
35 int
36 init_module(
37         int             argc, 
38         char            *argv[] )
39 {
40         BackendInfo bi;
41
42         memset( &bi, '\0', sizeof( bi ) );
43         bi.bi_type = "sql";
44         bi.bi_init = backsql_initialize;
45
46         backend_add( &bi );
47         return 0;
48 }
49
50 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
51
52 int
53 backsql_initialize(
54         BackendInfo     *bi )
55
56         static char *controls[] = {
57 #if 0 /* needs updating */
58 #ifdef LDAP_CONTROL_NOOP
59                 LDAP_CONTROL_NOOP,
60 #endif /* LDAP_CONTROL_NOOP */
61 #endif
62 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
63                 LDAP_CONTROL_VALUESRETURNFILTER,
64 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
65                 NULL
66         };
67
68         bi->bi_controls = controls;
69
70         Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
71         
72         bi->bi_open = 0;
73         bi->bi_config = 0;
74         bi->bi_close = 0;
75         bi->bi_destroy = 0;
76
77         bi->bi_db_init = backsql_db_init;
78         bi->bi_db_config = backsql_db_config;
79         bi->bi_db_open = backsql_db_open;
80         bi->bi_db_close = backsql_db_close;
81         bi->bi_db_destroy = backsql_db_destroy;
82
83         bi->bi_op_abandon = 0;
84         bi->bi_op_compare = backsql_compare;
85         bi->bi_op_bind = backsql_bind;
86         bi->bi_op_unbind = 0;
87         bi->bi_op_search = backsql_search;
88         bi->bi_op_modify = backsql_modify;
89         bi->bi_op_modrdn = backsql_modrdn;
90         bi->bi_op_add = backsql_add;
91         bi->bi_op_delete = backsql_delete;
92         
93         bi->bi_chk_referrals = 0;
94         bi->bi_operational = backsql_operational;
95  
96         bi->bi_connection_init = 0;
97         bi->bi_connection_destroy = backsql_connection_destroy;
98
99         Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
100         return 0;
101 }
102
103
104 int
105 backsql_destroy( 
106         BackendInfo     *bi )
107 {
108         Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
109         Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
110         return 0;
111 }
112
113 int
114 backsql_db_init(
115         BackendDB       *bd )
116 {
117         backsql_info *si;
118  
119         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
120         si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
121         memset( si, '\0', sizeof( backsql_info ) );
122         ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
123         ldap_pvt_thread_mutex_init( &si->schema_mutex );
124         backsql_init_db_env( si );
125
126         bd->be_private = si;
127         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
128         return 0;
129 }
130
131 int
132 backsql_db_destroy(
133         BackendDB       *bd )
134 {
135         backsql_info *si = (backsql_info*)bd->be_private;
136  
137         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
138         ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
139         backsql_free_db_env( si );
140         ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
141         ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
142         ldap_pvt_thread_mutex_lock( &si->schema_mutex );
143         backsql_destroy_schema_map( si );
144         ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
145         ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
146         free( si->dbname );
147         free( si->dbuser );
148         if ( si->dbpasswd ) {
149                 free( si->dbpasswd );
150         }
151         if ( si->dbhost ) {
152                 free( si->dbhost );
153         }
154         if ( si->upper_func.bv_val ) {
155                 free( si->upper_func.bv_val );
156                 free( si->upper_func_open.bv_val );
157                 free( si->upper_func_close.bv_val );
158         }
159         
160         free( si->subtree_cond.bv_val );
161         free( si->oc_query );
162         free( si->at_query );
163         free( si->insentry_query );
164         free( si->delentry_query );
165         free( si->delobjclasses_query );
166         free( si->delreferrals_query );
167         free( si );
168         
169         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
170         return 0;
171 }
172
173 int
174 backsql_db_open(
175         BackendDB       *bd )
176 {
177         backsql_info    *si = (backsql_info*)bd->be_private;
178         SQLHDBC         dbh;
179         ber_len_t       idq_len;
180         struct berbuf   bb = BB_NULL;
181
182         Operation       otmp = { 0 };
183                 
184         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
185                 "testing RDBMS connection\n", 0, 0, 0 );
186         if ( si->dbname == NULL ) {
187                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
188                         "datasource name not specified "
189                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
190                 return 1;
191         }
192
193         if ( si->concat_func == NULL ) {
194                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
195                         "concat func not specified (use \"concat_pattern\" "
196                         "directive in slapd.conf)\n", 0, 0, 0 );
197
198                 if ( backsql_split_pattern( backsql_def_concat_func, 
199                                 &si->concat_func, 2 ) ) {
200                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
201                                 "unable to parse pattern \"%s\"",
202                                 backsql_def_concat_func, 0, 0 );
203                         return 1;
204                 }
205         }
206
207         /*
208          * Prepare cast string as required
209          */
210         if ( si->upper_func.bv_val ) {
211                 char buf[1024];
212
213                 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
214                         snprintf( buf, sizeof( buf ), 
215                                 "%s(cast (" /* ? as varchar(%d))) */ , 
216                                 si->upper_func.bv_val );
217                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
218
219                         snprintf( buf, sizeof( buf ),
220                                 /* (cast(? */ " as varchar(%d)))",
221                                 BACKSQL_MAX_DN_LEN );
222                         ber_str2bv( buf, 0, 1, &si->upper_func_close );
223
224                 } else {
225                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
226                                         si->upper_func.bv_val );
227                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
228
229                         ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
230                 }
231         }
232
233         /* normalize filter values only if necessary */
234         si->bi_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
235         assert( si->bi_caseIgnoreMatch );
236
237         si->bi_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
238         assert( si->bi_telephoneNumberMatch );
239
240         if ( si->dbuser == NULL ) {
241                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
242                         "user name not specified "
243                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
244                 return 1;
245         }
246         
247         if ( si->subtree_cond.bv_val == NULL ) {
248                 /*
249                  * Prepare concat function for subtree search condition
250                  */
251                 struct berval   concat;
252                 struct berval   values[] = {
253                         BER_BVC( "'%'" ),
254                         BER_BVC( "?" ),
255                         BER_BVNULL
256                 };
257                 struct berbuf   bb = BB_NULL;
258
259                 if ( backsql_prepare_pattern( si->concat_func, values, 
260                                 &concat ) ) {
261                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
262                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
263                         return 1;
264                 }
265                         
266                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
267                         "subtree search SQL condition not specified "
268                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
269                         0, 0, 0);
270
271                 if ( si->upper_func.bv_val ) {
272
273                         /*
274                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
275                          */
276
277                         backsql_strfcat( &bb, "blbbb",
278                                         &si->upper_func,
279                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
280                                                 "(ldap_entries.dn) LIKE ",
281                                         &si->upper_func_open,
282                                         &concat,
283                                         &si->upper_func_close );
284
285                 } else {
286
287                         /*
288                          * ldap_entries.dn LIKE CONCAT('%',?)
289                          */
290
291                         backsql_strfcat( &bb, "lb",
292                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
293                                                 "ldap_entries.dn LIKE ",
294                                         &concat );
295                 }
296
297                 si->subtree_cond = bb.bb_val;
298                         
299                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
300                         "setting \"%s\" as default\n",
301                         si->subtree_cond.bv_val, 0, 0 );
302         }
303
304         if ( si->children_cond.bv_val == NULL ) {
305                 struct berbuf   bb = BB_NULL;
306
307                 if ( si->upper_func.bv_val ) {
308
309                         /*
310                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
311                          */
312
313                         backsql_strfcat( &bb, "blbl",
314                                         &si->upper_func,
315                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
316                                                 "(ldap_entries.dn)=",
317                                         &si->upper_func,
318                                         (ber_len_t)STRLENOF( "(?)" ), "(?)" );
319
320                 } else {
321
322                         /*
323                          * ldap_entries.dn LIKE CONCAT('%,',?)
324                          */
325
326                         backsql_strfcat( &bb, "l",
327                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
328                                                 "ldap_entries.dn=?");
329                 }
330
331                 si->children_cond = bb.bb_val;
332                         
333                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334                         "setting \"%s\" as default\n",
335                         si->children_cond.bv_val, 0, 0 );
336         }
337
338         if ( si->oc_query == NULL ) {
339                 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
340                         si->oc_query =
341                                 ch_strdup( backsql_def_needs_select_oc_query );
342
343                 } else {
344                         si->oc_query = ch_strdup( backsql_def_oc_query );
345                 }
346
347                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
348                         "objectclass mapping SQL statement not specified "
349                         "(use \"oc_query\" directive in slapd.conf)\n", 
350                         0, 0, 0 );
351                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
352                         "setting \"%s\" by default\n", si->oc_query, 0, 0 );
353         }
354         
355         if ( si->at_query == NULL ) {
356                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
357                         "attribute mapping SQL statement not specified "
358                         "(use \"at_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_at_query, 0, 0 );
363                 si->at_query = ch_strdup( backsql_def_at_query );
364         }
365         
366         if ( si->insentry_query == NULL ) {
367                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
368                         "entry insertion SQL statement not specified "
369                         "(use \"insentry_query\" directive in slapd.conf)\n",
370                         0, 0, 0 );
371                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
372                         "setting \"%s\" by default\n",
373                         backsql_def_insentry_query, 0, 0 );
374                 si->insentry_query = ch_strdup( backsql_def_insentry_query );
375         }
376         
377         if ( si->delentry_query == NULL ) {
378                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
379                         "entry deletion SQL statement not specified "
380                         "(use \"delentry_query\" directive in slapd.conf)\n",
381                         0, 0, 0 );
382                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
383                         "setting \"%s\" by default\n",
384                         backsql_def_delentry_query, 0, 0 );
385                 si->delentry_query = ch_strdup( backsql_def_delentry_query );
386         }
387
388         if ( si->delobjclasses_query == NULL ) {
389                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
390                         "objclasses deletion SQL statement not specified "
391                         "(use \"delobjclasses_query\" directive in slapd.conf)\n",
392                         0, 0, 0 );
393                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
394                         "setting \"%s\" by default\n",
395                         backsql_def_delobjclasses_query, 0, 0 );
396                 si->delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
397         }
398
399         if ( si->delreferrals_query == NULL ) {
400                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
401                         "referrals deletion SQL statement not specified "
402                         "(use \"delreferrals_query\" directive in slapd.conf)\n",
403                         0, 0, 0 );
404                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
405                         "setting \"%s\" by default\n",
406                         backsql_def_delreferrals_query, 0, 0 );
407                 si->delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
408         }
409
410         otmp.o_connid = (unsigned long)(-1);
411         otmp.o_bd = bd;
412         if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
413                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
414                         "connection failed, exiting\n", 0, 0, 0 );
415                 return 1;
416         }
417
418         /*
419          * Prepare ID selection query
420          */
421         si->id_query = NULL;
422         idq_len = 0;
423
424         if ( si->upper_func.bv_val == NULL ) {
425                 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
426
427         } else {
428                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
429                         backsql_strcat( &bb, backsql_id_query,
430                                         "dn_ru=?", NULL );
431                 } else {
432                         if ( BACKSQL_USE_REVERSE_DN( si ) ) {
433                                 backsql_strfcat( &bb, "sbl",
434                                                 backsql_id_query,
435                                                 &si->upper_func, 
436                                                 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
437                         } else {
438                                 backsql_strfcat( &bb, "sblbcb",
439                                                 backsql_id_query,
440                                                 &si->upper_func, 
441                                                 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
442                                                 &si->upper_func_open, 
443                                                 '?', 
444                                                 &si->upper_func_close );
445                         }
446                 }
447         }
448         si->id_query = bb.bb_val.bv_val;
449
450         /*
451          * Prepare children ID selection query
452          */
453         si->has_children_query = NULL;
454
455         bb.bb_val.bv_val = NULL;
456         bb.bb_val.bv_len = 0;
457         bb.bb_len = 0;
458         backsql_strfcat( &bb, "sb",
459                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
460
461                         &si->children_cond );
462         si->has_children_query = bb.bb_val.bv_val;
463  
464         backsql_free_db_conn( &otmp );
465         if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
466                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
467                         "test failed, schema map not loaded - exiting\n",
468                         0, 0, 0 );
469                 return 1;
470         }
471         
472         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
473                 "test succeeded, schema map loaded\n", 0, 0, 0 );
474         return 0;
475 }
476
477 int
478 backsql_db_close(
479         BackendDB       *bd )
480 {
481         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
482         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
483         return 0;
484 }
485
486 int
487 backsql_connection_destroy( Backend *bd, Connection *c )
488 {
489         Operation o = { 0 };
490         o.o_bd = bd;
491         o.o_connid = c->c_connid;
492
493         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
494         backsql_free_db_conn( &o );
495         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
496
497         return 0;
498 }
499
500 #endif /* SLAPD_SQL */
501