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 ) {
350 Debug( LDAP_DEBUG_ANY,
351 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
356 if ( lutil_atoi( &mi->mi_network_timeout, argv[ 1 ] ) ) {
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 ] );
362 } else if ( mi->mi_network_timeout < 0 ) {
363 Debug( LDAP_DEBUG_ANY,
364 "%s: line %d: ilegal negative timeout in \"network-timeout <seconds>\" line\n",
365 fname, lineno, argv[ 1 ] );
369 /* name to use for meta_back_group */
370 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
371 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
373 int i = mi->mi_ntargets - 1;
377 Debug( LDAP_DEBUG_ANY,
378 "%s: line %d: need \"uri\" directive first\n",
384 Debug( LDAP_DEBUG_ANY,
385 "%s: line %d: missing name in \"binddn <name>\" line\n",
390 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
391 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
392 "\"binddn\" statement is deprecated; "
393 "use \"acl-authcDN\" instead\n",
395 /* FIXME: some day we'll need to throw an error */
398 dn.bv_val = argv[ 1 ];
399 dn.bv_len = strlen( argv[ 1 ] );
400 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
401 NULL ) != LDAP_SUCCESS )
403 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
404 "bind DN '%s' is invalid\n",
405 fname, lineno, argv[ 1 ] );
409 /* password to use for meta_back_group */
410 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
411 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
413 int i = mi->mi_ntargets - 1;
416 Debug( LDAP_DEBUG_ANY,
417 "%s: line %d: need \"uri\" directive first\n",
423 Debug( LDAP_DEBUG_ANY,
424 "%s: line %d: missing password in \"bindpw <password>\" line\n",
429 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
430 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
431 "\"bindpw\" statement is deprecated; "
432 "use \"acl-passwd\" instead\n",
434 /* FIXME: some day we'll need to throw an error */
437 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
439 /* save bind creds for referral rebinds? */
440 } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
442 Debug( LDAP_DEBUG_ANY,
443 "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
449 Debug( LDAP_DEBUG_ANY,
450 "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
452 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
455 switch ( check_true_false( argv[ 1 ] ) ) {
457 mi->mi_flags &= ~LDAP_BACK_F_SAVECRED;
461 mi->mi_flags |= LDAP_BACK_F_SAVECRED;
465 Debug( LDAP_DEBUG_ANY,
466 "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
467 fname, lineno, argv[ 1 ] );
472 } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
473 unsigned *flagsp = mi->mi_ntargets ?
474 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
478 Debug( LDAP_DEBUG_ANY,
479 "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
484 /* this is the default; we add it because the default might change... */
485 switch ( check_true_false( argv[ 1 ] ) ) {
487 *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
491 *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
495 Debug( LDAP_DEBUG_ANY,
496 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
497 fname, lineno, argv[ 1 ] );
501 } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
502 unsigned *flagsp = mi->mi_ntargets ?
503 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
507 Debug( LDAP_DEBUG_ANY,
508 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
514 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
515 *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
518 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
519 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
520 *flagsp |= LDAP_BACK_F_USE_TLS;
522 /* propagate start tls */
523 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
524 *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
527 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
528 *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
529 *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
532 Debug( LDAP_DEBUG_ANY,
533 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
534 fname, lineno, argv[ 1 ] );
538 } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
539 unsigned *flagsp = mi->mi_ntargets ?
540 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
544 Debug( LDAP_DEBUG_ANY,
545 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
550 switch ( check_true_false( argv[ 1 ] ) ) {
552 *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
556 *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
560 if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
561 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
564 Debug( LDAP_DEBUG_ANY,
565 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
566 fname, lineno, argv[ 1 ] );
573 } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
575 Debug( LDAP_DEBUG_ANY,
576 "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
581 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
582 mi->mi_flags &= ~META_BACK_F_ONERR_STOP;
584 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
585 mi->mi_flags |= META_BACK_F_ONERR_STOP;
588 Debug( LDAP_DEBUG_ANY,
589 "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
590 fname, lineno, argv[ 1 ] );
595 } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
597 Debug( LDAP_DEBUG_ANY,
598 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
603 switch ( check_true_false( argv[ 1 ] ) ) {
605 mi->mi_flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
609 mi->mi_flags |= META_BACK_F_DEFER_ROOTDN_BIND;
613 Debug( LDAP_DEBUG_ANY,
614 "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
615 fname, lineno, argv[ 1 ] );
619 } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
621 time_t *tv = mi->mi_ntargets ?
622 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
627 Debug( LDAP_DEBUG_ANY,
628 "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
633 for ( c = 1; c < argc; c++ ) {
637 sep = strchr( argv[ c ], '=' );
639 size_t len = sep - argv[ c ];
641 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
642 t = &tv[ LDAP_BACK_OP_ADD ];
643 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
644 t = &tv[ LDAP_BACK_OP_DELETE ];
645 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
646 t = &tv[ LDAP_BACK_OP_MODIFY ];
647 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
648 t = &tv[ LDAP_BACK_OP_MODRDN ];
650 char buf[ SLAP_TEXT_BUFLEN ];
651 snprintf( buf, sizeof( buf ),
652 "unknown operation \"%s\" for timeout #%d",
654 Debug( LDAP_DEBUG_ANY,
655 "%s: line %d: %s.\n",
656 fname, lineno, buf );
665 if ( lutil_parse_time( sep, &val ) != 0 ) {
666 Debug( LDAP_DEBUG_ANY,
667 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
668 fname, lineno, sep );
678 for ( i = 0; i < LDAP_BACK_OP_LAST; i++ ) {
679 tv[ i ] = (time_t)val;
684 /* name to use as pseudo-root dn */
685 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
686 int i = mi->mi_ntargets - 1;
690 Debug( LDAP_DEBUG_ANY,
691 "%s: line %d: need \"uri\" directive first\n",
697 Debug( LDAP_DEBUG_ANY,
698 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
703 dn.bv_val = argv[ 1 ];
704 dn.bv_len = strlen( argv[ 1 ] );
705 if ( dnNormalize( 0, NULL, NULL, &dn,
706 &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
708 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
709 "pseudoroot DN '%s' is invalid\n",
710 fname, lineno, argv[ 1 ] );
714 /* password to use as pseudo-root */
715 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
716 int i = mi->mi_ntargets - 1;
719 Debug( LDAP_DEBUG_ANY,
720 "%s: line %d: need \"uri\" directive first\n",
726 Debug( LDAP_DEBUG_ANY,
727 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
731 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
734 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
736 int i = mi->mi_ntargets - 1, rc;
737 struct berval dn, nvnc, pvnc, nrnc, prnc;
740 Debug( LDAP_DEBUG_ANY,
741 "%s: line %d: need \"uri\" directive first\n",
749 * suffixmassage <suffix> <massaged suffix>
751 * the <suffix> field must be defined as a valid suffix
752 * (or suffixAlias?) for the current database;
753 * the <massaged suffix> shouldn't have already been
754 * defined as a valid suffix or suffixAlias for the
758 Debug( LDAP_DEBUG_ANY,
759 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
764 ber_str2bv( argv[ 1 ], 0, 0, &dn );
765 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
766 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
767 "suffix '%s' is invalid\n",
768 fname, lineno, argv[ 1 ] );
772 tmp_be = select_backend( &nvnc, 0, 0 );
773 if ( tmp_be != NULL && tmp_be != be ) {
774 Debug( LDAP_DEBUG_ANY,
775 "%s: line %d: suffix already in use by another backend in"
776 " \"suffixMassage <suffix> <massaged suffix>\"\n",
783 ber_str2bv( argv[ 2 ], 0, 0, &dn );
784 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
785 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
786 "massaged suffix '%s' is invalid\n",
787 fname, lineno, argv[ 2 ] );
794 tmp_be = select_backend( &nrnc, 0, 0 );
795 if ( tmp_be != NULL ) {
796 Debug( LDAP_DEBUG_ANY,
797 "%s: line %d: massaged suffix already in use by another backend in"
798 " \"suffixMassage <suffix> <massaged suffix>\"\n",
809 * The suffix massaging is emulated by means of the
810 * rewrite capabilities
811 * FIXME: no extra rewrite capabilities should be added
814 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
815 &pvnc, &nvnc, &prnc, &nrnc );
824 /* rewrite stuff ... */
825 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
826 int i = mi->mi_ntargets - 1;
829 Debug( LDAP_DEBUG_ANY, "%s: line %d: \"rewrite\" "
830 "statement outside target definition.\n",
835 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
836 fname, lineno, argc, argv );
838 /* objectclass/attribute mapping */
839 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
840 int i = mi->mi_ntargets - 1;
843 Debug( LDAP_DEBUG_ANY,
844 "%s: line %d: need \"uri\" directive first\n",
849 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
850 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
851 fname, lineno, argc, argv );
853 } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
854 int i = mi->mi_ntargets - 1;
855 int nretries = META_RETRY_UNDEFINED;
858 Debug( LDAP_DEBUG_ANY,
859 "%s: line %d: need value in \"nretries <value>\"\n",
864 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
865 nretries = META_RETRY_FOREVER;
867 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
868 nretries = META_RETRY_NEVER;
871 if ( lutil_atoi( &nretries, argv[ 1 ] ) != 0 ) {
872 Debug( LDAP_DEBUG_ANY,
873 "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
874 fname, lineno, argv[ 1 ] );
880 mi->mi_nretries = nretries;
883 mi->mi_targets[ i ].mt_nretries = nretries;
888 return SLAP_CONF_UNKNOWN;
894 ldap_back_map_config(
895 struct ldapmap *oc_map,
896 struct ldapmap *at_map,
903 struct ldapmapping *mapping;
907 if ( argc < 3 || argc > 4 ) {
908 Debug( LDAP_DEBUG_ANY,
909 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
914 if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
918 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
922 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is "
923 "\"map {objectclass | attribute} [<local> | *] "
924 "{<foreign> | *}\"\n",
929 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
930 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
931 map->drop_missing = ( argc < 4 );
934 src = dst = argv[ 3 ];
936 } else if ( argc < 4 ) {
942 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
945 if ( ( map == at_map )
946 && ( strcasecmp( src, "objectclass" ) == 0
947 || strcasecmp( dst, "objectclass" ) == 0 ) )
949 Debug( LDAP_DEBUG_ANY,
950 "%s: line %d: objectclass attribute cannot be mapped\n",
954 mapping = (struct ldapmapping *)ch_calloc( 2,
955 sizeof(struct ldapmapping) );
956 if ( mapping == NULL ) {
957 Debug( LDAP_DEBUG_ANY,
958 "%s: line %d: out of memory\n",
962 ber_str2bv( src, 0, 1, &mapping[ 0 ].src );
963 ber_str2bv( dst, 0, 1, &mapping[ 0 ].dst );
964 mapping[ 1 ].src = mapping[ 0 ].dst;
965 mapping[ 1 ].dst = mapping[ 0 ].src;
971 if ( src[ 0 ] != '\0' ) {
972 if ( oc_bvfind( &mapping[ 0 ].src ) == NULL ) {
973 Debug( LDAP_DEBUG_ANY,
974 "%s: line %d: warning, source objectClass '%s' "
975 "should be defined in schema\n",
976 fname, lineno, src );
979 * FIXME: this should become an err
985 if ( oc_bvfind( &mapping[ 0 ].dst ) == NULL ) {
986 Debug( LDAP_DEBUG_ANY,
987 "%s: line %d: warning, destination objectClass '%s' "
988 "is not defined in schema\n",
989 fname, lineno, dst );
993 const char *text = NULL;
994 AttributeDescription *ad = NULL;
996 if ( src[ 0 ] != '\0' ) {
997 rc = slap_bv2ad( &mapping[ 0 ].src, &ad, &text );
998 if ( rc != LDAP_SUCCESS ) {
999 Debug( LDAP_DEBUG_ANY,
1000 "%s: line %d: warning, source attributeType '%s' "
1001 "should be defined in schema\n",
1002 fname, lineno, src );
1005 * FIXME: this should become an err
1008 * we create a fake "proxied" ad
1012 rc = slap_bv2undef_ad( &mapping[ 0 ].src,
1013 &ad, &text, SLAP_AD_PROXIED );
1014 if ( rc != LDAP_SUCCESS ) {
1015 char buf[ SLAP_TEXT_BUFLEN ];
1017 snprintf( buf, sizeof( buf ),
1018 "source attributeType \"%s\": %d (%s)",
1019 src, rc, text ? text : "" );
1020 Debug( LDAP_DEBUG_ANY,
1021 "%s: line %d: %s\n",
1022 fname, lineno, buf );
1030 rc = slap_bv2ad( &mapping[ 0 ].dst, &ad, &text );
1031 if ( rc != LDAP_SUCCESS ) {
1032 Debug( LDAP_DEBUG_ANY,
1033 "%s: line %d: warning, destination attributeType '%s' "
1034 "is not defined in schema\n",
1035 fname, lineno, dst );
1038 * we create a fake "proxied" ad
1042 rc = slap_bv2undef_ad( &mapping[ 0 ].dst,
1043 &ad, &text, SLAP_AD_PROXIED );
1044 if ( rc != LDAP_SUCCESS ) {
1045 char buf[ SLAP_TEXT_BUFLEN ];
1047 snprintf( buf, sizeof( buf ),
1048 "source attributeType \"%s\": %d (%s)\n",
1049 dst, rc, text ? text : "" );
1050 Debug( LDAP_DEBUG_ANY,
1051 "%s: line %d: %s\n",
1052 fname, lineno, buf );
1058 if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)&mapping[ 0 ], mapping_cmp ) != NULL)
1059 || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1061 Debug( LDAP_DEBUG_ANY,
1062 "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1067 if ( src[ 0 ] != '\0' ) {
1068 avl_insert( &map->map, (caddr_t)&mapping[ 0 ],
1069 mapping_cmp, mapping_dup );
1071 avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1072 mapping_cmp, mapping_dup );
1078 ch_free( mapping[ 0 ].src.bv_val );
1079 ch_free( mapping[ 0 ].dst.bv_val );
1087 #ifdef ENABLE_REWRITE
1089 suffix_massage_regexize( const char *s )
1095 if ( s[ 0 ] == '\0' ) {
1096 return ch_strdup( "^(.+)$" );
1100 ( r = strchr( p, ',' ) ) != NULL;
1104 res = ch_calloc( sizeof( char ),
1106 + STRLENOF( "((.+),)?" )
1107 + STRLENOF( "[ ]?" ) * i
1108 + STRLENOF( "$" ) + 1 );
1110 ptr = lutil_strcopy( res, "((.+),)?" );
1112 ( r = strchr( p, ',' ) ) != NULL;
1114 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1115 ptr = lutil_strcopy( ptr, "[ ]?" );
1117 if ( r[ 1 ] == ' ' ) {
1121 ptr = lutil_strcopy( ptr, p );
1130 suffix_massage_patternize( const char *s, const char *p )
1137 if ( s[ 0 ] == '\0' ) {
1141 res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1142 if ( res == NULL ) {
1146 ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1147 if ( s[ 0 ] == '\0' ) {
1151 lutil_strcopy( ptr, p );
1157 suffix_massage_config(
1158 struct rewrite_info *info,
1159 struct berval *pvnc,
1160 struct berval *nvnc,
1161 struct berval *prnc,
1168 rargv[ 0 ] = "rewriteEngine";
1171 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1173 rargv[ 0 ] = "rewriteContext";
1174 rargv[ 1 ] = "default";
1176 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1178 rargv[ 0 ] = "rewriteRule";
1179 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1180 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1183 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1184 ch_free( rargv[ 1 ] );
1185 ch_free( rargv[ 2 ] );
1187 if ( BER_BVISEMPTY( pvnc ) ) {
1188 rargv[ 0 ] = "rewriteRule";
1190 rargv[ 2 ] = prnc->bv_val;
1193 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1196 rargv[ 0 ] = "rewriteContext";
1197 rargv[ 1 ] = "searchEntryDN";
1199 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1201 rargv[ 0 ] = "rewriteRule";
1202 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1203 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1206 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1207 ch_free( rargv[ 1 ] );
1208 ch_free( rargv[ 2 ] );
1210 if ( BER_BVISEMPTY( prnc ) ) {
1211 rargv[ 0 ] = "rewriteRule";
1213 rargv[ 2 ] = pvnc->bv_val;
1216 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1219 /* backward compatibility */
1220 rargv[ 0 ] = "rewriteContext";
1221 rargv[ 1 ] = "searchResult";
1222 rargv[ 2 ] = "alias";
1223 rargv[ 3 ] = "searchEntryDN";
1225 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1227 rargv[ 0 ] = "rewriteContext";
1228 rargv[ 1 ] = "matchedDN";
1229 rargv[ 2 ] = "alias";
1230 rargv[ 3 ] = "searchEntryDN";
1232 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1234 rargv[ 0 ] = "rewriteContext";
1235 rargv[ 1 ] = "searchAttrDN";
1236 rargv[ 2 ] = "alias";
1237 rargv[ 3 ] = "searchEntryDN";
1239 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1241 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1242 * see servers/slapd/overlays/rwm.h for details */
1243 rargv[ 0 ] = "rewriteContext";
1244 rargv[ 1 ] = "referralAttrDN";
1246 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1248 rargv[ 0 ] = "rewriteContext";
1249 rargv[ 1 ] = "referralDN";
1251 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1255 #endif /* ENABLE_REWRITE */