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_conn_ttl = mi->mi_conn_ttl;
162 mi->mi_targets[ i ].mt_idle_timeout = mi->mi_idle_timeout;
163 mi->mi_targets[ i ].mt_bind_timeout = mi->mi_bind_timeout;
164 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
165 mi->mi_targets[ i ].mt_timeout[ c ] = mi->mi_timeout[ c ];
171 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
172 Debug( LDAP_DEBUG_ANY,
173 "%s: line %d: unable to parse URI"
174 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
180 * uri MUST have the <dn> part!
182 if ( ludp->lud_dn == NULL ) {
183 Debug( LDAP_DEBUG_ANY,
184 "%s: line %d: missing <naming context> "
185 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
189 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
192 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
193 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
198 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
199 Debug( LDAP_DEBUG_ANY,
200 "%s: line %d: missing <naming context> "
201 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
208 * copies and stores uri and suffix
210 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
211 rc = dnPrettyNormal( NULL, &dn, &mi->mi_targets[ i ].mt_psuffix,
212 &mi->mi_targets[ i ].mt_nsuffix, NULL );
213 if( rc != LDAP_SUCCESS ) {
214 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
215 "target \"%s\" DN is invalid\n",
216 fname, lineno, argv[ 1 ] );
220 ludp->lud_dn[ 0 ] = '\0';
222 switch ( ludp->lud_scope ) {
223 case LDAP_SCOPE_DEFAULT:
224 mi->mi_targets[ i ].mt_scope = LDAP_SCOPE_SUBTREE;
227 case LDAP_SCOPE_SUBTREE:
228 case LDAP_SCOPE_SUBORDINATE:
229 mi->mi_targets[ i ].mt_scope = ludp->lud_scope;
233 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
234 "invalid scope for target \"%s\"\n",
235 fname, lineno, argv[ 1 ] );
239 /* check all, to apply the scope check on the first one */
240 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
241 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
242 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
243 "multiple URIs must have "
251 mi->mi_targets[ i ].mt_uri = ldap_url_list2urls( ludp );
252 ldap_free_urllist( ludp );
253 if ( mi->mi_targets[ i ].mt_uri == NULL) {
254 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
260 * uri MUST be a branch of suffix!
262 #if 0 /* too strict a constraint */
263 if ( select_backend( &mi->mi_targets[ i ].suffix, 0, 0 ) != be ) {
264 Debug( LDAP_DEBUG_ANY,
265 "%s: line %d: <naming context> of URI does not refer to current backend"
266 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
272 * uri MUST be a branch of a suffix!
274 if ( select_backend( &mi->mi_targets[ i ].mt_nsuffix, 0, 0 ) == NULL ) {
275 Debug( LDAP_DEBUG_ANY,
276 "%s: line %d: <naming context> of URI does not resolve to a backend"
277 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
283 /* default target directive */
284 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
285 int i = mi->mi_ntargets - 1;
289 Debug( LDAP_DEBUG_ANY,
290 "%s: line %d: \"default-target\" alone need be"
291 " inside a \"uri\" directive\n",
295 mi->mi_defaulttarget = i;
298 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
300 Debug( LDAP_DEBUG_ANY,
301 "%s: line %d: \"default-target none\""
302 " should go before uri definitions\n",
305 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
309 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
310 || mi->mi_defaulttarget < 0
311 || mi->mi_defaulttarget >= i - 1 )
313 Debug( LDAP_DEBUG_ANY,
314 "%s: line %d: illegal target number %d\n",
315 fname, lineno, mi->mi_defaulttarget );
321 /* ttl of dn cache */
322 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
324 Debug( LDAP_DEBUG_ANY,
325 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
330 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
331 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
333 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
334 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
339 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
340 Debug( LDAP_DEBUG_ANY,
341 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
342 fname, lineno, argv[ 1 ] );
345 mi->mi_cache.ttl = (time_t)t;
348 /* network timeout when connecting to ldap servers */
349 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
351 time_t *tp = mi->mi_ntargets ?
352 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_network_timeout
353 : &mi->mi_network_timeout;
356 Debug( LDAP_DEBUG_ANY,
357 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
362 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
365 fname, lineno, argv[ 1 ] );
372 /* idle timeout when connecting to ldap servers */
373 } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
375 time_t *tp = mi->mi_ntargets ?
376 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_idle_timeout
377 : &mi->mi_idle_timeout;
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
388 Debug( LDAP_DEBUG_ANY,
389 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
394 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
395 Debug( LDAP_DEBUG_ANY,
396 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
397 fname, lineno, argv[ 1 ] );
405 } else if ( strcasecmp( argv[ 0 ], "conn-ttl" ) == 0 ) {
407 time_t *tp = mi->mi_ntargets ?
408 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_conn_ttl
413 Debug( LDAP_DEBUG_ANY,
414 "%s: line %d: missing ttl value in \"conn-ttl <seconds>\" line\n",
420 Debug( LDAP_DEBUG_ANY,
421 "%s: line %d: extra cruft after ttl value in \"conn-ttl <seconds>\" line\n",
426 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
427 Debug( LDAP_DEBUG_ANY,
428 "%s: line %d: unable to parse ttl \"%s\" in \"conn-ttl <seconds>\" line\n",
429 fname, lineno, argv[ 1 ] );
436 /* bind timeout when connecting to ldap servers */
437 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
439 struct timeval *tp = mi->mi_ntargets ?
440 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_bind_timeout
441 : &mi->mi_bind_timeout;
445 Debug( LDAP_DEBUG_ANY,
446 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
452 Debug( LDAP_DEBUG_ANY,
453 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
458 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
459 Debug( LDAP_DEBUG_ANY,
460 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
461 fname, lineno, argv[ 1 ] );
466 tp->tv_sec = t/1000000;
467 tp->tv_usec = t%1000000;
469 /* name to use for meta_back_group */
470 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
471 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
473 int i = mi->mi_ntargets - 1;
477 Debug( LDAP_DEBUG_ANY,
478 "%s: line %d: need \"uri\" directive first\n",
484 Debug( LDAP_DEBUG_ANY,
485 "%s: line %d: missing name in \"binddn <name>\" line\n",
490 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
491 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
492 "\"binddn\" statement is deprecated; "
493 "use \"acl-authcDN\" instead\n",
495 /* FIXME: some day we'll need to throw an error */
498 dn.bv_val = argv[ 1 ];
499 dn.bv_len = strlen( argv[ 1 ] );
500 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
501 NULL ) != LDAP_SUCCESS )
503 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
504 "bind DN '%s' is invalid\n",
505 fname, lineno, argv[ 1 ] );
509 /* password to use for meta_back_group */
510 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
511 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
513 int i = mi->mi_ntargets - 1;
516 Debug( LDAP_DEBUG_ANY,
517 "%s: line %d: need \"uri\" directive first\n",
523 Debug( LDAP_DEBUG_ANY,
524 "%s: line %d: missing password in \"bindpw <password>\" line\n",
529 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
530 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
531 "\"bindpw\" statement is deprecated; "
532 "use \"acl-passwd\" instead\n",
534 /* FIXME: some day we'll need to throw an error */
537 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
539 /* save bind creds for referral rebinds? */
540 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
542 Debug( LDAP_DEBUG_ANY,
543 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
549 Debug( LDAP_DEBUG_ANY,
550 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
552 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
555 switch ( check_true_false( argv[ 1 ] ) ) {
557 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
561 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
565 Debug( LDAP_DEBUG_ANY,
566 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
567 fname, lineno, argv[ 1 ] );
572 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
573 unsigned *flagsp = mi->mi_ntargets ?
574 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
578 Debug( LDAP_DEBUG_ANY,
579 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
584 /* this is the default; we add it because the default might change... */
585 switch ( check_true_false( argv[ 1 ] ) ) {
587 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
591 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
595 Debug( LDAP_DEBUG_ANY,
596 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
597 fname, lineno, argv[ 1 ] );
601 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
602 unsigned *flagsp = mi->mi_ntargets ?
603 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
607 Debug( LDAP_DEBUG_ANY,
608 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
614 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
615 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
618 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
619 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
620 *flagsp |= LDAP_BACK_F_USE_TLS;
622 /* propagate start tls */
623 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
624 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
627 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
628 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
629 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
632 Debug( LDAP_DEBUG_ANY,
633 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
634 fname, lineno, argv[ 1 ] );
638 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
639 unsigned *flagsp = mi->mi_ntargets ?
640 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
644 Debug( LDAP_DEBUG_ANY,
645 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
650 switch ( check_true_false( argv[ 1 ] ) ) {
652 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
656 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
660 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
661 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
664 Debug( LDAP_DEBUG_ANY,
665 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
666 fname, lineno, argv[ 1 ] );
673 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
675 Debug( LDAP_DEBUG_ANY,
676 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
681 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
682 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
684 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
685 mi->mi_flags |= META_BACK_F_ONERR_STOP;
688 Debug( LDAP_DEBUG_ANY,
689 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
690 fname, lineno, argv[ 1 ] );
695 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
697 Debug( LDAP_DEBUG_ANY,
698 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
703 switch ( check_true_false( argv[ 1 ] ) ) {
705 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
709 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
713 Debug( LDAP_DEBUG_ANY,
714 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
715 fname, lineno, argv[ 1 ] );
719 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
721 time_t *tv = mi->mi_ntargets ?
722 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
727 Debug( LDAP_DEBUG_ANY,
728 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
733 for ( c = 1; c < argc; c++ ) {
737 sep = strchr( argv[ c ], '=' );
739 size_t len = sep - argv[ c ];
741 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
742 t = &tv[ LDAP_BACK_OP_ADD ];
743 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
744 t = &tv[ LDAP_BACK_OP_DELETE ];
745 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
746 t = &tv[ LDAP_BACK_OP_MODIFY ];
747 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
748 t = &tv[ LDAP_BACK_OP_MODRDN ];
750 char buf[ SLAP_TEXT_BUFLEN ];
751 snprintf( buf, sizeof( buf ),
752 "unknown operation \"%s\" for timeout #%d",
754 Debug( LDAP_DEBUG_ANY,
755 "%s: line %d: %s.\n",
756 fname, lineno, buf );
765 if ( lutil_parse_time( sep, &val ) != 0 ) {
766 Debug( LDAP_DEBUG_ANY,
767 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
768 fname, lineno, sep );
778 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
779 tv[ i ] = (time_t)val;
784 /* name to use as pseudo-root dn */
785 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
786 int i = mi->mi_ntargets - 1;
790 Debug( LDAP_DEBUG_ANY,
791 "%s: line %d: need \"uri\" directive first\n",
797 Debug( LDAP_DEBUG_ANY,
798 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
803 dn.bv_val = argv[ 1 ];
804 dn.bv_len = strlen( argv[ 1 ] );
805 if ( dnNormalize( 0, NULL, NULL, &dn,
806 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
808 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
809 "pseudoroot DN '%s' is invalid\n",
810 fname, lineno, argv[ 1 ] );
814 /* password to use as pseudo-root */
815 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
816 int i = mi->mi_ntargets - 1;
819 Debug( LDAP_DEBUG_ANY,
820 "%s: line %d: need \"uri\" directive first\n",
826 Debug( LDAP_DEBUG_ANY,
827 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
831 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
834 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
836 int i = mi->mi_ntargets - 1, rc;
837 struct berval dn, nvnc, pvnc, nrnc, prnc;
840 Debug( LDAP_DEBUG_ANY,
841 "%s: line %d: need \"uri\" directive first\n",
849 * suffixmassage <suffix> <massaged suffix>
851 * the <suffix> field must be defined as a valid suffix
852 * (or suffixAlias?) for the current database;
853 * the <massaged suffix> shouldn't have already been
854 * defined as a valid suffix or suffixAlias for the
858 Debug( LDAP_DEBUG_ANY,
859 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
864 ber_str2bv( argv[ 1 ], 0, 0, &dn );
865 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
866 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
867 "suffix '%s' is invalid\n",
868 fname, lineno, argv[ 1 ] );
872 tmp_be = select_backend( &nvnc, 0, 0 );
873 if ( tmp_be != NULL && tmp_be != be ) {
874 Debug( LDAP_DEBUG_ANY,
875 "%s: line %d: suffix already in use by another backend in"
876 " \"suffixMassage <suffix> <massaged suffix>\"\n",
883 ber_str2bv( argv[ 2 ], 0, 0, &dn );
884 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
885 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
886 "massaged suffix '%s' is invalid\n",
887 fname, lineno, argv[ 2 ] );
894 tmp_be = select_backend( &nrnc, 0, 0 );
895 if ( tmp_be != NULL ) {
896 Debug( LDAP_DEBUG_ANY,
897 "%s: line %d: massaged suffix already in use by another backend in"
898 " \"suffixMassage <suffix> <massaged suffix>\"\n",
909 * The suffix massaging is emulated by means of the
910 * rewrite capabilities
911 * FIXME: no extra rewrite capabilities should be added
914 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
915 &pvnc, &nvnc, &prnc, &nrnc );
924 /* rewrite stuff ... */
925 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
926 int i = mi->mi_ntargets - 1;
929 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
930 "statement outside target definition.\n",
935 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
936 fname, lineno, argc, argv );
938 /* objectclass/attribute mapping */
939 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
940 int i = mi->mi_ntargets - 1;
943 Debug( LDAP_DEBUG_ANY,
944 "%s: line %d: need \"uri\" directive first\n",
949 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
950 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
951 fname, lineno, argc, argv );
953 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
954 int i = mi->mi_ntargets - 1;
955 int nretries = META_RETRY_UNDEFINED;
958 Debug( LDAP_DEBUG_ANY,
959 "%s: line %d: need value in \"nretries <value>\"\n",
964 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
965 nretries = META_RETRY_FOREVER;
967 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
968 nretries = META_RETRY_NEVER;
971 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
972 Debug( LDAP_DEBUG_ANY,
973 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
974 fname, lineno, argv[ 1 ] );
980 mi->mi_nretries = nretries;
983 mi->mi_targets[ i ].mt_nretries = nretries;
988 return SLAP_CONF_UNKNOWN;
994 ldap_back_map_config(
995 struct ldapmap *oc_map,
996 struct ldapmap *at_map,
1002 struct ldapmap *map;
1003 struct ldapmapping *mapping;
1007 if ( argc < 3 || argc > 4 ) {
1008 Debug( LDAP_DEBUG_ANY,
1009 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
1014 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
1018 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
1022 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
1023 "\"map {objectclass | attribute} [<local> | *] "
1024 "{<foreign> | *}\"\n",
1029 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1030 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1031 map->drop_missing = ( argc < 4 );
1034 src = dst = argv[ 3 ];
1036 } else if ( argc < 4 ) {
1042 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1045 if ( ( map == at_map )
1046 && ( strcasecmp( src, "objectclass" ) == 0
1047 || strcasecmp( dst, "objectclass" ) == 0 ) )
1049 Debug( LDAP_DEBUG_ANY,
1050 "%s: line %d: objectclass attribute cannot be mapped\n",
1054 mapping = (struct ldapmapping *)ch_calloc( 2,
1055 sizeof(struct ldapmapping) );
1056 if ( mapping == NULL ) {
1057 Debug( LDAP_DEBUG_ANY,
1058 "%s: line %d: out of memory\n",
1062 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1063 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1064 mapping[ 1 ].src = mapping[ 0 ].dst;
1065 mapping[ 1 ].dst = mapping[ 0 ].src;
1071 if ( src[ 0 ] != '\0' ) {
1072 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1073 Debug( LDAP_DEBUG_ANY,
1074 "%s: line %d: warning, source objectClass '%s' "
1075 "should be defined in schema\n",
1076 fname, lineno, src );
1079 * FIXME: this should become an err
1085 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1086 Debug( LDAP_DEBUG_ANY,
1087 "%s: line %d: warning, destination objectClass '%s' "
1088 "is not defined in schema\n",
1089 fname, lineno, dst );
1093 const char *text = NULL;
1094 AttributeDescription *ad = NULL;
1096 if ( src[ 0 ] != '\0' ) {
1097 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1098 if ( rc != LDAP_SUCCESS ) {
1099 Debug( LDAP_DEBUG_ANY,
1100 "%s: line %d: warning, source attributeType '%s' "
1101 "should be defined in schema\n",
1102 fname, lineno, src );
1105 * FIXME: this should become an err
1108 * we create a fake "proxied" ad
1112 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1113 &ad, &text, SLAP_AD_PROXIED );
1114 if ( rc != LDAP_SUCCESS ) {
1115 char buf[ SLAP_TEXT_BUFLEN ];
1117 snprintf( buf, sizeof( buf ),
1118 "source attributeType \"%s\": %d (%s)",
1119 src, rc, text ? text : "" );
1120 Debug( LDAP_DEBUG_ANY,
1121 "%s: line %d: %s\n",
1122 fname, lineno, buf );
1130 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1131 if ( rc != LDAP_SUCCESS ) {
1132 Debug( LDAP_DEBUG_ANY,
1133 "%s: line %d: warning, destination attributeType '%s' "
1134 "is not defined in schema\n",
1135 fname, lineno, dst );
1138 * we create a fake "proxied" ad
1142 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1143 &ad, &text, SLAP_AD_PROXIED );
1144 if ( rc != LDAP_SUCCESS ) {
1145 char buf[ SLAP_TEXT_BUFLEN ];
1147 snprintf( buf, sizeof( buf ),
1148 "source attributeType \"%s\": %d (%s)\n",
1149 dst, rc, text ? text : "" );
1150 Debug( LDAP_DEBUG_ANY,
1151 "%s: line %d: %s\n",
1152 fname, lineno, buf );
1158 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1159 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1161 Debug( LDAP_DEBUG_ANY,
1162 "%s: line %d: duplicate mapping found.\n",
1167 if ( src[ 0 ] != '\0' ) {
1168 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1169 mapping_cmp, mapping_dup );
1171 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1172 mapping_cmp, mapping_dup );
1178 ch_free( mapping[ 0 ].src.bv_val );
1179 ch_free( mapping[ 0 ].dst.bv_val );
1187 #ifdef ENABLE_REWRITE
1189 suffix_massage_regexize( const char *s )
1195 if ( s[ 0 ] == '\0' ) {
1196 return ch_strdup( "^(.+)$" );
1200 ( r = strchr( p, ',' ) ) != NULL;
1204 res = ch_calloc( sizeof( char ),
1206 + STRLENOF( "((.+),)?" )
1207 + STRLENOF( "[ ]?" ) * i
1208 + STRLENOF( "$" ) + 1 );
1210 ptr = lutil_strcopy( res, "((.+),)?" );
1212 ( r = strchr( p, ',' ) ) != NULL;
1214 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1215 ptr = lutil_strcopy( ptr, "[ ]?" );
1217 if ( r[ 1 ] == ' ' ) {
1221 ptr = lutil_strcopy( ptr, p );
1230 suffix_massage_patternize( const char *s, const char *p )
1237 if ( s[ 0 ] == '\0' ) {
1241 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1242 if ( res == NULL ) {
1246 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1247 if ( s[ 0 ] == '\0' ) {
1251 lutil_strcopy( ptr, p );
1257 suffix_massage_config(
1258 struct rewrite_info *info,
1259 struct berval *pvnc,
1260 struct berval *nvnc,
1261 struct berval *prnc,
1268 rargv[ 0 ] = "rewriteEngine";
1271 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1273 rargv[ 0 ] = "rewriteContext";
1274 rargv[ 1 ] = "default";
1276 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1278 rargv[ 0 ] = "rewriteRule";
1279 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1280 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1283 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1284 ch_free( rargv[ 1 ] );
1285 ch_free( rargv[ 2 ] );
1287 if ( BER_BVISEMPTY( pvnc ) ) {
1288 rargv[ 0 ] = "rewriteRule";
1290 rargv[ 2 ] = prnc->bv_val;
1293 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1296 rargv[ 0 ] = "rewriteContext";
1297 rargv[ 1 ] = "searchEntryDN";
1299 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1301 rargv[ 0 ] = "rewriteRule";
1302 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1303 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1306 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1307 ch_free( rargv[ 1 ] );
1308 ch_free( rargv[ 2 ] );
1310 if ( BER_BVISEMPTY( prnc ) ) {
1311 rargv[ 0 ] = "rewriteRule";
1313 rargv[ 2 ] = pvnc->bv_val;
1316 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1319 /* backward compatibility */
1320 rargv[ 0 ] = "rewriteContext";
1321 rargv[ 1 ] = "searchResult";
1322 rargv[ 2 ] = "alias";
1323 rargv[ 3 ] = "searchEntryDN";
1325 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1327 rargv[ 0 ] = "rewriteContext";
1328 rargv[ 1 ] = "matchedDN";
1329 rargv[ 2 ] = "alias";
1330 rargv[ 3 ] = "searchEntryDN";
1332 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1334 rargv[ 0 ] = "rewriteContext";
1335 rargv[ 1 ] = "searchAttrDN";
1336 rargv[ 2 ] = "alias";
1337 rargv[ 3 ] = "searchEntryDN";
1339 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1341 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1342 * see servers/slapd/overlays/rwm.h for details */
1343 rargv[ 0 ] = "rewriteContext";
1344 rargv[ 1 ] = "referralAttrDN";
1346 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1348 rargv[ 0 ] = "rewriteContext";
1349 rargv[ 1 ] = "referralDN";
1351 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1355 #endif /* ENABLE_REWRITE */