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 ( 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;
161 for ( c = 0; c < LDAP_BACK_OP_LAST; c++ ) {
162 mi->mi_targets[ i ].mt_timeout[ c ] = mi->mi_timeout[ c ];
168 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
169 Debug( LDAP_DEBUG_ANY,
170 "%s: line %d: unable to parse URI"
171 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
177 * uri MUST have the <dn> part!
179 if ( ludp->lud_dn == NULL ) {
180 Debug( LDAP_DEBUG_ANY,
181 "%s: line %d: missing <naming context> "
182 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
186 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
189 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
190 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
195 if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: missing <naming context> "
198 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
205 * copies and stores uri and suffix
207 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
208 rc = dnPrettyNormal( NULL, &dn, &mi->mi_targets[ i ].mt_psuffix,
209 &mi->mi_targets[ i ].mt_nsuffix, NULL );
210 if( rc != LDAP_SUCCESS ) {
211 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
212 "target \"%s\" DN is invalid\n",
213 fname, lineno, argv[ 1 ] );
217 ludp->lud_dn[ 0 ] = '\0';
219 switch ( ludp->lud_scope ) {
220 case LDAP_SCOPE_DEFAULT:
221 mi->mi_targets[ i ].mt_scope = LDAP_SCOPE_SUBTREE;
224 case LDAP_SCOPE_SUBTREE:
225 case LDAP_SCOPE_SUBORDINATE:
226 mi->mi_targets[ i ].mt_scope = ludp->lud_scope;
230 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
231 "invalid scope for target \"%s\"\n",
232 fname, lineno, argv[ 1 ] );
236 /* check all, to apply the scope check on the first one */
237 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
238 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
239 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
240 "multiple URIs must have "
248 mi->mi_targets[ i ].mt_uri = ldap_url_list2urls( ludp );
249 ldap_free_urllist( ludp );
250 if ( mi->mi_targets[ i ].mt_uri == NULL) {
251 Debug( LDAP_DEBUG_ANY, "%s: line %d: no memory?\n",
257 * uri MUST be a branch of suffix!
259 #if 0 /* too strict a constraint */
260 if ( select_backend( &mi->mi_targets[ i ].suffix, 0, 0 ) != be ) {
261 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: <naming context> of URI does not refer to current backend"
263 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
269 * uri MUST be a branch of a suffix!
271 if ( select_backend( &mi->mi_targets[ i ].mt_nsuffix, 0, 0 ) == NULL ) {
272 Debug( LDAP_DEBUG_ANY,
273 "%s: line %d: <naming context> of URI does not resolve to a backend"
274 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
280 /* default target directive */
281 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
282 int i = mi->mi_ntargets - 1;
286 Debug( LDAP_DEBUG_ANY,
287 "%s: line %d: \"default-target\" alone need be"
288 " inside a \"uri\" directive\n",
292 mi->mi_defaulttarget = i;
295 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
297 Debug( LDAP_DEBUG_ANY,
298 "%s: line %d: \"default-target none\""
299 " should go before uri definitions\n",
302 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
306 if ( lutil_atoi( &mi->mi_defaulttarget, argv[ 1 ] ) != 0
307 || mi->mi_defaulttarget < 0
308 || mi->mi_defaulttarget >= i - 1 )
310 Debug( LDAP_DEBUG_ANY,
311 "%s: line %d: illegal target number %d\n",
312 fname, lineno, mi->mi_defaulttarget );
318 /* ttl of dn cache */
319 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
321 Debug( LDAP_DEBUG_ANY,
322 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
327 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
328 mi->mi_cache.ttl = META_DNCACHE_FOREVER;
330 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
331 mi->mi_cache.ttl = META_DNCACHE_DISABLED;
336 if ( lutil_parse_time( argv[ 1 ], &t ) != 0 ) {
337 Debug( LDAP_DEBUG_ANY,
338 "%s: line %d: unable to parse ttl \"%s\" in \"dncache-ttl <ttl>\" line\n",
339 fname, lineno, argv[ 1 ] );
342 mi->mi_cache.ttl = (time_t)t;
345 /* network timeout when connecting to ldap servers */
346 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
350 Debug( LDAP_DEBUG_ANY,
351 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
356 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
357 Debug( LDAP_DEBUG_ANY,
358 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
359 fname, lineno, argv[ 1 ] );
364 mi->mi_network_timeout = (int)t;
366 /* name to use for meta_back_group */
367 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
368 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
370 int i = mi->mi_ntargets - 1;
374 Debug( LDAP_DEBUG_ANY,
375 "%s: line %d: need \"uri\" directive first\n",
381 Debug( LDAP_DEBUG_ANY,
382 "%s: line %d: missing name in \"binddn <name>\" line\n",
387 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
388 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
389 "\"binddn\" statement is deprecated; "
390 "use \"acl-authcDN\" instead\n",
392 /* FIXME: some day we'll need to throw an error */
395 dn.bv_val = argv[ 1 ];
396 dn.bv_len = strlen( argv[ 1 ] );
397 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
398 NULL ) != LDAP_SUCCESS )
400 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
401 "bind DN '%s' is invalid\n",
402 fname, lineno, argv[ 1 ] );
406 /* password to use for meta_back_group */
407 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
408 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
410 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 password in \"bindpw <password>\" line\n",
426 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
427 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
428 "\"bindpw\" statement is deprecated; "
429 "use \"acl-passwd\" instead\n",
431 /* FIXME: some day we'll need to throw an error */
434 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
436 /* save bind creds for referral rebinds? */
437 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
439 Debug( LDAP_DEBUG_ANY,
440 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
446 Debug( LDAP_DEBUG_ANY,
447 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
449 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
452 switch ( check_true_false( argv[ 1 ] ) ) {
454 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
458 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
462 Debug( LDAP_DEBUG_ANY,
463 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
464 fname, lineno, argv[ 1 ] );
469 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
470 unsigned *flagsp = mi->mi_ntargets ?
471 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
475 Debug( LDAP_DEBUG_ANY,
476 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
481 /* this is the default; we add it because the default might change... */
482 switch ( check_true_false( argv[ 1 ] ) ) {
484 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
488 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
492 Debug( LDAP_DEBUG_ANY,
493 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
494 fname, lineno, argv[ 1 ] );
498 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
499 unsigned *flagsp = mi->mi_ntargets ?
500 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
504 Debug( LDAP_DEBUG_ANY,
505 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
511 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
512 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
515 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
516 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
517 *flagsp |= LDAP_BACK_F_USE_TLS;
519 /* propagate start tls */
520 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
521 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
524 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
525 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
526 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
529 Debug( LDAP_DEBUG_ANY,
530 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
531 fname, lineno, argv[ 1 ] );
535 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
536 unsigned *flagsp = mi->mi_ntargets ?
537 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
541 Debug( LDAP_DEBUG_ANY,
542 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
547 switch ( check_true_false( argv[ 1 ] ) ) {
549 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
553 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
557 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
558 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
561 Debug( LDAP_DEBUG_ANY,
562 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
563 fname, lineno, argv[ 1 ] );
570 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
572 Debug( LDAP_DEBUG_ANY,
573 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
578 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
579 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
581 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
582 mi->mi_flags |= META_BACK_F_ONERR_STOP;
585 Debug( LDAP_DEBUG_ANY,
586 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
587 fname, lineno, argv[ 1 ] );
592 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
594 Debug( LDAP_DEBUG_ANY,
595 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
600 switch ( check_true_false( argv[ 1 ] ) ) {
602 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
606 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
610 Debug( LDAP_DEBUG_ANY,
611 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
612 fname, lineno, argv[ 1 ] );
616 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
618 time_t *tv = mi->mi_ntargets ?
619 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
624 Debug( LDAP_DEBUG_ANY,
625 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
630 for ( c = 1; c < argc; c++ ) {
634 sep = strchr( argv[ c ], '=' );
636 size_t len = sep - argv[ c ];
638 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
639 t = &tv[ LDAP_BACK_OP_ADD ];
640 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
641 t = &tv[ LDAP_BACK_OP_DELETE ];
642 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
643 t = &tv[ LDAP_BACK_OP_MODIFY ];
644 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
645 t = &tv[ LDAP_BACK_OP_MODRDN ];
647 char buf[ SLAP_TEXT_BUFLEN ];
648 snprintf( buf, sizeof( buf ),
649 "unknown operation \"%s\" for timeout #%d",
651 Debug( LDAP_DEBUG_ANY,
652 "%s: line %d: %s.\n",
653 fname, lineno, buf );
662 if ( lutil_parse_time( sep, &val ) != 0 ) {
663 Debug( LDAP_DEBUG_ANY,
664 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
665 fname, lineno, sep );
675 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
676 tv[ i ] = (time_t)val;
681 /* name to use as pseudo-root dn */
682 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
683 int i = mi->mi_ntargets - 1;
687 Debug( LDAP_DEBUG_ANY,
688 "%s: line %d: need \"uri\" directive first\n",
694 Debug( LDAP_DEBUG_ANY,
695 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
700 dn.bv_val = argv[ 1 ];
701 dn.bv_len = strlen( argv[ 1 ] );
702 if ( dnNormalize( 0, NULL, NULL, &dn,
703 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
705 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
706 "pseudoroot DN '%s' is invalid\n",
707 fname, lineno, argv[ 1 ] );
711 /* password to use as pseudo-root */
712 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
713 int i = mi->mi_ntargets - 1;
716 Debug( LDAP_DEBUG_ANY,
717 "%s: line %d: need \"uri\" directive first\n",
723 Debug( LDAP_DEBUG_ANY,
724 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
728 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
731 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
733 int i = mi->mi_ntargets - 1, rc;
734 struct berval dn, nvnc, pvnc, nrnc, prnc;
737 Debug( LDAP_DEBUG_ANY,
738 "%s: line %d: need \"uri\" directive first\n",
746 * suffixmassage <suffix> <massaged suffix>
748 * the <suffix> field must be defined as a valid suffix
749 * (or suffixAlias?) for the current database;
750 * the <massaged suffix> shouldn't have already been
751 * defined as a valid suffix or suffixAlias for the
755 Debug( LDAP_DEBUG_ANY,
756 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
761 ber_str2bv( argv[ 1 ], 0, 0, &dn );
762 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
763 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
764 "suffix '%s' is invalid\n",
765 fname, lineno, argv[ 1 ] );
769 tmp_be = select_backend( &nvnc, 0, 0 );
770 if ( tmp_be != NULL && tmp_be != be ) {
771 Debug( LDAP_DEBUG_ANY,
772 "%s: line %d: suffix already in use by another backend in"
773 " \"suffixMassage <suffix> <massaged suffix>\"\n",
780 ber_str2bv( argv[ 2 ], 0, 0, &dn );
781 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
782 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
783 "massaged suffix '%s' is invalid\n",
784 fname, lineno, argv[ 2 ] );
791 tmp_be = select_backend( &nrnc, 0, 0 );
792 if ( tmp_be != NULL ) {
793 Debug( LDAP_DEBUG_ANY,
794 "%s: line %d: massaged suffix already in use by another backend in"
795 " \"suffixMassage <suffix> <massaged suffix>\"\n",
806 * The suffix massaging is emulated by means of the
807 * rewrite capabilities
808 * FIXME: no extra rewrite capabilities should be added
811 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
812 &pvnc, &nvnc, &prnc, &nrnc );
821 /* rewrite stuff ... */
822 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
823 int i = mi->mi_ntargets - 1;
826 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
827 "statement outside target definition.\n",
832 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
833 fname, lineno, argc, argv );
835 /* objectclass/attribute mapping */
836 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
837 int i = mi->mi_ntargets - 1;
840 Debug( LDAP_DEBUG_ANY,
841 "%s: line %d: need \"uri\" directive first\n",
846 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
847 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
848 fname, lineno, argc, argv );
850 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
851 int i = mi->mi_ntargets - 1;
852 int nretries = META_RETRY_UNDEFINED;
855 Debug( LDAP_DEBUG_ANY,
856 "%s: line %d: need value in \"nretries <value>\"\n",
861 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
862 nretries = META_RETRY_FOREVER;
864 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
865 nretries = META_RETRY_NEVER;
868 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
869 Debug( LDAP_DEBUG_ANY,
870 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
871 fname, lineno, argv[ 1 ] );
877 mi->mi_nretries = nretries;
880 mi->mi_targets[ i ].mt_nretries = nretries;
885 return SLAP_CONF_UNKNOWN;
891 ldap_back_map_config(
892 struct ldapmap *oc_map,
893 struct ldapmap *at_map,
900 struct ldapmapping *mapping;
904 if ( argc < 3 || argc > 4 ) {
905 Debug( LDAP_DEBUG_ANY,
906 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
911 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
915 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
919 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
920 "\"map {objectclass | attribute} [<local> | *] "
921 "{<foreign> | *}\"\n",
926 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
927 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
928 map->drop_missing = ( argc < 4 );
931 src = dst = argv[ 3 ];
933 } else if ( argc < 4 ) {
939 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
942 if ( ( map == at_map )
943 && ( strcasecmp( src, "objectclass" ) == 0
944 || strcasecmp( dst, "objectclass" ) == 0 ) )
946 Debug( LDAP_DEBUG_ANY,
947 "%s: line %d: objectclass attribute cannot be mapped\n",
951 mapping = (struct ldapmapping *)ch_calloc( 2,
952 sizeof(struct ldapmapping) );
953 if ( mapping == NULL ) {
954 Debug( LDAP_DEBUG_ANY,
955 "%s: line %d: out of memory\n",
959 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
960 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
961 mapping[ 1 ].src = mapping[ 0 ].dst;
962 mapping[ 1 ].dst = mapping[ 0 ].src;
968 if ( src[ 0 ] != '\0' ) {
969 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
970 Debug( LDAP_DEBUG_ANY,
971 "%s: line %d: warning, source objectClass '%s' "
972 "should be defined in schema\n",
973 fname, lineno, src );
976 * FIXME: this should become an err
982 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
983 Debug( LDAP_DEBUG_ANY,
984 "%s: line %d: warning, destination objectClass '%s' "
985 "is not defined in schema\n",
986 fname, lineno, dst );
990 const char *text = NULL;
991 AttributeDescription *ad = NULL;
993 if ( src[ 0 ] != '\0' ) {
994 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
995 if ( rc != LDAP_SUCCESS ) {
996 Debug( LDAP_DEBUG_ANY,
997 "%s: line %d: warning, source attributeType '%s' "
998 "should be defined in schema\n",
999 fname, lineno, src );
1002 * FIXME: this should become an err
1005 * we create a fake "proxied" ad
1009 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1010 &ad, &text, SLAP_AD_PROXIED );
1011 if ( rc != LDAP_SUCCESS ) {
1012 char buf[ SLAP_TEXT_BUFLEN ];
1014 snprintf( buf, sizeof( buf ),
1015 "source attributeType \"%s\": %d (%s)",
1016 src, rc, text ? text : "" );
1017 Debug( LDAP_DEBUG_ANY,
1018 "%s: line %d: %s\n",
1019 fname, lineno, buf );
1027 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1028 if ( rc != LDAP_SUCCESS ) {
1029 Debug( LDAP_DEBUG_ANY,
1030 "%s: line %d: warning, destination attributeType '%s' "
1031 "is not defined in schema\n",
1032 fname, lineno, dst );
1035 * we create a fake "proxied" ad
1039 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1040 &ad, &text, SLAP_AD_PROXIED );
1041 if ( rc != LDAP_SUCCESS ) {
1042 char buf[ SLAP_TEXT_BUFLEN ];
1044 snprintf( buf, sizeof( buf ),
1045 "source attributeType \"%s\": %d (%s)\n",
1046 dst, rc, text ? text : "" );
1047 Debug( LDAP_DEBUG_ANY,
1048 "%s: line %d: %s\n",
1049 fname, lineno, buf );
1055 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1056 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1058 Debug( LDAP_DEBUG_ANY,
1059 "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1064 if ( src[ 0 ] != '\0' ) {
1065 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1066 mapping_cmp, mapping_dup );
1068 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1069 mapping_cmp, mapping_dup );
1075 ch_free( mapping[ 0 ].src.bv_val );
1076 ch_free( mapping[ 0 ].dst.bv_val );
1084 #ifdef ENABLE_REWRITE
1086 suffix_massage_regexize( const char *s )
1092 if ( s[ 0 ] == '\0' ) {
1093 return ch_strdup( "^(.+)$" );
1097 ( r = strchr( p, ',' ) ) != NULL;
1101 res = ch_calloc( sizeof( char ),
1103 + STRLENOF( "((.+),)?" )
1104 + STRLENOF( "[ ]?" ) * i
1105 + STRLENOF( "$" ) + 1 );
1107 ptr = lutil_strcopy( res, "((.+),)?" );
1109 ( r = strchr( p, ',' ) ) != NULL;
1111 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1112 ptr = lutil_strcopy( ptr, "[ ]?" );
1114 if ( r[ 1 ] == ' ' ) {
1118 ptr = lutil_strcopy( ptr, p );
1127 suffix_massage_patternize( const char *s, const char *p )
1134 if ( s[ 0 ] == '\0' ) {
1138 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1139 if ( res == NULL ) {
1143 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1144 if ( s[ 0 ] == '\0' ) {
1148 lutil_strcopy( ptr, p );
1154 suffix_massage_config(
1155 struct rewrite_info *info,
1156 struct berval *pvnc,
1157 struct berval *nvnc,
1158 struct berval *prnc,
1165 rargv[ 0 ] = "rewriteEngine";
1168 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1170 rargv[ 0 ] = "rewriteContext";
1171 rargv[ 1 ] = "default";
1173 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1175 rargv[ 0 ] = "rewriteRule";
1176 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1177 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1180 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1181 ch_free( rargv[ 1 ] );
1182 ch_free( rargv[ 2 ] );
1184 if ( BER_BVISEMPTY( pvnc ) ) {
1185 rargv[ 0 ] = "rewriteRule";
1187 rargv[ 2 ] = prnc->bv_val;
1190 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1193 rargv[ 0 ] = "rewriteContext";
1194 rargv[ 1 ] = "searchEntryDN";
1196 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1198 rargv[ 0 ] = "rewriteRule";
1199 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1200 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1203 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1204 ch_free( rargv[ 1 ] );
1205 ch_free( rargv[ 2 ] );
1207 if ( BER_BVISEMPTY( prnc ) ) {
1208 rargv[ 0 ] = "rewriteRule";
1210 rargv[ 2 ] = pvnc->bv_val;
1213 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1216 /* backward compatibility */
1217 rargv[ 0 ] = "rewriteContext";
1218 rargv[ 1 ] = "searchResult";
1219 rargv[ 2 ] = "alias";
1220 rargv[ 3 ] = "searchEntryDN";
1222 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1224 rargv[ 0 ] = "rewriteContext";
1225 rargv[ 1 ] = "matchedDN";
1226 rargv[ 2 ] = "alias";
1227 rargv[ 3 ] = "searchEntryDN";
1229 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1231 rargv[ 0 ] = "rewriteContext";
1232 rargv[ 1 ] = "searchAttrDN";
1233 rargv[ 2 ] = "alias";
1234 rargv[ 3 ] = "searchEntryDN";
1236 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1238 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1239 * see servers/slapd/overlays/rwm.h for details */
1240 rargv[ 0 ] = "rewriteContext";
1241 rargv[ 1 ] = "referralAttrDN";
1243 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1245 rargv[ 0 ] = "rewriteContext";
1246 rargv[ 1 ] = "referralDN";
1248 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1252 #endif /* ENABLE_REWRITE */