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