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