2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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_idle_timeout = mi->mi_idle_timeout;
161 mi->mi_targets[ i ].mt_network_timeout = mi->mi_network_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 ) {
349 int i = mi->mi_ntargets - 1;
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 ) {
374 int i = mi->mi_ntargets - 1;
376 time_t *tp = mi->mi_ntargets ?
377 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_idle_timeout
378 : &mi->mi_idle_timeout;
382 Debug( LDAP_DEBUG_ANY,
383 "%s: line %d: missing timeout value in \"idle-timeout <seconds>\" line\n",
389 Debug( LDAP_DEBUG_ANY,
390 "%s: line %d: extra cruft after timeout value in \"idle-timeout <seconds>\" line\n",
395 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
396 Debug( LDAP_DEBUG_ANY,
397 "%s: line %d: unable to parse timeout \"%s\" in \"idle-timeout <seconds>\" line\n",
398 fname, lineno, argv[ 1 ] );
405 /* name to use for meta_back_group */
406 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
407 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
409 int i = mi->mi_ntargets - 1;
413 Debug( LDAP_DEBUG_ANY,
414 "%s: line %d: need \"uri\" directive first\n",
420 Debug( LDAP_DEBUG_ANY,
421 "%s: line %d: missing name in \"binddn <name>\" line\n",
426 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
427 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
428 "\"binddn\" statement is deprecated; "
429 "use \"acl-authcDN\" instead\n",
431 /* FIXME: some day we'll need to throw an error */
434 dn.bv_val = argv[ 1 ];
435 dn.bv_len = strlen( argv[ 1 ] );
436 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
437 NULL ) != LDAP_SUCCESS )
439 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
440 "bind DN '%s' is invalid\n",
441 fname, lineno, argv[ 1 ] );
445 /* password to use for meta_back_group */
446 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
447 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
449 int i = mi->mi_ntargets - 1;
452 Debug( LDAP_DEBUG_ANY,
453 "%s: line %d: need \"uri\" directive first\n",
459 Debug( LDAP_DEBUG_ANY,
460 "%s: line %d: missing password in \"bindpw <password>\" line\n",
465 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
466 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
467 "\"bindpw\" statement is deprecated; "
468 "use \"acl-passwd\" instead\n",
470 /* FIXME: some day we'll need to throw an error */
473 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
475 /* save bind creds for referral rebinds? */
476 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
478 Debug( LDAP_DEBUG_ANY,
479 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
485 Debug( LDAP_DEBUG_ANY,
486 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
488 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
491 switch ( check_true_false( argv[ 1 ] ) ) {
493 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
497 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
501 Debug( LDAP_DEBUG_ANY,
502 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
503 fname, lineno, argv[ 1 ] );
508 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
509 unsigned *flagsp = mi->mi_ntargets ?
510 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
514 Debug( LDAP_DEBUG_ANY,
515 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
520 /* this is the default; we add it because the default might change... */
521 switch ( check_true_false( argv[ 1 ] ) ) {
523 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
527 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
531 Debug( LDAP_DEBUG_ANY,
532 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
533 fname, lineno, argv[ 1 ] );
537 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
538 unsigned *flagsp = mi->mi_ntargets ?
539 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
543 Debug( LDAP_DEBUG_ANY,
544 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
550 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
551 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
554 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
555 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
556 *flagsp |= LDAP_BACK_F_USE_TLS;
558 /* propagate start tls */
559 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
560 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
563 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
564 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
565 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
568 Debug( LDAP_DEBUG_ANY,
569 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
570 fname, lineno, argv[ 1 ] );
574 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
575 unsigned *flagsp = mi->mi_ntargets ?
576 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
580 Debug( LDAP_DEBUG_ANY,
581 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
586 switch ( check_true_false( argv[ 1 ] ) ) {
588 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
592 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
596 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
597 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
600 Debug( LDAP_DEBUG_ANY,
601 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
602 fname, lineno, argv[ 1 ] );
609 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
611 Debug( LDAP_DEBUG_ANY,
612 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
617 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
618 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
620 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
621 mi->mi_flags |= META_BACK_F_ONERR_STOP;
624 Debug( LDAP_DEBUG_ANY,
625 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
626 fname, lineno, argv[ 1 ] );
631 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
633 Debug( LDAP_DEBUG_ANY,
634 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
639 switch ( check_true_false( argv[ 1 ] ) ) {
641 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
645 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
649 Debug( LDAP_DEBUG_ANY,
650 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
651 fname, lineno, argv[ 1 ] );
655 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
657 time_t *tv = mi->mi_ntargets ?
658 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
663 Debug( LDAP_DEBUG_ANY,
664 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
669 for ( c = 1; c < argc; c++ ) {
673 sep = strchr( argv[ c ], '=' );
675 size_t len = sep - argv[ c ];
677 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
678 t = &tv[ LDAP_BACK_OP_ADD ];
679 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
680 t = &tv[ LDAP_BACK_OP_DELETE ];
681 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
682 t = &tv[ LDAP_BACK_OP_MODIFY ];
683 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
684 t = &tv[ LDAP_BACK_OP_MODRDN ];
686 char buf[ SLAP_TEXT_BUFLEN ];
687 snprintf( buf, sizeof( buf ),
688 "unknown operation \"%s\" for timeout #%d",
690 Debug( LDAP_DEBUG_ANY,
691 "%s: line %d: %s.\n",
692 fname, lineno, buf );
701 if ( lutil_parse_time( sep, &val ) != 0 ) {
702 Debug( LDAP_DEBUG_ANY,
703 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
704 fname, lineno, sep );
714 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
715 tv[ i ] = (time_t)val;
720 /* name to use as pseudo-root dn */
721 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
722 int i = mi->mi_ntargets - 1;
726 Debug( LDAP_DEBUG_ANY,
727 "%s: line %d: need \"uri\" directive first\n",
733 Debug( LDAP_DEBUG_ANY,
734 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
739 dn.bv_val = argv[ 1 ];
740 dn.bv_len = strlen( argv[ 1 ] );
741 if ( dnNormalize( 0, NULL, NULL, &dn,
742 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
744 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
745 "pseudoroot DN '%s' is invalid\n",
746 fname, lineno, argv[ 1 ] );
750 /* password to use as pseudo-root */
751 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
752 int i = mi->mi_ntargets - 1;
755 Debug( LDAP_DEBUG_ANY,
756 "%s: line %d: need \"uri\" directive first\n",
762 Debug( LDAP_DEBUG_ANY,
763 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
767 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
770 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
772 int i = mi->mi_ntargets - 1, rc;
773 struct berval dn, nvnc, pvnc, nrnc, prnc;
776 Debug( LDAP_DEBUG_ANY,
777 "%s: line %d: need \"uri\" directive first\n",
785 * suffixmassage <suffix> <massaged suffix>
787 * the <suffix> field must be defined as a valid suffix
788 * (or suffixAlias?) for the current database;
789 * the <massaged suffix> shouldn't have already been
790 * defined as a valid suffix or suffixAlias for the
794 Debug( LDAP_DEBUG_ANY,
795 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
800 ber_str2bv( argv[ 1 ], 0, 0, &dn );
801 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
802 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
803 "suffix '%s' is invalid\n",
804 fname, lineno, argv[ 1 ] );
808 tmp_be = select_backend( &nvnc, 0, 0 );
809 if ( tmp_be != NULL && tmp_be != be ) {
810 Debug( LDAP_DEBUG_ANY,
811 "%s: line %d: suffix already in use by another backend in"
812 " \"suffixMassage <suffix> <massaged suffix>\"\n",
819 ber_str2bv( argv[ 2 ], 0, 0, &dn );
820 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
821 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
822 "massaged suffix '%s' is invalid\n",
823 fname, lineno, argv[ 2 ] );
830 tmp_be = select_backend( &nrnc, 0, 0 );
831 if ( tmp_be != NULL ) {
832 Debug( LDAP_DEBUG_ANY,
833 "%s: line %d: massaged suffix already in use by another backend in"
834 " \"suffixMassage <suffix> <massaged suffix>\"\n",
845 * The suffix massaging is emulated by means of the
846 * rewrite capabilities
847 * FIXME: no extra rewrite capabilities should be added
850 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
851 &pvnc, &nvnc, &prnc, &nrnc );
860 /* rewrite stuff ... */
861 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
862 int i = mi->mi_ntargets - 1;
865 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
866 "statement outside target definition.\n",
871 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
872 fname, lineno, argc, argv );
874 /* objectclass/attribute mapping */
875 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
876 int i = mi->mi_ntargets - 1;
879 Debug( LDAP_DEBUG_ANY,
880 "%s: line %d: need \"uri\" directive first\n",
885 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
886 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
887 fname, lineno, argc, argv );
889 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
890 int i = mi->mi_ntargets - 1;
891 int nretries = META_RETRY_UNDEFINED;
894 Debug( LDAP_DEBUG_ANY,
895 "%s: line %d: need value in \"nretries <value>\"\n",
900 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
901 nretries = META_RETRY_FOREVER;
903 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
904 nretries = META_RETRY_NEVER;
907 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
908 Debug( LDAP_DEBUG_ANY,
909 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
910 fname, lineno, argv[ 1 ] );
916 mi->mi_nretries = nretries;
919 mi->mi_targets[ i ].mt_nretries = nretries;
924 return SLAP_CONF_UNKNOWN;
930 ldap_back_map_config(
931 struct ldapmap *oc_map,
932 struct ldapmap *at_map,
939 struct ldapmapping *mapping;
943 if ( argc < 3 || argc > 4 ) {
944 Debug( LDAP_DEBUG_ANY,
945 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
950 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
954 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
958 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
959 "\"map {objectclass | attribute} [<local> | *] "
960 "{<foreign> | *}\"\n",
965 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
966 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
967 map->drop_missing = ( argc < 4 );
970 src = dst = argv[ 3 ];
972 } else if ( argc < 4 ) {
978 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
981 if ( ( map == at_map )
982 && ( strcasecmp( src, "objectclass" ) == 0
983 || strcasecmp( dst, "objectclass" ) == 0 ) )
985 Debug( LDAP_DEBUG_ANY,
986 "%s: line %d: objectclass attribute cannot be mapped\n",
990 mapping = (struct ldapmapping *)ch_calloc( 2,
991 sizeof(struct ldapmapping) );
992 if ( mapping == NULL ) {
993 Debug( LDAP_DEBUG_ANY,
994 "%s: line %d: out of memory\n",
998 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
999 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1000 mapping[ 1 ].src = mapping[ 0 ].dst;
1001 mapping[ 1 ].dst = mapping[ 0 ].src;
1007 if ( src[ 0 ] != '\0' ) {
1008 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1009 Debug( LDAP_DEBUG_ANY,
1010 "%s: line %d: warning, source objectClass '%s' "
1011 "should be defined in schema\n",
1012 fname, lineno, src );
1015 * FIXME: this should become an err
1021 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1022 Debug( LDAP_DEBUG_ANY,
1023 "%s: line %d: warning, destination objectClass '%s' "
1024 "is not defined in schema\n",
1025 fname, lineno, dst );
1029 const char *text = NULL;
1030 AttributeDescription *ad = NULL;
1032 if ( src[ 0 ] != '\0' ) {
1033 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1034 if ( rc != LDAP_SUCCESS ) {
1035 Debug( LDAP_DEBUG_ANY,
1036 "%s: line %d: warning, source attributeType '%s' "
1037 "should be defined in schema\n",
1038 fname, lineno, src );
1041 * FIXME: this should become an err
1044 * we create a fake "proxied" ad
1048 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1049 &ad, &text, SLAP_AD_PROXIED );
1050 if ( rc != LDAP_SUCCESS ) {
1051 char buf[ SLAP_TEXT_BUFLEN ];
1053 snprintf( buf, sizeof( buf ),
1054 "source attributeType \"%s\": %d (%s)",
1055 src, rc, text ? text : "" );
1056 Debug( LDAP_DEBUG_ANY,
1057 "%s: line %d: %s\n",
1058 fname, lineno, buf );
1066 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1067 if ( rc != LDAP_SUCCESS ) {
1068 Debug( LDAP_DEBUG_ANY,
1069 "%s: line %d: warning, destination attributeType '%s' "
1070 "is not defined in schema\n",
1071 fname, lineno, dst );
1074 * we create a fake "proxied" ad
1078 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1079 &ad, &text, SLAP_AD_PROXIED );
1080 if ( rc != LDAP_SUCCESS ) {
1081 char buf[ SLAP_TEXT_BUFLEN ];
1083 snprintf( buf, sizeof( buf ),
1084 "source attributeType \"%s\": %d (%s)\n",
1085 dst, rc, text ? text : "" );
1086 Debug( LDAP_DEBUG_ANY,
1087 "%s: line %d: %s\n",
1088 fname, lineno, buf );
1094 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1095 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1097 Debug( LDAP_DEBUG_ANY,
1098 "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1103 if ( src[ 0 ] != '\0' ) {
1104 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1105 mapping_cmp, mapping_dup );
1107 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1108 mapping_cmp, mapping_dup );
1114 ch_free( mapping[ 0 ].src.bv_val );
1115 ch_free( mapping[ 0 ].dst.bv_val );
1123 #ifdef ENABLE_REWRITE
1125 suffix_massage_regexize( const char *s )
1131 if ( s[ 0 ] == '\0' ) {
1132 return ch_strdup( "^(.+)$" );
1136 ( r = strchr( p, ',' ) ) != NULL;
1140 res = ch_calloc( sizeof( char ),
1142 + STRLENOF( "((.+),)?" )
1143 + STRLENOF( "[ ]?" ) * i
1144 + STRLENOF( "$" ) + 1 );
1146 ptr = lutil_strcopy( res, "((.+),)?" );
1148 ( r = strchr( p, ',' ) ) != NULL;
1150 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1151 ptr = lutil_strcopy( ptr, "[ ]?" );
1153 if ( r[ 1 ] == ' ' ) {
1157 ptr = lutil_strcopy( ptr, p );
1166 suffix_massage_patternize( const char *s, const char *p )
1173 if ( s[ 0 ] == '\0' ) {
1177 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1178 if ( res == NULL ) {
1182 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1183 if ( s[ 0 ] == '\0' ) {
1187 lutil_strcopy( ptr, p );
1193 suffix_massage_config(
1194 struct rewrite_info *info,
1195 struct berval *pvnc,
1196 struct berval *nvnc,
1197 struct berval *prnc,
1204 rargv[ 0 ] = "rewriteEngine";
1207 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1209 rargv[ 0 ] = "rewriteContext";
1210 rargv[ 1 ] = "default";
1212 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1214 rargv[ 0 ] = "rewriteRule";
1215 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1216 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1219 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1220 ch_free( rargv[ 1 ] );
1221 ch_free( rargv[ 2 ] );
1223 if ( BER_BVISEMPTY( pvnc ) ) {
1224 rargv[ 0 ] = "rewriteRule";
1226 rargv[ 2 ] = prnc->bv_val;
1229 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1232 rargv[ 0 ] = "rewriteContext";
1233 rargv[ 1 ] = "searchEntryDN";
1235 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1237 rargv[ 0 ] = "rewriteRule";
1238 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1239 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1242 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1243 ch_free( rargv[ 1 ] );
1244 ch_free( rargv[ 2 ] );
1246 if ( BER_BVISEMPTY( prnc ) ) {
1247 rargv[ 0 ] = "rewriteRule";
1249 rargv[ 2 ] = pvnc->bv_val;
1252 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1255 /* backward compatibility */
1256 rargv[ 0 ] = "rewriteContext";
1257 rargv[ 1 ] = "searchResult";
1258 rargv[ 2 ] = "alias";
1259 rargv[ 3 ] = "searchEntryDN";
1261 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1263 rargv[ 0 ] = "rewriteContext";
1264 rargv[ 1 ] = "matchedDN";
1265 rargv[ 2 ] = "alias";
1266 rargv[ 3 ] = "searchEntryDN";
1268 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1270 rargv[ 0 ] = "rewriteContext";
1271 rargv[ 1 ] = "searchAttrDN";
1272 rargv[ 2 ] = "alias";
1273 rargv[ 3 ] = "searchEntryDN";
1275 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1277 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1278 * see servers/slapd/overlays/rwm.h for details */
1279 rargv[ 0 ] = "rewriteContext";
1280 rargv[ 1 ] = "referralAttrDN";
1282 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1284 rargv[ 0 ] = "rewriteContext";
1285 rargv[ 1 ] = "referralDN";
1287 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1291 #endif /* ENABLE_REWRITE */