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;
44 memset( mt, 0, sizeof( metatarget_t ) );
46 mt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
47 if ( mt->mt_rwmap.rwm_rw == NULL ) {
53 * the filter rewrite as a string must be disabled
54 * by default; it can be re-enabled by adding rules;
55 * this creates an empty rewriteContext
57 rargv[ 0 ] = "rewriteContext";
58 rargv[ 1 ] = "searchFilter";
60 rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
62 rargv[ 0 ] = "rewriteContext";
63 rargv[ 1 ] = "default";
65 rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
67 ldap_back_map_init( &mt->mt_rwmap.rwm_at, &mapping );
73 check_true_false( char *str )
75 if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
79 if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
96 metainfo_t *mi = ( metainfo_t * )be->be_private;
100 /* URI of server to query */
101 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
102 int i = mi->mi_ntargets;
105 #endif /* uncomment if uri MUST be a branch of suffix */
106 LDAPURLDesc *ludp, *tmpludp;
113 Debug( LDAP_DEBUG_ANY,
114 "%s: line %d: missing URI "
115 "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
123 Debug( LDAP_DEBUG_ANY,
124 "%s: line %d: too many args "
125 "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
130 if ( be->be_nsuffix == NULL ) {
131 Debug( LDAP_DEBUG_ANY,
132 "%s: line %d: the suffix must be defined before any target.\n",
139 mi->mi_targets = ( metatarget_t * )ch_realloc( mi->mi_targets,
140 sizeof( metatarget_t ) * mi->mi_ntargets );
141 if ( mi->mi_targets == NULL ) {
142 Debug( LDAP_DEBUG_ANY,
143 "%s: line %d: out of memory while storing server name"
144 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
149 if ( meta_back_new_target( &mi->mi_targets[ i ] ) != 0 ) {
150 Debug( LDAP_DEBUG_ANY,
151 "%s: line %d: unable to init server"
152 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
157 mi->mi_targets[ i ].mt_nretries = mi->mi_nretries;
158 mi->mi_targets[ i ].mt_flags = mi->mi_flags;
159 mi->mi_targets[ i ].mt_version = mi->mi_version;
160 mi->mi_targets[ i ].mt_network_timeout = mi->mi_network_timeout;
161 mi->mi_targets[ i ].mt_bind_timeout = mi->mi_bind_timeout;
162 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
163 mi->mi_targets[ i ].mt_timeout[ c ] = mi->mi_timeout[ c ];
169 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
170 Debug( LDAP_DEBUG_ANY,
171 "%s: line %d: unable to parse URI"
172 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
178 * uri MUST have the <dn> part!
180 if ( ludp->lud_dn == NULL ) {
181 Debug( LDAP_DEBUG_ANY,
182 "%s: line %d: missing <naming context> "
183 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
187 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
190 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
191 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
196 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
197 Debug( LDAP_DEBUG_ANY,
198 "%s: line %d: missing <naming context> "
199 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
206 * copies and stores uri and suffix
208 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
209 rc = dnPrettyNormal( NULL, &dn, &mi->mi_targets[ i ].mt_psuffix,
210 &mi->mi_targets[ i ].mt_nsuffix, NULL );
211 if( rc != LDAP_SUCCESS ) {
212 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
213 "target \"%s\" DN is invalid\n",
214 fname, lineno, argv[ 1 ] );
218 ludp->lud_dn[ 0 ] = '\0';
220 switch ( ludp->lud_scope ) {
221 case LDAP_SCOPE_DEFAULT:
222 mi->mi_targets[ i ].mt_scope = LDAP_SCOPE_SUBTREE;
225 case LDAP_SCOPE_SUBTREE:
226 case LDAP_SCOPE_SUBORDINATE:
227 mi->mi_targets[ i ].mt_scope = ludp->lud_scope;
231 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
232 "invalid scope for target \"%s\"\n",
233 fname, lineno, argv[ 1 ] );
237 /* check all, to apply the scope check on the first one */
238 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
239 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
240 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
241 "multiple URIs must have "
249 mi->mi_targets[ i ].mt_uri = ldap_url_list2urls( ludp );
250 ldap_free_urllist( ludp );
251 if ( mi->mi_targets[ i ].mt_uri == NULL) {
252 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
258 * uri MUST be a branch of suffix!
260 #if 0 /* too strict a constraint */
261 if ( select_backend( &mi->mi_targets[ i ].suffix, 0, 0 ) != be ) {
262 Debug( LDAP_DEBUG_ANY,
263 "%s: line %d: <naming context> of URI does not refer to current backend"
264 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
270 * uri MUST be a branch of a suffix!
272 if ( select_backend( &mi->mi_targets[ i ].mt_nsuffix, 0, 0 ) == NULL ) {
273 Debug( LDAP_DEBUG_ANY,
274 "%s: line %d: <naming context> of URI does not resolve to a backend"
275 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
281 /* subtree-exclude */
282 } else if ( strcasecmp( argv[ 0 ], "subtree-exclude" ) == 0 ) {
283 int i = mi->mi_ntargets - 1;
284 struct berval dn, ndn;
287 Debug( LDAP_DEBUG_ANY,
288 "%s: line %d: need \"uri\" directive first\n",
295 Debug( LDAP_DEBUG_ANY,
296 "%s: line %d: missing DN in \"subtree-exclude <DN>\" line\n",
304 Debug( LDAP_DEBUG_ANY,
305 "%s: line %d: too many args in \"subtree-exclude <DN>\" line\n",
310 ber_str2bv( argv[ 1 ], 0, 0, &dn );
311 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL )
314 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
315 "subtree-exclude DN=\"%s\" is invalid\n",
316 fname, lineno, argv[ 1 ] );
320 if ( !dnIsSuffix( &ndn, &mi->mi_targets[ i ].mt_nsuffix ) ) {
321 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
322 "subtree-exclude DN=\"%s\" "
323 "must be subtree of target\n",
324 fname, lineno, argv[ 1 ] );
325 ber_memfree( ndn.bv_val );
329 if ( mi->mi_targets[ i ].mt_subtree_exclude != NULL ) {
332 for ( j = 0; !BER_BVISNULL( &mi->mi_targets[ i ].mt_subtree_exclude[ j ] ); j++ )
334 if ( dnIsSuffix( &mi->mi_targets[ i ].mt_subtree_exclude[ j ], &ndn ) ) {
335 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
336 "subtree-exclude DN=\"%s\" "
337 "is suffix of another subtree-exclude\n",
338 fname, lineno, argv[ 1 ] );
339 /* reject, because it might be superior
340 * to more than one subtree-exclude */
341 ber_memfree( ndn.bv_val );
344 } else if ( dnIsSuffix( &ndn, &mi->mi_targets[ i ].mt_subtree_exclude[ j ] ) ) {
345 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
346 "another subtree-exclude is suffix of "
347 "subtree-exclude DN=\"%s\"\n",
348 fname, lineno, argv[ 1 ] );
349 ber_memfree( ndn.bv_val );
355 ber_bvarray_add( &mi->mi_targets[ i ].mt_subtree_exclude, &ndn );
357 /* default target directive */
358 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
359 int i = mi->mi_ntargets - 1;
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: \"default-target\" alone need be"
365 " inside a \"uri\" directive\n",
369 mi->mi_defaulttarget = i;
372 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
374 Debug( LDAP_DEBUG_ANY,
375 "%s: line %d: \"default-target none\""
376 " should go before uri definitions\n",
379 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
383 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
384 || mi->mi_defaulttarget < 0
385 || mi->mi_defaulttarget >= i - 1 )
387 Debug( LDAP_DEBUG_ANY,
388 "%s: line %d: illegal target number %d\n",
389 fname, lineno, mi->mi_defaulttarget );
395 /* ttl of dn cache */
396 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
398 Debug( LDAP_DEBUG_ANY,
399 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
404 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
405 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
407 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
408 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
413 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
414 Debug( LDAP_DEBUG_ANY,
415 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
416 fname, lineno, argv[ 1 ] );
419 mi->mi_cache.ttl = (time_t)t;
422 /* network timeout when connecting to ldap servers */
423 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
425 time_t *tp = mi->mi_ntargets ?
426 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_network_timeout
427 : &mi->mi_network_timeout;
430 Debug( LDAP_DEBUG_ANY,
431 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
436 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
437 Debug( LDAP_DEBUG_ANY,
438 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
439 fname, lineno, argv[ 1 ] );
446 /* idle timeout when connecting to ldap servers */
447 } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
452 Debug( LDAP_DEBUG_ANY,
453 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
459 Debug( LDAP_DEBUG_ANY,
460 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
465 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
466 Debug( LDAP_DEBUG_ANY,
467 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
468 fname, lineno, argv[ 1 ] );
473 mi->mi_idle_timeout = (time_t)t;
476 } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
481 Debug( LDAP_DEBUG_ANY,
482 "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
488 Debug( LDAP_DEBUG_ANY,
489 "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
494 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
495 Debug( LDAP_DEBUG_ANY,
496 "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
497 fname, lineno, argv[ 1 ] );
502 mi->mi_conn_ttl = (time_t)t;
504 /* bind timeout when connecting to ldap servers */
505 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
507 struct timeval *tp = mi->mi_ntargets ?
508 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_bind_timeout
509 : &mi->mi_bind_timeout;
513 Debug( LDAP_DEBUG_ANY,
514 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
520 Debug( LDAP_DEBUG_ANY,
521 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
526 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
527 Debug( LDAP_DEBUG_ANY,
528 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
529 fname, lineno, argv[ 1 ] );
534 tp->tv_sec = t/1000000;
535 tp->tv_usec = t%1000000;
537 /* name to use for meta_back_group */
538 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
539 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
541 int i = mi->mi_ntargets - 1;
545 Debug( LDAP_DEBUG_ANY,
546 "%s: line %d: need \"uri\" directive first\n",
552 Debug( LDAP_DEBUG_ANY,
553 "%s: line %d: missing name in \"binddn <name>\" line\n",
558 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
559 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
560 "\"binddn\" statement is deprecated; "
561 "use \"acl-authcDN\" instead\n",
563 /* FIXME: some day we'll need to throw an error */
566 ber_str2bv( argv[ 1 ], 0, 0, &dn );
567 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
568 NULL ) != LDAP_SUCCESS )
570 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
571 "bind DN '%s' is invalid\n",
572 fname, lineno, argv[ 1 ] );
576 /* password to use for meta_back_group */
577 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
578 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
580 int i = mi->mi_ntargets - 1;
583 Debug( LDAP_DEBUG_ANY,
584 "%s: line %d: need \"uri\" directive first\n",
590 Debug( LDAP_DEBUG_ANY,
591 "%s: line %d: missing password in \"bindpw <password>\" line\n",
596 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
597 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
598 "\"bindpw\" statement is deprecated; "
599 "use \"acl-passwd\" instead\n",
601 /* FIXME: some day we'll need to throw an error */
604 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
606 /* save bind creds for referral rebinds? */
607 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
609 Debug( LDAP_DEBUG_ANY,
610 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
616 Debug( LDAP_DEBUG_ANY,
617 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
619 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
622 switch ( check_true_false( argv[ 1 ] ) ) {
624 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
628 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
632 Debug( LDAP_DEBUG_ANY,
633 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
634 fname, lineno, argv[ 1 ] );
639 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
640 unsigned *flagsp = mi->mi_ntargets ?
641 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
645 Debug( LDAP_DEBUG_ANY,
646 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
651 /* this is the default; we add it because the default might change... */
652 switch ( check_true_false( argv[ 1 ] ) ) {
654 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
658 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
662 Debug( LDAP_DEBUG_ANY,
663 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
664 fname, lineno, argv[ 1 ] );
668 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
669 unsigned *flagsp = mi->mi_ntargets ?
670 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
674 Debug( LDAP_DEBUG_ANY,
675 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
681 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
682 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
685 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
686 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
687 *flagsp |= LDAP_BACK_F_USE_TLS;
689 /* propagate start tls */
690 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
691 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
694 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
695 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
696 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
699 Debug( LDAP_DEBUG_ANY,
700 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
701 fname, lineno, argv[ 1 ] );
705 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
706 unsigned *flagsp = mi->mi_ntargets ?
707 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
711 Debug( LDAP_DEBUG_ANY,
712 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
717 switch ( check_true_false( argv[ 1 ] ) ) {
719 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
723 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
727 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
728 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
731 Debug( LDAP_DEBUG_ANY,
732 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
733 fname, lineno, argv[ 1 ] );
740 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
742 Debug( LDAP_DEBUG_ANY,
743 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
748 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
749 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
751 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
752 mi->mi_flags |= META_BACK_F_ONERR_STOP;
755 Debug( LDAP_DEBUG_ANY,
756 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
757 fname, lineno, argv[ 1 ] );
762 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
764 Debug( LDAP_DEBUG_ANY,
765 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
770 switch ( check_true_false( argv[ 1 ] ) ) {
772 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
776 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
780 Debug( LDAP_DEBUG_ANY,
781 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
782 fname, lineno, argv[ 1 ] );
786 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
788 time_t *tv = mi->mi_ntargets ?
789 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
794 Debug( LDAP_DEBUG_ANY,
795 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
800 for ( c = 1; c < argc; c++ ) {
804 sep = strchr( argv[ c ], '=' );
806 size_t len = sep - argv[ c ];
808 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
809 t = &tv[ LDAP_BACK_OP_ADD ];
810 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
811 t = &tv[ LDAP_BACK_OP_DELETE ];
812 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
813 t = &tv[ LDAP_BACK_OP_MODIFY ];
814 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
815 t = &tv[ LDAP_BACK_OP_MODRDN ];
817 char buf[ SLAP_TEXT_BUFLEN ];
818 snprintf( buf, sizeof( buf ),
819 "unknown operation \"%s\" for timeout #%d",
821 Debug( LDAP_DEBUG_ANY,
822 "%s: line %d: %s.\n",
823 fname, lineno, buf );
832 if ( lutil_parse_time( sep, &val ) != 0 ) {
833 Debug( LDAP_DEBUG_ANY,
834 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
835 fname, lineno, sep );
845 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
846 tv[ i ] = (time_t)val;
851 /* name to use as pseudo-root dn */
852 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
853 int i = mi->mi_ntargets - 1;
857 Debug( LDAP_DEBUG_ANY,
858 "%s: line %d: need \"uri\" directive first\n",
864 Debug( LDAP_DEBUG_ANY,
865 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
870 dn.bv_val = argv[ 1 ];
871 dn.bv_len = strlen( argv[ 1 ] );
872 if ( dnNormalize( 0, NULL, NULL, &dn,
873 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
875 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
876 "pseudoroot DN '%s' is invalid\n",
877 fname, lineno, argv[ 1 ] );
881 /* password to use as pseudo-root */
882 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
883 int i = mi->mi_ntargets - 1;
886 Debug( LDAP_DEBUG_ANY,
887 "%s: line %d: need \"uri\" directive first\n",
893 Debug( LDAP_DEBUG_ANY,
894 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
898 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
901 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
903 int i = mi->mi_ntargets - 1, rc;
904 struct berval dn, nvnc, pvnc, nrnc, prnc;
907 Debug( LDAP_DEBUG_ANY,
908 "%s: line %d: need \"uri\" directive first\n",
916 * suffixmassage <suffix> <massaged suffix>
918 * the <suffix> field must be defined as a valid suffix
919 * (or suffixAlias?) for the current database;
920 * the <massaged suffix> shouldn't have already been
921 * defined as a valid suffix or suffixAlias for the
925 Debug( LDAP_DEBUG_ANY,
926 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
931 ber_str2bv( argv[ 1 ], 0, 0, &dn );
932 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
933 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
934 "suffix '%s' is invalid\n",
935 fname, lineno, argv[ 1 ] );
939 tmp_be = select_backend( &nvnc, 0, 0 );
940 if ( tmp_be != NULL && tmp_be != be ) {
941 Debug( LDAP_DEBUG_ANY,
942 "%s: line %d: suffix already in use by another backend in"
943 " \"suffixMassage <suffix> <massaged suffix>\"\n",
950 ber_str2bv( argv[ 2 ], 0, 0, &dn );
951 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
952 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
953 "massaged suffix '%s' is invalid\n",
954 fname, lineno, argv[ 2 ] );
961 tmp_be = select_backend( &nrnc, 0, 0 );
962 if ( tmp_be != NULL ) {
963 Debug( LDAP_DEBUG_ANY,
964 "%s: line %d: massaged suffix already in use by another backend in"
965 " \"suffixMassage <suffix> <massaged suffix>\"\n",
976 * The suffix massaging is emulated by means of the
977 * rewrite capabilities
978 * FIXME: no extra rewrite capabilities should be added
981 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
982 &pvnc, &nvnc, &prnc, &nrnc );
991 /* rewrite stuff ... */
992 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
993 int i = mi->mi_ntargets - 1;
996 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
997 "statement outside target definition.\n",
1002 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
1003 fname, lineno, argc, argv );
1005 /* objectclass/attribute mapping */
1006 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
1007 int i = mi->mi_ntargets - 1;
1010 Debug( LDAP_DEBUG_ANY,
1011 "%s: line %d: need \"uri\" directive first\n",
1016 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
1017 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
1018 fname, lineno, argc, argv );
1020 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
1021 int i = mi->mi_ntargets - 1;
1022 int nretries = META_RETRY_UNDEFINED;
1025 Debug( LDAP_DEBUG_ANY,
1026 "%s: line %d: need value in \"nretries <value>\"\n",
1031 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
1032 nretries = META_RETRY_FOREVER;
1034 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
1035 nretries = META_RETRY_NEVER;
1038 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
1039 Debug( LDAP_DEBUG_ANY,
1040 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
1041 fname, lineno, argv[ 1 ] );
1047 mi->mi_nretries = nretries;
1050 mi->mi_targets[ i ].mt_nretries = nretries;
1055 return SLAP_CONF_UNKNOWN;
1061 ldap_back_map_config(
1062 struct ldapmap *oc_map,
1063 struct ldapmap *at_map,
1069 struct ldapmap *map;
1070 struct ldapmapping *mapping;
1074 if ( argc < 3 || argc > 4 ) {
1075 Debug( LDAP_DEBUG_ANY,
1076 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1081 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1085 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1089 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1090 "\"map {objectclass | attribute} [<local> | *] "
1091 "{<foreign> | *}\"\n",
1096 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1097 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1098 map->drop_missing = ( argc < 4 );
1101 src = dst = argv[ 3 ];
1103 } else if ( argc < 4 ) {
1109 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1112 if ( ( map == at_map )
1113 && ( strcasecmp( src, "objectclass" ) == 0
1114 || strcasecmp( dst, "objectclass" ) == 0 ) )
1116 Debug( LDAP_DEBUG_ANY,
1117 "%s: line %d: objectclass attribute cannot be mapped\n",
1121 mapping = (struct ldapmapping *)ch_calloc( 2,
1122 sizeof(struct ldapmapping) );
1123 if ( mapping == NULL ) {
1124 Debug( LDAP_DEBUG_ANY,
1125 "%s: line %d: out of memory\n",
1129 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1130 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1131 mapping[ 1 ].src = mapping[ 0 ].dst;
1132 mapping[ 1 ].dst = mapping[ 0 ].src;
1138 if ( src[ 0 ] != '\0' ) {
1139 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1140 Debug( LDAP_DEBUG_ANY,
1141 "%s: line %d: warning, source objectClass '%s' "
1142 "should be defined in schema\n",
1143 fname, lineno, src );
1146 * FIXME: this should become an err
1152 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1153 Debug( LDAP_DEBUG_ANY,
1154 "%s: line %d: warning, destination objectClass '%s' "
1155 "is not defined in schema\n",
1156 fname, lineno, dst );
1160 const char *text = NULL;
1161 AttributeDescription *ad = NULL;
1163 if ( src[ 0 ] != '\0' ) {
1164 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1165 if ( rc != LDAP_SUCCESS ) {
1166 Debug( LDAP_DEBUG_ANY,
1167 "%s: line %d: warning, source attributeType '%s' "
1168 "should be defined in schema\n",
1169 fname, lineno, src );
1172 * FIXME: this should become an err
1175 * we create a fake "proxied" ad
1179 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1180 &ad, &text, SLAP_AD_PROXIED );
1181 if ( rc != LDAP_SUCCESS ) {
1182 char buf[ SLAP_TEXT_BUFLEN ];
1184 snprintf( buf, sizeof( buf ),
1185 "source attributeType \"%s\": %d (%s)",
1186 src, rc, text ? text : "" );
1187 Debug( LDAP_DEBUG_ANY,
1188 "%s: line %d: %s\n",
1189 fname, lineno, buf );
1197 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1198 if ( rc != LDAP_SUCCESS ) {
1199 Debug( LDAP_DEBUG_ANY,
1200 "%s: line %d: warning, destination attributeType '%s' "
1201 "is not defined in schema\n",
1202 fname, lineno, dst );
1205 * we create a fake "proxied" ad
1209 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1210 &ad, &text, SLAP_AD_PROXIED );
1211 if ( rc != LDAP_SUCCESS ) {
1212 char buf[ SLAP_TEXT_BUFLEN ];
1214 snprintf( buf, sizeof( buf ),
1215 "source attributeType \"%s\": %d (%s)\n",
1216 dst, rc, text ? text : "" );
1217 Debug( LDAP_DEBUG_ANY,
1218 "%s: line %d: %s\n",
1219 fname, lineno, buf );
1225 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1226 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1228 Debug( LDAP_DEBUG_ANY,
1229 "%s: line %d: duplicate mapping found.\n",
1234 if ( src[ 0 ] != '\0' ) {
1235 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1236 mapping_cmp, mapping_dup );
1238 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1239 mapping_cmp, mapping_dup );
1245 ch_free( mapping[ 0 ].src.bv_val );
1246 ch_free( mapping[ 0 ].dst.bv_val );
1254 #ifdef ENABLE_REWRITE
1256 suffix_massage_regexize( const char *s )
1262 if ( s[ 0 ] == '\0' ) {
1263 return ch_strdup( "^(.+)$" );
1267 ( r = strchr( p, ',' ) ) != NULL;
1271 res = ch_calloc( sizeof( char ),
1273 + STRLENOF( "((.+),)?" )
1274 + STRLENOF( "[ ]?" ) * i
1275 + STRLENOF( "$" ) + 1 );
1277 ptr = lutil_strcopy( res, "((.+),)?" );
1279 ( r = strchr( p, ',' ) ) != NULL;
1281 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1282 ptr = lutil_strcopy( ptr, "[ ]?" );
1284 if ( r[ 1 ] == ' ' ) {
1288 ptr = lutil_strcopy( ptr, p );
1297 suffix_massage_patternize( const char *s, const char *p )
1304 if ( s[ 0 ] == '\0' ) {
1308 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1309 if ( res == NULL ) {
1313 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1314 if ( s[ 0 ] == '\0' ) {
1318 lutil_strcopy( ptr, p );
1324 suffix_massage_config(
1325 struct rewrite_info *info,
1326 struct berval *pvnc,
1327 struct berval *nvnc,
1328 struct berval *prnc,
1335 rargv[ 0 ] = "rewriteEngine";
1338 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1340 rargv[ 0 ] = "rewriteContext";
1341 rargv[ 1 ] = "default";
1343 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1345 rargv[ 0 ] = "rewriteRule";
1346 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1347 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1350 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1351 ch_free( rargv[ 1 ] );
1352 ch_free( rargv[ 2 ] );
1354 if ( BER_BVISEMPTY( pvnc ) ) {
1355 rargv[ 0 ] = "rewriteRule";
1357 rargv[ 2 ] = prnc->bv_val;
1360 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1363 rargv[ 0 ] = "rewriteContext";
1364 rargv[ 1 ] = "searchEntryDN";
1366 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1368 rargv[ 0 ] = "rewriteRule";
1369 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1370 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1373 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1374 ch_free( rargv[ 1 ] );
1375 ch_free( rargv[ 2 ] );
1377 if ( BER_BVISEMPTY( prnc ) ) {
1378 rargv[ 0 ] = "rewriteRule";
1380 rargv[ 2 ] = pvnc->bv_val;
1383 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1386 /* backward compatibility */
1387 rargv[ 0 ] = "rewriteContext";
1388 rargv[ 1 ] = "searchResult";
1389 rargv[ 2 ] = "alias";
1390 rargv[ 3 ] = "searchEntryDN";
1392 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1394 rargv[ 0 ] = "rewriteContext";
1395 rargv[ 1 ] = "matchedDN";
1396 rargv[ 2 ] = "alias";
1397 rargv[ 3 ] = "searchEntryDN";
1399 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1401 rargv[ 0 ] = "rewriteContext";
1402 rargv[ 1 ] = "searchAttrDN";
1403 rargv[ 2 ] = "alias";
1404 rargv[ 3 ] = "searchEntryDN";
1406 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1408 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1409 * see servers/slapd/overlays/rwm.h for details */
1410 rargv[ 0 ] = "rewriteContext";
1411 rargv[ 1 ] = "referralAttrDN";
1413 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1415 rargv[ 0 ] = "rewriteContext";
1416 rargv[ 1 ] = "referralDN";
1418 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1422 #endif /* ENABLE_REWRITE */