]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
a5402355f26dd6ba349e58f08d5336f34ef617ed
[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 #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_chk_referrals = 0;
87         bi->bi_operational = backsql_operational;
88  
89         bi->bi_connection_init = 0;
90         bi->bi_connection_destroy = backsql_connection_destroy;
91         
92         Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
93         return 0;
94 }
95
96
97 int
98 backsql_destroy( 
99         BackendInfo     *bi )
100 {
101         Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
102         Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
103         return 0;
104 }
105
106 int
107 backsql_db_init(
108         BackendDB       *bd )
109 {
110         backsql_info *si;
111  
112         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
113         si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
114         memset( si, '\0', sizeof( backsql_info ) );
115         ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
116         ldap_pvt_thread_mutex_init( &si->schema_mutex );
117         backsql_init_db_env( si );
118
119         bd->be_private = si;
120         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
121         return 0;
122 }
123
124 int
125 backsql_db_destroy(
126         BackendDB       *bd )
127 {
128         backsql_info *si = (backsql_info*)bd->be_private;
129  
130         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
131         ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
132         backsql_free_db_env( si );
133         ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
134         ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
135         ldap_pvt_thread_mutex_lock( &si->schema_mutex );
136         backsql_destroy_schema_map( si );
137         ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
138         ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
139         free( si->dbname );
140         free( si->dbuser );
141         if ( si->dbpasswd ) {
142                 free( si->dbpasswd );
143         }
144         if ( si->dbhost ) {
145                 free( si->dbhost );
146         }
147         if ( si->upper_func.bv_val ) {
148                 free( si->upper_func.bv_val );
149                 free( si->upper_func_open.bv_val );
150                 free( si->upper_func_close.bv_val );
151         }
152         
153         free( si->subtree_cond.bv_val );
154         free( si->oc_query );
155         free( si->at_query );
156         free( si->insentry_query );
157         free( si->delentry_query );
158         free( si );
159         
160         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
161         return 0;
162 }
163
164 int
165 backsql_db_open(
166         BackendDB       *bd )
167 {
168         backsql_info    *si = (backsql_info*)bd->be_private;
169         Connection      tmp;
170         SQLHDBC         dbh;
171         ber_len_t       idq_len;
172         struct berval   bv;
173
174         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
175                 "testing RDBMS connection\n", 0, 0, 0 );
176         if ( si->dbname == NULL ) {
177                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
178                         "datasource name not specified "
179                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
180                 return 1;
181         }
182
183         if ( si->concat_func == NULL ) {
184                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
185                         "concat func not specified (use \"concat_pattern\" "
186                         "directive in slapd.conf)\n", 0, 0, 0 );
187
188                 if ( backsql_split_pattern( backsql_def_concat_func, 
189                                 &si->concat_func, 2 ) ) {
190                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
191                                 "unable to parse pattern '%s'",
192                                 backsql_def_concat_func, 0, 0 );
193                         return 1;
194                 }
195         }
196
197         /*
198          * Prepare cast string as required
199          */
200         if ( si->upper_func.bv_val ) {
201                 char buf[1024];
202
203                 if ( BACKSQL_UPPER_NEEDS_CAST( si ) ) {
204                         snprintf( buf, sizeof( buf ), 
205                                 "%s(cast (" /* ? as varchar(%d))) */ , 
206                                 si->upper_func.bv_val );
207                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
208
209                         snprintf( buf, sizeof( buf ),
210                                 /* (cast(? */ " as varchar(%d)))",
211                                 BACKSQL_MAX_DN_LEN );
212                         ber_str2bv( buf, 0, 1, &si->upper_func_close );
213
214                 } else {
215                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
216                                         si->upper_func.bv_val );
217                         ber_str2bv( buf, 0, 1, &si->upper_func_open );
218
219                         ber_str2bv( /* (? */ ")", 0, 1, &si->upper_func_close );
220                 }
221         }
222         
223         if ( si->dbuser == NULL ) {
224                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
225                         "user name not specified "
226                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
227                 return 1;
228         }
229         
230         if ( si->subtree_cond.bv_val == NULL ) {
231                 /*
232                  * Prepare concat function for subtree search condition
233                  */
234                 struct berval   concat;
235                 ber_len_t       len = 0;
236                 struct berval   values[] = {
237                         { sizeof( "'%'" ) - 1,  "'%'" },
238                         { sizeof( "?" ) - 1,    "?" },
239                         { 0,                    NULL }
240                 };
241
242                 if ( backsql_prepare_pattern( si->concat_func, values, 
243                                 &concat ) ) {
244                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
245                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
246                         return 1;
247                 }
248                         
249                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
250                         "subtree search SQL condition not specified "
251                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
252                         0, 0, 0);
253
254                 si->subtree_cond.bv_val = NULL;
255                 si->subtree_cond.bv_len = 0;
256
257                 if ( si->upper_func.bv_val ) {
258
259                         /*
260                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
261                          */
262
263                         backsql_strfcat( &si->subtree_cond, &len, "blbbb",
264                                         &si->upper_func,
265                                         (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
266                                                 "(ldap_entries.dn) LIKE ",
267                                         &si->upper_func_open,
268                                         &concat,
269                                         &si->upper_func_close );
270
271                 } else {
272
273                         /*
274                          * ldap_entries.dn LIKE CONCAT('%',?)
275                          */
276
277                         backsql_strfcat( &si->subtree_cond, &len, "lb",
278                                         (ber_len_t)sizeof( "ldap_entries.dn LIKE " ) - 1,
279                                                 "ldap_entries.dn LIKE ",
280                                         &concat );
281                 }
282                         
283                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
284                         "setting '%s' as default\n",
285                         si->subtree_cond.bv_val, 0, 0 );
286         }
287
288         if ( si->children_cond.bv_val == NULL ) {
289                 ber_len_t       len = 0;
290
291                 if ( si->upper_func.bv_val ) {
292
293                         /*
294                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
295                          */
296
297                         backsql_strfcat( &si->children_cond, &len, "blbl",
298                                         &si->upper_func,
299                                         (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
300                                                 "(ldap_entries.dn)=",
301                                         &si->upper_func,
302                                         (ber_len_t)sizeof( "(?)" ) - 1, "(?)" );
303
304                 } else {
305
306                         /*
307                          * ldap_entries.dn LIKE CONCAT('%,',?)
308                          */
309
310                         backsql_strfcat( &si->children_cond, &len, "l",
311                                         (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
312                                                 "ldap_entries.dn=?");
313                 }
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
371         tmp.c_connid =- 1;
372         if ( backsql_get_db_conn( bd, &tmp, &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         bv.bv_val = NULL;
385         bv.bv_len = 0;
386         if ( si->upper_func.bv_val == NULL ) {
387                 backsql_strcat( &bv, &idq_len, backsql_id_query, 
388                                 "dn=?", NULL );
389         } else {
390                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( si ) ) {
391                         backsql_strcat( &bv, &idq_len, backsql_id_query,
392                                         "dn_ru=?", NULL );
393                 } else {
394                         if ( BACKSQL_USE_REVERSE_DN( si ) ) {
395                                 backsql_strfcat( &bv, &idq_len, "sbl",
396                                                 backsql_id_query,
397                                                 &si->upper_func, 
398                                                 (ber_len_t)sizeof( "(dn)=?" ) - 1, "(dn)=?" );
399                         } else {
400                                 backsql_strfcat( &bv, &idq_len, "sblbcb",
401                                                 backsql_id_query,
402                                                 &si->upper_func, 
403                                                 (ber_len_t)sizeof( "(dn)=" ) - 1, "(dn)=",
404                                                 &si->upper_func_open, 
405                                                 '?', 
406                                                 &si->upper_func_close );
407                         }
408                 }
409         }
410         si->id_query = bv.bv_val;
411
412         /*
413          * Prepare children ID selection query
414          */
415         si->has_children_query = NULL;
416         idq_len = 0;
417
418         bv.bv_val = NULL;
419         bv.bv_len = 0;
420         backsql_strfcat( &bv, &idq_len, "sb",
421                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries AS subordinates WHERE subordinates.parent=ldap_entries.id AND ",
422
423                         &si->children_cond );
424         si->has_children_query = bv.bv_val;
425  
426         backsql_free_db_conn( bd, &tmp );
427         if ( !BACKSQL_SCHEMA_LOADED( si ) ) {
428                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
429                         "test failed, schema map not loaded - exiting\n",
430                         0, 0, 0 );
431                 return 1;
432         }
433         
434         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
435                 "test succeeded, schema map loaded\n", 0, 0, 0 );
436         return 0;
437 }
438
439 int
440 backsql_db_close(
441         BackendDB       *bd )
442 {
443         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
444         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
445         return 0;
446 }
447
448 int
449 backsql_connection_destroy(
450         BackendDB       *be,
451         Connection      *conn )
452 {
453         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
454         backsql_free_db_conn( be, conn );
455         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
456         return 0;
457 }
458
459 #endif /* SLAPD_SQL */
460