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_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 ) {
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 /* bind timeout when connecting to ldap servers */
406 } else if ( strcasecmp( argv[ 0 ], "bind-timeout" ) == 0 ) {
407 int i = mi->mi_ntargets - 1;
409 struct timeval *tp = mi->mi_ntargets ?
410 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_bind_timeout
411 : &mi->mi_bind_timeout;
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: missing timeout value in \"bind-timeout <microseconds>\" line\n",
422 Debug( LDAP_DEBUG_ANY,
423 "%s: line %d: extra cruft after timeout value in \"bind-timeout <microseconds>\" line\n",
428 if ( lutil_atoul( &t, argv[ 1 ] ) != 0 ) {
429 Debug( LDAP_DEBUG_ANY,
430 "%s: line %d: unable to parse timeout \"%s\" in \"bind-timeout <microseconds>\" line\n",
431 fname, lineno, argv[ 1 ] );
436 tp->tv_sec = t/1000000;
437 tp->tv_usec = t%1000000;
439 /* name to use for meta_back_group */
440 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
441 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
443 int i = mi->mi_ntargets - 1;
447 Debug( LDAP_DEBUG_ANY,
448 "%s: line %d: need \"uri\" directive first\n",
454 Debug( LDAP_DEBUG_ANY,
455 "%s: line %d: missing name in \"binddn <name>\" line\n",
460 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
461 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
462 "\"binddn\" statement is deprecated; "
463 "use \"acl-authcDN\" instead\n",
465 /* FIXME: some day we'll need to throw an error */
468 dn.bv_val = argv[ 1 ];
469 dn.bv_len = strlen( argv[ 1 ] );
470 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
471 NULL ) != LDAP_SUCCESS )
473 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
474 "bind DN '%s' is invalid\n",
475 fname, lineno, argv[ 1 ] );
479 /* password to use for meta_back_group */
480 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
481 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
483 int i = mi->mi_ntargets - 1;
486 Debug( LDAP_DEBUG_ANY,
487 "%s: line %d: need \"uri\" directive first\n",
493 Debug( LDAP_DEBUG_ANY,
494 "%s: line %d: missing password in \"bindpw <password>\" line\n",
499 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
500 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
501 "\"bindpw\" statement is deprecated; "
502 "use \"acl-passwd\" instead\n",
504 /* FIXME: some day we'll need to throw an error */
507 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
509 /* save bind creds for referral rebinds? */
510 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
512 Debug( LDAP_DEBUG_ANY,
513 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
519 Debug( LDAP_DEBUG_ANY,
520 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
522 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
525 switch ( check_true_false( argv[ 1 ] ) ) {
527 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
531 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
535 Debug( LDAP_DEBUG_ANY,
536 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
537 fname, lineno, argv[ 1 ] );
542 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
543 unsigned *flagsp = mi->mi_ntargets ?
544 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
548 Debug( LDAP_DEBUG_ANY,
549 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
554 /* this is the default; we add it because the default might change... */
555 switch ( check_true_false( argv[ 1 ] ) ) {
557 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
561 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
565 Debug( LDAP_DEBUG_ANY,
566 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
567 fname, lineno, argv[ 1 ] );
571 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
572 unsigned *flagsp = mi->mi_ntargets ?
573 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
577 Debug( LDAP_DEBUG_ANY,
578 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
584 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
585 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
588 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
589 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
590 *flagsp |= LDAP_BACK_F_USE_TLS;
592 /* propagate start tls */
593 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
594 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
597 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
598 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
599 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
602 Debug( LDAP_DEBUG_ANY,
603 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
604 fname, lineno, argv[ 1 ] );
608 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
609 unsigned *flagsp = mi->mi_ntargets ?
610 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
614 Debug( LDAP_DEBUG_ANY,
615 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
620 switch ( check_true_false( argv[ 1 ] ) ) {
622 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
626 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
630 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
631 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
634 Debug( LDAP_DEBUG_ANY,
635 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
636 fname, lineno, argv[ 1 ] );
643 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
645 Debug( LDAP_DEBUG_ANY,
646 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
651 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
652 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
654 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
655 mi->mi_flags |= META_BACK_F_ONERR_STOP;
658 Debug( LDAP_DEBUG_ANY,
659 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
660 fname, lineno, argv[ 1 ] );
665 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
667 Debug( LDAP_DEBUG_ANY,
668 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
673 switch ( check_true_false( argv[ 1 ] ) ) {
675 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
679 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
683 Debug( LDAP_DEBUG_ANY,
684 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
685 fname, lineno, argv[ 1 ] );
689 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
691 time_t *tv = mi->mi_ntargets ?
692 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
697 Debug( LDAP_DEBUG_ANY,
698 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
703 for ( c = 1; c < argc; c++ ) {
707 sep = strchr( argv[ c ], '=' );
709 size_t len = sep - argv[ c ];
711 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
712 t = &tv[ LDAP_BACK_OP_ADD ];
713 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
714 t = &tv[ LDAP_BACK_OP_DELETE ];
715 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
716 t = &tv[ LDAP_BACK_OP_MODIFY ];
717 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
718 t = &tv[ LDAP_BACK_OP_MODRDN ];
720 char buf[ SLAP_TEXT_BUFLEN ];
721 snprintf( buf, sizeof( buf ),
722 "unknown operation \"%s\" for timeout #%d",
724 Debug( LDAP_DEBUG_ANY,
725 "%s: line %d: %s.\n",
726 fname, lineno, buf );
735 if ( lutil_parse_time( sep, &val ) != 0 ) {
736 Debug( LDAP_DEBUG_ANY,
737 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
738 fname, lineno, sep );
748 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
749 tv[ i ] = (time_t)val;
754 /* name to use as pseudo-root dn */
755 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
756 int i = mi->mi_ntargets - 1;
760 Debug( LDAP_DEBUG_ANY,
761 "%s: line %d: need \"uri\" directive first\n",
767 Debug( LDAP_DEBUG_ANY,
768 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
773 dn.bv_val = argv[ 1 ];
774 dn.bv_len = strlen( argv[ 1 ] );
775 if ( dnNormalize( 0, NULL, NULL, &dn,
776 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
778 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
779 "pseudoroot DN '%s' is invalid\n",
780 fname, lineno, argv[ 1 ] );
784 /* password to use as pseudo-root */
785 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
786 int i = mi->mi_ntargets - 1;
789 Debug( LDAP_DEBUG_ANY,
790 "%s: line %d: need \"uri\" directive first\n",
796 Debug( LDAP_DEBUG_ANY,
797 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
801 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
804 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
806 int i = mi->mi_ntargets - 1, rc;
807 struct berval dn, nvnc, pvnc, nrnc, prnc;
810 Debug( LDAP_DEBUG_ANY,
811 "%s: line %d: need \"uri\" directive first\n",
819 * suffixmassage <suffix> <massaged suffix>
821 * the <suffix> field must be defined as a valid suffix
822 * (or suffixAlias?) for the current database;
823 * the <massaged suffix> shouldn't have already been
824 * defined as a valid suffix or suffixAlias for the
828 Debug( LDAP_DEBUG_ANY,
829 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
834 ber_str2bv( argv[ 1 ], 0, 0, &dn );
835 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
836 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
837 "suffix '%s' is invalid\n",
838 fname, lineno, argv[ 1 ] );
842 tmp_be = select_backend( &nvnc, 0, 0 );
843 if ( tmp_be != NULL && tmp_be != be ) {
844 Debug( LDAP_DEBUG_ANY,
845 "%s: line %d: suffix already in use by another backend in"
846 " \"suffixMassage <suffix> <massaged suffix>\"\n",
853 ber_str2bv( argv[ 2 ], 0, 0, &dn );
854 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
855 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
856 "massaged suffix '%s' is invalid\n",
857 fname, lineno, argv[ 2 ] );
864 tmp_be = select_backend( &nrnc, 0, 0 );
865 if ( tmp_be != NULL ) {
866 Debug( LDAP_DEBUG_ANY,
867 "%s: line %d: massaged suffix already in use by another backend in"
868 " \"suffixMassage <suffix> <massaged suffix>\"\n",
879 * The suffix massaging is emulated by means of the
880 * rewrite capabilities
881 * FIXME: no extra rewrite capabilities should be added
884 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
885 &pvnc, &nvnc, &prnc, &nrnc );
894 /* rewrite stuff ... */
895 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
896 int i = mi->mi_ntargets - 1;
899 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
900 "statement outside target definition.\n",
905 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
906 fname, lineno, argc, argv );
908 /* objectclass/attribute mapping */
909 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
910 int i = mi->mi_ntargets - 1;
913 Debug( LDAP_DEBUG_ANY,
914 "%s: line %d: need \"uri\" directive first\n",
919 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
920 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
921 fname, lineno, argc, argv );
923 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
924 int i = mi->mi_ntargets - 1;
925 int nretries = META_RETRY_UNDEFINED;
928 Debug( LDAP_DEBUG_ANY,
929 "%s: line %d: need value in \"nretries <value>\"\n",
934 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
935 nretries = META_RETRY_FOREVER;
937 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
938 nretries = META_RETRY_NEVER;
941 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
942 Debug( LDAP_DEBUG_ANY,
943 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
944 fname, lineno, argv[ 1 ] );
950 mi->mi_nretries = nretries;
953 mi->mi_targets[ i ].mt_nretries = nretries;
958 return SLAP_CONF_UNKNOWN;
964 ldap_back_map_config(
965 struct ldapmap *oc_map,
966 struct ldapmap *at_map,
973 struct ldapmapping *mapping;
977 if ( argc < 3 || argc > 4 ) {
978 Debug( LDAP_DEBUG_ANY,
979 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
984 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
988 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
992 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
993 "\"map {objectclass | attribute} [<local> | *] "
994 "{<foreign> | *}\"\n",
999 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
1000 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
1001 map->drop_missing = ( argc < 4 );
1004 src = dst = argv[ 3 ];
1006 } else if ( argc < 4 ) {
1012 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
1015 if ( ( map == at_map )
1016 && ( strcasecmp( src, "objectclass" ) == 0
1017 || strcasecmp( dst, "objectclass" ) == 0 ) )
1019 Debug( LDAP_DEBUG_ANY,
1020 "%s: line %d: objectclass attribute cannot be mapped\n",
1024 mapping = (struct ldapmapping *)ch_calloc( 2,
1025 sizeof(struct ldapmapping) );
1026 if ( mapping == NULL ) {
1027 Debug( LDAP_DEBUG_ANY,
1028 "%s: line %d: out of memory\n",
1032 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
1033 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
1034 mapping[ 1 ].src = mapping[ 0 ].dst;
1035 mapping[ 1 ].dst = mapping[ 0 ].src;
1041 if ( src[ 0 ] != '\0' ) {
1042 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
1043 Debug( LDAP_DEBUG_ANY,
1044 "%s: line %d: warning, source objectClass '%s' "
1045 "should be defined in schema\n",
1046 fname, lineno, src );
1049 * FIXME: this should become an err
1055 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
1056 Debug( LDAP_DEBUG_ANY,
1057 "%s: line %d: warning, destination objectClass '%s' "
1058 "is not defined in schema\n",
1059 fname, lineno, dst );
1063 const char *text = NULL;
1064 AttributeDescription *ad = NULL;
1066 if ( src[ 0 ] != '\0' ) {
1067 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
1068 if ( rc != LDAP_SUCCESS ) {
1069 Debug( LDAP_DEBUG_ANY,
1070 "%s: line %d: warning, source attributeType '%s' "
1071 "should be defined in schema\n",
1072 fname, lineno, src );
1075 * FIXME: this should become an err
1078 * we create a fake "proxied" ad
1082 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1083 &ad, &text, SLAP_AD_PROXIED );
1084 if ( rc != LDAP_SUCCESS ) {
1085 char buf[ SLAP_TEXT_BUFLEN ];
1087 snprintf( buf, sizeof( buf ),
1088 "source attributeType \"%s\": %d (%s)",
1089 src, rc, text ? text : "" );
1090 Debug( LDAP_DEBUG_ANY,
1091 "%s: line %d: %s\n",
1092 fname, lineno, buf );
1100 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1101 if ( rc != LDAP_SUCCESS ) {
1102 Debug( LDAP_DEBUG_ANY,
1103 "%s: line %d: warning, destination attributeType '%s' "
1104 "is not defined in schema\n",
1105 fname, lineno, dst );
1108 * we create a fake "proxied" ad
1112 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
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)\n",
1119 dst, rc, text ? text : "" );
1120 Debug( LDAP_DEBUG_ANY,
1121 "%s: line %d: %s\n",
1122 fname, lineno, buf );
1128 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1129 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1131 Debug( LDAP_DEBUG_ANY,
1132 "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1137 if ( src[ 0 ] != '\0' ) {
1138 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1139 mapping_cmp, mapping_dup );
1141 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1142 mapping_cmp, mapping_dup );
1148 ch_free( mapping[ 0 ].src.bv_val );
1149 ch_free( mapping[ 0 ].dst.bv_val );
1157 #ifdef ENABLE_REWRITE
1159 suffix_massage_regexize( const char *s )
1165 if ( s[ 0 ] == '\0' ) {
1166 return ch_strdup( "^(.+)$" );
1170 ( r = strchr( p, ',' ) ) != NULL;
1174 res = ch_calloc( sizeof( char ),
1176 + STRLENOF( "((.+),)?" )
1177 + STRLENOF( "[ ]?" ) * i
1178 + STRLENOF( "$" ) + 1 );
1180 ptr = lutil_strcopy( res, "((.+),)?" );
1182 ( r = strchr( p, ',' ) ) != NULL;
1184 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1185 ptr = lutil_strcopy( ptr, "[ ]?" );
1187 if ( r[ 1 ] == ' ' ) {
1191 ptr = lutil_strcopy( ptr, p );
1200 suffix_massage_patternize( const char *s, const char *p )
1207 if ( s[ 0 ] == '\0' ) {
1211 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1212 if ( res == NULL ) {
1216 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1217 if ( s[ 0 ] == '\0' ) {
1221 lutil_strcopy( ptr, p );
1227 suffix_massage_config(
1228 struct rewrite_info *info,
1229 struct berval *pvnc,
1230 struct berval *nvnc,
1231 struct berval *prnc,
1238 rargv[ 0 ] = "rewriteEngine";
1241 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1243 rargv[ 0 ] = "rewriteContext";
1244 rargv[ 1 ] = "default";
1246 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1248 rargv[ 0 ] = "rewriteRule";
1249 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1250 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1253 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1254 ch_free( rargv[ 1 ] );
1255 ch_free( rargv[ 2 ] );
1257 if ( BER_BVISEMPTY( pvnc ) ) {
1258 rargv[ 0 ] = "rewriteRule";
1260 rargv[ 2 ] = prnc->bv_val;
1263 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1266 rargv[ 0 ] = "rewriteContext";
1267 rargv[ 1 ] = "searchEntryDN";
1269 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1271 rargv[ 0 ] = "rewriteRule";
1272 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1273 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1276 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1277 ch_free( rargv[ 1 ] );
1278 ch_free( rargv[ 2 ] );
1280 if ( BER_BVISEMPTY( prnc ) ) {
1281 rargv[ 0 ] = "rewriteRule";
1283 rargv[ 2 ] = pvnc->bv_val;
1286 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1289 /* backward compatibility */
1290 rargv[ 0 ] = "rewriteContext";
1291 rargv[ 1 ] = "searchResult";
1292 rargv[ 2 ] = "alias";
1293 rargv[ 3 ] = "searchEntryDN";
1295 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1297 rargv[ 0 ] = "rewriteContext";
1298 rargv[ 1 ] = "matchedDN";
1299 rargv[ 2 ] = "alias";
1300 rargv[ 3 ] = "searchEntryDN";
1302 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1304 rargv[ 0 ] = "rewriteContext";
1305 rargv[ 1 ] = "searchAttrDN";
1306 rargv[ 2 ] = "alias";
1307 rargv[ 3 ] = "searchEntryDN";
1309 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1311 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1312 * see servers/slapd/overlays/rwm.h for details */
1313 rargv[ 0 ] = "rewriteContext";
1314 rargv[ 1 ] = "referralAttrDN";
1316 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1318 rargv[ 0 ] = "rewriteContext";
1319 rargv[ 1 ] = "referralDN";
1321 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1325 #endif /* ENABLE_REWRITE */