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