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 #ifdef LDAP_SCOPE_SUBORDINATE
226 case LDAP_SCOPE_SUBORDINATE:
227 #endif /* 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 ) {
352 Debug( LDAP_DEBUG_ANY,
353 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
358 if ( lutil_parse_time( argv[ 1 ], &t ) ) {
359 Debug( LDAP_DEBUG_ANY,
360 "%s: line %d: unable to parse timeout \"%s\" in \"network-timeout <seconds>\" line\n",
361 fname, lineno, argv[ 1 ] );
366 mi->mi_network_timeout = (int)t;
368 /* name to use for meta_back_group */
369 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
370 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
372 int i = mi->mi_ntargets - 1;
376 Debug( LDAP_DEBUG_ANY,
377 "%s: line %d: need \"uri\" directive first\n",
383 Debug( LDAP_DEBUG_ANY,
384 "%s: line %d: missing name in \"binddn <name>\" line\n",
389 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
390 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
391 "\"binddn\" statement is deprecated; "
392 "use \"acl-authcDN\" instead\n",
394 /* FIXME: some day we'll need to throw an error */
397 dn.bv_val = argv[ 1 ];
398 dn.bv_len = strlen( argv[ 1 ] );
399 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
400 NULL ) != LDAP_SUCCESS )
402 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
403 "bind DN '%s' is invalid\n",
404 fname, lineno, argv[ 1 ] );
408 /* password to use for meta_back_group */
409 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
410 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
412 int i = mi->mi_ntargets - 1;
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: need \"uri\" directive first\n",
422 Debug( LDAP_DEBUG_ANY,
423 "%s: line %d: missing password in \"bindpw <password>\" line\n",
428 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
429 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
430 "\"bindpw\" statement is deprecated; "
431 "use \"acl-passwd\" instead\n",
433 /* FIXME: some day we'll need to throw an error */
436 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
438 /* save bind creds for referral rebinds? */
439 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
441 Debug( LDAP_DEBUG_ANY,
442 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
448 Debug( LDAP_DEBUG_ANY,
449 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
451 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
454 switch ( check_true_false( argv[ 1 ] ) ) {
456 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
460 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
464 Debug( LDAP_DEBUG_ANY,
465 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
466 fname, lineno, argv[ 1 ] );
471 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
472 unsigned *flagsp = mi->mi_ntargets ?
473 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
477 Debug( LDAP_DEBUG_ANY,
478 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
483 /* this is the default; we add it because the default might change... */
484 switch ( check_true_false( argv[ 1 ] ) ) {
486 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
490 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
494 Debug( LDAP_DEBUG_ANY,
495 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
496 fname, lineno, argv[ 1 ] );
500 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
501 unsigned *flagsp = mi->mi_ntargets ?
502 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
506 Debug( LDAP_DEBUG_ANY,
507 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
513 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
514 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
517 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
518 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
519 *flagsp |= LDAP_BACK_F_USE_TLS;
521 /* propagate start tls */
522 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
523 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
526 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
527 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
528 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
531 Debug( LDAP_DEBUG_ANY,
532 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
533 fname, lineno, argv[ 1 ] );
537 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 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: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
549 switch ( check_true_false( argv[ 1 ] ) ) {
551 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
555 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
559 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
560 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
563 Debug( LDAP_DEBUG_ANY,
564 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
565 fname, lineno, argv[ 1 ] );
572 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
574 Debug( LDAP_DEBUG_ANY,
575 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
580 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
581 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
583 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
584 mi->mi_flags |= META_BACK_F_ONERR_STOP;
587 Debug( LDAP_DEBUG_ANY,
588 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
589 fname, lineno, argv[ 1 ] );
594 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
596 Debug( LDAP_DEBUG_ANY,
597 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
602 switch ( check_true_false( argv[ 1 ] ) ) {
604 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
608 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
612 Debug( LDAP_DEBUG_ANY,
613 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
614 fname, lineno, argv[ 1 ] );
618 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
620 time_t *tv = mi->mi_ntargets ?
621 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
626 Debug( LDAP_DEBUG_ANY,
627 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
632 for ( c = 1; c < argc; c++ ) {
636 sep = strchr( argv[ c ], '=' );
638 size_t len = sep - argv[ c ];
640 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
641 t = &tv[ LDAP_BACK_OP_ADD ];
642 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
643 t = &tv[ LDAP_BACK_OP_DELETE ];
644 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
645 t = &tv[ LDAP_BACK_OP_MODIFY ];
646 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
647 t = &tv[ LDAP_BACK_OP_MODRDN ];
649 char buf[ SLAP_TEXT_BUFLEN ];
650 snprintf( buf, sizeof( buf ),
651 "unknown operation \"%s\" for timeout #%d",
653 Debug( LDAP_DEBUG_ANY,
654 "%s: line %d: %s.\n",
655 fname, lineno, buf );
664 if ( lutil_parse_time( sep, &val ) != 0 ) {
665 Debug( LDAP_DEBUG_ANY,
666 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
667 fname, lineno, sep );
677 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
678 tv[ i ] = (time_t)val;
683 /* name to use as pseudo-root dn */
684 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
685 int i = mi->mi_ntargets - 1;
689 Debug( LDAP_DEBUG_ANY,
690 "%s: line %d: need \"uri\" directive first\n",
696 Debug( LDAP_DEBUG_ANY,
697 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
702 dn.bv_val = argv[ 1 ];
703 dn.bv_len = strlen( argv[ 1 ] );
704 if ( dnNormalize( 0, NULL, NULL, &dn,
705 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
707 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
708 "pseudoroot DN '%s' is invalid\n",
709 fname, lineno, argv[ 1 ] );
713 /* password to use as pseudo-root */
714 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
715 int i = mi->mi_ntargets - 1;
718 Debug( LDAP_DEBUG_ANY,
719 "%s: line %d: need \"uri\" directive first\n",
725 Debug( LDAP_DEBUG_ANY,
726 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
730 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
733 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
735 int i = mi->mi_ntargets - 1, rc;
736 struct berval dn, nvnc, pvnc, nrnc, prnc;
739 Debug( LDAP_DEBUG_ANY,
740 "%s: line %d: need \"uri\" directive first\n",
748 * suffixmassage <suffix> <massaged suffix>
750 * the <suffix> field must be defined as a valid suffix
751 * (or suffixAlias?) for the current database;
752 * the <massaged suffix> shouldn't have already been
753 * defined as a valid suffix or suffixAlias for the
757 Debug( LDAP_DEBUG_ANY,
758 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
763 ber_str2bv( argv[ 1 ], 0, 0, &dn );
764 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
765 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
766 "suffix '%s' is invalid\n",
767 fname, lineno, argv[ 1 ] );
771 tmp_be = select_backend( &nvnc, 0, 0 );
772 if ( tmp_be != NULL && tmp_be != be ) {
773 Debug( LDAP_DEBUG_ANY,
774 "%s: line %d: suffix already in use by another backend in"
775 " \"suffixMassage <suffix> <massaged suffix>\"\n",
782 ber_str2bv( argv[ 2 ], 0, 0, &dn );
783 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
784 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
785 "massaged suffix '%s' is invalid\n",
786 fname, lineno, argv[ 2 ] );
793 tmp_be = select_backend( &nrnc, 0, 0 );
794 if ( tmp_be != NULL ) {
795 Debug( LDAP_DEBUG_ANY,
796 "%s: line %d: massaged suffix already in use by another backend in"
797 " \"suffixMassage <suffix> <massaged suffix>\"\n",
808 * The suffix massaging is emulated by means of the
809 * rewrite capabilities
810 * FIXME: no extra rewrite capabilities should be added
813 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
814 &pvnc, &nvnc, &prnc, &nrnc );
823 /* rewrite stuff ... */
824 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
825 int i = mi->mi_ntargets - 1;
828 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
829 "statement outside target definition.\n",
834 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
835 fname, lineno, argc, argv );
837 /* objectclass/attribute mapping */
838 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
839 int i = mi->mi_ntargets - 1;
842 Debug( LDAP_DEBUG_ANY,
843 "%s: line %d: need \"uri\" directive first\n",
848 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
849 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
850 fname, lineno, argc, argv );
852 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
853 int i = mi->mi_ntargets - 1;
854 int nretries = META_RETRY_UNDEFINED;
857 Debug( LDAP_DEBUG_ANY,
858 "%s: line %d: need value in \"nretries <value>\"\n",
863 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
864 nretries = META_RETRY_FOREVER;
866 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
867 nretries = META_RETRY_NEVER;
870 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
871 Debug( LDAP_DEBUG_ANY,
872 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
873 fname, lineno, argv[ 1 ] );
879 mi->mi_nretries = nretries;
882 mi->mi_targets[ i ].mt_nretries = nretries;
887 return SLAP_CONF_UNKNOWN;
893 ldap_back_map_config(
894 struct ldapmap *oc_map,
895 struct ldapmap *at_map,
902 struct ldapmapping *mapping;
906 if ( argc < 3 || argc > 4 ) {
907 Debug( LDAP_DEBUG_ANY,
908 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
913 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
917 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
921 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
922 "\"map {objectclass | attribute} [<local> | *] "
923 "{<foreign> | *}\"\n",
928 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
929 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
930 map->drop_missing = ( argc < 4 );
933 src = dst = argv[ 3 ];
935 } else if ( argc < 4 ) {
941 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
944 if ( ( map == at_map )
945 && ( strcasecmp( src, "objectclass" ) == 0
946 || strcasecmp( dst, "objectclass" ) == 0 ) )
948 Debug( LDAP_DEBUG_ANY,
949 "%s: line %d: objectclass attribute cannot be mapped\n",
953 mapping = (struct ldapmapping *)ch_calloc( 2,
954 sizeof(struct ldapmapping) );
955 if ( mapping == NULL ) {
956 Debug( LDAP_DEBUG_ANY,
957 "%s: line %d: out of memory\n",
961 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
962 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
963 mapping[ 1 ].src = mapping[ 0 ].dst;
964 mapping[ 1 ].dst = mapping[ 0 ].src;
970 if ( src[ 0 ] != '\0' ) {
971 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
972 Debug( LDAP_DEBUG_ANY,
973 "%s: line %d: warning, source objectClass '%s' "
974 "should be defined in schema\n",
975 fname, lineno, src );
978 * FIXME: this should become an err
984 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
985 Debug( LDAP_DEBUG_ANY,
986 "%s: line %d: warning, destination objectClass '%s' "
987 "is not defined in schema\n",
988 fname, lineno, dst );
992 const char *text = NULL;
993 AttributeDescription *ad = NULL;
995 if ( src[ 0 ] != '\0' ) {
996 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
997 if ( rc != LDAP_SUCCESS ) {
998 Debug( LDAP_DEBUG_ANY,
999 "%s: line %d: warning, source attributeType '%s' "
1000 "should be defined in schema\n",
1001 fname, lineno, src );
1004 * FIXME: this should become an err
1007 * we create a fake "proxied" ad
1011 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1012 &ad, &text, SLAP_AD_PROXIED );
1013 if ( rc != LDAP_SUCCESS ) {
1014 char buf[ SLAP_TEXT_BUFLEN ];
1016 snprintf( buf, sizeof( buf ),
1017 "source attributeType \"%s\": %d (%s)",
1018 src, rc, text ? text : "" );
1019 Debug( LDAP_DEBUG_ANY,
1020 "%s: line %d: %s\n",
1021 fname, lineno, buf );
1029 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1030 if ( rc != LDAP_SUCCESS ) {
1031 Debug( LDAP_DEBUG_ANY,
1032 "%s: line %d: warning, destination attributeType '%s' "
1033 "is not defined in schema\n",
1034 fname, lineno, dst );
1037 * we create a fake "proxied" ad
1041 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1042 &ad, &text, SLAP_AD_PROXIED );
1043 if ( rc != LDAP_SUCCESS ) {
1044 char buf[ SLAP_TEXT_BUFLEN ];
1046 snprintf( buf, sizeof( buf ),
1047 "source attributeType \"%s\": %d (%s)\n",
1048 dst, rc, text ? text : "" );
1049 Debug( LDAP_DEBUG_ANY,
1050 "%s: line %d: %s\n",
1051 fname, lineno, buf );
1057 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1058 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1060 Debug( LDAP_DEBUG_ANY,
1061 "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1066 if ( src[ 0 ] != '\0' ) {
1067 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1068 mapping_cmp, mapping_dup );
1070 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1071 mapping_cmp, mapping_dup );
1077 ch_free( mapping[ 0 ].src.bv_val );
1078 ch_free( mapping[ 0 ].dst.bv_val );
1086 #ifdef ENABLE_REWRITE
1088 suffix_massage_regexize( const char *s )
1094 if ( s[ 0 ] == '\0' ) {
1095 return ch_strdup( "^(.+)$" );
1099 ( r = strchr( p, ',' ) ) != NULL;
1103 res = ch_calloc( sizeof( char ),
1105 + STRLENOF( "((.+),)?" )
1106 + STRLENOF( "[ ]?" ) * i
1107 + STRLENOF( "$" ) + 1 );
1109 ptr = lutil_strcopy( res, "((.+),)?" );
1111 ( r = strchr( p, ',' ) ) != NULL;
1113 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1114 ptr = lutil_strcopy( ptr, "[ ]?" );
1116 if ( r[ 1 ] == ' ' ) {
1120 ptr = lutil_strcopy( ptr, p );
1129 suffix_massage_patternize( const char *s, const char *p )
1136 if ( s[ 0 ] == '\0' ) {
1140 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1141 if ( res == NULL ) {
1145 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1146 if ( s[ 0 ] == '\0' ) {
1150 lutil_strcopy( ptr, p );
1156 suffix_massage_config(
1157 struct rewrite_info *info,
1158 struct berval *pvnc,
1159 struct berval *nvnc,
1160 struct berval *prnc,
1167 rargv[ 0 ] = "rewriteEngine";
1170 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1172 rargv[ 0 ] = "rewriteContext";
1173 rargv[ 1 ] = "default";
1175 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1177 rargv[ 0 ] = "rewriteRule";
1178 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1179 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1182 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1183 ch_free( rargv[ 1 ] );
1184 ch_free( rargv[ 2 ] );
1186 if ( BER_BVISEMPTY( pvnc ) ) {
1187 rargv[ 0 ] = "rewriteRule";
1189 rargv[ 2 ] = prnc->bv_val;
1192 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1195 rargv[ 0 ] = "rewriteContext";
1196 rargv[ 1 ] = "searchEntryDN";
1198 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1200 rargv[ 0 ] = "rewriteRule";
1201 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1202 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1205 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1206 ch_free( rargv[ 1 ] );
1207 ch_free( rargv[ 2 ] );
1209 if ( BER_BVISEMPTY( prnc ) ) {
1210 rargv[ 0 ] = "rewriteRule";
1212 rargv[ 2 ] = pvnc->bv_val;
1215 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1218 /* backward compatibility */
1219 rargv[ 0 ] = "rewriteContext";
1220 rargv[ 1 ] = "searchResult";
1221 rargv[ 2 ] = "alias";
1222 rargv[ 3 ] = "searchEntryDN";
1224 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1226 rargv[ 0 ] = "rewriteContext";
1227 rargv[ 1 ] = "matchedDN";
1228 rargv[ 2 ] = "alias";
1229 rargv[ 3 ] = "searchEntryDN";
1231 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1233 rargv[ 0 ] = "rewriteContext";
1234 rargv[ 1 ] = "searchAttrDN";
1235 rargv[ 2 ] = "alias";
1236 rargv[ 3 ] = "searchEntryDN";
1238 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1240 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1241 * see servers/slapd/overlays/rwm.h for details */
1242 rargv[ 0 ] = "rewriteContext";
1243 rargv[ 1 ] = "referralAttrDN";
1245 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1247 rargv[ 0 ] = "rewriteContext";
1248 rargv[ 1 ] = "referralDN";
1250 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1254 #endif /* ENABLE_REWRITE */