]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
2e0c2829660c8330c8e30d6dd916a2602e26f907
[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 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 berbuf   bb = BB_NULL;
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         /* normalize filter values only if necessary */
220         si->bi_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
221
222         if ( si->dbuser == NULL ) {
223                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
224                         "user name not specified "
225                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
226                 return 1;
227         }
228         
229         if ( si->subtree_cond.bv_val == NULL ) {
230                 /*
231                  * Prepare concat function for subtree search condition
232                  */
233                 struct berval   concat;
234                 struct berval   values[] = {
235                         { sizeof( "'%'" ) - 1,  "'%'" },
236                         { sizeof( "?" ) - 1,    "?" },
237                         { 0,                    NULL }
238                 };
239                 struct berbuf   bb = BB_NULL;
240
241                 if ( backsql_prepare_pattern( si->concat_func, values, 
242                                 &concat ) ) {
243                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
244                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
245                         return 1;
246                 }
247                         
248                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
249                         "subtree search SQL condition not specified "
250                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
251                         0, 0, 0);
252
253                 if ( si->upper_func.bv_val ) {
254
255                         /*
256                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
257                          */
258
259                         backsql_strfcat( &bb, "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( &bb, "lb",
274                                         (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
275                                                 "ldap_entries.dn LIKE ",
276                                         &concat );
277                 }
278
279                 si->subtree_cond = bb.bb_val;
280                         
281                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
282                         "setting '%s' as default\n",
283                         si->subtree_cond.bv_val, 0, 0 );
284         }
285
286         if ( si->children_cond.bv_val == NULL ) {
287                 struct berbuf   bb = BB_NULL;
288
289                 if ( si->upper_func.bv_val ) {
290
291                         /*
292                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
293                          */
294
295                         backsql_strfcat( &bb, "blbl",
296                                         &si->upper_func,
297                                         (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
298                                                 "(ldap_entries.dn)=",
299                                         &si->upper_func,
300                                         (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
301
302                 } else {
303
304                         /*
305                          * ldap_entries.dn LIKE CONCAT('%,',?)
306                          */
307
308                         backsql_strfcat( &bb, "l",
309                                         (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
310                                                 "ldap_entries.dn=?");
311                 }
312
313                 si->children_cond = bb.bb_val;
314                         
315                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
316                         "setting '%s' as default\n",
317                         si->children_cond.bv_val, 0, 0 );
318         }
319
320         if ( si->oc_query == NULL ) {
321                 if ( BACKSQL_CREATE_NEEDS_SELECT( si ) ) {
322                         si->oc_query =
323                                 ch_strdup( backsql_def_needs_select_oc_query );
324
325                 } else {
326                         si->oc_query = ch_strdup( backsql_def_oc_query );
327                 }
328
329                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
330                         "objectclass mapping SQL statement not specified "
331                         "(use \"oc_query\" directive in slapd.conf)\n", 
332                         0, 0, 0 );
333                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
334                         "setting '%s' by default\n", si->oc_query, 0, 0 );
335         }
336         
337         if ( si->at_query == NULL ) {
338                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
339                         "attribute mapping SQL statement not specified "
340                         "(use \"at_query\" directive in slapd.conf)\n",
341                         0, 0, 0 );
342                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
343                         "setting '%s' by default\n",
344                         backsql_def_at_query, 0, 0 );
345                 si->at_query = ch_strdup( backsql_def_at_query );
346         }
347         
348         if ( si->insentry_query == NULL ) {
349                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
350                         "entry insertion SQL statement not specified "
351                         "(use \"insentry_query\" directive in slapd.conf)\n",
352                         0, 0, 0 );
353                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
354                         "setting '%s' by default\n",
355                         backsql_def_insentry_query, 0, 0 );
356                 si->insentry_query = ch_strdup( backsql_def_insentry_query );
357         }
358         
359         if ( si->delentry_query == NULL ) {
360                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
361                         "entry deletion SQL statement not specified "
362                         "(use \"delentry_query\" directive in slapd.conf)\n",
363                         0, 0, 0 );
364                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
365                         "setting '%s' by default\n",
366                         backsql_def_delentry_query, 0, 0 );
367                 si->delentry_query = ch_strdup( backsql_def_delentry_query );
368         }
369
370         otmp.o_connid = -1;
371         otmp.o_bd = bd;
372         if ( backsql_get_db_conn( &otmp, &dbh ) != LDAP_SUCCESS ) {
373                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
374                         "connection failed, exiting\n", 0, 0, 0 );
375                 return 1;
376         }
377
378         /*
379          * Prepare ID selection query
380          */
381         si->id_query = NULL;
382         idq_len = 0;
383
384         if ( si->upper_func.bv_val == NULL ) {
385                 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
386         } else {
387                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
388                         backsql_strcat( &bb, backsql_id_query,
389                                         "dn_ru=?", NULL );
390                 } else {
391                         if ( BACKSQL_USE_REVERSE_DN( si ) ) {
392                                 backsql_strfcat( &bb, "sbl",
393                                                 backsql_id_query,
394                                                 &si->upper_func, 
395                                                 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
396                         } else {
397                                 backsql_strfcat( &bb, "sblbcb",
398                                                 backsql_id_query,
399                                                 &si->upper_func, 
400                                                 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
401                                                 &si->upper_func_open, 
402                                                 '?', 
403                                                 &si->upper_func_close );
404                         }
405                 }
406         }
407         si->id_query = bb.bb_val.bv_val;
408
409         /*
410          * Prepare children ID selection query
411          */
412         si->has_children_query = NULL;
413
414         bb.bb_val.bv_val = NULL;
415         bb.bb_val.bv_len = 0;
416         bb.bb_len = 0;
417         backsql_strfcat( &bb, "sb",
418                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
419
420                         &si->children_cond );
421         si->has_children_query = bb.bb_val.bv_val;
422  
423         backsql_free_db_conn( &otmp );
424         if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
425                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
426                         "test failed, schema map not loaded - exiting\n",
427                         0, 0, 0 );
428                 return 1;
429         }
430         
431         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
432                 "test succeeded, schema map loaded\n", 0, 0, 0 );
433         return 0;
434 }
435
436 int
437 backsql_db_close(
438         BackendDB       *bd )
439 {
440         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
441         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
442         return 0;
443 }
444
445 int
446 backsql_connection_destroy( Backend *bd, Connection *c )
447 {
448         Operation o;
449         o.o_bd = bd;
450         o.o_connid = c->c_connid;
451
452         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
453         backsql_free_db_conn( &o );
454         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
455         return 0;
456 }
457
458 #endif /* SLAPD_SQL */
459