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 ) {
56 * the filter rewrite as a string must be disabled
57 * by default; it can be re-enabled by adding rules;
58 * this creates an empty rewriteContext
60 rargv[ 0 ] = "rewriteContext";
61 rargv[ 1 ] = "searchFilter";
63 rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
65 rargv[ 0 ] = "rewriteContext";
66 rargv[ 1 ] = "default";
68 rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
70 ldap_back_map_init( &mt->mt_rwmap.rwm_at, &mapping );
72 ldap_pvt_thread_mutex_init( &mt->mt_uri_mutex );
80 check_true_false( char *str )
82 if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
86 if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
103 metainfo_t *mi = ( metainfo_t * )be->be_private;
105 assert( mi != NULL );
107 /* URI of server to query */
108 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
109 int i = mi->mi_ntargets;
112 #endif /* uncomment if uri MUST be a branch of suffix */
113 LDAPURLDesc *ludp, *tmpludp;
122 Debug( LDAP_DEBUG_ANY,
123 "%s: line %d: missing URI "
124 "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
132 Debug( LDAP_DEBUG_ANY,
133 "%s: line %d: too many args "
134 "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
139 if ( be->be_nsuffix == NULL ) {
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: the suffix must be defined before any target.\n",
148 mi->mi_targets = ( metatarget_t ** )ch_realloc( mi->mi_targets,
149 sizeof( metatarget_t * ) * mi->mi_ntargets );
150 if ( mi->mi_targets == NULL ) {
151 Debug( LDAP_DEBUG_ANY,
152 "%s: line %d: out of memory while storing server name"
153 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
158 if ( meta_back_new_target( &mi->mi_targets[ i ] ) != 0 ) {
159 Debug( LDAP_DEBUG_ANY,
160 "%s: line %d: unable to init server"
161 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
166 mt = mi->mi_targets[ i ];
168 mt->mt_rebind_f = mi->mi_rebind_f;
169 mt->mt_urllist_f = mi->mi_urllist_f;
170 mt->mt_urllist_p = mt;
172 mt->mt_nretries = mi->mi_nretries;
173 mt->mt_flags = mi->mi_flags;
174 mt->mt_version = mi->mi_version;
175 mt->mt_network_timeout = mi->mi_network_timeout;
176 mt->mt_bind_timeout = mi->mi_bind_timeout;
177 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
178 mt->mt_timeout[ c ] = mi->mi_timeout[ c ];
184 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t",
185 LDAP_PVT_URL_PARSE_NONE ) != LDAP_SUCCESS )
187 Debug( LDAP_DEBUG_ANY,
188 "%s: line %d: unable to parse URI"
189 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
195 * uri MUST have the <dn> part!
197 if ( ludp->lud_dn == NULL ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: missing <naming context> "
200 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
204 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
207 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
208 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
213 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
214 Debug( LDAP_DEBUG_ANY,
215 "%s: line %d: missing <naming context> "
216 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
223 * copies and stores uri and suffix
225 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
226 rc = dnPrettyNormal( NULL, &dn, &mt->mt_psuffix,
227 &mt->mt_nsuffix, NULL );
228 if( rc != LDAP_SUCCESS ) {
229 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
230 "target \"%s\" DN is invalid\n",
231 fname, lineno, argv[ 1 ] );
235 ludp->lud_dn[ 0 ] = '\0';
237 switch ( ludp->lud_scope ) {
238 case LDAP_SCOPE_DEFAULT:
239 mt->mt_scope = LDAP_SCOPE_SUBTREE;
242 case LDAP_SCOPE_SUBTREE:
243 case LDAP_SCOPE_SUBORDINATE:
244 mt->mt_scope = ludp->lud_scope;
248 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
249 "invalid scope for target \"%s\"\n",
250 fname, lineno, argv[ 1 ] );
254 /* check all, to apply the scope check on the first one */
255 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
256 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
257 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
258 "multiple URIs must have "
266 mt->mt_uri = ldap_url_list2urls( ludp );
267 ldap_free_urllist( ludp );
268 if ( mt->mt_uri == NULL) {
269 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
275 * uri MUST be a branch of suffix!
277 #if 0 /* too strict a constraint */
278 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) != be ) {
279 Debug( LDAP_DEBUG_ANY,
280 "%s: line %d: <naming context> of URI does not refer to current backend"
281 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
287 * uri MUST be a branch of a suffix!
289 if ( select_backend( &mt->mt_nsuffix, 0, 0 ) == NULL ) {
290 Debug( LDAP_DEBUG_ANY,
291 "%s: line %d: <naming context> of URI does not resolve to a backend"
292 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
298 /* subtree-exclude */
299 } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
300 int i = mi->mi_ntargets - 1;
301 struct berval dn, ndn;
304 Debug( LDAP_DEBUG_ANY,
305 "%s: line %d: need \"uri\" directive first\n",
312 Debug( LDAP_DEBUG_ANY,
313 "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
327 ber_str2bv( argv[ 1 ], 0, 0, &dn );
328 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
331 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
332 "subtree-exclude DN=\"%s\" is invalid\n",
333 fname, lineno, argv[ 1 ] );
337 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_nsuffix ) ) {
338 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
339 "subtree-exclude DN=\"%s\" "
340 "must be subtree of target\n",
341 fname, lineno, argv[ 1 ] );
342 ber_memfree( ndn.bv_val );
346 if ( mi->mi_targets[ i ]->mt_subtree_exclude != NULL ) {
349 for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ); j++ )
351 if ( dnIsSuffix( &mi->mi_targets[ i ]->mt_subtree_exclude[ j ], &ndn ) ) {
352 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
353 "subtree-exclude DN=\"%s\" "
354 "is suffix of another subtree-exclude\n",
355 fname, lineno, argv[ 1 ] );
356 /* reject, because it might be superior
357 * to more than one subtree-exclude */
358 ber_memfree( ndn.bv_val );
361 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ]->mt_subtree_exclude[ j ] ) ) {
362 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
363 "another subtree-exclude is suffix of "
364 "subtree-exclude DN=\"%s\"\n",
365 fname, lineno, argv[ 1 ] );
366 ber_memfree( ndn.bv_val );
372 ber_bvarray_add( &mi->mi_targets[ i ]->mt_subtree_exclude, &ndn );
374 /* default target directive */
375 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
376 int i = mi->mi_ntargets - 1;
380 Debug( LDAP_DEBUG_ANY,
381 "%s: line %d: \"default-target\" alone need be"
382 " inside a \"uri\" directive\n",
386 mi->mi_defaulttarget = i;
389 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
391 Debug( LDAP_DEBUG_ANY,
392 "%s: line %d: \"default-target none\""
393 " should go before uri definitions\n",
396 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
400 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
401 || mi->mi_defaulttarget < 0
402 || mi->mi_defaulttarget >= i - 1 )
404 Debug( LDAP_DEBUG_ANY,
405 "%s: line %d: illegal target number %d\n",
406 fname, lineno, mi->mi_defaulttarget );
412 /* ttl of dn cache */
413 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
421 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
422 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
424 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
425 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
430 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
431 Debug( LDAP_DEBUG_ANY,
432 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
433 fname, lineno, argv[ 1 ] );
436 mi->mi_cache.ttl = (time_t)t;
439 /* network timeout when connecting to ldap servers */
440 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
442 time_t *tp = mi->mi_ntargets ?
443 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_network_timeout
444 : &mi->mi_network_timeout;
447 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
453 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
454 Debug( LDAP_DEBUG_ANY,
455 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
456 fname, lineno, argv[ 1 ] );
463 /* idle timeout when connecting to ldap servers */
464 } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
469 Debug( LDAP_DEBUG_ANY,
470 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
476 Debug( LDAP_DEBUG_ANY,
477 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
482 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
483 Debug( LDAP_DEBUG_ANY,
484 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
485 fname, lineno, argv[ 1 ] );
490 mi->mi_idle_timeout = (time_t)t;
493 } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
498 Debug( LDAP_DEBUG_ANY,
499 "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
505 Debug( LDAP_DEBUG_ANY,
506 "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
511 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
512 Debug( LDAP_DEBUG_ANY,
513 "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
514 fname, lineno, argv[ 1 ] );
519 mi->mi_conn_ttl = (time_t)t;
521 /* bind timeout when connecting to ldap servers */
522 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
524 struct timeval *tp = mi->mi_ntargets ?
525 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_bind_timeout
526 : &mi->mi_bind_timeout;
530 Debug( LDAP_DEBUG_ANY,
531 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
537 Debug( LDAP_DEBUG_ANY,
538 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
543 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
544 Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
546 fname, lineno, argv[ 1 ] );
551 tp->tv_sec = t/1000000;
552 tp->tv_usec = t%1000000;
554 /* name to use for meta_back_group */
555 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
556 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
558 int i = mi->mi_ntargets - 1;
562 Debug( LDAP_DEBUG_ANY,
563 "%s: line %d: need \"uri\" directive first\n",
569 Debug( LDAP_DEBUG_ANY,
570 "%s: line %d: missing name in \"binddn <name>\" line\n",
575 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
576 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
577 "\"binddn\" statement is deprecated; "
578 "use \"acl-authcDN\" instead\n",
580 /* FIXME: some day we'll need to throw an error */
583 ber_str2bv( argv[ 1 ], 0, 0, &dn );
584 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ]->mt_binddn,
585 NULL ) != LDAP_SUCCESS )
587 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
588 "bind DN '%s' is invalid\n",
589 fname, lineno, argv[ 1 ] );
593 /* password to use for meta_back_group */
594 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
595 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
597 int i = mi->mi_ntargets - 1;
600 Debug( LDAP_DEBUG_ANY,
601 "%s: line %d: need \"uri\" directive first\n",
607 Debug( LDAP_DEBUG_ANY,
608 "%s: line %d: missing password in \"bindpw <password>\" line\n",
613 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
614 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
615 "\"bindpw\" statement is deprecated; "
616 "use \"acl-passwd\" instead\n",
618 /* FIXME: some day we'll need to throw an error */
621 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_bindpw );
623 /* save bind creds for referral rebinds? */
624 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
626 Debug( LDAP_DEBUG_ANY,
627 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
633 Debug( LDAP_DEBUG_ANY,
634 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
636 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
639 switch ( check_true_false( argv[ 1 ] ) ) {
641 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
645 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
649 Debug( LDAP_DEBUG_ANY,
650 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
651 fname, lineno, argv[ 1 ] );
656 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
657 unsigned *flagsp = mi->mi_ntargets ?
658 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
662 Debug( LDAP_DEBUG_ANY,
663 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
668 /* this is the default; we add it because the default might change... */
669 switch ( check_true_false( argv[ 1 ] ) ) {
671 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
675 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
679 Debug( LDAP_DEBUG_ANY,
680 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
681 fname, lineno, argv[ 1 ] );
685 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
686 unsigned *flagsp = mi->mi_ntargets ?
687 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
691 Debug( LDAP_DEBUG_ANY,
692 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
698 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
699 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
702 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
703 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
704 *flagsp |= LDAP_BACK_F_USE_TLS;
706 /* propagate start tls */
707 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
708 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
711 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
712 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
713 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
716 Debug( LDAP_DEBUG_ANY,
717 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
718 fname, lineno, argv[ 1 ] );
722 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
723 unsigned *flagsp = mi->mi_ntargets ?
724 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_flags
728 Debug( LDAP_DEBUG_ANY,
729 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
734 switch ( check_true_false( argv[ 1 ] ) ) {
736 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
740 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
744 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
745 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
748 Debug( LDAP_DEBUG_ANY,
749 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
750 fname, lineno, argv[ 1 ] );
757 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
759 Debug( LDAP_DEBUG_ANY,
760 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
765 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
766 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
768 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
769 mi->mi_flags |= META_BACK_F_ONERR_STOP;
772 Debug( LDAP_DEBUG_ANY,
773 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
774 fname, lineno, argv[ 1 ] );
779 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
781 Debug( LDAP_DEBUG_ANY,
782 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
787 switch ( check_true_false( argv[ 1 ] ) ) {
789 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
793 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
797 Debug( LDAP_DEBUG_ANY,
798 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
799 fname, lineno, argv[ 1 ] );
803 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
805 time_t *tv = mi->mi_ntargets ?
806 mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_timeout
811 Debug( LDAP_DEBUG_ANY,
812 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
817 for ( c = 1; c < argc; c++ ) {
821 sep = strchr( argv[ c ], '=' );
823 size_t len = sep - argv[ c ];
825 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
826 t = &tv[ LDAP_BACK_OP_ADD ];
827 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
828 t = &tv[ LDAP_BACK_OP_DELETE ];
829 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
830 t = &tv[ LDAP_BACK_OP_MODIFY ];
831 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
832 t = &tv[ LDAP_BACK_OP_MODRDN ];
834 char buf[ SLAP_TEXT_BUFLEN ];
835 snprintf( buf, sizeof( buf ),
836 "unknown operation \"%s\" for timeout #%d",
838 Debug( LDAP_DEBUG_ANY,
839 "%s: line %d: %s.\n",
840 fname, lineno, buf );
849 if ( lutil_parse_time( sep, &val ) != 0 ) {
850 Debug( LDAP_DEBUG_ANY,
851 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
852 fname, lineno, sep );
862 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
863 tv[ i ] = (time_t)val;
868 /* name to use as pseudo-root dn */
869 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
870 int i = mi->mi_ntargets - 1;
874 Debug( LDAP_DEBUG_ANY,
875 "%s: line %d: need \"uri\" directive first\n",
881 Debug( LDAP_DEBUG_ANY,
882 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
887 dn.bv_val = argv[ 1 ];
888 dn.bv_len = strlen( argv[ 1 ] );
889 if ( dnNormalize( 0, NULL, NULL, &dn,
890 &mi->mi_targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
892 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
893 "pseudoroot DN '%s' is invalid\n",
894 fname, lineno, argv[ 1 ] );
898 /* password to use as pseudo-root */
899 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
900 int i = mi->mi_ntargets - 1;
903 Debug( LDAP_DEBUG_ANY,
904 "%s: line %d: need \"uri\" directive first\n",
910 Debug( LDAP_DEBUG_ANY,
911 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
915 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ]->mt_pseudorootpw );
918 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
920 int i = mi->mi_ntargets - 1, rc;
921 struct berval dn, nvnc, pvnc, nrnc, prnc;
924 Debug( LDAP_DEBUG_ANY,
925 "%s: line %d: need \"uri\" directive first\n",
933 * suffixmassage <suffix> <massaged suffix>
935 * the <suffix> field must be defined as a valid suffix
936 * (or suffixAlias?) for the current database;
937 * the <massaged suffix> shouldn't have already been
938 * defined as a valid suffix or suffixAlias for the
942 Debug( LDAP_DEBUG_ANY,
943 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
948 ber_str2bv( argv[ 1 ], 0, 0, &dn );
949 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
950 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
951 "suffix '%s' is invalid\n",
952 fname, lineno, argv[ 1 ] );
956 tmp_be = select_backend( &nvnc, 0, 0 );
957 if ( tmp_be != NULL && tmp_be != be ) {
958 Debug( LDAP_DEBUG_ANY,
959 "%s: line %d: suffix already in use by another backend in"
960 " \"suffixMassage <suffix> <massaged suffix>\"\n",
967 ber_str2bv( argv[ 2 ], 0, 0, &dn );
968 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
969 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
970 "massaged suffix '%s' is invalid\n",
971 fname, lineno, argv[ 2 ] );
978 tmp_be = select_backend( &nrnc, 0, 0 );
979 if ( tmp_be != NULL ) {
980 Debug( LDAP_DEBUG_ANY,
981 "%s: line %d: massaged suffix already in use by another backend in"
982 " \"suffixMassage <suffix> <massaged suffix>\"\n",
993 * The suffix massaging is emulated by means of the
994 * rewrite capabilities
995 * FIXME: no extra rewrite capabilities should be added
998 rc = suffix_massage_config( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
999 &pvnc, &nvnc, &prnc, &nrnc );
1001 free( pvnc.bv_val );
1002 free( nvnc.bv_val );
1003 free( prnc.bv_val );
1004 free( nrnc.bv_val );
1008 /* rewrite stuff ... */
1009 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
1010 int i = mi->mi_ntargets - 1;
1013 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
1014 "statement outside target definition.\n",
1019 return rewrite_parse( mi->mi_targets[ i ]->mt_rwmap.rwm_rw,
1020 fname, lineno, argc, argv );
1022 /* objectclass/attribute mapping */
1023 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1024 int i = mi->mi_ntargets - 1;
1027 Debug( LDAP_DEBUG_ANY,
1028 "%s: line %d: need \"uri\" directive first\n",
1033 return ldap_back_map_config( &mi->mi_targets[ i ]->mt_rwmap.rwm_oc,
1034 &mi->mi_targets[ i ]->mt_rwmap.rwm_at,
1035 fname, lineno, argc, argv );
1037 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1038 int i = mi->mi_ntargets - 1;
1039 int nretries = META_RETRY_UNDEFINED;
1042 Debug( LDAP_DEBUG_ANY,
1043 "%s: line %d: need value in \"nretries <value>\"\n",
1048 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1049 nretries = META_RETRY_FOREVER;
1051 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1052 nretries = META_RETRY_NEVER;
1055 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1056 Debug( LDAP_DEBUG_ANY,
1057 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1058 fname, lineno, argv[ 1 ] );
1064 mi->mi_nretries = nretries;
1067 mi->mi_targets[ i ]->mt_nretries = nretries;
1070 } else if ( strcasecmp( argv[ 0 ], "protocol-version" ) == 0 ) {
1071 int *version = mi->mi_ntargets ?
1072 &mi->mi_targets[ mi->mi_ntargets - 1 ]->mt_version
1076 Debug( LDAP_DEBUG_ANY,
1077 "%s: line %d: need value in \"protocol-version <version>\"\n",
1082 if ( lutil_atoi( version, argv[ 1 ] ) != 0 ) {
1083 Debug( LDAP_DEBUG_ANY,
1084 "%s: line %d: unable to parse version \"%s\" in \"protocol-version <version>\"\n",
1085 fname, lineno, argv[ 1 ] );
1089 if ( *version != 0 && ( *version < LDAP_VERSION_MIN || *version > LDAP_VERSION_MAX ) ) {
1090 Debug( LDAP_DEBUG_ANY,
1091 "%s: line %d: unsupported version \"%s\" in \"protocol-version <version>\"\n",
1092 fname, lineno, argv[ 1 ] );
1098 return SLAP_CONF_UNKNOWN;
1104 ldap_back_map_config(
1105 struct ldapmap *oc_map,
1106 struct ldapmap *at_map,
1112 struct ldapmap *map;
1113 struct ldapmapping *mapping;
1117 if ( argc < 3 || argc > 4 ) {
1118 Debug( LDAP_DEBUG_ANY,
1119 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1124 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1128 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1132 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1133 "\"map {objectclass | attribute} [<local> | *] "
1134 "{<foreign> | *}\"\n",
1139 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1140 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1141 map->drop_missing = ( argc < 4 );
1144 src = dst = argv[ 3 ];
1146 } else if ( argc < 4 ) {
1152 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1155 if ( ( map == at_map )
1156 && ( strcasecmp( src, "objectclass" ) == 0
1157 || strcasecmp( dst, "objectclass" ) == 0 ) )
1159 Debug( LDAP_DEBUG_ANY,
1160 "%s: line %d: objectclass attribute cannot be mapped\n",
1164 mapping = (struct ldapmapping *)ch_calloc( 2,
1165 sizeof(struct ldapmapping) );
1166 if ( mapping == NULL ) {
1167 Debug( LDAP_DEBUG_ANY,
1168 "%s: line %d: out of memory\n",
1172 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1173 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1174 mapping[ 1 ].src = mapping[ 0 ].dst;
1175 mapping[ 1 ].dst = mapping[ 0 ].src;
1181 if ( src[ 0 ] != '\0' ) {
1182 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1183 Debug( LDAP_DEBUG_ANY,
1184 "%s: line %d: warning, source objectClass '%s' "
1185 "should be defined in schema\n",
1186 fname, lineno, src );
1189 * FIXME: this should become an err
1195 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1196 Debug( LDAP_DEBUG_ANY,
1197 "%s: line %d: warning, destination objectClass '%s' "
1198 "is not defined in schema\n",
1199 fname, lineno, dst );
1203 const char *text = NULL;
1204 AttributeDescription *ad = NULL;
1206 if ( src[ 0 ] != '\0' ) {
1207 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1208 if ( rc != LDAP_SUCCESS ) {
1209 Debug( LDAP_DEBUG_ANY,
1210 "%s: line %d: warning, source attributeType '%s' "
1211 "should be defined in schema\n",
1212 fname, lineno, src );
1215 * FIXME: this should become an err
1218 * we create a fake "proxied" ad
1222 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1223 &ad, &text, SLAP_AD_PROXIED );
1224 if ( rc != LDAP_SUCCESS ) {
1225 char buf[ SLAP_TEXT_BUFLEN ];
1227 snprintf( buf, sizeof( buf ),
1228 "source attributeType \"%s\": %d (%s)",
1229 src, rc, text ? text : "" );
1230 Debug( LDAP_DEBUG_ANY,
1231 "%s: line %d: %s\n",
1232 fname, lineno, buf );
1240 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1241 if ( rc != LDAP_SUCCESS ) {
1242 Debug( LDAP_DEBUG_ANY,
1243 "%s: line %d: warning, destination attributeType '%s' "
1244 "is not defined in schema\n",
1245 fname, lineno, dst );
1248 * we create a fake "proxied" ad
1252 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1253 &ad, &text, SLAP_AD_PROXIED );
1254 if ( rc != LDAP_SUCCESS ) {
1255 char buf[ SLAP_TEXT_BUFLEN ];
1257 snprintf( buf, sizeof( buf ),
1258 "source attributeType \"%s\": %d (%s)\n",
1259 dst, rc, text ? text : "" );
1260 Debug( LDAP_DEBUG_ANY,
1261 "%s: line %d: %s\n",
1262 fname, lineno, buf );
1268 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1269 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1271 Debug( LDAP_DEBUG_ANY,
1272 "%s: line %d: duplicate mapping found.\n",
1277 if ( src[ 0 ] != '\0' ) {
1278 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1279 mapping_cmp, mapping_dup );
1281 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1282 mapping_cmp, mapping_dup );
1288 ch_free( mapping[ 0 ].src.bv_val );
1289 ch_free( mapping[ 0 ].dst.bv_val );
1297 #ifdef ENABLE_REWRITE
1299 suffix_massage_regexize( const char *s )
1305 if ( s[ 0 ] == '\0' ) {
1306 return ch_strdup( "^(.+)$" );
1310 ( r = strchr( p, ',' ) ) != NULL;
1314 res = ch_calloc( sizeof( char ),
1316 + STRLENOF( "((.+),)?" )
1317 + STRLENOF( "[ ]?" ) * i
1318 + STRLENOF( "$" ) + 1 );
1320 ptr = lutil_strcopy( res, "((.+),)?" );
1322 ( r = strchr( p, ',' ) ) != NULL;
1324 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1325 ptr = lutil_strcopy( ptr, "[ ]?" );
1327 if ( r[ 1 ] == ' ' ) {
1331 ptr = lutil_strcopy( ptr, p );
1340 suffix_massage_patternize( const char *s, const char *p )
1347 if ( s[ 0 ] == '\0' ) {
1351 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1352 if ( res == NULL ) {
1356 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1357 if ( s[ 0 ] == '\0' ) {
1361 lutil_strcopy( ptr, p );
1367 suffix_massage_config(
1368 struct rewrite_info *info,
1369 struct berval *pvnc,
1370 struct berval *nvnc,
1371 struct berval *prnc,
1378 rargv[ 0 ] = "rewriteEngine";
1381 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1383 rargv[ 0 ] = "rewriteContext";
1384 rargv[ 1 ] = "default";
1386 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1388 rargv[ 0 ] = "rewriteRule";
1389 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1390 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1393 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1394 ch_free( rargv[ 1 ] );
1395 ch_free( rargv[ 2 ] );
1397 if ( BER_BVISEMPTY( pvnc ) ) {
1398 rargv[ 0 ] = "rewriteRule";
1400 rargv[ 2 ] = prnc->bv_val;
1403 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1406 rargv[ 0 ] = "rewriteContext";
1407 rargv[ 1 ] = "searchEntryDN";
1409 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1411 rargv[ 0 ] = "rewriteRule";
1412 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1413 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1416 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1417 ch_free( rargv[ 1 ] );
1418 ch_free( rargv[ 2 ] );
1420 if ( BER_BVISEMPTY( prnc ) ) {
1421 rargv[ 0 ] = "rewriteRule";
1423 rargv[ 2 ] = pvnc->bv_val;
1426 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1429 /* backward compatibility */
1430 rargv[ 0 ] = "rewriteContext";
1431 rargv[ 1 ] = "searchResult";
1432 rargv[ 2 ] = "alias";
1433 rargv[ 3 ] = "searchEntryDN";
1435 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1437 rargv[ 0 ] = "rewriteContext";
1438 rargv[ 1 ] = "matchedDN";
1439 rargv[ 2 ] = "alias";
1440 rargv[ 3 ] = "searchEntryDN";
1442 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1444 rargv[ 0 ] = "rewriteContext";
1445 rargv[ 1 ] = "searchAttrDN";
1446 rargv[ 2 ] = "alias";
1447 rargv[ 3 ] = "searchEntryDN";
1449 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1451 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1452 * see servers/slapd/overlays/rwm.h for details */
1453 rargv[ 0 ] = "rewriteContext";
1454 rargv[ 1 ] = "referralAttrDN";
1456 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1458 rargv[ 0 ] = "rewriteContext";
1459 rargv[ 1 ] = "referralDN";
1461 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1465 #endif /* ENABLE_REWRITE */