]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
3ebdfea0a93e67154ca3e73eed0d8e828253482b
[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-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "slap.h"
30 #include "proto-sql.h"
31
32 int
33 sql_back_initialize(
34         BackendInfo     *bi )
35
36         static char *controls[] = {
37                 LDAP_CONTROL_ASSERT,
38                 LDAP_CONTROL_MANAGEDSAIT,
39 #if 0 /* needs improvements */
40                 LDAP_CONTROL_NOOP,
41 #endif
42 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
43                 LDAP_CONTROL_VALUESRETURNFILTER,
44 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
45                 NULL
46         };
47
48         bi->bi_controls = controls;
49
50         Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
51         
52         bi->bi_db_init = backsql_db_init;
53         bi->bi_db_config = backsql_db_config;
54         bi->bi_db_open = backsql_db_open;
55         bi->bi_db_close = backsql_db_close;
56         bi->bi_db_destroy = backsql_db_destroy;
57
58         bi->bi_op_abandon = 0;
59         bi->bi_op_compare = backsql_compare;
60         bi->bi_op_bind = backsql_bind;
61         bi->bi_op_unbind = 0;
62         bi->bi_op_search = backsql_search;
63         bi->bi_op_modify = backsql_modify;
64         bi->bi_op_modrdn = backsql_modrdn;
65         bi->bi_op_add = backsql_add;
66         bi->bi_op_delete = backsql_delete;
67         
68         bi->bi_chk_referrals = 0;
69         bi->bi_operational = backsql_operational;
70         bi->bi_entry_get_rw = backsql_entry_get;
71  
72         bi->bi_connection_init = 0;
73         bi->bi_connection_destroy = backsql_connection_destroy;
74
75         Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
76         return 0;
77 }
78
79 int
80 backsql_destroy( 
81         BackendInfo     *bi )
82 {
83         Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
84         Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
85         return 0;
86 }
87
88 int
89 backsql_db_init(
90         BackendDB       *bd )
91 {
92         backsql_info    *bi;
93  
94         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
95         bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
96         memset( bi, '\0', sizeof( backsql_info ) );
97         ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
98         ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
99         backsql_init_db_env( bi );
100
101         bd->be_private = bi;
102         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
103         return 0;
104 }
105
106 int
107 backsql_db_destroy(
108         BackendDB       *bd )
109 {
110         backsql_info    *bi = (backsql_info*)bd->be_private;
111  
112         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
113         ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
114         backsql_free_db_env( bi );
115         ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
116         ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
117         ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
118         backsql_destroy_schema_map( bi );
119         ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
120         ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
121         free( bi->sql_dbname );
122         free( bi->sql_dbuser );
123         if ( bi->sql_dbpasswd ) {
124                 free( bi->sql_dbpasswd );
125         }
126         if ( bi->sql_dbhost ) {
127                 free( bi->sql_dbhost );
128         }
129         if ( bi->sql_upper_func.bv_val ) {
130                 free( bi->sql_upper_func.bv_val );
131                 free( bi->sql_upper_func_open.bv_val );
132                 free( bi->sql_upper_func_close.bv_val );
133         }
134         
135         free( bi->sql_subtree_cond.bv_val );
136         free( bi->sql_oc_query );
137         free( bi->sql_at_query );
138         free( bi->sql_insentry_stmt );
139         free( bi->sql_delentry_stmt );
140         free( bi->sql_delobjclasses_stmt );
141         free( bi->sql_delreferrals_stmt );
142
143         if ( bi->sql_anlist ) {
144                 int     i;
145
146                 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[i].an_name ); i++ )
147                 {
148                         ch_free( bi->sql_anlist[i].an_name.bv_val );
149                 }
150                 ch_free( bi->sql_anlist );
151         }
152
153         if ( bi->sql_baseObject ) {
154                 entry_free( bi->sql_baseObject );
155         }
156         
157         free( bi );
158         
159         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
160         return 0;
161 }
162
163 int
164 backsql_db_open(
165         BackendDB       *bd )
166 {
167         backsql_info    *bi = (backsql_info*)bd->be_private;
168         SQLHDBC         dbh = SQL_NULL_HDBC;
169         struct berbuf   bb = BB_NULL;
170
171         char            opbuf[ OPERATION_BUFFER_SIZE ];
172         Operation*      op = (Operation *)opbuf;
173         
174         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
175                 "testing RDBMS connection\n", 0, 0, 0 );
176         if ( bi->sql_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 ( bi->sql_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                                 &bi->sql_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 ( bi->sql_upper_func.bv_val ) {
201                 char buf[1024];
202
203                 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
204                         snprintf( buf, sizeof( buf ), 
205                                 "%s(cast (" /* ? as varchar(%d))) */ , 
206                                 bi->sql_upper_func.bv_val );
207                         ber_str2bv( buf, 0, 1, &bi->sql_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, &bi->sql_upper_func_close );
213
214                 } else {
215                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
216                                         bi->sql_upper_func.bv_val );
217                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
218
219                         ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
220                 }
221         }
222
223         /* normalize filter values only if necessary */
224         bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
225         assert( bi->sql_caseIgnoreMatch );
226
227         bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
228         assert( bi->sql_telephoneNumberMatch );
229
230         if ( bi->sql_dbuser == NULL ) {
231                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
232                         "user name not specified "
233                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
234                 return 1;
235         }
236         
237         if ( bi->sql_subtree_cond.bv_val == NULL ) {
238                 /*
239                  * Prepare concat function for subtree search condition
240                  */
241                 struct berval   concat;
242                 struct berval   values[] = {
243                         BER_BVC( "'%'" ),
244                         BER_BVC( "?" ),
245                         BER_BVNULL
246                 };
247                 struct berbuf   bb = BB_NULL;
248
249                 if ( backsql_prepare_pattern( bi->sql_concat_func, values, 
250                                 &concat ) ) {
251                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
252                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
253                         return 1;
254                 }
255                         
256                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
257                         "subtree search SQL condition not specified "
258                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
259                         0, 0, 0);
260
261                 if ( bi->sql_upper_func.bv_val ) {
262
263                         /*
264                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
265                          */
266
267                         backsql_strfcat( &bb, "blbbb",
268                                         &bi->sql_upper_func,
269                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
270                                                 "(ldap_entries.dn) LIKE ",
271                                         &bi->sql_upper_func_open,
272                                         &concat,
273                                         &bi->sql_upper_func_close );
274
275                 } else {
276
277                         /*
278                          * ldap_entries.dn LIKE CONCAT('%',?)
279                          */
280
281                         backsql_strfcat( &bb, "lb",
282                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
283                                                 "ldap_entries.dn LIKE ",
284                                         &concat );
285                 }
286
287                 bi->sql_subtree_cond = bb.bb_val;
288                         
289                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
290                         "setting \"%s\" as default\n",
291                         bi->sql_subtree_cond.bv_val, 0, 0 );
292         }
293
294         if ( bi->sql_children_cond.bv_val == NULL ) {
295                 struct berbuf   bb = BB_NULL;
296
297                 if ( bi->sql_upper_func.bv_val ) {
298
299                         /*
300                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
301                          */
302
303                         backsql_strfcat( &bb, "blbl",
304                                         &bi->sql_upper_func,
305                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
306                                                 "(ldap_entries.dn)=",
307                                         &bi->sql_upper_func,
308                                         (ber_len_t)STRLENOF( "(?)" ), "(?)" );
309
310                 } else {
311
312                         /*
313                          * ldap_entries.dn LIKE CONCAT('%,',?)
314                          */
315
316                         backsql_strfcat( &bb, "l",
317                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
318                                                 "ldap_entries.dn=?");
319                 }
320
321                 bi->sql_children_cond = bb.bb_val;
322                         
323                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
324                         "setting \"%s\" as default\n",
325                         bi->sql_children_cond.bv_val, 0, 0 );
326         }
327
328         if ( bi->sql_oc_query == NULL ) {
329                 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
330                         bi->sql_oc_query =
331                                 ch_strdup( backsql_def_needs_select_oc_query );
332
333                 } else {
334                         bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
335                 }
336
337                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
338                         "objectclass mapping SQL statement not specified "
339                         "(use \"oc_query\" directive in slapd.conf)\n", 
340                         0, 0, 0 );
341                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
342                         "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
343         }
344         
345         if ( bi->sql_at_query == NULL ) {
346                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
347                         "attribute mapping SQL statement not specified "
348                         "(use \"at_query\" directive in slapd.conf)\n",
349                         0, 0, 0 );
350                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
351                         "setting \"%s\" by default\n",
352                         backsql_def_at_query, 0, 0 );
353                 bi->sql_at_query = ch_strdup( backsql_def_at_query );
354         }
355         
356         if ( bi->sql_insentry_stmt == NULL ) {
357                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
358                         "entry insertion SQL statement not specified "
359                         "(use \"insentry_stmt\" directive in slapd.conf)\n",
360                         0, 0, 0 );
361                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
362                         "setting \"%s\" by default\n",
363                         backsql_def_insentry_stmt, 0, 0 );
364                 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
365         }
366         
367         if ( bi->sql_delentry_stmt == NULL ) {
368                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
369                         "entry deletion SQL statement not specified "
370                         "(use \"delentry_stmt\" directive in slapd.conf)\n",
371                         0, 0, 0 );
372                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
373                         "setting \"%s\" by default\n",
374                         backsql_def_delentry_stmt, 0, 0 );
375                 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
376         }
377
378         if ( bi->sql_delobjclasses_stmt == NULL ) {
379                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
380                         "objclasses deletion SQL statement not specified "
381                         "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
382                         0, 0, 0 );
383                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
384                         "setting \"%s\" by default\n",
385                         backsql_def_delobjclasses_stmt, 0, 0 );
386                 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
387         }
388
389         if ( bi->sql_delreferrals_stmt == NULL ) {
390                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
391                         "referrals deletion SQL statement not specified "
392                         "(use \"delreferrals_stmt\" directive in slapd.conf)\n",
393                         0, 0, 0 );
394                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
395                         "setting \"%s\" by default\n",
396                         backsql_def_delreferrals_stmt, 0, 0 );
397                 bi->sql_delreferrals_stmt = ch_strdup( backsql_def_delreferrals_stmt );
398         }
399
400         op->o_hdr = (Opheader *)&op[ 1 ];
401         op->o_connid = (unsigned long)(-1);
402         op->o_bd = bd;
403         if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
404                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
405                         "connection failed, exiting\n", 0, 0, 0 );
406                 return 1;
407         }
408
409         /*
410          * Prepare ID selection query
411          */
412         if ( bi->sql_id_query == NULL ) {
413                 /* no custom id_query provided */
414                 if ( bi->sql_upper_func.bv_val == NULL ) {
415                         backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
416
417                 } else {
418                         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
419                                 backsql_strcat( &bb, backsql_id_query,
420                                                 "dn_ru=?", NULL );
421                         } else {
422                                 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
423                                         backsql_strfcat( &bb, "sbl",
424                                                         backsql_id_query,
425                                                         &bi->sql_upper_func, 
426                                                         (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
427                                 } else {
428                                         backsql_strfcat( &bb, "sblbcb",
429                                                         backsql_id_query,
430                                                         &bi->sql_upper_func, 
431                                                         (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
432                                                         &bi->sql_upper_func_open, 
433                                                         '?', 
434                                                         &bi->sql_upper_func_close );
435                                 }
436                         }
437                 }
438                 bi->sql_id_query = bb.bb_val.bv_val;
439         }
440
441         /*
442          * Prepare children ID selection query
443          */
444         bi->sql_has_children_query = NULL;
445
446         bb.bb_val.bv_val = NULL;
447         bb.bb_val.bv_len = 0;
448         bb.bb_len = 0;
449         backsql_strfcat( &bb, "sb",
450                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
451
452                         &bi->sql_children_cond );
453         bi->sql_has_children_query = bb.bb_val.bv_val;
454  
455         backsql_free_db_conn( op );
456         if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
457                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
458                         "test failed, schema map not loaded - exiting\n",
459                         0, 0, 0 );
460                 return 1;
461         }
462
463         /* should never happen! */
464         assert( bd->be_nsuffix != NULL );
465         
466         if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
467                 /* enable if only one suffix is defined */
468                 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
469         }
470         
471         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
472                 "test succeeded, schema map loaded\n", 0, 0, 0 );
473         return 0;
474 }
475
476 int
477 backsql_db_close(
478         BackendDB       *bd )
479 {
480         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
481         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
482         return 0;
483 }
484
485 int
486 backsql_connection_destroy( Backend *bd, Connection *c )
487 {
488         char            opbuf[ OPERATION_BUFFER_SIZE ];
489         Operation*      op = (Operation *)opbuf;
490
491         op->o_hdr = (Opheader *)&op[ 1 ];
492         op->o_connid = c->c_connid;
493         op->o_bd = bd;
494
495         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
496         backsql_free_db_conn( op );
497         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
498
499         return 0;
500 }
501
502 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
503
504 /* conditionally define the init_module() function */
505 SLAP_BACKEND_INIT_MODULE( sql )
506
507 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
508