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_idle_timeout = mi->mi_idle_timeout;
162 mi->mi_targets[ i ].mt_bind_timeout = mi->mi_bind_timeout;
163 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
164 mi->mi_targets[ i ].mt_timeout[ c ] = mi->mi_timeout[ c ];
170 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
171 Debug( LDAP_DEBUG_ANY,
172 "%s: line %d: unable to parse URI"
173 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
179 * uri MUST have the <dn> part!
181 if ( ludp->lud_dn == NULL ) {
182 Debug( LDAP_DEBUG_ANY,
183 "%s: line %d: missing <naming context> "
184 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
188 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
191 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
192 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
197 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
198 Debug( LDAP_DEBUG_ANY,
199 "%s: line %d: missing <naming context> "
200 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
207 * copies and stores uri and suffix
209 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
210 rc = dnPrettyNormal( NULL, &dn, &mi->mi_targets[ i ].mt_psuffix,
211 &mi->mi_targets[ i ].mt_nsuffix, NULL );
212 if( rc != LDAP_SUCCESS ) {
213 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
214 "target \"%s\" DN is invalid\n",
215 fname, lineno, argv[ 1 ] );
219 ludp->lud_dn[ 0 ] = '\0';
221 switch ( ludp->lud_scope ) {
222 case LDAP_SCOPE_DEFAULT:
223 mi->mi_targets[ i ].mt_scope = LDAP_SCOPE_SUBTREE;
226 case LDAP_SCOPE_SUBTREE:
227 case LDAP_SCOPE_SUBORDINATE:
228 mi->mi_targets[ i ].mt_scope = ludp->lud_scope;
232 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
233 "invalid scope for target \"%s\"\n",
234 fname, lineno, argv[ 1 ] );
238 /* check all, to apply the scope check on the first one */
239 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
240 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
241 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
242 "multiple URIs must have "
250 mi->mi_targets[ i ].mt_uri = ldap_url_list2urls( ludp );
251 ldap_free_urllist( ludp );
252 if ( mi->mi_targets[ i ].mt_uri == NULL) {
253 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
259 * uri MUST be a branch of suffix!
261 #if 0 /* too strict a constraint */
262 if ( select_backend( &mi->mi_targets[ i ].suffix, 0, 0 ) != be ) {
263 Debug( LDAP_DEBUG_ANY,
264 "%s: line %d: <naming context> of URI does not refer to current backend"
265 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
271 * uri MUST be a branch of a suffix!
273 if ( select_backend( &mi->mi_targets[ i ].mt_nsuffix, 0, 0 ) == NULL ) {
274 Debug( LDAP_DEBUG_ANY,
275 "%s: line %d: <naming context> of URI does not resolve to a backend"
276 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
282 /* default target directive */
283 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
284 int i = mi->mi_ntargets - 1;
288 Debug( LDAP_DEBUG_ANY,
289 "%s: line %d: \"default-target\" alone need be"
290 " inside a \"uri\" directive\n",
294 mi->mi_defaulttarget = i;
297 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "%s: line %d: \"default-target none\""
301 " should go before uri definitions\n",
304 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
308 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
309 || mi->mi_defaulttarget < 0
310 || mi->mi_defaulttarget >= i - 1 )
312 Debug( LDAP_DEBUG_ANY,
313 "%s: line %d: illegal target number %d\n",
314 fname, lineno, mi->mi_defaulttarget );
320 /* ttl of dn cache */
321 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
323 Debug( LDAP_DEBUG_ANY,
324 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
329 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
330 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
332 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
333 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
338 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
339 Debug( LDAP_DEBUG_ANY,
340 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
341 fname, lineno, argv[ 1 ] );
344 mi->mi_cache.ttl = (time_t)t;
347 /* network timeout when connecting to ldap servers */
348 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
350 time_t *tp = mi->mi_ntargets ?
351 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_network_timeout
352 : &mi->mi_network_timeout;
355 Debug( LDAP_DEBUG_ANY,
356 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
361 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
362 Debug( LDAP_DEBUG_ANY,
363 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
364 fname, lineno, argv[ 1 ] );
371 /* idle timeout when connecting to ldap servers */
372 } else if ( strcasecmp( argv[ 0 ], "idle-timeout" ) == 0 ) {
374 time_t *tp = mi->mi_ntargets ?
375 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_idle_timeout
376 : &mi->mi_idle_timeout;
380 Debug( LDAP_DEBUG_ANY,
381 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
387 Debug( LDAP_DEBUG_ANY,
388 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
393 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
394 Debug( LDAP_DEBUG_ANY,
395 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
396 fname, lineno, argv[ 1 ] );
403 /* bind timeout when connecting to ldap servers */
404 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
406 struct timeval *tp = mi->mi_ntargets ?
407 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_bind_timeout
408 : &mi->mi_bind_timeout;
412 Debug( LDAP_DEBUG_ANY,
413 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
419 Debug( LDAP_DEBUG_ANY,
420 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
425 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
426 Debug( LDAP_DEBUG_ANY,
427 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
428 fname, lineno, argv[ 1 ] );
433 tp->tv_sec = t/1000000;
434 tp->tv_usec = t%1000000;
436 /* name to use for meta_back_group */
437 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
438 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
440 int i = mi->mi_ntargets - 1;
444 Debug( LDAP_DEBUG_ANY,
445 "%s: line %d: need \"uri\" directive first\n",
451 Debug( LDAP_DEBUG_ANY,
452 "%s: line %d: missing name in \"binddn <name>\" line\n",
457 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
458 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
459 "\"binddn\" statement is deprecated; "
460 "use \"acl-authcDN\" instead\n",
462 /* FIXME: some day we'll need to throw an error */
465 dn.bv_val = argv[ 1 ];
466 dn.bv_len = strlen( argv[ 1 ] );
467 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
468 NULL ) != LDAP_SUCCESS )
470 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
471 "bind DN '%s' is invalid\n",
472 fname, lineno, argv[ 1 ] );
476 /* password to use for meta_back_group */
477 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
478 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
480 int i = mi->mi_ntargets - 1;
483 Debug( LDAP_DEBUG_ANY,
484 "%s: line %d: need \"uri\" directive first\n",
490 Debug( LDAP_DEBUG_ANY,
491 "%s: line %d: missing password in \"bindpw <password>\" line\n",
496 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
497 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
498 "\"bindpw\" statement is deprecated; "
499 "use \"acl-passwd\" instead\n",
501 /* FIXME: some day we'll need to throw an error */
504 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
506 /* save bind creds for referral rebinds? */
507 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
509 Debug( LDAP_DEBUG_ANY,
510 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
516 Debug( LDAP_DEBUG_ANY,
517 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
519 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
522 switch ( check_true_false( argv[ 1 ] ) ) {
524 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
528 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
532 Debug( LDAP_DEBUG_ANY,
533 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
534 fname, lineno, argv[ 1 ] );
539 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
540 unsigned *flagsp = mi->mi_ntargets ?
541 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
545 Debug( LDAP_DEBUG_ANY,
546 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
551 /* this is the default; we add it because the default might change... */
552 switch ( check_true_false( argv[ 1 ] ) ) {
554 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
558 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
562 Debug( LDAP_DEBUG_ANY,
563 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
564 fname, lineno, argv[ 1 ] );
568 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
569 unsigned *flagsp = mi->mi_ntargets ?
570 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
574 Debug( LDAP_DEBUG_ANY,
575 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
581 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
582 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
585 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
586 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
587 *flagsp |= LDAP_BACK_F_USE_TLS;
589 /* propagate start tls */
590 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
591 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
594 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
595 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
596 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
599 Debug( LDAP_DEBUG_ANY,
600 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
601 fname, lineno, argv[ 1 ] );
605 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
606 unsigned *flagsp = mi->mi_ntargets ?
607 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
611 Debug( LDAP_DEBUG_ANY,
612 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
617 switch ( check_true_false( argv[ 1 ] ) ) {
619 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
623 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
627 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
628 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
631 Debug( LDAP_DEBUG_ANY,
632 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
633 fname, lineno, argv[ 1 ] );
640 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
642 Debug( LDAP_DEBUG_ANY,
643 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
648 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
649 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
651 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
652 mi->mi_flags |= META_BACK_F_ONERR_STOP;
655 Debug( LDAP_DEBUG_ANY,
656 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
657 fname, lineno, argv[ 1 ] );
662 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
664 Debug( LDAP_DEBUG_ANY,
665 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
670 switch ( check_true_false( argv[ 1 ] ) ) {
672 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
676 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
680 Debug( LDAP_DEBUG_ANY,
681 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
682 fname, lineno, argv[ 1 ] );
686 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
688 time_t *tv = mi->mi_ntargets ?
689 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
694 Debug( LDAP_DEBUG_ANY,
695 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
700 for ( c = 1; c < argc; c++ ) {
704 sep = strchr( argv[ c ], '=' );
706 size_t len = sep - argv[ c ];
708 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
709 t = &tv[ LDAP_BACK_OP_ADD ];
710 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
711 t = &tv[ LDAP_BACK_OP_DELETE ];
712 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
713 t = &tv[ LDAP_BACK_OP_MODIFY ];
714 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
715 t = &tv[ LDAP_BACK_OP_MODRDN ];
717 char buf[ SLAP_TEXT_BUFLEN ];
718 snprintf( buf, sizeof( buf ),
719 "unknown operation \"%s\" for timeout #%d",
721 Debug( LDAP_DEBUG_ANY,
722 "%s: line %d: %s.\n",
723 fname, lineno, buf );
732 if ( lutil_parse_time( sep, &val ) != 0 ) {
733 Debug( LDAP_DEBUG_ANY,
734 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
735 fname, lineno, sep );
745 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
746 tv[ i ] = (time_t)val;
751 /* name to use as pseudo-root dn */
752 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
753 int i = mi->mi_ntargets - 1;
757 Debug( LDAP_DEBUG_ANY,
758 "%s: line %d: need \"uri\" directive first\n",
764 Debug( LDAP_DEBUG_ANY,
765 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
770 dn.bv_val = argv[ 1 ];
771 dn.bv_len = strlen( argv[ 1 ] );
772 if ( dnNormalize( 0, NULL, NULL, &dn,
773 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
775 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
776 "pseudoroot DN '%s' is invalid\n",
777 fname, lineno, argv[ 1 ] );
781 /* password to use as pseudo-root */
782 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
783 int i = mi->mi_ntargets - 1;
786 Debug( LDAP_DEBUG_ANY,
787 "%s: line %d: need \"uri\" directive first\n",
793 Debug( LDAP_DEBUG_ANY,
794 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
798 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
801 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
803 int i = mi->mi_ntargets - 1, rc;
804 struct berval dn, nvnc, pvnc, nrnc, prnc;
807 Debug( LDAP_DEBUG_ANY,
808 "%s: line %d: need \"uri\" directive first\n",
816 * suffixmassage <suffix> <massaged suffix>
818 * the <suffix> field must be defined as a valid suffix
819 * (or suffixAlias?) for the current database;
820 * the <massaged suffix> shouldn't have already been
821 * defined as a valid suffix or suffixAlias for the
825 Debug( LDAP_DEBUG_ANY,
826 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
831 ber_str2bv( argv[ 1 ], 0, 0, &dn );
832 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
833 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
834 "suffix '%s' is invalid\n",
835 fname, lineno, argv[ 1 ] );
839 tmp_be = select_backend( &nvnc, 0, 0 );
840 if ( tmp_be != NULL && tmp_be != be ) {
841 Debug( LDAP_DEBUG_ANY,
842 "%s: line %d: suffix already in use by another backend in"
843 " \"suffixMassage <suffix> <massaged suffix>\"\n",
850 ber_str2bv( argv[ 2 ], 0, 0, &dn );
851 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
852 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
853 "massaged suffix '%s' is invalid\n",
854 fname, lineno, argv[ 2 ] );
861 tmp_be = select_backend( &nrnc, 0, 0 );
862 if ( tmp_be != NULL ) {
863 Debug( LDAP_DEBUG_ANY,
864 "%s: line %d: massaged suffix already in use by another backend in"
865 " \"suffixMassage <suffix> <massaged suffix>\"\n",
876 * The suffix massaging is emulated by means of the
877 * rewrite capabilities
878 * FIXME: no extra rewrite capabilities should be added
881 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
882 &pvnc, &nvnc, &prnc, &nrnc );
891 /* rewrite stuff ... */
892 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
893 int i = mi->mi_ntargets - 1;
896 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
897 "statement outside target definition.\n",
902 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
903 fname, lineno, argc, argv );
905 /* objectclass/attribute mapping */
906 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
907 int i = mi->mi_ntargets - 1;
910 Debug( LDAP_DEBUG_ANY,
911 "%s: line %d: need \"uri\" directive first\n",
916 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
917 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
918 fname, lineno, argc, argv );
920 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
921 int i = mi->mi_ntargets - 1;
922 int nretries = META_RETRY_UNDEFINED;
925 Debug( LDAP_DEBUG_ANY,
926 "%s: line %d: need value in \"nretries <value>\"\n",
931 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
932 nretries = META_RETRY_FOREVER;
934 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
935 nretries = META_RETRY_NEVER;
938 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
939 Debug( LDAP_DEBUG_ANY,
940 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
941 fname, lineno, argv[ 1 ] );
947 mi->mi_nretries = nretries;
950 mi->mi_targets[ i ].mt_nretries = nretries;
955 return SLAP_CONF_UNKNOWN;
961 ldap_back_map_config(
962 struct ldapmap *oc_map,
963 struct ldapmap *at_map,
970 struct ldapmapping *mapping;
974 if ( argc < 3 || argc > 4 ) {
975 Debug( LDAP_DEBUG_ANY,
976 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
981 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
985 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
989 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
990 "\"map {objectclass | attribute} [<local> | *] "
991 "{<foreign> | *}\"\n",
996 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
997 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
998 map->drop_missing = ( argc < 4 );
1001 src = dst = argv[ 3 ];
1003 } else if ( argc < 4 ) {
1009 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1012 if ( ( map == at_map )
1013 && ( strcasecmp( src, "objectclass" ) == 0
1014 || strcasecmp( dst, "objectclass" ) == 0 ) )
1016 Debug( LDAP_DEBUG_ANY,
1017 "%s: line %d: objectclass attribute cannot be mapped\n",
1021 mapping = (struct ldapmapping *)ch_calloc( 2,
1022 sizeof(struct ldapmapping) );
1023 if ( mapping == NULL ) {
1024 Debug( LDAP_DEBUG_ANY,
1025 "%s: line %d: out of memory\n",
1029 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1030 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1031 mapping[ 1 ].src = mapping[ 0 ].dst;
1032 mapping[ 1 ].dst = mapping[ 0 ].src;
1038 if ( src[ 0 ] != '\0' ) {
1039 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1040 Debug( LDAP_DEBUG_ANY,
1041 "%s: line %d: warning, source objectClass '%s' "
1042 "should be defined in schema\n",
1043 fname, lineno, src );
1046 * FIXME: this should become an err
1052 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1053 Debug( LDAP_DEBUG_ANY,
1054 "%s: line %d: warning, destination objectClass '%s' "
1055 "is not defined in schema\n",
1056 fname, lineno, dst );
1060 const char *text = NULL;
1061 AttributeDescription *ad = NULL;
1063 if ( src[ 0 ] != '\0' ) {
1064 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1065 if ( rc != LDAP_SUCCESS ) {
1066 Debug( LDAP_DEBUG_ANY,
1067 "%s: line %d: warning, source attributeType '%s' "
1068 "should be defined in schema\n",
1069 fname, lineno, src );
1072 * FIXME: this should become an err
1075 * we create a fake "proxied" ad
1079 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1080 &ad, &text, SLAP_AD_PROXIED );
1081 if ( rc != LDAP_SUCCESS ) {
1082 char buf[ SLAP_TEXT_BUFLEN ];
1084 snprintf( buf, sizeof( buf ),
1085 "source attributeType \"%s\": %d (%s)",
1086 src, rc, text ? text : "" );
1087 Debug( LDAP_DEBUG_ANY,
1088 "%s: line %d: %s\n",
1089 fname, lineno, buf );
1097 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1098 if ( rc != LDAP_SUCCESS ) {
1099 Debug( LDAP_DEBUG_ANY,
1100 "%s: line %d: warning, destination attributeType '%s' "
1101 "is not defined in schema\n",
1102 fname, lineno, dst );
1105 * we create a fake "proxied" ad
1109 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1110 &ad, &text, SLAP_AD_PROXIED );
1111 if ( rc != LDAP_SUCCESS ) {
1112 char buf[ SLAP_TEXT_BUFLEN ];
1114 snprintf( buf, sizeof( buf ),
1115 "source attributeType \"%s\": %d (%s)\n",
1116 dst, rc, text ? text : "" );
1117 Debug( LDAP_DEBUG_ANY,
1118 "%s: line %d: %s\n",
1119 fname, lineno, buf );
1125 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1126 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1128 Debug( LDAP_DEBUG_ANY,
1129 "%s: line %d: duplicate mapping found.\n",
1134 if ( src[ 0 ] != '\0' ) {
1135 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1136 mapping_cmp, mapping_dup );
1138 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1139 mapping_cmp, mapping_dup );
1145 ch_free( mapping[ 0 ].src.bv_val );
1146 ch_free( mapping[ 0 ].dst.bv_val );
1154 #ifdef ENABLE_REWRITE
1156 suffix_massage_regexize( const char *s )
1162 if ( s[ 0 ] == '\0' ) {
1163 return ch_strdup( "^(.+)$" );
1167 ( r = strchr( p, ',' ) ) != NULL;
1171 res = ch_calloc( sizeof( char ),
1173 + STRLENOF( "((.+),)?" )
1174 + STRLENOF( "[ ]?" ) * i
1175 + STRLENOF( "$" ) + 1 );
1177 ptr = lutil_strcopy( res, "((.+),)?" );
1179 ( r = strchr( p, ',' ) ) != NULL;
1181 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1182 ptr = lutil_strcopy( ptr, "[ ]?" );
1184 if ( r[ 1 ] == ' ' ) {
1188 ptr = lutil_strcopy( ptr, p );
1197 suffix_massage_patternize( const char *s, const char *p )
1204 if ( s[ 0 ] == '\0' ) {
1208 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1209 if ( res == NULL ) {
1213 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1214 if ( s[ 0 ] == '\0' ) {
1218 lutil_strcopy( ptr, p );
1224 suffix_massage_config(
1225 struct rewrite_info *info,
1226 struct berval *pvnc,
1227 struct berval *nvnc,
1228 struct berval *prnc,
1235 rargv[ 0 ] = "rewriteEngine";
1238 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1240 rargv[ 0 ] = "rewriteContext";
1241 rargv[ 1 ] = "default";
1243 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1245 rargv[ 0 ] = "rewriteRule";
1246 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1247 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1250 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1251 ch_free( rargv[ 1 ] );
1252 ch_free( rargv[ 2 ] );
1254 if ( BER_BVISEMPTY( pvnc ) ) {
1255 rargv[ 0 ] = "rewriteRule";
1257 rargv[ 2 ] = prnc->bv_val;
1260 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1263 rargv[ 0 ] = "rewriteContext";
1264 rargv[ 1 ] = "searchEntryDN";
1266 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1268 rargv[ 0 ] = "rewriteRule";
1269 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1270 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1273 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1274 ch_free( rargv[ 1 ] );
1275 ch_free( rargv[ 2 ] );
1277 if ( BER_BVISEMPTY( prnc ) ) {
1278 rargv[ 0 ] = "rewriteRule";
1280 rargv[ 2 ] = pvnc->bv_val;
1283 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1286 /* backward compatibility */
1287 rargv[ 0 ] = "rewriteContext";
1288 rargv[ 1 ] = "searchResult";
1289 rargv[ 2 ] = "alias";
1290 rargv[ 3 ] = "searchEntryDN";
1292 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1294 rargv[ 0 ] = "rewriteContext";
1295 rargv[ 1 ] = "matchedDN";
1296 rargv[ 2 ] = "alias";
1297 rargv[ 3 ] = "searchEntryDN";
1299 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1301 rargv[ 0 ] = "rewriteContext";
1302 rargv[ 1 ] = "searchAttrDN";
1303 rargv[ 2 ] = "alias";
1304 rargv[ 3 ] = "searchEntryDN";
1306 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1308 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1309 * see servers/slapd/overlays/rwm.h for details */
1310 rargv[ 0 ] = "rewriteContext";
1311 rargv[ 1 ] = "referralAttrDN";
1313 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1315 rargv[ 0 ] = "rewriteContext";
1316 rargv[ 1 ] = "referralDN";
1318 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1322 #endif /* ENABLE_REWRITE */