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"
37 static struct metatarget *
40 struct metatarget *lt;
41 struct ldapmapping *mapping;
43 lt = ch_calloc( sizeof( struct metatarget ), 1 );
48 lt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
49 if ( lt->mt_rwmap.rwm_rw == NULL ) {
58 * the filter rewrite as a string must be disabled
59 * by default; it can be re-enabled by adding rules;
60 * this creates an empty rewriteContext
62 rargv[ 0 ] = "rewriteContext";
63 rargv[ 1 ] = "searchFilter";
65 rewrite_parse( lt->mt_rwmap.rwm_rw, "<suffix massage>",
68 rargv[ 0 ] = "rewriteContext";
69 rargv[ 1 ] = "default";
71 rewrite_parse( lt->mt_rwmap.rwm_rw, "<suffix massage>",
75 ldap_back_map_init( <->mt_rwmap.rwm_at, &mapping );
89 struct metainfo *li = ( struct metainfo * )be->be_private;
93 "%s: line %d: meta backend info is null!\n",
98 /* URI of server to query */
99 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
100 int i = li->ntargets;
103 #endif /* uncomment if uri MUST be a branch of suffix */
104 LDAPURLDesc *ludp, *tmpludp;
110 "%s: line %d: missing address"
111 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
118 li->targets = ch_realloc( li->targets,
119 sizeof( struct metatarget *)*li->ntargets );
120 if ( li->targets == NULL ) {
122 "%s: line %d: out of memory while storing server name"
123 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
128 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
130 "%s: line %d: unable to init server"
131 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
139 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
141 "%s: line %d: unable to parse URI"
142 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
148 * uri MUST have the <dn> part!
150 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
152 "%s: line %d: missing <naming context> "
153 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
159 * copies and stores uri and suffix
161 dn.bv_val = ludp->lud_dn;
162 dn.bv_len = strlen( ludp->lud_dn );
164 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->mt_psuffix,
165 &li->targets[ i ]->mt_nsuffix, NULL );
166 if( rc != LDAP_SUCCESS ) {
167 fprintf( stderr, "%s: line %d: "
168 "target '%s' DN is invalid\n",
169 fname, lineno, argv[ 1 ] );
173 ludp->lud_dn[ 0 ] = '\0';
175 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
176 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
177 fprintf( stderr, "%s: line %d: "
178 "multiple URIs must have "
186 li->targets[ i ]->mt_uri = ldap_url_list2urls( ludp );
187 ldap_free_urllist( ludp );
188 if ( li->targets[ i ]->mt_uri == NULL) {
189 fprintf( stderr, "%s: line %d: no memory?\n",
195 * uri MUST be a branch of suffix!
197 #if 0 /* too strict a constraint */
198 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
200 "%s: line %d: <naming context> of URI does not refer to current backend"
201 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
207 * uri MUST be a branch of a suffix!
209 if ( select_backend( &li->targets[ i ]->mt_nsuffix, 0, 0 ) == NULL ) {
211 "%s: line %d: <naming context> of URI does not resolve to a backend"
212 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
220 * uri MUST not be used by other URIs!
222 * FIXME: this limitation may be removed,
223 * or worked out, at least, in some manner
225 for ( j = 0; j < i-1; j++ ) {
226 if ( dn_match( &li->targets[ i ]->suffix,
227 &li->targets[ j ]->suffix ) ) {
229 "%s: line %d: naming context \"%s\" already used"
230 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
231 fname, lineno, last+1 );
238 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
239 fname, lineno, li->targets[ i ]->uri,
240 li->targets[ i ]->psuffix.bv_val );
243 /* default target directive */
244 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
245 int i = li->ntargets-1;
250 "%s: line %d: \"default-target\" alone need be"
251 " inside a \"uri\" directive\n",
255 li->defaulttarget = i;
257 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
260 "%s: line %d: \"default-target none\""
261 " should go before uri definitions\n",
264 li->defaulttarget = META_DEFAULT_TARGET_NONE;
266 int n = atoi( argv[ 1 ] );
267 if ( n < 1 || n >= i ) {
269 "%s: line %d: illegal target number %d\n",
273 li->defaulttarget = n-1;
277 /* ttl of dn cache */
278 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
281 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
286 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
287 li->cache.ttl = META_DNCACHE_FOREVER;
288 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
289 li->cache.ttl = META_DNCACHE_DISABLED;
291 li->cache.ttl = atol( argv[ 1 ] );
294 /* network timeout when connecting to ldap servers */
295 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
298 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
302 li->network_timeout = atol(argv[ 1 ]);
304 /* name to use for meta_back_group */
305 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
306 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
308 int i = li->ntargets-1;
313 "%s: line %d: need \"uri\" directive first\n",
320 "%s: line %d: missing name in \"binddn <name>\" line\n",
325 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
326 fprintf( stderr, "%s: line %d: "
327 "\"binddn\" statement is deprecated; "
328 "use \"acl-authcDN\" instead\n",
330 /* FIXME: some day we'll need to throw an error */
333 dn.bv_val = argv[ 1 ];
334 dn.bv_len = strlen( argv[ 1 ] );
335 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->mt_binddn,
336 NULL ) != LDAP_SUCCESS )
338 fprintf( stderr, "%s: line %d: "
339 "bind DN '%s' is invalid\n",
340 fname, lineno, argv[ 1 ] );
344 /* password to use for meta_back_group */
345 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
346 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
348 int i = li->ntargets-1;
352 "%s: line %d: need \"uri\" directive first\n",
359 "%s: line %d: missing password in \"bindpw <password>\" line\n",
364 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
365 fprintf( stderr, "%s: line %d: "
366 "\"bindpw\" statement is deprecated; "
367 "use \"acl-passwd\" instead\n",
369 /* FIXME: some day we'll need to throw an error */
372 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_bindpw );
374 /* save bind creds for referral rebinds? */
375 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
378 "%s: line %d: rebind-as-user takes no arguments\n",
384 /* name to use as pseudo-root dn */
385 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
386 int i = li->ntargets-1;
391 "%s: line %d: need \"uri\" directive first\n",
398 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
403 dn.bv_val = argv[ 1 ];
404 dn.bv_len = strlen( argv[ 1 ] );
405 if ( dnNormalize( 0, NULL, NULL, &dn,
406 &li->targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
408 fprintf( stderr, "%s: line %d: "
409 "pseudoroot DN '%s' is invalid\n",
410 fname, lineno, argv[ 1 ] );
414 /* password to use as pseudo-root */
415 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
416 int i = li->ntargets-1;
420 "%s: line %d: need \"uri\" directive first\n",
427 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
431 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_pseudorootpw );
434 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
436 int i = li->ntargets-1;
437 struct berval dn, nvnc, pvnc, nrnc, prnc;
441 "%s: line %d: need \"uri\" directive first\n",
449 * suffixmassage <suffix> <massaged suffix>
451 * the <suffix> field must be defined as a valid suffix
452 * (or suffixAlias?) for the current database;
453 * the <massaged suffix> shouldn't have already been
454 * defined as a valid suffix or suffixAlias for the
459 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
464 dn.bv_val = argv[ 1 ];
465 dn.bv_len = strlen( argv[ 1 ] );
466 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
467 fprintf( stderr, "%s: line %d: "
468 "suffix '%s' is invalid\n",
469 fname, lineno, argv[ 1 ] );
473 tmp_be = select_backend( &nvnc, 0, 0 );
474 if ( tmp_be != NULL && tmp_be != be ) {
476 "%s: line %d: suffix already in use by another backend in"
477 " \"suffixMassage <suffix> <massaged suffix>\"\n",
484 dn.bv_val = argv[ 2 ];
485 dn.bv_len = strlen( argv[ 2 ] );
486 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
487 fprintf( stderr, "%s: line %d: "
488 "massaged suffix '%s' is invalid\n",
489 fname, lineno, argv[ 2 ] );
496 tmp_be = select_backend( &nrnc, 0, 0 );
497 if ( tmp_be != NULL ) {
499 "%s: line %d: massaged suffix already in use by another backend in"
500 " \"suffixMassage <suffix> <massaged suffix>\"\n",
511 * The suffix massaging is emulated by means of the
512 * rewrite capabilities
513 * FIXME: no extra rewrite capabilities should be added
516 return suffix_massage_config( li->targets[ i ]->mt_rwmap.rwm_rw,
517 &pvnc, &nvnc, &prnc, &nrnc );
519 /* rewrite stuff ... */
520 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
521 int i = li->ntargets-1;
524 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
525 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
527 return rewrite_parse( li->rwinfo, fname, lineno,
531 return rewrite_parse( li->targets[ i ]->mt_rwmap.rwm_rw,
532 fname, lineno, argc, argv );
534 /* objectclass/attribute mapping */
535 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
536 int i = li->ntargets-1;
540 "%s: line %d: need \"uri\" directive first\n",
545 return ldap_back_map_config( &li->targets[ i ]->mt_rwmap.rwm_oc,
546 &li->targets[ i ]->mt_rwmap.rwm_at,
547 fname, lineno, argc, argv );
550 return SLAP_CONF_UNKNOWN;
556 ldap_back_map_config(
557 struct ldapmap *oc_map,
558 struct ldapmap *at_map,
565 struct ldapmapping *mapping;
569 if ( argc < 3 || argc > 4 ) {
571 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
576 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
580 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
584 fprintf( stderr, "%s: line %d: syntax is "
585 "\"map {objectclass | attribute} [<local> | *] "
586 "{<foreign> | *}\"\n",
591 if ( strcmp( argv[2], "*" ) == 0 ) {
592 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
593 map->drop_missing = ( argc < 4 );
598 } else if ( argc < 4 ) {
604 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
607 if ( ( map == at_map )
608 && ( strcasecmp( src, "objectclass" ) == 0
609 || strcasecmp( dst, "objectclass" ) == 0 ) )
612 "%s: line %d: objectclass attribute cannot be mapped\n",
616 mapping = (struct ldapmapping *)ch_calloc( 2,
617 sizeof(struct ldapmapping) );
618 if ( mapping == NULL ) {
620 "%s: line %d: out of memory\n",
624 ber_str2bv( src, 0, 1, &mapping->src );
625 ber_str2bv( dst, 0, 1, &mapping->dst );
626 mapping[1].src = mapping->dst;
627 mapping[1].dst = mapping->src;
633 if ( src[0] != '\0' ) {
634 if ( oc_bvfind( &mapping->src ) == NULL ) {
636 "%s: line %d: warning, source objectClass '%s' "
637 "should be defined in schema\n",
638 fname, lineno, src );
641 * FIXME: this should become an err
647 if ( oc_bvfind( &mapping->dst ) == NULL ) {
649 "%s: line %d: warning, destination objectClass '%s' "
650 "is not defined in schema\n",
651 fname, lineno, dst );
655 const char *text = NULL;
656 AttributeDescription *ad = NULL;
658 if ( src[0] != '\0' ) {
659 rc = slap_bv2ad( &mapping->src, &ad, &text );
660 if ( rc != LDAP_SUCCESS ) {
662 "%s: line %d: warning, source attributeType '%s' "
663 "should be defined in schema\n",
664 fname, lineno, src );
667 * FIXME: this should become an err
675 rc = slap_bv2ad( &mapping->dst, &ad, &text );
676 if ( rc != LDAP_SUCCESS ) {
678 "%s: line %d: warning, destination attributeType '%s' "
679 "is not defined in schema\n",
680 fname, lineno, dst );
684 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
685 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
688 "%s: line %d: duplicate mapping found (ignored)\n",
693 if ( src[0] != '\0' ) {
694 avl_insert( &map->map, (caddr_t)mapping,
695 mapping_cmp, mapping_dup );
697 avl_insert( &map->remap, (caddr_t)&mapping[1],
698 mapping_cmp, mapping_dup );
704 ch_free( mapping->src.bv_val );
705 ch_free( mapping->dst.bv_val );
713 #ifdef ENABLE_REWRITE
715 suffix_massage_regexize( const char *s )
722 ( r = strchr( p, ',' ) ) != NULL;
726 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
728 ptr = lutil_strcopy( res, "(.*)" );
730 ( r = strchr( p, ',' ) ) != NULL;
732 ptr = lutil_strncopy( ptr, p, r - p + 1 );
733 ptr = lutil_strcopy( ptr, "[ ]?" );
735 if ( r[ 1 ] == ' ' ) {
739 lutil_strcopy( ptr, p );
745 suffix_massage_patternize( const char *s )
752 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
758 strcpy( res + sizeof( "%1" ) - 1, s );
764 suffix_massage_config(
765 struct rewrite_info *info,
775 rargv[ 0 ] = "rewriteEngine";
778 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
780 rargv[ 0 ] = "rewriteContext";
781 rargv[ 1 ] = "default";
783 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
785 rargv[ 0 ] = "rewriteRule";
786 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
787 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
790 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
791 ch_free( rargv[ 1 ] );
792 ch_free( rargv[ 2 ] );
794 rargv[ 0 ] = "rewriteContext";
795 rargv[ 1 ] = "searchEntryDN";
797 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
799 rargv[ 0 ] = "rewriteRule";
800 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
801 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
804 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
805 ch_free( rargv[ 1 ] );
806 ch_free( rargv[ 2 ] );
808 /* backward compatibility */
809 rargv[ 0 ] = "rewriteContext";
810 rargv[ 1 ] = "searchResult";
811 rargv[ 2 ] = "alias";
812 rargv[ 3 ] = "searchEntryDN";
814 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
816 rargv[ 0 ] = "rewriteContext";
817 rargv[ 1 ] = "matchedDN";
818 rargv[ 2 ] = "alias";
819 rargv[ 3 ] = "searchEntryDN";
821 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
823 rargv[ 0 ] = "rewriteContext";
824 rargv[ 1 ] = "searchAttrDN";
825 rargv[ 2 ] = "alias";
826 rargv[ 3 ] = "searchEntryDN";
828 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
830 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
831 * see servers/slapd/overlays/rwm.h for details */
832 rargv[ 0 ] = "rewriteContext";
833 rargv[ 1 ] = "referralAttrDN";
835 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
837 rargv[ 0 ] = "rewriteContext";
838 rargv[ 1 ] = "referralDN";
840 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
844 #endif /* ENABLE_REWRITE */