2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/string.h>
28 #include <ac/socket.h>
32 #include "../back-ldap/back-ldap.h"
33 #undef ldap_debug /* silence a warning in ldap-int.h */
34 #include "../../../libraries/libldap/ldap-int.h"
35 #include "back-meta.h"
41 struct ldapmapping *mapping;
47 mt = ch_calloc( sizeof( metatarget_t ), 1 );
49 mt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
50 if ( mt->mt_rwmap.rwm_rw == NULL ) {
57 * the filter rewrite as a string must be disabled
58 * by default; it can be re-enabled by adding rules;
59 * this creates an empty rewriteContext
61 rargv[ 0 ] = "rewriteContext";
62 rargv[ 1 ] = "searchFilter";
64 rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
66 rargv[ 0 ] = "rewriteContext";
67 rargv[ 1 ] = "default";
69 rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
71 ldap_back_map_init( &mt->mt_rwmap.rwm_at, &mapping );
73 ldap_pvt_thread_mutex_init( &mt->mt_uri_mutex );
81 check_true_false( char *str )
83 if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
87 if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
104 metainfo_t *mi = ( metainfo_t * )be->be_private;
106 assert( mi != NULL );
108 /* URI of server to query */
109 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
110 int i = mi->mi_ntargets;
113 #endif /* uncomment if uri MUST be a branch of suffix */
114 LDAPURLDesc *ludp, *tmpludp;
123 Debug( LDAP_DEBUG_ANY,
124 "%s: line %d: missing URI "
125 "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
133 Debug( LDAP_DEBUG_ANY,
134 "%s: line %d: too many args "
135 "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
140 if ( be->be_nsuffix == NULL ) {
141 Debug( LDAP_DEBUG_ANY,
142 "%s: line %d: the suffix must be defined before any target.\n",
149 mi->mi_targets = ( metatarget_t ** )ch_realloc( mi->mi_targets,
150 sizeof( metatarget_t * ) * mi->mi_ntargets );
151 if ( mi->mi_targets == NULL ) {
152 Debug( LDAP_DEBUG_ANY,
153 "%s: line %d: out of memory while storing server name"
154 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
159 if ( meta_back_new_target( &mi->mi_targets[ i ] ) != 0 ) {
160 Debug( LDAP_DEBUG_ANY,
161 "%s: line %d: unable to init server"
162 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
167 mt = mi->mi_targets[ i ];
169 mt->mt_rebind_f = mi->mi_rebind_f;
170 mt->mt_urllist_f = mi->mi_urllist_f;
171 mt->mt_urllist_p = mt;
173 mt->mt_nretries = mi->mi_nretries;
174 mt->mt_quarantine = mi->mi_quarantine;
175 if ( META_BACK_QUARANTINE( mi ) ) {
176 ldap_pvt_thread_mutex_init( &mt->mt_quarantine_mutex );
178 mt->mt_flags = mi->mi_flags;
179 mt->mt_version = mi->mi_version;
180 mt->mt_network_timeout = mi->mi_network_timeout;
181 mt->mt_bind_timeout = mi->mi_bind_timeout;
182 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
183 mt->mt_timeout[ c ] = mi->mi_timeout[ c ];
189 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t",
190 LDAP_PVT_URL_PARSE_NONE ) != LDAP_SUCCESS )
192 Debug( LDAP_DEBUG_ANY,
193 "%s: line %d: unable to parse URI"
194 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
200 * uri MUST have the <dn> part!
202 if ( ludp->lud_dn == NULL ) {
203 Debug( LDAP_DEBUG_ANY,
204 "%s: line %d: missing <naming context> "
205 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
209 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
212 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
213 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
218 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
219 Debug( LDAP_DEBUG_ANY,
220 "%s: line %d: missing <naming context> "
221 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
228 * copies and stores uri and suffix
230 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
231 rc = dnPrettyNormal( NULL, &dn, &mt->mt_psuffix,
232 &mt->mt_nsuffix, NULL );
233 if( rc != LDAP_SUCCESS ) {
234 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
235 "target \"%s\" DN is invalid\n",
236 fname, lineno, argv[ 1 ] );
240 ludp->lud_dn[ 0 ] = '\0';
242 switch ( ludp->lud_scope ) {
243 case LDAP_SCOPE_DEFAULT:
244 mt->mt_scope = LDAP_SCOPE_SUBTREE;
247 case LDAP_SCOPE_SUBTREE:
248 case LDAP_SCOPE_SUBORDINATE:
249 mt->mt_scope = ludp->lud_scope;
253 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
254 "invalid scope for target \"%s\"\n",
255 fname, lineno, argv[ 1 ] );
259 /* check all, to apply the scope check on the first one */
260 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
261 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
262 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
263 "multiple URIs must have "
271 mt->mt_uri = ldap_url_list2urls( ludp );
272 ldap_free_urllist( ludp );
273 if ( mt->mt_uri == NULL) {
274 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
280 * uri MUST be a branch of suffix!
282 #if 0 /* too strict a constraint */
283 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) != be ) {
284 Debug( LDAP_DEBUG_ANY,
285 "%s: line %d: <naming context> of URI does not refer to current backend"
286 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
292 * uri MUST be a branch of a suffix!
294 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) == NULL ) {
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: <naming context> of URI does not resolve to a backend"
297 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
303 /* subtree-exclude */
304 } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
305 int i = mi->mi_ntargets - 1;
306 struct berval dn, ndn;
309 Debug( LDAP_DEBUG_ANY,
310 "%s: line %d: need \"uri\" directive first\n",
317 Debug( LDAP_DEBUG_ANY,
318 "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
326 Debug( LDAP_DEBUG_ANY,
327 "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
332 ber_str2bv( argv[ 1 ], 0, 0, &dn );
333 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
336 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
337 "subtree-exclude DN=\"%s\" is invalid\n",
338 fname, lineno, argv[ 1 ] );
342 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_nsuffix ) ) {
343 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
344 "subtree-exclude DN=\"%s\" "
345 "must be subtree of target\n",
346 fname, lineno, argv[ 1 ] );
347 ber_memfree( ndn.bv_val );
351 if ( mi->mi_targets[ i ]->mt_subtree_exclude != NULL ) {
354 for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ); j++ )
356 if ( dnIsSuffix( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ], &ndn ) ) {
357 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
358 "subtree-exclude DN=\"%s\" "
359 "is suffix of another subtree-exclude\n",
360 fname, lineno, argv[ 1 ] );
361 /* reject, because it might be superior
362 * to more than one subtree-exclude */
363 ber_memfree( ndn.bv_val );
366 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ) ) {
367 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
368 "another subtree-exclude is suffix of "
369 "subtree-exclude DN=\"%s\"\n",
370 fname, lineno, argv[ 1 ] );
371 ber_memfree( ndn.bv_val );
377 ber_bvarray_add( &mi->mi_targets[ i ]->mt_subtree_exclude, &ndn );
379 /* default target directive */
380 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
381 int i = mi->mi_ntargets - 1;
385 Debug( LDAP_DEBUG_ANY,
386 "%s: line %d: \"default-target\" alone need be"
387 " inside a \"uri\" directive\n",
391 mi->mi_defaulttarget = i;
394 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
396 Debug( LDAP_DEBUG_ANY,
397 "%s: line %d: \"default-target none\""
398 " should go before uri definitions\n",
401 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
405 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
406 || mi->mi_defaulttarget < 0
407 || mi->mi_defaulttarget >= i - 1 )
409 Debug( LDAP_DEBUG_ANY,
410 "%s: line %d: illegal target number %d\n",
411 fname, lineno, mi->mi_defaulttarget );
417 /* ttl of dn cache */
418 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
420 Debug( LDAP_DEBUG_ANY,
421 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
426 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
427 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
429 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
430 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
435 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
436 Debug( LDAP_DEBUG_ANY,
437 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
438 fname, lineno, argv[ 1 ] );
441 mi->mi_cache.ttl = (time_t)t;
444 /* network timeout when connecting to ldap servers */
445 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
447 time_t *tp = mi->mi_ntargets ?
448 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_network_timeout
449 : &mi->mi_network_timeout;
452 Debug( LDAP_DEBUG_ANY,
453 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
458 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
459 Debug( LDAP_DEBUG_ANY,
460 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
461 fname, lineno, argv[ 1 ] );
468 /* idle timeout when connecting to ldap servers */
469 } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
474 Debug( LDAP_DEBUG_ANY,
475 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
481 Debug( LDAP_DEBUG_ANY,
482 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
487 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
488 Debug( LDAP_DEBUG_ANY,
489 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
490 fname, lineno, argv[ 1 ] );
495 mi->mi_idle_timeout = (time_t)t;
498 } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
503 Debug( LDAP_DEBUG_ANY,
504 "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
510 Debug( LDAP_DEBUG_ANY,
511 "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
516 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
517 Debug( LDAP_DEBUG_ANY,
518 "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
519 fname, lineno, argv[ 1 ] );
524 mi->mi_conn_ttl = (time_t)t;
526 /* bind timeout when connecting to ldap servers */
527 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
529 struct timeval *tp = mi->mi_ntargets ?
530 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_bind_timeout
531 : &mi->mi_bind_timeout;
535 Debug( LDAP_DEBUG_ANY,
536 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
542 Debug( LDAP_DEBUG_ANY,
543 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
548 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
549 Debug( LDAP_DEBUG_ANY,
550 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
551 fname, lineno, argv[ 1 ] );
556 tp->tv_sec = t/1000000;
557 tp->tv_usec = t%1000000;
559 /* name to use for meta_back_group */
560 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
561 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
563 int i = mi->mi_ntargets - 1;
567 Debug( LDAP_DEBUG_ANY,
568 "%s: line %d: need \"uri\" directive first\n",
574 Debug( LDAP_DEBUG_ANY,
575 "%s: line %d: missing name in \"binddn <name>\" line\n",
580 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
581 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
582 "\"binddn\" statement is deprecated; "
583 "use \"acl-authcDN\" instead\n",
585 /* FIXME: some day we'll need to throw an error */
588 ber_str2bv( argv[ 1 ], 0, 0, &dn );
589 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ]->mt_binddn,
590 NULL ) != LDAP_SUCCESS )
592 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
593 "bind DN '%s' is invalid\n",
594 fname, lineno, argv[ 1 ] );
598 /* password to use for meta_back_group */
599 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
600 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
602 int i = mi->mi_ntargets - 1;
605 Debug( LDAP_DEBUG_ANY,
606 "%s: line %d: need \"uri\" directive first\n",
612 Debug( LDAP_DEBUG_ANY,
613 "%s: line %d: missing password in \"bindpw <password>\" line\n",
618 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
619 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
620 "\"bindpw\" statement is deprecated; "
621 "use \"acl-passwd\" instead\n",
623 /* FIXME: some day we'll need to throw an error */
626 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_bindpw );
628 /* save bind creds for referral rebinds? */
629 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
631 Debug( LDAP_DEBUG_ANY,
632 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
638 Debug( LDAP_DEBUG_ANY,
639 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
641 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
644 switch ( check_true_false( argv[ 1 ] ) ) {
646 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
650 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
654 Debug( LDAP_DEBUG_ANY,
655 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
656 fname, lineno, argv[ 1 ] );
661 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
662 unsigned *flagsp = mi->mi_ntargets ?
663 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
667 Debug( LDAP_DEBUG_ANY,
668 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
673 /* this is the default; we add it because the default might change... */
674 switch ( check_true_false( argv[ 1 ] ) ) {
676 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
680 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
684 Debug( LDAP_DEBUG_ANY,
685 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
686 fname, lineno, argv[ 1 ] );
690 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
691 unsigned *flagsp = mi->mi_ntargets ?
692 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
696 Debug( LDAP_DEBUG_ANY,
697 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
703 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
704 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
707 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
708 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
709 *flagsp |= LDAP_BACK_F_USE_TLS;
711 /* propagate start tls */
712 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
713 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
716 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
717 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
718 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
721 Debug( LDAP_DEBUG_ANY,
722 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
723 fname, lineno, argv[ 1 ] );
727 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
728 unsigned *flagsp = mi->mi_ntargets ?
729 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
733 Debug( LDAP_DEBUG_ANY,
734 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
739 switch ( check_true_false( argv[ 1 ] ) ) {
741 *flagsp &= ~LDAP_BACK_F_T_F_MASK2;
745 *flagsp |= LDAP_BACK_F_T_F;
749 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
750 *flagsp |= LDAP_BACK_F_T_F_DISCOVER;
753 Debug( LDAP_DEBUG_ANY,
754 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
755 fname, lineno, argv[ 1 ] );
762 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
764 Debug( LDAP_DEBUG_ANY,
765 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
770 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
771 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
773 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
774 mi->mi_flags |= META_BACK_F_ONERR_STOP;
777 Debug( LDAP_DEBUG_ANY,
778 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
779 fname, lineno, argv[ 1 ] );
784 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
786 Debug( LDAP_DEBUG_ANY,
787 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
792 switch ( check_true_false( argv[ 1 ] ) ) {
794 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
798 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
802 Debug( LDAP_DEBUG_ANY,
803 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
804 fname, lineno, argv[ 1 ] );
809 } else if ( strcasecmp( argv[ 0 ], "single-conn" ) == 0 ) {
811 Debug( LDAP_DEBUG_ANY,
812 "%s: line %d: \"single-conn {FALSE|true}\" takes 1 argument\n",
817 if ( mi->mi_ntargets > 0 ) {
818 Debug( LDAP_DEBUG_ANY,
819 "%s: line %d: \"single-conn\" must appear before target definitions\n",
824 switch ( check_true_false( argv[ 1 ] ) ) {
826 mi->mi_flags &= ~LDAP_BACK_F_SINGLECONN;
830 mi->mi_flags |= LDAP_BACK_F_SINGLECONN;
834 Debug( LDAP_DEBUG_ANY,
835 "%s: line %d: \"single-conn {FALSE|true}\": invalid arg \"%s\".\n",
836 fname, lineno, argv[ 1 ] );
840 } else if ( strcasecmp( argv[ 0 ], "cancel" ) == 0 ) {
842 unsigned *flagsp = mi->mi_ntargets ?
843 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
847 Debug( LDAP_DEBUG_ANY,
848 "%s: line %d: \"cancel {abandon|ignore|exop}\" takes 1 argument\n",
853 if ( strcasecmp( argv[ 1 ], "abandon" ) == 0 ) {
854 flag = LDAP_BACK_F_CANCEL_ABANDON;
856 } else if ( strcasecmp( argv[ 1 ], "ignore" ) == 0 ) {
857 flag = LDAP_BACK_F_CANCEL_IGNORE;
859 } else if ( strcasecmp( argv[ 1 ], "exop" ) == 0 ) {
860 flag = LDAP_BACK_F_CANCEL_EXOP;
862 } else if ( strcasecmp( argv[ 1 ], "exop-discover" ) == 0 ) {
863 flag = LDAP_BACK_F_CANCEL_EXOP_DISCOVER;
866 Debug( LDAP_DEBUG_ANY,
867 "%s: line %d: \"cancel {abandon|ignore|exop[-discover]}\": unknown mode \"%s\" \n",
868 fname, lineno, argv[ 1 ] );
872 *flagsp &= ~LDAP_BACK_F_CANCEL_MASK2;
875 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
877 time_t *tv = mi->mi_ntargets ?
878 mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_timeout
883 Debug( LDAP_DEBUG_ANY,
884 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
889 for ( c = 1; c < argc; c++ ) {
893 sep = strchr( argv[ c ], '=' );
895 size_t len = sep - argv[ c ];
897 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
898 t = &tv[ LDAP_BACK_OP_ADD ];
899 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
900 t = &tv[ LDAP_BACK_OP_DELETE ];
901 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
902 t = &tv[ LDAP_BACK_OP_MODIFY ];
903 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
904 t = &tv[ LDAP_BACK_OP_MODRDN ];
906 char buf[ SLAP_TEXT_BUFLEN ];
907 snprintf( buf, sizeof( buf ),
908 "unknown operation \"%s\" for timeout #%d",
910 Debug( LDAP_DEBUG_ANY,
911 "%s: line %d: %s.\n",
912 fname, lineno, buf );
921 if ( lutil_parse_time( sep, &val ) != 0 ) {
922 Debug( LDAP_DEBUG_ANY,
923 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
924 fname, lineno, sep );
934 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
935 tv[ i ] = (time_t)val;
940 /* name to use as pseudo-root dn */
941 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
942 int i = mi->mi_ntargets - 1;
946 Debug( LDAP_DEBUG_ANY,
947 "%s: line %d: need \"uri\" directive first\n",
953 Debug( LDAP_DEBUG_ANY,
954 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
959 dn.bv_val = argv[ 1 ];
960 dn.bv_len = strlen( argv[ 1 ] );
961 if ( dnNormalize( 0, NULL, NULL, &dn,
962 &mi->mi_targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
964 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
965 "pseudoroot DN '%s' is invalid\n",
966 fname, lineno, argv[ 1 ] );
970 /* password to use as pseudo-root */
971 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
972 int i = mi->mi_ntargets - 1;
975 Debug( LDAP_DEBUG_ANY,
976 "%s: line %d: need \"uri\" directive first\n",
982 Debug( LDAP_DEBUG_ANY,
983 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
987 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_pseudorootpw );
990 } else if ( strcasecmp( argv[ 0 ], "quarantine" ) == 0 ) {
991 char buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
992 slap_retry_info_t *ri = mi->mi_ntargets ?
993 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine
994 : &mi->mi_quarantine;
996 if ( ( mi->mi_ntargets == 0 && META_BACK_QUARANTINE( mi ) )
997 || ( mi->mi_ntargets > 0 && META_BACK_TGT_QUARANTINE( mi->mi_targets[ mi->mi_ntargets - 1 ] ) ) )
999 Debug( LDAP_DEBUG_ANY,
1000 "%s: line %d: quarantine already defined.\n",
1010 Debug( LDAP_DEBUG_ANY,
1011 "%s: line %d: missing arg in \"quarantine <pattern list>\".\n",
1016 Debug( LDAP_DEBUG_ANY,
1017 "%s: line %d: extra cruft after \"quarantine <pattern list>\".\n",
1022 if ( ri != &mi->mi_quarantine ) {
1023 ri->ri_interval = NULL;
1027 if ( mi->mi_ntargets > 0 && !META_BACK_QUARANTINE( mi ) ) {
1028 ldap_pvt_thread_mutex_init( &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_quarantine_mutex );
1031 if ( slap_retry_info_parse( argv[ 1 ], ri, buf, sizeof( buf ) ) ) {
1032 Debug( LDAP_DEBUG_ANY,
1033 "%s line %d: %s.\n",
1034 fname, lineno, buf );
1039 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
1041 int i = mi->mi_ntargets - 1, rc;
1042 struct berval dn, nvnc, pvnc, nrnc, prnc;
1045 Debug( LDAP_DEBUG_ANY,
1046 "%s: line %d: need \"uri\" directive first\n",
1054 * suffixmassage <suffix> <massaged suffix>
1056 * the <suffix> field must be defined as a valid suffix
1057 * (or suffixAlias?) for the current database;
1058 * the <massaged suffix> shouldn't have already been
1059 * defined as a valid suffix or suffixAlias for the
1063 Debug( LDAP_DEBUG_ANY,
1064 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
1069 ber_str2bv( argv[ 1 ], 0, 0, &dn );
1070 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1071 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1072 "suffix '%s' is invalid\n",
1073 fname, lineno, argv[ 1 ] );
1077 tmp_be = select_backend( &nvnc, 0, 0 );
1078 if ( tmp_be != NULL && tmp_be != be ) {
1079 Debug( LDAP_DEBUG_ANY,
1080 "%s: line %d: suffix already in use by another backend in"
1081 " \"suffixMassage <suffix> <massaged suffix>\"\n",
1083 free( pvnc.bv_val );
1084 free( nvnc.bv_val );
1088 ber_str2bv( argv[ 2 ], 0, 0, &dn );
1089 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1090 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1091 "massaged suffix '%s' is invalid\n",
1092 fname, lineno, argv[ 2 ] );
1093 free( pvnc.bv_val );
1094 free( nvnc.bv_val );
1099 tmp_be = select_backend( &nrnc, 0, 0 );
1100 if ( tmp_be != NULL ) {
1101 Debug( LDAP_DEBUG_ANY,
1102 "%s: line %d: massaged suffix already in use by another backend in"
1103 " \"suffixMassage <suffix> <massaged suffix>\"\n",
1105 free( pvnc.bv_val );
1106 free( nvnc.bv_val );
1107 free( prnc.bv_val );
1108 free( nrnc.bv_val );
1114 * The suffix massaging is emulated by means of the
1115 * rewrite capabilities
1116 * FIXME: no extra rewrite capabilities should be added
1119 rc = suffix_massage_config( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1120 &pvnc, &nvnc, &prnc, &nrnc );
1122 free( pvnc.bv_val );
1123 free( nvnc.bv_val );
1124 free( prnc.bv_val );
1125 free( nrnc.bv_val );
1129 /* rewrite stuff ... */
1130 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
1131 int i = mi->mi_ntargets - 1;
1134 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
1135 "statement outside target definition.\n",
1140 return rewrite_parse( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1141 fname, lineno, argc, argv );
1143 /* objectclass/attribute mapping */
1144 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1145 int i = mi->mi_ntargets - 1;
1148 Debug( LDAP_DEBUG_ANY,
1149 "%s: line %d: need \"uri\" directive first\n",
1154 return ldap_back_map_config( &mi->mi_targets[ i ]->mt_rwmap.rwm_oc,
1155 &mi->mi_targets[ i ]->mt_rwmap.rwm_at,
1156 fname, lineno, argc, argv );
1158 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1159 int i = mi->mi_ntargets - 1;
1160 int nretries = META_RETRY_UNDEFINED;
1163 Debug( LDAP_DEBUG_ANY,
1164 "%s: line %d: need value in \"nretries <value>\"\n",
1169 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1170 nretries = META_RETRY_FOREVER;
1172 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1173 nretries = META_RETRY_NEVER;
1176 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1177 Debug( LDAP_DEBUG_ANY,
1178 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1179 fname, lineno, argv[ 1 ] );
1185 mi->mi_nretries = nretries;
1188 mi->mi_targets[ i ]->mt_nretries = nretries;
1191 } else if ( strcasecmp( argv[ 0 ], "protocol-version" ) == 0 ) {
1192 int *version = mi->mi_ntargets ?
1193 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_version
1197 Debug( LDAP_DEBUG_ANY,
1198 "%s: line %d: need value in \"protocol-version <version>\"\n",
1203 if ( lutil_atoi( version, argv[ 1 ] ) != 0 ) {
1204 Debug( LDAP_DEBUG_ANY,
1205 "%s: line %d: unable to parse version \"%s\" in \"protocol-version <version>\"\n",
1206 fname, lineno, argv[ 1 ] );
1210 if ( *version != 0 && ( *version < LDAP_VERSION_MIN || *version > LDAP_VERSION_MAX ) ) {
1211 Debug( LDAP_DEBUG_ANY,
1212 "%s: line %d: unsupported version \"%s\" in \"protocol-version <version>\"\n",
1213 fname, lineno, argv[ 1 ] );
1219 return SLAP_CONF_UNKNOWN;
1225 ldap_back_map_config(
1226 struct ldapmap *oc_map,
1227 struct ldapmap *at_map,
1233 struct ldapmap *map;
1234 struct ldapmapping *mapping;
1238 if ( argc < 3 || argc > 4 ) {
1239 Debug( LDAP_DEBUG_ANY,
1240 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1245 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1249 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1253 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1254 "\"map {objectclass | attribute} [<local> | *] "
1255 "{<foreign> | *}\"\n",
1260 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1261 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1262 map->drop_missing = ( argc < 4 );
1265 src = dst = argv[ 3 ];
1267 } else if ( argc < 4 ) {
1273 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1276 if ( ( map == at_map )
1277 && ( strcasecmp( src, "objectclass" ) == 0
1278 || strcasecmp( dst, "objectclass" ) == 0 ) )
1280 Debug( LDAP_DEBUG_ANY,
1281 "%s: line %d: objectclass attribute cannot be mapped\n",
1285 mapping = (struct ldapmapping *)ch_calloc( 2,
1286 sizeof(struct ldapmapping) );
1287 if ( mapping == NULL ) {
1288 Debug( LDAP_DEBUG_ANY,
1289 "%s: line %d: out of memory\n",
1293 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1294 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1295 mapping[ 1 ].src = mapping[ 0 ].dst;
1296 mapping[ 1 ].dst = mapping[ 0 ].src;
1302 if ( src[ 0 ] != '\0' ) {
1303 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1304 Debug( LDAP_DEBUG_ANY,
1305 "%s: line %d: warning, source objectClass '%s' "
1306 "should be defined in schema\n",
1307 fname, lineno, src );
1310 * FIXME: this should become an err
1316 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1317 Debug( LDAP_DEBUG_ANY,
1318 "%s: line %d: warning, destination objectClass '%s' "
1319 "is not defined in schema\n",
1320 fname, lineno, dst );
1324 const char *text = NULL;
1325 AttributeDescription *ad = NULL;
1327 if ( src[ 0 ] != '\0' ) {
1328 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1329 if ( rc != LDAP_SUCCESS ) {
1330 Debug( LDAP_DEBUG_ANY,
1331 "%s: line %d: warning, source attributeType '%s' "
1332 "should be defined in schema\n",
1333 fname, lineno, src );
1336 * FIXME: this should become an err
1339 * we create a fake "proxied" ad
1343 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1344 &ad, &text, SLAP_AD_PROXIED );
1345 if ( rc != LDAP_SUCCESS ) {
1346 char buf[ SLAP_TEXT_BUFLEN ];
1348 snprintf( buf, sizeof( buf ),
1349 "source attributeType \"%s\": %d (%s)",
1350 src, rc, text ? text : "" );
1351 Debug( LDAP_DEBUG_ANY,
1352 "%s: line %d: %s\n",
1353 fname, lineno, buf );
1361 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1362 if ( rc != LDAP_SUCCESS ) {
1363 Debug( LDAP_DEBUG_ANY,
1364 "%s: line %d: warning, destination attributeType '%s' "
1365 "is not defined in schema\n",
1366 fname, lineno, dst );
1369 * we create a fake "proxied" ad
1373 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1374 &ad, &text, SLAP_AD_PROXIED );
1375 if ( rc != LDAP_SUCCESS ) {
1376 char buf[ SLAP_TEXT_BUFLEN ];
1378 snprintf( buf, sizeof( buf ),
1379 "source attributeType \"%s\": %d (%s)\n",
1380 dst, rc, text ? text : "" );
1381 Debug( LDAP_DEBUG_ANY,
1382 "%s: line %d: %s\n",
1383 fname, lineno, buf );
1389 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1390 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1392 Debug( LDAP_DEBUG_ANY,
1393 "%s: line %d: duplicate mapping found.\n",
1398 if ( src[ 0 ] != '\0' ) {
1399 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1400 mapping_cmp, mapping_dup );
1402 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1403 mapping_cmp, mapping_dup );
1409 ch_free( mapping[ 0 ].src.bv_val );
1410 ch_free( mapping[ 0 ].dst.bv_val );
1418 #ifdef ENABLE_REWRITE
1420 suffix_massage_regexize( const char *s )
1426 if ( s[ 0 ] == '\0' ) {
1427 return ch_strdup( "^(.+)$" );
1431 ( r = strchr( p, ',' ) ) != NULL;
1435 res = ch_calloc( sizeof( char ),
1437 + STRLENOF( "((.+),)?" )
1438 + STRLENOF( "[ ]?" ) * i
1439 + STRLENOF( "$" ) + 1 );
1441 ptr = lutil_strcopy( res, "((.+),)?" );
1443 ( r = strchr( p, ',' ) ) != NULL;
1445 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1446 ptr = lutil_strcopy( ptr, "[ ]?" );
1448 if ( r[ 1 ] == ' ' ) {
1452 ptr = lutil_strcopy( ptr, p );
1461 suffix_massage_patternize( const char *s, const char *p )
1468 if ( s[ 0 ] == '\0' ) {
1472 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1473 if ( res == NULL ) {
1477 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1478 if ( s[ 0 ] == '\0' ) {
1482 lutil_strcopy( ptr, p );
1488 suffix_massage_config(
1489 struct rewrite_info *info,
1490 struct berval *pvnc,
1491 struct berval *nvnc,
1492 struct berval *prnc,
1499 rargv[ 0 ] = "rewriteEngine";
1502 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1504 rargv[ 0 ] = "rewriteContext";
1505 rargv[ 1 ] = "default";
1507 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1509 rargv[ 0 ] = "rewriteRule";
1510 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1511 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1514 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1515 ch_free( rargv[ 1 ] );
1516 ch_free( rargv[ 2 ] );
1518 if ( BER_BVISEMPTY( pvnc ) ) {
1519 rargv[ 0 ] = "rewriteRule";
1521 rargv[ 2 ] = prnc->bv_val;
1524 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1527 rargv[ 0 ] = "rewriteContext";
1528 rargv[ 1 ] = "searchEntryDN";
1530 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1532 rargv[ 0 ] = "rewriteRule";
1533 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1534 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1537 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1538 ch_free( rargv[ 1 ] );
1539 ch_free( rargv[ 2 ] );
1541 if ( BER_BVISEMPTY( prnc ) ) {
1542 rargv[ 0 ] = "rewriteRule";
1544 rargv[ 2 ] = pvnc->bv_val;
1547 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1550 /* backward compatibility */
1551 rargv[ 0 ] = "rewriteContext";
1552 rargv[ 1 ] = "searchResult";
1553 rargv[ 2 ] = "alias";
1554 rargv[ 3 ] = "searchEntryDN";
1556 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1558 rargv[ 0 ] = "rewriteContext";
1559 rargv[ 1 ] = "matchedDN";
1560 rargv[ 2 ] = "alias";
1561 rargv[ 3 ] = "searchEntryDN";
1563 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1565 rargv[ 0 ] = "rewriteContext";
1566 rargv[ 1 ] = "searchAttrDN";
1567 rargv[ 2 ] = "alias";
1568 rargv[ 3 ] = "searchEntryDN";
1570 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1572 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1573 * see servers/slapd/overlays/rwm.h for details */
1574 rargv[ 0 ] = "rewriteContext";
1575 rargv[ 1 ] = "referralAttrDN";
1577 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1579 rargv[ 0 ] = "rewriteContext";
1580 rargv[ 1 ] = "referralDN";
1582 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1586 #endif /* ENABLE_REWRITE */