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_flags = mi->mi_flags;
175 mt->mt_version = mi->mi_version;
176 mt->mt_network_timeout = mi->mi_network_timeout;
177 mt->mt_bind_timeout = mi->mi_bind_timeout;
178 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
179 mt->mt_timeout[ c ] = mi->mi_timeout[ c ];
185 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t",
186 LDAP_PVT_URL_PARSE_NONE ) != LDAP_SUCCESS )
188 Debug( LDAP_DEBUG_ANY,
189 "%s: line %d: unable to parse URI"
190 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
196 * uri MUST have the <dn> part!
198 if ( ludp->lud_dn == NULL ) {
199 Debug( LDAP_DEBUG_ANY,
200 "%s: line %d: missing <naming context> "
201 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
205 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
208 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
209 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
214 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
215 Debug( LDAP_DEBUG_ANY,
216 "%s: line %d: missing <naming context> "
217 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
224 * copies and stores uri and suffix
226 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
227 rc = dnPrettyNormal( NULL, &dn, &mt->mt_psuffix,
228 &mt->mt_nsuffix, NULL );
229 if( rc != LDAP_SUCCESS ) {
230 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
231 "target \"%s\" DN is invalid\n",
232 fname, lineno, argv[ 1 ] );
236 ludp->lud_dn[ 0 ] = '\0';
238 switch ( ludp->lud_scope ) {
239 case LDAP_SCOPE_DEFAULT:
240 mt->mt_scope = LDAP_SCOPE_SUBTREE;
243 case LDAP_SCOPE_SUBTREE:
244 case LDAP_SCOPE_SUBORDINATE:
245 mt->mt_scope = ludp->lud_scope;
249 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
250 "invalid scope for target \"%s\"\n",
251 fname, lineno, argv[ 1 ] );
255 /* check all, to apply the scope check on the first one */
256 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
257 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
258 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
259 "multiple URIs must have "
267 mt->mt_uri = ldap_url_list2urls( ludp );
268 ldap_free_urllist( ludp );
269 if ( mt->mt_uri == NULL) {
270 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
276 * uri MUST be a branch of suffix!
278 #if 0 /* too strict a constraint */
279 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) != be ) {
280 Debug( LDAP_DEBUG_ANY,
281 "%s: line %d: <naming context> of URI does not refer to current backend"
282 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
288 * uri MUST be a branch of a suffix!
290 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) == NULL ) {
291 Debug( LDAP_DEBUG_ANY,
292 "%s: line %d: <naming context> of URI does not resolve to a backend"
293 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
299 /* subtree-exclude */
300 } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
301 int i = mi->mi_ntargets - 1;
302 struct berval dn, ndn;
305 Debug( LDAP_DEBUG_ANY,
306 "%s: line %d: need \"uri\" directive first\n",
313 Debug( LDAP_DEBUG_ANY,
314 "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
322 Debug( LDAP_DEBUG_ANY,
323 "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
328 ber_str2bv( argv[ 1 ], 0, 0, &dn );
329 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
332 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
333 "subtree-exclude DN=\"%s\" is invalid\n",
334 fname, lineno, argv[ 1 ] );
338 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_nsuffix ) ) {
339 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
340 "subtree-exclude DN=\"%s\" "
341 "must be subtree of target\n",
342 fname, lineno, argv[ 1 ] );
343 ber_memfree( ndn.bv_val );
347 if ( mi->mi_targets[ i ]->mt_subtree_exclude != NULL ) {
350 for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ); j++ )
352 if ( dnIsSuffix( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ], &ndn ) ) {
353 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
354 "subtree-exclude DN=\"%s\" "
355 "is suffix of another subtree-exclude\n",
356 fname, lineno, argv[ 1 ] );
357 /* reject, because it might be superior
358 * to more than one subtree-exclude */
359 ber_memfree( ndn.bv_val );
362 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ) ) {
363 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
364 "another subtree-exclude is suffix of "
365 "subtree-exclude DN=\"%s\"\n",
366 fname, lineno, argv[ 1 ] );
367 ber_memfree( ndn.bv_val );
373 ber_bvarray_add( &mi->mi_targets[ i ]->mt_subtree_exclude, &ndn );
375 /* default target directive */
376 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
377 int i = mi->mi_ntargets - 1;
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: \"default-target\" alone need be"
383 " inside a \"uri\" directive\n",
387 mi->mi_defaulttarget = i;
390 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
392 Debug( LDAP_DEBUG_ANY,
393 "%s: line %d: \"default-target none\""
394 " should go before uri definitions\n",
397 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
401 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
402 || mi->mi_defaulttarget < 0
403 || mi->mi_defaulttarget >= i - 1 )
405 Debug( LDAP_DEBUG_ANY,
406 "%s: line %d: illegal target number %d\n",
407 fname, lineno, mi->mi_defaulttarget );
413 /* ttl of dn cache */
414 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
416 Debug( LDAP_DEBUG_ANY,
417 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
422 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
423 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
425 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
426 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
431 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
432 Debug( LDAP_DEBUG_ANY,
433 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
434 fname, lineno, argv[ 1 ] );
437 mi->mi_cache.ttl = (time_t)t;
440 /* network timeout when connecting to ldap servers */
441 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
443 time_t *tp = mi->mi_ntargets ?
444 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_network_timeout
445 : &mi->mi_network_timeout;
448 Debug( LDAP_DEBUG_ANY,
449 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
454 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
455 Debug( LDAP_DEBUG_ANY,
456 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
457 fname, lineno, argv[ 1 ] );
464 /* idle timeout when connecting to ldap servers */
465 } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
470 Debug( LDAP_DEBUG_ANY,
471 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
477 Debug( LDAP_DEBUG_ANY,
478 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
483 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
484 Debug( LDAP_DEBUG_ANY,
485 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
486 fname, lineno, argv[ 1 ] );
491 mi->mi_idle_timeout = (time_t)t;
494 } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
499 Debug( LDAP_DEBUG_ANY,
500 "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
506 Debug( LDAP_DEBUG_ANY,
507 "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
512 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
513 Debug( LDAP_DEBUG_ANY,
514 "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
515 fname, lineno, argv[ 1 ] );
520 mi->mi_conn_ttl = (time_t)t;
522 /* bind timeout when connecting to ldap servers */
523 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
525 struct timeval *tp = mi->mi_ntargets ?
526 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_bind_timeout
527 : &mi->mi_bind_timeout;
531 Debug( LDAP_DEBUG_ANY,
532 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
538 Debug( LDAP_DEBUG_ANY,
539 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
544 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
545 Debug( LDAP_DEBUG_ANY,
546 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
547 fname, lineno, argv[ 1 ] );
552 tp->tv_sec = t/1000000;
553 tp->tv_usec = t%1000000;
555 /* name to use for meta_back_group */
556 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
557 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
559 int i = mi->mi_ntargets - 1;
563 Debug( LDAP_DEBUG_ANY,
564 "%s: line %d: need \"uri\" directive first\n",
570 Debug( LDAP_DEBUG_ANY,
571 "%s: line %d: missing name in \"binddn <name>\" line\n",
576 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
577 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
578 "\"binddn\" statement is deprecated; "
579 "use \"acl-authcDN\" instead\n",
581 /* FIXME: some day we'll need to throw an error */
584 ber_str2bv( argv[ 1 ], 0, 0, &dn );
585 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ]->mt_binddn,
586 NULL ) != LDAP_SUCCESS )
588 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
589 "bind DN '%s' is invalid\n",
590 fname, lineno, argv[ 1 ] );
594 /* password to use for meta_back_group */
595 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
596 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
598 int i = mi->mi_ntargets - 1;
601 Debug( LDAP_DEBUG_ANY,
602 "%s: line %d: need \"uri\" directive first\n",
608 Debug( LDAP_DEBUG_ANY,
609 "%s: line %d: missing password in \"bindpw <password>\" line\n",
614 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
615 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
616 "\"bindpw\" statement is deprecated; "
617 "use \"acl-passwd\" instead\n",
619 /* FIXME: some day we'll need to throw an error */
622 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_bindpw );
624 /* save bind creds for referral rebinds? */
625 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
627 Debug( LDAP_DEBUG_ANY,
628 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
634 Debug( LDAP_DEBUG_ANY,
635 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
637 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
640 switch ( check_true_false( argv[ 1 ] ) ) {
642 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
646 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
650 Debug( LDAP_DEBUG_ANY,
651 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
652 fname, lineno, argv[ 1 ] );
657 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
658 unsigned *flagsp = mi->mi_ntargets ?
659 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
663 Debug( LDAP_DEBUG_ANY,
664 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
669 /* this is the default; we add it because the default might change... */
670 switch ( check_true_false( argv[ 1 ] ) ) {
672 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
676 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
680 Debug( LDAP_DEBUG_ANY,
681 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
682 fname, lineno, argv[ 1 ] );
686 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
687 unsigned *flagsp = mi->mi_ntargets ?
688 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
692 Debug( LDAP_DEBUG_ANY,
693 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
699 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
700 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
703 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
704 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
705 *flagsp |= LDAP_BACK_F_USE_TLS;
707 /* propagate start tls */
708 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
709 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
712 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
713 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
714 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
717 Debug( LDAP_DEBUG_ANY,
718 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
719 fname, lineno, argv[ 1 ] );
723 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
724 unsigned *flagsp = mi->mi_ntargets ?
725 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
729 Debug( LDAP_DEBUG_ANY,
730 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
735 switch ( check_true_false( argv[ 1 ] ) ) {
737 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
741 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
745 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
746 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
749 Debug( LDAP_DEBUG_ANY,
750 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
751 fname, lineno, argv[ 1 ] );
758 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
760 Debug( LDAP_DEBUG_ANY,
761 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
766 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
767 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
769 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
770 mi->mi_flags |= META_BACK_F_ONERR_STOP;
773 Debug( LDAP_DEBUG_ANY,
774 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
775 fname, lineno, argv[ 1 ] );
780 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
782 Debug( LDAP_DEBUG_ANY,
783 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
788 switch ( check_true_false( argv[ 1 ] ) ) {
790 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
794 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
798 Debug( LDAP_DEBUG_ANY,
799 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
800 fname, lineno, argv[ 1 ] );
805 } else if ( strcasecmp( argv[ 0 ], "single-conn" ) == 0 ) {
807 Debug( LDAP_DEBUG_ANY,
808 "%s: line %d: \"single-conn {FALSE|true}\" takes 1 argument\n",
813 if ( mi->mi_ntargets > 0 ) {
814 Debug( LDAP_DEBUG_ANY,
815 "%s: line %d: \"single-conn\" must appear before target definitions\n",
820 switch ( check_true_false( argv[ 1 ] ) ) {
822 mi->mi_flags &= ~LDAP_BACK_F_SINGLECONN;
826 mi->mi_flags |= LDAP_BACK_F_SINGLECONN;
830 Debug( LDAP_DEBUG_ANY,
831 "%s: line %d: \"single-conn {FALSE|true}\": invalid arg \"%s\".\n",
832 fname, lineno, argv[ 1 ] );
836 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
838 time_t *tv = mi->mi_ntargets ?
839 mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_timeout
844 Debug( LDAP_DEBUG_ANY,
845 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
850 for ( c = 1; c < argc; c++ ) {
854 sep = strchr( argv[ c ], '=' );
856 size_t len = sep - argv[ c ];
858 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
859 t = &tv[ LDAP_BACK_OP_ADD ];
860 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
861 t = &tv[ LDAP_BACK_OP_DELETE ];
862 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
863 t = &tv[ LDAP_BACK_OP_MODIFY ];
864 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
865 t = &tv[ LDAP_BACK_OP_MODRDN ];
867 char buf[ SLAP_TEXT_BUFLEN ];
868 snprintf( buf, sizeof( buf ),
869 "unknown operation \"%s\" for timeout #%d",
871 Debug( LDAP_DEBUG_ANY,
872 "%s: line %d: %s.\n",
873 fname, lineno, buf );
882 if ( lutil_parse_time( sep, &val ) != 0 ) {
883 Debug( LDAP_DEBUG_ANY,
884 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
885 fname, lineno, sep );
895 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
896 tv[ i ] = (time_t)val;
901 /* name to use as pseudo-root dn */
902 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
903 int i = mi->mi_ntargets - 1;
907 Debug( LDAP_DEBUG_ANY,
908 "%s: line %d: need \"uri\" directive first\n",
914 Debug( LDAP_DEBUG_ANY,
915 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
920 dn.bv_val = argv[ 1 ];
921 dn.bv_len = strlen( argv[ 1 ] );
922 if ( dnNormalize( 0, NULL, NULL, &dn,
923 &mi->mi_targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
925 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
926 "pseudoroot DN '%s' is invalid\n",
927 fname, lineno, argv[ 1 ] );
931 /* password to use as pseudo-root */
932 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
933 int i = mi->mi_ntargets - 1;
936 Debug( LDAP_DEBUG_ANY,
937 "%s: line %d: need \"uri\" directive first\n",
943 Debug( LDAP_DEBUG_ANY,
944 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
948 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_pseudorootpw );
951 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
953 int i = mi->mi_ntargets - 1, rc;
954 struct berval dn, nvnc, pvnc, nrnc, prnc;
957 Debug( LDAP_DEBUG_ANY,
958 "%s: line %d: need \"uri\" directive first\n",
966 * suffixmassage <suffix> <massaged suffix>
968 * the <suffix> field must be defined as a valid suffix
969 * (or suffixAlias?) for the current database;
970 * the <massaged suffix> shouldn't have already been
971 * defined as a valid suffix or suffixAlias for the
975 Debug( LDAP_DEBUG_ANY,
976 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
981 ber_str2bv( argv[ 1 ], 0, 0, &dn );
982 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
983 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
984 "suffix '%s' is invalid\n",
985 fname, lineno, argv[ 1 ] );
989 tmp_be = select_backend( &nvnc, 0, 0 );
990 if ( tmp_be != NULL && tmp_be != be ) {
991 Debug( LDAP_DEBUG_ANY,
992 "%s: line %d: suffix already in use by another backend in"
993 " \"suffixMassage <suffix> <massaged suffix>\"\n",
1000 ber_str2bv( argv[ 2 ], 0, 0, &dn );
1001 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1002 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1003 "massaged suffix '%s' is invalid\n",
1004 fname, lineno, argv[ 2 ] );
1005 free( pvnc.bv_val );
1006 free( nvnc.bv_val );
1011 tmp_be = select_backend( &nrnc, 0, 0 );
1012 if ( tmp_be != NULL ) {
1013 Debug( LDAP_DEBUG_ANY,
1014 "%s: line %d: massaged suffix already in use by another backend in"
1015 " \"suffixMassage <suffix> <massaged suffix>\"\n",
1017 free( pvnc.bv_val );
1018 free( nvnc.bv_val );
1019 free( prnc.bv_val );
1020 free( nrnc.bv_val );
1026 * The suffix massaging is emulated by means of the
1027 * rewrite capabilities
1028 * FIXME: no extra rewrite capabilities should be added
1031 rc = suffix_massage_config( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1032 &pvnc, &nvnc, &prnc, &nrnc );
1034 free( pvnc.bv_val );
1035 free( nvnc.bv_val );
1036 free( prnc.bv_val );
1037 free( nrnc.bv_val );
1041 /* rewrite stuff ... */
1042 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
1043 int i = mi->mi_ntargets - 1;
1046 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
1047 "statement outside target definition.\n",
1052 return rewrite_parse( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1053 fname, lineno, argc, argv );
1055 /* objectclass/attribute mapping */
1056 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1057 int i = mi->mi_ntargets - 1;
1060 Debug( LDAP_DEBUG_ANY,
1061 "%s: line %d: need \"uri\" directive first\n",
1066 return ldap_back_map_config( &mi->mi_targets[ i ]->mt_rwmap.rwm_oc,
1067 &mi->mi_targets[ i ]->mt_rwmap.rwm_at,
1068 fname, lineno, argc, argv );
1070 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1071 int i = mi->mi_ntargets - 1;
1072 int nretries = META_RETRY_UNDEFINED;
1075 Debug( LDAP_DEBUG_ANY,
1076 "%s: line %d: need value in \"nretries <value>\"\n",
1081 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1082 nretries = META_RETRY_FOREVER;
1084 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1085 nretries = META_RETRY_NEVER;
1088 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1089 Debug( LDAP_DEBUG_ANY,
1090 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1091 fname, lineno, argv[ 1 ] );
1097 mi->mi_nretries = nretries;
1100 mi->mi_targets[ i ]->mt_nretries = nretries;
1103 } else if ( strcasecmp( argv[ 0 ], "protocol-version" ) == 0 ) {
1104 int *version = mi->mi_ntargets ?
1105 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_version
1109 Debug( LDAP_DEBUG_ANY,
1110 "%s: line %d: need value in \"protocol-version <version>\"\n",
1115 if ( lutil_atoi( version, argv[ 1 ] ) != 0 ) {
1116 Debug( LDAP_DEBUG_ANY,
1117 "%s: line %d: unable to parse version \"%s\" in \"protocol-version <version>\"\n",
1118 fname, lineno, argv[ 1 ] );
1122 if ( *version != 0 && ( *version < LDAP_VERSION_MIN || *version > LDAP_VERSION_MAX ) ) {
1123 Debug( LDAP_DEBUG_ANY,
1124 "%s: line %d: unsupported version \"%s\" in \"protocol-version <version>\"\n",
1125 fname, lineno, argv[ 1 ] );
1131 return SLAP_CONF_UNKNOWN;
1137 ldap_back_map_config(
1138 struct ldapmap *oc_map,
1139 struct ldapmap *at_map,
1145 struct ldapmap *map;
1146 struct ldapmapping *mapping;
1150 if ( argc < 3 || argc > 4 ) {
1151 Debug( LDAP_DEBUG_ANY,
1152 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1157 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1161 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1165 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1166 "\"map {objectclass | attribute} [<local> | *] "
1167 "{<foreign> | *}\"\n",
1172 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1173 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1174 map->drop_missing = ( argc < 4 );
1177 src = dst = argv[ 3 ];
1179 } else if ( argc < 4 ) {
1185 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1188 if ( ( map == at_map )
1189 && ( strcasecmp( src, "objectclass" ) == 0
1190 || strcasecmp( dst, "objectclass" ) == 0 ) )
1192 Debug( LDAP_DEBUG_ANY,
1193 "%s: line %d: objectclass attribute cannot be mapped\n",
1197 mapping = (struct ldapmapping *)ch_calloc( 2,
1198 sizeof(struct ldapmapping) );
1199 if ( mapping == NULL ) {
1200 Debug( LDAP_DEBUG_ANY,
1201 "%s: line %d: out of memory\n",
1205 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1206 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1207 mapping[ 1 ].src = mapping[ 0 ].dst;
1208 mapping[ 1 ].dst = mapping[ 0 ].src;
1214 if ( src[ 0 ] != '\0' ) {
1215 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1216 Debug( LDAP_DEBUG_ANY,
1217 "%s: line %d: warning, source objectClass '%s' "
1218 "should be defined in schema\n",
1219 fname, lineno, src );
1222 * FIXME: this should become an err
1228 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1229 Debug( LDAP_DEBUG_ANY,
1230 "%s: line %d: warning, destination objectClass '%s' "
1231 "is not defined in schema\n",
1232 fname, lineno, dst );
1236 const char *text = NULL;
1237 AttributeDescription *ad = NULL;
1239 if ( src[ 0 ] != '\0' ) {
1240 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1241 if ( rc != LDAP_SUCCESS ) {
1242 Debug( LDAP_DEBUG_ANY,
1243 "%s: line %d: warning, source attributeType '%s' "
1244 "should be defined in schema\n",
1245 fname, lineno, src );
1248 * FIXME: this should become an err
1251 * we create a fake "proxied" ad
1255 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1256 &ad, &text, SLAP_AD_PROXIED );
1257 if ( rc != LDAP_SUCCESS ) {
1258 char buf[ SLAP_TEXT_BUFLEN ];
1260 snprintf( buf, sizeof( buf ),
1261 "source attributeType \"%s\": %d (%s)",
1262 src, rc, text ? text : "" );
1263 Debug( LDAP_DEBUG_ANY,
1264 "%s: line %d: %s\n",
1265 fname, lineno, buf );
1273 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1274 if ( rc != LDAP_SUCCESS ) {
1275 Debug( LDAP_DEBUG_ANY,
1276 "%s: line %d: warning, destination attributeType '%s' "
1277 "is not defined in schema\n",
1278 fname, lineno, dst );
1281 * we create a fake "proxied" ad
1285 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1286 &ad, &text, SLAP_AD_PROXIED );
1287 if ( rc != LDAP_SUCCESS ) {
1288 char buf[ SLAP_TEXT_BUFLEN ];
1290 snprintf( buf, sizeof( buf ),
1291 "source attributeType \"%s\": %d (%s)\n",
1292 dst, rc, text ? text : "" );
1293 Debug( LDAP_DEBUG_ANY,
1294 "%s: line %d: %s\n",
1295 fname, lineno, buf );
1301 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1302 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1304 Debug( LDAP_DEBUG_ANY,
1305 "%s: line %d: duplicate mapping found.\n",
1310 if ( src[ 0 ] != '\0' ) {
1311 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1312 mapping_cmp, mapping_dup );
1314 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1315 mapping_cmp, mapping_dup );
1321 ch_free( mapping[ 0 ].src.bv_val );
1322 ch_free( mapping[ 0 ].dst.bv_val );
1330 #ifdef ENABLE_REWRITE
1332 suffix_massage_regexize( const char *s )
1338 if ( s[ 0 ] == '\0' ) {
1339 return ch_strdup( "^(.+)$" );
1343 ( r = strchr( p, ',' ) ) != NULL;
1347 res = ch_calloc( sizeof( char ),
1349 + STRLENOF( "((.+),)?" )
1350 + STRLENOF( "[ ]?" ) * i
1351 + STRLENOF( "$" ) + 1 );
1353 ptr = lutil_strcopy( res, "((.+),)?" );
1355 ( r = strchr( p, ',' ) ) != NULL;
1357 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1358 ptr = lutil_strcopy( ptr, "[ ]?" );
1360 if ( r[ 1 ] == ' ' ) {
1364 ptr = lutil_strcopy( ptr, p );
1373 suffix_massage_patternize( const char *s, const char *p )
1380 if ( s[ 0 ] == '\0' ) {
1384 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1385 if ( res == NULL ) {
1389 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1390 if ( s[ 0 ] == '\0' ) {
1394 lutil_strcopy( ptr, p );
1400 suffix_massage_config(
1401 struct rewrite_info *info,
1402 struct berval *pvnc,
1403 struct berval *nvnc,
1404 struct berval *prnc,
1411 rargv[ 0 ] = "rewriteEngine";
1414 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1416 rargv[ 0 ] = "rewriteContext";
1417 rargv[ 1 ] = "default";
1419 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1421 rargv[ 0 ] = "rewriteRule";
1422 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1423 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1426 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1427 ch_free( rargv[ 1 ] );
1428 ch_free( rargv[ 2 ] );
1430 if ( BER_BVISEMPTY( pvnc ) ) {
1431 rargv[ 0 ] = "rewriteRule";
1433 rargv[ 2 ] = prnc->bv_val;
1436 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1439 rargv[ 0 ] = "rewriteContext";
1440 rargv[ 1 ] = "searchEntryDN";
1442 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1444 rargv[ 0 ] = "rewriteRule";
1445 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1446 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1449 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1450 ch_free( rargv[ 1 ] );
1451 ch_free( rargv[ 2 ] );
1453 if ( BER_BVISEMPTY( prnc ) ) {
1454 rargv[ 0 ] = "rewriteRule";
1456 rargv[ 2 ] = pvnc->bv_val;
1459 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1462 /* backward compatibility */
1463 rargv[ 0 ] = "rewriteContext";
1464 rargv[ 1 ] = "searchResult";
1465 rargv[ 2 ] = "alias";
1466 rargv[ 3 ] = "searchEntryDN";
1468 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1470 rargv[ 0 ] = "rewriteContext";
1471 rargv[ 1 ] = "matchedDN";
1472 rargv[ 2 ] = "alias";
1473 rargv[ 3 ] = "searchEntryDN";
1475 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1477 rargv[ 0 ] = "rewriteContext";
1478 rargv[ 1 ] = "searchAttrDN";
1479 rargv[ 2 ] = "alias";
1480 rargv[ 3 ] = "searchEntryDN";
1482 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1484 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1485 * see servers/slapd/overlays/rwm.h for details */
1486 rargv[ 0 ] = "rewriteContext";
1487 rargv[ 1 ] = "referralAttrDN";
1489 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1491 rargv[ 0 ] = "rewriteContext";
1492 rargv[ 1 ] = "referralDN";
1494 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1498 #endif /* ENABLE_REWRITE */