2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include "ac/string.h"
27 #include <sys/types.h>
31 #include "proto-sql.h"
52 backsql_info *bi = (backsql_info *)be->be_private;
54 Debug( LDAP_DEBUG_TRACE, "==>backsql_db_config()\n", 0, 0, 0 );
57 if ( !strcasecmp( argv[ 0 ], "dbhost" ) ) {
59 Debug( LDAP_DEBUG_TRACE,
60 "<==backsql_db_config (%s line %d): "
61 "missing hostname in \"dbhost\" directive\n",
65 bi->sql_dbhost = ch_strdup( argv[ 1 ] );
66 Debug( LDAP_DEBUG_TRACE,
67 "<==backsql_db_config(): hostname=%s\n",
68 bi->sql_dbhost, 0, 0 );
70 } else if ( !strcasecmp( argv[ 0 ], "dbuser" ) ) {
72 Debug( LDAP_DEBUG_TRACE,
73 "<==backsql_db_config (%s line %d): "
74 "missing username in \"dbuser\" directive\n",
78 bi->sql_dbuser = ch_strdup( argv[ 1 ] );
79 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): dbuser=%s\n",
80 bi->sql_dbuser, 0, 0 );
82 } else if ( !strcasecmp( argv[ 0 ], "dbpasswd" ) ) {
84 Debug( LDAP_DEBUG_TRACE,
85 "<==backsql_db_config (%s line %d): "
86 "missing password in \"dbpasswd\" directive\n",
90 bi->sql_dbpasswd = ch_strdup( argv[ 1 ] );
91 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
92 "dbpasswd=%s\n", /* bi->sql_dbpasswd */ "xxxx", 0, 0 );
94 } else if ( !strcasecmp( argv[ 0 ], "dbname" ) ) {
96 Debug( LDAP_DEBUG_TRACE,
97 "<==backsql_db_config (%s line %d): "
98 "missing database name in \"dbname\" "
99 "directive\n", fname, lineno, 0 );
102 bi->sql_dbname = ch_strdup( argv[ 1 ] );
103 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): dbname=%s\n",
104 bi->sql_dbname, 0, 0 );
106 } else if ( !strcasecmp( argv[ 0 ], "concat_pattern" ) ) {
108 Debug( LDAP_DEBUG_TRACE,
109 "<==backsql_db_config (%s line %d): "
111 "in \"concat_pattern\" directive\n",
115 if ( backsql_split_pattern( argv[ 1 ], &bi->sql_concat_func, 2 ) ) {
116 Debug( LDAP_DEBUG_TRACE,
117 "<==backsql_db_config (%s line %d): "
118 "unable to parse pattern \"%s\"\n"
119 "in \"concat_pattern\" directive\n",
120 fname, lineno, argv[ 1 ] );
123 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
124 "concat_pattern=\"%s\"\n", argv[ 1 ], 0, 0 );
126 } else if ( !strcasecmp( argv[ 0 ], "subtree_cond" ) ) {
128 Debug( LDAP_DEBUG_TRACE,
129 "<==backsql_db_config (%s line %d): "
130 "missing SQL condition "
131 "in \"subtree_cond\" directive\n",
135 ber_str2bv( argv[ 1 ], 0, 1, &bi->sql_subtree_cond );
136 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
137 "subtree_cond=%s\n", bi->sql_subtree_cond.bv_val, 0, 0 );
139 } else if ( !strcasecmp( argv[ 0 ], "children_cond" ) ) {
141 Debug( LDAP_DEBUG_TRACE,
142 "<==backsql_db_config (%s line %d): "
143 "missing SQL condition "
144 "in \"children_cond\" directive\n",
148 ber_str2bv( argv[ 1 ], 0, 1, &bi->sql_children_cond );
149 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
150 "subtree_cond=%s\n", bi->sql_children_cond.bv_val, 0, 0 );
152 } else if ( !strcasecmp( argv[ 0 ], "oc_query" ) ) {
154 Debug( LDAP_DEBUG_TRACE,
155 "<==backsql_db_config (%s line %d): "
156 "missing SQL statement "
157 "in \"oc_query\" directive\n",
161 bi->sql_oc_query = ch_strdup( argv[ 1 ] );
162 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
163 "oc_query=%s\n", bi->sql_oc_query, 0, 0 );
165 } else if ( !strcasecmp( argv[ 0 ], "at_query" ) ) {
167 Debug( LDAP_DEBUG_TRACE,
168 "<==backsql_db_config (%s line %d): "
169 "missing SQL statement "
170 "in \"at_query\" directive\n",
174 bi->sql_at_query = ch_strdup( argv[ 1 ] );
175 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
176 "at_query=%s\n", bi->sql_at_query, 0, 0 );
178 } else if ( !strcasecmp( argv[ 0 ], "insentry_query" ) ) {
180 Debug( LDAP_DEBUG_TRACE,
181 "<==backsql_db_config (%s line %d): "
182 "missing SQL statement "
183 "in \"insentry_query\" directive\n",
187 bi->sql_insentry_query = ch_strdup( argv[ 1 ] );
188 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
189 "insentry_query=%s\n", bi->sql_insentry_query, 0, 0 );
191 } else if ( !strcasecmp( argv[ 0 ], "create_needs_select" ) ) {
193 Debug( LDAP_DEBUG_TRACE,
194 "<==backsql_db_config (%s line %d): "
195 "missing { yes | no }"
196 "in \"create_needs_select\" directive\n",
201 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
202 bi->sql_flags |= BSQLF_CREATE_NEEDS_SELECT;
204 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
205 bi->sql_flags &= ~BSQLF_CREATE_NEEDS_SELECT;
208 Debug( LDAP_DEBUG_TRACE,
209 "<==backsql_db_config (%s line %d): "
210 "\"create_needs_select\" directive arg "
211 "must be \"yes\" or \"no\"\n",
216 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
217 "create_needs_select =%s\n",
218 BACKSQL_CREATE_NEEDS_SELECT( bi ) ? "yes" : "no",
221 } else if ( !strcasecmp( argv[ 0 ], "upper_func" ) ) {
223 Debug( LDAP_DEBUG_TRACE,
224 "<==backsql_db_config (%s line %d): "
225 "missing function name "
226 "in \"upper_func\" directive\n",
230 ber_str2bv( argv[ 1 ], 0, 1, &bi->sql_upper_func );
231 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
232 "upper_func=%s\n", bi->sql_upper_func.bv_val, 0, 0 );
234 } else if ( !strcasecmp( argv[ 0 ], "upper_needs_cast" ) ) {
236 Debug( LDAP_DEBUG_TRACE,
237 "<==backsql_db_config (%s line %d): "
238 "missing { yes | no }"
239 "in \"upper_needs_cast\" directive\n",
244 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
245 bi->sql_flags |= BSQLF_UPPER_NEEDS_CAST;
247 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
248 bi->sql_flags &= ~BSQLF_UPPER_NEEDS_CAST;
251 Debug( LDAP_DEBUG_TRACE,
252 "<==backsql_db_config (%s line %d): "
253 "\"upper_needs_cast\" directive arg "
254 "must be \"yes\" or \"no\"\n",
259 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
260 "upper_needs_cast =%s\n",
261 BACKSQL_UPPER_NEEDS_CAST( bi ) ? "yes" : "no", 0, 0 );
263 } else if ( !strcasecmp( argv[ 0 ], "strcast_func" ) ) {
265 Debug( LDAP_DEBUG_TRACE,
266 "<==backsql_db_config (%s line %d): "
267 "missing function name "
268 "in \"strcast_func\" directive\n",
272 ber_str2bv( argv[ 1 ], 0, 1, &bi->sql_strcast_func );
273 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
274 "strcast_func=%s\n", bi->sql_strcast_func.bv_val, 0, 0 );
276 } else if ( !strcasecmp( argv[ 0 ], "delentry_query" ) ) {
278 Debug( LDAP_DEBUG_TRACE,
279 "<==backsql_db_config (%s line %d): "
280 "missing SQL statement "
281 "in \"delentry_query\" directive\n",
285 bi->sql_delentry_query = ch_strdup( argv[ 1 ] );
286 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
287 "delentry_query=%s\n", bi->sql_delentry_query, 0, 0 );
289 } else if ( !strcasecmp( argv[ 0 ], "delobjclasses_query" ) ) {
291 Debug( LDAP_DEBUG_TRACE,
292 "<==backsql_db_config (%s line %d): "
293 "missing SQL statement "
294 "in \"delobjclasses_query\" directive\n",
298 bi->sql_delobjclasses_query = ch_strdup( argv[ 1 ] );
299 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
300 "delobjclasses_query=%s\n", bi->sql_delobjclasses_query, 0, 0 );
302 } else if ( !strcasecmp( argv[ 0 ], "delreferrals_query" ) ) {
304 Debug( LDAP_DEBUG_TRACE,
305 "<==backsql_db_config (%s line %d): "
306 "missing SQL statement "
307 "in \"delreferrals_query\" directive\n",
311 bi->sql_delreferrals_query = ch_strdup( argv[ 1 ] );
312 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
313 "delreferrals_query=%s\n", bi->sql_delreferrals_query, 0, 0 );
315 } else if ( !strcasecmp( argv[ 0 ], "has_ldapinfo_dn_ru") ) {
317 Debug( LDAP_DEBUG_TRACE,
318 "<==backsql_db_config (%s line %d): "
319 "missing { yes | no }"
320 "in \"has_ldapinfo_dn_ru\" directive\n",
325 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
326 bi->sql_flags |= BSQLF_HAS_LDAPINFO_DN_RU;
327 bi->sql_flags |= BSQLF_DONTCHECK_LDAPINFO_DN_RU;
329 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
330 bi->sql_flags &= ~BSQLF_HAS_LDAPINFO_DN_RU;
331 bi->sql_flags |= BSQLF_DONTCHECK_LDAPINFO_DN_RU;
334 Debug( LDAP_DEBUG_TRACE,
335 "<==backsql_db_config (%s line %d): "
336 "\"has_ldapinfo_dn_ru\" directive arg "
337 "must be \"yes\" or \"no\"\n",
342 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
343 "has_ldapinfo_dn_ru=%s\n",
344 BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ? "yes" : "no", 0, 0 );
346 } else if ( !strcasecmp( argv[ 0 ], "fail_if_no_mapping") ) {
348 Debug( LDAP_DEBUG_TRACE,
349 "<==backsql_db_config (%s line %d): "
350 "missing { yes | no }"
351 "in \"fail_if_no_mapping\" directive\n",
356 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
357 bi->sql_flags |= BSQLF_FAIL_IF_NO_MAPPING;
359 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
360 bi->sql_flags &= ~BSQLF_FAIL_IF_NO_MAPPING;
363 Debug( LDAP_DEBUG_TRACE,
364 "<==backsql_db_config (%s line %d): "
365 "\"fail_if_no_mapping\" directive arg "
366 "must be \"yes\" or \"no\"\n",
371 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
372 "fail_if_no_mapping=%s\n",
373 BACKSQL_FAIL_IF_NO_MAPPING( bi ) ? "yes" : "no", 0, 0 );
375 } else if ( !strcasecmp( argv[ 0 ], "allow_orphans") ) {
377 Debug( LDAP_DEBUG_TRACE,
378 "<==backsql_db_config (%s line %d): "
379 "missing { yes | no }"
380 "in \"allow_orphans\" directive\n",
385 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
386 bi->sql_flags |= BSQLF_ALLOW_ORPHANS;
388 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
389 bi->sql_flags &= ~BSQLF_ALLOW_ORPHANS;
392 Debug( LDAP_DEBUG_TRACE,
393 "<==backsql_db_config (%s line %d): "
394 "\"allow_orphans\" directive arg "
395 "must be \"yes\" or \"no\"\n",
400 Debug( LDAP_DEBUG_TRACE, "<==backsql_db_config(): "
401 "allow_orphans=%s\n",
402 BACKSQL_ALLOW_ORPHANS( bi ) ? "yes" : "no", 0, 0 );
404 } else if ( !strcasecmp( argv[ 0 ], "baseobject" ) ) {
405 if ( be->be_suffix == NULL ) {
406 Debug( LDAP_DEBUG_TRACE,
407 "<==backsql_db_config (%s line %d): : "
408 "must be defined after \"suffix\"\n",
413 if ( bi->sql_baseObject ) {
414 Debug( LDAP_DEBUG_TRACE,
415 "<==backsql_db_config (%s line %d): : "
416 "\"baseObject\" already provided (will be overwritten)\n",
418 entry_free( bi->sql_baseObject );
423 return create_baseObject( be, fname, lineno );
426 return read_baseObject( be, argv[ 1 ] );
429 Debug( LDAP_DEBUG_TRACE,
430 "<==backsql_db_config (%s line %d): "
432 "in \"baseObject\" directive?\n",
437 } else if ( !strcasecmp( argv[ 0 ], "sqllayer") ) {
438 if ( backsql_api_config( bi, argv[ 1 ] ) ) {
439 Debug( LDAP_DEBUG_TRACE,
440 "<==backsql_db_config (%s line %d): "
441 "unable to load sqllayer \"%s\"\n",
442 fname, lineno, argv[ 1 ] );
447 return SLAP_CONF_UNKNOWN;
454 * Read the entries specified in fname and merge the attributes
455 * to the user defined baseObject entry. Note that if we find any errors
456 * what so ever, we will discard the entire entries, print an
457 * error message and return.
464 backsql_info *bi = (backsql_info *)be->be_private;
466 int rc = 0, lineno = 0, lmax = 0;
471 fp = fopen( fname, "r" );
473 Debug( LDAP_DEBUG_ANY,
474 "could not open back-sql baseObject attr file \"%s\" - absolute path?\n",
480 bi->sql_baseObject = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
481 if ( bi->sql_baseObject == NULL ) {
482 Debug( LDAP_DEBUG_ANY,
483 "read_baseObject_file: SLAP_CALLOC failed", 0, 0, 0 );
485 return LDAP_NO_MEMORY;
487 bi->sql_baseObject->e_name = be->be_suffix[0];
488 bi->sql_baseObject->e_nname = be->be_nsuffix[0];
489 bi->sql_baseObject->e_attrs = NULL;
491 while ( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
492 Entry *e = str2entry( buf );
496 fprintf( stderr, "back-sql baseObject: could not parse entry (line=%d)\n",
502 /* make sure the DN is the database's suffix */
503 if ( !be_issuffix( be, &e->e_nname ) ) {
505 "back-sql: invalid baseObject - dn=\"%s\" (line=%d)\n",
513 * we found a valid entry, so walk thru all the attributes in the
514 * entry, and add each attribute type and description to baseObject
516 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
517 if ( attr_merge( bi->sql_baseObject, a->a_desc, a->a_vals,
518 ( a->a_nvals == a->a_vals ) ? NULL : a->a_nvals ) )
532 entry_free( bi->sql_baseObject );
533 bi->sql_baseObject = NULL;
540 Debug( LDAP_DEBUG_CONFIG, "back-sql baseObject file \"%s\" read.\n", fname, 0, 0 );
551 backsql_info *bi = (backsql_info *)be->be_private;
557 snprintf( buf, sizeof(buf),
559 "objectClass: extensibleObject\n"
560 "description: builtin baseObject for back-sql\n"
561 "description: all entries mapped in the \"ldap_entries\" table\n"
562 "description: must have \"" BACKSQL_BASEOBJECT_IDSTR "\" "
563 "in the \"parent\" column",
564 be->be_suffix[0].bv_val );
566 bi->sql_baseObject = str2entry( buf );
567 if ( bi->sql_baseObject == NULL ) {
568 Debug( LDAP_DEBUG_TRACE,
569 "<==backsql_db_config (%s line %d): "
570 "unable to parse baseObject entry\n",
575 if ( BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ) {
579 rc = ldap_bv2rdn( &be->be_suffix[ 0 ], &rdn, (char **) &p, LDAP_DN_FORMAT_LDAP );
580 if ( rc != LDAP_SUCCESS ) {
581 snprintf( buf, sizeof(buf),
582 "unable to extract RDN from baseObject DN \"%s\" (%d: %s)",
583 be->be_suffix[ 0 ].bv_val, rc, ldap_err2string( rc ) );
584 Debug( LDAP_DEBUG_TRACE,
585 "<==backsql_db_config (%s line %d): %s\n",
586 fname, lineno, buf );
590 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
591 LDAPAVA *ava = rdn[ iAVA ];
592 AttributeDescription *ad = NULL;
593 slap_syntax_transform_func *transf = NULL;
594 struct berval bv = BER_BVNULL;
595 const char *text = NULL;
599 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
600 if ( rc != LDAP_SUCCESS ) {
601 snprintf( buf, sizeof(buf),
602 "AttributeDescription of naming "
603 "attribute #%d from baseObject "
605 iAVA, be->be_suffix[ 0 ].bv_val,
606 rc, ldap_err2string( rc ) );
607 Debug( LDAP_DEBUG_TRACE,
608 "<==backsql_db_config (%s line %d): %s\n",
609 fname, lineno, buf );
613 transf = ad->ad_type->sat_syntax->ssyn_pretty;
616 * transform value by pretty function
617 * if value is empty, use empty_bv
619 rc = ( *transf )( ad->ad_type->sat_syntax,
622 : (struct berval *) &slap_empty_bv,
625 if ( rc != LDAP_SUCCESS ) {
626 snprintf( buf, sizeof(buf),
627 "prettying of attribute #%d from baseObject "
628 "DN \"%s\" failed: %d: %s",
629 iAVA, be->be_suffix[ 0 ].bv_val,
630 rc, ldap_err2string( rc ) );
631 Debug( LDAP_DEBUG_TRACE,
632 "<==backsql_db_config (%s line %d): %s\n",
633 fname, lineno, buf );
638 if ( !BER_BVISNULL( &bv ) ) {
639 if ( ava->la_flags & LDAP_AVA_FREE_VALUE ) {
640 ber_memfree( ava->la_value.bv_val );
643 ava->la_flags |= LDAP_AVA_FREE_VALUE;
646 attr_merge_normalize_one( bi->sql_baseObject,
647 ad, &ava->la_value, NULL );
655 #endif /* SLAPD_SQL */