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